blob: 1f6bafce1500fd9d4416816447eeddb11d26bf01 [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
34// ThrowException - Wrapper around the ParseException class that automatically
35// fills in file line number and column number and options info.
36//
37// This also helps me because I keep typing 'throw new ParseException' instead
38// of just 'throw ParseException'... sigh...
39//
40static inline void ThrowException(const string &message) {
41 // TODO: column number in exception
Chris Lattnera2850432001-07-22 18:36:00 +000042 throw ParseException(CurFilename, message, llvmAsmlineno);
Chris Lattner00950542001-06-06 20:29:01 +000043}
44
45// ValID - Represents a reference of a definition of some sort. This may either
46// be a numeric reference or a symbolic (%var) reference. This is just a
47// discriminated union.
48//
49// Note that I can't implement this class in a straight forward manner with
50// constructors and stuff because it goes in a union, and GCC doesn't like
51// putting classes with ctor's in unions. :(
52//
53struct ValID {
54 int Type; // 0 = number, 1 = name, 2 = const pool,
Chris Lattner3d52b2f2001-07-15 00:17:01 +000055 // 3 = unsigned const pool, 4 = const string,
56 // 5 = const fp
Chris Lattner00950542001-06-06 20:29:01 +000057 union {
58 int Num; // If it's a numeric reference
59 char *Name; // If it's a named reference. Memory must be free'd.
60 int64_t ConstPool64; // Constant pool reference. This is the value
61 uint64_t UConstPool64;// Unsigned constant pool reference.
Chris Lattner3d52b2f2001-07-15 00:17:01 +000062 double ConstPoolFP; // Floating point constant pool reference
Chris Lattner00950542001-06-06 20:29:01 +000063 };
64
65 static ValID create(int Num) {
66 ValID D; D.Type = 0; D.Num = Num; return D;
67 }
68
69 static ValID create(char *Name) {
70 ValID D; D.Type = 1; D.Name = Name; return D;
71 }
72
73 static ValID create(int64_t Val) {
74 ValID D; D.Type = 2; D.ConstPool64 = Val; return D;
75 }
76
77 static ValID create(uint64_t Val) {
78 ValID D; D.Type = 3; D.UConstPool64 = Val; return D;
79 }
80
81 static ValID create_conststr(char *Name) {
82 ValID D; D.Type = 4; D.Name = Name; return D;
83 }
84
Chris Lattner3d52b2f2001-07-15 00:17:01 +000085 static ValID create(double Val) {
86 ValID D; D.Type = 5; D.ConstPoolFP = Val; return D;
87 }
88
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +000089 inline void destroy() const {
Chris Lattner00950542001-06-06 20:29:01 +000090 if (Type == 1 || Type == 4) free(Name); // Free this strdup'd memory...
91 }
92
93 inline ValID copy() const {
94 if (Type != 1 && Type != 4) return *this;
95 ValID Result = *this;
96 Result.Name = strdup(Name);
97 return Result;
98 }
99
100 inline string getName() const {
101 switch (Type) {
102 case 0: return string("#") + itostr(Num);
103 case 1: return Name;
104 case 4: return string("\"") + Name + string("\"");
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000105 case 5: return ftostr(ConstPoolFP);
Chris Lattner00950542001-06-06 20:29:01 +0000106 default: return string("%") + itostr(ConstPool64);
107 }
108 }
109};
110
111
112
113template<class SuperType>
114class PlaceholderDef : public SuperType {
115 ValID D;
116 // TODO: Placeholder def should hold Line #/Column # of definition in case
117 // there is an error resolving the defintition!
118public:
119 PlaceholderDef(const Type *Ty, const ValID &d) : SuperType(Ty), D(d) {}
120 ValID &getDef() { return D; }
121};
122
123struct InstPlaceHolderHelper : public Instruction {
124 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
125
126 virtual Instruction *clone() const { abort(); }
Chris Lattnera41f50d2001-07-07 19:24:15 +0000127 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +0000128};
129
130struct BBPlaceHolderHelper : public BasicBlock {
131 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
132 assert(Ty->isLabelType());
133 }
134};
135
136struct MethPlaceHolderHelper : public Method {
137 MethPlaceHolderHelper(const Type *Ty)
138 : Method((const MethodType*)Ty) {
139 assert(Ty->isMethodType() && "Method placeholders must be method types!");
140 }
141};
142
143typedef PlaceholderDef<InstPlaceHolderHelper> DefPlaceHolder;
144typedef PlaceholderDef<BBPlaceHolderHelper> BBPlaceHolder;
145typedef PlaceholderDef<MethPlaceHolderHelper> MethPlaceHolder;
146//typedef PlaceholderDef<ModulePlaceHolderHelper> ModulePlaceHolder;
147
148static inline ValID &getValIDFromPlaceHolder(Value *Def) {
149 switch (Def->getType()->getPrimitiveID()) {
150 case Type::LabelTyID: return ((BBPlaceHolder*)Def)->getDef();
151 case Type::MethodTyID: return ((MethPlaceHolder*)Def)->getDef();
152//case Type::ModuleTyID: return ((ModulePlaceHolder*)Def)->getDef();
153 default: return ((DefPlaceHolder*)Def)->getDef();
154 }
155}
156
157#endif