e3eb3480af976a81167777ba582a9fe2b4bdcc01
[oota-llvm.git] / lib / AsmParser / ParserInternals.h
1 //===-- ParserInternals.h - Definitions internal to the parser ---*- C++ -*--=//
2 //
3 //  This header file defines the various variables that are shared among the 
4 //  different components of the parser...
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef PARSER_INTERNALS_H
9 #define PARSER_INTERNALS_H
10
11 #define __STDC_LIMIT_MACROS
12 #include "llvm/BasicBlock.h"
13 #include "llvm/Constants.h"
14 #include "llvm/iOther.h"
15 #include "llvm/Function.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Assembly/Parser.h"
18 #include "Support/StringExtras.h"
19
20 class Module;
21
22 // Global variables exported from the lexer...
23 extern std::FILE *llvmAsmin;
24 extern int llvmAsmlineno;
25
26 // Globals exported by the parser...
27 extern std::string CurFilename;
28 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
29
30 extern char* llvmAsmtext;
31 extern int   llvmAsmleng;
32
33 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
34 // appropriate character.  If AllowNull is set to false, a \00 value will cause
35 // an exception to be thrown.
36 //
37 // If AllowNull is set to true, the return value of the function points to the
38 // last character of the string in memory.
39 //
40 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
41
42
43 // ThrowException - Wrapper around the ParseException class that automatically
44 // fills in file line number and column number and options info.
45 //
46 // This also helps me because I keep typing 'throw new ParseException' instead 
47 // of just 'throw ParseException'... sigh...
48 //
49 static inline void ThrowException(const std::string &message,
50                                   int LineNo = -1) {
51   if (LineNo == -1) LineNo = llvmAsmlineno;
52   // TODO: column number in exception
53   throw ParseException(CurFilename, message, LineNo);
54 }
55
56 // ValID - Represents a reference of a definition of some sort.  This may either
57 // be a numeric reference or a symbolic (%var) reference.  This is just a 
58 // discriminated union.
59 //
60 // Note that I can't implement this class in a straight forward manner with 
61 // constructors and stuff because it goes in a union, and GCC doesn't like 
62 // putting classes with ctor's in unions.  :(
63 //
64 struct ValID {
65   enum {
66     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
67     ConstantVal,
68   } Type;
69
70   union {
71     int      Num;         // If it's a numeric reference
72     char    *Name;        // If it's a named reference.  Memory must be free'd.
73     int64_t  ConstPool64; // Constant pool reference.  This is the value
74     uint64_t UConstPool64;// Unsigned constant pool reference.
75     double   ConstPoolFP; // Floating point constant pool reference
76     Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
77   };
78
79   static ValID create(int Num) {
80     ValID D; D.Type = NumberVal; D.Num = Num; return D;
81   }
82
83   static ValID create(char *Name) {
84     ValID D; D.Type = NameVal; D.Name = Name; return D;
85   }
86
87   static ValID create(int64_t Val) {
88     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
89   }
90
91   static ValID create(uint64_t Val) {
92     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
93   }
94
95   static ValID create(double Val) {
96     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
97   }
98
99   static ValID createNull() {
100     ValID D; D.Type = ConstNullVal; return D;
101   }
102
103   static ValID create(Constant *Val) {
104     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
105   }
106
107   inline void destroy() const {
108     if (Type == NameVal)
109       free(Name);    // Free this strdup'd memory...
110   }
111
112   inline ValID copy() const {
113     if (Type != NameVal) return *this;
114     ValID Result = *this;
115     Result.Name = strdup(Name);
116     return Result;
117   }
118
119   inline std::string getName() const {
120     switch (Type) {
121     case NumberVal     : return std::string("#") + itostr(Num);
122     case NameVal       : return Name;
123     case ConstFPVal    : return ftostr(ConstPoolFP);
124     case ConstNullVal  : return "null";
125     case ConstUIntVal  :
126     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
127     case ConstantVal:
128       if (ConstantValue == ConstantBool::True) return "true";
129       if (ConstantValue == ConstantBool::False) return "false";
130       return "<constant expression>";
131     default:
132       assert(0 && "Unknown value!");
133       abort();
134       return "";
135     }
136   }
137
138   bool operator<(const ValID &V) const {
139     if (Type != V.Type) return Type < V.Type;
140     switch (Type) {
141     case NumberVal:     return Num < V.Num;
142     case NameVal:       return strcmp(Name, V.Name) < 0;
143     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
144     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
145     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
146     case ConstNullVal:  return false;
147     case ConstantVal:   return ConstantValue < V.ConstantValue;
148     default:  assert(0 && "Unknown value type!"); return false;
149     }
150   }
151 };
152
153
154
155 template<class SuperType>
156 class PlaceholderValue : public SuperType {
157   ValID D;
158   int LineNum;
159 public:
160   PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
161     LineNum = llvmAsmlineno;
162   }
163   ValID &getDef() { return D; }
164   int getLineNum() const { return LineNum; }
165 };
166
167 struct InstPlaceHolderHelper : public Instruction {
168   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
169
170   virtual Instruction *clone() const { abort(); return 0; }
171   virtual const char *getOpcodeName() const { return "placeholder"; }
172 };
173
174 struct BBPlaceHolderHelper : public BasicBlock {
175   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
176     assert(Ty == Type::LabelTy);
177   }
178 };
179
180 struct MethPlaceHolderHelper : public Function {
181   MethPlaceHolderHelper(const Type *Ty)
182     : Function(cast<FunctionType>(Ty), true) {}
183 };
184
185 typedef PlaceholderValue<InstPlaceHolderHelper>  ValuePlaceHolder;
186 typedef PlaceholderValue<BBPlaceHolderHelper>    BBPlaceHolder;
187
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();
193
194   switch (Ty->getPrimitiveID()) {
195   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getDef();
196   default:               return ((ValuePlaceHolder*)Val)->getDef();
197   }
198 }
199
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();
205
206   switch (Ty->getPrimitiveID()) {
207   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getLineNum();
208   default:               return ((ValuePlaceHolder*)Val)->getLineNum();
209   }
210 }
211
212 #endif