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 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
35 // appropriate character. If AllowNull is set to false, a \00 value will cause
36 // an exception to be thrown.
38 // If AllowNull is set to true, the return value of the function points to the
39 // last character of the string in memory.
41 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
44 // ThrowException - Wrapper around the ParseException class that automatically
45 // fills in file line number and column number and options info.
47 // This also helps me because I keep typing 'throw new ParseException' instead
48 // of just 'throw ParseException'... sigh...
50 static inline void ThrowException(const string &message,
52 if (LineNo == -1) LineNo = llvmAsmlineno;
53 // TODO: column number in exception
54 throw ParseException(CurFilename, message, LineNo);
57 // ValID - Represents a reference of a definition of some sort. This may either
58 // be a numeric reference or a symbolic (%var) reference. This is just a
59 // discriminated union.
61 // Note that I can't implement this class in a straight forward manner with
62 // constructors and stuff because it goes in a union, and GCC doesn't like
63 // putting classes with ctor's in unions. :(
66 int Type; // 0 = number, 1 = name, 2 = const pool,
67 // 3 = unsigned const pool, 4 = const string,
70 int Num; // If it's a numeric reference
71 char *Name; // If it's a named reference. Memory must be free'd.
72 int64_t ConstPool64; // Constant pool reference. This is the value
73 uint64_t UConstPool64;// Unsigned constant pool reference.
74 double ConstPoolFP; // Floating point constant pool reference
77 static ValID create(int Num) {
78 ValID D; D.Type = 0; D.Num = Num; return D;
81 static ValID create(char *Name) {
82 ValID D; D.Type = 1; D.Name = Name; return D;
85 static ValID create(int64_t Val) {
86 ValID D; D.Type = 2; D.ConstPool64 = Val; return D;
89 static ValID create(uint64_t Val) {
90 ValID D; D.Type = 3; D.UConstPool64 = Val; return D;
93 static ValID create_conststr(char *Name) {
94 ValID D; D.Type = 4; D.Name = Name; return D;
97 static ValID create(double Val) {
98 ValID D; D.Type = 5; D.ConstPoolFP = Val; return D;
101 inline void destroy() const {
102 if (Type == 1 || Type == 4) free(Name); // Free this strdup'd memory...
105 inline ValID copy() const {
106 if (Type != 1 && Type != 4) return *this;
107 ValID Result = *this;
108 Result.Name = strdup(Name);
112 inline string getName() const {
114 case 0: return string("#") + itostr(Num);
116 case 4: return string("\"") + Name + string("\"");
117 case 5: return ftostr(ConstPoolFP);
118 default: return string("%") + itostr(ConstPool64);
125 template<class SuperType>
126 class PlaceholderValue : public SuperType {
130 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
131 LineNum = llvmAsmlineno;
133 ValID &getDef() { return D; }
134 int getLineNum() const { return LineNum; }
137 struct InstPlaceHolderHelper : public Instruction {
138 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
140 virtual Instruction *clone() const { abort(); }
141 virtual const char *getOpcodeName() const { return "placeholder"; }
144 struct BBPlaceHolderHelper : public BasicBlock {
145 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
146 assert(Ty->isLabelType());
150 struct MethPlaceHolderHelper : public Method {
151 MethPlaceHolderHelper(const Type *Ty)
152 : Method((const MethodType*)Ty) {
153 assert(Ty->isMethodType() && "Method placeholders must be method types!");
157 typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
158 typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
159 typedef PlaceholderValue<MethPlaceHolderHelper> MethPlaceHolder;
161 static inline ValID &getValIDFromPlaceHolder(Value *Val) {
162 switch (Val->getType()->getPrimitiveID()) {
163 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
164 case Type::MethodTyID: return ((MethPlaceHolder*)Val)->getDef();
165 default: return ((ValuePlaceHolder*)Val)->getDef();
169 static inline int getLineNumFromPlaceHolder(Value *Val) {
170 switch (Val->getType()->getPrimitiveID()) {
171 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
172 case Type::MethodTyID: return ((MethPlaceHolder*)Val)->getLineNum();
173 default: return ((ValuePlaceHolder*)Val)->getLineNum();