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
)];
319 if (buf
[cur
] == '(') {
322 while (cur
< len
&& buf
[cur
] != ')') {
323 readarg(d
->args
[d
->nargs
++]);
333 read_tilleol(d
->def
);
336 static char ebuf
[MARGLEN
];
340 static long evalexpr(void);
342 static long cpp_eval(void)
344 char evalbuf
[MARGLEN
];
348 read_tilleol(evalbuf
);
349 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
352 old_limit
= bufs_limit
;
354 while (!cpp_read(&cbuf
, &clen
)) {
355 memcpy(ebuf
+ elen
, cbuf
, clen
);
358 bufs_limit
= old_limit
;
364 static void jumpifs(int jumpelse
)
368 if (buf
[cur
] == '#') {
372 if (!strcmp("else", cmd
))
373 if (!depth
&& !jumpelse
)
375 if (!strcmp("elif", cmd
))
376 if (!depth
&& !jumpelse
&& cpp_eval())
378 if (!strcmp("endif", cmd
)) {
384 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
397 static int cpp_cmd(void)
402 if (!strcmp("define", cmd
)) {
406 if (!strcmp("undef", cmd
)) {
412 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
413 !strcmp("if", cmd
)) {
417 int not = cmd
[2] == 'n';
419 matched
= not ? macro_find(name
, 0) < 0 :
420 macro_find(name
, 0) >= 0;
422 matched
= cpp_eval();
428 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
432 if (!strcmp("endif", cmd
))
434 if (!strcmp("include", cmd
)) {
439 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
440 memcpy(file
, s
, e
- s
);
443 if (include_find(file
, *e
== '>') == -1)
444 err("cannot include <%s>\n", file
);
447 err("unknown directive <%s>\n", cmd
);
451 static int macro_arg(struct macro
*m
, char *arg
)
454 for (i
= 0; i
< m
->nargs
; i
++)
455 if (!strcmp(arg
, m
->args
[i
]))
460 static int buf_arg_find(char *name
)
463 for (i
= bufs_n
- 1; i
>= 0; i
--) {
464 struct buf
*mbuf
= &bufs
[i
];
465 struct macro
*m
= mbuf
->macro
;
466 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
468 if (mbuf
->type
== BUF_ARG
)
474 static void macro_expand(char *name
)
478 if ((mbuf
= buf_arg_find(name
)) >= 0) {
479 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
480 char *dat
= bufs
[mbuf
].args
[arg
];
484 m
= ¯os
[macro_find(name
, 0)];
490 if (buf
[cur
] == '(') {
492 struct buf
*mbuf
= &bufs
[bufs_n
];
495 while (cur
< len
&& buf
[cur
] != ')') {
496 readarg(mbuf
->args
[i
++]);
504 mbuf
->args
[i
++][0] = '\0';
510 static int buf_expanding(char *macro
)
513 for (i
= bufs_n
- 1; i
>= 0; i
--) {
514 if (bufs
[i
].type
== BUF_ARG
)
516 if (bufs
[i
].type
== BUF_MACRO
&&
517 !strcmp(macro
, bufs
[i
].macro
->name
))
523 /* return 1 for plain macros and arguments and 2 for function macros */
524 static int expandable(char *word
)
527 if (buf_arg_find(word
) >= 0)
529 if (buf_expanding(word
))
531 i
= macro_find(word
, 0);
532 return i
>= 0 ? macros
[i
].isfunc
+ 1 : 0;
535 void cpp_define(char *name
, char *def
)
537 char tmp_buf
[MDEFLEN
];
538 sprintf(tmp_buf
, "%s\t%s", name
, def
);
539 buf_new(BUF_TEMP
, tmp_buf
, strlen(tmp_buf
));
544 static int seen_macro
; /* seen a macro; 2 if a function macro */
545 static char seen_name
[NAMELEN
]; /* the name of the last macro */
550 int cpp_read(char **obuf
, long *olen
)
556 if (seen_macro
== 1) {
557 macro_expand(seen_name
);
561 if (bufs_n
< bufs_limit
+ 1)
566 if (cur
< len
&& buf
[cur
] == '#')
576 if (seen_macro
== 2) {
578 macro_expand(seen_name
);
585 if (isalnum(buf
[cur
]) || buf
[cur
] == '_') {
588 seen_macro
= expandable(word
);
590 strcpy(seen_name
, word
);
594 if (buf_iseval() && !strcmp("defined", word
)) {
597 if (buf
[cur
] == '(') {
611 /* macros are expanded later; ignoring their names */
612 end
= jump_name
? cur
- strlen(seen_name
) : cur
;
614 hunk_off
+= hunk_len
;
615 hunk_len
= end
- old
;
622 /* preprocessor constant expression evaluation */
624 #define TOK2(a) ((a)[0] << 16 | (a)[1] << 8)
629 static char etok
[NAMELEN
];
632 static char *tok2
[] = {
633 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
636 static int eval_tok(void)
640 while (ecur
< elen
) {
641 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
643 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
644 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
645 ebuf
[ecur
- 1] != '/'))
653 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
654 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
659 if (isdigit(ebuf
[ecur
])) {
660 while (isdigit(ebuf
[ecur
]))
662 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
667 for (i
= 0; i
< LEN(tok2
); i
++)
668 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
669 int ret
= TOK2(tok2
[i
]);
676 static int eval_see(void)
683 static int eval_get(void)
693 static long eval_num(void)
698 static int eval_jmp(int tok
)
700 if (eval_see() == tok
) {
707 static void eval_expect(int tok
)
712 static char *eval_id(void)
717 static long evalcexpr(void);
719 static long evalatom(void)
721 if (!eval_jmp(TOK_NUM
))
723 if (!eval_jmp(TOK_NAME
)) {
724 int parens
= !eval_jmp('(');
726 eval_expect(TOK_NAME
);
727 ret
= macro_find(eval_id(), 0) >= 0;
732 if (!eval_jmp('(')) {
733 long ret
= evalcexpr();
740 static long evalpre(void)
751 static long evalmul(void)
753 long ret
= evalpre();
755 if (!eval_jmp('*')) {
759 if (!eval_jmp('/')) {
763 if (!eval_jmp('%')) {
772 static long evaladd(void)
774 long ret
= evalmul();
776 if (!eval_jmp('+')) {
780 if (!eval_jmp('-')) {
789 static long evalshift(void)
791 long ret
= evaladd();
793 if (!eval_jmp(TOK2("<<"))) {
797 if (!eval_jmp(TOK2(">>"))) {
806 static long evalcmp(void)
808 long ret
= evalshift();
810 if (!eval_jmp('<')) {
811 ret
= ret
< evalshift();
814 if (!eval_jmp('>')) {
815 ret
= ret
> evalshift();
818 if (!eval_jmp(TOK2("<="))) {
819 ret
= ret
<= evalshift();
822 if (!eval_jmp(TOK2(">="))) {
823 ret
= ret
>= evalshift();
831 static long evaleq(void)
833 long ret
= evalcmp();
835 if (!eval_jmp(TOK2("=="))) {
836 ret
= ret
== evalcmp();
839 if (!eval_jmp(TOK2("!="))) {
840 ret
= ret
!= evalcmp();
848 static long evalbitand(void)
851 while (!eval_jmp('&'))
856 static long evalxor(void)
858 long ret
= evalbitand();
859 while (!eval_jmp('^'))
864 static long evalbitor(void)
866 long ret
= evalxor();
867 while (!eval_jmp('|'))
872 static long evaland(void)
874 long ret
= evalbitor();
875 while (!eval_jmp(TOK2("&&")))
876 ret
= ret
&& evalbitor();
880 static long evalor(void)
882 long ret
= evaland();
883 while (!eval_jmp(TOK2("||")))
884 ret
= ret
|| evaland();
888 static long evalcexpr(void)
895 while (eval_get() != ':')
900 static long evalexpr(void)
906 static int buf_loc(char *s
, int off
)
910 while ((s
= strchr(s
, '\n')) && s
< e
) {
917 char *cpp_loc(long addr
)
919 static char loc
[256];
922 for (i
= bufs_n
- 1; i
> 0; i
--)
923 if (bufs
[i
].type
== BUF_FILE
)
925 if (addr
>= hunk_off
&& i
== bufs_n
- 1)
926 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
928 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
929 sprintf(loc
, "%s:%d", bufs
[i
].path
, line
);