blob: 6c3d9a0e47e087ec5e5096db9cc2563375f04a4c [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
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 Lattner31bcdb82002-04-28 19:55:58 +000011#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/iOther.h"
Chris Lattner2aac6bf2002-04-04 22:19:18 +000013#include "llvm/Function.h"
Chris Lattnereb5ff8d2001-09-07 16:33:01 +000014#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/Assembly/Parser.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/StringExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000017
18class Module;
19
20// Global variables exported from the lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000021extern std::FILE *llvmAsmin;
Chris Lattner00950542001-06-06 20:29:01 +000022extern int llvmAsmlineno;
23
24// Globals exported by the parser...
Chris Lattner697954c2002-01-20 22:54:45 +000025extern std::string CurFilename;
26Module *RunVMAsmParser(const std::string &Filename, FILE *F);
Chris Lattner00950542001-06-06 20:29:01 +000027
Vikram S. Advef946bcd2002-07-14 22:49:40 +000028extern char* llvmAsmtext;
29extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000030
Chris Lattner93750fa2001-07-28 17:48:55 +000031// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
32// appropriate character. If AllowNull is set to false, a \00 value will cause
33// an exception to be thrown.
34//
35// If AllowNull is set to true, the return value of the function points to the
36// last character of the string in memory.
37//
38char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
39
40
Chris Lattner00950542001-06-06 20:29:01 +000041// ThrowException - Wrapper around the ParseException class that automatically
42// fills in file line number and column number and options info.
43//
44// This also helps me because I keep typing 'throw new ParseException' instead
45// of just 'throw ParseException'... sigh...
46//
Chris Lattner697954c2002-01-20 22:54:45 +000047static inline void ThrowException(const std::string &message,
Chris Lattner93750fa2001-07-28 17:48:55 +000048 int LineNo = -1) {
49 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000050 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000051 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000052}
53
54// ValID - Represents a reference of a definition of some sort. This may either
55// be a numeric reference or a symbolic (%var) reference. This is just a
56// discriminated union.
57//
58// Note that I can't implement this class in a straight forward manner with
59// constructors and stuff because it goes in a union, and GCC doesn't like
60// putting classes with ctor's in unions. :(
61//
62struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000063 enum {
Chris Lattnerd78700d2002-08-16 21:14:40 +000064 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
65 ConstantVal,
Chris Lattner1a1cb112001-09-30 22:46:54 +000066 } Type;
67
Chris Lattner00950542001-06-06 20:29:01 +000068 union {
69 int Num; // If it's a numeric reference
70 char *Name; // If it's a named reference. Memory must be free'd.
71 int64_t ConstPool64; // Constant pool reference. This is the value
72 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000073 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000074 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
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 Lattnerd78700d2002-08-16 21:14:40 +0000101 static ValID create(Constant *Val) {
102 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
103 }
104
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000105 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000106 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000107 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000108 }
109
110 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000111 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000112 ValID Result = *this;
113 Result.Name = strdup(Name);
114 return Result;
115 }
116
Chris Lattner697954c2002-01-20 22:54:45 +0000117 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000118 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000119 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000120 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000121 case ConstFPVal : return ftostr(ConstPoolFP);
122 case ConstNullVal : return "null";
123 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000124 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000125 case ConstantVal:
126 if (ConstantValue == ConstantBool::True) return "true";
127 if (ConstantValue == ConstantBool::False) return "false";
128 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000129 default:
130 assert(0 && "Unknown value!");
131 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000132 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000133 }
134 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000135
136 bool operator<(const ValID &V) const {
137 if (Type != V.Type) return Type < V.Type;
138 switch (Type) {
139 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000140 case NameVal: return strcmp(Name, V.Name) < 0;
141 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
142 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
143 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
144 case ConstNullVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000145 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000146 default: assert(0 && "Unknown value type!"); return false;
147 }
148 }
Chris Lattner00950542001-06-06 20:29:01 +0000149};
150
151
152
153template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000154class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000155 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000156 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000157public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000158 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
159 LineNum = llvmAsmlineno;
160 }
Chris Lattner00950542001-06-06 20:29:01 +0000161 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000162 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000163};
164
165struct InstPlaceHolderHelper : public Instruction {
166 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
167
Chris Lattner697954c2002-01-20 22:54:45 +0000168 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000169 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000170};
171
172struct BBPlaceHolderHelper : public BasicBlock {
173 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattnerc8a79af2002-04-08 21:59:08 +0000174 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000175 }
176};
177
Chris Lattner93750fa2001-07-28 17:48:55 +0000178typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
179typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000180
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000181static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000182 const Type *Ty = Val->getType();
183 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000184 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000185 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000186
187 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000188 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
Chris Lattner93750fa2001-07-28 17:48:55 +0000189 default: return ((ValuePlaceHolder*)Val)->getDef();
190 }
191}
192
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000193static inline int getLineNumFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000194 const Type *Ty = Val->getType();
195 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000196 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000197 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000198
199 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000200 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
Chris Lattner93750fa2001-07-28 17:48:55 +0000201 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000202 }
203}
204
205#endif