Add support to the parser to recognize floating point constants
[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                           // 5 = const fp
58   union {
59     int      Num;         // If it's a numeric reference
60     char    *Name;        // If it's a named reference.  Memory must be free'd.
61     int64_t  ConstPool64; // Constant pool reference.  This is the value
62     uint64_t UConstPool64;// Unsigned constant pool reference.
63     double   ConstPoolFP; // Floating point constant pool reference
64   };
65
66   static ValID create(int Num) {
67     ValID D; D.Type = 0; D.Num = Num; return D;
68   }
69
70   static ValID create(char *Name) {
71     ValID D; D.Type = 1; D.Name = Name; return D;
72   }
73
74   static ValID create(int64_t Val) {
75     ValID D; D.Type = 2; D.ConstPool64 = Val; return D;
76   }
77
78   static ValID create(uint64_t Val) {
79     ValID D; D.Type = 3; D.UConstPool64 = Val; return D;
80   }
81
82   static ValID create_conststr(char *Name) {
83     ValID D; D.Type = 4; D.Name = Name; return D;
84   }
85
86   static ValID create(double Val) {
87     ValID D; D.Type = 5; D.ConstPoolFP = Val; return D;
88   }
89
90   inline void destroy() {
91     if (Type == 1 || Type == 4) free(Name);  // Free this strdup'd memory...
92   }
93
94   inline ValID copy() const {
95     if (Type != 1 && Type != 4) return *this;
96     ValID Result = *this;
97     Result.Name = strdup(Name);
98     return Result;
99   }
100
101   inline string getName() const {
102     switch (Type) {
103     case 0:  return string("#") + itostr(Num);
104     case 1:  return Name;
105     case 4:  return string("\"") + Name + string("\"");
106     case 5:  return ftostr(ConstPoolFP);
107     default: return string("%") + itostr(ConstPool64);
108     }
109   }
110 };
111
112
113
114 template<class SuperType>
115 class PlaceholderDef : public SuperType {
116   ValID D;
117   // TODO: Placeholder def should hold Line #/Column # of definition in case
118   // there is an error resolving the defintition!
119 public:
120   PlaceholderDef(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {}
121   ValID &getDef() { return D; }
122 };
123
124 struct InstPlaceHolderHelper : public Instruction {
125   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
126
127   virtual Instruction *clone() const { abort(); }
128   virtual const char *getOpcodeName() const { return "placeholder"; }
129 };
130
131 struct BBPlaceHolderHelper : public BasicBlock {
132   BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
133     assert(Ty->isLabelType());
134   }
135 };
136
137 struct MethPlaceHolderHelper : public Method {
138   MethPlaceHolderHelper(const Type *Ty) 
139     : Method((const MethodType*)Ty) {
140     assert(Ty->isMethodType() && "Method placeholders must be method types!");
141   }
142 };
143
144 typedef PlaceholderDef<InstPlaceHolderHelper>  DefPlaceHolder;
145 typedef PlaceholderDef<BBPlaceHolderHelper>    BBPlaceHolder;
146 typedef PlaceholderDef<MethPlaceHolderHelper>  MethPlaceHolder;
147 //typedef PlaceholderDef<ModulePlaceHolderHelper> ModulePlaceHolder;
148
149 static inline ValID &getValIDFromPlaceHolder(Value *Def) {
150   switch (Def->getType()->getPrimitiveID()) {
151   case Type::LabelTyID:  return ((BBPlaceHolder*)Def)->getDef();
152   case Type::MethodTyID: return ((MethPlaceHolder*)Def)->getDef();
153 //case Type::ModuleTyID:  return ((ModulePlaceHolder*)Def)->getDef();
154   default:               return ((DefPlaceHolder*)Def)->getDef();
155   }
156 }
157
158 #endif