blob: e1e2c4954c020aa225ffed56679b5866258ed86e [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
11#include <stdio.h>
12#define __STDC_LIMIT_MACROS
13
14#include "llvm/InstrTypes.h"
15#include "llvm/BasicBlock.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000016#include "llvm/ConstantVals.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/iOther.h"
18#include "llvm/Method.h"
Chris Lattnereb5ff8d2001-09-07 16:33:01 +000019#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Assembly/Parser.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021#include "Support/StringExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000022
23class Module;
24
25// Global variables exported from the lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000026extern std::FILE *llvmAsmin;
Chris Lattner00950542001-06-06 20:29:01 +000027extern int llvmAsmlineno;
28
29// Globals exported by the parser...
Chris Lattner697954c2002-01-20 22:54:45 +000030extern std::string CurFilename;
31Module *RunVMAsmParser(const std::string &Filename, FILE *F);
Chris Lattner00950542001-06-06 20:29:01 +000032
33
Chris Lattner93750fa2001-07-28 17:48:55 +000034// 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.
37//
38// If AllowNull is set to true, the return value of the function points to the
39// last character of the string in memory.
40//
41char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
42
43
Chris Lattner00950542001-06-06 20:29:01 +000044// ThrowException - Wrapper around the ParseException class that automatically
45// fills in file line number and column number and options info.
46//
47// This also helps me because I keep typing 'throw new ParseException' instead
48// of just 'throw ParseException'... sigh...
49//
Chris Lattner697954c2002-01-20 22:54:45 +000050static inline void ThrowException(const std::string &message,
Chris Lattner93750fa2001-07-28 17:48:55 +000051 int LineNo = -1) {
52 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000053 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000054 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000055}
56
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.
60//
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. :(
64//
65struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000066 enum {
67 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstStringVal,
68 ConstFPVal, ConstNullVal
69 } Type;
70
Chris Lattner00950542001-06-06 20:29:01 +000071 union {
72 int Num; // If it's a numeric reference
73 char *Name; // If it's a named reference. Memory must be free'd.
74 int64_t ConstPool64; // Constant pool reference. This is the value
75 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000076 double ConstPoolFP; // Floating point constant pool reference
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
95 static ValID create_conststr(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000096 ValID D; D.Type = ConstStringVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000097 }
98
Chris Lattner3d52b2f2001-07-15 00:17:01 +000099 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000100 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
101 }
102
103 static ValID createNull() {
104 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000105 }
106
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000107 inline void destroy() const {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000108 if (Type == NameVal || Type == ConstStringVal)
109 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000110 }
111
112 inline ValID copy() const {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000113 if (Type != NameVal && Type != ConstStringVal) 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 Lattner697954c2002-01-20 22:54:45 +0000123 case ConstStringVal: return std::string("\"") + Name + std::string("\"");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000124 case ConstFPVal : return ftostr(ConstPoolFP);
125 case ConstNullVal : return "null";
126 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000127 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000128 default:
129 assert(0 && "Unknown value!");
130 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000131 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000132 }
133 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000134
135 bool operator<(const ValID &V) const {
136 if (Type != V.Type) return Type < V.Type;
137 switch (Type) {
138 case NumberVal: return Num < V.Num;
139 case ConstStringVal:
140 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;
145 default: assert(0 && "Unknown value type!"); return false;
146 }
147 }
Chris Lattner00950542001-06-06 20:29:01 +0000148};
149
150
151
152template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000153class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000154 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000155 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000156public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000157 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
158 LineNum = llvmAsmlineno;
159 }
Chris Lattner00950542001-06-06 20:29:01 +0000160 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000161 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000162};
163
164struct InstPlaceHolderHelper : public Instruction {
165 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
166
Chris Lattner697954c2002-01-20 22:54:45 +0000167 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000168 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000169};
170
171struct BBPlaceHolderHelper : public BasicBlock {
172 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
173 assert(Ty->isLabelType());
174 }
175};
176
177struct MethPlaceHolderHelper : public Method {
Chris Lattnerdda71962001-11-26 18:54:16 +0000178 MethPlaceHolderHelper(const Type *Ty) : Method(cast<const MethodType>(Ty),
179 true) {}
Chris Lattner00950542001-06-06 20:29:01 +0000180};
181
Chris Lattner93750fa2001-07-28 17:48:55 +0000182typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
183typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000184
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000185static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000186 const Type *Ty = Val->getType();
187 if (isa<PointerType>(Ty) &&
Chris Lattner7a176752001-12-04 00:03:30 +0000188 isa<MethodType>(cast<PointerType>(Ty)->getElementType()))
189 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000190
191 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000192 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
Chris Lattner93750fa2001-07-28 17:48:55 +0000193 default: return ((ValuePlaceHolder*)Val)->getDef();
194 }
195}
196
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000197static inline int getLineNumFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000198 const Type *Ty = Val->getType();
199 if (isa<PointerType>(Ty) &&
Chris Lattner7a176752001-12-04 00:03:30 +0000200 isa<MethodType>(cast<PointerType>(Ty)->getElementType()))
201 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000202
203 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000204 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
Chris Lattner93750fa2001-07-28 17:48:55 +0000205 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000206 }
207}
208
209#endif