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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* asmitree.c */
/*****************************************************************************/
/* AS-Portierung */
/* */
/* Opcode-Abfrage als Binaerbaum */
/* */
/* Historie: 30.10.1996 Grundsteinlegung */
/* 8.10.1997 Hash-Tabelle */
/* 6.12.1998 dynamisches Kopieren der Namen */
/* */
/*****************************************************************************/
#include "stdinc.h"
#include <string.h>
#include "chunks.h"
#include "strutil.h"
#include "asmdef.h"
#include "asmsub.h"
#include "asmitree.h"
/*---------------------------------------------------------------------------*/
static Boolean AddSingle(PInstTreeNode *Node, char *NName, InstProc NProc, Word NIndex)
BEGIN
PInstTreeNode p1,p2;
Boolean Result=False;
ChkStack();
if (*Node==Nil)
BEGIN
*Node=(PInstTreeNode) malloc(sizeof(TInstTreeNode));
(*Node)->Left=Nil; (*Node)->Right=Nil;
(*Node)->Proc=NProc; (*Node)->Index=NIndex;
(*Node)->Balance=0;
(*Node)->Name=strdup(NName);
Result=True;
END
else if (strcmp(NName,(*Node)->Name)<0)
BEGIN
if (AddSingle(&((*Node)->Left),NName,NProc,NIndex))
switch ((*Node)->Balance)
BEGIN
case 1:
(*Node)->Balance=0;
break;
case 0:
(*Node)->Balance=(-1);
Result=True;
break;
case -1:
p1=(*Node)->Left;
if (p1->Balance==-1)
BEGIN
(*Node)->Left=p1->Right; p1->Right=(*Node);
(*Node)->Balance=0; *Node=p1;
END
else
BEGIN
p2=p1->Right;
p1->Right=p2->Left; p2->Left=p1;
(*Node)->Left=p2->Right; p2->Right=(*Node);
if (p2->Balance==-1) (*Node)->Balance= 1; else (*Node)->Balance=0;
if (p2->Balance== 1) p1 ->Balance=(-1); else p1 ->Balance=0;
*Node=p2;
END
(*Node)->Balance=0;
break;
END
END
else
BEGIN
if (AddSingle(&((*Node)->Right),NName,NProc,NIndex))
switch ((*Node)->Balance)
BEGIN
case -1:
(*Node)->Balance=0;
break;
case 0:
(*Node)->Balance=1;
Result=True;
break;
case 1:
p1=(*Node)->Right;
if (p1->Balance==1)
BEGIN
(*Node)->Right=p1->Left; p1->Left=(*Node);
(*Node)->Balance=0; *Node=p1;
END
else
BEGIN
p2=p1->Left;
p1->Left=p2->Right; p2->Right=p1;
(*Node)->Right=p2->Left; p2->Left=(*Node);
if (p2->Balance== 1) (*Node)->Balance=(-1); else (*Node)->Balance=0;
if (p2->Balance==-1) p1 ->Balance= 1; else p1 ->Balance=0;
*Node=p2;
END
(*Node)->Balance=0;
break;
END
END
return Result;
END
void AddInstTree(PInstTreeNode *Root, char *NName, InstProc NProc, Word NIndex)
BEGIN
AddSingle(Root,NName,NProc,NIndex);
END
static void ClearSingle(PInstTreeNode *Node)
BEGIN
ChkStack();
if (*Node!=Nil)
BEGIN
free((*Node)->Name);
ClearSingle(&((*Node)->Left));
ClearSingle(&((*Node)->Right));
free(*Node); *Node=Nil;
END
END
void ClearInstTree(PInstTreeNode *Root)
BEGIN
ClearSingle(Root);
END
Boolean SearchInstTree(PInstTreeNode Root, char *OpPart)
BEGIN
int z;
z=0;
while ((Root!=Nil) AND (strcmp(Root->Name,OpPart)!=0))
BEGIN
Root=(strcmp(OpPart,Root->Name)<0)? Root->Left : Root->Right;
z++;
END
if (Root==Nil) return False;
else
BEGIN
Root->Proc(Root->Index);
return True;
END
END
static void PNode(PInstTreeNode Node, Word Lev)
BEGIN
ChkStack();
if (Node!=Nil)
BEGIN
PNode(Node->Left,Lev+1);
printf("%*s %s %p %p %d\n",5*Lev,"",Node->Name,Node->Left,Node->Right,Node->Balance);
PNode(Node->Right,Lev+1);
END
END
void PrintInstTree(PInstTreeNode Root)
BEGIN
PNode(Root,0);
END
/*----------------------------------------------------------------------------*/
static int GetKey(char *Name, LongWord TableSize)
BEGIN
register unsigned char *p;
LongWord tmp=0;
for (p=(unsigned char *)Name; *p!='\0'; p++) tmp=(tmp<<2)+((LongWord)*p);
return tmp%TableSize;
END
PInstTable CreateInstTable(int TableSize)
BEGIN
int z;
PInstTableEntry tmp;
PInstTable tab;
tmp=(PInstTableEntry) malloc(sizeof(TInstTableEntry)*TableSize);
for (z=0; z<TableSize; z++) tmp[z].Name=Nil;
tab=(PInstTable) malloc(sizeof(TInstTable));
tab->Fill=0; tab->Size=TableSize; tab->Entries=tmp; tab->Dynamic=FALSE;
return tab;
END
void SetDynamicInstTable(PInstTable Table)
BEGIN
Table->Dynamic=TRUE;
END
void DestroyInstTable(PInstTable tab)
BEGIN
int z;
if (tab->Dynamic)
for (z=0; z<tab->Size; z++)
free(tab->Entries[z].Name);
free(tab->Entries);
free(tab);
END
void AddInstTable(PInstTable tab, char *Name, Word Index, InstProc Proc)
BEGIN
LongWord h0=GetKey(Name,tab->Size),z=0;
/* mindestens ein freies Element lassen, damit der Sucher garantiert terminiert */
if (tab->Size-1<=tab->Fill) exit(255);
while (1)
BEGIN
if (tab->Entries[h0].Name==Nil)
BEGIN
tab->Entries[h0].Name=(tab->Dynamic) ? strdup(Name) : Name;
tab->Entries[h0].Proc=Proc;
tab->Entries[h0].Index=Index;
tab->Entries[h0].Coll=z;
tab->Fill++;
return;
END
z++;
if ((++h0)==tab->Size) h0=0;
END
END
void RemoveInstTable(PInstTable tab, char *Name)
BEGIN
LongWord h0=GetKey(Name,tab->Size);
while (1)
BEGIN
if (tab->Entries[h0].Name==Nil) return;
else if (strcmp(tab->Entries[h0].Name,Name)==0)
BEGIN
tab->Entries[h0].Name=Nil;
tab->Entries[h0].Proc=Nil;
tab->Fill--;
return;
END
if ((++h0)==tab->Size) h0=0;
END
END
Boolean LookupInstTable(PInstTable tab, char *Name)
BEGIN
LongWord h0=GetKey(Name,tab->Size);
while (1)
BEGIN
if (tab->Entries[h0].Name==Nil) return False;
else if (strcmp(tab->Entries[h0].Name,Name)==0)
BEGIN
tab->Entries[h0].Proc(tab->Entries[h0].Index);
return True;
END
if ((++h0)==tab->Size) h0=0;
END
END
void PrintInstTable(FILE *stream, PInstTable tab)
BEGIN
int z;
for (z=0; z<tab->Size; z++)
if (tab->Entries[z].Name!=Nil)
fprintf(stream,"[%3d]: %-10s Index %4d Coll %2d\n",z,tab->Entries[z].Name,tab->Entries[z].Index,tab->Entries[z].Coll);
END
/*----------------------------------------------------------------------------*/
void asmitree_init(void)
BEGIN
END
|