1 //===-- llvm-mc.cpp - Machine Code Hacking Driver -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility is a simple driver that allows command line hacking on machine
13 //===----------------------------------------------------------------------===//
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCSectionMachO.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/System/Signals.h"
28 #include "llvm/Target/TargetAsmParser.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Target/TargetSelect.h"
31 #include "AsmParser.h"
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Output filename"),
39 cl::value_desc("filename"));
42 Force("f", cl::desc("Overwrite output files"));
44 static cl::list<std::string>
45 IncludeDirs("I", cl::desc("Directory of include files"),
46 cl::value_desc("directory"), cl::Prefix);
48 static cl::opt<std::string>
49 TripleName("triple", cl::desc("Target triple to assemble for,"
50 "see -version for available targets"),
51 cl::init(LLVM_HOSTTRIPLE));
58 static cl::opt<ActionType>
59 Action(cl::desc("Action to perform:"),
60 cl::init(AC_Assemble),
61 cl::values(clEnumValN(AC_AsLex, "as-lex",
62 "Lex tokens from a .s file"),
63 clEnumValN(AC_Assemble, "assemble",
64 "Assemble a .s file (default)"),
67 static int AsLexInput(const char *ProgName) {
68 std::string ErrorMessage;
69 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
72 errs() << ProgName << ": ";
73 if (ErrorMessage.size())
74 errs() << ErrorMessage << "\n";
76 errs() << "input file didn't read correctly.\n";
82 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
83 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
85 // Record the location of the include directories so that the lexer can find
87 SrcMgr.setIncludeDirs(IncludeDirs);
89 AsmLexer Lexer(SrcMgr);
93 while (Lexer.Lex().isNot(AsmToken::Eof)) {
94 switch (Lexer.getKind()) {
96 Lexer.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
100 Error = true; // error already printed.
102 case AsmToken::Identifier:
103 outs() << "identifier: " << Lexer.getTok().getString() << '\n';
105 case AsmToken::Register:
106 outs() << "register: " << Lexer.getTok().getString() << '\n';
108 case AsmToken::String:
109 outs() << "string: " << Lexer.getTok().getString() << '\n';
111 case AsmToken::Integer:
112 outs() << "int: " << Lexer.getTok().getString() << '\n';
115 case AsmToken::Amp: outs() << "Amp\n"; break;
116 case AsmToken::AmpAmp: outs() << "AmpAmp\n"; break;
117 case AsmToken::Caret: outs() << "Caret\n"; break;
118 case AsmToken::Colon: outs() << "Colon\n"; break;
119 case AsmToken::Comma: outs() << "Comma\n"; break;
120 case AsmToken::Dollar: outs() << "Dollar\n"; break;
121 case AsmToken::EndOfStatement: outs() << "EndOfStatement\n"; break;
122 case AsmToken::Eof: outs() << "Eof\n"; break;
123 case AsmToken::Equal: outs() << "Equal\n"; break;
124 case AsmToken::EqualEqual: outs() << "EqualEqual\n"; break;
125 case AsmToken::Exclaim: outs() << "Exclaim\n"; break;
126 case AsmToken::ExclaimEqual: outs() << "ExclaimEqual\n"; break;
127 case AsmToken::Greater: outs() << "Greater\n"; break;
128 case AsmToken::GreaterEqual: outs() << "GreaterEqual\n"; break;
129 case AsmToken::GreaterGreater: outs() << "GreaterGreater\n"; break;
130 case AsmToken::LParen: outs() << "LParen\n"; break;
131 case AsmToken::Less: outs() << "Less\n"; break;
132 case AsmToken::LessEqual: outs() << "LessEqual\n"; break;
133 case AsmToken::LessGreater: outs() << "LessGreater\n"; break;
134 case AsmToken::LessLess: outs() << "LessLess\n"; break;
135 case AsmToken::Minus: outs() << "Minus\n"; break;
136 case AsmToken::Percent: outs() << "Percent\n"; break;
137 case AsmToken::Pipe: outs() << "Pipe\n"; break;
138 case AsmToken::PipePipe: outs() << "PipePipe\n"; break;
139 case AsmToken::Plus: outs() << "Plus\n"; break;
140 case AsmToken::RParen: outs() << "RParen\n"; break;
141 case AsmToken::Slash: outs() << "Slash\n"; break;
142 case AsmToken::Star: outs() << "Star\n"; break;
143 case AsmToken::Tilde: outs() << "Tilde\n"; break;
150 static const Target *GetTarget(const char *ProgName) {
151 // Get the target specific parser.
153 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
157 errs() << ProgName << ": error: unable to get target for '" << TripleName
158 << "', see --version and --triple.\n";
162 static formatted_raw_ostream *GetOutputStream() {
163 if (OutputFilename == "" || OutputFilename == "-")
166 // Make sure that the Out file gets unlinked from the disk if we get a
168 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
171 raw_fd_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(),
172 /*Binary=*/false, Force, Err);
174 errs() << Err << '\n';
176 errs() << "Use -f command line argument to force output\n";
181 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
184 static int AssembleInput(const char *ProgName) {
185 const Target *TheTarget = GetTarget(ProgName);
190 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, &Error);
192 errs() << ProgName << ": ";
194 errs() << Error << "\n";
196 errs() << "input file didn't read correctly.\n";
202 // Tell SrcMgr about this buffer, which is what the parser will pick up.
203 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
205 // Record the location of the include directories so that the lexer can find
207 SrcMgr.setIncludeDirs(IncludeDirs);
210 formatted_raw_ostream *Out = GetOutputStream();
215 // FIXME: We shouldn't need to do this (and link in codegen).
216 OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(TripleName, ""));
219 errs() << ProgName << ": error: could not create target for triple '"
220 << TripleName << "'.\n";
224 const TargetAsmInfo *TAI = TheTarget->createAsmInfo(TripleName);
225 assert(TAI && "Unable to create target asm info!");
227 OwningPtr<AsmPrinter> AP(TheTarget->createAsmPrinter(*Out, *TM, TAI, true));
228 OwningPtr<MCStreamer> Str(createAsmStreamer(Ctx, *Out, *TAI, AP.get()));
230 // FIXME: Target hook & command line option for initial section.
231 Str.get()->SwitchSection(MCSectionMachO::Create("__TEXT","__text",
232 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
233 0, SectionKind::getText(),
236 AsmParser Parser(SrcMgr, Ctx, *Str.get());
237 OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(Parser));
240 << ": error: this target does not support assembly parsing.\n";
244 Parser.setTargetParser(*TAP.get());
246 int Res = Parser.Run();
254 int main(int argc, char **argv) {
255 // Print a stack trace if we signal out.
256 sys::PrintStackTraceOnErrorSignal();
257 PrettyStackTraceProgram X(argc, argv);
258 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
260 // Initialize targets and assembly printers/parsers.
261 llvm::InitializeAllTargetInfos();
262 // FIXME: We shouldn't need to initialize the Target(Machine)s.
263 llvm::InitializeAllTargets();
264 llvm::InitializeAllAsmPrinters();
265 llvm::InitializeAllAsmParsers();
267 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
272 return AsLexInput(argv[0]);
274 return AssembleInput(argv[0]);