1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/ADT/StringExtras.h"
26 // Global variables exported from the lexer.
28 extern void error(const std::string& msg, int line = -1);
29 extern char* Upgradetext;
30 extern int Upgradeleng;
31 extern int Upgradelineno;
37 Module* UpgradeAssembly(const std::string &infile, std::istream& in,
38 bool debug, bool addAttrs);
41 extern std::istream* LexInput;
43 // 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
47 // If AllowNull is set to true, the return value of the function points to the
48 // last character of the string in memory.
50 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
52 /// InlineAsmDescriptor - This is a simple class that holds info about inline
53 /// asm blocks, for use by ValID.
54 struct InlineAsmDescriptor {
55 std::string AsmString, Constraints;
58 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
59 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
62 /// An enumeration for defining the Signedness of a type or value. Signless
63 /// means the signedness is not relevant to the type or value.
64 enum Signedness { Signless, Unsigned, Signed };
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.
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.
76 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
77 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
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;
91 static ValID create(int Num, Signedness Sign) {
92 ValID D; D.Type = NumberVal; D.Num = Num; D.S = Sign;
96 static ValID create(char *Name, Signedness Sign) {
97 ValID D; D.Type = NameVal; D.Name = Name; D.S = Sign;
101 static ValID create(int64_t Val) {
102 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; D.S = Signed;
106 static ValID create(uint64_t Val) {
107 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; D.S = Unsigned;
111 static ValID create(double Val) {
112 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; D.S = Signless;
116 static ValID createNull() {
117 ValID D; D.Type = ConstNullVal; D.S = Signless;
121 static ValID createUndef() {
122 ValID D; D.Type = ConstUndefVal; D.S = Signless;
126 static ValID createZeroInit() {
127 ValID D; D.Type = ConstZeroVal; D.S = Signless;
131 static ValID create(Constant *Val, Signedness Sign) {
132 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; D.S = Sign;
136 static ValID createInlineAsm(const std::string &AsmString,
137 const std::string &Constraints,
138 bool HasSideEffects) {
140 D.Type = InlineAsmVal;
141 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
145 inline void destroy() const {
147 free(Name); // Free this strdup'd memory.
148 else if (Type == InlineAsmVal)
152 inline ValID copy() const {
153 if (Type != NameVal) return *this;
154 ValID Result = *this;
155 Result.Name = strdup(Name);
159 inline std::string getName() const {
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";
168 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
170 if (ConstantValue == ConstantInt::get(Type::Int1Ty, true))
172 if (ConstantValue == ConstantInt::get(Type::Int1Ty, false))
174 return "<constant expression>";
176 assert(0 && "Unknown value!");
182 bool operator<(const ValID &V) const {
183 if (Type != V.Type) return Type < V.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;
199 /// 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.
203 RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp
208 DivOp, UDivOp, SDivOp, FDivOp,
209 RemOp, URemOp, SRemOp, FRemOp,
211 ShlOp, ShrOp, LShrOp, AShrOp,
212 SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT
216 MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp
220 PHIOp, CallOp, SelectOp, UserOp1, UserOp2, VAArg,
221 ExtractElementOp, InsertElementOp, ShuffleVectorOp,
226 CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp,
227 UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp
230 // An enumeration for the old calling conventions, ala LLVM 1.9
231 namespace OldCallingConv {
233 C = 0, CSRet = 1, Fast = 8, Cold = 9, X86_StdCall = 64, X86_FastCall = 65,
238 /// 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
245 bool operator<(const TypeInfo& that) const {
248 return (T < that.T) || (T == that.T && S < that.S);
250 bool operator==(const TypeInfo& that) const {
253 return T == that.T && S == that.S;
258 llvm::PATypeHolder* PAT;
273 llvm::Instruction *I;
278 std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P;
282 } // End llvm namespace