blob: 61de652c516316a5fb7c02be68ff223a8d5c40d0 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
Misha Brukman019b6392005-04-21 21:10:11 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// 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.
Misha Brukman019b6392005-04-21 21:10:11 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Misha Brukman019b6392005-04-21 21:10:11 +000010// This header file defines the various variables that are shared among the
Chris Lattner00950542001-06-06 20:29:01 +000011// 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 Lattnereb5ff8d2001-09-07 16:33:01 +000019#include "llvm/DerivedTypes.h"
Reid Spencer5694b6e2007-04-09 06:17:21 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000021#include "llvm/Function.h"
22#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000023#include "llvm/Assembly/Parser.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/StringExtras.h"
Dale Johannesen43421b32007-09-06 18:13:44 +000025#include "llvm/ADT/APFloat.h"
Chris Lattner6184feb2005-05-20 03:25:47 +000026
Chris Lattner00950542001-06-06 20:29:01 +000027// Global variables exported from the lexer...
Chris Lattner6184feb2005-05-20 03:25:47 +000028
Reid Spencer61c83e02006-08-18 08:43:06 +000029extern int llvmAsmlineno; /// FIXME: Not threading friendly
30extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
Chris Lattner00950542001-06-06 20:29:01 +000031
Chris Lattner6184feb2005-05-20 03:25:47 +000032extern std::string &llvmAsmTextin;
33
34// functions exported from the lexer
35void set_scan_file(FILE * F);
36void set_scan_string (const char * str);
37
Chris Lattner00950542001-06-06 20:29:01 +000038// Globals exported by the parser...
Vikram S. Advef946bcd2002-07-14 22:49:40 +000039extern char* llvmAsmtext;
40extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000041
Brian Gaeked0fde302003-11-11 22:41:34 +000042namespace llvm {
Reid Spencer6ecdcc12007-05-22 18:52:21 +000043class Module;
Brian Gaeked0fde302003-11-11 22:41:34 +000044
45// Globals exported by the parser...
Reid Spencer61c83e02006-08-18 08:43:06 +000046extern std::string CurFilename; /// FIXME: Not threading friendly
Brian Gaeked0fde302003-11-11 22:41:34 +000047
Reid Spencer6ecdcc12007-05-22 18:52:21 +000048// RunVMAsmParser - Parse a file and return Module
Brian Gaeked0fde302003-11-11 22:41:34 +000049Module *RunVMAsmParser(const std::string &Filename, FILE *F);
50
Chris Lattner6184feb2005-05-20 03:25:47 +000051// Parse a string directly
52Module *RunVMAsmParser(const char * AsmString, Module * M);
53
Chris Lattner93750fa2001-07-28 17:48:55 +000054// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
Reid Spencer6ecdcc12007-05-22 18:52:21 +000055// appropriate character.
56char *UnEscapeLexed(char *Buffer);
Chris Lattner93750fa2001-07-28 17:48:55 +000057
Reid Spencer6ecdcc12007-05-22 18:52:21 +000058// GenerateError - Wrapper around the ParseException class that automatically
Chris Lattner00950542001-06-06 20:29:01 +000059// fills in file line number and column number and options info.
60//
Misha Brukman019b6392005-04-21 21:10:11 +000061// This also helps me because I keep typing 'throw new ParseException' instead
Chris Lattner00950542001-06-06 20:29:01 +000062// of just 'throw ParseException'... sigh...
63//
Reid Spencer61c83e02006-08-18 08:43:06 +000064extern void GenerateError(const std::string &message, int LineNo = -1);
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattneraa2c8532006-01-25 22:26:43 +000066/// InlineAsmDescriptor - This is a simple class that holds info about inline
67/// asm blocks, for use by ValID.
68struct InlineAsmDescriptor {
69 std::string AsmString, Constraints;
70 bool HasSideEffects;
71
72 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
73 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
74};
75
76
Chris Lattner00950542001-06-06 20:29:01 +000077// ValID - Represents a reference of a definition of some sort. This may either
Misha Brukman019b6392005-04-21 21:10:11 +000078// be a numeric reference or a symbolic (%var) reference. This is just a
Chris Lattner00950542001-06-06 20:29:01 +000079// discriminated union.
80//
Misha Brukman019b6392005-04-21 21:10:11 +000081// Note that I can't implement this class in a straight forward manner with
Chris Lattnera989b232003-12-23 20:05:15 +000082// constructors and stuff because it goes in a union.
Chris Lattner00950542001-06-06 20:29:01 +000083//
84struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000085 enum {
Reid Spencerb2d17862007-01-26 08:04:51 +000086 LocalID, GlobalID, LocalName, GlobalName,
87 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattneraa2c8532006-01-25 22:26:43 +000088 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000089 } Type;
90
Chris Lattner00950542001-06-06 20:29:01 +000091 union {
Reid Spencerb2d17862007-01-26 08:04:51 +000092 unsigned Num; // If it's a numeric reference like %1234
Reid Spencer6ecdcc12007-05-22 18:52:21 +000093 std::string *Name; // If it's a named reference. Memory must be deleted.
Chris Lattner00950542001-06-06 20:29:01 +000094 int64_t ConstPool64; // Constant pool reference. This is the value
95 uint64_t UConstPool64;// Unsigned constant pool reference.
Dale Johannesen43421b32007-09-06 18:13:44 +000096 APFloat *ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000097 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattneraa2c8532006-01-25 22:26:43 +000098 InlineAsmDescriptor *IAD;
Dale Johannesen43421b32007-09-06 18:13:44 +000099 };
Chris Lattner00950542001-06-06 20:29:01 +0000100
Reid Spencerb2d17862007-01-26 08:04:51 +0000101 static ValID createLocalID(unsigned Num) {
102 ValID D; D.Type = LocalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000103 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000104 static ValID createGlobalID(unsigned Num) {
105 ValID D; D.Type = GlobalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000106 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000107 static ValID createLocalName(const std::string &Name) {
108 ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
Reid Spencerb2d17862007-01-26 08:04:51 +0000109 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000110 static ValID createGlobalName(const std::string &Name) {
111 ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
Reid Spencerb2d17862007-01-26 08:04:51 +0000112 }
113
Chris Lattner00950542001-06-06 20:29:01 +0000114 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000115 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000116 }
117
118 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000119 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000120 }
121
Dale Johannesen43421b32007-09-06 18:13:44 +0000122 static ValID create(APFloat *Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000123 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
124 }
125
126 static ValID createNull() {
127 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000128 }
129
Chris Lattner16710e92004-10-16 18:17:13 +0000130 static ValID createUndef() {
131 ValID D; D.Type = ConstUndefVal; return D;
132 }
133
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000134 static ValID createZeroInit() {
135 ValID D; D.Type = ConstZeroVal; return D;
136 }
137
Chris Lattnerd78700d2002-08-16 21:14:40 +0000138 static ValID create(Constant *Val) {
139 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
140 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000141
142 static ValID createInlineAsm(const std::string &AsmString,
143 const std::string &Constraints,
144 bool HasSideEffects) {
145 ValID D;
146 D.Type = InlineAsmVal;
147 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
148 return D;
149 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000150
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000151 inline void destroy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000152 if (Type == LocalName || Type == GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000153 delete Name; // Free this strdup'd memory.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000154 else if (Type == InlineAsmVal)
155 delete IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157
158 inline ValID copy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000159 if (Type != LocalName && Type != GlobalName) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000160 ValID Result = *this;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000161 Result.Name = new std::string(*Name);
Chris Lattner00950542001-06-06 20:29:01 +0000162 return Result;
163 }
164
Chris Lattner697954c2002-01-20 22:54:45 +0000165 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000166 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000167 case LocalID : return '%' + utostr(Num);
168 case GlobalID : return '@' + utostr(Num);
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000169 case LocalName : return *Name;
170 case GlobalName : return *Name;
Dale Johannesen43421b32007-09-06 18:13:44 +0000171 case ConstFPVal : return ftostr(*ConstPoolFP);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000172 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000173 case ConstUndefVal : return "undef";
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000174 case ConstZeroVal : return "zeroinitializer";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000175 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000176 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000177 case ConstantVal:
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000178 if (ConstantValue == ConstantInt::getTrue()) return "true";
179 if (ConstantValue == ConstantInt::getFalse()) return "false";
Chris Lattnerd78700d2002-08-16 21:14:40 +0000180 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000181 default:
182 assert(0 && "Unknown value!");
183 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000184 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000185 }
186 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000187
188 bool operator<(const ValID &V) const {
189 if (Type != V.Type) return Type < V.Type;
190 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000191 case LocalID:
192 case GlobalID: return Num < V.Num;
193 case LocalName:
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000194 case GlobalName: return *Name < *V.Name;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000195 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
196 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
Dale Johannesen43421b32007-09-06 18:13:44 +0000197 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
198 APFloat::cmpLessThan;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000199 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000200 case ConstUndefVal: return false;
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000201 case ConstZeroVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000202 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000203 default: assert(0 && "Unknown value type!"); return false;
204 }
205 }
Reid Spencer4e0422c2007-03-19 18:34:28 +0000206
207 bool operator==(const ValID &V) const {
208 if (Type == V.Type) {
209 switch (Type) {
210 case LocalID:
211 case GlobalID: return Num == V.Num;
212 case LocalName:
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000213 case GlobalName: return *Name == *(V.Name);
Reid Spencer4e0422c2007-03-19 18:34:28 +0000214 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
215 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
Dale Johannesen43421b32007-09-06 18:13:44 +0000216 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
217 APFloat::cmpEqual;
Reid Spencer4e0422c2007-03-19 18:34:28 +0000218 case ConstantVal: return ConstantValue == V.ConstantValue;
219 case ConstNullVal: return true;
220 case ConstUndefVal: return true;
221 case ConstZeroVal: return true;
222 default: assert(0 && "Unknown value type!"); return false;
223 }
224 }
225 return false;
226 }
Chris Lattner00950542001-06-06 20:29:01 +0000227};
228
Reid Spencer14310612006-12-31 05:40:51 +0000229struct TypeWithAttrs {
230 llvm::PATypeHolder *Ty;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000231 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000232};
233
234typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
235
236struct ArgListEntry {
Reid Spencer5694b6e2007-04-09 06:17:21 +0000237 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000238 llvm::PATypeHolder *Ty;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000239 std::string *Name;
Reid Spencer14310612006-12-31 05:40:51 +0000240};
241
242typedef std::vector<struct ArgListEntry> ArgListType;
243
244struct ValueRefListEntry {
245 Value *Val;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000246 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000247};
248
249typedef std::vector<ValueRefListEntry> ValueRefList;
250
251
Brian Gaeked0fde302003-11-11 22:41:34 +0000252} // End llvm namespace
253
Chris Lattner00950542001-06-06 20:29:01 +0000254#endif