1 /*****************************************************************************/
5 /* Pseudo instructions for the ca65 macroassembler */
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 /*****************************************************************************/
43 #include "assertdefs.h"
76 /*****************************************************************************/
78 /*****************************************************************************/
82 /* Keyword we're about to handle */
83 static char Keyword [sizeof (SVal)+1] = ".";
86 #define MAX_PUSHED_SEGMENTS 16
87 static Collection SegStack = STATIC_COLLECTION_INITIALIZER;
91 /*****************************************************************************/
93 /*****************************************************************************/
97 static void DoUnexpected (void);
98 /* Got an unexpected keyword */
100 static void DoInvalid (void);
101 /* Handle a token that is invalid here, since it should have been handled on
102 * a much lower level of the expression hierarchy. Getting this sort of token
103 * means that the lower level code has bugs.
104 * This function differs to DoUnexpected in that the latter may be triggered
105 * by the user by using keywords in the wrong location. DoUnexpected is not
106 * an error in the assembler itself, while DoInvalid is.
111 /*****************************************************************************/
112 /* Helper functions */
113 /*****************************************************************************/
117 static unsigned OptionalAddrSize (void)
118 /* If a colon follows, parse an optional address size spec and return it.
119 * Otherwise return ADDR_SIZE_DEFAULT.
122 unsigned AddrSize = ADDR_SIZE_DEFAULT;
123 if (Tok == TOK_COLON) {
125 AddrSize = ParseAddrSize ();
133 static void SetBoolOption (unsigned char* Flag)
134 /* Read a on/off/+/- option and set flag accordingly */
136 static const char* Keys[] = {
141 if (Tok == TOK_PLUS) {
144 } else if (Tok == TOK_MINUS) {
147 } else if (Tok == TOK_IDENT) {
148 /* Map the keyword to a number */
149 switch (GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]))) {
150 case 0: *Flag = 0; NextTok (); break;
151 case 1: *Flag = 1; NextTok (); break;
152 default: ErrorSkip ("`on' or `off' expected"); break;
154 } else if (TokIsSep (Tok)) {
155 /* Without anything assume switch on */
158 ErrorSkip ("`on' or `off' expected");
164 static void ExportImport (void (*Func) (SymEntry*, unsigned, unsigned),
165 unsigned DefAddrSize, unsigned Flags)
166 /* Export or import symbols */
173 /* We need an identifier here */
174 if (Tok != TOK_IDENT) {
175 ErrorSkip ("Identifier expected");
179 /* Find the symbol table entry, allocate a new one if necessary */
180 Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
185 /* Get an optional address size */
186 AddrSize = OptionalAddrSize ();
187 if (AddrSize == ADDR_SIZE_DEFAULT) {
188 AddrSize = DefAddrSize;
191 /* Call the actual import/export function */
192 Func (Sym, AddrSize, Flags);
195 if (Tok == TOK_COMMA) {
205 static long IntArg (long Min, long Max)
206 /* Read an integer argument and check a range. Accept the token "unlimited"
207 * and return -1 in this case.
210 if (Tok == TOK_IDENT && strcmp (SVal, "unlimited") == 0) {
214 long Val = ConstExpression ();
215 if (Val < Min || Val > Max) {
216 Error ("Range error");
225 static void ConDes (const char* Name, unsigned Type)
226 /* Parse remaining line for constructor/destructor of the remaining type */
230 /* Optional constructor priority */
231 if (Tok == TOK_COMMA) {
232 /* Priority value follows */
234 Prio = ConstExpression ();
235 if (Prio < CD_PRIO_MIN || Prio > CD_PRIO_MAX) {
236 /* Value out of range */
237 Error ("Range error");
241 /* Use the default priority value */
245 /* Define the symbol */
246 SymConDes (Name, Type, (unsigned) Prio);
251 /*****************************************************************************/
252 /* Handler functions */
253 /*****************************************************************************/
257 static void DoA16 (void)
258 /* Switch the accu to 16 bit mode (assembler only) */
260 if (GetCPU() != CPU_65816) {
261 Error ("Command is only valid in 65816 mode");
263 /* Immidiate mode has two extension bytes */
264 ExtBytes [AMI_IMM_ACCU] = 2;
270 static void DoA8 (void)
271 /* Switch the accu to 8 bit mode (assembler only) */
273 if (GetCPU() != CPU_65816) {
274 Error ("Command is only valid in 65816 mode");
276 /* Immidiate mode has one extension byte */
277 ExtBytes [AMI_IMM_ACCU] = 1;
283 static void DoAddr (void)
284 /* Define addresses */
287 if (GetCPU() == CPU_65816) {
288 EmitWord (GenWordExpr (Expression ()));
290 /* Do a range check */
291 EmitWord (Expression ());
293 if (Tok != TOK_COMMA) {
303 static void DoAlign (void)
304 /* Align the PC to some boundary */
310 /* Read the alignment value */
311 Align = ConstExpression ();
312 if (Align <= 0 || Align > 0x10000) {
313 ErrorSkip ("Range error");
317 /* Optional value follows */
318 if (Tok == TOK_COMMA) {
320 Val = ConstExpression ();
321 /* We need a byte value here */
322 if (!IsByteRange (Val)) {
323 ErrorSkip ("Range error");
330 /* Check if the alignment is a power of two */
331 Bit = BitFind (Align);
332 if (Align != (0x01L << Bit)) {
333 Error ("Alignment value must be a power of 2");
335 SegAlign (Bit, (int) Val);
341 static void DoASCIIZ (void)
342 /* Define text with a zero terminator */
347 /* Must have a string constant */
348 if (Tok != TOK_STRCON) {
349 ErrorSkip ("String constant expected");
353 /* Get the length of the string constant */
356 /* Translate into target charset and emit */
357 TgtTranslateBuf (SVal, Len);
358 EmitData ((unsigned char*) SVal, Len);
360 if (Tok == TOK_COMMA) {
371 static void DoAssert (void)
372 /* Add an assertion */
374 static const char* ActionTab [] = {
382 /* First we have the expression that has to evaluated */
383 ExprNode* Expr = Expression ();
387 if (Tok != TOK_IDENT) {
388 ErrorSkip ("Identifier expected");
391 Action = GetSubKey (ActionTab, sizeof (ActionTab) / sizeof (ActionTab[0]));
397 Action = ASSERT_ACT_WARN;
402 Action = ASSERT_ACT_ERROR;
406 Error ("Illegal assert action specifier");
411 /* Read the message */
412 if (Tok != TOK_STRCON) {
413 ErrorSkip ("String constant expected");
415 AddAssertion (Expr, Action, GetStringId (SVal));
422 static void DoAutoImport (void)
423 /* Mark unresolved symbols as imported */
425 SetBoolOption (&AutoImport);
430 static void DoBss (void)
431 /* Switch to the BSS segment */
438 static void DoByte (void)
442 if (Tok == TOK_STRCON) {
443 /* A string, translate into target charset and emit */
444 unsigned Len = strlen (SVal);
445 TgtTranslateBuf (SVal, Len);
446 EmitData ((unsigned char*) SVal, Len);
449 EmitByte (Expression ());
451 if (Tok != TOK_COMMA) {
455 /* Do smart handling of dangling comma */
456 if (Tok == TOK_SEP) {
457 Error ("Unexpected end of line");
466 static void DoCase (void)
467 /* Switch the IgnoreCase option */
469 SetBoolOption (&IgnoreCase);
470 IgnoreCase = !IgnoreCase;
475 static void DoCharMap (void)
476 /* Allow custome character mappings */
481 /* Read the index as numerical value */
482 Index = ConstExpression ();
483 if (Index < 1 || Index > 255) {
484 /* Value out of range */
485 ErrorSkip ("Range error");
492 /* Read the character code */
493 Code = ConstExpression ();
494 if (Code < 1 || Code > 255) {
495 /* Value out of range */
496 ErrorSkip ("Range error");
500 /* Set the character translation */
501 TgtTranslateSet ((unsigned) Index, (unsigned char) Code);
506 static void DoCode (void)
507 /* Switch to the code segment */
509 UseSeg (&CodeSegDef);
514 static void DoConDes (void)
515 /* Export a symbol as constructor/destructor */
517 static const char* Keys[] = {
521 char Name [sizeof (SVal)];
524 /* Symbol name follows */
525 if (Tok != TOK_IDENT) {
526 ErrorSkip ("Identifier expected");
532 /* Type follows. May be encoded as identifier or numerical */
534 if (Tok == TOK_IDENT) {
536 /* Map the following keyword to a number, then skip it */
537 Type = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
540 /* Check if we got a valid keyword */
542 Error ("Syntax error");
549 /* Read the type as numerical value */
550 Type = ConstExpression ();
551 if (Type < CD_TYPE_MIN || Type > CD_TYPE_MAX) {
552 /* Value out of range */
553 Error ("Range error");
559 /* Parse the remainder of the line and export the symbol */
560 ConDes (Name, (unsigned) Type);
565 static void DoConstructor (void)
566 /* Export a symbol as constructor */
568 char Name [sizeof (SVal)];
570 /* Symbol name follows */
571 if (Tok != TOK_IDENT) {
572 ErrorSkip ("Identifier expected");
578 /* Parse the remainder of the line and export the symbol */
579 ConDes (Name, CD_TYPE_CON);
584 static void DoData (void)
585 /* Switch to the data segment */
587 UseSeg (&DataSegDef);
592 static void DoDbg (void)
593 /* Add debug information from high level code */
595 static const char* Keys[] = {
603 /* We expect a subkey */
604 if (Tok != TOK_IDENT) {
605 ErrorSkip ("Identifier expected");
609 /* Map the following keyword to a number */
610 Key = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
612 /* Skip the subkey */
615 /* Check the key and dispatch to a handler */
617 case 0: DbgInfoFile (); break;
618 case 1: DbgInfoLine (); break;
619 case 2: DbgInfoSym (); break;
620 default: ErrorSkip ("Syntax error"); break;
626 static void DoDByt (void)
627 /* Output double bytes */
630 EmitWord (GenSwapExpr (Expression ()));
631 if (Tok != TOK_COMMA) {
641 static void DoDebugInfo (void)
642 /* Switch debug info on or off */
644 SetBoolOption (&DbgSyms);
649 static void DoDefine (void)
650 /* Define a one line macro */
652 MacDef (MAC_STYLE_DEFINE);
657 static void DoDestructor (void)
658 /* Export a symbol as destructor */
660 char Name [sizeof (SVal)];
662 /* Symbol name follows */
663 if (Tok != TOK_IDENT) {
664 ErrorSkip ("Identifier expected");
670 /* Parse the remainder of the line and export the symbol */
671 ConDes (Name, CD_TYPE_DES);
676 static void DoDWord (void)
680 EmitDWord (Expression ());
681 if (Tok != TOK_COMMA) {
691 static void DoEnd (void)
692 /* End of assembly */
700 static void DoEndProc (void)
701 /* Leave a lexical level */
703 if (CurrentScope != RootScope) {
707 ErrorSkip ("No open lexical level");
713 static void DoEndScope (void)
714 /* Leave a lexical level */
716 if (CurrentScope != RootScope) {
720 ErrorSkip ("No open lexical level");
726 static void DoError (void)
729 if (Tok != TOK_STRCON) {
730 ErrorSkip ("String constant expected");
732 Error ("User error: %s", SVal);
739 static void DoExitMacro (void)
740 /* Exit a macro expansion */
742 if (!InMacExpansion ()) {
743 /* We aren't expanding a macro currently */
752 static void DoExport (void)
753 /* Export a symbol */
755 ExportImport (SymExport, ADDR_SIZE_DEFAULT, SF_NONE);
760 static void DoExportZP (void)
761 /* Export a zeropage symbol */
763 ExportImport (SymExport, ADDR_SIZE_ZP, SF_NONE);
768 static void DoFarAddr (void)
769 /* Define far addresses (24 bit) */
772 EmitFarAddr (Expression ());
773 if (Tok != TOK_COMMA) {
783 static void DoFeature (void)
784 /* Switch the Feature option */
786 /* Allow a list of comma separated keywords */
789 /* We expect an identifier */
790 if (Tok != TOK_IDENT) {
791 ErrorSkip ("Identifier expected");
795 /* Make the string attribute lower case */
798 /* Set the feature and check for errors */
799 if (SetFeature (SVal) == FEAT_UNKNOWN) {
801 ErrorSkip ("Invalid feature: `%s'", SVal);
804 /* Skip the keyword */
808 /* Allow more than one keyword */
809 if (Tok == TOK_COMMA) {
819 static void DoFileOpt (void)
820 /* Insert a file option */
824 /* The option type may be given as a keyword or as a number. */
825 if (Tok == TOK_IDENT) {
827 /* Option given as keyword */
828 static const char* Keys [] = {
829 "AUTHOR", "COMMENT", "COMPILER"
832 /* Map the option to a number */
833 OptNum = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
836 ErrorSkip ("File option keyword expected");
840 /* Skip the keyword */
843 /* Must be followed by a comma */
846 /* We accept only string options for now */
847 if (Tok != TOK_STRCON) {
848 ErrorSkip ("String constant expected");
852 /* Insert the option */
871 Internal ("Invalid OptNum: %ld", OptNum);
880 /* Option given as number */
881 OptNum = ConstExpression ();
882 if (!IsByteRange (OptNum)) {
883 ErrorSkip ("Range error");
887 /* Must be followed by a comma */
890 /* We accept only string options for now */
891 if (Tok != TOK_STRCON) {
892 ErrorSkip ("String constant expected");
896 /* Insert the option */
897 OptStr ((unsigned char) OptNum, SVal);
906 static void DoForceImport (void)
907 /* Do a forced import on a symbol */
909 ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_FORCED);
914 static void DoGlobal (void)
915 /* Declare a global symbol */
917 ExportImport (SymGlobal, ADDR_SIZE_DEFAULT, SF_NONE);
922 static void DoGlobalZP (void)
923 /* Declare a global zeropage symbol */
925 ExportImport (SymGlobal, ADDR_SIZE_ZP, SF_NONE);
930 static void DoI16 (void)
931 /* Switch the index registers to 16 bit mode (assembler only) */
933 if (GetCPU() != CPU_65816) {
934 Error ("Command is only valid in 65816 mode");
936 /* Immidiate mode has two extension bytes */
937 ExtBytes [AMI_IMM_INDEX] = 2;
943 static void DoI8 (void)
944 /* Switch the index registers to 16 bit mode (assembler only) */
946 if (GetCPU() != CPU_65816) {
947 Error ("Command is only valid in 65816 mode");
949 /* Immidiate mode has one extension byte */
950 ExtBytes [AMI_IMM_INDEX] = 1;
956 static void DoImport (void)
957 /* Import a symbol */
959 ExportImport (SymImport, ADDR_SIZE_DEFAULT, SF_NONE);
964 static void DoImportZP (void)
965 /* Import a zero page symbol */
967 ExportImport (SymImport, ADDR_SIZE_ZP, SF_NONE);
972 static void DoIncBin (void)
973 /* Include a binary file */
975 char Name [sizeof (SVal)];
981 /* Name must follow */
982 if (Tok != TOK_STRCON) {
983 ErrorSkip ("String constant expected");
989 /* A starting offset may follow */
990 if (Tok == TOK_COMMA) {
992 Start = ConstExpression ();
994 /* And a length may follow */
995 if (Tok == TOK_COMMA) {
997 Count = ConstExpression ();
1002 /* Try to open the file */
1003 F = fopen (Name, "rb");
1006 /* Search for the file in the include directories. */
1007 char* PathName = FindInclude (Name);
1008 if (PathName == 0 || (F = fopen (PathName, "r")) == 0) {
1009 /* Not found or cannot open, print an error and bail out */
1010 ErrorSkip ("Cannot open include file `%s': %s", Name, strerror (errno));
1013 /* Free the allocated memory */
1016 /* If we had an error before, bail out now */
1022 /* Get the size of the file */
1023 fseek (F, 0, SEEK_END);
1026 /* If a count was not given, calculate it now */
1028 Count = Size - Start;
1030 /* Nothing to read - flag this as a range error */
1031 ErrorSkip ("Range error");
1035 /* Count was given, check if it is valid */
1036 if (Start + Count > Size) {
1037 ErrorSkip ("Range error");
1042 /* Seek to the start position */
1043 fseek (F, Start, SEEK_SET);
1045 /* Read chunks and insert them into the output */
1048 unsigned char Buf [1024];
1050 /* Calculate the number of bytes to read */
1051 size_t BytesToRead = (Count > (long)sizeof(Buf))? sizeof(Buf) : (size_t) Count;
1054 size_t BytesRead = fread (Buf, 1, BytesToRead, F);
1055 if (BytesToRead != BytesRead) {
1056 /* Some sort of error */
1057 ErrorSkip ("Cannot read from include file `%s': %s",
1058 Name, strerror (errno));
1062 /* Insert it into the output */
1063 EmitData (Buf, BytesRead);
1065 /* Keep the counters current */
1070 /* Close the file, ignore errors since it's r/o */
1076 static void DoInclude (void)
1077 /* Include another file */
1079 char Name [MAX_STR_LEN+1];
1081 /* Name must follow */
1082 if (Tok != TOK_STRCON) {
1083 ErrorSkip ("String constant expected");
1085 strcpy (Name, SVal);
1087 NewInputFile (Name);
1093 static void DoInvalid (void)
1094 /* Handle a token that is invalid here, since it should have been handled on
1095 * a much lower level of the expression hierarchy. Getting this sort of token
1096 * means that the lower level code has bugs.
1097 * This function differs to DoUnexpected in that the latter may be triggered
1098 * by the user by using keywords in the wrong location. DoUnexpected is not
1099 * an error in the assembler itself, while DoInvalid is.
1102 Internal ("Unexpected token: %s", Keyword);
1107 static void DoLineCont (void)
1108 /* Switch the use of line continuations */
1110 SetBoolOption (&LineCont);
1115 static void DoList (void)
1116 /* Enable/disable the listing */
1118 /* Get the setting */
1120 SetBoolOption (&List);
1122 /* Manage the counter */
1132 static void DoListBytes (void)
1133 /* Set maximum number of bytes to list for one line */
1135 SetListBytes (IntArg (MIN_LIST_BYTES, MAX_LIST_BYTES));
1140 static void DoLocalChar (void)
1141 /* Define the character that starts local labels */
1143 if (Tok != TOK_CHARCON) {
1144 ErrorSkip ("Character constant expected");
1146 if (IVal != '@' && IVal != '?') {
1147 Error ("Invalid start character for locals");
1149 LocalStart = (char) IVal;
1157 static void DoMacPack (void)
1158 /* Insert a macro package */
1160 /* Macro package names */
1161 static const char* Keys [] = {
1170 /* We expect an identifier */
1171 if (Tok != TOK_IDENT) {
1172 ErrorSkip ("Identifier expected");
1176 /* Map the keyword to a number */
1177 Package = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
1180 ErrorSkip ("Invalid macro package");
1184 /* Skip the package name */
1187 /* Insert the package */
1188 InsertMacPack (Package);
1193 static void DoMacro (void)
1194 /* Start a macro definition */
1196 MacDef (MAC_STYLE_CLASSIC);
1201 static void DoNull (void)
1202 /* Switch to the NULL segment */
1204 UseSeg (&NullSegDef);
1209 static void DoOrg (void)
1210 /* Start absolute code */
1212 long PC = ConstExpression ();
1213 if (PC < 0 || PC > 0xFFFFFF) {
1214 Error ("Range error");
1222 static void DoOut (void)
1223 /* Output a string */
1225 if (Tok != TOK_STRCON) {
1226 ErrorSkip ("String constant expected");
1228 /* Output the string and be sure to flush the output to keep it in
1229 * sync with any error messages if the output is redirected to a file.
1231 printf ("%s\n", SVal);
1239 static void DoP02 (void)
1240 /* Switch to 6502 CPU */
1247 static void DoPC02 (void)
1248 /* Switch to 65C02 CPU */
1255 static void DoP816 (void)
1256 /* Switch to 65816 CPU */
1263 static void DoPageLength (void)
1264 /* Set the page length for the listing */
1266 PageLength = IntArg (MIN_PAGE_LEN, MAX_PAGE_LEN);
1271 static void DoPopSeg (void)
1272 /* Pop an old segment from the segment stack */
1276 /* Must have a segment on the stack */
1277 if (CollCount (&SegStack) == 0) {
1278 ErrorSkip ("Segment stack is empty");
1282 /* Pop the last element */
1283 Def = CollPop (&SegStack);
1285 /* Restore this segment */
1288 /* Delete the segment definition */
1294 static void DoProc (void)
1295 /* Start a new lexical scope */
1297 if (Tok == TOK_IDENT) {
1301 /* The new scope has a name. Remember it. */
1302 char Name[sizeof(SVal)];
1303 strcpy (Name, SVal);
1305 /* Search for the symbol, generate a new one if needed */
1306 SymEntry* Sym = SymFind (CurrentScope, Name, SYM_ALLOC_NEW);
1308 /* Skip the scope name */
1311 /* Read an optional address size specifier */
1312 AddrSize = OptionalAddrSize ();
1314 /* Mark the symbol as defined */
1315 SymDef (Sym, GenCurrentPC (), AddrSize, SF_LABEL);
1317 /* Enter a new scope with the given name */
1318 SymEnterLevel (Name, AddrSize);
1322 /* A .PROC statement without a name */
1323 char Buf[sizeof (SVal)];
1324 SymEnterLevel (AnonName (Buf, sizeof (Buf), "Scope"), ADDR_SIZE_DEFAULT);
1325 Warning (1, "Unnamed .PROCs are deprecated, please use .SCOPE");
1332 static void DoPSC02 (void)
1333 /* Switch to 65SC02 CPU */
1335 SetCPU (CPU_65SC02);
1340 static void DoPushSeg (void)
1341 /* Push the current segment onto the segment stack */
1343 /* Can only push a limited size of segments */
1344 if (CollCount (&SegStack) >= MAX_PUSHED_SEGMENTS) {
1345 ErrorSkip ("Segment stack overflow");
1349 /* Get the current segment and push it */
1350 CollAppend (&SegStack, DupSegDef (GetCurrentSegDef ()));
1355 static void DoReloc (void)
1356 /* Enter relocatable mode */
1363 static void DoRepeat (void)
1364 /* Repeat some instruction block */
1371 static void DoRes (void)
1372 /* Reserve some number of storage bytes */
1377 Count = ConstExpression ();
1378 if (Count > 0xFFFF || Count < 0) {
1379 ErrorSkip ("Range error");
1382 if (Tok == TOK_COMMA) {
1384 Val = ConstExpression ();
1385 /* We need a byte value here */
1386 if (!IsByteRange (Val)) {
1387 ErrorSkip ("Range error");
1391 /* Emit constant values */
1393 Emit0 ((unsigned char) Val);
1397 /* Emit fill fragments */
1404 static void DoROData (void)
1405 /* Switch to the r/o data segment */
1407 UseSeg (&RODataSegDef);
1412 static void DoScope (void)
1413 /* Start a local scope */
1415 char Name[sizeof (SVal)];
1417 if (Tok == TOK_IDENT) {
1421 /* The new scope has a name. Remember and skip it. */
1422 strcpy (Name, SVal);
1425 /* Read an optional address size specifier */
1426 AddrSize = OptionalAddrSize ();
1428 /* Enter a new scope with the given name */
1429 SymEnterLevel (Name, AddrSize);
1433 /* An unnamed scope */
1434 SymEnterLevel (AnonName (Name, sizeof (Name), "Scope"), ADDR_SIZE_DEFAULT);
1441 static void DoSegment (void)
1442 /* Switch to another segment */
1444 char Name [sizeof (SVal)];
1448 if (Tok != TOK_STRCON) {
1449 ErrorSkip ("String constant expected");
1452 /* Save the name of the segment and skip it */
1453 strcpy (Name, SVal);
1456 /* Check for an optional address size modifier */
1457 Def.AddrSize = OptionalAddrSize ();
1459 /* Set the segment */
1466 static void DoSetCPU (void)
1467 /* Switch the CPU instruction set */
1469 /* We expect an identifier */
1470 if (Tok != TOK_STRCON) {
1471 ErrorSkip ("String constant expected");
1473 /* Try to find the CPU, then skip the identifier */
1474 cpu_t CPU = FindCPU (SVal);
1477 /* Switch to the new CPU */
1484 static void DoSmart (void)
1485 /* Smart mode on/off */
1487 SetBoolOption (&SmartMode);
1492 static void DoStruct (void)
1493 /* Struct definition */
1495 Error ("Not implemented");
1500 static void DoSunPlus (void)
1501 /* Switch to the SUNPLUS CPU */
1503 SetCPU (CPU_SUNPLUS);
1508 static void DoUnion (void)
1509 /* Union definition */
1511 Error ("Not implemented");
1516 static void DoUnexpected (void)
1517 /* Got an unexpected keyword */
1519 Error ("Unexpected `%s'", Keyword);
1525 static void DoWarning (void)
1528 if (Tok != TOK_STRCON) {
1529 ErrorSkip ("String constant expected");
1531 Warning (0, "User warning: %s", SVal);
1538 static void DoWord (void)
1542 EmitWord (Expression ());
1543 if (Tok != TOK_COMMA) {
1553 static void DoZeropage (void)
1554 /* Switch to the zeropage segment */
1556 UseSeg (&ZeropageSegDef);
1561 /*****************************************************************************/
1563 /*****************************************************************************/
1567 /* Control commands flags */
1569 ccNone = 0x0000, /* No special flags */
1570 ccKeepToken = 0x0001 /* Do not skip the current token */
1573 /* Control command table */
1574 typedef struct CtrlDesc CtrlDesc;
1576 unsigned Flags; /* Flags for this directive */
1577 void (*Handler) (void); /* Command handler */
1580 #define PSEUDO_COUNT (sizeof (CtrlCmdTab) / sizeof (CtrlCmdTab [0]))
1581 static CtrlDesc CtrlCmdTab [] = {
1584 { ccNone, DoAddr }, /* .ADDR */
1585 { ccNone, DoAlign },
1586 { ccNone, DoASCIIZ },
1587 { ccNone, DoAssert },
1588 { ccNone, DoAutoImport },
1589 { ccNone, DoUnexpected }, /* .BLANK */
1593 { ccNone, DoCharMap },
1595 { ccNone, DoUnexpected, }, /* .CONCAT */
1596 { ccNone, DoConDes },
1597 { ccNone, DoUnexpected }, /* .CONST */
1598 { ccNone, DoConstructor },
1599 { ccNone, DoUnexpected }, /* .CPU */
1603 { ccNone, DoDebugInfo },
1604 { ccNone, DoDefine },
1605 { ccNone, DoUnexpected }, /* .DEFINED */
1606 { ccNone, DoDestructor },
1607 { ccNone, DoDWord },
1608 { ccKeepToken, DoConditionals }, /* .ELSE */
1609 { ccKeepToken, DoConditionals }, /* .ELSEIF */
1610 { ccKeepToken, DoEnd },
1611 { ccKeepToken, DoConditionals }, /* .ENDIF */
1612 { ccNone, DoUnexpected }, /* .ENDMACRO */
1613 { ccNone, DoEndProc },
1614 { ccNone, DoUnexpected }, /* .ENDREPEAT */
1615 { ccNone, DoEndScope },
1616 { ccNone, DoUnexpected }, /* .ENDSTRUCT */
1617 { ccNone, DoError },
1618 { ccNone, DoExitMacro },
1619 { ccNone, DoExport },
1620 { ccNone, DoExportZP },
1621 { ccNone, DoFarAddr },
1622 { ccNone, DoFeature },
1623 { ccNone, DoFileOpt },
1624 { ccNone, DoForceImport },
1625 { ccNone, DoUnexpected }, /* .FORCEWORD */
1626 { ccNone, DoGlobal },
1627 { ccNone, DoGlobalZP },
1630 { ccKeepToken, DoConditionals }, /* .IF */
1631 { ccKeepToken, DoConditionals }, /* .IFBLANK */
1632 { ccKeepToken, DoConditionals }, /* .IFCONST */
1633 { ccKeepToken, DoConditionals }, /* .IFDEF */
1634 { ccKeepToken, DoConditionals }, /* .IFNBLANK */
1635 { ccKeepToken, DoConditionals }, /* .IFNCONST */
1636 { ccKeepToken, DoConditionals }, /* .IFNDEF */
1637 { ccKeepToken, DoConditionals }, /* .IFNREF */
1638 { ccKeepToken, DoConditionals }, /* .IFP02 */
1639 { ccKeepToken, DoConditionals }, /* .IFP816 */
1640 { ccKeepToken, DoConditionals }, /* .IFPC02 */
1641 { ccKeepToken, DoConditionals }, /* .IFPSC02 */
1642 { ccKeepToken, DoConditionals }, /* .IFREF */
1643 { ccNone, DoImport },
1644 { ccNone, DoImportZP },
1645 { ccNone, DoIncBin },
1646 { ccNone, DoInclude },
1647 { ccNone, DoInvalid }, /* .LEFT */
1648 { ccNone, DoLineCont },
1650 { ccNone, DoListBytes },
1651 { ccNone, DoUnexpected }, /* .LOCAL */
1652 { ccNone, DoLocalChar },
1653 { ccNone, DoMacPack },
1654 { ccNone, DoMacro },
1655 { ccNone, DoUnexpected }, /* .MATCH */
1656 { ccNone, DoInvalid }, /* .MID */
1662 { ccNone, DoPageLength },
1663 { ccNone, DoUnexpected }, /* .PARAMCOUNT */
1665 { ccNone, DoPopSeg },
1667 { ccNone, DoPSC02 },
1668 { ccNone, DoPushSeg },
1669 { ccNone, DoUnexpected }, /* .REFERENCED */
1670 { ccNone, DoReloc },
1671 { ccNone, DoRepeat },
1673 { ccNone, DoInvalid }, /* .RIGHT */
1674 { ccNone, DoROData },
1675 { ccNone, DoScope },
1676 { ccNone, DoSegment },
1677 { ccNone, DoSetCPU },
1678 { ccNone, DoSmart },
1679 { ccNone, DoUnexpected }, /* .STRAT */
1680 { ccNone, DoUnexpected }, /* .STRING */
1681 { ccNone, DoUnexpected }, /* .STRLEN */
1682 { ccNone, DoStruct },
1683 { ccNone, DoSunPlus },
1684 { ccNone, DoUnexpected }, /* .TAG */
1685 { ccNone, DoUnexpected }, /* .TCOUNT */
1686 { ccNone, DoUnexpected }, /* .TIME */
1687 { ccNone, DoUnion },
1688 { ccNone, DoUnexpected }, /* .VERSION */
1689 { ccNone, DoWarning },
1691 { ccNone, DoUnexpected }, /* .XMATCH */
1692 { ccNone, DoZeropage },
1697 /*****************************************************************************/
1699 /*****************************************************************************/
1703 int TokIsPseudo (unsigned Tok)
1704 /* Return true if the given token is a pseudo instruction token */
1706 return (Tok >= TOK_FIRSTPSEUDO && Tok <= TOK_LASTPSEUDO);
1711 void HandlePseudo (void)
1712 /* Handle a pseudo instruction */
1716 /* Calculate the index into the table */
1717 unsigned Index = Tok - TOK_FIRSTPSEUDO;
1720 if (PSEUDO_COUNT != (TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1)) {
1721 Internal ("Pseudo mismatch: PSEUDO_COUNT = %u, actual count = %u\n",
1722 PSEUDO_COUNT, TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1);
1724 CHECK (Index < PSEUDO_COUNT);
1726 /* Get the pseudo intruction descriptor */
1727 D = &CtrlCmdTab [Index];
1729 /* Remember the instruction, then skip it if needed */
1730 if ((D->Flags & ccKeepToken) == 0) {
1731 strcpy (Keyword+1, SVal);
1735 /* Call the handler */
1741 void SegStackCheck (void)
1742 /* Check if the segment stack is empty at end of assembly */
1744 if (CollCount (&SegStack) != 0) {
1745 Error ("Segment stack is not empty");