2015-11-04 23:32:32 +01:00
|
|
|
//===-- llvm-mc.cpp - Machine Code Hacking Driver ---------------*- C++ -*-===//
|
2009-06-19 01:04:45 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-06-19 01:05:21 +02:00
|
|
|
// This utility is a simple driver that allows command line hacking on machine
|
|
|
|
// code.
|
2009-06-19 01:04:45 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "Disassembler.h"
|
2011-07-26 01:24:55 +02:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2012-04-16 00:00:22 +02:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2017-10-12 01:34:47 +02:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-09-14 05:02:37 +02:00
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2011-07-11 05:57:24 +02:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2011-07-20 07:58:47 +02:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2018-05-18 20:26:45 +02:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/MC/MCParser/AsmLexer.h"
|
2016-01-27 11:01:28 +01:00
|
|
|
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
|
2011-07-20 07:58:47 +02:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2009-06-24 02:52:40 +02:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-07-09 07:47:46 +02:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2018-04-11 20:49:37 +02:00
|
|
|
#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
|
2009-06-19 01:04:45 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-03-28 22:00:25 +01:00
|
|
|
#include "llvm/Support/Compression.h"
|
2010-08-20 03:07:01 +02:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2009-08-14 05:48:55 +02:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/Host.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2009-06-19 01:04:45 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2011-08-24 20:08:43 +02:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2018-04-22 10:01:35 +02:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2015-11-04 23:32:32 +01:00
|
|
|
|
2009-06-19 01:04:45 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
|
|
|
|
2018-05-21 21:44:54 +02:00
|
|
|
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::value_desc("filename"),
|
|
|
|
cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string> SplitDwarfFile("split-dwarf-file",
|
|
|
|
cl::desc("DWO output filename"),
|
|
|
|
cl::value_desc("filename"));
|
2009-06-19 01:04:45 +02:00
|
|
|
|
2009-08-27 09:56:39 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
ShowEncoding("show-encoding", cl::desc("Show instruction encodings"));
|
|
|
|
|
2016-06-17 19:04:56 +02:00
|
|
|
static cl::opt<bool> RelaxELFRel(
|
|
|
|
"relax-relocations", cl::init(true),
|
|
|
|
cl::desc("Emit R_X86_64_GOTPCRELX instead of R_X86_64_GOTPCREL"));
|
2016-05-29 03:11:00 +02:00
|
|
|
|
2017-06-09 02:40:19 +02:00
|
|
|
static cl::opt<DebugCompressionType> CompressDebugSections(
|
|
|
|
"compress-debug-sections", cl::ValueOptional,
|
|
|
|
cl::init(DebugCompressionType::None),
|
|
|
|
cl::desc("Choose DWARF debug sections compression:"),
|
|
|
|
cl::values(clEnumValN(DebugCompressionType::None, "none", "No compression"),
|
|
|
|
clEnumValN(DebugCompressionType::Z, "zlib",
|
|
|
|
"Use zlib compression"),
|
|
|
|
clEnumValN(DebugCompressionType::GNU, "zlib-gnu",
|
|
|
|
"Use zlib-gnu compression (deprecated)")));
|
2014-03-27 21:45:58 +01:00
|
|
|
|
2010-02-03 19:18:30 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
ShowInst("show-inst", cl::desc("Show internal instruction representation"));
|
|
|
|
|
2010-08-11 08:37:09 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
ShowInstOperands("show-inst-operands",
|
|
|
|
cl::desc("Show instructions operands as parsed"));
|
|
|
|
|
2009-09-20 09:17:49 +02:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
OutputAsmVariant("output-asm-variant",
|
|
|
|
cl::desc("Syntax variant to use for output printing"));
|
|
|
|
|
2014-06-11 22:26:40 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintImmHex("print-imm-hex", cl::init(false),
|
|
|
|
cl::desc("Prefer hex format for immediate values"));
|
|
|
|
|
2015-06-07 03:46:24 +02:00
|
|
|
static cl::list<std::string>
|
|
|
|
DefineSymbol("defsym", cl::desc("Defines a symbol to be an integer constant"));
|
|
|
|
|
2016-07-11 14:42:14 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
PreserveComments("preserve-comments",
|
|
|
|
cl::desc("Preserve Comments in outputted assembly"));
|
|
|
|
|
llvm-mc: Start MCAssembler and MCMachOStreamer.
- Together these form the (Mach-O) back end of the assembler.
- MCAssembler is the actual assembler backend, which is designed to have a
reasonable API. This will eventually grow to support multiple object file
implementations, but for now its Mach-O/i386 only.
- MCMachOStreamer adapts the MCStreamer "actions" API to the MCAssembler API,
e.g. converting the various directives into fragments, managing state like
the current section, and so on.
- llvm-mc will use the new backend via '-filetype=obj', which may eventually
be, but is not yet, since I hear that people like assemblers which actually
assemble.
- The only thing that works at the moment is changing sections. For the time
being I have a Python Mach-O dumping tool in test/scripts so this stuff can
be easily tested, eventually I expect to replace this with a real LLVM tool.
- More doxyments to come.
I assume that since this stuff doesn't touch any of the things which are part of
2.6 that it is ok to put this in not so long before the freeze, but if someone
objects let me know, I can pull it.
llvm-svn: 79612
2009-08-21 11:11:24 +02:00
|
|
|
enum OutputFileType {
|
2010-03-24 00:47:12 +01:00
|
|
|
OFT_Null,
|
llvm-mc: Start MCAssembler and MCMachOStreamer.
- Together these form the (Mach-O) back end of the assembler.
- MCAssembler is the actual assembler backend, which is designed to have a
reasonable API. This will eventually grow to support multiple object file
implementations, but for now its Mach-O/i386 only.
- MCMachOStreamer adapts the MCStreamer "actions" API to the MCAssembler API,
e.g. converting the various directives into fragments, managing state like
the current section, and so on.
- llvm-mc will use the new backend via '-filetype=obj', which may eventually
be, but is not yet, since I hear that people like assemblers which actually
assemble.
- The only thing that works at the moment is changing sections. For the time
being I have a Python Mach-O dumping tool in test/scripts so this stuff can
be easily tested, eventually I expect to replace this with a real LLVM tool.
- More doxyments to come.
I assume that since this stuff doesn't touch any of the things which are part of
2.6 that it is ok to put this in not so long before the freeze, but if someone
objects let me know, I can pull it.
llvm-svn: 79612
2009-08-21 11:11:24 +02:00
|
|
|
OFT_AssemblyFile,
|
|
|
|
OFT_ObjectFile
|
|
|
|
};
|
|
|
|
static cl::opt<OutputFileType>
|
|
|
|
FileType("filetype", cl::init(OFT_AssemblyFile),
|
|
|
|
cl::desc("Choose an output file type:"),
|
|
|
|
cl::values(
|
|
|
|
clEnumValN(OFT_AssemblyFile, "asm",
|
|
|
|
"Emit an assembly ('.s') file"),
|
2010-03-24 00:47:12 +01:00
|
|
|
clEnumValN(OFT_Null, "null",
|
|
|
|
"Don't emit anything (for timing purposes)"),
|
llvm-mc: Start MCAssembler and MCMachOStreamer.
- Together these form the (Mach-O) back end of the assembler.
- MCAssembler is the actual assembler backend, which is designed to have a
reasonable API. This will eventually grow to support multiple object file
implementations, but for now its Mach-O/i386 only.
- MCMachOStreamer adapts the MCStreamer "actions" API to the MCAssembler API,
e.g. converting the various directives into fragments, managing state like
the current section, and so on.
- llvm-mc will use the new backend via '-filetype=obj', which may eventually
be, but is not yet, since I hear that people like assemblers which actually
assemble.
- The only thing that works at the moment is changing sections. For the time
being I have a Python Mach-O dumping tool in test/scripts so this stuff can
be easily tested, eventually I expect to replace this with a real LLVM tool.
- More doxyments to come.
I assume that since this stuff doesn't touch any of the things which are part of
2.6 that it is ok to put this in not so long before the freeze, but if someone
objects let me know, I can pull it.
llvm-svn: 79612
2009-08-21 11:11:24 +02:00
|
|
|
clEnumValN(OFT_ObjectFile, "obj",
|
2016-10-08 21:41:06 +02:00
|
|
|
"Emit a native object ('.o') file")));
|
llvm-mc: Start MCAssembler and MCMachOStreamer.
- Together these form the (Mach-O) back end of the assembler.
- MCAssembler is the actual assembler backend, which is designed to have a
reasonable API. This will eventually grow to support multiple object file
implementations, but for now its Mach-O/i386 only.
- MCMachOStreamer adapts the MCStreamer "actions" API to the MCAssembler API,
e.g. converting the various directives into fragments, managing state like
the current section, and so on.
- llvm-mc will use the new backend via '-filetype=obj', which may eventually
be, but is not yet, since I hear that people like assemblers which actually
assemble.
- The only thing that works at the moment is changing sections. For the time
being I have a Python Mach-O dumping tool in test/scripts so this stuff can
be easily tested, eventually I expect to replace this with a real LLVM tool.
- More doxyments to come.
I assume that since this stuff doesn't touch any of the things which are part of
2.6 that it is ok to put this in not so long before the freeze, but if someone
objects let me know, I can pull it.
llvm-svn: 79612
2009-08-21 11:11:24 +02:00
|
|
|
|
2009-06-21 07:22:37 +02:00
|
|
|
static cl::list<std::string>
|
|
|
|
IncludeDirs("I", cl::desc("Directory of include files"),
|
|
|
|
cl::value_desc("directory"), cl::Prefix);
|
2009-06-19 01:04:45 +02:00
|
|
|
|
2010-03-13 03:20:38 +01:00
|
|
|
static cl::opt<std::string>
|
|
|
|
ArchName("arch", cl::desc("Target arch to assemble for, "
|
2011-06-17 22:35:21 +02:00
|
|
|
"see -version for available targets"));
|
2010-03-13 03:20:38 +01:00
|
|
|
|
2009-07-18 00:38:58 +02:00
|
|
|
static cl::opt<std::string>
|
2009-11-01 23:07:54 +01:00
|
|
|
TripleName("triple", cl::desc("Target triple to assemble for, "
|
2010-03-13 03:20:38 +01:00
|
|
|
"see -version for available targets"));
|
2009-07-18 00:38:58 +02:00
|
|
|
|
2010-10-30 17:57:50 +02:00
|
|
|
static cl::opt<std::string>
|
|
|
|
MCPU("mcpu",
|
|
|
|
cl::desc("Target a specific cpu type (-mcpu=help for details)"),
|
|
|
|
cl::value_desc("cpu-name"),
|
|
|
|
cl::init(""));
|
|
|
|
|
2011-09-07 19:24:38 +02:00
|
|
|
static cl::list<std::string>
|
|
|
|
MAttrs("mattr",
|
|
|
|
cl::CommaSeparated,
|
|
|
|
cl::desc("Target specific attributes (-mattr=help for details)"),
|
|
|
|
cl::value_desc("a1,+a2,-a3,..."));
|
|
|
|
|
2016-05-18 13:58:50 +02:00
|
|
|
static cl::opt<bool> PIC("position-independent",
|
|
|
|
cl::desc("Position independent"), cl::init(false));
|
2011-07-19 08:37:02 +02:00
|
|
|
|
2017-08-02 22:32:26 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
LargeCodeModel("large-code-model",
|
|
|
|
cl::desc("Create cfi directives that assume the code might "
|
|
|
|
"be more than 2gb away"));
|
2011-07-20 09:51:56 +02:00
|
|
|
|
2010-03-13 03:20:57 +01:00
|
|
|
static cl::opt<bool>
|
2011-06-17 22:35:21 +02:00
|
|
|
NoInitialTextSection("n", cl::desc("Don't assume assembly file starts "
|
|
|
|
"in the text section"));
|
2010-03-13 03:20:57 +01:00
|
|
|
|
2011-11-01 23:27:22 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
GenDwarfForAssembly("g", cl::desc("Generate dwarf debugging info for assembly "
|
|
|
|
"source files"));
|
|
|
|
|
2012-12-17 22:32:42 +01:00
|
|
|
static cl::opt<std::string>
|
|
|
|
DebugCompilationDir("fdebug-compilation-dir",
|
|
|
|
cl::desc("Specifies the debug info's compilation dir"));
|
|
|
|
|
2018-07-10 16:41:54 +02:00
|
|
|
static cl::list<std::string>
|
|
|
|
DebugPrefixMap("fdebug-prefix-map",
|
|
|
|
cl::desc("Map file source paths in debug info"),
|
|
|
|
cl::value_desc("= separated key-value pairs"));
|
|
|
|
|
2012-12-18 01:31:01 +01:00
|
|
|
static cl::opt<std::string>
|
|
|
|
MainFileName("main-file-name",
|
|
|
|
cl::desc("Specifies the name we should consider the input file"));
|
|
|
|
|
2014-05-21 02:20:01 +02:00
|
|
|
static cl::opt<bool> SaveTempLabels("save-temp-labels",
|
|
|
|
cl::desc("Don't discard temporary labels"));
|
|
|
|
|
2014-05-21 23:05:09 +02:00
|
|
|
static cl::opt<bool> NoExecStack("no-exec-stack",
|
|
|
|
cl::desc("File doesn't need an exec stack"));
|
|
|
|
|
2009-06-21 07:22:37 +02:00
|
|
|
enum ActionType {
|
2009-06-21 22:16:42 +02:00
|
|
|
AC_AsLex,
|
2009-12-17 02:49:59 +01:00
|
|
|
AC_Assemble,
|
2010-04-12 21:43:00 +02:00
|
|
|
AC_Disassemble,
|
2012-12-05 19:13:19 +01:00
|
|
|
AC_MDisassemble,
|
2009-06-21 07:22:37 +02:00
|
|
|
};
|
2009-06-19 01:04:45 +02:00
|
|
|
|
2009-06-21 07:22:37 +02:00
|
|
|
static cl::opt<ActionType>
|
|
|
|
Action(cl::desc("Action to perform:"),
|
2009-06-21 22:16:42 +02:00
|
|
|
cl::init(AC_Assemble),
|
|
|
|
cl::values(clEnumValN(AC_AsLex, "as-lex",
|
|
|
|
"Lex tokens from a .s file"),
|
|
|
|
clEnumValN(AC_Assemble, "assemble",
|
2009-06-21 07:22:37 +02:00
|
|
|
"Assemble a .s file (default)"),
|
2009-12-17 02:49:59 +01:00
|
|
|
clEnumValN(AC_Disassemble, "disassemble",
|
|
|
|
"Disassemble strings of hex bytes"),
|
2012-10-23 00:31:46 +02:00
|
|
|
clEnumValN(AC_MDisassemble, "mdis",
|
2016-10-08 21:41:06 +02:00
|
|
|
"Marked up disassembly of strings of hex bytes")));
|
2009-06-19 01:04:45 +02:00
|
|
|
|
2009-09-04 23:45:34 +02:00
|
|
|
static const Target *GetTarget(const char *ProgName) {
|
2010-03-13 03:20:38 +01:00
|
|
|
// Figure out the target triple.
|
|
|
|
if (TripleName.empty())
|
2011-11-01 22:32:20 +01:00
|
|
|
TripleName = sys::getDefaultTargetTriple();
|
2011-07-26 21:02:16 +02:00
|
|
|
Triple TheTriple(Triple::normalize(TripleName));
|
|
|
|
|
2012-05-09 01:38:45 +02:00
|
|
|
// Get the target specific parser.
|
|
|
|
std::string Error;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
|
|
|
|
Error);
|
|
|
|
if (!TheTarget) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error(errs(), ProgName) << Error;
|
2014-04-25 06:24:47 +02:00
|
|
|
return nullptr;
|
2010-03-13 03:20:38 +01:00
|
|
|
}
|
|
|
|
|
2012-05-09 01:38:45 +02:00
|
|
|
// Update the triple name and return the found target.
|
2011-07-26 21:02:16 +02:00
|
|
|
TripleName = TheTriple.getTriple();
|
|
|
|
return TheTarget;
|
2009-09-04 23:45:34 +02:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:44:54 +02:00
|
|
|
static std::unique_ptr<ToolOutputFile> GetOutputStream(StringRef Path) {
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
2018-05-21 21:44:54 +02:00
|
|
|
auto Out = llvm::make_unique<ToolOutputFile>(Path, EC, sys::fs::F_None);
|
2014-08-25 20:16:47 +02:00
|
|
|
if (EC) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error() << EC.message() << '\n';
|
2014-04-25 06:24:47 +02:00
|
|
|
return nullptr;
|
2010-08-20 03:07:01 +02:00
|
|
|
}
|
2010-09-01 16:20:41 +02:00
|
|
|
|
|
|
|
return Out;
|
2010-08-20 03:07:01 +02:00
|
|
|
}
|
|
|
|
|
2011-12-09 19:09:40 +01:00
|
|
|
static std::string DwarfDebugFlags;
|
|
|
|
static void setDwarfDebugFlags(int argc, char **argv) {
|
|
|
|
if (!getenv("RC_DEBUG_OPTIONS"))
|
|
|
|
return;
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
DwarfDebugFlags += argv[i];
|
|
|
|
if (i + 1 < argc)
|
|
|
|
DwarfDebugFlags += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 18:46:23 +01:00
|
|
|
static std::string DwarfDebugProducer;
|
2015-11-04 23:32:32 +01:00
|
|
|
static void setDwarfDebugProducer() {
|
2013-01-16 18:46:23 +01:00
|
|
|
if(!getenv("DEBUG_PRODUCER"))
|
|
|
|
return;
|
|
|
|
DwarfDebugProducer += getenv("DEBUG_PRODUCER");
|
|
|
|
}
|
|
|
|
|
2014-06-19 08:22:01 +02:00
|
|
|
static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI,
|
2014-12-12 08:52:19 +01:00
|
|
|
raw_ostream &OS) {
|
2009-09-04 23:45:34 +02:00
|
|
|
|
2012-04-16 13:32:10 +02:00
|
|
|
AsmLexer Lexer(MAI);
|
2014-07-06 16:17:29 +02:00
|
|
|
Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer());
|
2010-07-18 20:31:28 +02:00
|
|
|
|
2009-06-21 22:16:42 +02:00
|
|
|
bool Error = false;
|
2009-07-28 18:56:42 +02:00
|
|
|
while (Lexer.Lex().isNot(AsmToken::Eof)) {
|
2018-03-06 15:02:14 +01:00
|
|
|
Lexer.getTok().dump(OS);
|
|
|
|
OS << "\n";
|
|
|
|
if (Lexer.getTok().getKind() == AsmToken::Error)
|
2009-06-21 22:16:42 +02:00
|
|
|
Error = true;
|
2009-06-21 09:19:10 +02:00
|
|
|
}
|
2009-08-11 06:34:48 +02:00
|
|
|
|
2010-08-20 03:07:01 +02:00
|
|
|
return Error;
|
2009-08-11 06:34:48 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:42:04 +01:00
|
|
|
static int fillCommandLineSymbols(MCAsmParser &Parser) {
|
2016-12-06 03:49:17 +01:00
|
|
|
for (auto &I: DefineSymbol) {
|
|
|
|
auto Pair = StringRef(I).split('=');
|
|
|
|
auto Sym = Pair.first;
|
|
|
|
auto Val = Pair.second;
|
|
|
|
|
|
|
|
if (Sym.empty() || Val.empty()) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error() << "defsym must be of the form: sym=value: " << I
|
|
|
|
<< "\n";
|
2016-12-06 03:49:17 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int64_t Value;
|
|
|
|
if (Val.getAsInteger(0, Value)) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error() << "value is not an integer: " << Val << "\n";
|
2015-06-07 03:46:24 +02:00
|
|
|
return 1;
|
2016-12-06 03:49:17 +01:00
|
|
|
}
|
|
|
|
Parser.getContext().setSymbolValue(Parser.getStreamer(), Sym, Value);
|
|
|
|
}
|
2015-06-07 03:46:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-22 04:12:43 +01:00
|
|
|
static int AssembleInput(const char *ProgName, const Target *TheTarget,
|
2012-04-16 13:32:10 +02:00
|
|
|
SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
|
2014-06-19 08:22:01 +02:00
|
|
|
MCAsmInfo &MAI, MCSubtargetInfo &STI,
|
2014-06-19 08:22:08 +02:00
|
|
|
MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
|
2014-04-23 13:16:03 +02:00
|
|
|
std::unique_ptr<MCAsmParser> Parser(
|
|
|
|
createMCAsmParser(SrcMgr, Ctx, Str, MAI));
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<MCTargetAsmParser> TAP(
|
2014-06-19 08:22:08 +02:00
|
|
|
TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions));
|
|
|
|
|
2012-04-16 13:32:10 +02:00
|
|
|
if (!TAP) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error(errs(), ProgName)
|
|
|
|
<< "this target does not support assembly parsing.\n";
|
2012-04-16 13:32:10 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-07 03:46:24 +02:00
|
|
|
int SymbolResult = fillCommandLineSymbols(*Parser);
|
|
|
|
if(SymbolResult)
|
|
|
|
return SymbolResult;
|
2012-04-16 13:32:10 +02:00
|
|
|
Parser->setShowParsedOperands(ShowInstOperands);
|
2014-12-12 08:52:19 +01:00
|
|
|
Parser->setTargetParser(*TAP);
|
2012-04-16 13:32:10 +02:00
|
|
|
|
|
|
|
int Res = Parser->Run(NoInitialTextSection);
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2018-04-13 20:26:06 +02:00
|
|
|
InitLLVM X(argc, argv);
|
2012-04-16 13:32:10 +02:00
|
|
|
|
|
|
|
// Initialize targets and assembly printers/parsers.
|
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllAsmParsers();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
|
|
|
// Register the target printer for --version.
|
|
|
|
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
|
2014-06-19 08:22:08 +02:00
|
|
|
MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
|
2012-04-16 13:32:10 +02:00
|
|
|
setDwarfDebugFlags(argc, argv);
|
|
|
|
|
2013-01-16 18:46:23 +01:00
|
|
|
setDwarfDebugProducer();
|
|
|
|
|
2012-04-16 13:32:10 +02:00
|
|
|
const char *ProgName = argv[0];
|
2009-08-14 05:48:55 +02:00
|
|
|
const Target *TheTarget = GetTarget(ProgName);
|
|
|
|
if (!TheTarget)
|
|
|
|
return 1;
|
2015-06-16 11:57:38 +02:00
|
|
|
// Now that GetTarget() has (potentially) replaced TripleName, it's safe to
|
|
|
|
// construct the Triple object.
|
|
|
|
Triple TheTriple(TripleName);
|
2009-08-14 05:48:55 +02:00
|
|
|
|
2014-07-06 19:43:13 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFilename);
|
|
|
|
if (std::error_code EC = BufferPtr.getError()) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error(errs(), ProgName)
|
|
|
|
<< InputFilename << ": " << EC.message() << '\n';
|
2009-06-21 22:16:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2014-08-21 22:44:56 +02:00
|
|
|
MemoryBuffer *Buffer = BufferPtr->get();
|
2011-05-09 22:05:25 +02:00
|
|
|
|
2009-06-21 22:16:42 +02:00
|
|
|
SourceMgr SrcMgr;
|
2011-05-09 22:05:25 +02:00
|
|
|
|
2009-08-19 18:25:53 +02:00
|
|
|
// Tell SrcMgr about this buffer, which is what the parser will pick up.
|
2014-08-21 22:44:56 +02:00
|
|
|
SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
|
2011-05-09 22:05:25 +02:00
|
|
|
|
2009-06-21 22:16:42 +02:00
|
|
|
// Record the location of the include directories so that the lexer can find
|
|
|
|
// it later.
|
|
|
|
SrcMgr.setIncludeDirs(IncludeDirs);
|
2011-05-09 22:05:25 +02:00
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
|
2011-07-18 22:57:22 +02:00
|
|
|
assert(MRI && "Unable to create target register info!");
|
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
|
2013-05-13 03:16:13 +02:00
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
2016-05-29 03:11:00 +02:00
|
|
|
MAI->setRelaxELFRelocations(RelaxELFRel);
|
|
|
|
|
2017-06-09 02:40:19 +02:00
|
|
|
if (CompressDebugSections != DebugCompressionType::None) {
|
2014-03-28 21:45:24 +01:00
|
|
|
if (!zlib::isAvailable()) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error(errs(), ProgName)
|
|
|
|
<< "build tools with zlib to enable -compress-debug-sections";
|
2014-03-28 21:45:24 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-27 14:27:32 +02:00
|
|
|
MAI->setCompressDebugSections(CompressDebugSections);
|
2014-03-28 21:45:24 +01:00
|
|
|
}
|
2016-07-11 14:42:14 +02:00
|
|
|
MAI->setPreserveAsmComments(PreserveComments);
|
2014-03-27 21:45:58 +01:00
|
|
|
|
2011-07-20 07:58:47 +02:00
|
|
|
// FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
|
|
|
|
// MCObjectFileInfo needs a MCContext reference in order to initialize itself.
|
2014-06-28 19:46:19 +02:00
|
|
|
MCObjectFileInfo MOFI;
|
|
|
|
MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
|
2017-08-02 22:32:26 +02:00
|
|
|
MOFI.InitMCObjectFileInfo(TheTriple, PIC, Ctx, LargeCodeModel);
|
2011-07-20 07:58:47 +02:00
|
|
|
|
2011-03-29 00:49:15 +02:00
|
|
|
if (SaveTempLabels)
|
|
|
|
Ctx.setAllowTemporaryLabels(false);
|
2010-12-10 08:39:47 +01:00
|
|
|
|
2011-11-01 23:27:22 +01:00
|
|
|
Ctx.setGenDwarfForAssembly(GenDwarfForAssembly);
|
2014-06-19 08:22:08 +02:00
|
|
|
// Default to 4 for dwarf version.
|
|
|
|
unsigned DwarfVersion = MCOptions.DwarfVersion ? MCOptions.DwarfVersion : 4;
|
2017-03-01 00:40:46 +01:00
|
|
|
if (DwarfVersion < 2 || DwarfVersion > 5) {
|
2014-05-01 10:46:02 +02:00
|
|
|
errs() << ProgName << ": Dwarf version " << DwarfVersion
|
|
|
|
<< " is not supported." << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Ctx.setDwarfVersion(DwarfVersion);
|
2012-12-17 22:32:42 +01:00
|
|
|
if (!DwarfDebugFlags.empty())
|
2011-12-09 19:09:40 +01:00
|
|
|
Ctx.setDwarfDebugFlags(StringRef(DwarfDebugFlags));
|
2013-01-16 18:46:23 +01:00
|
|
|
if (!DwarfDebugProducer.empty())
|
|
|
|
Ctx.setDwarfDebugProducer(StringRef(DwarfDebugProducer));
|
2012-12-17 22:32:42 +01:00
|
|
|
if (!DebugCompilationDir.empty())
|
|
|
|
Ctx.setCompilationDir(DebugCompilationDir);
|
2016-03-22 23:24:29 +01:00
|
|
|
else {
|
|
|
|
// If no compilation dir is set, try to use the current directory.
|
|
|
|
SmallString<128> CWD;
|
|
|
|
if (!sys::fs::current_path(CWD))
|
|
|
|
Ctx.setCompilationDir(CWD);
|
|
|
|
}
|
2018-07-10 16:41:54 +02:00
|
|
|
for (const auto &Arg : DebugPrefixMap) {
|
|
|
|
const auto &KV = StringRef(Arg).split('=');
|
|
|
|
Ctx.addDebugPrefixMapEntry(KV.first, KV.second);
|
|
|
|
}
|
2012-12-18 01:31:01 +01:00
|
|
|
if (!MainFileName.empty())
|
|
|
|
Ctx.setMainFileName(MainFileName);
|
2018-06-12 18:09:03 +02:00
|
|
|
if (GenDwarfForAssembly && DwarfVersion >= 5) {
|
2018-03-29 19:16:41 +02:00
|
|
|
// DWARF v5 needs the root file as well as the compilation directory.
|
|
|
|
// If we find a '.file 0' directive that will supersede these values.
|
|
|
|
MD5 Hash;
|
|
|
|
MD5::MD5Result *Cksum =
|
|
|
|
(MD5::MD5Result *)Ctx.allocate(sizeof(MD5::MD5Result), 1);
|
|
|
|
Hash.update(Buffer->getBuffer());
|
|
|
|
Hash.final(*Cksum);
|
|
|
|
Ctx.setMCLineTableRootFile(
|
|
|
|
/*CUID=*/0, Ctx.getCompilationDir(),
|
|
|
|
!MainFileName.empty() ? MainFileName : InputFilename, Cksum, None);
|
|
|
|
}
|
2011-11-01 23:27:22 +01:00
|
|
|
|
2011-09-07 19:24:38 +02:00
|
|
|
// Package up features to be passed to target/subtarget
|
|
|
|
std::string FeaturesStr;
|
|
|
|
if (MAttrs.size()) {
|
|
|
|
SubtargetFeatures Features;
|
|
|
|
for (unsigned i = 0; i != MAttrs.size(); ++i)
|
|
|
|
Features.AddFeature(MAttrs[i]);
|
|
|
|
FeaturesStr = Features.getString();
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:44:54 +02:00
|
|
|
std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OutputFilename);
|
2010-08-20 03:07:01 +02:00
|
|
|
if (!Out)
|
|
|
|
return 1;
|
|
|
|
|
2018-05-21 21:44:54 +02:00
|
|
|
std::unique_ptr<ToolOutputFile> DwoOut;
|
|
|
|
if (!SplitDwarfFile.empty()) {
|
|
|
|
if (FileType != OFT_ObjectFile) {
|
|
|
|
WithColor::error() << "dwo output only supported with object files\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
DwoOut = GetOutputStream(SplitDwarfFile);
|
|
|
|
if (!DwoOut)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:14:34 +02:00
|
|
|
std::unique_ptr<buffer_ostream> BOS;
|
|
|
|
raw_pwrite_stream *OS = &Out->os();
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<MCStreamer> Str;
|
2009-08-17 06:23:44 +02:00
|
|
|
|
2014-03-06 06:51:42 +01:00
|
|
|
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
|
|
|
|
std::unique_ptr<MCSubtargetInfo> STI(
|
|
|
|
TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
|
2011-07-09 07:47:46 +02:00
|
|
|
|
2014-04-25 06:24:47 +02:00
|
|
|
MCInstPrinter *IP = nullptr;
|
2009-09-04 23:45:34 +02:00
|
|
|
if (FileType == OFT_AssemblyFile) {
|
2015-09-15 18:17:27 +02:00
|
|
|
IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant,
|
|
|
|
*MAI, *MCII, *MRI);
|
2014-06-11 22:26:40 +02:00
|
|
|
|
2016-10-31 19:36:31 +01:00
|
|
|
if (!IP) {
|
2018-04-22 10:01:35 +02:00
|
|
|
WithColor::error()
|
|
|
|
<< "unable to create instruction printer for target triple '"
|
2016-10-31 19:36:31 +01:00
|
|
|
<< TheTriple.normalize() << "' with assembly variant "
|
|
|
|
<< OutputAsmVariant << ".\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:26:40 +02:00
|
|
|
// Set the display preference for hex vs. decimal immediates.
|
|
|
|
IP->setPrintImmHex(PrintImmHex);
|
|
|
|
|
|
|
|
// Set up the AsmStreamer.
|
2018-04-27 17:45:54 +02:00
|
|
|
std::unique_ptr<MCCodeEmitter> CE;
|
|
|
|
if (ShowEncoding)
|
|
|
|
CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
|
|
|
|
|
|
|
|
std::unique_ptr<MCAsmBackend> MAB(
|
|
|
|
TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
|
2015-04-15 00:14:34 +02:00
|
|
|
auto FOut = llvm::make_unique<formatted_raw_ostream>(*OS);
|
2018-04-27 17:45:54 +02:00
|
|
|
Str.reset(
|
|
|
|
TheTarget->createAsmStreamer(Ctx, std::move(FOut), /*asmverbose*/ true,
|
|
|
|
/*useDwarfDirectory*/ true, IP,
|
|
|
|
std::move(CE), std::move(MAB), ShowInst));
|
2011-12-06 00:20:14 +01:00
|
|
|
|
2010-03-24 00:47:12 +01:00
|
|
|
} else if (FileType == OFT_Null) {
|
2015-02-19 01:45:04 +01:00
|
|
|
Str.reset(TheTarget->createNullStreamer(Ctx));
|
llvm-mc: Start MCAssembler and MCMachOStreamer.
- Together these form the (Mach-O) back end of the assembler.
- MCAssembler is the actual assembler backend, which is designed to have a
reasonable API. This will eventually grow to support multiple object file
implementations, but for now its Mach-O/i386 only.
- MCMachOStreamer adapts the MCStreamer "actions" API to the MCAssembler API,
e.g. converting the various directives into fragments, managing state like
the current section, and so on.
- llvm-mc will use the new backend via '-filetype=obj', which may eventually
be, but is not yet, since I hear that people like assemblers which actually
assemble.
- The only thing that works at the moment is changing sections. For the time
being I have a Python Mach-O dumping tool in test/scripts so this stuff can
be easily tested, eventually I expect to replace this with a real LLVM tool.
- More doxyments to come.
I assume that since this stuff doesn't touch any of the things which are part of
2.6 that it is ok to put this in not so long before the freeze, but if someone
objects let me know, I can pull it.
llvm-svn: 79612
2009-08-21 11:11:24 +02:00
|
|
|
} else {
|
|
|
|
assert(FileType == OFT_ObjectFile && "Invalid file type!");
|
2015-04-15 00:14:34 +02:00
|
|
|
|
2015-06-17 18:26:47 +02:00
|
|
|
// Don't waste memory on names of temp labels.
|
|
|
|
Ctx.setUseNamesOnTempLabels(false);
|
|
|
|
|
2015-04-15 00:14:34 +02:00
|
|
|
if (!Out->os().supportsSeeking()) {
|
|
|
|
BOS = make_unique<buffer_ostream>(Out->os());
|
|
|
|
OS = BOS.get();
|
|
|
|
}
|
|
|
|
|
2015-03-10 23:03:14 +01:00
|
|
|
MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
|
2018-01-03 09:53:05 +01:00
|
|
|
MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions);
|
2015-12-21 23:09:27 +01:00
|
|
|
Str.reset(TheTarget->createMCObjectStreamer(
|
2018-05-18 20:26:45 +02:00
|
|
|
TheTriple, Ctx, std::unique_ptr<MCAsmBackend>(MAB),
|
2018-05-21 21:44:54 +02:00
|
|
|
DwoOut ? MAB->createDwoObjectWriter(*OS, DwoOut->os())
|
|
|
|
: MAB->createObjectWriter(*OS),
|
|
|
|
std::unique_ptr<MCCodeEmitter>(CE), *STI, MCOptions.MCRelaxAll,
|
|
|
|
MCOptions.MCIncrementalLinkerCompatible,
|
2015-12-21 23:09:27 +01:00
|
|
|
/*DWARFMustBeAtTheEnd*/ false));
|
2014-10-15 18:12:52 +02:00
|
|
|
if (NoExecStack)
|
|
|
|
Str->InitSections(true);
|
llvm-mc: Start MCAssembler and MCMachOStreamer.
- Together these form the (Mach-O) back end of the assembler.
- MCAssembler is the actual assembler backend, which is designed to have a
reasonable API. This will eventually grow to support multiple object file
implementations, but for now its Mach-O/i386 only.
- MCMachOStreamer adapts the MCStreamer "actions" API to the MCAssembler API,
e.g. converting the various directives into fragments, managing state like
the current section, and so on.
- llvm-mc will use the new backend via '-filetype=obj', which may eventually
be, but is not yet, since I hear that people like assemblers which actually
assemble.
- The only thing that works at the moment is changing sections. For the time
being I have a Python Mach-O dumping tool in test/scripts so this stuff can
be easily tested, eventually I expect to replace this with a real LLVM tool.
- More doxyments to come.
I assume that since this stuff doesn't touch any of the things which are part of
2.6 that it is ok to put this in not so long before the freeze, but if someone
objects let me know, I can pull it.
llvm-svn: 79612
2009-08-21 11:11:24 +02:00
|
|
|
}
|
2009-06-25 01:30:00 +02:00
|
|
|
|
[MC] Change AsmParser to leverage Assembler during evaluation
Teach AsmParser to check with Assembler for when evaluating constant
expressions. This improves the handing of preprocessor expressions
that must be resolved at parse time. This idiom can be found as
assembling-time assertion checks in source-level assemblers. Note that
this relies on the MCStreamer to keep sufficient tabs on Section /
Fragment information which the MCAsmStreamer does not. As a result the
textual output may fail where the equivalent object generation would
pass. This can most easily be resolved by folding the MCAsmStreamer
and MCObjectStreamer together which is planned for in a separate
patch.
Currently, this feature is only enabled for assembly input, keeping IR
compilation consistent between assembly and object generation.
Reviewers: echristo, rnk, probinson, espindola, peter.smith
Reviewed By: peter.smith
Subscribers: eraman, peter.smith, arichardson, jyknight, hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D45164
llvm-svn: 331218
2018-04-30 21:22:40 +02:00
|
|
|
// Use Assembler information for parsing.
|
|
|
|
Str->setUseAssemblerInfoForParsing(true);
|
|
|
|
|
2012-04-16 13:32:10 +02:00
|
|
|
int Res = 1;
|
2012-12-05 19:13:19 +01:00
|
|
|
bool disassemble = false;
|
2009-06-21 07:22:37 +02:00
|
|
|
switch (Action) {
|
2009-06-21 22:16:42 +02:00
|
|
|
case AC_AsLex:
|
2014-12-12 08:52:19 +01:00
|
|
|
Res = AsLexInput(SrcMgr, *MAI, Out->os());
|
2012-04-16 13:32:10 +02:00
|
|
|
break;
|
2009-06-21 07:22:37 +02:00
|
|
|
case AC_Assemble:
|
2014-06-19 08:22:01 +02:00
|
|
|
Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
|
2014-06-19 08:22:08 +02:00
|
|
|
*MCII, MCOptions);
|
2012-04-16 13:32:10 +02:00
|
|
|
break;
|
2012-10-23 00:31:46 +02:00
|
|
|
case AC_MDisassemble:
|
2013-02-27 00:04:17 +01:00
|
|
|
assert(IP && "Expected assembly output");
|
2012-10-23 00:31:46 +02:00
|
|
|
IP->setUseMarkup(1);
|
2012-12-05 19:13:19 +01:00
|
|
|
disassemble = true;
|
|
|
|
break;
|
2009-12-17 02:49:59 +01:00
|
|
|
case AC_Disassemble:
|
2012-12-05 19:13:19 +01:00
|
|
|
disassemble = true;
|
2012-04-16 13:32:10 +02:00
|
|
|
break;
|
2009-06-21 07:22:37 +02:00
|
|
|
}
|
2012-12-05 19:13:19 +01:00
|
|
|
if (disassemble)
|
|
|
|
Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str,
|
|
|
|
*Buffer, SrcMgr, Out->os());
|
2012-04-16 13:32:10 +02:00
|
|
|
|
|
|
|
// Keep output if no errors.
|
2018-05-21 21:44:54 +02:00
|
|
|
if (Res == 0) {
|
|
|
|
Out->keep();
|
|
|
|
if (DwoOut)
|
|
|
|
DwoOut->keep();
|
|
|
|
}
|
2012-04-16 13:32:10 +02:00
|
|
|
return Res;
|
2009-06-19 01:04:45 +02:00
|
|
|
}
|