blob: 2b50cf6d55402e89dbd251769d001879f8cc80aa [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"
Chris Lattner00950542001-06-06 20:29:01 +000025
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 {
43
44// Globals exported by the parser...
Reid Spencer61c83e02006-08-18 08:43:06 +000045extern std::string CurFilename; /// FIXME: Not threading friendly
Brian Gaeked0fde302003-11-11 22:41:34 +000046
47class Module;
48Module *RunVMAsmParser(const std::string &Filename, FILE *F);
49
Chris Lattner6184feb2005-05-20 03:25:47 +000050// Parse a string directly
51Module *RunVMAsmParser(const char * AsmString, Module * M);
52
Brian Gaeked0fde302003-11-11 22:41:34 +000053
Chris Lattner93750fa2001-07-28 17:48:55 +000054// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
55// appropriate character. If AllowNull is set to false, a \00 value will cause
Reid Spencer61c83e02006-08-18 08:43:06 +000056// an error.
Chris Lattner93750fa2001-07-28 17:48:55 +000057//
58// If AllowNull is set to true, the return value of the function points to the
59// last character of the string in memory.
60//
61char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
62
63
Chris Lattner00950542001-06-06 20:29:01 +000064// ThrowException - Wrapper around the ParseException class that automatically
65// fills in file line number and column number and options info.
66//
Misha Brukman019b6392005-04-21 21:10:11 +000067// This also helps me because I keep typing 'throw new ParseException' instead
Chris Lattner00950542001-06-06 20:29:01 +000068// of just 'throw ParseException'... sigh...
69//
Reid Spencer61c83e02006-08-18 08:43:06 +000070extern void GenerateError(const std::string &message, int LineNo = -1);
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattneraa2c8532006-01-25 22:26:43 +000072/// InlineAsmDescriptor - This is a simple class that holds info about inline
73/// asm blocks, for use by ValID.
74struct InlineAsmDescriptor {
75 std::string AsmString, Constraints;
76 bool HasSideEffects;
77
78 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
79 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
80};
81
82
Chris Lattner00950542001-06-06 20:29:01 +000083// ValID - Represents a reference of a definition of some sort. This may either
Misha Brukman019b6392005-04-21 21:10:11 +000084// be a numeric reference or a symbolic (%var) reference. This is just a
Chris Lattner00950542001-06-06 20:29:01 +000085// discriminated union.
86//
Misha Brukman019b6392005-04-21 21:10:11 +000087// Note that I can't implement this class in a straight forward manner with
Chris Lattnera989b232003-12-23 20:05:15 +000088// constructors and stuff because it goes in a union.
Chris Lattner00950542001-06-06 20:29:01 +000089//
90struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000091 enum {
Reid Spencerb2d17862007-01-26 08:04:51 +000092 LocalID, GlobalID, LocalName, GlobalName,
93 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattneraa2c8532006-01-25 22:26:43 +000094 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000095 } Type;
96
Chris Lattner00950542001-06-06 20:29:01 +000097 union {
Reid Spencerb2d17862007-01-26 08:04:51 +000098 unsigned Num; // If it's a numeric reference like %1234
Chris Lattner00950542001-06-06 20:29:01 +000099 char *Name; // If it's a named reference. Memory must be free'd.
100 int64_t ConstPool64; // Constant pool reference. This is the value
101 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000102 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +0000103 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000104 InlineAsmDescriptor *IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000105 };
106
Reid Spencerb2d17862007-01-26 08:04:51 +0000107 static ValID createLocalID(unsigned Num) {
108 ValID D; D.Type = LocalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000109 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000110 static ValID createGlobalID(unsigned Num) {
111 ValID D; D.Type = GlobalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000112 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000113 static ValID createLocalName(char *Name) {
114 ValID D; D.Type = LocalName; D.Name = Name; return D;
115 }
116 static ValID createGlobalName(char *Name) {
117 ValID D; D.Type = GlobalName; D.Name = Name; return D;
118 }
119
Chris Lattner00950542001-06-06 20:29:01 +0000120 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000121 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000122 }
123
124 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000125 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000126 }
127
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000128 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000129 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
130 }
131
132 static ValID createNull() {
133 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000134 }
135
Chris Lattner16710e92004-10-16 18:17:13 +0000136 static ValID createUndef() {
137 ValID D; D.Type = ConstUndefVal; return D;
138 }
139
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000140 static ValID createZeroInit() {
141 ValID D; D.Type = ConstZeroVal; return D;
142 }
143
Chris Lattnerd78700d2002-08-16 21:14:40 +0000144 static ValID create(Constant *Val) {
145 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
146 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000147
148 static ValID createInlineAsm(const std::string &AsmString,
149 const std::string &Constraints,
150 bool HasSideEffects) {
151 ValID D;
152 D.Type = InlineAsmVal;
153 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
154 return D;
155 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000156
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000157 inline void destroy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000158 if (Type == LocalName || Type == GlobalName)
Chris Lattneraa2c8532006-01-25 22:26:43 +0000159 free(Name); // Free this strdup'd memory.
160 else if (Type == InlineAsmVal)
161 delete IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000162 }
163
164 inline ValID copy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000165 if (Type != LocalName && Type != GlobalName) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000166 ValID Result = *this;
167 Result.Name = strdup(Name);
168 return Result;
169 }
170
Chris Lattner697954c2002-01-20 22:54:45 +0000171 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000172 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000173 case LocalID : return '%' + utostr(Num);
174 case GlobalID : return '@' + utostr(Num);
175 case LocalName : return Name;
176 case GlobalName : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000177 case ConstFPVal : return ftostr(ConstPoolFP);
178 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000179 case ConstUndefVal : return "undef";
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000180 case ConstZeroVal : return "zeroinitializer";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000181 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000182 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000183 case ConstantVal:
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000184 if (ConstantValue == ConstantInt::getTrue()) return "true";
185 if (ConstantValue == ConstantInt::getFalse()) return "false";
Chris Lattnerd78700d2002-08-16 21:14:40 +0000186 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000187 default:
188 assert(0 && "Unknown value!");
189 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000190 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000191 }
192 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000193
194 bool operator<(const ValID &V) const {
195 if (Type != V.Type) return Type < V.Type;
196 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000197 case LocalID:
198 case GlobalID: return Num < V.Num;
199 case LocalName:
200 case GlobalName: return strcmp(Name, V.Name) < 0;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000201 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
202 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
203 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
204 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000205 case ConstUndefVal: return false;
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000206 case ConstZeroVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000207 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000208 default: assert(0 && "Unknown value type!"); return false;
209 }
210 }
Reid Spencer4e0422c2007-03-19 18:34:28 +0000211
212 bool operator==(const ValID &V) const {
213 if (Type == V.Type) {
214 switch (Type) {
215 case LocalID:
216 case GlobalID: return Num == V.Num;
217 case LocalName:
218 case GlobalName: return strcmp(Name, V.Name) == 0;
219 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
220 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
221 case ConstFPVal: return ConstPoolFP == V.ConstPoolFP;
222 case ConstantVal: return ConstantValue == V.ConstantValue;
223 case ConstNullVal: return true;
224 case ConstUndefVal: return true;
225 case ConstZeroVal: return true;
226 default: assert(0 && "Unknown value type!"); return false;
227 }
228 }
229 return false;
230 }
Chris Lattner00950542001-06-06 20:29:01 +0000231};
232
Reid Spencer14310612006-12-31 05:40:51 +0000233struct TypeWithAttrs {
234 llvm::PATypeHolder *Ty;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000235 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000236};
237
238typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
239
240struct ArgListEntry {
Reid Spencer5694b6e2007-04-09 06:17:21 +0000241 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000242 llvm::PATypeHolder *Ty;
243 char *Name;
244};
245
246typedef std::vector<struct ArgListEntry> ArgListType;
247
248struct ValueRefListEntry {
249 Value *Val;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000250 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000251};
252
253typedef std::vector<ValueRefListEntry> ValueRefList;
254
255
Brian Gaeked0fde302003-11-11 22:41:34 +0000256} // End llvm namespace
257
Chris Lattner00950542001-06-06 20:29:01 +0000258#endif