blob: 656d718e385a0934f2f4c6336577a68fefd59fab [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
30
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 Lattner18b24ea2002-04-28 21:57:50 +000064 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000065 } Type;
66
Chris Lattner00950542001-06-06 20:29:01 +000067 union {
68 int Num; // If it's a numeric reference
69 char *Name; // If it's a named reference. Memory must be free'd.
70 int64_t ConstPool64; // Constant pool reference. This is the value
71 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000072 double ConstPoolFP; // Floating point constant pool reference
Chris Lattner00950542001-06-06 20:29:01 +000073 };
74
75 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000076 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000077 }
78
79 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000080 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000081 }
82
83 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000084 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000085 }
86
87 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000088 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000089 }
90
Chris Lattner3d52b2f2001-07-15 00:17:01 +000091 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000092 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
93 }
94
95 static ValID createNull() {
96 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +000097 }
98
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +000099 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000100 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000101 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000102 }
103
104 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000105 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000106 ValID Result = *this;
107 Result.Name = strdup(Name);
108 return Result;
109 }
110
Chris Lattner697954c2002-01-20 22:54:45 +0000111 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000112 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000113 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000114 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000115 case ConstFPVal : return ftostr(ConstPoolFP);
116 case ConstNullVal : return "null";
117 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000118 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000119 default:
120 assert(0 && "Unknown value!");
121 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000122 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000123 }
124 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000125
126 bool operator<(const ValID &V) const {
127 if (Type != V.Type) return Type < V.Type;
128 switch (Type) {
129 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000130 case NameVal: return strcmp(Name, V.Name) < 0;
131 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
132 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
133 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
134 case ConstNullVal: return false;
135 default: assert(0 && "Unknown value type!"); return false;
136 }
137 }
Chris Lattner00950542001-06-06 20:29:01 +0000138};
139
140
141
142template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000143class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000144 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000145 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000146public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000147 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
148 LineNum = llvmAsmlineno;
149 }
Chris Lattner00950542001-06-06 20:29:01 +0000150 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000151 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000152};
153
154struct InstPlaceHolderHelper : public Instruction {
155 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
156
Chris Lattner697954c2002-01-20 22:54:45 +0000157 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000158 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000159};
160
161struct BBPlaceHolderHelper : public BasicBlock {
162 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattnerc8a79af2002-04-08 21:59:08 +0000163 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000164 }
165};
166
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000167struct MethPlaceHolderHelper : public Function {
168 MethPlaceHolderHelper(const Type *Ty)
169 : Function(cast<FunctionType>(Ty), true) {}
Chris Lattner00950542001-06-06 20:29:01 +0000170};
171
Chris Lattner93750fa2001-07-28 17:48:55 +0000172typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
173typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000174
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000175static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000176 const Type *Ty = Val->getType();
177 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000178 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000179 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000180
181 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000182 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
Chris Lattner93750fa2001-07-28 17:48:55 +0000183 default: return ((ValuePlaceHolder*)Val)->getDef();
184 }
185}
186
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000187static inline int getLineNumFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000188 const Type *Ty = Val->getType();
189 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000190 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000191 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000192
193 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000194 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
Chris Lattner93750fa2001-07-28 17:48:55 +0000195 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000196 }
197}
198
199#endif