ab596f5106a8a65379e8fa4e77f2d6817648e35e
[oota-llvm.git] / lib / MC / MCDisassembler / EDToken.h
1 //===-EDToken.h - LLVM Enhanced Disassembler --------------------*- 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 file defines the interface for the Enhanced Disassembly library's token
11 // class.  The token is responsible for vending information about the token, 
12 // such as its type and logical value.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_EDTOKEN_H
17 #define LLVM_EDTOKEN_H
18
19 #include "llvm/ADT/StringRef.h"
20 #include <string>
21 #include <vector>
22
23 namespace llvm {
24   
25 struct EDDisassembler;
26
27 /// EDToken - Encapsulates a single token, which can provide a string
28 ///   representation of itself or interpret itself in various ways, depending
29 ///   on the token type.
30 struct EDToken {
31   enum tokenType {
32     kTokenWhitespace,
33     kTokenOpcode,
34     kTokenLiteral,
35     kTokenRegister,
36     kTokenPunctuation
37   };
38   
39   /// The parent disassembler
40   EDDisassembler &Disassembler;
41
42   /// The token's string representation
43   llvm::StringRef Str;
44   /// The token's string representation, but in a form suitable for export
45   std::string PermStr;
46   /// The type of the token, as exposed through the external API
47   enum tokenType Type;
48   /// The type of the token, as recorded by the syntax-specific tokenizer
49   uint64_t LocalType;
50   /// The operand corresponding to the token, or (unsigned int)-1 if not
51   ///   part of an operand.
52   int OperandID;
53   
54   /// The sign if the token is a literal (1 if negative, 0 otherwise)
55   bool LiteralSign;
56   /// The absolute value if the token is a literal
57   uint64_t LiteralAbsoluteValue;
58   /// The LLVM register ID if the token is a register name
59   unsigned RegisterID;
60   
61   /// Constructor - Initializes an EDToken with the information common to all
62   ///   tokens
63   ///
64   /// @arg str          - The string corresponding to the token
65   /// @arg type         - The token's type as exposed through the public API
66   /// @arg localType    - The token's type as recorded by the tokenizer
67   /// @arg disassembler - The disassembler responsible for the token
68   EDToken(llvm::StringRef str,
69           enum tokenType type,
70           uint64_t localType,
71           EDDisassembler &disassembler);
72   
73   /// makeLiteral - Adds the information specific to a literal
74   /// @arg sign           - The sign of the literal (1 if negative, 0 
75   ///                       otherwise)
76   ///
77   /// @arg absoluteValue  - The absolute value of the literal
78   void makeLiteral(bool sign, uint64_t absoluteValue);
79   /// makeRegister - Adds the information specific to a register
80   ///
81   /// @arg registerID - The LLVM register ID
82   void makeRegister(unsigned registerID);
83   
84   /// setOperandID - Links the token to a numbered operand
85   ///
86   /// @arg operandID  - The operand ID to link to
87   void setOperandID(int operandID);
88   
89   ~EDToken();
90   
91   /// type - Returns the public type of the token
92   enum tokenType type() const;
93   /// localType - Returns the tokenizer-specific type of the token
94   uint64_t localType() const;
95   /// string - Returns the string representation of the token
96   llvm::StringRef string() const;
97   /// operandID - Returns the operand ID of the token
98   int operandID() const;
99   
100   /// literalSign - Returns the sign of the token 
101   ///   (1 if negative, 0 if positive or unsigned, -1 if it is not a literal)
102   int literalSign() const;
103   /// literalAbsoluteValue - Retrieves the absolute value of the token, and
104   ///   returns -1 if the token is not a literal
105   /// @arg value  - A reference to a value that is filled in with the absolute
106   ///               value, if it is valid
107   int literalAbsoluteValue(uint64_t &value) const;
108   /// registerID - Retrieves the register ID of the token, and returns -1 if the
109   ///   token is not a register
110   ///
111   /// @arg registerID - A reference to a value that is filled in with the 
112   ///                   register ID, if it is valid
113   int registerID(unsigned &registerID) const;
114   
115   /// tokenize - Tokenizes a string using the platform- and syntax-specific
116   ///   tokenizer, and returns 0 on success (-1 on failure)
117   ///
118   /// @arg tokens       - A vector that will be filled in with pointers to
119   ///                     allocated tokens
120   /// @arg str          - The string, as outputted by the AsmPrinter
121   /// @arg operandOrder - The order of the operands from the operandFlags array
122   ///                     as they appear in str
123   /// @arg disassembler - The disassembler for the desired target and
124   //                      assembly syntax
125   static int tokenize(std::vector<EDToken*> &tokens,
126                       std::string &str,
127                       const char *operandOrder,
128                       EDDisassembler &disassembler);
129   
130   /// getString - Directs a character pointer to the string, returning 0 on
131   ///   success (-1 on failure)
132   /// @arg buf  - A reference to a pointer that is set to point to the string.
133   ///   The string is still owned by the token.
134   int getString(const char*& buf);
135 };
136
137 } // end namespace llvm
138 #endif