1 //===-- ParserInternals.h - Definitions internal to the parser ---*- C++ -*--=//
3 // This header file defines the various variables that are shared among the
4 // different components of the parser...
6 //===----------------------------------------------------------------------===//
8 #ifndef PARSER_INTERNALS_H
9 #define PARSER_INTERNALS_H
12 #define __STDC_LIMIT_MACROS
14 #include "llvm/InstrTypes.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/ConstPoolVals.h"
17 #include "llvm/iOther.h"
18 #include "llvm/Method.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Assembly/Parser.h"
21 #include "llvm/Support/StringExtras.h"
25 // Global variables exported from the lexer...
26 extern FILE *llvmAsmin;
27 extern int llvmAsmlineno;
29 // Globals exported by the parser...
30 extern string CurFilename;
31 Module *RunVMAsmParser(const string &Filename, FILE *F);
34 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
35 // appropriate character. If AllowNull is set to false, a \00 value will cause
36 // an exception to be thrown.
38 // If AllowNull is set to true, the return value of the function points to the
39 // last character of the string in memory.
41 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
44 // ThrowException - Wrapper around the ParseException class that automatically
45 // fills in file line number and column number and options info.
47 // This also helps me because I keep typing 'throw new ParseException' instead
48 // of just 'throw ParseException'... sigh...
50 static inline void ThrowException(const string &message,
52 if (LineNo == -1) LineNo = llvmAsmlineno;
53 // TODO: column number in exception
54 throw ParseException(CurFilename, message, LineNo);
57 // ValID - Represents a reference of a definition of some sort. This may either
58 // be a numeric reference or a symbolic (%var) reference. This is just a
59 // discriminated union.
61 // Note that I can't implement this class in a straight forward manner with
62 // constructors and stuff because it goes in a union, and GCC doesn't like
63 // putting classes with ctor's in unions. :(
67 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstStringVal,
68 ConstFPVal, ConstNullVal
72 int Num; // If it's a numeric reference
73 char *Name; // If it's a named reference. Memory must be free'd.
74 int64_t ConstPool64; // Constant pool reference. This is the value
75 uint64_t UConstPool64;// Unsigned constant pool reference.
76 double ConstPoolFP; // Floating point constant pool reference
79 static ValID create(int Num) {
80 ValID D; D.Type = NumberVal; D.Num = Num; return D;
83 static ValID create(char *Name) {
84 ValID D; D.Type = NameVal; D.Name = Name; return D;
87 static ValID create(int64_t Val) {
88 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
91 static ValID create(uint64_t Val) {
92 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
95 static ValID create_conststr(char *Name) {
96 ValID D; D.Type = ConstStringVal; D.Name = Name; return D;
99 static ValID create(double Val) {
100 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
103 static ValID createNull() {
104 ValID D; D.Type = ConstNullVal; return D;
107 inline void destroy() const {
108 if (Type == NameVal || Type == ConstStringVal)
109 free(Name); // Free this strdup'd memory...
112 inline ValID copy() const {
113 if (Type != NameVal && Type != ConstStringVal) return *this;
114 ValID Result = *this;
115 Result.Name = strdup(Name);
119 inline string getName() const {
121 case NumberVal : return string("#") + itostr(Num);
122 case NameVal : return Name;
123 case ConstStringVal: return string("\"") + Name + string("\"");
124 case ConstFPVal : return ftostr(ConstPoolFP);
125 case ConstNullVal : return "null";
127 case ConstSIntVal : return string("%") + itostr(ConstPool64);
129 assert(0 && "Unknown value!");
137 template<class SuperType>
138 class PlaceholderValue : public SuperType {
142 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
143 LineNum = llvmAsmlineno;
145 ValID &getDef() { return D; }
146 int getLineNum() const { return LineNum; }
149 struct TypePlaceHolderHelper : public OpaqueType {
150 TypePlaceHolderHelper(const Type *Ty) : OpaqueType() {
151 assert(Ty == Type::TypeTy);
156 struct InstPlaceHolderHelper : public Instruction {
157 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
159 virtual Instruction *clone() const { abort(); }
160 virtual const char *getOpcodeName() const { return "placeholder"; }
163 struct BBPlaceHolderHelper : public BasicBlock {
164 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
165 assert(Ty->isLabelType());
169 struct MethPlaceHolderHelper : public Method {
170 MethPlaceHolderHelper(const Type *Ty) : Method(cast<const MethodType>(Ty)) {}
173 typedef PlaceholderValue<TypePlaceHolderHelper> TypePlaceHolder;
174 typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
175 typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
176 typedef PlaceholderValue<MethPlaceHolderHelper> MethPlaceHolder;
178 static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
179 const Type *Ty = Val->getType();
180 if (isa<PointerType>(Ty) &&
181 isa<MethodType>(cast<PointerType>(Ty)->getValueType()))
182 Ty = cast<PointerType>(Ty)->getValueType();
184 switch (Ty->getPrimitiveID()) {
185 case Type::TypeTyID: return ((TypePlaceHolder*)Val)->getDef();
186 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
187 case Type::MethodTyID: return ((MethPlaceHolder*)Val)->getDef();
188 default: return ((ValuePlaceHolder*)Val)->getDef();
192 static inline int getLineNumFromPlaceHolder(const Value *Val) {
193 const Type *Ty = Val->getType();
194 if (isa<PointerType>(Ty) &&
195 isa<MethodType>(cast<PointerType>(Ty)->getValueType()))
196 Ty = cast<PointerType>(Ty)->getValueType();
198 switch (Ty->getPrimitiveID()) {
199 case Type::TypeTyID: return ((TypePlaceHolder*)Val)->getLineNum();
200 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
201 case Type::MethodTyID: return ((MethPlaceHolder*)Val)->getLineNum();
202 default: return ((ValuePlaceHolder*)Val)->getLineNum();