blob: 1bc744bc4bba3b0a2dd0649b0cc3eb77956febce [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"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000020#include "llvm/Function.h"
21#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Assembly/Parser.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000024
Chris Lattner6184feb2005-05-20 03:25:47 +000025
Chris Lattner00950542001-06-06 20:29:01 +000026// Global variables exported from the lexer...
Chris Lattner6184feb2005-05-20 03:25:47 +000027
Reid Spencer61c83e02006-08-18 08:43:06 +000028extern int llvmAsmlineno; /// FIXME: Not threading friendly
29extern llvm::ParseError* TheParseError; /// FIXME: Not threading friendly
Chris Lattner00950542001-06-06 20:29:01 +000030
Chris Lattner6184feb2005-05-20 03:25:47 +000031extern std::string &llvmAsmTextin;
32
33// functions exported from the lexer
34void set_scan_file(FILE * F);
35void set_scan_string (const char * str);
36
Chris Lattner00950542001-06-06 20:29:01 +000037// Globals exported by the parser...
Vikram S. Advef946bcd2002-07-14 22:49:40 +000038extern char* llvmAsmtext;
39extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000040
Brian Gaeked0fde302003-11-11 22:41:34 +000041namespace llvm {
42
43// Globals exported by the parser...
Reid Spencer61c83e02006-08-18 08:43:06 +000044extern std::string CurFilename; /// FIXME: Not threading friendly
Brian Gaeked0fde302003-11-11 22:41:34 +000045
46class Module;
47Module *RunVMAsmParser(const std::string &Filename, FILE *F);
48
Chris Lattner6184feb2005-05-20 03:25:47 +000049// Parse a string directly
50Module *RunVMAsmParser(const char * AsmString, Module * M);
51
Brian Gaeked0fde302003-11-11 22:41:34 +000052
Chris Lattner93750fa2001-07-28 17:48:55 +000053// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
54// appropriate character. If AllowNull is set to false, a \00 value will cause
Reid Spencer61c83e02006-08-18 08:43:06 +000055// an error.
Chris Lattner93750fa2001-07-28 17:48:55 +000056//
57// If AllowNull is set to true, the return value of the function points to the
58// last character of the string in memory.
59//
60char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
61
62
Chris Lattner00950542001-06-06 20:29:01 +000063// ThrowException - Wrapper around the ParseException class that automatically
64// fills in file line number and column number and options info.
65//
Misha Brukman019b6392005-04-21 21:10:11 +000066// This also helps me because I keep typing 'throw new ParseException' instead
Chris Lattner00950542001-06-06 20:29:01 +000067// of just 'throw ParseException'... sigh...
68//
Reid Spencer61c83e02006-08-18 08:43:06 +000069extern void GenerateError(const std::string &message, int LineNo = -1);
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattneraa2c8532006-01-25 22:26:43 +000071/// InlineAsmDescriptor - This is a simple class that holds info about inline
72/// asm blocks, for use by ValID.
73struct InlineAsmDescriptor {
74 std::string AsmString, Constraints;
75 bool HasSideEffects;
76
77 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
78 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
79};
80
81
Chris Lattner00950542001-06-06 20:29:01 +000082// ValID - Represents a reference of a definition of some sort. This may either
Misha Brukman019b6392005-04-21 21:10:11 +000083// be a numeric reference or a symbolic (%var) reference. This is just a
Chris Lattner00950542001-06-06 20:29:01 +000084// discriminated union.
85//
Misha Brukman019b6392005-04-21 21:10:11 +000086// Note that I can't implement this class in a straight forward manner with
Chris Lattnera989b232003-12-23 20:05:15 +000087// constructors and stuff because it goes in a union.
Chris Lattner00950542001-06-06 20:29:01 +000088//
89struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000090 enum {
Reid Spencerb2d17862007-01-26 08:04:51 +000091 LocalID, GlobalID, LocalName, GlobalName,
92 ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattneraa2c8532006-01-25 22:26:43 +000093 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000094 } Type;
95
Chris Lattner00950542001-06-06 20:29:01 +000096 union {
Reid Spencerb2d17862007-01-26 08:04:51 +000097 unsigned Num; // If it's a numeric reference like %1234
Chris Lattner00950542001-06-06 20:29:01 +000098 char *Name; // If it's a named reference. Memory must be free'd.
99 int64_t ConstPool64; // Constant pool reference. This is the value
100 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000101 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +0000102 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000103 InlineAsmDescriptor *IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000104 };
105
Reid Spencerb2d17862007-01-26 08:04:51 +0000106 static ValID createLocalID(unsigned Num) {
107 ValID D; D.Type = LocalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000108 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000109 static ValID createGlobalID(unsigned Num) {
110 ValID D; D.Type = GlobalID; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000111 }
Reid Spencerb2d17862007-01-26 08:04:51 +0000112 static ValID createLocalName(char *Name) {
113 ValID D; D.Type = LocalName; D.Name = Name; return D;
114 }
115 static ValID createGlobalName(char *Name) {
116 ValID D; D.Type = GlobalName; D.Name = Name; return D;
117 }
118
Chris Lattner00950542001-06-06 20:29:01 +0000119 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000120 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000121 }
122
123 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000124 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000125 }
126
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000127 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000128 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
129 }
130
131 static ValID createNull() {
132 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000133 }
134
Chris Lattner16710e92004-10-16 18:17:13 +0000135 static ValID createUndef() {
136 ValID D; D.Type = ConstUndefVal; return D;
137 }
138
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000139 static ValID createZeroInit() {
140 ValID D; D.Type = ConstZeroVal; return D;
141 }
142
Chris Lattnerd78700d2002-08-16 21:14:40 +0000143 static ValID create(Constant *Val) {
144 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
145 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000146
147 static ValID createInlineAsm(const std::string &AsmString,
148 const std::string &Constraints,
149 bool HasSideEffects) {
150 ValID D;
151 D.Type = InlineAsmVal;
152 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
153 return D;
154 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000155
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000156 inline void destroy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000157 if (Type == LocalName || Type == GlobalName)
Chris Lattneraa2c8532006-01-25 22:26:43 +0000158 free(Name); // Free this strdup'd memory.
159 else if (Type == InlineAsmVal)
160 delete IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000161 }
162
163 inline ValID copy() const {
Reid Spencerb2d17862007-01-26 08:04:51 +0000164 if (Type != LocalName && Type != GlobalName) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000165 ValID Result = *this;
166 Result.Name = strdup(Name);
167 return Result;
168 }
169
Chris Lattner697954c2002-01-20 22:54:45 +0000170 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000171 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000172 case LocalID : return '%' + utostr(Num);
173 case GlobalID : return '@' + utostr(Num);
174 case LocalName : return Name;
175 case GlobalName : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000176 case ConstFPVal : return ftostr(ConstPoolFP);
177 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000178 case ConstUndefVal : return "undef";
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000179 case ConstZeroVal : return "zeroinitializer";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000180 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000181 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000182 case ConstantVal:
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000183 if (ConstantValue == ConstantInt::getTrue()) return "true";
184 if (ConstantValue == ConstantInt::getFalse()) return "false";
Chris Lattnerd78700d2002-08-16 21:14:40 +0000185 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000186 default:
187 assert(0 && "Unknown value!");
188 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000189 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000190 }
191 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000192
193 bool operator<(const ValID &V) const {
194 if (Type != V.Type) return Type < V.Type;
195 switch (Type) {
Reid Spencerb2d17862007-01-26 08:04:51 +0000196 case LocalID:
197 case GlobalID: return Num < V.Num;
198 case LocalName:
199 case GlobalName: return strcmp(Name, V.Name) < 0;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000200 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
201 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
202 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
203 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000204 case ConstUndefVal: return false;
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000205 case ConstZeroVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000206 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000207 default: assert(0 && "Unknown value type!"); return false;
208 }
209 }
Chris Lattner00950542001-06-06 20:29:01 +0000210};
211
Reid Spencer14310612006-12-31 05:40:51 +0000212struct TypeWithAttrs {
213 llvm::PATypeHolder *Ty;
214 FunctionType::ParameterAttributes Attrs;
215};
216
217typedef std::vector<TypeWithAttrs> TypeWithAttrsList;
218
219struct ArgListEntry {
220 FunctionType::ParameterAttributes Attrs;
221 llvm::PATypeHolder *Ty;
222 char *Name;
223};
224
225typedef std::vector<struct ArgListEntry> ArgListType;
226
227struct ValueRefListEntry {
228 Value *Val;
229 FunctionType::ParameterAttributes Attrs;
230};
231
232typedef std::vector<ValueRefListEntry> ValueRefList;
233
234
Brian Gaeked0fde302003-11-11 22:41:34 +0000235} // End llvm namespace
236
Chris Lattner00950542001-06-06 20:29:01 +0000237#endif