xref: /llvm-project/llvm/tools/llvm-ml/llvm-ml.cpp (revision b7065a31b5e7e0bbdae1cbb6cd06fcfe05b7758f)
1 //===-- llvm-ml.cpp - masm-compatible assembler -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A simple driver around MasmParser; based on llvm-mc.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCCodeEmitter.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCInstPrinter.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCObjectFileInfo.h"
21 #include "llvm/MC/MCObjectWriter.h"
22 #include "llvm/MC/MCParser/AsmLexer.h"
23 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
29 #include "llvm/MC/TargetRegistry.h"
30 #include "llvm/Option/Arg.h"
31 #include "llvm/Option/ArgList.h"
32 #include "llvm/Option/Option.h"
33 #include "llvm/Support/Compression.h"
34 #include "llvm/Support/FileUtilities.h"
35 #include "llvm/Support/FormatVariadic.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/Host.h"
38 #include "llvm/Support/InitLLVM.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/Path.h"
41 #include "llvm/Support/Process.h"
42 #include "llvm/Support/SourceMgr.h"
43 #include "llvm/Support/TargetSelect.h"
44 #include "llvm/Support/ToolOutputFile.h"
45 #include "llvm/Support/WithColor.h"
46 #include <ctime>
47 #include <optional>
48 
49 using namespace llvm;
50 using namespace llvm::opt;
51 
52 namespace {
53 
54 enum ID {
55   OPT_INVALID = 0, // This is not an option ID.
56 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
57                HELPTEXT, METAVAR, VALUES)                                      \
58   OPT_##ID,
59 #include "Opts.inc"
60 #undef OPTION
61 };
62 
63 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
64 #include "Opts.inc"
65 #undef PREFIX
66 
67 static constexpr opt::OptTable::Info InfoTable[] = {
68 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
69                HELPTEXT, METAVAR, VALUES)                                      \
70   {                                                                            \
71       PREFIX,      NAME,      HELPTEXT,                                        \
72       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
73       PARAM,       FLAGS,     OPT_##GROUP,                                     \
74       OPT_##ALIAS, ALIASARGS, VALUES},
75 #include "Opts.inc"
76 #undef OPTION
77 };
78 
79 class MLOptTable : public opt::OptTable {
80 public:
81   MLOptTable() : OptTable(InfoTable, /*IgnoreCase=*/false) {}
82 };
83 } // namespace
84 
85 static Triple GetTriple(StringRef ProgName, opt::InputArgList &Args) {
86   // Figure out the target triple.
87   StringRef DefaultBitness = "32";
88   SmallString<255> Program = ProgName;
89   sys::path::replace_extension(Program, "");
90   if (Program.endswith("ml64"))
91     DefaultBitness = "64";
92 
93   StringRef TripleName =
94       StringSwitch<StringRef>(Args.getLastArgValue(OPT_bitness, DefaultBitness))
95           .Case("32", "i386-pc-windows")
96           .Case("64", "x86_64-pc-windows")
97           .Default("");
98   return Triple(Triple::normalize(TripleName));
99 }
100 
101 static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path) {
102   std::error_code EC;
103   auto Out = std::make_unique<ToolOutputFile>(Path, EC, sys::fs::OF_None);
104   if (EC) {
105     WithColor::error() << EC.message() << '\n';
106     return nullptr;
107   }
108 
109   return Out;
110 }
111 
112 static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, raw_ostream &OS) {
113   AsmLexer Lexer(MAI);
114   Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer());
115   Lexer.setLexMasmIntegers(true);
116   Lexer.useMasmDefaultRadix(true);
117   Lexer.setLexMasmHexFloats(true);
118   Lexer.setLexMasmStrings(true);
119 
120   bool Error = false;
121   while (Lexer.Lex().isNot(AsmToken::Eof)) {
122     Lexer.getTok().dump(OS);
123     OS << "\n";
124     if (Lexer.getTok().getKind() == AsmToken::Error)
125       Error = true;
126   }
127 
128   return Error;
129 }
130 
131 static int AssembleInput(StringRef ProgName, const Target *TheTarget,
132                          SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
133                          MCAsmInfo &MAI, MCSubtargetInfo &STI,
134                          MCInstrInfo &MCII, MCTargetOptions &MCOptions,
135                          const opt::ArgList &InputArgs) {
136   struct tm TM;
137   time_t Timestamp;
138   if (InputArgs.hasArg(OPT_timestamp)) {
139     StringRef TimestampStr = InputArgs.getLastArgValue(OPT_timestamp);
140     int64_t IntTimestamp;
141     if (TimestampStr.getAsInteger(10, IntTimestamp)) {
142       WithColor::error(errs(), ProgName)
143           << "invalid timestamp '" << TimestampStr
144           << "'; must be expressed in seconds since the UNIX epoch.\n";
145       return 1;
146     }
147     Timestamp = IntTimestamp;
148   } else {
149     Timestamp = time(nullptr);
150   }
151   if (InputArgs.hasArg(OPT_utc)) {
152     // Not thread-safe.
153     TM = *gmtime(&Timestamp);
154   } else {
155     // Not thread-safe.
156     TM = *localtime(&Timestamp);
157   }
158 
159   std::unique_ptr<MCAsmParser> Parser(
160       createMCMasmParser(SrcMgr, Ctx, Str, MAI, TM, 0));
161   std::unique_ptr<MCTargetAsmParser> TAP(
162       TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions));
163 
164   if (!TAP) {
165     WithColor::error(errs(), ProgName)
166         << "this target does not support assembly parsing.\n";
167     return 1;
168   }
169 
170   Parser->setShowParsedOperands(InputArgs.hasArg(OPT_show_inst_operands));
171   Parser->setTargetParser(*TAP);
172   Parser->getLexer().setLexMasmIntegers(true);
173   Parser->getLexer().useMasmDefaultRadix(true);
174   Parser->getLexer().setLexMasmHexFloats(true);
175   Parser->getLexer().setLexMasmStrings(true);
176 
177   auto Defines = InputArgs.getAllArgValues(OPT_define);
178   for (StringRef Define : Defines) {
179     const auto NameValue = Define.split('=');
180     StringRef Name = NameValue.first, Value = NameValue.second;
181     if (Parser->defineMacro(Name, Value)) {
182       WithColor::error(errs(), ProgName)
183           << "can't define macro '" << Name << "' = '" << Value << "'\n";
184       return 1;
185     }
186   }
187 
188   int Res = Parser->Run(/*NoInitialTextSection=*/true);
189 
190   return Res;
191 }
192 
193 int main(int Argc, char **Argv) {
194   InitLLVM X(Argc, Argv);
195   StringRef ProgName = sys::path::filename(Argv[0]);
196 
197   // Initialize targets and assembly printers/parsers.
198   llvm::InitializeAllTargetInfos();
199   llvm::InitializeAllTargetMCs();
200   llvm::InitializeAllAsmParsers();
201   llvm::InitializeAllDisassemblers();
202 
203   MLOptTable T;
204   unsigned MissingArgIndex, MissingArgCount;
205   ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, Argc - 1);
206   opt::InputArgList InputArgs =
207       T.ParseArgs(ArgsArr, MissingArgIndex, MissingArgCount);
208 
209   std::string InputFilename;
210   for (auto *Arg : InputArgs.filtered(OPT_INPUT)) {
211     std::string ArgString = Arg->getAsString(InputArgs);
212     bool IsFile = false;
213     std::error_code IsFileEC =
214         llvm::sys::fs::is_regular_file(ArgString, IsFile);
215     if (ArgString == "-" || IsFile) {
216       if (!InputFilename.empty()) {
217         WithColor::warning(errs(), ProgName)
218             << "does not support multiple assembly files in one command; "
219             << "ignoring '" << InputFilename << "'\n";
220       }
221       InputFilename = ArgString;
222     } else {
223       std::string Diag;
224       raw_string_ostream OS(Diag);
225       OS << ArgString << ": " << IsFileEC.message();
226 
227       std::string Nearest;
228       if (T.findNearest(ArgString, Nearest) < 2)
229         OS << ", did you mean '" << Nearest << "'?";
230 
231       WithColor::error(errs(), ProgName) << OS.str() << '\n';
232       exit(1);
233     }
234   }
235   for (auto *Arg : InputArgs.filtered(OPT_assembly_file)) {
236     if (!InputFilename.empty()) {
237       WithColor::warning(errs(), ProgName)
238           << "does not support multiple assembly files in one command; "
239           << "ignoring '" << InputFilename << "'\n";
240     }
241     InputFilename = Arg->getValue();
242   }
243 
244   for (auto *Arg : InputArgs.filtered(OPT_unsupported_Group)) {
245     WithColor::warning(errs(), ProgName)
246         << "ignoring unsupported '" << Arg->getOption().getName()
247         << "' option\n";
248   }
249 
250   if (InputArgs.hasArg(OPT_debug)) {
251     DebugFlag = true;
252   }
253   for (auto *Arg : InputArgs.filtered(OPT_debug_only)) {
254     setCurrentDebugTypes(Arg->getValues().data(), Arg->getNumValues());
255   }
256 
257   if (InputArgs.hasArg(OPT_help)) {
258     std::string Usage = llvm::formatv("{0} [ /options ] file", ProgName).str();
259     T.printHelp(outs(), Usage.c_str(), "LLVM MASM Assembler",
260                 /*ShowHidden=*/false);
261     return 0;
262   } else if (InputFilename.empty()) {
263     outs() << "USAGE: " << ProgName << " [ /options ] file\n"
264            << "Run \"" << ProgName << " /?\" or \"" << ProgName
265            << " /help\" for more info.\n";
266     return 0;
267   }
268 
269   MCTargetOptions MCOptions;
270   MCOptions.AssemblyLanguage = "masm";
271   MCOptions.MCFatalWarnings = InputArgs.hasArg(OPT_fatal_warnings);
272 
273   Triple TheTriple = GetTriple(ProgName, InputArgs);
274   std::string Error;
275   const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error);
276   if (!TheTarget) {
277     WithColor::error(errs(), ProgName) << Error;
278     return 1;
279   }
280   const std::string &TripleName = TheTriple.getTriple();
281 
282   bool SafeSEH = InputArgs.hasArg(OPT_safeseh);
283   if (SafeSEH && !(TheTriple.isArch32Bit() && TheTriple.isX86())) {
284     WithColor::warning()
285         << "/safeseh applies only to 32-bit X86 platforms; ignoring.\n";
286     SafeSEH = false;
287   }
288 
289   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
290       MemoryBuffer::getFileOrSTDIN(InputFilename);
291   if (std::error_code EC = BufferPtr.getError()) {
292     WithColor::error(errs(), ProgName)
293         << InputFilename << ": " << EC.message() << '\n';
294     return 1;
295   }
296 
297   SourceMgr SrcMgr;
298 
299   // Tell SrcMgr about this buffer, which is what the parser will pick up.
300   SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
301 
302   // Record the location of the include directories so that the lexer can find
303   // included files later.
304   std::vector<std::string> IncludeDirs =
305       InputArgs.getAllArgValues(OPT_include_path);
306   if (!InputArgs.hasArg(OPT_ignore_include_envvar)) {
307     if (std::optional<std::string> IncludeEnvVar =
308             llvm::sys::Process::GetEnv("INCLUDE")) {
309       SmallVector<StringRef, 8> Dirs;
310       StringRef(*IncludeEnvVar)
311           .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
312       IncludeDirs.reserve(IncludeDirs.size() + Dirs.size());
313       for (StringRef Dir : Dirs)
314         IncludeDirs.push_back(Dir.str());
315     }
316   }
317   SrcMgr.setIncludeDirs(IncludeDirs);
318 
319   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
320   assert(MRI && "Unable to create target register info!");
321 
322   std::unique_ptr<MCAsmInfo> MAI(
323       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
324   assert(MAI && "Unable to create target asm info!");
325 
326   MAI->setPreserveAsmComments(InputArgs.hasArg(OPT_preserve_comments));
327 
328   std::unique_ptr<MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo(
329       TripleName, /*CPU=*/"", /*Features=*/""));
330   assert(STI && "Unable to create subtarget info!");
331 
332   // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
333   // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
334   MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
335   std::unique_ptr<MCObjectFileInfo> MOFI(TheTarget->createMCObjectFileInfo(
336       Ctx, /*PIC=*/false, /*LargeCodeModel=*/true));
337   Ctx.setObjectFileInfo(MOFI.get());
338 
339   if (InputArgs.hasArg(OPT_save_temp_labels))
340     Ctx.setAllowTemporaryLabels(false);
341 
342   // Set compilation information.
343   SmallString<128> CWD;
344   if (!sys::fs::current_path(CWD))
345     Ctx.setCompilationDir(CWD);
346   Ctx.setMainFileName(InputFilename);
347 
348   StringRef FileType = InputArgs.getLastArgValue(OPT_filetype, "obj");
349   SmallString<255> DefaultOutputFilename;
350   if (InputArgs.hasArg(OPT_as_lex)) {
351     DefaultOutputFilename = "-";
352   } else {
353     DefaultOutputFilename = InputFilename;
354     sys::path::replace_extension(DefaultOutputFilename, FileType);
355   }
356   const StringRef OutputFilename =
357       InputArgs.getLastArgValue(OPT_output_file, DefaultOutputFilename);
358   std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename);
359   if (!Out)
360     return 1;
361 
362   std::unique_ptr<buffer_ostream> BOS;
363   raw_pwrite_stream *OS = &Out->os();
364   std::unique_ptr<MCStreamer> Str;
365 
366   std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
367   assert(MCII && "Unable to create instruction info!");
368 
369   MCInstPrinter *IP = nullptr;
370   if (FileType == "s") {
371     const bool OutputATTAsm = InputArgs.hasArg(OPT_output_att_asm);
372     const unsigned OutputAsmVariant = OutputATTAsm ? 0U   // ATT dialect
373                                                    : 1U;  // Intel dialect
374     IP = TheTarget->createMCInstPrinter(TheTriple, OutputAsmVariant, *MAI,
375                                         *MCII, *MRI);
376 
377     if (!IP) {
378       WithColor::error()
379           << "unable to create instruction printer for target triple '"
380           << TheTriple.normalize() << "' with "
381           << (OutputATTAsm ? "ATT" : "Intel") << " assembly variant.\n";
382       return 1;
383     }
384 
385     // Set the display preference for hex vs. decimal immediates.
386     IP->setPrintImmHex(InputArgs.hasArg(OPT_print_imm_hex));
387 
388     // Set up the AsmStreamer.
389     std::unique_ptr<MCCodeEmitter> CE;
390     if (InputArgs.hasArg(OPT_show_encoding))
391       CE.reset(TheTarget->createMCCodeEmitter(*MCII, Ctx));
392 
393     std::unique_ptr<MCAsmBackend> MAB(
394         TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
395     auto FOut = std::make_unique<formatted_raw_ostream>(*OS);
396     Str.reset(TheTarget->createAsmStreamer(
397         Ctx, std::move(FOut), /*asmverbose*/ true,
398         /*useDwarfDirectory*/ true, IP, std::move(CE), std::move(MAB),
399         InputArgs.hasArg(OPT_show_inst)));
400 
401   } else if (FileType == "null") {
402     Str.reset(TheTarget->createNullStreamer(Ctx));
403   } else if (FileType == "obj") {
404     if (!Out->os().supportsSeeking()) {
405       BOS = std::make_unique<buffer_ostream>(Out->os());
406       OS = BOS.get();
407     }
408 
409     MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, Ctx);
410     MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions);
411     Str.reset(TheTarget->createMCObjectStreamer(
412         TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB),
413         MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(CE), *STI,
414         MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
415         /*DWARFMustBeAtTheEnd*/ false));
416   } else {
417     llvm_unreachable("Invalid file type!");
418   }
419 
420   if (TheTriple.isOSBinFormatCOFF()) {
421     // Emit an absolute @feat.00 symbol. This is a features bitfield read by
422     // link.exe.
423     int64_t Feat00Flags = 0x2;
424     if (SafeSEH) {
425       // According to the PE-COFF spec, the LSB of this value marks the object
426       // for "registered SEH".  This means that all SEH handler entry points
427       // must be registered in .sxdata.  Use of any unregistered handlers will
428       // cause the process to terminate immediately.
429       Feat00Flags |= 0x1;
430     }
431     MCSymbol *Feat00Sym = Ctx.getOrCreateSymbol("@feat.00");
432     Feat00Sym->setRedefinable(true);
433     Str->emitSymbolAttribute(Feat00Sym, MCSA_Global);
434     Str->emitAssignment(Feat00Sym, MCConstantExpr::create(Feat00Flags, Ctx));
435   }
436 
437   // Use Assembler information for parsing.
438   Str->setUseAssemblerInfoForParsing(true);
439 
440   int Res = 1;
441   if (InputArgs.hasArg(OPT_as_lex)) {
442     // -as-lex; Lex only, and output a stream of tokens
443     Res = AsLexInput(SrcMgr, *MAI, Out->os());
444   } else {
445     Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
446                         *MCII, MCOptions, InputArgs);
447   }
448 
449   // Keep output if no errors.
450   if (Res == 0)
451     Out->keep();
452   return Res;
453 }
454