1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 Token CurTok; /* The current token */
70 Token NextTok; /* The next token */
76 TT_C89 = 0x01 << STD_C89, /* Token valid in C89 */
77 TT_C99 = 0x01 << STD_C99, /* Token valid in C99 */
78 TT_CC65 = 0x01 << STD_CC65 /* Token valid in cc65 */
82 static const struct Keyword {
83 char* Key; /* Keyword name */
84 unsigned char Tok; /* The token */
85 unsigned char Std; /* Token supported in which standards? */
87 { "_Pragma", TOK_PRAGMA, TT_C89 | TT_C99 | TT_CC65 }, /* !! */
88 { "__AX__", TOK_AX, TT_C89 | TT_C99 | TT_CC65 },
89 { "__A__", TOK_A, TT_C89 | TT_C99 | TT_CC65 },
90 { "__EAX__", TOK_EAX, TT_C89 | TT_C99 | TT_CC65 },
91 { "__X__", TOK_X, TT_C89 | TT_C99 | TT_CC65 },
92 { "__Y__", TOK_Y, TT_C89 | TT_C99 | TT_CC65 },
93 { "__asm__", TOK_ASM, TT_C89 | TT_C99 | TT_CC65 },
94 { "__attribute__", TOK_ATTRIBUTE, TT_C89 | TT_C99 | TT_CC65 },
95 { "__far__", TOK_FAR, TT_C89 | TT_C99 | TT_CC65 },
96 { "__fastcall__", TOK_FASTCALL, TT_C89 | TT_C99 | TT_CC65 },
97 { "__near__", TOK_NEAR, TT_C89 | TT_C99 | TT_CC65 },
98 { "asm", TOK_ASM, TT_CC65 },
99 { "auto", TOK_AUTO, TT_C89 | TT_C99 | TT_CC65 },
100 { "break", TOK_BREAK, TT_C89 | TT_C99 | TT_CC65 },
101 { "case", TOK_CASE, TT_C89 | TT_C99 | TT_CC65 },
102 { "char", TOK_CHAR, TT_C89 | TT_C99 | TT_CC65 },
103 { "const", TOK_CONST, TT_C89 | TT_C99 | TT_CC65 },
104 { "continue", TOK_CONTINUE, TT_C89 | TT_C99 | TT_CC65 },
105 { "default", TOK_DEFAULT, TT_C89 | TT_C99 | TT_CC65 },
106 { "do", TOK_DO, TT_C89 | TT_C99 | TT_CC65 },
107 { "double", TOK_DOUBLE, TT_C89 | TT_C99 | TT_CC65 },
108 { "else", TOK_ELSE, TT_C89 | TT_C99 | TT_CC65 },
109 { "enum", TOK_ENUM, TT_C89 | TT_C99 | TT_CC65 },
110 { "extern", TOK_EXTERN, TT_C89 | TT_C99 | TT_CC65 },
111 { "far", TOK_FAR, TT_CC65 },
112 { "fastcall", TOK_FASTCALL, TT_CC65 },
113 { "float", TOK_FLOAT, TT_C89 | TT_C99 | TT_CC65 },
114 { "for", TOK_FOR, TT_C89 | TT_C99 | TT_CC65 },
115 { "goto", TOK_GOTO, TT_C89 | TT_C99 | TT_CC65 },
116 { "if", TOK_IF, TT_C89 | TT_C99 | TT_CC65 },
117 { "int", TOK_INT, TT_C89 | TT_C99 | TT_CC65 },
118 { "long", TOK_LONG, TT_C89 | TT_C99 | TT_CC65 },
119 { "near", TOK_NEAR, TT_CC65 },
120 { "register", TOK_REGISTER, TT_C89 | TT_C99 | TT_CC65 },
121 { "restrict", TOK_RESTRICT, TT_C99 | TT_CC65 },
122 { "return", TOK_RETURN, TT_C89 | TT_C99 | TT_CC65 },
123 { "short", TOK_SHORT, TT_C89 | TT_C99 | TT_CC65 },
124 { "signed", TOK_SIGNED, TT_C89 | TT_C99 | TT_CC65 },
125 { "sizeof", TOK_SIZEOF, TT_C89 | TT_C99 | TT_CC65 },
126 { "static", TOK_STATIC, TT_C89 | TT_C99 | TT_CC65 },
127 { "struct", TOK_STRUCT, TT_C89 | TT_C99 | TT_CC65 },
128 { "switch", TOK_SWITCH, TT_C89 | TT_C99 | TT_CC65 },
129 { "typedef", TOK_TYPEDEF, TT_C89 | TT_C99 | TT_CC65 },
130 { "union", TOK_UNION, TT_C89 | TT_C99 | TT_CC65 },
131 { "unsigned", TOK_UNSIGNED, TT_C89 | TT_C99 | TT_CC65 },
132 { "void", TOK_VOID, TT_C89 | TT_C99 | TT_CC65 },
133 { "volatile", TOK_VOLATILE, TT_C89 | TT_C99 | TT_CC65 },
134 { "while", TOK_WHILE, TT_C89 | TT_C99 | TT_CC65 },
136 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
140 /* Stuff for determining the type of an integer constant */
144 #define IT_ULONG 0x08
148 /*****************************************************************************/
150 /*****************************************************************************/
154 static int CmpKey (const void* Key, const void* Elem)
155 /* Compare function for bsearch */
157 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
162 static token_t FindKey (const char* Key)
163 /* Find a keyword and return the token. Return IDENT if the token is not a
168 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
169 if (K && (K->Std & (0x01 << IS_Get (&Standard))) != 0) {
178 static int SkipWhite (void)
179 /* Skip white space in the input stream, reading and preprocessing new lines
180 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
184 while (CurC == '\0') {
185 if (NextLine () == 0) {
190 if (IsSpace (CurC)) {
200 void SymName (char* S)
201 /* Read a symbol from the input stream. The first character must have been
202 * checked before calling this function. The buffer is expected to be at
203 * least of size MAX_IDENTLEN+1.
208 if (Len < MAX_IDENTLEN) {
213 } while (IsIdent (CurC) || IsDigit (CurC));
220 /* If a symbol follows, read it and return 1, otherwise return 0 */
222 if (IsIdent (CurC)) {
232 static void UnknownChar (char C)
233 /* Error message for unknown character */
235 Error ("Invalid input character with code %02X", C & 0xFF);
236 NextChar (); /* Skip */
241 static void SetTok (int tok)
242 /* Set NextTok.Tok and bump line ptr */
250 static int ParseChar (void)
251 /* Parse a character. Converts escape chars into character codes. */
256 /* Check for escape chars */
295 /* Hex character constant */
296 if (!IsXDigit (NextC)) {
297 Error ("\\x used with no following hex digits");
302 while (IsXDigit (NextC)) {
303 if ((C << 4) >= 256) {
305 Error ("Hex character constant out of range");
309 C = (C << 4) | HexVal (NextC);
326 while (IsODigit (NextC)) {
327 if ((C << 3) >= 256) {
329 Error ("Octal character constant out of range");
333 C = (C << 3) | HexVal (NextC);
339 Error ("Illegal character constant");
341 /* Try to do error recovery, otherwise the compiler will spit
342 * out thousands of errors in this place and abort.
344 if (CurC != '\'' && CurC != '\0') {
345 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
355 /* Skip the character read */
358 /* Do correct sign extension */
359 return SignExtendChar (C);
364 static void CharConst (void)
365 /* Parse a character constant. */
375 /* Check for closing quote */
377 Error ("`\'' expected");
383 /* Setup values and attributes */
384 NextTok.Tok = TOK_CCONST;
386 /* Translate into target charset */
387 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
389 /* Character constants have type int */
390 NextTok.Type = type_int;
395 static void StringConst (void)
396 /* Parse a quoted string */
398 NextTok.IVal = GetLiteralPoolOffs ();
399 NextTok.Tok = TOK_SCONST;
401 /* Be sure to concatenate strings */
402 while (CurC == '\"') {
404 /* Skip the quote char */
407 while (CurC != '\"') {
409 Error ("Unexpected newline");
412 AddLiteralChar (ParseChar ());
415 /* Skip closing quote char if there was one */
418 /* Skip white space, read new input */
423 /* Terminate the string */
424 AddLiteralChar ('\0');
429 static void NumericConst (void)
430 /* Parse a numeric constant */
432 unsigned Base; /* Temporary number base */
433 unsigned Prefix; /* Base according to prefix */
438 unsigned long IVal; /* Value */
440 /* Check for a leading hex or octal prefix and determine the possible
444 /* Gobble 0 and examine next char */
446 if (toupper (CurC) == 'X') {
448 NextChar (); /* gobble "x" */
450 Base = 10; /* Assume 10 for now - see below */
451 Prefix = 8; /* Actual prefix says octal */
457 /* Because floating point numbers don't have octal prefixes (a number
458 * with a leading zero is decimal), we first have to read the number
459 * before converting it, so we can determine if it's a float or an
463 while (IsXDigit (CurC) && HexVal (CurC) < Base) {
464 SB_AppendChar (&S, CurC);
469 /* The following character tells us if we have an integer or floating
470 * point constant. Note: Hexadecimal floating point constants aren't
473 IsFloat = (CurC == '.' ||
474 (Base == 10 && toupper (CurC) == 'E') ||
475 (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
477 /* If we don't have a floating point type, an octal prefix results in an
480 if (!IsFloat && Prefix == 8) {
484 /* Since we do now know the correct base, convert the remembered input
489 while ((C = SB_Get (&S)) != '\0') {
490 DigitVal = HexVal (C);
491 if (DigitVal >= Base) {
492 Error ("Numeric constant contains digits beyond the radix");
494 IVal = (IVal * Base) + DigitVal;
497 /* We don't need the string buffer any longer */
500 /* Distinguish between integer and floating point constants */
506 /* Check for a suffix and determine the possible types */
508 if (toupper (CurC) == 'U') {
511 if (toupper (CurC) != 'L') {
512 Types = IT_UINT | IT_ULONG;
517 } else if (toupper (CurC) == 'L') {
520 if (toupper (CurC) != 'U') {
521 Types = IT_LONG | IT_ULONG;
529 /* Decimal constants are of any type but uint */
530 Types = IT_INT | IT_LONG | IT_ULONG;
532 /* Octal or hex constants are of any type */
533 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
537 /* Check the range to determine the type */
539 /* Out of range for int */
541 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
542 * allowed, and we don't have a type specifying suffix, emit a
543 * warning, because the constant is of type long.
545 if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
546 Warning ("Constant is long");
550 /* Out of range for unsigned int */
553 if (IVal > 0x7FFFFFFF) {
554 /* Out of range for long int */
558 /* Now set the type string to the smallest type in types */
559 if (Types & IT_INT) {
560 NextTok.Type = type_int;
561 } else if (Types & IT_UINT) {
562 NextTok.Type = type_uint;
563 } else if (Types & IT_LONG) {
564 NextTok.Type = type_long;
566 NextTok.Type = type_ulong;
569 /* Set the value and the token */
571 NextTok.Tok = TOK_ICONST;
576 double FVal = IVal; /* Convert to float */
578 /* Check for a fractional part and read it */
588 /* Read fractional digits. Since we support only 32 bit floats
589 * with a maximum of 7 fractional digits, we read the fractional
590 * part as integer with up to 8 digits and drop the remainder.
591 * This avoids an overflow of Frac and Scale.
596 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
598 Frac = Frac * Base + DigitVal;
605 /* Scale the fractional part and add it */
607 FVal += ((double) Frac) / ((double) Scale);
611 /* Check for an exponent and read it */
612 if ((Base == 16 && toupper (CurC) == 'F') ||
613 (Base == 10 && toupper (CurC) == 'E')) {
619 /* Skip the exponent notifier */
622 /* Read an optional sign */
629 /* Read exponent digits. Since we support only 32 bit floats
630 * with a maximum exponent of +-/127, we read the exponent
631 * part as integer with up to 3 digits and drop the remainder.
632 * This avoids an overflow of Exp. The exponent is always
633 * decimal, even for hex float consts.
637 while (IsDigit (CurC)) {
639 Exp = Exp * 10 + HexVal (CurC);
644 /* Check for errors: We must have exponent digits, and not more
648 Error ("Floating constant exponent has no digits");
649 } else if (Digits > 3) {
650 Warning ("Floating constant exponent is too large");
653 /* Scale the exponent and adjust the value accordingly */
655 FVal *= pow (10, Exp);
659 /* Check for a suffix and determine the type of the constant */
660 if (toupper (CurC) == 'F') {
662 NextTok.Type = type_float;
664 NextTok.Type = type_double;
667 /* Set the value and the token */
669 NextTok.Tok = TOK_FCONST;
676 void NextToken (void)
677 /* Get next token from input stream */
681 /* We have to skip white space here before shifting tokens, since the
682 * tokens and the current line info is invalid at startup and will get
683 * initialized by reading the first time from the file. Remember if
684 * we were at end of input and handle that later.
686 int GotEOF = (SkipWhite() == 0);
688 /* Current token is the lookahead token */
690 ReleaseLineInfo (CurTok.LI);
694 /* When reading the first time from the file, the line info in NextTok,
695 * which was copied to CurTok is invalid. Since the information from
696 * the token is used for error messages, we must make it valid.
698 if (CurTok.LI == 0) {
699 CurTok.LI = UseLineInfo (GetCurLineInfo ());
702 /* Remember the starting position of the next token */
703 NextTok.LI = UseLineInfo (GetCurLineInfo ());
705 /* Now handle end of input. */
707 /* End of file reached */
708 NextTok.Tok = TOK_CEOF;
712 /* Determine the next token from the lookahead */
713 if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
721 /* Check for a keyword */
722 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
723 /* Reserved word found */
726 /* No reserved word, check for special symbols */
727 if (token[0] == '_' && token[1] == '_') {
728 /* Special symbols */
729 if (strcmp (token+2, "FILE__") == 0) {
730 NextTok.IVal = AddLiteral (GetCurrentFile());
731 NextTok.Tok = TOK_SCONST;
733 } else if (strcmp (token+2, "LINE__") == 0) {
734 NextTok.Tok = TOK_ICONST;
735 NextTok.IVal = GetCurrentLine();
736 NextTok.Type = type_int;
738 } else if (strcmp (token+2, "func__") == 0) {
739 /* __func__ is only defined in functions */
741 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
742 NextTok.Tok = TOK_SCONST;
748 /* No reserved word but identifier */
749 strcpy (NextTok.Ident, token);
750 NextTok.Tok = TOK_IDENT;
754 /* Monstrous switch statement ahead... */
762 NextTok.Tok = TOK_BOOL_NOT;
773 SetTok (TOK_MOD_ASSIGN);
775 NextTok.Tok = TOK_MOD;
783 SetTok (TOK_BOOL_AND);
786 SetTok (TOK_AND_ASSIGN);
789 NextTok.Tok = TOK_AND;
808 SetTok (TOK_MUL_ASSIGN);
810 NextTok.Tok = TOK_STAR;
821 SetTok (TOK_PLUS_ASSIGN);
824 NextTok.Tok = TOK_PLUS;
839 SetTok (TOK_MINUS_ASSIGN);
842 SetTok (TOK_PTR_REF);
845 NextTok.Tok = TOK_MINUS;
854 SetTok (TOK_ELLIPSIS);
859 NextTok.Tok = TOK_DOT;
866 SetTok (TOK_DIV_ASSIGN);
868 NextTok.Tok = TOK_DIV;
889 SetTok (TOK_SHL_ASSIGN);
891 NextTok.Tok = TOK_SHL;
895 NextTok.Tok = TOK_LT;
904 NextTok.Tok = TOK_ASSIGN;
917 SetTok (TOK_SHR_ASSIGN);
919 NextTok.Tok = TOK_SHR;
923 NextTok.Tok = TOK_GT;
942 SetTok (TOK_XOR_ASSIGN);
944 NextTok.Tok = TOK_XOR;
956 SetTok (TOK_BOOL_OR);
959 SetTok (TOK_OR_ASSIGN);
962 NextTok.Tok = TOK_OR;
983 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
984 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
985 * This routine is used for error recovery.
988 while (CurTok.Tok != TOK_CEOF) {
990 /* Check if the current token is in the token list */
992 for (I = 0; I < TokenCount; ++I) {
993 if (CurTok.Tok == TokenList[I]) {
994 /* Found a token in the list */
999 /* Not in the list: Skip it */
1007 int Consume (token_t Token, const char* ErrorMsg)
1008 /* Eat token if it is the next in the input stream, otherwise print an error
1009 * message. Returns true if the token was found and false otherwise.
1012 if (CurTok.Tok == Token) {
1023 int ConsumeColon (void)
1024 /* Check for a colon and skip it. */
1026 return Consume (TOK_COLON, "`:' expected");
1031 int ConsumeSemi (void)
1032 /* Check for a semicolon and skip it. */
1034 /* Try do be smart about typos... */
1035 if (CurTok.Tok == TOK_SEMI) {
1039 Error ("`;' expected");
1040 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1049 int ConsumeComma (void)
1050 /* Check for a comma and skip it. */
1052 /* Try do be smart about typos... */
1053 if (CurTok.Tok == TOK_COMMA) {
1057 Error ("`,' expected");
1058 if (CurTok.Tok == TOK_SEMI) {
1067 int ConsumeLParen (void)
1068 /* Check for a left parenthesis and skip it */
1070 return Consume (TOK_LPAREN, "`(' expected");
1075 int ConsumeRParen (void)
1076 /* Check for a right parenthesis and skip it */
1078 return Consume (TOK_RPAREN, "`)' expected");
1083 int ConsumeLBrack (void)
1084 /* Check for a left bracket and skip it */
1086 return Consume (TOK_LBRACK, "`[' expected");
1091 int ConsumeRBrack (void)
1092 /* Check for a right bracket and skip it */
1094 return Consume (TOK_RBRACK, "`]' expected");
1099 int ConsumeLCurly (void)
1100 /* Check for a left curly brace and skip it */
1102 return Consume (TOK_LCURLY, "`{' expected");
1107 int ConsumeRCurly (void)
1108 /* Check for a right curly brace and skip it */
1110 return Consume (TOK_RCURLY, "`}' expected");