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) {}
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.
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.
72 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
73 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
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;
86 static ValID create(int Num) {
87 ValID D; D.Type = NumberVal; D.Num = Num; return D;
90 static ValID create(char *Name) {
91 ValID D; D.Type = NameVal; D.Name = Name; return D;
94 static ValID create(int64_t Val) {
95 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
98 static ValID create(uint64_t Val) {
99 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
102 static ValID create(double Val) {
103 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
106 static ValID createNull() {
107 ValID D; D.Type = ConstNullVal; return D;
110 static ValID createUndef() {
111 ValID D; D.Type = ConstUndefVal; return D;
114 static ValID createZeroInit() {
115 ValID D; D.Type = ConstZeroVal; return D;
118 static ValID create(Constant *Val) {
119 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
122 static ValID createInlineAsm(const std::string &AsmString,
123 const std::string &Constraints,
124 bool HasSideEffects) {
126 D.Type = InlineAsmVal;
127 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
131 inline void destroy() const {
133 free(Name); // Free this strdup'd memory.
134 else if (Type == InlineAsmVal)
138 inline ValID copy() const {
139 if (Type != NameVal) return *this;
140 ValID Result = *this;
141 Result.Name = strdup(Name);
145 inline std::string getName() const {
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";
154 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
156 if (ConstantValue == ConstantInt::get(Type::Int1Ty, true))
158 if (ConstantValue == ConstantInt::get(Type::Int1Ty, false))
160 return "<constant expression>";
162 assert(0 && "Unknown value!");
168 bool operator<(const ValID &V) const {
169 if (Type != V.Type) return Type < V.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;
185 /// 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.
189 RetOp, BrOp, SwitchOp, InvokeOp, UnwindOp, UnreachableOp
194 DivOp, UDivOp, SDivOp, FDivOp,
195 RemOp, URemOp, SRemOp, FRemOp,
197 SetEQ, SetNE, SetLE, SetGE, SetLT, SetGT
201 MallocOp, FreeOp, AllocaOp, LoadOp, StoreOp, GetElementPtrOp
205 PHIOp, CallOp, ShlOp, ShrOp, SelectOp, UserOp1, UserOp2, VAArg,
206 ExtractElementOp, InsertElementOp, ShuffleVectorOp,
212 CastOp, TruncOp, ZExtOp, SExtOp, FPTruncOp, FPExtOp, FPToUIOp, FPToSIOp,
213 UIToFPOp, SIToFPOp, PtrToIntOp, IntToPtrOp, BitCastOp
216 // An enumeration for the old calling conventions, ala LLVM 1.9
217 namespace OldCallingConv {
219 C = 0, CSRet = 1, Fast = 8, Cold = 9, X86_StdCall = 64, X86_FastCall = 65
223 /// An enumeration for defining the Signedness of a type or value. Signless
224 /// means the signedness is not relevant to the type or value.
225 enum Signedness { Signless, Unsigned, Signed };
227 /// These structures are used as the semantic values returned from various
228 /// productions in the grammar. They simply bundle an LLVM IR object with
229 /// its Signedness value. These help track signedness through the various
237 llvm::PATypeHolder* T;
252 llvm::Instruction *I;
257 std::list<std::pair<llvm::Value*, llvm::BasicBlock*> > *P;
261 } // End llvm namespace