blob: a6c93230c6cda364b78c450b6455957edbf7b177 [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
17#include "llvm/Support/DataTypes.h"
18#include <string>
19#include <cassert>
20
21namespace llvm {
22class MemoryBuffer;
23class SourceMgr;
24class SMLoc;
25
26namespace asmtok {
27 enum TokKind {
28 // Markers
29 Eof, Error,
30
Chris Lattner10a907d2009-06-21 19:56:35 +000031 // String values.
Chris Lattnera59e8772009-06-21 07:19:10 +000032 Identifier,
Chris Lattner4651bca2009-06-21 19:21:25 +000033 Register,
Chris Lattner10a907d2009-06-21 19:56:35 +000034 String,
35
36 // Integer values.
Chris Lattnera59e8772009-06-21 07:19:10 +000037 IntVal,
38
Chris Lattner10a907d2009-06-21 19:56:35 +000039 // No-value.
Chris Lattner4651bca2009-06-21 19:21:25 +000040 EndOfStatement,
Chris Lattnera59e8772009-06-21 07:19:10 +000041 Colon,
Chris Lattner74ec1a32009-06-22 06:32:03 +000042 Plus, Minus, Tilde,
Chris Lattner4651bca2009-06-21 19:21:25 +000043 Slash, // '/'
44 LParen, RParen,
45 Star, Comma, Dollar
Chris Lattnera59e8772009-06-21 07:19:10 +000046 };
47}
48
49/// AsmLexer - Lexer class for assembly files.
50class AsmLexer {
51 SourceMgr &SrcMgr;
52
53 const char *CurPtr;
54 const MemoryBuffer *CurBuf;
55
56 // Information about the current token.
57 const char *TokStart;
58 asmtok::TokKind CurKind;
59 std::string CurStrVal; // This is valid for Identifier.
60 int64_t CurIntVal;
61
62 /// CurBuffer - This is the current buffer index we're lexing from as managed
63 /// by the SourceMgr object.
64 int CurBuffer;
65
66public:
67 AsmLexer(SourceMgr &SrcMgr);
68 ~AsmLexer() {}
69
70 asmtok::TokKind Lex() {
71 return CurKind = LexToken();
72 }
73
74 asmtok::TokKind getKind() const { return CurKind; }
Chris Lattnerb0789ed2009-06-21 20:54:55 +000075 bool is(asmtok::TokKind K) const { return CurKind == K; }
76 bool isNot(asmtok::TokKind K) const { return CurKind != K; }
Chris Lattnera59e8772009-06-21 07:19:10 +000077
78 const std::string &getCurStrVal() const {
Chris Lattner10a907d2009-06-21 19:56:35 +000079 assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
80 CurKind == asmtok::String) &&
Chris Lattnera59e8772009-06-21 07:19:10 +000081 "This token doesn't have a string value");
82 return CurStrVal;
83 }
84 int64_t getCurIntVal() const {
85 assert(CurKind == asmtok::IntVal && "This token isn't an integer");
86 return CurIntVal;
87 }
88
89 SMLoc getLoc() const;
90
Chris Lattner14ee48a2009-06-21 21:22:11 +000091 void PrintMessage(SMLoc Loc, const std::string &Msg) const;
Chris Lattnera59e8772009-06-21 07:19:10 +000092
93private:
94 int getNextChar();
Chris Lattner4651bca2009-06-21 19:21:25 +000095 asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnera59e8772009-06-21 07:19:10 +000096
97 /// LexToken - Read the next token and return its code.
98 asmtok::TokKind LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +000099 asmtok::TokKind LexIdentifier();
100 asmtok::TokKind LexPercent();
101 asmtok::TokKind LexSlash();
102 asmtok::TokKind LexHash();
103 asmtok::TokKind LexDigit();
Chris Lattner10a907d2009-06-21 19:56:35 +0000104 asmtok::TokKind LexQuote();
Chris Lattnera59e8772009-06-21 07:19:10 +0000105};
106
107} // end namespace llvm
108
109#endif