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())
271 memcpy(s
, buf
+ beg
, cur
- beg
);
276 static int macro_find(char *name
)
278 char *n
= tab_get(&mtab
, name
);
281 return container(n
, struct macro
, name
) - macros
;
284 static void macro_undef(char *name
)
286 int i
= macro_find(name
);
288 tab_del(&mtab
, macros
[i
].name
);
291 static int macro_new(char *name
)
293 int i
= macro_find(name
);
296 if (nmacros
>= MAXDEFS
)
297 die("nomem: MAXDEFS reached!\n");
299 strcpy(macros
[i
].name
, name
);
300 tab_add(&mtab
, macros
[i
].name
);
304 static void macro_define(void)
309 d
= ¯os
[macro_new(name
)];
312 if (buf
[cur
] == '(') {
315 while (cur
< len
&& buf
[cur
] != ')') {
316 readarg(d
->args
[d
->nargs
++]);
326 read_tilleol(d
->def
);
329 int cpp_read(char *buf
);
331 static char ebuf
[BUFSIZE
];
335 static long evalexpr(void);
337 static int cpp_eval(void)
341 char evalbuf
[BUFSIZE
];
342 read_tilleol(evalbuf
);
343 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
347 while (bufid
< nbufs
|| (bufid
== nbufs
&& cur
< len
))
348 elen
+= cpp_read(ebuf
+ elen
);
354 static void jumpifs(int jumpelse
)
358 if (buf
[cur
] == '#') {
362 if (!strcmp("else", cmd
))
363 if (!depth
&& !jumpelse
)
365 if (!strcmp("elif", cmd
))
366 if (!depth
&& !jumpelse
&& cpp_eval())
368 if (!strcmp("endif", cmd
)) {
374 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
387 static int cpp_cmd(void)
392 if (!strcmp("define", cmd
)) {
396 if (!strcmp("undef", cmd
)) {
402 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
403 !strcmp("if", cmd
)) {
407 int not = cmd
[2] == 'n';
409 matched
= not ? macro_find(name
) < 0 :
410 macro_find(name
) >= 0;
412 matched
= cpp_eval();
418 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
422 if (!strcmp("endif", cmd
))
424 if (!strcmp("include", cmd
)) {
429 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
430 memcpy(file
, s
, e
- s
);
433 if (include_find(file
, *e
== '>') == -1)
434 die("cannot include file\n");
440 static int macro_arg(struct macro
*m
, char *arg
)
443 for (i
= 0; i
< m
->nargs
; i
++)
444 if (!strcmp(arg
, m
->args
[i
]))
449 static int buf_arg_find(char *name
)
452 for (i
= nbufs
- 1; i
>= 0; i
--) {
453 struct buf
*mbuf
= &bufs
[i
];
454 struct macro
*m
= mbuf
->macro
;
455 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
457 if (mbuf
->type
== BUF_ARG
)
463 static void macro_expand(void)
469 if ((mbuf
= buf_arg_find(name
)) >= 0) {
470 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
471 char *dat
= bufs
[mbuf
].args
[arg
];
475 m
= ¯os
[macro_find(name
)];
481 if (buf
[cur
] == '(') {
483 struct buf
*mbuf
= &bufs
[nbufs
];
486 while (cur
< len
&& buf
[cur
] != ')') {
487 readarg(mbuf
->args
[i
++]);
495 mbuf
->args
[i
++][0] = '\0';
501 static int buf_expanding(char *macro
)
504 for (i
= nbufs
- 1; i
>= 0; i
--) {
505 if (bufs
[i
].type
== BUF_ARG
)
507 if (bufs
[i
].type
== BUF_MACRO
&&
508 !strcmp(macro
, bufs
[i
].macro
->name
))
514 static int expandable(char *word
)
516 if (buf_arg_find(word
) >= 0)
518 return !buf_expanding(word
) && macro_find(word
) != -1;
521 void cpp_define(char *name
, char *def
)
523 char tmp_buf
[MACROLEN
];
528 buf_new(BUF_TEMP
, tmp_buf
, s
- tmp_buf
);
533 static int seen_macro
;
538 int cpp_read(char *s
)
546 struct buf
*cbuf
= &bufs
[nbufs
- 1];
549 if (cbuf
->type
& BUF_FILE
)
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
);