more verbose error messages for missing files
[neatcc/cc.git] / ncc.c
blobec8330799ab11cf8478676fe40a8cdc2f6a1cfaf
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 global_find(char *name)
128 int i;
129 for (i = 0; i < nglobals; i++)
130 if (!strcmp(name, globals[i].name))
131 return i;
132 return -1;
135 static void global_add(struct name *name)
137 int found = global_find(name->name);
138 int i = found == -1 ? nglobals++ : found;
139 if (nglobals >= MAXGLOBALS)
140 err("nomem: MAXGLOBALS reached!\n");
141 memcpy(&globals[i], name, sizeof(*name));
144 #define MAXENUMS (1 << 10)
146 static struct enumval {
147 char name[NAMELEN];
148 int n;
149 } enums[MAXENUMS];
150 static int nenums;
152 static void enum_add(char *name, int val)
154 struct enumval *ev = &enums[nenums++];
155 if (nenums >= MAXENUMS)
156 err("nomem: MAXENUMS reached!\n");
157 strcpy(ev->name, name);
158 ev->n = val;
161 static int enum_find(int *val, char *name)
163 int i;
164 for (i = nenums - 1; i >= 0; --i)
165 if (!strcmp(name, enums[i].name)) {
166 *val = enums[i].n;
167 return 0;
169 return 1;
172 #define MAXTYPEDEFS (1 << 10)
174 static struct typdefinfo {
175 char name[NAMELEN];
176 struct type type;
177 } typedefs[MAXTYPEDEFS];
178 static int ntypedefs;
180 static void typedef_add(char *name, struct type *type)
182 struct typdefinfo *ti = &typedefs[ntypedefs++];
183 if (ntypedefs >= MAXTYPEDEFS)
184 err("nomem: MAXTYPEDEFS reached!\n");
185 strcpy(ti->name, name);
186 memcpy(&ti->type, type, sizeof(*type));
189 static int typedef_find(char *name)
191 int i;
192 for (i = ntypedefs - 1; i >= 0; --i)
193 if (!strcmp(name, typedefs[i].name))
194 return i;
195 return -1;
198 #define MAXARRAYS (1 << 10)
200 static struct array {
201 struct type type;
202 int n;
203 } arrays[MAXARRAYS];
204 static int narrays;
206 static int array_add(struct type *type, int n)
208 struct array *a = &arrays[narrays++];
209 if (narrays >= MAXARRAYS)
210 err("nomem: MAXARRAYS reached!\n");
211 memcpy(&a->type, type, sizeof(*type));
212 a->n = n;
213 return a - arrays;
216 static void array2ptr(struct type *t)
218 if (t->flags & T_ARRAY && !t->ptr) {
219 memcpy(t, &arrays[t->id].type, sizeof(*t));
220 t->ptr++;
224 #define MAXSTRUCTS (1 << 10)
225 #define MAXFIELDS (1 << 7)
227 static struct structinfo {
228 char name[NAMELEN];
229 struct name fields[MAXFIELDS];
230 int nfields;
231 int isunion;
232 int size;
233 } structs[MAXSTRUCTS];
234 static int nstructs;
236 static int struct_find(char *name, int isunion)
238 int i;
239 for (i = nstructs - 1; i >= 0; --i)
240 if (*structs[i].name && !strcmp(name, structs[i].name) &&
241 structs[i].isunion == isunion)
242 return i;
243 i = nstructs++;
244 if (nstructs >= MAXSTRUCTS)
245 err("nomem: MAXTYPES reached!\n");
246 memset(&structs[i], 0, sizeof(structs[i]));
247 strcpy(structs[i].name, name);
248 structs[i].isunion = isunion;
249 return i;
252 static struct name *struct_field(int id, char *name)
254 struct structinfo *si = &structs[id];
255 int i;
256 for (i = 0; i < si->nfields; i++)
257 if (!strcmp(name, si->fields[i].name))
258 return &si->fields[i];
259 err("field not found\n");
262 #define MAXBREAK (1 << 7)
264 static long breaks[MAXBREAK];
265 static int nbreaks;
266 static long continues[MAXBREAK];
267 static int ncontinues;
269 static void break_fill(long addr, int till)
271 int i;
272 for (i = till; i < nbreaks; i++)
273 o_filljmp2(breaks[i], addr);
274 nbreaks = till;
277 static void continue_fill(long addr, int till)
279 int i;
280 for (i = till; i < ncontinues; i++)
281 o_filljmp2(continues[i], addr);
282 ncontinues = till;
285 static int type_totsz(struct type *t)
287 if (t->ptr)
288 return LONGSZ;
289 if (t->flags & T_ARRAY)
290 return arrays[t->id].n * type_totsz(&arrays[t->id].type);
291 return t->flags & T_STRUCT ? structs[t->id].size : BT_SZ(t->bt);
294 static unsigned type_szde(struct type *t)
296 struct type de = *t;
297 array2ptr(&de);
298 de.ptr--;
299 return type_totsz(&de);
302 static void ts_de(int deref)
304 struct type *t = &ts[nts - 1];
305 if (deref && t->addr && (!(t->flags & T_ARRAY) || (t->ptr)))
306 o_deref(TYPE_BT(t));
307 t->addr = 0;
310 static void ts_pop_de(struct type *t)
312 struct type de;
313 if (!t)
314 t = &de;
315 ts_pop(t);
316 array2ptr(t);
317 if (t->addr && (t->ptr || !(t->flags & T_FUNC)))
318 o_deref(TYPE_BT(t));
319 t->addr = 0;
322 static void ts_pop_de2(struct type *t1, struct type *t2)
324 ts_pop_de(t1);
325 o_tmpswap();
326 ts_pop_de(t2);
327 o_tmpswap();
330 static int tok_jmp(int tok)
332 if (tok_see() != tok)
333 return 1;
334 tok_get();
335 return 0;
338 static void tok_expect(int tok)
340 if (tok_get() != tok)
341 err("syntax error\n");
344 static unsigned bt_op(unsigned bt1, unsigned bt2)
346 unsigned s1 = BT_SZ(bt1);
347 unsigned s2 = BT_SZ(bt2);
348 return (bt1 | bt2) & BT_SIGNED | (s1 > s2 ? s1 : s2);
351 static void ts_binop(int op)
353 struct type t1, t2;
354 int bt;
355 ts_pop_de2(&t1, &t2);
356 if (op == O_DIV || op == O_MOD)
357 bt = TYPE_BT(&t2);
358 else
359 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
360 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
361 ts_push_bt(bt);
364 static void ts_addop(int op)
366 struct type t1, t2;
367 ts_pop_de2(&t1, &t2);
368 if (!t1.ptr && !t2.ptr) {
369 o_bop(op);
370 ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
371 return;
373 if (t1.ptr && !t2.ptr)
374 o_tmpswap();
375 if (!t1.ptr && t2.ptr)
376 if (type_szde(&t2) > 1) {
377 o_num(type_szde(&t2));
378 o_bop(O_MUL);
380 if (t1.ptr && !t2.ptr)
381 o_tmpswap();
382 o_bop(op);
383 if (t1.ptr && t2.ptr) {
384 int sz = type_szde(&t1);
385 if (sz > 1) {
386 o_num(sz);
387 o_bop(O_DIV);
389 ts_push_bt(4 | BT_SIGNED);
390 } else {
391 ts_push(t1.ptr ? &t1 : &t2);
395 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
396 #define MIN(a, b) ((a) < (b) ? (a) : (b))
398 static int type_alignment(struct type *t)
400 if (t->flags & T_ARRAY && !t->ptr)
401 return type_alignment(&arrays[t->id].type);
402 if (t->flags & T_STRUCT && !t->ptr)
403 return type_alignment(&structs[t->id].fields[0].type);
404 return MIN(LONGSZ, type_totsz(t));
407 static void structdef(void *data, struct name *name, unsigned flags)
409 struct structinfo *si = data;
410 if (si->isunion) {
411 name->addr = 0;
412 if (si->size < type_totsz(&name->type))
413 si->size = type_totsz(&name->type);
414 } else {
415 struct type *t = &name->type;
416 int alignment = type_alignment(t);
417 if (t->flags & T_ARRAY && !t->ptr)
418 alignment = MIN(LONGSZ, type_totsz(&arrays[t->id].type));
419 si->size = ALIGN(si->size, alignment);
420 name->addr = si->size;
421 si->size += type_totsz(&name->type);
423 memcpy(&si->fields[si->nfields++], name, sizeof(*name));
426 static int readdefs(void (*def)(void *, struct name *, unsigned f), void *data);
428 static int struct_create(char *name, int isunion)
430 int id = struct_find(name, isunion);
431 struct structinfo *si = &structs[id];
432 tok_expect('{');
433 while (tok_jmp('}')) {
434 readdefs(structdef, si);
435 tok_expect(';');
437 return id;
440 static void readexpr(void);
442 static void enum_create(void)
444 long n = 0;
445 tok_expect('{');
446 while (tok_jmp('}')) {
447 char name[NAMELEN];
448 tok_expect(TOK_NAME);
449 strcpy(name, tok_id());
450 if (!tok_jmp('=')) {
451 readexpr();
452 ts_pop_de(NULL);
453 if (o_popnum(&n))
454 err("const expr expected!\n");
456 enum_add(name, n++);
457 tok_jmp(',');
461 static int basetype(struct type *type, unsigned *flags)
463 int sign = 1;
464 int size = 4;
465 int done = 0;
466 int i = 0;
467 int isunion;
468 char name[NAMELEN] = "";
469 *flags = 0;
470 type->flags = 0;
471 type->ptr = 0;
472 type->addr = 0;
473 while (!done) {
474 switch (tok_see()) {
475 case TOK_STATIC:
476 *flags |= F_STATIC;
477 break;
478 case TOK_EXTERN:
479 *flags |= F_EXTERN;
480 break;
481 case TOK_VOID:
482 sign = 0;
483 size = 0;
484 done = 1;
485 break;
486 case TOK_INT:
487 done = 1;
488 break;
489 case TOK_CHAR:
490 size = 1;
491 done = 1;
492 break;
493 case TOK_SHORT:
494 size = 2;
495 break;
496 case TOK_LONG:
497 size = LONGSZ;
498 break;
499 case TOK_SIGNED:
500 break;
501 case TOK_UNSIGNED:
502 sign = 0;
503 break;
504 case TOK_UNION:
505 case TOK_STRUCT:
506 isunion = tok_get() == TOK_UNION;
507 if (!tok_jmp(TOK_NAME))
508 strcpy(name, tok_id());
509 if (tok_see() == '{')
510 type->id = struct_create(name, isunion);
511 else
512 type->id = struct_find(name, isunion);
513 type->flags |= T_STRUCT;
514 type->bt = LONGSZ;
515 return 0;
516 case TOK_ENUM:
517 tok_get();
518 tok_jmp(TOK_NAME);
519 if (tok_see() == '{')
520 enum_create();
521 type->bt = 4 | BT_SIGNED;
522 return 0;
523 default:
524 if (tok_see() == TOK_NAME) {
525 int id = typedef_find(tok_id());
526 if (id != -1) {
527 tok_get();
528 memcpy(type, &typedefs[id].type,
529 sizeof(*type));
530 return 0;
533 if (!i)
534 return 1;
535 done = 1;
536 continue;
538 i++;
539 tok_get();
541 type->bt = size | (sign ? BT_SIGNED : 0);
542 return 0;
545 static int readname(struct type *main, char *name,
546 struct type *base, unsigned flags);
548 static int readtype(struct type *type)
550 return readname(type, NULL, NULL, 0);
553 static void readptrs(struct type *type)
555 while (!tok_jmp('*')) {
556 type->ptr++;
557 if (!type->bt)
558 type->bt = 1;
562 /* used to differenciate labels from case and cond exprs */
563 static int ncexpr;
564 static int caseexpr;
566 static void readpre(void);
568 static char *tmp_str(char *buf, int len)
570 static char name[NAMELEN];
571 static int id;
572 void *dat;
573 sprintf(name, "__neatcc.s%d", id++);
574 dat = o_mkdat(name, len, 0);
575 memcpy(dat, buf, len);
576 return name;
579 static void readprimary(void)
581 int i;
582 if (!tok_jmp(TOK_NUM)) {
583 long n;
584 int bt = tok_num(&n);
585 ts_push_bt(bt);
586 o_num(n);
587 return;
589 if (!tok_jmp(TOK_STR)) {
590 struct type t;
591 char buf[BUFSIZE];
592 int len;
593 t.bt = 1 | BT_SIGNED;
594 t.ptr = 1;
595 t.addr = 0;
596 t.flags = 0;
597 ts_push(&t);
598 len = tok_str(buf);
599 o_sym(tmp_str(buf, len));
600 return;
602 if (!tok_jmp(TOK_NAME)) {
603 struct name unkn = {""};
604 char *name = unkn.name;
605 int n;
606 strcpy(name, tok_id());
607 /* don't search for labels here */
608 if (!ncexpr && !caseexpr && tok_see() == ':')
609 return;
610 for (i = nlocals - 1; i >= 0; --i) {
611 struct type *t = &locals[i].type;
612 if (!strcmp(locals[i].name, name)) {
613 o_local(locals[i].addr);
614 ts_push_addr(t);
615 return;
618 if ((n = global_find(name)) != -1) {
619 struct name *g = &globals[n];
620 struct type *t = &g->type;
621 char *elfname = *g->elfname ? g->elfname : g->name;
622 o_sym(elfname);
623 ts_push_addr(t);
624 return;
626 if (!enum_find(&n, name)) {
627 ts_push_bt(4 | BT_SIGNED);
628 o_num(n);
629 return;
631 if (tok_see() != '(')
632 err("unknown symbol\n");
633 global_add(&unkn);
634 ts_push_bt(LONGSZ);
635 o_sym(unkn.name);
636 return;
638 if (!tok_jmp('(')) {
639 struct type t;
640 if (!readtype(&t)) {
641 struct type o;
642 tok_expect(')');
643 readpre();
644 ts_pop_de(&o);
645 ts_push(&t);
646 if (!t.ptr || !o.ptr)
647 o_cast(TYPE_BT(&t));
648 } else {
649 readexpr();
650 tok_expect(')');
652 return;
656 static void arrayderef(void)
658 struct type t;
659 int sz;
660 ts_pop_de(NULL);
661 ts_pop(&t);
662 if (!(t.flags & T_ARRAY) && t.addr) {
663 o_tmpswap();
664 o_deref(TYPE_BT(&t));
665 o_tmpswap();
667 array2ptr(&t);
668 t.ptr--;
669 sz = type_totsz(&t);
670 t.addr = 1;
671 if (sz > 1) {
672 o_num(sz);
673 o_bop(O_MUL);
675 o_bop(O_ADD);
676 ts_push(&t);
679 static void inc_post(int op)
681 struct type t = ts[nts - 1];
682 /* pushing the value before inc */
683 o_tmpcopy();
684 ts_de(1);
685 o_load();
686 o_tmpswap();
688 /* increment by 1 or pointer size */
689 o_tmpcopy();
690 ts_push(&t);
691 ts_pop_de(&t);
692 o_num(t.ptr > 0 ? type_szde(&t) : 1);
693 o_bop(op);
695 /* assign back */
696 o_assign(TYPE_BT(&t));
697 o_tmpdrop(1);
700 static void readfield(void)
702 struct name *field;
703 struct type t;
704 tok_expect(TOK_NAME);
705 ts_pop(&t);
706 array2ptr(&t);
707 field = struct_field(t.id, tok_id());
708 if (field->addr) {
709 o_num(field->addr);
710 o_bop(O_ADD);
712 ts_push_addr(&field->type);
715 #define MAXFUNCS (1 << 10)
717 static struct funcinfo {
718 struct type args[MAXFIELDS];
719 struct type ret;
720 int nargs;
721 int varg;
722 } funcs[MAXFUNCS];
723 static int nfuncs;
725 static int func_create(struct type *ret, struct name *args, int nargs)
727 struct funcinfo *fi = &funcs[nfuncs++];
728 int i;
729 if (nfuncs >= MAXFUNCS)
730 err("nomem: MAXFUNCS reached!\n");
731 memcpy(&fi->ret, ret, sizeof(*ret));
732 for (i = 0; i < nargs; i++)
733 memcpy(&fi->args[i], &args[i].type, sizeof(*ret));
734 fi->nargs = nargs;
735 return fi - funcs;
738 static void readcall(void)
740 struct type t;
741 struct funcinfo *fi;
742 int argc = 0;
743 ts_pop(&t);
744 if (t.flags & T_FUNC && t.ptr > 0)
745 o_deref(LONGSZ);
746 fi = t.flags & T_FUNC ? &funcs[t.id] : NULL;
747 if (tok_see() != ')') {
748 do {
749 readexpr();
750 ts_pop_de(NULL);
751 argc++;
752 } while (!tok_jmp(','));
754 tok_expect(')');
755 o_call(argc, fi ? TYPE_BT(&fi->ret) : 4 | BT_SIGNED);
756 if (fi) {
757 if (TYPE_BT(&fi->ret))
758 o_cast(TYPE_BT(&fi->ret));
759 ts_push(&fi->ret);
760 } else {
761 ts_push_bt(4 | BT_SIGNED);
765 static void readpost(void)
767 readprimary();
768 while (1) {
769 if (!tok_jmp('[')) {
770 readexpr();
771 tok_expect(']');
772 arrayderef();
773 continue;
775 if (!tok_jmp('(')) {
776 readcall();
777 continue;
779 if (!tok_jmp(TOK2("++"))) {
780 inc_post(O_ADD);
781 continue;
783 if (!tok_jmp(TOK2("--"))) {
784 inc_post(O_SUB);
785 continue;
787 if (!tok_jmp('.')) {
788 readfield();
789 continue;
791 if (!tok_jmp(TOK2("->"))) {
792 ts_de(1);
793 readfield();
794 continue;
796 break;
800 static void inc_pre(int op)
802 struct type t;
803 readpre();
804 /* copy the destination */
805 o_tmpcopy();
806 ts_push(&ts[nts - 1]);
807 /* increment by 1 or pointer size */
808 ts_pop_de(&t);
809 o_num(t.ptr > 0 ? type_szde(&t) : 1);
810 o_bop(op);
811 /* assign the result */
812 o_assign(TYPE_BT(&t));
813 ts_de(0);
816 static void readpre(void)
818 if (!tok_jmp('&')) {
819 struct type type;
820 readpre();
821 ts_pop(&type);
822 if (!type.addr)
823 err("cannot use the address\n");
824 type.ptr++;
825 type.addr = 0;
826 ts_push(&type);
827 return;
829 if (!tok_jmp('*')) {
830 struct type t;
831 readpre();
832 ts_pop(&t);
833 array2ptr(&t);
834 if (!t.ptr)
835 err("dereferencing non-pointer\n");
836 if (t.addr)
837 o_deref(TYPE_BT(&t));
838 t.ptr--;
839 t.addr = 1;
840 ts_push(&t);
841 return;
843 if (!tok_jmp('!')) {
844 readpre();
845 ts_pop_de(NULL);
846 o_uop(O_LNOT);
847 ts_push_bt(4 | BT_SIGNED);
848 return;
850 if (!tok_jmp('-')) {
851 readpre();
852 ts_de(1);
853 o_uop(O_NEG);
854 return;
856 if (!tok_jmp('~')) {
857 readpre();
858 ts_de(1);
859 o_uop(O_NOT);
860 return;
862 if (!tok_jmp(TOK2("++"))) {
863 inc_pre(O_ADD);
864 return;
866 if (!tok_jmp(TOK2("--"))) {
867 inc_pre(O_SUB);
868 return;
870 if (!tok_jmp(TOK_SIZEOF)) {
871 struct type t;
872 int op = !tok_jmp('(');
873 if (readtype(&t)) {
874 nogen++;
875 if (op)
876 readexpr();
877 else
878 readpre();
879 nogen--;
880 ts_pop(&t);
882 ts_push_bt(4);
883 o_num(type_totsz(&t));
884 if (op)
885 tok_expect(')');
886 return;
888 readpost();
891 static void readmul(void)
893 readpre();
894 while (1) {
895 if (!tok_jmp('*')) {
896 readpre();
897 ts_binop(O_MUL);
898 continue;
900 if (!tok_jmp('/')) {
901 readpre();
902 ts_binop(O_DIV);
903 continue;
905 if (!tok_jmp('%')) {
906 readpre();
907 ts_binop(O_MOD);
908 continue;
910 break;
914 static void readadd(void)
916 readmul();
917 while (1) {
918 if (!tok_jmp('+')) {
919 readmul();
920 ts_addop(O_ADD);
921 continue;
923 if (!tok_jmp('-')) {
924 readmul();
925 ts_addop(O_SUB);
926 continue;
928 break;
932 static void shift(int op)
934 struct type t;
935 readadd();
936 ts_pop_de2(NULL, &t);
937 o_bop(op | (BT_SIGNED & TYPE_BT(&t) ? O_SIGNED : 0));
938 ts_push_bt(TYPE_BT(&t));
941 static void readshift(void)
943 readadd();
944 while (1) {
945 if (!tok_jmp(TOK2("<<"))) {
946 shift(O_SHL);
947 continue;
949 if (!tok_jmp(TOK2(">>"))) {
950 shift(O_SHR);
951 continue;
953 break;
957 static void cmp(int op)
959 struct type t1, t2;
960 int bt;
961 readshift();
962 ts_pop_de2(&t1, &t2);
963 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
964 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
965 ts_push_bt(4 | BT_SIGNED);
968 static void readcmp(void)
970 readshift();
971 while (1) {
972 if (!tok_jmp('<')) {
973 cmp(O_LT);
974 continue;
976 if (!tok_jmp('>')) {
977 cmp(O_GT);
978 continue;
980 if (!tok_jmp(TOK2("<="))) {
981 cmp(O_LE);
982 continue;
984 if (!tok_jmp(TOK2(">="))) {
985 cmp(O_GE);
986 continue;
988 break;
992 static void eq(int op)
994 readcmp();
995 ts_pop_de2(NULL, NULL);
996 o_bop(op);
997 ts_push_bt(4 | BT_SIGNED);
1000 static void readeq(void)
1002 readcmp();
1003 while (1) {
1004 if (!tok_jmp(TOK2("=="))) {
1005 eq(O_EQ);
1006 continue;
1008 if (!tok_jmp(TOK2("!="))) {
1009 eq(O_NEQ);
1010 continue;
1012 break;
1016 static void readbitand(void)
1018 readeq();
1019 while (!tok_jmp('&')) {
1020 readeq();
1021 ts_binop(O_AND);
1025 static void readxor(void)
1027 readbitand();
1028 while (!tok_jmp('^')) {
1029 readbitand();
1030 ts_binop(O_XOR);
1034 static void readbitor(void)
1036 readxor();
1037 while (!tok_jmp('|')) {
1038 readxor();
1039 ts_binop(O_OR);
1043 #define MAXCOND (1 << 7)
1045 static void readand(void)
1047 long conds[MAXCOND];
1048 int nconds = 0;
1049 long passed;
1050 int i;
1051 readbitor();
1052 if (tok_see() != TOK2("&&"))
1053 return;
1054 o_fork();
1055 ts_pop_de(NULL);
1056 conds[nconds++] = o_jz(0);
1057 while (!tok_jmp(TOK2("&&"))) {
1058 readbitor();
1059 ts_pop_de(NULL);
1060 conds[nconds++] = o_jz(0);
1062 o_num(1);
1063 o_forkpush();
1064 passed = o_jmp(0);
1065 for (i = 0; i < nconds; i++)
1066 o_filljmp(conds[i]);
1067 o_num(0);
1068 o_forkpush();
1069 o_forkjoin();
1070 o_filljmp(passed);
1071 ts_push_bt(4 | BT_SIGNED);
1074 static void reador(void)
1076 long conds[MAXCOND];
1077 int nconds = 0;
1078 long failed;
1079 int i;
1080 readand();
1081 if (tok_see() != TOK2("||"))
1082 return;
1083 o_fork();
1084 ts_pop_de(NULL);
1085 conds[nconds++] = o_jnz(0);
1086 while (!tok_jmp(TOK2("||"))) {
1087 readand();
1088 ts_pop_de(NULL);
1089 conds[nconds++] = o_jnz(0);
1091 o_num(0);
1092 o_forkpush();
1093 failed = o_jmp(0);
1094 for (i = 0; i < nconds; i++)
1095 o_filljmp(conds[i]);
1096 o_num(1);
1097 o_forkpush();
1098 o_forkjoin();
1099 o_filljmp(failed);
1100 ts_push_bt(4 | BT_SIGNED);
1103 static void readcexpr(void);
1105 static int readcexpr_const(void)
1107 long c;
1108 if (o_popnum(&c))
1109 return -1;
1110 if (!c)
1111 nogen++;
1112 readcexpr();
1113 /* both branches yield the same type; so ignore the first */
1114 ts_pop_de(NULL);
1115 tok_expect(':');
1116 if (c)
1117 nogen++;
1118 else
1119 nogen--;
1120 readcexpr();
1121 /* making sure t->addr == 0 on both branches */
1122 ts_de(1);
1123 if (c)
1124 nogen--;
1125 return 0;
1128 static void readcexpr(void)
1130 long l1, l2;
1131 reador();
1132 if (tok_jmp('?'))
1133 return;
1134 ncexpr++;
1135 ts_pop_de(NULL);
1136 o_fork();
1137 if (readcexpr_const()) {
1138 l1 = o_jz(0);
1139 readcexpr();
1140 /* both branches yield the same type; so ignore the first */
1141 ts_pop_de(NULL);
1142 o_forkpush();
1143 l2 = o_jmp(0);
1145 tok_expect(':');
1146 o_filljmp(l1);
1147 readcexpr();
1148 /* making sure t->addr == 0 on both branches */
1149 ts_de(1);
1150 o_forkpush();
1151 o_forkjoin();
1152 o_filljmp(l2);
1154 ncexpr--;
1157 static void opassign(int op, int ptrop)
1159 struct type t = ts[nts - 1];
1160 o_tmpcopy();
1161 ts_push(&t);
1162 readexpr();
1163 ts_addop(op);
1164 o_assign(TYPE_BT(&ts[nts - 2]));
1165 ts_pop(NULL);
1166 ts_de(0);
1169 static void doassign(void)
1171 struct type t = ts[nts - 1];
1172 if (!t.ptr && t.flags & T_STRUCT) {
1173 ts_pop(NULL);
1174 o_num(type_totsz(&t));
1175 o_memcpy();
1176 } else {
1177 ts_pop_de(NULL);
1178 o_assign(TYPE_BT(&ts[nts - 1]));
1179 ts_de(0);
1183 static void readexpr(void)
1185 readcexpr();
1186 if (!tok_jmp('=')) {
1187 readexpr();
1188 doassign();
1189 return;
1191 if (!tok_jmp(TOK2("+="))) {
1192 opassign(O_ADD, 1);
1193 return;
1195 if (!tok_jmp(TOK2("-="))) {
1196 opassign(O_SUB, 1);
1197 return;
1199 if (!tok_jmp(TOK2("*="))) {
1200 opassign(O_MUL, 0);
1201 return;
1203 if (!tok_jmp(TOK2("/="))) {
1204 opassign(O_DIV, 0);
1205 return;
1207 if (!tok_jmp(TOK2("%="))) {
1208 opassign(O_MOD, 0);
1209 return;
1211 if (!tok_jmp(TOK3("<<="))) {
1212 opassign(O_SHL, 0);
1213 return;
1215 if (!tok_jmp(TOK3(">>="))) {
1216 opassign(O_SHR, 0);
1217 return;
1219 if (!tok_jmp(TOK3("&="))) {
1220 opassign(O_AND, 0);
1221 return;
1223 if (!tok_jmp(TOK3("|="))) {
1224 opassign(O_OR, 0);
1225 return;
1227 if (!tok_jmp(TOK3("^="))) {
1228 opassign(O_XOR, 0);
1229 return;
1233 static void readestmt(void)
1235 do {
1236 o_tmpdrop(-1);
1237 nts = 0;
1238 readexpr();
1239 } while (!tok_jmp(','));
1242 static void o_localoff(long addr, int off)
1244 o_local(addr);
1245 if (off) {
1246 o_num(off);
1247 o_bop(O_ADD);
1251 static struct type *innertype(struct type *t)
1253 if (t->flags & T_ARRAY && !t->ptr)
1254 return innertype(&arrays[t->id].type);
1255 return t;
1258 static void initexpr(struct type *t, int off, void *obj,
1259 void (*set)(void *obj, int off, struct type *t))
1261 if (tok_jmp('{')) {
1262 set(obj, off, t);
1263 return;
1265 if (!t->ptr && t->flags & T_STRUCT) {
1266 struct structinfo *si = &structs[t->id];
1267 int i;
1268 for (i = 0; i < si->nfields; i++) {
1269 struct name *field = &si->fields[i];
1270 if (!tok_jmp('.')) {
1271 tok_expect(TOK_NAME);
1272 field = struct_field(t->id, tok_id());
1273 tok_expect('=');
1275 initexpr(&field->type, off + field->addr, obj, set);
1276 if (tok_jmp(',') || tok_see() == '}')
1277 break;
1279 } else if (t->flags & T_ARRAY) {
1280 struct type *t_de = &arrays[t->id].type;
1281 int i;
1282 for (i = 0; ; i++) {
1283 long idx = i;
1284 struct type *it = t_de;
1285 if (!tok_jmp('[')) {
1286 readexpr();
1287 ts_pop_de(NULL);
1288 o_popnum(&idx);
1289 tok_expect(']');
1290 tok_expect('=');
1292 if (tok_see() != '{')
1293 it = innertype(t_de);
1294 initexpr(it, off + type_totsz(it) * idx, obj, set);
1295 if (tok_jmp(',') || tok_see() == '}')
1296 break;
1299 tok_expect('}');
1302 static void jumpbrace(void)
1304 int depth = 0;
1305 while (tok_see() != '}' || depth--)
1306 if (tok_get() == '{')
1307 depth++;
1308 tok_expect('}');
1311 static int initsize(void)
1313 long addr = tok_addr();
1314 int n = 0;
1315 if (!tok_jmp(TOK_STR)) {
1316 n = tok_str(NULL);
1317 tok_jump(addr);
1318 return n;
1320 tok_expect('{');
1321 while (tok_jmp('}')) {
1322 long idx = n;
1323 if (!tok_jmp('[')) {
1324 readexpr();
1325 ts_pop_de(NULL);
1326 o_popnum(&idx);
1327 tok_expect(']');
1328 tok_expect('=');
1330 if (n < idx + 1)
1331 n = idx + 1;
1332 while (tok_see() != '}' && tok_see() != ',')
1333 if (tok_get() == '{')
1334 jumpbrace();
1335 tok_jmp(',');
1337 tok_jump(addr);
1338 return n;
1341 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1343 static void globalinit(void *obj, int off, struct type *t)
1345 struct name *name = obj;
1346 char *elfname = *name->elfname ? name->elfname : name->name;
1347 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1348 struct type *t_de = &arrays[t->id].type;
1349 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1350 char buf[BUFSIZE];
1351 int len;
1352 tok_expect(TOK_STR);
1353 len = tok_str(buf);
1354 memcpy((void *) name->addr + off, buf, len);
1355 return;
1358 readexpr();
1359 o_datset(elfname, off, TYPE_BT(t));
1360 ts_pop(NULL);
1363 static void globaldef(void *data, struct name *name, unsigned flags)
1365 struct type *t = &name->type;
1366 char *elfname = *name->elfname ? name->elfname : name->name;
1367 int sz;
1368 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1369 if (~flags & F_EXTERN)
1370 arrays[t->id].n = initsize();
1371 sz = type_totsz(t);
1372 if (!(flags & F_EXTERN) && (!(t->flags & T_FUNC) || t->ptr)) {
1373 if (flags & F_INIT)
1374 name->addr = (long) o_mkdat(elfname, sz, F_GLOBAL(flags));
1375 else
1376 o_mkbss(elfname, sz, F_GLOBAL(flags));
1378 global_add(name);
1379 if (flags & F_INIT)
1380 initexpr(t, 0, name, globalinit);
1383 static void localinit(void *obj, int off, struct type *t)
1385 long addr = *(long *) obj;
1386 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1387 struct type *t_de = &arrays[t->id].type;
1388 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1389 char buf[BUFSIZE];
1390 int len;
1391 tok_expect(TOK_STR);
1392 len = tok_str(buf);
1393 o_localoff(addr, off);
1394 o_sym(tmp_str(buf, len));
1395 o_num(len);
1396 o_memcpy();
1397 o_tmpdrop(1);
1398 return;
1401 o_localoff(addr, off);
1402 ts_push(t);
1403 readexpr();
1404 doassign();
1405 ts_pop(NULL);
1406 o_tmpdrop(1);
1409 /* current function name */
1410 static char func_name[NAMELEN];
1412 static void localdef(void *data, struct name *name, unsigned flags)
1414 struct type *t = &name->type;
1415 if ((flags & F_EXTERN) || (t->flags & T_FUNC) && !t->ptr) {
1416 global_add(name);
1417 return;
1419 if (flags & F_STATIC) {
1420 sprintf(name->elfname, "__neatcc.%s.%s", func_name, name->name);
1421 globaldef(data, name, flags);
1422 return;
1424 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1425 arrays[t->id].n = initsize();
1426 name->addr = o_mklocal(type_totsz(&name->type));
1427 local_add(name);
1428 if (flags & F_INIT) {
1429 if (t->flags & (T_ARRAY | T_STRUCT) && !t->ptr) {
1430 o_local(name->addr);
1431 o_num(0);
1432 o_num(type_totsz(t));
1433 o_memset();
1434 o_tmpdrop(1);
1436 initexpr(t, 0, &name->addr, localinit);
1440 static void funcdef(char *name, struct type *type, struct name *args,
1441 int nargs, int varg, unsigned flags)
1443 struct name global = {""};
1444 int i;
1445 strcpy(global.name, name);
1446 strcpy(func_name, name);
1447 memcpy(&global.type, type, sizeof(*type));
1448 o_func_beg(name, nargs, F_GLOBAL(flags), varg);
1449 global_add(&global);
1450 for (i = 0; i < nargs; i++) {
1451 args[i].addr = o_arg2loc(i);
1452 local_add(&args[i]);
1456 static int readargs(struct name *args, int *varg)
1458 int nargs = 0;
1459 tok_expect('(');
1460 *varg = 0;
1461 while (tok_see() != ')') {
1462 if (!tok_jmp(TOK3("..."))) {
1463 *varg = 1;
1464 break;
1466 readname(&args[nargs].type, args[nargs].name, NULL, 0);
1467 array2ptr(&args[nargs].type);
1468 nargs++;
1469 if (tok_jmp(','))
1470 break;
1472 tok_expect(')');
1473 if (nargs == 1 && !TYPE_BT(&args[0].type))
1474 return 0;
1475 return nargs;
1478 static int readname(struct type *main, char *name,
1479 struct type *base, unsigned flags)
1481 struct type tpool[3];
1482 int npool = 0;
1483 struct type *type = &tpool[npool++];
1484 struct type *func = NULL;
1485 struct type *ret = NULL;
1486 int arsz[10];
1487 int nar = 0;
1488 int i;
1489 memset(tpool, 0, sizeof(tpool));
1490 if (name)
1491 *name = '\0';
1492 if (!base) {
1493 if (basetype(type, &flags))
1494 return 1;
1495 } else {
1496 memcpy(type, base, sizeof(*base));
1498 readptrs(type);
1499 if (!tok_jmp('(')) {
1500 ret = type;
1501 type = &tpool[npool++];
1502 func = type;
1503 readptrs(type);
1505 if (!tok_jmp(TOK_NAME) && name)
1506 strcpy(name, tok_id());
1507 while (!tok_jmp('[')) {
1508 long n = 0;
1509 if (tok_jmp(']')) {
1510 readexpr();
1511 ts_pop_de(NULL);
1512 if (o_popnum(&n))
1513 err("const expr expected\n");
1514 tok_expect(']');
1516 arsz[nar++] = n;
1518 for (i = nar - 1; i >= 0; i--) {
1519 type->id = array_add(type, arsz[i]);
1520 if (func && i == nar - 1)
1521 func = &arrays[type->id].type;
1522 type->flags = T_ARRAY;
1523 type->bt = LONGSZ;
1524 type->ptr = 0;
1526 if (func)
1527 tok_expect(')');
1528 if (tok_see() == '(') {
1529 struct name args[MAXARGS] = {{""}};
1530 int varg = 0;
1531 int nargs = readargs(args, &varg);
1532 int fdef = !func;
1533 if (!func) {
1534 ret = type;
1535 type = &tpool[npool++];
1536 func = type;
1538 func->flags = T_FUNC;
1539 func->bt = LONGSZ;
1540 func->id = func_create(ret, args, nargs);
1541 if (fdef && tok_see() == '{') {
1542 funcdef(name, func, args, nargs, varg, flags);
1543 return 1;
1546 memcpy(main, type, sizeof(*type));
1547 return 0;
1550 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
1551 void *data)
1553 struct type base;
1554 unsigned base_flags;
1555 if (basetype(&base, &base_flags))
1556 return 1;
1557 while (tok_see() != ';' && tok_see() != '{') {
1558 struct name name = {{""}};
1559 unsigned flags = base_flags;
1560 if (readname(&name.type, name.name, &base, flags))
1561 break;
1562 if (!tok_jmp('='))
1563 flags |= F_INIT;
1564 def(data, &name, flags);
1565 tok_jmp(',');
1567 return 0;
1570 static void typedefdef(void *data, struct name *name, unsigned flags)
1572 typedef_add(name->name, &name->type);
1575 static void readstmt(void);
1577 #define MAXCASES (1 << 7)
1579 static void readswitch(void)
1581 int break_beg = nbreaks;
1582 long val_addr = o_mklocal(LONGSZ);
1583 struct type t;
1584 int ncases = 0; /* number of case labels */
1585 long last_failed = -1; /* address of last failed jmp */
1586 long last_matched = -1; /* address of last walk through jmp */
1587 long default_addr = -1; /* address of the default label */
1588 tok_expect('(');
1589 readexpr();
1590 ts_pop_de(&t);
1591 o_local(val_addr);
1592 o_tmpswap();
1593 o_assign(TYPE_BT(&t));
1594 ts_de(0);
1595 o_tmpdrop(1);
1596 tok_expect(')');
1597 tok_expect('{');
1598 while (tok_jmp('}')) {
1599 if (tok_see() != TOK_CASE && tok_see() != TOK_DEFAULT) {
1600 readstmt();
1601 continue;
1603 if (ncases)
1604 last_matched = o_jmp(0);
1605 if (tok_get() == TOK_CASE) {
1606 if (last_failed >= 0)
1607 o_filljmp(last_failed);
1608 caseexpr = 1;
1609 readexpr();
1610 ts_pop_de(NULL);
1611 caseexpr = 0;
1612 o_local(val_addr);
1613 o_deref(TYPE_BT(&t));
1614 o_bop(O_EQ);
1615 last_failed = o_jz(0);
1616 o_tmpdrop(1);
1617 } else {
1618 default_addr = o_mklabel();
1620 tok_expect(':');
1621 if (last_matched >= 0)
1622 o_filljmp(last_matched);
1623 ncases++;
1625 o_rmlocal(val_addr, LONGSZ);
1626 if (last_failed >= 0)
1627 o_filljmp2(last_failed,
1628 default_addr >= 0 ? default_addr : o_mklabel());
1629 break_fill(o_mklabel(), break_beg);
1632 #define MAXGOTO (1 << 10)
1634 static struct gotoinfo {
1635 char name[NAMELEN];
1636 long addr;
1637 } gotos[MAXGOTO];
1638 static int ngotos;
1640 static struct labelinfo {
1641 char name[NAMELEN];
1642 long addr;
1643 } labels[MAXGOTO];
1644 static int nlabels;
1646 static void goto_add(char *name)
1648 strcpy(gotos[ngotos].name, name);
1649 gotos[ngotos++].addr = o_jmp(0);
1652 static void label_add(char *name)
1654 strcpy(labels[nlabels].name, name);
1655 labels[nlabels++].addr = o_mklabel();
1658 static void goto_fill(void)
1660 int i, j;
1661 for (i = 0; i < ngotos; i++)
1662 for (j = 0; j < nlabels; j++)
1663 if (!strcmp(gotos[i].name, labels[j].name)) {
1664 o_filljmp2(gotos[i].addr, labels[j].addr);
1665 break;
1669 static void readstmt(void)
1671 o_tmpdrop(-1);
1672 nts = 0;
1673 if (!tok_jmp('{')) {
1674 int _nlocals = nlocals;
1675 int _nglobals = nglobals;
1676 int _nenums = nenums;
1677 int _ntypedefs = ntypedefs;
1678 int _nstructs = nstructs;
1679 int _nfuncs = nfuncs;
1680 int _narrays = narrays;
1681 while (tok_jmp('}'))
1682 readstmt();
1683 nlocals = _nlocals;
1684 nenums = _nenums;
1685 ntypedefs = _ntypedefs;
1686 nstructs = _nstructs;
1687 nfuncs = _nfuncs;
1688 narrays = _narrays;
1689 nglobals = _nglobals;
1690 return;
1692 if (!readdefs(localdef, NULL)) {
1693 tok_expect(';');
1694 return;
1696 if (!tok_jmp(TOK_TYPEDEF)) {
1697 readdefs(typedefdef, NULL);
1698 tok_expect(';');
1699 return;
1701 if (!tok_jmp(TOK_IF)) {
1702 long l1, l2;
1703 tok_expect('(');
1704 readexpr();
1705 tok_expect(')');
1706 ts_pop_de(NULL);
1707 l1 = o_jz(0);
1708 readstmt();
1709 if (!tok_jmp(TOK_ELSE)) {
1710 l2 = o_jmp(0);
1711 o_filljmp(l1);
1712 readstmt();
1713 o_filljmp(l2);
1714 } else {
1715 o_filljmp(l1);
1717 return;
1719 if (!tok_jmp(TOK_WHILE)) {
1720 long l1, l2;
1721 int break_beg = nbreaks;
1722 int continue_beg = ncontinues;
1723 l1 = o_mklabel();
1724 tok_expect('(');
1725 readexpr();
1726 tok_expect(')');
1727 ts_pop_de(NULL);
1728 l2 = o_jz(0);
1729 readstmt();
1730 o_jmp(l1);
1731 o_filljmp(l2);
1732 break_fill(o_mklabel(), break_beg);
1733 continue_fill(l1, continue_beg);
1734 return;
1736 if (!tok_jmp(TOK_DO)) {
1737 long l1, l2;
1738 int break_beg = nbreaks;
1739 int continue_beg = ncontinues;
1740 l1 = o_mklabel();
1741 readstmt();
1742 tok_expect(TOK_WHILE);
1743 tok_expect('(');
1744 l2 = o_mklabel();
1745 readexpr();
1746 ts_pop_de(NULL);
1747 o_jnz(l1);
1748 tok_expect(')');
1749 break_fill(o_mklabel(), break_beg);
1750 continue_fill(l2, continue_beg);
1751 tok_expect(';');
1752 return;
1754 if (!tok_jmp(TOK_FOR)) {
1755 long l_check, l_jump, j_fail, j_pass;
1756 int break_beg = nbreaks;
1757 int continue_beg = ncontinues;
1758 int has_cond = 0;
1759 tok_expect('(');
1760 if (tok_see() != ';')
1761 readestmt();
1762 tok_expect(';');
1763 l_check = o_mklabel();
1764 if (tok_see() != ';') {
1765 readestmt();
1766 ts_pop_de(NULL);
1767 j_fail = o_jz(0);
1768 has_cond = 1;
1770 tok_expect(';');
1771 j_pass = o_jmp(0);
1772 l_jump = o_mklabel();
1773 if (tok_see() != ')')
1774 readestmt();
1775 tok_expect(')');
1776 o_jmp(l_check);
1777 o_filljmp(j_pass);
1778 readstmt();
1779 o_jmp(l_jump);
1780 if (has_cond)
1781 o_filljmp(j_fail);
1782 break_fill(o_mklabel(), break_beg);
1783 continue_fill(l_jump, continue_beg);
1784 return;
1786 if (!tok_jmp(TOK_SWITCH)) {
1787 readswitch();
1788 return;
1790 if (!tok_jmp(TOK_RETURN)) {
1791 int ret = tok_see() != ';';
1792 if (ret) {
1793 readexpr();
1794 ts_pop_de(NULL);
1796 tok_expect(';');
1797 o_ret(ret);
1798 return;
1800 if (!tok_jmp(TOK_BREAK)) {
1801 tok_expect(';');
1802 breaks[nbreaks++] = o_jmp(0);
1803 return;
1805 if (!tok_jmp(TOK_CONTINUE)) {
1806 tok_expect(';');
1807 continues[ncontinues++] = o_jmp(0);
1808 return;
1810 if (!tok_jmp(TOK_GOTO)) {
1811 tok_expect(TOK_NAME);
1812 goto_add(tok_id());
1813 tok_expect(';');
1814 return;
1816 readestmt();
1817 /* labels */
1818 if (!tok_jmp(':')) {
1819 label_add(tok_id());
1820 return;
1822 tok_expect(';');
1825 static void readdecl(void)
1827 if (!tok_jmp(TOK_TYPEDEF)) {
1828 readdefs(typedefdef, NULL);
1829 tok_expect(';');
1830 return;
1832 readdefs(globaldef, NULL);
1833 if (tok_see() == '{') {
1834 readstmt();
1835 goto_fill();
1836 o_func_end();
1837 func_name[0] = '\0';
1838 nlocals = 0;
1839 ngotos = 0;
1840 nlabels = 0;
1841 return;
1843 tok_expect(';');
1846 static void parse(void)
1848 while (tok_see() != TOK_EOF)
1849 readdecl();
1852 static void compat_macros(void)
1854 cpp_define("__STDC__", "");
1855 cpp_define("__arm__", "");
1856 cpp_define("__linux__", "");
1858 /* ignored keywords */
1859 cpp_define("const", "");
1860 cpp_define("register", "");
1861 cpp_define("volatile", "");
1862 cpp_define("inline", "");
1863 cpp_define("restrict", "");
1864 cpp_define("__inline__", "");
1865 cpp_define("__restrict__", "");
1866 cpp_define("__attribute__(x)", "");
1867 cpp_define("__builtin_va_list__", "long");
1870 int main(int argc, char *argv[])
1872 char obj[128] = "";
1873 int ofd;
1874 int i = 1;
1875 compat_macros();
1876 while (i < argc && argv[i][0] == '-') {
1877 if (argv[i][1] == 'I')
1878 cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]);
1879 if (argv[i][1] == 'D') {
1880 char *name = argv[i] + 2;
1881 char *def = "";
1882 char *eq = strchr(name, '=');
1883 if (eq) {
1884 *eq = '\0';
1885 def = eq + 1;
1887 cpp_define(name, def);
1889 if (argv[i][1] == 'o')
1890 strcpy(obj, argv[i][2] ? argv[i] + 2 : argv[++i]);
1891 i++;
1893 if (i == argc)
1894 die("neatcc: no file given\n");
1895 if (cpp_init(argv[i]))
1896 die("neatcc: cannot open <%s>\n", argv[i]);
1897 parse();
1898 if (!*obj) {
1899 strcpy(obj, argv[i]);
1900 obj[strlen(obj) - 1] = 'o';
1902 ofd = open(obj, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1903 o_write(ofd);
1904 close(ofd);
1905 return 0;