gen: rename FORK_REG to REG_FORK
[neatcc/cc.git] / cpp.c
blob0cd88e236b3caee3850b0c3dc387ff20e740a345
1 #include <ctype.h>
2 #include <fcntl.h>
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include "tok.h"
11 #include "tab.h"
13 static char *buf;
14 static int len;
15 static int cur;
17 #define MAXDEFS (1 << 12)
18 #define MACROLEN (1 << 10)
19 #define MAXARGS (1 << 5)
20 #define NBUCKET (MAXDEFS << 1)
22 static struct macro {
23 char name[NAMELEN];
24 char def[MACROLEN];
25 char args[MAXARGS][NAMELEN];
26 int nargs;
27 int isfunc;
28 } macros[MAXDEFS];
29 static int nmacros;
30 /* macro hash table */
31 static struct tab mtab;
33 #define MAXBUFS (1 << 5)
34 #define BUF_FILE 0
35 #define BUF_MACRO 1
36 #define BUF_ARG 2
37 #define BUF_EVAL 3
38 #define BUF_TEMP 4
40 static struct buf {
41 char *buf;
42 int len;
43 int cur;
44 int type;
45 /* for BUF_FILE */
46 char path[NAMELEN];
47 /* for BUF_MACRO */
48 struct macro *macro;
49 char args[MAXARGS][MACROLEN]; /* arguments passed to a macro */
50 /* for BUF_ARG */
51 int arg_buf; /* the bufs index of the owning macro */
52 } bufs[MAXBUFS];
53 static int nbufs;
55 void die(char *msg)
57 write(2, msg, strlen(msg));
58 exit(1);
61 static void buf_new(int type, char *dat, int dlen)
63 if (nbufs) {
64 bufs[nbufs - 1].buf = buf;
65 bufs[nbufs - 1].cur = cur;
66 bufs[nbufs - 1].len = len;
68 if (nbufs >= MAXBUFS)
69 die("nomem: MAXBUFS reached!\n");
70 nbufs++;
71 cur = 0;
72 buf = dat;
73 len = dlen;
74 bufs[nbufs - 1].type = type;
77 static void buf_file(char *path, char *dat, int dlen)
79 buf_new(BUF_FILE, dat, dlen);
80 strcpy(bufs[nbufs - 1].path, path ? path : "");
83 static void buf_macro(struct macro *m)
85 buf_new(BUF_MACRO, m->def, strlen(m->def));
86 bufs[nbufs - 1].macro = m;
89 static void buf_arg(char *arg, int mbuf)
91 buf_new(BUF_ARG, arg, strlen(arg));
92 bufs[nbufs - 1].arg_buf = mbuf;
95 static void buf_pop(void)
97 nbufs--;
98 if (nbufs) {
99 cur = bufs[nbufs - 1].cur;
100 len = bufs[nbufs - 1].len;
101 buf = bufs[nbufs - 1].buf;
105 static int buf_iseval(void)
107 int i;
108 for (i = nbufs - 1; i >= 0; i--)
109 if (bufs[i].type == BUF_EVAL)
110 return 1;
111 return 0;
114 static size_t file_size(int fd)
116 struct stat st;
117 if (!fstat(fd, &st))
118 return st.st_size;
119 return 0;
122 static int include_file(char *path)
124 int fd = open(path, O_RDONLY);
125 int n = 0, nr = 0;
126 char *dat;
127 int size;
128 if (fd == -1)
129 return -1;
130 size = file_size(fd) + 1;
131 dat = malloc(size);
132 while ((n = read(fd, dat + nr, size - nr)) > 0)
133 nr += n;
134 close(fd);
135 dat[nr] = '\0';
136 buf_file(path, dat, nr);
137 return 0;
140 int cpp_init(char *path)
142 cpp_define("__STDC__", "");
143 cpp_define("__arm__", "");
144 cpp_define("__linux__", "");
145 return include_file(path);
148 static void jumpws(void)
150 while (cur < len && isspace(buf[cur]))
151 cur++;
154 static void read_word(char *dst)
156 jumpws();
157 while (cur < len && (isalnum(buf[cur]) || buf[cur] == '_'))
158 *dst++ = buf[cur++];
159 *dst = '\0';
162 static void jumpcomment(void)
164 while (++cur < len) {
165 if (buf[cur] == '*' && buf[cur + 1] == '/') {
166 cur += 2;
167 break;
172 static void read_tilleol(char *dst)
174 while (cur < len && isspace(buf[cur]) && buf[cur] != '\n')
175 cur++;
176 while (cur < len && buf[cur] != '\n') {
177 if (buf[cur] == '\\' && buf[cur + 1] == '\n')
178 cur += 2;
179 else if (buf[cur] == '/' && buf[cur + 1] == '*')
180 jumpcomment();
181 else
182 *dst++ = buf[cur++];
184 *dst = '\0';
187 static char *putstr(char *d, char *s)
189 while (*s)
190 *d++ = *s++;
191 *d = '\0';
192 return d;
195 #define MAXLOCS (1 << 10)
197 static char *locs[MAXLOCS] = {"/usr/include"};
198 static int nlocs = 1;
200 void cpp_addpath(char *s)
202 locs[nlocs++] = s;
205 static int include_find(char *name, int std)
207 int i;
208 for (i = std ? nlocs - 1 : nlocs; i >= 0; i--) {
209 char path[1 << 10];
210 char *s;
211 s = path;
212 if (locs[i]) {
213 s = putstr(s, locs[i]);
214 *s++ = '/';
216 s = putstr(s, name);
217 if (!include_file(path))
218 return 0;
220 return -1;
223 static void jumpstr(void)
225 if (buf[cur] == '\'') {
226 while (cur < len && buf[++cur] != '\'')
227 if (buf[cur] == '\\')
228 cur++;
229 cur++;
230 return;
232 if (buf[cur] == '"') {
233 while (cur < len && buf[++cur] != '"')
234 if (buf[cur] == '\\')
235 cur++;
236 cur++;
237 return;
241 static void readarg(char *s)
243 int depth = 0;
244 int beg = cur;
245 while (cur < len && (depth || buf[cur] != ',' && buf[cur] != ')')) {
246 switch (buf[cur]) {
247 case '(':
248 case '[':
249 case '{':
250 cur++;
251 depth++;
252 break;
253 case ')':
254 case ']':
255 case '}':
256 cur++;
257 depth--;
258 break;
259 case '\'':
260 case '"':
261 jumpstr();
262 break;
263 default:
264 if (buf[cur] == '/' && buf[cur + 1] == '*')
265 jumpcomment();
266 else
267 cur++;
270 if (s) {
271 memcpy(s, buf + beg, cur - beg);
272 s[cur - beg] = '\0';
276 static int macro_find(char *name)
278 char *n = tab_get(&mtab, name);
279 if (!n)
280 return -1;
281 return container(n, struct macro, name) - macros;
284 static void macro_undef(char *name)
286 int i = macro_find(name);
287 if (i >= 0)
288 tab_del(&mtab, macros[i].name);
291 static int macro_new(char *name)
293 int i = macro_find(name);
294 if (i >= 0)
295 return i;
296 if (nmacros >= MAXDEFS)
297 die("nomem: MAXDEFS reached!\n");
298 i = nmacros++;
299 strcpy(macros[i].name, name);
300 tab_add(&mtab, macros[i].name);
301 return i;
304 static void macro_define(void)
306 char name[NAMELEN];
307 struct macro *d;
308 read_word(name);
309 d = &macros[macro_new(name)];
310 d->isfunc = 0;
311 d->nargs = 0;
312 if (buf[cur] == '(') {
313 cur++;
314 jumpws();
315 while (cur < len && buf[cur] != ')') {
316 readarg(d->args[d->nargs++]);
317 jumpws();
318 if (buf[cur] != ',')
319 break;
320 cur++;
321 jumpws();
323 cur++;
324 d->isfunc = 1;
326 read_tilleol(d->def);
329 int cpp_read(char *buf);
331 static char ebuf[BUFSIZE];
332 static int elen;
333 static int ecur;
335 static long evalexpr(void);
337 static int cpp_eval(void)
339 int bufid;
340 int ret;
341 char evalbuf[BUFSIZE];
342 read_tilleol(evalbuf);
343 buf_new(BUF_EVAL, evalbuf, strlen(evalbuf));
344 bufid = nbufs;
345 elen = 0;
346 ecur = 0;
347 while (bufid < nbufs || (bufid == nbufs && cur < len))
348 elen += cpp_read(ebuf + elen);
349 ret = evalexpr();
350 buf_pop();
351 return ret;
354 static void jumpifs(int jumpelse)
356 int depth = 0;
357 while (cur < len) {
358 if (buf[cur] == '#') {
359 char cmd[NAMELEN];
360 cur++;
361 read_word(cmd);
362 if (!strcmp("else", cmd))
363 if (!depth && !jumpelse)
364 break;
365 if (!strcmp("elif", cmd))
366 if (!depth && !jumpelse && cpp_eval())
367 break;
368 if (!strcmp("endif", cmd)) {
369 if (!depth)
370 break;
371 else
372 depth--;
374 if (!strcmp("ifdef", cmd) || !strcmp("ifndef", cmd) ||
375 !strcmp("if", cmd))
376 depth++;
377 continue;
379 if (buf[cur] == '/' && buf[cur + 1] == '*') {
380 jumpcomment();
381 continue;
383 if (buf[cur] == '\'' || buf[cur] == '"') {
384 jumpstr();
385 continue;
387 cur++;
391 static int cpp_cmd(void)
393 char cmd[NAMELEN];
394 cur++;
395 read_word(cmd);
396 if (!strcmp("define", cmd)) {
397 macro_define();
398 return 0;
400 if (!strcmp("undef", cmd)) {
401 char name[NAMELEN];
402 read_word(name);
403 macro_undef(name);
404 return 0;
406 if (!strcmp("ifdef", cmd) || !strcmp("ifndef", cmd) ||
407 !strcmp("if", cmd)) {
408 char name[NAMELEN];
409 int matched = 0;
410 if (cmd[2]) {
411 int not = cmd[2] == 'n';
412 read_word(name);
413 matched = not ? macro_find(name) < 0 :
414 macro_find(name) >= 0;
415 } else {
416 matched = cpp_eval();
418 if (!matched)
419 jumpifs(0);
420 return 0;
422 if (!strcmp("else", cmd) || !strcmp("elif", cmd)) {
423 jumpifs(1);
424 return 0;
426 if (!strcmp("endif", cmd))
427 return 0;
428 if (!strcmp("include", cmd)) {
429 char file[NAMELEN];
430 char *s, *e;
431 jumpws();
432 s = buf + cur + 1;
433 e = strchr(buf + cur + 1, buf[cur] == '"' ? '"' : '>');
434 memcpy(file, s, e - s);
435 file[e - s] = '\0';
436 cur += e - s + 2;
437 if (include_find(file, *e == '>') == -1)
438 die("cannot include file\n");
439 return 0;
441 return 1;
444 static int macro_arg(struct macro *m, char *arg)
446 int i;
447 for (i = 0; i < m->nargs; i++)
448 if (!strcmp(arg, m->args[i]))
449 return i;
450 return -1;
453 static int buf_arg_find(char *name)
455 int i;
456 for (i = nbufs - 1; i >= 0; i--) {
457 struct buf *mbuf = &bufs[i];
458 struct macro *m = mbuf->macro;
459 if (mbuf->type == BUF_MACRO && macro_arg(m, name) >= 0)
460 return i;
461 if (mbuf->type == BUF_ARG)
462 i = mbuf->arg_buf;
464 return -1;
467 static void macro_expand(void)
469 char name[NAMELEN];
470 struct macro *m;
471 int mbuf;
472 read_word(name);
473 if ((mbuf = buf_arg_find(name)) >= 0) {
474 int arg = macro_arg(bufs[mbuf].macro, name);
475 char *dat = bufs[mbuf].args[arg];
476 buf_arg(dat, mbuf);
477 return;
479 m = &macros[macro_find(name)];
480 if (!m->isfunc) {
481 buf_macro(m);
482 return;
484 jumpws();
485 if (buf[cur] == '(') {
486 int i = 0;
487 struct buf *mbuf = &bufs[nbufs];
488 cur++;
489 jumpws();
490 while (cur < len && buf[cur] != ')') {
491 readarg(mbuf->args[i++]);
492 jumpws();
493 if (buf[cur] != ',')
494 break;
495 cur++;
496 jumpws();
498 while (i < m->nargs)
499 mbuf->args[i++][0] = '\0';
500 cur++;
501 buf_macro(m);
505 static int buf_expanding(char *macro)
507 int i;
508 for (i = nbufs - 1; i >= 0; i--) {
509 if (bufs[i].type == BUF_ARG)
510 return 0;
511 if (bufs[i].type == BUF_MACRO &&
512 !strcmp(macro, bufs[i].macro->name))
513 return 1;
515 return 0;
518 static int expandable(char *word)
520 if (buf_arg_find(word) >= 0)
521 return 1;
522 return !buf_expanding(word) && macro_find(word) != -1;
525 void cpp_define(char *name, char *def)
527 char tmp_buf[MACROLEN];
528 char *s = tmp_buf;
529 s = putstr(s, name);
530 *s++ = '\t';
531 s = putstr(s, def);
532 buf_new(BUF_TEMP, tmp_buf, s - tmp_buf);
533 macro_define();
534 buf_pop();
537 static int seen_macro;
539 static int hunk_off;
540 static int hunk_len;
542 int cpp_read(char *s)
544 int old;
545 if (seen_macro) {
546 seen_macro = 0;
547 macro_expand();
549 if (cur == len) {
550 struct buf *cbuf = &bufs[nbufs - 1];
551 if (nbufs < 2)
552 return -1;
553 if (cbuf->type & BUF_FILE)
554 free(buf);
555 buf_pop();
557 old = cur;
558 if (buf[cur] == '#')
559 if (!cpp_cmd())
560 return 0;
561 while (cur < len) {
562 if (buf[cur] == '#')
563 break;
564 if (buf[cur] == '/' && buf[cur + 1] == '*') {
565 jumpcomment();
566 continue;
568 if (buf[cur] == '\'' || buf[cur] == '"') {
569 jumpstr();
570 continue;
572 if (isalpha(buf[cur]) || buf[cur] == '_') {
573 char word[NAMELEN];
574 read_word(word);
575 if (expandable(word)) {
576 cur -= strlen(word);
577 seen_macro = 1;
578 break;
580 if (buf_iseval() && !strcmp("defined", word)) {
581 int parens = 0;
582 jumpws();
583 if (buf[cur] == '(') {
584 parens = 1;
585 cur++;
587 read_word(word);
588 if (parens) {
589 jumpws();
590 cur++;
593 continue;
595 cur++;
597 memcpy(s, buf + old, cur - old);
598 s[cur - old] = '\0';
599 if (!buf_iseval()) {
600 hunk_off += hunk_len;
601 hunk_len = cur - old;
603 return cur - old;
606 /* preprocessor constant expression evaluation */
608 static char etok[NAMELEN];
609 static int enext;
611 static char *tok2[] = {
612 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
615 static int eval_tok(void)
617 char *s = etok;
618 int i;
619 while (ecur < elen) {
620 while (ecur < elen && isspace(ebuf[ecur]))
621 ecur++;
622 if (ebuf[ecur] == '/' && ebuf[ecur + 1] == '*') {
623 while (ecur < elen && (ebuf[ecur - 2] != '*' ||
624 ebuf[ecur - 1] != '/'))
625 ecur++;
626 continue;
628 break;
630 if (ecur >= elen)
631 return TOK_EOF;
632 if (isalpha(ebuf[ecur]) || ebuf[ecur] == '_') {
633 while (isalnum(ebuf[ecur]) || ebuf[ecur] == '_')
634 *s++ = ebuf[ecur++];
635 *s = '\0';
636 return TOK_NAME;
638 if (isdigit(ebuf[ecur])) {
639 while (isdigit(ebuf[ecur]))
640 *s++ = ebuf[ecur++];
641 while (tolower(ebuf[ecur]) == 'u' || tolower(ebuf[ecur]) == 'l')
642 ecur++;
643 return TOK_NUM;
645 for (i = 0; i < ARRAY_SIZE(tok2); i++)
646 if (TOK2(tok2[i]) == TOK2(ebuf + ecur)) {
647 int ret = TOK2(tok2[i]);
648 ecur += 2;
649 return ret;
651 return ebuf[ecur++];
654 static int eval_see(void)
656 if (enext == -1)
657 enext = eval_tok();
658 return enext;
661 static int eval_get(void)
663 if (enext != -1) {
664 int ret = enext;
665 enext = -1;
666 return ret;
668 return eval_tok();
671 static long eval_num(void)
673 return atol(etok);
676 static int eval_jmp(int tok)
678 if (eval_see() == tok) {
679 eval_get();
680 return 0;
682 return 1;
685 static void eval_expect(int tok)
687 eval_jmp(tok);
690 static char *eval_id(void)
692 return etok;
695 static long evalcexpr(void);
697 static long evalatom(void)
699 if (!eval_jmp(TOK_NUM))
700 return eval_num();
701 if (!eval_jmp(TOK_NAME)) {
702 int parens = !eval_jmp('(');
703 long ret;
704 eval_expect(TOK_NAME);
705 ret = macro_find(eval_id()) >= 0;
706 if (parens)
707 eval_expect(')');
708 return ret;
710 if (!eval_jmp('(')) {
711 long ret = evalcexpr();
712 eval_expect(')');
713 return ret;
715 return -1;
718 static long evalpre(void)
720 if (!eval_jmp('!'))
721 return !evalpre();
722 if (!eval_jmp('-'))
723 return -evalpre();
724 if (!eval_jmp('~'))
725 return ~evalpre();
726 return evalatom();
729 static long evalmul(void)
731 long ret = evalpre();
732 while (1) {
733 if (!eval_jmp('*')) {
734 ret *= evalpre();
735 continue;
737 if (!eval_jmp('/')) {
738 ret /= evalpre();
739 continue;
741 if (!eval_jmp('%')) {
742 ret %= evalpre();
743 continue;
745 break;
747 return ret;
750 static long evaladd(void)
752 long ret = evalmul();
753 while (1) {
754 if (!eval_jmp('+')) {
755 ret += evalmul();
756 continue;
758 if (!eval_jmp('-')) {
759 ret -= evalmul();
760 continue;
762 break;
764 return ret;
767 static long evalshift(void)
769 long ret = evaladd();
770 while (1) {
771 if (!eval_jmp(TOK2("<<"))) {
772 ret <<= evaladd();
773 continue;
775 if (!eval_jmp(TOK2(">>"))) {
776 ret >>= evaladd();
777 continue;
779 break;
781 return ret;
784 static long evalcmp(void)
786 long ret = evalshift();
787 while (1) {
788 if (!eval_jmp('<')) {
789 ret = ret < evalshift();
790 continue;
792 if (!eval_jmp('>')) {
793 ret = ret > evalshift();
794 continue;
796 if (!eval_jmp(TOK2("<="))) {
797 ret = ret <= evalshift();
798 continue;
800 if (!eval_jmp(TOK2(">="))) {
801 ret = ret >= evalshift();
802 continue;
804 break;
806 return ret;
809 static long evaleq(void)
811 long ret = evalcmp();
812 while (1) {
813 if (!eval_jmp(TOK2("=="))) {
814 ret = ret == evalcmp();
815 continue;
817 if (!eval_jmp(TOK2("!="))) {
818 ret = ret != evalcmp();
819 continue;
821 break;
823 return ret;
826 static long evalbitand(void)
828 long ret = evaleq();
829 while (!eval_jmp('&'))
830 ret &= evaleq();
831 return ret;
834 static long evalxor(void)
836 long ret = evalbitand();
837 while (!eval_jmp('^'))
838 ret ^= evalbitand();
839 return ret;
842 static long evalbitor(void)
844 long ret = evalxor();
845 while (!eval_jmp('|'))
846 ret |= evalxor();
847 return ret;
850 static long evaland(void)
852 long ret = evalbitor();
853 while (!eval_jmp(TOK2("&&")))
854 ret = ret && evalbitor();
855 return ret;
858 static long evalor(void)
860 long ret = evaland();
861 while (!eval_jmp(TOK2("||")))
862 ret = ret || evaland();
863 return ret;
866 static long evalcexpr(void)
868 long ret = evalor();
869 if (eval_jmp('?'))
870 return ret;
871 if (ret)
872 return evalor();
873 while (eval_get() != ':')
875 return evalor();
878 static long evalexpr(void)
880 enext = -1;
881 return evalcexpr();
884 static int buf_loc(char *s, int off)
886 char *e = s + off;
887 int n = 1;
888 while ((s = strchr(s, '\n')) && s < e) {
889 n++;
890 s++;
892 return n;
895 int cpp_loc(char *s, long addr)
897 int line = -1;
898 int i;
899 for (i = nbufs - 1; i > 0; i--)
900 if (bufs[i].type == BUF_FILE)
901 break;
902 if (addr >= hunk_off && i == nbufs - 1)
903 line = buf_loc(buf, (cur - hunk_len) + (addr - hunk_off));
904 else
905 line = buf_loc(bufs[i].buf, bufs[i].cur);
906 sprintf(s, "%s:%d: ", bufs[i].path, line);
907 return strlen(s);