-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc.c
397 lines (350 loc) · 8.27 KB
/
rc.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# include "rasl.h"
# include "cdecl.h"
# include "cfunc.h"
/* Refal Compiler function: accepts as input pointer to
tree and returns compiled code. */
/* general translation functions and right side translation
functions. see also file rcleft.c */
int refcom (q)
struct node *q;
{
int rasl_ins, t, number;
union param p1;
struct node *sent;
struct rasl_instruction *translation [MAX_SENTENCES];
if (q -> nt == ENTRY)
{
rasl_ins = E;
/* add this function to the list of entry functions. */
rc_mkentry(q -> a2.pchar);
}
else rasl_ins = L;
p1.f = q -> a2.pchar;
/* initialize the list of RASL instructions */
/* the first sentence contains the label. */
t = 0;
ftransl = (struct rasl_instruction *)
malloc (sizeof (struct rasl_instruction));
/* 33 line. Not check result of malloc. Shura. 29.01.98 */
if (NULL == ftransl) {
fprintf (stderr, "No memory for rasl instrunction\n");
exit (1);
}
/* 34 line. Was ... = NULL. Shura. 29.01.98 */
ftransl -> code = 0;
ftransl -> next = NULL;
translation [t] = ftransl;
rc_out (rasl_ins, p1);
t ++;
q = q -> a3.tree;
while (q != NULL)
{
/* initialize the list of RASL instruction corresponding to
this sentence. */
if (t >= MAX_SENTENCES)
{
fprintf (stderr, "Too many sentences in a function. Aborted.\n");
exit (1);
}
ftransl = (struct rasl_instruction *)
malloc (sizeof (struct rasl_instruction));
/* 51 line.Not check result of malloc.Shura.29.01.98 */
if (NULL == ftransl) {
fprintf (stderr, "No memory for rasl instrunction\n");
exit (1);
}
/* 52 line. Was ... = NULL. Shura. 29.01.98 */
ftransl -> code = 0;
ftransl -> next = NULL;
translation [t] = ftransl;
t ++;
/* insert a dummy instruction. */
p1.n = 0L;
rc_out (B, p1);
/* process sentences one by one. */
sent = q -> a2.tree;
/* check */
if (sent -> nt != ST)
{
fprintf (stderr, "strange node type %d. ST expected\n", sent -> nt);
break;
}
/* clear the local variables table. */
table_len = 0;
/* process the left side. */
number = 1; /* first available number. */
transl_left (sent -> a2.chunk, &number);
/* process the right tail. */
tr_rtail (sent -> a3.tree, &number);
q = q -> a3.tree;
}
/* optimize the code. */
refc_opt (translation, t, &last_label);
rc_post_opt (translation, t);
/* output the code. */
rc_vyvod (translation, t);
refc_out (translation, t);
return 0;
}
int tr_rtail (tail, number)
struct node *tail;
int *number;
{
union param p1;
int label, table_save, num_save;
struct node *sub_tree;
/* 4 cases. */
while (tail != NULL)
{
switch (tail -> nt)
{
/* right side of a sentence. */
case RCS1:
p1.i = 0;
rc_out (RDY, p1);
transl_right (tail -> a2.chunk, 1);
tail = NULL;
break;
/* simple condition. */
case RCS2:
p1.n = 0L;
rc_out (PUSHVF, p1);
label = last_label; /* first available label. */
last_label ++; /* increment the first available label. */
transl_right (tail -> a2.chunk, 0);
p1.i = label;
rc_out (ECOND, p1);
rc_out (LABEL, p1);
rc_out (POPVF, p1);
(*number) ++;
transl_left (tail -> a3.chunk, number);
tail = tail -> a4.tree;
break;
/* branching on a left side of a condition. */
case RCS3:
p1.n = 0L;
rc_out (PUSHVF, p1);
label = last_label; /* first available label. */
last_label ++; /* increment the first available label. */
transl_right (tail -> a2.chunk, 0);
p1.i = label;
rc_out (ECOND, p1);
rc_out (LABEL, p1);
rc_out (POPVF, p1);
rc_out (STLEN, p1);
/* save the current number of variables in the table. */
table_save = table_len;
/* save the current table of element number */
(*number) ++;
num_save = *number;
sub_tree = tail -> a3.tree;
while (sub_tree != NULL)
{
/* check */
if (sub_tree -> nt != LSF)
{
fprintf (stderr, "Illegal Sub tree in RCS3: %d -- aborted\n",
sub_tree -> nt);
exit (1);
}
/* check */
if ((sub_tree -> a2.tree) -> nt != LSF1)
{
fprintf (stderr, "Illegal Sub tree in LSF: %d -- aborted\n",
(sub_tree -> a2.tree) -> nt);
exit (1);
}
if (sub_tree -> a3.tree != NULL)
{
label = last_label;
last_label ++;
p1.i = label;
rc_out (TRAN, p1);
}
transl_left ((sub_tree -> a2.tree) -> a2.chunk, number);
tr_rtail ((sub_tree -> a2.tree) -> a3.tree, number);
if (sub_tree -> a3.tree != NULL)
{
p1.i = label;
rc_out (LABEL, p1);
}
/* restore the table of variables */
table_len = table_save;
*number = num_save;
sub_tree = sub_tree -> a3.tree;
}
tail = tail -> a4.tree;
break;
/* branching on a whole condition. */
case RCS4:
/*fprintf (stderr, "RCS4: Not implemented: *number = %d -- aborted\n", *number);*/
fprintf (stderr, "Error on line %d\n", line_no);
exit (0);
break;
default:
fprintf (stderr, "internal errors in right_tail, node: %d -- aborted\n",
tail -> nt);
exit (1);
break;
}
}
return 0;
}
/* free memory allocated for translation. */
int refc_out (translation, t)
struct rasl_instruction *translation [];
int t;
{
int i;
struct rasl_instruction *q, *z;
for (i = 0; i < t; i ++)
{
q = translation [i];
while (q != NULL)
{
z = q -> next;
free ((void *) q);
q = z;
}
}
return 0;
}
int transl_right (e, transplant)
struct element *e;
int transplant;
{
/*register*/ int i;
int occur;
union param p1;
unsigned char flags [MAX_TABLE_LENGTH / 8]; /* see table. */
/* set all flags to zero. */
for (i = 0; i < MAX_TABLE_LENGTH / 8; i ++) flags [i] = 0;
i = 0;
/* 240 line. Was ... = NULL. Shura. 29.01.98 */
while (e[i].type != 0)
{
switch (e[i].type)
{
case T_VAR:
case E_VAR:
/* first try to transplant */
if (!transplant) occur = -1;
else if ((occur = get_var_index (e[i].body.i, flags)) != -1)
{
check_bit (flags, occur);
p1.i = table [occur].te_offset;
rc_out (TPLE, p1);
}
/* otherwise copy it. */
if (occur == -1)
{
occur = check_var (e[i].body.i);
p1.i = occur;
rc_out (MULE, p1);
}
break;
case S_VAR:
/* first try to transplant */
if (!transplant) occur = -1;
else if ((occur = get_var_index (e[i].body.i, flags)) != -1)
{
check_bit (flags, occur);
p1.i = table [occur].te_offset;
rc_out (TPLS, p1);
}
/* otherwise copy it. */
if (occur == -1)
{
occur = check_var (e[i].body.i);
p1.i = occur;
rc_out (MULS, p1);
}
break;
case CHAR:
p1.c = e[i].body.c;
rc_out (NS, p1);
break;
case ATOM:
p1.f = e[i].body.f;
rc_out (NCS, p1);
break;
case DIGIT:
p1.n = e[i].body.n;
rc_out (NNS, p1);
break;
case STRING:
occur = strlen (e[i].body.f);
if (occur > 1)
{
p1.f = e[i].body.f;
rc_out (TEXT, p1);
}
else
{
p1.c = *(e[i].body.f);
rc_out (NS, p1);
}
break;
case LPAR:
case ACT_LEFT:
p1.n = 0L;
rc_out (BL, p1);
break;
case RPAR:
case ACT_RIGHT:
p1.n = 0L;
rc_out (BR, p1);
if (e[i].type == RPAR) break;
p1.f = e [i].body.f;
rc_out (ACT1, p1);
break;
default:
fprintf (stderr, "internal errors in transl_right, Type: e[%d].type = %d\n",
i, e[i].type);
exit (1);
break;
}
i ++;
}
/* if called with transplant parameter issue OUTEST operator. */
if (transplant)
{
rc_out (OUTEST, p1);
}
return 0;
}
int get_var_index (index, bits)
int index;
unsigned char *bits;
{
/*register*/ int i;
for (i = 0; i < table_len; i ++)
{
if (table [i].index == index && !(is_bit_checked (bits, i))) return i;
}
/* not found. */
return -1;
}
int is_bit_checked (bitmap, index)
unsigned char *bitmap;
int index;
{
int bytes, bits;
unsigned char mask;
bytes = index / 8;
bits = index % 8;
mask = 1 << bits;
return (bitmap [bytes] & mask);
}
int check_bit (bitmap, index)
unsigned char * bitmap;
int index;
{
int bytes, bits;
unsigned char mask;
bytes = index / 8;
bits = index % 8;
mask = 1 << bits;
bitmap [bytes] = bitmap [bytes] | mask;
return 0;
}