blob: 1733dcd472817a5a0376b9a4efbd16b24f6c1045 [file] [log] [blame]
Eric Astord1f4a962020-01-20 09:18:25 -05001//===-- 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
Eric Astor41692112020-11-30 15:15:18 -050013#include "llvm/ADT/StringSwitch.h"
Eric Astord1f4a962020-01-20 09:18:25 -050014#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"
serge-sans-paille54972d32020-03-04 00:47:43 +010027#include "llvm/MC/MCTargetOptionsCommandFlags.h"
Eric Astor41692112020-11-30 15:15:18 -050028#include "llvm/Option/Arg.h"
29#include "llvm/Option/ArgList.h"
30#include "llvm/Option/Option.h"
Eric Astord1f4a962020-01-20 09:18:25 -050031#include "llvm/Support/Compression.h"
32#include "llvm/Support/FileUtilities.h"
Eric Astor41692112020-11-30 15:15:18 -050033#include "llvm/Support/FormatVariadic.h"
Eric Astord1f4a962020-01-20 09:18:25 -050034#include "llvm/Support/FormattedStream.h"
35#include "llvm/Support/Host.h"
36#include "llvm/Support/InitLLVM.h"
37#include "llvm/Support/MemoryBuffer.h"
Eric Astor41692112020-11-30 15:15:18 -050038#include "llvm/Support/Path.h"
Eric Astord1f4a962020-01-20 09:18:25 -050039#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
45using namespace llvm;
Eric Astor41692112020-11-30 15:15:18 -050046using namespace llvm::opt;
Eric Astord1f4a962020-01-20 09:18:25 -050047
Eric Astor41692112020-11-30 15:15:18 -050048namespace {
serge-sans-paille54972d32020-03-04 00:47:43 +010049
Eric Astor41692112020-11-30 15:15:18 -050050enum 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
Eric Astord1f4a962020-01-20 09:18:25 -050057};
58
Eric Astor41692112020-11-30 15:15:18 -050059#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
60#include "Opts.inc"
61#undef PREFIX
Eric Astord1f4a962020-01-20 09:18:25 -050062
Eric Astor41692112020-11-30 15:15:18 -050063static 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
75class MLOptTable : public opt::OptTable {
76public:
77 MLOptTable() : OptTable(InfoTable, /*IgnoreCase=*/false) {}
78};
79} // namespace
80
81static Triple GetTriple(StringRef ProgName, opt::InputArgList &Args) {
Eric Astord1f4a962020-01-20 09:18:25 -050082 // Figure out the target triple.
Eric Astor41692112020-11-30 15:15:18 -050083 StringRef DefaultBitness = "32";
84 SmallString<255> Program = ProgName;
85 sys::path::replace_extension(Program, "");
86 if (Program.endswith("ml64"))
87 DefaultBitness = "64";
Eric Astord1f4a962020-01-20 09:18:25 -050088
Eric Astor41692112020-11-30 15:15:18 -050089 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));
Eric Astord1f4a962020-01-20 09:18:25 -050095}
96
97static 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
108static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, raw_ostream &OS) {
109 AsmLexer Lexer(MAI);
110 Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer());
Eric Astor4cf04ec2020-09-02 16:03:01 -0400111 Lexer.setLexMasmIntegers(true);
Eric Astor6e1f0c22020-09-29 16:17:47 -0400112 Lexer.useMasmDefaultRadix(true);
Eric Astor32944462020-09-29 16:57:25 -0400113 Lexer.setLexMasmHexFloats(true);
Eric Astorafa57612020-11-04 15:28:22 -0500114 Lexer.setLexMasmStrings(true);
Eric Astord1f4a962020-01-20 09:18:25 -0500115
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
Eric Astor41692112020-11-30 15:15:18 -0500127static int AssembleInput(StringRef ProgName, const Target *TheTarget,
Eric Astord1f4a962020-01-20 09:18:25 -0500128 SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
129 MCAsmInfo &MAI, MCSubtargetInfo &STI,
Eric Astor41692112020-11-30 15:15:18 -0500130 MCInstrInfo &MCII, MCTargetOptions &MCOptions,
131 const opt::ArgList &InputArgs) {
Eric Astor22109fe2020-02-16 12:29:51 -0500132 std::unique_ptr<MCAsmParser> Parser(
Eric Astore378ef92020-09-29 17:02:18 -0400133 createMCMasmParser(SrcMgr, Ctx, Str, MAI, 0));
Eric Astord1f4a962020-01-20 09:18:25 -0500134 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
Eric Astor41692112020-11-30 15:15:18 -0500143 Parser->setShowParsedOperands(InputArgs.hasArg(OPT_show_inst_operands));
Eric Astord1f4a962020-01-20 09:18:25 -0500144 Parser->setTargetParser(*TAP);
145 Parser->getLexer().setLexMasmIntegers(true);
Eric Astor6e1f0c22020-09-29 16:17:47 -0400146 Parser->getLexer().useMasmDefaultRadix(true);
Eric Astor32944462020-09-29 16:57:25 -0400147 Parser->getLexer().setLexMasmHexFloats(true);
Eric Astorafa57612020-11-04 15:28:22 -0500148 Parser->getLexer().setLexMasmStrings(true);
Eric Astord1f4a962020-01-20 09:18:25 -0500149
Eric Astor11346702020-12-01 17:48:49 -0500150 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
Eric Astord1f4a962020-01-20 09:18:25 -0500161 int Res = Parser->Run(/*NoInitialTextSection=*/true);
162
163 return Res;
164}
165
Eric Astor41692112020-11-30 15:15:18 -0500166int main(int Argc, char **Argv) {
167 InitLLVM X(Argc, Argv);
168 StringRef ProgName = sys::path::filename(Argv[0]);
Eric Astord1f4a962020-01-20 09:18:25 -0500169
170 // Initialize targets and assembly printers/parsers.
171 llvm::InitializeAllTargetInfos();
172 llvm::InitializeAllTargetMCs();
173 llvm::InitializeAllAsmParsers();
174 llvm::InitializeAllDisassemblers();
175
Eric Astor41692112020-11-30 15:15:18 -0500176 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);
Eric Astord1f4a962020-01-20 09:18:25 -0500181
Eric Astor41692112020-11-30 15:15:18 -0500182 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;
Eric Astor22109fe2020-02-16 12:29:51 -0500233 MCOptions.AssemblyLanguage = "masm";
Eric Astord1f4a962020-01-20 09:18:25 -0500234
Eric Astor41692112020-11-30 15:15:18 -0500235 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;
Eric Astord1f4a962020-01-20 09:18:25 -0500240 return 1;
Eric Astor41692112020-11-30 15:15:18 -0500241 }
242 const std::string &TripleName = TheTriple.getTriple();
Eric Astord1f4a962020-01-20 09:18:25 -0500243
Eric Astor41692112020-11-30 15:15:18 -0500244 bool SafeSEH = InputArgs.hasArg(OPT_safeseh);
Eric Astore378ef92020-09-29 17:02:18 -0400245 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
Eric Astord1f4a962020-01-20 09:18:25 -0500251 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 }
Eric Astord1f4a962020-01-20 09:18:25 -0500258
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.
Eric Astor41692112020-11-30 15:15:18 -0500266 SrcMgr.setIncludeDirs(InputArgs.getAllArgValues(OPT_include_path));
Eric Astord1f4a962020-01-20 09:18:25 -0500267
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
Eric Astor41692112020-11-30 15:15:18 -0500275 MAI->setPreserveAsmComments(InputArgs.hasArg(OPT_preserve_comments));
Eric Astord1f4a962020-01-20 09:18:25 -0500276
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
Eric Astor41692112020-11-30 15:15:18 -0500284 if (InputArgs.hasArg(OPT_save_temp_labels))
Eric Astord1f4a962020-01-20 09:18:25 -0500285 Ctx.setAllowTemporaryLabels(false);
286
Eric Astor41692112020-11-30 15:15:18 -0500287 // Set compilation information.
288 SmallString<128> CWD;
289 if (!sys::fs::current_path(CWD))
290 Ctx.setCompilationDir(CWD);
291 Ctx.setMainFileName(InputFilename);
Eric Astord1f4a962020-01-20 09:18:25 -0500292
Eric Astor41692112020-11-30 15:15:18 -0500293 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);
Eric Astord1f4a962020-01-20 09:18:25 -0500303 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());
Ella Ma82a3e382020-11-21 21:04:12 -0800312 assert(MCII && "Unable to create instruction info!");
313
Eric Astord1f4a962020-01-20 09:18:25 -0500314 std::unique_ptr<MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo(
315 TripleName, /*CPU=*/"", /*Features=*/""));
Ella Ma82a3e382020-11-21 21:04:12 -0800316 assert(STI && "Unable to create subtarget info!");
Eric Astord1f4a962020-01-20 09:18:25 -0500317
318 MCInstPrinter *IP = nullptr;
Eric Astor41692112020-11-30 15:15:18 -0500319 if (FileType == "s") {
320 const bool OutputATTAsm = InputArgs.hasArg(OPT_output_att_asm);
Eric Astord1f4a962020-01-20 09:18:25 -0500321 const unsigned OutputAsmVariant = OutputATTAsm ? 0U // ATT dialect
322 : 1U; // Intel dialect
Eric Astor41692112020-11-30 15:15:18 -0500323 IP = TheTarget->createMCInstPrinter(TheTriple, OutputAsmVariant, *MAI,
324 *MCII, *MRI);
Eric Astord1f4a962020-01-20 09:18:25 -0500325
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.
Eric Astor41692112020-11-30 15:15:18 -0500335 IP->setPrintImmHex(InputArgs.hasArg(OPT_print_imm_hex));
Eric Astord1f4a962020-01-20 09:18:25 -0500336
337 // Set up the AsmStreamer.
338 std::unique_ptr<MCCodeEmitter> CE;
Eric Astor41692112020-11-30 15:15:18 -0500339 if (InputArgs.hasArg(OPT_show_encoding))
Eric Astord1f4a962020-01-20 09:18:25 -0500340 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);
Eric Astor41692112020-11-30 15:15:18 -0500345 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)));
Eric Astord1f4a962020-01-20 09:18:25 -0500349
Eric Astor41692112020-11-30 15:15:18 -0500350 } else if (FileType == "null") {
Eric Astord1f4a962020-01-20 09:18:25 -0500351 Str.reset(TheTarget->createNullStreamer(Ctx));
Eric Astor41692112020-11-30 15:15:18 -0500352 } else if (FileType == "obj") {
Eric Astord1f4a962020-01-20 09:18:25 -0500353 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));
Eric Astor41692112020-11-30 15:15:18 -0500365 } else {
366 llvm_unreachable("Invalid file type!");
Eric Astord1f4a962020-01-20 09:18:25 -0500367 }
368
Eric Astore378ef92020-09-29 17:02:18 -0400369 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
Eric Astord1f4a962020-01-20 09:18:25 -0500386 // Use Assembler information for parsing.
387 Str->setUseAssemblerInfoForParsing(true);
388
389 int Res = 1;
Eric Astor41692112020-11-30 15:15:18 -0500390 if (InputArgs.hasArg(OPT_as_lex)) {
391 // -as-lex; Lex only, and output a stream of tokens
Eric Astord1f4a962020-01-20 09:18:25 -0500392 Res = AsLexInput(SrcMgr, *MAI, Out->os());
Eric Astor41692112020-11-30 15:15:18 -0500393 } else {
Eric Astord1f4a962020-01-20 09:18:25 -0500394 Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
Eric Astor41692112020-11-30 15:15:18 -0500395 *MCII, MCOptions, InputArgs);
Eric Astord1f4a962020-01-20 09:18:25 -0500396 }
Eric Astord1f4a962020-01-20 09:18:25 -0500397
398 // Keep output if no errors.
399 if (Res == 0)
400 Out->keep();
401 return Res;
402}