ncc: handle sizeof x + 2
[neatcc.git] / ncc.c
blobbb8599cab92277c9870ddbfab9c248132b4239c4
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 char err[1 << 7];
105 int len = cpp_loc(err, tok_addr());
106 strcpy(err + len, msg);
107 die(err);
110 struct name {
111 char name[NAMELEN];
112 char elfname[NAMELEN]; /* local elf name for static variables in function */
113 struct type type;
114 long addr; /* local stack offset, global data addr, struct offset */
117 static struct name locals[MAXLOCALS];
118 static int nlocals;
119 static struct name globals[MAXGLOBALS];
120 static int nglobals;
122 static void local_add(struct name *name)
124 if (nlocals >= MAXLOCALS)
125 err("nomem: MAXLOCALS reached!\n");
126 memcpy(&locals[nlocals++], name, sizeof(*name));
129 static int global_find(char *name)
131 int i;
132 for (i = 0; i < nglobals; i++)
133 if (!strcmp(name, globals[i].name))
134 return i;
135 return -1;
138 static void global_add(struct name *name)
140 int found = global_find(name->name);
141 int i = found == -1 ? nglobals++ : found;
142 if (nglobals >= MAXGLOBALS)
143 err("nomem: MAXGLOBALS reached!\n");
144 memcpy(&globals[i], name, sizeof(*name));
147 #define MAXENUMS (1 << 10)
149 static struct enumval {
150 char name[NAMELEN];
151 int n;
152 } enums[MAXENUMS];
153 static int nenums;
155 static void enum_add(char *name, int val)
157 struct enumval *ev = &enums[nenums++];
158 if (nenums >= MAXENUMS)
159 err("nomem: MAXENUMS reached!\n");
160 strcpy(ev->name, name);
161 ev->n = val;
164 static int enum_find(int *val, char *name)
166 int i;
167 for (i = nenums - 1; i >= 0; --i)
168 if (!strcmp(name, enums[i].name)) {
169 *val = enums[i].n;
170 return 0;
172 return 1;
175 #define MAXTYPEDEFS (1 << 10)
177 static struct typdefinfo {
178 char name[NAMELEN];
179 struct type type;
180 } typedefs[MAXTYPEDEFS];
181 static int ntypedefs;
183 static void typedef_add(char *name, struct type *type)
185 struct typdefinfo *ti = &typedefs[ntypedefs++];
186 if (ntypedefs >= MAXTYPEDEFS)
187 err("nomem: MAXTYPEDEFS reached!\n");
188 strcpy(ti->name, name);
189 memcpy(&ti->type, type, sizeof(*type));
192 static int typedef_find(char *name)
194 int i;
195 for (i = ntypedefs - 1; i >= 0; --i)
196 if (!strcmp(name, typedefs[i].name))
197 return i;
198 return -1;
201 #define MAXARRAYS (1 << 10)
203 static struct array {
204 struct type type;
205 int n;
206 } arrays[MAXARRAYS];
207 static int narrays;
209 static int array_add(struct type *type, int n)
211 struct array *a = &arrays[narrays++];
212 if (narrays >= MAXARRAYS)
213 err("nomem: MAXARRAYS reached!\n");
214 memcpy(&a->type, type, sizeof(*type));
215 a->n = n;
216 return a - arrays;
219 static void array2ptr(struct type *t)
221 if (t->flags & T_ARRAY && !t->ptr) {
222 memcpy(t, &arrays[t->id].type, sizeof(*t));
223 t->ptr++;
227 #define MAXSTRUCTS (1 << 10)
228 #define MAXFIELDS (1 << 7)
230 static struct structinfo {
231 char name[NAMELEN];
232 struct name fields[MAXFIELDS];
233 int nfields;
234 int isunion;
235 int size;
236 } structs[MAXSTRUCTS];
237 static int nstructs;
239 static int struct_find(char *name, int isunion)
241 int i;
242 for (i = nstructs - 1; i >= 0; --i)
243 if (*structs[i].name && !strcmp(name, structs[i].name) &&
244 structs[i].isunion == isunion)
245 return i;
246 i = nstructs++;
247 if (nstructs >= MAXSTRUCTS)
248 err("nomem: MAXTYPES reached!\n");
249 memset(&structs[i], 0, sizeof(structs[i]));
250 strcpy(structs[i].name, name);
251 structs[i].isunion = isunion;
252 return i;
255 static struct name *struct_field(int id, char *name)
257 struct structinfo *si = &structs[id];
258 int i;
259 for (i = 0; i < si->nfields; i++)
260 if (!strcmp(name, si->fields[i].name))
261 return &si->fields[i];
262 err("field not found\n");
265 #define MAXBREAK (1 << 7)
267 static long breaks[MAXBREAK];
268 static int nbreaks;
269 static long continues[MAXBREAK];
270 static int ncontinues;
272 static void break_fill(long addr, int till)
274 int i;
275 for (i = till; i < nbreaks; i++)
276 o_filljmp2(breaks[i], addr);
277 nbreaks = till;
280 static void continue_fill(long addr, int till)
282 int i;
283 for (i = till; i < ncontinues; i++)
284 o_filljmp2(continues[i], addr);
285 ncontinues = till;
288 static int type_totsz(struct type *t)
290 if (t->ptr)
291 return LONGSZ;
292 if (t->flags & T_ARRAY)
293 return arrays[t->id].n * type_totsz(&arrays[t->id].type);
294 return t->flags & T_STRUCT ? structs[t->id].size : BT_SZ(t->bt);
297 static unsigned type_szde(struct type *t)
299 struct type de = *t;
300 array2ptr(&de);
301 de.ptr--;
302 return type_totsz(&de);
305 static void ts_de(int deref)
307 struct type *t = &ts[nts - 1];
308 if (deref && t->addr && (!(t->flags & T_ARRAY) || (t->ptr)))
309 o_deref(TYPE_BT(t));
310 t->addr = 0;
313 static void ts_pop_de(struct type *t)
315 struct type de;
316 if (!t)
317 t = &de;
318 ts_pop(t);
319 array2ptr(t);
320 if (t->addr && (t->ptr || !(t->flags & T_FUNC)))
321 o_deref(TYPE_BT(t));
322 t->addr = 0;
325 static void ts_pop_de2(struct type *t1, struct type *t2)
327 ts_pop_de(t1);
328 o_tmpswap();
329 ts_pop_de(t2);
330 o_tmpswap();
333 static int tok_jmp(int tok)
335 if (tok_see() != tok)
336 return 1;
337 tok_get();
338 return 0;
341 static void tok_expect(int tok)
343 if (tok_get() != tok)
344 err("syntax error\n");
347 static unsigned bt_op(unsigned bt1, unsigned bt2)
349 unsigned s1 = BT_SZ(bt1);
350 unsigned s2 = BT_SZ(bt2);
351 return (bt1 | bt2) & BT_SIGNED | (s1 > s2 ? s1 : s2);
354 static void ts_binop(int op)
356 struct type t1, t2;
357 int bt;
358 ts_pop_de2(&t1, &t2);
359 if (op == O_DIV || op == O_MOD)
360 bt = TYPE_BT(&t2);
361 else
362 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
363 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
364 ts_push_bt(bt);
367 static void ts_addop(int op)
369 struct type t1, t2;
370 ts_pop_de2(&t1, &t2);
371 if (!t1.ptr && !t2.ptr) {
372 o_bop(op);
373 ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
374 return;
376 if (t1.ptr && !t2.ptr)
377 o_tmpswap();
378 if (!t1.ptr && t2.ptr)
379 if (type_szde(&t2) > 1) {
380 o_num(type_szde(&t2));
381 o_bop(O_MUL);
383 if (t1.ptr && !t2.ptr)
384 o_tmpswap();
385 o_bop(op);
386 if (t1.ptr && t2.ptr) {
387 int sz = type_szde(&t1);
388 if (sz > 1) {
389 o_num(sz);
390 o_bop(O_DIV);
392 ts_push_bt(4 | BT_SIGNED);
393 } else {
394 ts_push(t1.ptr ? &t1 : &t2);
398 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
399 #define MIN(a, b) ((a) < (b) ? (a) : (b))
401 static int type_alignment(struct type *t)
403 if (t->flags & T_ARRAY && !t->ptr)
404 return type_alignment(&arrays[t->id].type);
405 if (t->flags & T_STRUCT && !t->ptr)
406 return type_alignment(&structs[t->id].fields[0].type);
407 return MIN(LONGSZ, type_totsz(t));
410 static void structdef(void *data, struct name *name, unsigned flags)
412 struct structinfo *si = data;
413 if (si->isunion) {
414 name->addr = 0;
415 if (si->size < type_totsz(&name->type))
416 si->size = type_totsz(&name->type);
417 } else {
418 struct type *t = &name->type;
419 int alignment = type_alignment(t);
420 if (t->flags & T_ARRAY && !t->ptr)
421 alignment = MIN(LONGSZ, type_totsz(&arrays[t->id].type));
422 si->size = ALIGN(si->size, alignment);
423 name->addr = si->size;
424 si->size += type_totsz(&name->type);
426 memcpy(&si->fields[si->nfields++], name, sizeof(*name));
429 static int readdefs(void (*def)(void *, struct name *, unsigned f), void *data);
431 static int struct_create(char *name, int isunion)
433 int id = struct_find(name, isunion);
434 struct structinfo *si = &structs[id];
435 tok_expect('{');
436 while (tok_jmp('}')) {
437 readdefs(structdef, si);
438 tok_expect(';');
440 return id;
443 static void readexpr(void);
445 static void enum_create(void)
447 long n = 0;
448 tok_expect('{');
449 while (tok_jmp('}')) {
450 char name[NAMELEN];
451 tok_expect(TOK_NAME);
452 strcpy(name, tok_id());
453 if (!tok_jmp('=')) {
454 readexpr();
455 ts_pop_de(NULL);
456 if (o_popnum(&n))
457 err("const expr expected!\n");
459 enum_add(name, n++);
460 tok_jmp(',');
464 static int basetype(struct type *type, unsigned *flags)
466 int sign = 1;
467 int size = 4;
468 int done = 0;
469 int i = 0;
470 int isunion;
471 char name[NAMELEN] = "";
472 *flags = 0;
473 type->flags = 0;
474 type->ptr = 0;
475 type->addr = 0;
476 while (!done) {
477 switch (tok_see()) {
478 case TOK_STATIC:
479 *flags |= F_STATIC;
480 break;
481 case TOK_EXTERN:
482 *flags |= F_EXTERN;
483 break;
484 case TOK_VOID:
485 sign = 0;
486 size = 0;
487 done = 1;
488 break;
489 case TOK_INT:
490 done = 1;
491 break;
492 case TOK_CHAR:
493 size = 1;
494 done = 1;
495 break;
496 case TOK_SHORT:
497 size = 2;
498 break;
499 case TOK_LONG:
500 size = LONGSZ;
501 break;
502 case TOK_SIGNED:
503 break;
504 case TOK_UNSIGNED:
505 sign = 0;
506 break;
507 case TOK_UNION:
508 case TOK_STRUCT:
509 isunion = tok_get() == TOK_UNION;
510 if (!tok_jmp(TOK_NAME))
511 strcpy(name, tok_id());
512 if (tok_see() == '{')
513 type->id = struct_create(name, isunion);
514 else
515 type->id = struct_find(name, isunion);
516 type->flags |= T_STRUCT;
517 type->bt = LONGSZ;
518 return 0;
519 case TOK_ENUM:
520 tok_get();
521 tok_jmp(TOK_NAME);
522 if (tok_see() == '{')
523 enum_create();
524 type->bt = 4 | BT_SIGNED;
525 return 0;
526 default:
527 if (tok_see() == TOK_NAME) {
528 int id = typedef_find(tok_id());
529 if (id != -1) {
530 tok_get();
531 memcpy(type, &typedefs[id].type,
532 sizeof(*type));
533 return 0;
536 if (!i)
537 return 1;
538 done = 1;
539 continue;
541 i++;
542 tok_get();
544 type->bt = size | (sign ? BT_SIGNED : 0);
545 return 0;
548 static int readname(struct type *main, char *name,
549 struct type *base, unsigned flags);
551 static int readtype(struct type *type)
553 return readname(type, NULL, NULL, 0);
556 static void readptrs(struct type *type)
558 while (!tok_jmp('*')) {
559 type->ptr++;
560 if (!type->bt)
561 type->bt = 1;
565 /* used to differenciate labels from case and cond exprs */
566 static int ncexpr;
567 static int caseexpr;
569 static void readpre(void);
571 static char *tmp_str(char *buf, int len)
573 static char name[NAMELEN];
574 static int id;
575 void *dat;
576 sprintf(name, "__neatcc.s%d", id++);
577 dat = o_mkdat(name, len, 0);
578 memcpy(dat, buf, len);
579 return name;
582 static void readprimary(void)
584 int i;
585 if (!tok_jmp(TOK_NUM)) {
586 long n;
587 int bt = tok_num(&n);
588 ts_push_bt(bt);
589 o_num(n);
590 return;
592 if (!tok_jmp(TOK_STR)) {
593 struct type t;
594 char buf[BUFSIZE];
595 int len;
596 t.bt = 1 | BT_SIGNED;
597 t.ptr = 1;
598 t.addr = 0;
599 t.flags = 0;
600 ts_push(&t);
601 len = tok_str(buf);
602 o_sym(tmp_str(buf, len));
603 return;
605 if (!tok_jmp(TOK_NAME)) {
606 struct name unkn = {""};
607 char *name = unkn.name;
608 int n;
609 strcpy(name, tok_id());
610 /* don't search for labels here */
611 if (!ncexpr && !caseexpr && tok_see() == ':')
612 return;
613 for (i = nlocals - 1; i >= 0; --i) {
614 struct type *t = &locals[i].type;
615 if (!strcmp(locals[i].name, name)) {
616 o_local(locals[i].addr);
617 ts_push_addr(t);
618 return;
621 if ((n = global_find(name)) != -1) {
622 struct name *g = &globals[n];
623 struct type *t = &g->type;
624 char *elfname = *g->elfname ? g->elfname : g->name;
625 o_sym(elfname);
626 ts_push_addr(t);
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 die("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 int readcexpr_const(void)
1108 long c;
1109 if (o_popnum(&c))
1110 return -1;
1111 if (!c)
1112 nogen++;
1113 reador();
1114 /* both branches yield the same type; so ignore the first */
1115 ts_pop_de(NULL);
1116 tok_expect(':');
1117 if (c)
1118 nogen++;
1119 else
1120 nogen--;
1121 reador();
1122 /* making sure t->addr == 0 on both branches */
1123 ts_de(1);
1124 if (c)
1125 nogen--;
1126 return 0;
1129 static void readcexpr(void)
1131 long l1, l2;
1132 reador();
1133 if (tok_jmp('?'))
1134 return;
1135 ncexpr++;
1136 ts_pop_de(NULL);
1137 o_fork();
1138 if (readcexpr_const()) {
1139 l1 = o_jz(0);
1140 reador();
1141 /* both branches yield the same type; so ignore the first */
1142 ts_pop_de(NULL);
1143 o_forkpush();
1144 l2 = o_jmp(0);
1146 tok_expect(':');
1147 o_filljmp(l1);
1148 reador();
1149 /* making sure t->addr == 0 on both branches */
1150 ts_de(1);
1151 o_forkpush();
1152 o_forkjoin();
1153 o_filljmp(l2);
1155 ncexpr--;
1158 static void opassign(int op, int ptrop)
1160 struct type t = ts[nts - 1];
1161 o_tmpcopy();
1162 ts_push(&t);
1163 readexpr();
1164 ts_addop(op);
1165 o_assign(TYPE_BT(&ts[nts - 2]));
1166 ts_pop(NULL);
1167 ts_de(0);
1170 static void doassign(void)
1172 struct type t = ts[nts - 1];
1173 if (!t.ptr && t.flags & T_STRUCT) {
1174 ts_pop(NULL);
1175 o_num(type_totsz(&t));
1176 o_memcpy();
1177 } else {
1178 ts_pop_de(NULL);
1179 o_assign(TYPE_BT(&ts[nts - 1]));
1180 ts_de(0);
1184 static void readexpr(void)
1186 readcexpr();
1187 if (!tok_jmp('=')) {
1188 readexpr();
1189 doassign();
1190 return;
1192 if (!tok_jmp(TOK2("+="))) {
1193 opassign(O_ADD, 1);
1194 return;
1196 if (!tok_jmp(TOK2("-="))) {
1197 opassign(O_SUB, 1);
1198 return;
1200 if (!tok_jmp(TOK2("*="))) {
1201 opassign(O_MUL, 0);
1202 return;
1204 if (!tok_jmp(TOK2("/="))) {
1205 opassign(O_DIV, 0);
1206 return;
1208 if (!tok_jmp(TOK2("%="))) {
1209 opassign(O_MOD, 0);
1210 return;
1212 if (!tok_jmp(TOK3("<<="))) {
1213 opassign(O_SHL, 0);
1214 return;
1216 if (!tok_jmp(TOK3(">>="))) {
1217 opassign(O_SHR, 0);
1218 return;
1220 if (!tok_jmp(TOK3("&="))) {
1221 opassign(O_AND, 0);
1222 return;
1224 if (!tok_jmp(TOK3("|="))) {
1225 opassign(O_OR, 0);
1226 return;
1228 if (!tok_jmp(TOK3("^="))) {
1229 opassign(O_XOR, 0);
1230 return;
1234 static void readestmt(void)
1236 do {
1237 o_tmpdrop(-1);
1238 nts = 0;
1239 readexpr();
1240 } while (!tok_jmp(','));
1243 static void o_localoff(long addr, int off)
1245 o_local(addr);
1246 if (off) {
1247 o_num(off);
1248 o_bop(O_ADD);
1252 static struct type *innertype(struct type *t)
1254 if (t->flags & T_ARRAY && !t->ptr)
1255 return innertype(&arrays[t->id].type);
1256 return t;
1259 static void initexpr(struct type *t, int off, void *obj,
1260 void (*set)(void *obj, int off, struct type *t))
1262 if (tok_jmp('{')) {
1263 set(obj, off, t);
1264 return;
1266 if (!t->ptr && t->flags & T_STRUCT) {
1267 struct structinfo *si = &structs[t->id];
1268 int i;
1269 for (i = 0; i < si->nfields; i++) {
1270 struct name *field = &si->fields[i];
1271 if (!tok_jmp('.')) {
1272 tok_expect(TOK_NAME);
1273 field = struct_field(t->id, tok_id());
1274 tok_expect('=');
1276 initexpr(&field->type, off + field->addr, obj, set);
1277 if (tok_jmp(',') || tok_see() == '}')
1278 break;
1280 } else if (t->flags & T_ARRAY) {
1281 struct type *t_de = &arrays[t->id].type;
1282 int i;
1283 for (i = 0; ; i++) {
1284 long idx = i;
1285 struct type *it = t_de;
1286 if (!tok_jmp('[')) {
1287 readexpr();
1288 ts_pop_de(NULL);
1289 o_popnum(&idx);
1290 tok_expect(']');
1291 tok_expect('=');
1293 if (tok_see() != '{')
1294 it = innertype(t_de);
1295 initexpr(it, off + type_totsz(it) * idx, obj, set);
1296 if (tok_jmp(',') || tok_see() == '}')
1297 break;
1300 tok_expect('}');
1303 static void jumpbrace(void)
1305 int depth = 0;
1306 while (tok_see() != '}' || depth--)
1307 if (tok_get() == '{')
1308 depth++;
1309 tok_expect('}');
1312 static int initsize(void)
1314 long addr = tok_addr();
1315 int n = 0;
1316 if (!tok_jmp(TOK_STR)) {
1317 n = tok_str(NULL);
1318 tok_jump(addr);
1319 return n;
1321 tok_expect('{');
1322 while (tok_jmp('}')) {
1323 long idx = n;
1324 if (!tok_jmp('[')) {
1325 readexpr();
1326 ts_pop_de(NULL);
1327 o_popnum(&idx);
1328 tok_expect(']');
1329 tok_expect('=');
1331 if (n < idx + 1)
1332 n = idx + 1;
1333 while (tok_see() != '}' && tok_see() != ',')
1334 if (tok_get() == '{')
1335 jumpbrace();
1336 tok_jmp(',');
1338 tok_jump(addr);
1339 return n;
1342 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1344 static void globalinit(void *obj, int off, struct type *t)
1346 struct name *name = obj;
1347 char *elfname = *name->elfname ? name->elfname : name->name;
1348 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1349 struct type *t_de = &arrays[t->id].type;
1350 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1351 char buf[BUFSIZE];
1352 int len;
1353 tok_expect(TOK_STR);
1354 len = tok_str(buf);
1355 memcpy((void *) name->addr + off, buf, len);
1356 return;
1359 readexpr();
1360 o_datset(elfname, off, TYPE_BT(t));
1361 ts_pop(NULL);
1364 static void globaldef(void *data, struct name *name, unsigned flags)
1366 struct type *t = &name->type;
1367 char *elfname = *name->elfname ? name->elfname : name->name;
1368 int sz;
1369 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1370 if (~flags & F_EXTERN)
1371 arrays[t->id].n = initsize();
1372 sz = type_totsz(t);
1373 if (!(flags & F_EXTERN) && (!(t->flags & T_FUNC) || t->ptr)) {
1374 if (flags & F_INIT)
1375 name->addr = (long) o_mkdat(elfname, sz, F_GLOBAL(flags));
1376 else
1377 o_mkbss(elfname, sz, F_GLOBAL(flags));
1379 global_add(name);
1380 if (flags & F_INIT)
1381 initexpr(t, 0, name, globalinit);
1384 static void localinit(void *obj, int off, struct type *t)
1386 long addr = *(long *) obj;
1387 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1388 struct type *t_de = &arrays[t->id].type;
1389 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1390 char buf[BUFSIZE];
1391 int len;
1392 tok_expect(TOK_STR);
1393 len = tok_str(buf);
1394 o_localoff(addr, off);
1395 o_sym(tmp_str(buf, len));
1396 o_num(len);
1397 o_memcpy();
1398 o_tmpdrop(1);
1399 return;
1402 o_localoff(addr, off);
1403 ts_push(t);
1404 readexpr();
1405 doassign();
1406 ts_pop(NULL);
1407 o_tmpdrop(1);
1410 /* current function name */
1411 static char func_name[NAMELEN];
1413 static void localdef(void *data, struct name *name, unsigned flags)
1415 struct type *t = &name->type;
1416 if ((flags & F_EXTERN) || (t->flags & T_FUNC) && !t->ptr) {
1417 global_add(name);
1418 return;
1420 if (flags & F_STATIC) {
1421 sprintf(name->elfname, "__neatcc.%s.%s", func_name, name->name);
1422 globaldef(data, name, flags);
1423 return;
1425 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1426 arrays[t->id].n = initsize();
1427 name->addr = o_mklocal(type_totsz(&name->type));
1428 local_add(name);
1429 if (flags & F_INIT) {
1430 if (t->flags & (T_ARRAY | T_STRUCT) && !t->ptr) {
1431 o_local(name->addr);
1432 o_num(0);
1433 o_num(type_totsz(t));
1434 o_memset();
1435 o_tmpdrop(1);
1437 initexpr(t, 0, &name->addr, localinit);
1441 static void funcdef(char *name, struct type *type, struct name *args,
1442 int nargs, int varg, unsigned flags)
1444 struct name global = {""};
1445 int i;
1446 strcpy(global.name, name);
1447 strcpy(func_name, name);
1448 memcpy(&global.type, type, sizeof(*type));
1449 o_func_beg(name, nargs, F_GLOBAL(flags), varg);
1450 global_add(&global);
1451 for (i = 0; i < nargs; i++) {
1452 args[i].addr = o_arg2loc(i);
1453 local_add(&args[i]);
1457 static int readargs(struct name *args, int *varg)
1459 int nargs = 0;
1460 tok_expect('(');
1461 *varg = 0;
1462 while (tok_see() != ')') {
1463 if (!tok_jmp(TOK3("..."))) {
1464 *varg = 1;
1465 break;
1467 readname(&args[nargs].type, args[nargs].name, NULL, 0);
1468 array2ptr(&args[nargs].type);
1469 nargs++;
1470 if (tok_jmp(','))
1471 break;
1473 tok_expect(')');
1474 if (nargs == 1 && !TYPE_BT(&args[0].type))
1475 return 0;
1476 return nargs;
1479 static int readname(struct type *main, char *name,
1480 struct type *base, unsigned flags)
1482 struct type tpool[3];
1483 int npool = 0;
1484 struct type *type = &tpool[npool++];
1485 struct type *func = NULL;
1486 struct type *ret = NULL;
1487 int arsz[10];
1488 int nar = 0;
1489 int i;
1490 memset(tpool, 0, sizeof(tpool));
1491 if (name)
1492 *name = '\0';
1493 if (!base) {
1494 if (basetype(type, &flags))
1495 return 1;
1496 } else {
1497 memcpy(type, base, sizeof(*base));
1499 readptrs(type);
1500 if (!tok_jmp('(')) {
1501 ret = type;
1502 type = &tpool[npool++];
1503 func = type;
1504 readptrs(type);
1506 if (!tok_jmp(TOK_NAME) && name)
1507 strcpy(name, tok_id());
1508 while (!tok_jmp('[')) {
1509 long n = 0;
1510 if (tok_jmp(']')) {
1511 readexpr();
1512 ts_pop_de(NULL);
1513 if (o_popnum(&n))
1514 err("const expr expected\n");
1515 tok_expect(']');
1517 arsz[nar++] = n;
1519 for (i = nar - 1; i >= 0; i--) {
1520 type->id = array_add(type, arsz[i]);
1521 if (func && i == nar - 1)
1522 func = &arrays[type->id].type;
1523 type->flags = T_ARRAY;
1524 type->bt = LONGSZ;
1525 type->ptr = 0;
1527 if (func)
1528 tok_expect(')');
1529 if (tok_see() == '(') {
1530 struct name args[MAXARGS] = {{""}};
1531 int varg = 0;
1532 int nargs = readargs(args, &varg);
1533 int fdef = !func;
1534 if (!func) {
1535 ret = type;
1536 type = &tpool[npool++];
1537 func = type;
1539 func->flags = T_FUNC;
1540 func->bt = LONGSZ;
1541 func->id = func_create(ret, args, nargs);
1542 if (fdef && tok_see() == '{') {
1543 funcdef(name, func, args, nargs, varg, flags);
1544 return 1;
1547 memcpy(main, type, sizeof(*type));
1548 return 0;
1551 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
1552 void *data)
1554 struct type base;
1555 unsigned base_flags;
1556 if (basetype(&base, &base_flags))
1557 return 1;
1558 while (tok_see() != ';' && tok_see() != '{') {
1559 struct name name = {{""}};
1560 unsigned flags = base_flags;
1561 if (readname(&name.type, name.name, &base, flags))
1562 break;
1563 if (!tok_jmp('='))
1564 flags |= F_INIT;
1565 def(data, &name, flags);
1566 tok_jmp(',');
1568 return 0;
1571 static void typedefdef(void *data, struct name *name, unsigned flags)
1573 typedef_add(name->name, &name->type);
1576 static void readstmt(void);
1578 #define MAXCASES (1 << 7)
1580 static void readswitch(void)
1582 int break_beg = nbreaks;
1583 long val_addr = o_mklocal(LONGSZ);
1584 struct type t;
1585 int ncases = 0; /* number of case labels */
1586 long last_failed = -1; /* address of last failed jmp */
1587 long last_matched = -1; /* address of last walk through jmp */
1588 long default_addr = -1; /* address of the default label */
1589 tok_expect('(');
1590 readexpr();
1591 ts_pop_de(&t);
1592 o_local(val_addr);
1593 o_tmpswap();
1594 o_assign(TYPE_BT(&t));
1595 ts_de(0);
1596 o_tmpdrop(1);
1597 tok_expect(')');
1598 tok_expect('{');
1599 while (tok_jmp('}')) {
1600 if (tok_see() != TOK_CASE && tok_see() != TOK_DEFAULT) {
1601 readstmt();
1602 continue;
1604 if (ncases)
1605 last_matched = o_jmp(0);
1606 if (tok_get() == TOK_CASE) {
1607 if (last_failed >= 0)
1608 o_filljmp(last_failed);
1609 caseexpr = 1;
1610 readexpr();
1611 ts_pop_de(NULL);
1612 caseexpr = 0;
1613 o_local(val_addr);
1614 o_deref(TYPE_BT(&t));
1615 o_bop(O_EQ);
1616 last_failed = o_jz(0);
1617 o_tmpdrop(1);
1618 } else {
1619 default_addr = o_mklabel();
1621 tok_expect(':');
1622 if (last_matched >= 0)
1623 o_filljmp(last_matched);
1624 ncases++;
1626 o_rmlocal(val_addr, LONGSZ);
1627 if (last_failed >= 0)
1628 o_filljmp2(last_failed,
1629 default_addr >= 0 ? default_addr : o_mklabel());
1630 break_fill(o_mklabel(), break_beg);
1633 #define MAXGOTO (1 << 10)
1635 static struct gotoinfo {
1636 char name[NAMELEN];
1637 long addr;
1638 } gotos[MAXGOTO];
1639 static int ngotos;
1641 static struct labelinfo {
1642 char name[NAMELEN];
1643 long addr;
1644 } labels[MAXGOTO];
1645 static int nlabels;
1647 static void goto_add(char *name)
1649 strcpy(gotos[ngotos].name, name);
1650 gotos[ngotos++].addr = o_jmp(0);
1653 static void label_add(char *name)
1655 strcpy(labels[nlabels].name, name);
1656 labels[nlabels++].addr = o_mklabel();
1659 static void goto_fill(void)
1661 int i, j;
1662 for (i = 0; i < ngotos; i++)
1663 for (j = 0; j < nlabels; j++)
1664 if (!strcmp(gotos[i].name, labels[j].name)) {
1665 o_filljmp2(gotos[i].addr, labels[j].addr);
1666 break;
1670 static void readstmt(void)
1672 o_tmpdrop(-1);
1673 nts = 0;
1674 if (!tok_jmp('{')) {
1675 int _nlocals = nlocals;
1676 int _nglobals = nglobals;
1677 int _nenums = nenums;
1678 int _ntypedefs = ntypedefs;
1679 int _nstructs = nstructs;
1680 int _nfuncs = nfuncs;
1681 int _narrays = narrays;
1682 while (tok_jmp('}'))
1683 readstmt();
1684 nlocals = _nlocals;
1685 nenums = _nenums;
1686 ntypedefs = _ntypedefs;
1687 nstructs = _nstructs;
1688 nfuncs = _nfuncs;
1689 narrays = _narrays;
1690 nglobals = _nglobals;
1691 return;
1693 if (!readdefs(localdef, NULL)) {
1694 tok_expect(';');
1695 return;
1697 if (!tok_jmp(TOK_TYPEDEF)) {
1698 readdefs(typedefdef, NULL);
1699 tok_expect(';');
1700 return;
1702 if (!tok_jmp(TOK_IF)) {
1703 long l1, l2;
1704 tok_expect('(');
1705 readexpr();
1706 tok_expect(')');
1707 ts_pop_de(NULL);
1708 l1 = o_jz(0);
1709 readstmt();
1710 if (!tok_jmp(TOK_ELSE)) {
1711 l2 = o_jmp(0);
1712 o_filljmp(l1);
1713 readstmt();
1714 o_filljmp(l2);
1715 } else {
1716 o_filljmp(l1);
1718 return;
1720 if (!tok_jmp(TOK_WHILE)) {
1721 long l1, l2;
1722 int break_beg = nbreaks;
1723 int continue_beg = ncontinues;
1724 l1 = o_mklabel();
1725 tok_expect('(');
1726 readexpr();
1727 tok_expect(')');
1728 ts_pop_de(NULL);
1729 l2 = o_jz(0);
1730 readstmt();
1731 o_jmp(l1);
1732 o_filljmp(l2);
1733 break_fill(o_mklabel(), break_beg);
1734 continue_fill(l1, continue_beg);
1735 return;
1737 if (!tok_jmp(TOK_DO)) {
1738 long l1, l2;
1739 int break_beg = nbreaks;
1740 int continue_beg = ncontinues;
1741 l1 = o_mklabel();
1742 readstmt();
1743 tok_expect(TOK_WHILE);
1744 tok_expect('(');
1745 l2 = o_mklabel();
1746 readexpr();
1747 ts_pop_de(NULL);
1748 o_jnz(l1);
1749 tok_expect(')');
1750 break_fill(o_mklabel(), break_beg);
1751 continue_fill(l2, continue_beg);
1752 tok_expect(';');
1753 return;
1755 if (!tok_jmp(TOK_FOR)) {
1756 long l_check, l_jump, j_fail, j_pass;
1757 int break_beg = nbreaks;
1758 int continue_beg = ncontinues;
1759 int has_cond = 0;
1760 tok_expect('(');
1761 if (tok_see() != ';')
1762 readestmt();
1763 tok_expect(';');
1764 l_check = o_mklabel();
1765 if (tok_see() != ';') {
1766 readestmt();
1767 ts_pop_de(NULL);
1768 j_fail = o_jz(0);
1769 has_cond = 1;
1771 tok_expect(';');
1772 j_pass = o_jmp(0);
1773 l_jump = o_mklabel();
1774 if (tok_see() != ')')
1775 readestmt();
1776 tok_expect(')');
1777 o_jmp(l_check);
1778 o_filljmp(j_pass);
1779 readstmt();
1780 o_jmp(l_jump);
1781 if (has_cond)
1782 o_filljmp(j_fail);
1783 break_fill(o_mklabel(), break_beg);
1784 continue_fill(l_jump, continue_beg);
1785 return;
1787 if (!tok_jmp(TOK_SWITCH)) {
1788 readswitch();
1789 return;
1791 if (!tok_jmp(TOK_RETURN)) {
1792 int ret = tok_see() != ';';
1793 if (ret) {
1794 readexpr();
1795 ts_pop_de(NULL);
1797 tok_expect(';');
1798 o_ret(ret);
1799 return;
1801 if (!tok_jmp(TOK_BREAK)) {
1802 tok_expect(';');
1803 breaks[nbreaks++] = o_jmp(0);
1804 return;
1806 if (!tok_jmp(TOK_CONTINUE)) {
1807 tok_expect(';');
1808 continues[ncontinues++] = o_jmp(0);
1809 return;
1811 if (!tok_jmp(TOK_GOTO)) {
1812 tok_expect(TOK_NAME);
1813 goto_add(tok_id());
1814 tok_expect(';');
1815 return;
1817 readestmt();
1818 /* labels */
1819 if (!tok_jmp(':')) {
1820 label_add(tok_id());
1821 return;
1823 tok_expect(';');
1826 static void readdecl(void)
1828 if (!tok_jmp(TOK_TYPEDEF)) {
1829 readdefs(typedefdef, NULL);
1830 tok_expect(';');
1831 return;
1833 readdefs(globaldef, NULL);
1834 if (tok_see() == '{') {
1835 readstmt();
1836 goto_fill();
1837 o_func_end();
1838 func_name[0] = '\0';
1839 nlocals = 0;
1840 ngotos = 0;
1841 nlabels = 0;
1842 return;
1844 tok_expect(';');
1847 static void parse(void)
1849 while (tok_see() != TOK_EOF)
1850 readdecl();
1853 static void compat_macros(void)
1855 cpp_define("__STDC__", "");
1856 cpp_define("__arm__", "");
1857 cpp_define("__linux__", "");
1859 /* ignored keywords */
1860 cpp_define("const", "");
1861 cpp_define("register", "");
1862 cpp_define("volatile", "");
1863 cpp_define("inline", "");
1864 cpp_define("restrict", "");
1865 cpp_define("__inline__", "");
1866 cpp_define("__restrict__", "");
1867 cpp_define("__attribute__(x)", "");
1868 cpp_define("__builtin_va_list__", "long");
1871 int main(int argc, char *argv[])
1873 char obj[128] = "";
1874 int ofd;
1875 int i = 1;
1876 compat_macros();
1877 while (i < argc && argv[i][0] == '-') {
1878 if (argv[i][1] == 'I')
1879 cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]);
1880 if (argv[i][1] == 'D') {
1881 char *name = argv[i] + 2;
1882 char *def = "";
1883 char *eq = strchr(name, '=');
1884 if (eq) {
1885 *eq = '\0';
1886 def = eq + 1;
1888 cpp_define(name, def);
1890 if (argv[i][1] == 'o')
1891 strcpy(obj, argv[i][2] ? argv[i] + 2 : argv[++i]);
1892 i++;
1894 if (i == argc)
1895 die("neatcc: no file given\n");
1896 if (cpp_init(argv[i]))
1897 die("neatcc: cannot open input file\n");
1898 parse();
1899 if (!*obj) {
1900 strcpy(obj, argv[i]);
1901 obj[strlen(obj) - 1] = 'o';
1903 ofd = open(obj, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1904 o_write(ofd);
1905 close(ofd);
1906 return 0;