llvm-mc.cpp revision 3fb7683bec8c8edb24e80c95f3b0668c6ecc0ae6
1//===-- llvm-mc.cpp - Machine Code Hacking Driver -------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This utility is a simple driver that allows command line hacking on machine
11// code.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCStreamer.h"
17#include "llvm/ADT/OwningPtr.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/PrettyStackTrace.h"
22#include "llvm/Support/SourceMgr.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/System/Signals.h"
25#include "AsmParser.h"
26using namespace llvm;
27
28static cl::opt<std::string>
29InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
30
31static cl::opt<std::string>
32OutputFilename("o", cl::desc("Output filename"),
33               cl::value_desc("filename"));
34
35static cl::list<std::string>
36IncludeDirs("I", cl::desc("Directory of include files"),
37            cl::value_desc("directory"), cl::Prefix);
38
39enum ActionType {
40  AC_AsLex,
41  AC_Assemble
42};
43
44static cl::opt<ActionType>
45Action(cl::desc("Action to perform:"),
46       cl::init(AC_Assemble),
47       cl::values(clEnumValN(AC_AsLex, "as-lex",
48                             "Lex tokens from a .s file"),
49                  clEnumValN(AC_Assemble, "assemble",
50                             "Assemble a .s file (default)"),
51                  clEnumValEnd));
52
53static int AsLexInput(const char *ProgName) {
54  std::string ErrorMessage;
55  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
56                                                      &ErrorMessage);
57  if (Buffer == 0) {
58    errs() << ProgName << ": ";
59    if (ErrorMessage.size())
60      errs() << ErrorMessage << "\n";
61    else
62      errs() << "input file didn't read correctly.\n";
63    return 1;
64  }
65
66  SourceMgr SrcMgr;
67
68  // Tell SrcMgr about this buffer, which is what TGParser will pick up.
69  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
70
71  // Record the location of the include directories so that the lexer can find
72  // it later.
73  SrcMgr.setIncludeDirs(IncludeDirs);
74
75  AsmLexer Lexer(SrcMgr);
76
77  bool Error = false;
78
79  asmtok::TokKind Tok = Lexer.Lex();
80  while (Tok != asmtok::Eof) {
81    switch (Tok) {
82    default:
83      Lexer.PrintMessage(Lexer.getLoc(), "unknown token", "warning");
84      Error = true;
85      break;
86    case asmtok::Error:
87      Error = true; // error already printed.
88      break;
89    case asmtok::Identifier:
90      outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
91      break;
92    case asmtok::Register:
93      outs() << "register: " << Lexer.getCurStrVal() << '\n';
94      break;
95    case asmtok::String:
96      outs() << "string: " << Lexer.getCurStrVal() << '\n';
97      break;
98    case asmtok::IntVal:
99      outs() << "int: " << Lexer.getCurIntVal() << '\n';
100      break;
101    case asmtok::EndOfStatement: outs() << "EndOfStatement\n"; break;
102    case asmtok::Colon:  outs() << "Colon\n"; break;
103    case asmtok::Plus:   outs() << "Plus\n"; break;
104    case asmtok::Minus:  outs() << "Minus\n"; break;
105    case asmtok::Tilde:  outs() << "Tilde\n"; break;
106    case asmtok::Slash:  outs() << "Slash\n"; break;
107    case asmtok::LParen: outs() << "LParen\n"; break;
108    case asmtok::RParen: outs() << "RParen\n"; break;
109    case asmtok::Star:   outs() << "Star\n"; break;
110    case asmtok::Comma:  outs() << "Comma\n"; break;
111    case asmtok::Dollar: outs() << "Dollar\n"; break;
112    }
113
114    Tok = Lexer.Lex();
115  }
116
117  return Error;
118}
119
120static int AssembleInput(const char *ProgName) {
121  std::string ErrorMessage;
122  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
123                                                      &ErrorMessage);
124  if (Buffer == 0) {
125    errs() << ProgName << ": ";
126    if (ErrorMessage.size())
127      errs() << ErrorMessage << "\n";
128    else
129      errs() << "input file didn't read correctly.\n";
130    return 1;
131  }
132
133  SourceMgr SrcMgr;
134
135  // Tell SrcMgr about this buffer, which is what TGParser will pick up.
136  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
137
138  // Record the location of the include directories so that the lexer can find
139  // it later.
140  SrcMgr.setIncludeDirs(IncludeDirs);
141
142  MCContext Ctx;
143  OwningPtr<MCStreamer> Str(createAsmStreamer(Ctx, outs()));
144
145  // FIXME: Target hook & command line option for initial section.
146  Str.get()->SwitchSection(Ctx.GetSection("__TEXT,__text,regular,pure_instructions"));
147
148  AsmParser Parser(SrcMgr, Ctx, *Str.get());
149  return Parser.Run();
150}
151
152
153int main(int argc, char **argv) {
154  // Print a stack trace if we signal out.
155  sys::PrintStackTraceOnErrorSignal();
156  PrettyStackTraceProgram X(argc, argv);
157  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
158  cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
159
160  switch (Action) {
161  default:
162  case AC_AsLex:
163    return AsLexInput(argv[0]);
164  case AC_Assemble:
165    return AssembleInput(argv[0]);
166  }
167
168  return 0;
169}
170
171