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: Lexer.PrintError(Lexer.getLoc(), "driver: unknown token"); break; 76 case asmtok::Error: 77 Lexer.PrintError(Lexer.getLoc(), "error, bad token"); 78 break; 79 case asmtok::Identifier: 80 outs() << "identifier: " << Lexer.getCurStrVal() << '\n'; 81 break; 82 case asmtok::Register: 83 outs() << "register: " << Lexer.getCurStrVal() << '\n'; 84 break; 85 case asmtok::IntVal: 86 outs() << "int: " << Lexer.getCurIntVal() << '\n'; 87 break; 88 case asmtok::EndOfStatement: outs() << "EndOfStatement\n"; break; 89 case asmtok::Colon: outs() << "Colon\n"; break; 90 case asmtok::Plus: outs() << "Plus\n"; break; 91 case asmtok::Minus: outs() << "Minus\n"; break; 92 case asmtok::Slash: outs() << "Slash\n"; break; 93 case asmtok::LParen: outs() << "LParen\n"; break; 94 case asmtok::RParen: outs() << "RParen\n"; break; 95 case asmtok::Star: outs() << "Star\n"; break; 96 case asmtok::Comma: outs() << "Comma\n"; break; 97 case asmtok::Dollar: outs() << "Dollar\n"; break; 98 } 99 100 Tok = Lexer.Lex(); 101 } 102 103 return 1; 104 } 105 106 107 int main(int argc, char **argv) { 108 // Print a stack trace if we signal out. 109 sys::PrintStackTraceOnErrorSignal(); 110 PrettyStackTraceProgram X(argc, argv); 111 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 112 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n"); 113 114 switch (Action) { 115 default: 116 case AC_Assemble: 117 return AssembleInput(argv[0]); 118 } 119 120 return 0; 121 } 122 123