blob: e60fa201e5e52507f07d8def12fc76bd92878c1b [file] [log] [blame]
Chris Lattnera59e8772009-06-21 07:19:10 +00001//===- AsmLexer.h - Lexer for Assembly Files --------------------*- C++ -*-===//
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 class declares the lexer for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef ASMLEXER_H
15#define ASMLEXER_H
16
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +000017#include "llvm/ADT/StringRef.h"
Daniel Dunbardbd692a2009-07-20 20:01:54 +000018#include "llvm/MC/MCAsmLexer.h"
Chris Lattnera59e8772009-06-21 07:19:10 +000019#include "llvm/Support/DataTypes.h"
20#include <string>
21#include <cassert>
22
23namespace llvm {
24class MemoryBuffer;
25class SourceMgr;
26class SMLoc;
27
28namespace asmtok {
29 enum TokKind {
30 // Markers
31 Eof, Error,
32
Chris Lattner10a907d2009-06-21 19:56:35 +000033 // String values.
Chris Lattnera59e8772009-06-21 07:19:10 +000034 Identifier,
Chris Lattner4651bca2009-06-21 19:21:25 +000035 Register,
Chris Lattner10a907d2009-06-21 19:56:35 +000036 String,
37
38 // Integer values.
Chris Lattnera59e8772009-06-21 07:19:10 +000039 IntVal,
40
Chris Lattner10a907d2009-06-21 19:56:35 +000041 // No-value.
Chris Lattner4651bca2009-06-21 19:21:25 +000042 EndOfStatement,
Chris Lattnera59e8772009-06-21 07:19:10 +000043 Colon,
Chris Lattner74ec1a32009-06-22 06:32:03 +000044 Plus, Minus, Tilde,
Chris Lattner4651bca2009-06-21 19:21:25 +000045 Slash, // '/'
46 LParen, RParen,
Daniel Dunbar475839e2009-06-29 20:37:27 +000047 Star, Comma, Dollar, Equal, EqualEqual,
Chris Lattner8dfbe6c2009-06-23 05:57:07 +000048
Daniel Dunbar475839e2009-06-29 20:37:27 +000049 Pipe, PipePipe, Caret,
50 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent,
51 Less, LessEqual, LessLess, LessGreater,
52 Greater, GreaterEqual, GreaterGreater
Chris Lattnera59e8772009-06-21 07:19:10 +000053 };
54}
55
Daniel Dunbarcb358b62009-07-28 03:00:54 +000056/// AsmToken - Target independent representation for an assembler token.
57struct AsmToken {
58 asmtok::TokKind Kind;
59
60 /// A reference to the entire token contents; this is always a pointer into
61 /// a memory buffer owned by the source manager.
62 StringRef Str;
63
64 int64_t IntVal;
65
66public:
67 AsmToken() {}
68 AsmToken(asmtok::TokKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
69 : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
70
71 asmtok::TokKind getKind() const { return Kind; }
72 bool is(asmtok::TokKind K) const { return Kind == K; }
73 bool isNot(asmtok::TokKind K) const { return Kind != K; }
74
75 SMLoc getLoc() const;
76
77 StringRef getString() const { return Str; }
78
79 int64_t getIntVal() const {
80 assert(Kind == asmtok::IntVal && "This token isn't an integer");
81 return IntVal;
82 }
83};
84
Chris Lattnera59e8772009-06-21 07:19:10 +000085/// AsmLexer - Lexer class for assembly files.
Daniel Dunbardbd692a2009-07-20 20:01:54 +000086class AsmLexer : public MCAsmLexer {
Chris Lattnera59e8772009-06-21 07:19:10 +000087 SourceMgr &SrcMgr;
88
89 const char *CurPtr;
90 const MemoryBuffer *CurBuf;
91
Chris Lattnera59e8772009-06-21 07:19:10 +000092 const char *TokStart;
Daniel Dunbarcb358b62009-07-28 03:00:54 +000093
94 /// The current token.
95 AsmToken CurTok;
Chris Lattnera59e8772009-06-21 07:19:10 +000096
Daniel Dunbarcb358b62009-07-28 03:00:54 +000097 /// This is the current buffer index we're lexing from as managed by the
98 /// SourceMgr object.
Chris Lattnera59e8772009-06-21 07:19:10 +000099 int CurBuffer;
100
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000101 void operator=(const AsmLexer&); // DO NOT IMPLEMENT
102 AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT
Chris Lattnera59e8772009-06-21 07:19:10 +0000103public:
104 AsmLexer(SourceMgr &SrcMgr);
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000105 ~AsmLexer();
Chris Lattnera59e8772009-06-21 07:19:10 +0000106
107 asmtok::TokKind Lex() {
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000108 return CurTok = LexToken(), getKind();
Chris Lattnera59e8772009-06-21 07:19:10 +0000109 }
110
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000111 asmtok::TokKind getKind() const { return CurTok.getKind(); }
112 bool is(asmtok::TokKind K) const { return CurTok.is(K); }
113 bool isNot(asmtok::TokKind K) const { return CurTok.isNot(K); }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000114
115 /// getCurStrVal - Get the string for the current token, this includes all
116 /// characters (for example, the quotes on strings) in the token.
117 ///
118 /// The returned StringRef points into the source manager's memory buffer, and
119 /// is safe to store across calls to Lex().
120 StringRef getCurStrVal() const {
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000121 return CurTok.getString();
Chris Lattnera59e8772009-06-21 07:19:10 +0000122 }
123 int64_t getCurIntVal() const {
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000124 return CurTok.getIntVal();
Chris Lattnera59e8772009-06-21 07:19:10 +0000125 }
126
127 SMLoc getLoc() const;
128
Chris Lattner8e25e2d2009-07-16 06:14:39 +0000129 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
130 bool EnterIncludeFile(const std::string &Filename);
131
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000132 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
Chris Lattnera59e8772009-06-21 07:19:10 +0000133
134private:
135 int getNextChar();
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000136 AsmToken ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnera59e8772009-06-21 07:19:10 +0000137
138 /// LexToken - Read the next token and return its code.
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000139 AsmToken LexToken();
140 AsmToken LexIdentifier();
141 AsmToken LexPercent();
142 AsmToken LexSlash();
143 AsmToken LexLineComment();
144 AsmToken LexDigit();
145 AsmToken LexQuote();
Chris Lattnera59e8772009-06-21 07:19:10 +0000146};
147
148} // end namespace llvm
149
150#endif