10b57cec5SDimitry Andric //===-- cc1as_main.cpp - Clang Assembler ---------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This is the entry point to the clang -cc1as functionality, which implements 100b57cec5SDimitry Andric // the direct interface to the LLVM MC based assembler. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h" 150b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h" 160b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h" 170b57cec5SDimitry Andric #include "clang/Driver/Options.h" 180b57cec5SDimitry Andric #include "clang/Frontend/FrontendDiagnostic.h" 190b57cec5SDimitry Andric #include "clang/Frontend/TextDiagnosticPrinter.h" 200b57cec5SDimitry Andric #include "clang/Frontend/Utils.h" 210b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 220b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h" 230b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 240b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 250b57cec5SDimitry Andric #include "llvm/MC/MCAsmBackend.h" 260b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 270b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h" 280b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 290b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h" 300b57cec5SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h" 310b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h" 320b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParser.h" 330b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCTargetAsmParser.h" 340b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 350b57cec5SDimitry Andric #include "llvm/MC/MCSectionMachO.h" 360b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 370b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h" 380b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h" 390b57cec5SDimitry Andric #include "llvm/Option/Arg.h" 400b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 410b57cec5SDimitry Andric #include "llvm/Option/OptTable.h" 420b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 430b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 440b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 450b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h" 460b57cec5SDimitry Andric #include "llvm/Support/Host.h" 470b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 480b57cec5SDimitry Andric #include "llvm/Support/Path.h" 4913138422SDimitry Andric #include "llvm/Support/Process.h" 500b57cec5SDimitry Andric #include "llvm/Support/Signals.h" 510b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h" 520b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h" 530b57cec5SDimitry Andric #include "llvm/Support/TargetSelect.h" 540b57cec5SDimitry Andric #include "llvm/Support/Timer.h" 550b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 560b57cec5SDimitry Andric #include <memory> 570b57cec5SDimitry Andric #include <system_error> 580b57cec5SDimitry Andric using namespace clang; 590b57cec5SDimitry Andric using namespace clang::driver; 600b57cec5SDimitry Andric using namespace clang::driver::options; 610b57cec5SDimitry Andric using namespace llvm; 620b57cec5SDimitry Andric using namespace llvm::opt; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric namespace { 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric /// Helper class for representing a single invocation of the assembler. 670b57cec5SDimitry Andric struct AssemblerInvocation { 680b57cec5SDimitry Andric /// @name Target Options 690b57cec5SDimitry Andric /// @{ 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric /// The name of the target triple to assemble for. 720b57cec5SDimitry Andric std::string Triple; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric /// If given, the name of the target CPU to determine which instructions 750b57cec5SDimitry Andric /// are legal. 760b57cec5SDimitry Andric std::string CPU; 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric /// The list of target specific features to enable or disable -- this should 790b57cec5SDimitry Andric /// be a list of strings starting with '+' or '-'. 800b57cec5SDimitry Andric std::vector<std::string> Features; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric /// The list of symbol definitions. 830b57cec5SDimitry Andric std::vector<std::string> SymbolDefs; 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric /// @} 860b57cec5SDimitry Andric /// @name Language Options 870b57cec5SDimitry Andric /// @{ 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric std::vector<std::string> IncludePaths; 900b57cec5SDimitry Andric unsigned NoInitialTextSection : 1; 910b57cec5SDimitry Andric unsigned SaveTemporaryLabels : 1; 920b57cec5SDimitry Andric unsigned GenDwarfForAssembly : 1; 930b57cec5SDimitry Andric unsigned RelaxELFRelocations : 1; 940b57cec5SDimitry Andric unsigned DwarfVersion; 950b57cec5SDimitry Andric std::string DwarfDebugFlags; 960b57cec5SDimitry Andric std::string DwarfDebugProducer; 970b57cec5SDimitry Andric std::string DebugCompilationDir; 980b57cec5SDimitry Andric std::map<const std::string, const std::string> DebugPrefixMap; 990b57cec5SDimitry Andric llvm::DebugCompressionType CompressDebugSections = 1000b57cec5SDimitry Andric llvm::DebugCompressionType::None; 1010b57cec5SDimitry Andric std::string MainFileName; 1020b57cec5SDimitry Andric std::string SplitDwarfOutput; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric /// @} 1050b57cec5SDimitry Andric /// @name Frontend Options 1060b57cec5SDimitry Andric /// @{ 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric std::string InputFile; 1090b57cec5SDimitry Andric std::vector<std::string> LLVMArgs; 1100b57cec5SDimitry Andric std::string OutputPath; 1110b57cec5SDimitry Andric enum FileType { 1120b57cec5SDimitry Andric FT_Asm, ///< Assembly (.s) output, transliterate mode. 1130b57cec5SDimitry Andric FT_Null, ///< No output, for timing purposes. 1140b57cec5SDimitry Andric FT_Obj ///< Object file output. 1150b57cec5SDimitry Andric }; 1160b57cec5SDimitry Andric FileType OutputType; 1170b57cec5SDimitry Andric unsigned ShowHelp : 1; 1180b57cec5SDimitry Andric unsigned ShowVersion : 1; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric /// @} 1210b57cec5SDimitry Andric /// @name Transliterate Options 1220b57cec5SDimitry Andric /// @{ 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric unsigned OutputAsmVariant; 1250b57cec5SDimitry Andric unsigned ShowEncoding : 1; 1260b57cec5SDimitry Andric unsigned ShowInst : 1; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric /// @} 1290b57cec5SDimitry Andric /// @name Assembler Options 1300b57cec5SDimitry Andric /// @{ 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric unsigned RelaxAll : 1; 1330b57cec5SDimitry Andric unsigned NoExecStack : 1; 1340b57cec5SDimitry Andric unsigned FatalWarnings : 1; 135a7dea167SDimitry Andric unsigned NoWarn : 1; 1360b57cec5SDimitry Andric unsigned IncrementalLinkerCompatible : 1; 1370b57cec5SDimitry Andric unsigned EmbedBitcode : 1; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric /// The name of the relocation model to use. 1400b57cec5SDimitry Andric std::string RelocationModel; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric /// The ABI targeted by the backend. Specified using -target-abi. Empty 1430b57cec5SDimitry Andric /// otherwise. 1440b57cec5SDimitry Andric std::string TargetABI; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric /// @} 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric public: 1490b57cec5SDimitry Andric AssemblerInvocation() { 1500b57cec5SDimitry Andric Triple = ""; 1510b57cec5SDimitry Andric NoInitialTextSection = 0; 1520b57cec5SDimitry Andric InputFile = "-"; 1530b57cec5SDimitry Andric OutputPath = "-"; 1540b57cec5SDimitry Andric OutputType = FT_Asm; 1550b57cec5SDimitry Andric OutputAsmVariant = 0; 1560b57cec5SDimitry Andric ShowInst = 0; 1570b57cec5SDimitry Andric ShowEncoding = 0; 1580b57cec5SDimitry Andric RelaxAll = 0; 1590b57cec5SDimitry Andric NoExecStack = 0; 1600b57cec5SDimitry Andric FatalWarnings = 0; 161a7dea167SDimitry Andric NoWarn = 0; 1620b57cec5SDimitry Andric IncrementalLinkerCompatible = 0; 1630b57cec5SDimitry Andric DwarfVersion = 0; 1640b57cec5SDimitry Andric EmbedBitcode = 0; 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric static bool CreateFromArgs(AssemblerInvocation &Res, 1680b57cec5SDimitry Andric ArrayRef<const char *> Argv, 1690b57cec5SDimitry Andric DiagnosticsEngine &Diags); 1700b57cec5SDimitry Andric }; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, 1750b57cec5SDimitry Andric ArrayRef<const char *> Argv, 1760b57cec5SDimitry Andric DiagnosticsEngine &Diags) { 1770b57cec5SDimitry Andric bool Success = true; 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric // Parse the arguments. 180a7dea167SDimitry Andric const OptTable &OptTbl = getDriverOptTable(); 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric const unsigned IncludedFlagsBitmask = options::CC1AsOption; 1830b57cec5SDimitry Andric unsigned MissingArgIndex, MissingArgCount; 184a7dea167SDimitry Andric InputArgList Args = OptTbl.ParseArgs(Argv, MissingArgIndex, MissingArgCount, 1850b57cec5SDimitry Andric IncludedFlagsBitmask); 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric // Check for missing argument error. 1880b57cec5SDimitry Andric if (MissingArgCount) { 1890b57cec5SDimitry Andric Diags.Report(diag::err_drv_missing_argument) 1900b57cec5SDimitry Andric << Args.getArgString(MissingArgIndex) << MissingArgCount; 1910b57cec5SDimitry Andric Success = false; 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric // Issue errors on unknown arguments. 1950b57cec5SDimitry Andric for (const Arg *A : Args.filtered(OPT_UNKNOWN)) { 1960b57cec5SDimitry Andric auto ArgString = A->getAsString(Args); 1970b57cec5SDimitry Andric std::string Nearest; 198a7dea167SDimitry Andric if (OptTbl.findNearest(ArgString, Nearest, IncludedFlagsBitmask) > 1) 1990b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument) << ArgString; 2000b57cec5SDimitry Andric else 2010b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument_with_suggestion) 2020b57cec5SDimitry Andric << ArgString << Nearest; 2030b57cec5SDimitry Andric Success = false; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric // Construct the invocation. 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric // Target Options 2090b57cec5SDimitry Andric Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple)); 210*5ffd83dbSDimitry Andric Opts.CPU = std::string(Args.getLastArgValue(OPT_target_cpu)); 2110b57cec5SDimitry Andric Opts.Features = Args.getAllArgValues(OPT_target_feature); 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric // Use the default target triple if unspecified. 2140b57cec5SDimitry Andric if (Opts.Triple.empty()) 2150b57cec5SDimitry Andric Opts.Triple = llvm::sys::getDefaultTargetTriple(); 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // Language Options 2180b57cec5SDimitry Andric Opts.IncludePaths = Args.getAllArgValues(OPT_I); 2190b57cec5SDimitry Andric Opts.NoInitialTextSection = Args.hasArg(OPT_n); 2200b57cec5SDimitry Andric Opts.SaveTemporaryLabels = Args.hasArg(OPT_msave_temp_labels); 2210b57cec5SDimitry Andric // Any DebugInfoKind implies GenDwarfForAssembly. 2220b57cec5SDimitry Andric Opts.GenDwarfForAssembly = Args.hasArg(OPT_debug_info_kind_EQ); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(OPT_compress_debug_sections, 2250b57cec5SDimitry Andric OPT_compress_debug_sections_EQ)) { 2260b57cec5SDimitry Andric if (A->getOption().getID() == OPT_compress_debug_sections) { 2270b57cec5SDimitry Andric // TODO: be more clever about the compression type auto-detection 2280b57cec5SDimitry Andric Opts.CompressDebugSections = llvm::DebugCompressionType::GNU; 2290b57cec5SDimitry Andric } else { 2300b57cec5SDimitry Andric Opts.CompressDebugSections = 2310b57cec5SDimitry Andric llvm::StringSwitch<llvm::DebugCompressionType>(A->getValue()) 2320b57cec5SDimitry Andric .Case("none", llvm::DebugCompressionType::None) 2330b57cec5SDimitry Andric .Case("zlib", llvm::DebugCompressionType::Z) 2340b57cec5SDimitry Andric .Case("zlib-gnu", llvm::DebugCompressionType::GNU) 2350b57cec5SDimitry Andric .Default(llvm::DebugCompressionType::None); 2360b57cec5SDimitry Andric } 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric Opts.RelaxELFRelocations = Args.hasArg(OPT_mrelax_relocations); 2400b57cec5SDimitry Andric Opts.DwarfVersion = getLastArgIntValue(Args, OPT_dwarf_version_EQ, 2, Diags); 241*5ffd83dbSDimitry Andric Opts.DwarfDebugFlags = 242*5ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_dwarf_debug_flags)); 243*5ffd83dbSDimitry Andric Opts.DwarfDebugProducer = 244*5ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_dwarf_debug_producer)); 245*5ffd83dbSDimitry Andric Opts.DebugCompilationDir = 246*5ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_fdebug_compilation_dir)); 247*5ffd83dbSDimitry Andric Opts.MainFileName = std::string(Args.getLastArgValue(OPT_main_file_name)); 2480b57cec5SDimitry Andric 249*5ffd83dbSDimitry Andric for (const auto &Arg : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ)) { 250*5ffd83dbSDimitry Andric auto Split = StringRef(Arg).split('='); 251*5ffd83dbSDimitry Andric Opts.DebugPrefixMap.insert( 252*5ffd83dbSDimitry Andric {std::string(Split.first), std::string(Split.second)}); 253*5ffd83dbSDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric // Frontend Options 2560b57cec5SDimitry Andric if (Args.hasArg(OPT_INPUT)) { 2570b57cec5SDimitry Andric bool First = true; 2580b57cec5SDimitry Andric for (const Arg *A : Args.filtered(OPT_INPUT)) { 2590b57cec5SDimitry Andric if (First) { 2600b57cec5SDimitry Andric Opts.InputFile = A->getValue(); 2610b57cec5SDimitry Andric First = false; 2620b57cec5SDimitry Andric } else { 2630b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args); 2640b57cec5SDimitry Andric Success = false; 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm); 269*5ffd83dbSDimitry Andric Opts.OutputPath = std::string(Args.getLastArgValue(OPT_o)); 270*5ffd83dbSDimitry Andric Opts.SplitDwarfOutput = 271*5ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_split_dwarf_output)); 2720b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(OPT_filetype)) { 2730b57cec5SDimitry Andric StringRef Name = A->getValue(); 2740b57cec5SDimitry Andric unsigned OutputType = StringSwitch<unsigned>(Name) 2750b57cec5SDimitry Andric .Case("asm", FT_Asm) 2760b57cec5SDimitry Andric .Case("null", FT_Null) 2770b57cec5SDimitry Andric .Case("obj", FT_Obj) 2780b57cec5SDimitry Andric .Default(~0U); 2790b57cec5SDimitry Andric if (OutputType == ~0U) { 2800b57cec5SDimitry Andric Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name; 2810b57cec5SDimitry Andric Success = false; 2820b57cec5SDimitry Andric } else 2830b57cec5SDimitry Andric Opts.OutputType = FileType(OutputType); 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric Opts.ShowHelp = Args.hasArg(OPT_help); 2860b57cec5SDimitry Andric Opts.ShowVersion = Args.hasArg(OPT_version); 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // Transliterate Options 2890b57cec5SDimitry Andric Opts.OutputAsmVariant = 2900b57cec5SDimitry Andric getLastArgIntValue(Args, OPT_output_asm_variant, 0, Diags); 2910b57cec5SDimitry Andric Opts.ShowEncoding = Args.hasArg(OPT_show_encoding); 2920b57cec5SDimitry Andric Opts.ShowInst = Args.hasArg(OPT_show_inst); 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric // Assemble Options 2950b57cec5SDimitry Andric Opts.RelaxAll = Args.hasArg(OPT_mrelax_all); 2960b57cec5SDimitry Andric Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack); 2970b57cec5SDimitry Andric Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings); 298a7dea167SDimitry Andric Opts.NoWarn = Args.hasArg(OPT_massembler_no_warn); 299*5ffd83dbSDimitry Andric Opts.RelocationModel = 300*5ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_mrelocation_model, "pic")); 301*5ffd83dbSDimitry Andric Opts.TargetABI = std::string(Args.getLastArgValue(OPT_target_abi)); 3020b57cec5SDimitry Andric Opts.IncrementalLinkerCompatible = 3030b57cec5SDimitry Andric Args.hasArg(OPT_mincremental_linker_compatible); 3040b57cec5SDimitry Andric Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym); 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric // EmbedBitcode Option. If -fembed-bitcode is enabled, set the flag. 3070b57cec5SDimitry Andric // EmbedBitcode behaves the same for all embed options for assembly files. 3080b57cec5SDimitry Andric if (auto *A = Args.getLastArg(OPT_fembed_bitcode_EQ)) { 3090b57cec5SDimitry Andric Opts.EmbedBitcode = llvm::StringSwitch<unsigned>(A->getValue()) 3100b57cec5SDimitry Andric .Case("all", 1) 3110b57cec5SDimitry Andric .Case("bitcode", 1) 3120b57cec5SDimitry Andric .Case("marker", 1) 3130b57cec5SDimitry Andric .Default(0); 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric return Success; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric static std::unique_ptr<raw_fd_ostream> 3200b57cec5SDimitry Andric getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) { 3210b57cec5SDimitry Andric // Make sure that the Out file gets unlinked from the disk if we get a 3220b57cec5SDimitry Andric // SIGINT. 3230b57cec5SDimitry Andric if (Path != "-") 3240b57cec5SDimitry Andric sys::RemoveFileOnSignal(Path); 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric std::error_code EC; 327a7dea167SDimitry Andric auto Out = std::make_unique<raw_fd_ostream>( 328a7dea167SDimitry Andric Path, EC, (Binary ? sys::fs::OF_None : sys::fs::OF_Text)); 3290b57cec5SDimitry Andric if (EC) { 3300b57cec5SDimitry Andric Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message(); 3310b57cec5SDimitry Andric return nullptr; 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric return Out; 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric static bool ExecuteAssembler(AssemblerInvocation &Opts, 3380b57cec5SDimitry Andric DiagnosticsEngine &Diags) { 3390b57cec5SDimitry Andric // Get the target specific parser. 3400b57cec5SDimitry Andric std::string Error; 3410b57cec5SDimitry Andric const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error); 3420b57cec5SDimitry Andric if (!TheTarget) 3430b57cec5SDimitry Andric return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 3460b57cec5SDimitry Andric MemoryBuffer::getFileOrSTDIN(Opts.InputFile); 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric if (std::error_code EC = Buffer.getError()) { 3490b57cec5SDimitry Andric Error = EC.message(); 3500b57cec5SDimitry Andric return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile; 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric SourceMgr SrcMgr; 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric // Tell SrcMgr about this buffer, which is what the parser will pick up. 3560b57cec5SDimitry Andric unsigned BufferIndex = SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc()); 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric // Record the location of the include directories so that the lexer can find 3590b57cec5SDimitry Andric // it later. 3600b57cec5SDimitry Andric SrcMgr.setIncludeDirs(Opts.IncludePaths); 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple)); 3630b57cec5SDimitry Andric assert(MRI && "Unable to create target register info!"); 3640b57cec5SDimitry Andric 365480093f4SDimitry Andric MCTargetOptions MCOptions; 366480093f4SDimitry Andric std::unique_ptr<MCAsmInfo> MAI( 367480093f4SDimitry Andric TheTarget->createMCAsmInfo(*MRI, Opts.Triple, MCOptions)); 3680b57cec5SDimitry Andric assert(MAI && "Unable to create target asm info!"); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric // Ensure MCAsmInfo initialization occurs before any use, otherwise sections 3710b57cec5SDimitry Andric // may be created with a combination of default and explicit settings. 3720b57cec5SDimitry Andric MAI->setCompressDebugSections(Opts.CompressDebugSections); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations); 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj; 3770b57cec5SDimitry Andric if (Opts.OutputPath.empty()) 3780b57cec5SDimitry Andric Opts.OutputPath = "-"; 3790b57cec5SDimitry Andric std::unique_ptr<raw_fd_ostream> FDOS = 3800b57cec5SDimitry Andric getOutputStream(Opts.OutputPath, Diags, IsBinary); 3810b57cec5SDimitry Andric if (!FDOS) 3820b57cec5SDimitry Andric return true; 3830b57cec5SDimitry Andric std::unique_ptr<raw_fd_ostream> DwoOS; 3840b57cec5SDimitry Andric if (!Opts.SplitDwarfOutput.empty()) 3850b57cec5SDimitry Andric DwoOS = getOutputStream(Opts.SplitDwarfOutput, Diags, IsBinary); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and 3880b57cec5SDimitry Andric // MCObjectFileInfo needs a MCContext reference in order to initialize itself. 3890b57cec5SDimitry Andric std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo()); 3900b57cec5SDimitry Andric 391a7dea167SDimitry Andric MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr, &MCOptions); 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric bool PIC = false; 3940b57cec5SDimitry Andric if (Opts.RelocationModel == "static") { 3950b57cec5SDimitry Andric PIC = false; 3960b57cec5SDimitry Andric } else if (Opts.RelocationModel == "pic") { 3970b57cec5SDimitry Andric PIC = true; 3980b57cec5SDimitry Andric } else { 3990b57cec5SDimitry Andric assert(Opts.RelocationModel == "dynamic-no-pic" && 4000b57cec5SDimitry Andric "Invalid PIC model!"); 4010b57cec5SDimitry Andric PIC = false; 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric MOFI->InitMCObjectFileInfo(Triple(Opts.Triple), PIC, Ctx); 4050b57cec5SDimitry Andric if (Opts.SaveTemporaryLabels) 4060b57cec5SDimitry Andric Ctx.setAllowTemporaryLabels(false); 4070b57cec5SDimitry Andric if (Opts.GenDwarfForAssembly) 4080b57cec5SDimitry Andric Ctx.setGenDwarfForAssembly(true); 4090b57cec5SDimitry Andric if (!Opts.DwarfDebugFlags.empty()) 4100b57cec5SDimitry Andric Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags)); 4110b57cec5SDimitry Andric if (!Opts.DwarfDebugProducer.empty()) 4120b57cec5SDimitry Andric Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer)); 4130b57cec5SDimitry Andric if (!Opts.DebugCompilationDir.empty()) 4140b57cec5SDimitry Andric Ctx.setCompilationDir(Opts.DebugCompilationDir); 4150b57cec5SDimitry Andric else { 4160b57cec5SDimitry Andric // If no compilation dir is set, try to use the current directory. 4170b57cec5SDimitry Andric SmallString<128> CWD; 4180b57cec5SDimitry Andric if (!sys::fs::current_path(CWD)) 4190b57cec5SDimitry Andric Ctx.setCompilationDir(CWD); 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric if (!Opts.DebugPrefixMap.empty()) 4220b57cec5SDimitry Andric for (const auto &KV : Opts.DebugPrefixMap) 4230b57cec5SDimitry Andric Ctx.addDebugPrefixMapEntry(KV.first, KV.second); 4240b57cec5SDimitry Andric if (!Opts.MainFileName.empty()) 4250b57cec5SDimitry Andric Ctx.setMainFileName(StringRef(Opts.MainFileName)); 4260b57cec5SDimitry Andric Ctx.setDwarfVersion(Opts.DwarfVersion); 4270b57cec5SDimitry Andric if (Opts.GenDwarfForAssembly) 4280b57cec5SDimitry Andric Ctx.setGenDwarfRootFile(Opts.InputFile, 4290b57cec5SDimitry Andric SrcMgr.getMemoryBuffer(BufferIndex)->getBuffer()); 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric // Build up the feature string from the target feature list. 432*5ffd83dbSDimitry Andric std::string FS = llvm::join(Opts.Features, ","); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric std::unique_ptr<MCStreamer> Str; 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); 4370b57cec5SDimitry Andric std::unique_ptr<MCSubtargetInfo> STI( 4380b57cec5SDimitry Andric TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS)); 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric raw_pwrite_stream *Out = FDOS.get(); 4410b57cec5SDimitry Andric std::unique_ptr<buffer_ostream> BOS; 4420b57cec5SDimitry Andric 443a7dea167SDimitry Andric MCOptions.MCNoWarn = Opts.NoWarn; 444a7dea167SDimitry Andric MCOptions.MCFatalWarnings = Opts.FatalWarnings; 4450b57cec5SDimitry Andric MCOptions.ABIName = Opts.TargetABI; 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric // FIXME: There is a bit of code duplication with addPassesToEmitFile. 4480b57cec5SDimitry Andric if (Opts.OutputType == AssemblerInvocation::FT_Asm) { 4490b57cec5SDimitry Andric MCInstPrinter *IP = TheTarget->createMCInstPrinter( 4500b57cec5SDimitry Andric llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE; 4530b57cec5SDimitry Andric if (Opts.ShowEncoding) 4540b57cec5SDimitry Andric CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); 4550b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB( 4560b57cec5SDimitry Andric TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); 4570b57cec5SDimitry Andric 458a7dea167SDimitry Andric auto FOut = std::make_unique<formatted_raw_ostream>(*Out); 4590b57cec5SDimitry Andric Str.reset(TheTarget->createAsmStreamer( 4600b57cec5SDimitry Andric Ctx, std::move(FOut), /*asmverbose*/ true, 4610b57cec5SDimitry Andric /*useDwarfDirectory*/ true, IP, std::move(CE), std::move(MAB), 4620b57cec5SDimitry Andric Opts.ShowInst)); 4630b57cec5SDimitry Andric } else if (Opts.OutputType == AssemblerInvocation::FT_Null) { 4640b57cec5SDimitry Andric Str.reset(createNullStreamer(Ctx)); 4650b57cec5SDimitry Andric } else { 4660b57cec5SDimitry Andric assert(Opts.OutputType == AssemblerInvocation::FT_Obj && 4670b57cec5SDimitry Andric "Invalid file type!"); 4680b57cec5SDimitry Andric if (!FDOS->supportsSeeking()) { 469a7dea167SDimitry Andric BOS = std::make_unique<buffer_ostream>(*FDOS); 4700b57cec5SDimitry Andric Out = BOS.get(); 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE( 4740b57cec5SDimitry Andric TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); 4750b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB( 4760b57cec5SDimitry Andric TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); 4770b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> OW = 4780b57cec5SDimitry Andric DwoOS ? MAB->createDwoObjectWriter(*Out, *DwoOS) 4790b57cec5SDimitry Andric : MAB->createObjectWriter(*Out); 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric Triple T(Opts.Triple); 4820b57cec5SDimitry Andric Str.reset(TheTarget->createMCObjectStreamer( 4830b57cec5SDimitry Andric T, Ctx, std::move(MAB), std::move(OW), std::move(CE), *STI, 4840b57cec5SDimitry Andric Opts.RelaxAll, Opts.IncrementalLinkerCompatible, 4850b57cec5SDimitry Andric /*DWARFMustBeAtTheEnd*/ true)); 4860b57cec5SDimitry Andric Str.get()->InitSections(Opts.NoExecStack); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric // When -fembed-bitcode is passed to clang_as, a 1-byte marker 4900b57cec5SDimitry Andric // is emitted in __LLVM,__asm section if the object file is MachO format. 4910b57cec5SDimitry Andric if (Opts.EmbedBitcode && Ctx.getObjectFileInfo()->getObjectFileType() == 4920b57cec5SDimitry Andric MCObjectFileInfo::IsMachO) { 4930b57cec5SDimitry Andric MCSection *AsmLabel = Ctx.getMachOSection( 4940b57cec5SDimitry Andric "__LLVM", "__asm", MachO::S_REGULAR, 4, SectionKind::getReadOnly()); 4950b57cec5SDimitry Andric Str.get()->SwitchSection(AsmLabel); 496*5ffd83dbSDimitry Andric Str.get()->emitZeros(1); 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // Assembly to object compilation should leverage assembly info. 5000b57cec5SDimitry Andric Str->setUseAssemblerInfoForParsing(true); 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric bool Failed = false; 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric std::unique_ptr<MCAsmParser> Parser( 5050b57cec5SDimitry Andric createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric // FIXME: init MCTargetOptions from sanitizer flags here. 5080b57cec5SDimitry Andric std::unique_ptr<MCTargetAsmParser> TAP( 5090b57cec5SDimitry Andric TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); 5100b57cec5SDimitry Andric if (!TAP) 5110b57cec5SDimitry Andric Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric // Set values for symbols, if any. 5140b57cec5SDimitry Andric for (auto &S : Opts.SymbolDefs) { 5150b57cec5SDimitry Andric auto Pair = StringRef(S).split('='); 5160b57cec5SDimitry Andric auto Sym = Pair.first; 5170b57cec5SDimitry Andric auto Val = Pair.second; 5180b57cec5SDimitry Andric int64_t Value; 5190b57cec5SDimitry Andric // We have already error checked this in the driver. 5200b57cec5SDimitry Andric Val.getAsInteger(0, Value); 5210b57cec5SDimitry Andric Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value); 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric if (!Failed) { 5250b57cec5SDimitry Andric Parser->setTargetParser(*TAP.get()); 5260b57cec5SDimitry Andric Failed = Parser->Run(Opts.NoInitialTextSection); 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric // Close Streamer first. 5300b57cec5SDimitry Andric // It might have a reference to the output stream. 5310b57cec5SDimitry Andric Str.reset(); 5320b57cec5SDimitry Andric // Close the output stream early. 5330b57cec5SDimitry Andric BOS.reset(); 5340b57cec5SDimitry Andric FDOS.reset(); 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric // Delete output file if there were errors. 5370b57cec5SDimitry Andric if (Failed) { 5380b57cec5SDimitry Andric if (Opts.OutputPath != "-") 5390b57cec5SDimitry Andric sys::fs::remove(Opts.OutputPath); 5400b57cec5SDimitry Andric if (!Opts.SplitDwarfOutput.empty() && Opts.SplitDwarfOutput != "-") 5410b57cec5SDimitry Andric sys::fs::remove(Opts.SplitDwarfOutput); 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric return Failed; 5450b57cec5SDimitry Andric } 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric static void LLVMErrorHandler(void *UserData, const std::string &Message, 5480b57cec5SDimitry Andric bool GenCrashDiag) { 5490b57cec5SDimitry Andric DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric Diags.Report(diag::err_fe_error_backend) << Message; 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric // We cannot recover from llvm errors. 55413138422SDimitry Andric sys::Process::Exit(1); 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { 5580b57cec5SDimitry Andric // Initialize targets and assembly printers/parsers. 5590b57cec5SDimitry Andric InitializeAllTargetInfos(); 5600b57cec5SDimitry Andric InitializeAllTargetMCs(); 5610b57cec5SDimitry Andric InitializeAllAsmParsers(); 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric // Construct our diagnostic client. 5640b57cec5SDimitry Andric IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 5650b57cec5SDimitry Andric TextDiagnosticPrinter *DiagClient 5660b57cec5SDimitry Andric = new TextDiagnosticPrinter(errs(), &*DiagOpts); 5670b57cec5SDimitry Andric DiagClient->setPrefix("clang -cc1as"); 5680b57cec5SDimitry Andric IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 5690b57cec5SDimitry Andric DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient); 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric // Set an error handler, so that any LLVM backend diagnostics go through our 5720b57cec5SDimitry Andric // error handler. 5730b57cec5SDimitry Andric ScopedFatalErrorHandler FatalErrorHandler 5740b57cec5SDimitry Andric (LLVMErrorHandler, static_cast<void*>(&Diags)); 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric // Parse the arguments. 5770b57cec5SDimitry Andric AssemblerInvocation Asm; 5780b57cec5SDimitry Andric if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags)) 5790b57cec5SDimitry Andric return 1; 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric if (Asm.ShowHelp) { 582a7dea167SDimitry Andric getDriverOptTable().PrintHelp( 583a7dea167SDimitry Andric llvm::outs(), "clang -cc1as [options] file...", 5840b57cec5SDimitry Andric "Clang Integrated Assembler", 5850b57cec5SDimitry Andric /*Include=*/driver::options::CC1AsOption, /*Exclude=*/0, 5860b57cec5SDimitry Andric /*ShowAllAliases=*/false); 5870b57cec5SDimitry Andric return 0; 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric // Honor -version. 5910b57cec5SDimitry Andric // 5920b57cec5SDimitry Andric // FIXME: Use a better -version message? 5930b57cec5SDimitry Andric if (Asm.ShowVersion) { 5940b57cec5SDimitry Andric llvm::cl::PrintVersionMessage(); 5950b57cec5SDimitry Andric return 0; 5960b57cec5SDimitry Andric } 5970b57cec5SDimitry Andric 5980b57cec5SDimitry Andric // Honor -mllvm. 5990b57cec5SDimitry Andric // 6000b57cec5SDimitry Andric // FIXME: Remove this, one day. 6010b57cec5SDimitry Andric if (!Asm.LLVMArgs.empty()) { 6020b57cec5SDimitry Andric unsigned NumArgs = Asm.LLVMArgs.size(); 603a7dea167SDimitry Andric auto Args = std::make_unique<const char*[]>(NumArgs + 2); 6040b57cec5SDimitry Andric Args[0] = "clang (LLVM option parsing)"; 6050b57cec5SDimitry Andric for (unsigned i = 0; i != NumArgs; ++i) 6060b57cec5SDimitry Andric Args[i + 1] = Asm.LLVMArgs[i].c_str(); 6070b57cec5SDimitry Andric Args[NumArgs + 1] = nullptr; 6080b57cec5SDimitry Andric llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get()); 6090b57cec5SDimitry Andric } 6100b57cec5SDimitry Andric 6110b57cec5SDimitry Andric // Execute the invocation, unless there were parsing errors. 6120b57cec5SDimitry Andric bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags); 6130b57cec5SDimitry Andric 6140b57cec5SDimitry Andric // If any timers were active but haven't been destroyed yet, print their 6150b57cec5SDimitry Andric // results now. 6160b57cec5SDimitry Andric TimerGroup::printAll(errs()); 617a7dea167SDimitry Andric TimerGroup::clearAll(); 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric return !!Failed; 6200b57cec5SDimitry Andric } 621