Split ConstantVals.h into Constant.h and Constants.h
[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 #include <stdio.h>
12 #define __STDC_LIMIT_MACROS
13
14 #include "llvm/InstrTypes.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Constants.h"
17 #include "llvm/iOther.h"
18 #include "llvm/Function.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Assembly/Parser.h"
21 #include "Support/StringExtras.h"
22
23 class Module;
24
25 // Global variables exported from the lexer...
26 extern std::FILE *llvmAsmin;
27 extern int llvmAsmlineno;
28
29 // Globals exported by the parser...
30 extern std::string CurFilename;
31 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
32
33
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.
37 //
38 // If AllowNull is set to true, the return value of the function points to the
39 // last character of the string in memory.
40 //
41 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
42
43
44 // ThrowException - Wrapper around the ParseException class that automatically
45 // fills in file line number and column number and options info.
46 //
47 // This also helps me because I keep typing 'throw new ParseException' instead 
48 // of just 'throw ParseException'... sigh...
49 //
50 static inline void ThrowException(const std::string &message,
51                                   int LineNo = -1) {
52   if (LineNo == -1) LineNo = llvmAsmlineno;
53   // TODO: column number in exception
54   throw ParseException(CurFilename, message, LineNo);
55 }
56
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.
60 //
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.  :(
64 //
65 struct ValID {
66   enum {
67     NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstStringVal, 
68     ConstFPVal, ConstNullVal
69   } Type;
70
71   union {
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
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_conststr(char *Name) {
96     ValID D; D.Type = ConstStringVal; D.Name = Name; return D;
97   }
98
99   static ValID create(double Val) {
100     ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
101   }
102
103   static ValID createNull() {
104     ValID D; D.Type = ConstNullVal; return D;
105   }
106
107   inline void destroy() const {
108     if (Type == NameVal || Type == ConstStringVal)
109       free(Name);    // Free this strdup'd memory...
110   }
111
112   inline ValID copy() const {
113     if (Type != NameVal && Type != ConstStringVal) 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 ConstStringVal: return std::string("\"") + Name + std::string("\"");
124     case ConstFPVal    : return ftostr(ConstPoolFP);
125     case ConstNullVal  : return "null";
126     case ConstUIntVal  :
127     case ConstSIntVal  : return std::string("%") + itostr(ConstPool64);
128     default:
129       assert(0 && "Unknown value!");
130       abort();
131       return "";
132     }
133   }
134
135   bool operator<(const ValID &V) const {
136     if (Type != V.Type) return Type < V.Type;
137     switch (Type) {
138     case NumberVal:     return Num < V.Num;
139     case ConstStringVal:
140     case NameVal:       return strcmp(Name, V.Name) < 0;
141     case ConstSIntVal:  return ConstPool64  < V.ConstPool64;
142     case ConstUIntVal:  return UConstPool64 < V.UConstPool64;
143     case ConstFPVal:    return ConstPoolFP  < V.ConstPoolFP;
144     case ConstNullVal:  return false;
145     default:  assert(0 && "Unknown value type!"); return false;
146     }
147   }
148 };
149
150
151
152 template<class SuperType>
153 class PlaceholderValue : public SuperType {
154   ValID D;
155   int LineNum;
156 public:
157   PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
158     LineNum = llvmAsmlineno;
159   }
160   ValID &getDef() { return D; }
161   int getLineNum() const { return LineNum; }
162 };
163
164 struct InstPlaceHolderHelper : public Instruction {
165   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
166
167   virtual Instruction *clone() const { abort(); return 0; }
168   virtual const char *getOpcodeName() const { return "placeholder"; }
169 };
170
171 struct BBPlaceHolderHelper : public BasicBlock {
172   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
173     assert(Ty == Type::LabelTy);
174   }
175 };
176
177 struct MethPlaceHolderHelper : public Function {
178   MethPlaceHolderHelper(const Type *Ty)
179     : Function(cast<FunctionType>(Ty), true) {}
180 };
181
182 typedef PlaceholderValue<InstPlaceHolderHelper>  ValuePlaceHolder;
183 typedef PlaceholderValue<BBPlaceHolderHelper>    BBPlaceHolder;
184
185 static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
186   const Type *Ty = Val->getType();
187   if (isa<PointerType>(Ty) &&
188       isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
189     Ty = cast<PointerType>(Ty)->getElementType();
190
191   switch (Ty->getPrimitiveID()) {
192   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getDef();
193   default:               return ((ValuePlaceHolder*)Val)->getDef();
194   }
195 }
196
197 static inline int getLineNumFromPlaceHolder(const Value *Val) {
198   const Type *Ty = Val->getType();
199   if (isa<PointerType>(Ty) &&
200       isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
201     Ty = cast<PointerType>(Ty)->getElementType();
202
203   switch (Ty->getPrimitiveID()) {
204   case Type::LabelTyID:  return ((BBPlaceHolder*)Val)->getLineNum();
205   default:               return ((ValuePlaceHolder*)Val)->getLineNum();
206   }
207 }
208
209 #endif