blob: 98ea0341c690c76e641b9a392f622ddab348df0a [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This header file defines the various variables that are shared among the
11// 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 Lattner00950542001-06-06 20:29:01 +000025// Global variables exported from the lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000026extern std::FILE *llvmAsmin;
Chris Lattner00950542001-06-06 20:29:01 +000027extern int llvmAsmlineno;
28
29// Globals exported by the parser...
Vikram S. Advef946bcd2002-07-14 22:49:40 +000030extern char* llvmAsmtext;
31extern int llvmAsmleng;
Chris Lattner00950542001-06-06 20:29:01 +000032
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
34
35// Globals exported by the parser...
36extern std::string CurFilename;
37
38class Module;
39Module *RunVMAsmParser(const std::string &Filename, FILE *F);
40
41
Chris Lattner93750fa2001-07-28 17:48:55 +000042// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
43// appropriate character. If AllowNull is set to false, a \00 value will cause
44// an exception to be thrown.
45//
46// If AllowNull is set to true, the return value of the function points to the
47// last character of the string in memory.
48//
49char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
50
51
Chris Lattner00950542001-06-06 20:29:01 +000052// ThrowException - Wrapper around the ParseException class that automatically
53// fills in file line number and column number and options info.
54//
55// This also helps me because I keep typing 'throw new ParseException' instead
56// of just 'throw ParseException'... sigh...
57//
Chris Lattner697954c2002-01-20 22:54:45 +000058static inline void ThrowException(const std::string &message,
Chris Lattner93750fa2001-07-28 17:48:55 +000059 int LineNo = -1) {
60 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000061 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000062 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000063}
64
65// ValID - Represents a reference of a definition of some sort. This may either
66// be a numeric reference or a symbolic (%var) reference. This is just a
67// discriminated union.
68//
69// 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 {
Chris Lattnerd78700d2002-08-16 21:14:40 +000074 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
Chris Lattner16710e92004-10-16 18:17:13 +000075 ConstUndefVal, ConstantVal,
Chris Lattner1a1cb112001-09-30 22:46:54 +000076 } Type;
77
Chris Lattner00950542001-06-06 20:29:01 +000078 union {
79 int Num; // If it's a numeric reference
80 char *Name; // If it's a named reference. Memory must be free'd.
81 int64_t ConstPool64; // Constant pool reference. This is the value
82 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000083 double ConstPoolFP; // Floating point constant pool reference
Chris Lattnerd78700d2002-08-16 21:14:40 +000084 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
Chris Lattner00950542001-06-06 20:29:01 +000085 };
86
87 static ValID create(int Num) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000088 ValID D; D.Type = NumberVal; D.Num = Num; return D;
Chris Lattner00950542001-06-06 20:29:01 +000089 }
90
91 static ValID create(char *Name) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000092 ValID D; D.Type = NameVal; D.Name = Name; return D;
Chris Lattner00950542001-06-06 20:29:01 +000093 }
94
95 static ValID create(int64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +000096 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +000097 }
98
99 static ValID create(uint64_t Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000100 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
Chris Lattner00950542001-06-06 20:29:01 +0000101 }
102
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000103 static ValID create(double Val) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000104 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
105 }
106
107 static ValID createNull() {
108 ValID D; D.Type = ConstNullVal; return D;
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000109 }
110
Chris Lattner16710e92004-10-16 18:17:13 +0000111 static ValID createUndef() {
112 ValID D; D.Type = ConstUndefVal; return D;
113 }
114
Chris Lattnerd78700d2002-08-16 21:14:40 +0000115 static ValID create(Constant *Val) {
116 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
117 }
118
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000119 inline void destroy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000120 if (Type == NameVal)
Chris Lattner1a1cb112001-09-30 22:46:54 +0000121 free(Name); // Free this strdup'd memory...
Chris Lattner00950542001-06-06 20:29:01 +0000122 }
123
124 inline ValID copy() const {
Chris Lattner18b24ea2002-04-28 21:57:50 +0000125 if (Type != NameVal) return *this;
Chris Lattner00950542001-06-06 20:29:01 +0000126 ValID Result = *this;
127 Result.Name = strdup(Name);
128 return Result;
129 }
130
Chris Lattner697954c2002-01-20 22:54:45 +0000131 inline std::string getName() const {
Chris Lattner00950542001-06-06 20:29:01 +0000132 switch (Type) {
Chris Lattner697954c2002-01-20 22:54:45 +0000133 case NumberVal : return std::string("#") + itostr(Num);
Chris Lattner1a1cb112001-09-30 22:46:54 +0000134 case NameVal : return Name;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000135 case ConstFPVal : return ftostr(ConstPoolFP);
136 case ConstNullVal : return "null";
Chris Lattner16710e92004-10-16 18:17:13 +0000137 case ConstUndefVal : return "undef";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000138 case ConstUIntVal :
Chris Lattner697954c2002-01-20 22:54:45 +0000139 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
Chris Lattnerd78700d2002-08-16 21:14:40 +0000140 case ConstantVal:
141 if (ConstantValue == ConstantBool::True) return "true";
142 if (ConstantValue == ConstantBool::False) return "false";
143 return "<constant expression>";
Chris Lattner1a1cb112001-09-30 22:46:54 +0000144 default:
145 assert(0 && "Unknown value!");
146 abort();
Chris Lattner697954c2002-01-20 22:54:45 +0000147 return "";
Chris Lattner00950542001-06-06 20:29:01 +0000148 }
149 }
Chris Lattner8c9c5862001-10-13 06:37:47 +0000150
151 bool operator<(const ValID &V) const {
152 if (Type != V.Type) return Type < V.Type;
153 switch (Type) {
154 case NumberVal: return Num < V.Num;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000155 case NameVal: return strcmp(Name, V.Name) < 0;
156 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
157 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
158 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
159 case ConstNullVal: return false;
Chris Lattner16710e92004-10-16 18:17:13 +0000160 case ConstUndefVal: return false;
Chris Lattnerd78700d2002-08-16 21:14:40 +0000161 case ConstantVal: return ConstantValue < V.ConstantValue;
Chris Lattner8c9c5862001-10-13 06:37:47 +0000162 default: assert(0 && "Unknown value type!"); return false;
163 }
164 }
Chris Lattner00950542001-06-06 20:29:01 +0000165};
166
Brian Gaeked0fde302003-11-11 22:41:34 +0000167} // End llvm namespace
168
Chris Lattner00950542001-06-06 20:29:01 +0000169#endif