1 /*****************************************************************************/
5 /* Source file line info structure */
9 /* (C) 1998-2003 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 */
75 #define TT_C 0 /* ANSI C token */
76 #define TT_EXT 1 /* cc65 extension */
79 static const struct Keyword {
80 char* Key; /* Keyword name */
81 unsigned char Tok; /* The token */
82 unsigned char Type; /* Token type */
84 { "_Pragma", TOK_PRAGMA, TT_C },
85 { "__AX__", TOK_AX, TT_C },
86 { "__A__", TOK_A, TT_C },
87 { "__EAX__", TOK_EAX, TT_C },
88 { "__X__", TOK_X, TT_C },
89 { "__Y__", TOK_Y, TT_C },
90 { "__asm__", TOK_ASM, TT_C },
91 { "__attribute__", TOK_ATTRIBUTE, TT_C },
92 { "__far__", TOK_FAR, TT_C },
93 { "__fastcall__", TOK_FASTCALL, TT_C },
94 { "__near__", TOK_NEAR, TT_C },
95 { "asm", TOK_ASM, TT_EXT },
96 { "auto", TOK_AUTO, TT_C },
97 { "break", TOK_BREAK, TT_C },
98 { "case", TOK_CASE, TT_C },
99 { "char", TOK_CHAR, TT_C },
100 { "const", TOK_CONST, TT_C },
101 { "continue", TOK_CONTINUE, TT_C },
102 { "default", TOK_DEFAULT, TT_C },
103 { "do", TOK_DO, TT_C },
104 { "double", TOK_DOUBLE, TT_C },
105 { "else", TOK_ELSE, TT_C },
106 { "enum", TOK_ENUM, TT_C },
107 { "extern", TOK_EXTERN, TT_C },
108 { "far", TOK_FAR, TT_EXT },
109 { "fastcall", TOK_FASTCALL, TT_EXT },
110 { "float", TOK_FLOAT, TT_C },
111 { "for", TOK_FOR, TT_C },
112 { "goto", TOK_GOTO, TT_C },
113 { "if", TOK_IF, TT_C },
114 { "int", TOK_INT, TT_C },
115 { "long", TOK_LONG, TT_C },
116 { "near", TOK_NEAR, TT_EXT },
117 { "register", TOK_REGISTER, TT_C },
118 { "restrict", TOK_RESTRICT, TT_C },
119 { "return", TOK_RETURN, TT_C },
120 { "short", TOK_SHORT, TT_C },
121 { "signed", TOK_SIGNED, TT_C },
122 { "sizeof", TOK_SIZEOF, TT_C },
123 { "static", TOK_STATIC, TT_C },
124 { "struct", TOK_STRUCT, TT_C },
125 { "switch", TOK_SWITCH, TT_C },
126 { "typedef", TOK_TYPEDEF, TT_C },
127 { "union", TOK_UNION, TT_C },
128 { "unsigned", TOK_UNSIGNED, TT_C },
129 { "void", TOK_VOID, TT_C },
130 { "volatile", TOK_VOLATILE, TT_C },
131 { "while", TOK_WHILE, TT_C },
133 #define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
137 /* Stuff for determining the type of an integer constant */
141 #define IT_ULONG 0x08
145 /*****************************************************************************/
147 /*****************************************************************************/
151 static int CmpKey (const void* Key, const void* Elem)
152 /* Compare function for bsearch */
154 return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
159 static int FindKey (const char* Key)
160 /* Find a keyword and return the token. Return IDENT if the token is not a
165 K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
166 if (K && (K->Type != TT_EXT || ANSI == 0)) {
175 static int SkipWhite (void)
176 /* Skip white space in the input stream, reading and preprocessing new lines
177 * if necessary. Return 0 if end of file is reached, return 1 otherwise.
182 if (NextLine () == 0) {
187 if (IsSpace (CurC)) {
197 void SymName (char* s)
198 /* Get symbol from input stream */
202 if (k != MAX_IDENTLEN) {
207 } while (IsIdent (CurC) || IsDigit (CurC));
214 /* Get symbol from input stream or return 0 if not a symbol. */
216 if (IsIdent (CurC)) {
226 static void UnknownChar (char C)
227 /* Error message for unknown character */
229 Error ("Invalid input character with code %02X", C & 0xFF);
230 NextChar (); /* Skip */
235 static void SetTok (int tok)
236 /* Set NextTok.Tok and bump line ptr */
244 static int ParseChar (void)
245 /* Parse a character. Converts \n into EOL, etc. */
251 /* Check for escape chars */
290 /* Hex character constant */
292 Val = HexVal (CurC) << 4;
294 C = Val | HexVal (CurC); /* Do not translate */
307 while (NextC >= '0' && NextC <= '7' && ++I <= 3) {
309 Val = (Val << 3) | (CurC - '0');
313 Error ("Character constant out of range");
318 Error ("Illegal character constant");
320 /* Try to do error recovery, otherwise the compiler will spit
321 * out thousands of errors in this place and abort.
323 if (CurC != '\'' && CurC != '\0') {
324 while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
334 /* Skip the character read */
337 /* Do correct sign extension */
338 return SignExtendChar (C);
343 static void CharConst (void)
344 /* Parse a character constant. */
354 /* Check for closing quote */
356 Error ("`\'' expected");
362 /* Setup values and attributes */
363 NextTok.Tok = TOK_CCONST;
365 /* Translate into target charset */
366 NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
368 /* Character constants have type int */
369 NextTok.Type = type_int;
374 static void StringConst (void)
375 /* Parse a quoted string */
377 NextTok.IVal = GetLiteralPoolOffs ();
378 NextTok.Tok = TOK_SCONST;
380 /* Be sure to concatenate strings */
381 while (CurC == '\"') {
383 /* Skip the quote char */
386 while (CurC != '\"') {
388 Error ("Unexpected newline");
391 AddLiteralChar (ParseChar ());
394 /* Skip closing quote char if there was one */
397 /* Skip white space, read new input */
402 /* Terminate the string */
403 AddLiteralChar ('\0');
408 static void NumericConst (void)
409 /* Parse a numeric constant */
411 unsigned Base; /* Temporary number base */
412 unsigned Prefix; /* Base according to prefix */
417 unsigned long IVal; /* Value */
419 /* Check for a leading hex or octal prefix and determine the possible
423 /* Gobble 0 and examine next char */
425 if (toupper (CurC) == 'X') {
427 NextChar (); /* gobble "x" */
429 Base = 10; /* Assume 10 for now - see below */
430 Prefix = 8; /* Actual prefix says octal */
436 /* Because floating point numbers don't have octal prefixes (a number
437 * with a leading zero is decimal), we first have to read the number
438 * before converting it, so we can determine if it's a float or an
442 while (IsXDigit (CurC) && HexVal (CurC) < Base) {
443 SB_AppendChar (&S, CurC);
448 /* The following character tells us if we have an integer or floating
451 IsFloat = (CurC == '.' ||
452 (Base == 10 && toupper (CurC) == 'E') ||
453 (Base == 16 && toupper (CurC) == 'P'));
455 /* If we don't have a floating point type, an octal prefix results in an
458 if (!IsFloat && Prefix == 8) {
462 /* Since we do now know the correct base, convert the remembered input
467 while ((C = SB_Get (&S)) != '\0') {
468 DigitVal = HexVal (C);
469 if (DigitVal >= Base) {
470 Error ("Numeric constant contains digits beyond the radix");
472 IVal = (IVal * Base) + DigitVal;
475 /* We don't need the string buffer any longer */
478 /* Distinguish between integer and floating point constants */
484 /* Check for a suffix and determine the possible types */
486 if (toupper (CurC) == 'U') {
489 if (toupper (CurC) != 'L') {
490 Types = IT_UINT | IT_ULONG;
495 } else if (toupper (CurC) == 'L') {
498 if (toupper (CurC) != 'U') {
499 Types = IT_LONG | IT_ULONG;
507 /* Decimal constants are of any type but uint */
508 Types = IT_INT | IT_LONG | IT_ULONG;
510 /* Octal or hex constants are of any type */
511 Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
515 /* Check the range to determine the type */
517 /* Out of range for int */
519 /* If the value is in the range 0x8000..0xFFFF, unsigned int is not
520 * allowed, and we don't have a type specifying suffix, emit a
521 * warning, because the constant is of type long.
523 if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
524 Warning ("Constant is long");
528 /* Out of range for unsigned int */
531 if (IVal > 0x7FFFFFFF) {
532 /* Out of range for long int */
536 /* Now set the type string to the smallest type in types */
537 if (Types & IT_INT) {
538 NextTok.Type = type_int;
539 } else if (Types & IT_UINT) {
540 NextTok.Type = type_uint;
541 } else if (Types & IT_LONG) {
542 NextTok.Type = type_long;
544 NextTok.Type = type_ulong;
547 /* Set the value and the token */
549 NextTok.Tok = TOK_ICONST;
554 double FVal = IVal; /* Convert to float */
556 /* Check for a fractional part and read it */
566 /* Read fractional digits. Since we support only 32 bit floats
567 * with a maximum of 7 fractional digits, we read the fractional
568 * part as integer with up to 8 digits and drop the remainder.
569 * This avoids an overflow of Frac and Scale.
574 while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
576 Frac = Frac * Base + DigitVal;
583 /* Scale the fractional part and add it */
585 FVal += ((double) Frac) / ((double) Scale);
589 /* Check for an exponent and read it */
590 if ((Base == 16 && toupper (CurC) == 'F') ||
591 (Base == 10 && toupper (CurC) == 'E')) {
597 /* Skip the exponent notifier */
600 /* Read an optional sign */
607 /* Read exponent digits. Since we support only 32 bit floats
608 * with a maximum exponent of +-/127, we read the exponent
609 * part as integer with up to 3 digits and drop the remainder.
610 * This avoids an overflow of Exp. The exponent is always
611 * decimal, even for hex float consts.
615 while (IsDigit (CurC)) {
617 Exp = Exp * 10 + HexVal (CurC);
622 /* Check for errors: We must have exponent digits, and not more
626 Error ("Floating constant exponent has no digits");
627 } else if (Digits > 3) {
628 Warning ("Floating constant exponent is too large");
631 /* Scale the exponent and adjust the value accordingly */
633 FVal *= pow (10, Exp);
637 /* Check for a suffix and determine the type of the constant */
638 if (toupper (CurC) == 'F') {
640 NextTok.Type = type_float;
642 NextTok.Type = type_double;
645 /* Set the value and the token */
647 NextTok.Tok = TOK_FCONST;
654 void NextToken (void)
655 /* Get next token from input stream */
659 /* We have to skip white space here before shifting tokens, since the
660 * tokens and the current line info is invalid at startup and will get
661 * initialized by reading the first time from the file. Remember if
662 * we were at end of input and handle that later.
664 int GotEOF = (SkipWhite() == 0);
666 /* Current token is the lookahead token */
668 ReleaseLineInfo (CurTok.LI);
672 /* When reading the first time from the file, the line info in NextTok,
673 * which was copied to CurTok is invalid. Since the information from
674 * the token is used for error messages, we must make it valid.
676 if (CurTok.LI == 0) {
677 CurTok.LI = UseLineInfo (GetCurLineInfo ());
680 /* Remember the starting position of the next token */
681 NextTok.LI = UseLineInfo (GetCurLineInfo ());
683 /* Now handle end of input. */
685 /* End of file reached */
686 NextTok.Tok = TOK_CEOF;
690 /* Determine the next token from the lookahead */
691 if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
699 /* Check for a keyword */
700 if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
701 /* Reserved word found */
704 /* No reserved word, check for special symbols */
705 if (token [0] == '_') {
706 /* Special symbols */
707 if (strcmp (token, "__FILE__") == 0) {
708 NextTok.IVal = AddLiteral (GetCurrentFile());
709 NextTok.Tok = TOK_SCONST;
711 } else if (strcmp (token, "__LINE__") == 0) {
712 NextTok.Tok = TOK_ICONST;
713 NextTok.IVal = GetCurrentLine();
714 NextTok.Type = type_int;
716 } else if (strcmp (token, "__func__") == 0) {
717 /* __func__ is only defined in functions */
719 NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
720 NextTok.Tok = TOK_SCONST;
726 /* No reserved word but identifier */
727 strcpy (NextTok.Ident, token);
728 NextTok.Tok = TOK_IDENT;
732 /* Monstrous switch statement ahead... */
740 NextTok.Tok = TOK_BOOL_NOT;
751 SetTok (TOK_MOD_ASSIGN);
753 NextTok.Tok = TOK_MOD;
761 SetTok (TOK_BOOL_AND);
764 SetTok (TOK_AND_ASSIGN);
767 NextTok.Tok = TOK_AND;
786 SetTok (TOK_MUL_ASSIGN);
788 NextTok.Tok = TOK_STAR;
799 SetTok (TOK_PLUS_ASSIGN);
802 NextTok.Tok = TOK_PLUS;
817 SetTok (TOK_MINUS_ASSIGN);
820 SetTok (TOK_PTR_REF);
823 NextTok.Tok = TOK_MINUS;
832 SetTok (TOK_ELLIPSIS);
837 NextTok.Tok = TOK_DOT;
844 SetTok (TOK_DIV_ASSIGN);
846 NextTok.Tok = TOK_DIV;
867 SetTok (TOK_SHL_ASSIGN);
869 NextTok.Tok = TOK_SHL;
873 NextTok.Tok = TOK_LT;
882 NextTok.Tok = TOK_ASSIGN;
895 SetTok (TOK_SHR_ASSIGN);
897 NextTok.Tok = TOK_SHR;
901 NextTok.Tok = TOK_GT;
920 SetTok (TOK_XOR_ASSIGN);
922 NextTok.Tok = TOK_XOR;
934 SetTok (TOK_BOOL_OR);
937 SetTok (TOK_OR_ASSIGN);
940 NextTok.Tok = TOK_OR;
961 void SkipTokens (const token_t* TokenList, unsigned TokenCount)
962 /* Skip tokens until we reach TOK_CEOF or a token in the given token list.
963 * This routine is used for error recovery.
966 while (CurTok.Tok != TOK_CEOF) {
968 /* Check if the current token is in the token list */
970 for (I = 0; I < TokenCount; ++I) {
971 if (CurTok.Tok == TokenList[I]) {
972 /* Found a token in the list */
977 /* Not in the list: Skip it */
985 int Consume (token_t Token, const char* ErrorMsg)
986 /* Eat token if it is the next in the input stream, otherwise print an error
987 * message. Returns true if the token was found and false otherwise.
990 if (CurTok.Tok == Token) {
1001 int ConsumeColon (void)
1002 /* Check for a colon and skip it. */
1004 return Consume (TOK_COLON, "`:' expected");
1009 int ConsumeSemi (void)
1010 /* Check for a semicolon and skip it. */
1012 /* Try do be smart about typos... */
1013 if (CurTok.Tok == TOK_SEMI) {
1017 Error ("`;' expected");
1018 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
1027 int ConsumeComma (void)
1028 /* Check for a comma and skip it. */
1030 /* Try do be smart about typos... */
1031 if (CurTok.Tok == TOK_COMMA) {
1035 Error ("`,' expected");
1036 if (CurTok.Tok == TOK_SEMI) {
1045 int ConsumeLParen (void)
1046 /* Check for a left parenthesis and skip it */
1048 return Consume (TOK_LPAREN, "`(' expected");
1053 int ConsumeRParen (void)
1054 /* Check for a right parenthesis and skip it */
1056 return Consume (TOK_RPAREN, "`)' expected");
1061 int ConsumeLBrack (void)
1062 /* Check for a left bracket and skip it */
1064 return Consume (TOK_LBRACK, "`[' expected");
1069 int ConsumeRBrack (void)
1070 /* Check for a right bracket and skip it */
1072 return Consume (TOK_RBRACK, "`]' expected");
1077 int ConsumeLCurly (void)
1078 /* Check for a left curly brace and skip it */
1080 return Consume (TOK_LCURLY, "`{' expected");
1085 int ConsumeRCurly (void)
1086 /* Check for a right curly brace and skip it */
1088 return Consume (TOK_RCURLY, "`}' expected");