1 //===-- ParserInternals.h - Definitions internal to the parser ---*- C++ -*--=//
3 // This header file defines the various variables that are shared among the
4 // different components of the parser...
6 //===----------------------------------------------------------------------===//
8 #ifndef PARSER_INTERNALS_H
9 #define PARSER_INTERNALS_H
12 #define __STDC_LIMIT_MACROS
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/Support/StringExtras.h"
25 // Global variables exported from the lexer...
26 extern FILE *llvmAsmin;
27 extern int llvmAsmlineno;
29 // Globals exported by the parser...
30 extern string CurFilename;
31 Module *RunVMAsmParser(const string &Filename, FILE *F);
34 // ThrowException - Wrapper around the ParseException class that automatically
35 // fills in file line number and column number and options info.
37 // This also helps me because I keep typing 'throw new ParseException' instead
38 // of just 'throw ParseException'... sigh...
40 static inline void ThrowException(const string &message) {
41 // TODO: column number in exception
42 throw ParseException(CurFilename, message, llvmAsmlineno);
45 // ValID - Represents a reference of a definition of some sort. This may either
46 // be a numeric reference or a symbolic (%var) reference. This is just a
47 // discriminated union.
49 // Note that I can't implement this class in a straight forward manner with
50 // constructors and stuff because it goes in a union, and GCC doesn't like
51 // putting classes with ctor's in unions. :(
54 int Type; // 0 = number, 1 = name, 2 = const pool,
55 // 3 = unsigned const pool, 4 = const string,
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 double ConstPoolFP; // Floating point constant pool reference
65 static ValID create(int Num) {
66 ValID D; D.Type = 0; D.Num = Num; return D;
69 static ValID create(char *Name) {
70 ValID D; D.Type = 1; D.Name = Name; return D;
73 static ValID create(int64_t Val) {
74 ValID D; D.Type = 2; D.ConstPool64 = Val; return D;
77 static ValID create(uint64_t Val) {
78 ValID D; D.Type = 3; D.UConstPool64 = Val; return D;
81 static ValID create_conststr(char *Name) {
82 ValID D; D.Type = 4; D.Name = Name; return D;
85 static ValID create(double Val) {
86 ValID D; D.Type = 5; D.ConstPoolFP = Val; return D;
89 inline void destroy() const {
90 if (Type == 1 || Type == 4) free(Name); // Free this strdup'd memory...
93 inline ValID copy() const {
94 if (Type != 1 && Type != 4) return *this;
96 Result.Name = strdup(Name);
100 inline string getName() const {
102 case 0: return string("#") + itostr(Num);
104 case 4: return string("\"") + Name + string("\"");
105 case 5: return ftostr(ConstPoolFP);
106 default: return string("%") + itostr(ConstPool64);
113 template<class SuperType>
114 class PlaceholderDef : public SuperType {
116 // TODO: Placeholder def should hold Line #/Column # of definition in case
117 // there is an error resolving the defintition!
119 PlaceholderDef(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {}
120 ValID &getDef() { return D; }
123 struct InstPlaceHolderHelper : public Instruction {
124 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
126 virtual Instruction *clone() const { abort(); }
127 virtual const char *getOpcodeName() const { return "placeholder"; }
130 struct BBPlaceHolderHelper : public BasicBlock {
131 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
132 assert(Ty->isLabelType());
136 struct MethPlaceHolderHelper : public Method {
137 MethPlaceHolderHelper(const Type *Ty)
138 : Method((const MethodType*)Ty) {
139 assert(Ty->isMethodType() && "Method placeholders must be method types!");
143 typedef PlaceholderDef<InstPlaceHolderHelper> DefPlaceHolder;
144 typedef PlaceholderDef<BBPlaceHolderHelper> BBPlaceHolder;
145 typedef PlaceholderDef<MethPlaceHolderHelper> MethPlaceHolder;
146 //typedef PlaceholderDef<ModulePlaceHolderHelper> ModulePlaceHolder;
148 static inline ValID &getValIDFromPlaceHolder(Value *Def) {
149 switch (Def->getType()->getPrimitiveID()) {
150 case Type::LabelTyID: return ((BBPlaceHolder*)Def)->getDef();
151 case Type::MethodTyID: return ((MethPlaceHolder*)Def)->getDef();
152 //case Type::ModuleTyID: return ((ModulePlaceHolder*)Def)->getDef();
153 default: return ((DefPlaceHolder*)Def)->getDef();