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