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 { "__inline__", TOK_INLINE, TT_C89 | TT_C99 | TT_CC65 },
98 { "__near__", TOK_NEAR, TT_C89 | TT_C99 | TT_CC65 },
99 { "asm", TOK_ASM, TT_CC65 },
100 { "auto", TOK_AUTO, TT_C89 | TT_C99 | TT_CC65 },
101 { "break", TOK_BREAK, TT_C89 | TT_C99 | TT_CC65 },
102 { "case", TOK_CASE, TT_C89 | TT_C99 | TT_CC65 },
103 { "char", TOK_CHAR, TT_C89 | TT_C99 | TT_CC65 },
104 { "const", TOK_CONST, TT_C89 | TT_C99 | TT_CC65 },
105 { "continue", TOK_CONTINUE, TT_C89 | TT_C99 | TT_CC65 },
106 { "default", TOK_DEFAULT, TT_C89 | TT_C99 | TT_CC65 },
107 { "do", TOK_DO, TT_C89 | TT_C99 | TT_CC65 },
108 { "double", TOK_DOUBLE, TT_C89 | TT_C99 | TT_CC65 },
109 { "else", TOK_ELSE, TT_C89 | TT_C99 | TT_CC65 },
110 { "enum", TOK_ENUM, TT_C89 | TT_C99 | TT_CC65 },
111 { "extern", TOK_EXTERN, TT_C89 | TT_C99 | TT_CC65 },
112 { "far", TOK_FAR, TT_CC65 },
113 { "fastcall", TOK_FASTCALL, TT_CC65 },
114 { "float", TOK_FLOAT, TT_C89 | TT_C99 | TT_CC65 },
115 { "for", TOK_FOR, TT_C89 | TT_C99 | TT_CC65 },
116 { "goto", TOK_GOTO, TT_C89 | TT_C99 | TT_CC65 },
117 { "if", TOK_IF, TT_C89 | TT_C99 | TT_CC65 },
118 { "inline", TOK_INLINE, TT_C99 | TT_CC65 },
119 { "int", TOK_INT, TT_C89 | TT_C99 | TT_CC65 },
120 { "long", TOK_LONG, TT_C89 | TT_C99 | TT_CC65 },
121 { "near", TOK_NEAR, TT_CC65 },
122 { "register", TOK_REGISTER, TT_C89 | TT_C99 | TT_CC65 },
123 { "restrict", TOK_RESTRICT, TT_C99 | TT_CC65 },
124 { "return", TOK_RETURN, TT_C89 | TT_C99 | TT_CC65 },
125 { "short", TOK_SHORT, TT_C89 | TT_C99 | TT_CC65 },
126 { "signed", TOK_SIGNED, TT_C89 | TT_C99 | TT_CC65 },
127 { "sizeof", TOK_SIZEOF, TT_C89 | TT_C99 | TT_CC65 },
128 { "static", TOK_STATIC, TT_C89 | TT_C99 | TT_CC65 },
129 { "struct", TOK_STRUCT, TT_C89 | TT_C99 | TT_CC65 },
130 { "switch", TOK_SWITCH, TT_C89 | TT_C99 | TT_CC65 },
131 { "typedef", TOK_TYPEDEF, TT_C89 | TT_C99 | TT_CC65 },
132 { "union", TOK_UNION, TT_C89 | TT_C99 | TT_CC65 },
133 { "unsigned", TOK_UNSIGNED, TT_C89 | TT_C99 | TT_CC65 },
134 { "void", TOK_VOID, TT_C89 | TT_C99 | TT_CC65 },
135 { "volatile", TOK_VOLATILE, TT_C89 | TT_C99 | TT_CC65 },
136 { "while", TOK_WHILE, TT_C89 | TT_C99 | TT_CC65 },
138 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
142 /* Stuff for determining the type of an integer constant */
146 #define IT_ULONG 0x08
150 /*****************************************************************************/
152 /*****************************************************************************/
156 static int CmpKey (const void* Key, const void* Elem)
157 /* Compare function for bsearch */
159 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
164 static token_t FindKey (const char* Key)
165 /* Find a keyword and return the token. Return IDENT if the token is not a
170 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
171 if (K && (K->Std & (0x01 << IS_Get (&Standard))) != 0) {
180 static int SkipWhite (void)
181 /* Skip white space in the input stream, reading and preprocessing new lines
182 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
186 while (CurC == '\0') {
187 if (NextLine () == 0) {
192 if (IsSpace (CurC)) {
202 void SymName (char* S)
203 /* Read a symbol from the input stream. The first character must have been
204 * checked before calling this function. The buffer is expected to be at
205 * least of size MAX_IDENTLEN+1.
210 if (Len < MAX_IDENTLEN) {
215 } while (IsIdent (CurC) || IsDigit (CurC));
222 /* If a symbol follows, read it and return 1, otherwise return 0 */
224 if (IsIdent (CurC)) {
234 static void UnknownChar (char C)
235 /* Error message for unknown character */
237 Error ("Invalid input character with code %02X", C & 0xFF);
238 NextChar (); /* Skip */
243 static void SetTok (int tok)
244 /* Set NextTok.Tok and bump line ptr */
252 static int ParseChar (void)
253 /* Parse a character. Converts escape chars into character codes. */
258 /* Check for escape chars */
297 /* Hex character constant */
298 if (!IsXDigit (NextC)) {
299 Error ("\\x used with no following hex digits");
304 while (IsXDigit (NextC)) {
305 if ((C << 4) >= 256) {
307 Error ("Hex character constant out of range");
311 C = (C << 4) | HexVal (NextC);
328 while (IsODigit (NextC)) {
329 if ((C << 3) >= 256) {
331 Error ("Octal character constant out of range");
335 C = (C << 3) | HexVal (NextC);
341 Error ("Illegal character constant");
343 /* Try to do error recovery, otherwise the compiler will spit
344 * out thousands of errors in this place and abort.
346 if (CurC != '\'' && CurC != '\0') {
347 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
357 /* Skip the character read */
360 /* Do correct sign extension */
361 return SignExtendChar (C);
366 static void CharConst (void)
367 /* Parse a character constant. */
377 /* Check for closing quote */
379 Error ("`\'' expected");
385 /* Setup values and attributes */
386 NextTok.Tok = TOK_CCONST;
388 /* Translate into target charset */
389 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
391 /* Character constants have type int */
392 NextTok.Type = type_int;
397 static void StringConst (void)
398 /* Parse a quoted string */
400 NextTok.IVal = GetLiteralPoolOffs ();
401 NextTok.Tok = TOK_SCONST;
403 /* Be sure to concatenate strings */
404 while (CurC == '\"') {
406 /* Skip the quote char */
409 while (CurC != '\"') {
411 Error ("Unexpected newline");
414 AddLiteralChar (ParseChar ());
417 /* Skip closing quote char if there was one */
420 /* Skip white space, read new input */
425 /* Terminate the string */
426 AddLiteralChar ('\0');
431 static void NumericConst (void)
432 /* Parse a numeric constant */
434 unsigned Base; /* Temporary number base */
435 unsigned Prefix; /* Base according to prefix */
440 unsigned long IVal; /* Value */
442 /* Check for a leading hex or octal prefix and determine the possible
446 /* Gobble 0 and examine next char */
448 if (toupper (CurC) == 'X') {
450 NextChar (); /* gobble "x" */
452 Base = 10; /* Assume 10 for now - see below */
453 Prefix = 8; /* Actual prefix says octal */
459 /* Because floating point numbers don't have octal prefixes (a number
460 * with a leading zero is decimal), we first have to read the number
461 * before converting it, so we can determine if it's a float or an
465 while (IsXDigit (CurC) && HexVal (CurC) < Base) {
466 SB_AppendChar (&S, CurC);
471 /* The following character tells us if we have an integer or floating
472 * point constant. Note: Hexadecimal floating point constants aren't
475 IsFloat = (CurC == '.' ||
476 (Base == 10 && toupper (CurC) == 'E') ||
477 (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
479 /* If we don't have a floating point type, an octal prefix results in an
482 if (!IsFloat && Prefix == 8) {
486 /* Since we do now know the correct base, convert the remembered input
491 while ((C = SB_Get (&S)) != '\0') {
492 DigitVal = HexVal (C);
493 if (DigitVal >= Base) {
494 Error ("Numeric constant contains digits beyond the radix");
496 IVal = (IVal * Base) + DigitVal;
499 /* We don't need the string buffer any longer */
502 /* Distinguish between integer and floating point constants */
508 /* Check for a suffix and determine the possible types */
510 if (toupper (CurC) == 'U') {
513 if (toupper (CurC) != 'L') {
514 Types = IT_UINT | IT_ULONG;
519 } else if (toupper (CurC) == 'L') {
522 if (toupper (CurC) != 'U') {
523 Types = IT_LONG | IT_ULONG;
531 /* Decimal constants are of any type but uint */
532 Types = IT_INT | IT_LONG | IT_ULONG;
534 /* Octal or hex constants are of any type */
535 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
539 /* Check the range to determine the type */
541 /* Out of range for int */
543 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
544 * allowed, and we don't have a type specifying suffix, emit a
545 * warning, because the constant is of type long.
547 if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
548 Warning ("Constant is long");
552 /* Out of range for unsigned int */
555 if (IVal > 0x7FFFFFFF) {
556 /* Out of range for long int */
560 /* Now set the type string to the smallest type in types */
561 if (Types & IT_INT) {
562 NextTok.Type = type_int;
563 } else if (Types & IT_UINT) {
564 NextTok.Type = type_uint;
565 } else if (Types & IT_LONG) {
566 NextTok.Type = type_long;
568 NextTok.Type = type_ulong;
571 /* Set the value and the token */
573 NextTok.Tok = TOK_ICONST;
578 double FVal = IVal; /* Convert to float */
580 /* Check for a fractional part and read it */
590 /* Read fractional digits. Since we support only 32 bit floats
591 * with a maximum of 7 fractional digits, we read the fractional
592 * part as integer with up to 8 digits and drop the remainder.
593 * This avoids an overflow of Frac and Scale.
598 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
600 Frac = Frac * Base + DigitVal;
607 /* Scale the fractional part and add it */
609 FVal += ((double) Frac) / ((double) Scale);
613 /* Check for an exponent and read it */
614 if ((Base == 16 && toupper (CurC) == 'F') ||
615 (Base == 10 && toupper (CurC) == 'E')) {
621 /* Skip the exponent notifier */
624 /* Read an optional sign */
631 /* Read exponent digits. Since we support only 32 bit floats
632 * with a maximum exponent of +-/127, we read the exponent
633 * part as integer with up to 3 digits and drop the remainder.
634 * This avoids an overflow of Exp. The exponent is always
635 * decimal, even for hex float consts.
639 while (IsDigit (CurC)) {
641 Exp = Exp * 10 + HexVal (CurC);
646 /* Check for errors: We must have exponent digits, and not more
650 Error ("Floating constant exponent has no digits");
651 } else if (Digits > 3) {
652 Warning ("Floating constant exponent is too large");
655 /* Scale the exponent and adjust the value accordingly */
657 FVal *= pow (10, Exp);
661 /* Check for a suffix and determine the type of the constant */
662 if (toupper (CurC) == 'F') {
664 NextTok.Type = type_float;
666 NextTok.Type = type_double;
669 /* Set the value and the token */
671 NextTok.Tok = TOK_FCONST;
678 void NextToken (void)
679 /* Get next token from input stream */
683 /* We have to skip white space here before shifting tokens, since the
684 * tokens and the current line info is invalid at startup and will get
685 * initialized by reading the first time from the file. Remember if
686 * we were at end of input and handle that later.
688 int GotEOF = (SkipWhite() == 0);
690 /* Current token is the lookahead token */
692 ReleaseLineInfo (CurTok.LI);
696 /* When reading the first time from the file, the line info in NextTok,
697 * which was copied to CurTok is invalid. Since the information from
698 * the token is used for error messages, we must make it valid.
700 if (CurTok.LI == 0) {
701 CurTok.LI = UseLineInfo (GetCurLineInfo ());
704 /* Remember the starting position of the next token */
705 NextTok.LI = UseLineInfo (GetCurLineInfo ());
707 /* Now handle end of input. */
709 /* End of file reached */
710 NextTok.Tok = TOK_CEOF;
714 /* Determine the next token from the lookahead */
715 if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
723 /* Check for a keyword */
724 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
725 /* Reserved word found */
728 /* No reserved word, check for special symbols */
729 if (token[0] == '_' && token[1] == '_') {
730 /* Special symbols */
731 if (strcmp (token+2, "FILE__") == 0) {
732 NextTok.IVal = AddLiteral (GetCurrentFile());
733 NextTok.Tok = TOK_SCONST;
735 } else if (strcmp (token+2, "LINE__") == 0) {
736 NextTok.Tok = TOK_ICONST;
737 NextTok.IVal = GetCurrentLine();
738 NextTok.Type = type_int;
740 } else if (strcmp (token+2, "func__") == 0) {
741 /* __func__ is only defined in functions */
743 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
744 NextTok.Tok = TOK_SCONST;
750 /* No reserved word but identifier */
751 strcpy (NextTok.Ident, token);
752 NextTok.Tok = TOK_IDENT;
756 /* Monstrous switch statement ahead... */
764 NextTok.Tok = TOK_BOOL_NOT;
775 SetTok (TOK_MOD_ASSIGN);
777 NextTok.Tok = TOK_MOD;
785 SetTok (TOK_BOOL_AND);
788 SetTok (TOK_AND_ASSIGN);
791 NextTok.Tok = TOK_AND;
810 SetTok (TOK_MUL_ASSIGN);
812 NextTok.Tok = TOK_STAR;
823 SetTok (TOK_PLUS_ASSIGN);
826 NextTok.Tok = TOK_PLUS;
841 SetTok (TOK_MINUS_ASSIGN);
844 SetTok (TOK_PTR_REF);
847 NextTok.Tok = TOK_MINUS;
856 SetTok (TOK_ELLIPSIS);
861 NextTok.Tok = TOK_DOT;
868 SetTok (TOK_DIV_ASSIGN);
870 NextTok.Tok = TOK_DIV;
891 SetTok (TOK_SHL_ASSIGN);
893 NextTok.Tok = TOK_SHL;
897 NextTok.Tok = TOK_LT;
906 NextTok.Tok = TOK_ASSIGN;
919 SetTok (TOK_SHR_ASSIGN);
921 NextTok.Tok = TOK_SHR;
925 NextTok.Tok = TOK_GT;
944 SetTok (TOK_XOR_ASSIGN);
946 NextTok.Tok = TOK_XOR;
958 SetTok (TOK_BOOL_OR);
961 SetTok (TOK_OR_ASSIGN);
964 NextTok.Tok = TOK_OR;
985 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
986 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
987 * This routine is used for error recovery.
990 while (CurTok.Tok != TOK_CEOF) {
992 /* Check if the current token is in the token list */
994 for (I = 0; I < TokenCount; ++I) {
995 if (CurTok.Tok == TokenList[I]) {
996 /* Found a token in the list */
1001 /* Not in the list: Skip it */
1009 int Consume (token_t Token, const char* ErrorMsg)
1010 /* Eat token if it is the next in the input stream, otherwise print an error
1011 * message. Returns true if the token was found and false otherwise.
1014 if (CurTok.Tok == Token) {
1025 int ConsumeColon (void)
1026 /* Check for a colon and skip it. */
1028 return Consume (TOK_COLON, "`:' expected");
1033 int ConsumeSemi (void)
1034 /* Check for a semicolon and skip it. */
1036 /* Try do be smart about typos... */
1037 if (CurTok.Tok == TOK_SEMI) {
1041 Error ("`;' expected");
1042 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1051 int ConsumeComma (void)
1052 /* Check for a comma and skip it. */
1054 /* Try do be smart about typos... */
1055 if (CurTok.Tok == TOK_COMMA) {
1059 Error ("`,' expected");
1060 if (CurTok.Tok == TOK_SEMI) {
1069 int ConsumeLParen (void)
1070 /* Check for a left parenthesis and skip it */
1072 return Consume (TOK_LPAREN, "`(' expected");
1077 int ConsumeRParen (void)
1078 /* Check for a right parenthesis and skip it */
1080 return Consume (TOK_RPAREN, "`)' expected");
1085 int ConsumeLBrack (void)
1086 /* Check for a left bracket and skip it */
1088 return Consume (TOK_LBRACK, "`[' expected");
1093 int ConsumeRBrack (void)
1094 /* Check for a right bracket and skip it */
1096 return Consume (TOK_RBRACK, "`]' expected");
1101 int ConsumeLCurly (void)
1102 /* Check for a left curly brace and skip it */
1104 return Consume (TOK_LCURLY, "`{' expected");
1109 int ConsumeRCurly (void)
1110 /* Check for a right curly brace and skip it */
1112 return Consume (TOK_RCURLY, "`}' expected");