1 /*****************************************************************************/
5 /* Pseudo instructions for the ca65 macroassembler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
65 /*****************************************************************************/
67 /*****************************************************************************/
71 /* Keyword we're about to handle */
72 static char Keyword [sizeof (SVal)+1] = ".";
76 /*****************************************************************************/
78 /*****************************************************************************/
82 static void DoUnexpected (void);
83 /* Got an unexpected keyword */
85 static void DoInvalid (void);
86 /* Handle a token that is invalid here, since it should have been handled on
87 * a much lower level of the expression hierarchy. Getting this sort of token
88 * means that the lower level code has bugs.
89 * This function differs to DoUnexpected in that the latter may be triggered
90 * by the user by using keywords in the wrong location. DoUnexpected is not
91 * an error in the assembler itself, while DoInvalid is.
96 /*****************************************************************************/
97 /* Helper functions */
98 /*****************************************************************************/
102 static void SetBoolOption (unsigned char* Flag)
103 /* Read a on/off/+/- option and set flag accordingly */
105 static const char* Keys[] = {
110 if (Tok == TOK_PLUS) {
113 } else if (Tok == TOK_MINUS) {
116 } else if (Tok == TOK_IDENT) {
117 /* Map the keyword to a number */
118 switch (GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]))) {
119 case 0: *Flag = 0; NextTok (); break;
120 case 1: *Flag = 1; NextTok (); break;
121 default: ErrorSkip (ERR_ONOFF_EXPECTED); break;
123 } else if (Tok == TOK_SEP || Tok == TOK_EOF) {
124 /* Without anything assume switch on */
127 ErrorSkip (ERR_ONOFF_EXPECTED);
133 static void ExportImport (void (*SymFunc) (const char*, int), int ZP)
134 /* Export or import symbols */
137 if (Tok != TOK_IDENT) {
138 ErrorSkip (ERR_IDENT_EXPECTED);
143 if (Tok == TOK_COMMA) {
153 static long IntArg (long Min, long Max)
154 /* Read an integer argument and check a range. Accept the token "unlimited"
155 * and return -1 in this case.
158 if (Tok == TOK_IDENT && strcmp (SVal, "unlimited") == 0) {
162 long Val = ConstExpression ();
163 if (Val < Min || Val > Max) {
173 /*****************************************************************************/
174 /* Handler functions */
175 /*****************************************************************************/
179 static void DoA16 (void)
180 /* Switch the accu to 16 bit mode (assembler only) */
182 if (GetCPU() != CPU_65816) {
183 Error (ERR_816_MODE_ONLY);
185 /* Immidiate mode has two extension bytes */
186 ExtBytes [AMI_IMM_ACCU] = 2;
192 static void DoA8 (void)
193 /* Switch the accu to 8 bit mode (assembler only) */
195 if (GetCPU() != CPU_65816) {
196 Error (ERR_816_MODE_ONLY);
198 /* Immidiate mode has one extension byte */
199 ExtBytes [AMI_IMM_ACCU] = 1;
205 static void DoAddr (void)
206 /* Define addresses */
209 if (GetCPU() == CPU_65816) {
210 EmitWord (ForceWordExpr (Expression ()));
212 /* Do a range check */
213 EmitWord (Expression ());
215 if (Tok != TOK_COMMA) {
225 static void DoAlign (void)
226 /* Align the PC to some boundary */
232 /* Read the alignment value */
233 Align = ConstExpression ();
234 if (Align <= 0 || Align > 0x10000) {
235 ErrorSkip (ERR_RANGE);
239 /* Optional value follows */
240 if (Tok == TOK_COMMA) {
242 Val = ConstExpression ();
243 /* We need a byte value here */
244 if (!IsByteRange (Val)) {
245 ErrorSkip (ERR_RANGE);
252 /* Check if the alignment is a power of two */
253 Bit = BitFind (Align);
254 if (Align != (0x01L << Bit)) {
257 SegAlign (Bit, (int) Val);
263 static void DoASCIIZ (void)
264 /* Define text with a zero terminator */
267 if (Tok != TOK_STRCON) {
268 ErrorSkip (ERR_STRCON_EXPECTED);
271 EmitData ((unsigned char*) SVal, strlen (SVal));
273 if (Tok == TOK_COMMA) {
284 static void DoAutoImport (void)
285 /* Mark unresolved symbols as imported */
287 SetBoolOption (&AutoImport);
292 static void DoBss (void)
293 /* Switch to the BSS segment */
300 static void DoByte (void)
304 if (Tok == TOK_STRCON) {
306 EmitData ((unsigned char*) SVal, strlen (SVal));
309 EmitByte (Expression ());
311 if (Tok != TOK_COMMA) {
315 /* Do smart handling of dangling comma */
316 if (Tok == TOK_SEP) {
317 Error (ERR_UNEXPECTED_EOL);
326 static void DoCase (void)
327 /* Switch the IgnoreCase option */
329 SetBoolOption (&IgnoreCase);
330 IgnoreCase = !IgnoreCase;
335 static void DoCode (void)
336 /* Switch to the code segment */
343 static void DoData (void)
344 /* Switch to the data segment */
351 static void DoDbg (void)
352 /* Add debug information from high level code */
354 static const char* Keys[] = {
362 /* We expect a subkey */
363 if (Tok != TOK_IDENT) {
364 ErrorSkip (ERR_IDENT_EXPECTED);
368 /* Map the following keyword to a number */
369 Key = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
371 /* Skip the subkey */
374 /* Parameters are separated by a comma */
377 /* Check the key and dispatch to a handler */
379 case 0: DbgInfoFile (); break;
381 default: ErrorSkip (ERR_SYNTAX); break;
387 static void DoDByt (void)
388 /* Output double bytes */
391 EmitWord (SwapExpr (Expression ()));
392 if (Tok != TOK_COMMA) {
402 static void DoDebugInfo (void)
403 /* Switch debug info on or off */
405 SetBoolOption (&DbgSyms);
410 static void DoDefine (void)
411 /* Define a one line macro */
413 MacDef (MAC_STYLE_DEFINE);
418 static void DoDWord (void)
422 EmitDWord (Expression ());
423 if (Tok != TOK_COMMA) {
433 static void DoEnd (void)
434 /* End of assembly */
441 static void DoEndProc (void)
442 /* Leave a lexical level */
449 static void DoError (void)
452 if (Tok != TOK_STRCON) {
453 ErrorSkip (ERR_STRCON_EXPECTED);
455 Error (ERR_USER, SVal);
462 static void DoExitMacro (void)
463 /* Exit a macro expansion */
465 if (!InMacExpansion ()) {
466 /* We aren't expanding a macro currently */
475 static void DoExport (void)
476 /* Export a symbol */
478 ExportImport (SymExport, 0);
483 static void DoExportZP (void)
484 /* Export a zeropage symbol */
486 ExportImport (SymExport, 1);
491 static void DoFarAddr (void)
492 /* Define far addresses (24 bit) */
495 EmitFarAddr (Expression ());
496 if (Tok != TOK_COMMA) {
506 static void DoFeature (void)
507 /* Switch the Feature option */
511 static const char* Keys[] = {
513 "LABELS_WITHOUT_COLONS",
516 "DOLLAR_IN_IDENTIFIERS",
519 /* Allow a list of comma separated keywords */
522 /* We expect an identifier */
523 if (Tok != TOK_IDENT) {
524 ErrorSkip (ERR_IDENT_EXPECTED);
528 /* Map the keyword to a number */
529 Feature = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
532 ErrorSkip (ERR_ILLEGAL_FEATURE);
536 /* Skip the keyword */
539 /* Switch the feature on */
541 case 0: DollarIsPC = 1; break;
542 case 1: NoColonLabels = 1; break;
543 case 2: LooseStringTerm = 1; break;
544 case 3: AtInIdents = 1; break;
545 case 4: DollarInIdents = 1; break;
546 default: Internal ("Invalid feature: %d", Feature);
549 /* Allow more than one keyword */
550 if (Tok == TOK_COMMA) {
560 static void DoFileOpt (void)
561 /* Insert a file option */
565 /* The option type may be given as a keyword or as a number. */
566 if (Tok == TOK_IDENT) {
568 /* Option given as keyword */
569 static const char* Keys [] = {
570 "AUTHOR", "COMMENT", "COMPILER"
573 /* Map the option to a number */
574 OptNum = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
577 ErrorSkip (ERR_OPTION_KEY_EXPECTED);
581 /* Skip the keyword */
584 /* Must be followed by a comma */
587 /* We accept only string options for now */
588 if (Tok != TOK_STRCON) {
589 ErrorSkip (ERR_STRCON_EXPECTED);
593 /* Insert the option */
612 Internal ("Invalid OptNum: %l", OptNum);
621 /* Option given as number */
622 OptNum = ConstExpression ();
623 if (!IsByteRange (OptNum)) {
624 ErrorSkip (ERR_RANGE);
628 /* Must be followed by a comma */
631 /* We accept only string options for now */
632 if (Tok != TOK_STRCON) {
633 ErrorSkip (ERR_STRCON_EXPECTED);
637 /* Insert the option */
638 OptStr ((unsigned char) OptNum, SVal);
647 static void DoGlobal (void)
648 /* Declare a global symbol */
650 ExportImport (SymGlobal, 0);
655 static void DoGlobalZP (void)
656 /* Declare a global zeropage symbol */
658 ExportImport (SymGlobal, 1);
663 static void DoI16 (void)
664 /* Switch the index registers to 16 bit mode (assembler only) */
666 if (GetCPU() != CPU_65816) {
667 Error (ERR_816_MODE_ONLY);
669 /* Immidiate mode has two extension bytes */
670 ExtBytes [AMI_IMM_INDEX] = 2;
676 static void DoI8 (void)
677 /* Switch the index registers to 16 bit mode (assembler only) */
679 if (GetCPU() != CPU_65816) {
680 Error (ERR_816_MODE_ONLY);
682 /* Immidiate mode has one extension byte */
683 ExtBytes [AMI_IMM_INDEX] = 1;
689 static void DoImport (void)
690 /* Import a symbol */
692 ExportImport (SymImport, 0);
697 static void DoImportZP (void)
698 /* Import a zero page symbol */
700 ExportImport (SymImport, 1);
705 static void DoIncBin (void)
706 /* Include a binary file */
708 /* Name must follow */
709 if (Tok != TOK_STRCON) {
710 ErrorSkip (ERR_STRCON_EXPECTED);
712 /* Try to open the file */
713 FILE* F = fopen (SVal, "rb");
715 Error (ERR_CANNOT_OPEN_INCLUDE, SVal, strerror (errno));
717 unsigned char Buf [1024];
719 /* Read chunks and insert them into the output */
720 while ((Count = fread (Buf, 1, sizeof (Buf), F)) > 0) {
721 EmitData (Buf, Count);
723 /* Close the file, ignore errors since it's r/o */
733 static void DoInclude (void)
734 /* Include another file */
736 char Name [MAX_STR_LEN+1];
738 /* Name must follow */
739 if (Tok != TOK_STRCON) {
740 ErrorSkip (ERR_STRCON_EXPECTED);
750 static void DoInvalid (void)
751 /* Handle a token that is invalid here, since it should have been handled on
752 * a much lower level of the expression hierarchy. Getting this sort of token
753 * means that the lower level code has bugs.
754 * This function differs to DoUnexpected in that the latter may be triggered
755 * by the user by using keywords in the wrong location. DoUnexpected is not
756 * an error in the assembler itself, while DoInvalid is.
759 Internal ("Unexpected token: %s", Keyword);
764 static void DoLineCont (void)
765 /* Switch the use of line continuations */
767 SetBoolOption (&LineCont);
772 static void DoList (void)
773 /* Enable/disable the listing */
775 /* Get the setting */
777 SetBoolOption (&List);
779 /* Manage the counter */
789 static void DoListBytes (void)
790 /* Set maximum number of bytes to list for one line */
792 SetListBytes (IntArg (MIN_LIST_BYTES, MAX_LIST_BYTES));
797 static void DoLocalChar (void)
798 /* Define the character that starts local labels */
800 if (Tok != TOK_CHARCON) {
801 ErrorSkip (ERR_CHARCON_EXPECTED);
803 if (IVal != '@' && IVal != '?') {
804 Error (ERR_ILLEGAL_LOCALSTART);
806 LocalStart = (char) IVal;
814 static void DoMacPack (void)
815 /* Insert a macro package */
817 /* Macro package names */
818 static const char* Keys [] = {
825 /* We expect an identifier */
826 if (Tok != TOK_IDENT) {
827 ErrorSkip (ERR_IDENT_EXPECTED);
831 /* Map the keyword to a number */
832 Package = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
835 ErrorSkip (ERR_ILLEGAL_MACPACK);
839 /* Skip the package name */
842 /* Insert the package */
843 InsertMacPack (Package);
848 static void DoMacro (void)
849 /* Start a macro definition */
851 MacDef (MAC_STYLE_CLASSIC);
856 static void DoNull (void)
857 /* Switch to the NULL segment */
864 static void DoOrg (void)
865 /* Start absolute code */
867 long PC = ConstExpression ();
868 if (PC < 0 || PC > 0xFFFF) {
877 static void DoOut (void)
878 /* Output a string */
880 if (Tok != TOK_STRCON) {
881 ErrorSkip (ERR_STRCON_EXPECTED);
883 /* Output the string and be sure to flush the output to keep it in
884 * sync with any error messages if the output is redirected to a file.
886 printf ("%s\n", SVal);
894 static void DoP02 (void)
895 /* Switch to 6502 CPU */
902 static void DoPC02 (void)
903 /* Switch to 65C02 CPU */
910 static void DoP816 (void)
911 /* Switch to 65816 CPU */
918 static void DoPageLength (void)
919 /* Set the page length for the listing */
921 PageLength = IntArg (MIN_PAGE_LEN, MAX_PAGE_LEN);
926 static void DoProc (void)
927 /* Start a new lexical scope */
929 if (Tok == TOK_IDENT) {
930 /* The new scope has a name */
931 SymDef (SVal, CurrentPC (), IsZPSeg ());
939 static void DoReloc (void)
940 /* Enter relocatable mode */
947 static void DoRepeat (void)
948 /* Repeat some instruction block */
955 static void DoRes (void)
956 /* Reserve some number of storage bytes */
961 Count = ConstExpression ();
962 if (Count > 0xFFFF || Count < 0) {
963 ErrorSkip (ERR_RANGE);
966 if (Tok == TOK_COMMA) {
968 Val = ConstExpression ();
969 /* We need a byte value here */
970 if (!IsByteRange (Val)) {
971 ErrorSkip (ERR_RANGE);
975 /* Emit constant values */
977 Emit0 ((unsigned char) Val);
981 /* Emit fill fragments */
988 static void DoROData (void)
989 /* Switch to the r/o data segment */
996 static void DoSegment (void)
997 /* Switch to another segment */
999 static const char* AttrTab [] = {
1000 "ZEROPAGE", "DIRECT",
1004 char Name [sizeof (SVal)];
1007 if (Tok != TOK_STRCON) {
1008 ErrorSkip (ERR_STRCON_EXPECTED);
1011 /* Save the name of the segment and skip it */
1012 strcpy (Name, SVal);
1015 /* Check for an optional segment attribute */
1016 SegType = SEGTYPE_DEFAULT;
1017 if (Tok == TOK_COMMA) {
1019 if (Tok != TOK_IDENT) {
1020 ErrorSkip (ERR_IDENT_EXPECTED);
1022 int Attr = GetSubKey (AttrTab, sizeof (AttrTab) / sizeof (AttrTab [0]));
1028 SegType = SEGTYPE_ZP;
1033 SegType = SEGTYPE_ABS;
1039 SegType = SEGTYPE_FAR;
1043 Error (ERR_ILLEGAL_SEG_ATTR);
1049 /* Set the segment */
1050 UseSeg (Name, SegType);
1056 static void DoSmart (void)
1057 /* Smart mode on/off */
1059 SetBoolOption (&SmartMode);
1064 static void DoSunPlus (void)
1065 /* Switch to the SUNPLUS CPU */
1067 SetCPU (CPU_SUNPLUS);
1072 static void DoUnexpected (void)
1073 /* Got an unexpected keyword */
1075 Error (ERR_UNEXPECTED, Keyword);
1081 static void DoWarning (void)
1084 if (Tok != TOK_STRCON) {
1085 ErrorSkip (ERR_STRCON_EXPECTED);
1087 Warning (WARN_USER, SVal);
1094 static void DoWord (void)
1098 EmitWord (Expression ());
1099 if (Tok != TOK_COMMA) {
1109 static void DoZeropage (void)
1110 /* Switch to the zeropage segment */
1117 /*****************************************************************************/
1119 /*****************************************************************************/
1123 /* Control commands flags */
1125 ccNone = 0x0000, /* No special flags */
1126 ccKeepToken = 0x0001 /* Do not skip the current token */
1129 /* Control command table */
1131 unsigned Flags; /* Flags for this directive */
1132 void (*Handler) (void); /* Command handler */
1134 typedef struct CtrlDesc_ CtrlDesc;
1136 #define PSEUDO_COUNT (sizeof (CtrlCmdTab) / sizeof (CtrlCmdTab [0]))
1137 static CtrlDesc CtrlCmdTab [] = {
1140 { ccNone, DoAddr }, /* .ADDR */
1141 { ccNone, DoAlign },
1142 { ccNone, DoASCIIZ },
1143 { ccNone, DoAutoImport },
1144 { ccNone, DoUnexpected }, /* .BLANK */
1149 { ccNone, DoUnexpected, }, /* .CONCAT */
1150 { ccNone, DoUnexpected }, /* .CONST */
1151 { ccNone, DoUnexpected }, /* .CPU */
1155 { ccNone, DoDebugInfo },
1156 { ccNone, DoDefine },
1157 { ccNone, DoUnexpected }, /* .DEFINED */
1158 { ccNone, DoDWord },
1159 { ccKeepToken, DoConditionals }, /* .ELSE */
1160 { ccKeepToken, DoConditionals }, /* .ELSEIF */
1162 { ccKeepToken, DoConditionals }, /* .ENDIF */
1163 { ccNone, DoUnexpected }, /* .ENDMACRO */
1164 { ccNone, DoEndProc },
1165 { ccNone, DoUnexpected }, /* .ENDREPEAT */
1166 { ccNone, DoError },
1167 { ccNone, DoExitMacro },
1168 { ccNone, DoExport },
1169 { ccNone, DoExportZP },
1170 { ccNone, DoFarAddr },
1171 { ccNone, DoFeature },
1172 { ccNone, DoFileOpt },
1173 { ccNone, DoUnexpected }, /* .FORCEWORD */
1174 { ccNone, DoGlobal },
1175 { ccNone, DoGlobalZP },
1178 { ccKeepToken, DoConditionals }, /* .IF */
1179 { ccKeepToken, DoConditionals }, /* .IFBLANK */
1180 { ccKeepToken, DoConditionals }, /* .IFCONST */
1181 { ccKeepToken, DoConditionals }, /* .IFDEF */
1182 { ccKeepToken, DoConditionals }, /* .IFNBLANK */
1183 { ccKeepToken, DoConditionals }, /* .IFNCONST */
1184 { ccKeepToken, DoConditionals }, /* .IFNDEF */
1185 { ccKeepToken, DoConditionals }, /* .IFNREF */
1186 { ccKeepToken, DoConditionals }, /* .IFP02 */
1187 { ccKeepToken, DoConditionals }, /* .IFP816 */
1188 { ccKeepToken, DoConditionals }, /* .IFPC02 */
1189 { ccKeepToken, DoConditionals }, /* .IFREF */
1190 { ccNone, DoImport },
1191 { ccNone, DoImportZP },
1192 { ccNone, DoIncBin },
1193 { ccNone, DoInclude },
1194 { ccNone, DoInvalid }, /* .LEFT */
1195 { ccNone, DoLineCont },
1197 { ccNone, DoListBytes },
1198 { ccNone, DoUnexpected }, /* .LOCAL */
1199 { ccNone, DoLocalChar },
1200 { ccNone, DoMacPack },
1201 { ccNone, DoMacro },
1202 { ccNone, DoUnexpected }, /* .MATCH */
1203 { ccNone, DoInvalid }, /* .MID */
1209 { ccNone, DoPageLength },
1210 { ccNone, DoUnexpected }, /* .PARAMCOUNT */
1213 { ccNone, DoUnexpected }, /* .REFERENCED */
1214 { ccNone, DoReloc },
1215 { ccNone, DoRepeat },
1217 { ccNone, DoInvalid }, /* .RIGHT */
1218 { ccNone, DoROData },
1219 { ccNone, DoSegment },
1220 { ccNone, DoSmart },
1221 { ccNone, DoUnexpected }, /* .STRAT */
1222 { ccNone, DoUnexpected }, /* .STRING */
1223 { ccNone, DoUnexpected }, /* .STRLEN */
1224 { ccNone, DoSunPlus },
1225 { ccNone, DoUnexpected }, /* .TCOUNT */
1226 { ccNone, DoWarning },
1228 { ccNone, DoUnexpected }, /* .XMATCH */
1229 { ccNone, DoZeropage },
1234 /*****************************************************************************/
1236 /*****************************************************************************/
1240 int TokIsPseudo (unsigned Tok)
1241 /* Return true if the given token is a pseudo instruction token */
1243 return (Tok >= TOK_FIRSTPSEUDO && Tok <= TOK_LASTPSEUDO);
1248 void HandlePseudo (void)
1249 /* Handle a pseudo instruction */
1253 /* Calculate the index into the table */
1254 unsigned Index = Tok - TOK_FIRSTPSEUDO;
1257 if (PSEUDO_COUNT != (TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1)) {
1258 Internal ("Pseudo mismatch: PSEUDO_COUNT = %u, actual count = %u\n",
1259 PSEUDO_COUNT, TOK_LASTPSEUDO - TOK_FIRSTPSEUDO + 1);
1261 CHECK (Index < PSEUDO_COUNT);
1263 /* Get the pseudo intruction descriptor */
1264 D = &CtrlCmdTab [Index];
1266 /* Remember the instruction, then skip it if needed */
1267 if ((D->Flags & ccKeepToken) == 0) {
1268 strcpy (Keyword+1, SVal);
1272 /* Call the handler */