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; 94*fe6060f1SDimitry Andric unsigned Dwarf64 : 1; 950b57cec5SDimitry Andric unsigned DwarfVersion; 960b57cec5SDimitry Andric std::string DwarfDebugFlags; 970b57cec5SDimitry Andric std::string DwarfDebugProducer; 980b57cec5SDimitry Andric std::string DebugCompilationDir; 990b57cec5SDimitry Andric std::map<const std::string, const std::string> DebugPrefixMap; 1000b57cec5SDimitry Andric llvm::DebugCompressionType CompressDebugSections = 1010b57cec5SDimitry Andric llvm::DebugCompressionType::None; 1020b57cec5SDimitry Andric std::string MainFileName; 1030b57cec5SDimitry Andric std::string SplitDwarfOutput; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric /// @} 1060b57cec5SDimitry Andric /// @name Frontend Options 1070b57cec5SDimitry Andric /// @{ 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric std::string InputFile; 1100b57cec5SDimitry Andric std::vector<std::string> LLVMArgs; 1110b57cec5SDimitry Andric std::string OutputPath; 1120b57cec5SDimitry Andric enum FileType { 1130b57cec5SDimitry Andric FT_Asm, ///< Assembly (.s) output, transliterate mode. 1140b57cec5SDimitry Andric FT_Null, ///< No output, for timing purposes. 1150b57cec5SDimitry Andric FT_Obj ///< Object file output. 1160b57cec5SDimitry Andric }; 1170b57cec5SDimitry Andric FileType OutputType; 1180b57cec5SDimitry Andric unsigned ShowHelp : 1; 1190b57cec5SDimitry Andric unsigned ShowVersion : 1; 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric /// @} 1220b57cec5SDimitry Andric /// @name Transliterate Options 1230b57cec5SDimitry Andric /// @{ 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric unsigned OutputAsmVariant; 1260b57cec5SDimitry Andric unsigned ShowEncoding : 1; 1270b57cec5SDimitry Andric unsigned ShowInst : 1; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric /// @} 1300b57cec5SDimitry Andric /// @name Assembler Options 1310b57cec5SDimitry Andric /// @{ 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric unsigned RelaxAll : 1; 1340b57cec5SDimitry Andric unsigned NoExecStack : 1; 1350b57cec5SDimitry Andric unsigned FatalWarnings : 1; 136a7dea167SDimitry Andric unsigned NoWarn : 1; 1370b57cec5SDimitry Andric unsigned IncrementalLinkerCompatible : 1; 1380b57cec5SDimitry Andric unsigned EmbedBitcode : 1; 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric /// The name of the relocation model to use. 1410b57cec5SDimitry Andric std::string RelocationModel; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric /// The ABI targeted by the backend. Specified using -target-abi. Empty 1440b57cec5SDimitry Andric /// otherwise. 1450b57cec5SDimitry Andric std::string TargetABI; 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric /// @} 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric public: 1500b57cec5SDimitry Andric AssemblerInvocation() { 1510b57cec5SDimitry Andric Triple = ""; 1520b57cec5SDimitry Andric NoInitialTextSection = 0; 1530b57cec5SDimitry Andric InputFile = "-"; 1540b57cec5SDimitry Andric OutputPath = "-"; 1550b57cec5SDimitry Andric OutputType = FT_Asm; 1560b57cec5SDimitry Andric OutputAsmVariant = 0; 1570b57cec5SDimitry Andric ShowInst = 0; 1580b57cec5SDimitry Andric ShowEncoding = 0; 1590b57cec5SDimitry Andric RelaxAll = 0; 1600b57cec5SDimitry Andric NoExecStack = 0; 1610b57cec5SDimitry Andric FatalWarnings = 0; 162a7dea167SDimitry Andric NoWarn = 0; 1630b57cec5SDimitry Andric IncrementalLinkerCompatible = 0; 164*fe6060f1SDimitry Andric Dwarf64 = 0; 1650b57cec5SDimitry Andric DwarfVersion = 0; 1660b57cec5SDimitry Andric EmbedBitcode = 0; 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric static bool CreateFromArgs(AssemblerInvocation &Res, 1700b57cec5SDimitry Andric ArrayRef<const char *> Argv, 1710b57cec5SDimitry Andric DiagnosticsEngine &Diags); 1720b57cec5SDimitry Andric }; 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, 1770b57cec5SDimitry Andric ArrayRef<const char *> Argv, 1780b57cec5SDimitry Andric DiagnosticsEngine &Diags) { 1790b57cec5SDimitry Andric bool Success = true; 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric // Parse the arguments. 182a7dea167SDimitry Andric const OptTable &OptTbl = getDriverOptTable(); 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric const unsigned IncludedFlagsBitmask = options::CC1AsOption; 1850b57cec5SDimitry Andric unsigned MissingArgIndex, MissingArgCount; 186a7dea167SDimitry Andric InputArgList Args = OptTbl.ParseArgs(Argv, MissingArgIndex, MissingArgCount, 1870b57cec5SDimitry Andric IncludedFlagsBitmask); 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric // Check for missing argument error. 1900b57cec5SDimitry Andric if (MissingArgCount) { 1910b57cec5SDimitry Andric Diags.Report(diag::err_drv_missing_argument) 1920b57cec5SDimitry Andric << Args.getArgString(MissingArgIndex) << MissingArgCount; 1930b57cec5SDimitry Andric Success = false; 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric // Issue errors on unknown arguments. 1970b57cec5SDimitry Andric for (const Arg *A : Args.filtered(OPT_UNKNOWN)) { 1980b57cec5SDimitry Andric auto ArgString = A->getAsString(Args); 1990b57cec5SDimitry Andric std::string Nearest; 200a7dea167SDimitry Andric if (OptTbl.findNearest(ArgString, Nearest, IncludedFlagsBitmask) > 1) 2010b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument) << ArgString; 2020b57cec5SDimitry Andric else 2030b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument_with_suggestion) 2040b57cec5SDimitry Andric << ArgString << Nearest; 2050b57cec5SDimitry Andric Success = false; 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric // Construct the invocation. 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric // Target Options 2110b57cec5SDimitry Andric Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple)); 2125ffd83dbSDimitry Andric Opts.CPU = std::string(Args.getLastArgValue(OPT_target_cpu)); 2130b57cec5SDimitry Andric Opts.Features = Args.getAllArgValues(OPT_target_feature); 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric // Use the default target triple if unspecified. 2160b57cec5SDimitry Andric if (Opts.Triple.empty()) 2170b57cec5SDimitry Andric Opts.Triple = llvm::sys::getDefaultTargetTriple(); 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric // Language Options 2200b57cec5SDimitry Andric Opts.IncludePaths = Args.getAllArgValues(OPT_I); 2210b57cec5SDimitry Andric Opts.NoInitialTextSection = Args.hasArg(OPT_n); 2220b57cec5SDimitry Andric Opts.SaveTemporaryLabels = Args.hasArg(OPT_msave_temp_labels); 2230b57cec5SDimitry Andric // Any DebugInfoKind implies GenDwarfForAssembly. 2240b57cec5SDimitry Andric Opts.GenDwarfForAssembly = Args.hasArg(OPT_debug_info_kind_EQ); 2250b57cec5SDimitry Andric 226e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(OPT_compress_debug_sections_EQ)) { 2270b57cec5SDimitry Andric Opts.CompressDebugSections = 2280b57cec5SDimitry Andric llvm::StringSwitch<llvm::DebugCompressionType>(A->getValue()) 2290b57cec5SDimitry Andric .Case("none", llvm::DebugCompressionType::None) 2300b57cec5SDimitry Andric .Case("zlib", llvm::DebugCompressionType::Z) 2310b57cec5SDimitry Andric .Case("zlib-gnu", llvm::DebugCompressionType::GNU) 2320b57cec5SDimitry Andric .Default(llvm::DebugCompressionType::None); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric Opts.RelaxELFRelocations = Args.hasArg(OPT_mrelax_relocations); 236*fe6060f1SDimitry Andric if (auto *DwarfFormatArg = Args.getLastArg(OPT_gdwarf64, OPT_gdwarf32)) 237*fe6060f1SDimitry Andric Opts.Dwarf64 = DwarfFormatArg->getOption().matches(OPT_gdwarf64); 2380b57cec5SDimitry Andric Opts.DwarfVersion = getLastArgIntValue(Args, OPT_dwarf_version_EQ, 2, Diags); 2395ffd83dbSDimitry Andric Opts.DwarfDebugFlags = 2405ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_dwarf_debug_flags)); 2415ffd83dbSDimitry Andric Opts.DwarfDebugProducer = 2425ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_dwarf_debug_producer)); 243*fe6060f1SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ, 244*fe6060f1SDimitry Andric options::OPT_fdebug_compilation_dir_EQ)) 245*fe6060f1SDimitry Andric Opts.DebugCompilationDir = A->getValue(); 2465ffd83dbSDimitry Andric Opts.MainFileName = std::string(Args.getLastArgValue(OPT_main_file_name)); 2470b57cec5SDimitry Andric 2485ffd83dbSDimitry Andric for (const auto &Arg : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ)) { 2495ffd83dbSDimitry Andric auto Split = StringRef(Arg).split('='); 2505ffd83dbSDimitry Andric Opts.DebugPrefixMap.insert( 2515ffd83dbSDimitry Andric {std::string(Split.first), std::string(Split.second)}); 2525ffd83dbSDimitry Andric } 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric // Frontend Options 2550b57cec5SDimitry Andric if (Args.hasArg(OPT_INPUT)) { 2560b57cec5SDimitry Andric bool First = true; 2570b57cec5SDimitry Andric for (const Arg *A : Args.filtered(OPT_INPUT)) { 2580b57cec5SDimitry Andric if (First) { 2590b57cec5SDimitry Andric Opts.InputFile = A->getValue(); 2600b57cec5SDimitry Andric First = false; 2610b57cec5SDimitry Andric } else { 2620b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args); 2630b57cec5SDimitry Andric Success = false; 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm); 2685ffd83dbSDimitry Andric Opts.OutputPath = std::string(Args.getLastArgValue(OPT_o)); 2695ffd83dbSDimitry Andric Opts.SplitDwarfOutput = 2705ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_split_dwarf_output)); 2710b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(OPT_filetype)) { 2720b57cec5SDimitry Andric StringRef Name = A->getValue(); 2730b57cec5SDimitry Andric unsigned OutputType = StringSwitch<unsigned>(Name) 2740b57cec5SDimitry Andric .Case("asm", FT_Asm) 2750b57cec5SDimitry Andric .Case("null", FT_Null) 2760b57cec5SDimitry Andric .Case("obj", FT_Obj) 2770b57cec5SDimitry Andric .Default(~0U); 2780b57cec5SDimitry Andric if (OutputType == ~0U) { 2790b57cec5SDimitry Andric Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name; 2800b57cec5SDimitry Andric Success = false; 2810b57cec5SDimitry Andric } else 2820b57cec5SDimitry Andric Opts.OutputType = FileType(OutputType); 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric Opts.ShowHelp = Args.hasArg(OPT_help); 2850b57cec5SDimitry Andric Opts.ShowVersion = Args.hasArg(OPT_version); 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric // Transliterate Options 2880b57cec5SDimitry Andric Opts.OutputAsmVariant = 2890b57cec5SDimitry Andric getLastArgIntValue(Args, OPT_output_asm_variant, 0, Diags); 2900b57cec5SDimitry Andric Opts.ShowEncoding = Args.hasArg(OPT_show_encoding); 2910b57cec5SDimitry Andric Opts.ShowInst = Args.hasArg(OPT_show_inst); 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric // Assemble Options 2940b57cec5SDimitry Andric Opts.RelaxAll = Args.hasArg(OPT_mrelax_all); 2950b57cec5SDimitry Andric Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack); 2960b57cec5SDimitry Andric Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings); 297a7dea167SDimitry Andric Opts.NoWarn = Args.hasArg(OPT_massembler_no_warn); 2985ffd83dbSDimitry Andric Opts.RelocationModel = 2995ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_mrelocation_model, "pic")); 3005ffd83dbSDimitry Andric Opts.TargetABI = std::string(Args.getLastArgValue(OPT_target_abi)); 3010b57cec5SDimitry Andric Opts.IncrementalLinkerCompatible = 3020b57cec5SDimitry Andric Args.hasArg(OPT_mincremental_linker_compatible); 3030b57cec5SDimitry Andric Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym); 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric // EmbedBitcode Option. If -fembed-bitcode is enabled, set the flag. 3060b57cec5SDimitry Andric // EmbedBitcode behaves the same for all embed options for assembly files. 3070b57cec5SDimitry Andric if (auto *A = Args.getLastArg(OPT_fembed_bitcode_EQ)) { 3080b57cec5SDimitry Andric Opts.EmbedBitcode = llvm::StringSwitch<unsigned>(A->getValue()) 3090b57cec5SDimitry Andric .Case("all", 1) 3100b57cec5SDimitry Andric .Case("bitcode", 1) 3110b57cec5SDimitry Andric .Case("marker", 1) 3120b57cec5SDimitry Andric .Default(0); 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric return Success; 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric static std::unique_ptr<raw_fd_ostream> 3190b57cec5SDimitry Andric getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) { 3200b57cec5SDimitry Andric // Make sure that the Out file gets unlinked from the disk if we get a 3210b57cec5SDimitry Andric // SIGINT. 3220b57cec5SDimitry Andric if (Path != "-") 3230b57cec5SDimitry Andric sys::RemoveFileOnSignal(Path); 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric std::error_code EC; 326a7dea167SDimitry Andric auto Out = std::make_unique<raw_fd_ostream>( 327*fe6060f1SDimitry Andric Path, EC, (Binary ? sys::fs::OF_None : sys::fs::OF_TextWithCRLF)); 3280b57cec5SDimitry Andric if (EC) { 3290b57cec5SDimitry Andric Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message(); 3300b57cec5SDimitry Andric return nullptr; 3310b57cec5SDimitry Andric } 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric return Out; 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 336*fe6060f1SDimitry Andric static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts, 3370b57cec5SDimitry Andric DiagnosticsEngine &Diags) { 3380b57cec5SDimitry Andric // Get the target specific parser. 3390b57cec5SDimitry Andric std::string Error; 3400b57cec5SDimitry Andric const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error); 3410b57cec5SDimitry Andric if (!TheTarget) 3420b57cec5SDimitry Andric return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 345*fe6060f1SDimitry Andric MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true); 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric if (std::error_code EC = Buffer.getError()) { 3480b57cec5SDimitry Andric Error = EC.message(); 3490b57cec5SDimitry Andric return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile; 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric SourceMgr SrcMgr; 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // Tell SrcMgr about this buffer, which is what the parser will pick up. 3550b57cec5SDimitry Andric unsigned BufferIndex = SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc()); 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric // Record the location of the include directories so that the lexer can find 3580b57cec5SDimitry Andric // it later. 3590b57cec5SDimitry Andric SrcMgr.setIncludeDirs(Opts.IncludePaths); 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple)); 3620b57cec5SDimitry Andric assert(MRI && "Unable to create target register info!"); 3630b57cec5SDimitry Andric 364480093f4SDimitry Andric MCTargetOptions MCOptions; 365480093f4SDimitry Andric std::unique_ptr<MCAsmInfo> MAI( 366480093f4SDimitry Andric TheTarget->createMCAsmInfo(*MRI, Opts.Triple, MCOptions)); 3670b57cec5SDimitry Andric assert(MAI && "Unable to create target asm info!"); 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric // Ensure MCAsmInfo initialization occurs before any use, otherwise sections 3700b57cec5SDimitry Andric // may be created with a combination of default and explicit settings. 3710b57cec5SDimitry Andric MAI->setCompressDebugSections(Opts.CompressDebugSections); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations); 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj; 3760b57cec5SDimitry Andric if (Opts.OutputPath.empty()) 3770b57cec5SDimitry Andric Opts.OutputPath = "-"; 3780b57cec5SDimitry Andric std::unique_ptr<raw_fd_ostream> FDOS = 3790b57cec5SDimitry Andric getOutputStream(Opts.OutputPath, Diags, IsBinary); 3800b57cec5SDimitry Andric if (!FDOS) 3810b57cec5SDimitry Andric return true; 3820b57cec5SDimitry Andric std::unique_ptr<raw_fd_ostream> DwoOS; 3830b57cec5SDimitry Andric if (!Opts.SplitDwarfOutput.empty()) 3840b57cec5SDimitry Andric DwoOS = getOutputStream(Opts.SplitDwarfOutput, Diags, IsBinary); 3850b57cec5SDimitry Andric 386*fe6060f1SDimitry Andric // Build up the feature string from the target feature list. 387*fe6060f1SDimitry Andric std::string FS = llvm::join(Opts.Features, ","); 3880b57cec5SDimitry Andric 389*fe6060f1SDimitry Andric std::unique_ptr<MCSubtargetInfo> STI( 390*fe6060f1SDimitry Andric TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS)); 391*fe6060f1SDimitry Andric assert(STI && "Unable to create subtarget info!"); 392*fe6060f1SDimitry Andric 393*fe6060f1SDimitry Andric MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr, 394*fe6060f1SDimitry Andric &MCOptions); 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric bool PIC = false; 3970b57cec5SDimitry Andric if (Opts.RelocationModel == "static") { 3980b57cec5SDimitry Andric PIC = false; 3990b57cec5SDimitry Andric } else if (Opts.RelocationModel == "pic") { 4000b57cec5SDimitry Andric PIC = true; 4010b57cec5SDimitry Andric } else { 4020b57cec5SDimitry Andric assert(Opts.RelocationModel == "dynamic-no-pic" && 4030b57cec5SDimitry Andric "Invalid PIC model!"); 4040b57cec5SDimitry Andric PIC = false; 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 407*fe6060f1SDimitry Andric // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and 408*fe6060f1SDimitry Andric // MCObjectFileInfo needs a MCContext reference in order to initialize itself. 409*fe6060f1SDimitry Andric std::unique_ptr<MCObjectFileInfo> MOFI( 410*fe6060f1SDimitry Andric TheTarget->createMCObjectFileInfo(Ctx, PIC)); 411*fe6060f1SDimitry Andric Ctx.setObjectFileInfo(MOFI.get()); 412*fe6060f1SDimitry Andric 4130b57cec5SDimitry Andric if (Opts.SaveTemporaryLabels) 4140b57cec5SDimitry Andric Ctx.setAllowTemporaryLabels(false); 4150b57cec5SDimitry Andric if (Opts.GenDwarfForAssembly) 4160b57cec5SDimitry Andric Ctx.setGenDwarfForAssembly(true); 4170b57cec5SDimitry Andric if (!Opts.DwarfDebugFlags.empty()) 4180b57cec5SDimitry Andric Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags)); 4190b57cec5SDimitry Andric if (!Opts.DwarfDebugProducer.empty()) 4200b57cec5SDimitry Andric Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer)); 4210b57cec5SDimitry Andric if (!Opts.DebugCompilationDir.empty()) 4220b57cec5SDimitry Andric Ctx.setCompilationDir(Opts.DebugCompilationDir); 4230b57cec5SDimitry Andric else { 4240b57cec5SDimitry Andric // If no compilation dir is set, try to use the current directory. 4250b57cec5SDimitry Andric SmallString<128> CWD; 4260b57cec5SDimitry Andric if (!sys::fs::current_path(CWD)) 4270b57cec5SDimitry Andric Ctx.setCompilationDir(CWD); 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric if (!Opts.DebugPrefixMap.empty()) 4300b57cec5SDimitry Andric for (const auto &KV : Opts.DebugPrefixMap) 4310b57cec5SDimitry Andric Ctx.addDebugPrefixMapEntry(KV.first, KV.second); 4320b57cec5SDimitry Andric if (!Opts.MainFileName.empty()) 4330b57cec5SDimitry Andric Ctx.setMainFileName(StringRef(Opts.MainFileName)); 434*fe6060f1SDimitry Andric Ctx.setDwarfFormat(Opts.Dwarf64 ? dwarf::DWARF64 : dwarf::DWARF32); 4350b57cec5SDimitry Andric Ctx.setDwarfVersion(Opts.DwarfVersion); 4360b57cec5SDimitry Andric if (Opts.GenDwarfForAssembly) 4370b57cec5SDimitry Andric Ctx.setGenDwarfRootFile(Opts.InputFile, 4380b57cec5SDimitry Andric SrcMgr.getMemoryBuffer(BufferIndex)->getBuffer()); 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric std::unique_ptr<MCStreamer> Str; 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); 443e8d8bef9SDimitry Andric assert(MCII && "Unable to create instruction info!"); 444e8d8bef9SDimitry Andric 4450b57cec5SDimitry Andric raw_pwrite_stream *Out = FDOS.get(); 4460b57cec5SDimitry Andric std::unique_ptr<buffer_ostream> BOS; 4470b57cec5SDimitry Andric 448a7dea167SDimitry Andric MCOptions.MCNoWarn = Opts.NoWarn; 449a7dea167SDimitry Andric MCOptions.MCFatalWarnings = Opts.FatalWarnings; 4500b57cec5SDimitry Andric MCOptions.ABIName = Opts.TargetABI; 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric // FIXME: There is a bit of code duplication with addPassesToEmitFile. 4530b57cec5SDimitry Andric if (Opts.OutputType == AssemblerInvocation::FT_Asm) { 4540b57cec5SDimitry Andric MCInstPrinter *IP = TheTarget->createMCInstPrinter( 4550b57cec5SDimitry Andric llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI); 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE; 4580b57cec5SDimitry Andric if (Opts.ShowEncoding) 4590b57cec5SDimitry Andric CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); 4600b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB( 4610b57cec5SDimitry Andric TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); 4620b57cec5SDimitry Andric 463a7dea167SDimitry Andric auto FOut = std::make_unique<formatted_raw_ostream>(*Out); 4640b57cec5SDimitry Andric Str.reset(TheTarget->createAsmStreamer( 4650b57cec5SDimitry Andric Ctx, std::move(FOut), /*asmverbose*/ true, 4660b57cec5SDimitry Andric /*useDwarfDirectory*/ true, IP, std::move(CE), std::move(MAB), 4670b57cec5SDimitry Andric Opts.ShowInst)); 4680b57cec5SDimitry Andric } else if (Opts.OutputType == AssemblerInvocation::FT_Null) { 4690b57cec5SDimitry Andric Str.reset(createNullStreamer(Ctx)); 4700b57cec5SDimitry Andric } else { 4710b57cec5SDimitry Andric assert(Opts.OutputType == AssemblerInvocation::FT_Obj && 4720b57cec5SDimitry Andric "Invalid file type!"); 4730b57cec5SDimitry Andric if (!FDOS->supportsSeeking()) { 474a7dea167SDimitry Andric BOS = std::make_unique<buffer_ostream>(*FDOS); 4750b57cec5SDimitry Andric Out = BOS.get(); 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE( 4790b57cec5SDimitry Andric TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); 4800b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB( 4810b57cec5SDimitry Andric TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); 482e8d8bef9SDimitry Andric assert(MAB && "Unable to create asm backend!"); 483e8d8bef9SDimitry Andric 4840b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> OW = 4850b57cec5SDimitry Andric DwoOS ? MAB->createDwoObjectWriter(*Out, *DwoOS) 4860b57cec5SDimitry Andric : MAB->createObjectWriter(*Out); 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric Triple T(Opts.Triple); 4890b57cec5SDimitry Andric Str.reset(TheTarget->createMCObjectStreamer( 4900b57cec5SDimitry Andric T, Ctx, std::move(MAB), std::move(OW), std::move(CE), *STI, 4910b57cec5SDimitry Andric Opts.RelaxAll, Opts.IncrementalLinkerCompatible, 4920b57cec5SDimitry Andric /*DWARFMustBeAtTheEnd*/ true)); 4930b57cec5SDimitry Andric Str.get()->InitSections(Opts.NoExecStack); 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric // When -fembed-bitcode is passed to clang_as, a 1-byte marker 4970b57cec5SDimitry Andric // is emitted in __LLVM,__asm section if the object file is MachO format. 498*fe6060f1SDimitry Andric if (Opts.EmbedBitcode && Ctx.getObjectFileType() == MCContext::IsMachO) { 4990b57cec5SDimitry Andric MCSection *AsmLabel = Ctx.getMachOSection( 5000b57cec5SDimitry Andric "__LLVM", "__asm", MachO::S_REGULAR, 4, SectionKind::getReadOnly()); 5010b57cec5SDimitry Andric Str.get()->SwitchSection(AsmLabel); 5025ffd83dbSDimitry Andric Str.get()->emitZeros(1); 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric // Assembly to object compilation should leverage assembly info. 5060b57cec5SDimitry Andric Str->setUseAssemblerInfoForParsing(true); 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric bool Failed = false; 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric std::unique_ptr<MCAsmParser> Parser( 5110b57cec5SDimitry Andric createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric // FIXME: init MCTargetOptions from sanitizer flags here. 5140b57cec5SDimitry Andric std::unique_ptr<MCTargetAsmParser> TAP( 5150b57cec5SDimitry Andric TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); 5160b57cec5SDimitry Andric if (!TAP) 5170b57cec5SDimitry Andric Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric // Set values for symbols, if any. 5200b57cec5SDimitry Andric for (auto &S : Opts.SymbolDefs) { 5210b57cec5SDimitry Andric auto Pair = StringRef(S).split('='); 5220b57cec5SDimitry Andric auto Sym = Pair.first; 5230b57cec5SDimitry Andric auto Val = Pair.second; 5240b57cec5SDimitry Andric int64_t Value; 5250b57cec5SDimitry Andric // We have already error checked this in the driver. 5260b57cec5SDimitry Andric Val.getAsInteger(0, Value); 5270b57cec5SDimitry Andric Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value); 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric if (!Failed) { 5310b57cec5SDimitry Andric Parser->setTargetParser(*TAP.get()); 5320b57cec5SDimitry Andric Failed = Parser->Run(Opts.NoInitialTextSection); 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric 535*fe6060f1SDimitry Andric return Failed; 536*fe6060f1SDimitry Andric } 537*fe6060f1SDimitry Andric 538*fe6060f1SDimitry Andric static bool ExecuteAssembler(AssemblerInvocation &Opts, 539*fe6060f1SDimitry Andric DiagnosticsEngine &Diags) { 540*fe6060f1SDimitry Andric bool Failed = ExecuteAssemblerImpl(Opts, Diags); 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Delete output file if there were errors. 5430b57cec5SDimitry Andric if (Failed) { 5440b57cec5SDimitry Andric if (Opts.OutputPath != "-") 5450b57cec5SDimitry Andric sys::fs::remove(Opts.OutputPath); 5460b57cec5SDimitry Andric if (!Opts.SplitDwarfOutput.empty() && Opts.SplitDwarfOutput != "-") 5470b57cec5SDimitry Andric sys::fs::remove(Opts.SplitDwarfOutput); 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric return Failed; 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric static void LLVMErrorHandler(void *UserData, const std::string &Message, 5540b57cec5SDimitry Andric bool GenCrashDiag) { 5550b57cec5SDimitry Andric DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric Diags.Report(diag::err_fe_error_backend) << Message; 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric // We cannot recover from llvm errors. 56013138422SDimitry Andric sys::Process::Exit(1); 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { 5640b57cec5SDimitry Andric // Initialize targets and assembly printers/parsers. 5650b57cec5SDimitry Andric InitializeAllTargetInfos(); 5660b57cec5SDimitry Andric InitializeAllTargetMCs(); 5670b57cec5SDimitry Andric InitializeAllAsmParsers(); 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric // Construct our diagnostic client. 5700b57cec5SDimitry Andric IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 5710b57cec5SDimitry Andric TextDiagnosticPrinter *DiagClient 5720b57cec5SDimitry Andric = new TextDiagnosticPrinter(errs(), &*DiagOpts); 5730b57cec5SDimitry Andric DiagClient->setPrefix("clang -cc1as"); 5740b57cec5SDimitry Andric IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 5750b57cec5SDimitry Andric DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient); 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric // Set an error handler, so that any LLVM backend diagnostics go through our 5780b57cec5SDimitry Andric // error handler. 5790b57cec5SDimitry Andric ScopedFatalErrorHandler FatalErrorHandler 5800b57cec5SDimitry Andric (LLVMErrorHandler, static_cast<void*>(&Diags)); 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric // Parse the arguments. 5830b57cec5SDimitry Andric AssemblerInvocation Asm; 5840b57cec5SDimitry Andric if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags)) 5850b57cec5SDimitry Andric return 1; 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric if (Asm.ShowHelp) { 588*fe6060f1SDimitry Andric getDriverOptTable().printHelp( 589a7dea167SDimitry Andric llvm::outs(), "clang -cc1as [options] file...", 5900b57cec5SDimitry Andric "Clang Integrated Assembler", 5910b57cec5SDimitry Andric /*Include=*/driver::options::CC1AsOption, /*Exclude=*/0, 5920b57cec5SDimitry Andric /*ShowAllAliases=*/false); 5930b57cec5SDimitry Andric return 0; 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric // Honor -version. 5970b57cec5SDimitry Andric // 5980b57cec5SDimitry Andric // FIXME: Use a better -version message? 5990b57cec5SDimitry Andric if (Asm.ShowVersion) { 6000b57cec5SDimitry Andric llvm::cl::PrintVersionMessage(); 6010b57cec5SDimitry Andric return 0; 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric // Honor -mllvm. 6050b57cec5SDimitry Andric // 6060b57cec5SDimitry Andric // FIXME: Remove this, one day. 6070b57cec5SDimitry Andric if (!Asm.LLVMArgs.empty()) { 6080b57cec5SDimitry Andric unsigned NumArgs = Asm.LLVMArgs.size(); 609a7dea167SDimitry Andric auto Args = std::make_unique<const char*[]>(NumArgs + 2); 6100b57cec5SDimitry Andric Args[0] = "clang (LLVM option parsing)"; 6110b57cec5SDimitry Andric for (unsigned i = 0; i != NumArgs; ++i) 6120b57cec5SDimitry Andric Args[i + 1] = Asm.LLVMArgs[i].c_str(); 6130b57cec5SDimitry Andric Args[NumArgs + 1] = nullptr; 6140b57cec5SDimitry Andric llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get()); 6150b57cec5SDimitry Andric } 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric // Execute the invocation, unless there were parsing errors. 6180b57cec5SDimitry Andric bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags); 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric // If any timers were active but haven't been destroyed yet, print their 6210b57cec5SDimitry Andric // results now. 6220b57cec5SDimitry Andric TimerGroup::printAll(errs()); 623a7dea167SDimitry Andric TimerGroup::clearAll(); 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric return !!Failed; 6260b57cec5SDimitry Andric } 627