gen: move the register allocation to reg.c
[neatcc.git] / ncc.c
blob8e20d1c95492cb92fd27b685bbc191c54782614d
1 /*
2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2013 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
7 */
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include "gen.h"
17 #include "tok.h"
18 #include "out.h"
20 static int nogen; /* don't generate code */
21 /* nogen macros */
22 #define o_bop(op) {if (!nogen) o_bop(op);}
23 #define o_uop(op) {if (!nogen) o_uop(op);}
24 #define o_cast(bt) {if (!nogen) o_cast(bt);}
25 #define o_memcpy() {if (!nogen) o_memcpy();}
26 #define o_memset() {if (!nogen) o_memset();}
27 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
28 #define o_ret(ret) {if (!nogen) o_ret(ret);}
29 #define o_assign(bt) {if (!nogen) o_assign(bt);}
30 #define o_deref(bt) {if (!nogen) o_deref(bt);}
31 #define o_load() {if (!nogen) o_load();}
32 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
33 #define o_num(n) {if (!nogen) o_num(n);}
34 #define o_local(addr) {if (!nogen) o_local(addr);}
35 #define o_sym(sym) {if (!nogen) o_sym(sym);}
36 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
37 #define o_tmpswap() {if (!nogen) o_tmpswap();}
38 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
39 #define o_label(id) {if (!nogen) o_label(id);}
40 #define o_jz(id) {if (!nogen) o_jz(id);}
41 #define o_jnz(id) {if (!nogen) o_jnz(id);}
42 #define o_jmp(id) {if (!nogen) o_jmp(id);}
43 #define o_fork() {if (!nogen) o_fork();}
44 #define o_forkpush() {if (!nogen) o_forkpush();}
45 #define o_forkjoin() {if (!nogen) o_forkjoin();}
47 #define MAXLOCALS (1 << 10)
48 #define MAXGLOBALS (1 << 10)
49 #define MAXARGS (1 << 5)
51 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
55 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
56 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
58 /* type->flag values */
59 #define T_ARRAY 0x01
60 #define T_STRUCT 0x02
61 #define T_FUNC 0x04
63 /* variable definition flags */
64 #define F_STATIC 0x01
65 #define F_EXTERN 0x02
67 struct type {
68 unsigned bt;
69 unsigned flags;
70 int ptr;
71 int id; /* for structs, functions and arrays */
72 int addr; /* the address is passed to gen.c; deref for value */
75 /* type stack */
76 static struct type ts[MAXTMP];
77 static int nts;
79 static void ts_push_bt(unsigned bt)
81 ts[nts].ptr = 0;
82 ts[nts].flags = 0;
83 ts[nts].addr = 0;
84 ts[nts++].bt = bt;
87 static void ts_push(struct type *t)
89 struct type *d = &ts[nts++];
90 memcpy(d, t, sizeof(*t));
93 static void ts_push_addr(struct type *t)
95 ts_push(t);
96 ts[nts - 1].addr = 1;
99 static void ts_pop(struct type *type)
101 nts--;
102 if (type)
103 *type = ts[nts];
106 void err(char *fmt, ...)
108 va_list ap;
109 char msg[512];
110 va_start(ap, fmt);
111 vsprintf(msg, fmt, ap);
112 va_end(ap);
113 die("%s: %s", cpp_loc(tok_addr()), msg);
116 struct name {
117 char name[NAMELEN];
118 char elfname[NAMELEN]; /* local elf name for static variables in function */
119 struct type type;
120 long addr; /* local stack offset, global data addr, struct offset */
123 static struct name locals[MAXLOCALS];
124 static int nlocals;
125 static struct name globals[MAXGLOBALS];
126 static int nglobals;
128 static void local_add(struct name *name)
130 if (nlocals >= MAXLOCALS)
131 err("nomem: MAXLOCALS reached!\n");
132 memcpy(&locals[nlocals++], name, sizeof(*name));
135 static int local_find(char *name)
137 int i;
138 for (i = nlocals - 1; i >= 0; --i)
139 if (!strcmp(locals[i].name, name))
140 return i;
141 return -1;
144 static int global_find(char *name)
146 int i;
147 for (i = nglobals - 1; i >= 0; i--)
148 if (!strcmp(name, globals[i].name))
149 return i;
150 return -1;
153 static void global_add(struct name *name)
155 if (nglobals >= MAXGLOBALS)
156 err("nomem: MAXGLOBALS reached!\n");
157 memcpy(&globals[nglobals++], name, sizeof(*name));
160 #define LABEL() (++label)
162 static int label; /* last used label id */
163 static int l_break; /* current break label */
164 static int l_cont; /* current continue label */
166 #define MAXENUMS (1 << 10)
168 static struct enumval {
169 char name[NAMELEN];
170 int n;
171 } enums[MAXENUMS];
172 static int nenums;
174 static void enum_add(char *name, int val)
176 struct enumval *ev = &enums[nenums++];
177 if (nenums >= MAXENUMS)
178 err("nomem: MAXENUMS reached!\n");
179 strcpy(ev->name, name);
180 ev->n = val;
183 static int enum_find(int *val, char *name)
185 int i;
186 for (i = nenums - 1; i >= 0; --i)
187 if (!strcmp(name, enums[i].name)) {
188 *val = enums[i].n;
189 return 0;
191 return 1;
194 #define MAXTYPEDEFS (1 << 10)
196 static struct typdefinfo {
197 char name[NAMELEN];
198 struct type type;
199 } typedefs[MAXTYPEDEFS];
200 static int ntypedefs;
202 static void typedef_add(char *name, struct type *type)
204 struct typdefinfo *ti = &typedefs[ntypedefs++];
205 if (ntypedefs >= MAXTYPEDEFS)
206 err("nomem: MAXTYPEDEFS reached!\n");
207 strcpy(ti->name, name);
208 memcpy(&ti->type, type, sizeof(*type));
211 static int typedef_find(char *name)
213 int i;
214 for (i = ntypedefs - 1; i >= 0; --i)
215 if (!strcmp(name, typedefs[i].name))
216 return i;
217 return -1;
220 #define MAXARRAYS (1 << 10)
222 static struct array {
223 struct type type;
224 int n;
225 } arrays[MAXARRAYS];
226 static int narrays;
228 static int array_add(struct type *type, int n)
230 struct array *a = &arrays[narrays++];
231 if (narrays >= MAXARRAYS)
232 err("nomem: MAXARRAYS reached!\n");
233 memcpy(&a->type, type, sizeof(*type));
234 a->n = n;
235 return a - arrays;
238 static void array2ptr(struct type *t)
240 if (t->flags & T_ARRAY && !t->ptr) {
241 memcpy(t, &arrays[t->id].type, sizeof(*t));
242 t->ptr++;
246 #define MAXSTRUCTS (1 << 10)
247 #define MAXFIELDS (1 << 7)
249 static struct structinfo {
250 char name[NAMELEN];
251 struct name fields[MAXFIELDS];
252 int nfields;
253 int isunion;
254 int size;
255 } structs[MAXSTRUCTS];
256 static int nstructs;
258 static int struct_find(char *name, int isunion)
260 int i;
261 for (i = nstructs - 1; i >= 0; --i)
262 if (*structs[i].name && !strcmp(name, structs[i].name) &&
263 structs[i].isunion == isunion)
264 return i;
265 i = nstructs++;
266 if (nstructs >= MAXSTRUCTS)
267 err("nomem: MAXTYPES reached!\n");
268 memset(&structs[i], 0, sizeof(structs[i]));
269 strcpy(structs[i].name, name);
270 structs[i].isunion = isunion;
271 return i;
274 static struct name *struct_field(int id, char *name)
276 struct structinfo *si = &structs[id];
277 int i;
278 for (i = 0; i < si->nfields; i++)
279 if (!strcmp(name, si->fields[i].name))
280 return &si->fields[i];
281 err("field not found\n");
282 return NULL;
285 /* return t's size */
286 static int type_totsz(struct type *t)
288 if (t->ptr)
289 return LONGSZ;
290 if (t->flags & T_ARRAY)
291 return arrays[t->id].n * type_totsz(&arrays[t->id].type);
292 return t->flags & T_STRUCT ? structs[t->id].size : BT_SZ(t->bt);
295 /* return t's dereferenced size */
296 static unsigned type_szde(struct type *t)
298 struct type de = *t;
299 array2ptr(&de);
300 de.ptr--;
301 return type_totsz(&de);
304 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
305 static void ts_de(int deref)
307 struct type *t = &ts[nts - 1];
308 array2ptr(t);
309 if (deref && t->addr && (t->ptr || !(t->flags & T_FUNC)))
310 o_deref(TYPE_BT(t));
311 t->addr = 0;
314 /* pop stack pop to *t and dereference if t->addr */
315 static void ts_pop_de(struct type *t)
317 ts_de(1);
318 ts_pop(t);
321 /* pop the top 2 stack values and dereference them if t->addr */
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 /* function prototypes for parsing function and variable declarations */
396 static int readname(struct type *main, char *name, struct type *base);
397 static int readtype(struct type *type);
398 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
399 void *data);
400 static int readdefs_int(void (*def)(void *data, struct name *name, unsigned flags),
401 void *data);
403 /* function prototypes for parsing initializer expressions */
404 static int initsize(void);
405 static void initexpr(struct type *t, int off, void *obj,
406 void (*set)(void *obj, int off, struct type *t));
408 static int type_alignment(struct type *t)
410 if (t->flags & T_ARRAY && !t->ptr)
411 return type_alignment(&arrays[t->id].type);
412 if (t->flags & T_STRUCT && !t->ptr)
413 return type_alignment(&structs[t->id].fields[0].type);
414 return MIN(LONGSZ, type_totsz(t));
417 static void structdef(void *data, struct name *name, unsigned flags)
419 struct structinfo *si = data;
420 if (si->isunion) {
421 name->addr = 0;
422 if (si->size < type_totsz(&name->type))
423 si->size = type_totsz(&name->type);
424 } else {
425 struct type *t = &name->type;
426 int alignment = type_alignment(t);
427 if (t->flags & T_ARRAY && !t->ptr)
428 alignment = MIN(LONGSZ, type_totsz(&arrays[t->id].type));
429 si->size = ALIGN(si->size, alignment);
430 name->addr = si->size;
431 si->size += type_totsz(&name->type);
433 memcpy(&si->fields[si->nfields++], name, sizeof(*name));
436 static int struct_create(char *name, int isunion)
438 int id = struct_find(name, isunion);
439 struct structinfo *si = &structs[id];
440 tok_expect('{');
441 while (tok_jmp('}')) {
442 readdefs(structdef, si);
443 tok_expect(';');
445 return id;
448 static void readexpr(void);
450 static void enum_create(void)
452 long n = 0;
453 tok_expect('{');
454 while (tok_jmp('}')) {
455 char name[NAMELEN];
456 tok_expect(TOK_NAME);
457 strcpy(name, tok_id());
458 if (!tok_jmp('=')) {
459 readexpr();
460 ts_pop_de(NULL);
461 if (o_popnum(&n))
462 err("const expr expected!\n");
464 enum_add(name, n++);
465 tok_jmp(',');
469 /* used to differenciate labels from case and cond exprs */
470 static int ncexpr;
471 static int caseexpr;
473 static void readpre(void);
475 static char *tmp_str(char *buf, int len)
477 static char name[NAMELEN];
478 static int id;
479 void *dat;
480 sprintf(name, "__neatcc.s%d", id++);
481 dat = o_mkdat(name, len, 0);
482 memcpy(dat, buf, len);
483 return name;
486 static void readprimary(void)
488 if (!tok_jmp(TOK_NUM)) {
489 long n;
490 int bt = tok_num(&n);
491 ts_push_bt(bt);
492 o_num(n);
493 return;
495 if (!tok_jmp(TOK_STR)) {
496 struct type t = {}; /* char type inside the arrays */
497 struct type a = {}; /* the char array type */
498 char buf[BUFSIZE];
499 int len = tok_str(buf);
500 t.bt = 1 | BT_SIGNED;
501 a.id = array_add(&t, len);
502 a.flags = T_ARRAY;
503 ts_push(&a);
504 o_sym(tmp_str(buf, len));
505 return;
507 if (!tok_jmp(TOK_NAME)) {
508 struct name unkn = {""};
509 char *name = unkn.name;
510 int n;
511 strcpy(name, tok_id());
512 /* don't search for labels here */
513 if (!ncexpr && !caseexpr && tok_see() == ':')
514 return;
515 if ((n = local_find(name)) != -1) {
516 struct name *l = &locals[n];
517 o_local(l->addr);
518 ts_push_addr(&l->type);
519 return;
521 if ((n = global_find(name)) != -1) {
522 struct name *g = &globals[n];
523 o_sym(*g->elfname ? g->elfname : g->name);
524 ts_push_addr(&g->type);
525 return;
527 if (!enum_find(&n, name)) {
528 ts_push_bt(4 | BT_SIGNED);
529 o_num(n);
530 return;
532 if (tok_see() != '(')
533 err("unknown symbol <%s>\n", name);
534 global_add(&unkn);
535 ts_push_bt(LONGSZ);
536 o_sym(unkn.name);
537 return;
539 if (!tok_jmp('(')) {
540 struct type t;
541 if (!readtype(&t)) {
542 struct type o;
543 tok_expect(')');
544 readpre();
545 ts_pop_de(&o);
546 ts_push(&t);
547 if (!t.ptr || !o.ptr)
548 o_cast(TYPE_BT(&t));
549 } else {
550 readexpr();
551 while (tok_jmp(')')) {
552 tok_expect(',');
553 ts_pop(NULL);
554 o_tmpdrop(1);
555 readexpr();
558 return;
562 static void arrayderef(void)
564 struct type t;
565 int sz;
566 ts_pop_de(NULL);
567 ts_pop(&t);
568 if (!(t.flags & T_ARRAY && !t.ptr) && t.addr) {
569 o_tmpswap();
570 o_deref(TYPE_BT(&t));
571 o_tmpswap();
573 array2ptr(&t);
574 t.ptr--;
575 sz = type_totsz(&t);
576 t.addr = 1;
577 if (sz > 1) {
578 o_num(sz);
579 o_bop(O_MUL);
581 o_bop(O_ADD);
582 ts_push(&t);
585 static void inc_post(int op)
587 struct type t = ts[nts - 1];
588 /* pushing the value before inc */
589 o_tmpcopy();
590 ts_de(1);
591 o_load();
592 o_tmpswap();
594 /* increment by 1 or pointer size */
595 o_tmpcopy();
596 ts_push(&t);
597 ts_pop_de(&t);
598 o_num(t.ptr > 0 ? type_szde(&t) : 1);
599 o_bop(op);
601 /* assign back */
602 o_assign(TYPE_BT(&t));
603 o_tmpdrop(1);
606 static void readfield(void)
608 struct name *field;
609 struct type t;
610 tok_expect(TOK_NAME);
611 ts_pop(&t);
612 array2ptr(&t);
613 field = struct_field(t.id, tok_id());
614 if (field->addr) {
615 o_num(field->addr);
616 o_bop(O_ADD);
618 ts_push_addr(&field->type);
621 #define MAXFUNCS (1 << 10)
623 static struct funcinfo {
624 struct type args[MAXARGS];
625 struct type ret;
626 int nargs;
627 int varg;
628 /* function and argument names; useful only when defining */
629 char argnames[MAXARGS][NAMELEN];
630 char name[NAMELEN];
631 } funcs[MAXFUNCS];
632 static int nfuncs;
634 static int func_create(struct type *ret, char *name, char argnames[][NAMELEN],
635 struct type *args, int nargs, int varg)
637 struct funcinfo *fi = &funcs[nfuncs++];
638 int i;
639 if (nfuncs >= MAXFUNCS)
640 err("nomem: MAXFUNCS reached!\n");
641 memcpy(&fi->ret, ret, sizeof(*ret));
642 for (i = 0; i < nargs; i++)
643 memcpy(&fi->args[i], &args[i], sizeof(*ret));
644 fi->nargs = nargs;
645 fi->varg = varg;
646 strcpy(fi->name, name ? name : "");
647 for (i = 0; i < nargs; i++)
648 strcpy(fi->argnames[i], argnames[i]);
649 return fi - funcs;
652 static void readcall(void)
654 struct type t;
655 struct funcinfo *fi;
656 int argc = 0;
657 ts_pop(&t);
658 if (t.flags & T_FUNC && t.ptr > 0)
659 o_deref(LONGSZ);
660 fi = t.flags & T_FUNC ? &funcs[t.id] : NULL;
661 if (tok_see() != ')') {
662 do {
663 readexpr();
664 ts_pop_de(NULL);
665 argc++;
666 } while (!tok_jmp(','));
668 tok_expect(')');
669 o_call(argc, fi ? TYPE_BT(&fi->ret) : 4 | BT_SIGNED);
670 if (fi) {
671 if (TYPE_BT(&fi->ret))
672 o_cast(TYPE_BT(&fi->ret));
673 ts_push(&fi->ret);
674 } else {
675 ts_push_bt(4 | BT_SIGNED);
679 static void readpost(void)
681 readprimary();
682 while (1) {
683 if (!tok_jmp('[')) {
684 readexpr();
685 tok_expect(']');
686 arrayderef();
687 continue;
689 if (!tok_jmp('(')) {
690 readcall();
691 continue;
693 if (!tok_jmp(TOK2("++"))) {
694 inc_post(O_ADD);
695 continue;
697 if (!tok_jmp(TOK2("--"))) {
698 inc_post(O_SUB);
699 continue;
701 if (!tok_jmp('.')) {
702 readfield();
703 continue;
705 if (!tok_jmp(TOK2("->"))) {
706 ts_de(1);
707 readfield();
708 continue;
710 break;
714 static void inc_pre(int op)
716 struct type t;
717 readpre();
718 /* copy the destination */
719 o_tmpcopy();
720 ts_push(&ts[nts - 1]);
721 /* increment by 1 or pointer size */
722 ts_pop_de(&t);
723 o_num(t.ptr > 0 ? type_szde(&t) : 1);
724 o_bop(op);
725 /* assign the result */
726 o_assign(TYPE_BT(&t));
727 ts_de(0);
730 static void readpre(void)
732 if (!tok_jmp('&')) {
733 struct type type;
734 readpre();
735 ts_pop(&type);
736 if (!type.addr)
737 err("cannot use the address\n");
738 type.ptr++;
739 type.addr = 0;
740 ts_push(&type);
741 return;
743 if (!tok_jmp('*')) {
744 struct type t;
745 readpre();
746 ts_pop(&t);
747 array2ptr(&t);
748 if (!t.ptr)
749 err("dereferencing non-pointer\n");
750 if (t.addr)
751 o_deref(TYPE_BT(&t));
752 t.ptr--;
753 t.addr = 1;
754 ts_push(&t);
755 return;
757 if (!tok_jmp('!')) {
758 readpre();
759 ts_pop_de(NULL);
760 o_uop(O_LNOT);
761 ts_push_bt(4 | BT_SIGNED);
762 return;
764 if (!tok_jmp('+')) {
765 readpre();
766 return;
768 if (!tok_jmp('-')) {
769 readpre();
770 ts_de(1);
771 o_uop(O_NEG);
772 return;
774 if (!tok_jmp('~')) {
775 readpre();
776 ts_de(1);
777 o_uop(O_NOT);
778 return;
780 if (!tok_jmp(TOK2("++"))) {
781 inc_pre(O_ADD);
782 return;
784 if (!tok_jmp(TOK2("--"))) {
785 inc_pre(O_SUB);
786 return;
788 if (!tok_jmp(TOK_SIZEOF)) {
789 struct type t;
790 int op = !tok_jmp('(');
791 if (readtype(&t)) {
792 nogen++;
793 if (op)
794 readexpr();
795 else
796 readpre();
797 nogen--;
798 ts_pop(&t);
800 ts_push_bt(4);
801 o_num(type_totsz(&t));
802 if (op)
803 tok_expect(')');
804 return;
806 readpost();
809 static void readmul(void)
811 readpre();
812 while (1) {
813 if (!tok_jmp('*')) {
814 readpre();
815 ts_binop(O_MUL);
816 continue;
818 if (!tok_jmp('/')) {
819 readpre();
820 ts_binop(O_DIV);
821 continue;
823 if (!tok_jmp('%')) {
824 readpre();
825 ts_binop(O_MOD);
826 continue;
828 break;
832 static void readadd(void)
834 readmul();
835 while (1) {
836 if (!tok_jmp('+')) {
837 readmul();
838 ts_addop(O_ADD);
839 continue;
841 if (!tok_jmp('-')) {
842 readmul();
843 ts_addop(O_SUB);
844 continue;
846 break;
850 static void shift(int op)
852 struct type t;
853 readadd();
854 ts_pop_de2(NULL, &t);
855 o_bop(op | (BT_SIGNED & TYPE_BT(&t) ? O_SIGNED : 0));
856 ts_push_bt(TYPE_BT(&t));
859 static void readshift(void)
861 readadd();
862 while (1) {
863 if (!tok_jmp(TOK2("<<"))) {
864 shift(O_SHL);
865 continue;
867 if (!tok_jmp(TOK2(">>"))) {
868 shift(O_SHR);
869 continue;
871 break;
875 static void cmp(int op)
877 struct type t1, t2;
878 int bt;
879 readshift();
880 ts_pop_de2(&t1, &t2);
881 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
882 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
883 ts_push_bt(4 | BT_SIGNED);
886 static void readcmp(void)
888 readshift();
889 while (1) {
890 if (!tok_jmp('<')) {
891 cmp(O_LT);
892 continue;
894 if (!tok_jmp('>')) {
895 cmp(O_GT);
896 continue;
898 if (!tok_jmp(TOK2("<="))) {
899 cmp(O_LE);
900 continue;
902 if (!tok_jmp(TOK2(">="))) {
903 cmp(O_GE);
904 continue;
906 break;
910 static void eq(int op)
912 readcmp();
913 ts_pop_de2(NULL, NULL);
914 o_bop(op);
915 ts_push_bt(4 | BT_SIGNED);
918 static void readeq(void)
920 readcmp();
921 while (1) {
922 if (!tok_jmp(TOK2("=="))) {
923 eq(O_EQ);
924 continue;
926 if (!tok_jmp(TOK2("!="))) {
927 eq(O_NEQ);
928 continue;
930 break;
934 static void readbitand(void)
936 readeq();
937 while (!tok_jmp('&')) {
938 readeq();
939 ts_binop(O_AND);
943 static void readxor(void)
945 readbitand();
946 while (!tok_jmp('^')) {
947 readbitand();
948 ts_binop(O_XOR);
952 static void readbitor(void)
954 readxor();
955 while (!tok_jmp('|')) {
956 readxor();
957 ts_binop(O_OR);
961 #define MAXCOND (1 << 7)
963 static void readand(void)
965 int l_out = LABEL();
966 int l_fail = LABEL();
967 readbitor();
968 if (tok_see() != TOK2("&&"))
969 return;
970 o_fork();
971 ts_pop_de(NULL);
972 o_jz(l_fail);
973 while (!tok_jmp(TOK2("&&"))) {
974 readbitor();
975 ts_pop_de(NULL);
976 o_jz(l_fail);
978 o_num(1);
979 o_forkpush();
980 o_jmp(l_out);
981 o_label(l_fail);
982 o_num(0);
983 o_forkpush();
984 o_forkjoin();
985 o_label(l_out);
986 ts_push_bt(4 | BT_SIGNED);
989 static void reador(void)
991 int l_pass = LABEL();
992 int l_end = LABEL();
993 readand();
994 if (tok_see() != TOK2("||"))
995 return;
996 o_fork();
997 ts_pop_de(NULL);
998 o_jnz(l_pass);
999 while (!tok_jmp(TOK2("||"))) {
1000 readand();
1001 ts_pop_de(NULL);
1002 o_jnz(l_pass);
1004 o_num(0);
1005 o_forkpush();
1006 o_jmp(l_end);
1007 o_label(l_pass);
1008 o_num(1);
1009 o_forkpush();
1010 o_forkjoin();
1011 o_label(l_end);
1012 ts_push_bt(4 | BT_SIGNED);
1015 static void readcexpr(void);
1017 static int readcexpr_const(void)
1019 long c;
1020 if (o_popnum(&c))
1021 return -1;
1022 if (!c)
1023 nogen++;
1024 readcexpr();
1025 /* both branches yield the same type; so ignore the first */
1026 ts_pop_de(NULL);
1027 tok_expect(':');
1028 if (c)
1029 nogen++;
1030 else
1031 nogen--;
1032 readcexpr();
1033 /* making sure t->addr == 0 on both branches */
1034 ts_de(1);
1035 if (c)
1036 nogen--;
1037 return 0;
1040 static void readcexpr(void)
1042 reador();
1043 if (tok_jmp('?'))
1044 return;
1045 ncexpr++;
1046 ts_pop_de(NULL);
1047 o_fork();
1048 if (readcexpr_const()) {
1049 int l_fail = LABEL();
1050 int l_end = LABEL();
1051 struct type ret;
1052 o_jz(l_fail);
1053 readcexpr();
1054 /* both branches yield the same type; so ignore the first */
1055 ts_pop_de(&ret);
1056 if (!TYPE_VOID(&ret))
1057 o_forkpush();
1058 o_jmp(l_end);
1060 tok_expect(':');
1061 o_label(l_fail);
1062 readcexpr();
1063 /* making sure t->addr == 0 on both branches */
1064 ts_de(1);
1065 if (!TYPE_VOID(&ret)) {
1066 o_forkpush();
1067 o_forkjoin();
1069 o_label(l_end);
1071 ncexpr--;
1074 static void opassign(int op, int ptrop)
1076 struct type t = ts[nts - 1];
1077 o_tmpcopy();
1078 ts_push(&t);
1079 readexpr();
1080 if (op == O_ADD || op == O_SUB)
1081 ts_addop(op);
1082 else
1083 ts_binop(op);
1084 o_assign(TYPE_BT(&ts[nts - 2]));
1085 ts_pop(NULL);
1086 ts_de(0);
1089 static void doassign(void)
1091 struct type t = ts[nts - 1];
1092 if (!t.ptr && t.flags & T_STRUCT) {
1093 ts_pop(NULL);
1094 o_num(type_totsz(&t));
1095 o_memcpy();
1096 } else {
1097 ts_pop_de(NULL);
1098 o_assign(TYPE_BT(&ts[nts - 1]));
1099 ts_de(0);
1103 static void readexpr(void)
1105 readcexpr();
1106 if (!tok_jmp('=')) {
1107 readexpr();
1108 doassign();
1109 return;
1111 if (!tok_jmp(TOK2("+="))) {
1112 opassign(O_ADD, 1);
1113 return;
1115 if (!tok_jmp(TOK2("-="))) {
1116 opassign(O_SUB, 1);
1117 return;
1119 if (!tok_jmp(TOK2("*="))) {
1120 opassign(O_MUL, 0);
1121 return;
1123 if (!tok_jmp(TOK2("/="))) {
1124 opassign(O_DIV, 0);
1125 return;
1127 if (!tok_jmp(TOK2("%="))) {
1128 opassign(O_MOD, 0);
1129 return;
1131 if (!tok_jmp(TOK3("<<="))) {
1132 opassign(O_SHL, 0);
1133 return;
1135 if (!tok_jmp(TOK3(">>="))) {
1136 opassign(O_SHR, 0);
1137 return;
1139 if (!tok_jmp(TOK3("&="))) {
1140 opassign(O_AND, 0);
1141 return;
1143 if (!tok_jmp(TOK3("|="))) {
1144 opassign(O_OR, 0);
1145 return;
1147 if (!tok_jmp(TOK3("^="))) {
1148 opassign(O_XOR, 0);
1149 return;
1153 static void readestmt(void)
1155 do {
1156 o_tmpdrop(-1);
1157 nts = 0;
1158 readexpr();
1159 } while (!tok_jmp(','));
1162 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1164 static void globalinit(void *obj, int off, struct type *t)
1166 struct name *name = obj;
1167 char *elfname = *name->elfname ? name->elfname : name->name;
1168 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1169 struct type *t_de = &arrays[t->id].type;
1170 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1171 char buf[BUFSIZE];
1172 int len;
1173 tok_expect(TOK_STR);
1174 len = tok_str(buf);
1175 memcpy((void *) name->addr + off, buf, len);
1176 return;
1179 readexpr();
1180 o_datset(elfname, off, TYPE_BT(t));
1181 ts_pop(NULL);
1184 static void readfunc(struct name *name, int flags);
1186 static void globaldef(void *data, struct name *name, unsigned flags)
1188 struct type *t = &name->type;
1189 char *elfname = *name->elfname ? name->elfname : name->name;
1190 int sz;
1191 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1192 if (~flags & F_EXTERN)
1193 arrays[t->id].n = initsize();
1194 sz = type_totsz(t);
1195 if (!(flags & F_EXTERN) && (!(t->flags & T_FUNC) || t->ptr)) {
1196 if (tok_see() == '=')
1197 name->addr = (long) o_mkdat(elfname, sz, F_GLOBAL(flags));
1198 else
1199 o_mkbss(elfname, sz, F_GLOBAL(flags));
1201 global_add(name);
1202 if (!tok_jmp('='))
1203 initexpr(t, 0, name, globalinit);
1204 if (tok_see() == '{' && name->type.flags & T_FUNC)
1205 readfunc(name, flags);
1208 /* generate the address of local + off */
1209 static void o_localoff(long addr, int off)
1211 o_local(addr);
1212 if (off) {
1213 o_num(off);
1214 o_bop(O_ADD);
1218 static void localinit(void *obj, int off, struct type *t)
1220 long addr = *(long *) obj;
1221 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1222 struct type *t_de = &arrays[t->id].type;
1223 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1224 char buf[BUFSIZE];
1225 int len;
1226 tok_expect(TOK_STR);
1227 len = tok_str(buf);
1228 o_localoff(addr, off);
1229 o_sym(tmp_str(buf, len));
1230 o_num(len);
1231 o_memcpy();
1232 o_tmpdrop(1);
1233 return;
1236 o_localoff(addr, off);
1237 ts_push(t);
1238 readexpr();
1239 doassign();
1240 ts_pop(NULL);
1241 o_tmpdrop(1);
1244 /* current function name */
1245 static char func_name[NAMELEN];
1247 static void localdef(void *data, struct name *name, unsigned flags)
1249 struct type *t = &name->type;
1250 if ((flags & F_EXTERN) || ((t->flags & T_FUNC) && !t->ptr)) {
1251 global_add(name);
1252 return;
1254 if (flags & F_STATIC) {
1255 sprintf(name->elfname, "__neatcc.%s.%s", func_name, name->name);
1256 globaldef(data, name, flags);
1257 return;
1259 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1260 arrays[t->id].n = initsize();
1261 name->addr = o_mklocal(type_totsz(&name->type));
1262 local_add(name);
1263 if (!tok_jmp('=')) {
1264 if (t->flags & (T_ARRAY | T_STRUCT) && !t->ptr) {
1265 o_local(name->addr);
1266 o_num(0);
1267 o_num(type_totsz(t));
1268 o_memset();
1269 o_tmpdrop(1);
1271 initexpr(t, 0, &name->addr, localinit);
1275 static void typedefdef(void *data, struct name *name, unsigned flags)
1277 typedef_add(name->name, &name->type);
1280 static void readstmt(void);
1282 #define MAXCASES (1 << 7)
1284 static void readswitch(void)
1286 int o_break = l_break;
1287 long val_addr = o_mklocal(LONGSZ);
1288 struct type t;
1289 int ncases = 0; /* number of case labels */
1290 int l_failed = LABEL(); /* address of last failed jmp */
1291 int l_matched = LABEL(); /* address of last walk through jmp */
1292 int l_default = 0; /* default case label */
1293 l_break = LABEL();
1294 tok_expect('(');
1295 readexpr();
1296 ts_pop_de(&t);
1297 o_local(val_addr);
1298 o_tmpswap();
1299 o_assign(TYPE_BT(&t));
1300 ts_de(0);
1301 o_tmpdrop(1);
1302 tok_expect(')');
1303 tok_expect('{');
1304 while (tok_jmp('}')) {
1305 if (tok_see() != TOK_CASE && tok_see() != TOK_DEFAULT) {
1306 readstmt();
1307 continue;
1309 if (ncases)
1310 o_jmp(l_matched);
1311 if (tok_get() == TOK_CASE) {
1312 o_label(l_failed);
1313 l_failed = LABEL();
1314 caseexpr = 1;
1315 readexpr();
1316 ts_pop_de(NULL);
1317 caseexpr = 0;
1318 o_local(val_addr);
1319 o_deref(TYPE_BT(&t));
1320 o_bop(O_EQ);
1321 o_jz(l_failed);
1322 o_tmpdrop(1);
1323 } else {
1324 if (!ncases)
1325 o_jmp(l_failed);
1326 l_default = LABEL();
1327 o_label(l_default);
1329 tok_expect(':');
1330 o_label(l_matched);
1331 l_matched = LABEL();
1332 ncases++;
1334 o_rmlocal(val_addr, LONGSZ);
1335 o_jmp(l_break);
1336 o_label(l_failed);
1337 if (l_default)
1338 o_jmp(l_default);
1339 o_label(l_break);
1340 l_break = o_break;
1343 #define MAXLABELS (1 << 10)
1345 static char label_name[MAXLABELS][NAMELEN];
1346 static int label_ids[MAXLABELS];
1347 static int nlabels;
1349 static int label_id(char *name)
1351 int i;
1352 for (i = nlabels - 1; i >= 0; --i)
1353 if (!strcmp(label_name[i], name))
1354 return label_ids[i];
1355 strcpy(label_name[nlabels], name);
1356 label_ids[nlabels] = LABEL();
1357 return label_ids[nlabels++];
1360 static void readstmt(void)
1362 o_tmpdrop(-1);
1363 nts = 0;
1364 if (!tok_jmp('{')) {
1365 int _nlocals = nlocals;
1366 int _nglobals = nglobals;
1367 int _nenums = nenums;
1368 int _ntypedefs = ntypedefs;
1369 int _nstructs = nstructs;
1370 int _nfuncs = nfuncs;
1371 int _narrays = narrays;
1372 while (tok_jmp('}'))
1373 readstmt();
1374 nlocals = _nlocals;
1375 nenums = _nenums;
1376 ntypedefs = _ntypedefs;
1377 nstructs = _nstructs;
1378 nfuncs = _nfuncs;
1379 narrays = _narrays;
1380 nglobals = _nglobals;
1381 return;
1383 if (!readdefs(localdef, NULL)) {
1384 tok_expect(';');
1385 return;
1387 if (!tok_jmp(TOK_TYPEDEF)) {
1388 readdefs(typedefdef, NULL);
1389 tok_expect(';');
1390 return;
1392 if (!tok_jmp(TOK_IF)) {
1393 int l_fail = LABEL();
1394 int l_end = LABEL();
1395 tok_expect('(');
1396 readexpr();
1397 tok_expect(')');
1398 ts_pop_de(NULL);
1399 o_jz(l_fail);
1400 readstmt();
1401 if (!tok_jmp(TOK_ELSE)) {
1402 o_jmp(l_end);
1403 o_label(l_fail);
1404 readstmt();
1405 o_label(l_end);
1406 } else {
1407 o_label(l_fail);
1409 return;
1411 if (!tok_jmp(TOK_WHILE)) {
1412 int o_break = l_break;
1413 int o_cont = l_cont;
1414 l_break = LABEL();
1415 l_cont = LABEL();
1416 o_label(l_cont);
1417 tok_expect('(');
1418 readexpr();
1419 tok_expect(')');
1420 ts_pop_de(NULL);
1421 o_jz(l_break);
1422 readstmt();
1423 o_jmp(l_cont);
1424 o_label(l_break);
1425 l_break = o_break;
1426 l_cont = o_cont;
1427 return;
1429 if (!tok_jmp(TOK_DO)) {
1430 int o_break = l_break;
1431 int o_cont = l_cont;
1432 int l_beg = LABEL();
1433 l_break = LABEL();
1434 l_cont = LABEL();
1435 o_label(l_beg);
1436 readstmt();
1437 tok_expect(TOK_WHILE);
1438 tok_expect('(');
1439 o_label(l_cont);
1440 readexpr();
1441 ts_pop_de(NULL);
1442 o_jnz(l_beg);
1443 tok_expect(')');
1444 o_label(l_break);
1445 tok_expect(';');
1446 l_break = o_break;
1447 l_cont = o_cont;
1448 return;
1450 if (!tok_jmp(TOK_FOR)) {
1451 int o_break = l_break;
1452 int o_cont = l_cont;
1453 int l_check = LABEL(); /* for condition label */
1454 int l_body = LABEL(); /* for block label */
1455 l_cont = LABEL();
1456 l_break = LABEL();
1457 tok_expect('(');
1458 if (tok_see() != ';')
1459 readestmt();
1460 tok_expect(';');
1461 o_label(l_check);
1462 if (tok_see() != ';') {
1463 readestmt();
1464 ts_pop_de(NULL);
1465 o_jz(l_break);
1467 tok_expect(';');
1468 o_jmp(l_body);
1469 o_label(l_cont);
1470 if (tok_see() != ')')
1471 readestmt();
1472 tok_expect(')');
1473 o_jmp(l_check);
1474 o_label(l_body);
1475 readstmt();
1476 o_jmp(l_cont);
1477 o_label(l_break);
1478 l_break = o_break;
1479 l_cont = o_cont;
1480 return;
1482 if (!tok_jmp(TOK_SWITCH)) {
1483 readswitch();
1484 return;
1486 if (!tok_jmp(TOK_RETURN)) {
1487 int ret = tok_see() != ';';
1488 if (ret) {
1489 readexpr();
1490 ts_pop_de(NULL);
1492 tok_expect(';');
1493 o_ret(ret);
1494 return;
1496 if (!tok_jmp(TOK_BREAK)) {
1497 tok_expect(';');
1498 o_jmp(l_break);
1499 return;
1501 if (!tok_jmp(TOK_CONTINUE)) {
1502 tok_expect(';');
1503 o_jmp(l_cont);
1504 return;
1506 if (!tok_jmp(TOK_GOTO)) {
1507 tok_expect(TOK_NAME);
1508 o_jmp(label_id(tok_id()));
1509 tok_expect(';');
1510 return;
1512 readestmt();
1513 /* labels */
1514 if (!tok_jmp(':')) {
1515 o_label(label_id(tok_id()));
1516 return;
1518 tok_expect(';');
1521 static void readfunc(struct name *name, int flags)
1523 struct funcinfo *fi = &funcs[name->type.id];
1524 long beg = tok_addr();
1525 int i;
1526 strcpy(func_name, fi->name);
1527 o_func_beg(func_name, fi->nargs, F_GLOBAL(flags), fi->varg);
1528 for (i = 0; i < fi->nargs; i++) {
1529 struct name arg = {"", "", fi->args[i], o_arg2loc(i)};
1530 strcpy(arg.name, fi->argnames[i]);
1531 local_add(&arg);
1533 /* first pass: collecting statistics */
1534 o_pass1();
1535 readstmt();
1536 tok_jump(beg);
1537 /* second pass: generating code */
1538 o_pass2();
1539 readstmt();
1540 o_func_end();
1541 func_name[0] = '\0';
1542 nlocals = 0;
1543 label = 0;
1544 nlabels = 0;
1547 static void readdecl(void)
1549 if (!tok_jmp(TOK_TYPEDEF)) {
1550 readdefs(typedefdef, NULL);
1551 tok_expect(';');
1552 return;
1554 readdefs_int(globaldef, NULL);
1555 tok_jmp(';');
1558 static void parse(void)
1560 while (tok_see() != TOK_EOF)
1561 readdecl();
1564 static void compat_macros(void)
1566 cpp_define("__STDC__", "");
1567 cpp_define("__linux__", "");
1568 cpp_define(I_ARCH, "");
1570 /* ignored keywords */
1571 cpp_define("const", "");
1572 cpp_define("register", "");
1573 cpp_define("volatile", "");
1574 cpp_define("inline", "");
1575 cpp_define("restrict", "");
1576 cpp_define("__inline__", "");
1577 cpp_define("__restrict__", "");
1578 cpp_define("__attribute__(x)", "");
1579 cpp_define("__builtin_va_list__", "long");
1582 int main(int argc, char *argv[])
1584 char obj[128] = "";
1585 int ofd;
1586 int i = 1;
1587 compat_macros();
1588 while (i < argc && argv[i][0] == '-') {
1589 if (argv[i][1] == 'I')
1590 cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]);
1591 if (argv[i][1] == 'D') {
1592 char *name = argv[i] + 2;
1593 char *def = "";
1594 char *eq = strchr(name, '=');
1595 if (eq) {
1596 *eq = '\0';
1597 def = eq + 1;
1599 cpp_define(name, def);
1601 if (argv[i][1] == 'o')
1602 strcpy(obj, argv[i][2] ? argv[i] + 2 : argv[++i]);
1603 i++;
1605 if (i == argc)
1606 die("neatcc: no file given\n");
1607 if (cpp_init(argv[i]))
1608 die("neatcc: cannot open <%s>\n", argv[i]);
1609 parse();
1610 if (!*obj) {
1611 strcpy(obj, argv[i]);
1612 obj[strlen(obj) - 1] = 'o';
1614 ofd = open(obj, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1615 o_write(ofd);
1616 close(ofd);
1617 return 0;
1621 /* parsing function and variable declarations */
1623 /* read the base type of a variable */
1624 static int basetype(struct type *type, unsigned *flags)
1626 int sign = 1;
1627 int size = 4;
1628 int done = 0;
1629 int i = 0;
1630 int isunion;
1631 char name[NAMELEN] = "";
1632 *flags = 0;
1633 type->flags = 0;
1634 type->ptr = 0;
1635 type->addr = 0;
1636 while (!done) {
1637 switch (tok_see()) {
1638 case TOK_STATIC:
1639 *flags |= F_STATIC;
1640 break;
1641 case TOK_EXTERN:
1642 *flags |= F_EXTERN;
1643 break;
1644 case TOK_VOID:
1645 sign = 0;
1646 size = 0;
1647 done = 1;
1648 break;
1649 case TOK_INT:
1650 done = 1;
1651 break;
1652 case TOK_CHAR:
1653 size = 1;
1654 done = 1;
1655 break;
1656 case TOK_SHORT:
1657 size = 2;
1658 break;
1659 case TOK_LONG:
1660 size = LONGSZ;
1661 break;
1662 case TOK_SIGNED:
1663 break;
1664 case TOK_UNSIGNED:
1665 sign = 0;
1666 break;
1667 case TOK_UNION:
1668 case TOK_STRUCT:
1669 isunion = tok_get() == TOK_UNION;
1670 if (!tok_jmp(TOK_NAME))
1671 strcpy(name, tok_id());
1672 if (tok_see() == '{')
1673 type->id = struct_create(name, isunion);
1674 else
1675 type->id = struct_find(name, isunion);
1676 type->flags |= T_STRUCT;
1677 type->bt = LONGSZ;
1678 return 0;
1679 case TOK_ENUM:
1680 tok_get();
1681 tok_jmp(TOK_NAME);
1682 if (tok_see() == '{')
1683 enum_create();
1684 type->bt = 4 | BT_SIGNED;
1685 return 0;
1686 default:
1687 if (tok_see() == TOK_NAME) {
1688 int id = typedef_find(tok_id());
1689 if (id != -1) {
1690 tok_get();
1691 memcpy(type, &typedefs[id].type,
1692 sizeof(*type));
1693 return 0;
1696 if (!i)
1697 return 1;
1698 done = 1;
1699 continue;
1701 i++;
1702 tok_get();
1704 type->bt = size | (sign ? BT_SIGNED : 0);
1705 return 0;
1708 static void readptrs(struct type *type)
1710 while (!tok_jmp('*')) {
1711 type->ptr++;
1712 if (!type->bt)
1713 type->bt = 1;
1717 /* read function arguments */
1718 static int readargs(struct type *args, char argnames[][NAMELEN], int *varg)
1720 int nargs = 0;
1721 tok_expect('(');
1722 *varg = 0;
1723 while (tok_see() != ')') {
1724 if (!tok_jmp(TOK3("..."))) {
1725 *varg = 1;
1726 break;
1728 if (readname(&args[nargs], argnames[nargs], NULL)) {
1729 /* argument has no type, assume int */
1730 tok_expect(TOK_NAME);
1731 memset(&args[nargs], 0, sizeof(struct type));
1732 args[nargs].bt = 4 | BT_SIGNED;
1733 strcpy(argnames[nargs], tok_id());
1735 /* argument arrays are pointers */
1736 array2ptr(&args[nargs]);
1737 nargs++;
1738 if (tok_jmp(','))
1739 break;
1741 tok_expect(')');
1742 /* void argument */
1743 if (nargs == 1 && !TYPE_BT(&args[0]))
1744 return 0;
1745 return nargs;
1748 /* read K&R function arguments */
1749 static void krdef(void *data, struct name *name, unsigned flags)
1751 struct funcinfo *fi = data;
1752 int i;
1753 for (i = 0; i < fi->nargs; i++)
1754 if (!strcmp(fi->argnames[i], name->name))
1755 memcpy(&fi->args[i], &name->type, sizeof(name->type));
1759 * readarrays() parses array specifiers when reading a definition in
1760 * readname(). The "type" parameter contains the type contained in the
1761 * inner array; for instance, type in "int *a[10][20]" would be an int
1762 * pointer. When returning, the "type" parameter is changed to point
1763 * to the final array. The function returns a pointer to the type in
1764 * the inner array; this is useful when the type is not complete yet,
1765 * like when creating an array of function pointers as in
1766 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1768 static struct type *readarrays(struct type *type)
1770 long arsz[16];
1771 struct type *inner = NULL;
1772 int nar = 0;
1773 int i;
1774 while (!tok_jmp('[')) {
1775 long n = 0;
1776 if (tok_jmp(']')) {
1777 readexpr();
1778 ts_pop_de(NULL);
1779 if (o_popnum(&n))
1780 err("const expr expected\n");
1781 tok_expect(']');
1783 arsz[nar++] = n;
1785 for (i = nar - 1; i >= 0; i--) {
1786 type->id = array_add(type, arsz[i]);
1787 if (!inner)
1788 inner = &arrays[type->id].type;
1789 type->flags = T_ARRAY;
1790 type->bt = LONGSZ;
1791 type->ptr = 0;
1793 return inner;
1797 * readname() reads a variable definition; the name is copied into
1798 * "name" and the type is copied into "main" argument. The "base"
1799 * argument, if not NULL, indicates the base type of the variable.
1800 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1801 * "int". If NULL, basetype() is called directly to read the base
1802 * type of the variable. readname() returns zero, only if the
1803 * variable can be read.
1805 static int readname(struct type *main, char *name, struct type *base)
1807 struct type tpool[3];
1808 int npool = 0;
1809 struct type *type = &tpool[npool++];
1810 struct type *ptype = NULL; /* type inside parenthesis */
1811 struct type *btype = NULL; /* type before parenthesis */
1812 struct type *inner;
1813 unsigned flags;
1814 memset(tpool, 0, sizeof(tpool));
1815 if (name)
1816 *name = '\0';
1817 if (!base) {
1818 if (basetype(type, &flags))
1819 return 1;
1820 } else {
1821 memcpy(type, base, sizeof(*base));
1823 readptrs(type);
1824 if (!tok_jmp('(')) {
1825 btype = type;
1826 type = &tpool[npool++];
1827 ptype = type;
1828 readptrs(type);
1830 if (!tok_jmp(TOK_NAME) && name)
1831 strcpy(name, tok_id());
1832 inner = readarrays(type);
1833 if (ptype && inner)
1834 ptype = inner;
1835 if (ptype)
1836 tok_expect(')');
1837 if (tok_see() == '(') {
1838 struct type args[MAXARGS];
1839 char argnames[MAXARGS][NAMELEN];
1840 int varg = 0;
1841 int nargs = readargs(args, argnames, &varg);
1842 if (!ptype) {
1843 btype = type;
1844 type = &tpool[npool++];
1845 ptype = type;
1847 ptype->flags = T_FUNC;
1848 ptype->bt = LONGSZ;
1849 ptype->id = func_create(btype, name, argnames, args, nargs, varg);
1850 if (tok_see() != ';')
1851 while (tok_see() != '{' && !readdefs(krdef, &funcs[ptype->id]))
1852 tok_expect(';');
1853 } else {
1854 if (ptype && readarrays(type))
1855 array2ptr(type);
1857 memcpy(main, type, sizeof(*type));
1858 return 0;
1861 static int readtype(struct type *type)
1863 return readname(type, NULL, NULL);
1867 * readdef() reads a variable definitions statement. The definition
1868 * statement can appear in anywhere: global variables, function
1869 * local variables, struct fields, and typedefs. For each defined
1870 * variable, def() callback is called with the appropriate name
1871 * struct and flags; the callback should finish parsing the definition
1872 * by possibly reading the initializer expression and saving the name
1873 * struct.
1875 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
1876 void *data)
1878 struct type base;
1879 unsigned base_flags;
1880 if (basetype(&base, &base_flags))
1881 return 1;
1882 if (tok_see() == ';' || tok_see() == '{')
1883 return 0;
1884 do {
1885 struct name name = {{""}};
1886 if (readname(&name.type, name.name, &base))
1887 break;
1888 def(data, &name, base_flags);
1889 } while (!tok_jmp(','));
1890 return 0;
1893 /* just like readdefs, but default to int type; for handling K&R functions */
1894 static int readdefs_int(void (*def)(void *data, struct name *name, unsigned flags),
1895 void *data)
1897 struct type base;
1898 unsigned flags = 0;
1899 if (basetype(&base, &flags)) {
1900 if (tok_see() != TOK_NAME)
1901 return 1;
1902 memset(&base, 0, sizeof(base));
1903 base.bt = 4 | BT_SIGNED;
1905 if (tok_see() != ';') {
1906 do {
1907 struct name name = {{""}};
1908 if (readname(&name.type, name.name, &base))
1909 break;
1910 def(data, &name, flags);
1911 } while (!tok_jmp(','));
1913 return 0;
1917 /* parsing initializer expressions */
1919 static void jumpbrace(void)
1921 int depth = 0;
1922 while (tok_see() != '}' || depth--)
1923 if (tok_get() == '{')
1924 depth++;
1925 tok_expect('}');
1928 /* compute the size of the initializer expression */
1929 static int initsize(void)
1931 long addr = tok_addr();
1932 int n = 0;
1933 if (tok_jmp('='))
1934 return 0;
1935 if (!tok_jmp(TOK_STR)) {
1936 n = tok_str(NULL);
1937 tok_jump(addr);
1938 return n;
1940 tok_expect('{');
1941 while (tok_jmp('}')) {
1942 long idx = n;
1943 if (!tok_jmp('[')) {
1944 readexpr();
1945 ts_pop_de(NULL);
1946 o_popnum(&idx);
1947 tok_expect(']');
1948 tok_expect('=');
1950 if (n < idx + 1)
1951 n = idx + 1;
1952 while (tok_see() != '}' && tok_see() != ',')
1953 if (tok_get() == '{')
1954 jumpbrace();
1955 tok_jmp(',');
1957 tok_jump(addr);
1958 return n;
1961 static struct type *innertype(struct type *t)
1963 if (t->flags & T_ARRAY && !t->ptr)
1964 return innertype(&arrays[t->id].type);
1965 return t;
1968 /* read the initializer expression and initialize basic types using set() cb */
1969 static void initexpr(struct type *t, int off, void *obj,
1970 void (*set)(void *obj, int off, struct type *t))
1972 if (tok_jmp('{')) {
1973 set(obj, off, t);
1974 return;
1976 if (!t->ptr && t->flags & T_STRUCT) {
1977 struct structinfo *si = &structs[t->id];
1978 int i;
1979 for (i = 0; i < si->nfields && tok_see() != '}'; i++) {
1980 struct name *field = &si->fields[i];
1981 if (!tok_jmp('.')) {
1982 tok_expect(TOK_NAME);
1983 field = struct_field(t->id, tok_id());
1984 tok_expect('=');
1986 initexpr(&field->type, off + field->addr, obj, set);
1987 if (tok_jmp(','))
1988 break;
1990 } else if (t->flags & T_ARRAY) {
1991 struct type *t_de = &arrays[t->id].type;
1992 int i;
1993 /* handling extra braces as in: char s[] = {"sth"} */
1994 if (TYPE_SZ(t_de) == 1 && tok_see() == TOK_STR) {
1995 set(obj, off, t);
1996 tok_expect('}');
1997 return;
1999 for (i = 0; tok_see() != '}'; i++) {
2000 long idx = i;
2001 struct type *it = t_de;
2002 if (!tok_jmp('[')) {
2003 readexpr();
2004 ts_pop_de(NULL);
2005 o_popnum(&idx);
2006 tok_expect(']');
2007 tok_expect('=');
2009 if (tok_see() != '{' && (tok_see() != TOK_STR ||
2010 !(it->flags & T_ARRAY)))
2011 it = innertype(t_de);
2012 initexpr(it, off + type_totsz(it) * idx, obj, set);
2013 if (tok_jmp(','))
2014 break;
2017 tok_expect('}');