blob: e3eb3480af976a81167777ba582a9fe2b4bdcc01 [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 Lattnerd78700d2002-08-16 21:14:40 +000066 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
67 ConstantVal,
Chris Lattner1a1cb112001-09-30 22:46:54 +000068 } Type;
69
Chris Lattner00950542001-06-06 20:29:01 +000070 union {
71 int Num; // If it's a numeric reference
72 char *Name; // If it's a named reference. Memory must be free'd.
73 int64_t ConstPool64; // Constant pool reference. This is the value
74 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000075 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000076 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattner00950542001-06-06 20:29:01 +000077 };
78
79 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000080 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000081 }
82
83 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000084 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000085 }
86
87 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000088 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000089 }
90
91 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000092 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000093 }
94
Chris Lattner3d52b2f2001-07-15 00:17:01 +000095 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000096 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
97 }
98
99 static ValID createNull() {
100 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000101 }
102
Chris Lattnerd78700d2002-08-16 21:14:40 +0000103 static ValID create(Constant *Val) {
104 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
105 }
106
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000107 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000108 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000109 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000110 }
111
112 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000113 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000114 ValID Result = *this;
115 Result.Name = strdup(Name);
116 return Result;
117 }
118
Chris Lattner697954c2002-01-20 22:54:45 +0000119 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000120 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000121 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000122 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000123 case ConstFPVal : return ftostr(ConstPoolFP);
124 case ConstNullVal : return "null";
125 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000126 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000127 case ConstantVal:
128 if (ConstantValue == ConstantBool::True) return "true";
129 if (ConstantValue == ConstantBool::False) return "false";
130 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000131 default:
132 assert(0 && "Unknown value!");
133 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000134 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000135 }
136 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000137
138 bool operator<(const ValID &V) const {
139 if (Type != V.Type) return Type < V.Type;
140 switch (Type) {
141 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000142 case NameVal: return strcmp(Name, V.Name) < 0;
143 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
144 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
145 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
146 case ConstNullVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000147 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000148 default: assert(0 && "Unknown value type!"); return false;
149 }
150 }
Chris Lattner00950542001-06-06 20:29:01 +0000151};
152
153
154
155template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000156class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000157 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000158 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000159public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000160 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
161 LineNum = llvmAsmlineno;
162 }
Chris Lattner00950542001-06-06 20:29:01 +0000163 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000164 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000165};
166
167struct InstPlaceHolderHelper : public Instruction {
168 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
169
Chris Lattner697954c2002-01-20 22:54:45 +0000170 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000171 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000172};
173
174struct BBPlaceHolderHelper : public BasicBlock {
175 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattnerc8a79af2002-04-08 21:59:08 +0000176 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000177 }
178};
179
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000180struct MethPlaceHolderHelper : public Function {
181 MethPlaceHolderHelper(const Type *Ty)
182 : Function(cast<FunctionType>(Ty), true) {}
Chris Lattner00950542001-06-06 20:29:01 +0000183};
184
Chris Lattner93750fa2001-07-28 17:48:55 +0000185typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
186typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000187
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000188static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000189 const Type *Ty = Val->getType();
190 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000191 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000192 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000193
194 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000195 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
Chris Lattner93750fa2001-07-28 17:48:55 +0000196 default: return ((ValuePlaceHolder*)Val)->getDef();
197 }
198}
199
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000200static inline int getLineNumFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000201 const Type *Ty = Val->getType();
202 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000203 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000204 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000205
206 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000207 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
Chris Lattner93750fa2001-07-28 17:48:55 +0000208 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000209 }
210}
211
212#endif