blob: 0a99e4d23816c73c32a99669d25bbdfb31c09dc1 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- ParserInternals.h - Definitions internal to the parser ---*- C++ -*--=//
2//
3// This header file defines the various variables that are shared among the
4// different components of the parser...
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef PARSER_INTERNALS_H
9#define PARSER_INTERNALS_H
10
11#include <stdio.h>
12#define __STDC_LIMIT_MACROS
13
14#include "llvm/InstrTypes.h"
15#include "llvm/BasicBlock.h"
16#include "llvm/ConstPoolVals.h"
17#include "llvm/iOther.h"
18#include "llvm/Method.h"
19#include "llvm/Type.h"
20#include "llvm/Assembly/Parser.h"
Chris Lattner57dbb3a2001-07-23 17:46:59 +000021#include "llvm/Support/StringExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000022
23class Module;
24
25// Global variables exported from the lexer...
26extern FILE *llvmAsmin;
27extern int llvmAsmlineno;
28
29// Globals exported by the parser...
Chris Lattnera2850432001-07-22 18:36:00 +000030extern string CurFilename;
31Module *RunVMAsmParser(const string &Filename, FILE *F);
Chris Lattner00950542001-06-06 20:29:01 +000032
33
Chris Lattner93750fa2001-07-28 17:48:55 +000034// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
35// appropriate character. If AllowNull is set to false, a \00 value will cause
36// an exception to be thrown.
37//
38// If AllowNull is set to true, the return value of the function points to the
39// last character of the string in memory.
40//
41char *UnEscapeLexed(char *Buffer, bool AllowNull = false);
42
43
Chris Lattner00950542001-06-06 20:29:01 +000044// ThrowException - Wrapper around the ParseException class that automatically
45// fills in file line number and column number and options info.
46//
47// This also helps me because I keep typing 'throw new ParseException' instead
48// of just 'throw ParseException'... sigh...
49//
Chris Lattner93750fa2001-07-28 17:48:55 +000050static inline void ThrowException(const string &message,
51 int LineNo = -1) {
52 if (LineNo == -1) LineNo = llvmAsmlineno;
Chris Lattner00950542001-06-06 20:29:01 +000053 // TODO: column number in exception
Chris Lattner93750fa2001-07-28 17:48:55 +000054 throw ParseException(CurFilename, message, LineNo);
Chris Lattner00950542001-06-06 20:29:01 +000055}
56
57// ValID - Represents a reference of a definition of some sort. This may either
58// be a numeric reference or a symbolic (%var) reference. This is just a
59// discriminated union.
60//
61// Note that I can't implement this class in a straight forward manner with
62// constructors and stuff because it goes in a union, and GCC doesn't like
63// putting classes with ctor's in unions. :(
64//
65struct ValID {
66 int Type; // 0 = number, 1 = name, 2 = const pool,
Chris Lattner3d52b2f2001-07-15 00:17:01 +000067 // 3 = unsigned const pool, 4 = const string,
68 // 5 = const fp
Chris Lattner00950542001-06-06 20:29:01 +000069 union {
70 int Num; // If it's a numeric reference
71 char *Name; // If it's a named reference. Memory must be free'd.
72 int64_t ConstPool64; // Constant pool reference. This is the value
73 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000074 double ConstPoolFP; // Floating point constant pool reference
Chris Lattner00950542001-06-06 20:29:01 +000075 };
76
77 static ValID create(int Num) {
78 ValID D; D.Type = 0; D.Num = Num; return D;
79 }
80
81 static ValID create(char *Name) {
82 ValID D; D.Type = 1; D.Name = Name; return D;
83 }
84
85 static ValID create(int64_t Val) {
86 ValID D; D.Type = 2; D.ConstPool64 = Val; return D;
87 }
88
89 static ValID create(uint64_t Val) {
90 ValID D; D.Type = 3; D.UConstPool64 = Val; return D;
91 }
92
93 static ValID create_conststr(char *Name) {
94 ValID D; D.Type = 4; D.Name = Name; return D;
95 }
96
Chris Lattner3d52b2f2001-07-15 00:17:01 +000097 static ValID create(double Val) {
98 ValID D; D.Type = 5; D.ConstPoolFP = Val; return D;
99 }
100
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000101 inline void destroy() const {
Chris Lattner00950542001-06-06 20:29:01 +0000102 if (Type == 1 || Type == 4) free(Name); // Free this strdup'd memory...
103 }
104
105 inline ValID copy() const {
106 if (Type != 1 && Type != 4) return *this;
107 ValID Result = *this;
108 Result.Name = strdup(Name);
109 return Result;
110 }
111
112 inline string getName() const {
113 switch (Type) {
114 case 0: return string("#") + itostr(Num);
115 case 1: return Name;
116 case 4: return string("\"") + Name + string("\"");
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000117 case 5: return ftostr(ConstPoolFP);
Chris Lattner00950542001-06-06 20:29:01 +0000118 default: return string("%") + itostr(ConstPool64);
119 }
120 }
121};
122
123
124
125template<class SuperType>
Chris Lattner93750fa2001-07-28 17:48:55 +0000126class PlaceholderValue : public SuperType {
Chris Lattner00950542001-06-06 20:29:01 +0000127 ValID D;
Chris Lattner93750fa2001-07-28 17:48:55 +0000128 int LineNum;
Chris Lattner00950542001-06-06 20:29:01 +0000129public:
Chris Lattner93750fa2001-07-28 17:48:55 +0000130 PlaceholderValue(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {
131 LineNum = llvmAsmlineno;
132 }
Chris Lattner00950542001-06-06 20:29:01 +0000133 ValID &getDef() { return D; }
Chris Lattner93750fa2001-07-28 17:48:55 +0000134 int getLineNum() const { return LineNum; }
Chris Lattner00950542001-06-06 20:29:01 +0000135};
136
137struct InstPlaceHolderHelper : public Instruction {
138 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
139
140 virtual Instruction *clone() const { abort(); }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000141 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000142};
143
144struct BBPlaceHolderHelper : public BasicBlock {
145 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
146 assert(Ty->isLabelType());
147 }
148};
149
150struct MethPlaceHolderHelper : public Method {
151 MethPlaceHolderHelper(const Type *Ty)
152 : Method((const MethodType*)Ty) {
153 assert(Ty->isMethodType() && "Method placeholders must be method types!");
154 }
155};
156
Chris Lattner93750fa2001-07-28 17:48:55 +0000157typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
158typedef PlaceholderValue<BBPlaceHolderHelper> BBPlaceHolder;
159typedef PlaceholderValue<MethPlaceHolderHelper> MethPlaceHolder;
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattner93750fa2001-07-28 17:48:55 +0000161static inline ValID &getValIDFromPlaceHolder(Value *Val) {
162 switch (Val->getType()->getPrimitiveID()) {
163 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getDef();
164 case Type::MethodTyID: return ((MethPlaceHolder*)Val)->getDef();
165 default: return ((ValuePlaceHolder*)Val)->getDef();
166 }
167}
168
169static inline int getLineNumFromPlaceHolder(Value *Val) {
170 switch (Val->getType()->getPrimitiveID()) {
171 case Type::LabelTyID: return ((BBPlaceHolder*)Val)->getLineNum();
172 case Type::MethodTyID: return ((MethPlaceHolder*)Val)->getLineNum();
173 default: return ((ValuePlaceHolder*)Val)->getLineNum();
Chris Lattner00950542001-06-06 20:29:01 +0000174 }
175}
176
177#endif