1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file defines the various variables that are shared among the
11 // different components of the parser...
13 //===----------------------------------------------------------------------===//
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Attributes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Assembly/Parser.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/APFloat.h"
26 #include "llvm/ADT/APSInt.h"
27 namespace llvm { class MemoryBuffer; }
29 // Global variables exported from the lexer...
31 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
33 // functions exported from the lexer
34 void InitLLLexer(llvm::MemoryBuffer *MB);
35 const char *LLLgetTokenStart();
36 unsigned LLLgetTokenLength();
37 std::string LLLgetFilename();
38 unsigned LLLgetLineNo();
44 // RunVMAsmParser - Parse a buffer and return Module
45 Module *RunVMAsmParser(llvm::MemoryBuffer *MB);
47 // GenerateError - Wrapper around the ParseException class that automatically
48 // fills in file line number and column number and options info.
50 // This also helps me because I keep typing 'throw new ParseException' instead
51 // of just 'throw ParseException'... sigh...
53 extern void GenerateError(const std::string &message, int LineNo = -1);
55 /// InlineAsmDescriptor - This is a simple class that holds info about inline
56 /// asm blocks, for use by ValID.
57 struct InlineAsmDescriptor {
58 std::string AsmString, Constraints;
61 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
62 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
66 // ValID - Represents a reference of a definition of some sort. This may either
67 // be a numeric reference or a symbolic (%var) reference. This is just a
68 // discriminated union.
70 // Note that I can't implement this class in a straight forward manner with
71 // constructors and stuff because it goes in a union.
75 LocalID, GlobalID, LocalName, GlobalName,
76 ConstSIntVal, ConstUIntVal, ConstAPInt, ConstFPVal, ConstNullVal,
77 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
81 unsigned Num; // If it's a numeric reference like %1234
82 std::string *Name; // If it's a named reference. Memory must be deleted.
83 int64_t ConstPool64; // Constant pool reference. This is the value
84 uint64_t UConstPool64; // Unsigned constant pool reference.
85 APSInt *ConstPoolInt; // Large Integer constant pool reference
86 APFloat *ConstPoolFP; // Floating point constant pool reference
87 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
88 InlineAsmDescriptor *IAD;
91 static ValID createLocalID(unsigned Num) {
92 ValID D; D.Type = LocalID; D.Num = Num; return D;
94 static ValID createGlobalID(unsigned Num) {
95 ValID D; D.Type = GlobalID; D.Num = Num; return D;
97 static ValID createLocalName(const std::string &Name) {
98 ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
100 static ValID createGlobalName(const std::string &Name) {
101 ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
104 static ValID create(int64_t Val) {
105 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
108 static ValID create(uint64_t Val) {
109 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
112 static ValID create(APFloat *Val) {
113 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
116 static ValID create(const APInt &Val, bool isSigned) {
117 ValID D; D.Type = ConstAPInt;
118 D.ConstPoolInt = new APSInt(Val, !isSigned);
123 static ValID createNull() {
124 ValID D; D.Type = ConstNullVal; return D;
127 static ValID createUndef() {
128 ValID D; D.Type = ConstUndefVal; return D;
131 static ValID createZeroInit() {
132 ValID D; D.Type = ConstZeroVal; return D;
135 static ValID create(Constant *Val) {
136 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
139 static ValID createInlineAsm(const std::string &AsmString,
140 const std::string &Constraints,
141 bool HasSideEffects) {
143 D.Type = InlineAsmVal;
144 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
148 inline void destroy() const {
149 if (Type == LocalName || Type == GlobalName)
150 delete Name; // Free this strdup'd memory.
151 else if (Type == InlineAsmVal)
153 else if (Type == ConstAPInt)
155 else if (Type == ConstFPVal)
159 inline ValID copy() const {
160 ValID Result = *this;
161 if (Type == ConstAPInt)
162 Result.ConstPoolInt = new APSInt(*ConstPoolInt);
164 if (Type != LocalName && Type != GlobalName) return Result;
165 Result.Name = new std::string(*Name);
169 inline std::string getName() const {
171 case LocalID : return '%' + utostr(Num);
172 case GlobalID : return '@' + utostr(Num);
173 case LocalName : return *Name;
174 case GlobalName : return *Name;
175 case ConstAPInt : return ConstPoolInt->toString(10);
176 case ConstFPVal : return ftostr(*ConstPoolFP);
177 case ConstNullVal : return "null";
178 case ConstUndefVal : return "undef";
179 case ConstZeroVal : return "zeroinitializer";
181 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
183 if (ConstantValue == ConstantInt::getTrue()) return "true";
184 if (ConstantValue == ConstantInt::getFalse()) return "false";
185 return "<constant expression>";
187 assert(0 && "Unknown value!");
193 bool operator<(const ValID &V) const {
194 if (Type != V.Type) return Type < V.Type;
197 case GlobalID: return Num < V.Num;
199 case GlobalName: return *Name < *V.Name;
200 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
201 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
202 case ConstAPInt: return ConstPoolInt->ult(*V.ConstPoolInt);
203 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
204 APFloat::cmpLessThan;
205 case ConstNullVal: return false;
206 case ConstUndefVal: return false;
207 case ConstZeroVal: return false;
208 case ConstantVal: return ConstantValue < V.ConstantValue;
209 default: assert(0 && "Unknown value type!"); return false;
213 bool operator==(const ValID &V) const {
214 if (Type != V.Type) return false;
217 default: assert(0 && "Unknown value type!");
219 case GlobalID: return Num == V.Num;
221 case GlobalName: return *Name == *(V.Name);
222 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
223 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
224 case ConstAPInt: return *ConstPoolInt == *V.ConstPoolInt;
225 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
227 case ConstantVal: return ConstantValue == V.ConstantValue;
228 case ConstNullVal: return true;
229 case ConstUndefVal: return true;
230 case ConstZeroVal: return true;
235 struct TypeWithAttrs {
236 llvm::PATypeHolder *Ty;
240 typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
242 struct ArgListEntry {
244 llvm::PATypeHolder *Ty;
248 typedef std::vector<struct ArgListEntry> ArgListType;
250 struct ParamListEntry {
255 typedef std::vector<ParamListEntry> ParamList;
258 } // End llvm namespace