llvm-ml.cpp 16 KB

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