1 //===-- ParserInternals.h - Definitions internal to the parser --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header file defines the various variables that are shared among the
11 // different components of the parser...
13 //===----------------------------------------------------------------------===//
15 #ifndef PARSER_INTERNALS_H
16 #define PARSER_INTERNALS_H
18 #include "llvm/Constants.h"
19 #include "llvm/iOther.h"
20 #include "llvm/Function.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Assembly/Parser.h"
23 #include "Support/StringExtras.h"
25 // Global variables exported from the lexer...
26 extern std::FILE *llvmAsmin;
27 extern int llvmAsmlineno;
29 // Globals exported by the parser...
30 extern char* llvmAsmtext;
31 extern int llvmAsmleng;
35 // Globals exported by the parser...
36 extern std::string CurFilename;
39 Module *RunVMAsmParser(const std::string &Filename, FILE *F);
42 // 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.
46 // If AllowNull is set to true, the return value of the function points to the
47 // last character of the string in memory.
49 char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
52 // ThrowException - Wrapper around the ParseException class that automatically
53 // fills in file line number and column number and options info.
55 // This also helps me because I keep typing 'throw new ParseException' instead
56 // of just 'throw ParseException'... sigh...
58 static inline void ThrowException(const std::string &message,
60 if (LineNo == -1) LineNo = llvmAsmlineno;
61 // TODO: column number in exception
62 throw ParseException(CurFilename, message, LineNo);
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.
69 // Note that I can't implement this class in a straight forward manner with
70 // constructors and stuff because it goes in a union.
74 NumberVal, NameVal, ConstSIntVal, ConstUIntVal, ConstFPVal, ConstNullVal,
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.
83 double ConstPoolFP; // Floating point constant pool reference
84 Constant *ConstantValue; // Fully resolved constant for ConstantVal case.
87 static ValID create(int Num) {
88 ValID D; D.Type = NumberVal; D.Num = Num; return D;
91 static ValID create(char *Name) {
92 ValID D; D.Type = NameVal; D.Name = Name; return D;
95 static ValID create(int64_t Val) {
96 ValID D; D.Type = ConstSIntVal; D.ConstPool64 = Val; return D;
99 static ValID create(uint64_t Val) {
100 ValID D; D.Type = ConstUIntVal; D.UConstPool64 = Val; return D;
103 static ValID create(double Val) {
104 ValID D; D.Type = ConstFPVal; D.ConstPoolFP = Val; return D;
107 static ValID createNull() {
108 ValID D; D.Type = ConstNullVal; return D;
111 static ValID create(Constant *Val) {
112 ValID D; D.Type = ConstantVal; D.ConstantValue = Val; return D;
115 inline void destroy() const {
117 free(Name); // Free this strdup'd memory...
120 inline ValID copy() const {
121 if (Type != NameVal) return *this;
122 ValID Result = *this;
123 Result.Name = strdup(Name);
127 inline std::string getName() const {
129 case NumberVal : return std::string("#") + itostr(Num);
130 case NameVal : return Name;
131 case ConstFPVal : return ftostr(ConstPoolFP);
132 case ConstNullVal : return "null";
134 case ConstSIntVal : return std::string("%") + itostr(ConstPool64);
136 if (ConstantValue == ConstantBool::True) return "true";
137 if (ConstantValue == ConstantBool::False) return "false";
138 return "<constant expression>";
140 assert(0 && "Unknown value!");
146 bool operator<(const ValID &V) const {
147 if (Type != V.Type) return Type < V.Type;
149 case NumberVal: return Num < V.Num;
150 case NameVal: return strcmp(Name, V.Name) < 0;
151 case ConstSIntVal: return ConstPool64 < V.ConstPool64;
152 case ConstUIntVal: return UConstPool64 < V.UConstPool64;
153 case ConstFPVal: return ConstPoolFP < V.ConstPoolFP;
154 case ConstNullVal: return false;
155 case ConstantVal: return ConstantValue < V.ConstantValue;
156 default: assert(0 && "Unknown value type!"); return false;
163 template<class SuperType>
164 class PlaceholderValue : public SuperType {
168 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
169 LineNum = llvmAsmlineno;
171 ValID &getDef() { return D; }
172 int getLineNum() const { return LineNum; }
175 struct InstPlaceHolderHelper : public Instruction {
176 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
178 virtual Instruction *clone() const { abort(); return 0; }
179 virtual const char *getOpcodeName() const { return "placeholder"; }
182 struct BBPlaceHolderHelper : public BasicBlock {
183 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
184 assert(Ty == Type::LabelTy);
188 typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
189 typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
191 static inline ValID &getValIDFromPlaceHolder(const Value *Val) {
192 const Type *Ty = Val->getType();
193 if (isa<PointerType>(Ty) &&
194 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
195 Ty = cast<PointerType>(Ty)->getElementType();
197 switch (Ty->getTypeID()) {
198 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
199 default: return ((ValuePlaceHolder*)Val)->getDef();
203 static inline int getLineNumFromPlaceHolder(const Value *Val) {
204 const Type *Ty = Val->getType();
205 if (isa<PointerType>(Ty) &&
206 isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
207 Ty = cast<PointerType>(Ty)->getElementType();
209 switch (Ty->getTypeID()) {
210 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
211 default: return ((ValuePlaceHolder*)Val)->getLineNum();
215 } // End llvm namespace