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"
2206c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
230b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.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"
39349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
400b57cec5SDimitry Andric #include "llvm/Option/Arg.h"
410b57cec5SDimitry Andric #include "llvm/Option/ArgList.h"
420b57cec5SDimitry Andric #include "llvm/Option/OptTable.h"
430b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
440b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
450b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
460b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.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/TargetSelect.h"
530b57cec5SDimitry Andric #include "llvm/Support/Timer.h"
540b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
5506c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h"
5606c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
570b57cec5SDimitry Andric #include <memory>
58bdd1243dSDimitry Andric #include <optional>
590b57cec5SDimitry Andric #include <system_error>
600b57cec5SDimitry Andric using namespace clang;
610b57cec5SDimitry Andric using namespace clang::driver;
620b57cec5SDimitry Andric using namespace clang::driver::options;
630b57cec5SDimitry Andric using namespace llvm;
640b57cec5SDimitry Andric using namespace llvm::opt;
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric namespace {
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric /// Helper class for representing a single invocation of the assembler.
690b57cec5SDimitry Andric struct AssemblerInvocation {
700b57cec5SDimitry Andric /// @name Target Options
710b57cec5SDimitry Andric /// @{
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric /// The name of the target triple to assemble for.
740b57cec5SDimitry Andric std::string Triple;
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric /// If given, the name of the target CPU to determine which instructions
770b57cec5SDimitry Andric /// are legal.
780b57cec5SDimitry Andric std::string CPU;
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric /// The list of target specific features to enable or disable -- this should
810b57cec5SDimitry Andric /// be a list of strings starting with '+' or '-'.
820b57cec5SDimitry Andric std::vector<std::string> Features;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric /// The list of symbol definitions.
850b57cec5SDimitry Andric std::vector<std::string> SymbolDefs;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric /// @}
880b57cec5SDimitry Andric /// @name Language Options
890b57cec5SDimitry Andric /// @{
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric std::vector<std::string> IncludePaths;
92*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
930b57cec5SDimitry Andric unsigned NoInitialTextSection : 1;
94*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
950b57cec5SDimitry Andric unsigned SaveTemporaryLabels : 1;
96*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
970b57cec5SDimitry Andric unsigned GenDwarfForAssembly : 1;
98*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
990b57cec5SDimitry Andric unsigned RelaxELFRelocations : 1;
100*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
101*0fca6ea1SDimitry Andric unsigned SSE2AVX : 1;
102*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
103fe6060f1SDimitry Andric unsigned Dwarf64 : 1;
1040b57cec5SDimitry Andric unsigned DwarfVersion;
1050b57cec5SDimitry Andric std::string DwarfDebugFlags;
1060b57cec5SDimitry Andric std::string DwarfDebugProducer;
1070b57cec5SDimitry Andric std::string DebugCompilationDir;
10806c3fb27SDimitry Andric llvm::SmallVector<std::pair<std::string, std::string>, 0> DebugPrefixMap;
1090b57cec5SDimitry Andric llvm::DebugCompressionType CompressDebugSections =
1100b57cec5SDimitry Andric llvm::DebugCompressionType::None;
1110b57cec5SDimitry Andric std::string MainFileName;
1120b57cec5SDimitry Andric std::string SplitDwarfOutput;
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric /// @}
1150b57cec5SDimitry Andric /// @name Frontend Options
1160b57cec5SDimitry Andric /// @{
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric std::string InputFile;
1190b57cec5SDimitry Andric std::vector<std::string> LLVMArgs;
1200b57cec5SDimitry Andric std::string OutputPath;
1210b57cec5SDimitry Andric enum FileType {
1220b57cec5SDimitry Andric FT_Asm, ///< Assembly (.s) output, transliterate mode.
1230b57cec5SDimitry Andric FT_Null, ///< No output, for timing purposes.
1240b57cec5SDimitry Andric FT_Obj ///< Object file output.
1250b57cec5SDimitry Andric };
1260b57cec5SDimitry Andric FileType OutputType;
127*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1280b57cec5SDimitry Andric unsigned ShowHelp : 1;
129*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1300b57cec5SDimitry Andric unsigned ShowVersion : 1;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric /// @}
1330b57cec5SDimitry Andric /// @name Transliterate Options
1340b57cec5SDimitry Andric /// @{
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric unsigned OutputAsmVariant;
137*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1380b57cec5SDimitry Andric unsigned ShowEncoding : 1;
139*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1400b57cec5SDimitry Andric unsigned ShowInst : 1;
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric /// @}
1430b57cec5SDimitry Andric /// @name Assembler Options
1440b57cec5SDimitry Andric /// @{
1450b57cec5SDimitry Andric
146*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1470b57cec5SDimitry Andric unsigned RelaxAll : 1;
148*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1490b57cec5SDimitry Andric unsigned NoExecStack : 1;
150*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1510b57cec5SDimitry Andric unsigned FatalWarnings : 1;
152*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
153a7dea167SDimitry Andric unsigned NoWarn : 1;
154*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
155bdd1243dSDimitry Andric unsigned NoTypeCheck : 1;
156*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1570b57cec5SDimitry Andric unsigned IncrementalLinkerCompatible : 1;
158*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
1590b57cec5SDimitry Andric unsigned EmbedBitcode : 1;
1600b57cec5SDimitry Andric
16181ad6265SDimitry Andric /// Whether to emit DWARF unwind info.
16281ad6265SDimitry Andric EmitDwarfUnwindType EmitDwarfUnwind;
16381ad6265SDimitry Andric
16406c3fb27SDimitry Andric // Whether to emit compact-unwind for non-canonical entries.
16506c3fb27SDimitry Andric // Note: maybe overriden by other constraints.
166*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
16706c3fb27SDimitry Andric unsigned EmitCompactUnwindNonCanonical : 1;
16806c3fb27SDimitry Andric
169*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool)
170*0fca6ea1SDimitry Andric unsigned Crel : 1;
171*0fca6ea1SDimitry Andric
1720b57cec5SDimitry Andric /// The name of the relocation model to use.
1730b57cec5SDimitry Andric std::string RelocationModel;
1740b57cec5SDimitry Andric
1750b57cec5SDimitry Andric /// The ABI targeted by the backend. Specified using -target-abi. Empty
1760b57cec5SDimitry Andric /// otherwise.
1770b57cec5SDimitry Andric std::string TargetABI;
1780b57cec5SDimitry Andric
17981ad6265SDimitry Andric /// Darwin target variant triple, the variant of the deployment target
18081ad6265SDimitry Andric /// for which the code is being compiled.
181bdd1243dSDimitry Andric std::optional<llvm::Triple> DarwinTargetVariantTriple;
182bdd1243dSDimitry Andric
183bdd1243dSDimitry Andric /// The version of the darwin target variant SDK which was used during the
184bdd1243dSDimitry Andric /// compilation
185bdd1243dSDimitry Andric llvm::VersionTuple DarwinTargetVariantSDKVersion;
186bdd1243dSDimitry Andric
187bdd1243dSDimitry Andric /// The name of a file to use with \c .secure_log_unique directives.
188bdd1243dSDimitry Andric std::string AsSecureLogFile;
1890b57cec5SDimitry Andric /// @}
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric public:
AssemblerInvocation__anonf0e1379e0111::AssemblerInvocation1920b57cec5SDimitry Andric AssemblerInvocation() {
1930b57cec5SDimitry Andric Triple = "";
1940b57cec5SDimitry Andric NoInitialTextSection = 0;
1950b57cec5SDimitry Andric InputFile = "-";
1960b57cec5SDimitry Andric OutputPath = "-";
1970b57cec5SDimitry Andric OutputType = FT_Asm;
1980b57cec5SDimitry Andric OutputAsmVariant = 0;
1990b57cec5SDimitry Andric ShowInst = 0;
2000b57cec5SDimitry Andric ShowEncoding = 0;
2010b57cec5SDimitry Andric RelaxAll = 0;
202*0fca6ea1SDimitry Andric SSE2AVX = 0;
2030b57cec5SDimitry Andric NoExecStack = 0;
2040b57cec5SDimitry Andric FatalWarnings = 0;
205a7dea167SDimitry Andric NoWarn = 0;
206bdd1243dSDimitry Andric NoTypeCheck = 0;
2070b57cec5SDimitry Andric IncrementalLinkerCompatible = 0;
208fe6060f1SDimitry Andric Dwarf64 = 0;
2090b57cec5SDimitry Andric DwarfVersion = 0;
2100b57cec5SDimitry Andric EmbedBitcode = 0;
21181ad6265SDimitry Andric EmitDwarfUnwind = EmitDwarfUnwindType::Default;
21206c3fb27SDimitry Andric EmitCompactUnwindNonCanonical = false;
213*0fca6ea1SDimitry Andric Crel = false;
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andric static bool CreateFromArgs(AssemblerInvocation &Res,
2170b57cec5SDimitry Andric ArrayRef<const char *> Argv,
2180b57cec5SDimitry Andric DiagnosticsEngine &Diags);
2190b57cec5SDimitry Andric };
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric
CreateFromArgs(AssemblerInvocation & Opts,ArrayRef<const char * > Argv,DiagnosticsEngine & Diags)2230b57cec5SDimitry Andric bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
2240b57cec5SDimitry Andric ArrayRef<const char *> Argv,
2250b57cec5SDimitry Andric DiagnosticsEngine &Diags) {
2260b57cec5SDimitry Andric bool Success = true;
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric // Parse the arguments.
229a7dea167SDimitry Andric const OptTable &OptTbl = getDriverOptTable();
2300b57cec5SDimitry Andric
2315f757f3fSDimitry Andric llvm::opt::Visibility VisibilityMask(options::CC1AsOption);
2320b57cec5SDimitry Andric unsigned MissingArgIndex, MissingArgCount;
2335f757f3fSDimitry Andric InputArgList Args =
2345f757f3fSDimitry Andric OptTbl.ParseArgs(Argv, MissingArgIndex, MissingArgCount, VisibilityMask);
2350b57cec5SDimitry Andric
2360b57cec5SDimitry Andric // Check for missing argument error.
2370b57cec5SDimitry Andric if (MissingArgCount) {
2380b57cec5SDimitry Andric Diags.Report(diag::err_drv_missing_argument)
2390b57cec5SDimitry Andric << Args.getArgString(MissingArgIndex) << MissingArgCount;
2400b57cec5SDimitry Andric Success = false;
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric
2430b57cec5SDimitry Andric // Issue errors on unknown arguments.
2440b57cec5SDimitry Andric for (const Arg *A : Args.filtered(OPT_UNKNOWN)) {
2450b57cec5SDimitry Andric auto ArgString = A->getAsString(Args);
2460b57cec5SDimitry Andric std::string Nearest;
2475f757f3fSDimitry Andric if (OptTbl.findNearest(ArgString, Nearest, VisibilityMask) > 1)
2480b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument) << ArgString;
2490b57cec5SDimitry Andric else
2500b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument_with_suggestion)
2510b57cec5SDimitry Andric << ArgString << Nearest;
2520b57cec5SDimitry Andric Success = false;
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric // Construct the invocation.
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric // Target Options
2580b57cec5SDimitry Andric Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
25981ad6265SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_darwin_target_variant_triple))
26081ad6265SDimitry Andric Opts.DarwinTargetVariantTriple = llvm::Triple(A->getValue());
261bdd1243dSDimitry Andric if (Arg *A = Args.getLastArg(OPT_darwin_target_variant_sdk_version_EQ)) {
262bdd1243dSDimitry Andric VersionTuple Version;
263bdd1243dSDimitry Andric if (Version.tryParse(A->getValue()))
264bdd1243dSDimitry Andric Diags.Report(diag::err_drv_invalid_value)
265bdd1243dSDimitry Andric << A->getAsString(Args) << A->getValue();
266bdd1243dSDimitry Andric else
267bdd1243dSDimitry Andric Opts.DarwinTargetVariantSDKVersion = Version;
268bdd1243dSDimitry Andric }
26981ad6265SDimitry Andric
2705ffd83dbSDimitry Andric Opts.CPU = std::string(Args.getLastArgValue(OPT_target_cpu));
2710b57cec5SDimitry Andric Opts.Features = Args.getAllArgValues(OPT_target_feature);
2720b57cec5SDimitry Andric
2730b57cec5SDimitry Andric // Use the default target triple if unspecified.
2740b57cec5SDimitry Andric if (Opts.Triple.empty())
2750b57cec5SDimitry Andric Opts.Triple = llvm::sys::getDefaultTargetTriple();
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric // Language Options
2780b57cec5SDimitry Andric Opts.IncludePaths = Args.getAllArgValues(OPT_I);
2790b57cec5SDimitry Andric Opts.NoInitialTextSection = Args.hasArg(OPT_n);
2800b57cec5SDimitry Andric Opts.SaveTemporaryLabels = Args.hasArg(OPT_msave_temp_labels);
2810b57cec5SDimitry Andric // Any DebugInfoKind implies GenDwarfForAssembly.
2820b57cec5SDimitry Andric Opts.GenDwarfForAssembly = Args.hasArg(OPT_debug_info_kind_EQ);
2830b57cec5SDimitry Andric
284e8d8bef9SDimitry Andric if (const Arg *A = Args.getLastArg(OPT_compress_debug_sections_EQ)) {
2850b57cec5SDimitry Andric Opts.CompressDebugSections =
2860b57cec5SDimitry Andric llvm::StringSwitch<llvm::DebugCompressionType>(A->getValue())
2870b57cec5SDimitry Andric .Case("none", llvm::DebugCompressionType::None)
288bdd1243dSDimitry Andric .Case("zlib", llvm::DebugCompressionType::Zlib)
289bdd1243dSDimitry Andric .Case("zstd", llvm::DebugCompressionType::Zstd)
2900b57cec5SDimitry Andric .Default(llvm::DebugCompressionType::None);
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric
293bdd1243dSDimitry Andric Opts.RelaxELFRelocations = !Args.hasArg(OPT_mrelax_relocations_no);
294*0fca6ea1SDimitry Andric Opts.SSE2AVX = Args.hasArg(OPT_msse2avx);
295fe6060f1SDimitry Andric if (auto *DwarfFormatArg = Args.getLastArg(OPT_gdwarf64, OPT_gdwarf32))
296fe6060f1SDimitry Andric Opts.Dwarf64 = DwarfFormatArg->getOption().matches(OPT_gdwarf64);
2970b57cec5SDimitry Andric Opts.DwarfVersion = getLastArgIntValue(Args, OPT_dwarf_version_EQ, 2, Diags);
2985ffd83dbSDimitry Andric Opts.DwarfDebugFlags =
2995ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_dwarf_debug_flags));
3005ffd83dbSDimitry Andric Opts.DwarfDebugProducer =
3015ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_dwarf_debug_producer));
302fe6060f1SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
303fe6060f1SDimitry Andric options::OPT_fdebug_compilation_dir_EQ))
304fe6060f1SDimitry Andric Opts.DebugCompilationDir = A->getValue();
3055ffd83dbSDimitry Andric Opts.MainFileName = std::string(Args.getLastArgValue(OPT_main_file_name));
3060b57cec5SDimitry Andric
3075ffd83dbSDimitry Andric for (const auto &Arg : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ)) {
3085ffd83dbSDimitry Andric auto Split = StringRef(Arg).split('=');
30906c3fb27SDimitry Andric Opts.DebugPrefixMap.emplace_back(Split.first, Split.second);
3105ffd83dbSDimitry Andric }
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric // Frontend Options
3130b57cec5SDimitry Andric if (Args.hasArg(OPT_INPUT)) {
3140b57cec5SDimitry Andric bool First = true;
3150b57cec5SDimitry Andric for (const Arg *A : Args.filtered(OPT_INPUT)) {
3160b57cec5SDimitry Andric if (First) {
3170b57cec5SDimitry Andric Opts.InputFile = A->getValue();
3180b57cec5SDimitry Andric First = false;
3190b57cec5SDimitry Andric } else {
3200b57cec5SDimitry Andric Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args);
3210b57cec5SDimitry Andric Success = false;
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
3265ffd83dbSDimitry Andric Opts.OutputPath = std::string(Args.getLastArgValue(OPT_o));
3275ffd83dbSDimitry Andric Opts.SplitDwarfOutput =
3285ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_split_dwarf_output));
3290b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(OPT_filetype)) {
3300b57cec5SDimitry Andric StringRef Name = A->getValue();
3310b57cec5SDimitry Andric unsigned OutputType = StringSwitch<unsigned>(Name)
3320b57cec5SDimitry Andric .Case("asm", FT_Asm)
3330b57cec5SDimitry Andric .Case("null", FT_Null)
3340b57cec5SDimitry Andric .Case("obj", FT_Obj)
3350b57cec5SDimitry Andric .Default(~0U);
3360b57cec5SDimitry Andric if (OutputType == ~0U) {
3370b57cec5SDimitry Andric Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
3380b57cec5SDimitry Andric Success = false;
3390b57cec5SDimitry Andric } else
3400b57cec5SDimitry Andric Opts.OutputType = FileType(OutputType);
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric Opts.ShowHelp = Args.hasArg(OPT_help);
3430b57cec5SDimitry Andric Opts.ShowVersion = Args.hasArg(OPT_version);
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric // Transliterate Options
3460b57cec5SDimitry Andric Opts.OutputAsmVariant =
3470b57cec5SDimitry Andric getLastArgIntValue(Args, OPT_output_asm_variant, 0, Diags);
3480b57cec5SDimitry Andric Opts.ShowEncoding = Args.hasArg(OPT_show_encoding);
3490b57cec5SDimitry Andric Opts.ShowInst = Args.hasArg(OPT_show_inst);
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric // Assemble Options
3520b57cec5SDimitry Andric Opts.RelaxAll = Args.hasArg(OPT_mrelax_all);
3530b57cec5SDimitry Andric Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
3540b57cec5SDimitry Andric Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);
355a7dea167SDimitry Andric Opts.NoWarn = Args.hasArg(OPT_massembler_no_warn);
356bdd1243dSDimitry Andric Opts.NoTypeCheck = Args.hasArg(OPT_mno_type_check);
3575ffd83dbSDimitry Andric Opts.RelocationModel =
3585ffd83dbSDimitry Andric std::string(Args.getLastArgValue(OPT_mrelocation_model, "pic"));
3595ffd83dbSDimitry Andric Opts.TargetABI = std::string(Args.getLastArgValue(OPT_target_abi));
3600b57cec5SDimitry Andric Opts.IncrementalLinkerCompatible =
3610b57cec5SDimitry Andric Args.hasArg(OPT_mincremental_linker_compatible);
3620b57cec5SDimitry Andric Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric // EmbedBitcode Option. If -fembed-bitcode is enabled, set the flag.
3650b57cec5SDimitry Andric // EmbedBitcode behaves the same for all embed options for assembly files.
3660b57cec5SDimitry Andric if (auto *A = Args.getLastArg(OPT_fembed_bitcode_EQ)) {
3670b57cec5SDimitry Andric Opts.EmbedBitcode = llvm::StringSwitch<unsigned>(A->getValue())
3680b57cec5SDimitry Andric .Case("all", 1)
3690b57cec5SDimitry Andric .Case("bitcode", 1)
3700b57cec5SDimitry Andric .Case("marker", 1)
3710b57cec5SDimitry Andric .Default(0);
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
37481ad6265SDimitry Andric if (auto *A = Args.getLastArg(OPT_femit_dwarf_unwind_EQ)) {
37581ad6265SDimitry Andric Opts.EmitDwarfUnwind =
37681ad6265SDimitry Andric llvm::StringSwitch<EmitDwarfUnwindType>(A->getValue())
37781ad6265SDimitry Andric .Case("always", EmitDwarfUnwindType::Always)
37881ad6265SDimitry Andric .Case("no-compact-unwind", EmitDwarfUnwindType::NoCompactUnwind)
37981ad6265SDimitry Andric .Case("default", EmitDwarfUnwindType::Default);
38081ad6265SDimitry Andric }
38181ad6265SDimitry Andric
38206c3fb27SDimitry Andric Opts.EmitCompactUnwindNonCanonical =
38306c3fb27SDimitry Andric Args.hasArg(OPT_femit_compact_unwind_non_canonical);
384*0fca6ea1SDimitry Andric Opts.Crel = Args.hasArg(OPT_crel);
38506c3fb27SDimitry Andric
386bdd1243dSDimitry Andric Opts.AsSecureLogFile = Args.getLastArgValue(OPT_as_secure_log_file);
387bdd1243dSDimitry Andric
3880b57cec5SDimitry Andric return Success;
3890b57cec5SDimitry Andric }
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric static std::unique_ptr<raw_fd_ostream>
getOutputStream(StringRef Path,DiagnosticsEngine & Diags,bool Binary)3920b57cec5SDimitry Andric getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) {
3930b57cec5SDimitry Andric // Make sure that the Out file gets unlinked from the disk if we get a
3940b57cec5SDimitry Andric // SIGINT.
3950b57cec5SDimitry Andric if (Path != "-")
3960b57cec5SDimitry Andric sys::RemoveFileOnSignal(Path);
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric std::error_code EC;
399a7dea167SDimitry Andric auto Out = std::make_unique<raw_fd_ostream>(
400fe6060f1SDimitry Andric Path, EC, (Binary ? sys::fs::OF_None : sys::fs::OF_TextWithCRLF));
4010b57cec5SDimitry Andric if (EC) {
4020b57cec5SDimitry Andric Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
4030b57cec5SDimitry Andric return nullptr;
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andric return Out;
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric
ExecuteAssemblerImpl(AssemblerInvocation & Opts,DiagnosticsEngine & Diags)409fe6060f1SDimitry Andric static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
4100b57cec5SDimitry Andric DiagnosticsEngine &Diags) {
4110b57cec5SDimitry Andric // Get the target specific parser.
4120b57cec5SDimitry Andric std::string Error;
4130b57cec5SDimitry Andric const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
4140b57cec5SDimitry Andric if (!TheTarget)
4150b57cec5SDimitry Andric return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
4160b57cec5SDimitry Andric
4170b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
418fe6060f1SDimitry Andric MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true);
4190b57cec5SDimitry Andric
4200b57cec5SDimitry Andric if (std::error_code EC = Buffer.getError()) {
42106c3fb27SDimitry Andric return Diags.Report(diag::err_fe_error_reading)
42206c3fb27SDimitry Andric << Opts.InputFile << EC.message();
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andric SourceMgr SrcMgr;
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric // Tell SrcMgr about this buffer, which is what the parser will pick up.
4280b57cec5SDimitry Andric unsigned BufferIndex = SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric // Record the location of the include directories so that the lexer can find
4310b57cec5SDimitry Andric // it later.
4320b57cec5SDimitry Andric SrcMgr.setIncludeDirs(Opts.IncludePaths);
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
4350b57cec5SDimitry Andric assert(MRI && "Unable to create target register info!");
4360b57cec5SDimitry Andric
437480093f4SDimitry Andric MCTargetOptions MCOptions;
438*0fca6ea1SDimitry Andric MCOptions.MCRelaxAll = Opts.RelaxAll;
43981ad6265SDimitry Andric MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind;
44006c3fb27SDimitry Andric MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;
441*0fca6ea1SDimitry Andric MCOptions.MCSaveTempLabels = Opts.SaveTemporaryLabels;
442*0fca6ea1SDimitry Andric MCOptions.Crel = Opts.Crel;
443*0fca6ea1SDimitry Andric MCOptions.X86RelaxRelocations = Opts.RelaxELFRelocations;
444*0fca6ea1SDimitry Andric MCOptions.X86Sse2Avx = Opts.SSE2AVX;
445*0fca6ea1SDimitry Andric MCOptions.CompressDebugSections = Opts.CompressDebugSections;
446bdd1243dSDimitry Andric MCOptions.AsSecureLogFile = Opts.AsSecureLogFile;
44781ad6265SDimitry Andric
448480093f4SDimitry Andric std::unique_ptr<MCAsmInfo> MAI(
449480093f4SDimitry Andric TheTarget->createMCAsmInfo(*MRI, Opts.Triple, MCOptions));
4500b57cec5SDimitry Andric assert(MAI && "Unable to create target asm info!");
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andric // Ensure MCAsmInfo initialization occurs before any use, otherwise sections
4530b57cec5SDimitry Andric // may be created with a combination of default and explicit settings.
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric
4560b57cec5SDimitry Andric bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
4570b57cec5SDimitry Andric if (Opts.OutputPath.empty())
4580b57cec5SDimitry Andric Opts.OutputPath = "-";
4590b57cec5SDimitry Andric std::unique_ptr<raw_fd_ostream> FDOS =
4600b57cec5SDimitry Andric getOutputStream(Opts.OutputPath, Diags, IsBinary);
4610b57cec5SDimitry Andric if (!FDOS)
4620b57cec5SDimitry Andric return true;
4630b57cec5SDimitry Andric std::unique_ptr<raw_fd_ostream> DwoOS;
4640b57cec5SDimitry Andric if (!Opts.SplitDwarfOutput.empty())
4650b57cec5SDimitry Andric DwoOS = getOutputStream(Opts.SplitDwarfOutput, Diags, IsBinary);
4660b57cec5SDimitry Andric
467fe6060f1SDimitry Andric // Build up the feature string from the target feature list.
468fe6060f1SDimitry Andric std::string FS = llvm::join(Opts.Features, ",");
4690b57cec5SDimitry Andric
470fe6060f1SDimitry Andric std::unique_ptr<MCSubtargetInfo> STI(
471fe6060f1SDimitry Andric TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
472fe6060f1SDimitry Andric assert(STI && "Unable to create subtarget info!");
473fe6060f1SDimitry Andric
474fe6060f1SDimitry Andric MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr,
475fe6060f1SDimitry Andric &MCOptions);
4760b57cec5SDimitry Andric
4770b57cec5SDimitry Andric bool PIC = false;
4780b57cec5SDimitry Andric if (Opts.RelocationModel == "static") {
4790b57cec5SDimitry Andric PIC = false;
4800b57cec5SDimitry Andric } else if (Opts.RelocationModel == "pic") {
4810b57cec5SDimitry Andric PIC = true;
4820b57cec5SDimitry Andric } else {
4830b57cec5SDimitry Andric assert(Opts.RelocationModel == "dynamic-no-pic" &&
4840b57cec5SDimitry Andric "Invalid PIC model!");
4850b57cec5SDimitry Andric PIC = false;
4860b57cec5SDimitry Andric }
4870b57cec5SDimitry Andric
488fe6060f1SDimitry Andric // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
489fe6060f1SDimitry Andric // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
490fe6060f1SDimitry Andric std::unique_ptr<MCObjectFileInfo> MOFI(
491fe6060f1SDimitry Andric TheTarget->createMCObjectFileInfo(Ctx, PIC));
49281ad6265SDimitry Andric if (Opts.DarwinTargetVariantTriple)
49381ad6265SDimitry Andric MOFI->setDarwinTargetVariantTriple(*Opts.DarwinTargetVariantTriple);
494bdd1243dSDimitry Andric if (!Opts.DarwinTargetVariantSDKVersion.empty())
495bdd1243dSDimitry Andric MOFI->setDarwinTargetVariantSDKVersion(Opts.DarwinTargetVariantSDKVersion);
496fe6060f1SDimitry Andric Ctx.setObjectFileInfo(MOFI.get());
497fe6060f1SDimitry Andric
4980b57cec5SDimitry Andric if (Opts.GenDwarfForAssembly)
4990b57cec5SDimitry Andric Ctx.setGenDwarfForAssembly(true);
5000b57cec5SDimitry Andric if (!Opts.DwarfDebugFlags.empty())
5010b57cec5SDimitry Andric Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
5020b57cec5SDimitry Andric if (!Opts.DwarfDebugProducer.empty())
5030b57cec5SDimitry Andric Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
5040b57cec5SDimitry Andric if (!Opts.DebugCompilationDir.empty())
5050b57cec5SDimitry Andric Ctx.setCompilationDir(Opts.DebugCompilationDir);
5060b57cec5SDimitry Andric else {
5070b57cec5SDimitry Andric // If no compilation dir is set, try to use the current directory.
5080b57cec5SDimitry Andric SmallString<128> CWD;
5090b57cec5SDimitry Andric if (!sys::fs::current_path(CWD))
5100b57cec5SDimitry Andric Ctx.setCompilationDir(CWD);
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric if (!Opts.DebugPrefixMap.empty())
5130b57cec5SDimitry Andric for (const auto &KV : Opts.DebugPrefixMap)
5140b57cec5SDimitry Andric Ctx.addDebugPrefixMapEntry(KV.first, KV.second);
5150b57cec5SDimitry Andric if (!Opts.MainFileName.empty())
5160b57cec5SDimitry Andric Ctx.setMainFileName(StringRef(Opts.MainFileName));
517fe6060f1SDimitry Andric Ctx.setDwarfFormat(Opts.Dwarf64 ? dwarf::DWARF64 : dwarf::DWARF32);
5180b57cec5SDimitry Andric Ctx.setDwarfVersion(Opts.DwarfVersion);
5190b57cec5SDimitry Andric if (Opts.GenDwarfForAssembly)
5200b57cec5SDimitry Andric Ctx.setGenDwarfRootFile(Opts.InputFile,
5210b57cec5SDimitry Andric SrcMgr.getMemoryBuffer(BufferIndex)->getBuffer());
5220b57cec5SDimitry Andric
5230b57cec5SDimitry Andric std::unique_ptr<MCStreamer> Str;
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
526e8d8bef9SDimitry Andric assert(MCII && "Unable to create instruction info!");
527e8d8bef9SDimitry Andric
5280b57cec5SDimitry Andric raw_pwrite_stream *Out = FDOS.get();
5290b57cec5SDimitry Andric std::unique_ptr<buffer_ostream> BOS;
5300b57cec5SDimitry Andric
531a7dea167SDimitry Andric MCOptions.MCNoWarn = Opts.NoWarn;
532a7dea167SDimitry Andric MCOptions.MCFatalWarnings = Opts.FatalWarnings;
533bdd1243dSDimitry Andric MCOptions.MCNoTypeCheck = Opts.NoTypeCheck;
534*0fca6ea1SDimitry Andric MCOptions.ShowMCInst = Opts.ShowInst;
535*0fca6ea1SDimitry Andric MCOptions.AsmVerbose = true;
536*0fca6ea1SDimitry Andric MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory;
5370b57cec5SDimitry Andric MCOptions.ABIName = Opts.TargetABI;
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric // FIXME: There is a bit of code duplication with addPassesToEmitFile.
5400b57cec5SDimitry Andric if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
5410b57cec5SDimitry Andric MCInstPrinter *IP = TheTarget->createMCInstPrinter(
5420b57cec5SDimitry Andric llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI);
5430b57cec5SDimitry Andric
5440b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE;
5450b57cec5SDimitry Andric if (Opts.ShowEncoding)
54681ad6265SDimitry Andric CE.reset(TheTarget->createMCCodeEmitter(*MCII, Ctx));
5470b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB(
5480b57cec5SDimitry Andric TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
5490b57cec5SDimitry Andric
550a7dea167SDimitry Andric auto FOut = std::make_unique<formatted_raw_ostream>(*Out);
551*0fca6ea1SDimitry Andric Str.reset(TheTarget->createAsmStreamer(Ctx, std::move(FOut), IP,
552*0fca6ea1SDimitry Andric std::move(CE), std::move(MAB)));
5530b57cec5SDimitry Andric } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
5540b57cec5SDimitry Andric Str.reset(createNullStreamer(Ctx));
5550b57cec5SDimitry Andric } else {
5560b57cec5SDimitry Andric assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
5570b57cec5SDimitry Andric "Invalid file type!");
5580b57cec5SDimitry Andric if (!FDOS->supportsSeeking()) {
559a7dea167SDimitry Andric BOS = std::make_unique<buffer_ostream>(*FDOS);
5600b57cec5SDimitry Andric Out = BOS.get();
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE(
56481ad6265SDimitry Andric TheTarget->createMCCodeEmitter(*MCII, Ctx));
5650b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB(
5660b57cec5SDimitry Andric TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
567e8d8bef9SDimitry Andric assert(MAB && "Unable to create asm backend!");
568e8d8bef9SDimitry Andric
5690b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> OW =
5700b57cec5SDimitry Andric DwoOS ? MAB->createDwoObjectWriter(*Out, *DwoOS)
5710b57cec5SDimitry Andric : MAB->createObjectWriter(*Out);
5720b57cec5SDimitry Andric
5730b57cec5SDimitry Andric Triple T(Opts.Triple);
5740b57cec5SDimitry Andric Str.reset(TheTarget->createMCObjectStreamer(
575*0fca6ea1SDimitry Andric T, Ctx, std::move(MAB), std::move(OW), std::move(CE), *STI));
576349cc55cSDimitry Andric Str.get()->initSections(Opts.NoExecStack, *STI);
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric
5790b57cec5SDimitry Andric // When -fembed-bitcode is passed to clang_as, a 1-byte marker
5800b57cec5SDimitry Andric // is emitted in __LLVM,__asm section if the object file is MachO format.
581fe6060f1SDimitry Andric if (Opts.EmbedBitcode && Ctx.getObjectFileType() == MCContext::IsMachO) {
5820b57cec5SDimitry Andric MCSection *AsmLabel = Ctx.getMachOSection(
5830b57cec5SDimitry Andric "__LLVM", "__asm", MachO::S_REGULAR, 4, SectionKind::getReadOnly());
58481ad6265SDimitry Andric Str.get()->switchSection(AsmLabel);
5855ffd83dbSDimitry Andric Str.get()->emitZeros(1);
5860b57cec5SDimitry Andric }
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric bool Failed = false;
5890b57cec5SDimitry Andric
5900b57cec5SDimitry Andric std::unique_ptr<MCAsmParser> Parser(
5910b57cec5SDimitry Andric createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
5920b57cec5SDimitry Andric
5930b57cec5SDimitry Andric // FIXME: init MCTargetOptions from sanitizer flags here.
5940b57cec5SDimitry Andric std::unique_ptr<MCTargetAsmParser> TAP(
5950b57cec5SDimitry Andric TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
5960b57cec5SDimitry Andric if (!TAP)
5970b57cec5SDimitry Andric Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
5980b57cec5SDimitry Andric
5990b57cec5SDimitry Andric // Set values for symbols, if any.
6000b57cec5SDimitry Andric for (auto &S : Opts.SymbolDefs) {
6010b57cec5SDimitry Andric auto Pair = StringRef(S).split('=');
6020b57cec5SDimitry Andric auto Sym = Pair.first;
6030b57cec5SDimitry Andric auto Val = Pair.second;
6040b57cec5SDimitry Andric int64_t Value;
6050b57cec5SDimitry Andric // We have already error checked this in the driver.
6060b57cec5SDimitry Andric Val.getAsInteger(0, Value);
6070b57cec5SDimitry Andric Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value);
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric
6100b57cec5SDimitry Andric if (!Failed) {
6110b57cec5SDimitry Andric Parser->setTargetParser(*TAP.get());
6120b57cec5SDimitry Andric Failed = Parser->Run(Opts.NoInitialTextSection);
6130b57cec5SDimitry Andric }
6140b57cec5SDimitry Andric
615fe6060f1SDimitry Andric return Failed;
616fe6060f1SDimitry Andric }
617fe6060f1SDimitry Andric
ExecuteAssembler(AssemblerInvocation & Opts,DiagnosticsEngine & Diags)618fe6060f1SDimitry Andric static bool ExecuteAssembler(AssemblerInvocation &Opts,
619fe6060f1SDimitry Andric DiagnosticsEngine &Diags) {
620fe6060f1SDimitry Andric bool Failed = ExecuteAssemblerImpl(Opts, Diags);
6210b57cec5SDimitry Andric
6220b57cec5SDimitry Andric // Delete output file if there were errors.
6230b57cec5SDimitry Andric if (Failed) {
6240b57cec5SDimitry Andric if (Opts.OutputPath != "-")
6250b57cec5SDimitry Andric sys::fs::remove(Opts.OutputPath);
6260b57cec5SDimitry Andric if (!Opts.SplitDwarfOutput.empty() && Opts.SplitDwarfOutput != "-")
6270b57cec5SDimitry Andric sys::fs::remove(Opts.SplitDwarfOutput);
6280b57cec5SDimitry Andric }
6290b57cec5SDimitry Andric
6300b57cec5SDimitry Andric return Failed;
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric
LLVMErrorHandler(void * UserData,const char * Message,bool GenCrashDiag)633349cc55cSDimitry Andric static void LLVMErrorHandler(void *UserData, const char *Message,
6340b57cec5SDimitry Andric bool GenCrashDiag) {
6350b57cec5SDimitry Andric DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andric Diags.Report(diag::err_fe_error_backend) << Message;
6380b57cec5SDimitry Andric
6390b57cec5SDimitry Andric // We cannot recover from llvm errors.
64013138422SDimitry Andric sys::Process::Exit(1);
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric
cc1as_main(ArrayRef<const char * > Argv,const char * Argv0,void * MainAddr)6430b57cec5SDimitry Andric int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
6440b57cec5SDimitry Andric // Initialize targets and assembly printers/parsers.
6450b57cec5SDimitry Andric InitializeAllTargetInfos();
6460b57cec5SDimitry Andric InitializeAllTargetMCs();
6470b57cec5SDimitry Andric InitializeAllAsmParsers();
6480b57cec5SDimitry Andric
6490b57cec5SDimitry Andric // Construct our diagnostic client.
6500b57cec5SDimitry Andric IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
6510b57cec5SDimitry Andric TextDiagnosticPrinter *DiagClient
6520b57cec5SDimitry Andric = new TextDiagnosticPrinter(errs(), &*DiagOpts);
6530b57cec5SDimitry Andric DiagClient->setPrefix("clang -cc1as");
6540b57cec5SDimitry Andric IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
6550b57cec5SDimitry Andric DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
6560b57cec5SDimitry Andric
6570b57cec5SDimitry Andric // Set an error handler, so that any LLVM backend diagnostics go through our
6580b57cec5SDimitry Andric // error handler.
6590b57cec5SDimitry Andric ScopedFatalErrorHandler FatalErrorHandler
6600b57cec5SDimitry Andric (LLVMErrorHandler, static_cast<void*>(&Diags));
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric // Parse the arguments.
6630b57cec5SDimitry Andric AssemblerInvocation Asm;
6640b57cec5SDimitry Andric if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags))
6650b57cec5SDimitry Andric return 1;
6660b57cec5SDimitry Andric
6670b57cec5SDimitry Andric if (Asm.ShowHelp) {
668fe6060f1SDimitry Andric getDriverOptTable().printHelp(
669a7dea167SDimitry Andric llvm::outs(), "clang -cc1as [options] file...",
6705f757f3fSDimitry Andric "Clang Integrated Assembler", /*ShowHidden=*/false,
6715f757f3fSDimitry Andric /*ShowAllAliases=*/false,
6725f757f3fSDimitry Andric llvm::opt::Visibility(driver::options::CC1AsOption));
6735f757f3fSDimitry Andric
6740b57cec5SDimitry Andric return 0;
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric
6770b57cec5SDimitry Andric // Honor -version.
6780b57cec5SDimitry Andric //
6790b57cec5SDimitry Andric // FIXME: Use a better -version message?
6800b57cec5SDimitry Andric if (Asm.ShowVersion) {
6810b57cec5SDimitry Andric llvm::cl::PrintVersionMessage();
6820b57cec5SDimitry Andric return 0;
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric
6850b57cec5SDimitry Andric // Honor -mllvm.
6860b57cec5SDimitry Andric //
6870b57cec5SDimitry Andric // FIXME: Remove this, one day.
6880b57cec5SDimitry Andric if (!Asm.LLVMArgs.empty()) {
6890b57cec5SDimitry Andric unsigned NumArgs = Asm.LLVMArgs.size();
690a7dea167SDimitry Andric auto Args = std::make_unique<const char*[]>(NumArgs + 2);
6910b57cec5SDimitry Andric Args[0] = "clang (LLVM option parsing)";
6920b57cec5SDimitry Andric for (unsigned i = 0; i != NumArgs; ++i)
6930b57cec5SDimitry Andric Args[i + 1] = Asm.LLVMArgs[i].c_str();
6940b57cec5SDimitry Andric Args[NumArgs + 1] = nullptr;
6950b57cec5SDimitry Andric llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric
6980b57cec5SDimitry Andric // Execute the invocation, unless there were parsing errors.
6990b57cec5SDimitry Andric bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
7000b57cec5SDimitry Andric
7010b57cec5SDimitry Andric // If any timers were active but haven't been destroyed yet, print their
7020b57cec5SDimitry Andric // results now.
7030b57cec5SDimitry Andric TimerGroup::printAll(errs());
704a7dea167SDimitry Andric TimerGroup::clearAll();
7050b57cec5SDimitry Andric
7060b57cec5SDimitry Andric return !!Failed;
7070b57cec5SDimitry Andric }
708