blob: 83642988e375bb9a4ef6b7310e0a3bed925c5087 [file] [log] [blame]
Chris Lattnerbb4688a2009-06-18 23:05:21 +00001//===-- llvm-mc.cpp - Machine Code Hacking Driver -------------------------===//
Chris Lattnerf9f065e2009-06-18 23:04:45 +00002//
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//
Chris Lattnerbb4688a2009-06-18 23:05:21 +000010// This utility is a simple driver that allows command line hacking on machine
11// code.
Chris Lattnerf9f065e2009-06-18 23:04:45 +000012//
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"
Chris Lattnerb23677e2009-06-21 05:22:37 +000019#include "llvm/Support/SourceMgr.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000020#include "llvm/Support/raw_ostream.h"
21#include "llvm/System/Signals.h"
Chris Lattnera59e8772009-06-21 07:19:10 +000022#include "AsmLexer.h"
Chris Lattnerf9f065e2009-06-18 23:04:45 +000023using namespace llvm;
24
25static cl::opt<std::string>
26InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
27
28static cl::opt<std::string>
29OutputFilename("o", cl::desc("Output filename"),
30 cl::value_desc("filename"));
31
Chris Lattnerb23677e2009-06-21 05:22:37 +000032static cl::list<std::string>
33IncludeDirs("I", cl::desc("Directory of include files"),
34 cl::value_desc("directory"), cl::Prefix);
Chris Lattnerf9f065e2009-06-18 23:04:45 +000035
Chris Lattnerb23677e2009-06-21 05:22:37 +000036enum ActionType {
37 AC_Assemble
38};
Chris Lattnerf9f065e2009-06-18 23:04:45 +000039
Chris Lattnerb23677e2009-06-21 05:22:37 +000040static cl::opt<ActionType>
41Action(cl::desc("Action to perform:"),
42 cl::values(clEnumValN(AC_Assemble, "assemble",
43 "Assemble a .s file (default)"),
44 clEnumValEnd));
45
46static int AssembleInput(const char *ProgName) {
Chris Lattnerf9f065e2009-06-18 23:04:45 +000047 std::string ErrorMessage;
Chris Lattnerb23677e2009-06-21 05:22:37 +000048 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
49 &ErrorMessage);
Chris Lattnerf9f065e2009-06-18 23:04:45 +000050 if (Buffer == 0) {
Chris Lattnerb23677e2009-06-21 05:22:37 +000051 errs() << ProgName << ": ";
Chris Lattnerf9f065e2009-06-18 23:04:45 +000052 if (ErrorMessage.size())
53 errs() << ErrorMessage << "\n";
54 else
55 errs() << "input file didn't read correctly.\n";
56 return 1;
57 }
Chris Lattnerb23677e2009-06-21 05:22:37 +000058
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);
Chris Lattnera59e8772009-06-21 07:19:10 +000067
Chris Lattnerb23677e2009-06-21 05:22:37 +000068
Chris Lattnerf9f065e2009-06-18 23:04:45 +000069
Chris Lattnera59e8772009-06-21 07:19:10 +000070 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 }
Chris Lattnerf9f065e2009-06-18 23:04:45 +000090
Chris Lattnerb23677e2009-06-21 05:22:37 +000091 return 1;
92}
93
94
95int 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 }
Chris Lattnerf9f065e2009-06-18 23:04:45 +0000107
108 return 0;
109}
110