1 /*****************************************************************************/
5 /* Pseudo instructions for the ca65 macroassembler */
9 /* (C) 1998-2004 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[] = {
530 char Name [sizeof (SVal)];
533 /* Symbol name follows */
534 if (Tok != TOK_IDENT) {
535 ErrorSkip ("Identifier expected");
541 /* Type follows. May be encoded as identifier or numerical */
543 if (Tok == TOK_IDENT) {
545 /* Map the following keyword to a number, then skip it */
546 Type = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
549 /* Check if we got a valid keyword */
551 Error ("Syntax error");
558 /* Read the type as numerical value */
559 Type = ConstExpression ();
560 if (Type < CD_TYPE_MIN || Type > CD_TYPE_MAX) {
561 /* Value out of range */
562 Error ("Range error");
568 /* Parse the remainder of the line and export the symbol */
569 ConDes (Name, (unsigned) Type);
574 static void DoConstructor (void)
575 /* Export a symbol as constructor */
577 char Name [sizeof (SVal)];
579 /* Symbol name follows */
580 if (Tok != TOK_IDENT) {
581 ErrorSkip ("Identifier expected");
587 /* Parse the remainder of the line and export the symbol */
588 ConDes (Name, CD_TYPE_CON);
593 static void DoData (void)
594 /* Switch to the data segment */
596 UseSeg (&DataSegDef);
601 static void DoDbg (void)
602 /* Add debug information from high level code */
604 static const char* Keys[] = {
612 /* We expect a subkey */
613 if (Tok != TOK_IDENT) {
614 ErrorSkip ("Identifier expected");
618 /* Map the following keyword to a number */
619 Key = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
621 /* Skip the subkey */
624 /* Check the key and dispatch to a handler */
626 case 0: DbgInfoFile (); break;
627 case 1: DbgInfoLine (); break;
628 case 2: DbgInfoSym (); break;
629 default: ErrorSkip ("Syntax error"); break;
635 static void DoDByt (void)
636 /* Output double bytes */
639 EmitWord (GenSwapExpr (Expression ()));
640 if (Tok != TOK_COMMA) {
650 static void DoDebugInfo (void)
651 /* Switch debug info on or off */
653 SetBoolOption (&DbgSyms);
658 static void DoDefine (void)
659 /* Define a one line macro */
661 MacDef (MAC_STYLE_DEFINE);
666 static void DoDestructor (void)
667 /* Export a symbol as destructor */
669 char Name [sizeof (SVal)];
671 /* Symbol name follows */
672 if (Tok != TOK_IDENT) {
673 ErrorSkip ("Identifier expected");
679 /* Parse the remainder of the line and export the symbol */
680 ConDes (Name, CD_TYPE_DES);
685 static void DoDWord (void)
689 EmitDWord (Expression ());
690 if (Tok != TOK_COMMA) {
700 static void DoEnd (void)
701 /* End of assembly */
709 static void DoEndProc (void)
710 /* Leave a lexical level */
712 if (GetCurrentSymTabType () != ST_PROC) {
714 ErrorSkip ("No open .PROC");
722 static void DoEndScope (void)
723 /* Leave a lexical level */
725 if ( GetCurrentSymTabType () != ST_SCOPE) {
727 ErrorSkip ("No open .SCOPE");
735 static void DoError (void)
738 if (Tok != TOK_STRCON) {
739 ErrorSkip ("String constant expected");
741 Error ("User error: %s", SVal);
748 static void DoExitMacro (void)
749 /* Exit a macro expansion */
751 if (!InMacExpansion ()) {
752 /* We aren't expanding a macro currently */
761 static void DoExport (void)
762 /* Export a symbol */
764 ExportImport (SymExport, ADDR_SIZE_DEFAULT, SF_NONE);
769 static void DoExportZP (void)
770 /* Export a zeropage symbol */
772 ExportImport (SymExport, ADDR_SIZE_ZP, SF_NONE);
777 static void DoFarAddr (void)
778 /* Define far addresses (24 bit) */
781 EmitFarAddr (Expression ());
782 if (Tok != TOK_COMMA) {
792 static void DoFeature (void)
793 /* Switch the Feature option */
795 /* Allow a list of comma separated keywords */
798 /* We expect an identifier */
799 if (Tok != TOK_IDENT) {
800 ErrorSkip ("Identifier expected");
804 /* Make the string attribute lower case */
807 /* Set the feature and check for errors */
808 if (SetFeature (SVal) == FEAT_UNKNOWN) {
810 ErrorSkip ("Invalid feature: `%s'", SVal);
813 /* Skip the keyword */
817 /* Allow more than one keyword */
818 if (Tok == TOK_COMMA) {
828 static void DoFileOpt (void)
829 /* Insert a file option */
833 /* The option type may be given as a keyword or as a number. */
834 if (Tok == TOK_IDENT) {
836 /* Option given as keyword */
837 static const char* Keys [] = {
838 "AUTHOR", "COMMENT", "COMPILER"
841 /* Map the option to a number */
842 OptNum = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
845 ErrorSkip ("File option keyword expected");
849 /* Skip the keyword */
852 /* Must be followed by a comma */
855 /* We accept only string options for now */
856 if (Tok != TOK_STRCON) {
857 ErrorSkip ("String constant expected");
861 /* Insert the option */
880 Internal ("Invalid OptNum: %ld", OptNum);
889 /* Option given as number */
890 OptNum = ConstExpression ();
891 if (!IsByteRange (OptNum)) {
892 ErrorSkip ("Range error");
896 /* Must be followed by a comma */
899 /* We accept only string options for now */
900 if (Tok != TOK_STRCON) {
901 ErrorSkip ("String constant expected");
905 /* Insert the option */
906 OptStr ((unsigned char) OptNum, SVal);
915 static void DoForceImport (void)
916 /* Do a forced import on a symbol */
918 ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_FORCED);
923 static void DoGlobal (void)
924 /* Declare a global symbol */
926 ExportImport (SymGlobal, ADDR_SIZE_DEFAULT, SF_NONE);
931 static void DoGlobalZP (void)
932 /* Declare a global zeropage symbol */
934 ExportImport (SymGlobal, ADDR_SIZE_ZP, SF_NONE);
939 static void DoI16 (void)
940 /* Switch the index registers to 16 bit mode (assembler only) */
942 if (GetCPU() != CPU_65816) {
943 Error ("Command is only valid in 65816 mode");
945 /* Immidiate mode has two extension bytes */
946 ExtBytes [AMI_IMM_INDEX] = 2;
952 static void DoI8 (void)
953 /* Switch the index registers to 16 bit mode (assembler only) */
955 if (GetCPU() != CPU_65816) {
956 Error ("Command is only valid in 65816 mode");
958 /* Immidiate mode has one extension byte */
959 ExtBytes [AMI_IMM_INDEX] = 1;
965 static void DoImport (void)
966 /* Import a symbol */
968 ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_NONE);
973 static void DoImportZP (void)
974 /* Import a zero page symbol */
976 ExportImport (SymImport, ADDR_SIZE_ZP, SF_NONE);
981 static void DoIncBin (void)
982 /* Include a binary file */
984 char Name [sizeof (SVal)];
990 /* Name must follow */
991 if (Tok != TOK_STRCON) {
992 ErrorSkip ("String constant expected");
998 /* A starting offset may follow */
999 if (Tok == TOK_COMMA) {
1001 Start = ConstExpression ();
1003 /* And a length may follow */
1004 if (Tok == TOK_COMMA) {
1006 Count = ConstExpression ();
1011 /* Try to open the file */
1012 F = fopen (Name, "rb");
1015 /* Search for the file in the include directories. */
1016 char* PathName = FindInclude (Name);
1017 if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
1018 /* Not found or cannot open, print an error and bail out */
1019 ErrorSkip ("Cannot open include file `%s': %s", Name, strerror (errno));
1022 /* Free the allocated memory */
1025 /* If we had an error before, bail out now */
1031 /* Get the size of the file */
1032 fseek (F, 0, SEEK_END);
1035 /* If a count was not given, calculate it now */
1037 Count = Size - Start;
1039 /* Nothing to read - flag this as a range error */
1040 ErrorSkip ("Range error");
1044 /* Count was given, check if it is valid */
1045 if (Start + Count > Size) {
1046 ErrorSkip ("Range error");
1051 /* Seek to the start position */
1052 fseek (F, Start, SEEK_SET);
1054 /* Read chunks and insert them into the output */
1057 unsigned char Buf [1024];
1059 /* Calculate the number of bytes to read */
1060 size_t BytesToRead = (Count > (long)sizeof(Buf))? sizeof(Buf) : (size_t) Count;
1063 size_t BytesRead = fread (Buf, 1, BytesToRead, F);
1064 if (BytesToRead != BytesRead) {
1065 /* Some sort of error */
1066 ErrorSkip ("Cannot read from include file `%s': %s",
1067 Name, strerror (errno));
1071 /* Insert it into the output */
1072 EmitData (Buf, BytesRead);
1074 /* Keep the counters current */
1079 /* Close the file, ignore errors since it's r/o */
1085 static void DoInclude (void)
1086 /* Include another file */
1088 char Name [MAX_STR_LEN+1];
1090 /* Name must follow */
1091 if (Tok != TOK_STRCON) {
1092 ErrorSkip ("String constant expected");
1094 strcpy (Name, SVal);
1096 NewInputFile (Name);
1102 static void DoInterruptor (void)
1103 /* Export a symbol as interruptor */
1105 char Name [sizeof (SVal)];
1107 /* Symbol name follows */
1108 if (Tok != TOK_IDENT) {
1109 ErrorSkip ("Identifier expected");
1112 strcpy (Name, SVal);
1115 /* Parse the remainder of the line and export the symbol */
1116 ConDes (Name, CD_TYPE_INT);
1121 static void DoInvalid (void)
1122 /* Handle a token that is invalid here, since it should have been handled on
1123 * a much lower level of the expression hierarchy. Getting this sort of token
1124 * means that the lower level code has bugs.
1125 * This function differs to DoUnexpected in that the latter may be triggered
1126 * by the user by using keywords in the wrong location. DoUnexpected is not
1127 * an error in the assembler itself, while DoInvalid is.
1130 Internal ("Unexpected token: %s", Keyword);
1135 static void DoLineCont (void)
1136 /* Switch the use of line continuations */
1138 SetBoolOption (&LineCont);
1143 static void DoList (void)
1144 /* Enable/disable the listing */
1146 /* Get the setting */
1148 SetBoolOption (&List);
1150 /* Manage the counter */
1160 static void DoListBytes (void)
1161 /* Set maximum number of bytes to list for one line */
1163 SetListBytes (IntArg (MIN_LIST_BYTES, MAX_LIST_BYTES));
1168 static void DoLocalChar (void)
1169 /* Define the character that starts local labels */
1171 if (Tok != TOK_CHARCON) {
1172 ErrorSkip ("Character constant expected");
1174 if (IVal != '@' && IVal != '?') {
1175 Error ("Invalid start character for locals");
1177 LocalStart = (char) IVal;
1185 static void DoMacPack (void)
1186 /* Insert a macro package */
1188 /* Macro package names */
1189 static const char* Keys [] = {
1198 /* We expect an identifier */
1199 if (Tok != TOK_IDENT) {
1200 ErrorSkip ("Identifier expected");
1204 /* Map the keyword to a number */
1205 Package = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
1208 ErrorSkip ("Invalid macro package");
1212 /* Skip the package name */
1215 /* Insert the package */
1216 InsertMacPack (Package);
1221 static void DoMacro (void)
1222 /* Start a macro definition */
1224 MacDef (MAC_STYLE_CLASSIC);
1229 static void DoNull (void)
1230 /* Switch to the NULL segment */
1232 UseSeg (&NullSegDef);
1237 static void DoOrg (void)
1238 /* Start absolute code */
1240 long PC = ConstExpression ();
1241 if (PC < 0 || PC > 0xFFFFFF) {
1242 Error ("Range error");
1250 static void DoOut (void)
1251 /* Output a string */
1253 if (Tok != TOK_STRCON) {
1254 ErrorSkip ("String constant expected");
1256 /* Output the string and be sure to flush the output to keep it in
1257 * sync with any error messages if the output is redirected to a file.
1259 printf ("%s\n", SVal);
1267 static void DoP02 (void)
1268 /* Switch to 6502 CPU */
1275 static void DoPC02 (void)
1276 /* Switch to 65C02 CPU */
1283 static void DoP816 (void)
1284 /* Switch to 65816 CPU */
1291 static void DoPageLength (void)
1292 /* Set the page length for the listing */
1294 PageLength = IntArg (MIN_PAGE_LEN, MAX_PAGE_LEN);
1299 static void DoPopSeg (void)
1300 /* Pop an old segment from the segment stack */
1304 /* Must have a segment on the stack */
1305 if (CollCount (&SegStack) == 0) {
1306 ErrorSkip ("Segment stack is empty");
1310 /* Pop the last element */
1311 Def = CollPop (&SegStack);
1313 /* Restore this segment */
1316 /* Delete the segment definition */
1322 static void DoProc (void)
1323 /* Start a new lexical scope */
1325 char Name[sizeof(SVal)];
1326 unsigned char AddrSize;
1328 if (Tok == TOK_IDENT) {
1332 /* The new scope has a name. Remember it. */
1333 strcpy (Name, SVal);
1335 /* Search for the symbol, generate a new one if needed */
1336 Sym = SymFind (CurrentScope, Name, SYM_ALLOC_NEW);
1338 /* Skip the scope name */
1341 /* Read an optional address size specifier */
1342 AddrSize = OptionalAddrSize ();
1344 /* Mark the symbol as defined */
1345 SymDef (Sym, GenCurrentPC (), AddrSize, SF_LABEL);
1349 /* A .PROC statement without a name */
1350 Warning (1, "Unnamed .PROCs are deprecated, please use .SCOPE");
1351 AnonName (Name, sizeof (Name), "PROC");
1352 AddrSize = ADDR_SIZE_DEFAULT;
1356 /* Enter a new scope */
1357 SymEnterLevel (Name, ST_PROC, AddrSize);
1362 static void DoPSC02 (void)
1363 /* Switch to 65SC02 CPU */
1365 SetCPU (CPU_65SC02);
1370 static void DoPushSeg (void)
1371 /* Push the current segment onto the segment stack */
1373 /* Can only push a limited size of segments */
1374 if (CollCount (&SegStack) >= MAX_PUSHED_SEGMENTS) {
1375 ErrorSkip ("Segment stack overflow");
1379 /* Get the current segment and push it */
1380 CollAppend (&SegStack, DupSegDef (GetCurrentSegDef ()));
1385 static void DoReloc (void)
1386 /* Enter relocatable mode */
1393 static void DoRepeat (void)
1394 /* Repeat some instruction block */
1401 static void DoRes (void)
1402 /* Reserve some number of storage bytes */
1407 Count = ConstExpression ();
1408 if (Count > 0xFFFF || Count < 0) {
1409 ErrorSkip ("Range error");
1412 if (Tok == TOK_COMMA) {
1414 Val = ConstExpression ();
1415 /* We need a byte value here */
1416 if (!IsByteRange (Val)) {
1417 ErrorSkip ("Range error");
1421 /* Emit constant values */
1423 Emit0 ((unsigned char) Val);
1427 /* Emit fill fragments */
1434 static void DoROData (void)
1435 /* Switch to the r/o data segment */
1437 UseSeg (&RODataSegDef);
1442 static void DoScope (void)
1443 /* Start a local scope */
1445 char Name[sizeof (SVal)];
1446 unsigned char AddrSize;
1449 if (Tok == TOK_IDENT) {
1451 /* The new scope has a name. Remember and skip it. */
1452 strcpy (Name, SVal);
1457 /* An unnamed scope */
1458 AnonName (Name, sizeof (Name), "SCOPE");
1462 /* Read an optional address size specifier */
1463 AddrSize = OptionalAddrSize ();
1465 /* Enter the new scope */
1466 SymEnterLevel (Name, ST_SCOPE, AddrSize);
1472 static void DoSegment (void)
1473 /* Switch to another segment */
1475 char Name [sizeof (SVal)];
1479 if (Tok != TOK_STRCON) {
1480 ErrorSkip ("String constant expected");
1483 /* Save the name of the segment and skip it */
1484 strcpy (Name, SVal);
1487 /* Check for an optional address size modifier */
1488 Def.AddrSize = OptionalAddrSize ();
1490 /* Set the segment */
1497 static void DoSetCPU (void)
1498 /* Switch the CPU instruction set */
1500 /* We expect an identifier */
1501 if (Tok != TOK_STRCON) {
1502 ErrorSkip ("String constant expected");
1504 /* Try to find the CPU, then skip the identifier */
1505 cpu_t CPU = FindCPU (SVal);
1508 /* Switch to the new CPU */
1515 static void DoSmart (void)
1516 /* Smart mode on/off */
1518 SetBoolOption (&SmartMode);
1523 static void DoSunPlus (void)
1524 /* Switch to the SUNPLUS CPU */
1526 SetCPU (CPU_SUNPLUS);
1531 static void DoTag (void)
1532 /* Allocate space for a struct */
1537 /* Read the struct name */
1538 SymTable* Struct = ParseScopedSymTable ();
1540 /* Check the supposed struct */
1542 ErrorSkip ("Unknown struct");
1545 if (GetSymTabType (Struct) != ST_STRUCT) {
1546 ErrorSkip ("Not a struct");
1550 /* Get the symbol that defines the size of the struct */
1551 SizeSym = GetSizeOfScope (Struct);
1553 /* Check if it does exist and if its value is known */
1554 if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
1555 ErrorSkip ("Size of struct/union is unknown");
1559 /* Optional multiplicator may follow */
1560 if (Tok == TOK_COMMA) {
1563 Multiplicator = ConstExpression ();
1564 /* Multiplicator must make sense */
1565 if (Multiplicator <= 0) {
1566 ErrorSkip ("Range error");
1569 Size *= Multiplicator;
1572 /* Emit fill fragments */
1578 static void DoUnexpected (void)
1579 /* Got an unexpected keyword */
1581 Error ("Unexpected `%s'", Keyword);
1587 static void DoWarning (void)
1590 if (Tok != TOK_STRCON) {
1591 ErrorSkip ("String constant expected");
1593 Warning (0, "User warning: %s", SVal);
1600 static void DoWord (void)
1604 EmitWord (Expression ());
1605 if (Tok != TOK_COMMA) {
1615 static void DoZeropage (void)
1616 /* Switch to the zeropage segment */
1618 UseSeg (&ZeropageSegDef);
1623 /*****************************************************************************/
1625 /*****************************************************************************/
1629 /* Control commands flags */
1631 ccNone = 0x0000, /* No special flags */
1632 ccKeepToken = 0x0001 /* Do not skip the current token */
1635 /* Control command table */
1636 typedef struct CtrlDesc CtrlDesc;
1638 unsigned Flags; /* Flags for this directive */
1639 void (*Handler) (void); /* Command handler */
1642 #define PSEUDO_COUNT (sizeof (CtrlCmdTab) / sizeof (CtrlCmdTab [0]))
1643 static CtrlDesc CtrlCmdTab [] = {
1646 { ccNone, DoAddr }, /* .ADDR */
1647 { ccNone, DoAlign },
1648 { ccNone, DoASCIIZ },
1649 { ccNone, DoAssert },
1650 { ccNone, DoAutoImport },
1651 { ccNone, DoUnexpected }, /* .BANKBYTE */
1652 { ccNone, DoUnexpected }, /* .BLANK */
1656 { ccNone, DoCharMap },
1658 { ccNone, DoUnexpected, }, /* .CONCAT */
1659 { ccNone, DoConDes },
1660 { ccNone, DoUnexpected }, /* .CONST */
1661 { ccNone, DoConstructor },
1662 { ccNone, DoUnexpected }, /* .CPU */
1666 { ccNone, DoDebugInfo },
1667 { ccNone, DoDefine },
1668 { ccNone, DoUnexpected }, /* .DEFINED */
1669 { ccNone, DoDestructor },
1670 { ccNone, DoDWord },
1671 { ccKeepToken, DoConditionals }, /* .ELSE */
1672 { ccKeepToken, DoConditionals }, /* .ELSEIF */
1673 { ccKeepToken, DoEnd },
1674 { ccNone, DoUnexpected }, /* .ENDENUM */
1675 { ccKeepToken, DoConditionals }, /* .ENDIF */
1676 { ccNone, DoUnexpected }, /* .ENDMACRO */
1677 { ccNone, DoEndProc },
1678 { ccNone, DoUnexpected }, /* .ENDREPEAT */
1679 { ccNone, DoEndScope },
1680 { ccNone, DoUnexpected }, /* .ENDSTRUCT */
1681 { ccNone, DoUnexpected }, /* .ENDUNION */
1683 { ccNone, DoError },
1684 { ccNone, DoExitMacro },
1685 { ccNone, DoExport },
1686 { ccNone, DoExportZP },
1687 { ccNone, DoFarAddr },
1688 { ccNone, DoFeature },
1689 { ccNone, DoFileOpt },
1690 { ccNone, DoForceImport },
1691 { ccNone, DoUnexpected }, /* .FORCEWORD */
1692 { ccNone, DoGlobal },
1693 { ccNone, DoGlobalZP },
1694 { ccNone, DoUnexpected }, /* .HIBYTE */
1695 { ccNone, DoUnexpected }, /* .HIWORD */
1698 { ccKeepToken, DoConditionals }, /* .IF */
1699 { ccKeepToken, DoConditionals }, /* .IFBLANK */
1700 { ccKeepToken, DoConditionals }, /* .IFCONST */
1701 { ccKeepToken, DoConditionals }, /* .IFDEF */
1702 { ccKeepToken, DoConditionals }, /* .IFNBLANK */
1703 { ccKeepToken, DoConditionals }, /* .IFNCONST */
1704 { ccKeepToken, DoConditionals }, /* .IFNDEF */
1705 { ccKeepToken, DoConditionals }, /* .IFNREF */
1706 { ccKeepToken, DoConditionals }, /* .IFP02 */
1707 { ccKeepToken, DoConditionals }, /* .IFP816 */
1708 { ccKeepToken, DoConditionals }, /* .IFPC02 */
1709 { ccKeepToken, DoConditionals }, /* .IFPSC02 */
1710 { ccKeepToken, DoConditionals }, /* .IFREF */
1711 { ccNone, DoImport },
1712 { ccNone, DoImportZP },
1713 { ccNone, DoIncBin },
1714 { ccNone, DoInclude },
1715 { ccNone, DoInterruptor },
1716 { ccNone, DoInvalid }, /* .LEFT */
1717 { ccNone, DoLineCont },
1719 { ccNone, DoListBytes },
1720 { ccNone, DoUnexpected }, /* .LOBYTE */
1721 { ccNone, DoUnexpected }, /* .LOCAL */
1722 { ccNone, DoLocalChar },
1723 { ccNone, DoUnexpected }, /* .LOWORD */
1724 { ccNone, DoMacPack },
1725 { ccNone, DoMacro },
1726 { ccNone, DoUnexpected }, /* .MATCH */
1727 { ccNone, DoInvalid }, /* .MID */
1733 { ccNone, DoPageLength },
1734 { ccNone, DoUnexpected }, /* .PARAMCOUNT */
1736 { ccNone, DoPopSeg },
1738 { ccNone, DoPSC02 },
1739 { ccNone, DoPushSeg },
1740 { ccNone, DoUnexpected }, /* .REFERENCED */
1741 { ccNone, DoReloc },
1742 { ccNone, DoRepeat },
1744 { ccNone, DoInvalid }, /* .RIGHT */
1745 { ccNone, DoROData },
1746 { ccNone, DoScope },
1747 { ccNone, DoSegment },
1748 { ccNone, DoSetCPU },
1749 { ccNone, DoUnexpected }, /* .SIZEOF */
1750 { ccNone, DoSmart },
1751 { ccNone, DoUnexpected }, /* .STRAT */
1752 { ccNone, DoUnexpected }, /* .STRING */
1753 { ccNone, DoUnexpected }, /* .STRLEN */
1754 { ccNone, DoStruct },
1755 { ccNone, DoSunPlus },
1757 { ccNone, DoUnexpected }, /* .TCOUNT */
1758 { ccNone, DoUnexpected }, /* .TIME */
1759 { ccNone, DoUnion },
1760 { ccNone, DoUnexpected }, /* .VERSION */
1761 { ccNone, DoWarning },
1763 { ccNone, DoUnexpected }, /* .XMATCH */
1764 { ccNone, DoZeropage },
1769 /*****************************************************************************/
1771 /*****************************************************************************/
1775 void HandlePseudo (void)
1776 /* Handle a pseudo instruction */
1780 /* Calculate the index into the table */
1781 unsigned Index = Tok - TOK_FIRSTPSEUDO;
1784 if (PSEUDO_COUNT != (TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1)) {
1785 Internal ("Pseudo mismatch: PSEUDO_COUNT = %u, actual count = %u\n",
1786 (unsigned) PSEUDO_COUNT, TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1);
1788 CHECK (Index < PSEUDO_COUNT);
1790 /* Get the pseudo intruction descriptor */
1791 D = &CtrlCmdTab [Index];
1793 /* Remember the instruction, then skip it if needed */
1794 if ((D->Flags & ccKeepToken) == 0) {
1795 strcpy (Keyword, SVal);
1799 /* Call the handler */
1805 void SegStackCheck (void)
1806 /* Check if the segment stack is empty at end of assembly */
1808 if (CollCount (&SegStack) != 0) {
1809 Error ("Segment stack is not empty");