gen: remove the unused c_op()
[neatcc/cc.git] / ncc.c
blob7aeed11df052411c8a187d9edbe6d1d31960c30b
1 /*
2 * neatcc - A small and simple C compiler
4 * Copyright (C) 2010 Ali Gholami Rudi
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, as published by the
8 * Free Software Foundation.
9 */
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include "gen.h"
17 #include "tok.h"
19 #define MAXLOCALS (1 << 10)
20 #define MAXGLOBALS (1 << 10)
21 #define MAXARGS (1 << 5)
22 #define print(s) write(2, (s), strlen(s));
24 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
25 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
27 #define T_ARRAY 0x01
28 #define T_STRUCT 0x02
29 #define T_FUNC 0x04
31 #define F_INIT 0x01
32 #define F_STATIC 0x02
33 #define F_EXTERN 0x04
35 struct type {
36 unsigned bt;
37 unsigned flags;
38 int ptr;
39 int id; /* for structs, functions and arrays */
42 /* type stack */
43 static struct type ts[MAXTMP];
44 static int nts;
46 static void ts_push_bt(unsigned bt)
48 ts[nts].ptr = 0;
49 ts[nts].flags = 0;
50 ts[nts++].bt = bt;
53 static void ts_push(struct type *t)
55 memcpy(&ts[nts++], t, sizeof(*t));
56 if (t->flags & (T_FUNC | T_ARRAY) && !t->ptr)
57 o_addr();
60 static void ts_pop(struct type *type)
62 nts--;
63 if (type)
64 *type = ts[nts];
67 void die(char *msg)
69 print(msg);
70 exit(1);
73 void err(char *msg)
75 char err[1 << 7];
76 int len = cpp_loc(err, tok_addr());
77 strcpy(err + len, msg);
78 die(err);
81 struct name {
82 char name[NAMELEN];
83 struct type type;
84 long addr;
85 int unused; /* unreferenced external symbols */
88 static struct name locals[MAXLOCALS];
89 static int nlocals;
90 static struct name globals[MAXGLOBALS];
91 static int nglobals;
93 static void local_add(struct name *name)
95 if (nlocals >= MAXLOCALS)
96 err("nomem: MAXLOCALS reached!\n");
97 memcpy(&locals[nlocals++], name, sizeof(*name));
100 static int global_find(char *name)
102 int i;
103 for (i = 0; i < nglobals; i++)
104 if (!strcmp(name, globals[i].name))
105 return i;
106 return -1;
109 static void global_add(struct name *name)
111 int found = global_find(name->name);
112 int i = found == -1 ? nglobals++ : found;
113 if (nglobals >= MAXGLOBALS)
114 err("nomem: MAXGLOBALS reached!\n");
115 memcpy(&globals[i], name, sizeof(*name));
118 #define MAXENUMS (1 << 10)
120 static struct enumval {
121 char name[NAMELEN];
122 int n;
123 } enums[MAXENUMS];
124 static int nenums;
126 static void enum_add(char *name, int val)
128 struct enumval *ev = &enums[nenums++];
129 if (nenums >= MAXENUMS)
130 err("nomem: MAXENUMS reached!\n");
131 strcpy(ev->name, name);
132 ev->n = val;
135 static int enum_find(int *val, char *name)
137 int i;
138 for (i = nenums - 1; i >= 0; --i)
139 if (!strcmp(name, enums[i].name)) {
140 *val = enums[i].n;
141 return 0;
143 return 1;
146 #define MAXTYPEDEFS (1 << 10)
148 static struct typdefinfo {
149 char name[NAMELEN];
150 struct type type;
151 } typedefs[MAXTYPEDEFS];
152 static int ntypedefs;
154 static void typedef_add(char *name, struct type *type)
156 struct typdefinfo *ti = &typedefs[ntypedefs++];
157 if (ntypedefs >= MAXTYPEDEFS)
158 err("nomem: MAXTYPEDEFS reached!\n");
159 strcpy(ti->name, name);
160 memcpy(&ti->type, type, sizeof(*type));
163 static int typedef_find(char *name)
165 int i;
166 for (i = ntypedefs - 1; i >= 0; --i)
167 if (!strcmp(name, typedefs[i].name))
168 return i;
169 return -1;
172 #define MAXARRAYS (1 << 10)
174 static struct array {
175 struct type type;
176 int n;
177 } arrays[MAXARRAYS];
178 static int narrays;
180 static int array_add(struct type *type, int n)
182 struct array *a = &arrays[narrays++];
183 if (narrays >= MAXARRAYS)
184 err("nomem: MAXARRAYS reached!\n");
185 memcpy(&a->type, type, sizeof(*type));
186 a->n = n;
187 return a - arrays;
190 static void array2ptr(struct type *t)
192 if (!(t->flags & T_ARRAY) || t->ptr)
193 return;
194 memcpy(t, &arrays[t->id].type, sizeof(*t));
195 t->ptr++;
198 #define MAXSTRUCTS (1 << 10)
199 #define MAXFIELDS (1 << 7)
201 static struct structinfo {
202 char name[NAMELEN];
203 struct name fields[MAXFIELDS];
204 int nfields;
205 int isunion;
206 int size;
207 } structs[MAXSTRUCTS];
208 static int nstructs;
210 static int struct_find(char *name, int isunion)
212 int i;
213 for (i = nstructs - 1; i >= 0; --i)
214 if (*structs[i].name && !strcmp(name, structs[i].name) &&
215 structs[i].isunion == isunion)
216 return i;
217 i = nstructs++;
218 if (nstructs >= MAXSTRUCTS)
219 err("nomem: MAXTYPES reached!\n");
220 memset(&structs[i], 0, sizeof(structs[i]));
221 strcpy(structs[i].name, name);
222 structs[i].isunion = isunion;
223 return i;
226 static struct name *struct_field(int id, char *name)
228 struct structinfo *si = &structs[id];
229 int i;
230 for (i = 0; i < si->nfields; i++)
231 if (!strcmp(name, si->fields[i].name))
232 return &si->fields[i];
233 err("field not found\n");
236 #define MAXBREAK (1 << 7)
238 static long breaks[MAXBREAK];
239 static int nbreaks;
240 static long continues[MAXBREAK];
241 static int ncontinues;
243 static void break_fill(long addr, int till)
245 int i;
246 for (i = till; i < nbreaks; i++)
247 o_filljmp2(breaks[i], addr);
248 nbreaks = till;
251 static void continue_fill(long addr, int till)
253 int i;
254 for (i = till; i < ncontinues; i++)
255 o_filljmp2(continues[i], addr);
256 ncontinues = till;
259 static int type_totsz(struct type *t)
261 if (t->ptr)
262 return LONGSZ;
263 if (t->flags & T_ARRAY)
264 return arrays[t->id].n * type_totsz(&arrays[t->id].type);
265 return t->flags & T_STRUCT ? structs[t->id].size : BT_SZ(t->bt);
268 static unsigned type_szde(struct type *t)
270 struct type de = *t;
271 array2ptr(&de);
272 de.ptr--;
273 return type_totsz(&de);
276 static int tok_jmp(int tok)
278 if (tok_see() != tok)
279 return 1;
280 tok_get();
281 return 0;
284 static void tok_expect(int tok)
286 if (tok_get() != tok)
287 err("syntax error\n");
290 static unsigned bt_op(unsigned bt1, unsigned bt2)
292 unsigned s1 = BT_SZ(bt1);
293 unsigned s2 = BT_SZ(bt2);
294 return (bt1 | bt2) & BT_SIGNED | (s1 > s2 ? s1 : s2);
297 static void ts_binop(int op)
299 struct type t1, t2;
300 ts_pop(&t1);
301 ts_pop(&t2);
302 o_bop(op);
303 ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
306 static void ts_addop(int op)
308 struct type t1, t2;
309 ts_pop(&t1);
310 ts_pop(&t2);
311 array2ptr(&t1);
312 array2ptr(&t2);
313 if (!t1.ptr && !t2.ptr) {
314 o_bop(op);
315 ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
316 return;
318 if (t1.ptr && !t2.ptr) {
319 struct type t = t2;
320 t2 = t1;
321 t1 = t;
322 o_tmpswap();
324 if (!t1.ptr && t2.ptr)
325 if (type_szde(&t2) > 1) {
326 o_num(type_szde(&t2), 4);
327 o_bop(O_MUL);
329 o_bop(op);
330 if (t1.ptr && t2.ptr) {
331 int sz = type_szde(&t1);
332 if (sz > 1) {
333 o_num(sz, 4);
334 o_bop(O_DIV);
336 ts_push_bt(4 | BT_SIGNED);
337 } else {
338 ts_push(&t2);
342 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
343 #define MIN(a, b) ((a) < (b) ? (a) : (b))
345 static int type_alignment(struct type *t)
347 if (t->flags & T_ARRAY && !t->ptr)
348 return type_alignment(&arrays[t->id].type);
349 if (t->flags & T_STRUCT && !t->ptr)
350 return type_alignment(&structs[t->id].fields[0].type);
351 return MIN(LONGSZ, type_totsz(t));
354 static void structdef(void *data, struct name *name, unsigned flags)
356 struct structinfo *si = data;
357 if (si->isunion) {
358 name->addr = 0;
359 if (si->size < type_totsz(&name->type))
360 si->size = type_totsz(&name->type);
361 } else {
362 struct type *t = &name->type;
363 int alignment = type_alignment(t);
364 if (t->flags & T_ARRAY && !t->ptr)
365 alignment = MIN(LONGSZ, type_totsz(&arrays[t->id].type));
366 si->size = ALIGN(si->size, alignment);
367 name->addr = si->size;
368 si->size += type_totsz(&name->type);
370 memcpy(&si->fields[si->nfields++], name, sizeof(*name));
373 static int readdefs(void (*def)(void *, struct name *, unsigned f), void *data);
375 static int struct_create(char *name, int isunion)
377 int id = struct_find(name, isunion);
378 struct structinfo *si = &structs[id];
379 tok_expect('{');
380 while (tok_jmp('}')) {
381 readdefs(structdef, si);
382 tok_expect(';');
384 return id;
387 static void readexpr(void);
389 static void enum_create(void)
391 long n = 0;
392 tok_expect('{');
393 while (tok_jmp('}')) {
394 char name[NAMELEN];
395 tok_expect(TOK_NAME);
396 strcpy(name, tok_id());
397 if (tok_see() == '=') {
398 tok_get();
399 readexpr();
400 ts_pop(NULL);
401 if (o_popnum(&n))
402 err("const expr expected!\n");
404 enum_add(name, n++);
405 tok_jmp(',');
409 static int basetype(struct type *type, unsigned *flags)
411 int sign = 1;
412 int size = 4;
413 int done = 0;
414 int i = 0;
415 int isunion;
416 char name[NAMELEN] = "";
417 *flags = 0;
418 type->flags = 0;
419 type->ptr = 0;
420 while (!done) {
421 switch (tok_see()) {
422 case TOK_STATIC:
423 *flags |= F_STATIC;
424 break;
425 case TOK_EXTERN:
426 *flags |= F_EXTERN;
427 break;
428 case TOK_VOID:
429 sign = 0;
430 size = 0;
431 done = 1;
432 break;
433 case TOK_INT:
434 done = 1;
435 break;
436 case TOK_CHAR:
437 size = 1;
438 done = 1;
439 break;
440 case TOK_SHORT:
441 size = 2;
442 break;
443 case TOK_LONG:
444 size = LONGSZ;
445 break;
446 case TOK_SIGNED:
447 break;
448 case TOK_UNSIGNED:
449 sign = 0;
450 break;
451 case TOK_UNION:
452 case TOK_STRUCT:
453 isunion = tok_get() == TOK_UNION;
454 if (!tok_jmp(TOK_NAME))
455 strcpy(name, tok_id());
456 if (tok_see() == '{')
457 type->id = struct_create(name, isunion);
458 else
459 type->id = struct_find(name, isunion);
460 type->flags |= T_STRUCT;
461 type->bt = LONGSZ;
462 return 0;
463 case TOK_ENUM:
464 tok_get();
465 tok_jmp(TOK_NAME);
466 if (tok_see() == '{')
467 enum_create();
468 type->bt = 4 | BT_SIGNED;
469 return 0;
470 default:
471 if (tok_see() == TOK_NAME) {
472 int id = typedef_find(tok_id());
473 if (id != -1) {
474 tok_get();
475 memcpy(type, &typedefs[id].type,
476 sizeof(*type));
477 return 0;
480 if (!i)
481 return 1;
482 done = 1;
483 continue;
485 i++;
486 tok_get();
488 type->bt = size | (sign ? BT_SIGNED : 0);
489 return 0;
492 static int readname(struct type *main, char *name,
493 struct type *base, unsigned flags);
495 static int readtype(struct type *type)
497 return readname(type, NULL, NULL, 0);
500 static void readptrs(struct type *type)
502 while (!tok_jmp('*')) {
503 type->ptr++;
504 if (!type->bt)
505 type->bt = 1;
509 /* used to differenciate labels from case and cond exprs */
510 static int ncexpr;
511 static int caseexpr;
513 static void readpre(void);
515 static void readprimary(void)
517 int i;
518 if (!tok_jmp(TOK_NUM)) {
519 long n;
520 int bt = tok_num(&n);
521 ts_push_bt(bt);
522 o_num(n, bt);
523 return;
525 if (!tok_jmp(TOK_STR)) {
526 struct type t;
527 char buf[BUFSIZE];
528 int len;
529 t.bt = 1 | BT_SIGNED;
530 t.ptr = 1;
531 t.flags = 0;
532 ts_push(&t);
533 len = tok_str(buf);
534 o_symaddr(out_mkdat(NULL, buf, len, 0), TYPE_BT(&t));
535 o_addr();
536 return;
538 if (!tok_jmp(TOK_NAME)) {
539 struct name unkn = {0};
540 char *name = unkn.name;
541 int n;
542 strcpy(name, tok_id());
543 /* don't search for labels here */
544 if (!ncexpr && !caseexpr && tok_see() == ':')
545 return;
546 for (i = nlocals - 1; i >= 0; --i) {
547 struct type *t = &locals[i].type;
548 if (!strcmp(locals[i].name, name)) {
549 o_local(locals[i].addr, TYPE_BT(t));
550 ts_push(t);
551 return;
554 if ((n = global_find(name)) != -1) {
555 struct name *g = &globals[n];
556 struct type *t = &g->type;
557 if (g->unused) {
558 g->unused = 0;
559 if (t->flags & T_FUNC && !t->ptr)
560 g->addr = out_mkundef(name, 0);
561 else
562 g->addr = out_mkundef(name, type_totsz(t));
564 o_symaddr(g->addr, TYPE_BT(t));
565 ts_push(t);
566 return;
568 if (!enum_find(&n, name)) {
569 ts_push_bt(4 | BT_SIGNED);
570 o_num(n, 4 | BT_SIGNED);
571 return;
573 if (tok_see() != '(')
574 err("unknown symbol\n");
575 unkn.addr = out_mkundef(unkn.name, 0);
576 global_add(&unkn);
577 ts_push_bt(LONGSZ);
578 o_symaddr(unkn.addr, LONGSZ);
579 return;
581 if (!tok_jmp('(')) {
582 struct type t;
583 if (!readtype(&t)) {
584 struct type o;
585 tok_expect(')');
586 readpre();
587 ts_pop(&o);
588 ts_push(&t);
589 if (!t.ptr || !o.ptr)
590 o_cast(TYPE_BT(&t));
591 } else {
592 readexpr();
593 tok_expect(')');
595 return;
599 void arrayderef(struct type *t)
601 int sz = type_totsz(t);
602 if (sz > 1) {
603 o_num(sz, 4);
604 o_bop(O_MUL);
606 o_bop(O_ADD);
607 o_deref(TYPE_BT(t));
610 static void inc_post(int inc_one, int inc_many)
612 struct type *t = &ts[nts - 1];
613 int sz = type_szde(t);
614 o_tmpcopy();
615 o_load();
616 o_tmpswap();
617 if (!t->ptr || sz == 1) {
618 o_uop(inc_one);
619 } else {
620 o_tmpcopy();
621 o_num(sz, 4);
622 o_bop(inc_many);
623 o_assign(TYPE_BT(t));
625 o_tmpdrop(1);
628 static void readfield(void)
630 struct name *field;
631 struct type t;
632 tok_expect(TOK_NAME);
633 ts_pop(&t);
634 array2ptr(&t);
635 field = struct_field(t.id, tok_id());
636 if (field->addr) {
637 o_num(field->addr, 4);
638 o_bop(O_ADD);
640 o_deref(TYPE_BT(&field->type));
641 ts_push(&field->type);
644 #define MAXFUNCS (1 << 10)
646 static struct funcinfo {
647 struct type args[MAXFIELDS];
648 struct type ret;
649 int nargs;
650 } funcs[MAXFUNCS];
651 static int nfuncs;
652 static unsigned ret_bt;
654 static int func_create(struct type *ret, struct name *args, int nargs)
656 struct funcinfo *fi = &funcs[nfuncs++];
657 int i;
658 if (nfuncs >= MAXFUNCS)
659 err("nomem: MAXFUNCS reached!\n");
660 memcpy(&fi->ret, ret, sizeof(*ret));
661 for (i = 0; i < nargs; i++)
662 memcpy(&fi->args[i], &args[i].type, sizeof(*ret));
663 fi->nargs = nargs;
664 return fi - funcs;
667 static void readcall(void)
669 struct type t;
670 unsigned bt[MAXARGS];
671 struct funcinfo *fi;
672 int argc = 0;
673 int i;
674 ts_pop(&t);
675 if (t.flags & T_FUNC && t.ptr > 0)
676 o_deref(LONGSZ);
677 fi = t.flags & T_FUNC ? &funcs[t.id] : NULL;
678 if (tok_see() != ')') {
679 readexpr();
680 ts_pop(&t);
681 bt[argc++] = TYPE_BT(&t);
683 while (!tok_jmp(',')) {
684 readexpr();
685 ts_pop(&t);
686 bt[argc++] = TYPE_BT(&t);
688 tok_expect(')');
689 if (fi)
690 for (i = 0; i < fi->nargs; i++)
691 bt[i] = TYPE_BT(&fi->args[i]);
692 o_call(argc, bt, fi ? TYPE_BT(&fi->ret) : 4 | BT_SIGNED);
693 if (fi)
694 ts_push(&fi->ret);
695 else
696 ts_push_bt(4 | BT_SIGNED);
699 static void readpost(void)
701 readprimary();
702 while (1) {
703 if (!tok_jmp('[')) {
704 struct type t;
705 ts_pop(&t);
706 readexpr();
707 ts_pop(NULL);
708 tok_expect(']');
709 array2ptr(&t);
710 t.ptr--;
711 arrayderef(&t);
712 ts_push(&t);
713 continue;
715 if (!tok_jmp('(')) {
716 readcall();
717 continue;
719 if (!tok_jmp(TOK2("++"))) {
720 inc_post(O_INC, O_ADD);
721 continue;
723 if (!tok_jmp(TOK2("--"))) {
724 inc_post(O_DEC, O_SUB);
725 continue;
727 if (!tok_jmp('.')) {
728 o_addr();
729 readfield();
730 continue;
732 if (!tok_jmp(TOK2("->"))) {
733 readfield();
734 continue;
736 break;
740 static void inc_pre(int inc_one, int inc_many)
742 struct type t;
743 int sz;
744 readpre();
745 ts_pop(&t);
746 array2ptr(&t);
747 sz = type_totsz(&t);
748 if (!t.ptr || sz == 1) {
749 o_uop(inc_one);
750 } else {
751 o_num(sz, 4);
752 o_bop(inc_many);
753 o_assign(TYPE_BT(&t));
755 ts_push(&t);
758 static void readpre(void)
760 if (!tok_jmp('&')) {
761 struct type type;
762 readpre();
763 ts_pop(&type);
764 if (!(type.flags & T_FUNC) && !type.ptr)
765 type.ptr++;
766 ts_push(&type);
767 o_addr();
768 return;
770 if (!tok_jmp('*')) {
771 struct type t;
772 readpre();
773 ts_pop(&t);
774 array2ptr(&t);
775 if (!(t.flags & T_FUNC) || t.ptr > 0) {
776 t.ptr--;
777 o_deref(TYPE_BT(&t));
779 ts_push(&t);
780 return;
782 if (!tok_jmp('!')) {
783 struct type type;
784 readpre();
785 ts_pop(&type);
786 o_uop(O_LNOT);
787 ts_push_bt(4 | BT_SIGNED);
788 return;
790 if (!tok_jmp('-')) {
791 readpre();
792 o_uop(O_NEG);
793 return;
795 if (!tok_jmp('~')) {
796 readpre();
797 o_uop(O_NOT);
798 return;
800 if (!tok_jmp(TOK2("++"))) {
801 inc_pre(O_INC, O_ADD);
802 return;
804 if (!tok_jmp(TOK2("--"))) {
805 inc_pre(O_DEC, O_SUB);
806 return;
808 if (!tok_jmp(TOK_SIZEOF)) {
809 struct type t;
810 int op = !tok_jmp('(');
811 if (readtype(&t)) {
812 int nogen = !o_nogen();
813 readexpr();
814 if (nogen)
815 o_dogen();
816 ts_pop(&t);
817 o_tmpdrop(1);
819 ts_push_bt(4);
820 o_num(type_totsz(&t), 4);
821 if (op)
822 tok_expect(')');
823 return;
825 readpost();
828 static void readmul(void)
830 readpre();
831 while (1) {
832 if (!tok_jmp('*')) {
833 readpre();
834 ts_binop(O_MUL);
835 continue;
837 if (!tok_jmp('/')) {
838 readpre();
839 ts_binop(O_DIV);
840 continue;
842 if (!tok_jmp('%')) {
843 readpre();
844 ts_binop(O_MOD);
845 continue;
847 break;
851 static void readadd(void)
853 readmul();
854 while (1) {
855 if (!tok_jmp('+')) {
856 readmul();
857 ts_addop(O_ADD);
858 continue;
860 if (!tok_jmp('-')) {
861 readmul();
862 ts_addop(O_SUB);
863 continue;
865 break;
869 static void shift(int op)
871 struct type t;
872 readadd();
873 ts_pop(NULL);
874 ts_pop(&t);
875 o_bop(op);
876 ts_push_bt(TYPE_BT(&t));
879 static void readshift(void)
881 readadd();
882 while (1) {
883 if (!tok_jmp(TOK2("<<"))) {
884 shift(O_SHL);
885 continue;
887 if (!tok_jmp(TOK2(">>"))) {
888 shift(O_SHR);
889 continue;
891 break;
895 static void cmp(int op)
897 readshift();
898 ts_pop(NULL);
899 ts_pop(NULL);
900 o_bop(op);
901 ts_push_bt(4 | BT_SIGNED);
904 static void readcmp(void)
906 readshift();
907 while (1) {
908 if (!tok_jmp('<')) {
909 cmp(O_LT);
910 continue;
912 if (!tok_jmp('>')) {
913 cmp(O_GT);
914 continue;
916 if (!tok_jmp(TOK2("<="))) {
917 cmp(O_LE);
918 continue;
920 if (!tok_jmp(TOK2(">="))) {
921 cmp(O_GE);
922 continue;
924 break;
928 static void eq(int op)
930 readcmp();
931 ts_pop(NULL);
932 ts_pop(NULL);
933 o_bop(op);
934 ts_push_bt(4 | BT_SIGNED);
937 static void readeq(void)
939 readcmp();
940 while (1) {
941 if (!tok_jmp(TOK2("=="))) {
942 eq(O_EQ);
943 continue;
945 if (!tok_jmp(TOK2("!="))) {
946 eq(O_NEQ);
947 continue;
949 break;
953 static void readbitand(void)
955 readeq();
956 while (!tok_jmp('&')) {
957 readeq();
958 ts_binop(O_AND);
962 static void readxor(void)
964 readbitand();
965 while (!tok_jmp('^')) {
966 readbitand();
967 ts_binop(O_XOR);
971 static void readbitor(void)
973 readxor();
974 while (!tok_jmp('|')) {
975 readxor();
976 ts_binop(O_OR);
980 #define MAXCOND (1 << 7)
982 static void readand(void)
984 long conds[MAXCOND];
985 int nconds = 0;
986 long passed;
987 int i;
988 readbitor();
989 if (tok_see() != TOK2("&&"))
990 return;
991 o_fork();
992 conds[nconds++] = o_jz(0);
993 ts_pop(NULL);
994 while (!tok_jmp(TOK2("&&"))) {
995 readbitor();
996 conds[nconds++] = o_jz(0);
997 ts_pop(NULL);
999 o_num(1, 4 | BT_SIGNED);
1000 o_forkpush();
1001 passed = o_jmp(0);
1002 for (i = 0; i < nconds; i++)
1003 o_filljmp(conds[i]);
1004 o_num(0, 4 | BT_SIGNED);
1005 o_forkpush();
1006 o_forkjoin();
1007 o_filljmp(passed);
1008 ts_push_bt(4 | BT_SIGNED);
1011 static void reador(void)
1013 long conds[MAXCOND];
1014 int nconds = 0;
1015 long failed;
1016 int i;
1017 readand();
1018 if (tok_see() != TOK2("||"))
1019 return;
1020 o_fork();
1021 conds[nconds++] = o_jnz(0);
1022 ts_pop(NULL);
1023 while (!tok_jmp(TOK2("||"))) {
1024 readand();
1025 conds[nconds++] = o_jnz(0);
1026 ts_pop(NULL);
1028 o_num(0, 4 | BT_SIGNED);
1029 o_forkpush();
1030 failed = o_jmp(0);
1031 for (i = 0; i < nconds; i++)
1032 o_filljmp(conds[i]);
1033 o_num(1, 4 | BT_SIGNED);
1034 o_forkpush();
1035 o_forkjoin();
1036 o_filljmp(failed);
1037 ts_push_bt(4 | BT_SIGNED);
1040 static int readcexpr_const(void)
1042 long c;
1043 int nogen;
1044 if (o_popnum(&c))
1045 return -1;
1046 if (!c)
1047 nogen = !o_nogen();
1048 reador();
1049 ts_pop(NULL);
1050 tok_expect(':');
1051 if (c) {
1052 nogen = !o_nogen();
1053 } else {
1054 if (nogen)
1055 o_dogen();
1056 o_tmpdrop(1);
1058 reador();
1059 if (c) {
1060 if (nogen)
1061 o_dogen();
1062 o_tmpdrop(1);
1064 return 0;
1067 static void readcexpr(void)
1069 long l1, l2;
1070 reador();
1071 if (tok_jmp('?'))
1072 return;
1073 ncexpr++;
1074 ts_pop(NULL);
1075 o_fork();
1076 if (readcexpr_const()) {
1077 l1 = o_jz(0);
1078 reador();
1079 o_forkpush();
1080 l2 = o_jmp(0);
1081 ts_pop(NULL);
1083 tok_expect(':');
1084 o_filljmp(l1);
1085 reador();
1086 o_forkpush();
1087 o_forkjoin();
1088 o_filljmp(l2);
1090 ncexpr--;
1093 static void opassign(int op, int ptrop)
1095 struct type *t = &ts[nts - 1];
1096 if (ptrop && (t->flags & T_ARRAY || t->ptr)) {
1097 o_tmpcopy();
1098 readexpr();
1099 ts_addop(op);
1100 ts_pop(NULL);
1101 o_assign(TYPE_BT(&ts[nts - 1]));
1102 } else {
1103 readexpr();
1104 o_bop(op | O_SET);
1105 ts_pop(NULL);
1109 static void doassign(void)
1111 struct type t;
1112 ts_pop(&t);
1113 if (!t.ptr && t.flags & T_STRUCT)
1114 o_memcpy(type_totsz(&t));
1115 else
1116 o_assign(TYPE_BT(&ts[nts - 1]));
1119 static void readexpr(void)
1121 readcexpr();
1122 if (!tok_jmp('=')) {
1123 readexpr();
1124 doassign();
1125 return;
1127 if (!tok_jmp(TOK2("+="))) {
1128 opassign(O_ADD, 1);
1129 return;
1131 if (!tok_jmp(TOK2("-="))) {
1132 opassign(O_SUB, 1);
1133 return;
1135 if (!tok_jmp(TOK2("*="))) {
1136 opassign(O_MUL, 0);
1137 return;
1139 if (!tok_jmp(TOK2("/="))) {
1140 opassign(O_DIV, 0);
1141 return;
1143 if (!tok_jmp(TOK2("%="))) {
1144 opassign(O_MOD, 0);
1145 return;
1147 if (!tok_jmp(TOK3("<<="))) {
1148 opassign(O_SHL, 0);
1149 return;
1151 if (!tok_jmp(TOK3(">>="))) {
1152 opassign(O_SHR, 0);
1153 return;
1155 if (!tok_jmp(TOK3("&="))) {
1156 opassign(O_AND, 0);
1157 return;
1159 if (!tok_jmp(TOK3("|="))) {
1160 opassign(O_OR, 0);
1161 return;
1163 if (!tok_jmp(TOK3("^="))) {
1164 opassign(O_XOR, 0);
1165 return;
1169 static void readestmt(void)
1171 do {
1172 o_tmpdrop(-1);
1173 nts = 0;
1174 readexpr();
1175 } while (!tok_jmp(','));
1178 static void o_localoff(long addr, int off, unsigned bt)
1180 o_local(addr, bt);
1181 if (off) {
1182 o_addr();
1183 o_num(off, 4);
1184 o_bop(O_ADD);
1185 o_deref(bt);
1189 static struct type *innertype(struct type *t)
1191 if (t->flags & T_ARRAY && !t->ptr)
1192 return innertype(&arrays[t->id].type);
1193 return t;
1196 static void initexpr(struct type *t, int off, void *obj,
1197 void (*set)(void *obj, int off, struct type *t))
1199 if (tok_jmp('{')) {
1200 set(obj, off, t);
1201 return;
1203 if (!t->ptr && t->flags & T_STRUCT) {
1204 struct structinfo *si = &structs[t->id];
1205 int i;
1206 for (i = 0; i < si->nfields; i++) {
1207 struct name *field = &si->fields[i];
1208 if (!tok_jmp('.')) {
1209 tok_expect(TOK_NAME);
1210 field = struct_field(t->id, tok_id());
1211 tok_expect('=');
1213 initexpr(&field->type, off + field->addr, obj, set);
1214 if (tok_jmp(',') || tok_see() == '}')
1215 break;
1217 } else if (t->flags & T_ARRAY) {
1218 struct type *t_de = &arrays[t->id].type;
1219 int i;
1220 for (i = 0; ; i++) {
1221 long idx = i;
1222 struct type *it = t_de;
1223 if (!tok_jmp('[')) {
1224 readexpr();
1225 o_popnum(&idx);
1226 ts_pop(NULL);
1227 tok_expect(']');
1228 tok_expect('=');
1230 if (tok_see() != '{')
1231 it = innertype(t_de);
1232 initexpr(it, off + type_totsz(it) * idx, obj, set);
1233 if (tok_jmp(',') || tok_see() == '}')
1234 break;
1237 tok_expect('}');
1240 static void jumpbrace(void)
1242 int depth = 0;
1243 while (tok_see() != '}' || depth--)
1244 if (tok_get() == '{')
1245 depth++;
1246 tok_expect('}');
1249 static int initsize(void)
1251 long addr = tok_addr();
1252 int n = 0;
1253 if (!tok_jmp(TOK_STR)) {
1254 n = tok_str(NULL);
1255 tok_jump(addr);
1256 return n;
1258 o_nogen();
1259 tok_expect('{');
1260 while (tok_jmp('}')) {
1261 long idx = n;
1262 if (!tok_jmp('[')) {
1263 readexpr();
1264 o_popnum(&idx);
1265 ts_pop(NULL);
1266 tok_expect(']');
1267 tok_expect('=');
1269 if (n < idx + 1)
1270 n = idx + 1;
1271 while (tok_see() != '}' && tok_see() != ',')
1272 if (tok_get() == '{')
1273 jumpbrace();
1274 tok_jmp(',');
1276 o_dogen();
1277 tok_jump(addr);
1278 return n;
1281 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1283 static void globalinit(void *obj, int off, struct type *t)
1285 long addr = *(long *) obj;
1286 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1287 struct type *t_de = &arrays[t->id].type;
1288 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1289 char buf[BUFSIZE];
1290 int len;
1291 tok_expect(TOK_STR);
1292 len = tok_str(buf);
1293 out_datcpy(addr, off, buf, len);
1294 return;
1297 readexpr();
1298 o_datset(addr, off, TYPE_BT(t));
1299 ts_pop(NULL);
1302 static void globaldef(void *data, struct name *name, unsigned flags)
1304 struct type *t = &name->type;
1305 char *varname = flags & F_STATIC ? NULL : name->name;
1306 int sz;
1307 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1308 arrays[t->id].n = initsize();
1309 sz = type_totsz(t);
1310 if (flags & F_EXTERN || t->flags & T_FUNC && !t->ptr)
1311 name->unused = 1;
1312 else if (flags & F_INIT)
1313 name->addr = out_mkdat(varname, NULL, sz, F_GLOBAL(flags));
1314 else
1315 name->addr = out_mkvar(varname, sz, F_GLOBAL(flags));
1316 global_add(name);
1317 if (flags & F_INIT)
1318 initexpr(t, 0, &name->addr, globalinit);
1321 static void localinit(void *obj, int off, struct type *t)
1323 long addr = *(long *) obj;
1324 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1325 struct type *t_de = &arrays[t->id].type;
1326 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1327 char buf[BUFSIZE];
1328 int len;
1329 tok_expect(TOK_STR);
1330 len = tok_str(buf);
1331 o_localoff(addr, off, TYPE_BT(t));
1332 o_symaddr(out_mkdat(NULL, buf, len, 0), TYPE_BT(t));
1333 o_memcpy(len);
1334 o_tmpdrop(1);
1335 return;
1338 o_localoff(addr, off, TYPE_BT(t));
1339 ts_push(t);
1340 readexpr();
1341 doassign();
1342 ts_pop(NULL);
1343 o_tmpdrop(1);
1346 static void localdef(void *data, struct name *name, unsigned flags)
1348 struct type *t = &name->type;
1349 if (flags & (F_STATIC | F_EXTERN)) {
1350 globaldef(data, name, flags);
1351 return;
1353 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1354 arrays[t->id].n = initsize();
1355 name->addr = o_mklocal(type_totsz(&name->type));
1356 local_add(name);
1357 if (flags & F_INIT) {
1358 if (t->flags & (T_ARRAY | T_STRUCT) && !t->ptr) {
1359 o_local(name->addr, TYPE_BT(t));
1360 o_memset(0, type_totsz(t));
1361 o_tmpdrop(1);
1363 initexpr(t, 0, &name->addr, localinit);
1367 static void funcdef(char *name, struct type *type, struct name *args,
1368 int nargs, unsigned flags)
1370 struct name global;
1371 int i;
1372 strcpy(global.name, name);
1373 memcpy(&global.type, type, sizeof(*type));
1374 global.addr = o_func_beg(name, F_GLOBAL(flags));
1375 global.unused = 0;
1376 global_add(&global);
1377 ret_bt = TYPE_BT(&funcs[type->id].ret);
1378 for (i = 0; i < nargs; i++) {
1379 args[i].addr = o_arg(i, type_totsz(&args[i].type));
1380 local_add(&args[i]);
1384 static int readargs(struct name *args)
1386 int nargs = 0;
1387 tok_expect('(');
1388 while (tok_see() != ')') {
1389 if (!tok_jmp(TOK3("...")))
1390 break;
1391 readname(&args[nargs].type, args[nargs].name, NULL, 0);
1392 array2ptr(&args[nargs].type);
1393 nargs++;
1394 if (tok_jmp(','))
1395 break;
1397 tok_expect(')');
1398 if (nargs == 1 && !TYPE_BT(&args[0].type))
1399 return 0;
1400 return nargs;
1403 static int readname(struct type *main, char *name,
1404 struct type *base, unsigned flags)
1406 struct type tpool[3];
1407 int npool = 0;
1408 struct type *type = &tpool[npool++];
1409 struct type *func = NULL;
1410 struct type *ret = NULL;
1411 int arsz[10];
1412 int nar = 0;
1413 int i;
1414 memset(tpool, 0, sizeof(tpool));
1415 if (name)
1416 *name = '\0';
1417 if (!base) {
1418 if (basetype(type, &flags))
1419 return 1;
1420 } else {
1421 memcpy(type, base, sizeof(*base));
1423 readptrs(type);
1424 if (!tok_jmp('(')) {
1425 ret = type;
1426 type = &tpool[npool++];
1427 func = type;
1428 readptrs(type);
1430 if (!tok_jmp(TOK_NAME) && name)
1431 strcpy(name, tok_id());
1432 while (!tok_jmp('[')) {
1433 long n = 0;
1434 if (tok_jmp(']')) {
1435 readexpr();
1436 ts_pop(NULL);
1437 if (o_popnum(&n))
1438 err("const expr expected\n");
1439 tok_expect(']');
1441 arsz[nar++] = n;
1443 for (i = nar - 1; i >= 0; i--) {
1444 type->id = array_add(type, arsz[i]);
1445 if (type->flags & T_FUNC)
1446 func = &arrays[type->id].type;
1447 type->flags = T_ARRAY;
1448 type->bt = LONGSZ;
1449 type->ptr = 0;
1451 if (func)
1452 tok_expect(')');
1453 if (tok_see() == '(') {
1454 struct name args[MAXARGS];
1455 int nargs = readargs(args);
1456 int fdef = !func;
1457 if (!func) {
1458 ret = type;
1459 type = &tpool[npool++];
1460 func = type;
1462 func->flags = T_FUNC;
1463 func->bt = LONGSZ;
1464 func->id = func_create(ret, args, nargs);
1465 if (fdef && tok_see() == '{') {
1466 funcdef(name, func, args, nargs, flags);
1467 return 1;
1470 memcpy(main, type, sizeof(*type));
1471 return 0;
1474 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
1475 void *data)
1477 struct type base;
1478 unsigned flags;
1479 if (basetype(&base, &flags))
1480 return 1;
1481 while (tok_see() != ';' && tok_see() != '{') {
1482 struct name name;
1483 name.unused = 0;
1484 if (readname(&name.type, name.name, &base, flags))
1485 break;
1486 if (!tok_jmp('='))
1487 flags |= F_INIT;
1488 def(data, &name, flags);
1489 tok_jmp(',');
1491 return 0;
1494 static void typedefdef(void *data, struct name *name, unsigned flags)
1496 typedef_add(name->name, &name->type);
1499 static void readstmt(void);
1501 #define MAXCASES (1 << 7)
1503 static void readswitch(void)
1505 int break_beg = nbreaks;
1506 long val_addr = o_mklocal(LONGSZ);
1507 long matched[MAXCASES];
1508 int nmatched = 0;
1509 struct type t;
1510 long next;
1511 int ref = 1;
1512 int i;
1513 tok_expect('(');
1514 readexpr();
1515 ts_pop(&t);
1516 o_local(val_addr, TYPE_BT(&t));
1517 o_tmpswap();
1518 o_assign(TYPE_BT(&t));
1519 o_tmpdrop(1);
1520 tok_expect(')');
1521 tok_expect('{');
1522 while (tok_jmp('}')) {
1523 int n = 0;
1524 while (tok_see() == TOK_CASE || tok_see() == TOK_DEFAULT) {
1525 if (n++ > 0)
1526 matched[nmatched++] = o_jmp(0);
1527 if (!ref++)
1528 o_filljmp(next);
1529 if (!tok_jmp(TOK_CASE)) {
1530 caseexpr = 1;
1531 readexpr();
1532 caseexpr = 0;
1533 o_local(val_addr, TYPE_BT(&t));
1534 o_bop(O_EQ);
1535 next = o_jz(0);
1536 ref = 0;
1537 tok_expect(':');
1538 o_tmpdrop(1);
1539 ts_pop(NULL);
1540 continue;
1542 if (!tok_jmp(TOK_DEFAULT)) {
1543 tok_expect(':');
1544 continue;
1547 for (i = 0; i < nmatched; i++)
1548 o_filljmp(matched[i]);
1549 nmatched = 0;
1550 readstmt();
1552 o_rmlocal(val_addr, LONGSZ);
1553 if (!ref++)
1554 o_filljmp(next);
1555 break_fill(o_mklabel(), break_beg);
1558 #define MAXGOTO (1 << 10)
1560 static struct gotoinfo {
1561 char name[NAMELEN];
1562 long addr;
1563 } gotos[MAXGOTO];
1564 static int ngotos;
1566 static struct labelinfo {
1567 char name[NAMELEN];
1568 long addr;
1569 } labels[MAXGOTO];
1570 static int nlabels;
1572 static void goto_add(char *name)
1574 strcpy(gotos[ngotos].name, name);
1575 gotos[ngotos++].addr = o_jmp(0);
1578 static void label_add(char *name)
1580 strcpy(labels[nlabels].name, name);
1581 labels[nlabels++].addr = o_mklabel();
1584 static void goto_fill(void)
1586 int i, j;
1587 for (i = 0; i < ngotos; i++)
1588 for (j = 0; j < nlabels; j++)
1589 if (!strcmp(gotos[i].name, labels[j].name)) {
1590 o_filljmp2(gotos[i].addr, labels[j].addr);
1591 break;
1595 static void readstmt(void)
1597 o_tmpdrop(-1);
1598 nts = 0;
1599 if (!tok_jmp('{')) {
1600 int _nlocals = nlocals;
1601 int _nglobals = nglobals;
1602 int _nenums = nenums;
1603 int _ntypedefs = ntypedefs;
1604 int _nstructs = nstructs;
1605 int _nfuncs = nfuncs;
1606 int _narrays = narrays;
1607 while (tok_jmp('}'))
1608 readstmt();
1609 nlocals = _nlocals;
1610 nenums = _nenums;
1611 ntypedefs = _ntypedefs;
1612 nstructs = _nstructs;
1613 nfuncs = _nfuncs;
1614 narrays = _narrays;
1615 nglobals = _nglobals;
1616 return;
1618 if (!readdefs(localdef, NULL)) {
1619 tok_expect(';');
1620 return;
1622 if (!tok_jmp(TOK_TYPEDEF)) {
1623 readdefs(typedefdef, NULL);
1624 tok_expect(';');
1625 return;
1627 if (!tok_jmp(TOK_IF)) {
1628 long l1, l2;
1629 tok_expect('(');
1630 readexpr();
1631 tok_expect(')');
1632 l1 = o_jz(0);
1633 readstmt();
1634 if (!tok_jmp(TOK_ELSE)) {
1635 l2 = o_jmp(0);
1636 o_filljmp(l1);
1637 readstmt();
1638 o_filljmp(l2);
1639 } else {
1640 o_filljmp(l1);
1642 return;
1644 if (!tok_jmp(TOK_WHILE)) {
1645 long l1, l2;
1646 int break_beg = nbreaks;
1647 int continue_beg = ncontinues;
1648 l1 = o_mklabel();
1649 tok_expect('(');
1650 readexpr();
1651 tok_expect(')');
1652 l2 = o_jz(0);
1653 readstmt();
1654 o_jmp(l1);
1655 o_filljmp(l2);
1656 break_fill(o_mklabel(), break_beg);
1657 continue_fill(l1, continue_beg);
1658 return;
1660 if (!tok_jmp(TOK_DO)) {
1661 long l1, l2;
1662 int break_beg = nbreaks;
1663 int continue_beg = ncontinues;
1664 l1 = o_mklabel();
1665 readstmt();
1666 tok_expect(TOK_WHILE);
1667 tok_expect('(');
1668 l2 = o_mklabel();
1669 readexpr();
1670 o_jnz(l1);
1671 tok_expect(')');
1672 break_fill(o_mklabel(), break_beg);
1673 continue_fill(l2, continue_beg);
1674 return;
1676 if (!tok_jmp(TOK_FOR)) {
1677 long l_check, l_jump, j_fail, j_pass;
1678 int break_beg = nbreaks;
1679 int continue_beg = ncontinues;
1680 int has_cond = 0;
1681 tok_expect('(');
1682 if (tok_see() != ';')
1683 readestmt();
1684 tok_expect(';');
1685 l_check = o_mklabel();
1686 if (tok_see() != ';') {
1687 readestmt();
1688 j_fail = o_jz(0);
1689 has_cond = 1;
1691 tok_expect(';');
1692 j_pass = o_jmp(0);
1693 l_jump = o_mklabel();
1694 if (tok_see() != ')')
1695 readestmt();
1696 tok_expect(')');
1697 o_jmp(l_check);
1698 o_filljmp(j_pass);
1699 readstmt();
1700 o_jmp(l_jump);
1701 if (has_cond)
1702 o_filljmp(j_fail);
1703 break_fill(o_mklabel(), break_beg);
1704 continue_fill(l_jump, continue_beg);
1705 return;
1707 if (!tok_jmp(TOK_SWITCH)) {
1708 readswitch();
1709 return;
1711 if (!tok_jmp(TOK_RETURN)) {
1712 int ret = tok_see() != ';';
1713 if (ret)
1714 readexpr();
1715 tok_expect(';');
1716 o_ret(ret_bt);
1717 return;
1719 if (!tok_jmp(TOK_BREAK)) {
1720 tok_expect(';');
1721 breaks[nbreaks++] = o_jmp(0);
1722 return;
1724 if (!tok_jmp(TOK_CONTINUE)) {
1725 tok_expect(';');
1726 continues[ncontinues++] = o_jmp(0);
1727 return;
1729 if (!tok_jmp(TOK_GOTO)) {
1730 tok_expect(TOK_NAME);
1731 goto_add(tok_id());
1732 tok_expect(';');
1733 return;
1735 readestmt();
1736 /* labels */
1737 if (!tok_jmp(':')) {
1738 label_add(tok_id());
1739 return;
1741 tok_expect(';');
1744 static void readdecl(void)
1746 if (!tok_jmp(TOK_TYPEDEF)) {
1747 readdefs(typedefdef, NULL);
1748 tok_expect(';');
1749 return;
1751 readdefs(globaldef, NULL);
1752 if (tok_see() == '{') {
1753 readstmt();
1754 goto_fill();
1755 o_func_end();
1756 nlocals = 0;
1757 ngotos = 0;
1758 nlabels = 0;
1759 return;
1761 tok_expect(';');
1764 static void parse(void)
1766 while (tok_see() != TOK_EOF)
1767 readdecl();
1770 int main(int argc, char *argv[])
1772 char obj[128];
1773 int ofd;
1774 int i = 1;
1775 while (i < argc && argv[i][0] == '-') {
1776 if (argv[i][1] == 'I')
1777 cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]);
1778 if (argv[i][1] == 'D') {
1779 char *name = argv[i] + 2;
1780 char *def = "";
1781 char *eq = strchr(name, '=');
1782 if (eq) {
1783 *eq = '\0';
1784 def = eq + 1;
1786 cpp_define(name, def);
1788 i++;
1790 if (i == argc)
1791 die("neatcc: no file given\n");
1792 if (cpp_init(argv[i]))
1793 die("neatcc: cannot open input file\n");
1794 parse();
1795 strcpy(obj, argv[i]);
1796 obj[strlen(obj) - 1] = 'o';
1797 ofd = open(obj, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1798 out_write(ofd);
1799 close(ofd);
1800 return 0;