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