ncc: don't overwrite global names by static function variables
[neatcc.git] / ncc.c
blob6f88a9029d43cc731b9002361035711311b07e6f
1 /*
2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2011 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
7 */
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include "gen.h"
16 #include "tok.h"
17 #include "out.h"
19 static int nogen; /* don't generate code */
20 /* nogen macros */
21 #define o_bop(op) {if (!nogen) o_bop(op);}
22 #define o_uop(op) {if (!nogen) o_uop(op);}
23 #define o_cast(bt) {if (!nogen) o_cast(bt);}
24 #define o_memcpy() {if (!nogen) o_memcpy();}
25 #define o_memset() {if (!nogen) o_memset();}
26 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
27 #define o_ret(ret) {if (!nogen) o_ret(ret);}
28 #define o_assign(bt) {if (!nogen) o_assign(bt);}
29 #define o_deref(bt) {if (!nogen) o_deref(bt);}
30 #define o_load() {if (!nogen) o_load();}
31 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
32 #define o_num(n) {if (!nogen) o_num(n);}
33 #define o_local(addr) {if (!nogen) o_local(addr);}
34 #define o_sym(sym) {if (!nogen) o_sym(sym);}
35 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
36 #define o_tmpswap() {if (!nogen) o_tmpswap();}
37 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
38 #define o_mklabel() (nogen ? 0 : o_mklabel())
39 #define o_jz(addr) (nogen ? 0 : o_jz(addr))
40 #define o_jnz(addr) (nogen ? 0 : o_jnz(addr))
41 #define o_jmp(addr) (nogen ? 0 : o_jmp(addr))
42 #define o_filljmp(addr) {if (!nogen) o_filljmp(addr);}
43 #define o_filljmp2(addr, dst) {if (!nogen) o_filljmp2(addr, dst);}
44 #define o_fork() {if (!nogen) o_fork();}
45 #define o_forkpush() {if (!nogen) o_forkpush();}
46 #define o_forkjoin() {if (!nogen) o_forkjoin();}
48 #define MAXLOCALS (1 << 10)
49 #define MAXGLOBALS (1 << 10)
50 #define MAXARGS (1 << 5)
52 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
53 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
55 #define T_ARRAY 0x01
56 #define T_STRUCT 0x02
57 #define T_FUNC 0x04
59 #define F_INIT 0x01
60 #define F_STATIC 0x02
61 #define F_EXTERN 0x04
63 struct type {
64 unsigned bt;
65 unsigned flags;
66 int ptr;
67 int id; /* for structs, functions and arrays */
68 int addr; /* the address is passed to gen.c; deref for value */
71 /* type stack */
72 static struct type ts[MAXTMP];
73 static int nts;
75 static void ts_push_bt(unsigned bt)
77 ts[nts].ptr = 0;
78 ts[nts].flags = 0;
79 ts[nts].addr = 0;
80 ts[nts++].bt = bt;
83 static void ts_push(struct type *t)
85 struct type *d = &ts[nts++];
86 memcpy(d, t, sizeof(*t));
89 static void ts_push_addr(struct type *t)
91 ts_push(t);
92 ts[nts - 1].addr = 1;
95 static void ts_pop(struct type *type)
97 nts--;
98 if (type)
99 *type = ts[nts];
102 void err(char *msg)
104 die("%s: %s", cpp_loc(tok_addr()), msg);
107 struct name {
108 char name[NAMELEN];
109 char elfname[NAMELEN]; /* local elf name for static variables in function */
110 struct type type;
111 long addr; /* local stack offset, global data addr, struct offset */
114 static struct name locals[MAXLOCALS];
115 static int nlocals;
116 static struct name globals[MAXGLOBALS];
117 static int nglobals;
119 static void local_add(struct name *name)
121 if (nlocals >= MAXLOCALS)
122 err("nomem: MAXLOCALS reached!\n");
123 memcpy(&locals[nlocals++], name, sizeof(*name));
126 static int local_find(char *name)
128 int i;
129 for (i = nlocals - 1; i >= 0; --i)
130 if (!strcmp(locals[i].name, name))
131 return i;
132 return -1;
135 static int global_find(char *name)
137 int i;
138 for (i = nglobals - 1; i >= 0; i--)
139 if (!strcmp(name, globals[i].name))
140 return i;
141 return -1;
144 static void global_add(struct name *name)
146 if (nglobals >= MAXGLOBALS)
147 err("nomem: MAXGLOBALS reached!\n");
148 memcpy(&globals[nglobals++], name, sizeof(*name));
151 #define MAXENUMS (1 << 10)
153 static struct enumval {
154 char name[NAMELEN];
155 int n;
156 } enums[MAXENUMS];
157 static int nenums;
159 static void enum_add(char *name, int val)
161 struct enumval *ev = &enums[nenums++];
162 if (nenums >= MAXENUMS)
163 err("nomem: MAXENUMS reached!\n");
164 strcpy(ev->name, name);
165 ev->n = val;
168 static int enum_find(int *val, char *name)
170 int i;
171 for (i = nenums - 1; i >= 0; --i)
172 if (!strcmp(name, enums[i].name)) {
173 *val = enums[i].n;
174 return 0;
176 return 1;
179 #define MAXTYPEDEFS (1 << 10)
181 static struct typdefinfo {
182 char name[NAMELEN];
183 struct type type;
184 } typedefs[MAXTYPEDEFS];
185 static int ntypedefs;
187 static void typedef_add(char *name, struct type *type)
189 struct typdefinfo *ti = &typedefs[ntypedefs++];
190 if (ntypedefs >= MAXTYPEDEFS)
191 err("nomem: MAXTYPEDEFS reached!\n");
192 strcpy(ti->name, name);
193 memcpy(&ti->type, type, sizeof(*type));
196 static int typedef_find(char *name)
198 int i;
199 for (i = ntypedefs - 1; i >= 0; --i)
200 if (!strcmp(name, typedefs[i].name))
201 return i;
202 return -1;
205 #define MAXARRAYS (1 << 10)
207 static struct array {
208 struct type type;
209 int n;
210 } arrays[MAXARRAYS];
211 static int narrays;
213 static int array_add(struct type *type, int n)
215 struct array *a = &arrays[narrays++];
216 if (narrays >= MAXARRAYS)
217 err("nomem: MAXARRAYS reached!\n");
218 memcpy(&a->type, type, sizeof(*type));
219 a->n = n;
220 return a - arrays;
223 static void array2ptr(struct type *t)
225 if (t->flags & T_ARRAY && !t->ptr) {
226 memcpy(t, &arrays[t->id].type, sizeof(*t));
227 t->ptr++;
231 #define MAXSTRUCTS (1 << 10)
232 #define MAXFIELDS (1 << 7)
234 static struct structinfo {
235 char name[NAMELEN];
236 struct name fields[MAXFIELDS];
237 int nfields;
238 int isunion;
239 int size;
240 } structs[MAXSTRUCTS];
241 static int nstructs;
243 static int struct_find(char *name, int isunion)
245 int i;
246 for (i = nstructs - 1; i >= 0; --i)
247 if (*structs[i].name && !strcmp(name, structs[i].name) &&
248 structs[i].isunion == isunion)
249 return i;
250 i = nstructs++;
251 if (nstructs >= MAXSTRUCTS)
252 err("nomem: MAXTYPES reached!\n");
253 memset(&structs[i], 0, sizeof(structs[i]));
254 strcpy(structs[i].name, name);
255 structs[i].isunion = isunion;
256 return i;
259 static struct name *struct_field(int id, char *name)
261 struct structinfo *si = &structs[id];
262 int i;
263 for (i = 0; i < si->nfields; i++)
264 if (!strcmp(name, si->fields[i].name))
265 return &si->fields[i];
266 err("field not found\n");
269 #define MAXBREAK (1 << 7)
271 static long breaks[MAXBREAK];
272 static int nbreaks;
273 static long continues[MAXBREAK];
274 static int ncontinues;
276 static void break_fill(long addr, int till)
278 int i;
279 for (i = till; i < nbreaks; i++)
280 o_filljmp2(breaks[i], addr);
281 nbreaks = till;
284 static void continue_fill(long addr, int till)
286 int i;
287 for (i = till; i < ncontinues; i++)
288 o_filljmp2(continues[i], addr);
289 ncontinues = till;
292 static int type_totsz(struct type *t)
294 if (t->ptr)
295 return LONGSZ;
296 if (t->flags & T_ARRAY)
297 return arrays[t->id].n * type_totsz(&arrays[t->id].type);
298 return t->flags & T_STRUCT ? structs[t->id].size : BT_SZ(t->bt);
301 static unsigned type_szde(struct type *t)
303 struct type de = *t;
304 array2ptr(&de);
305 de.ptr--;
306 return type_totsz(&de);
309 static void ts_de(int deref)
311 struct type *t = &ts[nts - 1];
312 if (deref && t->addr && (!(t->flags & T_ARRAY) || (t->ptr)))
313 o_deref(TYPE_BT(t));
314 t->addr = 0;
317 static void ts_pop_de(struct type *t)
319 struct type de;
320 if (!t)
321 t = &de;
322 ts_pop(t);
323 array2ptr(t);
324 if (t->addr && (t->ptr || !(t->flags & T_FUNC)))
325 o_deref(TYPE_BT(t));
326 t->addr = 0;
329 static void ts_pop_de2(struct type *t1, struct type *t2)
331 ts_pop_de(t1);
332 o_tmpswap();
333 ts_pop_de(t2);
334 o_tmpswap();
337 static int tok_jmp(int tok)
339 if (tok_see() != tok)
340 return 1;
341 tok_get();
342 return 0;
345 static void tok_expect(int tok)
347 if (tok_get() != tok)
348 err("syntax error\n");
351 static unsigned bt_op(unsigned bt1, unsigned bt2)
353 unsigned s1 = BT_SZ(bt1);
354 unsigned s2 = BT_SZ(bt2);
355 return (bt1 | bt2) & BT_SIGNED | (s1 > s2 ? s1 : s2);
358 static void ts_binop(int op)
360 struct type t1, t2;
361 int bt;
362 ts_pop_de2(&t1, &t2);
363 if (op == O_DIV || op == O_MOD)
364 bt = TYPE_BT(&t2);
365 else
366 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
367 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
368 ts_push_bt(bt);
371 static void ts_addop(int op)
373 struct type t1, t2;
374 ts_pop_de2(&t1, &t2);
375 if (!t1.ptr && !t2.ptr) {
376 o_bop(op);
377 ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
378 return;
380 if (t1.ptr && !t2.ptr)
381 o_tmpswap();
382 if (!t1.ptr && t2.ptr)
383 if (type_szde(&t2) > 1) {
384 o_num(type_szde(&t2));
385 o_bop(O_MUL);
387 if (t1.ptr && !t2.ptr)
388 o_tmpswap();
389 o_bop(op);
390 if (t1.ptr && t2.ptr) {
391 int sz = type_szde(&t1);
392 if (sz > 1) {
393 o_num(sz);
394 o_bop(O_DIV);
396 ts_push_bt(4 | BT_SIGNED);
397 } else {
398 ts_push(t1.ptr ? &t1 : &t2);
402 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
403 #define MIN(a, b) ((a) < (b) ? (a) : (b))
405 static int type_alignment(struct type *t)
407 if (t->flags & T_ARRAY && !t->ptr)
408 return type_alignment(&arrays[t->id].type);
409 if (t->flags & T_STRUCT && !t->ptr)
410 return type_alignment(&structs[t->id].fields[0].type);
411 return MIN(LONGSZ, type_totsz(t));
414 static void structdef(void *data, struct name *name, unsigned flags)
416 struct structinfo *si = data;
417 if (si->isunion) {
418 name->addr = 0;
419 if (si->size < type_totsz(&name->type))
420 si->size = type_totsz(&name->type);
421 } else {
422 struct type *t = &name->type;
423 int alignment = type_alignment(t);
424 if (t->flags & T_ARRAY && !t->ptr)
425 alignment = MIN(LONGSZ, type_totsz(&arrays[t->id].type));
426 si->size = ALIGN(si->size, alignment);
427 name->addr = si->size;
428 si->size += type_totsz(&name->type);
430 memcpy(&si->fields[si->nfields++], name, sizeof(*name));
433 static int readdefs(void (*def)(void *, struct name *, unsigned f), void *data);
435 static int struct_create(char *name, int isunion)
437 int id = struct_find(name, isunion);
438 struct structinfo *si = &structs[id];
439 tok_expect('{');
440 while (tok_jmp('}')) {
441 readdefs(structdef, si);
442 tok_expect(';');
444 return id;
447 static void readexpr(void);
449 static void enum_create(void)
451 long n = 0;
452 tok_expect('{');
453 while (tok_jmp('}')) {
454 char name[NAMELEN];
455 tok_expect(TOK_NAME);
456 strcpy(name, tok_id());
457 if (!tok_jmp('=')) {
458 readexpr();
459 ts_pop_de(NULL);
460 if (o_popnum(&n))
461 err("const expr expected!\n");
463 enum_add(name, n++);
464 tok_jmp(',');
468 static int basetype(struct type *type, unsigned *flags)
470 int sign = 1;
471 int size = 4;
472 int done = 0;
473 int i = 0;
474 int isunion;
475 char name[NAMELEN] = "";
476 *flags = 0;
477 type->flags = 0;
478 type->ptr = 0;
479 type->addr = 0;
480 while (!done) {
481 switch (tok_see()) {
482 case TOK_STATIC:
483 *flags |= F_STATIC;
484 break;
485 case TOK_EXTERN:
486 *flags |= F_EXTERN;
487 break;
488 case TOK_VOID:
489 sign = 0;
490 size = 0;
491 done = 1;
492 break;
493 case TOK_INT:
494 done = 1;
495 break;
496 case TOK_CHAR:
497 size = 1;
498 done = 1;
499 break;
500 case TOK_SHORT:
501 size = 2;
502 break;
503 case TOK_LONG:
504 size = LONGSZ;
505 break;
506 case TOK_SIGNED:
507 break;
508 case TOK_UNSIGNED:
509 sign = 0;
510 break;
511 case TOK_UNION:
512 case TOK_STRUCT:
513 isunion = tok_get() == TOK_UNION;
514 if (!tok_jmp(TOK_NAME))
515 strcpy(name, tok_id());
516 if (tok_see() == '{')
517 type->id = struct_create(name, isunion);
518 else
519 type->id = struct_find(name, isunion);
520 type->flags |= T_STRUCT;
521 type->bt = LONGSZ;
522 return 0;
523 case TOK_ENUM:
524 tok_get();
525 tok_jmp(TOK_NAME);
526 if (tok_see() == '{')
527 enum_create();
528 type->bt = 4 | BT_SIGNED;
529 return 0;
530 default:
531 if (tok_see() == TOK_NAME) {
532 int id = typedef_find(tok_id());
533 if (id != -1) {
534 tok_get();
535 memcpy(type, &typedefs[id].type,
536 sizeof(*type));
537 return 0;
540 if (!i)
541 return 1;
542 done = 1;
543 continue;
545 i++;
546 tok_get();
548 type->bt = size | (sign ? BT_SIGNED : 0);
549 return 0;
552 static int readname(struct type *main, char *name,
553 struct type *base, unsigned flags);
555 static int readtype(struct type *type)
557 return readname(type, NULL, NULL, 0);
560 static void readptrs(struct type *type)
562 while (!tok_jmp('*')) {
563 type->ptr++;
564 if (!type->bt)
565 type->bt = 1;
569 /* used to differenciate labels from case and cond exprs */
570 static int ncexpr;
571 static int caseexpr;
573 static void readpre(void);
575 static char *tmp_str(char *buf, int len)
577 static char name[NAMELEN];
578 static int id;
579 void *dat;
580 sprintf(name, "__neatcc.s%d", id++);
581 dat = o_mkdat(name, len, 0);
582 memcpy(dat, buf, len);
583 return name;
586 static void readprimary(void)
588 int i;
589 if (!tok_jmp(TOK_NUM)) {
590 long n;
591 int bt = tok_num(&n);
592 ts_push_bt(bt);
593 o_num(n);
594 return;
596 if (!tok_jmp(TOK_STR)) {
597 struct type t;
598 char buf[BUFSIZE];
599 int len;
600 t.bt = 1 | BT_SIGNED;
601 t.ptr = 1;
602 t.addr = 0;
603 t.flags = 0;
604 ts_push(&t);
605 len = tok_str(buf);
606 o_sym(tmp_str(buf, len));
607 return;
609 if (!tok_jmp(TOK_NAME)) {
610 struct name unkn = {""};
611 char *name = unkn.name;
612 int n;
613 strcpy(name, tok_id());
614 /* don't search for labels here */
615 if (!ncexpr && !caseexpr && tok_see() == ':')
616 return;
617 if ((n = local_find(name)) != -1) {
618 struct name *l = &locals[n];
619 o_local(l->addr);
620 ts_push_addr(&l->type);
621 return;
623 if ((n = global_find(name)) != -1) {
624 struct name *g = &globals[n];
625 o_sym(*g->elfname ? g->elfname : g->name);
626 ts_push_addr(&g->type);
627 return;
629 if (!enum_find(&n, name)) {
630 ts_push_bt(4 | BT_SIGNED);
631 o_num(n);
632 return;
634 if (tok_see() != '(')
635 err("unknown symbol\n");
636 global_add(&unkn);
637 ts_push_bt(LONGSZ);
638 o_sym(unkn.name);
639 return;
641 if (!tok_jmp('(')) {
642 struct type t;
643 if (!readtype(&t)) {
644 struct type o;
645 tok_expect(')');
646 readpre();
647 ts_pop_de(&o);
648 ts_push(&t);
649 if (!t.ptr || !o.ptr)
650 o_cast(TYPE_BT(&t));
651 } else {
652 readexpr();
653 tok_expect(')');
655 return;
659 static void arrayderef(void)
661 struct type t;
662 int sz;
663 ts_pop_de(NULL);
664 ts_pop(&t);
665 if (!(t.flags & T_ARRAY) && t.addr) {
666 o_tmpswap();
667 o_deref(TYPE_BT(&t));
668 o_tmpswap();
670 array2ptr(&t);
671 t.ptr--;
672 sz = type_totsz(&t);
673 t.addr = 1;
674 if (sz > 1) {
675 o_num(sz);
676 o_bop(O_MUL);
678 o_bop(O_ADD);
679 ts_push(&t);
682 static void inc_post(int op)
684 struct type t = ts[nts - 1];
685 /* pushing the value before inc */
686 o_tmpcopy();
687 ts_de(1);
688 o_load();
689 o_tmpswap();
691 /* increment by 1 or pointer size */
692 o_tmpcopy();
693 ts_push(&t);
694 ts_pop_de(&t);
695 o_num(t.ptr > 0 ? type_szde(&t) : 1);
696 o_bop(op);
698 /* assign back */
699 o_assign(TYPE_BT(&t));
700 o_tmpdrop(1);
703 static void readfield(void)
705 struct name *field;
706 struct type t;
707 tok_expect(TOK_NAME);
708 ts_pop(&t);
709 array2ptr(&t);
710 field = struct_field(t.id, tok_id());
711 if (field->addr) {
712 o_num(field->addr);
713 o_bop(O_ADD);
715 ts_push_addr(&field->type);
718 #define MAXFUNCS (1 << 10)
720 static struct funcinfo {
721 struct type args[MAXFIELDS];
722 struct type ret;
723 int nargs;
724 int varg;
725 } funcs[MAXFUNCS];
726 static int nfuncs;
728 static int func_create(struct type *ret, struct name *args, int nargs)
730 struct funcinfo *fi = &funcs[nfuncs++];
731 int i;
732 if (nfuncs >= MAXFUNCS)
733 err("nomem: MAXFUNCS reached!\n");
734 memcpy(&fi->ret, ret, sizeof(*ret));
735 for (i = 0; i < nargs; i++)
736 memcpy(&fi->args[i], &args[i].type, sizeof(*ret));
737 fi->nargs = nargs;
738 return fi - funcs;
741 static void readcall(void)
743 struct type t;
744 struct funcinfo *fi;
745 int argc = 0;
746 ts_pop(&t);
747 if (t.flags & T_FUNC && t.ptr > 0)
748 o_deref(LONGSZ);
749 fi = t.flags & T_FUNC ? &funcs[t.id] : NULL;
750 if (tok_see() != ')') {
751 do {
752 readexpr();
753 ts_pop_de(NULL);
754 argc++;
755 } while (!tok_jmp(','));
757 tok_expect(')');
758 o_call(argc, fi ? TYPE_BT(&fi->ret) : 4 | BT_SIGNED);
759 if (fi) {
760 if (TYPE_BT(&fi->ret))
761 o_cast(TYPE_BT(&fi->ret));
762 ts_push(&fi->ret);
763 } else {
764 ts_push_bt(4 | BT_SIGNED);
768 static void readpost(void)
770 readprimary();
771 while (1) {
772 if (!tok_jmp('[')) {
773 readexpr();
774 tok_expect(']');
775 arrayderef();
776 continue;
778 if (!tok_jmp('(')) {
779 readcall();
780 continue;
782 if (!tok_jmp(TOK2("++"))) {
783 inc_post(O_ADD);
784 continue;
786 if (!tok_jmp(TOK2("--"))) {
787 inc_post(O_SUB);
788 continue;
790 if (!tok_jmp('.')) {
791 readfield();
792 continue;
794 if (!tok_jmp(TOK2("->"))) {
795 ts_de(1);
796 readfield();
797 continue;
799 break;
803 static void inc_pre(int op)
805 struct type t;
806 readpre();
807 /* copy the destination */
808 o_tmpcopy();
809 ts_push(&ts[nts - 1]);
810 /* increment by 1 or pointer size */
811 ts_pop_de(&t);
812 o_num(t.ptr > 0 ? type_szde(&t) : 1);
813 o_bop(op);
814 /* assign the result */
815 o_assign(TYPE_BT(&t));
816 ts_de(0);
819 static void readpre(void)
821 if (!tok_jmp('&')) {
822 struct type type;
823 readpre();
824 ts_pop(&type);
825 if (!type.addr)
826 err("cannot use the address\n");
827 type.ptr++;
828 type.addr = 0;
829 ts_push(&type);
830 return;
832 if (!tok_jmp('*')) {
833 struct type t;
834 readpre();
835 ts_pop(&t);
836 array2ptr(&t);
837 if (!t.ptr)
838 err("dereferencing non-pointer\n");
839 if (t.addr)
840 o_deref(TYPE_BT(&t));
841 t.ptr--;
842 t.addr = 1;
843 ts_push(&t);
844 return;
846 if (!tok_jmp('!')) {
847 readpre();
848 ts_pop_de(NULL);
849 o_uop(O_LNOT);
850 ts_push_bt(4 | BT_SIGNED);
851 return;
853 if (!tok_jmp('-')) {
854 readpre();
855 ts_de(1);
856 o_uop(O_NEG);
857 return;
859 if (!tok_jmp('~')) {
860 readpre();
861 ts_de(1);
862 o_uop(O_NOT);
863 return;
865 if (!tok_jmp(TOK2("++"))) {
866 inc_pre(O_ADD);
867 return;
869 if (!tok_jmp(TOK2("--"))) {
870 inc_pre(O_SUB);
871 return;
873 if (!tok_jmp(TOK_SIZEOF)) {
874 struct type t;
875 int op = !tok_jmp('(');
876 if (readtype(&t)) {
877 nogen++;
878 if (op)
879 readexpr();
880 else
881 readpre();
882 nogen--;
883 ts_pop(&t);
885 ts_push_bt(4);
886 o_num(type_totsz(&t));
887 if (op)
888 tok_expect(')');
889 return;
891 readpost();
894 static void readmul(void)
896 readpre();
897 while (1) {
898 if (!tok_jmp('*')) {
899 readpre();
900 ts_binop(O_MUL);
901 continue;
903 if (!tok_jmp('/')) {
904 readpre();
905 ts_binop(O_DIV);
906 continue;
908 if (!tok_jmp('%')) {
909 readpre();
910 ts_binop(O_MOD);
911 continue;
913 break;
917 static void readadd(void)
919 readmul();
920 while (1) {
921 if (!tok_jmp('+')) {
922 readmul();
923 ts_addop(O_ADD);
924 continue;
926 if (!tok_jmp('-')) {
927 readmul();
928 ts_addop(O_SUB);
929 continue;
931 break;
935 static void shift(int op)
937 struct type t;
938 readadd();
939 ts_pop_de2(NULL, &t);
940 o_bop(op | (BT_SIGNED & TYPE_BT(&t) ? O_SIGNED : 0));
941 ts_push_bt(TYPE_BT(&t));
944 static void readshift(void)
946 readadd();
947 while (1) {
948 if (!tok_jmp(TOK2("<<"))) {
949 shift(O_SHL);
950 continue;
952 if (!tok_jmp(TOK2(">>"))) {
953 shift(O_SHR);
954 continue;
956 break;
960 static void cmp(int op)
962 struct type t1, t2;
963 int bt;
964 readshift();
965 ts_pop_de2(&t1, &t2);
966 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
967 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
968 ts_push_bt(4 | BT_SIGNED);
971 static void readcmp(void)
973 readshift();
974 while (1) {
975 if (!tok_jmp('<')) {
976 cmp(O_LT);
977 continue;
979 if (!tok_jmp('>')) {
980 cmp(O_GT);
981 continue;
983 if (!tok_jmp(TOK2("<="))) {
984 cmp(O_LE);
985 continue;
987 if (!tok_jmp(TOK2(">="))) {
988 cmp(O_GE);
989 continue;
991 break;
995 static void eq(int op)
997 readcmp();
998 ts_pop_de2(NULL, NULL);
999 o_bop(op);
1000 ts_push_bt(4 | BT_SIGNED);
1003 static void readeq(void)
1005 readcmp();
1006 while (1) {
1007 if (!tok_jmp(TOK2("=="))) {
1008 eq(O_EQ);
1009 continue;
1011 if (!tok_jmp(TOK2("!="))) {
1012 eq(O_NEQ);
1013 continue;
1015 break;
1019 static void readbitand(void)
1021 readeq();
1022 while (!tok_jmp('&')) {
1023 readeq();
1024 ts_binop(O_AND);
1028 static void readxor(void)
1030 readbitand();
1031 while (!tok_jmp('^')) {
1032 readbitand();
1033 ts_binop(O_XOR);
1037 static void readbitor(void)
1039 readxor();
1040 while (!tok_jmp('|')) {
1041 readxor();
1042 ts_binop(O_OR);
1046 #define MAXCOND (1 << 7)
1048 static void readand(void)
1050 long conds[MAXCOND];
1051 int nconds = 0;
1052 long passed;
1053 int i;
1054 readbitor();
1055 if (tok_see() != TOK2("&&"))
1056 return;
1057 o_fork();
1058 ts_pop_de(NULL);
1059 conds[nconds++] = o_jz(0);
1060 while (!tok_jmp(TOK2("&&"))) {
1061 readbitor();
1062 ts_pop_de(NULL);
1063 conds[nconds++] = o_jz(0);
1065 o_num(1);
1066 o_forkpush();
1067 passed = o_jmp(0);
1068 for (i = 0; i < nconds; i++)
1069 o_filljmp(conds[i]);
1070 o_num(0);
1071 o_forkpush();
1072 o_forkjoin();
1073 o_filljmp(passed);
1074 ts_push_bt(4 | BT_SIGNED);
1077 static void reador(void)
1079 long conds[MAXCOND];
1080 int nconds = 0;
1081 long failed;
1082 int i;
1083 readand();
1084 if (tok_see() != TOK2("||"))
1085 return;
1086 o_fork();
1087 ts_pop_de(NULL);
1088 conds[nconds++] = o_jnz(0);
1089 while (!tok_jmp(TOK2("||"))) {
1090 readand();
1091 ts_pop_de(NULL);
1092 conds[nconds++] = o_jnz(0);
1094 o_num(0);
1095 o_forkpush();
1096 failed = o_jmp(0);
1097 for (i = 0; i < nconds; i++)
1098 o_filljmp(conds[i]);
1099 o_num(1);
1100 o_forkpush();
1101 o_forkjoin();
1102 o_filljmp(failed);
1103 ts_push_bt(4 | BT_SIGNED);
1106 static void readcexpr(void);
1108 static int readcexpr_const(void)
1110 long c;
1111 if (o_popnum(&c))
1112 return -1;
1113 if (!c)
1114 nogen++;
1115 readcexpr();
1116 /* both branches yield the same type; so ignore the first */
1117 ts_pop_de(NULL);
1118 tok_expect(':');
1119 if (c)
1120 nogen++;
1121 else
1122 nogen--;
1123 readcexpr();
1124 /* making sure t->addr == 0 on both branches */
1125 ts_de(1);
1126 if (c)
1127 nogen--;
1128 return 0;
1131 static void readcexpr(void)
1133 long l1, l2;
1134 reador();
1135 if (tok_jmp('?'))
1136 return;
1137 ncexpr++;
1138 ts_pop_de(NULL);
1139 o_fork();
1140 if (readcexpr_const()) {
1141 l1 = o_jz(0);
1142 readcexpr();
1143 /* both branches yield the same type; so ignore the first */
1144 ts_pop_de(NULL);
1145 o_forkpush();
1146 l2 = o_jmp(0);
1148 tok_expect(':');
1149 o_filljmp(l1);
1150 readcexpr();
1151 /* making sure t->addr == 0 on both branches */
1152 ts_de(1);
1153 o_forkpush();
1154 o_forkjoin();
1155 o_filljmp(l2);
1157 ncexpr--;
1160 static void opassign(int op, int ptrop)
1162 struct type t = ts[nts - 1];
1163 o_tmpcopy();
1164 ts_push(&t);
1165 readexpr();
1166 ts_addop(op);
1167 o_assign(TYPE_BT(&ts[nts - 2]));
1168 ts_pop(NULL);
1169 ts_de(0);
1172 static void doassign(void)
1174 struct type t = ts[nts - 1];
1175 if (!t.ptr && t.flags & T_STRUCT) {
1176 ts_pop(NULL);
1177 o_num(type_totsz(&t));
1178 o_memcpy();
1179 } else {
1180 ts_pop_de(NULL);
1181 o_assign(TYPE_BT(&ts[nts - 1]));
1182 ts_de(0);
1186 static void readexpr(void)
1188 readcexpr();
1189 if (!tok_jmp('=')) {
1190 readexpr();
1191 doassign();
1192 return;
1194 if (!tok_jmp(TOK2("+="))) {
1195 opassign(O_ADD, 1);
1196 return;
1198 if (!tok_jmp(TOK2("-="))) {
1199 opassign(O_SUB, 1);
1200 return;
1202 if (!tok_jmp(TOK2("*="))) {
1203 opassign(O_MUL, 0);
1204 return;
1206 if (!tok_jmp(TOK2("/="))) {
1207 opassign(O_DIV, 0);
1208 return;
1210 if (!tok_jmp(TOK2("%="))) {
1211 opassign(O_MOD, 0);
1212 return;
1214 if (!tok_jmp(TOK3("<<="))) {
1215 opassign(O_SHL, 0);
1216 return;
1218 if (!tok_jmp(TOK3(">>="))) {
1219 opassign(O_SHR, 0);
1220 return;
1222 if (!tok_jmp(TOK3("&="))) {
1223 opassign(O_AND, 0);
1224 return;
1226 if (!tok_jmp(TOK3("|="))) {
1227 opassign(O_OR, 0);
1228 return;
1230 if (!tok_jmp(TOK3("^="))) {
1231 opassign(O_XOR, 0);
1232 return;
1236 static void readestmt(void)
1238 do {
1239 o_tmpdrop(-1);
1240 nts = 0;
1241 readexpr();
1242 } while (!tok_jmp(','));
1245 static void o_localoff(long addr, int off)
1247 o_local(addr);
1248 if (off) {
1249 o_num(off);
1250 o_bop(O_ADD);
1254 static struct type *innertype(struct type *t)
1256 if (t->flags & T_ARRAY && !t->ptr)
1257 return innertype(&arrays[t->id].type);
1258 return t;
1261 static void initexpr(struct type *t, int off, void *obj,
1262 void (*set)(void *obj, int off, struct type *t))
1264 if (tok_jmp('{')) {
1265 set(obj, off, t);
1266 return;
1268 if (!t->ptr && t->flags & T_STRUCT) {
1269 struct structinfo *si = &structs[t->id];
1270 int i;
1271 for (i = 0; i < si->nfields && tok_see() != '}'; i++) {
1272 struct name *field = &si->fields[i];
1273 if (!tok_jmp('.')) {
1274 tok_expect(TOK_NAME);
1275 field = struct_field(t->id, tok_id());
1276 tok_expect('=');
1278 initexpr(&field->type, off + field->addr, obj, set);
1279 if (tok_jmp(','))
1280 break;
1282 } else if (t->flags & T_ARRAY) {
1283 struct type *t_de = &arrays[t->id].type;
1284 int i;
1285 for (i = 0; tok_see() != '}'; i++) {
1286 long idx = i;
1287 struct type *it = t_de;
1288 if (!tok_jmp('[')) {
1289 readexpr();
1290 ts_pop_de(NULL);
1291 o_popnum(&idx);
1292 tok_expect(']');
1293 tok_expect('=');
1295 if (tok_see() != '{')
1296 it = innertype(t_de);
1297 initexpr(it, off + type_totsz(it) * idx, obj, set);
1298 if (tok_jmp(','))
1299 break;
1302 tok_expect('}');
1305 static void jumpbrace(void)
1307 int depth = 0;
1308 while (tok_see() != '}' || depth--)
1309 if (tok_get() == '{')
1310 depth++;
1311 tok_expect('}');
1314 static int initsize(void)
1316 long addr = tok_addr();
1317 int n = 0;
1318 if (!tok_jmp(TOK_STR)) {
1319 n = tok_str(NULL);
1320 tok_jump(addr);
1321 return n;
1323 tok_expect('{');
1324 while (tok_jmp('}')) {
1325 long idx = n;
1326 if (!tok_jmp('[')) {
1327 readexpr();
1328 ts_pop_de(NULL);
1329 o_popnum(&idx);
1330 tok_expect(']');
1331 tok_expect('=');
1333 if (n < idx + 1)
1334 n = idx + 1;
1335 while (tok_see() != '}' && tok_see() != ',')
1336 if (tok_get() == '{')
1337 jumpbrace();
1338 tok_jmp(',');
1340 tok_jump(addr);
1341 return n;
1344 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1346 static void globalinit(void *obj, int off, struct type *t)
1348 struct name *name = obj;
1349 char *elfname = *name->elfname ? name->elfname : name->name;
1350 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1351 struct type *t_de = &arrays[t->id].type;
1352 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1353 char buf[BUFSIZE];
1354 int len;
1355 tok_expect(TOK_STR);
1356 len = tok_str(buf);
1357 memcpy((void *) name->addr + off, buf, len);
1358 return;
1361 readexpr();
1362 o_datset(elfname, off, TYPE_BT(t));
1363 ts_pop(NULL);
1366 static void globaldef(void *data, struct name *name, unsigned flags)
1368 struct type *t = &name->type;
1369 char *elfname = *name->elfname ? name->elfname : name->name;
1370 int sz;
1371 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1372 if (~flags & F_EXTERN)
1373 arrays[t->id].n = initsize();
1374 sz = type_totsz(t);
1375 if (!(flags & F_EXTERN) && (!(t->flags & T_FUNC) || t->ptr)) {
1376 if (flags & F_INIT)
1377 name->addr = (long) o_mkdat(elfname, sz, F_GLOBAL(flags));
1378 else
1379 o_mkbss(elfname, sz, F_GLOBAL(flags));
1381 global_add(name);
1382 if (flags & F_INIT)
1383 initexpr(t, 0, name, globalinit);
1386 static void localinit(void *obj, int off, struct type *t)
1388 long addr = *(long *) obj;
1389 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1390 struct type *t_de = &arrays[t->id].type;
1391 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1392 char buf[BUFSIZE];
1393 int len;
1394 tok_expect(TOK_STR);
1395 len = tok_str(buf);
1396 o_localoff(addr, off);
1397 o_sym(tmp_str(buf, len));
1398 o_num(len);
1399 o_memcpy();
1400 o_tmpdrop(1);
1401 return;
1404 o_localoff(addr, off);
1405 ts_push(t);
1406 readexpr();
1407 doassign();
1408 ts_pop(NULL);
1409 o_tmpdrop(1);
1412 /* current function name */
1413 static char func_name[NAMELEN];
1415 static void localdef(void *data, struct name *name, unsigned flags)
1417 struct type *t = &name->type;
1418 if ((flags & F_EXTERN) || (t->flags & T_FUNC) && !t->ptr) {
1419 global_add(name);
1420 return;
1422 if (flags & F_STATIC) {
1423 sprintf(name->elfname, "__neatcc.%s.%s", func_name, name->name);
1424 globaldef(data, name, flags);
1425 return;
1427 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1428 arrays[t->id].n = initsize();
1429 name->addr = o_mklocal(type_totsz(&name->type));
1430 local_add(name);
1431 if (flags & F_INIT) {
1432 if (t->flags & (T_ARRAY | T_STRUCT) && !t->ptr) {
1433 o_local(name->addr);
1434 o_num(0);
1435 o_num(type_totsz(t));
1436 o_memset();
1437 o_tmpdrop(1);
1439 initexpr(t, 0, &name->addr, localinit);
1443 static void funcdef(char *name, struct type *type, struct name *args,
1444 int nargs, int varg, unsigned flags)
1446 struct name global = {""};
1447 int i;
1448 strcpy(global.name, name);
1449 strcpy(func_name, name);
1450 memcpy(&global.type, type, sizeof(*type));
1451 o_func_beg(name, nargs, F_GLOBAL(flags), varg);
1452 global_add(&global);
1453 for (i = 0; i < nargs; i++) {
1454 args[i].addr = o_arg2loc(i);
1455 local_add(&args[i]);
1459 static int readargs(struct name *args, int *varg)
1461 int nargs = 0;
1462 tok_expect('(');
1463 *varg = 0;
1464 while (tok_see() != ')') {
1465 if (!tok_jmp(TOK3("..."))) {
1466 *varg = 1;
1467 break;
1469 readname(&args[nargs].type, args[nargs].name, NULL, 0);
1470 array2ptr(&args[nargs].type);
1471 nargs++;
1472 if (tok_jmp(','))
1473 break;
1475 tok_expect(')');
1476 if (nargs == 1 && !TYPE_BT(&args[0].type))
1477 return 0;
1478 return nargs;
1481 static int readname(struct type *main, char *name,
1482 struct type *base, unsigned flags)
1484 struct type tpool[3];
1485 int npool = 0;
1486 struct type *type = &tpool[npool++];
1487 struct type *func = NULL;
1488 struct type *ret = NULL;
1489 int arsz[10];
1490 int nar = 0;
1491 int i;
1492 memset(tpool, 0, sizeof(tpool));
1493 if (name)
1494 *name = '\0';
1495 if (!base) {
1496 if (basetype(type, &flags))
1497 return 1;
1498 } else {
1499 memcpy(type, base, sizeof(*base));
1501 readptrs(type);
1502 if (!tok_jmp('(')) {
1503 ret = type;
1504 type = &tpool[npool++];
1505 func = type;
1506 readptrs(type);
1508 if (!tok_jmp(TOK_NAME) && name)
1509 strcpy(name, tok_id());
1510 while (!tok_jmp('[')) {
1511 long n = 0;
1512 if (tok_jmp(']')) {
1513 readexpr();
1514 ts_pop_de(NULL);
1515 if (o_popnum(&n))
1516 err("const expr expected\n");
1517 tok_expect(']');
1519 arsz[nar++] = n;
1521 for (i = nar - 1; i >= 0; i--) {
1522 type->id = array_add(type, arsz[i]);
1523 if (func && i == nar - 1)
1524 func = &arrays[type->id].type;
1525 type->flags = T_ARRAY;
1526 type->bt = LONGSZ;
1527 type->ptr = 0;
1529 if (func)
1530 tok_expect(')');
1531 if (tok_see() == '(') {
1532 struct name args[MAXARGS] = {{""}};
1533 int varg = 0;
1534 int nargs = readargs(args, &varg);
1535 int fdef = !func;
1536 if (!func) {
1537 ret = type;
1538 type = &tpool[npool++];
1539 func = type;
1541 func->flags = T_FUNC;
1542 func->bt = LONGSZ;
1543 func->id = func_create(ret, args, nargs);
1544 if (fdef && tok_see() == '{') {
1545 funcdef(name, func, args, nargs, varg, flags);
1546 return 1;
1549 memcpy(main, type, sizeof(*type));
1550 return 0;
1553 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
1554 void *data)
1556 struct type base;
1557 unsigned base_flags;
1558 if (basetype(&base, &base_flags))
1559 return 1;
1560 while (tok_see() != ';' && tok_see() != '{') {
1561 struct name name = {{""}};
1562 unsigned flags = base_flags;
1563 if (readname(&name.type, name.name, &base, flags))
1564 break;
1565 if (!tok_jmp('='))
1566 flags |= F_INIT;
1567 def(data, &name, flags);
1568 tok_jmp(',');
1570 return 0;
1573 static void typedefdef(void *data, struct name *name, unsigned flags)
1575 typedef_add(name->name, &name->type);
1578 static void readstmt(void);
1580 #define MAXCASES (1 << 7)
1582 static void readswitch(void)
1584 int break_beg = nbreaks;
1585 long val_addr = o_mklocal(LONGSZ);
1586 struct type t;
1587 int ncases = 0; /* number of case labels */
1588 long last_failed = -1; /* address of last failed jmp */
1589 long last_matched = -1; /* address of last walk through jmp */
1590 long default_addr = -1; /* address of the default label */
1591 tok_expect('(');
1592 readexpr();
1593 ts_pop_de(&t);
1594 o_local(val_addr);
1595 o_tmpswap();
1596 o_assign(TYPE_BT(&t));
1597 ts_de(0);
1598 o_tmpdrop(1);
1599 tok_expect(')');
1600 tok_expect('{');
1601 while (tok_jmp('}')) {
1602 if (tok_see() != TOK_CASE && tok_see() != TOK_DEFAULT) {
1603 readstmt();
1604 continue;
1606 if (ncases)
1607 last_matched = o_jmp(0);
1608 if (tok_get() == TOK_CASE) {
1609 if (last_failed >= 0)
1610 o_filljmp(last_failed);
1611 caseexpr = 1;
1612 readexpr();
1613 ts_pop_de(NULL);
1614 caseexpr = 0;
1615 o_local(val_addr);
1616 o_deref(TYPE_BT(&t));
1617 o_bop(O_EQ);
1618 last_failed = o_jz(0);
1619 o_tmpdrop(1);
1620 } else {
1621 default_addr = o_mklabel();
1623 tok_expect(':');
1624 if (last_matched >= 0)
1625 o_filljmp(last_matched);
1626 ncases++;
1628 o_rmlocal(val_addr, LONGSZ);
1629 if (last_failed >= 0)
1630 o_filljmp2(last_failed,
1631 default_addr >= 0 ? default_addr : o_mklabel());
1632 break_fill(o_mklabel(), break_beg);
1635 #define MAXGOTO (1 << 10)
1637 static struct gotoinfo {
1638 char name[NAMELEN];
1639 long addr;
1640 } gotos[MAXGOTO];
1641 static int ngotos;
1643 static struct labelinfo {
1644 char name[NAMELEN];
1645 long addr;
1646 } labels[MAXGOTO];
1647 static int nlabels;
1649 static void goto_add(char *name)
1651 strcpy(gotos[ngotos].name, name);
1652 gotos[ngotos++].addr = o_jmp(0);
1655 static void label_add(char *name)
1657 strcpy(labels[nlabels].name, name);
1658 labels[nlabels++].addr = o_mklabel();
1661 static void goto_fill(void)
1663 int i, j;
1664 for (i = 0; i < ngotos; i++)
1665 for (j = 0; j < nlabels; j++)
1666 if (!strcmp(gotos[i].name, labels[j].name)) {
1667 o_filljmp2(gotos[i].addr, labels[j].addr);
1668 break;
1672 static void readstmt(void)
1674 o_tmpdrop(-1);
1675 nts = 0;
1676 if (!tok_jmp('{')) {
1677 int _nlocals = nlocals;
1678 int _nglobals = nglobals;
1679 int _nenums = nenums;
1680 int _ntypedefs = ntypedefs;
1681 int _nstructs = nstructs;
1682 int _nfuncs = nfuncs;
1683 int _narrays = narrays;
1684 while (tok_jmp('}'))
1685 readstmt();
1686 nlocals = _nlocals;
1687 nenums = _nenums;
1688 ntypedefs = _ntypedefs;
1689 nstructs = _nstructs;
1690 nfuncs = _nfuncs;
1691 narrays = _narrays;
1692 nglobals = _nglobals;
1693 return;
1695 if (!readdefs(localdef, NULL)) {
1696 tok_expect(';');
1697 return;
1699 if (!tok_jmp(TOK_TYPEDEF)) {
1700 readdefs(typedefdef, NULL);
1701 tok_expect(';');
1702 return;
1704 if (!tok_jmp(TOK_IF)) {
1705 long l1, l2;
1706 tok_expect('(');
1707 readexpr();
1708 tok_expect(')');
1709 ts_pop_de(NULL);
1710 l1 = o_jz(0);
1711 readstmt();
1712 if (!tok_jmp(TOK_ELSE)) {
1713 l2 = o_jmp(0);
1714 o_filljmp(l1);
1715 readstmt();
1716 o_filljmp(l2);
1717 } else {
1718 o_filljmp(l1);
1720 return;
1722 if (!tok_jmp(TOK_WHILE)) {
1723 long l1, l2;
1724 int break_beg = nbreaks;
1725 int continue_beg = ncontinues;
1726 l1 = o_mklabel();
1727 tok_expect('(');
1728 readexpr();
1729 tok_expect(')');
1730 ts_pop_de(NULL);
1731 l2 = o_jz(0);
1732 readstmt();
1733 o_jmp(l1);
1734 o_filljmp(l2);
1735 break_fill(o_mklabel(), break_beg);
1736 continue_fill(l1, continue_beg);
1737 return;
1739 if (!tok_jmp(TOK_DO)) {
1740 long l1, l2;
1741 int break_beg = nbreaks;
1742 int continue_beg = ncontinues;
1743 l1 = o_mklabel();
1744 readstmt();
1745 tok_expect(TOK_WHILE);
1746 tok_expect('(');
1747 l2 = o_mklabel();
1748 readexpr();
1749 ts_pop_de(NULL);
1750 o_jnz(l1);
1751 tok_expect(')');
1752 break_fill(o_mklabel(), break_beg);
1753 continue_fill(l2, continue_beg);
1754 tok_expect(';');
1755 return;
1757 if (!tok_jmp(TOK_FOR)) {
1758 long l_check, l_jump, j_fail, j_pass;
1759 int break_beg = nbreaks;
1760 int continue_beg = ncontinues;
1761 int has_cond = 0;
1762 tok_expect('(');
1763 if (tok_see() != ';')
1764 readestmt();
1765 tok_expect(';');
1766 l_check = o_mklabel();
1767 if (tok_see() != ';') {
1768 readestmt();
1769 ts_pop_de(NULL);
1770 j_fail = o_jz(0);
1771 has_cond = 1;
1773 tok_expect(';');
1774 j_pass = o_jmp(0);
1775 l_jump = o_mklabel();
1776 if (tok_see() != ')')
1777 readestmt();
1778 tok_expect(')');
1779 o_jmp(l_check);
1780 o_filljmp(j_pass);
1781 readstmt();
1782 o_jmp(l_jump);
1783 if (has_cond)
1784 o_filljmp(j_fail);
1785 break_fill(o_mklabel(), break_beg);
1786 continue_fill(l_jump, continue_beg);
1787 return;
1789 if (!tok_jmp(TOK_SWITCH)) {
1790 readswitch();
1791 return;
1793 if (!tok_jmp(TOK_RETURN)) {
1794 int ret = tok_see() != ';';
1795 if (ret) {
1796 readexpr();
1797 ts_pop_de(NULL);
1799 tok_expect(';');
1800 o_ret(ret);
1801 return;
1803 if (!tok_jmp(TOK_BREAK)) {
1804 tok_expect(';');
1805 breaks[nbreaks++] = o_jmp(0);
1806 return;
1808 if (!tok_jmp(TOK_CONTINUE)) {
1809 tok_expect(';');
1810 continues[ncontinues++] = o_jmp(0);
1811 return;
1813 if (!tok_jmp(TOK_GOTO)) {
1814 tok_expect(TOK_NAME);
1815 goto_add(tok_id());
1816 tok_expect(';');
1817 return;
1819 readestmt();
1820 /* labels */
1821 if (!tok_jmp(':')) {
1822 label_add(tok_id());
1823 return;
1825 tok_expect(';');
1828 static void readdecl(void)
1830 if (!tok_jmp(TOK_TYPEDEF)) {
1831 readdefs(typedefdef, NULL);
1832 tok_expect(';');
1833 return;
1835 readdefs(globaldef, NULL);
1836 if (tok_see() == '{') {
1837 readstmt();
1838 goto_fill();
1839 o_func_end();
1840 func_name[0] = '\0';
1841 nlocals = 0;
1842 ngotos = 0;
1843 nlabels = 0;
1844 return;
1846 tok_expect(';');
1849 static void parse(void)
1851 while (tok_see() != TOK_EOF)
1852 readdecl();
1855 static void compat_macros(void)
1857 cpp_define("__STDC__", "");
1858 cpp_define("__arm__", "");
1859 cpp_define("__linux__", "");
1861 /* ignored keywords */
1862 cpp_define("const", "");
1863 cpp_define("register", "");
1864 cpp_define("volatile", "");
1865 cpp_define("inline", "");
1866 cpp_define("restrict", "");
1867 cpp_define("__inline__", "");
1868 cpp_define("__restrict__", "");
1869 cpp_define("__attribute__(x)", "");
1870 cpp_define("__builtin_va_list__", "long");
1873 int main(int argc, char *argv[])
1875 char obj[128] = "";
1876 int ofd;
1877 int i = 1;
1878 compat_macros();
1879 while (i < argc && argv[i][0] == '-') {
1880 if (argv[i][1] == 'I')
1881 cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]);
1882 if (argv[i][1] == 'D') {
1883 char *name = argv[i] + 2;
1884 char *def = "";
1885 char *eq = strchr(name, '=');
1886 if (eq) {
1887 *eq = '\0';
1888 def = eq + 1;
1890 cpp_define(name, def);
1892 if (argv[i][1] == 'o')
1893 strcpy(obj, argv[i][2] ? argv[i] + 2 : argv[++i]);
1894 i++;
1896 if (i == argc)
1897 die("neatcc: no file given\n");
1898 if (cpp_init(argv[i]))
1899 die("neatcc: cannot open <%s>\n", argv[i]);
1900 parse();
1901 if (!*obj) {
1902 strcpy(obj, argv[i]);
1903 obj[strlen(obj) - 1] = 'o';
1905 ofd = open(obj, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1906 o_write(ofd);
1907 close(ofd);
1908 return 0;