c7837959e61d0a25c6b92743014b38add094bad0
[oota-llvm.git] / lib / AsmParser / ParserInternals.h
1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file defines the various variables that are shared among the 
11 //  different components of the parser...
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
17
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"
24
25 class Module;
26
27 // Global variables exported from the lexer...
28 extern std::FILE *llvmAsmin;
29 extern int llvmAsmlineno;
30
31 // Globals exported by the parser...
32 extern std::string CurFilename;
33 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
34
35 extern char* llvmAsmtext;
36 extern int   llvmAsmleng;
37
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.
41 //
42 // If AllowNull is set to true, the return value of the function points to the
43 // last character of the string in memory.
44 //
45 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
46
47
48 // ThrowException - Wrapper around the ParseException class that automatically
49 // fills in file line number and column number and options info.
50 //
51 // This also helps me because I keep typing 'throw new ParseException' instead 
52 // of just 'throw ParseException'... sigh...
53 //
54 static inline void ThrowException(const std::string &message,
55                                   int LineNo = -1) {
56   if (LineNo == -1) LineNo = llvmAsmlineno;
57   // TODO: column number in exception
58   throw ParseException(CurFilename, message, LineNo);
59 }
60
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.
64 //
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.  :(
68 //
69 struct ValID {
70   enum {
71     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
72     ConstantVal,
73   } Type;
74
75   union {
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.
82   };
83
84   static ValID create(int Num) {
85     ValID D; D.Type = NumberVal; D.Num = Num; return D;
86   }
87
88   static ValID create(char *Name) {
89     ValID D; D.Type = NameVal; D.Name = Name; return D;
90   }
91
92   static ValID create(int64_t Val) {
93     ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
94   }
95
96   static ValID create(uint64_t Val) {
97     ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
98   }
99
100   static ValID create(double Val) {
101     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
102   }
103
104   static ValID createNull() {
105     ValID D; D.Type = ConstNullVal; return D;
106   }
107
108   static ValID create(Constant *Val) {
109     ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
110   }
111
112   inline void destroy() const {
113     if (Type == NameVal)
114       free(Name);    // Free this strdup'd memory...
115   }
116
117   inline ValID copy() const {
118     if (Type != NameVal) return *this;
119     ValID Result = *this;
120     Result.Name = strdup(Name);
121     return Result;
122   }
123
124   inline std::string getName() const {
125     switch (Type) {
126     case NumberVal     : return std::string("#") + itostr(Num);
127     case NameVal       : return Name;
128     case ConstFPVal    : return ftostr(ConstPoolFP);
129     case ConstNullVal  : return "null";
130     case ConstUIntVal  :
131     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
132     case ConstantVal:
133       if (ConstantValue == ConstantBool::True) return "true";
134       if (ConstantValue == ConstantBool::False) return "false";
135       return "<constant expression>";
136     default:
137       assert(0 && "Unknown value!");
138       abort();
139       return "";
140     }
141   }
142
143   bool operator<(const ValID &V) const {
144     if (Type != V.Type) return Type < V.Type;
145     switch (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;
154     }
155   }
156 };
157
158
159
160 template<class SuperType>
161 class PlaceholderValue : public SuperType {
162   ValID D;
163   int LineNum;
164 public:
165   PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
166     LineNum = llvmAsmlineno;
167   }
168   ValID &getDef() { return D; }
169   int getLineNum() const { return LineNum; }
170 };
171
172 struct InstPlaceHolderHelper : public Instruction {
173   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
174
175   virtual Instruction *clone() const { abort(); return 0; }
176   virtual const char *getOpcodeName() const { return "placeholder"; }
177 };
178
179 struct BBPlaceHolderHelper : public BasicBlock {
180   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
181     assert(Ty == Type::LabelTy);
182   }
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