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