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/ParameterAttributes.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 namespace llvm { class MemoryBuffer; }
28 // Global variables exported from the lexer...
30 extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
32 // functions exported from the lexer
33 void InitLLLexer(llvm::MemoryBuffer *MB);
34 const char *LLLgetTokenStart();
35 unsigned LLLgetTokenLength();
36 std::string LLLgetFilename();
37 unsigned LLLgetLineNo();
43 // RunVMAsmParser - Parse a buffer and return Module
44 Module *RunVMAsmParser(llvm::MemoryBuffer *MB);
46 // GenerateError - Wrapper around the ParseException class that automatically
47 // fills in file line number and column number and options info.
49 // This also helps me because I keep typing 'throw new ParseException' instead
50 // of just 'throw ParseException'... sigh...
52 extern void GenerateError(const std::string &message, int LineNo = -1);
54 /// InlineAsmDescriptor - This is a simple class that holds info about inline
55 /// asm blocks, for use by ValID.
56 struct InlineAsmDescriptor {
57 std::string AsmString, Constraints;
60 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
61 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
65 // ValID - Represents a reference of a definition of some sort. This may either
66 // be a numeric reference or a symbolic (%var) reference. This is just a
67 // discriminated union.
69 // Note that I can't implement this class in a straight forward manner with
70 // constructors and stuff because it goes in a union.
74 LocalID, GlobalID, LocalName, GlobalName,
75 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
76 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
80 unsigned Num; // If it's a numeric reference like %1234
81 std::string *Name; // If it's a named reference. Memory must be deleted.
82 int64_t ConstPool64; // Constant pool reference. This is the value
83 uint64_t UConstPool64;// Unsigned constant pool reference.
84 APFloat *ConstPoolFP; // Floating point constant pool reference
85 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
86 InlineAsmDescriptor *IAD;
89 static ValID createLocalID(unsigned Num) {
90 ValID D; D.Type = LocalID; D.Num = Num; return D;
92 static ValID createGlobalID(unsigned Num) {
93 ValID D; D.Type = GlobalID; D.Num = Num; return D;
95 static ValID createLocalName(const std::string &Name) {
96 ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
98 static ValID createGlobalName(const std::string &Name) {
99 ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
102 static ValID create(int64_t Val) {
103 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
106 static ValID create(uint64_t Val) {
107 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
110 static ValID create(APFloat *Val) {
111 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
114 static ValID createNull() {
115 ValID D; D.Type = ConstNullVal; return D;
118 static ValID createUndef() {
119 ValID D; D.Type = ConstUndefVal; return D;
122 static ValID createZeroInit() {
123 ValID D; D.Type = ConstZeroVal; return D;
126 static ValID create(Constant *Val) {
127 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
130 static ValID createInlineAsm(const std::string &AsmString,
131 const std::string &Constraints,
132 bool HasSideEffects) {
134 D.Type = InlineAsmVal;
135 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
139 inline void destroy() const {
140 if (Type == LocalName || Type == GlobalName)
141 delete Name; // Free this strdup'd memory.
142 else if (Type == InlineAsmVal)
146 inline ValID copy() const {
147 if (Type != LocalName && Type != GlobalName) return *this;
148 ValID Result = *this;
149 Result.Name = new std::string(*Name);
153 inline std::string getName() const {
155 case LocalID : return '%' + utostr(Num);
156 case GlobalID : return '@' + utostr(Num);
157 case LocalName : return *Name;
158 case GlobalName : return *Name;
159 case ConstFPVal : return ftostr(*ConstPoolFP);
160 case ConstNullVal : return "null";
161 case ConstUndefVal : return "undef";
162 case ConstZeroVal : return "zeroinitializer";
164 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
166 if (ConstantValue == ConstantInt::getTrue()) return "true";
167 if (ConstantValue == ConstantInt::getFalse()) return "false";
168 return "<constant expression>";
170 assert(0 && "Unknown value!");
176 bool operator<(const ValID &V) const {
177 if (Type != V.Type) return Type < V.Type;
180 case GlobalID: return Num < V.Num;
182 case GlobalName: return *Name < *V.Name;
183 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
184 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
185 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
186 APFloat::cmpLessThan;
187 case ConstNullVal: return false;
188 case ConstUndefVal: return false;
189 case ConstZeroVal: return false;
190 case ConstantVal: return ConstantValue < V.ConstantValue;
191 default: assert(0 && "Unknown value type!"); return false;
195 bool operator==(const ValID &V) const {
196 if (Type == V.Type) {
199 case GlobalID: return Num == V.Num;
201 case GlobalName: return *Name == *(V.Name);
202 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
203 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
204 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
206 case ConstantVal: return ConstantValue == V.ConstantValue;
207 case ConstNullVal: return true;
208 case ConstUndefVal: return true;
209 case ConstZeroVal: return true;
210 default: assert(0 && "Unknown value type!"); return false;
217 struct TypeWithAttrs {
218 llvm::PATypeHolder *Ty;
219 ParameterAttributes Attrs;
222 typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
224 struct ArgListEntry {
225 ParameterAttributes Attrs;
226 llvm::PATypeHolder *Ty;
230 typedef std::vector<struct ArgListEntry> ArgListType;
232 struct ParamListEntry {
234 ParameterAttributes Attrs;
237 typedef std::vector<ParamListEntry> ParamList;
240 } // End llvm namespace