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.
185 if (NextLine () == 0) {
190 if (IsSpace (CurC)) {
200 void SymName (char* s)
201 /* Get symbol from input stream */
205 if (k != MAX_IDENTLEN) {
210 } while (IsIdent (CurC) || IsDigit (CurC));
217 /* Get symbol from input stream or return 0 if not a symbol. */
219 if (IsIdent (CurC)) {
229 static void UnknownChar (char C)
230 /* Error message for unknown character */
232 Error ("Invalid input character with code %02X", C & 0xFF);
233 NextChar (); /* Skip */
238 static void SetTok (int tok)
239 /* Set NextTok.Tok and bump line ptr */
247 static int ParseChar (void)
248 /* Parse a character. Converts \n into EOL, etc. */
254 /* Check for escape chars */
293 /* Hex character constant */
295 Val = HexVal (CurC) << 4;
297 C = Val | HexVal (CurC); /* Do not translate */
310 while (NextC >= '0' && NextC <= '7' && ++I <= 3) {
312 Val = (Val << 3) | (CurC - '0');
316 Error ("Character constant out of range");
321 Error ("Illegal character constant");
323 /* Try to do error recovery, otherwise the compiler will spit
324 * out thousands of errors in this place and abort.
326 if (CurC != '\'' && CurC != '\0') {
327 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
337 /* Skip the character read */
340 /* Do correct sign extension */
341 return SignExtendChar (C);
346 static void CharConst (void)
347 /* Parse a character constant. */
357 /* Check for closing quote */
359 Error ("`\'' expected");
365 /* Setup values and attributes */
366 NextTok.Tok = TOK_CCONST;
368 /* Translate into target charset */
369 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
371 /* Character constants have type int */
372 NextTok.Type = type_int;
377 static void StringConst (void)
378 /* Parse a quoted string */
380 NextTok.IVal = GetLiteralPoolOffs ();
381 NextTok.Tok = TOK_SCONST;
383 /* Be sure to concatenate strings */
384 while (CurC == '\"') {
386 /* Skip the quote char */
389 while (CurC != '\"') {
391 Error ("Unexpected newline");
394 AddLiteralChar (ParseChar ());
397 /* Skip closing quote char if there was one */
400 /* Skip white space, read new input */
405 /* Terminate the string */
406 AddLiteralChar ('\0');
411 static void NumericConst (void)
412 /* Parse a numeric constant */
414 unsigned Base; /* Temporary number base */
415 unsigned Prefix; /* Base according to prefix */
420 unsigned long IVal; /* Value */
422 /* Check for a leading hex or octal prefix and determine the possible
426 /* Gobble 0 and examine next char */
428 if (toupper (CurC) == 'X') {
430 NextChar (); /* gobble "x" */
432 Base = 10; /* Assume 10 for now - see below */
433 Prefix = 8; /* Actual prefix says octal */
439 /* Because floating point numbers don't have octal prefixes (a number
440 * with a leading zero is decimal), we first have to read the number
441 * before converting it, so we can determine if it's a float or an
445 while (IsXDigit (CurC) && HexVal (CurC) < Base) {
446 SB_AppendChar (&S, CurC);
451 /* The following character tells us if we have an integer or floating
452 * point constant. Note: Hexadecimal floating point constants aren't
455 IsFloat = (CurC == '.' ||
456 (Base == 10 && toupper (CurC) == 'E') ||
457 (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
459 /* If we don't have a floating point type, an octal prefix results in an
462 if (!IsFloat && Prefix == 8) {
466 /* Since we do now know the correct base, convert the remembered input
471 while ((C = SB_Get (&S)) != '\0') {
472 DigitVal = HexVal (C);
473 if (DigitVal >= Base) {
474 Error ("Numeric constant contains digits beyond the radix");
476 IVal = (IVal * Base) + DigitVal;
479 /* We don't need the string buffer any longer */
482 /* Distinguish between integer and floating point constants */
488 /* Check for a suffix and determine the possible types */
490 if (toupper (CurC) == 'U') {
493 if (toupper (CurC) != 'L') {
494 Types = IT_UINT | IT_ULONG;
499 } else if (toupper (CurC) == 'L') {
502 if (toupper (CurC) != 'U') {
503 Types = IT_LONG | IT_ULONG;
511 /* Decimal constants are of any type but uint */
512 Types = IT_INT | IT_LONG | IT_ULONG;
514 /* Octal or hex constants are of any type */
515 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
519 /* Check the range to determine the type */
521 /* Out of range for int */
523 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
524 * allowed, and we don't have a type specifying suffix, emit a
525 * warning, because the constant is of type long.
527 if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
528 Warning ("Constant is long");
532 /* Out of range for unsigned int */
535 if (IVal > 0x7FFFFFFF) {
536 /* Out of range for long int */
540 /* Now set the type string to the smallest type in types */
541 if (Types & IT_INT) {
542 NextTok.Type = type_int;
543 } else if (Types & IT_UINT) {
544 NextTok.Type = type_uint;
545 } else if (Types & IT_LONG) {
546 NextTok.Type = type_long;
548 NextTok.Type = type_ulong;
551 /* Set the value and the token */
553 NextTok.Tok = TOK_ICONST;
558 double FVal = IVal; /* Convert to float */
560 /* Check for a fractional part and read it */
570 /* Read fractional digits. Since we support only 32 bit floats
571 * with a maximum of 7 fractional digits, we read the fractional
572 * part as integer with up to 8 digits and drop the remainder.
573 * This avoids an overflow of Frac and Scale.
578 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
580 Frac = Frac * Base + DigitVal;
587 /* Scale the fractional part and add it */
589 FVal += ((double) Frac) / ((double) Scale);
593 /* Check for an exponent and read it */
594 if ((Base == 16 && toupper (CurC) == 'F') ||
595 (Base == 10 && toupper (CurC) == 'E')) {
601 /* Skip the exponent notifier */
604 /* Read an optional sign */
611 /* Read exponent digits. Since we support only 32 bit floats
612 * with a maximum exponent of +-/127, we read the exponent
613 * part as integer with up to 3 digits and drop the remainder.
614 * This avoids an overflow of Exp. The exponent is always
615 * decimal, even for hex float consts.
619 while (IsDigit (CurC)) {
621 Exp = Exp * 10 + HexVal (CurC);
626 /* Check for errors: We must have exponent digits, and not more
630 Error ("Floating constant exponent has no digits");
631 } else if (Digits > 3) {
632 Warning ("Floating constant exponent is too large");
635 /* Scale the exponent and adjust the value accordingly */
637 FVal *= pow (10, Exp);
641 /* Check for a suffix and determine the type of the constant */
642 if (toupper (CurC) == 'F') {
644 NextTok.Type = type_float;
646 NextTok.Type = type_double;
649 /* Set the value and the token */
651 NextTok.Tok = TOK_FCONST;
658 void NextToken (void)
659 /* Get next token from input stream */
663 /* We have to skip white space here before shifting tokens, since the
664 * tokens and the current line info is invalid at startup and will get
665 * initialized by reading the first time from the file. Remember if
666 * we were at end of input and handle that later.
668 int GotEOF = (SkipWhite() == 0);
670 /* Current token is the lookahead token */
672 ReleaseLineInfo (CurTok.LI);
676 /* When reading the first time from the file, the line info in NextTok,
677 * which was copied to CurTok is invalid. Since the information from
678 * the token is used for error messages, we must make it valid.
680 if (CurTok.LI == 0) {
681 CurTok.LI = UseLineInfo (GetCurLineInfo ());
684 /* Remember the starting position of the next token */
685 NextTok.LI = UseLineInfo (GetCurLineInfo ());
687 /* Now handle end of input. */
689 /* End of file reached */
690 NextTok.Tok = TOK_CEOF;
694 /* Determine the next token from the lookahead */
695 if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
703 /* Check for a keyword */
704 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
705 /* Reserved word found */
708 /* No reserved word, check for special symbols */
709 if (token[0] == '_' && token[1] == '_') {
710 /* Special symbols */
711 if (strcmp (token+2, "FILE__") == 0) {
712 NextTok.IVal = AddLiteral (GetCurrentFile());
713 NextTok.Tok = TOK_SCONST;
715 } else if (strcmp (token+2, "LINE__") == 0) {
716 NextTok.Tok = TOK_ICONST;
717 NextTok.IVal = GetCurrentLine();
718 NextTok.Type = type_int;
720 } else if (strcmp (token+2, "func__") == 0) {
721 /* __func__ is only defined in functions */
723 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
724 NextTok.Tok = TOK_SCONST;
730 /* No reserved word but identifier */
731 strcpy (NextTok.Ident, token);
732 NextTok.Tok = TOK_IDENT;
736 /* Monstrous switch statement ahead... */
744 NextTok.Tok = TOK_BOOL_NOT;
755 SetTok (TOK_MOD_ASSIGN);
757 NextTok.Tok = TOK_MOD;
765 SetTok (TOK_BOOL_AND);
768 SetTok (TOK_AND_ASSIGN);
771 NextTok.Tok = TOK_AND;
790 SetTok (TOK_MUL_ASSIGN);
792 NextTok.Tok = TOK_STAR;
803 SetTok (TOK_PLUS_ASSIGN);
806 NextTok.Tok = TOK_PLUS;
821 SetTok (TOK_MINUS_ASSIGN);
824 SetTok (TOK_PTR_REF);
827 NextTok.Tok = TOK_MINUS;
836 SetTok (TOK_ELLIPSIS);
841 NextTok.Tok = TOK_DOT;
848 SetTok (TOK_DIV_ASSIGN);
850 NextTok.Tok = TOK_DIV;
871 SetTok (TOK_SHL_ASSIGN);
873 NextTok.Tok = TOK_SHL;
877 NextTok.Tok = TOK_LT;
886 NextTok.Tok = TOK_ASSIGN;
899 SetTok (TOK_SHR_ASSIGN);
901 NextTok.Tok = TOK_SHR;
905 NextTok.Tok = TOK_GT;
924 SetTok (TOK_XOR_ASSIGN);
926 NextTok.Tok = TOK_XOR;
938 SetTok (TOK_BOOL_OR);
941 SetTok (TOK_OR_ASSIGN);
944 NextTok.Tok = TOK_OR;
965 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
966 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
967 * This routine is used for error recovery.
970 while (CurTok.Tok != TOK_CEOF) {
972 /* Check if the current token is in the token list */
974 for (I = 0; I < TokenCount; ++I) {
975 if (CurTok.Tok == TokenList[I]) {
976 /* Found a token in the list */
981 /* Not in the list: Skip it */
989 int Consume (token_t Token, const char* ErrorMsg)
990 /* Eat token if it is the next in the input stream, otherwise print an error
991 * message. Returns true if the token was found and false otherwise.
994 if (CurTok.Tok == Token) {
1005 int ConsumeColon (void)
1006 /* Check for a colon and skip it. */
1008 return Consume (TOK_COLON, "`:' expected");
1013 int ConsumeSemi (void)
1014 /* Check for a semicolon and skip it. */
1016 /* Try do be smart about typos... */
1017 if (CurTok.Tok == TOK_SEMI) {
1021 Error ("`;' expected");
1022 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1031 int ConsumeComma (void)
1032 /* Check for a comma and skip it. */
1034 /* Try do be smart about typos... */
1035 if (CurTok.Tok == TOK_COMMA) {
1039 Error ("`,' expected");
1040 if (CurTok.Tok == TOK_SEMI) {
1049 int ConsumeLParen (void)
1050 /* Check for a left parenthesis and skip it */
1052 return Consume (TOK_LPAREN, "`(' expected");
1057 int ConsumeRParen (void)
1058 /* Check for a right parenthesis and skip it */
1060 return Consume (TOK_RPAREN, "`)' expected");
1065 int ConsumeLBrack (void)
1066 /* Check for a left bracket and skip it */
1068 return Consume (TOK_LBRACK, "`[' expected");
1073 int ConsumeRBrack (void)
1074 /* Check for a right bracket and skip it */
1076 return Consume (TOK_RBRACK, "`]' expected");
1081 int ConsumeLCurly (void)
1082 /* Check for a left curly brace and skip it */
1084 return Consume (TOK_LCURLY, "`{' expected");
1089 int ConsumeRCurly (void)
1090 /* Check for a right curly brace and skip it */
1092 return Consume (TOK_RCURLY, "`}' expected");