1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2010, 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 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 Token CurTok; /* The current token */
71 Token NextTok; /* The next token */
77 TT_C89 = 0x01 << STD_C89, /* Token valid in C89 */
78 TT_C99 = 0x01 << STD_C99, /* Token valid in C99 */
79 TT_CC65 = 0x01 << STD_CC65 /* Token valid in cc65 */
83 static const struct Keyword {
84 char* Key; /* Keyword name */
85 unsigned char Tok; /* The token */
86 unsigned char Std; /* Token supported in which standards? */
88 { "_Pragma", TOK_PRAGMA, TT_C89 | TT_C99 | TT_CC65 }, /* !! */
89 { "__AX__", TOK_AX, TT_C89 | TT_C99 | TT_CC65 },
90 { "__A__", TOK_A, TT_C89 | TT_C99 | TT_CC65 },
91 { "__EAX__", TOK_EAX, TT_C89 | TT_C99 | TT_CC65 },
92 { "__X__", TOK_X, TT_C89 | TT_C99 | TT_CC65 },
93 { "__Y__", TOK_Y, TT_C89 | TT_C99 | TT_CC65 },
94 { "__asm__", TOK_ASM, TT_C89 | TT_C99 | TT_CC65 },
95 { "__attribute__", TOK_ATTRIBUTE, TT_C89 | TT_C99 | TT_CC65 },
96 { "__cdecl__", TOK_CDECL, TT_C89 | TT_C99 | TT_CC65 },
97 { "__far__", TOK_FAR, TT_C89 | TT_C99 | TT_CC65 },
98 { "__fastcall__", TOK_FASTCALL, TT_C89 | TT_C99 | TT_CC65 },
99 { "__inline__", TOK_INLINE, TT_C89 | TT_C99 | TT_CC65 },
100 { "__near__", TOK_NEAR, TT_C89 | TT_C99 | TT_CC65 },
101 { "asm", TOK_ASM, TT_CC65 },
102 { "auto", TOK_AUTO, TT_C89 | TT_C99 | TT_CC65 },
103 { "break", TOK_BREAK, TT_C89 | TT_C99 | TT_CC65 },
104 { "case", TOK_CASE, TT_C89 | TT_C99 | TT_CC65 },
105 { "cdecl", TOK_CDECL, TT_CC65 },
106 { "char", TOK_CHAR, TT_C89 | TT_C99 | TT_CC65 },
107 { "const", TOK_CONST, TT_C89 | TT_C99 | TT_CC65 },
108 { "continue", TOK_CONTINUE, TT_C89 | TT_C99 | TT_CC65 },
109 { "default", TOK_DEFAULT, TT_C89 | TT_C99 | TT_CC65 },
110 { "do", TOK_DO, TT_C89 | TT_C99 | TT_CC65 },
111 { "double", TOK_DOUBLE, TT_C89 | TT_C99 | TT_CC65 },
112 { "else", TOK_ELSE, TT_C89 | TT_C99 | TT_CC65 },
113 { "enum", TOK_ENUM, TT_C89 | TT_C99 | TT_CC65 },
114 { "extern", TOK_EXTERN, TT_C89 | TT_C99 | TT_CC65 },
115 { "far", TOK_FAR, TT_CC65 },
116 { "fastcall", TOK_FASTCALL, TT_CC65 },
117 { "float", TOK_FLOAT, TT_C89 | TT_C99 | TT_CC65 },
118 { "for", TOK_FOR, TT_C89 | TT_C99 | TT_CC65 },
119 { "goto", TOK_GOTO, TT_C89 | TT_C99 | TT_CC65 },
120 { "if", TOK_IF, TT_C89 | TT_C99 | TT_CC65 },
121 { "inline", TOK_INLINE, TT_C99 | TT_CC65 },
122 { "int", TOK_INT, TT_C89 | TT_C99 | TT_CC65 },
123 { "long", TOK_LONG, TT_C89 | TT_C99 | TT_CC65 },
124 { "near", TOK_NEAR, TT_CC65 },
125 { "register", TOK_REGISTER, TT_C89 | TT_C99 | TT_CC65 },
126 { "restrict", TOK_RESTRICT, TT_C99 | TT_CC65 },
127 { "return", TOK_RETURN, TT_C89 | TT_C99 | TT_CC65 },
128 { "short", TOK_SHORT, TT_C89 | TT_C99 | TT_CC65 },
129 { "signed", TOK_SIGNED, TT_C89 | TT_C99 | TT_CC65 },
130 { "sizeof", TOK_SIZEOF, TT_C89 | TT_C99 | TT_CC65 },
131 { "static", TOK_STATIC, TT_C89 | TT_C99 | TT_CC65 },
132 { "struct", TOK_STRUCT, TT_C89 | TT_C99 | TT_CC65 },
133 { "switch", TOK_SWITCH, TT_C89 | TT_C99 | TT_CC65 },
134 { "typedef", TOK_TYPEDEF, TT_C89 | TT_C99 | TT_CC65 },
135 { "union", TOK_UNION, TT_C89 | TT_C99 | TT_CC65 },
136 { "unsigned", TOK_UNSIGNED, TT_C89 | TT_C99 | TT_CC65 },
137 { "void", TOK_VOID, TT_C89 | TT_C99 | TT_CC65 },
138 { "volatile", TOK_VOLATILE, TT_C89 | TT_C99 | TT_CC65 },
139 { "while", TOK_WHILE, TT_C89 | TT_C99 | TT_CC65 },
141 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
145 /* Stuff for determining the type of an integer constant */
149 #define IT_ULONG 0x08
153 /*****************************************************************************/
155 /*****************************************************************************/
159 static int CmpKey (const void* Key, const void* Elem)
160 /* Compare function for bsearch */
162 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
167 static token_t FindKey (const char* Key)
168 /* Find a keyword and return the token. Return IDENT if the token is not a
173 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
174 if (K && (K->Std & (0x01 << IS_Get (&Standard))) != 0) {
183 static int SkipWhite (void)
184 /* Skip white space in the input stream, reading and preprocessing new lines
185 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
189 while (CurC == '\0') {
190 if (NextLine () == 0) {
195 if (IsSpace (CurC)) {
205 int TokIsFuncSpec (const Token* T)
206 /* Return true if the token is a function specifier */
208 return (T->Tok == TOK_INLINE) || (T->Tok == TOK_FASTCALL) ||
209 (T->Tok == TOK_NEAR) || (T->Tok == TOK_FAR);
214 void SymName (char* S)
215 /* Read a symbol from the input stream. The first character must have been
216 * checked before calling this function. The buffer is expected to be at
217 * least of size MAX_IDENTLEN+1.
222 if (Len < MAX_IDENTLEN) {
227 } while (IsIdent (CurC) || IsDigit (CurC));
234 /* If a symbol follows, read it and return 1, otherwise return 0 */
236 if (IsIdent (CurC)) {
246 static void UnknownChar (char C)
247 /* Error message for unknown character */
249 Error ("Invalid input character with code %02X", C & 0xFF);
250 NextChar (); /* Skip */
255 static void SetTok (int tok)
256 /* Set NextTok.Tok and bump line ptr */
264 static int ParseChar (void)
265 /* Parse a character. Converts escape chars into character codes. */
270 /* Check for escape chars */
309 /* Hex character constant */
310 if (!IsXDigit (NextC)) {
311 Error ("\\x used with no following hex digits");
316 while (IsXDigit (NextC)) {
317 if ((C << 4) >= 256) {
319 Error ("Hex character constant out of range");
323 C = (C << 4) | HexVal (NextC);
340 while (IsODigit (NextC)) {
341 if ((C << 3) >= 256) {
343 Error ("Octal character constant out of range");
347 C = (C << 3) | HexVal (NextC);
353 Error ("Illegal character constant");
355 /* Try to do error recovery, otherwise the compiler will spit
356 * out thousands of errors in this place and abort.
358 if (CurC != '\'' && CurC != '\0') {
359 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
369 /* Skip the character read */
372 /* Do correct sign extension */
373 return SignExtendChar (C);
378 static void CharConst (void)
379 /* Parse a character constant. */
389 /* Check for closing quote */
391 Error ("`\'' expected");
397 /* Setup values and attributes */
398 NextTok.Tok = TOK_CCONST;
400 /* Translate into target charset */
401 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
403 /* Character constants have type int */
404 NextTok.Type = type_int;
409 static void StringConst (void)
410 /* Parse a quoted string */
413 StrBuf S = AUTO_STRBUF_INITIALIZER;
415 /* Assume next token is a string constant */
416 NextTok.Tok = TOK_SCONST;
418 /* Concatenate strings. If at least one of the concenated strings is a wide
419 * character literal, the whole string is a wide char literal, otherwise
420 * it's a normal string literal.
424 /* Check if this is a normal or a wide char string */
425 if (CurC == 'L' && NextC == '\"') {
426 /* Wide character literal */
427 NextTok.Tok = TOK_WCSCONST;
430 } else if (CurC == '\"') {
431 /* Skip the quote char */
438 /* Read until end of string */
439 while (CurC != '\"') {
441 Error ("Unexpected newline");
444 SB_AppendChar (&S, ParseChar ());
447 /* Skip closing quote char if there was one */
450 /* Skip white space, read new input */
455 /* Terminate the string */
456 SB_AppendChar (&S, '\0');
458 /* Add the whole string to the literal pool */
459 NextTok.SVal = AddLiteralStr (&S);
461 /* Free the buffer */
467 static void NumericConst (void)
468 /* Parse a numeric constant */
470 unsigned Base; /* Temporary number base */
471 unsigned Prefix; /* Base according to prefix */
472 StrBuf S = STATIC_STRBUF_INITIALIZER;
476 unsigned long IVal; /* Value */
478 /* Check for a leading hex or octal prefix and determine the possible
482 /* Gobble 0 and examine next char */
484 if (toupper (CurC) == 'X') {
486 NextChar (); /* gobble "x" */
488 Base = 10; /* Assume 10 for now - see below */
489 Prefix = 8; /* Actual prefix says octal */
495 /* Because floating point numbers don't have octal prefixes (a number
496 * with a leading zero is decimal), we first have to read the number
497 * before converting it, so we can determine if it's a float or an
500 while (IsXDigit (CurC) && HexVal (CurC) < Base) {
501 SB_AppendChar (&S, CurC);
506 /* The following character tells us if we have an integer or floating
507 * point constant. Note: Hexadecimal floating point constants aren't
510 IsFloat = (CurC == '.' ||
511 (Base == 10 && toupper (CurC) == 'E') ||
512 (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
514 /* If we don't have a floating point type, an octal prefix results in an
517 if (!IsFloat && Prefix == 8) {
521 /* Since we do now know the correct base, convert the remembered input
526 while ((C = SB_Get (&S)) != '\0') {
527 DigitVal = HexVal (C);
528 if (DigitVal >= Base) {
529 Error ("Numeric constant contains digits beyond the radix");
531 IVal = (IVal * Base) + DigitVal;
534 /* We don't need the string buffer any longer */
537 /* Distinguish between integer and floating point constants */
543 /* Check for a suffix and determine the possible types */
545 if (toupper (CurC) == 'U') {
548 if (toupper (CurC) != 'L') {
549 Types = IT_UINT | IT_ULONG;
554 } else if (toupper (CurC) == 'L') {
557 if (toupper (CurC) != 'U') {
558 Types = IT_LONG | IT_ULONG;
566 /* Decimal constants are of any type but uint */
567 Types = IT_INT | IT_LONG | IT_ULONG;
569 /* Octal or hex constants are of any type */
570 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
574 /* Check the range to determine the type */
576 /* Out of range for int */
578 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
579 * allowed, and we don't have a type specifying suffix, emit a
580 * warning, because the constant is of type long.
582 if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
583 Warning ("Constant is long");
587 /* Out of range for unsigned int */
590 if (IVal > 0x7FFFFFFF) {
591 /* Out of range for long int */
595 /* Now set the type string to the smallest type in types */
596 if (Types & IT_INT) {
597 NextTok.Type = type_int;
598 } else if (Types & IT_UINT) {
599 NextTok.Type = type_uint;
600 } else if (Types & IT_LONG) {
601 NextTok.Type = type_long;
603 NextTok.Type = type_ulong;
606 /* Set the value and the token */
608 NextTok.Tok = TOK_ICONST;
613 Double FVal = FP_D_FromInt (IVal); /* Convert to double */
615 /* Check for a fractional part and read it */
623 /* Read fractional digits */
624 Scale = FP_D_Make (1.0);
625 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
626 /* Get the value of this digit */
627 Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
628 /* Add it to the float value */
629 FVal = FP_D_Add (FVal, FracVal);
631 Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
637 /* Check for an exponent and read it */
638 if ((Base == 16 && toupper (CurC) == 'F') ||
639 (Base == 10 && toupper (CurC) == 'E')) {
645 /* Skip the exponent notifier */
648 /* Read an optional sign */
653 } else if (CurC == '+') {
657 /* Read exponent digits. Since we support only 32 bit floats
658 * with a maximum exponent of +-/127, we read the exponent
659 * part as integer with up to 3 digits and drop the remainder.
660 * This avoids an overflow of Exp. The exponent is always
661 * decimal, even for hex float consts.
665 while (IsDigit (CurC)) {
667 Exp = Exp * 10 + HexVal (CurC);
672 /* Check for errors: We must have exponent digits, and not more
676 Error ("Floating constant exponent has no digits");
677 } else if (Digits > 3) {
678 Warning ("Floating constant exponent is too large");
681 /* Scale the exponent and adjust the value accordingly */
683 FVal = FP_D_Mul (FVal, FP_D_Make (pow (10, Exp)));
687 /* Check for a suffix and determine the type of the constant */
688 if (toupper (CurC) == 'F') {
690 NextTok.Type = type_float;
692 NextTok.Type = type_double;
695 /* Set the value and the token */
697 NextTok.Tok = TOK_FCONST;
704 void NextToken (void)
705 /* Get next token from input stream */
709 /* We have to skip white space here before shifting tokens, since the
710 * tokens and the current line info is invalid at startup and will get
711 * initialized by reading the first time from the file. Remember if
712 * we were at end of input and handle that later.
714 int GotEOF = (SkipWhite() == 0);
716 /* Current token is the lookahead token */
718 ReleaseLineInfo (CurTok.LI);
722 /* When reading the first time from the file, the line info in NextTok,
723 * which was copied to CurTok is invalid. Since the information from
724 * the token is used for error messages, we must make it valid.
726 if (CurTok.LI == 0) {
727 CurTok.LI = UseLineInfo (GetCurLineInfo ());
730 /* Remember the starting position of the next token */
731 NextTok.LI = UseLineInfo (GetCurLineInfo ());
733 /* Now handle end of input. */
735 /* End of file reached */
736 NextTok.Tok = TOK_CEOF;
740 /* Determine the next token from the lookahead */
741 if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
747 /* Check for wide character literals */
748 if (CurC == 'L' && NextC == '\"') {
753 /* Check for keywords and identifiers */
756 /* Check for a keyword */
757 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
758 /* Reserved word found */
761 /* No reserved word, check for special symbols */
762 if (token[0] == '_' && token[1] == '_') {
763 /* Special symbols */
764 if (strcmp (token+2, "FILE__") == 0) {
765 NextTok.SVal = AddLiteral (GetCurrentFile());
766 NextTok.Tok = TOK_SCONST;
768 } else if (strcmp (token+2, "LINE__") == 0) {
769 NextTok.Tok = TOK_ICONST;
770 NextTok.IVal = GetCurrentLine();
771 NextTok.Type = type_int;
773 } else if (strcmp (token+2, "func__") == 0) {
774 /* __func__ is only defined in functions */
776 NextTok.SVal = AddLiteral (F_GetFuncName (CurrentFunc));
777 NextTok.Tok = TOK_SCONST;
783 /* No reserved word but identifier */
784 strcpy (NextTok.Ident, token);
785 NextTok.Tok = TOK_IDENT;
789 /* Monstrous switch statement ahead... */
797 NextTok.Tok = TOK_BOOL_NOT;
808 SetTok (TOK_MOD_ASSIGN);
810 NextTok.Tok = TOK_MOD;
818 SetTok (TOK_BOOL_AND);
821 SetTok (TOK_AND_ASSIGN);
824 NextTok.Tok = TOK_AND;
843 SetTok (TOK_MUL_ASSIGN);
845 NextTok.Tok = TOK_STAR;
856 SetTok (TOK_PLUS_ASSIGN);
859 NextTok.Tok = TOK_PLUS;
874 SetTok (TOK_MINUS_ASSIGN);
877 SetTok (TOK_PTR_REF);
880 NextTok.Tok = TOK_MINUS;
889 SetTok (TOK_ELLIPSIS);
894 NextTok.Tok = TOK_DOT;
901 SetTok (TOK_DIV_ASSIGN);
903 NextTok.Tok = TOK_DIV;
924 SetTok (TOK_SHL_ASSIGN);
926 NextTok.Tok = TOK_SHL;
930 NextTok.Tok = TOK_LT;
939 NextTok.Tok = TOK_ASSIGN;
952 SetTok (TOK_SHR_ASSIGN);
954 NextTok.Tok = TOK_SHR;
958 NextTok.Tok = TOK_GT;
977 SetTok (TOK_XOR_ASSIGN);
979 NextTok.Tok = TOK_XOR;
991 SetTok (TOK_BOOL_OR);
994 SetTok (TOK_OR_ASSIGN);
997 NextTok.Tok = TOK_OR;
1002 SetTok (TOK_RCURLY);
1018 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
1019 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
1020 * This routine is used for error recovery.
1023 while (CurTok.Tok != TOK_CEOF) {
1025 /* Check if the current token is in the token list */
1027 for (I = 0; I < TokenCount; ++I) {
1028 if (CurTok.Tok == TokenList[I]) {
1029 /* Found a token in the list */
1034 /* Not in the list: Skip it */
1042 int Consume (token_t Token, const char* ErrorMsg)
1043 /* Eat token if it is the next in the input stream, otherwise print an error
1044 * message. Returns true if the token was found and false otherwise.
1047 if (CurTok.Tok == Token) {
1051 Error ("%s", ErrorMsg);
1058 int ConsumeColon (void)
1059 /* Check for a colon and skip it. */
1061 return Consume (TOK_COLON, "`:' expected");
1066 int ConsumeSemi (void)
1067 /* Check for a semicolon and skip it. */
1069 /* Try do be smart about typos... */
1070 if (CurTok.Tok == TOK_SEMI) {
1074 Error ("`;' expected");
1075 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1084 int ConsumeComma (void)
1085 /* Check for a comma and skip it. */
1087 /* Try do be smart about typos... */
1088 if (CurTok.Tok == TOK_COMMA) {
1092 Error ("`,' expected");
1093 if (CurTok.Tok == TOK_SEMI) {
1102 int ConsumeLParen (void)
1103 /* Check for a left parenthesis and skip it */
1105 return Consume (TOK_LPAREN, "`(' expected");
1110 int ConsumeRParen (void)
1111 /* Check for a right parenthesis and skip it */
1113 return Consume (TOK_RPAREN, "`)' expected");
1118 int ConsumeLBrack (void)
1119 /* Check for a left bracket and skip it */
1121 return Consume (TOK_LBRACK, "`[' expected");
1126 int ConsumeRBrack (void)
1127 /* Check for a right bracket and skip it */
1129 return Consume (TOK_RBRACK, "`]' expected");
1134 int ConsumeLCurly (void)
1135 /* Check for a left curly brace and skip it */
1137 return Consume (TOK_LCURLY, "`{' expected");
1142 int ConsumeRCurly (void)
1143 /* Check for a right curly brace and skip it */
1145 return Consume (TOK_RCURLY, "`}' expected");