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 */
57 write(2, msg
, strlen(msg
));
61 static void buf_new(int type
, char *dat
, int dlen
)
64 bufs
[nbufs
- 1].buf
= buf
;
65 bufs
[nbufs
- 1].cur
= cur
;
66 bufs
[nbufs
- 1].len
= len
;
69 die("nomem: MAXBUFS reached!\n");
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)
99 cur
= bufs
[nbufs
- 1].cur
;
100 len
= bufs
[nbufs
- 1].len
;
101 buf
= bufs
[nbufs
- 1].buf
;
105 static int buf_iseval(void)
108 for (i
= nbufs
- 1; i
>= 0; i
--)
109 if (bufs
[i
].type
== BUF_EVAL
)
114 static size_t file_size(int fd
)
122 static int include_file(char *path
)
124 int fd
= open(path
, O_RDONLY
);
130 size
= file_size(fd
) + 1;
132 while ((n
= read(fd
, dat
+ nr
, size
- nr
)) > 0)
136 buf_file(path
, dat
, nr
);
140 int cpp_init(char *path
)
142 return include_file(path
);
145 static void jumpws(void)
147 while (cur
< len
&& isspace(buf
[cur
]))
151 static void read_word(char *dst
)
154 while (cur
< len
&& (isalnum(buf
[cur
]) || buf
[cur
] == '_'))
159 static int jumpcomment(void)
161 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
162 while (++cur
< len
) {
163 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
169 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '/') {
171 if (buf
[cur
] == '\n')
178 static void read_tilleol(char *dst
)
180 while (cur
< len
&& isspace(buf
[cur
]) && buf
[cur
] != '\n')
182 while (cur
< len
&& buf
[cur
] != '\n') {
183 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n')
185 else if (jumpcomment())
191 static char *putstr(char *d
, char *s
)
199 #define MAXLOCS (1 << 10)
201 static char *locs
[MAXLOCS
] = {"/usr/include"};
202 static int nlocs
= 1;
204 void cpp_addpath(char *s
)
209 static int include_find(char *name
, int std
)
212 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
217 s
= putstr(s
, locs
[i
]);
221 if (!include_file(path
))
227 static int jumpstr(void)
229 if (buf
[cur
] == '\'') {
230 while (cur
< len
&& buf
[++cur
] != '\'')
231 if (buf
[cur
] == '\\')
236 if (buf
[cur
] == '"') {
237 while (cur
< len
&& buf
[++cur
] != '"')
238 if (buf
[cur
] == '\\')
246 static void readarg(char *s
)
250 while (cur
< len
&& (depth
|| buf
[cur
] != ',' && buf
[cur
] != ')')) {
251 if (!jumpstr() || !jumpcomment())
253 switch (buf
[cur
++]) {
267 memcpy(s
, buf
+ beg
, cur
- beg
);
272 static int macro_find(char *name
)
274 char *n
= tab_get(&mtab
, name
);
277 return container(n
, struct macro
, name
) - macros
;
280 static void macro_undef(char *name
)
282 int i
= macro_find(name
);
284 tab_del(&mtab
, macros
[i
].name
);
287 static int macro_new(char *name
)
289 int i
= macro_find(name
);
292 if (nmacros
>= MAXDEFS
)
293 die("nomem: MAXDEFS reached!\n");
295 strcpy(macros
[i
].name
, name
);
296 tab_add(&mtab
, macros
[i
].name
);
300 static void macro_define(void)
305 d
= ¯os
[macro_new(name
)];
308 if (buf
[cur
] == '(') {
311 while (cur
< len
&& buf
[cur
] != ')') {
312 readarg(d
->args
[d
->nargs
++]);
322 read_tilleol(d
->def
);
325 int cpp_read(char *buf
);
327 static char ebuf
[BUFSIZE
];
331 static long evalexpr(void);
333 static int cpp_eval(void)
337 char evalbuf
[BUFSIZE
];
338 read_tilleol(evalbuf
);
339 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
343 while (bufid
< nbufs
|| (bufid
== nbufs
&& cur
< len
))
344 elen
+= cpp_read(ebuf
+ elen
);
350 static void jumpifs(int jumpelse
)
354 if (buf
[cur
] == '#') {
358 if (!strcmp("else", cmd
))
359 if (!depth
&& !jumpelse
)
361 if (!strcmp("elif", cmd
))
362 if (!depth
&& !jumpelse
&& cpp_eval())
364 if (!strcmp("endif", cmd
)) {
370 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
383 static int 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");
436 static int macro_arg(struct macro
*m
, char *arg
)
439 for (i
= 0; i
< m
->nargs
; i
++)
440 if (!strcmp(arg
, m
->args
[i
]))
445 static int buf_arg_find(char *name
)
448 for (i
= nbufs
- 1; i
>= 0; i
--) {
449 struct buf
*mbuf
= &bufs
[i
];
450 struct macro
*m
= mbuf
->macro
;
451 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
453 if (mbuf
->type
== BUF_ARG
)
459 static void macro_expand(void)
465 if ((mbuf
= buf_arg_find(name
)) >= 0) {
466 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
467 char *dat
= bufs
[mbuf
].args
[arg
];
471 m
= ¯os
[macro_find(name
)];
477 if (buf
[cur
] == '(') {
479 struct buf
*mbuf
= &bufs
[nbufs
];
482 while (cur
< len
&& buf
[cur
] != ')') {
483 readarg(mbuf
->args
[i
++]);
491 mbuf
->args
[i
++][0] = '\0';
497 static int buf_expanding(char *macro
)
500 for (i
= nbufs
- 1; i
>= 0; i
--) {
501 if (bufs
[i
].type
== BUF_ARG
)
503 if (bufs
[i
].type
== BUF_MACRO
&&
504 !strcmp(macro
, bufs
[i
].macro
->name
))
510 static int expandable(char *word
)
512 if (buf_arg_find(word
) >= 0)
514 return !buf_expanding(word
) && macro_find(word
) != -1;
517 void cpp_define(char *name
, char *def
)
519 char tmp_buf
[MACROLEN
];
524 buf_new(BUF_TEMP
, tmp_buf
, s
- tmp_buf
);
529 static int seen_macro
;
534 int cpp_read(char *s
)
542 struct buf
*cbuf
= &bufs
[nbufs
- 1];
545 if (cbuf
->type
& BUF_FILE
)
560 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
563 if (expandable(word
)) {
568 if (buf_iseval() && !strcmp("defined", word
)) {
571 if (buf
[cur
] == '(') {
585 memcpy(s
, buf
+ old
, cur
- old
);
588 hunk_off
+= hunk_len
;
589 hunk_len
= cur
- old
;
594 /* preprocessor constant expression evaluation */
596 static char etok
[NAMELEN
];
599 static char *tok2
[] = {
600 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
603 static int eval_tok(void)
607 while (ecur
< elen
) {
608 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
610 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
611 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
612 ebuf
[ecur
- 1] != '/'))
620 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
621 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
626 if (isdigit(ebuf
[ecur
])) {
627 while (isdigit(ebuf
[ecur
]))
629 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
633 for (i
= 0; i
< ARRAY_SIZE(tok2
); i
++)
634 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
635 int ret
= TOK2(tok2
[i
]);
642 static int eval_see(void)
649 static int eval_get(void)
659 static long eval_num(void)
664 static int eval_jmp(int tok
)
666 if (eval_see() == tok
) {
673 static void eval_expect(int tok
)
678 static char *eval_id(void)
683 static long evalcexpr(void);
685 static long evalatom(void)
687 if (!eval_jmp(TOK_NUM
))
689 if (!eval_jmp(TOK_NAME
)) {
690 int parens
= !eval_jmp('(');
692 eval_expect(TOK_NAME
);
693 ret
= macro_find(eval_id()) >= 0;
698 if (!eval_jmp('(')) {
699 long ret
= evalcexpr();
706 static long evalpre(void)
717 static long evalmul(void)
719 long ret
= evalpre();
721 if (!eval_jmp('*')) {
725 if (!eval_jmp('/')) {
729 if (!eval_jmp('%')) {
738 static long evaladd(void)
740 long ret
= evalmul();
742 if (!eval_jmp('+')) {
746 if (!eval_jmp('-')) {
755 static long evalshift(void)
757 long ret
= evaladd();
759 if (!eval_jmp(TOK2("<<"))) {
763 if (!eval_jmp(TOK2(">>"))) {
772 static long evalcmp(void)
774 long ret
= evalshift();
776 if (!eval_jmp('<')) {
777 ret
= ret
< evalshift();
780 if (!eval_jmp('>')) {
781 ret
= ret
> evalshift();
784 if (!eval_jmp(TOK2("<="))) {
785 ret
= ret
<= evalshift();
788 if (!eval_jmp(TOK2(">="))) {
789 ret
= ret
>= evalshift();
797 static long evaleq(void)
799 long ret
= evalcmp();
801 if (!eval_jmp(TOK2("=="))) {
802 ret
= ret
== evalcmp();
805 if (!eval_jmp(TOK2("!="))) {
806 ret
= ret
!= evalcmp();
814 static long evalbitand(void)
817 while (!eval_jmp('&'))
822 static long evalxor(void)
824 long ret
= evalbitand();
825 while (!eval_jmp('^'))
830 static long evalbitor(void)
832 long ret
= evalxor();
833 while (!eval_jmp('|'))
838 static long evaland(void)
840 long ret
= evalbitor();
841 while (!eval_jmp(TOK2("&&")))
842 ret
= ret
&& evalbitor();
846 static long evalor(void)
848 long ret
= evaland();
849 while (!eval_jmp(TOK2("||")))
850 ret
= ret
|| evaland();
854 static long evalcexpr(void)
861 while (eval_get() != ':')
866 static long evalexpr(void)
872 static int buf_loc(char *s
, int off
)
876 while ((s
= strchr(s
, '\n')) && s
< e
) {
883 int cpp_loc(char *s
, long addr
)
887 for (i
= nbufs
- 1; i
> 0; i
--)
888 if (bufs
[i
].type
== BUF_FILE
)
890 if (addr
>= hunk_off
&& i
== nbufs
- 1)
891 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
893 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
894 sprintf(s
, "%s:%d: ", bufs
[i
].path
, line
);