blob: 87c6af7b4a34ba2b34c93bab1cb43dfe4639d1fb [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 {
Chris Lattnerd78700d2002-08-16 21:14:40 +000091 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattneraa2c8532006-01-25 22:26:43 +000092 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000093 } Type;
94
Chris Lattner00950542001-06-06 20:29:01 +000095 union {
96 int Num; // If it's a numeric reference
97 char *Name; // If it's a named reference. Memory must be free'd.
98 int64_t ConstPool64; // Constant pool reference. This is the value
99 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000100 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +0000101 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000102 InlineAsmDescriptor *IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000103 };
104
105 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000106 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000107 }
108
109 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000110 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000111 }
112
113 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000114 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000115 }
116
117 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000118 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000119 }
120
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000121 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000122 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
123 }
124
125 static ValID createNull() {
126 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000127 }
128
Chris Lattner16710e92004-10-16 18:17:13 +0000129 static ValID createUndef() {
130 ValID D; D.Type = ConstUndefVal; return D;
131 }
132
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000133 static ValID createZeroInit() {
134 ValID D; D.Type = ConstZeroVal; return D;
135 }
136
Chris Lattnerd78700d2002-08-16 21:14:40 +0000137 static ValID create(Constant *Val) {
138 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
139 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000140
141 static ValID createInlineAsm(const std::string &AsmString,
142 const std::string &Constraints,
143 bool HasSideEffects) {
144 ValID D;
145 D.Type = InlineAsmVal;
146 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
147 return D;
148 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000149
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000150 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000151 if (Type == NameVal)
Chris Lattneraa2c8532006-01-25 22:26:43 +0000152 free(Name); // Free this strdup'd memory.
153 else if (Type == InlineAsmVal)
154 delete IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156
157 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000158 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000159 ValID Result = *this;
160 Result.Name = strdup(Name);
161 return Result;
162 }
163
Chris Lattner697954c2002-01-20 22:54:45 +0000164 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000165 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000166 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000167 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000168 case ConstFPVal : return ftostr(ConstPoolFP);
169 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000170 case ConstUndefVal : return "undef";
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000171 case ConstZeroVal : return "zeroinitializer";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000172 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000173 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000174 case ConstantVal:
Chris Lattner47811b72006-09-28 23:35:22 +0000175 if (ConstantValue == ConstantBool::getTrue()) return "true";
176 if (ConstantValue == ConstantBool::getFalse()) return "false";
Chris Lattnerd78700d2002-08-16 21:14:40 +0000177 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000178 default:
179 assert(0 && "Unknown value!");
180 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000181 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000182 }
183 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000184
185 bool operator<(const ValID &V) const {
186 if (Type != V.Type) return Type < V.Type;
187 switch (Type) {
188 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000189 case NameVal: return strcmp(Name, V.Name) < 0;
190 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
191 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
192 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
193 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000194 case ConstUndefVal: return false;
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000195 case ConstZeroVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000196 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000197 default: assert(0 && "Unknown value type!"); return false;
198 }
199 }
Chris Lattner00950542001-06-06 20:29:01 +0000200};
201
Brian Gaeked0fde302003-11-11 22:41:34 +0000202} // End llvm namespace
203
Reid Spencer1628cec2006-10-26 06:15:43 +0000204// This structure is used to keep track of obsolete opcodes. The lexer will
205// retain the ability to parse obsolete opcode mnemonics. In this case it will
206// set "obsolete" to true and the opcode will be the replacement opcode. For
207// example if "rem" is encountered then opcode will be set to "urem" and the
208// "obsolete" flag will be true. If the opcode is not obsolete then "obsolete"
209// will be false.
210template <class Enum>
211struct OpcodeInfo {
212 Enum opcode;
213 bool obsolete;
214};
Reid Spencer3da59db2006-11-27 01:05:10 +0000215typedef OpcodeInfo<llvm::Instruction::BinaryOps> BinaryOpInfo;
216typedef OpcodeInfo<llvm::Instruction::TermOps> TermOpInfo;
217typedef OpcodeInfo<llvm::Instruction::MemoryOps> MemOpInfo;
218typedef OpcodeInfo<llvm::Instruction::CastOps> CastOpInfo;
219typedef OpcodeInfo<llvm::Instruction::OtherOps> OtherOpInfo;
Reid Spencer1628cec2006-10-26 06:15:43 +0000220
Reid Spencer36699ca2006-11-19 23:07:00 +0000221/// This enumeration is used to indicate if a type is signed, signless or
222/// unsigned. It is used for backwards compatibility with assembly code that
223/// pre-dates the signless types conversion.
224enum Signedness {
225 isSigned,
226 isUnsigned,
227 isSignless
228};
229
230/// This structure is used to keep track of the signedness of the obsolete
231/// integer types. Instead of creating an llvm::Type directly, the Lexer will
232/// create instances of TypeInfo which retains the signedness indication so
233/// it can be used by the parser for upgrade decisions.
234/// For example if "uint" is encountered then the type will be set "int32"
235/// and the "signedness" will be "isUnsigned". If the type is not obsolete
236/// then "signedness" will be "isSignless".
237struct TypeInfo {
238 llvm::PATypeHolder *type;
239 Signedness signedness;
240};
241
242struct ValueInfo {
243 std::vector<llvm::Value*> valuelist;
244 std::vector<Signedness> signlist;
245};
246
Chris Lattner00950542001-06-06 20:29:01 +0000247#endif