5 #include "symbol_table_t.h"
7 #include "adt/strset.h"
10 #include "target_architecture.h"
23 /* No strtold on windows and no replacement yet */
24 #define strtold(s, e) strtod(s, e)
27 #if defined HAS_SIGNED_CHAR
28 typedef signed char char_type;
29 #elif defined HAS_UNSIGNED_CHAR
30 typedef unsigned char char_type;
32 # error signedness of char not determined
39 static char buf[1024 + MAX_PUTBACK];
40 static const char *bufend;
41 static const char *bufpos;
42 static strset_t stringset;
44 static type_t *type_int = NULL;
45 static type_t *type_uint = NULL;
46 static type_t *type_long = NULL;
47 static type_t *type_ulong = NULL;
48 static type_t *type_longlong = NULL;
49 static type_t *type_ulonglong = NULL;
50 static type_t *type_float = NULL;
51 static type_t *type_double = NULL;
52 static type_t *type_longdouble = NULL;
54 static void error_prefix_at(const char *input_name, unsigned linenr)
56 fprintf(stderr, "%s:%u: Error: ", input_name, linenr);
59 static void error_prefix(void)
61 error_prefix_at(lexer_token.source_position.input_name,
62 lexer_token.source_position.linenr);
65 static void parse_error(const char *msg)
68 fprintf(stderr, "%s\n", msg);
71 static inline void next_real_char(void)
74 if(bufpos >= bufend) {
75 size_t s = fread(buf + MAX_PUTBACK, 1, sizeof(buf) - MAX_PUTBACK,
81 bufpos = buf + MAX_PUTBACK;
82 bufend = buf + MAX_PUTBACK + s;
87 static inline void put_back(int pc)
89 assert(bufpos >= buf);
90 //assert(bufpos < buf+MAX_PUTBACK || *bufpos == pc);
92 char *p = buf + (bufpos - buf);
95 /* going backwards in the buffer is legal as long as it's not more often
100 printf("putback '%c'\n", pc);
104 static inline void next_char(void);
106 #define MATCH_NEWLINE(code) \
112 lexer_token.source_position.linenr++; \
116 lexer_token.source_position.linenr++; \
119 #define eat(c_type) do { assert(c == c_type); next_char(); } while(0)
121 static void maybe_concat_lines(void)
126 MATCH_NEWLINE(return;)
136 static inline void next_char(void)
140 /* filter trigraphs */
141 if(UNLIKELY(c == '\\')) {
142 maybe_concat_lines();
143 goto end_of_next_char;
147 goto end_of_next_char;
150 if(LIKELY(c != '?')) {
153 goto end_of_next_char;
158 case '=': c = '#'; break;
159 case '(': c = '['; break;
160 case '/': c = '\\'; maybe_concat_lines(); break;
161 case ')': c = ']'; break;
162 case '\'': c = '^'; break;
163 case '<': c = '{'; break;
164 case '!': c = '|'; break;
165 case '>': c = '}'; break;
166 case '-': c = '~'; break;
176 printf("nchar '%c'\n", c);
180 #define SYMBOL_CHARS \
247 static void parse_symbol(void)
252 obstack_1grow(&symbol_obstack, (char) c);
259 obstack_1grow(&symbol_obstack, (char) c);
269 obstack_1grow(&symbol_obstack, '\0');
271 string = obstack_finish(&symbol_obstack);
272 symbol = symbol_table_insert(string);
274 lexer_token.type = symbol->ID;
275 lexer_token.v.symbol = symbol;
277 if(symbol->string != string) {
278 obstack_free(&symbol_obstack, string);
282 static void parse_integer_suffix(bool is_oct_hex)
284 bool is_unsigned = false;
285 bool min_long = false;
286 bool min_longlong = false;
288 if(c == 'U' || c == 'u') {
291 if(c == 'L' || c == 'l') {
294 if(c == 'L' || c == 'l') {
299 } else if(c == 'l' || c == 'L') {
302 if(c == 'l' || c == 'L') {
305 if(c == 'u' || c == 'U') {
309 } else if(c == 'u' || c == 'U') {
312 lexer_token.datatype = type_ulong;
317 long long v = lexer_token.v.intvalue;
319 if(v >= TARGET_INT_MIN && v <= TARGET_INT_MAX) {
320 lexer_token.datatype = type_int;
322 } else if(is_oct_hex && v >= 0 && v <= TARGET_UINT_MAX) {
323 lexer_token.datatype = type_uint;
328 if(v >= TARGET_LONG_MIN && v <= TARGET_LONG_MAX) {
329 lexer_token.datatype = type_long;
331 } else if(is_oct_hex && v >= 0 && v <= TARGET_ULONG_MAX) {
332 lexer_token.datatype = type_ulong;
336 unsigned long long uv = (unsigned long long) v;
337 if(is_oct_hex && uv > (unsigned long long) TARGET_LONGLONG_MAX) {
338 lexer_token.datatype = type_ulonglong;
342 lexer_token.datatype = type_longlong;
344 unsigned long long v = (unsigned long long) lexer_token.v.intvalue;
345 if(!min_long && v <= TARGET_UINT_MAX) {
346 lexer_token.datatype = type_uint;
349 if(!min_longlong && v <= TARGET_ULONG_MAX) {
350 lexer_token.datatype = type_ulong;
353 lexer_token.datatype = type_ulonglong;
357 static void parse_floating_suffix(void)
360 /* TODO: do something usefull with the suffixes... */
364 lexer_token.datatype = type_float;
369 lexer_token.datatype = type_longdouble;
372 lexer_token.datatype = type_double;
378 * A replacement for strtoull. Only those parts needed for
379 * our parser are implemented.
381 static unsigned long long parse_int_string(const char *s, const char **endptr, int base) {
382 unsigned long long v = 0;
387 /* check for overrun */
388 if (v >= 0x1000000000000000ULL)
390 switch (tolower(*s)) {
391 case '0': v <<= 4; break;
392 case '1': v <<= 4; v |= 0x1; break;
393 case '2': v <<= 4; v |= 0x2; break;
394 case '3': v <<= 4; v |= 0x3; break;
395 case '4': v <<= 4; v |= 0x4; break;
396 case '5': v <<= 4; v |= 0x5; break;
397 case '6': v <<= 4; v |= 0x6; break;
398 case '7': v <<= 4; v |= 0x7; break;
399 case '8': v <<= 4; v |= 0x8; break;
400 case '9': v <<= 4; v |= 0x9; break;
401 case 'a': v <<= 4; v |= 0xa; break;
402 case 'b': v <<= 4; v |= 0xb; break;
403 case 'c': v <<= 4; v |= 0xc; break;
404 case 'd': v <<= 4; v |= 0xd; break;
405 case 'e': v <<= 4; v |= 0xe; break;
406 case 'f': v <<= 4; v |= 0xf; break;
414 /* check for overrun */
415 if (v >= 0x2000000000000000ULL)
417 switch (tolower(*s)) {
418 case '0': v <<= 3; break;
419 case '1': v <<= 3; v |= 1; break;
420 case '2': v <<= 3; v |= 2; break;
421 case '3': v <<= 3; v |= 3; break;
422 case '4': v <<= 3; v |= 4; break;
423 case '5': v <<= 3; v |= 5; break;
424 case '6': v <<= 3; v |= 6; break;
425 case '7': v <<= 3; v |= 7; break;
433 /* check for overrun */
434 if (v > 0x1999999999999999ULL)
436 switch (tolower(*s)) {
437 case '0': v *= 10; break;
438 case '1': v *= 10; v += 1; break;
439 case '2': v *= 10; v += 2; break;
440 case '3': v *= 10; v += 3; break;
441 case '4': v *= 10; v += 4; break;
442 case '5': v *= 10; v += 5; break;
443 case '6': v *= 10; v += 6; break;
444 case '7': v *= 10; v += 7; break;
445 case '8': v *= 10; v += 8; break;
446 case '9': v *= 10; v += 9; break;
461 static void parse_number_hex(void)
463 assert(c == 'x' || c == 'X');
467 obstack_1grow(&symbol_obstack, (char) c);
470 obstack_1grow(&symbol_obstack, '\0');
471 char *string = obstack_finish(&symbol_obstack);
473 if(c == '.' || c == 'p' || c == 'P') {
475 panic("Hex floating point numbers not implemented yet");
477 if(*string == '\0') {
478 parse_error("invalid hex number");
479 lexer_token.type = T_ERROR;
483 lexer_token.type = T_INTEGER;
484 lexer_token.v.intvalue = parse_int_string(string, &endptr, 16);
485 if(*endptr != '\0') {
486 parse_error("hex number literal too long");
489 obstack_free(&symbol_obstack, string);
490 parse_integer_suffix(true);
493 static inline bool is_octal_digit(int chr)
495 return '0' <= chr && chr <= '7';
498 static void parse_number_oct(void)
500 while(is_octal_digit(c)) {
501 obstack_1grow(&symbol_obstack, (char) c);
504 obstack_1grow(&symbol_obstack, '\0');
505 char *string = obstack_finish(&symbol_obstack);
508 lexer_token.type = T_INTEGER;
509 lexer_token.v.intvalue = parse_int_string(string, &endptr, 8);
510 if(*endptr != '\0') {
511 parse_error("octal number literal too long");
514 obstack_free(&symbol_obstack, string);
515 parse_integer_suffix(true);
518 static void parse_number_dec(void)
520 bool is_float = false;
522 obstack_1grow(&symbol_obstack, (char) c);
527 obstack_1grow(&symbol_obstack, '.');
531 obstack_1grow(&symbol_obstack, (char) c);
536 if(c == 'e' || c == 'E') {
537 obstack_1grow(&symbol_obstack, 'e');
540 if(c == '-' || c == '+') {
541 obstack_1grow(&symbol_obstack, (char) c);
546 obstack_1grow(&symbol_obstack, (char) c);
552 obstack_1grow(&symbol_obstack, '\0');
553 char *string = obstack_finish(&symbol_obstack);
557 lexer_token.type = T_FLOATINGPOINT;
558 lexer_token.v.floatvalue = strtold(string, &endptr);
560 if(*endptr != '\0') {
561 parse_error("invalid number literal");
564 parse_floating_suffix();
567 lexer_token.type = T_INTEGER;
568 lexer_token.v.intvalue = parse_int_string(string, &endptr, 10);
570 if(*endptr != '\0') {
571 parse_error("invalid number literal");
574 parse_integer_suffix(false);
576 obstack_free(&symbol_obstack, string);
579 static void parse_number(void)
601 parse_error("invalid octal number");
602 lexer_token.type = T_ERROR;
608 obstack_1grow(&symbol_obstack, '0');
617 static int parse_octal_sequence(const int first_digit)
619 assert(is_octal_digit(first_digit));
620 int value = first_digit - '0';
621 if (!is_octal_digit(c)) return value;
622 value = 8 * value + c - '0';
624 if (!is_octal_digit(c)) return value;
625 value = 8 * value + c - '0';
627 return (char_type)value;
630 static int parse_hex_sequence(void)
634 if (c >= '0' && c <= '9') {
635 value = 16 * value + c - '0';
636 } else if ('A' <= c && c <= 'F') {
637 value = 16 * value + c - 'A' + 10;
638 } else if ('a' <= c && c <= 'f') {
639 value = 16 * value + c - 'a' + 10;
646 return (char_type)value;
649 static int parse_escape_sequence(void)
657 case '"': return '"';
658 case '\'': return '\'';
659 case '\\': return '\\';
660 case '?': return '\?';
661 case 'a': return '\a';
662 case 'b': return '\b';
663 case 'f': return '\f';
664 case 'n': return '\n';
665 case 'r': return '\r';
666 case 't': return '\t';
667 case 'v': return '\v';
669 return parse_hex_sequence();
678 return parse_octal_sequence(ec);
680 parse_error("reached end of file while parsing escape sequence");
683 parse_error("unknown escape sequence");
688 const char *concat_strings(const char *s1, const char *s2)
690 size_t len1 = strlen(s1);
691 size_t len2 = strlen(s2);
693 char *concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
694 memcpy(concat, s1, len1);
695 memcpy(concat + len1, s2, len2 + 1);
697 const char *result = strset_insert(&stringset, concat);
698 if(result != concat) {
699 obstack_free(&symbol_obstack, concat);
705 static void parse_string_literal(void)
707 unsigned start_linenr = lexer_token.source_position.linenr;
718 tc = parse_escape_sequence();
719 obstack_1grow(&symbol_obstack, (char) tc);
723 error_prefix_at(lexer_token.source_position.input_name,
725 fprintf(stderr, "string has no end\n");
726 lexer_token.type = T_ERROR;
734 obstack_1grow(&symbol_obstack, (char) c);
742 /* TODO: concatenate multiple strings separated by whitespace... */
744 /* add finishing 0 to the string */
745 obstack_1grow(&symbol_obstack, '\0');
746 string = obstack_finish(&symbol_obstack);
748 /* check if there is already a copy of the string */
749 result = strset_insert(&stringset, string);
750 if(result != string) {
751 obstack_free(&symbol_obstack, string);
754 lexer_token.type = T_STRING_LITERAL;
755 lexer_token.v.string = result;
758 static void parse_wide_character_constant(void)
766 found_char = parse_escape_sequence();
770 parse_error("newline while parsing character constant");
776 goto end_of_wide_char_constant;
779 parse_error("EOF while parsing character constant");
780 lexer_token.type = T_ERROR;
784 if(found_char != 0) {
785 parse_error("more than 1 characters in character "
787 goto end_of_wide_char_constant;
796 end_of_wide_char_constant:
797 lexer_token.type = T_INTEGER;
798 lexer_token.v.intvalue = found_char;
799 lexer_token.datatype = type_wchar_t;
802 static void parse_wide_string_literal(void)
804 const unsigned start_linenr = lexer_token.source_position.linenr;
812 wchar_rep_t tc = parse_escape_sequence();
813 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
818 error_prefix_at(lexer_token.source_position.input_name,
820 fprintf(stderr, "string has no end\n");
821 lexer_token.type = T_ERROR;
830 obstack_grow(&symbol_obstack, &tc, sizeof(tc));
839 /* TODO: concatenate multiple strings separated by whitespace... */
841 /* add finishing 0 to the string */
842 wchar_rep_t nul = L'\0';
843 obstack_grow(&symbol_obstack, &nul, sizeof(nul));
844 const size_t size = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
845 const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
847 #if 0 /* TODO hash */
848 /* check if there is already a copy of the string */
849 const wchar_rep_t *const result = strset_insert(&stringset, string);
850 if(result != string) {
851 obstack_free(&symbol_obstack, string);
854 const wchar_rep_t *const result = string;
857 lexer_token.type = T_WIDE_STRING_LITERAL;
858 lexer_token.v.wide_string.begin = result;
859 lexer_token.v.wide_string.size = size;
862 static void parse_character_constant(void)
870 found_char = parse_escape_sequence();
874 parse_error("newline while parsing character constant");
880 goto end_of_char_constant;
883 parse_error("EOF while parsing character constant");
884 lexer_token.type = T_ERROR;
888 if(found_char != 0) {
889 parse_error("more than 1 characters in character "
891 goto end_of_char_constant;
900 end_of_char_constant:
901 lexer_token.type = T_INTEGER;
902 lexer_token.v.intvalue = found_char;
903 lexer_token.datatype = type_int;
906 static void skip_multiline_comment(void)
908 unsigned start_linenr = lexer_token.source_position.linenr;
920 MATCH_NEWLINE(break;)
923 error_prefix_at(lexer_token.source_position.input_name,
925 fprintf(stderr, "at end of file while looking for comment end\n");
935 static void skip_line_comment(void)
953 static token_t pp_token;
955 static inline void next_pp_token(void)
957 lexer_next_preprocessing_token();
958 pp_token = lexer_token;
961 static void eat_until_newline(void)
963 while(pp_token.type != '\n' && pp_token.type != T_EOF) {
968 static void error_directive(void)
971 fprintf(stderr, "#error directive: \n");
973 /* parse pp-tokens until new-line */
976 static void define_directive(void)
978 lexer_next_preprocessing_token();
979 if(lexer_token.type != T_IDENTIFIER) {
980 parse_error("expected identifier after #define\n");
985 static void ifdef_directive(int is_ifndef)
988 lexer_next_preprocessing_token();
989 //expect_identifier();
993 static void endif_directive(void)
998 static void parse_line_directive(void)
1000 if(pp_token.type != T_INTEGER) {
1001 parse_error("expected integer");
1003 lexer_token.source_position.linenr = (unsigned int)(pp_token.v.intvalue - 1);
1006 if(pp_token.type == T_STRING_LITERAL) {
1007 lexer_token.source_position.input_name = pp_token.v.string;
1011 eat_until_newline();
1014 static void parse_preprocessor_identifier(void)
1016 assert(pp_token.type == T_IDENTIFIER);
1017 symbol_t *symbol = pp_token.v.symbol;
1019 switch(symbol->pp_ID) {
1021 printf("include - enable header name parsing!\n");
1037 parse_line_directive();
1051 static void parse_preprocessor_directive(void)
1055 switch(pp_token.type) {
1057 parse_preprocessor_identifier();
1060 parse_line_directive();
1063 parse_error("invalid preprocessor directive");
1064 eat_until_newline();
1069 #define MAYBE_PROLOG \
1074 #define MAYBE(ch, set_type) \
1077 lexer_token.type = set_type; \
1080 #define ELSE_CODE(code) \
1084 } /* end of while(1) */ \
1087 #define ELSE(set_type) \
1089 lexer_token.type = set_type; \
1093 void lexer_next_preprocessing_token(void)
1103 lexer_token.type = '\n';
1109 /* might be a wide string ( L"string" ) */
1110 if(lexer_token.type == T_IDENTIFIER &&
1111 lexer_token.v.symbol == symbol_L) {
1113 parse_wide_string_literal();
1114 } else if(c == '\'') {
1115 parse_wide_character_constant();
1125 parse_string_literal();
1129 parse_character_constant();
1136 MAYBE('.', T_DOTDOTDOT)
1140 lexer_token.type = '.';
1146 MAYBE('&', T_ANDAND)
1147 MAYBE('=', T_ANDEQUAL)
1151 MAYBE('=', T_ASTERISKEQUAL)
1155 MAYBE('+', T_PLUSPLUS)
1156 MAYBE('=', T_PLUSEQUAL)
1160 MAYBE('>', T_MINUSGREATER)
1161 MAYBE('-', T_MINUSMINUS)
1162 MAYBE('=', T_MINUSEQUAL)
1166 MAYBE('=', T_EXCLAMATIONMARKEQUAL)
1170 MAYBE('=', T_SLASHEQUAL)
1173 skip_multiline_comment();
1174 lexer_next_preprocessing_token();
1178 skip_line_comment();
1179 lexer_next_preprocessing_token();
1184 MAYBE('>', T_PERCENTGREATER)
1185 MAYBE('=', T_PERCENTEQUAL)
1190 MAYBE(':', T_PERCENTCOLONPERCENTCOLON)
1194 lexer_token.type = T_PERCENTCOLON;
1197 ELSE(T_PERCENTCOLON)
1201 MAYBE(':', T_LESSCOLON)
1202 MAYBE('%', T_LESSPERCENT)
1203 MAYBE('=', T_LESSEQUAL)
1206 MAYBE('=', T_LESSLESSEQUAL)
1211 MAYBE('=', T_GREATEREQUAL)
1214 MAYBE('=', T_GREATERGREATEREQUAL)
1215 ELSE(T_GREATERGREATER)
1219 MAYBE('=', T_CARETEQUAL)
1223 MAYBE('=', T_PIPEEQUAL)
1224 MAYBE('|', T_PIPEPIPE)
1228 MAYBE('>', T_COLONGREATER)
1232 MAYBE('=', T_EQUALEQUAL)
1236 MAYBE('#', T_HASHHASH)
1250 lexer_token.type = c;
1255 lexer_token.type = T_EOF;
1261 fprintf(stderr, "unknown character '%c' found\n", c);
1262 lexer_token.type = T_ERROR;
1268 void lexer_next_token(void)
1270 lexer_next_preprocessing_token();
1271 if(lexer_token.type != '\n')
1276 lexer_next_preprocessing_token();
1277 } while(lexer_token.type == '\n');
1279 if(lexer_token.type == '#') {
1280 parse_preprocessor_directive();
1285 void init_lexer(void)
1287 strset_init(&stringset);
1289 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
1290 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_NONE);
1291 type_long = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
1292 type_ulong = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
1293 type_longlong = make_atomic_type(ATOMIC_TYPE_LONGLONG,
1294 TYPE_QUALIFIER_NONE);
1295 type_ulonglong = make_atomic_type(ATOMIC_TYPE_ULONGLONG,
1296 TYPE_QUALIFIER_NONE);
1298 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_CONST);
1299 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
1300 TYPE_QUALIFIER_CONST);
1301 type_longdouble = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
1302 TYPE_QUALIFIER_CONST);
1305 void lexer_open_stream(FILE *stream, const char *input_name)
1308 lexer_token.source_position.linenr = 0;
1309 lexer_token.source_position.input_name = input_name;
1311 symbol_L = symbol_table_insert("L");
1313 /* place a virtual \n at the beginning so the lexer knows that we're
1314 * at the beginning of a line */
1318 void exit_lexer(void)
1320 strset_destroy(&stringset);
1323 static __attribute__((unused))
1324 void dbg_pos(const source_position_t source_position)
1326 fprintf(stdout, "%s:%u\n", source_position.input_name,
1327 source_position.linenr);