xref: /llvm-project/llvm/tools/llvm-mc/llvm-mc.cpp (revision c8dfbcbb311874ac851a8daa30f7e3e71669eb5c)
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/Support/CommandLine.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/PrettyStackTrace.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/System/Signals.h"
22 #include "AsmLexer.h"
23 using namespace llvm;
24 
25 static cl::opt<std::string>
26 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
27 
28 static cl::opt<std::string>
29 OutputFilename("o", cl::desc("Output filename"),
30                cl::value_desc("filename"));
31 
32 static cl::list<std::string>
33 IncludeDirs("I", cl::desc("Directory of include files"),
34             cl::value_desc("directory"), cl::Prefix);
35 
36 enum ActionType {
37   AC_Assemble
38 };
39 
40 static cl::opt<ActionType>
41 Action(cl::desc("Action to perform:"),
42        cl::values(clEnumValN(AC_Assemble, "assemble",
43                              "Assemble a .s file (default)"),
44                   clEnumValEnd));
45 
46 static int AssembleInput(const char *ProgName) {
47   std::string ErrorMessage;
48   MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
49                                                       &ErrorMessage);
50   if (Buffer == 0) {
51     errs() << ProgName << ": ";
52     if (ErrorMessage.size())
53       errs() << ErrorMessage << "\n";
54     else
55       errs() << "input file didn't read correctly.\n";
56     return 1;
57   }
58 
59   SourceMgr SrcMgr;
60 
61   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
62   SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
63 
64   // Record the location of the include directories so that the lexer can find
65   // it later.
66   SrcMgr.setIncludeDirs(IncludeDirs);
67 
68 
69 
70   AsmLexer Lexer(SrcMgr);
71 
72   asmtok::TokKind Tok = Lexer.Lex();
73   while (Tok != asmtok::Eof) {
74     switch (Tok) {
75     default: outs() << "<<unknown token>>\n"; break;
76     case asmtok::Error: outs() << "<<error>>\n"; break;
77     case asmtok::Identifier:
78       outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
79       break;
80     case asmtok::IntVal:
81       outs() << "int: " << Lexer.getCurIntVal() << '\n';
82       break;
83     case asmtok::Colon:  outs() << "Colon\n"; break;
84     case asmtok::Plus:   outs() << "Plus\n"; break;
85     case asmtok::Minus:  outs() << "Minus\n"; break;
86     }
87 
88     Tok = Lexer.Lex();
89   }
90 
91   return 1;
92 }
93 
94 
95 int main(int argc, char **argv) {
96   // Print a stack trace if we signal out.
97   sys::PrintStackTraceOnErrorSignal();
98   PrettyStackTraceProgram X(argc, argv);
99   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
100   cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
101 
102   switch (Action) {
103   default:
104   case AC_Assemble:
105     return AssembleInput(argv[0]);
106   }
107 
108   return 0;
109 }
110 
111