1 /*****************************************************************************/
5 /* Pseudo instructions for the ca65 macroassembler */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
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 /*****************************************************************************/
43 #include "assertdefs.h"
80 /*****************************************************************************/
82 /*****************************************************************************/
86 /* Keyword we're about to handle */
87 static char Keyword [sizeof (SVal)+1];
90 #define MAX_PUSHED_SEGMENTS 16
91 static Collection SegStack = STATIC_COLLECTION_INITIALIZER;
95 /*****************************************************************************/
97 /*****************************************************************************/
101 static void DoUnexpected (void);
102 /* Got an unexpected keyword */
104 static void DoInvalid (void);
105 /* Handle a token that is invalid here, since it should have been handled on
106 * a much lower level of the expression hierarchy. Getting this sort of token
107 * means that the lower level code has bugs.
108 * This function differs to DoUnexpected in that the latter may be triggered
109 * by the user by using keywords in the wrong location. DoUnexpected is not
110 * an error in the assembler itself, while DoInvalid is.
115 /*****************************************************************************/
116 /* Helper functions */
117 /*****************************************************************************/
121 static unsigned char OptionalAddrSize (void)
122 /* If a colon follows, parse an optional address size spec and return it.
123 * Otherwise return ADDR_SIZE_DEFAULT.
126 unsigned AddrSize = ADDR_SIZE_DEFAULT;
127 if (Tok == TOK_COLON) {
129 AddrSize = ParseAddrSize ();
137 static void SetBoolOption (unsigned char* Flag)
138 /* Read a on/off/+/- option and set flag accordingly */
140 static const char* Keys[] = {
145 if (Tok == TOK_PLUS) {
148 } else if (Tok == TOK_MINUS) {
151 } else if (Tok == TOK_IDENT) {
152 /* Map the keyword to a number */
153 switch (GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]))) {
154 case 0: *Flag = 0; NextTok (); break;
155 case 1: *Flag = 1; NextTok (); break;
156 default: ErrorSkip ("`on' or `off' expected"); break;
158 } else if (TokIsSep (Tok)) {
159 /* Without anything assume switch on */
162 ErrorSkip ("`on' or `off' expected");
168 static void ExportImport (void (*Func) (SymEntry*, unsigned char, unsigned),
169 unsigned char DefAddrSize, unsigned Flags)
170 /* Export or import symbols */
173 unsigned char AddrSize;
177 /* We need an identifier here */
178 if (Tok != TOK_IDENT) {
179 ErrorSkip ("Identifier expected");
183 /* Find the symbol table entry, allocate a new one if necessary */
184 Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
189 /* Get an optional address size */
190 AddrSize = OptionalAddrSize ();
191 if (AddrSize == ADDR_SIZE_DEFAULT) {
192 AddrSize = DefAddrSize;
195 /* Call the actual import/export function */
196 Func (Sym, AddrSize, Flags);
199 if (Tok == TOK_COMMA) {
209 static long IntArg (long Min, long Max)
210 /* Read an integer argument and check a range. Accept the token "unlimited"
211 * and return -1 in this case.
214 if (Tok == TOK_IDENT && strcmp (SVal, "unlimited") == 0) {
218 long Val = ConstExpression ();
219 if (Val < Min || Val > Max) {
220 Error ("Range error");
229 static void ConDes (const char* Name, unsigned Type)
230 /* Parse remaining line for constructor/destructor of the remaining type */
235 /* Find the symbol table entry, allocate a new one if necessary */
236 SymEntry* Sym = SymFind (CurrentScope, Name, SYM_ALLOC_NEW);
238 /* Optional constructor priority */
239 if (Tok == TOK_COMMA) {
240 /* Priority value follows */
242 Prio = ConstExpression ();
243 if (Prio < CD_PRIO_MIN || Prio > CD_PRIO_MAX) {
244 /* Value out of range */
245 Error ("Range error");
249 /* Use the default priority value */
253 /* Define the symbol */
254 SymConDes (Sym, ADDR_SIZE_DEFAULT, Type, (unsigned) Prio);
259 /*****************************************************************************/
260 /* Handler functions */
261 /*****************************************************************************/
265 static void DoA16 (void)
266 /* Switch the accu to 16 bit mode (assembler only) */
268 if (GetCPU() != CPU_65816) {
269 Error ("Command is only valid in 65816 mode");
271 /* Immidiate mode has two extension bytes */
272 ExtBytes [AMI_IMM_ACCU] = 2;
278 static void DoA8 (void)
279 /* Switch the accu to 8 bit mode (assembler only) */
281 if (GetCPU() != CPU_65816) {
282 Error ("Command is only valid in 65816 mode");
284 /* Immidiate mode has one extension byte */
285 ExtBytes [AMI_IMM_ACCU] = 1;
291 static void DoAddr (void)
292 /* Define addresses */
295 if (GetCPU() == CPU_65816) {
296 EmitWord (GenWordExpr (Expression ()));
298 /* Do a range check */
299 EmitWord (Expression ());
301 if (Tok != TOK_COMMA) {
311 static void DoAlign (void)
312 /* Align the PC to some boundary */
318 /* Read the alignment value */
319 Align = ConstExpression ();
320 if (Align <= 0 || Align > 0x10000) {
321 ErrorSkip ("Range error");
325 /* Optional value follows */
326 if (Tok == TOK_COMMA) {
328 Val = ConstExpression ();
329 /* We need a byte value here */
330 if (!IsByteRange (Val)) {
331 ErrorSkip ("Range error");
338 /* Check if the alignment is a power of two */
339 Bit = BitFind (Align);
340 if (Align != (0x01L << Bit)) {
341 Error ("Alignment value must be a power of 2");
343 SegAlign (Bit, (int) Val);
349 static void DoASCIIZ (void)
350 /* Define text with a zero terminator */
355 /* Must have a string constant */
356 if (Tok != TOK_STRCON) {
357 ErrorSkip ("String constant expected");
361 /* Get the length of the string constant */
364 /* Translate into target charset and emit */
365 TgtTranslateBuf (SVal, Len);
366 EmitData ((unsigned char*) SVal, Len);
368 if (Tok == TOK_COMMA) {
379 static void DoAssert (void)
380 /* Add an assertion */
382 static const char* ActionTab [] = {
390 /* First we have the expression that has to evaluated */
391 ExprNode* Expr = Expression ();
395 if (Tok != TOK_IDENT) {
396 ErrorSkip ("Identifier expected");
399 Action = GetSubKey (ActionTab, sizeof (ActionTab) / sizeof (ActionTab[0]));
405 Action = ASSERT_ACT_WARN;
410 Action = ASSERT_ACT_ERROR;
414 Error ("Illegal assert action specifier");
419 /* Read the message */
420 if (Tok != TOK_STRCON) {
421 ErrorSkip ("String constant expected");
423 AddAssertion (Expr, Action, GetStringId (SVal));
430 static void DoAutoImport (void)
431 /* Mark unresolved symbols as imported */
433 SetBoolOption (&AutoImport);
438 static void DoBss (void)
439 /* Switch to the BSS segment */
446 static void DoByte (void)
450 if (Tok == TOK_STRCON) {
451 /* A string, translate into target charset and emit */
452 unsigned Len = strlen (SVal);
453 TgtTranslateBuf (SVal, Len);
454 EmitData ((unsigned char*) SVal, Len);
457 EmitByte (Expression ());
459 if (Tok != TOK_COMMA) {
463 /* Do smart handling of dangling comma */
464 if (Tok == TOK_SEP) {
465 Error ("Unexpected end of line");
474 static void DoCase (void)
475 /* Switch the IgnoreCase option */
477 SetBoolOption (&IgnoreCase);
478 IgnoreCase = !IgnoreCase;
483 static void DoCharMap (void)
484 /* Allow custome character mappings */
489 /* Read the index as numerical value */
490 Index = ConstExpression ();
491 if (Index < 1 || Index > 255) {
492 /* Value out of range */
493 ErrorSkip ("Range error");
500 /* Read the character code */
501 Code = ConstExpression ();
502 if (Code < 1 || Code > 255) {
503 /* Value out of range */
504 ErrorSkip ("Range error");
508 /* Set the character translation */
509 TgtTranslateSet ((unsigned) Index, (unsigned char) Code);
514 static void DoCode (void)
515 /* Switch to the code segment */
517 UseSeg (&CodeSegDef);
522 static void DoConDes (void)
523 /* Export a symbol as constructor/destructor */
525 static const char* Keys[] = {
529 char Name [sizeof (SVal)];
532 /* Symbol name follows */
533 if (Tok != TOK_IDENT) {
534 ErrorSkip ("Identifier expected");
540 /* Type follows. May be encoded as identifier or numerical */
542 if (Tok == TOK_IDENT) {
544 /* Map the following keyword to a number, then skip it */
545 Type = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
548 /* Check if we got a valid keyword */
550 Error ("Syntax error");
557 /* Read the type as numerical value */
558 Type = ConstExpression ();
559 if (Type < CD_TYPE_MIN || Type > CD_TYPE_MAX) {
560 /* Value out of range */
561 Error ("Range error");
567 /* Parse the remainder of the line and export the symbol */
568 ConDes (Name, (unsigned) Type);
573 static void DoConstructor (void)
574 /* Export a symbol as constructor */
576 char Name [sizeof (SVal)];
578 /* Symbol name follows */
579 if (Tok != TOK_IDENT) {
580 ErrorSkip ("Identifier expected");
586 /* Parse the remainder of the line and export the symbol */
587 ConDes (Name, CD_TYPE_CON);
592 static void DoData (void)
593 /* Switch to the data segment */
595 UseSeg (&DataSegDef);
600 static void DoDbg (void)
601 /* Add debug information from high level code */
603 static const char* Keys[] = {
611 /* We expect a subkey */
612 if (Tok != TOK_IDENT) {
613 ErrorSkip ("Identifier expected");
617 /* Map the following keyword to a number */
618 Key = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
620 /* Skip the subkey */
623 /* Check the key and dispatch to a handler */
625 case 0: DbgInfoFile (); break;
626 case 1: DbgInfoLine (); break;
627 case 2: DbgInfoSym (); break;
628 default: ErrorSkip ("Syntax error"); break;
634 static void DoDByt (void)
635 /* Output double bytes */
638 EmitWord (GenSwapExpr (Expression ()));
639 if (Tok != TOK_COMMA) {
649 static void DoDebugInfo (void)
650 /* Switch debug info on or off */
652 SetBoolOption (&DbgSyms);
657 static void DoDefine (void)
658 /* Define a one line macro */
660 MacDef (MAC_STYLE_DEFINE);
665 static void DoDestructor (void)
666 /* Export a symbol as destructor */
668 char Name [sizeof (SVal)];
670 /* Symbol name follows */
671 if (Tok != TOK_IDENT) {
672 ErrorSkip ("Identifier expected");
678 /* Parse the remainder of the line and export the symbol */
679 ConDes (Name, CD_TYPE_DES);
684 static void DoDWord (void)
688 EmitDWord (Expression ());
689 if (Tok != TOK_COMMA) {
699 static void DoEnd (void)
700 /* End of assembly */
708 static void DoEndProc (void)
709 /* Leave a lexical level */
711 if (GetCurrentSymTabType () != ST_PROC) {
713 ErrorSkip ("No open .PROC");
721 static void DoEndScope (void)
722 /* Leave a lexical level */
724 if ( GetCurrentSymTabType () != ST_SCOPE) {
726 ErrorSkip ("No open .SCOPE");
734 static void DoError (void)
737 if (Tok != TOK_STRCON) {
738 ErrorSkip ("String constant expected");
740 Error ("User error: %s", SVal);
747 static void DoExitMacro (void)
748 /* Exit a macro expansion */
750 if (!InMacExpansion ()) {
751 /* We aren't expanding a macro currently */
760 static void DoExport (void)
761 /* Export a symbol */
763 ExportImport (SymExport, ADDR_SIZE_DEFAULT, SF_NONE);
768 static void DoExportZP (void)
769 /* Export a zeropage symbol */
771 ExportImport (SymExport, ADDR_SIZE_ZP, SF_NONE);
776 static void DoFarAddr (void)
777 /* Define far addresses (24 bit) */
780 EmitFarAddr (Expression ());
781 if (Tok != TOK_COMMA) {
791 static void DoFeature (void)
792 /* Switch the Feature option */
794 /* Allow a list of comma separated keywords */
797 /* We expect an identifier */
798 if (Tok != TOK_IDENT) {
799 ErrorSkip ("Identifier expected");
803 /* Make the string attribute lower case */
806 /* Set the feature and check for errors */
807 if (SetFeature (SVal) == FEAT_UNKNOWN) {
809 ErrorSkip ("Invalid feature: `%s'", SVal);
812 /* Skip the keyword */
816 /* Allow more than one keyword */
817 if (Tok == TOK_COMMA) {
827 static void DoFileOpt (void)
828 /* Insert a file option */
832 /* The option type may be given as a keyword or as a number. */
833 if (Tok == TOK_IDENT) {
835 /* Option given as keyword */
836 static const char* Keys [] = {
837 "AUTHOR", "COMMENT", "COMPILER"
840 /* Map the option to a number */
841 OptNum = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
844 ErrorSkip ("File option keyword expected");
848 /* Skip the keyword */
851 /* Must be followed by a comma */
854 /* We accept only string options for now */
855 if (Tok != TOK_STRCON) {
856 ErrorSkip ("String constant expected");
860 /* Insert the option */
879 Internal ("Invalid OptNum: %ld", OptNum);
888 /* Option given as number */
889 OptNum = ConstExpression ();
890 if (!IsByteRange (OptNum)) {
891 ErrorSkip ("Range error");
895 /* Must be followed by a comma */
898 /* We accept only string options for now */
899 if (Tok != TOK_STRCON) {
900 ErrorSkip ("String constant expected");
904 /* Insert the option */
905 OptStr ((unsigned char) OptNum, SVal);
914 static void DoForceImport (void)
915 /* Do a forced import on a symbol */
917 ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_FORCED);
922 static void DoGlobal (void)
923 /* Declare a global symbol */
925 ExportImport (SymGlobal, ADDR_SIZE_DEFAULT, SF_NONE);
930 static void DoGlobalZP (void)
931 /* Declare a global zeropage symbol */
933 ExportImport (SymGlobal, ADDR_SIZE_ZP, SF_NONE);
938 static void DoI16 (void)
939 /* Switch the index registers to 16 bit mode (assembler only) */
941 if (GetCPU() != CPU_65816) {
942 Error ("Command is only valid in 65816 mode");
944 /* Immidiate mode has two extension bytes */
945 ExtBytes [AMI_IMM_INDEX] = 2;
951 static void DoI8 (void)
952 /* Switch the index registers to 16 bit mode (assembler only) */
954 if (GetCPU() != CPU_65816) {
955 Error ("Command is only valid in 65816 mode");
957 /* Immidiate mode has one extension byte */
958 ExtBytes [AMI_IMM_INDEX] = 1;
964 static void DoImport (void)
965 /* Import a symbol */
967 ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_NONE);
972 static void DoImportZP (void)
973 /* Import a zero page symbol */
975 ExportImport (SymImport, ADDR_SIZE_ZP, SF_NONE);
980 static void DoIncBin (void)
981 /* Include a binary file */
983 char Name [sizeof (SVal)];
989 /* Name must follow */
990 if (Tok != TOK_STRCON) {
991 ErrorSkip ("String constant expected");
997 /* A starting offset may follow */
998 if (Tok == TOK_COMMA) {
1000 Start = ConstExpression ();
1002 /* And a length may follow */
1003 if (Tok == TOK_COMMA) {
1005 Count = ConstExpression ();
1010 /* Try to open the file */
1011 F = fopen (Name, "rb");
1014 /* Search for the file in the include directories. */
1015 char* PathName = FindInclude (Name);
1016 if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
1017 /* Not found or cannot open, print an error and bail out */
1018 ErrorSkip ("Cannot open include file `%s': %s", Name, strerror (errno));
1021 /* Free the allocated memory */
1024 /* If we had an error before, bail out now */
1030 /* Get the size of the file */
1031 fseek (F, 0, SEEK_END);
1034 /* If a count was not given, calculate it now */
1036 Count = Size - Start;
1038 /* Nothing to read - flag this as a range error */
1039 ErrorSkip ("Range error");
1043 /* Count was given, check if it is valid */
1044 if (Start + Count > Size) {
1045 ErrorSkip ("Range error");
1050 /* Seek to the start position */
1051 fseek (F, Start, SEEK_SET);
1053 /* Read chunks and insert them into the output */
1056 unsigned char Buf [1024];
1058 /* Calculate the number of bytes to read */
1059 size_t BytesToRead = (Count > (long)sizeof(Buf))? sizeof(Buf) : (size_t) Count;
1062 size_t BytesRead = fread (Buf, 1, BytesToRead, F);
1063 if (BytesToRead != BytesRead) {
1064 /* Some sort of error */
1065 ErrorSkip ("Cannot read from include file `%s': %s",
1066 Name, strerror (errno));
1070 /* Insert it into the output */
1071 EmitData (Buf, BytesRead);
1073 /* Keep the counters current */
1078 /* Close the file, ignore errors since it's r/o */
1084 static void DoInclude (void)
1085 /* Include another file */
1087 char Name [MAX_STR_LEN+1];
1089 /* Name must follow */
1090 if (Tok != TOK_STRCON) {
1091 ErrorSkip ("String constant expected");
1093 strcpy (Name, SVal);
1095 NewInputFile (Name);
1101 static void DoInvalid (void)
1102 /* Handle a token that is invalid here, since it should have been handled on
1103 * a much lower level of the expression hierarchy. Getting this sort of token
1104 * means that the lower level code has bugs.
1105 * This function differs to DoUnexpected in that the latter may be triggered
1106 * by the user by using keywords in the wrong location. DoUnexpected is not
1107 * an error in the assembler itself, while DoInvalid is.
1110 Internal ("Unexpected token: %s", Keyword);
1115 static void DoLineCont (void)
1116 /* Switch the use of line continuations */
1118 SetBoolOption (&LineCont);
1123 static void DoList (void)
1124 /* Enable/disable the listing */
1126 /* Get the setting */
1128 SetBoolOption (&List);
1130 /* Manage the counter */
1140 static void DoListBytes (void)
1141 /* Set maximum number of bytes to list for one line */
1143 SetListBytes (IntArg (MIN_LIST_BYTES, MAX_LIST_BYTES));
1148 static void DoLocalChar (void)
1149 /* Define the character that starts local labels */
1151 if (Tok != TOK_CHARCON) {
1152 ErrorSkip ("Character constant expected");
1154 if (IVal != '@' && IVal != '?') {
1155 Error ("Invalid start character for locals");
1157 LocalStart = (char) IVal;
1165 static void DoMacPack (void)
1166 /* Insert a macro package */
1168 /* Macro package names */
1169 static const char* Keys [] = {
1178 /* We expect an identifier */
1179 if (Tok != TOK_IDENT) {
1180 ErrorSkip ("Identifier expected");
1184 /* Map the keyword to a number */
1185 Package = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
1188 ErrorSkip ("Invalid macro package");
1192 /* Skip the package name */
1195 /* Insert the package */
1196 InsertMacPack (Package);
1201 static void DoMacro (void)
1202 /* Start a macro definition */
1204 MacDef (MAC_STYLE_CLASSIC);
1209 static void DoNull (void)
1210 /* Switch to the NULL segment */
1212 UseSeg (&NullSegDef);
1217 static void DoOrg (void)
1218 /* Start absolute code */
1220 long PC = ConstExpression ();
1221 if (PC < 0 || PC > 0xFFFFFF) {
1222 Error ("Range error");
1230 static void DoOut (void)
1231 /* Output a string */
1233 if (Tok != TOK_STRCON) {
1234 ErrorSkip ("String constant expected");
1236 /* Output the string and be sure to flush the output to keep it in
1237 * sync with any error messages if the output is redirected to a file.
1239 printf ("%s\n", SVal);
1247 static void DoP02 (void)
1248 /* Switch to 6502 CPU */
1255 static void DoPC02 (void)
1256 /* Switch to 65C02 CPU */
1263 static void DoP816 (void)
1264 /* Switch to 65816 CPU */
1271 static void DoPageLength (void)
1272 /* Set the page length for the listing */
1274 PageLength = IntArg (MIN_PAGE_LEN, MAX_PAGE_LEN);
1279 static void DoPopSeg (void)
1280 /* Pop an old segment from the segment stack */
1284 /* Must have a segment on the stack */
1285 if (CollCount (&SegStack) == 0) {
1286 ErrorSkip ("Segment stack is empty");
1290 /* Pop the last element */
1291 Def = CollPop (&SegStack);
1293 /* Restore this segment */
1296 /* Delete the segment definition */
1302 static void DoProc (void)
1303 /* Start a new lexical scope */
1305 char Name[sizeof(SVal)];
1306 unsigned char AddrSize;
1308 if (Tok == TOK_IDENT) {
1312 /* The new scope has a name. Remember it. */
1313 strcpy (Name, SVal);
1315 /* Search for the symbol, generate a new one if needed */
1316 Sym = SymFind (CurrentScope, Name, SYM_ALLOC_NEW);
1318 /* Skip the scope name */
1321 /* Read an optional address size specifier */
1322 AddrSize = OptionalAddrSize ();
1324 /* Mark the symbol as defined */
1325 SymDef (Sym, GenCurrentPC (), AddrSize, SF_LABEL);
1329 /* A .PROC statement without a name */
1330 Warning (1, "Unnamed .PROCs are deprecated, please use .SCOPE");
1331 AnonName (Name, sizeof (Name), "PROC");
1332 AddrSize = ADDR_SIZE_DEFAULT;
1336 /* Enter a new scope */
1337 SymEnterLevel (Name, ST_PROC, AddrSize);
1342 static void DoPSC02 (void)
1343 /* Switch to 65SC02 CPU */
1345 SetCPU (CPU_65SC02);
1350 static void DoPushSeg (void)
1351 /* Push the current segment onto the segment stack */
1353 /* Can only push a limited size of segments */
1354 if (CollCount (&SegStack) >= MAX_PUSHED_SEGMENTS) {
1355 ErrorSkip ("Segment stack overflow");
1359 /* Get the current segment and push it */
1360 CollAppend (&SegStack, DupSegDef (GetCurrentSegDef ()));
1365 static void DoReloc (void)
1366 /* Enter relocatable mode */
1373 static void DoRepeat (void)
1374 /* Repeat some instruction block */
1381 static void DoRes (void)
1382 /* Reserve some number of storage bytes */
1387 Count = ConstExpression ();
1388 if (Count > 0xFFFF || Count < 0) {
1389 ErrorSkip ("Range error");
1392 if (Tok == TOK_COMMA) {
1394 Val = ConstExpression ();
1395 /* We need a byte value here */
1396 if (!IsByteRange (Val)) {
1397 ErrorSkip ("Range error");
1401 /* Emit constant values */
1403 Emit0 ((unsigned char) Val);
1407 /* Emit fill fragments */
1414 static void DoROData (void)
1415 /* Switch to the r/o data segment */
1417 UseSeg (&RODataSegDef);
1422 static void DoScope (void)
1423 /* Start a local scope */
1425 char Name[sizeof (SVal)];
1426 unsigned char AddrSize;
1429 if (Tok == TOK_IDENT) {
1431 /* The new scope has a name. Remember and skip it. */
1432 strcpy (Name, SVal);
1437 /* An unnamed scope */
1438 AnonName (Name, sizeof (Name), "SCOPE");
1442 /* Read an optional address size specifier */
1443 AddrSize = OptionalAddrSize ();
1445 /* Enter the new scope */
1446 SymEnterLevel (Name, ST_SCOPE, AddrSize);
1452 static void DoSegment (void)
1453 /* Switch to another segment */
1455 char Name [sizeof (SVal)];
1459 if (Tok != TOK_STRCON) {
1460 ErrorSkip ("String constant expected");
1463 /* Save the name of the segment and skip it */
1464 strcpy (Name, SVal);
1467 /* Check for an optional address size modifier */
1468 Def.AddrSize = OptionalAddrSize ();
1470 /* Set the segment */
1477 static void DoSetCPU (void)
1478 /* Switch the CPU instruction set */
1480 /* We expect an identifier */
1481 if (Tok != TOK_STRCON) {
1482 ErrorSkip ("String constant expected");
1484 /* Try to find the CPU, then skip the identifier */
1485 cpu_t CPU = FindCPU (SVal);
1488 /* Switch to the new CPU */
1495 static void DoSmart (void)
1496 /* Smart mode on/off */
1498 SetBoolOption (&SmartMode);
1503 static void DoSunPlus (void)
1504 /* Switch to the SUNPLUS CPU */
1506 SetCPU (CPU_SUNPLUS);
1511 static void DoTag (void)
1512 /* Allocate space for a struct */
1517 /* Read the struct name */
1518 SymTable* Struct = ParseScopedSymTable ();
1520 /* Check the supposed struct */
1522 ErrorSkip ("Unknown struct");
1525 if (GetSymTabType (Struct) != ST_STRUCT) {
1526 ErrorSkip ("Not a struct");
1530 /* Get the symbol that defines the size of the struct */
1531 SizeSym = GetSizeOfScope (Struct);
1533 /* Check if it does exist and if its value is known */
1534 if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
1535 ErrorSkip ("Size of struct/union is unknown");
1539 /* Optional multiplicator may follow */
1540 if (Tok == TOK_COMMA) {
1543 Multiplicator = ConstExpression ();
1544 /* Multiplicator must make sense */
1545 if (Multiplicator <= 0) {
1546 ErrorSkip ("Range error");
1549 Size *= Multiplicator;
1552 /* Emit fill fragments */
1558 static void DoUnexpected (void)
1559 /* Got an unexpected keyword */
1561 Error ("Unexpected `%s'", Keyword);
1567 static void DoWarning (void)
1570 if (Tok != TOK_STRCON) {
1571 ErrorSkip ("String constant expected");
1573 Warning (0, "User warning: %s", SVal);
1580 static void DoWord (void)
1584 EmitWord (Expression ());
1585 if (Tok != TOK_COMMA) {
1595 static void DoZeropage (void)
1596 /* Switch to the zeropage segment */
1598 UseSeg (&ZeropageSegDef);
1603 /*****************************************************************************/
1605 /*****************************************************************************/
1609 /* Control commands flags */
1611 ccNone = 0x0000, /* No special flags */
1612 ccKeepToken = 0x0001 /* Do not skip the current token */
1615 /* Control command table */
1616 typedef struct CtrlDesc CtrlDesc;
1618 unsigned Flags; /* Flags for this directive */
1619 void (*Handler) (void); /* Command handler */
1622 #define PSEUDO_COUNT (sizeof (CtrlCmdTab) / sizeof (CtrlCmdTab [0]))
1623 static CtrlDesc CtrlCmdTab [] = {
1626 { ccNone, DoAddr }, /* .ADDR */
1627 { ccNone, DoAlign },
1628 { ccNone, DoASCIIZ },
1629 { ccNone, DoAssert },
1630 { ccNone, DoAutoImport },
1631 { ccNone, DoUnexpected }, /* .BANKBYTE */
1632 { ccNone, DoUnexpected }, /* .BLANK */
1636 { ccNone, DoCharMap },
1638 { ccNone, DoUnexpected, }, /* .CONCAT */
1639 { ccNone, DoConDes },
1640 { ccNone, DoUnexpected }, /* .CONST */
1641 { ccNone, DoConstructor },
1642 { ccNone, DoUnexpected }, /* .CPU */
1646 { ccNone, DoDebugInfo },
1647 { ccNone, DoDefine },
1648 { ccNone, DoUnexpected }, /* .DEFINED */
1649 { ccNone, DoDestructor },
1650 { ccNone, DoDWord },
1651 { ccKeepToken, DoConditionals }, /* .ELSE */
1652 { ccKeepToken, DoConditionals }, /* .ELSEIF */
1653 { ccKeepToken, DoEnd },
1654 { ccNone, DoUnexpected }, /* .ENDENUM */
1655 { ccKeepToken, DoConditionals }, /* .ENDIF */
1656 { ccNone, DoUnexpected }, /* .ENDMACRO */
1657 { ccNone, DoEndProc },
1658 { ccNone, DoUnexpected }, /* .ENDREPEAT */
1659 { ccNone, DoEndScope },
1660 { ccNone, DoUnexpected }, /* .ENDSTRUCT */
1661 { ccNone, DoUnexpected }, /* .ENDUNION */
1663 { ccNone, DoError },
1664 { ccNone, DoExitMacro },
1665 { ccNone, DoExport },
1666 { ccNone, DoExportZP },
1667 { ccNone, DoFarAddr },
1668 { ccNone, DoFeature },
1669 { ccNone, DoFileOpt },
1670 { ccNone, DoForceImport },
1671 { ccNone, DoUnexpected }, /* .FORCEWORD */
1672 { ccNone, DoGlobal },
1673 { ccNone, DoGlobalZP },
1674 { ccNone, DoUnexpected }, /* .HIBYTE */
1675 { ccNone, DoUnexpected }, /* .HIWORD */
1678 { ccKeepToken, DoConditionals }, /* .IF */
1679 { ccKeepToken, DoConditionals }, /* .IFBLANK */
1680 { ccKeepToken, DoConditionals }, /* .IFCONST */
1681 { ccKeepToken, DoConditionals }, /* .IFDEF */
1682 { ccKeepToken, DoConditionals }, /* .IFNBLANK */
1683 { ccKeepToken, DoConditionals }, /* .IFNCONST */
1684 { ccKeepToken, DoConditionals }, /* .IFNDEF */
1685 { ccKeepToken, DoConditionals }, /* .IFNREF */
1686 { ccKeepToken, DoConditionals }, /* .IFP02 */
1687 { ccKeepToken, DoConditionals }, /* .IFP816 */
1688 { ccKeepToken, DoConditionals }, /* .IFPC02 */
1689 { ccKeepToken, DoConditionals }, /* .IFPSC02 */
1690 { ccKeepToken, DoConditionals }, /* .IFREF */
1691 { ccNone, DoImport },
1692 { ccNone, DoImportZP },
1693 { ccNone, DoIncBin },
1694 { ccNone, DoInclude },
1695 { ccNone, DoInvalid }, /* .LEFT */
1696 { ccNone, DoLineCont },
1698 { ccNone, DoListBytes },
1699 { ccNone, DoUnexpected }, /* .LOBYTE */
1700 { ccNone, DoUnexpected }, /* .LOCAL */
1701 { ccNone, DoLocalChar },
1702 { ccNone, DoUnexpected }, /* .LOWORD */
1703 { ccNone, DoMacPack },
1704 { ccNone, DoMacro },
1705 { ccNone, DoUnexpected }, /* .MATCH */
1706 { ccNone, DoInvalid }, /* .MID */
1712 { ccNone, DoPageLength },
1713 { ccNone, DoUnexpected }, /* .PARAMCOUNT */
1715 { ccNone, DoPopSeg },
1717 { ccNone, DoPSC02 },
1718 { ccNone, DoPushSeg },
1719 { ccNone, DoUnexpected }, /* .REFERENCED */
1720 { ccNone, DoReloc },
1721 { ccNone, DoRepeat },
1723 { ccNone, DoInvalid }, /* .RIGHT */
1724 { ccNone, DoROData },
1725 { ccNone, DoScope },
1726 { ccNone, DoSegment },
1727 { ccNone, DoSetCPU },
1728 { ccNone, DoUnexpected }, /* .SIZEOF */
1729 { ccNone, DoSmart },
1730 { ccNone, DoUnexpected }, /* .STRAT */
1731 { ccNone, DoUnexpected }, /* .STRING */
1732 { ccNone, DoUnexpected }, /* .STRLEN */
1733 { ccNone, DoStruct },
1734 { ccNone, DoSunPlus },
1736 { ccNone, DoUnexpected }, /* .TCOUNT */
1737 { ccNone, DoUnexpected }, /* .TIME */
1738 { ccNone, DoUnion },
1739 { ccNone, DoUnexpected }, /* .VERSION */
1740 { ccNone, DoWarning },
1742 { ccNone, DoUnexpected }, /* .XMATCH */
1743 { ccNone, DoZeropage },
1748 /*****************************************************************************/
1750 /*****************************************************************************/
1754 void HandlePseudo (void)
1755 /* Handle a pseudo instruction */
1759 /* Calculate the index into the table */
1760 unsigned Index = Tok - TOK_FIRSTPSEUDO;
1763 if (PSEUDO_COUNT != (TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1)) {
1764 Internal ("Pseudo mismatch: PSEUDO_COUNT = %u, actual count = %u\n",
1765 PSEUDO_COUNT, TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1);
1767 CHECK (Index < PSEUDO_COUNT);
1769 /* Get the pseudo intruction descriptor */
1770 D = &CtrlCmdTab [Index];
1772 /* Remember the instruction, then skip it if needed */
1773 if ((D->Flags & ccKeepToken) == 0) {
1774 strcpy (Keyword, SVal);
1778 /* Call the handler */
1784 void SegStackCheck (void)
1785 /* Check if the segment stack is empty at end of assembly */
1787 if (CollCount (&SegStack) != 0) {
1788 Error ("Segment stack is not empty");