blob: bbf673782821682cdcb11771aba3c39b07da9578 [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
62
63// ValID - Represents a reference of a definition of some sort. This may either
64// be a numeric reference or a symbolic (%var) reference. This is just a
65// discriminated union.
66//
67// Note that I can't implement this class in a straight forward manner with
68// constructors and stuff because it goes in a union.
69//
70struct ValID {
71 enum {
72 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
73 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
74 } Type;
75
76 union {
77 int Num; // If it's a numeric reference
78 char *Name; // If it's a named reference. Memory must be free'd.
79 int64_t ConstPool64; // Constant pool reference. This is the value
80 uint64_t UConstPool64;// Unsigned constant pool reference.
81 double ConstPoolFP; // Floating point constant pool reference
82 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
83 InlineAsmDescriptor *IAD;
84 };
85
Reid Spencer44f87ee2007-03-15 03:25:34 +000086 static ValID create(int Num) {
87 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +000088 }
89
Reid Spencer44f87ee2007-03-15 03:25:34 +000090 static ValID create(char *Name) {
91 ValID D; D.Type = NameVal; D.Name = Name; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +000092 }
93
94 static ValID create(int64_t Val) {
Reid Spencer44f87ee2007-03-15 03:25:34 +000095 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +000096 }
97
98 static ValID create(uint64_t Val) {
Reid Spencer44f87ee2007-03-15 03:25:34 +000099 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000100 }
101
102 static ValID create(double Val) {
Reid Spencer44f87ee2007-03-15 03:25:34 +0000103 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000104 }
105
106 static ValID createNull() {
Reid Spencer44f87ee2007-03-15 03:25:34 +0000107 ValID D; D.Type = ConstNullVal; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000108 }
109
110 static ValID createUndef() {
Reid Spencer44f87ee2007-03-15 03:25:34 +0000111 ValID D; D.Type = ConstUndefVal; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000112 }
113
114 static ValID createZeroInit() {
Reid Spencer44f87ee2007-03-15 03:25:34 +0000115 ValID D; D.Type = ConstZeroVal; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000116 }
117
Reid Spencer44f87ee2007-03-15 03:25:34 +0000118 static ValID create(Constant *Val) {
119 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
Reid Spencerefd53d52007-01-26 08:18:34 +0000120 }
121
122 static ValID createInlineAsm(const std::string &AsmString,
123 const std::string &Constraints,
124 bool HasSideEffects) {
125 ValID D;
126 D.Type = InlineAsmVal;
127 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
128 return D;
129 }
130
131 inline void destroy() const {
132 if (Type == NameVal)
133 free(Name); // Free this strdup'd memory.
134 else if (Type == InlineAsmVal)
135 delete IAD;
136 }
137
138 inline ValID copy() const {
139 if (Type != NameVal) return *this;
140 ValID Result = *this;
141 Result.Name = strdup(Name);
142 return Result;
143 }
144
145 inline std::string getName() const {
146 switch (Type) {
147 case NumberVal : return std::string("#") + itostr(Num);
148 case NameVal : return Name;
149 case ConstFPVal : return ftostr(ConstPoolFP);
150 case ConstNullVal : return "null";
151 case ConstUndefVal : return "undef";
152 case ConstZeroVal : return "zeroinitializer";
153 case ConstUIntVal :
154 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
155 case ConstantVal:
156 if (ConstantValue == ConstantInt::get(Type::Int1Ty, true))
157 return "true";
158 if (ConstantValue == ConstantInt::get(Type::Int1Ty, false))
159 return "false";
160 return "<constant expression>";
161 default:
162 assert(0 && "Unknown value!");
163 abort();
164 return "";
165 }
166 }
167
168 bool operator<(const ValID &V) const {
169 if (Type != V.Type) return Type < V.Type;
170 switch (Type) {
171 case NumberVal: return Num < V.Num;
172 case NameVal: return strcmp(Name, V.Name) < 0;
173 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
174 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
175 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
176 case ConstNullVal: return false;
177 case ConstUndefVal: return false;
178 case ConstZeroVal: return false;
179 case ConstantVal: return ConstantValue < V.ConstantValue;
180 default: assert(0 && "Unknown value type!"); return false;
181 }
182 }
183};
184
Reid Spencer9373d272007-01-26 17:13:53 +0000185/// The following enums are used to keep track of prior opcodes. The lexer will
186/// retain the ability to parse obsolete opcode mnemonics and generates semantic
187/// values containing one of these enumerators.
Reid Spencerefd53d52007-01-26 08:18:34 +0000188enum TermOps {
189 RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp
190};
191
192enum BinaryOps {
193 AddOp, SubOp, MulOp,
194 DivOp, UDivOp, SDivOp, FDivOp,
195 RemOp, URemOp, SRemOp, FRemOp,
196 AndOp, OrOp, XorOp,
Reid Spencer832254e2007-02-02 02:16:23 +0000197 ShlOp, ShrOp, LShrOp, AShrOp,
Reid Spencerefd53d52007-01-26 08:18:34 +0000198 SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT
199};
200
201enum MemoryOps {
202 MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp
203};
204
205enum OtherOps {
Reid Spencer832254e2007-02-02 02:16:23 +0000206 PHIOp, CallOp, SelectOp, UserOp1, UserOp2, VAArg,
Reid Spencerefd53d52007-01-26 08:18:34 +0000207 ExtractElementOp, InsertElementOp, ShuffleVectorOp,
Reid Spencer832254e2007-02-02 02:16:23 +0000208 ICmpOp, FCmpOp
Reid Spencerefd53d52007-01-26 08:18:34 +0000209};
210
211enum CastOps {
212 CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp,
213 UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp
214};
215
Reid Spencer43f76c92007-01-29 05:41:09 +0000216// An enumeration for the old calling conventions, ala LLVM 1.9
217namespace OldCallingConv {
218 enum ID {
Reid Spencer2dcb5832007-02-08 09:08:23 +0000219 C = 0, CSRet = 1, Fast = 8, Cold = 9, X86_StdCall = 64, X86_FastCall = 65,
220 None = 99999
Reid Spencer43f76c92007-01-29 05:41:09 +0000221 };
222}
223
Reid Spencer44f87ee2007-03-15 03:25:34 +0000224/// An enumeration for defining the Signedness of a type or value. Signless
225/// means the signedness is not relevant to the type or value.
226enum Signedness { Signless, Unsigned, Signed };
227
Reid Spencer9373d272007-01-26 17:13:53 +0000228/// These structures are used as the semantic values returned from various
229/// productions in the grammar. They simply bundle an LLVM IR object with
230/// its Signedness value. These help track signedness through the various
231/// productions.
Reid Spencerefd53d52007-01-26 08:18:34 +0000232struct TypeInfo {
233 const llvm::Type *T;
234 Signedness S;
235};
236
237struct PATypeInfo {
Reid Spencer2dcb5832007-02-08 09:08:23 +0000238 llvm::PATypeHolder* PAT;
Reid Spencerefd53d52007-01-26 08:18:34 +0000239 Signedness S;
240};
241
242struct ConstInfo {
243 llvm::Constant* C;
244 Signedness S;
245};
246
247struct ValueInfo {
248 llvm::Value* V;
249 Signedness S;
250};
251
252struct InstrInfo {
253 llvm::Instruction *I;
254 Signedness S;
255};
256
257struct PHIListInfo {
258 std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P;
259 Signedness S;
260};
261
262} // End llvm namespace
Reid Spencer90eb4d62007-01-05 17:18:58 +0000263
264#endif