-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnodeop.c
More file actions
782 lines (718 loc) · 19.2 KB
/
nodeop.c
File metadata and controls
782 lines (718 loc) · 19.2 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#include <assert.h>
#include <stdarg.h>
#include <memory.h>
#include <sys/types.h>
#include <string.h>
#include "c2ada.h"
#undef NULL
#define NULL 0
#define streq(a, b) (!(strcmp(a, b)))
extern file_pos_t yypos;
static node_t* free_list;
typedef enum
{
_Binary,
_Unary,
_Pointer,
_Other
} node_class_t;
static bool is_const_int(node_pt n)
{
assert(n != NULL);
switch(n->node_kind)
{
case _Int_Number:
return true;
case _Sym:
assert(n->node.sym);
return n->node.sym->sym_kind == enum_literal;
case _Macro_ID:
{
macro_t* m = n->node.macro;
return m->macro_evald && m->const_value.eval_result_kind == eval_int;
}
default:
return false;
}
}
static bool is_const_fp(node_pt n)
{
assert(n != NULL);
switch(n->node_kind)
{
case _FP_Number:
return true;
case _Macro_ID:
{
macro_t* m = n->node.macro;
return m->macro_evald && m->const_value.eval_result_kind == eval_float;
}
default:
return false;
}
}
static host_int_t const_int_val(node_t* n)
{
assert(is_const_int(n));
switch(n->node_kind)
{
case _Int_Number:
return n->node.ival;
case _Sym:
return n->node.sym->sym_value.intval;
case _Macro_ID:
return n->node.macro->const_value.eval_result.ival;
default:
assert(0);
return 0; /* bogus return to prevent compilation warning */
}
}
#define is_constant(x) (is_const_int(x) || is_const_fp(x))
void free_node(node_t* n)
{
assert(n->node_kind != _Ident);
n->node_kind = (node_kind_t)0;
n->node.unary = free_list;
free_list = n;
}
static node_t* alloc_node(node_kind_t kind)
{
node_t* n;
int i;
n = free_list;
if(n == NULL)
{
n = (node_t*)allocate(sizeof(node_t) * 64);
for(i = 1; i < 63; i++)
{
n[i].node.unary = &n[i + 1];
}
free_list = &n[1];
}
else
{
free_list = n->node.unary;
memset(n, 0, sizeof(node_t));
}
n->node_def = yypos;
n->node_kind = kind;
return n;
}
node_class_t node_classof(node_kind_t kind)
{
if(kind >= _List && kind <= _Shr)
{
return _Binary;
}
if(kind >= _Sizeof && kind <= _Indirect)
{
return _Unary;
}
switch(kind)
{
case _Exp:
case _Concat:
return _Binary;
case _BoolTyp:
case _UnBool:
case _Char_to_Int:
case _Int_to_Char:
return _Unary;
case _Ident:
return _Pointer;
default:
break;
}
return _Other;
}
node_kind_t non_assign_op(node_kind_t op)
/* returns the non-assign operator corresponding to an assign-op */
{
switch(op)
{
case _Mul_Assign:
return _Mul;
case _Div_Assign:
return _Div;
case _Mod_Assign:
return _Rem;
case _Add_Assign:
return _Add;
case _Sub_Assign:
return _Sub;
case _Shl_Assign:
return _Shl;
case _Shr_Assign:
return _Shr;
case _Band_Assign:
return _Band;
case _Xor_Assign:
return _Xor;
case _Bor_Assign:
return _Bor;
default:
return _Error;
}
}
/* operations on node-linked lists */
node_pt reshape_list(node_pt e)
/* turn left-recursive list into right-recursive list:
* (((a,b),c),d) => (a,(b,(c,d)))
* The parser builds left-recursive lists, but right-recursive
* is more convenient in later phases of the translation.
*/
{
node_pt top = e;
while(top->node_kind == _List && top->node.binary.l->node_kind == _List)
{
node_pt tmp = top;
node_pt el, er;
top = top->node.binary.l;
el = top->node.binary.l;
er = top->node.binary.r;
tmp->node.binary.l = er;
top->node.binary.r = tmp;
}
return top;
}
/* Node iteration.
* init_node_iter initializes an iterator.
* next_list_ref returns the head of the list,
* and modifies the iterator so the next call returns the next element.
* next_list_ref returns 0 on end of list.
*/
node_iter_t init_node_iter(node_pt* n)
{
node_iter_t result;
result.head = 0; /* will be filled in later */
result.rest = n;
return result;
}
node_pt* next_list_ref(node_iter_t* iter)
{
if(!iter->rest)
{
return 0;
}
else
{
node_pt rest = *iter->rest;
if(rest->node_kind == _List)
{
iter->head = &rest->node.binary.l;
iter->tail = iter->rest;
iter->rest = &rest->node.binary.r;
return iter->head;
}
else
{
iter->head = iter->rest;
iter->tail = iter->rest;
iter->rest = 0;
return iter->head;
}
}
} /* next_list_ref */
node_pt* node_iter_tail(node_iter_t* iter)
{
return iter->tail;
}
bool is_null_ptr_value(node_pt e)
{
if(e->node_kind == _Int_Number && e->node.ival == 0)
return true;
if(e->node_kind == _Macro_ID && streq(e->node.macro->macro_name, "NULL"))
{
return true;
}
return false;
}
static node_pt new_node_v(node_kind_t kind, va_list args)
{
node_t* n;
n = alloc_node(kind);
switch(node_classof(kind))
{
case _Binary:
n->node.binary.l = va_arg(args, node_t*);
n->node.binary.r = va_arg(args, node_t*);
break;
case _Unary:
n->node.unary = va_arg(args, node_t*);
break;
case _Pointer:
n->node.id.name = va_arg(args, char*);
break;
case _Other:
switch(kind)
{
case _Ellipsis:
break;
case _String:
n->node.str.form = va_arg(args, char*);
n->node.str.len = va_arg(args, int);
break;
case _Sym:
n->node.sym = va_arg(args, symbol_t*);
assert(n->node.sym != 0);
break;
case _Macro_ID:
n->node.macro = va_arg(args, macro_t*);
break;
case _Type:
n->node.typ = va_arg(args, typeinfo_t*);
break;
case _Cond:
n->node.cond.boolval = va_arg(args, node_t*);
n->node.cond.tru = va_arg(args, node_t*);
n->node.cond.fals = va_arg(args, node_t*);
break;
case _FP_Number:
n->node.fval = va_arg(args, host_float_t);
break;
case _Int_Number:
n->node.ival = va_arg(args, host_int_t);
n->baseval = va_arg(args, host_int_t);
break;
default:
fatal(__FILE__, __LINE__, "Unandled noded - (%d)", kind);
break;
}
break;
default:
assert(0);
break;
}
return n;
}
node_pt new_node(node_kind_t kind, ...)
{
va_list args;
node_pt result;
va_start(args, kind);
result = new_node_v(kind, args);
va_end(args);
return result;
}
node_pt new_pos_node(file_pos_t pos, node_kind_t kind, ...)
{
va_list args;
node_pt result;
va_start(args, kind);
result = new_node_v(kind, args);
va_end(args);
result->node_def = pos;
return result;
}
static void promote(node_t* n)
{
node_t *l, *r;
host_float_t fv;
assert(n != NULL);
l = n->node.binary.l;
r = n->node.binary.r;
assert(l != NULL);
assert(r != NULL);
if(is_const_int(l) && is_const_fp(r))
{
fv = (host_float_t)const_int_val(l);
l->node_kind = _FP_Number;
l->node.fval = fv;
}
else if(is_const_fp(l) && is_const_int(r))
{
fv = (host_float_t)const_int_val(r);
r->node_kind = _FP_Number;
r->node.fval = fv;
}
}
static void reduce_binary(node_t*);
/* Force constants to RHS */
static void distributive(node_t* n)
{
node_t *l, *r, *dr;
assert(n != NULL);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_constant(l) && !is_constant(r))
{
n->node.binary.l = r;
n->node.binary.r = l;
}
if(is_constant(r) && l->node_kind == n->node_kind)
{
dr = l->node.binary.r;
if(is_constant(dr))
{
/* Transformation
*
* (n) (n)
* / \ / \
* (l) (C) (?) (l)
* / \ / \
* (?) (C) (C) (C)
*/
n->node.binary.l = l->node.binary.l;
n->node.binary.r = l;
l->node.binary.l = dr;
l->node.binary.r = r;
reduce_binary(l);
}
}
}
static void reduce_binary(node_t* n)
{
node_t *l, *r;
if(n == NULL)
return;
switch(n->node_kind)
{
case _Add:
distributive(n);
promote(n);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_constant(l))
{
assert(is_constant(r)); /* because distributive() */
if(is_const_int(l))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) + const_int_val(r);
}
else
{
n->node_kind = _FP_Number;
n->node.fval = l->node.fval + r->node.fval;
}
free_node(l);
free_node(r);
}
break;
case _Sub:
promote(n);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_constant(l) && is_constant(r))
{
if(is_const_int(l))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) - const_int_val(r);
}
else
{
n->node_kind = _FP_Number;
n->node.fval = l->node.fval - r->node.fval;
}
free_node(l);
free_node(r);
}
break;
case _Mul:
distributive(n);
promote(n);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_constant(l))
{
assert(is_constant(r)); /* because distributive() */
if(is_const_int(l))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) * const_int_val(r);
}
else
{
n->node_kind = _FP_Number;
n->node.fval = l->node.fval * r->node.fval;
}
free_node(l);
free_node(r);
}
break;
case _Div:
promote(n);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_constant(l) && is_constant(r))
{
if(is_const_int(l))
{
if(const_int_val(r) != 0)
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) / const_int_val(r);
free_node(l);
free_node(r);
}
}
else
{
if(r->node.fval != 0.0)
{
n->node_kind = _FP_Number;
n->node.fval = l->node.fval / r->node.fval;
free_node(l);
free_node(r);
}
}
}
break;
case _Rem:
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_const_int(l) && is_const_int(r))
{
if(const_int_val(r) != 0)
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) % const_int_val(r);
free_node(l);
free_node(r);
}
}
break;
case _Shl:
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_const_int(l) && is_const_int(r))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) << const_int_val(r);
free_node(l);
free_node(r);
}
break;
case _Shr:
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_const_int(l) && is_const_int(r))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) >> const_int_val(r);
free_node(l);
free_node(r);
}
break;
case _Bor:
distributive(n);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_const_int(l) && is_const_int(r))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) | const_int_val(r);
free_node(l);
free_node(r);
}
break;
case _Band:
distributive(n);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_const_int(l) && is_const_int(r))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) & const_int_val(r);
free_node(l);
free_node(r);
}
break;
case _Xor:
distributive(n);
l = n->node.binary.l;
assert(l != NULL);
r = n->node.binary.r;
assert(r != NULL);
if(is_const_int(l) && is_const_int(r))
{
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l) ^ const_int_val(r);
free_node(l);
free_node(r);
}
break;
default:
break;
}
}
static void reduce_unary(node_t* n)
{
node_t* l;
typeinfo_t* typ;
symbol_t* sym;
host_int_t iv;
assert(n != NULL);
l = n->node.unary;
if(l == NULL)
return;
switch(n->node_kind)
{
case _Sizeof:
switch(l->node_kind)
{
case _Type:
typ = l->node.typ;
break;
case _Sym:
sym = l->node.sym;
if(sym == NULL)
return;
typ = sym->sym_type;
break;
default:
return;
}
if(typ == NULL)
return;
iv = type_sizeof(typ);
free_node(l);
n->node_kind = _Int_Number;
n->node.ival = iv;
break;
case _Unary_Plus:
switch(l->node_kind)
{
case _Int_Number:
n->node_kind = _Int_Number;
n->node.ival = const_int_val(l);
free_node(l);
break;
case _FP_Number:
n->node_kind = _FP_Number;
n->node.fval = l->node.fval;
free_node(l);
break;
default:
break;
}
break;
case _Unary_Minus:
switch(l->node_kind)
{
case _Int_Number:
n->node_kind = _Int_Number;
n->node.ival = -const_int_val(l);
free_node(l);
break;
case _FP_Number:
n->node_kind = _FP_Number;
n->node.fval = -l->node.fval;
free_node(l);
break;
default:
break;
}
break;
case _Ones_Complement:
if(l->node_kind == _Int_Number)
{
n->node_kind = _Int_Number;
n->node.ival = ~const_int_val(l);
free_node(l);
}
break;
case _Not:
if(l->node_kind == _Int_Number)
{
n->node_kind = _Int_Number;
n->node.ival = !const_int_val(l);
free_node(l);
}
break;
case _Pre_Inc:
case _Pre_Dec:
case _Post_Inc:
case _Post_Dec:
case _Addrof:
case _Aggregate:
case _Indirect:
break;
default:
break;
}
}
void reduce_node(node_pt n)
/* reduces a node as much as possible to a constant expression */
{
if(n == NULL)
return;
switch(node_classof(n->node_kind))
{
case _Binary:
reduce_node(n->node.binary.l);
reduce_node(n->node.binary.r);
reduce_binary(n);
break;
case _Unary:
reduce_node(n->node.unary);
reduce_unary(n);
break;
default:
if(n->node_kind != _Int_Number && is_const_int(n))
{
host_int_t val = const_int_val(n);
n->node_kind = _Int_Number;
n->node.ival = val;
}
return;
}
}
node_t *access_to(node_t *ptr, node_t* decl)
{
node_t* n;
assert(ptr != NULL);
assert(decl != NULL);
assert(ptr->node_kind == _Indirect);
for(n = ptr; n;)
{
switch(n->node_kind)
{
case _Indirect:
if(n->node.unary == NULL)
{
n->node.unary = decl;
return ptr;
}
n = n->node.unary;
break;
default:
fatal(__FILE__, __LINE__, "Unhandled node (%d)", n->node_kind);
break;
}
}
assert(0);
return ptr;
}
node_t* id_from_typedef(typeinfo_t* typ)
{
symbol_t* basetype;
assert(typ != NULL);
basetype = typ->type_base;
assert(basetype != NULL);
assert(basetype->sym_ident != NULL);
assert(basetype->sym_ident->node_kind == _Ident);
assert(basetype->sym_ident->node.id.name != NULL);
return new_node(_Ident, basetype->sym_ident->node.id.name);
}