blob: 3571138a0d290a450319308ce2ce31969761e6e4 [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 Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/iOther.h"
Chris Lattner2aac6bf2002-04-04 22:19:18 +000018#include "llvm/Function.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 {
Chris Lattner18b24ea2002-04-28 21:57:50 +000067 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal
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 Lattner00950542001-06-06 20:29:01 +000076 };
77
78 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000079 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000080 }
81
82 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000083 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000084 }
85
86 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000087 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000088 }
89
90 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000091 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000092 }
93
Chris Lattner3d52b2f2001-07-15 00:17:01 +000094 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000095 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
96 }
97
98 static ValID createNull() {
99 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000100 }
101
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000102 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000103 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000104 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000105 }
106
107 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000108 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000109 ValID Result = *this;
110 Result.Name = strdup(Name);
111 return Result;
112 }
113
Chris Lattner697954c2002-01-20 22:54:45 +0000114 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000115 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000116 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000117 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000118 case ConstFPVal : return ftostr(ConstPoolFP);
119 case ConstNullVal : return "null";
120 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000121 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000122 default:
123 assert(0 && "Unknown value!");
124 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000125 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000126 }
127 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000128
129 bool operator<(const ValID &V) const {
130 if (Type != V.Type) return Type < V.Type;
131 switch (Type) {
132 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000133 case NameVal: return strcmp(Name, V.Name) < 0;
134 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
135 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
136 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
137 case ConstNullVal: return false;
138 default: assert(0 && "Unknown value type!"); return false;
139 }
140 }
Chris Lattner00950542001-06-06 20:29:01 +0000141};
142
143
144
145template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000146class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000147 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000148 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000149public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000150 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
151 LineNum = llvmAsmlineno;
152 }
Chris Lattner00950542001-06-06 20:29:01 +0000153 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000154 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000155};
156
157struct InstPlaceHolderHelper : public Instruction {
158 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
159
Chris Lattner697954c2002-01-20 22:54:45 +0000160 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000161 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000162};
163
164struct BBPlaceHolderHelper : public BasicBlock {
165 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattnerc8a79af2002-04-08 21:59:08 +0000166 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000167 }
168};
169
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000170struct MethPlaceHolderHelper : public Function {
171 MethPlaceHolderHelper(const Type *Ty)
172 : Function(cast<FunctionType>(Ty), true) {}
Chris Lattner00950542001-06-06 20:29:01 +0000173};
174
Chris Lattner93750fa2001-07-28 17:48:55 +0000175typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
176typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000177
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000178static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000179 const Type *Ty = Val->getType();
180 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000181 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000182 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000183
184 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000185 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
Chris Lattner93750fa2001-07-28 17:48:55 +0000186 default: return ((ValuePlaceHolder*)Val)->getDef();
187 }
188}
189
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000190static inline int getLineNumFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000191 const Type *Ty = Val->getType();
192 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000193 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000194 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000195
196 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000197 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
Chris Lattner93750fa2001-07-28 17:48:55 +0000198 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000199 }
200}
201
202#endif