1 /*****************************************************************************/
5 /* The scanner for the ca65 macroassembler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 enum Token Tok = TOK_NONE; /* Current token */
70 int WS; /* Flag: Whitespace before token */
71 long IVal; /* Integer token attribute */
72 char SVal [MAX_STR_LEN+1]; /* String token attribute */
74 FilePos CurPos = { 0, 0, 0 }; /* Name and position in current file */
78 /* Struct to handle include files. Note: The length of the input line may
79 * not exceed 255+1, since the column is stored in the file position struct
80 * as a character. Increasing this value means changing the FilePos struct,
81 * and the read and write routines in the assembler and linker.
83 typedef struct InputFile_ InputFile;
85 FILE* F; /* Input file descriptor */
86 FilePos Pos; /* Position in file */
87 enum Token Tok; /* Last token */
88 int C; /* Last character */
89 char Line[256]; /* The current input line */
90 InputFile* Next; /* Linked list of input files */
93 /* Struct to handle textual input data */
94 typedef struct InputData_ InputData;
96 const char* Data; /* Pointer to the data */
97 const char* Pos; /* Pointer to current position */
98 int Malloced; /* Memory was malloced */
99 enum Token Tok; /* Last token */
100 int C; /* Last character */
101 InputData* Next; /* Linked list of input data */
104 /* Current input variables */
105 static InputFile* IFile = 0; /* Current input file */
106 static InputData* IData = 0; /* Current input memory data */
107 static unsigned ICount = 0; /* Count of input files */
108 static int C = 0; /* Current input character */
110 /* Force end of assembly */
113 /* List of dot keywords with the corresponding tokens */
115 const char* Key; /* MUST be first field */
120 { "ADDR", TOK_ADDR },
121 { "ALIGN", TOK_ALIGN },
123 { "ASCIIZ", TOK_ASCIIZ },
124 { "AUTOIMPORT", TOK_AUTOIMPORT },
125 { "BITAND", TOK_AND },
126 { "BITNOT", TOK_NOT },
128 { "BITXOR", TOK_XOR },
129 { "BLANK", TOK_BLANK },
132 { "BYTE", TOK_BYTE },
133 { "CASE", TOK_CASE },
134 { "CODE", TOK_CODE },
135 { "CONCAT", TOK_CONCAT },
136 { "CONST", TOK_CONST },
138 { "DATA", TOK_DATA },
140 { "DBYT", TOK_DBYT },
141 { "DEBUGINFO", TOK_DEBUGINFO },
142 { "DEF", TOK_DEFINED },
143 { "DEFINE", TOK_DEFINE },
144 { "DEFINED", TOK_DEFINED },
145 { "DWORD", TOK_DWORD },
146 { "ELSE", TOK_ELSE },
147 { "ELSEIF", TOK_ELSEIF },
149 { "ENDIF", TOK_ENDIF },
150 { "ENDMAC", TOK_ENDMACRO },
151 { "ENDMACRO", TOK_ENDMACRO },
152 { "ENDPROC", TOK_ENDPROC },
153 { "ENDREP", TOK_ENDREP },
154 { "ENDREPEAT", TOK_ENDREP },
155 { "ERROR", TOK_ERROR },
156 { "EXITMAC", TOK_EXITMACRO },
157 { "EXITMACRO", TOK_EXITMACRO },
158 { "EXPORT", TOK_EXPORT },
159 { "EXPORTZP", TOK_EXPORTZP },
160 { "FARADDR", TOK_FARADDR },
161 { "FEATURE", TOK_FEATURE },
162 { "FILEOPT", TOK_FILEOPT },
163 { "FOPT", TOK_FILEOPT },
164 { "FORCEWORD", TOK_FORCEWORD },
165 { "GLOBAL", TOK_GLOBAL },
166 { "GLOBALZP", TOK_GLOBALZP },
170 { "IFBLANK", TOK_IFBLANK },
171 { "IFCONST", TOK_IFCONST },
172 { "IFDEF", TOK_IFDEF },
173 { "IFNBLANK", TOK_IFNBLANK },
174 { "IFNCONST", TOK_IFNCONST },
175 { "IFNDEF", TOK_IFNDEF },
176 { "IFNREF", TOK_IFNREF },
177 { "IFP02", TOK_IFP02 },
178 { "IFP816", TOK_IFP816 },
179 { "IFPC02", TOK_IFPC02 },
180 { "IFREF", TOK_IFREF },
181 { "IMPORT", TOK_IMPORT },
182 { "IMPORTZP", TOK_IMPORTZP },
183 { "INCBIN", TOK_INCBIN },
184 { "INCLUDE", TOK_INCLUDE },
185 { "INITIALIZER", TOK_INITIALIZER },
186 { "LEFT", TOK_LEFT },
187 { "LINECONT", TOK_LINECONT },
188 { "LIST", TOK_LIST },
189 { "LISTBYTES", TOK_LISTBYTES },
190 { "LOCAL", TOK_LOCAL },
191 { "LOCALCHAR", TOK_LOCALCHAR },
192 { "MAC", TOK_MACRO },
193 { "MACPACK", TOK_MACPACK },
194 { "MACRO", TOK_MACRO },
195 { "MATCH", TOK_MATCH },
199 { "NULL", TOK_NULL },
204 { "P816", TOK_P816 },
205 { "PAGELEN", TOK_PAGELENGTH },
206 { "PAGELENGTH", TOK_PAGELENGTH },
207 { "PARAMCOUNT", TOK_PARAMCOUNT },
208 { "PC02", TOK_PC02 },
209 { "PROC", TOK_PROC },
210 { "REF", TOK_REFERENCED },
211 { "REFERENCED", TOK_REFERENCED },
212 { "RELOC", TOK_RELOC },
213 { "REPEAT", TOK_REPEAT },
215 { "RIGHT", TOK_RIGHT },
216 { "RODATA", TOK_RODATA },
217 { "SEGMENT", TOK_SEGMENT },
220 { "SMART", TOK_SMART },
221 { "STRAT", TOK_STRAT },
222 { "STRING", TOK_STRING },
223 { "STRLEN", TOK_STRLEN },
224 { "SUNPLUS", TOK_SUNPLUS },
225 { "TCOUNT", TOK_TCOUNT },
226 { "WARNING", TOK_WARNING },
227 { "WORD", TOK_WORD },
228 { "XMATCH", TOK_XMATCH },
230 { "ZEROPAGE", TOK_ZEROPAGE },
235 /*****************************************************************************/
237 /*****************************************************************************/
241 static void NextChar (void);
242 /* Read the next character from the input file */
246 /*****************************************************************************/
247 /* Character classification functions */
248 /*****************************************************************************/
252 static int IsBlank (int C)
253 /* Return true if the character is a blank or tab */
255 return (C == ' ' || C == '\t');
260 static int IsDigit (int C)
261 /* Return true if the character is a digit */
268 static int IsXDigit (int C)
269 /* Return true if the character is a hexadecimal digit */
276 static int IsDDigit (int C)
277 /* Return true if the character is a dual digit */
279 return (C == '0' || C == '1');
284 static int IsIdChar (int C)
285 /* Return true if the character is a valid character for an identifier */
287 return isalnum (C) ||
289 (C == '@' && AtInIdents) ||
290 (C == '$' && DollarInIdents);
295 static int IsIdStart (int C)
296 /* Return true if the character may start an identifier */
298 return isalpha (C) || C == '_';
303 /*****************************************************************************/
305 /*****************************************************************************/
309 void NewInputFile (const char* Name)
310 /* Open a new input file */
315 /* First try to open the file */
316 F = fopen (Name, "r");
321 /* Error (fatal error if this is the main file) */
323 Fatal (FAT_CANNOT_OPEN_INPUT, Name, strerror (errno));
326 /* We are on include level. Search for the file in the include
329 PathName = FindInclude (Name);
330 if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
331 /* Not found or cannot open, print an error and bail out */
332 Error (ERR_CANNOT_OPEN_INCLUDE, Name, strerror (errno));
335 /* Free the allocated memory */
340 /* check again if we do now have an open file */
345 /* Stat the file and remember the values */
347 if (fstat (fileno (F), &Buf) != 0) {
348 Fatal (FAT_CANNOT_STAT_INPUT, Name, strerror (errno));
351 /* Add the file to the input file table and remember the index */
352 FileIdx = AddFile (Name, Buf.st_size, Buf.st_mtime);
354 /* Create a new state variable and initialize it */
355 I = xmalloc (sizeof (*I));
359 I->Pos.Name = FileIdx;
364 /* Use the new file */
376 void DoneInputFile (void)
377 /* Close the current input file */
381 /* Restore the old token */
385 /* Save a pointer to the current struct, then set it back */
389 /* Cleanup the current stuff */
397 void NewInputData (const char* Data, int Malloced)
398 /* Add a chunk of input data to the input stream */
402 /* Create a new state variable and initialize it */
403 I = xmalloc (sizeof (*I));
406 I->Malloced = Malloced;
410 /* Use the new data */
420 static void DoneInputData (void)
421 /* End the current input data stream */
425 /* Restore the old token */
429 /* Save a pointer to the current struct, then set it back */
433 /* Cleanup the current stuff */
442 static unsigned DigitVal (unsigned char C)
443 /* Convert a digit into it's numerical representation */
448 return toupper (C) - 'A' + 10;
454 static void NextChar (void)
455 /* Read the next character from the input file */
457 /* If we have an input data structure, read from there */
462 /* End of input data, will set to last file char */
468 /* Check for end of line, read the next line if needed */
469 while (IFile->Line [IFile->Pos.Col] == '\0') {
471 /* End of current line reached, read next line */
472 if (fgets (IFile->Line, sizeof (IFile->Line), IFile->F) == 0) {
473 /* End of file. Add an empty line to the listing. This is a
474 * small hack needed to keep the PC output in sync.
476 NewListingLine ("", IFile->Pos.Name, ICount);
485 /* Remember the new line for the listing */
486 NewListingLine (IFile->Line, IFile->Pos.Name, ICount);
490 /* Return the next character from the file */
491 C = IFile->Line [IFile->Pos.Col++];
498 void LocaseSVal (void)
499 /* Make SVal lower case */
503 SVal [I] = tolower (SVal [I]);
510 void UpcaseSVal (void)
511 /* Make SVal upper case */
515 SVal [I] = toupper (SVal [I]);
522 static int CmpDotKeyword (const void* K1, const void* K2)
523 /* Compare function for the dot keyword search */
525 return strcmp (((struct DotKeyword*)K1)->Key, ((struct DotKeyword*)K2)->Key);
530 static unsigned char FindDotKeyword (void)
531 /* Find the dot keyword in SVal. Return the corresponding token if found,
532 * return TOK_NONE if not found.
535 static const struct DotKeyword K = { SVal, 0 };
536 struct DotKeyword* R;
538 /* If we aren't in ignore case mode, we have to uppercase the keyword */
543 /* Search for the keyword */
544 R = bsearch (&K, DotKeywords, sizeof (DotKeywords) / sizeof (DotKeywords [0]),
545 sizeof (DotKeywords [0]), CmpDotKeyword);
555 static void ReadIdent (void)
556 /* Read an identifier from the current input position into Ident. It is
557 * assumed that the first character has already been checked.
560 /* Read the identifier */
563 if (I < MAX_STR_LEN) {
567 } while (IsIdChar (C));
570 /* If we should ignore case, convert the identifier to upper case */
578 static unsigned ReadStringConst (int StringTerm)
579 /* Read a string constant into SVal. Check for maximum string length and all
580 * other stuff. The length of the string is returned.
585 /* Skip the leading string terminator */
588 /* Read the string */
591 if (C == StringTerm) {
594 if (C == '\n' || C == EOF) {
595 Error (ERR_NEWLINE_IN_STRING);
599 /* Check for string length, print an error message once */
600 if (I == MAX_STR_LEN) {
601 Error (ERR_STRING_TOO_LONG);
602 } else if (I < MAX_STR_LEN) {
607 /* Skip the character */
611 /* Skip the trailing terminator */
614 /* Terminate the string */
615 if (I >= MAX_STR_LEN) {
620 /* Return the length of the string */
626 void NextRawTok (void)
627 /* Read the next raw token from the input stream */
629 /* If we've a forced end of assembly, don't read further */
636 /* Check if we have tokens from another input source */
637 if (InputFromStack ()) {
642 /* Skip whitespace, remember if we had some */
643 if ((WS = IsBlank (C)) != 0) {
646 } while (IsBlank (C));
649 /* If we're reading from the file, update the location from where the
650 * next token will be read. If we're reading from input data, keep the
657 /* Hex number or PC symbol? */
661 /* Hex digit must follow or DollarIsPC must be enabled */
667 Error (ERR_HEX_DIGIT_EXPECTED);
671 /* Read the number */
673 while (IsXDigit (C)) {
674 if (IVal & 0xF0000000) {
675 Error (ERR_NUM_OVERFLOW);
678 IVal = (IVal << 4) + DigitVal (C);
682 /* This is an integer constant */
691 /* 0 or 1 must follow */
693 Error (ERR_01_EXPECTED);
696 /* Read the number */
698 while (IsDDigit (C)) {
699 if (IVal & 0x80000000) {
700 Error (ERR_NUM_OVERFLOW);
703 IVal = (IVal << 1) + DigitVal (C);
707 /* This is an integer constant */
712 /* Decimal number? */
715 /* Read the number */
717 while (IsDigit (C)) {
718 if (IVal > (0xFFFFFFFF / 10)) {
719 Error (ERR_NUM_OVERFLOW);
722 IVal = (IVal * 10) + DigitVal (C);
726 /* This is an integer constant */
731 /* Control command? */
736 if (!IsIdStart (C)) {
737 Error (ERR_PSEUDO_EXPECTED);
738 /* Try to read an identifier */
742 /* Read the identifier */
745 /* Search the keyword */
746 Tok = FindDotKeyword ();
747 if (Tok == TOK_NONE) {
749 Error (ERR_PSEUDO_EXPECTED);
756 if (C == LocalStart) {
758 /* Read the identifier */
761 /* Start character alone is not enough */
762 if (SVal [1] == '\0') {
763 Error (ERR_IDENT_EXPECTED);
773 /* Identifier or keyword? */
776 /* Read the identifier */
779 /* Check for special names */
780 if (SVal [1] == '\0') {
781 switch (toupper (SVal [0])) {
805 /* Search for an opcode */
806 IVal = FindInstruction (SVal);
808 /* This is a mnemonic */
810 } else if (IsDefine (SVal)) {
811 /* This is a define style macro - expand it */
821 /* Ok, let's do the switch */
910 while (C != '\n' && C != EOF) {
945 } else if (C == '<') {
948 } else if (C == '>') {
971 } else if (C == '>') {
985 /* Hack: If we allow ' as terminating character for strings, read
986 * the following stuff as a string, and check for a one character
989 if (LooseStringTerm) {
990 if (ReadStringConst ('\'') == 1) {
997 /* Always a character constant */
999 if (C == '\n' || C == EOF) {
1000 Error (ERR_ILLEGAL_CHARCON);
1007 Error (ERR_ILLEGAL_CHARCON);
1015 ReadStringConst ('\"');
1020 /* Line continuation? */
1024 /* Handle as white space */
1038 /* Check if we have any open .IFs in this file */
1040 /* Check if we have any open token lists in this file */
1043 /* If this was an include file, then close it and handle like a
1044 * separator. Do not close the main file, but return EOF.
1055 /* If we go here, we could not identify the current character. Skip it
1058 Error (ERR_INVALID_CHAR, C & 0xFF);
1065 int TokHasSVal (enum Token Tok)
1066 /* Return true if the given token has an attached SVal */
1068 return (Tok == TOK_IDENT || Tok == TOK_STRCON);
1073 int TokHasIVal (enum Token Tok)
1074 /* Return true if the given token has an attached IVal */
1076 return (Tok == TOK_INTCON || Tok == TOK_CHARCON || Tok == TOK_MNEMO);
1081 int GetSubKey (const char** Keys, unsigned Count)
1082 /* Search for a subkey in a table of keywords. The current token must be an
1083 * identifier and all keys must be in upper case. The identifier will be
1084 * uppercased in the process. The function returns the index of the keyword,
1085 * or -1 if the keyword was not found.
1090 /* Must have an identifier */
1091 PRECONDITION (Tok == TOK_IDENT);
1093 /* If we aren't in ignore case mode, we have to uppercase the identifier */
1098 /* Do a linear search (a binary search is not worth the effort) */
1099 for (I = 0; I < Count; ++I) {
1100 if (strcmp (SVal, Keys [I]) == 0) {
1112 void InitScanner (const char* InFile)
1113 /* Initialize the scanner, open the given input file */
1115 /* Open the input file */
1116 NewInputFile (InFile);
1121 void DoneScanner (void)
1122 /* Release scanner resources */