-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
391 lines (353 loc) · 10.4 KB
/
tree.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
#include "tree.h"
#include "y.tab.h"
#define max(a, b) (((a) > (b)) ? (a) : (b))
FILE *fptr;
void aux_print_tree( tree_t *, int );
void gencode( tree_t *t, int *registers, int start, int r);
void gencode_statements( tree_t *t, int *registers, int start, int r);
void gencode_expressions( tree_t *t, int *registers, int start, int r);
void swap_top(int *arr);
/* basic constructor */
tree_t *make_tree( int type, tree_t *l, tree_t *r )
{
tree_t *p = (tree_t *)malloc( sizeof(tree_t) );
assert( p != NULL );
p->type = type;
p->left = l;
p->right = r;
return p;
}
tree_t *make_id( list_t *name ){
tree_t *p = make_tree( ID, NULL, NULL );
p->attribute.sval = name;
return p;
}
tree_t *make_inum( int val ){
tree_t *p = make_tree( INUM, NULL, NULL );
p->attribute.ival = val;
return p;
}
tree_t *make_rnum( float val ){
tree_t *p = make_tree( RNUM, NULL, NULL );
p->attribute.rval = val;
return p;
}
/* pre-order tree traversal */
void print_tree( tree_t *t )
{
aux_print_tree( t, 0 ); //for spacing purposes
}
void aux_print_tree( tree_t *t, int spaces )
{
if ( t == NULL ) return;
/* indenting for visual effects */
for (int i=0; i<spaces; i++)
fprintf( stderr, " " );
switch( t->type ) {
case ASSIGNOP:
//fprintf(stderr, "hit\n");
fprintf( stderr, "[ASSIGNOP:%d]\n", t->attribute.sval );
aux_print_tree( t->left, spaces+4 ); //should be variable that is getting assigned
aux_print_tree( t->right, spaces+4 ); //what variable is being assigned to
break;
case RELOP: /*this is the first thing to change*/
fprintf( stderr, "[RELOP:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case ADDOP: /*this is the first thing to change*/
fprintf( stderr, "[ADDOP:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case MULOP:
fprintf( stderr, "[MULOP:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case ID: /*need RNUM*/
fprintf( stderr, "[ID:%s]\n", t->attribute.sval );
break;
case INUM: /*need RNUM*/
fprintf( stderr, "[INUM:%d]\n", t->attribute.ival );
break;
case RNUM: /*need RNUM*/
fprintf( stderr, "[RNUM:%f]\n", t->attribute.rval );
break;
default:
fprintf( stderr, "Error: print_tree, unknown type\n" );
exit(1);
}
/* case LT: //LE GT GE EQ NE PLUS MINUS OR STAR SLASH DIV MOD AND NOT
fprintf( stderr, "[LT:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case LE:
fprintf( stderr, "[LE:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case GT:
fprintf( stderr, "[GT:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case GE:
fprintf( stderr, "[GE:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case EQ:
fprintf( stderr, "[EQ:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case NE:
fprintf( stderr, "[NE:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case PLUS:
fprintf( stderr, "[PLUS:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case MINUS:
fprintf( stderr, "[MINUS:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case OR:
fprintf( stderr, "[OR:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case STAR:
fprintf( stderr, "[STAR:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case SLASH:
fprintf( stderr, "[SLASH:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case DIV:
fprintf( stderr, "[DIV:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case MOD:
fprintf( stderr, "[MOD:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case AND:
fprintf( stderr, "[AND:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
case NOT:
fprintf( stderr, "[NOT:%d]\n", t->attribute.opval );
aux_print_tree( t->left, spaces+4 );
aux_print_tree( t->right, spaces+4 );
break;
*/
}
/* evaluate the tree */
int eval_tree( tree_t *t )
{
if ( t == NULL ){
return -1;
fprintf(stderr, "Panic in eval_tree\n");
exit(1);
}
switch( t->type ) {
case ADDOP:
switch(t->attribute.opval){
case PLUS:
t->evaluation = eval_tree(t->left) + eval_tree(t->right);
break;
case MINUS:
t->evaluation = eval_tree(t->left) - eval_tree(t->right);
break;
case OR:
t->evaluation = eval_tree(t->left) ^ eval_tree(t->right);
break;
default:
fprintf( stderr, "Error: eval_tree, unknown type\n" );
exit(1);
}
//t->evaluation = eval_tree(t->left) + eval_tree(t->right);
//break;
case MULOP:
switch(t->attribute.opval){
case STAR:
t->evaluation = eval_tree(t->left) * eval_tree(t->right);
break;
case SLASH:
t->evaluation = eval_tree(t->left) / eval_tree(t->right);
break;
case DIV:
t->evaluation = eval_tree(t->left) / eval_tree(t->right);
break;
case MOD:
t->evaluation = eval_tree(t->left) % eval_tree(t->right);
break;
case AND:
t->evaluation = eval_tree(t->left) & eval_tree(t->right);
break;
default:
fprintf( stderr, "Error: eval_tree, unknown type\n" );
exit(1);
}
//t->evaluation = eval_tree(t->left) * eval_tree(t->right);
//break;
case RELOP:
switch(t->attribute.opval){
case LT:
t->evaluation = eval_tree(t->left) < eval_tree(t->right);
break;
case LE:
t->evaluation = eval_tree(t->left) <= eval_tree(t->right);
break;
case GT:
t->evaluation = eval_tree(t->left) > eval_tree(t->right);
break;
case GE:
t->evaluation = eval_tree(t->left) >= eval_tree(t->right);
break;
case EQ:
t->evaluation = eval_tree(t->left) == eval_tree(t->right);
break;
case NE:
t->evaluation = eval_tree(t->left) != eval_tree(t->right);
break;
default:
fprintf( stderr, "Error: eval_tree, unknown type\n" );
exit(1);
}
//t->evaluation = eval_tree(t->left) + eval_tree(t->right);
//break;
case INUM:
t->evaluation = t->attribute.ival; //how to get the value out of NUM?
break;
case RNUM:
t->evaluation = t->attribute.rval; //how to get the value out of NUM?
break;
default:
fprintf( stderr, "Error: eval_tree, unknown type\n" );
exit(1);
}
return(t->evaluation);
}
/*rank of tree*/
int rank( tree_t *t, int d)
{
int left, right;
if( (t->left == NULL) && (t->right == NULL) )
t->rank = d;
else{
left = rank( t->left, 1);
right = rank( t->right, 0);
if(left != right)
t->rank = max(left, right);
else
t->rank = 1 + left;
}
return t->rank;
}
void gencode_start( tree_t *t ){
fptr = fopen("assembly.s","w"); //test 1
if(fptr == NULL){
printf("Error with file");
exit(1);
}
/* t->rank = rank(t, 1);
int* stack_registers = (int*) malloc(t->rank * sizeof(int));
//int stack_registers[t->rank];
int i;
for( i = 0; i < t->rank; i++)
stack_registers[i] = i;
int start = 0;
gencode( t, stack_registers, start, t->rank); */
}
//generation of assembley code
void gencode( tree_t *t, int *registers, int start, int r){
//separate out what is a statement and what is an expression
//if expression
// gencode_expressions( tree_t *t, int *registers, int start, int r );
//else
// gencode_statements( tree_t *t, int *registers, int start, int r );
}
//generation of code for statements
void gencode_statements( tree_t *t, int *registers, int start, int r){
}
//generation of code for expressions
void gencode_expressions( tree_t *t, int *registers, int start, int r){
int popped = 0;
int right_rank = 0;
int left_rank = 0;
/* case 0 */
if( (t->left == NULL) && (t->right == NULL) && (t->rank == 1) ){
fprintf(fptr, "MOV %d, R%d\n", t->attribute, registers[start]);
}
else if( (t->left != NULL) || (t->right != NULL)){
right_rank = rank(t->right, 0);
left_rank = rank(t->left, 1);
/* case 1 */
if ( right_rank == 0 ){
gencode(t->left, registers, start, r);
if(t->type == ADDOP)
//can we do a switch case statement based on what type of ADDOP value?
fprintf(fptr, "ADD %d, R%d\n", t->right->attribute, registers[start]);
else
fprintf(fptr, "MUL %d, R%d\n", t->right->attribute, registers[start]);
//continue?
}
/* case 2 */
else if (( 1 <= left_rank )&&( left_rank < right_rank )
&& ( left_rank < r )) { //r is the number of registers
swap_top(registers);
gencode(t->right, registers, start, r);
popped = registers[start];
start++;
gencode(t->left, registers, start, r);
//make these all into a function
if(t->type == '+')
fprintf(fptr, "ADD R%d, R%d\n", popped, registers[start]); //can also make pop registers[start-1]
else
fprintf(fptr, "MUL R%d, R%d\n", popped, registers[start]);
start--;
swap_top(registers);
}
/* case 3 */
else if (( 1 <= right_rank ) && ( right_rank <= left_rank )
&& (right_rank < r)){
gencode(t->left, registers, start, r);
//pop
popped = registers[start];
start++;
gencode(t->right, registers, start, r);
if(t->type == '+')
fprintf(fptr, "ADD R%d, R%d\n", registers[start], popped);
else
fprintf(fptr, "MUL R%d, R%d\n", registers[start], popped);
//push
start--;
}
else
fprintf(stderr, "falling through");
/* not doing case 4 yet? */
}
}
void swap_top(int *arr){
int temp = arr[0];
arr[0] = arr[1];
arr[1] = temp;
}