18 #define MAXDEFS (1 << 12)
19 #define MACROLEN (1 << 10)
20 #define MAXARGS (1 << 5)
21 #define NBUCKET (MAXDEFS << 1)
26 char args
[MAXARGS
][NAMELEN
];
31 /* macro hash table */
32 static struct tab mtab
;
34 #define MAXBUFS (1 << 5)
50 char args
[MAXARGS
][MACROLEN
]; /* arguments passed to a macro */
52 int arg_buf
; /* the bufs index of the owning macro */
56 void die(char *fmt
, ...)
61 vsprintf(msg
, fmt
, ap
);
63 write(2, msg
, strlen(msg
));
67 static void buf_new(int type
, char *dat
, int dlen
)
70 bufs
[nbufs
- 1].buf
= buf
;
71 bufs
[nbufs
- 1].cur
= cur
;
72 bufs
[nbufs
- 1].len
= len
;
75 die("nomem: MAXBUFS reached!\n");
80 bufs
[nbufs
- 1].type
= type
;
83 static void buf_file(char *path
, char *dat
, int dlen
)
85 buf_new(BUF_FILE
, dat
, dlen
);
86 strcpy(bufs
[nbufs
- 1].path
, path
? path
: "");
89 static void buf_macro(struct macro
*m
)
91 buf_new(BUF_MACRO
, m
->def
, strlen(m
->def
));
92 bufs
[nbufs
- 1].macro
= m
;
95 static void buf_arg(char *arg
, int mbuf
)
97 buf_new(BUF_ARG
, arg
, strlen(arg
));
98 bufs
[nbufs
- 1].arg_buf
= mbuf
;
101 static void buf_pop(void)
105 cur
= bufs
[nbufs
- 1].cur
;
106 len
= bufs
[nbufs
- 1].len
;
107 buf
= bufs
[nbufs
- 1].buf
;
111 static int buf_iseval(void)
114 for (i
= nbufs
- 1; i
>= 0; i
--)
115 if (bufs
[i
].type
== BUF_EVAL
)
120 static size_t file_size(int fd
)
128 static int include_file(char *path
)
130 int fd
= open(path
, O_RDONLY
);
136 size
= file_size(fd
) + 1;
138 while ((n
= read(fd
, dat
+ nr
, size
- nr
)) > 0)
142 buf_file(path
, dat
, nr
);
146 int cpp_init(char *path
)
148 return include_file(path
);
151 static void jumpws(void)
153 while (cur
< len
&& isspace(buf
[cur
]))
157 static void read_word(char *dst
)
160 while (cur
< len
&& (isalnum(buf
[cur
]) || buf
[cur
] == '_'))
165 static int jumpcomment(void)
167 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
168 while (++cur
< len
) {
169 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
175 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '/') {
177 if (buf
[cur
] == '\n')
184 static int jumpstr(void)
186 if (buf
[cur
] == '\'') {
187 while (cur
< len
&& buf
[++cur
] != '\'')
188 if (buf
[cur
] == '\\')
193 if (buf
[cur
] == '"') {
194 while (cur
< len
&& buf
[++cur
] != '"')
195 if (buf
[cur
] == '\\')
203 static void read_tilleol(char *dst
)
205 while (cur
< len
&& isspace(buf
[cur
]) && buf
[cur
] != '\n')
207 while (cur
< len
&& buf
[cur
] != '\n') {
209 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n') {
211 } else if (jumpcomment()) {
213 } else if (jumpstr()) {
214 memcpy(dst
, buf
+ last
, cur
- last
);
221 static char *putstr(char *d
, char *s
)
229 #define MAXLOCS (1 << 10)
231 static char *locs
[MAXLOCS
] = {};
232 static int nlocs
= 0;
234 void cpp_addpath(char *s
)
239 static int include_find(char *name
, int std
)
242 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
247 s
= putstr(s
, locs
[i
]);
251 if (!include_file(path
))
257 static void readarg(char *s
)
261 while (cur
< len
&& (depth
|| buf
[cur
] != ',' && buf
[cur
] != ')')) {
262 if (!jumpstr() || !jumpcomment())
264 switch (buf
[cur
++]) {
278 memcpy(s
, buf
+ beg
, cur
- beg
);
283 static int macro_find(char *name
)
285 char *n
= tab_get(&mtab
, name
);
288 return container(n
, struct macro
, name
) - macros
;
291 static void macro_undef(char *name
)
293 int i
= macro_find(name
);
295 tab_del(&mtab
, macros
[i
].name
);
298 static int macro_new(char *name
)
300 int i
= macro_find(name
);
303 if (nmacros
>= MAXDEFS
)
304 die("nomem: MAXDEFS reached!\n");
306 strcpy(macros
[i
].name
, name
);
307 tab_add(&mtab
, macros
[i
].name
);
311 static void macro_define(void)
316 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 int cpp_read(char *buf
);
338 static char ebuf
[BUFSIZE
];
342 static long evalexpr(void);
344 static int cpp_eval(void)
348 char evalbuf
[BUFSIZE
];
349 read_tilleol(evalbuf
);
350 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
354 while (bufid
< nbufs
|| (bufid
== nbufs
&& cur
< len
))
355 elen
+= cpp_read(ebuf
+ elen
);
361 static void jumpifs(int jumpelse
)
365 if (buf
[cur
] == '#') {
369 if (!strcmp("else", cmd
))
370 if (!depth
&& !jumpelse
)
372 if (!strcmp("elif", cmd
))
373 if (!depth
&& !jumpelse
&& cpp_eval())
375 if (!strcmp("endif", cmd
)) {
381 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
394 static int cpp_cmd(void)
399 if (!strcmp("define", cmd
)) {
403 if (!strcmp("undef", cmd
)) {
409 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
410 !strcmp("if", cmd
)) {
414 int not = cmd
[2] == 'n';
416 matched
= not ? macro_find(name
) < 0 :
417 macro_find(name
) >= 0;
419 matched
= cpp_eval();
425 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
429 if (!strcmp("endif", cmd
))
431 if (!strcmp("include", cmd
)) {
436 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
437 memcpy(file
, s
, e
- s
);
440 if (include_find(file
, *e
== '>') == -1)
441 die("cannot include <%s>\n", file
);
447 static int macro_arg(struct macro
*m
, char *arg
)
450 for (i
= 0; i
< m
->nargs
; i
++)
451 if (!strcmp(arg
, m
->args
[i
]))
456 static int buf_arg_find(char *name
)
459 for (i
= nbufs
- 1; i
>= 0; i
--) {
460 struct buf
*mbuf
= &bufs
[i
];
461 struct macro
*m
= mbuf
->macro
;
462 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
464 if (mbuf
->type
== BUF_ARG
)
470 static void macro_expand(void)
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
)];
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 static int expandable(char *word
)
523 if (buf_arg_find(word
) >= 0)
525 return !buf_expanding(word
) && macro_find(word
) != -1;
528 void cpp_define(char *name
, char *def
)
530 char tmp_buf
[MACROLEN
];
535 buf_new(BUF_TEMP
, tmp_buf
, s
- tmp_buf
);
540 static int seen_macro
;
545 int cpp_read(char *s
)
553 struct buf
*cbuf
= &bufs
[nbufs
- 1];
556 if (cbuf
->type
& BUF_FILE
)
571 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
574 if (expandable(word
)) {
579 if (buf_iseval() && !strcmp("defined", word
)) {
582 if (buf
[cur
] == '(') {
596 memcpy(s
, buf
+ old
, cur
- old
);
599 hunk_off
+= hunk_len
;
600 hunk_len
= cur
- old
;
605 /* preprocessor constant expression evaluation */
607 static char etok
[NAMELEN
];
610 static char *tok2
[] = {
611 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
614 static int eval_tok(void)
618 while (ecur
< elen
) {
619 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
621 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
622 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
623 ebuf
[ecur
- 1] != '/'))
631 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
632 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
637 if (isdigit(ebuf
[ecur
])) {
638 while (isdigit(ebuf
[ecur
]))
640 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
644 for (i
= 0; i
< ARRAY_SIZE(tok2
); i
++)
645 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
646 int ret
= TOK2(tok2
[i
]);
653 static int eval_see(void)
660 static int eval_get(void)
670 static long eval_num(void)
675 static int eval_jmp(int tok
)
677 if (eval_see() == tok
) {
684 static void eval_expect(int tok
)
689 static char *eval_id(void)
694 static long evalcexpr(void);
696 static long evalatom(void)
698 if (!eval_jmp(TOK_NUM
))
700 if (!eval_jmp(TOK_NAME
)) {
701 int parens
= !eval_jmp('(');
703 eval_expect(TOK_NAME
);
704 ret
= macro_find(eval_id()) >= 0;
709 if (!eval_jmp('(')) {
710 long ret
= evalcexpr();
717 static long evalpre(void)
728 static long evalmul(void)
730 long ret
= evalpre();
732 if (!eval_jmp('*')) {
736 if (!eval_jmp('/')) {
740 if (!eval_jmp('%')) {
749 static long evaladd(void)
751 long ret
= evalmul();
753 if (!eval_jmp('+')) {
757 if (!eval_jmp('-')) {
766 static long evalshift(void)
768 long ret
= evaladd();
770 if (!eval_jmp(TOK2("<<"))) {
774 if (!eval_jmp(TOK2(">>"))) {
783 static long evalcmp(void)
785 long ret
= evalshift();
787 if (!eval_jmp('<')) {
788 ret
= ret
< evalshift();
791 if (!eval_jmp('>')) {
792 ret
= ret
> evalshift();
795 if (!eval_jmp(TOK2("<="))) {
796 ret
= ret
<= evalshift();
799 if (!eval_jmp(TOK2(">="))) {
800 ret
= ret
>= evalshift();
808 static long evaleq(void)
810 long ret
= evalcmp();
812 if (!eval_jmp(TOK2("=="))) {
813 ret
= ret
== evalcmp();
816 if (!eval_jmp(TOK2("!="))) {
817 ret
= ret
!= evalcmp();
825 static long evalbitand(void)
828 while (!eval_jmp('&'))
833 static long evalxor(void)
835 long ret
= evalbitand();
836 while (!eval_jmp('^'))
841 static long evalbitor(void)
843 long ret
= evalxor();
844 while (!eval_jmp('|'))
849 static long evaland(void)
851 long ret
= evalbitor();
852 while (!eval_jmp(TOK2("&&")))
853 ret
= ret
&& evalbitor();
857 static long evalor(void)
859 long ret
= evaland();
860 while (!eval_jmp(TOK2("||")))
861 ret
= ret
|| evaland();
865 static long evalcexpr(void)
872 while (eval_get() != ':')
877 static long evalexpr(void)
883 static int buf_loc(char *s
, int off
)
887 while ((s
= strchr(s
, '\n')) && s
< e
) {
894 char *cpp_loc(long addr
)
896 static char loc
[256];
899 for (i
= nbufs
- 1; i
> 0; i
--)
900 if (bufs
[i
].type
== BUF_FILE
)
902 if (addr
>= hunk_off
&& i
== nbufs
- 1)
903 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
905 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
906 sprintf(loc
, "%s:%d", bufs
[i
].path
, line
);