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