blob: bb248c34ed1ac5dde74bf815a126446f2e6297a4 [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
Chris Lattner00950542001-06-06 20:29:01 +000025// 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...
Vikram S. Advef946bcd2002-07-14 22:49:40 +000030extern char* llvmAsmtext;
31extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000032
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
34
35// Globals exported by the parser...
36extern std::string CurFilename;
37
38class Module;
39Module *RunVMAsmParser(const std::string &Filename, FILE *F);
40
41
Chris Lattner93750fa2001-07-28 17:48:55 +000042// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
43// appropriate character. If AllowNull is set to false, a \00 value will cause
44// an exception to be thrown.
45//
46// If AllowNull is set to true, the return value of the function points to the
47// last character of the string in memory.
48//
49char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
50
51
Chris Lattner00950542001-06-06 20:29:01 +000052// ThrowException - Wrapper around the ParseException class that automatically
53// fills in file line number and column number and options info.
54//
55// This also helps me because I keep typing 'throw new ParseException' instead
56// of just 'throw ParseException'... sigh...
57//
Chris Lattner697954c2002-01-20 22:54:45 +000058static inline void ThrowException(const std::string &message,
Chris Lattner93750fa2001-07-28 17:48:55 +000059 int LineNo = -1) {
60 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000061 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000062 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000063}
64
65// ValID - Represents a reference of a definition of some sort. This may either
66// be a numeric reference or a symbolic (%var) reference. This is just a
67// discriminated union.
68//
69// Note that I can't implement this class in a straight forward manner with
Chris Lattnera989b232003-12-23 20:05:15 +000070// constructors and stuff because it goes in a union.
Chris Lattner00950542001-06-06 20:29:01 +000071//
72struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000073 enum {
Chris Lattnerd78700d2002-08-16 21:14:40 +000074 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
75 ConstantVal,
Chris Lattner1a1cb112001-09-30 22:46:54 +000076 } Type;
77
Chris Lattner00950542001-06-06 20:29:01 +000078 union {
79 int Num; // If it's a numeric reference
80 char *Name; // If it's a named reference. Memory must be free'd.
81 int64_t ConstPool64; // Constant pool reference. This is the value
82 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000083 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000084 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattner00950542001-06-06 20:29:01 +000085 };
86
87 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000088 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000089 }
90
91 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000092 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000093 }
94
95 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000096 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000097 }
98
99 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000100 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000101 }
102
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000103 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000104 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
105 }
106
107 static ValID createNull() {
108 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000109 }
110
Chris Lattnerd78700d2002-08-16 21:14:40 +0000111 static ValID create(Constant *Val) {
112 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
113 }
114
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000115 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000116 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000117 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000118 }
119
120 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000121 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000122 ValID Result = *this;
123 Result.Name = strdup(Name);
124 return Result;
125 }
126
Chris Lattner697954c2002-01-20 22:54:45 +0000127 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000128 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000129 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000130 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000131 case ConstFPVal : return ftostr(ConstPoolFP);
132 case ConstNullVal : return "null";
133 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000134 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000135 case ConstantVal:
136 if (ConstantValue == ConstantBool::True) return "true";
137 if (ConstantValue == ConstantBool::False) return "false";
138 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000139 default:
140 assert(0 && "Unknown value!");
141 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000142 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000143 }
144 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000145
146 bool operator<(const ValID &V) const {
147 if (Type != V.Type) return Type < V.Type;
148 switch (Type) {
149 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000150 case NameVal: return strcmp(Name, V.Name) < 0;
151 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
152 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
153 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
154 case ConstNullVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000155 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000156 default: assert(0 && "Unknown value type!"); return false;
157 }
158 }
Chris Lattner00950542001-06-06 20:29:01 +0000159};
160
161
162
163template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000164class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000165 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000166 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000167public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000168 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
169 LineNum = llvmAsmlineno;
170 }
Chris Lattner00950542001-06-06 20:29:01 +0000171 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000172 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000173};
174
175struct InstPlaceHolderHelper : public Instruction {
176 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
177
Chris Lattner697954c2002-01-20 22:54:45 +0000178 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000179 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000180};
181
182struct BBPlaceHolderHelper : public BasicBlock {
183 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
Chris Lattnerc8a79af2002-04-08 21:59:08 +0000184 assert(Ty == Type::LabelTy);
Chris Lattner00950542001-06-06 20:29:01 +0000185 }
186};
187
Chris Lattner93750fa2001-07-28 17:48:55 +0000188typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
189typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000190
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000191static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000192 const Type *Ty = Val->getType();
193 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000194 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000195 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000196
197 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000198 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
Chris Lattner93750fa2001-07-28 17:48:55 +0000199 default: return ((ValuePlaceHolder*)Val)->getDef();
200 }
201}
202
Chris Lattnereb5ff8d2001-09-07 16:33:01 +0000203static inline int getLineNumFromPlaceHolder(const Value *Val) {
Chris Lattneref9c23f2001-10-03 14:53:21 +0000204 const Type *Ty = Val->getType();
205 if (isa<PointerType>(Ty) &&
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000206 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000207 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattneref9c23f2001-10-03 14:53:21 +0000208
209 switch (Ty->getPrimitiveID()) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000210 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
Chris Lattner93750fa2001-07-28 17:48:55 +0000211 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000212 }
213}
214
Brian Gaeked0fde302003-11-11 22:41:34 +0000215} // End llvm namespace
216
Chris Lattner00950542001-06-06 20:29:01 +0000217#endif