blob: a50eb93821376a417a2f1242dd6725369c1b86fa [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header file defines the various variables that are shared among the
11// different components of the parser...
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef PARSER_INTERNALS_H
16#define PARSER_INTERNALS_H
17
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"
Dale Johannesenb9de9f02007-09-06 18:13:44 +000025#include "llvm/ADT/APFloat.h"
Chris Lattner17e73c22007-11-18 08:46:26 +000026namespace llvm { class MemoryBuffer; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027
28// Global variables exported from the lexer...
29
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
31
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032// functions exported from the lexer
Chris Lattner17e73c22007-11-18 08:46:26 +000033void InitLLLexer(llvm::MemoryBuffer *MB);
34const char *LLLgetTokenStart();
35unsigned LLLgetTokenLength();
36std::string LLLgetFilename();
37unsigned LLLgetLineNo();
38void FreeLexer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40namespace llvm {
41class Module;
42
Chris Lattner17e73c22007-11-18 08:46:26 +000043// RunVMAsmParser - Parse a buffer and return Module
44Module *RunVMAsmParser(llvm::MemoryBuffer *MB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
46// GenerateError - Wrapper around the ParseException class that automatically
47// fills in file line number and column number and options info.
48//
49// This also helps me because I keep typing 'throw new ParseException' instead
50// of just 'throw ParseException'... sigh...
51//
52extern void GenerateError(const std::string &message, int LineNo = -1);
53
54/// InlineAsmDescriptor - This is a simple class that holds info about inline
55/// asm blocks, for use by ValID.
56struct InlineAsmDescriptor {
57 std::string AsmString, Constraints;
58 bool HasSideEffects;
59
60 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
61 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
62};
63
64
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.
68//
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.
71//
72struct ValID {
73 enum {
74 LocalID, GlobalID, LocalName, GlobalName,
75 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
76 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
77 } Type;
78
79 union {
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.
Dale Johannesenb9de9f02007-09-06 18:13:44 +000084 APFloat *ConstPoolFP; // Floating point constant pool reference
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
86 InlineAsmDescriptor *IAD;
Dale Johannesenb9de9f02007-09-06 18:13:44 +000087 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
89 static ValID createLocalID(unsigned Num) {
90 ValID D; D.Type = LocalID; D.Num = Num; return D;
91 }
92 static ValID createGlobalID(unsigned Num) {
93 ValID D; D.Type = GlobalID; D.Num = Num; return D;
94 }
95 static ValID createLocalName(const std::string &Name) {
96 ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
97 }
98 static ValID createGlobalName(const std::string &Name) {
99 ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
100 }
101
102 static ValID create(int64_t Val) {
103 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
104 }
105
106 static ValID create(uint64_t Val) {
107 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
108 }
109
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000110 static ValID create(APFloat *Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
112 }
113
114 static ValID createNull() {
115 ValID D; D.Type = ConstNullVal; return D;
116 }
117
118 static ValID createUndef() {
119 ValID D; D.Type = ConstUndefVal; return D;
120 }
121
122 static ValID createZeroInit() {
123 ValID D; D.Type = ConstZeroVal; return D;
124 }
125
126 static ValID create(Constant *Val) {
127 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
128 }
129
130 static ValID createInlineAsm(const std::string &AsmString,
131 const std::string &Constraints,
132 bool HasSideEffects) {
133 ValID D;
134 D.Type = InlineAsmVal;
135 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
136 return D;
137 }
138
139 inline void destroy() const {
140 if (Type == LocalName || Type == GlobalName)
141 delete Name; // Free this strdup'd memory.
142 else if (Type == InlineAsmVal)
143 delete IAD;
144 }
145
146 inline ValID copy() const {
147 if (Type != LocalName && Type != GlobalName) return *this;
148 ValID Result = *this;
149 Result.Name = new std::string(*Name);
150 return Result;
151 }
152
153 inline std::string getName() const {
154 switch (Type) {
155 case LocalID : return '%' + utostr(Num);
156 case GlobalID : return '@' + utostr(Num);
157 case LocalName : return *Name;
158 case GlobalName : return *Name;
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000159 case ConstFPVal : return ftostr(*ConstPoolFP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 case ConstNullVal : return "null";
161 case ConstUndefVal : return "undef";
162 case ConstZeroVal : return "zeroinitializer";
163 case ConstUIntVal :
164 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
165 case ConstantVal:
166 if (ConstantValue == ConstantInt::getTrue()) return "true";
167 if (ConstantValue == ConstantInt::getFalse()) return "false";
168 return "<constant expression>";
169 default:
170 assert(0 && "Unknown value!");
171 abort();
172 return "";
173 }
174 }
175
176 bool operator<(const ValID &V) const {
177 if (Type != V.Type) return Type < V.Type;
178 switch (Type) {
179 case LocalID:
180 case GlobalID: return Num < V.Num;
181 case LocalName:
182 case GlobalName: return *Name < *V.Name;
183 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
184 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000185 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
186 APFloat::cmpLessThan;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 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;
192 }
193 }
194
195 bool operator==(const ValID &V) const {
196 if (Type == V.Type) {
197 switch (Type) {
198 case LocalID:
199 case GlobalID: return Num == V.Num;
200 case LocalName:
201 case GlobalName: return *Name == *(V.Name);
202 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
203 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000204 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
205 APFloat::cmpEqual;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 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;
211 }
212 }
213 return false;
214 }
215};
216
217struct TypeWithAttrs {
218 llvm::PATypeHolder *Ty;
219 uint16_t Attrs;
220};
221
222typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
223
224struct ArgListEntry {
225 uint16_t Attrs;
226 llvm::PATypeHolder *Ty;
227 std::string *Name;
228};
229
230typedef std::vector<struct ArgListEntry> ArgListType;
231
Dale Johannesencfb19e62007-11-05 21:20:28 +0000232struct ParamListEntry {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 Value *Val;
234 uint16_t Attrs;
235};
236
Dale Johannesencfb19e62007-11-05 21:20:28 +0000237typedef std::vector<ParamListEntry> ParamList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238
239
240} // End llvm namespace
241
242#endif