1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2009, 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 { "__far__", TOK_FAR, TT_C89 | TT_C99 | TT_CC65 },
97 { "__fastcall__", TOK_FASTCALL, TT_C89 | TT_C99 | TT_CC65 },
98 { "__inline__", TOK_INLINE, TT_C89 | TT_C99 | TT_CC65 },
99 { "__near__", TOK_NEAR, TT_C89 | TT_C99 | TT_CC65 },
100 { "asm", TOK_ASM, TT_CC65 },
101 { "auto", TOK_AUTO, TT_C89 | TT_C99 | TT_CC65 },
102 { "break", TOK_BREAK, TT_C89 | TT_C99 | TT_CC65 },
103 { "case", TOK_CASE, TT_C89 | TT_C99 | TT_CC65 },
104 { "char", TOK_CHAR, TT_C89 | TT_C99 | TT_CC65 },
105 { "const", TOK_CONST, TT_C89 | TT_C99 | TT_CC65 },
106 { "continue", TOK_CONTINUE, TT_C89 | TT_C99 | TT_CC65 },
107 { "default", TOK_DEFAULT, TT_C89 | TT_C99 | TT_CC65 },
108 { "do", TOK_DO, TT_C89 | TT_C99 | TT_CC65 },
109 { "double", TOK_DOUBLE, TT_C89 | TT_C99 | TT_CC65 },
110 { "else", TOK_ELSE, TT_C89 | TT_C99 | TT_CC65 },
111 { "enum", TOK_ENUM, TT_C89 | TT_C99 | TT_CC65 },
112 { "extern", TOK_EXTERN, TT_C89 | TT_C99 | TT_CC65 },
113 { "far", TOK_FAR, TT_CC65 },
114 { "fastcall", TOK_FASTCALL, TT_CC65 },
115 { "float", TOK_FLOAT, TT_C89 | TT_C99 | TT_CC65 },
116 { "for", TOK_FOR, TT_C89 | TT_C99 | TT_CC65 },
117 { "goto", TOK_GOTO, TT_C89 | TT_C99 | TT_CC65 },
118 { "if", TOK_IF, TT_C89 | TT_C99 | TT_CC65 },
119 { "inline", TOK_INLINE, TT_C99 | TT_CC65 },
120 { "int", TOK_INT, TT_C89 | TT_C99 | TT_CC65 },
121 { "long", TOK_LONG, TT_C89 | TT_C99 | TT_CC65 },
122 { "near", TOK_NEAR, TT_CC65 },
123 { "register", TOK_REGISTER, TT_C89 | TT_C99 | TT_CC65 },
124 { "restrict", TOK_RESTRICT, TT_C99 | TT_CC65 },
125 { "return", TOK_RETURN, TT_C89 | TT_C99 | TT_CC65 },
126 { "short", TOK_SHORT, TT_C89 | TT_C99 | TT_CC65 },
127 { "signed", TOK_SIGNED, TT_C89 | TT_C99 | TT_CC65 },
128 { "sizeof", TOK_SIZEOF, TT_C89 | TT_C99 | TT_CC65 },
129 { "static", TOK_STATIC, TT_C89 | TT_C99 | TT_CC65 },
130 { "struct", TOK_STRUCT, TT_C89 | TT_C99 | TT_CC65 },
131 { "switch", TOK_SWITCH, TT_C89 | TT_C99 | TT_CC65 },
132 { "typedef", TOK_TYPEDEF, TT_C89 | TT_C99 | TT_CC65 },
133 { "union", TOK_UNION, TT_C89 | TT_C99 | TT_CC65 },
134 { "unsigned", TOK_UNSIGNED, TT_C89 | TT_C99 | TT_CC65 },
135 { "void", TOK_VOID, TT_C89 | TT_C99 | TT_CC65 },
136 { "volatile", TOK_VOLATILE, TT_C89 | TT_C99 | TT_CC65 },
137 { "while", TOK_WHILE, TT_C89 | TT_C99 | TT_CC65 },
139 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
143 /* Stuff for determining the type of an integer constant */
147 #define IT_ULONG 0x08
151 /*****************************************************************************/
153 /*****************************************************************************/
157 static int CmpKey (const void* Key, const void* Elem)
158 /* Compare function for bsearch */
160 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
165 static token_t FindKey (const char* Key)
166 /* Find a keyword and return the token. Return IDENT if the token is not a
171 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
172 if (K && (K->Std & (0x01 << IS_Get (&Standard))) != 0) {
181 static int SkipWhite (void)
182 /* Skip white space in the input stream, reading and preprocessing new lines
183 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
187 while (CurC == '\0') {
188 if (NextLine () == 0) {
193 if (IsSpace (CurC)) {
203 int TokIsFuncSpec (const Token* T)
204 /* Return true if the token is a function specifier */
206 return (T->Tok == TOK_INLINE) || (T->Tok == TOK_FASTCALL) ||
207 (T->Tok == TOK_NEAR) || (T->Tok == TOK_FAR);
212 void SymName (char* S)
213 /* Read a symbol from the input stream. The first character must have been
214 * checked before calling this function. The buffer is expected to be at
215 * least of size MAX_IDENTLEN+1.
220 if (Len < MAX_IDENTLEN) {
225 } while (IsIdent (CurC) || IsDigit (CurC));
232 /* If a symbol follows, read it and return 1, otherwise return 0 */
234 if (IsIdent (CurC)) {
244 static void UnknownChar (char C)
245 /* Error message for unknown character */
247 Error ("Invalid input character with code %02X", C & 0xFF);
248 NextChar (); /* Skip */
253 static void SetTok (int tok)
254 /* Set NextTok.Tok and bump line ptr */
262 static int ParseChar (void)
263 /* Parse a character. Converts escape chars into character codes. */
268 /* Check for escape chars */
307 /* Hex character constant */
308 if (!IsXDigit (NextC)) {
309 Error ("\\x used with no following hex digits");
314 while (IsXDigit (NextC)) {
315 if ((C << 4) >= 256) {
317 Error ("Hex character constant out of range");
321 C = (C << 4) | HexVal (NextC);
338 while (IsODigit (NextC)) {
339 if ((C << 3) >= 256) {
341 Error ("Octal character constant out of range");
345 C = (C << 3) | HexVal (NextC);
351 Error ("Illegal character constant");
353 /* Try to do error recovery, otherwise the compiler will spit
354 * out thousands of errors in this place and abort.
356 if (CurC != '\'' && CurC != '\0') {
357 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
367 /* Skip the character read */
370 /* Do correct sign extension */
371 return SignExtendChar (C);
376 static void CharConst (void)
377 /* Parse a character constant. */
387 /* Check for closing quote */
389 Error ("`\'' expected");
395 /* Setup values and attributes */
396 NextTok.Tok = TOK_CCONST;
398 /* Translate into target charset */
399 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
401 /* Character constants have type int */
402 NextTok.Type = type_int;
407 static void StringConst (void)
408 /* Parse a quoted string */
410 NextTok.IVal = GetLiteralPoolOffs ();
411 NextTok.Tok = TOK_SCONST;
413 /* Concatenate strings. If at least one of the concenated strings is a wide
414 * character literal, the whole string is a wide char literal, otherwise
415 * it's a normal string literal.
419 /* Check if this is a normal or a wide char string */
420 if (CurC == 'L' && NextC == '\"') {
421 /* Wide character literal */
422 NextTok.Tok = TOK_WCSCONST;
425 } else if (CurC == '\"') {
426 /* Skip the quote char */
433 /* Read until end of string */
434 while (CurC != '\"') {
436 Error ("Unexpected newline");
439 AddLiteralChar (ParseChar ());
442 /* Skip closing quote char if there was one */
445 /* Skip white space, read new input */
450 /* Terminate the string */
451 AddLiteralChar ('\0');
456 static void NumericConst (void)
457 /* Parse a numeric constant */
459 unsigned Base; /* Temporary number base */
460 unsigned Prefix; /* Base according to prefix */
461 StrBuf S = STATIC_STRBUF_INITIALIZER;
465 unsigned long IVal; /* Value */
467 /* Check for a leading hex or octal prefix and determine the possible
471 /* Gobble 0 and examine next char */
473 if (toupper (CurC) == 'X') {
475 NextChar (); /* gobble "x" */
477 Base = 10; /* Assume 10 for now - see below */
478 Prefix = 8; /* Actual prefix says octal */
484 /* Because floating point numbers don't have octal prefixes (a number
485 * with a leading zero is decimal), we first have to read the number
486 * before converting it, so we can determine if it's a float or an
489 while (IsXDigit (CurC) && HexVal (CurC) < Base) {
490 SB_AppendChar (&S, CurC);
495 /* The following character tells us if we have an integer or floating
496 * point constant. Note: Hexadecimal floating point constants aren't
499 IsFloat = (CurC == '.' ||
500 (Base == 10 && toupper (CurC) == 'E') ||
501 (Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
503 /* If we don't have a floating point type, an octal prefix results in an
506 if (!IsFloat && Prefix == 8) {
510 /* Since we do now know the correct base, convert the remembered input
515 while ((C = SB_Get (&S)) != '\0') {
516 DigitVal = HexVal (C);
517 if (DigitVal >= Base) {
518 Error ("Numeric constant contains digits beyond the radix");
520 IVal = (IVal * Base) + DigitVal;
523 /* We don't need the string buffer any longer */
526 /* Distinguish between integer and floating point constants */
532 /* Check for a suffix and determine the possible types */
534 if (toupper (CurC) == 'U') {
537 if (toupper (CurC) != 'L') {
538 Types = IT_UINT | IT_ULONG;
543 } else if (toupper (CurC) == 'L') {
546 if (toupper (CurC) != 'U') {
547 Types = IT_LONG | IT_ULONG;
555 /* Decimal constants are of any type but uint */
556 Types = IT_INT | IT_LONG | IT_ULONG;
558 /* Octal or hex constants are of any type */
559 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
563 /* Check the range to determine the type */
565 /* Out of range for int */
567 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
568 * allowed, and we don't have a type specifying suffix, emit a
569 * warning, because the constant is of type long.
571 if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
572 Warning ("Constant is long");
576 /* Out of range for unsigned int */
579 if (IVal > 0x7FFFFFFF) {
580 /* Out of range for long int */
584 /* Now set the type string to the smallest type in types */
585 if (Types & IT_INT) {
586 NextTok.Type = type_int;
587 } else if (Types & IT_UINT) {
588 NextTok.Type = type_uint;
589 } else if (Types & IT_LONG) {
590 NextTok.Type = type_long;
592 NextTok.Type = type_ulong;
595 /* Set the value and the token */
597 NextTok.Tok = TOK_ICONST;
602 Double FVal = FP_D_FromInt (IVal); /* Convert to double */
604 /* Check for a fractional part and read it */
612 /* Read fractional digits */
613 Scale = FP_D_Make (1.0);
614 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
615 /* Get the value of this digit */
616 Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
617 /* Add it to the float value */
618 FVal = FP_D_Add (FVal, FracVal);
620 Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
626 /* Check for an exponent and read it */
627 if ((Base == 16 && toupper (CurC) == 'F') ||
628 (Base == 10 && toupper (CurC) == 'E')) {
634 /* Skip the exponent notifier */
637 /* Read an optional sign */
642 } else if (CurC == '+') {
646 /* Read exponent digits. Since we support only 32 bit floats
647 * with a maximum exponent of +-/127, we read the exponent
648 * part as integer with up to 3 digits and drop the remainder.
649 * This avoids an overflow of Exp. The exponent is always
650 * decimal, even for hex float consts.
654 while (IsDigit (CurC)) {
656 Exp = Exp * 10 + HexVal (CurC);
661 /* Check for errors: We must have exponent digits, and not more
665 Error ("Floating constant exponent has no digits");
666 } else if (Digits > 3) {
667 Warning ("Floating constant exponent is too large");
670 /* Scale the exponent and adjust the value accordingly */
672 FVal = FP_D_Mul (FVal, FP_D_Make (pow (10, Exp)));
676 /* Check for a suffix and determine the type of the constant */
677 if (toupper (CurC) == 'F') {
679 NextTok.Type = type_float;
681 NextTok.Type = type_double;
684 /* Set the value and the token */
686 NextTok.Tok = TOK_FCONST;
693 void NextToken (void)
694 /* Get next token from input stream */
698 /* We have to skip white space here before shifting tokens, since the
699 * tokens and the current line info is invalid at startup and will get
700 * initialized by reading the first time from the file. Remember if
701 * we were at end of input and handle that later.
703 int GotEOF = (SkipWhite() == 0);
705 /* Current token is the lookahead token */
707 ReleaseLineInfo (CurTok.LI);
711 /* When reading the first time from the file, the line info in NextTok,
712 * which was copied to CurTok is invalid. Since the information from
713 * the token is used for error messages, we must make it valid.
715 if (CurTok.LI == 0) {
716 CurTok.LI = UseLineInfo (GetCurLineInfo ());
719 /* Remember the starting position of the next token */
720 NextTok.LI = UseLineInfo (GetCurLineInfo ());
722 /* Now handle end of input. */
724 /* End of file reached */
725 NextTok.Tok = TOK_CEOF;
729 /* Determine the next token from the lookahead */
730 if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
736 /* Check for wide character literals */
737 if (CurC == 'L' && NextC == '\"') {
742 /* Check for keywords and identifiers */
745 /* Check for a keyword */
746 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
747 /* Reserved word found */
750 /* No reserved word, check for special symbols */
751 if (token[0] == '_' && token[1] == '_') {
752 /* Special symbols */
753 if (strcmp (token+2, "FILE__") == 0) {
754 NextTok.IVal = AddLiteral (GetCurrentFile());
755 NextTok.Tok = TOK_SCONST;
757 } else if (strcmp (token+2, "LINE__") == 0) {
758 NextTok.Tok = TOK_ICONST;
759 NextTok.IVal = GetCurrentLine();
760 NextTok.Type = type_int;
762 } else if (strcmp (token+2, "func__") == 0) {
763 /* __func__ is only defined in functions */
765 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
766 NextTok.Tok = TOK_SCONST;
772 /* No reserved word but identifier */
773 strcpy (NextTok.Ident, token);
774 NextTok.Tok = TOK_IDENT;
778 /* Monstrous switch statement ahead... */
786 NextTok.Tok = TOK_BOOL_NOT;
797 SetTok (TOK_MOD_ASSIGN);
799 NextTok.Tok = TOK_MOD;
807 SetTok (TOK_BOOL_AND);
810 SetTok (TOK_AND_ASSIGN);
813 NextTok.Tok = TOK_AND;
832 SetTok (TOK_MUL_ASSIGN);
834 NextTok.Tok = TOK_STAR;
845 SetTok (TOK_PLUS_ASSIGN);
848 NextTok.Tok = TOK_PLUS;
863 SetTok (TOK_MINUS_ASSIGN);
866 SetTok (TOK_PTR_REF);
869 NextTok.Tok = TOK_MINUS;
878 SetTok (TOK_ELLIPSIS);
883 NextTok.Tok = TOK_DOT;
890 SetTok (TOK_DIV_ASSIGN);
892 NextTok.Tok = TOK_DIV;
913 SetTok (TOK_SHL_ASSIGN);
915 NextTok.Tok = TOK_SHL;
919 NextTok.Tok = TOK_LT;
928 NextTok.Tok = TOK_ASSIGN;
941 SetTok (TOK_SHR_ASSIGN);
943 NextTok.Tok = TOK_SHR;
947 NextTok.Tok = TOK_GT;
966 SetTok (TOK_XOR_ASSIGN);
968 NextTok.Tok = TOK_XOR;
980 SetTok (TOK_BOOL_OR);
983 SetTok (TOK_OR_ASSIGN);
986 NextTok.Tok = TOK_OR;
1007 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
1008 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
1009 * This routine is used for error recovery.
1012 while (CurTok.Tok != TOK_CEOF) {
1014 /* Check if the current token is in the token list */
1016 for (I = 0; I < TokenCount; ++I) {
1017 if (CurTok.Tok == TokenList[I]) {
1018 /* Found a token in the list */
1023 /* Not in the list: Skip it */
1031 int Consume (token_t Token, const char* ErrorMsg)
1032 /* Eat token if it is the next in the input stream, otherwise print an error
1033 * message. Returns true if the token was found and false otherwise.
1036 if (CurTok.Tok == Token) {
1040 Error ("%s", ErrorMsg);
1047 int ConsumeColon (void)
1048 /* Check for a colon and skip it. */
1050 return Consume (TOK_COLON, "`:' expected");
1055 int ConsumeSemi (void)
1056 /* Check for a semicolon and skip it. */
1058 /* Try do be smart about typos... */
1059 if (CurTok.Tok == TOK_SEMI) {
1063 Error ("`;' expected");
1064 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1073 int ConsumeComma (void)
1074 /* Check for a comma and skip it. */
1076 /* Try do be smart about typos... */
1077 if (CurTok.Tok == TOK_COMMA) {
1081 Error ("`,' expected");
1082 if (CurTok.Tok == TOK_SEMI) {
1091 int ConsumeLParen (void)
1092 /* Check for a left parenthesis and skip it */
1094 return Consume (TOK_LPAREN, "`(' expected");
1099 int ConsumeRParen (void)
1100 /* Check for a right parenthesis and skip it */
1102 return Consume (TOK_RPAREN, "`)' expected");
1107 int ConsumeLBrack (void)
1108 /* Check for a left bracket and skip it */
1110 return Consume (TOK_LBRACK, "`[' expected");
1115 int ConsumeRBrack (void)
1116 /* Check for a right bracket and skip it */
1118 return Consume (TOK_RBRACK, "`]' expected");
1123 int ConsumeLCurly (void)
1124 /* Check for a left curly brace and skip it */
1126 return Consume (TOK_LCURLY, "`{' expected");
1131 int ConsumeRCurly (void)
1132 /* Check for a right curly brace and skip it */
1134 return Consume (TOK_RCURLY, "`}' expected");