gen: don't save variable registers before calls
[neatcc/cc.git] / ncc.c
blobb23e1aeac45e47b138a4601cca1685917bdf3023
1 /*
2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2011 Ali Gholami Rudi
6 * This file 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 #define MAXLOCALS (1 << 10)
20 #define MAXGLOBALS (1 << 10)
21 #define MAXARGS (1 << 5)
23 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
24 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
26 #define T_ARRAY 0x01
27 #define T_STRUCT 0x02
28 #define T_FUNC 0x04
30 #define F_INIT 0x01
31 #define F_STATIC 0x02
32 #define F_EXTERN 0x04
34 struct type {
35 unsigned bt;
36 unsigned flags;
37 int ptr;
38 int id; /* for structs, functions and arrays */
39 int addr; /* the address is passed to gen.c; deref for value */
42 /* type stack */
43 static struct type ts[MAXTMP];
44 static int nts;
46 static void ts_push_bt(unsigned bt)
48 ts[nts].ptr = 0;
49 ts[nts].flags = 0;
50 ts[nts].addr = 0;
51 ts[nts++].bt = bt;
54 static void ts_push(struct type *t)
56 struct type *d = &ts[nts++];
57 memcpy(d, t, sizeof(*t));
60 static void ts_push_addr(struct type *t)
62 ts_push(t);
63 ts[nts - 1].addr = 1;
66 static void ts_pop(struct type *type)
68 nts--;
69 if (type)
70 *type = ts[nts];
73 void err(char *msg)
75 char err[1 << 7];
76 int len = cpp_loc(err, tok_addr());
77 strcpy(err + len, msg);
78 die(err);
81 struct name {
82 char name[NAMELEN];
83 char elfname[NAMELEN]; /* local elf name for static variables in function */
84 struct type type;
85 long addr; /* local stack offset, global data addr, struct offset */
88 static struct name locals[MAXLOCALS];
89 static int nlocals;
90 static struct name globals[MAXGLOBALS];
91 static int nglobals;
93 static void local_add(struct name *name)
95 if (nlocals >= MAXLOCALS)
96 err("nomem: MAXLOCALS reached!\n");
97 memcpy(&locals[nlocals++], name, sizeof(*name));
100 static int global_find(char *name)
102 int i;
103 for (i = 0; i < nglobals; i++)
104 if (!strcmp(name, globals[i].name))
105 return i;
106 return -1;
109 static void global_add(struct name *name)
111 int found = global_find(name->name);
112 int i = found == -1 ? nglobals++ : found;
113 if (nglobals >= MAXGLOBALS)
114 err("nomem: MAXGLOBALS reached!\n");
115 memcpy(&globals[i], name, sizeof(*name));
118 #define MAXENUMS (1 << 10)
120 static struct enumval {
121 char name[NAMELEN];
122 int n;
123 } enums[MAXENUMS];
124 static int nenums;
126 static void enum_add(char *name, int val)
128 struct enumval *ev = &enums[nenums++];
129 if (nenums >= MAXENUMS)
130 err("nomem: MAXENUMS reached!\n");
131 strcpy(ev->name, name);
132 ev->n = val;
135 static int enum_find(int *val, char *name)
137 int i;
138 for (i = nenums - 1; i >= 0; --i)
139 if (!strcmp(name, enums[i].name)) {
140 *val = enums[i].n;
141 return 0;
143 return 1;
146 #define MAXTYPEDEFS (1 << 10)
148 static struct typdefinfo {
149 char name[NAMELEN];
150 struct type type;
151 } typedefs[MAXTYPEDEFS];
152 static int ntypedefs;
154 static void typedef_add(char *name, struct type *type)
156 struct typdefinfo *ti = &typedefs[ntypedefs++];
157 if (ntypedefs >= MAXTYPEDEFS)
158 err("nomem: MAXTYPEDEFS reached!\n");
159 strcpy(ti->name, name);
160 memcpy(&ti->type, type, sizeof(*type));
163 static int typedef_find(char *name)
165 int i;
166 for (i = ntypedefs - 1; i >= 0; --i)
167 if (!strcmp(name, typedefs[i].name))
168 return i;
169 return -1;
172 #define MAXARRAYS (1 << 10)
174 static struct array {
175 struct type type;
176 int n;
177 } arrays[MAXARRAYS];
178 static int narrays;
180 static int array_add(struct type *type, int n)
182 struct array *a = &arrays[narrays++];
183 if (narrays >= MAXARRAYS)
184 err("nomem: MAXARRAYS reached!\n");
185 memcpy(&a->type, type, sizeof(*type));
186 a->n = n;
187 return a - arrays;
190 static void array2ptr(struct type *t)
192 if (t->flags & T_ARRAY && !t->ptr) {
193 memcpy(t, &arrays[t->id].type, sizeof(*t));
194 t->ptr++;
198 #define MAXSTRUCTS (1 << 10)
199 #define MAXFIELDS (1 << 7)
201 static struct structinfo {
202 char name[NAMELEN];
203 struct name fields[MAXFIELDS];
204 int nfields;
205 int isunion;
206 int size;
207 } structs[MAXSTRUCTS];
208 static int nstructs;
210 static int struct_find(char *name, int isunion)
212 int i;
213 for (i = nstructs - 1; i >= 0; --i)
214 if (*structs[i].name && !strcmp(name, structs[i].name) &&
215 structs[i].isunion == isunion)
216 return i;
217 i = nstructs++;
218 if (nstructs >= MAXSTRUCTS)
219 err("nomem: MAXTYPES reached!\n");
220 memset(&structs[i], 0, sizeof(structs[i]));
221 strcpy(structs[i].name, name);
222 structs[i].isunion = isunion;
223 return i;
226 static struct name *struct_field(int id, char *name)
228 struct structinfo *si = &structs[id];
229 int i;
230 for (i = 0; i < si->nfields; i++)
231 if (!strcmp(name, si->fields[i].name))
232 return &si->fields[i];
233 err("field not found\n");
236 #define MAXBREAK (1 << 7)
238 static long breaks[MAXBREAK];
239 static int nbreaks;
240 static long continues[MAXBREAK];
241 static int ncontinues;
243 static void break_fill(long addr, int till)
245 int i;
246 for (i = till; i < nbreaks; i++)
247 o_filljmp2(breaks[i], addr);
248 nbreaks = till;
251 static void continue_fill(long addr, int till)
253 int i;
254 for (i = till; i < ncontinues; i++)
255 o_filljmp2(continues[i], addr);
256 ncontinues = till;
259 static int type_totsz(struct type *t)
261 if (t->ptr)
262 return LONGSZ;
263 if (t->flags & T_ARRAY)
264 return arrays[t->id].n * type_totsz(&arrays[t->id].type);
265 return t->flags & T_STRUCT ? structs[t->id].size : BT_SZ(t->bt);
268 static unsigned type_szde(struct type *t)
270 struct type de = *t;
271 array2ptr(&de);
272 de.ptr--;
273 return type_totsz(&de);
276 static void ts_de(int deref)
278 struct type *t = &ts[nts - 1];
279 if (deref && t->addr && (!(t->flags & T_ARRAY) || (t->ptr)))
280 o_deref(TYPE_BT(t));
281 t->addr = 0;
284 static void ts_pop_de(struct type *t)
286 struct type de;
287 if (!t)
288 t = &de;
289 ts_pop(t);
290 array2ptr(t);
291 if (t->addr && !(t->flags & T_FUNC))
292 o_deref(TYPE_BT(t));
293 t->addr = 0;
296 static void ts_pop_de2(struct type *t1, struct type *t2)
298 ts_pop_de(t1);
299 o_tmpswap();
300 ts_pop_de(t2);
301 o_tmpswap();
304 static int tok_jmp(int tok)
306 if (tok_see() != tok)
307 return 1;
308 tok_get();
309 return 0;
312 static void tok_expect(int tok)
314 if (tok_get() != tok)
315 err("syntax error\n");
318 static unsigned bt_op(unsigned bt1, unsigned bt2)
320 unsigned s1 = BT_SZ(bt1);
321 unsigned s2 = BT_SZ(bt2);
322 return (bt1 | bt2) & BT_SIGNED | (s1 > s2 ? s1 : s2);
325 static void ts_binop(int op)
327 struct type t1, t2;
328 int bt;
329 ts_pop_de2(&t1, &t2);
330 if (op == O_DIV || op == O_MOD)
331 bt = TYPE_BT(&t2);
332 else
333 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
334 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
335 ts_push_bt(bt);
338 static void ts_addop(int op)
340 struct type t1, t2;
341 ts_pop_de2(&t1, &t2);
342 if (!t1.ptr && !t2.ptr) {
343 o_bop(op);
344 ts_push_bt(bt_op(TYPE_BT(&t1), TYPE_BT(&t2)));
345 return;
347 if (t1.ptr && !t2.ptr)
348 o_tmpswap();
349 if (!t1.ptr && t2.ptr)
350 if (type_szde(&t2) > 1) {
351 o_num(type_szde(&t2));
352 o_bop(O_MUL);
354 if (t1.ptr && !t2.ptr)
355 o_tmpswap();
356 o_bop(op);
357 if (t1.ptr && t2.ptr) {
358 int sz = type_szde(&t1);
359 if (sz > 1) {
360 o_num(sz);
361 o_bop(O_DIV);
363 ts_push_bt(4 | BT_SIGNED);
364 } else {
365 ts_push(t1.ptr ? &t1 : &t2);
369 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
370 #define MIN(a, b) ((a) < (b) ? (a) : (b))
372 static int type_alignment(struct type *t)
374 if (t->flags & T_ARRAY && !t->ptr)
375 return type_alignment(&arrays[t->id].type);
376 if (t->flags & T_STRUCT && !t->ptr)
377 return type_alignment(&structs[t->id].fields[0].type);
378 return MIN(LONGSZ, type_totsz(t));
381 static void structdef(void *data, struct name *name, unsigned flags)
383 struct structinfo *si = data;
384 if (si->isunion) {
385 name->addr = 0;
386 if (si->size < type_totsz(&name->type))
387 si->size = type_totsz(&name->type);
388 } else {
389 struct type *t = &name->type;
390 int alignment = type_alignment(t);
391 if (t->flags & T_ARRAY && !t->ptr)
392 alignment = MIN(LONGSZ, type_totsz(&arrays[t->id].type));
393 si->size = ALIGN(si->size, alignment);
394 name->addr = si->size;
395 si->size += type_totsz(&name->type);
397 memcpy(&si->fields[si->nfields++], name, sizeof(*name));
400 static int readdefs(void (*def)(void *, struct name *, unsigned f), void *data);
402 static int struct_create(char *name, int isunion)
404 int id = struct_find(name, isunion);
405 struct structinfo *si = &structs[id];
406 tok_expect('{');
407 while (tok_jmp('}')) {
408 readdefs(structdef, si);
409 tok_expect(';');
411 return id;
414 static void readexpr(void);
416 static void enum_create(void)
418 long n = 0;
419 tok_expect('{');
420 while (tok_jmp('}')) {
421 char name[NAMELEN];
422 tok_expect(TOK_NAME);
423 strcpy(name, tok_id());
424 if (tok_see() == '=') {
425 tok_get();
426 readexpr();
427 ts_pop(NULL);
428 if (o_popnum(&n))
429 err("const expr expected!\n");
431 enum_add(name, n++);
432 tok_jmp(',');
436 static int basetype(struct type *type, unsigned *flags)
438 int sign = 1;
439 int size = 4;
440 int done = 0;
441 int i = 0;
442 int isunion;
443 char name[NAMELEN] = "";
444 *flags = 0;
445 type->flags = 0;
446 type->ptr = 0;
447 type->addr = 0;
448 while (!done) {
449 switch (tok_see()) {
450 case TOK_STATIC:
451 *flags |= F_STATIC;
452 break;
453 case TOK_EXTERN:
454 *flags |= F_EXTERN;
455 break;
456 case TOK_VOID:
457 sign = 0;
458 size = 0;
459 done = 1;
460 break;
461 case TOK_INT:
462 done = 1;
463 break;
464 case TOK_CHAR:
465 size = 1;
466 done = 1;
467 break;
468 case TOK_SHORT:
469 size = 2;
470 break;
471 case TOK_LONG:
472 size = LONGSZ;
473 break;
474 case TOK_SIGNED:
475 break;
476 case TOK_UNSIGNED:
477 sign = 0;
478 break;
479 case TOK_UNION:
480 case TOK_STRUCT:
481 isunion = tok_get() == TOK_UNION;
482 if (!tok_jmp(TOK_NAME))
483 strcpy(name, tok_id());
484 if (tok_see() == '{')
485 type->id = struct_create(name, isunion);
486 else
487 type->id = struct_find(name, isunion);
488 type->flags |= T_STRUCT;
489 type->bt = LONGSZ;
490 return 0;
491 case TOK_ENUM:
492 tok_get();
493 tok_jmp(TOK_NAME);
494 if (tok_see() == '{')
495 enum_create();
496 type->bt = 4 | BT_SIGNED;
497 return 0;
498 default:
499 if (tok_see() == TOK_NAME) {
500 int id = typedef_find(tok_id());
501 if (id != -1) {
502 tok_get();
503 memcpy(type, &typedefs[id].type,
504 sizeof(*type));
505 return 0;
508 if (!i)
509 return 1;
510 done = 1;
511 continue;
513 i++;
514 tok_get();
516 type->bt = size | (sign ? BT_SIGNED : 0);
517 return 0;
520 static int readname(struct type *main, char *name,
521 struct type *base, unsigned flags);
523 static int readtype(struct type *type)
525 return readname(type, NULL, NULL, 0);
528 static void readptrs(struct type *type)
530 while (!tok_jmp('*')) {
531 type->ptr++;
532 if (!type->bt)
533 type->bt = 1;
537 /* used to differenciate labels from case and cond exprs */
538 static int ncexpr;
539 static int caseexpr;
541 static void readpre(void);
543 static char *tmp_str(char *buf, int len)
545 static char name[NAMELEN];
546 static int id;
547 void *dat;
548 sprintf(name, "__neatcc.s%d", id++);
549 dat = dat_dat(name, len, 0);
550 memcpy(dat, buf, len);
551 return name;
554 static void readprimary(void)
556 int i;
557 if (!tok_jmp(TOK_NUM)) {
558 long n;
559 int bt = tok_num(&n);
560 ts_push_bt(bt);
561 o_num(n);
562 return;
564 if (!tok_jmp(TOK_STR)) {
565 struct type t;
566 char buf[BUFSIZE];
567 int len;
568 t.bt = 1 | BT_SIGNED;
569 t.ptr = 1;
570 t.addr = 0;
571 t.flags = 0;
572 ts_push(&t);
573 len = tok_str(buf);
574 o_sym(tmp_str(buf, len));
575 return;
577 if (!tok_jmp(TOK_NAME)) {
578 struct name unkn = {""};
579 char *name = unkn.name;
580 int n;
581 strcpy(name, tok_id());
582 /* don't search for labels here */
583 if (!ncexpr && !caseexpr && tok_see() == ':')
584 return;
585 for (i = nlocals - 1; i >= 0; --i) {
586 struct type *t = &locals[i].type;
587 if (!strcmp(locals[i].name, name)) {
588 o_local(locals[i].addr);
589 ts_push_addr(t);
590 return;
593 if ((n = global_find(name)) != -1) {
594 struct name *g = &globals[n];
595 struct type *t = &g->type;
596 char *elfname = *g->elfname ? g->elfname : g->name;
597 o_sym(elfname);
598 ts_push_addr(t);
599 return;
601 if (!enum_find(&n, name)) {
602 ts_push_bt(4 | BT_SIGNED);
603 o_num(n);
604 return;
606 if (tok_see() != '(')
607 err("unknown symbol\n");
608 global_add(&unkn);
609 ts_push_bt(LONGSZ);
610 o_sym(unkn.name);
611 return;
613 if (!tok_jmp('(')) {
614 struct type t;
615 if (!readtype(&t)) {
616 struct type o;
617 tok_expect(')');
618 readpre();
619 ts_pop_de(&o);
620 ts_push(&t);
621 if (!t.ptr || !o.ptr)
622 o_cast(TYPE_BT(&t));
623 } else {
624 readexpr();
625 tok_expect(')');
627 return;
631 static void arrayderef(void)
633 struct type t;
634 int sz;
635 ts_pop_de(NULL);
636 ts_pop(&t);
637 if (!(t.flags & T_ARRAY) && t.addr) {
638 o_tmpswap();
639 o_deref(TYPE_BT(&t));
640 o_tmpswap();
642 array2ptr(&t);
643 t.ptr--;
644 sz = type_totsz(&t);
645 t.addr = 1;
646 if (sz > 1) {
647 o_num(sz);
648 o_bop(O_MUL);
650 o_bop(O_ADD);
651 ts_push(&t);
654 static void inc_post(int op)
656 struct type t = ts[nts - 1];
657 /* pushing the value before inc */
658 o_tmpcopy();
659 ts_de(1);
660 o_load();
661 o_tmpswap();
663 /* increment by 1 or pointer size */
664 o_tmpcopy();
665 ts_push(&t);
666 ts_pop_de(&t);
667 o_num(t.ptr > 0 ? type_szde(&t) : 1);
668 o_bop(op);
670 /* assign back */
671 o_assign(TYPE_BT(&t));
672 o_tmpdrop(1);
675 static void readfield(void)
677 struct name *field;
678 struct type t;
679 tok_expect(TOK_NAME);
680 ts_pop(&t);
681 array2ptr(&t);
682 field = struct_field(t.id, tok_id());
683 if (field->addr) {
684 o_num(field->addr);
685 o_bop(O_ADD);
687 ts_push_addr(&field->type);
690 #define MAXFUNCS (1 << 10)
692 static struct funcinfo {
693 struct type args[MAXFIELDS];
694 struct type ret;
695 int nargs;
696 } funcs[MAXFUNCS];
697 static int nfuncs;
699 static int func_create(struct type *ret, struct name *args, int nargs)
701 struct funcinfo *fi = &funcs[nfuncs++];
702 int i;
703 if (nfuncs >= MAXFUNCS)
704 err("nomem: MAXFUNCS reached!\n");
705 memcpy(&fi->ret, ret, sizeof(*ret));
706 for (i = 0; i < nargs; i++)
707 memcpy(&fi->args[i], &args[i].type, sizeof(*ret));
708 fi->nargs = nargs;
709 return fi - funcs;
712 static void readcall(void)
714 struct type t;
715 struct funcinfo *fi;
716 int argc = 0;
717 ts_pop(&t);
718 if (t.flags & T_FUNC && t.ptr > 0)
719 o_deref(LONGSZ);
720 fi = t.flags & T_FUNC ? &funcs[t.id] : NULL;
721 if (tok_see() != ')') {
722 do {
723 readexpr();
724 ts_pop_de(NULL);
725 argc++;
726 } while (!tok_jmp(','));
728 tok_expect(')');
729 o_call(argc, fi ? TYPE_BT(&fi->ret) : 4 | BT_SIGNED);
730 if (fi) {
731 if (TYPE_BT(&fi->ret))
732 o_cast(TYPE_BT(&fi->ret));
733 ts_push(&fi->ret);
734 } else {
735 ts_push_bt(4 | BT_SIGNED);
739 static void readpost(void)
741 readprimary();
742 while (1) {
743 if (!tok_jmp('[')) {
744 readexpr();
745 tok_expect(']');
746 arrayderef();
747 continue;
749 if (!tok_jmp('(')) {
750 readcall();
751 continue;
753 if (!tok_jmp(TOK2("++"))) {
754 inc_post(O_ADD);
755 continue;
757 if (!tok_jmp(TOK2("--"))) {
758 inc_post(O_SUB);
759 continue;
761 if (!tok_jmp('.')) {
762 readfield();
763 continue;
765 if (!tok_jmp(TOK2("->"))) {
766 ts_de(1);
767 readfield();
768 continue;
770 break;
774 static void inc_pre(int op)
776 struct type t;
777 readpre();
778 /* copy the destination */
779 o_tmpcopy();
780 ts_push(&ts[nts - 1]);
781 /* increment by 1 or pointer size */
782 ts_pop_de(&t);
783 o_num(t.ptr > 0 ? type_szde(&t) : 1);
784 o_bop(op);
785 /* assign the result */
786 o_assign(TYPE_BT(&t));
787 ts_de(0);
790 static void readpre(void)
792 if (!tok_jmp('&')) {
793 struct type type;
794 readpre();
795 ts_pop(&type);
796 if (!type.addr)
797 die("cannot use the address\n");
798 type.ptr++;
799 type.addr = 0;
800 ts_push(&type);
801 return;
803 if (!tok_jmp('*')) {
804 struct type t;
805 readpre();
806 ts_pop(&t);
807 array2ptr(&t);
808 if (!t.ptr)
809 err("dereferencing non-pointer\n");
810 if (t.addr)
811 o_deref(TYPE_BT(&t));
812 t.ptr--;
813 t.addr = 1;
814 ts_push(&t);
815 return;
817 if (!tok_jmp('!')) {
818 readpre();
819 ts_pop_de(NULL);
820 o_uop(O_LNOT);
821 ts_push_bt(4 | BT_SIGNED);
822 return;
824 if (!tok_jmp('-')) {
825 readpre();
826 ts_de(1);
827 o_uop(O_NEG);
828 return;
830 if (!tok_jmp('~')) {
831 readpre();
832 ts_de(1);
833 o_uop(O_NOT);
834 return;
836 if (!tok_jmp(TOK2("++"))) {
837 inc_pre(O_ADD);
838 return;
840 if (!tok_jmp(TOK2("--"))) {
841 inc_pre(O_SUB);
842 return;
844 if (!tok_jmp(TOK_SIZEOF)) {
845 struct type t;
846 int op = !tok_jmp('(');
847 if (readtype(&t)) {
848 int nogen = !o_nogen();
849 readexpr();
850 if (nogen)
851 o_dogen();
852 ts_pop(&t);
853 o_tmpdrop(1);
855 ts_push_bt(4);
856 o_num(type_totsz(&t));
857 if (op)
858 tok_expect(')');
859 return;
861 readpost();
864 static void readmul(void)
866 readpre();
867 while (1) {
868 if (!tok_jmp('*')) {
869 readpre();
870 ts_binop(O_MUL);
871 continue;
873 if (!tok_jmp('/')) {
874 readpre();
875 ts_binop(O_DIV);
876 continue;
878 if (!tok_jmp('%')) {
879 readpre();
880 ts_binop(O_MOD);
881 continue;
883 break;
887 static void readadd(void)
889 readmul();
890 while (1) {
891 if (!tok_jmp('+')) {
892 readmul();
893 ts_addop(O_ADD);
894 continue;
896 if (!tok_jmp('-')) {
897 readmul();
898 ts_addop(O_SUB);
899 continue;
901 break;
905 static void shift(int op)
907 struct type t;
908 readadd();
909 ts_pop_de2(NULL, &t);
910 o_bop(op | (BT_SIGNED & TYPE_BT(&t) ? O_SIGNED : 0));
911 ts_push_bt(TYPE_BT(&t));
914 static void readshift(void)
916 readadd();
917 while (1) {
918 if (!tok_jmp(TOK2("<<"))) {
919 shift(O_SHL);
920 continue;
922 if (!tok_jmp(TOK2(">>"))) {
923 shift(O_SHR);
924 continue;
926 break;
930 static void cmp(int op)
932 struct type t1, t2;
933 int bt;
934 readshift();
935 ts_pop_de2(&t1, &t2);
936 bt = bt_op(TYPE_BT(&t1), TYPE_BT(&t2));
937 o_bop(op | (bt & BT_SIGNED ? O_SIGNED : 0));
938 ts_push_bt(4 | BT_SIGNED);
941 static void readcmp(void)
943 readshift();
944 while (1) {
945 if (!tok_jmp('<')) {
946 cmp(O_LT);
947 continue;
949 if (!tok_jmp('>')) {
950 cmp(O_GT);
951 continue;
953 if (!tok_jmp(TOK2("<="))) {
954 cmp(O_LE);
955 continue;
957 if (!tok_jmp(TOK2(">="))) {
958 cmp(O_GE);
959 continue;
961 break;
965 static void eq(int op)
967 readcmp();
968 ts_pop_de2(NULL, NULL);
969 o_bop(op);
970 ts_push_bt(4 | BT_SIGNED);
973 static void readeq(void)
975 readcmp();
976 while (1) {
977 if (!tok_jmp(TOK2("=="))) {
978 eq(O_EQ);
979 continue;
981 if (!tok_jmp(TOK2("!="))) {
982 eq(O_NEQ);
983 continue;
985 break;
989 static void readbitand(void)
991 readeq();
992 while (!tok_jmp('&')) {
993 readeq();
994 ts_binop(O_AND);
998 static void readxor(void)
1000 readbitand();
1001 while (!tok_jmp('^')) {
1002 readbitand();
1003 ts_binop(O_XOR);
1007 static void readbitor(void)
1009 readxor();
1010 while (!tok_jmp('|')) {
1011 readxor();
1012 ts_binop(O_OR);
1016 #define MAXCOND (1 << 7)
1018 static void readand(void)
1020 long conds[MAXCOND];
1021 int nconds = 0;
1022 long passed;
1023 int i;
1024 readbitor();
1025 if (tok_see() != TOK2("&&"))
1026 return;
1027 o_fork();
1028 ts_pop_de(NULL);
1029 conds[nconds++] = o_jz(0);
1030 while (!tok_jmp(TOK2("&&"))) {
1031 readbitor();
1032 ts_pop_de(NULL);
1033 conds[nconds++] = o_jz(0);
1035 o_num(1);
1036 o_forkpush();
1037 passed = o_jmp(0);
1038 for (i = 0; i < nconds; i++)
1039 o_filljmp(conds[i]);
1040 o_num(0);
1041 o_forkpush();
1042 o_forkjoin();
1043 o_filljmp(passed);
1044 ts_push_bt(4 | BT_SIGNED);
1047 static void reador(void)
1049 long conds[MAXCOND];
1050 int nconds = 0;
1051 long failed;
1052 int i;
1053 readand();
1054 if (tok_see() != TOK2("||"))
1055 return;
1056 o_fork();
1057 ts_pop_de(NULL);
1058 conds[nconds++] = o_jnz(0);
1059 while (!tok_jmp(TOK2("||"))) {
1060 readand();
1061 ts_pop_de(NULL);
1062 conds[nconds++] = o_jnz(0);
1064 o_num(0);
1065 o_forkpush();
1066 failed = o_jmp(0);
1067 for (i = 0; i < nconds; i++)
1068 o_filljmp(conds[i]);
1069 o_num(1);
1070 o_forkpush();
1071 o_forkjoin();
1072 o_filljmp(failed);
1073 ts_push_bt(4 | BT_SIGNED);
1076 static int readcexpr_const(void)
1078 long c;
1079 int nogen;
1080 if (o_popnum(&c))
1081 return -1;
1082 if (!c)
1083 nogen = !o_nogen();
1084 reador();
1085 ts_pop(NULL);
1086 tok_expect(':');
1087 if (c) {
1088 nogen = !o_nogen();
1089 } else {
1090 if (nogen)
1091 o_dogen();
1092 o_tmpdrop(1);
1094 reador();
1095 if (c) {
1096 if (nogen)
1097 o_dogen();
1098 o_tmpdrop(1);
1100 return 0;
1103 static void readcexpr(void)
1105 long l1, l2;
1106 reador();
1107 if (tok_jmp('?'))
1108 return;
1109 ncexpr++;
1110 ts_pop_de(NULL);
1111 o_fork();
1112 if (readcexpr_const()) {
1113 l1 = o_jz(0);
1114 reador();
1115 ts_de(1);
1116 o_forkpush();
1117 l2 = o_jmp(0);
1118 ts_pop(NULL);
1120 tok_expect(':');
1121 o_filljmp(l1);
1122 reador();
1123 ts_de(1);
1124 o_forkpush();
1125 o_forkjoin();
1126 o_filljmp(l2);
1128 ncexpr--;
1131 static void opassign(int op, int ptrop)
1133 struct type t = ts[nts - 1];
1134 o_tmpcopy();
1135 ts_push(&t);
1136 readexpr();
1137 ts_addop(op);
1138 o_assign(TYPE_BT(&ts[nts - 1]));
1139 ts_pop(NULL);
1140 ts_de(0);
1143 static void doassign(void)
1145 struct type t = ts[nts - 1];
1146 if (!t.ptr && t.flags & T_STRUCT) {
1147 ts_pop(NULL);
1148 o_num(type_totsz(&t));
1149 o_memcpy();
1150 } else {
1151 ts_pop_de(NULL);
1152 o_assign(TYPE_BT(&ts[nts - 1]));
1153 ts_de(0);
1157 static void readexpr(void)
1159 readcexpr();
1160 if (!tok_jmp('=')) {
1161 readexpr();
1162 doassign();
1163 return;
1165 if (!tok_jmp(TOK2("+="))) {
1166 opassign(O_ADD, 1);
1167 return;
1169 if (!tok_jmp(TOK2("-="))) {
1170 opassign(O_SUB, 1);
1171 return;
1173 if (!tok_jmp(TOK2("*="))) {
1174 opassign(O_MUL, 0);
1175 return;
1177 if (!tok_jmp(TOK2("/="))) {
1178 opassign(O_DIV, 0);
1179 return;
1181 if (!tok_jmp(TOK2("%="))) {
1182 opassign(O_MOD, 0);
1183 return;
1185 if (!tok_jmp(TOK3("<<="))) {
1186 opassign(O_SHL, 0);
1187 return;
1189 if (!tok_jmp(TOK3(">>="))) {
1190 opassign(O_SHR, 0);
1191 return;
1193 if (!tok_jmp(TOK3("&="))) {
1194 opassign(O_AND, 0);
1195 return;
1197 if (!tok_jmp(TOK3("|="))) {
1198 opassign(O_OR, 0);
1199 return;
1201 if (!tok_jmp(TOK3("^="))) {
1202 opassign(O_XOR, 0);
1203 return;
1207 static void readestmt(void)
1209 do {
1210 o_tmpdrop(-1);
1211 nts = 0;
1212 readexpr();
1213 } while (!tok_jmp(','));
1216 static void o_localoff(long addr, int off)
1218 o_local(addr);
1219 if (off) {
1220 o_num(off);
1221 o_bop(O_ADD);
1225 static struct type *innertype(struct type *t)
1227 if (t->flags & T_ARRAY && !t->ptr)
1228 return innertype(&arrays[t->id].type);
1229 return t;
1232 static void initexpr(struct type *t, int off, void *obj,
1233 void (*set)(void *obj, int off, struct type *t))
1235 if (tok_jmp('{')) {
1236 set(obj, off, t);
1237 return;
1239 if (!t->ptr && t->flags & T_STRUCT) {
1240 struct structinfo *si = &structs[t->id];
1241 int i;
1242 for (i = 0; i < si->nfields; i++) {
1243 struct name *field = &si->fields[i];
1244 if (!tok_jmp('.')) {
1245 tok_expect(TOK_NAME);
1246 field = struct_field(t->id, tok_id());
1247 tok_expect('=');
1249 initexpr(&field->type, off + field->addr, obj, set);
1250 if (tok_jmp(',') || tok_see() == '}')
1251 break;
1253 } else if (t->flags & T_ARRAY) {
1254 struct type *t_de = &arrays[t->id].type;
1255 int i;
1256 for (i = 0; ; i++) {
1257 long idx = i;
1258 struct type *it = t_de;
1259 if (!tok_jmp('[')) {
1260 readexpr();
1261 o_popnum(&idx);
1262 ts_pop(NULL);
1263 tok_expect(']');
1264 tok_expect('=');
1266 if (tok_see() != '{')
1267 it = innertype(t_de);
1268 initexpr(it, off + type_totsz(it) * idx, obj, set);
1269 if (tok_jmp(',') || tok_see() == '}')
1270 break;
1273 tok_expect('}');
1276 static void jumpbrace(void)
1278 int depth = 0;
1279 while (tok_see() != '}' || depth--)
1280 if (tok_get() == '{')
1281 depth++;
1282 tok_expect('}');
1285 static int initsize(void)
1287 long addr = tok_addr();
1288 int n = 0;
1289 if (!tok_jmp(TOK_STR)) {
1290 n = tok_str(NULL);
1291 tok_jump(addr);
1292 return n;
1294 o_nogen();
1295 tok_expect('{');
1296 while (tok_jmp('}')) {
1297 long idx = n;
1298 if (!tok_jmp('[')) {
1299 readexpr();
1300 o_popnum(&idx);
1301 ts_pop(NULL);
1302 tok_expect(']');
1303 tok_expect('=');
1305 if (n < idx + 1)
1306 n = idx + 1;
1307 while (tok_see() != '}' && tok_see() != ',')
1308 if (tok_get() == '{')
1309 jumpbrace();
1310 tok_jmp(',');
1312 o_dogen();
1313 tok_jump(addr);
1314 return n;
1317 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1319 static void globalinit(void *obj, int off, struct type *t)
1321 struct name *name = obj;
1322 char *elfname = *name->elfname ? name->elfname : name->name;
1323 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1324 struct type *t_de = &arrays[t->id].type;
1325 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1326 char buf[BUFSIZE];
1327 int len;
1328 tok_expect(TOK_STR);
1329 len = tok_str(buf);
1330 memcpy((void *) name->addr + off, buf, len);
1331 return;
1334 readexpr();
1335 o_datset(elfname, off, TYPE_BT(t));
1336 ts_pop(NULL);
1339 static void globaldef(void *data, struct name *name, unsigned flags)
1341 struct type *t = &name->type;
1342 char *elfname = *name->elfname ? name->elfname : name->name;
1343 int sz;
1344 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1345 if (~flags & F_EXTERN)
1346 arrays[t->id].n = initsize();
1347 sz = type_totsz(t);
1348 if (!(flags & F_EXTERN) && (!(t->flags & T_FUNC) || t->ptr)) {
1349 if (flags & F_INIT)
1350 name->addr = (long) dat_dat(elfname, sz, F_GLOBAL(flags));
1351 else
1352 dat_bss(elfname, sz, F_GLOBAL(flags));
1354 global_add(name);
1355 if (flags & F_INIT)
1356 initexpr(t, 0, name, globalinit);
1359 static void localinit(void *obj, int off, struct type *t)
1361 long addr = *(long *) obj;
1362 if (t->flags & T_ARRAY && tok_see() == TOK_STR) {
1363 struct type *t_de = &arrays[t->id].type;
1364 if (!t_de->ptr && !t_de->flags && TYPE_SZ(t_de) == 1) {
1365 char buf[BUFSIZE];
1366 int len;
1367 tok_expect(TOK_STR);
1368 len = tok_str(buf);
1369 o_localoff(addr, off);
1370 o_sym(tmp_str(buf, len));
1371 o_num(len);
1372 o_memcpy();
1373 o_tmpdrop(1);
1374 return;
1377 o_localoff(addr, off);
1378 ts_push(t);
1379 readexpr();
1380 doassign();
1381 ts_pop(NULL);
1382 o_tmpdrop(1);
1385 /* current function name */
1386 static char func_name[NAMELEN];
1388 static void localdef(void *data, struct name *name, unsigned flags)
1390 struct type *t = &name->type;
1391 if (flags & (F_STATIC | F_EXTERN)) {
1392 sprintf(name->elfname, "__neatcc.%s.%s", func_name, name->name);
1393 globaldef(data, name, flags);
1394 return;
1396 if (t->flags & T_ARRAY && !t->ptr && !arrays[t->id].n)
1397 arrays[t->id].n = initsize();
1398 name->addr = o_mklocal(type_totsz(&name->type));
1399 local_add(name);
1400 if (flags & F_INIT) {
1401 if (t->flags & (T_ARRAY | T_STRUCT) && !t->ptr) {
1402 o_local(name->addr);
1403 o_num(0);
1404 o_num(type_totsz(t));
1405 o_memset();
1406 o_tmpdrop(1);
1408 initexpr(t, 0, &name->addr, localinit);
1412 static void funcdef(char *name, struct type *type, struct name *args,
1413 int nargs, unsigned flags)
1415 struct name global = {""};
1416 int i;
1417 strcpy(global.name, name);
1418 strcpy(func_name, name);
1419 memcpy(&global.type, type, sizeof(*type));
1420 o_func_beg(name, F_GLOBAL(flags));
1421 global_add(&global);
1422 for (i = 0; i < nargs; i++) {
1423 args[i].addr = o_arg(i);
1424 local_add(&args[i]);
1428 static int readargs(struct name *args)
1430 int nargs = 0;
1431 tok_expect('(');
1432 while (tok_see() != ')') {
1433 if (!tok_jmp(TOK3("...")))
1434 break;
1435 readname(&args[nargs].type, args[nargs].name, NULL, 0);
1436 array2ptr(&args[nargs].type);
1437 nargs++;
1438 if (tok_jmp(','))
1439 break;
1441 tok_expect(')');
1442 if (nargs == 1 && !TYPE_BT(&args[0].type))
1443 return 0;
1444 return nargs;
1447 static int readname(struct type *main, char *name,
1448 struct type *base, unsigned flags)
1450 struct type tpool[3];
1451 int npool = 0;
1452 struct type *type = &tpool[npool++];
1453 struct type *func = NULL;
1454 struct type *ret = NULL;
1455 int arsz[10];
1456 int nar = 0;
1457 int i;
1458 memset(tpool, 0, sizeof(tpool));
1459 if (name)
1460 *name = '\0';
1461 if (!base) {
1462 if (basetype(type, &flags))
1463 return 1;
1464 } else {
1465 memcpy(type, base, sizeof(*base));
1467 readptrs(type);
1468 if (!tok_jmp('(')) {
1469 ret = type;
1470 type = &tpool[npool++];
1471 func = type;
1472 readptrs(type);
1474 if (!tok_jmp(TOK_NAME) && name)
1475 strcpy(name, tok_id());
1476 while (!tok_jmp('[')) {
1477 long n = 0;
1478 if (tok_jmp(']')) {
1479 readexpr();
1480 ts_pop(NULL);
1481 if (o_popnum(&n))
1482 err("const expr expected\n");
1483 tok_expect(']');
1485 arsz[nar++] = n;
1487 for (i = nar - 1; i >= 0; i--) {
1488 type->id = array_add(type, arsz[i]);
1489 if (func && i == nar - 1)
1490 func = &arrays[type->id].type;
1491 type->flags = T_ARRAY;
1492 type->bt = LONGSZ;
1493 type->ptr = 0;
1495 if (func)
1496 tok_expect(')');
1497 if (tok_see() == '(') {
1498 struct name args[MAXARGS] = {{""}};
1499 int nargs = readargs(args);
1500 int fdef = !func;
1501 if (!func) {
1502 ret = type;
1503 type = &tpool[npool++];
1504 func = type;
1506 func->flags = T_FUNC;
1507 func->bt = LONGSZ;
1508 func->id = func_create(ret, args, nargs);
1509 if (fdef && tok_see() == '{') {
1510 funcdef(name, func, args, nargs, flags);
1511 return 1;
1514 memcpy(main, type, sizeof(*type));
1515 return 0;
1518 static int readdefs(void (*def)(void *data, struct name *name, unsigned flags),
1519 void *data)
1521 struct type base;
1522 unsigned base_flags;
1523 if (basetype(&base, &base_flags))
1524 return 1;
1525 while (tok_see() != ';' && tok_see() != '{') {
1526 struct name name = {{""}};
1527 unsigned flags = base_flags;
1528 if (readname(&name.type, name.name, &base, flags))
1529 break;
1530 if (!tok_jmp('='))
1531 flags |= F_INIT;
1532 def(data, &name, flags);
1533 tok_jmp(',');
1535 return 0;
1538 static void typedefdef(void *data, struct name *name, unsigned flags)
1540 typedef_add(name->name, &name->type);
1543 static void readstmt(void);
1545 #define MAXCASES (1 << 7)
1547 static void readswitch(void)
1549 int break_beg = nbreaks;
1550 long val_addr = o_mklocal(LONGSZ);
1551 long matched[MAXCASES];
1552 int nmatched = 0;
1553 struct type t;
1554 long next;
1555 int ref = 1;
1556 int i;
1557 tok_expect('(');
1558 readexpr();
1559 ts_pop_de(&t);
1560 o_local(val_addr);
1561 o_tmpswap();
1562 o_assign(TYPE_BT(&t));
1563 ts_de(0);
1564 o_tmpdrop(1);
1565 tok_expect(')');
1566 tok_expect('{');
1567 while (tok_jmp('}')) {
1568 int n = 0;
1569 while (tok_see() == TOK_CASE || tok_see() == TOK_DEFAULT) {
1570 if (n++ > 0)
1571 matched[nmatched++] = o_jmp(0);
1572 if (!ref++)
1573 o_filljmp(next);
1574 if (!tok_jmp(TOK_CASE)) {
1575 caseexpr = 1;
1576 readexpr();
1577 ts_pop_de(NULL);
1578 caseexpr = 0;
1579 o_local(val_addr);
1580 o_deref(TYPE_BT(&t));
1581 o_bop(O_EQ);
1582 next = o_jz(0);
1583 ref = 0;
1584 tok_expect(':');
1585 o_tmpdrop(1);
1586 continue;
1588 if (!tok_jmp(TOK_DEFAULT)) {
1589 tok_expect(':');
1590 continue;
1593 for (i = 0; i < nmatched; i++)
1594 o_filljmp(matched[i]);
1595 nmatched = 0;
1596 readstmt();
1598 o_rmlocal(val_addr, LONGSZ);
1599 if (!ref++)
1600 o_filljmp(next);
1601 break_fill(o_mklabel(), break_beg);
1604 #define MAXGOTO (1 << 10)
1606 static struct gotoinfo {
1607 char name[NAMELEN];
1608 long addr;
1609 } gotos[MAXGOTO];
1610 static int ngotos;
1612 static struct labelinfo {
1613 char name[NAMELEN];
1614 long addr;
1615 } labels[MAXGOTO];
1616 static int nlabels;
1618 static void goto_add(char *name)
1620 strcpy(gotos[ngotos].name, name);
1621 gotos[ngotos++].addr = o_jmp(0);
1624 static void label_add(char *name)
1626 strcpy(labels[nlabels].name, name);
1627 labels[nlabels++].addr = o_mklabel();
1630 static void goto_fill(void)
1632 int i, j;
1633 for (i = 0; i < ngotos; i++)
1634 for (j = 0; j < nlabels; j++)
1635 if (!strcmp(gotos[i].name, labels[j].name)) {
1636 o_filljmp2(gotos[i].addr, labels[j].addr);
1637 break;
1641 static void readstmt(void)
1643 o_tmpdrop(-1);
1644 nts = 0;
1645 if (!tok_jmp('{')) {
1646 int _nlocals = nlocals;
1647 int _nglobals = nglobals;
1648 int _nenums = nenums;
1649 int _ntypedefs = ntypedefs;
1650 int _nstructs = nstructs;
1651 int _nfuncs = nfuncs;
1652 int _narrays = narrays;
1653 while (tok_jmp('}'))
1654 readstmt();
1655 nlocals = _nlocals;
1656 nenums = _nenums;
1657 ntypedefs = _ntypedefs;
1658 nstructs = _nstructs;
1659 nfuncs = _nfuncs;
1660 narrays = _narrays;
1661 nglobals = _nglobals;
1662 return;
1664 if (!readdefs(localdef, NULL)) {
1665 tok_expect(';');
1666 return;
1668 if (!tok_jmp(TOK_TYPEDEF)) {
1669 readdefs(typedefdef, NULL);
1670 tok_expect(';');
1671 return;
1673 if (!tok_jmp(TOK_IF)) {
1674 long l1, l2;
1675 tok_expect('(');
1676 readexpr();
1677 tok_expect(')');
1678 ts_pop_de(NULL);
1679 l1 = o_jz(0);
1680 readstmt();
1681 if (!tok_jmp(TOK_ELSE)) {
1682 l2 = o_jmp(0);
1683 o_filljmp(l1);
1684 readstmt();
1685 o_filljmp(l2);
1686 } else {
1687 o_filljmp(l1);
1689 return;
1691 if (!tok_jmp(TOK_WHILE)) {
1692 long l1, l2;
1693 int break_beg = nbreaks;
1694 int continue_beg = ncontinues;
1695 l1 = o_mklabel();
1696 tok_expect('(');
1697 readexpr();
1698 tok_expect(')');
1699 ts_pop_de(NULL);
1700 l2 = o_jz(0);
1701 readstmt();
1702 o_jmp(l1);
1703 o_filljmp(l2);
1704 break_fill(o_mklabel(), break_beg);
1705 continue_fill(l1, continue_beg);
1706 return;
1708 if (!tok_jmp(TOK_DO)) {
1709 long l1, l2;
1710 int break_beg = nbreaks;
1711 int continue_beg = ncontinues;
1712 l1 = o_mklabel();
1713 readstmt();
1714 tok_expect(TOK_WHILE);
1715 tok_expect('(');
1716 l2 = o_mklabel();
1717 readexpr();
1718 ts_pop_de(NULL);
1719 o_jnz(l1);
1720 tok_expect(')');
1721 break_fill(o_mklabel(), break_beg);
1722 continue_fill(l2, continue_beg);
1723 return;
1725 if (!tok_jmp(TOK_FOR)) {
1726 long l_check, l_jump, j_fail, j_pass;
1727 int break_beg = nbreaks;
1728 int continue_beg = ncontinues;
1729 int has_cond = 0;
1730 tok_expect('(');
1731 if (tok_see() != ';')
1732 readestmt();
1733 tok_expect(';');
1734 l_check = o_mklabel();
1735 if (tok_see() != ';') {
1736 readestmt();
1737 ts_pop_de(NULL);
1738 j_fail = o_jz(0);
1739 has_cond = 1;
1741 tok_expect(';');
1742 j_pass = o_jmp(0);
1743 l_jump = o_mklabel();
1744 if (tok_see() != ')')
1745 readestmt();
1746 tok_expect(')');
1747 o_jmp(l_check);
1748 o_filljmp(j_pass);
1749 readstmt();
1750 o_jmp(l_jump);
1751 if (has_cond)
1752 o_filljmp(j_fail);
1753 break_fill(o_mklabel(), break_beg);
1754 continue_fill(l_jump, continue_beg);
1755 return;
1757 if (!tok_jmp(TOK_SWITCH)) {
1758 readswitch();
1759 return;
1761 if (!tok_jmp(TOK_RETURN)) {
1762 int ret = tok_see() != ';';
1763 if (ret) {
1764 readexpr();
1765 ts_pop_de(NULL);
1767 tok_expect(';');
1768 o_ret(ret);
1769 return;
1771 if (!tok_jmp(TOK_BREAK)) {
1772 tok_expect(';');
1773 breaks[nbreaks++] = o_jmp(0);
1774 return;
1776 if (!tok_jmp(TOK_CONTINUE)) {
1777 tok_expect(';');
1778 continues[ncontinues++] = o_jmp(0);
1779 return;
1781 if (!tok_jmp(TOK_GOTO)) {
1782 tok_expect(TOK_NAME);
1783 goto_add(tok_id());
1784 tok_expect(';');
1785 return;
1787 readestmt();
1788 /* labels */
1789 if (!tok_jmp(':')) {
1790 label_add(tok_id());
1791 return;
1793 tok_expect(';');
1796 static void readdecl(void)
1798 if (!tok_jmp(TOK_TYPEDEF)) {
1799 readdefs(typedefdef, NULL);
1800 tok_expect(';');
1801 return;
1803 readdefs(globaldef, NULL);
1804 if (tok_see() == '{') {
1805 readstmt();
1806 goto_fill();
1807 o_func_end();
1808 func_name[0] = '\0';
1809 nlocals = 0;
1810 ngotos = 0;
1811 nlabels = 0;
1812 return;
1814 tok_expect(';');
1817 static void parse(void)
1819 while (tok_see() != TOK_EOF)
1820 readdecl();
1823 int main(int argc, char *argv[])
1825 char obj[128];
1826 int ofd;
1827 int i = 1;
1828 while (i < argc && argv[i][0] == '-') {
1829 if (argv[i][1] == 'I')
1830 cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]);
1831 if (argv[i][1] == 'D') {
1832 char *name = argv[i] + 2;
1833 char *def = "";
1834 char *eq = strchr(name, '=');
1835 if (eq) {
1836 *eq = '\0';
1837 def = eq + 1;
1839 cpp_define(name, def);
1841 i++;
1843 if (i == argc)
1844 die("neatcc: no file given\n");
1845 if (cpp_init(argv[i]))
1846 die("neatcc: cannot open input file\n");
1847 parse();
1848 strcpy(obj, argv[i]);
1849 obj[strlen(obj) - 1] = 'o';
1850 ofd = open(obj, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1851 o_write(ofd);
1852 close(ofd);
1853 return 0;