blob: 8fd5031c05a42141610dbe7df7500f78d02dad04 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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 Lattner8e3a8e02007-11-18 08:46:26 +000026namespace llvm { class MemoryBuffer; }
Chris Lattner6184feb2005-05-20 03:25:47 +000027
Chris Lattner00950542001-06-06 20:29:01 +000028// Global variables exported from the lexer...
Chris Lattner6184feb2005-05-20 03:25:47 +000029
Reid Spencer61c83e02006-08-18 08:43:06 +000030extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
Chris Lattner00950542001-06-06 20:29:01 +000031
Chris Lattner6184feb2005-05-20 03:25:47 +000032// functions exported from the lexer
Chris Lattner8e3a8e02007-11-18 08:46:26 +000033void InitLLLexer(llvm::MemoryBuffer *MB);
34const char *LLLgetTokenStart();
35unsigned LLLgetTokenLength();
36std::string LLLgetFilename();
37unsigned LLLgetLineNo();
38void FreeLexer();
Chris Lattner00950542001-06-06 20:29:01 +000039
Brian Gaeked0fde302003-11-11 22:41:34 +000040namespace llvm {
Reid Spencer6ecdcc12007-05-22 18:52:21 +000041class Module;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Chris Lattner8e3a8e02007-11-18 08:46:26 +000043// RunVMAsmParser - Parse a buffer and return Module
44Module *RunVMAsmParser(llvm::MemoryBuffer *MB);
Chris Lattner93750fa2001-07-28 17:48:55 +000045
Reid Spencer6ecdcc12007-05-22 18:52:21 +000046// GenerateError - Wrapper around the ParseException class that automatically
Chris Lattner00950542001-06-06 20:29:01 +000047// fills in file line number and column number and options info.
48//
Misha Brukman019b6392005-04-21 21:10:11 +000049// This also helps me because I keep typing 'throw new ParseException' instead
Chris Lattner00950542001-06-06 20:29:01 +000050// of just 'throw ParseException'... sigh...
51//
Reid Spencer61c83e02006-08-18 08:43:06 +000052extern void GenerateError(const std::string &message, int LineNo = -1);
Chris Lattner00950542001-06-06 20:29:01 +000053
Chris Lattneraa2c8532006-01-25 22:26:43 +000054/// InlineAsmDescriptor - This is a simple class that holds info about inline
55/// asm blocks, for use by ValID.
56struct InlineAsmDescriptor {
57 std::string AsmString, Constraints;
58 bool HasSideEffects;
59
60 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
61 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
62};
63
64
Chris Lattner00950542001-06-06 20:29:01 +000065// ValID - Represents a reference of a definition of some sort. This may either
Misha Brukman019b6392005-04-21 21:10:11 +000066// be a numeric reference or a symbolic (%var) reference. This is just a
Chris Lattner00950542001-06-06 20:29:01 +000067// discriminated union.
68//
Misha Brukman019b6392005-04-21 21:10:11 +000069// 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 {
Reid Spencerb2d17862007-01-26 08:04:51 +000074 LocalID, GlobalID, LocalName, GlobalName,
75 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattneraa2c8532006-01-25 22:26:43 +000076 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000077 } Type;
78
Chris Lattner00950542001-06-06 20:29:01 +000079 union {
Reid Spencerb2d17862007-01-26 08:04:51 +000080 unsigned Num; // If it's a numeric reference like %1234
Reid Spencer6ecdcc12007-05-22 18:52:21 +000081 std::string *Name; // If it's a named reference. Memory must be deleted.
Chris Lattner00950542001-06-06 20:29:01 +000082 int64_t ConstPool64; // Constant pool reference. This is the value
83 uint64_t UConstPool64;// Unsigned constant pool reference.
Dale Johannesen43421b32007-09-06 18:13:44 +000084 APFloat *ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000085 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattneraa2c8532006-01-25 22:26:43 +000086 InlineAsmDescriptor *IAD;
Dale Johannesen43421b32007-09-06 18:13:44 +000087 };
Chris Lattner00950542001-06-06 20:29:01 +000088
Reid Spencerb2d17862007-01-26 08:04:51 +000089 static ValID createLocalID(unsigned Num) {
90 ValID D; D.Type = LocalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000091 }
Reid Spencerb2d17862007-01-26 08:04:51 +000092 static ValID createGlobalID(unsigned Num) {
93 ValID D; D.Type = GlobalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000094 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +000095 static ValID createLocalName(const std::string &Name) {
96 ValID D; D.Type = LocalName; D.Name = new std::string(Name); return D;
Reid Spencerb2d17862007-01-26 08:04:51 +000097 }
Reid Spencer6ecdcc12007-05-22 18:52:21 +000098 static ValID createGlobalName(const std::string &Name) {
99 ValID D; D.Type = GlobalName; D.Name = new std::string(Name); return D;
Reid Spencerb2d17862007-01-26 08:04:51 +0000100 }
101
Chris Lattner00950542001-06-06 20:29:01 +0000102 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000103 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000104 }
105
106 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000107 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000108 }
109
Dale Johannesen43421b32007-09-06 18:13:44 +0000110 static ValID create(APFloat *Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000111 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
112 }
113
114 static ValID createNull() {
115 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000116 }
117
Chris Lattner16710e92004-10-16 18:17:13 +0000118 static ValID createUndef() {
119 ValID D; D.Type = ConstUndefVal; return D;
120 }
121
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000122 static ValID createZeroInit() {
123 ValID D; D.Type = ConstZeroVal; return D;
124 }
125
Chris Lattnerd78700d2002-08-16 21:14:40 +0000126 static ValID create(Constant *Val) {
127 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
128 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000129
130 static ValID createInlineAsm(const std::string &AsmString,
131 const std::string &Constraints,
132 bool HasSideEffects) {
133 ValID D;
134 D.Type = InlineAsmVal;
135 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
136 return D;
137 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000138
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000139 inline void destroy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000140 if (Type == LocalName || Type == GlobalName)
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000141 delete Name; // Free this strdup'd memory.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000142 else if (Type == InlineAsmVal)
143 delete IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000144 }
145
146 inline ValID copy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000147 if (Type != LocalName && Type != GlobalName) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000148 ValID Result = *this;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000149 Result.Name = new std::string(*Name);
Chris Lattner00950542001-06-06 20:29:01 +0000150 return Result;
151 }
152
Chris Lattner697954c2002-01-20 22:54:45 +0000153 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000154 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000155 case LocalID : return '%' + utostr(Num);
156 case GlobalID : return '@' + utostr(Num);
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000157 case LocalName : return *Name;
158 case GlobalName : return *Name;
Dale Johannesen43421b32007-09-06 18:13:44 +0000159 case ConstFPVal : return ftostr(*ConstPoolFP);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000160 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000161 case ConstUndefVal : return "undef";
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000162 case ConstZeroVal : return "zeroinitializer";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000163 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000164 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000165 case ConstantVal:
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000166 if (ConstantValue == ConstantInt::getTrue()) return "true";
167 if (ConstantValue == ConstantInt::getFalse()) return "false";
Chris Lattnerd78700d2002-08-16 21:14:40 +0000168 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000169 default:
170 assert(0 && "Unknown value!");
171 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000172 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000173 }
174 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000175
176 bool operator<(const ValID &V) const {
177 if (Type != V.Type) return Type < V.Type;
178 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000179 case LocalID:
180 case GlobalID: return Num < V.Num;
181 case LocalName:
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000182 case GlobalName: return *Name < *V.Name;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000183 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
184 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
Dale Johannesen43421b32007-09-06 18:13:44 +0000185 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
186 APFloat::cmpLessThan;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000187 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000188 case ConstUndefVal: return false;
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000189 case ConstZeroVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000190 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000191 default: assert(0 && "Unknown value type!"); return false;
192 }
193 }
Reid Spencer4e0422c2007-03-19 18:34:28 +0000194
195 bool operator==(const ValID &V) const {
196 if (Type == V.Type) {
197 switch (Type) {
198 case LocalID:
199 case GlobalID: return Num == V.Num;
200 case LocalName:
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000201 case GlobalName: return *Name == *(V.Name);
Reid Spencer4e0422c2007-03-19 18:34:28 +0000202 case ConstSIntVal: return ConstPool64 == V.ConstPool64;
203 case ConstUIntVal: return UConstPool64 == V.UConstPool64;
Dale Johannesen43421b32007-09-06 18:13:44 +0000204 case ConstFPVal: return ConstPoolFP->compare(*V.ConstPoolFP) ==
205 APFloat::cmpEqual;
Reid Spencer4e0422c2007-03-19 18:34:28 +0000206 case ConstantVal: return ConstantValue == V.ConstantValue;
207 case ConstNullVal: return true;
208 case ConstUndefVal: return true;
209 case ConstZeroVal: return true;
210 default: assert(0 && "Unknown value type!"); return false;
211 }
212 }
213 return false;
214 }
Chris Lattner00950542001-06-06 20:29:01 +0000215};
216
Reid Spencer14310612006-12-31 05:40:51 +0000217struct TypeWithAttrs {
218 llvm::PATypeHolder *Ty;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000219 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000220};
221
222typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
223
224struct ArgListEntry {
Reid Spencer5694b6e2007-04-09 06:17:21 +0000225 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000226 llvm::PATypeHolder *Ty;
Reid Spencer6ecdcc12007-05-22 18:52:21 +0000227 std::string *Name;
Reid Spencer14310612006-12-31 05:40:51 +0000228};
229
230typedef std::vector<struct ArgListEntry> ArgListType;
231
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000232struct ParamListEntry {
Reid Spencer14310612006-12-31 05:40:51 +0000233 Value *Val;
Reid Spencer5694b6e2007-04-09 06:17:21 +0000234 uint16_t Attrs;
Reid Spencer14310612006-12-31 05:40:51 +0000235};
236
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000237typedef std::vector<ParamListEntry> ParamList;
Reid Spencer14310612006-12-31 05:40:51 +0000238
239
Brian Gaeked0fde302003-11-11 22:41:34 +0000240} // End llvm namespace
241
Chris Lattner00950542001-06-06 20:29:01 +0000242#endif