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/Tools/CommandLine.h"
22 #include "llvm/Tools/StringExtras.h"
26 // Global variables exported from the lexer...
27 extern FILE *llvmAsmin;
28 extern int llvmAsmlineno;
30 // Globals exported by the parser...
31 extern const ToolCommandLine *CurOptions;
32 Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F);
35 // ThrowException - Wrapper around the ParseException class that automatically
36 // fills in file line number and column number and options info.
38 // This also helps me because I keep typing 'throw new ParseException' instead
39 // of just 'throw ParseException'... sigh...
41 static inline void ThrowException(const string &message) {
42 // TODO: column number in exception
43 throw ParseException(*CurOptions, message, llvmAsmlineno);
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.
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. :(
55 int Type; // 0 = number, 1 = name, 2 = const pool,
56 // 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.
64 static ValID create(int Num) {
65 ValID D; D.Type = 0; D.Num = Num; return D;
68 static ValID create(char *Name) {
69 ValID D; D.Type = 1; D.Name = Name; return D;
72 static ValID create(int64_t Val) {
73 ValID D; D.Type = 2; D.ConstPool64 = Val; return D;
76 static ValID create(uint64_t Val) {
77 ValID D; D.Type = 3; D.UConstPool64 = Val; return D;
80 static ValID create_conststr(char *Name) {
81 ValID D; D.Type = 4; D.Name = Name; return D;
84 inline void destroy() {
85 if (Type == 1 || Type == 4) free(Name); // Free this strdup'd memory...
88 inline ValID copy() const {
89 if (Type != 1 && Type != 4) return *this;
91 Result.Name = strdup(Name);
95 inline string getName() const {
97 case 0: return string("#") + itostr(Num);
99 case 4: return string("\"") + Name + string("\"");
100 default: return string("%") + itostr(ConstPool64);
107 template<class SuperType>
108 class PlaceholderDef : public SuperType {
110 // TODO: Placeholder def should hold Line #/Column # of definition in case
111 // there is an error resolving the defintition!
113 PlaceholderDef(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {}
114 ValID &getDef() { return D; }
117 struct InstPlaceHolderHelper : public Instruction {
118 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
120 virtual Instruction *clone() const { abort(); }
121 virtual string getOpcode() const { return "placeholder"; }
124 struct BBPlaceHolderHelper : public BasicBlock {
125 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
126 assert(Ty->isLabelType());
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!");
137 typedef PlaceholderDef<InstPlaceHolderHelper> DefPlaceHolder;
138 typedef PlaceholderDef<BBPlaceHolderHelper> BBPlaceHolder;
139 typedef PlaceholderDef<MethPlaceHolderHelper> MethPlaceHolder;
140 //typedef PlaceholderDef<ModulePlaceHolderHelper> ModulePlaceHolder;
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();