Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 13 | #include "llvm/ADT/StringSwitch.h" |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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" |
serge-sans-paille | 54972d3 | 2020-03-04 00:47:43 +0100 | [diff] [blame] | 27 | #include "llvm/MC/MCTargetOptionsCommandFlags.h" |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 28 | #include "llvm/Option/Arg.h" |
| 29 | #include "llvm/Option/ArgList.h" |
| 30 | #include "llvm/Option/Option.h" |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 31 | #include "llvm/Support/Compression.h" |
| 32 | #include "llvm/Support/FileUtilities.h" |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 33 | #include "llvm/Support/FormatVariadic.h" |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 34 | #include "llvm/Support/FormattedStream.h" |
| 35 | #include "llvm/Support/Host.h" |
| 36 | #include "llvm/Support/InitLLVM.h" |
| 37 | #include "llvm/Support/MemoryBuffer.h" |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 38 | #include "llvm/Support/Path.h" |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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; |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 46 | using namespace llvm::opt; |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 47 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 48 | namespace { |
serge-sans-paille | 54972d3 | 2020-03-04 00:47:43 +0100 | [diff] [blame] | 49 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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 |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 59 | #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; |
| 60 | #include "Opts.inc" |
| 61 | #undef PREFIX |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 62 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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) { |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 82 | // Figure out the target triple. |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 83 | StringRef DefaultBitness = "32"; |
| 84 | SmallString<255> Program = ProgName; |
| 85 | sys::path::replace_extension(Program, ""); |
| 86 | if (Program.endswith("ml64")) |
| 87 | DefaultBitness = "64"; |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 88 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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)); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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()); |
Eric Astor | 4cf04ec | 2020-09-02 16:03:01 -0400 | [diff] [blame] | 111 | Lexer.setLexMasmIntegers(true); |
Eric Astor | 6e1f0c2 | 2020-09-29 16:17:47 -0400 | [diff] [blame] | 112 | Lexer.useMasmDefaultRadix(true); |
Eric Astor | 3294446 | 2020-09-29 16:57:25 -0400 | [diff] [blame] | 113 | Lexer.setLexMasmHexFloats(true); |
Eric Astor | afa5761 | 2020-11-04 15:28:22 -0500 | [diff] [blame] | 114 | Lexer.setLexMasmStrings(true); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 127 | static int AssembleInput(StringRef ProgName, const Target *TheTarget, |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 128 | SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str, |
| 129 | MCAsmInfo &MAI, MCSubtargetInfo &STI, |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 130 | MCInstrInfo &MCII, MCTargetOptions &MCOptions, |
| 131 | const opt::ArgList &InputArgs) { |
Eric Astor | 22109fe | 2020-02-16 12:29:51 -0500 | [diff] [blame] | 132 | std::unique_ptr<MCAsmParser> Parser( |
Eric Astor | e378ef9 | 2020-09-29 17:02:18 -0400 | [diff] [blame] | 133 | createMCMasmParser(SrcMgr, Ctx, Str, MAI, 0)); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 143 | Parser->setShowParsedOperands(InputArgs.hasArg(OPT_show_inst_operands)); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 144 | Parser->setTargetParser(*TAP); |
| 145 | Parser->getLexer().setLexMasmIntegers(true); |
Eric Astor | 6e1f0c2 | 2020-09-29 16:17:47 -0400 | [diff] [blame] | 146 | Parser->getLexer().useMasmDefaultRadix(true); |
Eric Astor | 3294446 | 2020-09-29 16:57:25 -0400 | [diff] [blame] | 147 | Parser->getLexer().setLexMasmHexFloats(true); |
Eric Astor | afa5761 | 2020-11-04 15:28:22 -0500 | [diff] [blame] | 148 | Parser->getLexer().setLexMasmStrings(true); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 149 | |
Eric Astor | 1134670 | 2020-12-01 17:48:49 -0500 | [diff] [blame] | 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 | |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 161 | int Res = Parser->Run(/*NoInitialTextSection=*/true); |
| 162 | |
| 163 | return Res; |
| 164 | } |
| 165 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 166 | int main(int Argc, char **Argv) { |
| 167 | InitLLVM X(Argc, Argv); |
| 168 | StringRef ProgName = sys::path::filename(Argv[0]); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 169 | |
| 170 | // Initialize targets and assembly printers/parsers. |
| 171 | llvm::InitializeAllTargetInfos(); |
| 172 | llvm::InitializeAllTargetMCs(); |
| 173 | llvm::InitializeAllAsmParsers(); |
| 174 | llvm::InitializeAllDisassemblers(); |
| 175 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 181 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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; |
Eric Astor | 22109fe | 2020-02-16 12:29:51 -0500 | [diff] [blame] | 233 | MCOptions.AssemblyLanguage = "masm"; |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 234 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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; |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 240 | return 1; |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 241 | } |
| 242 | const std::string &TripleName = TheTriple.getTriple(); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 243 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 244 | bool SafeSEH = InputArgs.hasArg(OPT_safeseh); |
Eric Astor | e378ef9 | 2020-09-29 17:02:18 -0400 | [diff] [blame] | 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 | |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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 | } |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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. |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 266 | SrcMgr.setIncludeDirs(InputArgs.getAllArgValues(OPT_include_path)); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 275 | MAI->setPreserveAsmComments(InputArgs.hasArg(OPT_preserve_comments)); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 284 | if (InputArgs.hasArg(OPT_save_temp_labels)) |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 285 | Ctx.setAllowTemporaryLabels(false); |
| 286 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 287 | // Set compilation information. |
| 288 | SmallString<128> CWD; |
| 289 | if (!sys::fs::current_path(CWD)) |
| 290 | Ctx.setCompilationDir(CWD); |
| 291 | Ctx.setMainFileName(InputFilename); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 292 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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()); |
Ella Ma | 82a3e38 | 2020-11-21 21:04:12 -0800 | [diff] [blame] | 312 | assert(MCII && "Unable to create instruction info!"); |
| 313 | |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 314 | std::unique_ptr<MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo( |
| 315 | TripleName, /*CPU=*/"", /*Features=*/"")); |
Ella Ma | 82a3e38 | 2020-11-21 21:04:12 -0800 | [diff] [blame] | 316 | assert(STI && "Unable to create subtarget info!"); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 317 | |
| 318 | MCInstPrinter *IP = nullptr; |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 319 | if (FileType == "s") { |
| 320 | const bool OutputATTAsm = InputArgs.hasArg(OPT_output_att_asm); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 321 | const unsigned OutputAsmVariant = OutputATTAsm ? 0U // ATT dialect |
| 322 | : 1U; // Intel dialect |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 323 | IP = TheTarget->createMCInstPrinter(TheTriple, OutputAsmVariant, *MAI, |
| 324 | *MCII, *MRI); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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. |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 335 | IP->setPrintImmHex(InputArgs.hasArg(OPT_print_imm_hex)); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 336 | |
| 337 | // Set up the AsmStreamer. |
| 338 | std::unique_ptr<MCCodeEmitter> CE; |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 339 | if (InputArgs.hasArg(OPT_show_encoding)) |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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); |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 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))); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 349 | |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 350 | } else if (FileType == "null") { |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 351 | Str.reset(TheTarget->createNullStreamer(Ctx)); |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 352 | } else if (FileType == "obj") { |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 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)); |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 365 | } else { |
| 366 | llvm_unreachable("Invalid file type!"); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 367 | } |
| 368 | |
Eric Astor | e378ef9 | 2020-09-29 17:02:18 -0400 | [diff] [blame] | 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 | |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 386 | // Use Assembler information for parsing. |
| 387 | Str->setUseAssemblerInfoForParsing(true); |
| 388 | |
| 389 | int Res = 1; |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 390 | if (InputArgs.hasArg(OPT_as_lex)) { |
| 391 | // -as-lex; Lex only, and output a stream of tokens |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 392 | Res = AsLexInput(SrcMgr, *MAI, Out->os()); |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 393 | } else { |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 394 | Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, |
Eric Astor | 4169211 | 2020-11-30 15:15:18 -0500 | [diff] [blame] | 395 | *MCII, MCOptions, InputArgs); |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 396 | } |
Eric Astor | d1f4a96 | 2020-01-20 09:18:25 -0500 | [diff] [blame] | 397 | |
| 398 | // Keep output if no errors. |
| 399 | if (Res == 0) |
| 400 | Out->keep(); |
| 401 | return Res; |
| 402 | } |