2 * This is a really stupid C tokenizer. It doesn't do any include
3 * files or anything complex at all. That's the preprocessor.
5 * Copyright (C) 2003 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
26 int input_stream_nr
= 0;
27 struct stream
*input_streams
;
28 static int input_streams_allocated
;
29 unsigned int tabstop
= 8;
31 #define BUFSIZE (8192)
36 int newline
, whitespace
;
37 struct token
**tokenlist
;
39 unsigned char *buffer
;
42 const char *stream_name(int stream
)
44 if (stream
< 0 || stream
> input_stream_nr
)
45 return "<bad stream>";
46 return input_streams
[stream
].name
;
49 static struct position
stream_pos(stream_t
*stream
)
53 pos
.stream
= stream
->nr
;
54 pos
.newline
= stream
->newline
;
55 pos
.whitespace
= stream
->whitespace
;
56 pos
.pos
= stream
->pos
;
57 pos
.line
= stream
->line
;
62 const char *show_special(int val
)
64 static char buffer
[4];
68 if (val
>= SPECIAL_BASE
)
69 strcpy(buffer
, (char *) combinations
[val
- SPECIAL_BASE
]);
73 const char *show_ident(const struct ident
*ident
)
75 static char buffer
[256];
78 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
82 static char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
85 if (c
== escape
|| c
== '\\')
100 return ptr
+ sprintf(ptr
, "%o", c
);
102 return ptr
+ sprintf(ptr
, "%03o", c
);
105 const char *show_string(const struct string
*string
)
107 static char buffer
[4 * MAX_STRING
+ 3];
112 return "<bad_string>";
115 for (i
= 0; i
< string
->length
-1; i
++) {
116 const char *p
= string
->data
+ i
;
117 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
124 static const char *show_char(const char *s
, size_t len
, char prefix
, char delim
)
126 static char buffer
[MAX_STRING
+ 4];
138 static const char *quote_char(const char *s
, size_t len
, char prefix
, char delim
)
140 static char buffer
[2*MAX_STRING
+ 6];
148 for (i
= 0; i
< len
; i
++) {
149 if (s
[i
] == '"' || s
[i
] == '\\')
160 const char *show_token(const struct token
*token
)
162 static char buffer
[256];
166 switch (token_type(token
)) {
168 return "syntax error";
171 return "end-of-input";
174 return show_ident(token
->ident
);
177 return token
->number
;
180 return show_special(token
->special
);
183 return show_char(token
->string
->data
,
184 token
->string
->length
- 1, 0, '\'');
185 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
186 return show_char(token
->embedded
,
187 token_type(token
) - TOKEN_CHAR
, 0, '\'');
188 case TOKEN_WIDE_CHAR
:
189 return show_char(token
->string
->data
,
190 token
->string
->length
- 1, 'L', '\'');
191 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
192 return show_char(token
->embedded
,
193 token_type(token
) - TOKEN_WIDE_CHAR
, 'L', '\'');
195 return show_char(token
->string
->data
,
196 token
->string
->length
- 1, 0, '"');
197 case TOKEN_WIDE_STRING
:
198 return show_char(token
->string
->data
,
199 token
->string
->length
- 1, 'L', '"');
201 case TOKEN_STREAMBEGIN
:
202 sprintf(buffer
, "<beginning of '%s'>", stream_name(token
->pos
.stream
));
205 case TOKEN_STREAMEND
:
206 sprintf(buffer
, "<end of '%s'>", stream_name(token
->pos
.stream
));
210 sprintf(buffer
, "<untaint>");
213 case TOKEN_ARG_COUNT
:
214 sprintf(buffer
, "<argcnt>");
218 sprintf(buffer
, "unhandled token type '%d' ", token_type(token
));
223 const char *quote_token(const struct token
*token
)
225 static char buffer
[256];
227 switch (token_type(token
)) {
229 return "syntax error";
232 return show_ident(token
->ident
);
235 return token
->number
;
238 return show_special(token
->special
);
241 return quote_char(token
->string
->data
,
242 token
->string
->length
- 1, 0, '\'');
243 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
244 return quote_char(token
->embedded
,
245 token_type(token
) - TOKEN_CHAR
, 0, '\'');
246 case TOKEN_WIDE_CHAR
:
247 return quote_char(token
->string
->data
,
248 token
->string
->length
- 1, 'L', '\'');
249 case TOKEN_WIDE_CHAR_EMBEDDED_0
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
250 return quote_char(token
->embedded
,
251 token_type(token
) - TOKEN_WIDE_CHAR
, 'L', '\'');
253 return quote_char(token
->string
->data
,
254 token
->string
->length
- 1, 0, '"');
255 case TOKEN_WIDE_STRING
:
256 return quote_char(token
->string
->data
,
257 token
->string
->length
- 1, 'L', '"');
259 sprintf(buffer
, "unhandled token type '%d' ", token_type(token
));
264 #define HASHED_INPUT_BITS (6)
265 #define HASHED_INPUT (1 << HASHED_INPUT_BITS)
266 #define HASH_PRIME 0x9e370001UL
268 static int input_stream_hashes
[HASHED_INPUT
] = { [0 ... HASHED_INPUT
-1] = -1 };
270 int *hash_stream(const char *name
)
275 while ((c
= *name
++) != 0)
276 hash
= (hash
+ (c
<< 4) + (c
>> 4)) * 11;
279 hash
>>= 32 - HASHED_INPUT_BITS
;
280 return input_stream_hashes
+ hash
;
283 int init_stream(const char *name
, int fd
, const char **next_path
)
285 int stream
= input_stream_nr
, *hash
;
286 struct stream
*current
;
288 if (stream
>= input_streams_allocated
) {
289 int newalloc
= stream
* 4 / 3 + 10;
290 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
292 die("Unable to allocate more streams space");
293 input_streams_allocated
= newalloc
;
295 current
= input_streams
+ stream
;
296 memset(current
, 0, sizeof(*current
));
297 current
->name
= name
;
299 current
->next_path
= next_path
;
300 current
->path
= NULL
;
301 current
->constant
= CONSTANT_FILE_MAYBE
;
302 input_stream_nr
= stream
+1;
303 hash
= hash_stream(name
);
304 current
->next_stream
= *hash
;
309 static struct token
* alloc_token(stream_t
*stream
)
311 struct token
*token
= __alloc_token(0);
312 token
->pos
= stream_pos(stream
);
317 * Argh... That was surprisingly messy - handling '\r' complicates the
320 static int nextchar_slow(stream_t
*stream
)
322 int offset
= stream
->offset
;
323 int size
= stream
->size
;
325 int spliced
= 0, had_cr
, had_backslash
;
328 had_cr
= had_backslash
= 0;
331 if (offset
>= size
) {
334 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
338 stream
->offset
= offset
= 0;
341 c
= stream
->buffer
[offset
++];
351 if (!had_backslash
) {
354 stream
->pos
+= tabstop
- stream
->pos
% tabstop
;
379 stream
->offset
= offset
;
395 warning(stream_pos(stream
), "no newline at end of file");
397 warning(stream_pos(stream
), "backslash-newline at end of file");
402 * We want that as light as possible while covering all normal cases.
403 * Slow path (including the logics with line-splicing and EOF sanity
404 * checks) is in nextchar_slow().
406 static inline int nextchar(stream_t
*stream
)
408 int offset
= stream
->offset
;
410 if (offset
< stream
->size
) {
411 int c
= stream
->buffer
[offset
++];
412 static const char special
[256] = {
413 ['\t'] = 1, ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
416 stream
->offset
= offset
;
421 return nextchar_slow(stream
);
424 struct token eof_token_entry
;
426 static struct token
*mark_eof(stream_t
*stream
)
430 end
= alloc_token(stream
);
431 token_type(end
) = TOKEN_STREAMEND
;
432 end
->pos
.newline
= 1;
434 eof_token_entry
.next
= &eof_token_entry
;
435 eof_token_entry
.pos
.newline
= 1;
437 end
->next
= &eof_token_entry
;
438 *stream
->tokenlist
= end
;
439 stream
->tokenlist
= NULL
;
443 static void add_token(stream_t
*stream
)
445 struct token
*token
= stream
->token
;
447 stream
->token
= NULL
;
449 *stream
->tokenlist
= token
;
450 stream
->tokenlist
= &token
->next
;
453 static void drop_token(stream_t
*stream
)
455 stream
->newline
|= stream
->token
->pos
.newline
;
456 stream
->whitespace
|= stream
->token
->pos
.whitespace
;
457 stream
->token
= NULL
;
471 static const long cclass
[257] = {
472 ['0' + 1 ... '7' + 1] = Digit
| Hex
| Escape
, /* \<octal> */
473 ['8' + 1 ... '9' + 1] = Digit
| Hex
,
474 ['A' + 1 ... 'D' + 1] = Letter
| Hex
,
475 ['E' + 1] = Letter
| Hex
| Exp
, /* E<exp> */
476 ['F' + 1] = Letter
| Hex
,
477 ['G' + 1 ... 'O' + 1] = Letter
,
478 ['P' + 1] = Letter
| Exp
, /* P<exp> */
479 ['Q' + 1 ... 'Z' + 1] = Letter
,
480 ['a' + 1 ... 'b' + 1] = Letter
| Hex
| Escape
, /* \a, \b */
481 ['c' + 1 ... 'd' + 1] = Letter
| Hex
,
482 ['e' + 1] = Letter
| Hex
| Exp
| Escape
,/* \e, e<exp> */
483 ['f' + 1] = Letter
| Hex
| Escape
, /* \f */
484 ['g' + 1 ... 'm' + 1] = Letter
,
485 ['n' + 1] = Letter
| Escape
, /* \n */
487 ['p' + 1] = Letter
| Exp
, /* p<exp> */
489 ['r' + 1] = Letter
| Escape
, /* \r */
491 ['t' + 1] = Letter
| Escape
, /* \t */
493 ['v' + 1] = Letter
| Escape
, /* \v */
495 ['x' + 1] = Letter
| Escape
, /* \x<hex> */
496 ['y' + 1 ... 'z' + 1] = Letter
,
498 ['.' + 1] = Dot
| ValidSecond
,
499 ['=' + 1] = ValidSecond
,
500 ['+' + 1] = ValidSecond
,
501 ['-' + 1] = ValidSecond
,
502 ['>' + 1] = ValidSecond
,
503 ['<' + 1] = ValidSecond
,
504 ['&' + 1] = ValidSecond
,
505 ['|' + 1] = ValidSecond
,
506 ['#' + 1] = ValidSecond
,
507 ['\'' + 1] = Quote
| Escape
,
508 ['"' + 1] = Quote
| Escape
,
518 * pp-number identifier-nodigit
525 static int get_one_number(int c
, int next
, stream_t
*stream
)
528 static char buffer
[4095];
529 char *p
= buffer
, *buf
, *buffer_end
= buffer
+ sizeof (buffer
);
534 long class = cclass
[next
+ 1];
535 if (!(class & (Dot
| Digit
| Letter
)))
539 next
= nextchar(stream
);
541 if (next
== '-' || next
== '+') {
544 next
= nextchar(stream
);
549 if (p
== buffer_end
) {
550 sparse_error(stream_pos(stream
), "number token exceeds %td characters",
551 buffer_end
- buffer
);
552 // Pretend we saw just "1".
559 buf
= __alloc_bytes(len
);
560 memcpy(buf
, buffer
, len
);
562 token
= stream
->token
;
563 token_type(token
) = TOKEN_NUMBER
;
570 static int eat_string(int next
, stream_t
*stream
, enum token_type type
)
572 static char buffer
[MAX_STRING
];
573 struct string
*string
;
574 struct token
*token
= stream
->token
;
578 char delim
= type
< TOKEN_STRING
? '\'' : '"';
580 for (escape
= 0; escape
|| next
!= delim
; next
= nextchar(stream
)) {
581 if (len
< MAX_STRING
)
585 warning(stream_pos(stream
),
586 "Newline in string or character constant");
587 if (delim
== '\'') /* assume it's lost ' */
591 warning(stream_pos(stream
),
592 "End of file in middle of string");
596 if (want_hex
&& !(cclass
[next
+ 1] & Hex
))
597 warning(stream_pos(stream
),
598 "\\x used with no following hex digits");
600 escape
= next
== '\\';
602 if (!(cclass
[next
+ 1] & Escape
))
603 warning(stream_pos(stream
),
604 "Unknown escape '%c'", next
);
606 want_hex
= next
== 'x';
610 warning(stream_pos(stream
),
611 "\\x used with no following hex digits");
612 if (len
> MAX_STRING
) {
613 warning(stream_pos(stream
), "string too long (%d bytes, %d bytes max)", len
, MAX_STRING
);
616 if (delim
== '\'' && len
<= 4) {
618 sparse_error(stream_pos(stream
),
619 "empty character constant");
620 return nextchar(stream
);
622 token_type(token
) = type
+ len
;
623 memset(buffer
+ len
, '\0', 4 - len
);
624 memcpy(token
->embedded
, buffer
, 4);
626 token_type(token
) = type
;
627 string
= __alloc_string(len
+1);
628 memcpy(string
->data
, buffer
, len
);
629 string
->data
[len
] = '\0';
630 string
->length
= len
+1;
631 token
->string
= string
;
635 token
= stream
->token
;
637 return nextchar(stream
);
640 static int drop_stream_eoln(stream_t
*stream
)
644 switch (nextchar(stream
)) {
648 return nextchar(stream
);
653 static int drop_stream_comment(stream_t
*stream
)
658 newline
= stream
->newline
;
660 next
= nextchar(stream
);
664 warning(stream_pos(stream
), "End of file in the middle of a comment");
667 next
= nextchar(stream
);
668 if (curr
== '*' && next
== '/')
671 stream
->newline
= newline
;
672 return nextchar(stream
);
675 unsigned char combinations
[][4] = COMBINATION_STRINGS
;
677 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
679 /* hash function for two-character punctuators - all give unique values */
680 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
683 * note that we won't get false positives - special_hash(0,0) is 0 and
684 * entry 0 is filled (by +=), so all the missing ones are OK.
686 static unsigned char hash_results
[32][2] = {
687 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
688 RES('+', '='), /* 00 */
689 RES('/', '='), /* 01 */
690 RES('^', '='), /* 05 */
691 RES('&', '&'), /* 07 */
692 RES('#', '#'), /* 08 */
693 RES('<', '<'), /* 0a */
694 RES('<', '='), /* 0c */
695 RES('!', '='), /* 0e */
696 RES('%', '='), /* 0f */
697 RES('-', '-'), /* 10 */
698 RES('-', '='), /* 11 */
699 RES('-', '>'), /* 13 */
700 RES('=', '='), /* 15 */
701 RES('&', '='), /* 17 */
702 RES('*', '='), /* 18 */
703 RES('.', '.'), /* 1a */
704 RES('+', '+'), /* 1b */
705 RES('|', '='), /* 1c */
706 RES('>', '='), /* 1d */
707 RES('|', '|'), /* 1e */
708 RES('>', '>') /* 1f */
711 static int code
[32] = {
712 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
713 CODE('+', '=', SPECIAL_ADD_ASSIGN
), /* 00 */
714 CODE('/', '=', SPECIAL_DIV_ASSIGN
), /* 01 */
715 CODE('^', '=', SPECIAL_XOR_ASSIGN
), /* 05 */
716 CODE('&', '&', SPECIAL_LOGICAL_AND
), /* 07 */
717 CODE('#', '#', SPECIAL_HASHHASH
), /* 08 */
718 CODE('<', '<', SPECIAL_LEFTSHIFT
), /* 0a */
719 CODE('<', '=', SPECIAL_LTE
), /* 0c */
720 CODE('!', '=', SPECIAL_NOTEQUAL
), /* 0e */
721 CODE('%', '=', SPECIAL_MOD_ASSIGN
), /* 0f */
722 CODE('-', '-', SPECIAL_DECREMENT
), /* 10 */
723 CODE('-', '=', SPECIAL_SUB_ASSIGN
), /* 11 */
724 CODE('-', '>', SPECIAL_DEREFERENCE
), /* 13 */
725 CODE('=', '=', SPECIAL_EQUAL
), /* 15 */
726 CODE('&', '=', SPECIAL_AND_ASSIGN
), /* 17 */
727 CODE('*', '=', SPECIAL_MUL_ASSIGN
), /* 18 */
728 CODE('.', '.', SPECIAL_DOTDOT
), /* 1a */
729 CODE('+', '+', SPECIAL_INCREMENT
), /* 1b */
730 CODE('|', '=', SPECIAL_OR_ASSIGN
), /* 1c */
731 CODE('>', '=', SPECIAL_GTE
), /* 1d */
732 CODE('|', '|', SPECIAL_LOGICAL_OR
), /* 1e */
733 CODE('>', '>', SPECIAL_RIGHTSHIFT
) /* 1f */
737 static int get_one_special(int c
, stream_t
*stream
)
742 next
= nextchar(stream
);
745 * Check for numbers, strings, character constants, and comments
749 if (next
>= '0' && next
<= '9')
750 return get_one_number(c
, next
, stream
);
753 return eat_string(next
, stream
, TOKEN_STRING
);
755 return eat_string(next
, stream
, TOKEN_CHAR
);
758 return drop_stream_eoln(stream
);
760 return drop_stream_comment(stream
);
764 * Check for combinations
767 if (cclass
[next
+ 1] & ValidSecond
) {
768 i
= special_hash(c
, next
);
769 if (hash_results
[i
][0] == c
&& hash_results
[i
][1] == next
) {
771 next
= nextchar(stream
);
772 if (value
>= SPECIAL_LEFTSHIFT
&&
773 next
== "==."[value
- SPECIAL_LEFTSHIFT
]) {
775 next
= nextchar(stream
);
781 token
= stream
->token
;
782 token_type(token
) = TOKEN_SPECIAL
;
783 token
->special
= value
;
788 #define IDENT_HASH_BITS (13)
789 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
790 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
792 #define ident_hash_init(c) (c)
793 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
794 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
796 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
797 static int ident_hit
, ident_miss
, idents
;
799 void show_identifier_stats(void)
802 int distribution
[100];
804 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
805 ident_hit
, ident_miss
);
807 for (i
= 0; i
< 100; i
++)
810 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
811 struct ident
* ident
= hash_table
[i
];
820 distribution
[count
]++;
823 for (i
= 0; i
< 100; i
++) {
825 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
829 static struct ident
*alloc_ident(const char *name
, int len
)
831 struct ident
*ident
= __alloc_ident(len
);
832 ident
->symbols
= NULL
;
835 memcpy(ident
->name
, name
, len
);
839 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
841 ident
->next
= hash_table
[hash
];
842 hash_table
[hash
] = ident
;
847 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
852 p
= &hash_table
[hash
];
853 while ((ident
= *p
) != NULL
) {
854 if (ident
->len
== (unsigned char) len
) {
855 if (strncmp(name
, ident
->name
, len
) != 0)
865 ident
= alloc_ident(name
, len
);
873 static unsigned long hash_name(const char *name
, int len
)
876 const unsigned char *p
= (const unsigned char *)name
;
878 hash
= ident_hash_init(*p
++);
880 unsigned int i
= *p
++;
881 hash
= ident_hash_add(hash
, i
);
883 return ident_hash_end(hash
);
886 struct ident
*hash_ident(struct ident
*ident
)
888 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
891 struct ident
*built_in_ident(const char *name
)
893 int len
= strlen(name
);
894 return create_hashed_ident(name
, len
, hash_name(name
, len
));
897 struct token
*built_in_token(int stream
, const char *name
)
901 token
= __alloc_token(0);
902 token
->pos
.stream
= stream
;
903 token_type(token
) = TOKEN_IDENT
;
904 token
->ident
= built_in_ident(name
);
908 static int get_one_identifier(int c
, stream_t
*stream
)
917 hash
= ident_hash_init(c
);
920 next
= nextchar(stream
);
921 if (!(cclass
[next
+ 1] & (Letter
| Digit
)))
923 if (len
>= sizeof(buf
))
925 hash
= ident_hash_add(hash
, next
);
929 if (cclass
[next
+ 1] & Quote
) {
930 if (len
== 1 && buf
[0] == 'L') {
932 return eat_string(nextchar(stream
), stream
,
935 return eat_string(nextchar(stream
), stream
,
939 hash
= ident_hash_end(hash
);
940 ident
= create_hashed_ident(buf
, len
, hash
);
943 token
= stream
->token
;
944 token_type(token
) = TOKEN_IDENT
;
945 token
->ident
= ident
;
950 static int get_one_token(int c
, stream_t
*stream
)
952 long class = cclass
[c
+ 1];
954 return get_one_number(c
, nextchar(stream
), stream
);
956 return get_one_identifier(c
, stream
);
957 return get_one_special(c
, stream
);
960 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
961 unsigned char *buf
, unsigned int buf_size
)
968 stream
->whitespace
= 0;
971 stream
->token
= NULL
;
974 stream
->size
= buf_size
;
975 stream
->buffer
= buf
;
977 begin
= alloc_token(stream
);
978 token_type(begin
) = TOKEN_STREAMBEGIN
;
979 stream
->tokenlist
= &begin
->next
;
983 static struct token
*tokenize_stream(stream_t
*stream
)
985 int c
= nextchar(stream
);
988 struct token
*token
= alloc_token(stream
);
989 stream
->token
= token
;
991 stream
->whitespace
= 0;
992 c
= get_one_token(c
, stream
);
995 stream
->whitespace
= 1;
996 c
= nextchar(stream
);
998 return mark_eof(stream
);
1001 struct token
* tokenize_buffer(void *buffer
, unsigned long size
, struct token
**endtoken
)
1004 struct token
*begin
;
1006 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
1007 *endtoken
= tokenize_stream(&stream
);
1011 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
, const char **next_path
)
1013 struct token
*begin
, *end
;
1015 unsigned char buffer
[BUFSIZE
];
1018 idx
= init_stream(name
, fd
, next_path
);
1020 // info(endtoken->pos, "File %s is const", name);
1024 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
1025 end
= tokenize_stream(&stream
);
1027 end
->next
= endtoken
;