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