tok: support hex constants with capital X
[neatcc.git] / ncc.c
blob57ce54c01d282c66db869794f277d92bc12ea24a
1 /*
2 * neatcc - the neatcc compiler
4 * Copyright (C) 2010-2014 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
7 */
8 /*
9 * neatcc parser
11 * The parser reads tokens from the tokenizer (tok_*) and calls the
12 * appropriate code generation functions (o_*). The generator
13 * maintains a stack of values pushed via, for instance, o_num()
14 * and generates the necessary code for the accesses to the items
15 * in this stack, like o_bop() for performing a binary operations
16 * on the top two items of the stack. The parser maintains the
17 * types of values pushed to the generator stack in its type stack
18 * (ts_*). For instance, for binary operations two types are
19 * popped first and the resulting type is pushed to the type stack
20 * (ts_binop()).
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include "gen.h"
32 #include "ncc.h"
33 #include "out.h"
34 #include "tok.h"
36 static int nogen; /* do not generate code, if set */
37 #define o_bop(op) {if (!nogen) o_bop(op);}
38 #define o_uop(op) {if (!nogen) o_uop(op);}
39 #define o_cast(bt) {if (!nogen) o_cast(bt);}
40 #define o_memcpy() {if (!nogen) o_memcpy();}
41 #define o_memset() {if (!nogen) o_memset();}
42 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
43 #define o_ret(ret) {if (!nogen) o_ret(ret);}
44 #define o_assign(bt) {if (!nogen) o_assign(bt);}
45 #define o_deref(bt) {if (!nogen) o_deref(bt);}
46 #define o_load() {if (!nogen) o_load();}
47 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
48 #define o_num(n) {if (!nogen) o_num(n);}
49 #define o_local(addr) {if (!nogen) o_local(addr);}
50 #define o_sym(sym) {if (!nogen) o_sym(sym);}
51 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
52 #define o_tmpswap() {if (!nogen) o_tmpswap();}
53 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
54 #define o_label(id) {if (!nogen) o_label(id);}
55 #define o_jz(id) {if (!nogen) o_jz(id);}
56 #define o_jnz(id) {if (!nogen) o_jnz(id);}
57 #define o_jmp(id) {if (!nogen) o_jmp(id);}
58 #define o_fork() {if (!nogen) o_fork();}
59 #define o_forkpush() {if (!nogen) o_forkpush();}
60 #define o_forkjoin() {if (!nogen) o_forkjoin();}
62 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #define MAX(a, b) ((a) < (b) ? (b) : (a))
66 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
67 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
68 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
70 /* type->flag values */
71 #define T_ARRAY 0x01
72 #define T_STRUCT 0x02
73 #define T_FUNC 0x04
75 /* variable definition flags */
76 #define F_STATIC 0x01
77 #define F_EXTERN 0x02
79 struct type {
80 unsigned bt;
81 unsigned flags;
82 int ptr;
83 int id; /* for structs, functions and arrays */
84 int addr; /* the address is passed to gen.c; deref for value */
87 /* type stack */
88 static struct type ts[NTMPS];
89 static int nts;
91 static void ts_push_bt(unsigned bt)
93 ts[nts].ptr = 0;
94 ts[nts].flags = 0;
95 ts[nts].addr = 0;
96 ts[nts++].bt = bt;
99 static void ts_push(struct type *t)
101 struct type *d = &ts[nts++];
102 memcpy(d, t, sizeof(*t));
105 static void ts_push_addr(struct type *t)
107 ts_push(t);
108 ts[nts - 1].addr = 1;
111 static void ts_pop(struct type *type)
113 nts--;
114 if (type)
115 *type = ts[nts];
118 void err(char *fmt, ...)
120 va_list ap;
121 char msg[512];
122 va_start(ap, fmt);
123 vsprintf(msg, fmt, ap);
124 va_end(ap);
125 die("%s: %s", cpp_loc(tok_addr()), msg);
128 struct name {
129 char name[NAMELEN];
130 char elfname[NAMELEN]; /* local elf name for function static variables */
131 struct type type;
132 long addr; /* local stack offset, global data addr, struct offset */
135 static struct name locals[NLOCALS];
136 static int nlocals;
137 static struct name globals[NGLOBALS];
138 static int nglobals;
140 static void local_add(struct name *name)
142 if (nlocals >= NLOCALS)
143 err("nomem: NLOCALS reached!\n");
144 memcpy(&locals[nlocals++], name, sizeof(*name));
147 static int local_find(char *name)
149 int i;
150 for (i = nlocals - 1; i >= 0; --i)
151 if (!strcmp(locals[i].name, name))
152 return i;
153 return -1;
156 static int global_find(char *name)
158 int i;
159 for (i = nglobals - 1; i >= 0; i--)
160 if (!strcmp(name, globals[i].name))
161 return i;
162 return -1;
165 static void global_add(struct name *name)
167 if (nglobals >= NGLOBALS)
168 err("nomem: NGLOBALS reached!\n");
169 memcpy(&globals[nglobals++], name, sizeof(*name));
172 #define LABEL() (++label)
174 static int label; /* last used label id */
175 static int l_break; /* current break label */
176 static int l_cont; /* current continue label */
178 static struct enumval {
179 char name[NAMELEN];
180 int n;
181 } enums[NENUMS];
182 static int nenums;
184 static void enum_add(char *name, int val)
186 struct enumval *ev = &enums[nenums++];
187 if (nenums >= NENUMS)
188 err("nomem: NENUMS reached!\n");
189 strcpy(ev->name, name);
190 ev->n = val;
193 static int enum_find(int *val, char *name)
195 int i;
196 for (i = nenums - 1; i >= 0; --i)
197 if (!strcmp(name, enums[i].name)) {
198 *val = enums[i].n;
199 return 0;
201 return 1;
204 static struct typdefinfo {
205 char name[NAMELEN];
206 struct type type;
207 } typedefs[NTYPEDEFS];
208 static int ntypedefs;
210 static void typedef_add(char *name, struct type *type)
212 struct typdefinfo *ti = &typedefs[ntypedefs++];
213 if (ntypedefs >= NTYPEDEFS)
214 err("nomem: NTYPEDEFS reached!\n");
215 strcpy(ti->name, name);
216 memcpy(&ti->type, type, sizeof(*type));
219 static int typedef_find(char *name)
221 int i;
222 for (i = ntypedefs - 1; i >= 0; --i)
223 if (!strcmp(name, typedefs[i].name))
224 return i;
225 return -1;
228 static struct array {
229 struct type type;
230 int n;
231 } arrays[NARRAYS];
232 static int narrays;
234 static int array_add(struct type *type, int n)
236 struct array *a = &arrays[narrays++];
237 if (narrays >= NARRAYS)
238 err("nomem: NARRAYS reached!\n");
239 memcpy(&a->type, type, sizeof(*type));
240 a->n = n;
241 return a - arrays;
244 static void array2ptr(struct type *t)
246 if (t->flags & T_ARRAY && !t->ptr) {
247 memcpy(t, &arrays[t->id].type, sizeof(*t));
248 t->ptr++;
252 static struct structinfo {
253 char name[NAMELEN];
254 struct name fields[NFIELDS];
255 int nfields;
256 int isunion;
257 int size;
258 } structs[NSTRUCTS];
259 static int nstructs;
261 static int struct_find(char *name, int isunion)
263 int i;
264 for (i = nstructs - 1; i >= 0; --i)
265 if (*structs[i].name && !strcmp(name, structs[i].name) &&
266 structs[i].isunion == isunion)
267 return i;
268 i = nstructs++;
269 if (nstructs >= NSTRUCTS)
270 err("nomem: NSTRUCTS reached!\n");
271 memset(&structs[i], 0, sizeof(structs[i]));
272 strcpy(structs[i].name, name);
273 structs[i].isunion = isunion;
274 return i;
277 static struct name *struct_field(int id, char *name)
279 struct structinfo *si = &structs[id];
280 int i;
281 for (i = 0; i < si->nfields; i++)
282 if (!strcmp(name, si->fields[i].name))
283 return &si->fields[i];
284 err("field not found\n");
285 return NULL;
288 /* return t's size */
289 static int type_totsz(struct type *t)
291 if (t->ptr)
292 return LONGSZ;
293 if (t->flags & T_ARRAY)
294 return arrays[t->id].n * type_totsz(&arrays[t->id].type);
295 return t->flags & T_STRUCT ? structs[t->id].size : BT_SZ(t->bt);
298 /* return t's dereferenced size */
299 static unsigned type_szde(struct type *t)
301 struct type de = *t;
302 array2ptr(&de);
303 de.ptr--;
304 return type_totsz(&de);
307 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
308 static void ts_de(int deref)
310 struct type *t = &ts[nts - 1];
311 array2ptr(t);
312 if (deref && t->addr && (t->ptr || !(t->flags & T_FUNC)))
313 o_deref(TYPE_BT(t));
314 t->addr = 0;
317 /* pop stack pop to *t and dereference if t->addr */
318 static void ts_pop_de(struct type *t)
320 ts_de(1);
321 ts_pop(t);
324 /* pop the top 2 stack values and dereference them if t->addr */
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 /* the result of a binary operation on variables of type bt1 and bt2 */
348 static unsigned bt_op(unsigned bt1, unsigned bt2)
350 int sz = MAX(BT_SZ(bt1), BT_SZ(bt2));
351 return ((bt1 | bt2) & BT_SIGNED) | MAX(sz, 4);
354 /* the result of a unary operation on variables of bt */
355 static unsigned bt_uop(unsigned bt)
357 return bt_op(bt, 4);
360 /* push the result of a binary operation on the type stack */
361 static void ts_binop(int op)
363 struct type t1, t2;
364 unsigned bt1, bt2, bt;
365 ts_pop_de2(&t1, &t2);
366 bt1 = TYPE_BT(&t1);
367 bt2 = TYPE_BT(&t2);
368 bt = bt_op(bt1, bt2);
369 if (op == O_DIV || op == O_MOD)
370 bt = BT(bt2 & BT_SIGNED, bt);
371 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
372 ts_push_bt(bt);
375 /* push the result of an additive binary operation on the type stack */
376 static void ts_addop(int op)
378 struct type t1, t2;
379 ts_pop_de2(&t1, &t2);
380 if (!t1.ptr && !t2.ptr) {
381 o_bop(op);
382 ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
383 return;
385 if (t1.ptr && !t2.ptr)
386 o_tmpswap();
387 if (!t1.ptr && t2.ptr)
388 if (type_szde(&t2) > 1) {
389 o_num(type_szde(&t2));
390 o_bop(O_MUL);
392 if (t1.ptr && !t2.ptr)
393 o_tmpswap();
394 o_bop(op);
395 if (t1.ptr && t2.ptr) {
396 int sz = type_szde(&t1);
397 if (sz > 1) {
398 o_num(sz);
399 o_bop(O_DIV);
401 ts_push_bt(LONGSZ | BT_SIGNED);
402 } else {
403 ts_push(t1.ptr ? &t1 : &t2);
407 /* function prototypes for parsing function and variable declarations */
408 static int readname(struct type *main, char *name, struct type *base);
409 static int readtype(struct type *type);
410 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
411 void *data);
412 static int readdefs_int(void (*def)(void *data, struct name *name, unsigned flags),
413 void *data);
415 /* function prototypes for parsing initializer expressions */
416 static int initsize(void);
417 static void initexpr(struct type *t, int off, void *obj,
418 void (*set)(void *obj, int off, struct type *t));
420 static int type_alignment(struct type *t)
422 if (t->flags & T_ARRAY && !t->ptr)
423 return type_alignment(&arrays[t->id].type);
424 if (t->flags & T_STRUCT && !t->ptr)
425 return type_alignment(&structs[t->id].fields[0].type);
426 return MIN(LONGSZ, type_totsz(t));
429 static void structdef(void *data, struct name *name, unsigned flags)
431 struct structinfo *si = data;
432 if (si->isunion) {
433 name->addr = 0;
434 if (si->size < type_totsz(&name->type))
435 si->size = type_totsz(&name->type);
436 } else {
437 struct type *t = &name->type;
438 int alignment = type_alignment(t);
439 if (t->flags & T_ARRAY && !t->ptr)
440 alignment = MIN(LONGSZ, type_totsz(&arrays[t->id].type));
441 si->size = ALIGN(si->size, alignment);
442 name->addr = si->size;
443 si->size += type_totsz(&name->type);
445 memcpy(&si->fields[si->nfields++], name, sizeof(*name));
448 static int struct_create(char *name, int isunion)
450 int id = struct_find(name, isunion);
451 struct structinfo *si = &structs[id];
452 tok_expect('{');
453 while (tok_jmp('}')) {
454 readdefs(structdef, si);
455 tok_expect(';');
457 return id;
460 static void readexpr(void);
462 static void enum_create(void)
464 long n = 0;
465 tok_expect('{');
466 while (tok_jmp('}')) {
467 char name[NAMELEN];
468 tok_expect(TOK_NAME);
469 strcpy(name, tok_id());
470 if (!tok_jmp('=')) {
471 readexpr();
472 ts_pop_de(NULL);
473 if (o_popnum(&n))
474 err("const expr expected!\n");
476 enum_add(name, n++);
477 tok_jmp(',');
481 /* used to differentiate labels from case and cond exprs */
482 static int ncexpr;
483 static int caseexpr;
485 static void readpre(void);
487 static char *tmp_str(char *buf, int len)
489 static char name[NAMELEN];
490 static int id;
491 sprintf(name, "__neatcc.s%d", id++);
492 o_dscpy(o_dsnew(name, len, 0), buf, len);
493 return name;
496 static void readprimary(void)
498 if (!tok_jmp(TOK_NUM)) {
499 long n;
500 int bt = tok_num(&n);
501 o_num(n);
502 ts_push_bt(bt);
503 return;
505 if (!tok_jmp(TOK_STR)) {
506 struct type t = {}; /* char type inside the arrays */
507 struct type a = {}; /* the char array type */
508 char *buf;
509 int len;
510 tok_str(&buf, &len);
511 t.bt = 1 | BT_SIGNED;
512 a.id = array_add(&t, len);
513 a.flags = T_ARRAY;
514 o_sym(tmp_str(buf, len));
515 ts_push(&a);
516 return;
518 if (!tok_jmp(TOK_NAME)) {
519 struct name unkn = {""};
520 char *name = unkn.name;
521 int n;
522 strcpy(name, tok_id());
523 /* don't search for labels here */
524 if (!ncexpr && !caseexpr && tok_see() == ':')
525 return;
526 if ((n = local_find(name)) != -1) {
527 struct name *l = &locals[n];
528 o_local(l->addr);
529 ts_push_addr(&l->type);
530 return;
532 if ((n = global_find(name)) != -1) {
533 struct name *g = &globals[n];
534 o_sym(*g->elfname ? g->elfname : g->name);
535 ts_push_addr(&g->type);
536 return;
538 if (!enum_find(&n, name)) {
539 o_num(n);
540 ts_push_bt(4 | BT_SIGNED);
541 return;
543 if (tok_see() != '(')
544 err("unknown symbol <%s>\n", name);
545 global_add(&unkn);
546 o_sym(unkn.name);
547 ts_push_bt(LONGSZ);
548 return;
550 if (!tok_jmp('(')) {
551 struct type t;
552 if (!readtype(&t)) {
553 struct type o;
554 tok_expect(')');
555 readpre();
556 ts_pop_de(&o);
557 ts_push(&t);
558 if (!t.ptr || !o.ptr)
559 o_cast(TYPE_BT(&t));
560 } else {
561 readexpr();
562 while (tok_jmp(')')) {
563 tok_expect(',');
564 ts_pop(NULL);
565 o_tmpdrop(1);
566 readexpr();
569 return;
573 static void arrayderef(void)
575 struct type t;
576 int sz;
577 ts_pop_de(NULL);
578 ts_pop(&t);
579 if (!(t.flags & T_ARRAY && !t.ptr) && t.addr) {
580 o_tmpswap();
581 o_deref(TYPE_BT(&t));
582 o_tmpswap();
584 array2ptr(&t);
585 t.ptr--;
586 sz = type_totsz(&t);
587 t.addr = 1;
588 if (sz > 1) {
589 o_num(sz);
590 o_bop(O_MUL);
592 o_bop(O_ADD);
593 ts_push(&t);
596 static void inc_post(int op)
598 struct type t = ts[nts - 1];
599 /* pushing the value before inc */
600 o_tmpcopy();
601 ts_de(1);
602 o_load();
603 o_tmpswap();
605 /* increment by 1 or pointer size */
606 o_tmpcopy();
607 ts_push(&t);
608 ts_pop_de(&t);
609 o_num(t.ptr > 0 ? type_szde(&t) : 1);
610 o_bop(op);
612 /* assign back */
613 o_assign(TYPE_BT(&t));
614 o_tmpdrop(1);
617 static void readfield(void)
619 struct name *field;
620 struct type t;
621 tok_expect(TOK_NAME);
622 ts_pop(&t);
623 array2ptr(&t);
624 field = struct_field(t.id, tok_id());
625 if (field->addr) {
626 o_num(field->addr);
627 o_bop(O_ADD);
629 ts_push_addr(&field->type);
632 static struct funcinfo {
633 struct type args[NARGS];
634 struct type ret;
635 int nargs;
636 int varg;
637 /* function and argument names; useful only when defining */
638 char argnames[NARGS][NAMELEN];
639 char name[NAMELEN];
640 } funcs[NFUNCS];
641 static int nfuncs;
643 static int func_create(struct type *ret, char *name, char argnames[][NAMELEN],
644 struct type *args, int nargs, int varg)
646 struct funcinfo *fi = &funcs[nfuncs++];
647 int i;
648 if (nfuncs >= NFUNCS)
649 err("nomem: NFUNCS reached!\n");
650 memcpy(&fi->ret, ret, sizeof(*ret));
651 for (i = 0; i < nargs; i++)
652 memcpy(&fi->args[i], &args[i], sizeof(*ret));
653 fi->nargs = nargs;
654 fi->varg = varg;
655 strcpy(fi->name, name ? name : "");
656 for (i = 0; i < nargs; i++)
657 strcpy(fi->argnames[i], argnames[i]);
658 return fi - funcs;
661 static void readcall(void)
663 struct type t;
664 struct funcinfo *fi;
665 int argc = 0;
666 ts_pop(&t);
667 if (t.flags & T_FUNC && t.ptr > 0)
668 o_deref(LONGSZ);
669 fi = t.flags & T_FUNC ? &funcs[t.id] : NULL;
670 if (tok_see() != ')') {
671 do {
672 readexpr();
673 ts_pop_de(NULL);
674 argc++;
675 } while (!tok_jmp(','));
677 tok_expect(')');
678 o_call(argc, fi ? TYPE_BT(&fi->ret) : 4 | BT_SIGNED);
679 if (fi) {
680 if (TYPE_BT(&fi->ret))
681 o_cast(TYPE_BT(&fi->ret));
682 ts_push(&fi->ret);
683 } else {
684 ts_push_bt(4 | BT_SIGNED);
688 static void readpost(void)
690 readprimary();
691 while (1) {
692 if (!tok_jmp('[')) {
693 readexpr();
694 tok_expect(']');
695 arrayderef();
696 continue;
698 if (!tok_jmp('(')) {
699 readcall();
700 continue;
702 if (!tok_jmp(TOK2("++"))) {
703 inc_post(O_ADD);
704 continue;
706 if (!tok_jmp(TOK2("--"))) {
707 inc_post(O_SUB);
708 continue;
710 if (!tok_jmp('.')) {
711 readfield();
712 continue;
714 if (!tok_jmp(TOK2("->"))) {
715 ts_de(1);
716 readfield();
717 continue;
719 break;
723 static void inc_pre(int op)
725 struct type t;
726 readpre();
727 /* copy the destination */
728 o_tmpcopy();
729 ts_push(&ts[nts - 1]);
730 /* increment by 1 or pointer size */
731 ts_pop_de(&t);
732 o_num(t.ptr > 0 ? type_szde(&t) : 1);
733 o_bop(op);
734 /* assign the result */
735 o_assign(TYPE_BT(&t));
736 ts_de(0);
739 static void readpre(void)
741 struct type t;
742 if (!tok_jmp('&')) {
743 readpre();
744 ts_pop(&t);
745 if (!t.addr)
746 err("cannot use the address\n");
747 t.ptr++;
748 t.addr = 0;
749 ts_push(&t);
750 return;
752 if (!tok_jmp('*')) {
753 readpre();
754 ts_pop(&t);
755 array2ptr(&t);
756 if (!t.ptr)
757 err("dereferencing non-pointer\n");
758 if (t.addr)
759 o_deref(TYPE_BT(&t));
760 t.ptr--;
761 t.addr = 1;
762 ts_push(&t);
763 return;
765 if (!tok_jmp('!')) {
766 readpre();
767 ts_pop_de(NULL);
768 o_uop(O_LNOT);
769 ts_push_bt(4 | BT_SIGNED);
770 return;
772 if (!tok_jmp('+')) {
773 readpre();
774 ts_de(1);
775 ts_pop(&t);
776 ts_push_bt(bt_uop(TYPE_BT(&t)));
777 return;
779 if (!tok_jmp('-')) {
780 readpre();
781 ts_de(1);
782 ts_pop(&t);
783 o_uop(O_NEG);
784 ts_push_bt(bt_uop(TYPE_BT(&t)));
785 return;
787 if (!tok_jmp('~')) {
788 readpre();
789 ts_de(1);
790 ts_pop(&t);
791 o_uop(O_NOT);
792 ts_push_bt(bt_uop(TYPE_BT(&t)));
793 return;
795 if (!tok_jmp(TOK2("++"))) {
796 inc_pre(O_ADD);
797 return;
799 if (!tok_jmp(TOK2("--"))) {
800 inc_pre(O_SUB);
801 return;
803 if (!tok_jmp(TOK_SIZEOF)) {
804 struct type t;
805 int op = !tok_jmp('(');
806 if (readtype(&t)) {
807 nogen++;
808 if (op)
809 readexpr();
810 else
811 readpre();
812 nogen--;
813 ts_pop(&t);
815 o_num(type_totsz(&t));
816 ts_push_bt(LONGSZ);
817 if (op)
818 tok_expect(')');
819 return;
821 readpost();
824 static void readmul(void)
826 readpre();
827 while (1) {
828 if (!tok_jmp('*')) {
829 readpre();
830 ts_binop(O_MUL);
831 continue;
833 if (!tok_jmp('/')) {
834 readpre();
835 ts_binop(O_DIV);
836 continue;
838 if (!tok_jmp('%')) {
839 readpre();
840 ts_binop(O_MOD);
841 continue;
843 break;
847 static void readadd(void)
849 readmul();
850 while (1) {
851 if (!tok_jmp('+')) {
852 readmul();
853 ts_addop(O_ADD);
854 continue;
856 if (!tok_jmp('-')) {
857 readmul();
858 ts_addop(O_SUB);
859 continue;
861 break;
865 static void shift(int op)
867 struct type t;
868 readadd();
869 ts_pop_de2(NULL, &t);
870 o_bop(op | (BT_SIGNED & TYPE_BT(&t) ? O_SIGNED : 0));
871 ts_push_bt(bt_uop(TYPE_BT(&t)));
874 static void readshift(void)
876 readadd();
877 while (1) {
878 if (!tok_jmp(TOK2("<<"))) {
879 shift(O_SHL);
880 continue;
882 if (!tok_jmp(TOK2(">>"))) {
883 shift(O_SHR);
884 continue;
886 break;
890 static void cmp(int op)
892 struct type t1, t2;
893 int bt;
894 readshift();
895 ts_pop_de2(&t1, &t2);
896 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
897 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
898 ts_push_bt(4 | BT_SIGNED);
901 static void readcmp(void)
903 readshift();
904 while (1) {
905 if (!tok_jmp('<')) {
906 cmp(O_LT);
907 continue;
909 if (!tok_jmp('>')) {
910 cmp(O_GT);
911 continue;
913 if (!tok_jmp(TOK2("<="))) {
914 cmp(O_LE);
915 continue;
917 if (!tok_jmp(TOK2(">="))) {
918 cmp(O_GE);
919 continue;
921 break;
925 static void eq(int op)
927 readcmp();
928 ts_pop_de2(NULL, NULL);
929 o_bop(op);
930 ts_push_bt(4 | BT_SIGNED);
933 static void readeq(void)
935 readcmp();
936 while (1) {
937 if (!tok_jmp(TOK2("=="))) {
938 eq(O_EQ);
939 continue;
941 if (!tok_jmp(TOK2("!="))) {
942 eq(O_NEQ);
943 continue;
945 break;
949 static void readbitand(void)
951 readeq();
952 while (!tok_jmp('&')) {
953 readeq();
954 ts_binop(O_AND);
958 static void readxor(void)
960 readbitand();
961 while (!tok_jmp('^')) {
962 readbitand();
963 ts_binop(O_XOR);
967 static void readbitor(void)
969 readxor();
970 while (!tok_jmp('|')) {
971 readxor();
972 ts_binop(O_OR);
976 static void readand(void)
978 int l_out, l_fail;
979 readbitor();
980 if (tok_see() != TOK2("&&"))
981 return;
982 l_out = LABEL();
983 l_fail = LABEL();
984 o_fork();
985 ts_pop_de(NULL);
986 o_jz(l_fail);
987 while (!tok_jmp(TOK2("&&"))) {
988 readbitor();
989 ts_pop_de(NULL);
990 o_jz(l_fail);
992 o_num(1);
993 o_forkpush();
994 o_jmp(l_out);
995 o_label(l_fail);
996 o_num(0);
997 o_forkpush();
998 o_forkjoin();
999 o_label(l_out);
1000 ts_push_bt(4 | BT_SIGNED);
1003 static void reador(void)
1005 int l_pass, l_end;
1006 readand();
1007 if (tok_see() != TOK2("||"))
1008 return;
1009 l_pass = LABEL();
1010 l_end = LABEL();
1011 o_fork();
1012 ts_pop_de(NULL);
1013 o_jnz(l_pass);
1014 while (!tok_jmp(TOK2("||"))) {
1015 readand();
1016 ts_pop_de(NULL);
1017 o_jnz(l_pass);
1019 o_num(0);
1020 o_forkpush();
1021 o_jmp(l_end);
1022 o_label(l_pass);
1023 o_num(1);
1024 o_forkpush();
1025 o_forkjoin();
1026 o_label(l_end);
1027 ts_push_bt(4 | BT_SIGNED);
1030 static void readcexpr(void);
1032 static int readcexpr_const(void)
1034 long c;
1035 if (o_popnum(&c))
1036 return -1;
1037 if (!c)
1038 nogen++;
1039 readcexpr();
1040 /* both branches yield the same type; so ignore the first */
1041 ts_pop_de(NULL);
1042 tok_expect(':');
1043 if (c)
1044 nogen++;
1045 else
1046 nogen--;
1047 readcexpr();
1048 /* making sure t->addr == 0 on both branches */
1049 ts_de(1);
1050 if (c)
1051 nogen--;
1052 return 0;
1055 static void readcexpr(void)
1057 reador();
1058 if (tok_jmp('?'))
1059 return;
1060 ncexpr++;
1061 ts_pop_de(NULL);
1062 o_fork();
1063 if (readcexpr_const()) {
1064 int l_fail = LABEL();
1065 int l_end = LABEL();
1066 struct type ret;
1067 o_jz(l_fail);
1068 readcexpr();
1069 /* both branches yield the same type; so ignore the first */
1070 ts_pop_de(&ret);
1071 if (!TYPE_VOID(&ret))
1072 o_forkpush();
1073 o_jmp(l_end);
1075 tok_expect(':');
1076 o_label(l_fail);
1077 readcexpr();
1078 /* making sure t->addr == 0 on both branches */
1079 ts_de(1);
1080 if (!TYPE_VOID(&ret)) {
1081 o_forkpush();
1082 o_forkjoin();
1084 o_label(l_end);
1086 ncexpr--;
1089 static void opassign(int op, int ptrop)
1091 struct type t = ts[nts - 1];
1092 o_tmpcopy();
1093 ts_push(&t);
1094 readexpr();
1095 if (op == O_ADD || op == O_SUB)
1096 ts_addop(op);
1097 else
1098 ts_binop(op);
1099 o_assign(TYPE_BT(&ts[nts - 2]));
1100 ts_pop(NULL);
1101 ts_de(0);
1104 static void doassign(void)
1106 struct type t = ts[nts - 1];
1107 if (!t.ptr && t.flags & T_STRUCT) {
1108 ts_pop(NULL);
1109 o_num(type_totsz(&t));
1110 o_memcpy();
1111 } else {
1112 ts_pop_de(NULL);
1113 o_assign(TYPE_BT(&ts[nts - 1]));
1114 ts_de(0);
1118 static void readexpr(void)
1120 readcexpr();
1121 if (!tok_jmp('=')) {
1122 readexpr();
1123 doassign();
1124 return;
1126 if (!tok_jmp(TOK2("+="))) {
1127 opassign(O_ADD, 1);
1128 return;
1130 if (!tok_jmp(TOK2("-="))) {
1131 opassign(O_SUB, 1);
1132 return;
1134 if (!tok_jmp(TOK2("*="))) {
1135 opassign(O_MUL, 0);
1136 return;
1138 if (!tok_jmp(TOK2("/="))) {
1139 opassign(O_DIV, 0);
1140 return;
1142 if (!tok_jmp(TOK2("%="))) {
1143 opassign(O_MOD, 0);
1144 return;
1146 if (!tok_jmp(TOK3("<<="))) {
1147 opassign(O_SHL, 0);
1148 return;
1150 if (!tok_jmp(TOK3(">>="))) {
1151 opassign(O_SHR, 0);
1152 return;
1154 if (!tok_jmp(TOK3("&="))) {
1155 opassign(O_AND, 0);
1156 return;
1158 if (!tok_jmp(TOK3("|="))) {
1159 opassign(O_OR, 0);
1160 return;
1162 if (!tok_jmp(TOK3("^="))) {
1163 opassign(O_XOR, 0);
1164 return;
1168 static void readestmt(void)
1170 do {
1171 o_tmpdrop(-1);
1172 nts = 0;
1173 readexpr();
1174 } while (!tok_jmp(','));
1177 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1179 static void globalinit(void *obj, int off, struct type *t)
1181 struct name *name = obj;
1182 char *elfname = *name->elfname ? name->elfname : name->name;
1183 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1184 struct type *t_de = &arrays[t->id].type;
1185 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1186 char *buf;
1187 int len;
1188 tok_expect(TOK_STR);
1189 tok_str(&buf, &len);
1190 o_dscpy(name->addr + off, buf, len);
1191 return;
1194 readexpr();
1195 o_dsset(elfname, off, TYPE_BT(t));
1196 ts_pop(NULL);
1199 static void readfunc(struct name *name, int flags);
1201 static void globaldef(void *data, struct name *name, unsigned flags)
1203 struct type *t = &name->type;
1204 char *elfname = *name->elfname ? name->elfname : name->name;
1205 int sz;
1206 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1207 if (~flags & F_EXTERN)
1208 arrays[t->id].n = initsize();
1209 sz = type_totsz(t);
1210 if (!(flags & F_EXTERN) && (!(t->flags & T_FUNC) || t->ptr)) {
1211 if (tok_see() == '=')
1212 name->addr = o_dsnew(elfname, sz, F_GLOBAL(flags));
1213 else
1214 o_bsnew(elfname, sz, F_GLOBAL(flags));
1216 global_add(name);
1217 if (!tok_jmp('='))
1218 initexpr(t, 0, name, globalinit);
1219 if (tok_see() == '{' && name->type.flags & T_FUNC)
1220 readfunc(name, flags);
1223 /* generate the address of local + off */
1224 static void o_localoff(long addr, int off)
1226 o_local(addr);
1227 if (off) {
1228 o_num(off);
1229 o_bop(O_ADD);
1233 static void localinit(void *obj, int off, struct type *t)
1235 long addr = *(long *) obj;
1236 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1237 struct type *t_de = &arrays[t->id].type;
1238 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1239 char *buf;
1240 int len;
1241 tok_expect(TOK_STR);
1242 tok_str(&buf, &len);
1243 o_localoff(addr, off);
1244 o_sym(tmp_str(buf, len));
1245 o_num(len);
1246 o_memcpy();
1247 o_tmpdrop(1);
1248 return;
1251 o_localoff(addr, off);
1252 ts_push(t);
1253 readexpr();
1254 doassign();
1255 ts_pop(NULL);
1256 o_tmpdrop(1);
1259 /* current function name */
1260 static char func_name[NAMELEN];
1262 static void localdef(void *data, struct name *name, unsigned flags)
1264 struct type *t = &name->type;
1265 if ((flags & F_EXTERN) || ((t->flags & T_FUNC) && !t->ptr)) {
1266 global_add(name);
1267 return;
1269 if (flags & F_STATIC) {
1270 sprintf(name->elfname, "__neatcc.%s.%s", func_name, name->name);
1271 globaldef(data, name, flags);
1272 return;
1274 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1275 arrays[t->id].n = initsize();
1276 name->addr = o_mklocal(type_totsz(&name->type));
1277 local_add(name);
1278 if (!tok_jmp('=')) {
1279 if (t->flags & (T_ARRAY | T_STRUCT) && !t->ptr) {
1280 o_local(name->addr);
1281 o_num(0);
1282 o_num(type_totsz(t));
1283 o_memset();
1284 o_tmpdrop(1);
1286 initexpr(t, 0, &name->addr, localinit);
1290 static void typedefdef(void *data, struct name *name, unsigned flags)
1292 typedef_add(name->name, &name->type);
1295 static void readstmt(void);
1297 static void readswitch(void)
1299 int o_break = l_break;
1300 long val_addr = o_mklocal(LONGSZ);
1301 struct type t;
1302 int ncases = 0; /* number of case labels */
1303 int l_failed = LABEL(); /* address of last failed jmp */
1304 int l_matched = LABEL(); /* address of last walk through jmp */
1305 int l_default = 0; /* default case label */
1306 l_break = LABEL();
1307 tok_expect('(');
1308 readexpr();
1309 ts_pop_de(&t);
1310 o_local(val_addr);
1311 o_tmpswap();
1312 o_assign(TYPE_BT(&t));
1313 ts_de(0);
1314 o_tmpdrop(1);
1315 tok_expect(')');
1316 tok_expect('{');
1317 while (tok_jmp('}')) {
1318 if (tok_see() != TOK_CASE && tok_see() != TOK_DEFAULT) {
1319 readstmt();
1320 continue;
1322 if (ncases)
1323 o_jmp(l_matched);
1324 if (tok_get() == TOK_CASE) {
1325 o_label(l_failed);
1326 l_failed = LABEL();
1327 caseexpr = 1;
1328 readexpr();
1329 ts_pop_de(NULL);
1330 caseexpr = 0;
1331 o_local(val_addr);
1332 o_deref(TYPE_BT(&t));
1333 o_bop(O_EQ);
1334 o_jz(l_failed);
1335 o_tmpdrop(1);
1336 } else {
1337 if (!ncases)
1338 o_jmp(l_failed);
1339 l_default = LABEL();
1340 o_label(l_default);
1342 tok_expect(':');
1343 o_label(l_matched);
1344 l_matched = LABEL();
1345 ncases++;
1347 o_rmlocal(val_addr, LONGSZ);
1348 o_jmp(l_break);
1349 o_label(l_failed);
1350 if (l_default)
1351 o_jmp(l_default);
1352 o_label(l_break);
1353 l_break = o_break;
1356 static char label_name[NLABELS][NAMELEN];
1357 static int label_ids[NLABELS];
1358 static int nlabels;
1360 static int label_id(char *name)
1362 int i;
1363 for (i = nlabels - 1; i >= 0; --i)
1364 if (!strcmp(label_name[i], name))
1365 return label_ids[i];
1366 strcpy(label_name[nlabels], name);
1367 label_ids[nlabels] = LABEL();
1368 return label_ids[nlabels++];
1371 static void readstmt(void)
1373 o_tmpdrop(-1);
1374 nts = 0;
1375 if (!tok_jmp('{')) {
1376 int _nlocals = nlocals;
1377 int _nglobals = nglobals;
1378 int _nenums = nenums;
1379 int _ntypedefs = ntypedefs;
1380 int _nstructs = nstructs;
1381 int _nfuncs = nfuncs;
1382 int _narrays = narrays;
1383 while (tok_jmp('}'))
1384 readstmt();
1385 nlocals = _nlocals;
1386 nenums = _nenums;
1387 ntypedefs = _ntypedefs;
1388 nstructs = _nstructs;
1389 nfuncs = _nfuncs;
1390 narrays = _narrays;
1391 nglobals = _nglobals;
1392 return;
1394 if (!readdefs(localdef, NULL)) {
1395 tok_expect(';');
1396 return;
1398 if (!tok_jmp(TOK_TYPEDEF)) {
1399 readdefs(typedefdef, NULL);
1400 tok_expect(';');
1401 return;
1403 if (!tok_jmp(TOK_IF)) {
1404 int l_fail = LABEL();
1405 int l_end = LABEL();
1406 tok_expect('(');
1407 readexpr();
1408 tok_expect(')');
1409 ts_pop_de(NULL);
1410 o_jz(l_fail);
1411 readstmt();
1412 if (!tok_jmp(TOK_ELSE)) {
1413 o_jmp(l_end);
1414 o_label(l_fail);
1415 readstmt();
1416 o_label(l_end);
1417 } else {
1418 o_label(l_fail);
1420 return;
1422 if (!tok_jmp(TOK_WHILE)) {
1423 int o_break = l_break;
1424 int o_cont = l_cont;
1425 l_break = LABEL();
1426 l_cont = LABEL();
1427 o_label(l_cont);
1428 tok_expect('(');
1429 readexpr();
1430 tok_expect(')');
1431 ts_pop_de(NULL);
1432 o_jz(l_break);
1433 readstmt();
1434 o_jmp(l_cont);
1435 o_label(l_break);
1436 l_break = o_break;
1437 l_cont = o_cont;
1438 return;
1440 if (!tok_jmp(TOK_DO)) {
1441 int o_break = l_break;
1442 int o_cont = l_cont;
1443 int l_beg = LABEL();
1444 l_break = LABEL();
1445 l_cont = LABEL();
1446 o_label(l_beg);
1447 readstmt();
1448 tok_expect(TOK_WHILE);
1449 tok_expect('(');
1450 o_label(l_cont);
1451 readexpr();
1452 ts_pop_de(NULL);
1453 o_jnz(l_beg);
1454 tok_expect(')');
1455 o_label(l_break);
1456 tok_expect(';');
1457 l_break = o_break;
1458 l_cont = o_cont;
1459 return;
1461 if (!tok_jmp(TOK_FOR)) {
1462 int o_break = l_break;
1463 int o_cont = l_cont;
1464 int l_check = LABEL(); /* for condition label */
1465 int l_body = LABEL(); /* for block label */
1466 l_cont = LABEL();
1467 l_break = LABEL();
1468 tok_expect('(');
1469 if (tok_see() != ';')
1470 readestmt();
1471 tok_expect(';');
1472 o_label(l_check);
1473 if (tok_see() != ';') {
1474 readestmt();
1475 ts_pop_de(NULL);
1476 o_jz(l_break);
1478 tok_expect(';');
1479 o_jmp(l_body);
1480 o_label(l_cont);
1481 if (tok_see() != ')')
1482 readestmt();
1483 tok_expect(')');
1484 o_jmp(l_check);
1485 o_label(l_body);
1486 readstmt();
1487 o_jmp(l_cont);
1488 o_label(l_break);
1489 l_break = o_break;
1490 l_cont = o_cont;
1491 return;
1493 if (!tok_jmp(TOK_SWITCH)) {
1494 readswitch();
1495 return;
1497 if (!tok_jmp(TOK_RETURN)) {
1498 int ret = tok_see() != ';';
1499 if (ret) {
1500 readexpr();
1501 ts_pop_de(NULL);
1503 tok_expect(';');
1504 o_ret(ret);
1505 return;
1507 if (!tok_jmp(TOK_BREAK)) {
1508 tok_expect(';');
1509 o_jmp(l_break);
1510 return;
1512 if (!tok_jmp(TOK_CONTINUE)) {
1513 tok_expect(';');
1514 o_jmp(l_cont);
1515 return;
1517 if (!tok_jmp(TOK_GOTO)) {
1518 tok_expect(TOK_NAME);
1519 o_jmp(label_id(tok_id()));
1520 tok_expect(';');
1521 return;
1523 readestmt();
1524 /* labels */
1525 if (!tok_jmp(':')) {
1526 o_label(label_id(tok_id()));
1527 return;
1529 tok_expect(';');
1532 static void readfunc(struct name *name, int flags)
1534 struct funcinfo *fi = &funcs[name->type.id];
1535 long beg = tok_addr();
1536 int i;
1537 strcpy(func_name, fi->name);
1538 o_func_beg(func_name, fi->nargs, F_GLOBAL(flags), fi->varg);
1539 for (i = 0; i < fi->nargs; i++) {
1540 struct name arg = {"", "", fi->args[i], o_arg2loc(i)};
1541 strcpy(arg.name, fi->argnames[i]);
1542 local_add(&arg);
1544 /* first pass: collecting statistics */
1545 label = 0;
1546 nlabels = 0;
1547 o_pass1();
1548 readstmt();
1549 tok_jump(beg);
1550 /* second pass: generating code */
1551 label = 0;
1552 nlabels = 0;
1553 o_pass2();
1554 readstmt();
1555 o_func_end();
1556 func_name[0] = '\0';
1557 nlocals = 0;
1560 static void readdecl(void)
1562 if (!tok_jmp(TOK_TYPEDEF)) {
1563 readdefs(typedefdef, NULL);
1564 tok_expect(';');
1565 return;
1567 readdefs_int(globaldef, NULL);
1568 tok_jmp(';');
1571 static void parse(void)
1573 while (tok_see() != TOK_EOF)
1574 readdecl();
1577 static void compat_macros(void)
1579 cpp_define("__STDC__", "");
1580 cpp_define("__linux__", "");
1581 cpp_define(I_ARCH, "");
1583 /* ignored keywords */
1584 cpp_define("const", "");
1585 cpp_define("register", "");
1586 cpp_define("volatile", "");
1587 cpp_define("inline", "");
1588 cpp_define("restrict", "");
1589 cpp_define("__inline__", "");
1590 cpp_define("__restrict__", "");
1591 cpp_define("__attribute__(x)", "");
1592 cpp_define("__builtin_va_list__", "long");
1595 int main(int argc, char *argv[])
1597 char obj[128] = "";
1598 int ofd;
1599 int i = 1;
1600 compat_macros();
1601 while (i < argc && argv[i][0] == '-') {
1602 if (argv[i][1] == 'I')
1603 cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]);
1604 if (argv[i][1] == 'D') {
1605 char *name = argv[i] + 2;
1606 char *def = "";
1607 char *eq = strchr(name, '=');
1608 if (eq) {
1609 *eq = '\0';
1610 def = eq + 1;
1612 cpp_define(name, def);
1614 if (argv[i][1] == 'o')
1615 strcpy(obj, argv[i][2] ? argv[i] + 2 : argv[++i]);
1616 i++;
1618 if (i == argc)
1619 die("neatcc: no file given\n");
1620 if (cpp_init(argv[i]))
1621 die("neatcc: cannot open <%s>\n", argv[i]);
1622 parse();
1623 if (!*obj) {
1624 strcpy(obj, argv[i]);
1625 obj[strlen(obj) - 1] = 'o';
1627 ofd = open(obj, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1628 o_write(ofd);
1629 close(ofd);
1630 return 0;
1634 /* parsing function and variable declarations */
1636 /* read the base type of a variable */
1637 static int basetype(struct type *type, unsigned *flags)
1639 int sign = 1;
1640 int size = 4;
1641 int done = 0;
1642 int i = 0;
1643 int isunion;
1644 char name[NAMELEN] = "";
1645 *flags = 0;
1646 type->flags = 0;
1647 type->ptr = 0;
1648 type->addr = 0;
1649 while (!done) {
1650 switch (tok_see()) {
1651 case TOK_STATIC:
1652 *flags |= F_STATIC;
1653 break;
1654 case TOK_EXTERN:
1655 *flags |= F_EXTERN;
1656 break;
1657 case TOK_VOID:
1658 sign = 0;
1659 size = 0;
1660 done = 1;
1661 break;
1662 case TOK_INT:
1663 done = 1;
1664 break;
1665 case TOK_CHAR:
1666 size = 1;
1667 done = 1;
1668 break;
1669 case TOK_SHORT:
1670 size = 2;
1671 break;
1672 case TOK_LONG:
1673 size = LONGSZ;
1674 break;
1675 case TOK_SIGNED:
1676 break;
1677 case TOK_UNSIGNED:
1678 sign = 0;
1679 break;
1680 case TOK_UNION:
1681 case TOK_STRUCT:
1682 isunion = tok_get() == TOK_UNION;
1683 if (!tok_jmp(TOK_NAME))
1684 strcpy(name, tok_id());
1685 if (tok_see() == '{')
1686 type->id = struct_create(name, isunion);
1687 else
1688 type->id = struct_find(name, isunion);
1689 type->flags |= T_STRUCT;
1690 type->bt = LONGSZ;
1691 return 0;
1692 case TOK_ENUM:
1693 tok_get();
1694 tok_jmp(TOK_NAME);
1695 if (tok_see() == '{')
1696 enum_create();
1697 type->bt = 4 | BT_SIGNED;
1698 return 0;
1699 default:
1700 if (tok_see() == TOK_NAME) {
1701 int id = typedef_find(tok_id());
1702 if (id != -1) {
1703 tok_get();
1704 memcpy(type, &typedefs[id].type,
1705 sizeof(*type));
1706 return 0;
1709 if (!i)
1710 return 1;
1711 done = 1;
1712 continue;
1714 i++;
1715 tok_get();
1717 type->bt = size | (sign ? BT_SIGNED : 0);
1718 return 0;
1721 static void readptrs(struct type *type)
1723 while (!tok_jmp('*')) {
1724 type->ptr++;
1725 if (!type->bt)
1726 type->bt = 1;
1730 /* read function arguments */
1731 static int readargs(struct type *args, char argnames[][NAMELEN], int *varg)
1733 int nargs = 0;
1734 tok_expect('(');
1735 *varg = 0;
1736 while (tok_see() != ')') {
1737 if (!tok_jmp(TOK3("..."))) {
1738 *varg = 1;
1739 break;
1741 if (readname(&args[nargs], argnames[nargs], NULL)) {
1742 /* argument has no type, assume int */
1743 tok_expect(TOK_NAME);
1744 memset(&args[nargs], 0, sizeof(struct type));
1745 args[nargs].bt = 4 | BT_SIGNED;
1746 strcpy(argnames[nargs], tok_id());
1748 /* argument arrays are pointers */
1749 array2ptr(&args[nargs]);
1750 nargs++;
1751 if (tok_jmp(','))
1752 break;
1754 tok_expect(')');
1755 /* void argument */
1756 if (nargs == 1 && !TYPE_BT(&args[0]))
1757 return 0;
1758 return nargs;
1761 /* read K&R function arguments */
1762 static void krdef(void *data, struct name *name, unsigned flags)
1764 struct funcinfo *fi = data;
1765 int i;
1766 for (i = 0; i < fi->nargs; i++)
1767 if (!strcmp(fi->argnames[i], name->name))
1768 memcpy(&fi->args[i], &name->type, sizeof(name->type));
1772 * readarrays() parses array specifiers when reading a definition in
1773 * readname(). The "type" parameter contains the type contained in the
1774 * inner array; for instance, type in "int *a[10][20]" would be an int
1775 * pointer. When returning, the "type" parameter is changed to point
1776 * to the final array. The function returns a pointer to the type in
1777 * the inner array; this is useful when the type is not complete yet,
1778 * like when creating an array of function pointers as in
1779 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1781 static struct type *readarrays(struct type *type)
1783 long arsz[16];
1784 struct type *inner = NULL;
1785 int nar = 0;
1786 int i;
1787 while (!tok_jmp('[')) {
1788 long n = 0;
1789 if (tok_jmp(']')) {
1790 readexpr();
1791 ts_pop_de(NULL);
1792 if (o_popnum(&n))
1793 err("const expr expected\n");
1794 tok_expect(']');
1796 arsz[nar++] = n;
1798 for (i = nar - 1; i >= 0; i--) {
1799 type->id = array_add(type, arsz[i]);
1800 if (!inner)
1801 inner = &arrays[type->id].type;
1802 type->flags = T_ARRAY;
1803 type->bt = LONGSZ;
1804 type->ptr = 0;
1806 return inner;
1810 * readname() reads a variable definition; the name is copied into
1811 * "name" and the type is copied into "main" argument. The "base"
1812 * argument, if not NULL, indicates the base type of the variable.
1813 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1814 * "int". If NULL, basetype() is called directly to read the base
1815 * type of the variable. readname() returns zero, only if the
1816 * variable can be read.
1818 static int readname(struct type *main, char *name, struct type *base)
1820 struct type tpool[3];
1821 int npool = 0;
1822 struct type *type = &tpool[npool++];
1823 struct type *ptype = NULL; /* type inside parenthesis */
1824 struct type *btype = NULL; /* type before parenthesis */
1825 struct type *inner;
1826 unsigned flags;
1827 memset(tpool, 0, sizeof(tpool));
1828 if (name)
1829 *name = '\0';
1830 if (!base) {
1831 if (basetype(type, &flags))
1832 return 1;
1833 } else {
1834 memcpy(type, base, sizeof(*base));
1836 readptrs(type);
1837 if (!tok_jmp('(')) {
1838 btype = type;
1839 type = &tpool[npool++];
1840 ptype = type;
1841 readptrs(type);
1843 if (!tok_jmp(TOK_NAME) && name)
1844 strcpy(name, tok_id());
1845 inner = readarrays(type);
1846 if (ptype && inner)
1847 ptype = inner;
1848 if (ptype)
1849 tok_expect(')');
1850 if (tok_see() == '(') {
1851 struct type args[NARGS];
1852 char argnames[NARGS][NAMELEN];
1853 int varg = 0;
1854 int nargs = readargs(args, argnames, &varg);
1855 if (!ptype) {
1856 btype = type;
1857 type = &tpool[npool++];
1858 ptype = type;
1860 ptype->flags = T_FUNC;
1861 ptype->bt = LONGSZ;
1862 ptype->id = func_create(btype, name, argnames, args, nargs, varg);
1863 if (tok_see() != ';')
1864 while (tok_see() != '{' && !readdefs(krdef, &funcs[ptype->id]))
1865 tok_expect(';');
1866 } else {
1867 if (ptype && readarrays(type))
1868 array2ptr(type);
1870 memcpy(main, type, sizeof(*type));
1871 return 0;
1874 static int readtype(struct type *type)
1876 return readname(type, NULL, NULL);
1880 * readdef() reads a variable definitions statement. The definition
1881 * statement can appear in anywhere: global variables, function
1882 * local variables, struct fields, and typedefs. For each defined
1883 * variable, def() callback is called with the appropriate name
1884 * struct and flags; the callback should finish parsing the definition
1885 * by possibly reading the initializer expression and saving the name
1886 * struct.
1888 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
1889 void *data)
1891 struct type base;
1892 unsigned base_flags;
1893 if (basetype(&base, &base_flags))
1894 return 1;
1895 if (tok_see() == ';' || tok_see() == '{')
1896 return 0;
1897 do {
1898 struct name name = {{""}};
1899 if (readname(&name.type, name.name, &base))
1900 break;
1901 def(data, &name, base_flags);
1902 } while (!tok_jmp(','));
1903 return 0;
1906 /* just like readdefs, but default to int type; for handling K&R functions */
1907 static int readdefs_int(void (*def)(void *data, struct name *name, unsigned flags),
1908 void *data)
1910 struct type base;
1911 unsigned flags = 0;
1912 if (basetype(&base, &flags)) {
1913 if (tok_see() != TOK_NAME)
1914 return 1;
1915 memset(&base, 0, sizeof(base));
1916 base.bt = 4 | BT_SIGNED;
1918 if (tok_see() != ';') {
1919 do {
1920 struct name name = {{""}};
1921 if (readname(&name.type, name.name, &base))
1922 break;
1923 def(data, &name, flags);
1924 } while (!tok_jmp(','));
1926 return 0;
1930 /* parsing initializer expressions */
1932 static void jumpbrace(void)
1934 int depth = 0;
1935 while (tok_see() != '}' || depth--)
1936 if (tok_get() == '{')
1937 depth++;
1938 tok_expect('}');
1941 /* compute the size of the initializer expression */
1942 static int initsize(void)
1944 long addr = tok_addr();
1945 int n = 0;
1946 if (tok_jmp('='))
1947 return 0;
1948 if (!tok_jmp(TOK_STR)) {
1949 tok_str(NULL, &n);
1950 tok_jump(addr);
1951 return n;
1953 tok_expect('{');
1954 while (tok_jmp('}')) {
1955 long idx = n;
1956 if (!tok_jmp('[')) {
1957 readexpr();
1958 ts_pop_de(NULL);
1959 o_popnum(&idx);
1960 tok_expect(']');
1961 tok_expect('=');
1963 if (n < idx + 1)
1964 n = idx + 1;
1965 while (tok_see() != '}' && tok_see() != ',')
1966 if (tok_get() == '{')
1967 jumpbrace();
1968 tok_jmp(',');
1970 tok_jump(addr);
1971 return n;
1974 static struct type *innertype(struct type *t)
1976 if (t->flags & T_ARRAY && !t->ptr)
1977 return innertype(&arrays[t->id].type);
1978 return t;
1981 /* read the initializer expression and initialize basic types using set() cb */
1982 static void initexpr(struct type *t, int off, void *obj,
1983 void (*set)(void *obj, int off, struct type *t))
1985 if (tok_jmp('{')) {
1986 set(obj, off, t);
1987 return;
1989 if (!t->ptr && t->flags & T_STRUCT) {
1990 struct structinfo *si = &structs[t->id];
1991 int i;
1992 for (i = 0; i < si->nfields && tok_see() != '}'; i++) {
1993 struct name *field = &si->fields[i];
1994 if (!tok_jmp('.')) {
1995 tok_expect(TOK_NAME);
1996 field = struct_field(t->id, tok_id());
1997 tok_expect('=');
1999 initexpr(&field->type, off + field->addr, obj, set);
2000 if (tok_jmp(','))
2001 break;
2003 } else if (t->flags & T_ARRAY) {
2004 struct type *t_de = &arrays[t->id].type;
2005 int i;
2006 /* handling extra braces as in: char s[] = {"sth"} */
2007 if (TYPE_SZ(t_de) == 1 && tok_see() == TOK_STR) {
2008 set(obj, off, t);
2009 tok_expect('}');
2010 return;
2012 for (i = 0; tok_see() != '}'; i++) {
2013 long idx = i;
2014 struct type *it = t_de;
2015 if (!tok_jmp('[')) {
2016 readexpr();
2017 ts_pop_de(NULL);
2018 o_popnum(&idx);
2019 tok_expect(']');
2020 tok_expect('=');
2022 if (tok_see() != '{' && (tok_see() != TOK_STR ||
2023 !(it->flags & T_ARRAY)))
2024 it = innertype(t_de);
2025 initexpr(it, off + type_totsz(it) * idx, obj, set);
2026 if (tok_jmp(','))
2027 break;
2030 tok_expect('}');