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 (CurrentScope == RootScope || GetCurrentSymTabType () != ST_PROC) {
713 ErrorSkip ("No open .PROC");
721 static void DoEndScope (void)
722 /* Leave a lexical level */
724 if (CurrentScope == RootScope || 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) {
1310 /* The new scope has a name. Remember it. */
1311 strcpy (Name, SVal);
1313 /* Search for the symbol, generate a new one if needed */
1314 SymEntry* Sym = SymFind (CurrentScope, Name, SYM_ALLOC_NEW);
1316 /* Skip the scope name */
1319 /* Read an optional address size specifier */
1320 AddrSize = OptionalAddrSize ();
1322 /* Mark the symbol as defined */
1323 SymDef (Sym, GenCurrentPC (), AddrSize, SF_LABEL);
1327 /* A .PROC statement without a name */
1328 Warning (1, "Unnamed .PROCs are deprecated, please use .SCOPE");
1329 AnonName (Name, sizeof (Name), "PROC");
1330 AddrSize = ADDR_SIZE_DEFAULT;
1334 /* Enter a new scope */
1335 SymEnterLevel (Name, ST_PROC, AddrSize);
1340 static void DoPSC02 (void)
1341 /* Switch to 65SC02 CPU */
1343 SetCPU (CPU_65SC02);
1348 static void DoPushSeg (void)
1349 /* Push the current segment onto the segment stack */
1351 /* Can only push a limited size of segments */
1352 if (CollCount (&SegStack) >= MAX_PUSHED_SEGMENTS) {
1353 ErrorSkip ("Segment stack overflow");
1357 /* Get the current segment and push it */
1358 CollAppend (&SegStack, DupSegDef (GetCurrentSegDef ()));
1363 static void DoReloc (void)
1364 /* Enter relocatable mode */
1371 static void DoRepeat (void)
1372 /* Repeat some instruction block */
1379 static void DoRes (void)
1380 /* Reserve some number of storage bytes */
1385 Count = ConstExpression ();
1386 if (Count > 0xFFFF || Count < 0) {
1387 ErrorSkip ("Range error");
1390 if (Tok == TOK_COMMA) {
1392 Val = ConstExpression ();
1393 /* We need a byte value here */
1394 if (!IsByteRange (Val)) {
1395 ErrorSkip ("Range error");
1399 /* Emit constant values */
1401 Emit0 ((unsigned char) Val);
1405 /* Emit fill fragments */
1412 static void DoROData (void)
1413 /* Switch to the r/o data segment */
1415 UseSeg (&RODataSegDef);
1420 static void DoScope (void)
1421 /* Start a local scope */
1423 char Name[sizeof (SVal)];
1424 unsigned char AddrSize;
1427 if (Tok == TOK_IDENT) {
1429 /* The new scope has a name. Remember and skip it. */
1430 strcpy (Name, SVal);
1435 /* An unnamed scope */
1436 AnonName (Name, sizeof (Name), "SCOPE");
1440 /* Read an optional address size specifier */
1441 AddrSize = OptionalAddrSize ();
1443 /* Enter the new scope */
1444 SymEnterLevel (Name, ST_SCOPE, AddrSize);
1450 static void DoSegment (void)
1451 /* Switch to another segment */
1453 char Name [sizeof (SVal)];
1457 if (Tok != TOK_STRCON) {
1458 ErrorSkip ("String constant expected");
1461 /* Save the name of the segment and skip it */
1462 strcpy (Name, SVal);
1465 /* Check for an optional address size modifier */
1466 Def.AddrSize = OptionalAddrSize ();
1468 /* Set the segment */
1475 static void DoSetCPU (void)
1476 /* Switch the CPU instruction set */
1478 /* We expect an identifier */
1479 if (Tok != TOK_STRCON) {
1480 ErrorSkip ("String constant expected");
1482 /* Try to find the CPU, then skip the identifier */
1483 cpu_t CPU = FindCPU (SVal);
1486 /* Switch to the new CPU */
1493 static void DoSmart (void)
1494 /* Smart mode on/off */
1496 SetBoolOption (&SmartMode);
1501 static void DoSunPlus (void)
1502 /* Switch to the SUNPLUS CPU */
1504 SetCPU (CPU_SUNPLUS);
1509 static void DoTag (void)
1510 /* Allocate space for a struct */
1515 /* Read the struct name */
1516 SymTable* Struct = ParseScopedSymTable ();
1518 /* Check the supposed struct */
1520 ErrorSkip ("Unknown struct");
1523 if (GetSymTabType (Struct) != ST_STRUCT) {
1524 ErrorSkip ("Not a struct");
1528 /* Get the symbol that defines the size of the struct */
1529 SizeSym = GetSizeOfScope (Struct);
1531 /* Check if it does exist and if its value is known */
1532 if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
1533 ErrorSkip ("Size of struct/union is unknown");
1537 /* Optional multiplicator may follow */
1538 if (Tok == TOK_COMMA) {
1541 Multiplicator = ConstExpression ();
1542 /* Multiplicator must make sense */
1543 if (Multiplicator <= 0) {
1544 ErrorSkip ("Range error");
1547 Size *= Multiplicator;
1550 /* Emit fill fragments */
1556 static void DoUnexpected (void)
1557 /* Got an unexpected keyword */
1559 Error ("Unexpected `%s'", Keyword);
1565 static void DoWarning (void)
1568 if (Tok != TOK_STRCON) {
1569 ErrorSkip ("String constant expected");
1571 Warning (0, "User warning: %s", SVal);
1578 static void DoWord (void)
1582 EmitWord (Expression ());
1583 if (Tok != TOK_COMMA) {
1593 static void DoZeropage (void)
1594 /* Switch to the zeropage segment */
1596 UseSeg (&ZeropageSegDef);
1601 /*****************************************************************************/
1603 /*****************************************************************************/
1607 /* Control commands flags */
1609 ccNone = 0x0000, /* No special flags */
1610 ccKeepToken = 0x0001 /* Do not skip the current token */
1613 /* Control command table */
1614 typedef struct CtrlDesc CtrlDesc;
1616 unsigned Flags; /* Flags for this directive */
1617 void (*Handler) (void); /* Command handler */
1620 #define PSEUDO_COUNT (sizeof (CtrlCmdTab) / sizeof (CtrlCmdTab [0]))
1621 static CtrlDesc CtrlCmdTab [] = {
1624 { ccNone, DoAddr }, /* .ADDR */
1625 { ccNone, DoAlign },
1626 { ccNone, DoASCIIZ },
1627 { ccNone, DoAssert },
1628 { ccNone, DoAutoImport },
1629 { ccNone, DoUnexpected }, /* .BLANK */
1633 { ccNone, DoCharMap },
1635 { ccNone, DoUnexpected, }, /* .CONCAT */
1636 { ccNone, DoConDes },
1637 { ccNone, DoUnexpected }, /* .CONST */
1638 { ccNone, DoConstructor },
1639 { ccNone, DoUnexpected }, /* .CPU */
1643 { ccNone, DoDebugInfo },
1644 { ccNone, DoDefine },
1645 { ccNone, DoUnexpected }, /* .DEFINED */
1646 { ccNone, DoDestructor },
1647 { ccNone, DoDWord },
1648 { ccKeepToken, DoConditionals }, /* .ELSE */
1649 { ccKeepToken, DoConditionals }, /* .ELSEIF */
1650 { ccKeepToken, DoEnd },
1651 { ccNone, DoUnexpected }, /* .ENDENUM */
1652 { ccKeepToken, DoConditionals }, /* .ENDIF */
1653 { ccNone, DoUnexpected }, /* .ENDMACRO */
1654 { ccNone, DoEndProc },
1655 { ccNone, DoUnexpected }, /* .ENDREPEAT */
1656 { ccNone, DoEndScope },
1657 { ccNone, DoUnexpected }, /* .ENDSTRUCT */
1658 { ccNone, DoUnexpected }, /* .ENDUNION */
1660 { ccNone, DoError },
1661 { ccNone, DoExitMacro },
1662 { ccNone, DoExport },
1663 { ccNone, DoExportZP },
1664 { ccNone, DoFarAddr },
1665 { ccNone, DoFeature },
1666 { ccNone, DoFileOpt },
1667 { ccNone, DoForceImport },
1668 { ccNone, DoUnexpected }, /* .FORCEWORD */
1669 { ccNone, DoGlobal },
1670 { ccNone, DoGlobalZP },
1673 { ccKeepToken, DoConditionals }, /* .IF */
1674 { ccKeepToken, DoConditionals }, /* .IFBLANK */
1675 { ccKeepToken, DoConditionals }, /* .IFCONST */
1676 { ccKeepToken, DoConditionals }, /* .IFDEF */
1677 { ccKeepToken, DoConditionals }, /* .IFNBLANK */
1678 { ccKeepToken, DoConditionals }, /* .IFNCONST */
1679 { ccKeepToken, DoConditionals }, /* .IFNDEF */
1680 { ccKeepToken, DoConditionals }, /* .IFNREF */
1681 { ccKeepToken, DoConditionals }, /* .IFP02 */
1682 { ccKeepToken, DoConditionals }, /* .IFP816 */
1683 { ccKeepToken, DoConditionals }, /* .IFPC02 */
1684 { ccKeepToken, DoConditionals }, /* .IFPSC02 */
1685 { ccKeepToken, DoConditionals }, /* .IFREF */
1686 { ccNone, DoImport },
1687 { ccNone, DoImportZP },
1688 { ccNone, DoIncBin },
1689 { ccNone, DoInclude },
1690 { ccNone, DoInvalid }, /* .LEFT */
1691 { ccNone, DoLineCont },
1693 { ccNone, DoListBytes },
1694 { ccNone, DoUnexpected }, /* .LOCAL */
1695 { ccNone, DoLocalChar },
1696 { ccNone, DoMacPack },
1697 { ccNone, DoMacro },
1698 { ccNone, DoUnexpected }, /* .MATCH */
1699 { ccNone, DoInvalid }, /* .MID */
1705 { ccNone, DoPageLength },
1706 { ccNone, DoUnexpected }, /* .PARAMCOUNT */
1708 { ccNone, DoPopSeg },
1710 { ccNone, DoPSC02 },
1711 { ccNone, DoPushSeg },
1712 { ccNone, DoUnexpected }, /* .REFERENCED */
1713 { ccNone, DoReloc },
1714 { ccNone, DoRepeat },
1716 { ccNone, DoInvalid }, /* .RIGHT */
1717 { ccNone, DoROData },
1718 { ccNone, DoScope },
1719 { ccNone, DoSegment },
1720 { ccNone, DoSetCPU },
1721 { ccNone, DoUnexpected }, /* .SIZEOF */
1722 { ccNone, DoSmart },
1723 { ccNone, DoUnexpected }, /* .STRAT */
1724 { ccNone, DoUnexpected }, /* .STRING */
1725 { ccNone, DoUnexpected }, /* .STRLEN */
1726 { ccNone, DoStruct },
1727 { ccNone, DoSunPlus },
1729 { ccNone, DoUnexpected }, /* .TCOUNT */
1730 { ccNone, DoUnexpected }, /* .TIME */
1731 { ccNone, DoUnion },
1732 { ccNone, DoUnexpected }, /* .VERSION */
1733 { ccNone, DoWarning },
1735 { ccNone, DoUnexpected }, /* .XMATCH */
1736 { ccNone, DoZeropage },
1741 /*****************************************************************************/
1743 /*****************************************************************************/
1747 void HandlePseudo (void)
1748 /* Handle a pseudo instruction */
1752 /* Calculate the index into the table */
1753 unsigned Index = Tok - TOK_FIRSTPSEUDO;
1756 if (PSEUDO_COUNT != (TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1)) {
1757 Internal ("Pseudo mismatch: PSEUDO_COUNT = %u, actual count = %u\n",
1758 PSEUDO_COUNT, TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1);
1760 CHECK (Index < PSEUDO_COUNT);
1762 /* Get the pseudo intruction descriptor */
1763 D = &CtrlCmdTab [Index];
1765 /* Remember the instruction, then skip it if needed */
1766 if ((D->Flags & ccKeepToken) == 0) {
1767 strcpy (Keyword, SVal);
1771 /* Call the handler */
1777 void SegStackCheck (void)
1778 /* Check if the segment stack is empty at end of assembly */
1780 if (CollCount (&SegStack) != 0) {
1781 Error ("Segment stack is not empty");