blob: 820e5ba8a655674b6f6c78717684d95b00a2279d [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
Chris Lattner00950542001-06-06 20:29:01 +000028extern int llvmAsmlineno;
29
Chris Lattner6184feb2005-05-20 03:25:47 +000030extern std::string &llvmAsmTextin;
31
32// functions exported from the lexer
33void set_scan_file(FILE * F);
34void set_scan_string (const char * str);
35
Chris Lattner00950542001-06-06 20:29:01 +000036// Globals exported by the parser...
Vikram S. Advef946bcd2002-07-14 22:49:40 +000037extern char* llvmAsmtext;
38extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000039
Brian Gaeked0fde302003-11-11 22:41:34 +000040namespace llvm {
41
42// Globals exported by the parser...
43extern std::string CurFilename;
44
45class Module;
46Module *RunVMAsmParser(const std::string &Filename, FILE *F);
47
Chris Lattner6184feb2005-05-20 03:25:47 +000048// Parse a string directly
49Module *RunVMAsmParser(const char * AsmString, Module * M);
50
Brian Gaeked0fde302003-11-11 22:41:34 +000051
Chris Lattner93750fa2001-07-28 17:48:55 +000052// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
53// appropriate character. If AllowNull is set to false, a \00 value will cause
54// an exception to be thrown.
55//
56// If AllowNull is set to true, the return value of the function points to the
57// last character of the string in memory.
58//
59char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
60
61
Chris Lattner00950542001-06-06 20:29:01 +000062// ThrowException - Wrapper around the ParseException class that automatically
63// fills in file line number and column number and options info.
64//
Misha Brukman019b6392005-04-21 21:10:11 +000065// This also helps me because I keep typing 'throw new ParseException' instead
Chris Lattner00950542001-06-06 20:29:01 +000066// of just 'throw ParseException'... sigh...
67//
Chris Lattner697954c2002-01-20 22:54:45 +000068static inline void ThrowException(const std::string &message,
Misha Brukman019b6392005-04-21 21:10:11 +000069 int LineNo = -1) {
Chris Lattner93750fa2001-07-28 17:48:55 +000070 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000071 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000072 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000073}
74
Chris Lattneraa2c8532006-01-25 22:26:43 +000075/// InlineAsmDescriptor - This is a simple class that holds info about inline
76/// asm blocks, for use by ValID.
77struct InlineAsmDescriptor {
78 std::string AsmString, Constraints;
79 bool HasSideEffects;
80
81 InlineAsmDescriptor(const std::string &as, const std::string &c, bool HSE)
82 : AsmString(as), Constraints(c), HasSideEffects(HSE) {}
83};
84
85
Chris Lattner00950542001-06-06 20:29:01 +000086// ValID - Represents a reference of a definition of some sort. This may either
Misha Brukman019b6392005-04-21 21:10:11 +000087// be a numeric reference or a symbolic (%var) reference. This is just a
Chris Lattner00950542001-06-06 20:29:01 +000088// discriminated union.
89//
Misha Brukman019b6392005-04-21 21:10:11 +000090// Note that I can't implement this class in a straight forward manner with
Chris Lattnera989b232003-12-23 20:05:15 +000091// constructors and stuff because it goes in a union.
Chris Lattner00950542001-06-06 20:29:01 +000092//
93struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000094 enum {
Chris Lattnerd78700d2002-08-16 21:14:40 +000095 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattneraa2c8532006-01-25 22:26:43 +000096 ConstUndefVal, ConstZeroVal, ConstantVal, InlineAsmVal
Chris Lattner1a1cb112001-09-30 22:46:54 +000097 } Type;
98
Chris Lattner00950542001-06-06 20:29:01 +000099 union {
100 int Num; // If it's a numeric reference
101 char *Name; // If it's a named reference. Memory must be free'd.
102 int64_t ConstPool64; // Constant pool reference. This is the value
103 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000104 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +0000105 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000106 InlineAsmDescriptor *IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000107 };
108
109 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000110 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000111 }
112
113 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000114 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000115 }
116
117 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000118 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000119 }
120
121 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000122 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000123 }
124
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000125 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000126 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
127 }
128
129 static ValID createNull() {
130 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000131 }
132
Chris Lattner16710e92004-10-16 18:17:13 +0000133 static ValID createUndef() {
134 ValID D; D.Type = ConstUndefVal; return D;
135 }
136
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000137 static ValID createZeroInit() {
138 ValID D; D.Type = ConstZeroVal; return D;
139 }
140
Chris Lattnerd78700d2002-08-16 21:14:40 +0000141 static ValID create(Constant *Val) {
142 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
143 }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000144
145 static ValID createInlineAsm(const std::string &AsmString,
146 const std::string &Constraints,
147 bool HasSideEffects) {
148 ValID D;
149 D.Type = InlineAsmVal;
150 D.IAD = new InlineAsmDescriptor(AsmString, Constraints, HasSideEffects);
151 return D;
152 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000153
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000154 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000155 if (Type == NameVal)
Chris Lattneraa2c8532006-01-25 22:26:43 +0000156 free(Name); // Free this strdup'd memory.
157 else if (Type == InlineAsmVal)
158 delete IAD;
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
160
161 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000162 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000163 ValID Result = *this;
164 Result.Name = strdup(Name);
165 return Result;
166 }
167
Chris Lattner697954c2002-01-20 22:54:45 +0000168 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000169 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000170 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000171 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000172 case ConstFPVal : return ftostr(ConstPoolFP);
173 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000174 case ConstUndefVal : return "undef";
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000175 case ConstZeroVal : return "zeroinitializer";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000176 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000177 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000178 case ConstantVal:
179 if (ConstantValue == ConstantBool::True) return "true";
180 if (ConstantValue == ConstantBool::False) return "false";
181 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000182 default:
183 assert(0 && "Unknown value!");
184 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000185 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000186 }
187 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000188
189 bool operator<(const ValID &V) const {
190 if (Type != V.Type) return Type < V.Type;
191 switch (Type) {
192 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000193 case NameVal: return strcmp(Name, V.Name) < 0;
194 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
195 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
196 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
197 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000198 case ConstUndefVal: return false;
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000199 case ConstZeroVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000200 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000201 default: assert(0 && "Unknown value type!"); return false;
202 }
203 }
Chris Lattner00950542001-06-06 20:29:01 +0000204};
205
Brian Gaeked0fde302003-11-11 22:41:34 +0000206} // End llvm namespace
207
Chris Lattner00950542001-06-06 20:29:01 +0000208#endif