17 #define MAXDEFS (1 << 12)
18 #define MACROLEN (1 << 10)
19 #define MAXARGS (1 << 5)
20 #define NBUCKET (MAXDEFS << 1)
25 char args
[MAXARGS
][NAMELEN
];
30 /* macro hash table */
31 static struct tab mtab
;
33 #define MAXBUFS (1 << 5)
49 char args
[MAXARGS
][MACROLEN
]; /* arguments passed to a macro */
51 int arg_buf
; /* the bufs index of the owning macro */
55 static void buf_new(int type
, char *dat
, int dlen
)
58 bufs
[nbufs
- 1].buf
= buf
;
59 bufs
[nbufs
- 1].cur
= cur
;
60 bufs
[nbufs
- 1].len
= len
;
63 die("nomem: MAXBUFS reached!\n");
68 bufs
[nbufs
- 1].type
= type
;
71 static void buf_file(char *path
, char *dat
, int dlen
)
73 buf_new(BUF_FILE
, dat
, dlen
);
74 strcpy(bufs
[nbufs
- 1].path
, path
? path
: "");
77 static void buf_macro(struct macro
*m
)
79 buf_new(BUF_MACRO
, m
->def
, strlen(m
->def
));
80 bufs
[nbufs
- 1].macro
= m
;
83 static void buf_arg(char *arg
, int mbuf
)
85 buf_new(BUF_ARG
, arg
, strlen(arg
));
86 bufs
[nbufs
- 1].arg_buf
= mbuf
;
89 static void buf_pop(void)
93 cur
= bufs
[nbufs
- 1].cur
;
94 len
= bufs
[nbufs
- 1].len
;
95 buf
= bufs
[nbufs
- 1].buf
;
99 static int buf_iseval(void)
102 for (i
= nbufs
- 1; i
>= 0; i
--)
103 if (bufs
[i
].type
== BUF_EVAL
)
108 static size_t file_size(int fd
)
116 static int include_file(char *path
)
118 int fd
= open(path
, O_RDONLY
);
124 size
= file_size(fd
) + 1;
126 while ((n
= read(fd
, dat
+ nr
, size
- nr
)) > 0)
130 buf_file(path
, dat
, nr
);
134 int cpp_init(char *path
)
136 cpp_define("__STDC__", "");
137 cpp_define("__i386__", "");
138 cpp_define("__linux__", "");
139 return include_file(path
);
142 static void jumpws(void)
144 while (cur
< len
&& isspace(buf
[cur
]))
148 static void read_word(char *dst
)
151 while (cur
< len
&& (isalnum(buf
[cur
]) || buf
[cur
] == '_'))
156 static void jumpcomment(void)
158 while (++cur
< len
) {
159 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
166 static void read_tilleol(char *dst
)
168 while (cur
< len
&& isspace(buf
[cur
]) && buf
[cur
] != '\n')
170 while (cur
< len
&& buf
[cur
] != '\n') {
171 if (buf
[cur
] == '\\')
173 else if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
181 static char *putstr(char *d
, char *s
)
189 #define MAXLOCS (1 << 10)
191 static char *locs
[MAXLOCS
] = {"/usr/include"};
192 static int nlocs
= 1;
194 void cpp_addpath(char *s
)
199 static int include_find(char *name
, int std
)
202 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
207 s
= putstr(s
, locs
[i
]);
211 if (!include_file(path
))
217 static void jumpstr(void)
219 if (buf
[cur
] == '\'') {
220 while (cur
< len
&& buf
[++cur
] != '\'')
221 if (buf
[cur
] == '\\')
226 if (buf
[cur
] == '"') {
227 while (cur
< len
&& buf
[++cur
] != '"')
228 if (buf
[cur
] == '\\')
235 static void readarg(char *s
)
239 while (cur
< len
&& (depth
|| buf
[cur
] != ',' && buf
[cur
] != ')')) {
258 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
265 memcpy(s
, buf
+ beg
, cur
- beg
);
270 static int macro_find(char *name
)
272 char *n
= tab_get(&mtab
, name
);
275 return container(n
, struct macro
, name
) - macros
;
278 static void macro_undef(char *name
)
280 int i
= macro_find(name
);
282 tab_del(&mtab
, macros
[i
].name
);
285 static int macro_new(char *name
)
287 int i
= macro_find(name
);
290 if (nmacros
>= MAXDEFS
)
291 die("nomem: MAXDEFS reached!\n");
293 strcpy(macros
[i
].name
, name
);
294 tab_add(&mtab
, macros
[i
].name
);
298 static void macro_define(void)
303 d
= ¯os
[macro_new(name
)];
306 if (buf
[cur
] == '(') {
309 while (cur
< len
&& buf
[cur
] != ')') {
310 readarg(d
->args
[d
->nargs
++]);
312 if (buf
[cur
++] != ',')
318 read_tilleol(d
->def
);
321 int cpp_read(char *buf
);
323 static char ebuf
[BUFSIZE
];
327 static long evalexpr(void);
329 static int cpp_eval(void)
333 char evalbuf
[BUFSIZE
];
334 read_tilleol(evalbuf
);
335 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
339 while (bufid
< nbufs
|| (bufid
== nbufs
&& cur
< len
))
340 elen
+= cpp_read(ebuf
+ elen
);
346 static void jumpifs(int jumpelse
)
350 if (buf
[cur
] == '#') {
354 if (!strcmp("else", cmd
))
355 if (!depth
&& !jumpelse
)
357 if (!strcmp("elif", cmd
))
358 if (!depth
&& !jumpelse
&& cpp_eval())
360 if (!strcmp("endif", cmd
)) {
366 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
371 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
375 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
383 static void cpp_cmd(void)
388 if (!strcmp("define", cmd
)) {
392 if (!strcmp("undef", cmd
)) {
398 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
399 !strcmp("if", cmd
)) {
403 int not = cmd
[2] == 'n';
405 matched
= not ? macro_find(name
) < 0 :
406 macro_find(name
) >= 0;
408 matched
= cpp_eval();
414 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
418 if (!strcmp("endif", cmd
))
420 if (!strcmp("include", cmd
)) {
425 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
426 memcpy(file
, s
, e
- s
);
429 if (include_find(file
, *e
== '>') == -1)
430 die("cannot include file\n");
435 static int macro_arg(struct macro
*m
, char *arg
)
438 for (i
= 0; i
< m
->nargs
; i
++)
439 if (!strcmp(arg
, m
->args
[i
]))
444 static int buf_arg_find(char *name
)
447 for (i
= nbufs
- 1; i
>= 0; i
--) {
448 struct buf
*mbuf
= &bufs
[i
];
449 struct macro
*m
= mbuf
->macro
;
450 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
452 if (mbuf
->type
== BUF_ARG
)
458 static void macro_expand(void)
464 if ((mbuf
= buf_arg_find(name
)) >= 0) {
465 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
466 char *dat
= bufs
[mbuf
].args
[arg
];
470 m
= ¯os
[macro_find(name
)];
476 if (buf
[cur
] == '(') {
478 struct buf
*mbuf
= &bufs
[nbufs
];
481 while (cur
< len
&& buf
[cur
] != ')') {
482 readarg(mbuf
->args
[i
++]);
490 mbuf
->args
[i
++][0] = '\0';
496 static int buf_expanding(char *macro
)
499 for (i
= nbufs
- 1; i
>= 0; i
--) {
500 if (bufs
[i
].type
== BUF_ARG
)
502 if (bufs
[i
].type
== BUF_MACRO
&&
503 !strcmp(macro
, bufs
[i
].macro
->name
))
509 static int expandable(char *word
)
511 if (buf_arg_find(word
) >= 0)
513 return !buf_expanding(word
) && macro_find(word
) != -1;
516 void cpp_define(char *name
, char *def
)
518 char tmp_buf
[MACROLEN
];
523 buf_new(BUF_TEMP
, tmp_buf
, s
- tmp_buf
);
528 static int seen_macro
;
533 int cpp_read(char *s
)
541 struct buf
*cbuf
= &bufs
[nbufs
- 1];
544 if (cbuf
->type
& BUF_FILE
)
549 if (buf
[cur
] == '#') {
556 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
560 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
564 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
567 if (expandable(word
)) {
572 if (buf_iseval() && !strcmp("defined", word
)) {
575 if (buf
[cur
] == '(') {
589 memcpy(s
, buf
+ old
, cur
- old
);
592 hunk_off
+= hunk_len
;
593 hunk_len
= cur
- old
;
598 /* preprocessor constant expression evaluation */
600 static char etok
[NAMELEN
];
603 static char *tok2
[] = {
604 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
607 static int eval_tok(void)
611 while (ecur
< elen
) {
612 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
614 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
615 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
616 ebuf
[ecur
- 1] != '/'))
624 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
625 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
630 if (isdigit(ebuf
[ecur
])) {
631 while (isdigit(ebuf
[ecur
]))
633 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
637 for (i
= 0; i
< ARRAY_SIZE(tok2
); i
++)
638 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
639 int ret
= TOK2(tok2
[i
]);
646 static int eval_see(void)
653 static int eval_get(void)
663 static long eval_num(void)
668 static int eval_jmp(int tok
)
670 if (eval_see() == tok
) {
677 static void eval_expect(int tok
)
682 static char *eval_id(void)
687 static long evalcexpr(void);
689 static long evalatom(void)
691 if (!eval_jmp(TOK_NUM
))
693 if (!eval_jmp(TOK_NAME
)) {
694 int parens
= !eval_jmp('(');
696 eval_expect(TOK_NAME
);
697 ret
= macro_find(eval_id()) >= 0;
702 if (!eval_jmp('(')) {
703 long ret
= evalcexpr();
710 static long evalpre(void)
721 static long evalmul(void)
723 long ret
= evalpre();
725 if (!eval_jmp('*')) {
729 if (!eval_jmp('/')) {
733 if (!eval_jmp('%')) {
742 static long evaladd(void)
744 long ret
= evalmul();
746 if (!eval_jmp('+')) {
750 if (!eval_jmp('-')) {
759 static long evalshift(void)
761 long ret
= evaladd();
763 if (!eval_jmp(TOK2("<<"))) {
767 if (!eval_jmp(TOK2(">>"))) {
776 static long evalcmp(void)
778 long ret
= evalshift();
780 if (!eval_jmp('<')) {
781 ret
= ret
< evalshift();
784 if (!eval_jmp('>')) {
785 ret
= ret
> evalshift();
788 if (!eval_jmp(TOK2("<="))) {
789 ret
= ret
<= evalshift();
792 if (!eval_jmp(TOK2(">="))) {
793 ret
= ret
>= evalshift();
801 static long evaleq(void)
803 long ret
= evalcmp();
805 if (!eval_jmp(TOK2("=="))) {
806 ret
= ret
== evalcmp();
809 if (!eval_jmp(TOK2("!="))) {
810 ret
= ret
!= evalcmp();
818 static long evalbitand(void)
821 while (!eval_jmp('&'))
826 static long evalxor(void)
828 long ret
= evalbitand();
829 while (!eval_jmp('^'))
834 static long evalbitor(void)
836 long ret
= evalxor();
837 while (!eval_jmp('|'))
842 static long evaland(void)
844 long ret
= evalbitor();
845 while (!eval_jmp(TOK2("&&")))
846 ret
= ret
&& evalbitor();
850 static long evalor(void)
852 long ret
= evaland();
853 while (!eval_jmp(TOK2("||")))
854 ret
= ret
|| evaland();
858 static long evalcexpr(void)
865 while (eval_get() != ':')
870 static long evalexpr(void)
876 static int buf_loc(char *s
, int off
)
880 while ((s
= strchr(s
, '\n')) && s
< e
) {
887 int cpp_loc(char *s
, long addr
)
891 for (i
= nbufs
- 1; i
> 0; i
--)
892 if (bufs
[i
].type
== BUF_FILE
)
894 if (addr
>= hunk_off
&& i
== nbufs
- 1)
895 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
897 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
898 sprintf(s
, "%s:%d: ", bufs
[i
].path
, line
);