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/iOther.h"
20 #include "llvm/Function.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Assembly/Parser.h"
23 #include "Support/StringExtras.h"
27 // Global variables exported from the lexer...
28 extern std::FILE *llvmAsmin;
29 extern int llvmAsmlineno;
31 // Globals exported by the parser...
32 extern std::string CurFilename;
33 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
35 extern char* llvmAsmtext;
36 extern int llvmAsmleng;
38 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
39 // appropriate character. If AllowNull is set to false, a \00 value will cause
40 // an exception to be thrown.
42 // If AllowNull is set to true, the return value of the function points to the
43 // last character of the string in memory.
45 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
48 // ThrowException - Wrapper around the ParseException class that automatically
49 // fills in file line number and column number and options info.
51 // This also helps me because I keep typing 'throw new ParseException' instead
52 // of just 'throw ParseException'... sigh...
54 static inline void ThrowException(const std::string &message,
56 if (LineNo == -1) LineNo = llvmAsmlineno;
57 // TODO: column number in exception
58 throw ParseException(CurFilename, message, LineNo);
61 // ValID - Represents a reference of a definition of some sort. This may either
62 // be a numeric reference or a symbolic (%var) reference. This is just a
63 // discriminated union.
65 // Note that I can't implement this class in a straight forward manner with
66 // constructors and stuff because it goes in a union, and GCC doesn't like
67 // putting classes with ctor's in unions. :(
71 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
76 int Num; // If it's a numeric reference
77 char *Name; // If it's a named reference. Memory must be free'd.
78 int64_t ConstPool64; // Constant pool reference. This is the value
79 uint64_t UConstPool64;// Unsigned constant pool reference.
80 double ConstPoolFP; // Floating point constant pool reference
81 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
84 static ValID create(int Num) {
85 ValID D; D.Type = NumberVal; D.Num = Num; return D;
88 static ValID create(char *Name) {
89 ValID D; D.Type = NameVal; D.Name = Name; return D;
92 static ValID create(int64_t Val) {
93 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
96 static ValID create(uint64_t Val) {
97 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
100 static ValID create(double Val) {
101 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
104 static ValID createNull() {
105 ValID D; D.Type = ConstNullVal; return D;
108 static ValID create(Constant *Val) {
109 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
112 inline void destroy() const {
114 free(Name); // Free this strdup'd memory...
117 inline ValID copy() const {
118 if (Type != NameVal) return *this;
119 ValID Result = *this;
120 Result.Name = strdup(Name);
124 inline std::string getName() const {
126 case NumberVal : return std::string("#") + itostr(Num);
127 case NameVal : return Name;
128 case ConstFPVal : return ftostr(ConstPoolFP);
129 case ConstNullVal : return "null";
131 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
133 if (ConstantValue == ConstantBool::True) return "true";
134 if (ConstantValue == ConstantBool::False) return "false";
135 return "<constant expression>";
137 assert(0 && "Unknown value!");
143 bool operator<(const ValID &V) const {
144 if (Type != V.Type) return Type < V.Type;
146 case NumberVal: return Num < V.Num;
147 case NameVal: return strcmp(Name, V.Name) < 0;
148 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
149 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
150 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
151 case ConstNullVal: return false;
152 case ConstantVal: return ConstantValue < V.ConstantValue;
153 default: assert(0 && "Unknown value type!"); return false;
160 template<class SuperType>
161 class PlaceholderValue : public SuperType {
165 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
166 LineNum = llvmAsmlineno;
168 ValID &getDef() { return D; }
169 int getLineNum() const { return LineNum; }
172 struct InstPlaceHolderHelper : public Instruction {
173 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
175 virtual Instruction *clone() const { abort(); return 0; }
176 virtual const char *getOpcodeName() const { return "placeholder"; }
179 struct BBPlaceHolderHelper : public BasicBlock {
180 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
181 assert(Ty == Type::LabelTy);
185 typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
186 typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
188 static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
189 const Type *Ty = Val->getType();
190 if (isa<PointerType>(Ty) &&
191 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
192 Ty = cast<PointerType>(Ty)->getElementType();
194 switch (Ty->getPrimitiveID()) {
195 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
196 default: return ((ValuePlaceHolder*)Val)->getDef();
200 static inline int getLineNumFromPlaceHolder(const Value *Val) {
201 const Type *Ty = Val->getType();
202 if (isa<PointerType>(Ty) &&
203 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
204 Ty = cast<PointerType>(Ty)->getElementType();
206 switch (Ty->getPrimitiveID()) {
207 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
208 default: return ((ValuePlaceHolder*)Val)->getLineNum();