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;
32 #define BUFSIZE (8192)
37 int newline
, whitespace
;
38 struct token
**tokenlist
;
40 unsigned char *buffer
;
43 const char *stream_name(int stream
)
45 if (stream
< 0 || stream
> input_stream_nr
)
46 return "<bad stream>";
47 return input_streams
[stream
].name
;
50 static struct position
stream_pos(stream_t
*stream
)
54 pos
.stream
= stream
->nr
;
55 pos
.newline
= stream
->newline
;
56 pos
.whitespace
= stream
->whitespace
;
57 pos
.pos
= stream
->pos
;
59 pos
.line
= stream
->line
;
67 const char *show_special(int val
)
69 static char buffer
[4];
73 if (val
>= SPECIAL_BASE
)
74 strcpy(buffer
, (char *) combinations
[val
- SPECIAL_BASE
]);
78 const char *show_ident(const struct ident
*ident
)
80 static char buffer
[256];
83 sprintf(buffer
, "%.*s", ident
->len
, ident
->name
);
87 static char *charstr(char *ptr
, unsigned char c
, unsigned char escape
, unsigned char next
)
90 if (c
== escape
|| c
== '\\')
105 return ptr
+ sprintf(ptr
, "%o", c
);
107 return ptr
+ sprintf(ptr
, "%03o", c
);
110 const char *show_string(const struct string
*string
)
112 static char buffer
[4 * MAX_STRING
+ 3];
117 return "<bad_string>";
120 for (i
= 0; i
< string
->length
-1; i
++) {
121 const char *p
= string
->data
+ i
;
122 ptr
= charstr(ptr
, p
[0], '"', p
[1]);
129 const char *show_token(const struct token
*token
)
131 static char buffer
[256];
135 switch (token_type(token
)) {
137 return "syntax error";
140 return "end-of-input";
143 return show_ident(token
->ident
);
146 case TOKEN_WIDE_STRING
:
147 return show_string(token
->string
);
150 return token
->number
;
153 return show_special(token
->special
);
156 case TOKEN_WIDE_CHAR
: {
158 int c
= token
->character
;
160 ptr
= charstr(ptr
, c
, '\'', 0);
166 case TOKEN_STREAMBEGIN
:
167 sprintf(buffer
, "<beginning of '%s'>", stream_name(token
->pos
.stream
));
170 case TOKEN_STREAMEND
:
171 sprintf(buffer
, "<end of '%s'>", stream_name(token
->pos
.stream
));
175 sprintf(buffer
, "<untaint>");
178 case TOKEN_ARG_COUNT
:
179 sprintf(buffer
, "<argcnt>");
183 sprintf(buffer
, "unhandled token type '%d' ", token_type(token
));
188 #define HASHED_INPUT_BITS (6)
189 #define HASHED_INPUT (1 << HASHED_INPUT_BITS)
190 #define HASH_PRIME 0x9e370001UL
192 static int input_stream_hashes
[HASHED_INPUT
] = { [0 ... HASHED_INPUT
-1] = -1 };
194 int *hash_stream(const char *name
)
199 while ((c
= *name
++) != 0)
200 hash
= (hash
+ (c
<< 4) + (c
>> 4)) * 11;
203 hash
>>= 32 - HASHED_INPUT_BITS
;
204 return input_stream_hashes
+ hash
;
207 int init_stream(const char *name
, int fd
, const char **next_path
)
209 int stream
= input_stream_nr
, *hash
;
210 struct stream
*current
;
212 if (stream
>= input_streams_allocated
) {
213 int newalloc
= stream
* 4 / 3 + 10;
214 input_streams
= realloc(input_streams
, newalloc
* sizeof(struct stream
));
216 die("Unable to allocate more streams space");
217 input_streams_allocated
= newalloc
;
219 current
= input_streams
+ stream
;
220 memset(current
, 0, sizeof(*current
));
221 current
->name
= name
;
223 current
->next_path
= next_path
;
224 current
->path
= NULL
;
225 current
->constant
= CONSTANT_FILE_MAYBE
;
226 input_stream_nr
= stream
+1;
227 hash
= hash_stream(name
);
228 current
->next_stream
= *hash
;
233 static struct token
* alloc_token(stream_t
*stream
)
235 struct token
*token
= __alloc_token(0);
236 token
->pos
= stream_pos(stream
);
241 * Argh... That was surprisingly messy - handling '\r' complicates the
244 static int nextchar_slow(stream_t
*stream
)
246 int offset
= stream
->offset
;
247 int size
= stream
->size
;
249 int spliced
= 0, had_cr
, had_backslash
, complain
;
252 had_cr
= had_backslash
= complain
= 0;
255 if (offset
>= size
) {
258 size
= read(stream
->fd
, stream
->buffer
, BUFSIZE
);
262 stream
->offset
= offset
= 0;
265 c
= stream
->buffer
[offset
++];
267 if (had_cr
&& c
!= '\n')
275 stream
->pos
+= (c
== '\t') ? (tabstop
- stream
->pos
% tabstop
) : 1;
282 if (!had_backslash
) {
292 warning(stream_pos(stream
), "non-ASCII data stream");
302 stream
->offset
= offset
;
304 warning(stream_pos(stream
), "non-ASCII data stream");
314 warning(stream_pos(stream
), "no newline at end of file");
316 warning(stream_pos(stream
), "non-ASCII data stream");
318 warning(stream_pos(stream
), "backslash-newline at end of file");
323 * We want that as light as possible while covering all normal cases.
324 * Slow path (including the logics with line-splicing and EOF sanity
325 * checks) is in nextchar_slow().
327 static inline int nextchar(stream_t
*stream
)
329 int offset
= stream
->offset
;
331 if (offset
< stream
->size
) {
332 int c
= stream
->buffer
[offset
++];
333 static const char special
[256] = {
334 ['\t'] = 1, ['\r'] = 1, ['\n'] = 1, ['\\'] = 1
337 stream
->offset
= offset
;
342 return nextchar_slow(stream
);
345 struct token eof_token_entry
;
347 static struct token
*mark_eof(stream_t
*stream
)
351 end
= alloc_token(stream
);
352 token_type(end
) = TOKEN_STREAMEND
;
353 end
->pos
.newline
= 1;
355 eof_token_entry
.next
= &eof_token_entry
;
356 eof_token_entry
.pos
.newline
= 1;
358 end
->next
= &eof_token_entry
;
359 *stream
->tokenlist
= end
;
360 stream
->tokenlist
= NULL
;
364 static void add_token(stream_t
*stream
)
366 struct token
*token
= stream
->token
;
368 stream
->token
= NULL
;
370 *stream
->tokenlist
= token
;
371 stream
->tokenlist
= &token
->next
;
374 static void drop_token(stream_t
*stream
)
376 stream
->newline
|= stream
->token
->pos
.newline
;
377 stream
->whitespace
|= stream
->token
->pos
.whitespace
;
378 stream
->token
= NULL
;
390 static const long cclass
[257] = {
391 ['0' + 1 ... '9' + 1] = Digit
| Hex
,
392 ['A' + 1 ... 'D' + 1] = Letter
| Hex
,
393 ['E' + 1] = Letter
| Hex
| Exp
,
394 ['F' + 1] = Letter
| Hex
,
395 ['G' + 1 ... 'O' + 1] = Letter
,
396 ['P' + 1] = Letter
| Exp
,
397 ['Q' + 1 ... 'Z' + 1] = Letter
,
398 ['a' + 1 ... 'd' + 1] = Letter
| Hex
,
399 ['e' + 1] = Letter
| Hex
| Exp
,
400 ['f' + 1] = Letter
| Hex
,
401 ['g' + 1 ... 'o' + 1] = Letter
,
402 ['p' + 1] = Letter
| Exp
,
403 ['q' + 1 ... 'z' + 1] = Letter
,
405 ['.' + 1] = Dot
| ValidSecond
,
406 ['=' + 1] = ValidSecond
,
407 ['+' + 1] = ValidSecond
,
408 ['-' + 1] = ValidSecond
,
409 ['>' + 1] = ValidSecond
,
410 ['<' + 1] = ValidSecond
,
411 ['&' + 1] = ValidSecond
,
412 ['|' + 1] = ValidSecond
,
413 ['#' + 1] = ValidSecond
,
421 * pp-number identifier-nodigit
428 static int get_one_number(int c
, int next
, stream_t
*stream
)
431 static char buffer
[4095];
432 char *p
= buffer
, *buf
, *buffer_end
= buffer
+ sizeof (buffer
);
437 long class = cclass
[next
+ 1];
438 if (!(class & (Dot
| Digit
| Letter
)))
442 next
= nextchar(stream
);
444 if (next
== '-' || next
== '+') {
447 next
= nextchar(stream
);
452 if (p
== buffer_end
) {
453 sparse_error(stream_pos(stream
), "number token exceeds %td characters",
454 buffer_end
- buffer
);
455 // Pretend we saw just "1".
462 buf
= __alloc_bytes(len
);
463 memcpy(buf
, buffer
, len
);
465 token
= stream
->token
;
466 token_type(token
) = TOKEN_NUMBER
;
473 static int escapechar(int first
, int type
, stream_t
*stream
, int *valp
)
477 next
= nextchar(stream
);
481 warning(stream_pos(stream
), "Newline in string or character constant");
483 if (first
== '\\' && next
!= EOF
) {
485 next
= nextchar(stream
);
521 warning(stream_pos(stream
), "Newline in string or character constant");
526 while (next
>= '0' && next
<= '7') {
527 value
= (value
<< 3) + (next
-'0');
528 next
= nextchar(stream
);
536 int hex
= hexval(next
);
539 next
= nextchar(stream
);
540 while ((hex
= hexval(next
)) < 16) {
541 value
= (value
<< 4) + hex
;
542 next
= nextchar(stream
);
550 warning(stream_pos(stream
), "Unknown escape '%c'", value
);
553 /* Mark it as escaped */
560 static int get_char_token(int next
, stream_t
*stream
, enum token_type type
)
565 next
= escapechar(next
, '\'', stream
, &value
);
566 if (value
== '\'' || next
!= '\'') {
567 sparse_error(stream_pos(stream
), "Bad character constant");
572 token
= stream
->token
;
573 token_type(token
) = type
;
574 token
->character
= value
& 0xff;
577 return nextchar(stream
);
580 static int get_string_token(int next
, stream_t
*stream
, enum token_type type
)
582 static char buffer
[MAX_STRING
];
583 struct string
*string
;
589 next
= escapechar(next
, '"', stream
, &val
);
593 warning(stream_pos(stream
), "End of file in middle of string");
596 if (len
< MAX_STRING
)
601 if (len
> MAX_STRING
) {
602 warning(stream_pos(stream
), "string too long (%d bytes, %d bytes max)", len
, MAX_STRING
);
606 string
= __alloc_string(len
+1);
607 memcpy(string
->data
, buffer
, len
);
608 string
->data
[len
] = '\0';
609 string
->length
= len
+1;
612 token
= stream
->token
;
613 token_type(token
) = type
;
614 token
->string
= string
;
620 static int drop_stream_eoln(stream_t
*stream
)
624 switch (nextchar(stream
)) {
628 return nextchar(stream
);
633 static int drop_stream_comment(stream_t
*stream
)
638 newline
= stream
->newline
;
640 next
= nextchar(stream
);
644 warning(stream_pos(stream
), "End of file in the middle of a comment");
647 next
= nextchar(stream
);
648 if (curr
== '*' && next
== '/')
651 stream
->newline
= newline
;
652 return nextchar(stream
);
655 unsigned char combinations
[][4] = COMBINATION_STRINGS
;
657 #define NR_COMBINATIONS (SPECIAL_ARG_SEPARATOR - SPECIAL_BASE)
659 /* hash function for two-character punctuators - all give unique values */
660 #define special_hash(c0, c1) (((c0*8+c1*2)+((c0*8+c1*2)>>5))&31)
663 * note that we won't get false positives - special_hash(0,0) is 0 and
664 * entry 0 is filled (by +=), so all the missing ones are OK.
666 static unsigned char hash_results
[32][2] = {
667 #define RES(c0, c1) [special_hash(c0, c1)] = {c0, c1}
668 RES('+', '='), /* 00 */
669 RES('/', '='), /* 01 */
670 RES('^', '='), /* 05 */
671 RES('&', '&'), /* 07 */
672 RES('#', '#'), /* 08 */
673 RES('<', '<'), /* 0a */
674 RES('<', '='), /* 0c */
675 RES('!', '='), /* 0e */
676 RES('%', '='), /* 0f */
677 RES('-', '-'), /* 10 */
678 RES('-', '='), /* 11 */
679 RES('-', '>'), /* 13 */
680 RES('=', '='), /* 15 */
681 RES('&', '='), /* 17 */
682 RES('*', '='), /* 18 */
683 RES('.', '.'), /* 1a */
684 RES('+', '+'), /* 1b */
685 RES('|', '='), /* 1c */
686 RES('>', '='), /* 1d */
687 RES('|', '|'), /* 1e */
688 RES('>', '>') /* 1f */
691 static int code
[32] = {
692 #define CODE(c0, c1, value) [special_hash(c0, c1)] = value
693 CODE('+', '=', SPECIAL_ADD_ASSIGN
), /* 00 */
694 CODE('/', '=', SPECIAL_DIV_ASSIGN
), /* 01 */
695 CODE('^', '=', SPECIAL_XOR_ASSIGN
), /* 05 */
696 CODE('&', '&', SPECIAL_LOGICAL_AND
), /* 07 */
697 CODE('#', '#', SPECIAL_HASHHASH
), /* 08 */
698 CODE('<', '<', SPECIAL_LEFTSHIFT
), /* 0a */
699 CODE('<', '=', SPECIAL_LTE
), /* 0c */
700 CODE('!', '=', SPECIAL_NOTEQUAL
), /* 0e */
701 CODE('%', '=', SPECIAL_MOD_ASSIGN
), /* 0f */
702 CODE('-', '-', SPECIAL_DECREMENT
), /* 10 */
703 CODE('-', '=', SPECIAL_SUB_ASSIGN
), /* 11 */
704 CODE('-', '>', SPECIAL_DEREFERENCE
), /* 13 */
705 CODE('=', '=', SPECIAL_EQUAL
), /* 15 */
706 CODE('&', '=', SPECIAL_AND_ASSIGN
), /* 17 */
707 CODE('*', '=', SPECIAL_MUL_ASSIGN
), /* 18 */
708 CODE('.', '.', SPECIAL_DOTDOT
), /* 1a */
709 CODE('+', '+', SPECIAL_INCREMENT
), /* 1b */
710 CODE('|', '=', SPECIAL_OR_ASSIGN
), /* 1c */
711 CODE('>', '=', SPECIAL_GTE
), /* 1d */
712 CODE('|', '|', SPECIAL_LOGICAL_OR
), /* 1e */
713 CODE('>', '>', SPECIAL_RIGHTSHIFT
) /* 1f */
717 static int get_one_special(int c
, stream_t
*stream
)
722 next
= nextchar(stream
);
725 * Check for numbers, strings, character constants, and comments
729 if (next
>= '0' && next
<= '9')
730 return get_one_number(c
, next
, stream
);
733 return get_string_token(next
, stream
, TOKEN_STRING
);
735 return get_char_token(next
, stream
, TOKEN_CHAR
);
738 return drop_stream_eoln(stream
);
740 return drop_stream_comment(stream
);
744 * Check for combinations
747 if (cclass
[next
+ 1] & ValidSecond
) {
748 i
= special_hash(c
, next
);
749 if (hash_results
[i
][0] == c
&& hash_results
[i
][1] == next
) {
751 next
= nextchar(stream
);
752 if (value
>= SPECIAL_LEFTSHIFT
&&
753 next
== "==."[value
- SPECIAL_LEFTSHIFT
]) {
755 next
= nextchar(stream
);
761 token
= stream
->token
;
762 token_type(token
) = TOKEN_SPECIAL
;
763 token
->special
= value
;
768 #define IDENT_HASH_BITS (13)
769 #define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
770 #define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
772 #define ident_hash_init(c) (c)
773 #define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
774 #define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
776 static struct ident
*hash_table
[IDENT_HASH_SIZE
];
777 static int ident_hit
, ident_miss
, idents
;
779 void show_identifier_stats(void)
782 int distribution
[100];
784 fprintf(stderr
, "identifiers: %d hits, %d misses\n",
785 ident_hit
, ident_miss
);
787 for (i
= 0; i
< 100; i
++)
790 for (i
= 0; i
< IDENT_HASH_SIZE
; i
++) {
791 struct ident
* ident
= hash_table
[i
];
800 distribution
[count
]++;
803 for (i
= 0; i
< 100; i
++) {
805 fprintf(stderr
, "%2d: %d buckets\n", i
, distribution
[i
]);
809 static struct ident
*alloc_ident(const char *name
, int len
)
811 struct ident
*ident
= __alloc_ident(len
);
812 ident
->symbols
= NULL
;
815 memcpy(ident
->name
, name
, len
);
819 static struct ident
* insert_hash(struct ident
*ident
, unsigned long hash
)
821 ident
->next
= hash_table
[hash
];
822 hash_table
[hash
] = ident
;
827 static struct ident
*create_hashed_ident(const char *name
, int len
, unsigned long hash
)
832 p
= &hash_table
[hash
];
833 while ((ident
= *p
) != NULL
) {
834 if (ident
->len
== (unsigned char) len
) {
835 if (strncmp(name
, ident
->name
, len
) != 0)
845 ident
= alloc_ident(name
, len
);
853 static unsigned long hash_name(const char *name
, int len
)
856 const unsigned char *p
= (const unsigned char *)name
;
858 hash
= ident_hash_init(*p
++);
860 unsigned int i
= *p
++;
861 hash
= ident_hash_add(hash
, i
);
863 return ident_hash_end(hash
);
866 struct ident
*hash_ident(struct ident
*ident
)
868 return insert_hash(ident
, hash_name(ident
->name
, ident
->len
));
871 struct ident
*built_in_ident(const char *name
)
873 int len
= strlen(name
);
874 return create_hashed_ident(name
, len
, hash_name(name
, len
));
877 struct token
*built_in_token(int stream
, const char *name
)
881 token
= __alloc_token(0);
882 token
->pos
.stream
= stream
;
883 token_type(token
) = TOKEN_IDENT
;
884 token
->ident
= built_in_ident(name
);
888 static int get_one_identifier(int c
, stream_t
*stream
)
897 hash
= ident_hash_init(c
);
900 next
= nextchar(stream
);
901 if (!(cclass
[next
+ 1] & (Letter
| Digit
)))
903 if (len
>= sizeof(buf
))
905 hash
= ident_hash_add(hash
, next
);
909 hash
= ident_hash_end(hash
);
911 ident
= create_hashed_ident(buf
, len
, hash
);
913 if (ident
== &L_ident
) {
915 return get_char_token(nextchar(stream
), stream
, TOKEN_WIDE_CHAR
);
917 return get_string_token(nextchar(stream
), stream
, TOKEN_WIDE_STRING
);
921 token
= stream
->token
;
922 token_type(token
) = TOKEN_IDENT
;
923 token
->ident
= ident
;
928 static int get_one_token(int c
, stream_t
*stream
)
930 long class = cclass
[c
+ 1];
932 return get_one_number(c
, nextchar(stream
), stream
);
934 return get_one_identifier(c
, stream
);
935 return get_one_special(c
, stream
);
938 static struct token
*setup_stream(stream_t
*stream
, int idx
, int fd
,
939 unsigned char *buf
, unsigned int buf_size
)
946 stream
->whitespace
= 0;
949 stream
->token
= NULL
;
952 stream
->size
= buf_size
;
953 stream
->buffer
= buf
;
955 begin
= alloc_token(stream
);
956 token_type(begin
) = TOKEN_STREAMBEGIN
;
957 stream
->tokenlist
= &begin
->next
;
961 static struct token
*tokenize_stream(stream_t
*stream
)
963 int c
= nextchar(stream
);
966 struct token
*token
= alloc_token(stream
);
967 stream
->token
= token
;
969 stream
->whitespace
= 0;
970 c
= get_one_token(c
, stream
);
973 stream
->whitespace
= 1;
974 c
= nextchar(stream
);
976 return mark_eof(stream
);
979 struct token
* tokenize_buffer(void *buffer
, unsigned long size
, struct token
**endtoken
)
984 begin
= setup_stream(&stream
, 0, -1, buffer
, size
);
985 *endtoken
= tokenize_stream(&stream
);
989 struct token
* tokenize(const char *name
, int fd
, struct token
*endtoken
, const char **next_path
)
991 struct token
*begin
, *end
;
993 unsigned char buffer
[BUFSIZE
];
996 idx
= init_stream(name
, fd
, next_path
);
998 // info(endtoken->pos, "File %s is const", name);
1002 begin
= setup_stream(&stream
, idx
, fd
, buffer
, 0);
1003 end
= tokenize_stream(&stream
);
1005 end
->next
= endtoken
;