blob: cbbc1e6ad0baedbf8b89e1c6b3f2bbfeb1903fbb [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//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 Lattner28e64d32008-07-11 00:30:06 +000026#include "llvm/ADT/APSInt.h"
Chris Lattner17e73c22007-11-18 08:46:26 +000027namespace llvm { class MemoryBuffer; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
29// Global variables exported from the lexer...
30
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033// functions exported from the lexer
Chris Lattner17e73c22007-11-18 08:46:26 +000034void InitLLLexer(llvm::MemoryBuffer *MB);
35const char *LLLgetTokenStart();
36unsigned LLLgetTokenLength();
37std::string LLLgetFilename();
38unsigned LLLgetLineNo();
39void FreeLexer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
41namespace llvm {
42class Module;
43
Chris Lattner17e73c22007-11-18 08:46:26 +000044// RunVMAsmParser - Parse a buffer and return Module
45Module *RunVMAsmParser(llvm::MemoryBuffer *MB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47// GenerateError - Wrapper around the ParseException class that automatically
48// fills in file line number and column number and options info.
49//
50// This also helps me because I keep typing 'throw new ParseException' instead
51// of just 'throw ParseException'... sigh...
52//
53extern void GenerateError(const std::string &message, int LineNo = -1);
54
55/// InlineAsmDescriptor - This is a simple class that holds info about inline
56/// asm blocks, for use by ValID.
57struct InlineAsmDescriptor {
58 std::string AsmString, Constraints;
59 bool HasSideEffects;
60
61 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
62 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
63};
64
65
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.
69//
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.
72//
73struct ValID {
74 enum {
75 LocalID, GlobalID, LocalName, GlobalName,
Chris Lattner28e64d32008-07-11 00:30:06 +000076 ConstSIntVal, ConstUIntVal, ConstAPInt, ConstFPVal, ConstNullVal,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
78 } Type;
79
80 union {
Chris Lattner28e64d32008-07-11 00:30:06 +000081 unsigned Num; // If it's a numeric reference like %1234
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 std::string *Name; // If it's a named reference. Memory must be deleted.
Chris Lattner28e64d32008-07-11 00:30:06 +000083 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
88 InlineAsmDescriptor *IAD;
Dale Johannesenb9de9f02007-09-06 18:13:44 +000089 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090
91 static ValID createLocalID(unsigned Num) {
92 ValID D; D.Type = LocalID; D.Num = Num; return D;
93 }
94 static ValID createGlobalID(unsigned Num) {
95 ValID D; D.Type = GlobalID; D.Num = Num; return D;
96 }
97 static ValID createLocalName(const std::string &Name) {
98 ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
99 }
100 static ValID createGlobalName(const std::string &Name) {
101 ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
102 }
103
104 static ValID create(int64_t Val) {
105 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
106 }
107
108 static ValID create(uint64_t Val) {
109 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
110 }
111
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000112 static ValID create(APFloat *Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
114 }
Chris Lattner28e64d32008-07-11 00:30:06 +0000115
116 static ValID create(const APInt &Val, bool isSigned) {
117 ValID D; D.Type = ConstAPInt;
118 D.ConstPoolInt = new APSInt(Val, !isSigned);
119 return D;
120 }
121
122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 static ValID createNull() {
124 ValID D; D.Type = ConstNullVal; return D;
125 }
126
127 static ValID createUndef() {
128 ValID D; D.Type = ConstUndefVal; return D;
129 }
130
131 static ValID createZeroInit() {
132 ValID D; D.Type = ConstZeroVal; return D;
133 }
134
135 static ValID create(Constant *Val) {
136 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
137 }
138
139 static ValID createInlineAsm(const std::string &AsmString,
140 const std::string &Constraints,
141 bool HasSideEffects) {
142 ValID D;
143 D.Type = InlineAsmVal;
144 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
145 return D;
146 }
147
148 inline void destroy() const {
149 if (Type == LocalName || Type == GlobalName)
150 delete Name; // Free this strdup'd memory.
151 else if (Type == InlineAsmVal)
152 delete IAD;
Chris Lattner28e64d32008-07-11 00:30:06 +0000153 else if (Type == ConstAPInt)
154 delete ConstPoolInt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 }
156
157 inline ValID copy() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 ValID Result = *this;
Chris Lattner28e64d32008-07-11 00:30:06 +0000159 if (Type == ConstAPInt)
160 Result.ConstPoolInt = new APSInt(*ConstPoolInt);
161
162 if (Type != LocalName && Type != GlobalName) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 Result.Name = new std::string(*Name);
164 return Result;
165 }
166
167 inline std::string getName() const {
168 switch (Type) {
169 case LocalID : return '%' + utostr(Num);
170 case GlobalID : return '@' + utostr(Num);
171 case LocalName : return *Name;
172 case GlobalName : return *Name;
Chris Lattner28e64d32008-07-11 00:30:06 +0000173 case ConstAPInt : return ConstPoolInt->toString();
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000174 case ConstFPVal : return ftostr(*ConstPoolFP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 case ConstNullVal : return "null";
176 case ConstUndefVal : return "undef";
177 case ConstZeroVal : return "zeroinitializer";
178 case ConstUIntVal :
179 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
180 case ConstantVal:
181 if (ConstantValue == ConstantInt::getTrue()) return "true";
182 if (ConstantValue == ConstantInt::getFalse()) return "false";
183 return "<constant expression>";
184 default:
185 assert(0 && "Unknown value!");
186 abort();
187 return "";
188 }
189 }
190
191 bool operator<(const ValID &V) const {
192 if (Type != V.Type) return Type < V.Type;
193 switch (Type) {
194 case LocalID:
195 case GlobalID: return Num < V.Num;
196 case LocalName:
197 case GlobalName: return *Name < *V.Name;
198 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
199 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
Chris Lattner28e64d32008-07-11 00:30:06 +0000200 case ConstAPInt : return ConstPoolInt->ult(*V.ConstPoolInt);
Dale Johannesenb9de9f02007-09-06 18:13:44 +0000201 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
202 APFloat::cmpLessThan;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 case ConstNullVal: return false;
204 case ConstUndefVal: return false;
205 case ConstZeroVal: return false;
206 case ConstantVal: return ConstantValue < V.ConstantValue;
207 default: assert(0 && "Unknown value type!"); return false;
208 }
209 }
210
211 bool operator==(const ValID &V) const {
Chris Lattner28e64d32008-07-11 00:30:06 +0000212 if (Type != V.Type) return false;
213
214 switch (Type) {
215 default: assert(0 && "Unknown value type!");
216 case LocalID:
217 case GlobalID: return Num == V.Num;
218 case LocalName:
219 case GlobalName: return *Name == *(V.Name);
220 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
221 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
222 case ConstAPInt: return *ConstPoolInt == *V.ConstPoolInt;
223 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
224 APFloat::cmpEqual;
225 case ConstantVal: return ConstantValue == V.ConstantValue;
226 case ConstNullVal: return true;
227 case ConstUndefVal: return true;
228 case ConstZeroVal: return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 }
231};
232
233struct TypeWithAttrs {
234 llvm::PATypeHolder *Ty;
Dale Johannesen387d7792008-02-19 21:40:10 +0000235 ParameterAttributes Attrs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236};
237
238typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
239
240struct ArgListEntry {
Dale Johannesen387d7792008-02-19 21:40:10 +0000241 ParameterAttributes Attrs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 llvm::PATypeHolder *Ty;
243 std::string *Name;
244};
245
246typedef std::vector<struct ArgListEntry> ArgListType;
247
Dale Johannesencfb19e62007-11-05 21:20:28 +0000248struct ParamListEntry {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 Value *Val;
Dale Johannesen387d7792008-02-19 21:40:10 +0000250 ParameterAttributes Attrs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251};
252
Dale Johannesencfb19e62007-11-05 21:20:28 +0000253typedef std::vector<ParamListEntry> ParamList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254
255
256} // End llvm namespace
257
258#endif