1 /* neatcc preprocessor */
10 #include <sys/types.h>
21 char name
[NAMELEN
]; /* macro name */
22 char def
[MDEFLEN
]; /* macro definition */
23 char args
[NARGS
][NAMELEN
];
24 int nargs
; /* number of arguments */
25 int isfunc
; /* macro is a function */
26 int undef
; /* macro is removed */
28 static int mcount
= 1; /* number of macros */
29 static int mhead
[256]; /* macro hash table heads */
30 static int mnext
[NDEFS
]; /* macro hash table next entries */
38 /* preprocessing input buffers for files, macros and macro arguments */
48 char args
[NARGS
][MARGLEN
]; /* arguments passed to a macro */
50 int arg_buf
; /* the bufs index of the owning macro */
53 static int bufs_limit
= 1; /* cpp_read() limit; useful in cpp_eval() */
55 void die(char *fmt
, ...)
60 vsprintf(msg
, fmt
, ap
);
62 write(2, msg
, strlen(msg
));
66 static void buf_new(int type
, char *dat
, int dlen
)
69 bufs
[nbufs
- 1].buf
= buf
;
70 bufs
[nbufs
- 1].cur
= cur
;
71 bufs
[nbufs
- 1].len
= len
;
74 die("nomem: NBUFS reached!\n");
79 bufs
[nbufs
- 1].type
= type
;
82 static void buf_file(char *path
, char *dat
, int dlen
)
84 buf_new(BUF_FILE
, dat
, dlen
);
85 strcpy(bufs
[nbufs
- 1].path
, path
? path
: "");
88 static void buf_macro(struct macro
*m
)
90 buf_new(BUF_MACRO
, m
->def
, strlen(m
->def
));
91 bufs
[nbufs
- 1].macro
= m
;
94 static void buf_arg(char *arg
, int mbuf
)
96 buf_new(BUF_ARG
, arg
, strlen(arg
));
97 bufs
[nbufs
- 1].arg_buf
= mbuf
;
100 static void buf_pop(void)
104 cur
= bufs
[nbufs
- 1].cur
;
105 len
= bufs
[nbufs
- 1].len
;
106 buf
= bufs
[nbufs
- 1].buf
;
110 static int buf_iseval(void)
113 for (i
= nbufs
- 1; i
>= 0; i
--)
114 if (bufs
[i
].type
== BUF_EVAL
)
119 static size_t file_size(int fd
)
127 static int include_file(char *path
)
129 int fd
= open(path
, O_RDONLY
);
135 size
= file_size(fd
) + 1;
137 while ((n
= read(fd
, dat
+ nr
, size
- nr
)) > 0)
141 buf_file(path
, dat
, nr
);
145 int cpp_init(char *path
)
147 return include_file(path
);
150 static int jumpws(void)
153 while (cur
< len
&& isspace(buf
[cur
]))
158 static void read_word(char *dst
)
161 while (cur
< len
&& (isalnum(buf
[cur
]) || buf
[cur
] == '_'))
166 static int jumpcomment(void)
168 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
169 while (++cur
< len
) {
170 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
176 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '/') {
177 while (++cur
< len
&& buf
[cur
] != '\n')
178 if (buf
[cur
] == '\\')
185 static int jumpstr(void)
187 if (buf
[cur
] == '\'') {
188 while (++cur
< len
&& buf
[cur
] != '\'')
189 if (buf
[cur
] == '\\')
194 if (buf
[cur
] == '"') {
195 while (++cur
< len
&& buf
[cur
] != '"')
196 if (buf
[cur
] == '\\')
204 static void read_tilleol(char *dst
)
206 while (cur
< len
&& isspace(buf
[cur
]) && buf
[cur
] != '\n')
208 while (cur
< len
&& buf
[cur
] != '\n') {
210 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n') {
215 memcpy(dst
, buf
+ last
, cur
- last
);
226 static char *locs
[NLOCS
] = {};
227 static int nlocs
= 0;
229 void cpp_addpath(char *s
)
234 static int include_find(char *name
, int std
)
237 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
240 sprintf(path
, "%s/%s", locs
[i
], name
);
243 if (!include_file(path
))
249 static void readarg(char *s
)
253 while (cur
< len
&& (depth
|| (buf
[cur
] != ',' && buf
[cur
] != ')'))) {
254 if (!jumpstr() || !jumpcomment())
256 switch (buf
[cur
++]) {
270 memcpy(s
, buf
+ beg
, cur
- beg
);
275 /* find a macro; if undef is nonzero, search #undef-ed macros too */
276 static int macro_find(char *name
, int undef
)
278 int i
= mhead
[(unsigned char) name
[0]];
280 if (!strcmp(name
, macros
[i
].name
))
281 if (!macros
[i
].undef
|| undef
)
288 static void macro_undef(char *name
)
290 int i
= macro_find(name
, 0);
295 static int macro_new(char *name
)
297 int i
= macro_find(name
, 1);
301 die("nomem: NDEFS reached!\n");
303 strcpy(macros
[i
].name
, name
);
304 mnext
[i
] = mhead
[(unsigned char) name
[0]];
305 mhead
[(unsigned char) name
[0]] = i
;
309 static void macro_define(void)
314 d
= ¯os
[macro_new(name
)];
317 if (buf
[cur
] == '(') {
320 while (cur
< len
&& buf
[cur
] != ')') {
321 readarg(d
->args
[d
->nargs
++]);
331 read_tilleol(d
->def
);
334 static char ebuf
[MARGLEN
];
338 static long evalexpr(void);
340 static int cpp_eval(void)
342 char evalbuf
[MARGLEN
];
346 read_tilleol(evalbuf
);
347 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
350 old_limit
= bufs_limit
;
352 while (!cpp_read(&cbuf
, &clen
)) {
353 memcpy(ebuf
+ elen
, cbuf
, clen
);
356 bufs_limit
= old_limit
;
362 static void jumpifs(int jumpelse
)
366 if (buf
[cur
] == '#') {
370 if (!strcmp("else", cmd
))
371 if (!depth
&& !jumpelse
)
373 if (!strcmp("elif", cmd
))
374 if (!depth
&& !jumpelse
&& cpp_eval())
376 if (!strcmp("endif", cmd
)) {
382 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
395 static int cpp_cmd(void)
400 if (!strcmp("define", cmd
)) {
404 if (!strcmp("undef", cmd
)) {
410 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
411 !strcmp("if", cmd
)) {
415 int not = cmd
[2] == 'n';
417 matched
= not ? macro_find(name
, 0) < 0 :
418 macro_find(name
, 0) >= 0;
420 matched
= cpp_eval();
426 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
430 if (!strcmp("endif", cmd
))
432 if (!strcmp("include", cmd
)) {
437 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
438 memcpy(file
, s
, e
- s
);
441 if (include_find(file
, *e
== '>') == -1)
442 err("cannot include <%s>\n", file
);
445 err("unknown directive <%s>\n", cmd
);
449 static int macro_arg(struct macro
*m
, char *arg
)
452 for (i
= 0; i
< m
->nargs
; i
++)
453 if (!strcmp(arg
, m
->args
[i
]))
458 static int buf_arg_find(char *name
)
461 for (i
= nbufs
- 1; i
>= 0; i
--) {
462 struct buf
*mbuf
= &bufs
[i
];
463 struct macro
*m
= mbuf
->macro
;
464 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
466 if (mbuf
->type
== BUF_ARG
)
472 static void macro_expand(char *name
)
476 if ((mbuf
= buf_arg_find(name
)) >= 0) {
477 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
478 char *dat
= bufs
[mbuf
].args
[arg
];
482 m
= ¯os
[macro_find(name
, 0)];
488 if (buf
[cur
] == '(') {
490 struct buf
*mbuf
= &bufs
[nbufs
];
493 while (cur
< len
&& buf
[cur
] != ')') {
494 readarg(mbuf
->args
[i
++]);
502 mbuf
->args
[i
++][0] = '\0';
508 static int buf_expanding(char *macro
)
511 for (i
= nbufs
- 1; i
>= 0; i
--) {
512 if (bufs
[i
].type
== BUF_ARG
)
514 if (bufs
[i
].type
== BUF_MACRO
&&
515 !strcmp(macro
, bufs
[i
].macro
->name
))
521 /* return 1 for plain macros and arguments and 2 for function macros */
522 static int expandable(char *word
)
525 if (buf_arg_find(word
) >= 0)
527 if (buf_expanding(word
))
529 i
= macro_find(word
, 0);
530 return i
>= 0 ? macros
[i
].isfunc
+ 1 : 0;
533 void cpp_define(char *name
, char *def
)
535 char tmp_buf
[MDEFLEN
];
536 sprintf(tmp_buf
, "%s\t%s", name
, def
);
537 buf_new(BUF_TEMP
, tmp_buf
, strlen(tmp_buf
));
542 static int seen_macro
; /* seen a macro; 2 if a function macro */
543 static char seen_name
[NAMELEN
]; /* the name of the last macro */
548 int cpp_read(char **obuf
, int *olen
)
554 if (seen_macro
== 1) {
555 macro_expand(seen_name
);
559 struct buf
*cbuf
= &bufs
[nbufs
- 1];
560 if (nbufs
< bufs_limit
+ 1)
562 if (cbuf
->type
== BUF_FILE
)
577 if (seen_macro
== 2) {
579 macro_expand(seen_name
);
586 if (isalnum(buf
[cur
]) || buf
[cur
] == '_') {
589 seen_macro
= expandable(word
);
591 strcpy(seen_name
, word
);
595 if (buf_iseval() && !strcmp("defined", word
)) {
598 if (buf
[cur
] == '(') {
612 /* macros are expanded later; ignoring their names */
613 end
= jump_name
? cur
- strlen(seen_name
) : cur
;
615 hunk_off
+= hunk_len
;
616 hunk_len
= end
- old
;
623 /* preprocessor constant expression evaluation */
625 static char etok
[NAMELEN
];
628 static char *tok2
[] = {
629 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
632 static int eval_tok(void)
636 while (ecur
< elen
) {
637 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
639 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
640 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
641 ebuf
[ecur
- 1] != '/'))
649 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
650 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
655 if (isdigit(ebuf
[ecur
])) {
656 while (isdigit(ebuf
[ecur
]))
658 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
663 for (i
= 0; i
< LEN(tok2
); i
++)
664 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
665 int ret
= TOK2(tok2
[i
]);
672 static int eval_see(void)
679 static int eval_get(void)
689 static long eval_num(void)
694 static int eval_jmp(int tok
)
696 if (eval_see() == tok
) {
703 static void eval_expect(int tok
)
708 static char *eval_id(void)
713 static long evalcexpr(void);
715 static long evalatom(void)
717 if (!eval_jmp(TOK_NUM
))
719 if (!eval_jmp(TOK_NAME
)) {
720 int parens
= !eval_jmp('(');
722 eval_expect(TOK_NAME
);
723 ret
= macro_find(eval_id(), 0) >= 0;
728 if (!eval_jmp('(')) {
729 long ret
= evalcexpr();
736 static long evalpre(void)
747 static long evalmul(void)
749 long ret
= evalpre();
751 if (!eval_jmp('*')) {
755 if (!eval_jmp('/')) {
759 if (!eval_jmp('%')) {
768 static long evaladd(void)
770 long ret
= evalmul();
772 if (!eval_jmp('+')) {
776 if (!eval_jmp('-')) {
785 static long evalshift(void)
787 long ret
= evaladd();
789 if (!eval_jmp(TOK2("<<"))) {
793 if (!eval_jmp(TOK2(">>"))) {
802 static long evalcmp(void)
804 long ret
= evalshift();
806 if (!eval_jmp('<')) {
807 ret
= ret
< evalshift();
810 if (!eval_jmp('>')) {
811 ret
= ret
> evalshift();
814 if (!eval_jmp(TOK2("<="))) {
815 ret
= ret
<= evalshift();
818 if (!eval_jmp(TOK2(">="))) {
819 ret
= ret
>= evalshift();
827 static long evaleq(void)
829 long ret
= evalcmp();
831 if (!eval_jmp(TOK2("=="))) {
832 ret
= ret
== evalcmp();
835 if (!eval_jmp(TOK2("!="))) {
836 ret
= ret
!= evalcmp();
844 static long evalbitand(void)
847 while (!eval_jmp('&'))
852 static long evalxor(void)
854 long ret
= evalbitand();
855 while (!eval_jmp('^'))
860 static long evalbitor(void)
862 long ret
= evalxor();
863 while (!eval_jmp('|'))
868 static long evaland(void)
870 long ret
= evalbitor();
871 while (!eval_jmp(TOK2("&&")))
872 ret
= ret
&& evalbitor();
876 static long evalor(void)
878 long ret
= evaland();
879 while (!eval_jmp(TOK2("||")))
880 ret
= ret
|| evaland();
884 static long evalcexpr(void)
891 while (eval_get() != ':')
896 static long evalexpr(void)
902 static int buf_loc(char *s
, int off
)
906 while ((s
= strchr(s
, '\n')) && s
< e
) {
913 char *cpp_loc(long addr
)
915 static char loc
[256];
918 for (i
= nbufs
- 1; i
> 0; i
--)
919 if (bufs
[i
].type
== BUF_FILE
)
921 if (addr
>= hunk_off
&& i
== nbufs
- 1)
922 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
924 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
925 sprintf(loc
, "%s:%d", bufs
[i
].path
, line
);