-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol.h
More file actions
224 lines (185 loc) · 11.7 KB
/
symbol.h
File metadata and controls
224 lines (185 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/******************************************************************************
* CVS version:
* $Id: symbol.h,v 1.1 2003/05/13 22:21:01 nickie Exp $
******************************************************************************
*
* C header file : symbol.h
* Project : PCL Compiler
* Version : 1.0 alpha
* Written by : Nikolaos S. Papaspyrou (nickie@softlab.ntua.gr)
* Date : May 14, 2003
* Description : Generic symbol table in C
*
* Comments: (in Greek iso-8859-7)
* ---------
* ������ �������� �����������.
* ����� ������������ ��������� ��� ��������� �����������.
* ������ ����������� ������������ ��� �����������.
* ���������� ����������� ����������
*/
#ifndef __SYMBOL_H__
#define __SYMBOL_H__
/* ---------------------------------------------------------------------
-------------------------- ����� bool -------------------------------
--------------------------------------------------------------------- */
#include <stdbool.h>
/*
* �� �� �������� include ��� ������������� ��� ��� ���������
* ��� C ��� ��������������, �������������� �� �� �� ��������:
*/
#if 0
typedef enum { false=0, true=1 } bool;
#endif
/* ---------------------------------------------------------------------
------------ ������� �������� ��� ������ �������� -------------------
--------------------------------------------------------------------- */
#define START_POSITIVE_OFFSET 8 /* ������ ������ offset ��� �.�. */
#define START_NEGATIVE_OFFSET 0 /* ������ �������� offset ��� �.�. */
/* ---------------------------------------------------------------------
--------------- ������� ����� ��� ������ �������� -------------------
--------------------------------------------------------------------- */
/* ����� ��������� ��� ��� ��������� ��� �������� */
typedef int RepInteger; /* �������� */
typedef unsigned char RepBoolean; /* ������� ����� */
typedef char RepChar; /* ���������� */
typedef long double RepReal; /* ����������� */
typedef const char *RepString; /* ������������� */
/* ����� ��������� ��� ������������� ����������� */
// enum { /***** �� ����� ��� ����� ****/
// TYPE_VOID, /* ����� ����� ������������� */
// TYPE_INTEGER, /* �������� */
// TYPE_BOOLEAN, /* ������� ����� */
// TYPE_CHAR, /* ���������� */
// TYPE_REAL, /* ����������� */
// TYPE_ARRAY, /* ������� ������� �������� */
// TYPE_IARRAY, /* ������� �������� �������� */
// TYPE_POINTER /* ������� */
// } kind;
typedef struct Type_tag *Type;
typedef Type myType;
struct Type_tag {
enum { /***** �� ����� ��� ����� ****/
TYPE_VOID, /* ����� ����� ������������� */
TYPE_INTEGER, /* �������� */
TYPE_BOOLEAN, /* ������� ����� */
TYPE_CHAR, /* ���������� */
TYPE_REAL, /* ����������� */
TYPE_ARRAY, /* ������� ������� �������� */
TYPE_IARRAY, /* ������� �������� �������� */
TYPE_POINTER /* ������� */
} kind;
Type refType; /* ����� �������� */
RepInteger size; /* �������, �� ����� ������� */
unsigned int refCount; /* �������� �������� */
};
/* ����� �������� ��� ������ �������� */
typedef enum {
ENTRY_VARIABLE, /* ���������� */
ENTRY_CONSTANT, /* �������� */
ENTRY_FUNCTION, /* ����������� */
ENTRY_PARAMETER, /* ���������� ����������� */
ENTRY_TEMPORARY /* ���������� ���������� */
} EntryType;
/* ����� ���������� ���������� */
typedef enum {
PASS_BY_VALUE, /* ���' ���� */
PASS_BY_REFERENCE /* ���' ������� */
} PassMode;
/* ����� �������� ���� ������ �������� */
typedef struct SymbolEntry_tag SymbolEntry;
struct SymbolEntry_tag {
const char *id; /* ����� �������������� */
EntryType entryType; /* ����� ��� �������� */
unsigned int nestingLevel; /* ����� ����������� */
unsigned int hashValue; /* ���� ��������������� */
SymbolEntry *nextHash; /* ������� ������� ���� �.�. */
SymbolEntry *nextInScope; /* ������� ������� ���� �������� */
union { /* ������� �� ��� ���� ��������: */
struct { /******* ��������� *******/
Type type; /* ����� */
int offset; /* Offset ��� �.�. */
} eVariable;
struct { /******** ������� ********/
Type type; /* ����� */
union { /* ���� */
RepInteger vInteger; /* ������� */
RepBoolean vBoolean; /* ������ */
RepChar vChar; /* ���������� */
RepReal vReal; /* ���������� */
RepString vString; /* ������������ */
} value;
} eConstant;
struct { /******* ��������� *******/
bool isForward; /* ������ forward */
SymbolEntry *firstArgument; /* ����� ���������� */
SymbolEntry *lastArgument; /* ��������� ���������� */
Type resultType; /* ����� ������������� */
enum { /* ��������� ���������� */
PARDEF_COMPLETE, /* ������ ������� */
PARDEF_DEFINE, /* �� ���� ������� */
PARDEF_CHECK /* �� ���� ������� */
} pardef;
int firstQuad; /* ������ ������� */
} eFunction;
struct { /****** ���������� *******/
Type type; /* ����� */
int offset; /* Offset ��� �.�. */
PassMode mode; /* ������ ���������� */
SymbolEntry *next; /* ������� ���������� */
} eParameter;
struct { /** ��������� ��������� **/
Type type; /* ����� */
int offset; /* Offset ��� �.�. */
int number;
} eTemporary;
} u; /* ����� ��� union */
};
/* ����� ������� �������� ��� ���������� ���� ���� �������� */
typedef struct Scope_tag Scope;
struct Scope_tag {
unsigned int nestingLevel; /* ����� ����������� */
unsigned int negOffset; /* ������ �������� offset */
Scope *parent; /* ������������ �������� */
SymbolEntry *entries; /* ������� ��� ��������� */
};
/* ����� ���������� ���� ������ �������� */
typedef enum { LOOKUP_CURRENT_SCOPE, LOOKUP_ALL_SCOPES } LookupType;
/* ---------------------------------------------------------------------
------------- ��������� ���������� ��� ������ �������� --------------
--------------------------------------------------------------------- */
extern Scope *currentScope; /* �������� �������� */
extern unsigned int quadNext; /* ������� �������� �������� */
extern unsigned int tempNumber; /* �������� ��� temporaries */
extern const Type typeVoid;
extern const Type typeInteger;
extern const Type typeBoolean;
extern const Type typeChar;
extern const Type typeReal;
/* ---------------------------------------------------------------------
------ ��������� ��� ����������� ��������� ��� ������ �������� ------
--------------------------------------------------------------------- */
void initSymbolTable(unsigned int size);
void destroySymbolTable(void);
void openScope(void);
void closeScope(void);
SymbolEntry *newVariable(const char *name, Type type);
SymbolEntry *newConstant(const char *name, Type type, ...);
SymbolEntry *newFunction(const char *name);
SymbolEntry *newParameter(const char *name, Type type, PassMode mode,
SymbolEntry *f);
SymbolEntry *newTemporary(Type type);
void forwardFunction(SymbolEntry *f);
void endFunctionHeader(SymbolEntry *f, Type type);
void destroyEntry(SymbolEntry *e);
SymbolEntry *lookupEntry(const char *name, LookupType type, bool err);
Type typeArray(RepInteger size, Type refType);
Type typeIArray(Type refType);
Type typePointer(Type refType);
void destroyType(Type type);
unsigned int sizeOfType(Type type);
bool equalType(Type type1, Type type2);
void printType(Type type);
void printMode(PassMode mode);
bool isIArrayType(Type type1);
bool isArrayType(Type type1);
#endif