1 /* neatcc preprocessor */
10 #include <sys/types.h>
19 char name
[NAMELEN
]; /* macro name */
20 char def
[MDEFLEN
]; /* macro definition */
21 char args
[NARGS
][NAMELEN
];
22 int nargs
; /* number of arguments */
23 int isfunc
; /* macro is a function */
24 int undef
; /* macro is removed */
26 static int mcount
= 1; /* number of macros */
27 static int mhead
[256]; /* macro hash table heads */
28 static int mnext
[NDEFS
]; /* macro hash table next entries */
36 /* preprocessing input buffers for files, macros and macro arguments */
46 char args
[NARGS
][MARGLEN
]; /* arguments passed to a macro */
48 int arg_buf
; /* the bufs index of the owning macro */
51 static int bufs_limit
= 0; /* cpp_read() limit; useful in cpp_eval() */
53 void die(char *fmt
, ...)
58 vsprintf(msg
, fmt
, ap
);
60 write(2, msg
, strlen(msg
));
64 static void buf_new(int type
, char *dat
, long dlen
)
67 bufs
[bufs_n
- 1].buf
= buf
;
68 bufs
[bufs_n
- 1].cur
= cur
;
69 bufs
[bufs_n
- 1].len
= len
;
72 die("nomem: NBUFS reached!\n");
77 bufs
[bufs_n
- 1].type
= type
;
80 static void buf_file(char *path
, char *dat
, int dlen
)
82 buf_new(BUF_FILE
, dat
, dlen
);
83 strcpy(bufs
[bufs_n
- 1].path
, path
? path
: "");
86 static void buf_macro(struct macro
*m
)
88 buf_new(BUF_MACRO
, m
->def
, strlen(m
->def
));
89 bufs
[bufs_n
- 1].macro
= m
;
92 static void buf_arg(char *arg
, int mbuf
)
94 buf_new(BUF_ARG
, arg
, strlen(arg
));
95 bufs
[bufs_n
- 1].arg_buf
= mbuf
;
98 static void buf_pop(void)
101 if (bufs
[bufs_n
].type
== BUF_FILE
)
104 cur
= bufs
[bufs_n
- 1].cur
;
105 len
= bufs
[bufs_n
- 1].len
;
106 buf
= bufs
[bufs_n
- 1].buf
;
110 static int buf_iseval(void)
113 for (i
= bufs_n
- 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 /* header directory */
230 void cpp_path(char *s
)
235 static int include_find(char *name
, int std
)
238 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
241 sprintf(path
, "%s/%s", locs
[i
], name
);
244 if (!include_file(path
))
250 static void readarg(char *s
)
254 while (cur
< len
&& (depth
|| (buf
[cur
] != ',' && buf
[cur
] != ')'))) {
255 if (!jumpstr() || !jumpcomment())
257 switch (buf
[cur
++]) {
271 memcpy(s
, buf
+ beg
, cur
- beg
);
276 /* find a macro; if undef is nonzero, search #undef-ed macros too */
277 static int macro_find(char *name
, int undef
)
279 int i
= mhead
[(unsigned char) name
[0]];
281 if (!strcmp(name
, macros
[i
].name
))
282 if (!macros
[i
].undef
|| undef
)
289 static void macro_undef(char *name
)
291 int i
= macro_find(name
, 0);
296 static int macro_new(char *name
)
298 int i
= macro_find(name
, 1);
302 die("nomem: NDEFS reached!\n");
304 strcpy(macros
[i
].name
, name
);
305 mnext
[i
] = mhead
[(unsigned char) name
[0]];
306 mhead
[(unsigned char) name
[0]] = i
;
310 static void macro_define(void)
315 d
= ¯os
[macro_new(name
)];
318 if (buf
[cur
] == '(') {
321 while (cur
< len
&& buf
[cur
] != ')') {
322 readarg(d
->args
[d
->nargs
++]);
332 read_tilleol(d
->def
);
335 static char ebuf
[MARGLEN
];
339 static long evalexpr(void);
341 static long cpp_eval(void)
343 char evalbuf
[MARGLEN
];
347 read_tilleol(evalbuf
);
348 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
351 old_limit
= bufs_limit
;
353 while (!cpp_read(&cbuf
, &clen
)) {
354 memcpy(ebuf
+ elen
, cbuf
, clen
);
357 bufs_limit
= old_limit
;
363 static void jumpifs(int jumpelse
)
367 if (buf
[cur
] == '#') {
371 if (!strcmp("else", cmd
))
372 if (!depth
&& !jumpelse
)
374 if (!strcmp("elif", cmd
))
375 if (!depth
&& !jumpelse
&& cpp_eval())
377 if (!strcmp("endif", cmd
)) {
383 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
396 static int cpp_cmd(void)
401 if (!strcmp("define", cmd
)) {
405 if (!strcmp("undef", cmd
)) {
411 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
412 !strcmp("if", cmd
)) {
416 int not = cmd
[2] == 'n';
418 matched
= not ? macro_find(name
, 0) < 0 :
419 macro_find(name
, 0) >= 0;
421 matched
= cpp_eval();
427 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
431 if (!strcmp("endif", cmd
))
433 if (!strcmp("include", cmd
)) {
438 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
439 memcpy(file
, s
, e
- s
);
442 if (include_find(file
, *e
== '>') == -1)
443 err("cannot include <%s>\n", file
);
446 err("unknown directive <%s>\n", cmd
);
450 static int macro_arg(struct macro
*m
, char *arg
)
453 for (i
= 0; i
< m
->nargs
; i
++)
454 if (!strcmp(arg
, m
->args
[i
]))
459 static int buf_arg_find(char *name
)
462 for (i
= bufs_n
- 1; i
>= 0; i
--) {
463 struct buf
*mbuf
= &bufs
[i
];
464 struct macro
*m
= mbuf
->macro
;
465 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
467 if (mbuf
->type
== BUF_ARG
)
473 static void macro_expand(char *name
)
477 if ((mbuf
= buf_arg_find(name
)) >= 0) {
478 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
479 char *dat
= bufs
[mbuf
].args
[arg
];
483 m
= ¯os
[macro_find(name
, 0)];
489 if (buf
[cur
] == '(') {
491 struct buf
*mbuf
= &bufs
[bufs_n
];
494 while (cur
< len
&& buf
[cur
] != ')') {
495 readarg(mbuf
->args
[i
++]);
503 mbuf
->args
[i
++][0] = '\0';
509 static int buf_expanding(char *macro
)
512 for (i
= bufs_n
- 1; i
>= 0; i
--) {
513 if (bufs
[i
].type
== BUF_ARG
)
515 if (bufs
[i
].type
== BUF_MACRO
&&
516 !strcmp(macro
, bufs
[i
].macro
->name
))
522 /* return 1 for plain macros and arguments and 2 for function macros */
523 static int expandable(char *word
)
526 if (buf_arg_find(word
) >= 0)
528 if (buf_expanding(word
))
530 i
= macro_find(word
, 0);
531 return i
>= 0 ? macros
[i
].isfunc
+ 1 : 0;
534 void cpp_define(char *name
, char *def
)
536 char tmp_buf
[MDEFLEN
];
537 sprintf(tmp_buf
, "%s\t%s", name
, def
);
538 buf_new(BUF_TEMP
, tmp_buf
, strlen(tmp_buf
));
543 static int seen_macro
; /* seen a macro; 2 if a function macro */
544 static char seen_name
[NAMELEN
]; /* the name of the last macro */
549 int cpp_read(char **obuf
, long *olen
)
555 if (seen_macro
== 1) {
556 macro_expand(seen_name
);
560 if (bufs_n
< bufs_limit
+ 1)
565 if (cur
< len
&& buf
[cur
] == '#')
575 if (seen_macro
== 2) {
577 macro_expand(seen_name
);
584 if (isalnum(buf
[cur
]) || buf
[cur
] == '_') {
587 seen_macro
= expandable(word
);
589 strcpy(seen_name
, word
);
593 if (buf_iseval() && !strcmp("defined", word
)) {
596 if (buf
[cur
] == '(') {
610 /* macros are expanded later; ignoring their names */
611 end
= jump_name
? cur
- strlen(seen_name
) : cur
;
613 hunk_off
+= hunk_len
;
614 hunk_len
= end
- old
;
621 /* preprocessor constant expression evaluation */
623 #define TOK2(a) ((a)[0] << 16 | (a)[1] << 8)
628 static char etok
[NAMELEN
];
631 static char *tok2
[] = {
632 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
635 static int eval_tok(void)
639 while (ecur
< elen
) {
640 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
642 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
643 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
644 ebuf
[ecur
- 1] != '/'))
652 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
653 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
658 if (isdigit(ebuf
[ecur
])) {
659 while (isdigit(ebuf
[ecur
]))
661 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
666 for (i
= 0; i
< LEN(tok2
); i
++)
667 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
668 int ret
= TOK2(tok2
[i
]);
675 static int eval_see(void)
682 static int eval_get(void)
692 static long eval_num(void)
697 static int eval_jmp(int tok
)
699 if (eval_see() == tok
) {
706 static void eval_expect(int tok
)
711 static char *eval_id(void)
716 static long evalcexpr(void);
718 static long evalatom(void)
720 if (!eval_jmp(TOK_NUM
))
722 if (!eval_jmp(TOK_NAME
)) {
723 int parens
= !eval_jmp('(');
725 eval_expect(TOK_NAME
);
726 ret
= macro_find(eval_id(), 0) >= 0;
731 if (!eval_jmp('(')) {
732 long ret
= evalcexpr();
739 static long evalpre(void)
750 static long evalmul(void)
752 long ret
= evalpre();
754 if (!eval_jmp('*')) {
758 if (!eval_jmp('/')) {
762 if (!eval_jmp('%')) {
771 static long evaladd(void)
773 long ret
= evalmul();
775 if (!eval_jmp('+')) {
779 if (!eval_jmp('-')) {
788 static long evalshift(void)
790 long ret
= evaladd();
792 if (!eval_jmp(TOK2("<<"))) {
796 if (!eval_jmp(TOK2(">>"))) {
805 static long evalcmp(void)
807 long ret
= evalshift();
809 if (!eval_jmp('<')) {
810 ret
= ret
< evalshift();
813 if (!eval_jmp('>')) {
814 ret
= ret
> evalshift();
817 if (!eval_jmp(TOK2("<="))) {
818 ret
= ret
<= evalshift();
821 if (!eval_jmp(TOK2(">="))) {
822 ret
= ret
>= evalshift();
830 static long evaleq(void)
832 long ret
= evalcmp();
834 if (!eval_jmp(TOK2("=="))) {
835 ret
= ret
== evalcmp();
838 if (!eval_jmp(TOK2("!="))) {
839 ret
= ret
!= evalcmp();
847 static long evalbitand(void)
850 while (!eval_jmp('&'))
855 static long evalxor(void)
857 long ret
= evalbitand();
858 while (!eval_jmp('^'))
863 static long evalbitor(void)
865 long ret
= evalxor();
866 while (!eval_jmp('|'))
871 static long evaland(void)
873 long ret
= evalbitor();
874 while (!eval_jmp(TOK2("&&")))
875 ret
= ret
&& evalbitor();
879 static long evalor(void)
881 long ret
= evaland();
882 while (!eval_jmp(TOK2("||")))
883 ret
= ret
|| evaland();
887 static long evalcexpr(void)
894 while (eval_get() != ':')
899 static long evalexpr(void)
905 static int buf_loc(char *s
, int off
)
909 while ((s
= strchr(s
, '\n')) && s
< e
) {
916 char *cpp_loc(long addr
)
918 static char loc
[256];
921 for (i
= bufs_n
- 1; i
> 0; i
--)
922 if (bufs
[i
].type
== BUF_FILE
)
924 if (addr
>= hunk_off
&& i
== bufs_n
- 1)
925 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
927 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
928 sprintf(loc
, "%s:%d", bufs
[i
].path
, line
);