blob: 1302348166241b1564b99f6b06a2e200876365ff [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- 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
Chris Lattner00950542001-06-06 20:29:01 +000011#define __STDC_LIMIT_MACROS
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/BasicBlock.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000013#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/iOther.h"
Chris Lattner2aac6bf2002-04-04 22:19:18 +000015#include "llvm/Function.h"
Chris Lattnereb5ff8d2001-09-07 16:33:01 +000016#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Assembly/Parser.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include "Support/StringExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000019
20class Module;
21
22// Global variables exported from the lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000023extern std::FILE *llvmAsmin;
Chris Lattner00950542001-06-06 20:29:01 +000024extern int llvmAsmlineno;
25
26// Globals exported by the parser...
Chris Lattner697954c2002-01-20 22:54:45 +000027extern std::string CurFilename;
28Module *RunVMAsmParser(const std::string &Filename, FILE *F);
Chris Lattner00950542001-06-06 20:29:01 +000029
Vikram S. Advef946bcd2002-07-14 22:49:40 +000030extern char* llvmAsmtext;
31extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000032
Chris Lattner93750fa2001-07-28 17:48:55 +000033// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
34// appropriate character. If AllowNull is set to false, a \00 value will cause
35// an exception to be thrown.
36//
37// If AllowNull is set to true, the return value of the function points to the
38// last character of the string in memory.
39//
40char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
41
42
Chris Lattner00950542001-06-06 20:29:01 +000043// ThrowException - Wrapper around the ParseException class that automatically
44// fills in file line number and column number and options info.
45//
46// This also helps me because I keep typing 'throw new ParseException' instead
47// of just 'throw ParseException'... sigh...
48//
Chris Lattner697954c2002-01-20 22:54:45 +000049static inline void ThrowException(const std::string &message,
Chris Lattner93750fa2001-07-28 17:48:55 +000050 int LineNo = -1) {
51 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000052 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000053 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000054}
55
56// ValID - Represents a reference of a definition of some sort. This may either
57// be a numeric reference or a symbolic (%var) reference. This is just a
58// discriminated union.
59//
60// Note that I can't implement this class in a straight forward manner with
61// constructors and stuff because it goes in a union, and GCC doesn't like
62// putting classes with ctor's in unions. :(
63//
64struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000065 enum {
Chris Lattner18b24ea2002-04-28 21:57:50 +000066 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000067 } Type;
68
Chris Lattner00950542001-06-06 20:29:01 +000069 union {
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.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000074 double ConstPoolFP; // Floating point constant pool reference
Chris Lattner00950542001-06-06 20:29:01 +000075 };
76
77 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000078 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000079 }
80
81 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000082 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000083 }
84
85 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000086 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000087 }
88
89 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000090 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000091 }
92
Chris Lattner3d52b2f2001-07-15 00:17:01 +000093 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000094 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
95 }
96
97 static ValID createNull() {
98 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +000099 }
100
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000101 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000102 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000103 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000104 }
105
106 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000107 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000108 ValID Result = *this;
109 Result.Name = strdup(Name);
110 return Result;
111 }
112
Chris Lattner697954c2002-01-20 22:54:45 +0000113 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000114 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000115 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000116 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000117 case ConstFPVal : return ftostr(ConstPoolFP);
118 case ConstNullVal : return "null";
119 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000120 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000121 default:
122 assert(0 && "Unknown value!");
123 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000124 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000125 }
126 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000127
128 bool operator<(const ValID &V) const {
129 if (Type != V.Type) return Type < V.Type;
130 switch (Type) {
131 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000132 case NameVal: return strcmp(Name, V.Name) < 0;
133 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
134 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
135 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
136 case ConstNullVal: return false;
137 default: assert(0 && "Unknown value type!"); return false;
138 }
139 }
Chris Lattner00950542001-06-06 20:29:01 +0000140};
141
142
143
144template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000145class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000146 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000147 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000148public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000149 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
150 LineNum = llvmAsmlineno;
151 }
Chris Lattner00950542001-06-06 20:29:01 +0000152 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000153 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000154};
155
156struct InstPlaceHolderHelper : public Instruction {
157 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
158
Chris Lattner697954c2002-01-20 22:54:45 +0000159 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000160 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000161};
162
163struct BBPlaceHolderHelper : public BasicBlock {
164 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattnerc8a79af2002-04-08 21:59:08 +0000165 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000166 }
167};
168
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000169struct MethPlaceHolderHelper : public Function {
170 MethPlaceHolderHelper(const Type *Ty)
171 : Function(cast<FunctionType>(Ty), true) {}
Chris Lattner00950542001-06-06 20:29:01 +0000172};
173
Chris Lattner93750fa2001-07-28 17:48:55 +0000174typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
175typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000177static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000178 const Type *Ty = Val->getType();
179 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000180 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000181 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000182
183 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000184 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
Chris Lattner93750fa2001-07-28 17:48:55 +0000185 default: return ((ValuePlaceHolder*)Val)->getDef();
186 }
187}
188
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000189static inline int getLineNumFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000190 const Type *Ty = Val->getType();
191 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000192 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000193 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000194
195 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000196 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
Chris Lattner93750fa2001-07-28 17:48:55 +0000197 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000198 }
199}
200
201#endif