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 cpp_define("__STDC__", "");
143 cpp_define("__arm__", "");
144 cpp_define("__linux__", "");
145 return include_file(path
);
148 static void jumpws(void)
150 while (cur
< len
&& isspace(buf
[cur
]))
154 static void read_word(char *dst
)
157 while (cur
< len
&& (isalnum(buf
[cur
]) || buf
[cur
] == '_'))
162 static void jumpcomment(void)
164 while (++cur
< len
) {
165 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
172 static void read_tilleol(char *dst
)
174 while (cur
< len
&& isspace(buf
[cur
]) && buf
[cur
] != '\n')
176 while (cur
< len
&& buf
[cur
] != '\n') {
177 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n')
179 else if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
187 static char *putstr(char *d
, char *s
)
195 #define MAXLOCS (1 << 10)
197 static char *locs
[MAXLOCS
] = {"/usr/include"};
198 static int nlocs
= 1;
200 void cpp_addpath(char *s
)
205 static int include_find(char *name
, int std
)
208 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
213 s
= putstr(s
, locs
[i
]);
217 if (!include_file(path
))
223 static void jumpstr(void)
225 if (buf
[cur
] == '\'') {
226 while (cur
< len
&& buf
[++cur
] != '\'')
227 if (buf
[cur
] == '\\')
232 if (buf
[cur
] == '"') {
233 while (cur
< len
&& buf
[++cur
] != '"')
234 if (buf
[cur
] == '\\')
241 static void readarg(char *s
)
245 while (cur
< len
&& (depth
|| buf
[cur
] != ',' && buf
[cur
] != ')')) {
264 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
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
) ||
379 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
383 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
391 static int cpp_cmd(void)
396 if (!strcmp("define", cmd
)) {
400 if (!strcmp("undef", cmd
)) {
406 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
407 !strcmp("if", cmd
)) {
411 int not = cmd
[2] == 'n';
413 matched
= not ? macro_find(name
) < 0 :
414 macro_find(name
) >= 0;
416 matched
= cpp_eval();
422 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
426 if (!strcmp("endif", cmd
))
428 if (!strcmp("include", cmd
)) {
433 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
434 memcpy(file
, s
, e
- s
);
437 if (include_find(file
, *e
== '>') == -1)
438 die("cannot include file\n");
444 static int macro_arg(struct macro
*m
, char *arg
)
447 for (i
= 0; i
< m
->nargs
; i
++)
448 if (!strcmp(arg
, m
->args
[i
]))
453 static int buf_arg_find(char *name
)
456 for (i
= nbufs
- 1; i
>= 0; i
--) {
457 struct buf
*mbuf
= &bufs
[i
];
458 struct macro
*m
= mbuf
->macro
;
459 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
461 if (mbuf
->type
== BUF_ARG
)
467 static void macro_expand(void)
473 if ((mbuf
= buf_arg_find(name
)) >= 0) {
474 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
475 char *dat
= bufs
[mbuf
].args
[arg
];
479 m
= ¯os
[macro_find(name
)];
485 if (buf
[cur
] == '(') {
487 struct buf
*mbuf
= &bufs
[nbufs
];
490 while (cur
< len
&& buf
[cur
] != ')') {
491 readarg(mbuf
->args
[i
++]);
499 mbuf
->args
[i
++][0] = '\0';
505 static int buf_expanding(char *macro
)
508 for (i
= nbufs
- 1; i
>= 0; i
--) {
509 if (bufs
[i
].type
== BUF_ARG
)
511 if (bufs
[i
].type
== BUF_MACRO
&&
512 !strcmp(macro
, bufs
[i
].macro
->name
))
518 static int expandable(char *word
)
520 if (buf_arg_find(word
) >= 0)
522 return !buf_expanding(word
) && macro_find(word
) != -1;
525 void cpp_define(char *name
, char *def
)
527 char tmp_buf
[MACROLEN
];
532 buf_new(BUF_TEMP
, tmp_buf
, s
- tmp_buf
);
537 static int seen_macro
;
542 int cpp_read(char *s
)
550 struct buf
*cbuf
= &bufs
[nbufs
- 1];
553 if (cbuf
->type
& BUF_FILE
)
564 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
568 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
572 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
575 if (expandable(word
)) {
580 if (buf_iseval() && !strcmp("defined", word
)) {
583 if (buf
[cur
] == '(') {
597 memcpy(s
, buf
+ old
, cur
- old
);
600 hunk_off
+= hunk_len
;
601 hunk_len
= cur
- old
;
606 /* preprocessor constant expression evaluation */
608 static char etok
[NAMELEN
];
611 static char *tok2
[] = {
612 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
615 static int eval_tok(void)
619 while (ecur
< elen
) {
620 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
622 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
623 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
624 ebuf
[ecur
- 1] != '/'))
632 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
633 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
638 if (isdigit(ebuf
[ecur
])) {
639 while (isdigit(ebuf
[ecur
]))
641 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
645 for (i
= 0; i
< ARRAY_SIZE(tok2
); i
++)
646 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
647 int ret
= TOK2(tok2
[i
]);
654 static int eval_see(void)
661 static int eval_get(void)
671 static long eval_num(void)
676 static int eval_jmp(int tok
)
678 if (eval_see() == tok
) {
685 static void eval_expect(int tok
)
690 static char *eval_id(void)
695 static long evalcexpr(void);
697 static long evalatom(void)
699 if (!eval_jmp(TOK_NUM
))
701 if (!eval_jmp(TOK_NAME
)) {
702 int parens
= !eval_jmp('(');
704 eval_expect(TOK_NAME
);
705 ret
= macro_find(eval_id()) >= 0;
710 if (!eval_jmp('(')) {
711 long ret
= evalcexpr();
718 static long evalpre(void)
729 static long evalmul(void)
731 long ret
= evalpre();
733 if (!eval_jmp('*')) {
737 if (!eval_jmp('/')) {
741 if (!eval_jmp('%')) {
750 static long evaladd(void)
752 long ret
= evalmul();
754 if (!eval_jmp('+')) {
758 if (!eval_jmp('-')) {
767 static long evalshift(void)
769 long ret
= evaladd();
771 if (!eval_jmp(TOK2("<<"))) {
775 if (!eval_jmp(TOK2(">>"))) {
784 static long evalcmp(void)
786 long ret
= evalshift();
788 if (!eval_jmp('<')) {
789 ret
= ret
< evalshift();
792 if (!eval_jmp('>')) {
793 ret
= ret
> evalshift();
796 if (!eval_jmp(TOK2("<="))) {
797 ret
= ret
<= evalshift();
800 if (!eval_jmp(TOK2(">="))) {
801 ret
= ret
>= evalshift();
809 static long evaleq(void)
811 long ret
= evalcmp();
813 if (!eval_jmp(TOK2("=="))) {
814 ret
= ret
== evalcmp();
817 if (!eval_jmp(TOK2("!="))) {
818 ret
= ret
!= evalcmp();
826 static long evalbitand(void)
829 while (!eval_jmp('&'))
834 static long evalxor(void)
836 long ret
= evalbitand();
837 while (!eval_jmp('^'))
842 static long evalbitor(void)
844 long ret
= evalxor();
845 while (!eval_jmp('|'))
850 static long evaland(void)
852 long ret
= evalbitor();
853 while (!eval_jmp(TOK2("&&")))
854 ret
= ret
&& evalbitor();
858 static long evalor(void)
860 long ret
= evaland();
861 while (!eval_jmp(TOK2("||")))
862 ret
= ret
|| evaland();
866 static long evalcexpr(void)
873 while (eval_get() != ':')
878 static long evalexpr(void)
884 static int buf_loc(char *s
, int off
)
888 while ((s
= strchr(s
, '\n')) && s
< e
) {
895 int cpp_loc(char *s
, long addr
)
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(s
, "%s:%d: ", bufs
[i
].path
, line
);