-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathncc.c
2147 lines (2015 loc) · 42.8 KB
/
ncc.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
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* THE NEATCC C COMPILER
*
* Copyright (C) 2010-2016 Ali Gholami Rudi
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* neatcc parser
*
* The parser reads tokens from the tokenizer (tok_*) and calls the
* appropriate code generation functions (o_*). The generator
* maintains a stack of values pushed via, for instance, o_num()
* and generates the necessary code for the accesses to the items
* in this stack, like o_bop() for performing a binary operations
* on the top two items of the stack. The parser maintains the
* types of values pushed to the generator stack in its type stack
* (ts_*). For instance, for binary operations two types are
* popped first and the resulting type is pushed to the type stack
* (ts_binop()).
*
*/
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "ncc.h"
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define TYPE_BT(t) ((t)->ptr ? ULNG : (t)->bt)
#define TYPE_SZ(t) ((t)->ptr ? ULNG : (t)->bt & T_MSIZE)
#define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
/* type->flag values */
#define T_ARRAY 0x01
#define T_STRUCT 0x02
#define T_FUNC 0x04
/* variable definition flags */
#define F_STATIC 0x01
#define F_EXTERN 0x02
struct type {
unsigned bt;
unsigned flags;
int ptr;
int id; /* for structs, functions and arrays */
int addr; /* the address is passed to gen.c; deref for value */
};
/* type stack */
static struct type ts[NTMPS];
static int nts;
static void ts_push_bt(unsigned bt)
{
ts[nts].ptr = 0;
ts[nts].flags = 0;
ts[nts].addr = 0;
ts[nts++].bt = bt;
#ifdef NCCWORDCAST
o_cast(bt); /* casting to architecture word */
#endif
}
static void ts_push(struct type *t)
{
struct type *d = &ts[nts++];
memcpy(d, t, sizeof(*t));
}
static void ts_push_addr(struct type *t)
{
ts_push(t);
ts[nts - 1].addr = 1;
}
static void ts_pop(struct type *type)
{
nts--;
if (type)
*type = ts[nts];
}
void err(char *fmt, ...)
{
va_list ap;
char msg[512];
va_start(ap, fmt);
vsprintf(msg, fmt, ap);
va_end(ap);
die("%s: %s", cpp_loc(tok_addr()), msg);
}
void *mextend(void *old, long oldsz, long newsz, long memsz)
{
void *new = malloc(newsz * memsz);
memcpy(new, old, oldsz * memsz);
memset(new + oldsz * memsz, 0, (newsz - oldsz) * memsz);
free(old);
return new;
}
struct name {
char name[NAMELEN];
char elfname[NAMELEN]; /* local elf name for function static variables */
struct type type;
long addr; /* local stack offset, global data addr, struct offset */
};
static struct name *locals;
static int locals_n, locals_sz;
static struct name *globals;
static int globals_n, globals_sz;
static void local_add(struct name *name)
{
if (locals_n >= locals_sz) {
locals_sz = MAX(128, locals_sz * 2);
locals = mextend(locals, locals_n, locals_sz, sizeof(locals[0]));
}
memcpy(&locals[locals_n++], name, sizeof(*name));
}
static int local_find(char *name)
{
int i;
for (i = locals_n - 1; i >= 0; --i)
if (!strcmp(locals[i].name, name))
return i;
return -1;
}
static int global_find(char *name)
{
int i;
for (i = globals_n - 1; i >= 0; i--)
if (!strcmp(name, globals[i].name))
return i;
return -1;
}
static void global_add(struct name *name)
{
if (globals_n >= globals_sz) {
globals_sz = MAX(128, globals_sz * 2);
globals = mextend(globals, globals_n, globals_sz, sizeof(globals[0]));
}
memcpy(&globals[globals_n++], name, sizeof(*name));
}
#define LABEL() (++label)
static int label; /* last used label id */
static int l_break; /* current break label */
static int l_cont; /* current continue label */
static struct enumval {
char name[NAMELEN];
int n;
} *enums;
static int enums_n, enums_sz;
static void enum_add(char *name, int val)
{
struct enumval *ev;
if (enums_n >= enums_sz) {
enums_sz = MAX(128, enums_sz * 2);
enums = mextend(enums, enums_n, enums_sz, sizeof(enums[0]));
}
ev = &enums[enums_n++];
strcpy(ev->name, name);
ev->n = val;
}
static int enum_find(int *val, char *name)
{
int i;
for (i = enums_n - 1; i >= 0; --i)
if (!strcmp(name, enums[i].name)) {
*val = enums[i].n;
return 0;
}
return 1;
}
static struct typdefinfo {
char name[NAMELEN];
struct type type;
} *typedefs;
static int typedefs_n, typedefs_sz;
static void typedef_add(char *name, struct type *type)
{
struct typdefinfo *ti;
if (typedefs_n >= typedefs_sz) {
typedefs_sz = MAX(128, typedefs_sz * 2);
typedefs = mextend(typedefs, typedefs_n, typedefs_sz,
sizeof(typedefs[0]));
}
ti = &typedefs[typedefs_n++];
strcpy(ti->name, name);
memcpy(&ti->type, type, sizeof(*type));
}
static int typedef_find(char *name)
{
int i;
for (i = typedefs_n - 1; i >= 0; --i)
if (!strcmp(name, typedefs[i].name))
return i;
return -1;
}
static struct array {
struct type type;
int n;
} *arrays;
static int arrays_n, arrays_sz;
static int array_add(struct type *type, int n)
{
struct array *a;
if (arrays_n >= arrays_sz) {
arrays_sz = MAX(128, arrays_sz * 2);
arrays = mextend(arrays, arrays_n, arrays_sz, sizeof(arrays[0]));
}
a = &arrays[arrays_n++];
memcpy(&a->type, type, sizeof(*type));
a->n = n;
return a - arrays;
}
static void array2ptr(struct type *t)
{
if (t->flags & T_ARRAY && !t->ptr) {
memcpy(t, &arrays[t->id].type, sizeof(*t));
t->ptr++;
}
}
static struct structinfo {
char name[NAMELEN];
struct name fields[NFIELDS];
int nfields;
int isunion;
int size;
} *structs;
static int structs_n, structs_sz;
static int struct_find(char *name, int isunion)
{
int i;
for (i = structs_n - 1; i >= 0; --i)
if (*structs[i].name && !strcmp(name, structs[i].name) &&
structs[i].isunion == isunion)
return i;
if (structs_n >= structs_sz) {
structs_sz = MAX(128, structs_sz * 2);
structs = mextend(structs, structs_n, structs_sz, sizeof(structs[0]));
}
i = structs_n++;
memset(&structs[i], 0, sizeof(structs[i]));
strcpy(structs[i].name, name);
structs[i].isunion = isunion;
return i;
}
static struct name *struct_field(int id, char *name)
{
struct structinfo *si = &structs[id];
int i;
for (i = 0; i < si->nfields; i++)
if (!strcmp(name, si->fields[i].name))
return &si->fields[i];
err("unknown field <%s>\n", name);
return NULL;
}
/* return t's size */
static int type_totsz(struct type *t)
{
if (t->ptr)
return ULNG;
if (t->flags & T_ARRAY)
return arrays[t->id].n * type_totsz(&arrays[t->id].type);
return t->flags & T_STRUCT ? structs[t->id].size : T_SZ(t->bt);
}
/* return t's dereferenced size */
static unsigned type_szde(struct type *t)
{
struct type de = *t;
array2ptr(&de);
de.ptr--;
return type_totsz(&de);
}
/* dereference stack top if t->addr (ie. address is pushed to gen.c) */
static void ts_de(int deref)
{
struct type *t = &ts[nts - 1];
array2ptr(t);
if (deref && t->addr && (t->ptr || !(t->flags & T_FUNC)))
o_deref(TYPE_BT(t));
t->addr = 0;
}
/* pop stack pop to *t and dereference if t->addr */
static void ts_pop_de(struct type *t)
{
ts_de(1);
ts_pop(t);
}
/* pop the top 2 stack values and dereference them if t->addr */
static void ts_pop_de2(struct type *t1, struct type *t2)
{
ts_pop_de(t1);
o_tmpswap();
ts_pop_de(t2);
o_tmpswap();
}
/* the previous identifier; to handle labels */
static char tok_previden[NAMELEN];
static char *tok_iden(void)
{
snprintf(tok_previden, sizeof(tok_previden), "%s", tok_get());
return tok_previden;
}
static int tok_jmp(char *tok)
{
if (strcmp(tok, tok_see()))
return 1;
tok_get();
return 0;
}
static int tok_comes(char *tok)
{
return !strcmp(tok, tok_see());
}
static void tok_req(char *tok)
{
char *got = tok_get();
if (strcmp(tok, got))
err("syntax error (expected <%s> but got <%s>)\n", tok, got);
}
static int tok_grp(void)
{
int c = (unsigned char) tok_see()[0];
if (c == '"')
return '"';
if (c == '\'' || isdigit(c))
return '0';
if (c == '_' || isalpha(c))
return 'a';
return 0;
}
/* the result of a binary operation on variables of type bt1 and bt2 */
static unsigned bt_op(unsigned bt1, unsigned bt2)
{
/* Type conversion according to ISO9899, sec. 6.3.1.8.
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
*
* Summary:
* Size: Always convert to largest size.
* Sign:
* - Same sign: Keep sign.
* - Same size or unsigned larger: Convert to unsigned.
* - Signed larger: Convert to signed.
*/
int sz = MAX(T_SZ(bt1), T_SZ(bt2));
/* the unsigned operand is at least as large as the signed one */
if ((!T_SG(bt1) && T_SG(bt2) && T_SZ(bt1) >= T_SZ(bt2)) ||
(T_SG(bt1) && !T_SG(bt2) && T_SZ(bt1) <= T_SZ(bt2)))
return MAX(sz, UINT);
return T_SG(bt1) | T_SG(bt2) | MAX(sz, UINT);
}
/* the result of a unary operation on variables of bt */
static unsigned bt_uop(unsigned bt)
{
return bt_op(bt, SINT);
}
/* push the result of a binary operation on the type stack */
static void ts_binop(int op)
{
struct type t1, t2;
unsigned bt1, bt2, bt;
ts_pop_de2(&t1, &t2);
bt1 = TYPE_BT(&t1);
bt2 = TYPE_BT(&t2);
bt = bt_op(bt1, bt2);
if (op == O_DIV || op == O_MOD)
bt = T_MK(bt2 & T_MSIGN, bt);
o_bop(O_MK(op, bt));
ts_push_bt(bt);
}
/* push the result of an additive binary operation on the type stack */
static void ts_addop(int op)
{
struct type t1, t2;
ts_pop_de2(&t1, &t2);
if (!t1.ptr && !t2.ptr) {
o_bop(op);
ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
return;
}
if (t1.ptr && !t2.ptr)
o_tmpswap();
if (!t1.ptr && t2.ptr)
if (type_szde(&t2) > 1) {
o_num(type_szde(&t2));
o_bop(O_MUL);
}
if (t1.ptr && !t2.ptr)
o_tmpswap();
o_bop(op);
if (t1.ptr && t2.ptr) {
int sz = type_szde(&t1);
if (sz > 1) {
o_num(sz);
o_bop(O_DIV);
}
ts_push_bt(SLNG);
} else {
ts_push(t1.ptr ? &t1 : &t2);
}
}
/* function prototypes for parsing function and variable declarations */
static int readname(struct type *main, char *name, struct type *base);
static int readtype(struct type *type);
static int readdefs(void (*def)(long data, struct name *name, unsigned flags),
long data);
static int readdefs_int(void (*def)(long data, struct name *name, unsigned flags),
long data);
/* function prototypes for parsing initializer expressions */
static int initsize(void);
static void initexpr(struct type *t, int off, void *obj,
void (*set)(void *obj, int off, struct type *t));
static int type_alignment(struct type *t)
{
if (t->flags & T_ARRAY && !t->ptr)
return type_alignment(&arrays[t->id].type);
if (t->flags & T_STRUCT && !t->ptr)
return type_alignment(&structs[t->id].fields[0].type);
return MIN(ULNG, type_totsz(t));
}
static void structdef(long data, struct name *name, unsigned flags)
{
struct structinfo *si = &structs[data];
if (si->isunion) {
name->addr = 0;
if (si->size < type_totsz(&name->type))
si->size = type_totsz(&name->type);
} else {
struct type *t = &name->type;
int alignment = type_alignment(t);
if (t->flags & T_ARRAY && !t->ptr)
alignment = MIN(ULNG, type_totsz(&arrays[t->id].type));
si->size = ALIGN(si->size, alignment);
name->addr = si->size;
si->size += type_totsz(&name->type);
}
memcpy(&si->fields[si->nfields++], name, sizeof(*name));
}
static int struct_create(char *name, int isunion)
{
int id = struct_find(name, isunion);
tok_req("{");
while (tok_jmp("}")) {
readdefs(structdef, id);
tok_req(";");
}
return id;
}
static void readexpr(void);
static void enum_create(void)
{
long n = 0;
tok_req("{");
while (tok_jmp("}")) {
char name[NAMELEN];
strcpy(name, tok_get());
if (!tok_jmp("=")) {
readexpr();
ts_pop_de(NULL);
if (o_popnum(&n))
err("const expr expected!\n");
}
enum_add(name, n++);
tok_jmp(",");
}
}
/* used to differentiate labels from case and cond exprs */
static int ncexpr;
static int caseexpr;
static void readpre(void);
static char *tmp_str(char *buf, int len)
{
static char name[NAMELEN];
static int id;
sprintf(name, "__neatcc.s%d", id++);
buf[len] = '\0';
o_dscpy(o_dsnew(name, len + 1, 0), buf, len + 1);
return name;
}
static void readprimary(void)
{
if (tok_grp() == '0') {
long n;
int bt = tok_num(tok_get(), &n);
o_num(n);
ts_push_bt(bt);
return;
}
if (tok_grp() == '"') {
struct type t = {}; /* char type inside the arrays */
struct type a = {}; /* the char array type */
char *buf = tok_get() + 1;
int len = tok_len() - 2;
t.bt = 1 | T_MSIGN;
a.id = array_add(&t, len + 1);
a.flags = T_ARRAY;
o_sym(tmp_str(buf, len));
ts_push(&a);
return;
}
if (tok_grp() == 'a') {
struct name unkn = {""};
char *name = unkn.name;
int n;
strcpy(name, tok_iden());
/* don't search for labels here */
if (!ncexpr && !caseexpr && tok_comes(":"))
return;
if ((n = local_find(name)) != -1) {
struct name *l = &locals[n];
o_local(l->addr);
ts_push_addr(&l->type);
return;
}
if ((n = global_find(name)) != -1) {
struct name *g = &globals[n];
o_sym(*g->elfname ? g->elfname : g->name);
ts_push_addr(&g->type);
return;
}
if (!enum_find(&n, name)) {
o_num(n);
ts_push_bt(SINT);
return;
}
if (!tok_comes("("))
err("unknown symbol <%s>\n", name);
global_add(&unkn);
o_sym(unkn.name);
ts_push_bt(ULNG);
return;
}
if (!tok_jmp("(")) {
struct type t;
if (!readtype(&t)) {
struct type o;
tok_req(")");
readpre();
ts_pop_de(&o);
ts_push(&t);
if (!t.ptr || !o.ptr)
o_cast(TYPE_BT(&t));
} else {
readexpr();
while (tok_jmp(")")) {
tok_req(",");
ts_pop(NULL);
o_tmpdrop(1);
readexpr();
}
}
return;
}
}
static void arrayderef(void)
{
struct type t;
int sz;
ts_pop_de(NULL);
ts_pop(&t);
if (!(t.flags & T_ARRAY && !t.ptr) && t.addr) {
o_tmpswap();
o_deref(TYPE_BT(&t));
o_tmpswap();
}
array2ptr(&t);
t.ptr--;
sz = type_totsz(&t);
t.addr = 1;
if (sz > 1) {
o_num(sz);
o_bop(O_MUL);
}
o_bop(O_ADD);
ts_push(&t);
}
static void inc_post(int op)
{
struct type t = ts[nts - 1];
/* pushing the value before inc */
o_tmpcopy();
ts_de(1);
o_tmpswap();
/* increment by 1 or pointer size */
o_tmpcopy();
ts_push(&t);
ts_pop_de(&t);
o_num(t.ptr > 0 ? type_szde(&t) : 1);
o_bop(op);
/* assign back */
o_assign(TYPE_BT(&t));
o_tmpdrop(1);
}
static void readfield(void)
{
struct name *field;
struct type t;
ts_pop(&t);
array2ptr(&t);
field = struct_field(t.id, tok_get());
if (field->addr) {
o_num(field->addr);
o_bop(O_ADD);
}
ts_push_addr(&field->type);
}
static struct funcinfo {
struct type args[NARGS];
struct type ret;
int nargs;
int varg;
/* function and argument names; useful only when defining */
char argnames[NARGS][NAMELEN];
char name[NAMELEN];
} *funcs;
static int funcs_n, funcs_sz;
static int func_create(struct type *ret, char *name, char argnames[][NAMELEN],
struct type *args, int nargs, int varg)
{
struct funcinfo *fi;
int i;
if (funcs_n >= funcs_sz) {
funcs_sz = MAX(128, funcs_sz * 2);
funcs = mextend(funcs, funcs_n, funcs_sz, sizeof(funcs[0]));
}
fi = &funcs[funcs_n++];
memcpy(&fi->ret, ret, sizeof(*ret));
for (i = 0; i < nargs; i++)
memcpy(&fi->args[i], &args[i], sizeof(*ret));
fi->nargs = nargs;
fi->varg = varg;
strcpy(fi->name, name ? name : "");
for (i = 0; i < nargs; i++)
strcpy(fi->argnames[i], argnames[i]);
return fi - funcs;
}
static void readcall(void)
{
struct type t;
struct funcinfo *fi;
int argc = 0;
ts_pop(&t);
if (t.flags & T_FUNC && t.ptr > 0)
o_deref(ULNG);
if (!tok_comes(")")) {
do {
readexpr();
ts_pop_de(NULL);
argc++;
} while (!tok_jmp(","));
}
tok_req(")");
fi = t.flags & T_FUNC ? &funcs[t.id] : NULL;
o_call(argc, fi ? TYPE_BT(&fi->ret) : SINT);
if (fi) {
if (TYPE_BT(&fi->ret))
o_cast(TYPE_BT(&fi->ret));
ts_push(&fi->ret);
} else {
ts_push_bt(SINT);
}
}
static void readpost(void)
{
readprimary();
while (1) {
if (!tok_jmp("[")) {
readexpr();
tok_req("]");
arrayderef();
continue;
}
if (!tok_jmp("(")) {
readcall();
continue;
}
if (!tok_jmp("++")) {
inc_post(O_ADD);
continue;
}
if (!tok_jmp("--")) {
inc_post(O_SUB);
continue;
}
if (!tok_jmp(".")) {
readfield();
continue;
}
if (!tok_jmp("->")) {
ts_de(1);
readfield();
continue;
}
break;
}
}
static void inc_pre(int op)
{
struct type t;
readpre();
/* copy the destination */
o_tmpcopy();
ts_push(&ts[nts - 1]);
/* increment by 1 or pointer size */
ts_pop_de(&t);
o_num(t.ptr > 0 ? type_szde(&t) : 1);
o_bop(op);
/* assign the result */
o_assign(TYPE_BT(&t));
ts_de(0);
}
static void readpre(void)
{
struct type t;
if (!tok_jmp("&")) {
readpre();
ts_pop(&t);
if (!t.addr)
err("cannot use the address\n");
t.ptr++;
t.addr = 0;
ts_push(&t);
return;
}
if (!tok_jmp("*")) {
readpre();
ts_pop(&t);
array2ptr(&t);
if (!t.ptr)
err("dereferencing non-pointer\n");
if (t.addr)
o_deref(TYPE_BT(&t));
t.ptr--;
t.addr = 1;
ts_push(&t);
return;
}
if (!tok_jmp("!")) {
readpre();
ts_pop_de(NULL);
o_uop(O_LNOT);
ts_push_bt(SINT);
return;
}
if (!tok_jmp("+")) {
readpre();
ts_de(1);
ts_pop(&t);
ts_push_bt(bt_uop(TYPE_BT(&t)));
return;
}
if (!tok_jmp("-")) {
readpre();
ts_de(1);
ts_pop(&t);
o_uop(O_NEG);
ts_push_bt(bt_uop(TYPE_BT(&t)));
return;
}
if (!tok_jmp("~")) {
readpre();
ts_de(1);
ts_pop(&t);
o_uop(O_NOT);
ts_push_bt(bt_uop(TYPE_BT(&t)));
return;
}
if (!tok_jmp("++")) {
inc_pre(O_ADD);
return;
}
if (!tok_jmp("--")) {
inc_pre(O_SUB);
return;
}
if (!tok_jmp("sizeof")) {
struct type t;
int op = !tok_jmp("(");
if (readtype(&t)) {
long m = o_mark();
if (op)
readexpr();
else
readpre();
o_back(m);
ts_pop(&t);
o_tmpdrop(1);
}
o_num(type_totsz(&t));
ts_push_bt(ULNG);
if (op)
tok_req(")");
return;
}
readpost();
}
static void readmul(void)
{
readpre();
while (1) {
if (!tok_jmp("*")) {
readpre();
ts_binop(O_MUL);
continue;
}
if (!tok_jmp("/")) {
readpre();
ts_binop(O_DIV);
continue;
}
if (!tok_jmp("%")) {
readpre();
ts_binop(O_MOD);
continue;
}
break;
}
}
static void readadd(void)
{
readmul();
while (1) {
if (!tok_jmp("+")) {
readmul();
ts_addop(O_ADD);
continue;
}
if (!tok_jmp("-")) {
readmul();
ts_addop(O_SUB);
continue;
}
break;
}
}
static void shift(int op)
{
struct type t;
readadd();
ts_pop_de2(NULL, &t);
o_bop(O_MK(op, TYPE_BT(&t)));
ts_push_bt(bt_uop(TYPE_BT(&t)));
}
static void readshift(void)
{
readadd();
while (1) {
if (!tok_jmp("<<")) {
shift(O_SHL);
continue;
}
if (!tok_jmp(">>")) {
shift(O_SHR);
continue;
}
break;
}
}
static void cmp(int op)
{
struct type t1, t2;
int bt;
readshift();
ts_pop_de2(&t1, &t2);
bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
o_bop(O_MK(op, bt));
ts_push_bt(SINT);
}
static void readcmp(void)
{
readshift();
while (1) {
if (!tok_jmp("<")) {
cmp(O_LT);
continue;
}
if (!tok_jmp(">")) {
cmp(O_GT);
continue;
}
if (!tok_jmp("<=")) {
cmp(O_LE);
continue;
}
if (!tok_jmp(">=")) {
cmp(O_GE);
continue;
}
break;
}
}
static void eq(int op)
{
readcmp();
ts_pop_de2(NULL, NULL);
o_bop(op);
ts_push_bt(SINT);
}
static void readeq(void)
{
readcmp();
while (1) {
if (!tok_jmp("==")) {
eq(O_EQ);
continue;
}
if (!tok_jmp("!=")) {
eq(O_NE);
continue;
}
break;
}
}
static void readbitand(void)