blob: 7a0446a0111fa2268e31566d84d7da20c56418a7 [file] [log] [blame]
Reid Spencerefd53d52007-01-26 08:18:34 +00001//===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
Reid Spencer90eb4d62007-01-05 17:18:58 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencerefd53d52007-01-26 08:18:34 +00005// This file was developed by the LLVM research group and is distributed under
Reid Spencer90eb4d62007-01-05 17:18:58 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencerefd53d52007-01-26 08:18:34 +000010// This header file defines the various variables that are shared among the
11// different components of the parser...
Reid Spencer90eb4d62007-01-05 17:18:58 +000012//
13//===----------------------------------------------------------------------===//
14
Reid Spencerefd53d52007-01-26 08:18:34 +000015#ifndef PARSER_INTERNALS_H
16#define PARSER_INTERNALS_H
Reid Spencer90eb4d62007-01-05 17:18:58 +000017
Reid Spencerefd53d52007-01-26 08:18:34 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/Instructions.h"
22#include "llvm/ADT/StringExtras.h"
23#include <list>
24
Reid Spencer90eb4d62007-01-05 17:18:58 +000025
Reid Spencere0a15bb2007-01-15 00:25:53 +000026// Global variables exported from the lexer.
Reid Spencerefd53d52007-01-26 08:18:34 +000027extern int yydebug;
28extern void error(const std::string& msg, int line = -1);
Reid Spencer90eb4d62007-01-05 17:18:58 +000029extern char* Upgradetext;
30extern int Upgradeleng;
Reid Spencerefd53d52007-01-26 08:18:34 +000031extern int Upgradelineno;
Reid Spencer90eb4d62007-01-05 17:18:58 +000032
Reid Spencerefd53d52007-01-26 08:18:34 +000033namespace llvm {
Reid Spencer90eb4d62007-01-05 17:18:58 +000034
Reid Spencerefd53d52007-01-26 08:18:34 +000035
36class Module;
37Module* UpgradeAssembly(const std::string &infile, std::istream& in,
38 bool debug, bool addAttrs);
39
40
Reid Spencerefd53d52007-01-26 08:18:34 +000041extern std::istream* LexInput;
Reid Spencer90eb4d62007-01-05 17:18:58 +000042
Reid Spencerefd53d52007-01-26 08:18:34 +000043// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
44// appropriate character. If AllowNull is set to false, a \00 value will cause
45// an error.
46//
47// If AllowNull is set to true, the return value of the function points to the
48// last character of the string in memory.
49//
50char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
Reid Spencer90eb4d62007-01-05 17:18:58 +000051
Reid Spencerefd53d52007-01-26 08:18:34 +000052/// InlineAsmDescriptor - This is a simple class that holds info about inline
53/// asm blocks, for use by ValID.
54struct InlineAsmDescriptor {
55 std::string AsmString, Constraints;
56 bool HasSideEffects;
57
58 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
59 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
60};
61
Reid Spencerc1882212007-03-14 23:08:04 +000062/// An enumeration for defining the Signedness of a type or value. Signless
63/// means the signedness is not relevant to the type or value.
64enum Signedness { Signless, Unsigned, Signed };
65
Reid Spencerefd53d52007-01-26 08:18:34 +000066
67// ValID - Represents a reference of a definition of some sort. This may either
68// be a numeric reference or a symbolic (%var) reference. This is just a
69// discriminated union.
70//
71// Note that I can't implement this class in a straight forward manner with
72// constructors and stuff because it goes in a union.
73//
74struct ValID {
75 enum {
76 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
77 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
78 } Type;
79
80 union {
81 int Num; // If it's a numeric reference
82 char *Name; // If it's a named reference. Memory must be free'd.
83 int64_t ConstPool64; // Constant pool reference. This is the value
84 uint64_t UConstPool64;// Unsigned constant pool reference.
85 double ConstPoolFP; // Floating point constant pool reference
86 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
87 InlineAsmDescriptor *IAD;
88 };
Reid Spencerc1882212007-03-14 23:08:04 +000089 Signedness S;
Reid Spencerefd53d52007-01-26 08:18:34 +000090
Reid Spencerc1882212007-03-14 23:08:04 +000091 static ValID create(int Num, Signedness Sign) {
92 ValID D; D.Type = NumberVal; D.Num = Num; D.S = Sign;
93 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +000094 }
95
Reid Spencerc1882212007-03-14 23:08:04 +000096 static ValID create(char *Name, Signedness Sign) {
97 ValID D; D.Type = NameVal; D.Name = Name; D.S = Sign;
98 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +000099 }
100
101 static ValID create(int64_t Val) {
Reid Spencerc1882212007-03-14 23:08:04 +0000102 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; D.S = Signed;
103 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000104 }
105
106 static ValID create(uint64_t Val) {
Reid Spencerc1882212007-03-14 23:08:04 +0000107 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; D.S = Unsigned;
108 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000109 }
110
111 static ValID create(double Val) {
Reid Spencerc1882212007-03-14 23:08:04 +0000112 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; D.S = Signless;
113 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000114 }
115
116 static ValID createNull() {
Reid Spencerc1882212007-03-14 23:08:04 +0000117 ValID D; D.Type = ConstNullVal; D.S = Signless;
118 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000119 }
120
121 static ValID createUndef() {
Reid Spencerc1882212007-03-14 23:08:04 +0000122 ValID D; D.Type = ConstUndefVal; D.S = Signless;
123 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000124 }
125
126 static ValID createZeroInit() {
Reid Spencerc1882212007-03-14 23:08:04 +0000127 ValID D; D.Type = ConstZeroVal; D.S = Signless;
128 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000129 }
130
Reid Spencerc1882212007-03-14 23:08:04 +0000131 static ValID create(Constant *Val, Signedness Sign) {
132 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; D.S = Sign;
133 return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000134 }
135
136 static ValID createInlineAsm(const std::string &AsmString,
137 const std::string &Constraints,
138 bool HasSideEffects) {
139 ValID D;
140 D.Type = InlineAsmVal;
141 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
142 return D;
143 }
144
145 inline void destroy() const {
146 if (Type == NameVal)
147 free(Name); // Free this strdup'd memory.
148 else if (Type == InlineAsmVal)
149 delete IAD;
150 }
151
152 inline ValID copy() const {
153 if (Type != NameVal) return *this;
154 ValID Result = *this;
155 Result.Name = strdup(Name);
156 return Result;
157 }
158
159 inline std::string getName() const {
160 switch (Type) {
161 case NumberVal : return std::string("#") + itostr(Num);
162 case NameVal : return Name;
163 case ConstFPVal : return ftostr(ConstPoolFP);
164 case ConstNullVal : return "null";
165 case ConstUndefVal : return "undef";
166 case ConstZeroVal : return "zeroinitializer";
167 case ConstUIntVal :
168 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
169 case ConstantVal:
170 if (ConstantValue == ConstantInt::get(Type::Int1Ty, true))
171 return "true";
172 if (ConstantValue == ConstantInt::get(Type::Int1Ty, false))
173 return "false";
174 return "<constant expression>";
175 default:
176 assert(0 && "Unknown value!");
177 abort();
178 return "";
179 }
180 }
181
182 bool operator<(const ValID &V) const {
183 if (Type != V.Type) return Type < V.Type;
184 switch (Type) {
185 case NumberVal: return Num < V.Num;
186 case NameVal: return strcmp(Name, V.Name) < 0;
187 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
188 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
189 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
190 case ConstNullVal: return false;
191 case ConstUndefVal: return false;
192 case ConstZeroVal: return false;
193 case ConstantVal: return ConstantValue < V.ConstantValue;
194 default: assert(0 && "Unknown value type!"); return false;
195 }
196 }
197};
198
Reid Spencer9373d272007-01-26 17:13:53 +0000199/// The following enums are used to keep track of prior opcodes. The lexer will
200/// retain the ability to parse obsolete opcode mnemonics and generates semantic
201/// values containing one of these enumerators.
Reid Spencerefd53d52007-01-26 08:18:34 +0000202enum TermOps {
203 RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp
204};
205
206enum BinaryOps {
207 AddOp, SubOp, MulOp,
208 DivOp, UDivOp, SDivOp, FDivOp,
209 RemOp, URemOp, SRemOp, FRemOp,
210 AndOp, OrOp, XorOp,
Reid Spencer832254e2007-02-02 02:16:23 +0000211 ShlOp, ShrOp, LShrOp, AShrOp,
Reid Spencerefd53d52007-01-26 08:18:34 +0000212 SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT
213};
214
215enum MemoryOps {
216 MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp
217};
218
219enum OtherOps {
Reid Spencer832254e2007-02-02 02:16:23 +0000220 PHIOp, CallOp, SelectOp, UserOp1, UserOp2, VAArg,
Reid Spencerefd53d52007-01-26 08:18:34 +0000221 ExtractElementOp, InsertElementOp, ShuffleVectorOp,
Reid Spencer832254e2007-02-02 02:16:23 +0000222 ICmpOp, FCmpOp
Reid Spencerefd53d52007-01-26 08:18:34 +0000223};
224
225enum CastOps {
226 CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp,
227 UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp
228};
229
Reid Spencer43f76c92007-01-29 05:41:09 +0000230// An enumeration for the old calling conventions, ala LLVM 1.9
231namespace OldCallingConv {
232 enum ID {
Reid Spencer2dcb5832007-02-08 09:08:23 +0000233 C = 0, CSRet = 1, Fast = 8, Cold = 9, X86_StdCall = 64, X86_FastCall = 65,
234 None = 99999
Reid Spencer43f76c92007-01-29 05:41:09 +0000235 };
236}
237
Reid Spencer9373d272007-01-26 17:13:53 +0000238/// These structures are used as the semantic values returned from various
239/// productions in the grammar. They simply bundle an LLVM IR object with
240/// its Signedness value. These help track signedness through the various
241/// productions.
Reid Spencerefd53d52007-01-26 08:18:34 +0000242struct TypeInfo {
243 const llvm::Type *T;
244 Signedness S;
Reid Spencerc1882212007-03-14 23:08:04 +0000245 bool operator<(const TypeInfo& that) const {
246 if (this == &that)
247 return false;
248 return (T < that.T) || (T == that.T && S < that.S);
249 }
250 bool operator==(const TypeInfo& that) const {
251 if (this == &that)
252 return true;
253 return T == that.T && S == that.S;
254 }
Reid Spencerefd53d52007-01-26 08:18:34 +0000255};
256
257struct PATypeInfo {
Reid Spencer2dcb5832007-02-08 09:08:23 +0000258 llvm::PATypeHolder* PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +0000259 Signedness S;
260};
261
262struct ConstInfo {
263 llvm::Constant* C;
264 Signedness S;
265};
266
267struct ValueInfo {
268 llvm::Value* V;
269 Signedness S;
270};
271
272struct InstrInfo {
273 llvm::Instruction *I;
274 Signedness S;
275};
276
277struct PHIListInfo {
278 std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P;
279 Signedness S;
280};
281
282} // End llvm namespace
Reid Spencer90eb4d62007-01-05 17:18:58 +0000283
284#endif