bd26fdbaaf55a13516e8ca0c270ee8b7201c7a83
[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/ConstPoolVals.h"
17 #include "llvm/iOther.h"
18 #include "llvm/Method.h"
19 #include "llvm/Type.h"
20 #include "llvm/Assembly/Parser.h"
21 #include "llvm/Tools/CommandLine.h"
22 #include "llvm/Tools/StringExtras.h"
23
24 class Module;
25
26 // Global variables exported from the lexer...
27 extern FILE *llvmAsmin;
28 extern int llvmAsmlineno;
29
30 // Globals exported by the parser...
31 extern const ToolCommandLine *CurOptions;
32 Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F);
33
34
35 // ThrowException - Wrapper around the ParseException class that automatically
36 // fills in file line number and column number and options info.
37 //
38 // This also helps me because I keep typing 'throw new ParseException' instead 
39 // of just 'throw ParseException'... sigh...
40 //
41 static inline void ThrowException(const string &message) {
42   // TODO: column number in exception
43   throw ParseException(*CurOptions, message, llvmAsmlineno);
44 }
45
46 // ValID - Represents a reference of a definition of some sort.  This may either
47 // be a numeric reference or a symbolic (%var) reference.  This is just a 
48 // discriminated union.
49 //
50 // Note that I can't implement this class in a straight forward manner with 
51 // constructors and stuff because it goes in a union, and GCC doesn't like 
52 // putting classes with ctor's in unions.  :(
53 //
54 struct ValID {
55   int Type;               // 0 = number, 1 = name, 2 = const pool, 
56                           // 3 = unsigned const pool, 4 = const string
57   union {
58     int      Num;         // If it's a numeric reference
59     char    *Name;        // If it's a named reference.  Memory must be free'd.
60     int64_t  ConstPool64; // Constant pool reference.  This is the value
61     uint64_t UConstPool64;// Unsigned constant pool reference.
62   };
63
64   static ValID create(int Num) {
65     ValID D; D.Type = 0; D.Num = Num; return D;
66   }
67
68   static ValID create(char *Name) {
69     ValID D; D.Type = 1; D.Name = Name; return D;
70   }
71
72   static ValID create(int64_t Val) {
73     ValID D; D.Type = 2; D.ConstPool64 = Val; return D;
74   }
75
76   static ValID create(uint64_t Val) {
77     ValID D; D.Type = 3; D.UConstPool64 = Val; return D;
78   }
79
80   static ValID create_conststr(char *Name) {
81     ValID D; D.Type = 4; D.Name = Name; return D;
82   }
83
84   inline void destroy() {
85     if (Type == 1 || Type == 4) free(Name);  // Free this strdup'd memory...
86   }
87
88   inline ValID copy() const {
89     if (Type != 1 && Type != 4) return *this;
90     ValID Result = *this;
91     Result.Name = strdup(Name);
92     return Result;
93   }
94
95   inline string getName() const {
96     switch (Type) {
97     case 0:  return string("#") + itostr(Num);
98     case 1:  return Name;
99     case 4:  return string("\"") + Name + string("\"");
100     default: return string("%") + itostr(ConstPool64);
101     }
102   }
103 };
104
105
106
107 template<class SuperType>
108 class PlaceholderDef : public SuperType {
109   ValID D;
110   // TODO: Placeholder def should hold Line #/Column # of definition in case
111   // there is an error resolving the defintition!
112 public:
113   PlaceholderDef(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {}
114   ValID &getDef() { return D; }
115 };
116
117 struct InstPlaceHolderHelper : public Instruction {
118   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
119
120   virtual Instruction *clone() const { abort(); }
121   virtual const char *getOpcodeName() const { return "placeholder"; }
122 };
123
124 struct BBPlaceHolderHelper : public BasicBlock {
125   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
126     assert(Ty->isLabelType());
127   }
128 };
129
130 struct MethPlaceHolderHelper : public Method {
131   MethPlaceHolderHelper(const Type *Ty) 
132     : Method((const MethodType*)Ty) {
133     assert(Ty->isMethodType() && "Method placeholders must be method types!");
134   }
135 };
136
137 typedef PlaceholderDef<InstPlaceHolderHelper>  DefPlaceHolder;
138 typedef PlaceholderDef<BBPlaceHolderHelper>    BBPlaceHolder;
139 typedef PlaceholderDef<MethPlaceHolderHelper>  MethPlaceHolder;
140 //typedef PlaceholderDef<ModulePlaceHolderHelper> ModulePlaceHolder;
141
142 static inline ValID &getValIDFromPlaceHolder(Value *Def) {
143   switch (Def->getType()->getPrimitiveID()) {
144   case Type::LabelTyID:  return ((BBPlaceHolder*)Def)->getDef();
145   case Type::MethodTyID: return ((MethPlaceHolder*)Def)->getDef();
146 //case Type::ModuleTyID:  return ((ModulePlaceHolder*)Def)->getDef();
147   default:               return ((DefPlaceHolder*)Def)->getDef();
148   }
149 }
150
151 #endif