本文整理汇总了C++中SourceMgr::setIncludeDirs方法的典型用法代码示例。如果您正苦于以下问题:C++ SourceMgr::setIncludeDirs方法的具体用法?C++ SourceMgr::setIncludeDirs怎么用?C++ SourceMgr::setIncludeDirs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SourceMgr
的用法示例。
在下文中一共展示了SourceMgr::setIncludeDirs方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ParseFile
static bool ParseFile(const std::string &Filename,
const std::vector<std::string> &IncludeDirs) {
llvm::OwningPtr<llvm::MemoryBuffer> MB;
if (llvm::error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(Filename.c_str(),
MB)) {
llvm::errs() << "Could not open input file '" << Filename << "': "
<< ec.message() <<"\n";
return true;
}
// Record the location of the include directory so that the lexer can find it
// later.
SourceMgr SrcMgr;
SrcMgr.setIncludeDirs(IncludeDirs);
// Tell SrcMgr about this buffer, which is what Parser will pick up.
SrcMgr.AddNewSourceBuffer(MB.take(), SMLoc());
LangOptions Opts;
Opts.ReturnComments = ReturnComments;
TextDiagnosticPrinter TDP(SrcMgr);
Diagnostic Diag(&SrcMgr, &TDP, false);
#if 0
PrintAction PA(Diag);
#endif
ASTContext Context(SrcMgr);
Sema SA(Context, Diag);
Parser P(SrcMgr, Opts, Diag, SA);
return P.ParseProgramUnits();
}
示例2: ExecuteAssembler
static bool ExecuteAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
// Get the target specific parser.
std::string Error;
const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
if (!TheTarget) {
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
return false;
}
std::unique_ptr<MemoryBuffer> BufferPtr;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Error = ec.message();
Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
return false;
}
MemoryBuffer *Buffer = BufferPtr.release();
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(Opts.IncludePaths);
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
assert(MRI && "Unable to create target register info!");
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
assert(MAI && "Unable to create target asm info!");
// Ensure MCAsmInfo initialization occurs before any use, otherwise sections
// may be created with a combination of default and explicit settings.
if (Opts.CompressDebugSections)
MAI->setCompressDebugSections(true);
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
if (!Out)
return false;
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
// FIXME: Assembler behavior can change with -static.
MOFI->InitMCObjectFileInfo(Opts.Triple,
Reloc::Default, CodeModel::Default, Ctx);
if (Opts.SaveTemporaryLabels)
Ctx.setAllowTemporaryLabels(false);
if (Opts.GenDwarfForAssembly)
Ctx.setGenDwarfForAssembly(true);
if (!Opts.DwarfDebugFlags.empty())
Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
if (!Opts.DwarfDebugProducer.empty())
Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
if (!Opts.DebugCompilationDir.empty())
Ctx.setCompilationDir(Opts.DebugCompilationDir);
if (!Opts.MainFileName.empty())
Ctx.setMainFileName(StringRef(Opts.MainFileName));
Ctx.setDwarfVersion(Opts.DwarfVersion);
// Build up the feature string from the target feature list.
std::string FS;
if (!Opts.Features.empty()) {
FS = Opts.Features[0];
for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
FS += "," + Opts.Features[i];
}
std::unique_ptr<MCStreamer> Str;
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
std::unique_ptr<MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
MCInstPrinter *IP =
TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
*STI);
MCCodeEmitter *CE = 0;
MCAsmBackend *MAB = 0;
if (Opts.ShowEncoding) {
CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
}
Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
/*useDwarfDirectory*/ true,
IP, CE, MAB,
Opts.ShowInst));
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
Str.reset(createNullStreamer(Ctx));
} else {
assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
"Invalid file type!");
MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
//.........这里部分代码省略.........
示例3: main
int main(int argc, char **argv) {
// Print a stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal(argv[0]);
PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
// Initialize targets and assembly printers/parsers.
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllDisassemblers();
// Register the target printer for --version.
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
TripleName = Triple::normalize(TripleName);
setDwarfDebugFlags(argc, argv);
setDwarfDebugProducer();
const char *ProgName = argv[0];
const Target *TheTarget = GetTarget(ProgName);
if (!TheTarget)
return 1;
// Now that GetTarget() has (potentially) replaced TripleName, it's safe to
// construct the Triple object.
Triple TheTriple(TripleName);
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
MemoryBuffer::getFileOrSTDIN(InputFilename);
if (std::error_code EC = BufferPtr.getError()) {
errs() << InputFilename << ": " << EC.message() << '\n';
return 1;
}
MemoryBuffer *Buffer = BufferPtr->get();
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
assert(MRI && "Unable to create target register info!");
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
assert(MAI && "Unable to create target asm info!");
MAI->setRelaxELFRelocations(RelaxELFRel);
if (CompressDebugSections != DebugCompressionType::DCT_None) {
if (!zlib::isAvailable()) {
errs() << ProgName
<< ": build tools with zlib to enable -compress-debug-sections";
return 1;
}
MAI->setCompressDebugSections(CompressDebugSections);
}
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
MCObjectFileInfo MOFI;
MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
MOFI.InitMCObjectFileInfo(TheTriple, PIC, CMModel, Ctx);
if (SaveTempLabels)
Ctx.setAllowTemporaryLabels(false);
Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
// Default to 4 for dwarf version.
unsigned DwarfVersion = MCOptions.DwarfVersion ? MCOptions.DwarfVersion : 4;
if (DwarfVersion < 2 || DwarfVersion > 4) {
errs() << ProgName << ": Dwarf version " << DwarfVersion
<< " is not supported." << '\n';
return 1;
}
Ctx.setDwarfVersion(DwarfVersion);
if (!DwarfDebugFlags.empty())
Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
if (!DwarfDebugProducer.empty())
Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
if (!DebugCompilationDir.empty())
Ctx.setCompilationDir(DebugCompilationDir);
else {
// If no compilation dir is set, try to use the current directory.
SmallString<128> CWD;
if (!sys::fs::current_path(CWD))
Ctx.setCompilationDir(CWD);
}
if (!MainFileName.empty())
Ctx.setMainFileName(MainFileName);
// Package up features to be passed to target/subtarget
std::string FeaturesStr;
if (MAttrs.size()) {
//.........这里部分代码省略.........
示例4: AssembleInput
static int AssembleInput(const char *ProgName) {
const Target *TheTarget = GetTarget(ProgName);
if (!TheTarget)
return 1;
std::string Error;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, &Error);
if (Buffer == 0) {
errs() << ProgName << ": ";
if (Error.size())
errs() << Error << "\n";
else
errs() << "input file didn't read correctly.\n";
return 1;
}
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
MCContext Ctx;
formatted_raw_ostream *Out = GetOutputStream();
if (!Out)
return 1;
// FIXME: We shouldn't need to do this (and link in codegen).
OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(TripleName, ""));
if (!TM) {
errs() << ProgName << ": error: could not create target for triple '"
<< TripleName << "'.\n";
return 1;
}
OwningPtr<AsmPrinter> AP;
OwningPtr<MCStreamer> Str;
if (FileType == OFT_AssemblyFile) {
const TargetAsmInfo *TAI = TheTarget->createAsmInfo(TripleName);
assert(TAI && "Unable to create target asm info!");
AP.reset(TheTarget->createAsmPrinter(*Out, *TM, TAI, true));
Str.reset(createAsmStreamer(Ctx, *Out, *TAI, AP.get()));
} else {
assert(FileType == OFT_ObjectFile && "Invalid file type!");
Str.reset(createMachOStreamer(Ctx, *Out));
}
// FIXME: Target hook & command line option for initial section.
Str.get()->SwitchSection(MCSectionMachO::Create("__TEXT","__text",
MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
0, SectionKind::getText(),
Ctx));
AsmParser Parser(SrcMgr, Ctx, *Str.get());
OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(Parser));
if (!TAP) {
errs() << ProgName
<< ": error: this target does not support assembly parsing.\n";
return 1;
}
Parser.setTargetParser(*TAP.get());
int Res = Parser.Run();
if (Out != &fouts())
delete Out;
return Res;
}
示例5: AsLexInput
static int AsLexInput(const char *ProgName) {
std::string ErrorMessage;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
&ErrorMessage);
if (Buffer == 0) {
errs() << ProgName << ": ";
if (ErrorMessage.size())
errs() << ErrorMessage << "\n";
else
errs() << "input file didn't read correctly.\n";
return 1;
}
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
AsmLexer Lexer(SrcMgr);
bool Error = false;
while (Lexer.Lex().isNot(AsmToken::Eof)) {
switch (Lexer.getKind()) {
default:
Lexer.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
Error = true;
break;
case AsmToken::Error:
Error = true; // error already printed.
break;
case AsmToken::Identifier:
outs() << "identifier: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::Register:
outs() << "register: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::String:
outs() << "string: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::Integer:
outs() << "int: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::Amp: outs() << "Amp\n"; break;
case AsmToken::AmpAmp: outs() << "AmpAmp\n"; break;
case AsmToken::Caret: outs() << "Caret\n"; break;
case AsmToken::Colon: outs() << "Colon\n"; break;
case AsmToken::Comma: outs() << "Comma\n"; break;
case AsmToken::Dollar: outs() << "Dollar\n"; break;
case AsmToken::EndOfStatement: outs() << "EndOfStatement\n"; break;
case AsmToken::Eof: outs() << "Eof\n"; break;
case AsmToken::Equal: outs() << "Equal\n"; break;
case AsmToken::EqualEqual: outs() << "EqualEqual\n"; break;
case AsmToken::Exclaim: outs() << "Exclaim\n"; break;
case AsmToken::ExclaimEqual: outs() << "ExclaimEqual\n"; break;
case AsmToken::Greater: outs() << "Greater\n"; break;
case AsmToken::GreaterEqual: outs() << "GreaterEqual\n"; break;
case AsmToken::GreaterGreater: outs() << "GreaterGreater\n"; break;
case AsmToken::LParen: outs() << "LParen\n"; break;
case AsmToken::Less: outs() << "Less\n"; break;
case AsmToken::LessEqual: outs() << "LessEqual\n"; break;
case AsmToken::LessGreater: outs() << "LessGreater\n"; break;
case AsmToken::LessLess: outs() << "LessLess\n"; break;
case AsmToken::Minus: outs() << "Minus\n"; break;
case AsmToken::Percent: outs() << "Percent\n"; break;
case AsmToken::Pipe: outs() << "Pipe\n"; break;
case AsmToken::PipePipe: outs() << "PipePipe\n"; break;
case AsmToken::Plus: outs() << "Plus\n"; break;
case AsmToken::RParen: outs() << "RParen\n"; break;
case AsmToken::Slash: outs() << "Slash\n"; break;
case AsmToken::Star: outs() << "Star\n"; break;
case AsmToken::Tilde: outs() << "Tilde\n"; break;
}
}
return Error;
}
示例6: ExecuteAssembler
static bool ExecuteAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
// Get the target specific parser.
std::string Error;
const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
if (!TheTarget) {
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
return false;
}
OwningPtr<MemoryBuffer> BufferPtr;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
Error = ec.message();
Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
return false;
}
MemoryBuffer *Buffer = BufferPtr.take();
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(Opts.IncludePaths);
OwningPtr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(Opts.Triple));
assert(MAI && "Unable to create target asm info!");
OwningPtr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
assert(MRI && "Unable to create target register info!");
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
if (!Out)
return false;
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
OwningPtr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
MCContext Ctx(*MAI, *MRI, MOFI.get());
// FIXME: Assembler behavior can change with -static.
MOFI->InitMCObjectFileInfo(Opts.Triple,
Reloc::Default, CodeModel::Default, Ctx);
if (Opts.SaveTemporaryLabels)
Ctx.setAllowTemporaryLabels(false);
OwningPtr<MCStreamer> Str;
OwningPtr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
OwningPtr<MCSubtargetInfo>
STI(TheTarget->createMCSubtargetInfo(Opts.Triple, "", ""));
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
MCInstPrinter *IP =
TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI, *MCII, *MRI,
*STI);
MCCodeEmitter *CE = 0;
MCAsmBackend *MAB = 0;
if (Opts.ShowEncoding) {
CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
MAB = TheTarget->createMCAsmBackend(Opts.Triple);
}
Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
/*useLoc*/ true, /*useCFI*/ true,
/*useDwarfDirectory*/ true, IP, CE,
MAB, Opts.ShowInst));
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
Str.reset(createNullStreamer(Ctx));
} else {
assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
"Invalid file type!");
MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, *STI, Ctx);
MCAsmBackend *MAB = TheTarget->createMCAsmBackend(Opts.Triple);
Str.reset(TheTarget->createMCObjectStreamer(Opts.Triple, Ctx, *MAB, *Out,
CE, Opts.RelaxAll,
Opts.NoExecStack));
Str.get()->InitSections();
}
OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, Ctx, *Str.get(),
*MAI));
OwningPtr<MCTargetAsmParser> TAP(TheTarget->createMCAsmParser(*STI, *Parser));
if (!TAP) {
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
return false;
}
Parser->setTargetParser(*TAP.get());
bool Success = !Parser->Run(Opts.NoInitialTextSection);
// Close the output.
delete Out;
// Delete output on errors.
if (!Success && Opts.OutputPath != "-")
sys::Path(Opts.OutputPath).eraseFromDisk();
//.........这里部分代码省略.........
示例7: AssembleOneInput
int AssembleOneInput(const uint8_t *Data, size_t Size) {
const bool ShowInst = false;
const bool AsmVerbose = false;
const bool UseDwarfDirectory = true;
Triple TheTriple(Triple::normalize(TripleName));
SourceMgr SrcMgr;
std::unique_ptr<MemoryBuffer> BufferPtr(new LLVMFuzzerInputBuffer(Data, Size));
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(BufferPtr), SMLoc());
static const std::vector<std::string> NoIncludeDirs;
SrcMgr.setIncludeDirs(NoIncludeDirs);
static std::string ArchName;
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
Error);
if (!TheTarget) {
errs() << "error: this target '" << TheTriple.normalize()
<< "/" << ArchName << "', was not found: '" << Error << "'\n";
abort();
}
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
if (!MRI) {
errs() << "Unable to create target register info!";
abort();
}
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
if (!MAI) {
errs() << "Unable to create target asm info!";
abort();
}
MCObjectFileInfo MOFI;
MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
static const bool UsePIC = false;
MOFI.InitMCObjectFileInfo(TheTriple, UsePIC, Ctx);
const unsigned OutputAsmVariant = 0;
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
MCInstPrinter *IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant,
*MAI, *MCII, *MRI);
if (!IP) {
errs()
<< "error: unable to create instruction printer for target triple '"
<< TheTriple.normalize() << "' with assembly variant "
<< OutputAsmVariant << ".\n";
abort();
}
const char *ProgName = "llvm-mc-fuzzer";
std::unique_ptr<MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
MCCodeEmitter *CE = nullptr;
MCAsmBackend *MAB = nullptr;
MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
std::string OutputString;
raw_string_ostream Out(OutputString);
auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
std::unique_ptr<MCStreamer> Str;
if (FileType == OFT_AssemblyFile) {
Str.reset(TheTarget->createAsmStreamer(
Ctx, std::move(FOut), AsmVerbose,
UseDwarfDirectory, IP, CE, MAB, ShowInst));
} else {
assert(FileType == OFT_ObjectFile && "Invalid file type!");
std::error_code EC;
const std::string OutputFilename = "-";
auto Out =
llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
if (EC) {
errs() << EC.message() << '\n';
abort();
}
// Don't waste memory on names of temp labels.
Ctx.setUseNamesOnTempLabels(false);
std::unique_ptr<buffer_ostream> BOS;
raw_pwrite_stream *OS = &Out->os();
if (!Out->os().supportsSeeking()) {
BOS = make_unique<buffer_ostream>(Out->os());
OS = BOS.get();
}
//.........这里部分代码省略.........
示例8: ExecuteAssembler
static bool ExecuteAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
// Get the target specific parser.
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
if (!TheTarget)
return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
if (std::error_code EC = Buffer.getError()) {
Error = EC.message();
return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
}
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(Opts.IncludePaths);
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
assert(MRI && "Unable to create target register info!");
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
assert(MAI && "Unable to create target asm info!");
// Ensure MCAsmInfo initialization occurs before any use, otherwise sections
// may be created with a combination of default and explicit settings.
MAI->setCompressDebugSections(Opts.CompressDebugSections);
MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations);
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
if (Opts.OutputPath.empty())
Opts.OutputPath = "-";
std::unique_ptr<raw_fd_ostream> FDOS =
getOutputStream(Opts.OutputPath, Diags, IsBinary);
if (!FDOS)
return true;
std::unique_ptr<raw_fd_ostream> DwoOS;
if (!Opts.SplitDwarfFile.empty())
DwoOS = getOutputStream(Opts.SplitDwarfFile, Diags, IsBinary);
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
bool PIC = false;
if (Opts.RelocationModel == "static") {
PIC = false;
} else if (Opts.RelocationModel == "pic") {
PIC = true;
} else {
assert(Opts.RelocationModel == "dynamic-no-pic" &&
"Invalid PIC model!");
PIC = false;
}
MOFI->InitMCObjectFileInfo(Triple(Opts.Triple), PIC, Ctx);
if (Opts.SaveTemporaryLabels)
Ctx.setAllowTemporaryLabels(false);
if (Opts.GenDwarfForAssembly)
Ctx.setGenDwarfForAssembly(true);
if (!Opts.DwarfDebugFlags.empty())
Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
if (!Opts.DwarfDebugProducer.empty())
Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
if (!Opts.DebugCompilationDir.empty())
Ctx.setCompilationDir(Opts.DebugCompilationDir);
if (!Opts.DebugPrefixMap.empty())
for (const auto &KV : Opts.DebugPrefixMap)
Ctx.addDebugPrefixMapEntry(KV.first, KV.second);
if (!Opts.MainFileName.empty())
Ctx.setMainFileName(StringRef(Opts.MainFileName));
Ctx.setDwarfVersion(Opts.DwarfVersion);
// Build up the feature string from the target feature list.
std::string FS;
if (!Opts.Features.empty()) {
FS = Opts.Features[0];
for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
FS += "," + Opts.Features[i];
}
std::unique_ptr<MCStreamer> Str;
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
std::unique_ptr<MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
raw_pwrite_stream *Out = FDOS.get();
std::unique_ptr<buffer_ostream> BOS;
//.........这里部分代码省略.........
示例9: main
int main(int argc, char **argv) {
// Print a stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
// Initialize targets and assembly printers/parsers.
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllDisassemblers();
// Register the target printer for --version.
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
TripleName = Triple::normalize(TripleName);
setDwarfDebugFlags(argc, argv);
setDwarfDebugProducer();
const char *ProgName = argv[0];
const Target *TheTarget = GetTarget(ProgName);
if (!TheTarget)
return 1;
std::unique_ptr<MemoryBuffer> BufferPtr;
if (std::error_code ec =
MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
errs() << ProgName << ": " << ec.message() << '\n';
return 1;
}
MemoryBuffer *Buffer = BufferPtr.release();
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
assert(MRI && "Unable to create target register info!");
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
assert(MAI && "Unable to create target asm info!");
if (CompressDebugSections) {
if (!zlib::isAvailable()) {
errs() << ProgName << ": build tools with zlib to enable -compress-debug-sections";
return 1;
}
MAI->setCompressDebugSections(true);
}
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
MOFI->InitMCObjectFileInfo(TripleName, RelocModel, CMModel, Ctx);
if (SaveTempLabels)
Ctx.setAllowTemporaryLabels(false);
Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
if (DwarfVersion < 2 || DwarfVersion > 4) {
errs() << ProgName << ": Dwarf version " << DwarfVersion
<< " is not supported." << '\n';
return 1;
}
Ctx.setDwarfVersion(DwarfVersion);
if (!DwarfDebugFlags.empty())
Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
if (!DwarfDebugProducer.empty())
Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
if (!DebugCompilationDir.empty())
Ctx.setCompilationDir(DebugCompilationDir);
if (!MainFileName.empty())
Ctx.setMainFileName(MainFileName);
// Package up features to be passed to target/subtarget
std::string FeaturesStr;
if (MAttrs.size()) {
SubtargetFeatures Features;
for (unsigned i = 0; i != MAttrs.size(); ++i)
Features.AddFeature(MAttrs[i]);
FeaturesStr = Features.getString();
}
std::unique_ptr<tool_output_file> Out(GetOutputStream());
if (!Out)
return 1;
formatted_raw_ostream FOS(Out->os());
std::unique_ptr<MCStreamer> Str;
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
std::unique_ptr<MCSubtargetInfo> STI(
//.........这里部分代码省略.........
示例10: main
int main(int argc, char **argv) {
RecordKeeper Records;
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
cl::ParseCommandLineOptions(argc, argv);
try {
// Parse the input file.
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
errs() << "Could not open input file '" << InputFilename << "': "
<< ec.message() <<"\n";
return 1;
}
MemoryBuffer *F = File.take();
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
SrcMgr.AddNewSourceBuffer(F, SMLoc());
// Record the location of the include directory so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
TGParser Parser(SrcMgr, Records);
if (Parser.ParseFile())
return 1;
std::string Error;
tool_output_file Out(OutputFilename.c_str(), Error);
if (!Error.empty()) {
errs() << argv[0] << ": error opening " << OutputFilename
<< ":" << Error << "\n";
return 1;
}
if (!DependFilename.empty()) {
if (OutputFilename == "-") {
errs() << argv[0] << ": the option -d must be used together with -o\n";
return 1;
}
tool_output_file DepOut(DependFilename.c_str(), Error);
if (!Error.empty()) {
errs() << argv[0] << ": error opening " << DependFilename
<< ":" << Error << "\n";
return 1;
}
DepOut.os() << DependFilename << ":";
const std::vector<std::string> &Dependencies = Parser.getDependencies();
for (std::vector<std::string>::const_iterator I = Dependencies.begin(),
E = Dependencies.end();
I != E; ++I) {
DepOut.os() << " " << (*I);
}
DepOut.os() << "\n";
DepOut.keep();
}
switch (Action) {
case PrintRecords:
Out.os() << Records; // No argument, dump all contents
break;
case GenEmitter:
CodeEmitterGen(Records).run(Out.os());
break;
case GenRegisterEnums:
RegisterInfoEmitter(Records).runEnums(Out.os());
break;
case GenRegister:
RegisterInfoEmitter(Records).run(Out.os());
break;
case GenRegisterHeader:
RegisterInfoEmitter(Records).runHeader(Out.os());
break;
case GenInstrEnums:
InstrEnumEmitter(Records).run(Out.os());
break;
case GenInstrs:
InstrInfoEmitter(Records).run(Out.os());
break;
case GenCallingConv:
CallingConvEmitter(Records).run(Out.os());
break;
case GenAsmWriter:
AsmWriterEmitter(Records).run(Out.os());
break;
case GenARMDecoder:
ARMDecoderEmitter(Records).run(Out.os());
break;
case GenAsmMatcher:
AsmMatcherEmitter(Records).run(Out.os());
break;
case GenClangAttrClasses:
ClangAttrClassEmitter(Records).run(Out.os());
break;
case GenClangAttrImpl:
ClangAttrImplEmitter(Records).run(Out.os());
break;
//.........这里部分代码省略.........
示例11: ParseFile
static bool ParseFile(const std::string &Filename,
const std::vector<std::string> &IncludeDirs,
SmallVectorImpl<std::string> &OutputFiles) {
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
MemoryBuffer::getFileOrSTDIN(Filename);
if (std::error_code EC = MBOrErr.getError()) {
llvm::errs() << "Could not open input file '" << Filename << "': "
<< EC.message() <<"\n";
return true;
}
std::unique_ptr<llvm::MemoryBuffer> MB = std::move(MBOrErr.get());
// Record the location of the include directory so that the lexer can find it
// later.
SourceMgr SrcMgr;
SrcMgr.setIncludeDirs(IncludeDirs);
// Tell SrcMgr about this buffer, which is what Parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(MB), llvm::SMLoc());
LangOptions Opts;
Opts.DefaultReal8 = DefaultReal8;
Opts.DefaultDouble8 = DefaultDouble8;
Opts.DefaultInt8 = DefaultInt8;
Opts.ReturnComments = ReturnComments;
if(!FreeForm && !FixedForm) {
llvm::StringRef Ext = llvm::sys::path::extension(Filename);
if(Ext.equals_lower(".f")) {
Opts.FixedForm = 1;
Opts.FreeForm = 0;
}
} else if(FixedForm) {
Opts.FixedForm = 1;
Opts.FreeForm = 0;
}
TextDiagnosticPrinter TDP(SrcMgr);
DiagnosticsEngine Diag(new DiagnosticIDs,&SrcMgr, &TDP, false);
// Chain in -verify checker, if requested.
if(RunVerifier)
Diag.setClient(new VerifyDiagnosticConsumer(Diag));
ASTContext Context(SrcMgr, Opts);
Sema SA(Context, Diag);
Parser P(SrcMgr, Opts, Diag, SA);
Diag.getClient()->BeginSourceFile(Opts, &P.getLexer());
P.ParseProgramUnits();
Diag.getClient()->EndSourceFile();
// Dump
if(PrintAST || DumpAST) {
auto Dumper = CreateASTDumper("");
Dumper->HandleTranslationUnit(Context);
delete Dumper;
}
// Emit
if(!SyntaxOnly && !Diag.hadErrors()) {
flang::TargetOptions TargetOptions;
TargetOptions.Triple = TargetTriple.empty()? llvm::sys::getDefaultTargetTriple() :
TargetTriple;
TargetOptions.CPU = llvm::sys::getHostCPUName();
auto CG = CreateLLVMCodeGen(Diag, Filename == ""? std::string("module") : Filename,
CodeGenOptions(), TargetOptions, llvm::getGlobalContext());
CG->Initialize(Context);
CG->HandleTranslationUnit(Context);
BackendAction BA = Backend_EmitObj;
if(EmitASM) BA = Backend_EmitAssembly;
if(EmitLLVM) BA = Backend_EmitLL;
const llvm::Target *TheTarget = 0;
std::string Err;
TheTarget = llvm::TargetRegistry::lookupTarget(TargetOptions.Triple, Err);
CodeGenOpt::Level TMOptLevel = CodeGenOpt::Default;
switch(OptLevel) {
case 0: TMOptLevel = CodeGenOpt::None; break;
case 3: TMOptLevel = CodeGenOpt::Aggressive; break;
}
llvm::TargetOptions Options;
auto TM = TheTarget->createTargetMachine(TargetOptions.Triple, TargetOptions.CPU, "", Options,
Reloc::Default, CodeModel::Default,
TMOptLevel);
if(!(EmitLLVM && OptLevel == 0)) {
auto TheModule = CG->GetModule();
auto PM = new llvm::legacy::PassManager();
//llvm::legacy::FunctionPassManager *FPM = new llvm::legacy::FunctionPassManager(TheModule);
//FPM->add(new DataLayoutPass());
//PM->add(new llvm::DataLayoutPass());
//TM->addAnalysisPasses(*PM);
PM->add(createPromoteMemoryToRegisterPass());
PassManagerBuilder PMBuilder;
PMBuilder.OptLevel = OptLevel;
PMBuilder.SizeLevel = 0;
//.........这里部分代码省略.........
示例12: ExecuteAssembler
static bool ExecuteAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
// Get the target specific parser.
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
if (!TheTarget)
return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
if (std::error_code EC = Buffer.getError()) {
Error = EC.message();
return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
}
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(Opts.IncludePaths);
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
assert(MRI && "Unable to create target register info!");
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
assert(MAI && "Unable to create target asm info!");
// Ensure MCAsmInfo initialization occurs before any use, otherwise sections
// may be created with a combination of default and explicit settings.
if (Opts.CompressDebugSections)
MAI->setCompressDebugSections(true);
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
std::unique_ptr<formatted_raw_ostream> Out(
GetOutputStream(Opts, Diags, IsBinary));
if (!Out)
return true;
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
// FIXME: Assembler behavior can change with -static.
MOFI->InitMCObjectFileInfo(Opts.Triple,
Reloc::Default, CodeModel::Default, Ctx);
if (Opts.SaveTemporaryLabels)
Ctx.setAllowTemporaryLabels(false);
if (Opts.GenDwarfForAssembly)
Ctx.setGenDwarfForAssembly(true);
if (!Opts.DwarfDebugFlags.empty())
Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
if (!Opts.DwarfDebugProducer.empty())
Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
if (!Opts.DebugCompilationDir.empty())
Ctx.setCompilationDir(Opts.DebugCompilationDir);
if (!Opts.MainFileName.empty())
Ctx.setMainFileName(StringRef(Opts.MainFileName));
Ctx.setDwarfVersion(Opts.DwarfVersion);
// Build up the feature string from the target feature list.
std::string FS;
if (!Opts.Features.empty()) {
FS = Opts.Features[0];
for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
FS += "," + Opts.Features[i];
}
std::unique_ptr<MCStreamer> Str;
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
std::unique_ptr<MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
MCInstPrinter *IP = TheTarget->createMCInstPrinter(
llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI);
MCCodeEmitter *CE = nullptr;
MCAsmBackend *MAB = nullptr;
if (Opts.ShowEncoding) {
CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple, Opts.CPU);
}
Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
/*useDwarfDirectory*/ true,
IP, CE, MAB,
Opts.ShowInst));
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
Str.reset(createNullStreamer(Ctx));
} else {
assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
"Invalid file type!");
MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*MRI, Opts.Triple,
Opts.CPU);
//.........这里部分代码省略.........
示例13: AsLexInput
static int AsLexInput(const char *ProgName) {
std::string ErrorMessage;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
&ErrorMessage);
if (Buffer == 0) {
errs() << ProgName << ": ";
if (ErrorMessage.size())
errs() << ErrorMessage << "\n";
else
errs() << "input file didn't read correctly.\n";
return 1;
}
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
const Target *TheTarget = GetTarget(ProgName);
if (!TheTarget)
return 1;
llvm::OwningPtr<MCAsmInfo> MAI(TheTarget->createAsmInfo(TripleName));
assert(MAI && "Unable to create target asm info!");
AsmLexer Lexer(*MAI);
Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
OwningPtr<tool_output_file> Out(GetOutputStream());
if (!Out)
return 1;
bool Error = false;
while (Lexer.Lex().isNot(AsmToken::Eof)) {
switch (Lexer.getKind()) {
default:
SrcMgr.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
Error = true;
break;
case AsmToken::Error:
Error = true; // error already printed.
break;
case AsmToken::Identifier:
Out->os() << "identifier: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::Integer:
Out->os() << "int: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::Real:
Out->os() << "real: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::Register:
Out->os() << "register: " << Lexer.getTok().getRegVal() << '\n';
break;
case AsmToken::String:
Out->os() << "string: " << Lexer.getTok().getString() << '\n';
break;
case AsmToken::Amp:
Out->os() << "Amp\n";
break;
case AsmToken::AmpAmp:
Out->os() << "AmpAmp\n";
break;
case AsmToken::At:
Out->os() << "At\n";
break;
case AsmToken::Caret:
Out->os() << "Caret\n";
break;
case AsmToken::Colon:
Out->os() << "Colon\n";
break;
case AsmToken::Comma:
Out->os() << "Comma\n";
break;
case AsmToken::Dollar:
Out->os() << "Dollar\n";
break;
case AsmToken::Dot:
Out->os() << "Dot\n";
break;
case AsmToken::EndOfStatement:
Out->os() << "EndOfStatement\n";
break;
case AsmToken::Eof:
Out->os() << "Eof\n";
break;
case AsmToken::Equal:
Out->os() << "Equal\n";
break;
case AsmToken::EqualEqual:
Out->os() << "EqualEqual\n";
break;
case AsmToken::Exclaim:
Out->os() << "Exclaim\n";
//.........这里部分代码省略.........
示例14: ExecuteAssembler
static bool ExecuteAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
// Get the target specific parser.
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
if (!TheTarget)
return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
if (std::error_code EC = Buffer.getError()) {
Error = EC.message();
return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
}
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(Opts.IncludePaths);
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
assert(MRI && "Unable to create target register info!");
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
assert(MAI && "Unable to create target asm info!");
// Ensure MCAsmInfo initialization occurs before any use, otherwise sections
// may be created with a combination of default and explicit settings.
if (Opts.CompressDebugSections)
MAI->setCompressDebugSections(DebugCompressionType::DCT_ZlibGnu);
MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations);
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
std::unique_ptr<raw_fd_ostream> FDOS = getOutputStream(Opts, Diags, IsBinary);
if (!FDOS)
return true;
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
bool PIC = false;
if (Opts.RelocationModel == "static") {
PIC = false;
} else if (Opts.RelocationModel == "pic") {
PIC = true;
} else {
assert(Opts.RelocationModel == "dynamic-no-pic" &&
"Invalid PIC model!");
PIC = false;
}
MOFI->InitMCObjectFileInfo(Triple(Opts.Triple), PIC, CodeModel::Default, Ctx);
if (Opts.SaveTemporaryLabels)
Ctx.setAllowTemporaryLabels(false);
if (Opts.GenDwarfForAssembly)
Ctx.setGenDwarfForAssembly(true);
if (!Opts.DwarfDebugFlags.empty())
Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
if (!Opts.DwarfDebugProducer.empty())
Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
if (!Opts.DebugCompilationDir.empty())
Ctx.setCompilationDir(Opts.DebugCompilationDir);
if (!Opts.MainFileName.empty())
Ctx.setMainFileName(StringRef(Opts.MainFileName));
Ctx.setDwarfVersion(Opts.DwarfVersion);
// Build up the feature string from the target feature list.
std::string FS;
if (!Opts.Features.empty()) {
FS = Opts.Features[0];
for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
FS += "," + Opts.Features[i];
}
std::unique_ptr<MCStreamer> Str;
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
std::unique_ptr<MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
raw_pwrite_stream *Out = FDOS.get();
std::unique_ptr<buffer_ostream> BOS;
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
MCInstPrinter *IP = TheTarget->createMCInstPrinter(
llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI);
MCCodeEmitter *CE = nullptr;
MCAsmBackend *MAB = nullptr;
if (Opts.ShowEncoding) {
CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
//.........这里部分代码省略.........