1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 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 int TokIsFuncSpec (const Token* T)
203 /* Return true if the token is a function specifier */
205 return (T->Tok == TOK_INLINE) || (T->Tok == TOK_FASTCALL) ||
206 (T->Tok == TOK_NEAR) || (T->Tok == TOK_FAR);
211 void SymName (char* S)
212 /* Read a symbol from the input stream. The first character must have been
213 * checked before calling this function. The buffer is expected to be at
214 * least of size MAX_IDENTLEN+1.
219 if (Len < MAX_IDENTLEN) {
224 } while (IsIdent (CurC) || IsDigit (CurC));
231 /* If a symbol follows, read it and return 1, otherwise return 0 */
233 if (IsIdent (CurC)) {
243 static void UnknownChar (char C)
244 /* Error message for unknown character */
246 Error ("Invalid input character with code %02X", C & 0xFF);
247 NextChar (); /* Skip */
252 static void SetTok (int tok)
253 /* Set NextTok.Tok and bump line ptr */
261 static int ParseChar (void)
262 /* Parse a character. Converts escape chars into character codes. */
267 /* Check for escape chars */
306 /* Hex character constant */
307 if (!IsXDigit (NextC)) {
308 Error ("\\x used with no following hex digits");
313 while (IsXDigit (NextC)) {
314 if ((C << 4) >= 256) {
316 Error ("Hex character constant out of range");
320 C = (C << 4) | HexVal (NextC);
337 while (IsODigit (NextC)) {
338 if ((C << 3) >= 256) {
340 Error ("Octal character constant out of range");
344 C = (C << 3) | HexVal (NextC);
350 Error ("Illegal character constant");
352 /* Try to do error recovery, otherwise the compiler will spit
353 * out thousands of errors in this place and abort.
355 if (CurC != '\'' && CurC != '\0') {
356 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
366 /* Skip the character read */
369 /* Do correct sign extension */
370 return SignExtendChar (C);
375 static void CharConst (void)
376 /* Parse a character constant. */
386 /* Check for closing quote */
388 Error ("`\'' expected");
394 /* Setup values and attributes */
395 NextTok.Tok = TOK_CCONST;
397 /* Translate into target charset */
398 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
400 /* Character constants have type int */
401 NextTok.Type = type_int;
406 static void StringConst (void)
407 /* Parse a quoted string */
409 NextTok.IVal = GetLiteralPoolOffs ();
410 NextTok.Tok = TOK_SCONST;
412 /* Be sure to concatenate strings */
413 while (CurC == '\"') {
415 /* Skip the quote char */
418 while (CurC != '\"') {
420 Error ("Unexpected newline");
423 AddLiteralChar (ParseChar ());
426 /* Skip closing quote char if there was one */
429 /* Skip white space, read new input */
434 /* Terminate the string */
435 AddLiteralChar ('\0');
440 static void NumericConst (void)
441 /* Parse a numeric constant */
443 unsigned Base; /* Temporary number base */
444 unsigned Prefix; /* Base according to prefix */
445 StrBuf S = STATIC_STRBUF_INITIALIZER;
449 unsigned long IVal; /* Value */
451 /* Check for a leading hex or octal prefix and determine the possible
455 /* Gobble 0 and examine next char */
457 if (toupper (CurC) == 'X') {
459 NextChar (); /* gobble "x" */
461 Base = 10; /* Assume 10 for now - see below */
462 Prefix = 8; /* Actual prefix says octal */
468 /* Because floating point numbers don't have octal prefixes (a number
469 * with a leading zero is decimal), we first have to read the number
470 * before converting it, so we can determine if it's a float or an
473 while (IsXDigit (CurC) && HexVal (CurC) < Base) {
474 SB_AppendChar (&S, CurC);
479 /* The following character tells us if we have an integer or floating
480 * point constant. Note: Hexadecimal floating point constants aren't
483 IsFloat = (CurC == '.' ||
484 (Base == 10 && toupper (CurC) == 'E') ||
485 (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
487 /* If we don't have a floating point type, an octal prefix results in an
490 if (!IsFloat && Prefix == 8) {
494 /* Since we do now know the correct base, convert the remembered input
499 while ((C = SB_Get (&S)) != '\0') {
500 DigitVal = HexVal (C);
501 if (DigitVal >= Base) {
502 Error ("Numeric constant contains digits beyond the radix");
504 IVal = (IVal * Base) + DigitVal;
507 /* We don't need the string buffer any longer */
510 /* Distinguish between integer and floating point constants */
516 /* Check for a suffix and determine the possible types */
518 if (toupper (CurC) == 'U') {
521 if (toupper (CurC) != 'L') {
522 Types = IT_UINT | IT_ULONG;
527 } else if (toupper (CurC) == 'L') {
530 if (toupper (CurC) != 'U') {
531 Types = IT_LONG | IT_ULONG;
539 /* Decimal constants are of any type but uint */
540 Types = IT_INT | IT_LONG | IT_ULONG;
542 /* Octal or hex constants are of any type */
543 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
547 /* Check the range to determine the type */
549 /* Out of range for int */
551 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
552 * allowed, and we don't have a type specifying suffix, emit a
553 * warning, because the constant is of type long.
555 if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
556 Warning ("Constant is long");
560 /* Out of range for unsigned int */
563 if (IVal > 0x7FFFFFFF) {
564 /* Out of range for long int */
568 /* Now set the type string to the smallest type in types */
569 if (Types & IT_INT) {
570 NextTok.Type = type_int;
571 } else if (Types & IT_UINT) {
572 NextTok.Type = type_uint;
573 } else if (Types & IT_LONG) {
574 NextTok.Type = type_long;
576 NextTok.Type = type_ulong;
579 /* Set the value and the token */
581 NextTok.Tok = TOK_ICONST;
586 double FVal = IVal; /* Convert to float */
588 /* Check for a fractional part and read it */
598 /* Read fractional digits. Since we support only 32 bit floats
599 * with a maximum of 7 fractional digits, we read the fractional
600 * part as integer with up to 8 digits and drop the remainder.
601 * This avoids an overflow of Frac and Scale.
606 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
608 Frac = Frac * Base + DigitVal;
615 /* Scale the fractional part and add it */
617 FVal += ((double) Frac) / ((double) Scale);
621 /* Check for an exponent and read it */
622 if ((Base == 16 && toupper (CurC) == 'F') ||
623 (Base == 10 && toupper (CurC) == 'E')) {
629 /* Skip the exponent notifier */
632 /* Read an optional sign */
637 } else if (CurC == '+') {
641 /* Read exponent digits. Since we support only 32 bit floats
642 * with a maximum exponent of +-/127, we read the exponent
643 * part as integer with up to 3 digits and drop the remainder.
644 * This avoids an overflow of Exp. The exponent is always
645 * decimal, even for hex float consts.
649 while (IsDigit (CurC)) {
651 Exp = Exp * 10 + HexVal (CurC);
656 /* Check for errors: We must have exponent digits, and not more
660 Error ("Floating constant exponent has no digits");
661 } else if (Digits > 3) {
662 Warning ("Floating constant exponent is too large");
665 /* Scale the exponent and adjust the value accordingly */
667 FVal *= pow (10, Exp);
671 /* Check for a suffix and determine the type of the constant */
672 if (toupper (CurC) == 'F') {
674 NextTok.Type = type_float;
676 NextTok.Type = type_double;
679 /* Set the value and the token */
681 NextTok.Tok = TOK_FCONST;
688 void NextToken (void)
689 /* Get next token from input stream */
693 /* We have to skip white space here before shifting tokens, since the
694 * tokens and the current line info is invalid at startup and will get
695 * initialized by reading the first time from the file. Remember if
696 * we were at end of input and handle that later.
698 int GotEOF = (SkipWhite() == 0);
700 /* Current token is the lookahead token */
702 ReleaseLineInfo (CurTok.LI);
706 /* When reading the first time from the file, the line info in NextTok,
707 * which was copied to CurTok is invalid. Since the information from
708 * the token is used for error messages, we must make it valid.
710 if (CurTok.LI == 0) {
711 CurTok.LI = UseLineInfo (GetCurLineInfo ());
714 /* Remember the starting position of the next token */
715 NextTok.LI = UseLineInfo (GetCurLineInfo ());
717 /* Now handle end of input. */
719 /* End of file reached */
720 NextTok.Tok = TOK_CEOF;
724 /* Determine the next token from the lookahead */
725 if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
733 /* Check for a keyword */
734 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
735 /* Reserved word found */
738 /* No reserved word, check for special symbols */
739 if (token[0] == '_' && token[1] == '_') {
740 /* Special symbols */
741 if (strcmp (token+2, "FILE__") == 0) {
742 NextTok.IVal = AddLiteral (GetCurrentFile());
743 NextTok.Tok = TOK_SCONST;
745 } else if (strcmp (token+2, "LINE__") == 0) {
746 NextTok.Tok = TOK_ICONST;
747 NextTok.IVal = GetCurrentLine();
748 NextTok.Type = type_int;
750 } else if (strcmp (token+2, "func__") == 0) {
751 /* __func__ is only defined in functions */
753 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
754 NextTok.Tok = TOK_SCONST;
760 /* No reserved word but identifier */
761 strcpy (NextTok.Ident, token);
762 NextTok.Tok = TOK_IDENT;
766 /* Monstrous switch statement ahead... */
774 NextTok.Tok = TOK_BOOL_NOT;
785 SetTok (TOK_MOD_ASSIGN);
787 NextTok.Tok = TOK_MOD;
795 SetTok (TOK_BOOL_AND);
798 SetTok (TOK_AND_ASSIGN);
801 NextTok.Tok = TOK_AND;
820 SetTok (TOK_MUL_ASSIGN);
822 NextTok.Tok = TOK_STAR;
833 SetTok (TOK_PLUS_ASSIGN);
836 NextTok.Tok = TOK_PLUS;
851 SetTok (TOK_MINUS_ASSIGN);
854 SetTok (TOK_PTR_REF);
857 NextTok.Tok = TOK_MINUS;
866 SetTok (TOK_ELLIPSIS);
871 NextTok.Tok = TOK_DOT;
878 SetTok (TOK_DIV_ASSIGN);
880 NextTok.Tok = TOK_DIV;
901 SetTok (TOK_SHL_ASSIGN);
903 NextTok.Tok = TOK_SHL;
907 NextTok.Tok = TOK_LT;
916 NextTok.Tok = TOK_ASSIGN;
929 SetTok (TOK_SHR_ASSIGN);
931 NextTok.Tok = TOK_SHR;
935 NextTok.Tok = TOK_GT;
954 SetTok (TOK_XOR_ASSIGN);
956 NextTok.Tok = TOK_XOR;
968 SetTok (TOK_BOOL_OR);
971 SetTok (TOK_OR_ASSIGN);
974 NextTok.Tok = TOK_OR;
995 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
996 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
997 * This routine is used for error recovery.
1000 while (CurTok.Tok != TOK_CEOF) {
1002 /* Check if the current token is in the token list */
1004 for (I = 0; I < TokenCount; ++I) {
1005 if (CurTok.Tok == TokenList[I]) {
1006 /* Found a token in the list */
1011 /* Not in the list: Skip it */
1019 int Consume (token_t Token, const char* ErrorMsg)
1020 /* Eat token if it is the next in the input stream, otherwise print an error
1021 * message. Returns true if the token was found and false otherwise.
1024 if (CurTok.Tok == Token) {
1035 int ConsumeColon (void)
1036 /* Check for a colon and skip it. */
1038 return Consume (TOK_COLON, "`:' expected");
1043 int ConsumeSemi (void)
1044 /* Check for a semicolon and skip it. */
1046 /* Try do be smart about typos... */
1047 if (CurTok.Tok == TOK_SEMI) {
1051 Error ("`;' expected");
1052 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1061 int ConsumeComma (void)
1062 /* Check for a comma and skip it. */
1064 /* Try do be smart about typos... */
1065 if (CurTok.Tok == TOK_COMMA) {
1069 Error ("`,' expected");
1070 if (CurTok.Tok == TOK_SEMI) {
1079 int ConsumeLParen (void)
1080 /* Check for a left parenthesis and skip it */
1082 return Consume (TOK_LPAREN, "`(' expected");
1087 int ConsumeRParen (void)
1088 /* Check for a right parenthesis and skip it */
1090 return Consume (TOK_RPAREN, "`)' expected");
1095 int ConsumeLBrack (void)
1096 /* Check for a left bracket and skip it */
1098 return Consume (TOK_LBRACK, "`[' expected");
1103 int ConsumeRBrack (void)
1104 /* Check for a right bracket and skip it */
1106 return Consume (TOK_RBRACK, "`]' expected");
1111 int ConsumeLCurly (void)
1112 /* Check for a left curly brace and skip it */
1114 return Consume (TOK_LCURLY, "`{' expected");
1119 int ConsumeRCurly (void)
1120 /* Check for a right curly brace and skip it */
1122 return Consume (TOK_RCURLY, "`}' expected");