README: ignored keywords are now built into ncc.c
[neatcc/cc.git] / cpp.c
blob7c02841b34e2bb98200c9f741fa55be64b4cb75b
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 return include_file(path);
145 static void jumpws(void)
147 while (cur < len && isspace(buf[cur]))
148 cur++;
151 static void read_word(char *dst)
153 jumpws();
154 while (cur < len && (isalnum(buf[cur]) || buf[cur] == '_'))
155 *dst++ = buf[cur++];
156 *dst = '\0';
159 static int jumpcomment(void)
161 if (buf[cur] == '/' && buf[cur + 1] == '*') {
162 while (++cur < len) {
163 if (buf[cur] == '*' && buf[cur + 1] == '/') {
164 cur += 2;
165 return 0;
169 if (buf[cur] == '/' && buf[cur + 1] == '/') {
170 while (++cur < len)
171 if (buf[cur] == '\n')
172 break;
173 return 0;
175 return 1;
178 static void read_tilleol(char *dst)
180 while (cur < len && isspace(buf[cur]) && buf[cur] != '\n')
181 cur++;
182 while (cur < len && buf[cur] != '\n') {
183 if (buf[cur] == '\\' && buf[cur + 1] == '\n')
184 cur += 2;
185 else if (jumpcomment())
186 *dst++ = buf[cur++];
188 *dst = '\0';
191 static char *putstr(char *d, char *s)
193 while (*s)
194 *d++ = *s++;
195 *d = '\0';
196 return d;
199 #define MAXLOCS (1 << 10)
201 static char *locs[MAXLOCS] = {"/usr/include"};
202 static int nlocs = 1;
204 void cpp_addpath(char *s)
206 locs[nlocs++] = s;
209 static int include_find(char *name, int std)
211 int i;
212 for (i = std ? nlocs - 1 : nlocs; i >= 0; i--) {
213 char path[1 << 10];
214 char *s;
215 s = path;
216 if (locs[i]) {
217 s = putstr(s, locs[i]);
218 *s++ = '/';
220 s = putstr(s, name);
221 if (!include_file(path))
222 return 0;
224 return -1;
227 static int jumpstr(void)
229 if (buf[cur] == '\'') {
230 while (cur < len && buf[++cur] != '\'')
231 if (buf[cur] == '\\')
232 cur++;
233 cur++;
234 return 0;
236 if (buf[cur] == '"') {
237 while (cur < len && buf[++cur] != '"')
238 if (buf[cur] == '\\')
239 cur++;
240 cur++;
241 return 0;
243 return 1;
246 static void readarg(char *s)
248 int depth = 0;
249 int beg = cur;
250 while (cur < len && (depth || buf[cur] != ',' && buf[cur] != ')')) {
251 if (!jumpstr() || !jumpcomment())
252 continue;
253 switch (buf[cur]) {
254 case '(':
255 case '[':
256 case '{':
257 cur++;
258 depth++;
259 break;
260 case ')':
261 case ']':
262 case '}':
263 cur++;
264 depth--;
265 break;
266 default:
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 (!jumpcomment())
380 continue;
381 if (!jumpstr())
382 continue;
383 cur++;
387 static int cpp_cmd(void)
389 char cmd[NAMELEN];
390 cur++;
391 read_word(cmd);
392 if (!strcmp("define", cmd)) {
393 macro_define();
394 return 0;
396 if (!strcmp("undef", cmd)) {
397 char name[NAMELEN];
398 read_word(name);
399 macro_undef(name);
400 return 0;
402 if (!strcmp("ifdef", cmd) || !strcmp("ifndef", cmd) ||
403 !strcmp("if", cmd)) {
404 char name[NAMELEN];
405 int matched = 0;
406 if (cmd[2]) {
407 int not = cmd[2] == 'n';
408 read_word(name);
409 matched = not ? macro_find(name) < 0 :
410 macro_find(name) >= 0;
411 } else {
412 matched = cpp_eval();
414 if (!matched)
415 jumpifs(0);
416 return 0;
418 if (!strcmp("else", cmd) || !strcmp("elif", cmd)) {
419 jumpifs(1);
420 return 0;
422 if (!strcmp("endif", cmd))
423 return 0;
424 if (!strcmp("include", cmd)) {
425 char file[NAMELEN];
426 char *s, *e;
427 jumpws();
428 s = buf + cur + 1;
429 e = strchr(buf + cur + 1, buf[cur] == '"' ? '"' : '>');
430 memcpy(file, s, e - s);
431 file[e - s] = '\0';
432 cur += e - s + 2;
433 if (include_find(file, *e == '>') == -1)
434 die("cannot include file\n");
435 return 0;
437 return 1;
440 static int macro_arg(struct macro *m, char *arg)
442 int i;
443 for (i = 0; i < m->nargs; i++)
444 if (!strcmp(arg, m->args[i]))
445 return i;
446 return -1;
449 static int buf_arg_find(char *name)
451 int i;
452 for (i = nbufs - 1; i >= 0; i--) {
453 struct buf *mbuf = &bufs[i];
454 struct macro *m = mbuf->macro;
455 if (mbuf->type == BUF_MACRO && macro_arg(m, name) >= 0)
456 return i;
457 if (mbuf->type == BUF_ARG)
458 i = mbuf->arg_buf;
460 return -1;
463 static void macro_expand(void)
465 char name[NAMELEN];
466 struct macro *m;
467 int mbuf;
468 read_word(name);
469 if ((mbuf = buf_arg_find(name)) >= 0) {
470 int arg = macro_arg(bufs[mbuf].macro, name);
471 char *dat = bufs[mbuf].args[arg];
472 buf_arg(dat, mbuf);
473 return;
475 m = &macros[macro_find(name)];
476 if (!m->isfunc) {
477 buf_macro(m);
478 return;
480 jumpws();
481 if (buf[cur] == '(') {
482 int i = 0;
483 struct buf *mbuf = &bufs[nbufs];
484 cur++;
485 jumpws();
486 while (cur < len && buf[cur] != ')') {
487 readarg(mbuf->args[i++]);
488 jumpws();
489 if (buf[cur] != ',')
490 break;
491 cur++;
492 jumpws();
494 while (i < m->nargs)
495 mbuf->args[i++][0] = '\0';
496 cur++;
497 buf_macro(m);
501 static int buf_expanding(char *macro)
503 int i;
504 for (i = nbufs - 1; i >= 0; i--) {
505 if (bufs[i].type == BUF_ARG)
506 return 0;
507 if (bufs[i].type == BUF_MACRO &&
508 !strcmp(macro, bufs[i].macro->name))
509 return 1;
511 return 0;
514 static int expandable(char *word)
516 if (buf_arg_find(word) >= 0)
517 return 1;
518 return !buf_expanding(word) && macro_find(word) != -1;
521 void cpp_define(char *name, char *def)
523 char tmp_buf[MACROLEN];
524 char *s = tmp_buf;
525 s = putstr(s, name);
526 *s++ = '\t';
527 s = putstr(s, def);
528 buf_new(BUF_TEMP, tmp_buf, s - tmp_buf);
529 macro_define();
530 buf_pop();
533 static int seen_macro;
535 static int hunk_off;
536 static int hunk_len;
538 int cpp_read(char *s)
540 int old;
541 if (seen_macro) {
542 seen_macro = 0;
543 macro_expand();
545 if (cur == len) {
546 struct buf *cbuf = &bufs[nbufs - 1];
547 if (nbufs < 2)
548 return -1;
549 if (cbuf->type & BUF_FILE)
550 free(buf);
551 buf_pop();
553 old = cur;
554 if (buf[cur] == '#')
555 if (!cpp_cmd())
556 return 0;
557 while (cur < len) {
558 if (buf[cur] == '#')
559 break;
560 if (!jumpcomment())
561 continue;
562 if (!jumpstr())
563 continue;
564 if (isalpha(buf[cur]) || buf[cur] == '_') {
565 char word[NAMELEN];
566 read_word(word);
567 if (expandable(word)) {
568 cur -= strlen(word);
569 seen_macro = 1;
570 break;
572 if (buf_iseval() && !strcmp("defined", word)) {
573 int parens = 0;
574 jumpws();
575 if (buf[cur] == '(') {
576 parens = 1;
577 cur++;
579 read_word(word);
580 if (parens) {
581 jumpws();
582 cur++;
585 continue;
587 cur++;
589 memcpy(s, buf + old, cur - old);
590 s[cur - old] = '\0';
591 if (!buf_iseval()) {
592 hunk_off += hunk_len;
593 hunk_len = cur - old;
595 return cur - old;
598 /* preprocessor constant expression evaluation */
600 static char etok[NAMELEN];
601 static int enext;
603 static char *tok2[] = {
604 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
607 static int eval_tok(void)
609 char *s = etok;
610 int i;
611 while (ecur < elen) {
612 while (ecur < elen && isspace(ebuf[ecur]))
613 ecur++;
614 if (ebuf[ecur] == '/' && ebuf[ecur + 1] == '*') {
615 while (ecur < elen && (ebuf[ecur - 2] != '*' ||
616 ebuf[ecur - 1] != '/'))
617 ecur++;
618 continue;
620 break;
622 if (ecur >= elen)
623 return TOK_EOF;
624 if (isalpha(ebuf[ecur]) || ebuf[ecur] == '_') {
625 while (isalnum(ebuf[ecur]) || ebuf[ecur] == '_')
626 *s++ = ebuf[ecur++];
627 *s = '\0';
628 return TOK_NAME;
630 if (isdigit(ebuf[ecur])) {
631 while (isdigit(ebuf[ecur]))
632 *s++ = ebuf[ecur++];
633 while (tolower(ebuf[ecur]) == 'u' || tolower(ebuf[ecur]) == 'l')
634 ecur++;
635 return TOK_NUM;
637 for (i = 0; i < ARRAY_SIZE(tok2); i++)
638 if (TOK2(tok2[i]) == TOK2(ebuf + ecur)) {
639 int ret = TOK2(tok2[i]);
640 ecur += 2;
641 return ret;
643 return ebuf[ecur++];
646 static int eval_see(void)
648 if (enext == -1)
649 enext = eval_tok();
650 return enext;
653 static int eval_get(void)
655 if (enext != -1) {
656 int ret = enext;
657 enext = -1;
658 return ret;
660 return eval_tok();
663 static long eval_num(void)
665 return atol(etok);
668 static int eval_jmp(int tok)
670 if (eval_see() == tok) {
671 eval_get();
672 return 0;
674 return 1;
677 static void eval_expect(int tok)
679 eval_jmp(tok);
682 static char *eval_id(void)
684 return etok;
687 static long evalcexpr(void);
689 static long evalatom(void)
691 if (!eval_jmp(TOK_NUM))
692 return eval_num();
693 if (!eval_jmp(TOK_NAME)) {
694 int parens = !eval_jmp('(');
695 long ret;
696 eval_expect(TOK_NAME);
697 ret = macro_find(eval_id()) >= 0;
698 if (parens)
699 eval_expect(')');
700 return ret;
702 if (!eval_jmp('(')) {
703 long ret = evalcexpr();
704 eval_expect(')');
705 return ret;
707 return -1;
710 static long evalpre(void)
712 if (!eval_jmp('!'))
713 return !evalpre();
714 if (!eval_jmp('-'))
715 return -evalpre();
716 if (!eval_jmp('~'))
717 return ~evalpre();
718 return evalatom();
721 static long evalmul(void)
723 long ret = evalpre();
724 while (1) {
725 if (!eval_jmp('*')) {
726 ret *= evalpre();
727 continue;
729 if (!eval_jmp('/')) {
730 ret /= evalpre();
731 continue;
733 if (!eval_jmp('%')) {
734 ret %= evalpre();
735 continue;
737 break;
739 return ret;
742 static long evaladd(void)
744 long ret = evalmul();
745 while (1) {
746 if (!eval_jmp('+')) {
747 ret += evalmul();
748 continue;
750 if (!eval_jmp('-')) {
751 ret -= evalmul();
752 continue;
754 break;
756 return ret;
759 static long evalshift(void)
761 long ret = evaladd();
762 while (1) {
763 if (!eval_jmp(TOK2("<<"))) {
764 ret <<= evaladd();
765 continue;
767 if (!eval_jmp(TOK2(">>"))) {
768 ret >>= evaladd();
769 continue;
771 break;
773 return ret;
776 static long evalcmp(void)
778 long ret = evalshift();
779 while (1) {
780 if (!eval_jmp('<')) {
781 ret = ret < evalshift();
782 continue;
784 if (!eval_jmp('>')) {
785 ret = ret > evalshift();
786 continue;
788 if (!eval_jmp(TOK2("<="))) {
789 ret = ret <= evalshift();
790 continue;
792 if (!eval_jmp(TOK2(">="))) {
793 ret = ret >= evalshift();
794 continue;
796 break;
798 return ret;
801 static long evaleq(void)
803 long ret = evalcmp();
804 while (1) {
805 if (!eval_jmp(TOK2("=="))) {
806 ret = ret == evalcmp();
807 continue;
809 if (!eval_jmp(TOK2("!="))) {
810 ret = ret != evalcmp();
811 continue;
813 break;
815 return ret;
818 static long evalbitand(void)
820 long ret = evaleq();
821 while (!eval_jmp('&'))
822 ret &= evaleq();
823 return ret;
826 static long evalxor(void)
828 long ret = evalbitand();
829 while (!eval_jmp('^'))
830 ret ^= evalbitand();
831 return ret;
834 static long evalbitor(void)
836 long ret = evalxor();
837 while (!eval_jmp('|'))
838 ret |= evalxor();
839 return ret;
842 static long evaland(void)
844 long ret = evalbitor();
845 while (!eval_jmp(TOK2("&&")))
846 ret = ret && evalbitor();
847 return ret;
850 static long evalor(void)
852 long ret = evaland();
853 while (!eval_jmp(TOK2("||")))
854 ret = ret || evaland();
855 return ret;
858 static long evalcexpr(void)
860 long ret = evalor();
861 if (eval_jmp('?'))
862 return ret;
863 if (ret)
864 return evalor();
865 while (eval_get() != ':')
867 return evalor();
870 static long evalexpr(void)
872 enext = -1;
873 return evalcexpr();
876 static int buf_loc(char *s, int off)
878 char *e = s + off;
879 int n = 1;
880 while ((s = strchr(s, '\n')) && s < e) {
881 n++;
882 s++;
884 return n;
887 int cpp_loc(char *s, long addr)
889 int line = -1;
890 int i;
891 for (i = nbufs - 1; i > 0; i--)
892 if (bufs[i].type == BUF_FILE)
893 break;
894 if (addr >= hunk_off && i == nbufs - 1)
895 line = buf_loc(buf, (cur - hunk_len) + (addr - hunk_off));
896 else
897 line = buf_loc(bufs[i].buf, bufs[i].cur);
898 sprintf(s, "%s:%d: ", bufs[i].path, line);
899 return strlen(s);