blob: c7837959e61d0a25c6b92743014b38add094bad0 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This header file defines the various variables that are shared among the
11// different components of the parser...
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef PARSER_INTERNALS_H
16#define PARSER_INTERNALS_H
17
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/iOther.h"
Chris Lattner2aac6bf2002-04-04 22:19:18 +000020#include "llvm/Function.h"
Chris Lattnereb5ff8d2001-09-07 16:33:01 +000021#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Assembly/Parser.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/StringExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000024
25class Module;
26
27// Global variables exported from the lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000028extern std::FILE *llvmAsmin;
Chris Lattner00950542001-06-06 20:29:01 +000029extern int llvmAsmlineno;
30
31// Globals exported by the parser...
Chris Lattner697954c2002-01-20 22:54:45 +000032extern std::string CurFilename;
33Module *RunVMAsmParser(const std::string &Filename, FILE *F);
Chris Lattner00950542001-06-06 20:29:01 +000034
Vikram S. Advef946bcd2002-07-14 22:49:40 +000035extern char* llvmAsmtext;
36extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner93750fa2001-07-28 17:48:55 +000038// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
39// appropriate character. If AllowNull is set to false, a \00 value will cause
40// an exception to be thrown.
41//
42// If AllowNull is set to true, the return value of the function points to the
43// last character of the string in memory.
44//
45char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
46
47
Chris Lattner00950542001-06-06 20:29:01 +000048// ThrowException - Wrapper around the ParseException class that automatically
49// fills in file line number and column number and options info.
50//
51// This also helps me because I keep typing 'throw new ParseException' instead
52// of just 'throw ParseException'... sigh...
53//
Chris Lattner697954c2002-01-20 22:54:45 +000054static inline void ThrowException(const std::string &message,
Chris Lattner93750fa2001-07-28 17:48:55 +000055 int LineNo = -1) {
56 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000057 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000058 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000059}
60
61// ValID - Represents a reference of a definition of some sort. This may either
62// be a numeric reference or a symbolic (%var) reference. This is just a
63// discriminated union.
64//
65// Note that I can't implement this class in a straight forward manner with
66// constructors and stuff because it goes in a union, and GCC doesn't like
67// putting classes with ctor's in unions. :(
68//
69struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000070 enum {
Chris Lattnerd78700d2002-08-16 21:14:40 +000071 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
72 ConstantVal,
Chris Lattner1a1cb112001-09-30 22:46:54 +000073 } Type;
74
Chris Lattner00950542001-06-06 20:29:01 +000075 union {
76 int Num; // If it's a numeric reference
77 char *Name; // If it's a named reference. Memory must be free'd.
78 int64_t ConstPool64; // Constant pool reference. This is the value
79 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000080 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000081 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattner00950542001-06-06 20:29:01 +000082 };
83
84 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000085 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000086 }
87
88 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000089 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000090 }
91
92 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000093 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000094 }
95
96 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000097 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000098 }
99
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000100 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000101 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
102 }
103
104 static ValID createNull() {
105 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000106 }
107
Chris Lattnerd78700d2002-08-16 21:14:40 +0000108 static ValID create(Constant *Val) {
109 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
110 }
111
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000112 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000113 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000114 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000115 }
116
117 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000118 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000119 ValID Result = *this;
120 Result.Name = strdup(Name);
121 return Result;
122 }
123
Chris Lattner697954c2002-01-20 22:54:45 +0000124 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000125 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000126 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000127 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000128 case ConstFPVal : return ftostr(ConstPoolFP);
129 case ConstNullVal : return "null";
130 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000131 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000132 case ConstantVal:
133 if (ConstantValue == ConstantBool::True) return "true";
134 if (ConstantValue == ConstantBool::False) return "false";
135 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000136 default:
137 assert(0 && "Unknown value!");
138 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000139 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000140 }
141 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000142
143 bool operator<(const ValID &V) const {
144 if (Type != V.Type) return Type < V.Type;
145 switch (Type) {
146 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000147 case NameVal: return strcmp(Name, V.Name) < 0;
148 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
149 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
150 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
151 case ConstNullVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000152 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000153 default: assert(0 && "Unknown value type!"); return false;
154 }
155 }
Chris Lattner00950542001-06-06 20:29:01 +0000156};
157
158
159
160template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000161class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000162 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000163 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000164public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000165 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
166 LineNum = llvmAsmlineno;
167 }
Chris Lattner00950542001-06-06 20:29:01 +0000168 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000169 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000170};
171
172struct InstPlaceHolderHelper : public Instruction {
173 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
174
Chris Lattner697954c2002-01-20 22:54:45 +0000175 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000176 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000177};
178
179struct BBPlaceHolderHelper : public BasicBlock {
180 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattnerc8a79af2002-04-08 21:59:08 +0000181 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000182 }
183};
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