blob: 6f747c742633609a2c5f577387ca70e116b852a7 [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
75// ValID - Represents a reference of a definition of some sort. This may either
Misha Brukman019b6392005-04-21 21:10:11 +000076// be a numeric reference or a symbolic (%var) reference. This is just a
Chris Lattner00950542001-06-06 20:29:01 +000077// discriminated union.
78//
Misha Brukman019b6392005-04-21 21:10:11 +000079// Note that I can't implement this class in a straight forward manner with
Chris Lattnera989b232003-12-23 20:05:15 +000080// constructors and stuff because it goes in a union.
Chris Lattner00950542001-06-06 20:29:01 +000081//
82struct ValID {
Chris Lattner1a1cb112001-09-30 22:46:54 +000083 enum {
Chris Lattnerd78700d2002-08-16 21:14:40 +000084 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattnerf1f03df2005-12-21 17:53:02 +000085 ConstUndefVal, ConstZeroVal, ConstantVal,
Chris Lattner1a1cb112001-09-30 22:46:54 +000086 } Type;
87
Chris Lattner00950542001-06-06 20:29:01 +000088 union {
89 int Num; // If it's a numeric reference
90 char *Name; // If it's a named reference. Memory must be free'd.
91 int64_t ConstPool64; // Constant pool reference. This is the value
92 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000093 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000094 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattner00950542001-06-06 20:29:01 +000095 };
96
97 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000098 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000099 }
100
101 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000102 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000103 }
104
105 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000106 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000107 }
108
109 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000110 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000111 }
112
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000113 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000114 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
115 }
116
117 static ValID createNull() {
118 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000119 }
120
Chris Lattner16710e92004-10-16 18:17:13 +0000121 static ValID createUndef() {
122 ValID D; D.Type = ConstUndefVal; return D;
123 }
124
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000125 static ValID createZeroInit() {
126 ValID D; D.Type = ConstZeroVal; return D;
127 }
128
Chris Lattnerd78700d2002-08-16 21:14:40 +0000129 static ValID create(Constant *Val) {
130 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
131 }
132
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000133 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000134 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000135 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000136 }
137
138 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000139 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000140 ValID Result = *this;
141 Result.Name = strdup(Name);
142 return Result;
143 }
144
Chris Lattner697954c2002-01-20 22:54:45 +0000145 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000146 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000147 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000148 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000149 case ConstFPVal : return ftostr(ConstPoolFP);
150 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000151 case ConstUndefVal : return "undef";
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000152 case ConstZeroVal : return "zeroinitializer";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000153 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000154 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000155 case ConstantVal:
156 if (ConstantValue == ConstantBool::True) return "true";
157 if (ConstantValue == ConstantBool::False) return "false";
158 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000159 default:
160 assert(0 && "Unknown value!");
161 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000162 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000165
166 bool operator<(const ValID &V) const {
167 if (Type != V.Type) return Type < V.Type;
168 switch (Type) {
169 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000170 case NameVal: return strcmp(Name, V.Name) < 0;
171 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
172 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
173 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
174 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000175 case ConstUndefVal: return false;
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000176 case ConstZeroVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000177 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000178 default: assert(0 && "Unknown value type!"); return false;
179 }
180 }
Chris Lattner00950542001-06-06 20:29:01 +0000181};
182
Brian Gaeked0fde302003-11-11 22:41:34 +0000183} // End llvm namespace
184
Chris Lattner00950542001-06-06 20:29:01 +0000185#endif