1 /*****************************************************************************/
5 /* Code segment entry */
9 /* (C) 2001 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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
62 static char EmptyArg[] = "";
66 /*****************************************************************************/
67 /* Helper functions */
68 /*****************************************************************************/
72 static void FreeArg (char* Arg)
73 /* Free a code entry argument */
75 if (Arg != EmptyArg) {
82 static char* GetArgCopy (const char* Arg)
83 /* Create an argument copy for assignment */
85 if (Arg && Arg[0] != '\0') {
89 /* Use the empty argument string */
96 static int NumArg (const char* Arg, unsigned long* Num)
97 /* If the given argument is numerical, convert it and return true. Otherwise
98 * set Num to zero and return false.
104 /* Determine the base */
109 } else if (*Arg == '%') {
114 /* Convert the value. strtol is not exactly what we want here, but it's
115 * cheap and may be replaced by something fancier later.
117 Val = strtoul (Arg, &End, Base);
119 /* Check if the conversion was successful */
122 /* Could not convert */
137 static void SetUseChgInfo (CodeEntry* E, const OPCDesc* D)
138 /* Set the Use and Chg in E */
142 /* If this is a subroutine call, or a jump to an external function,
143 * lookup the information about this function and use it. The jump itself
144 * does not change any registers, so we don't need to use the data from D.
146 if ((E->Info & (OF_BRA | OF_CALL)) != 0 && E->JumpTo == 0) {
147 /* A subroutine call or jump to external symbol (function exit) */
148 GetFuncInfo (E->Arg, &E->Use, &E->Chg);
150 /* Some other instruction. Use the values from the opcode description
151 * plus addressing mode info.
153 E->Use = D->Use | GetAMUseInfo (E->AM);
156 /* Check for special zero page registers used */
161 /* Be conservative: */
165 Info = GetZPInfo (E->Arg);
166 if (Info && Info->ByteUse != REG_NONE) {
167 if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
168 E->OPC == OP65_INC || E->OPC == OP65_LSR ||
169 E->OPC == OP65_ROL || E->OPC == OP65_ROR ||
170 E->OPC == OP65_TRB || E->OPC == OP65_TSB) {
171 /* The zp loc is both, input and output */
172 E->Chg |= Info->ByteUse;
173 E->Use |= Info->ByteUse;
174 } else if ((E->Info & OF_STORE) != 0) {
176 E->Chg |= Info->ByteUse;
179 E->Use |= Info->ByteUse;
187 Info = GetZPInfo (E->Arg);
188 if (Info && Info->ByteUse != REG_NONE) {
189 /* These addressing modes will never change the zp loc */
190 E->Use |= Info->WordUse;
195 /* Keep gcc silent */
203 /*****************************************************************************/
205 /*****************************************************************************/
209 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
210 CodeLabel* JumpTo, LineInfo* LI)
211 /* Create a new code entry, initialize and return it */
213 /* Get the opcode description */
214 const OPCDesc* D = GetOPCDesc (OPC);
216 /* Allocate memory */
217 CodeEntry* E = xmalloc (sizeof (CodeEntry));
219 /* Initialize the fields */
222 E->Arg = GetArgCopy (Arg);
223 E->Flags = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
225 E->Size = GetInsnSize (E->OPC, E->AM);
227 E->LI = UseLineInfo (LI);
229 SetUseChgInfo (E, D);
230 InitCollection (&E->Labels);
232 /* If we have a label given, add this entry to the label */
234 CollAppend (&JumpTo->JumpFrom, E);
237 /* Return the initialized struct */
243 void FreeCodeEntry (CodeEntry* E)
244 /* Free the given code entry */
246 /* Free the string argument if we have one */
249 /* Cleanup the collection */
250 DoneCollection (&E->Labels);
252 /* Release the line info */
253 ReleaseLineInfo (E->LI);
255 /* Delete the register info */
264 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
265 /* Replace the opcode of the instruction. This will also replace related info,
266 * Size, Use and Chg, but it will NOT update any arguments or labels.
269 /* Get the opcode descriptor */
270 const OPCDesc* D = GetOPCDesc (OPC);
272 /* Replace the opcode */
275 E->Size = GetInsnSize (E->OPC, E->AM);
276 SetUseChgInfo (E, D);
281 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
282 /* Check if both code entries are equal */
284 return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
289 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
290 /* Attach the label to the entry */
292 /* Add it to the entries label list */
293 CollAppend (&E->Labels, L);
295 /* Tell the label about it's owner */
301 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
302 /* Move the code label L from it's former owner to the code entry E. */
304 /* Delete the label from the owner */
305 CollDeleteItem (&L->Owner->Labels, L);
307 /* Set the new owner */
308 CollAppend (&E->Labels, L);
314 void CE_SetNumArg (CodeEntry* E, long Num)
315 /* Set a new numeric argument for the given code entry that must already
316 * have a numeric argument.
321 /* Check that the entry has a numerical argument */
322 CHECK (E->Flags & CEF_NUMARG);
324 /* Make the new argument string */
327 xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
328 } else if (E->Size == 3) {
330 xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
332 Internal ("Invalid instruction size in CE_SetNumArg");
335 /* Free the old argument */
338 /* Assign the new one */
339 E->Arg = GetArgCopy (Buf);
341 /* Use the new numerical value */
347 int CE_KnownImm (const CodeEntry* E)
348 /* Return true if the argument of E is a known immediate value */
350 return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
355 void CE_FreeRegInfo (CodeEntry* E)
356 /* Free an existing register info struct */
366 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
367 /* Generate register info for this instruction. If an old info exists, it is
371 /* Pointers to the register contents */
375 /* Function register usage */
376 unsigned short Use, Chg;
378 /* If we don't have a register info struct, allocate one. */
380 E->RI = NewRegInfo (InputRegs);
383 E->RI->In = *InputRegs;
385 RC_Invalidate (&E->RI->In);
387 E->RI->Out2 = E->RI->Out = E->RI->In;
390 /* Get pointers to the register contents */
394 /* Handle the different instructions */
398 /* We don't know the value of the carry, so the result is
406 if (CE_KnownImm (E)) {
407 Out->RegA = In->RegA & (short) E->Num;
415 if (E->AM == AM65_ACC && In->RegA >= 0) {
416 Out->RegA = (In->RegA << 1) & 0xFF;
417 } else if (E->AM == AM65_ZP) {
418 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
419 Out->SRegLo = (In->SRegLo << 1) & 0xFF;
420 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
421 Out->SRegHi = (In->SRegHi << 1) & 0xFF;
482 Out->RegA = (In->RegA - 1) & 0xFF;
487 if (E->AM == AM65_ACC && In->RegA >= 0) {
488 Out->RegA = (In->RegA - 1) & 0xFF;
489 } else if (E->AM == AM65_ZP) {
490 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
491 Out->SRegLo = (In->SRegLo - 1) & 0xFF;
492 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
493 Out->SRegHi = (In->SRegHi - 1) & 0xFF;
500 Out->RegX = (In->RegX - 1) & 0xFF;
506 Out->RegY = (In->RegY - 1) & 0xFF;
512 if (CE_KnownImm (E)) {
513 Out->RegA = In->RegA ^ (short) E->Num;
522 Out->RegA = (In->RegA + 1) & 0xFF;
527 if (E->AM == AM65_ACC && In->RegA >= 0) {
528 Out->RegA = (In->RegA + 1) & 0xFF;
529 } else if (E->AM == AM65_ZP) {
530 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
531 Out->SRegLo = (In->SRegLo + 1) & 0xFF;
532 } else if ((E->Chg & REG_SREG_HI) != 0 && In->SRegHi >= 0) {
533 Out->SRegHi = (In->SRegHi + 1) & 0xFF;
540 Out->RegX = (In->RegX + 1) & 0xFF;
546 Out->RegY = (In->RegY + 1) & 0xFF;
572 /* Get the code info for the function */
573 GetFuncInfo (E->Arg, &Use, &Chg);
583 if (Chg & REG_SREG_LO) {
586 if (Chg & REG_SREG_HI) {
598 if (CE_KnownImm (E)) {
599 Out->RegA = (unsigned char) E->Num;
600 } else if (E->AM == AM65_ZP) {
601 if (E->Use & REG_SREG_LO) {
602 Out->RegA = In->SRegLo;
603 } else if (E->Use & REG_SREG_HI) {
604 Out->RegA = In->SRegHi;
609 /* A is now unknown */
615 if (CE_KnownImm (E)) {
616 Out->RegX = (unsigned char) E->Num;
617 } else if (E->AM == AM65_ZP) {
618 if (E->Use & REG_SREG_LO) {
619 Out->RegX = In->SRegLo;
620 } else if (E->Use & REG_SREG_HI) {
621 Out->RegX = In->SRegHi;
626 /* X is now unknown */
632 if (CE_KnownImm (E)) {
633 Out->RegY = (unsigned char) E->Num;
634 } else if (E->AM == AM65_ZP) {
635 if (E->Use & REG_SREG_LO) {
636 Out->RegY = In->SRegLo;
637 } else if (E->Use & REG_SREG_HI) {
638 Out->RegY = In->SRegHi;
643 /* Y is now unknown */
649 if (E->AM == AM65_ACC && In->RegA >= 0) {
650 Out->RegA = (In->RegA >> 1) & 0xFF;
651 } else if (E->AM == AM65_ZP) {
652 if ((E->Chg & REG_SREG_LO) != 0 && In->SRegLo >= 0) {
653 Out->SRegLo = (In->SRegLo >> 1) & 0xFF;
654 } else if (E->Chg & REG_SREG_HI) {
655 Out->SRegHi = (In->SRegHi >> 1) & 0xFF;
665 if (CE_KnownImm (E)) {
666 Out->RegA = In->RegA | (short) E->Num;
668 /* A is now unknown */
702 if (E->AM == AM65_ACC) {
704 } else if (E->AM == AM65_ZP) {
705 if (E->Chg & REG_SREG_LO) {
707 } else if (E->Chg & REG_SREG_HI) {
714 if (E->AM == AM65_ACC) {
716 } else if (E->AM == AM65_ZP) {
717 if (E->Chg & REG_SREG_LO) {
719 } else if (E->Chg & REG_SREG_HI) {
732 /* We don't know the value of the carry bit */
746 if (E->AM == AM65_ZP) {
747 if (E->Chg & REG_SREG_LO) {
748 Out->SRegLo = In->RegA;
749 } else if (E->Chg & REG_SREG_HI) {
750 Out->SRegHi = In->RegA;
756 if (E->AM == AM65_ZP) {
757 if (E->Chg & REG_SREG_LO) {
758 Out->SRegLo = In->RegX;
759 } else if (E->Chg & REG_SREG_HI) {
760 Out->SRegHi = In->RegX;
766 if (E->AM == AM65_ZP) {
767 if (E->Chg & REG_SREG_LO) {
768 Out->SRegLo = In->RegY;
769 } else if (E->Chg & REG_SREG_HI) {
770 Out->SRegHi = In->RegY;
776 Out->RegX = In->RegA;
780 Out->RegY = In->RegA;
798 Out->RegA = In->RegX;
805 Out->RegA = In->RegY;
816 static char* RegInfoDesc (unsigned U, char* Buf)
817 /* Return a string containing register info */
821 strcat (Buf, U & REG_SREG_HI? "H" : "_");
822 strcat (Buf, U & REG_SREG_LO? "L" : "_");
823 strcat (Buf, U & REG_A? "A" : "_");
824 strcat (Buf, U & REG_X? "X" : "_");
825 strcat (Buf, U & REG_Y? "Y" : "_");
826 strcat (Buf, U & REG_TMP1? "T1" : "__");
827 strcat (Buf, U & REG_PTR1? "1" : "_");
828 strcat (Buf, U & REG_PTR2? "2" : "_");
829 strcat (Buf, U & REG_SAVE? "V" : "_");
836 void CE_Output (const CodeEntry* E, FILE* F)
837 /* Output the code entry to a file */
843 /* If we have a label, print that */
844 unsigned LabelCount = CollCount (&E->Labels);
846 for (I = 0; I < LabelCount; ++I) {
847 CL_Output (CollConstAt (&E->Labels, I), F);
850 /* Get the opcode description */
851 D = GetOPCDesc (E->OPC);
853 /* Print the mnemonic */
854 Chars = fprintf (F, "\t%s", D->Mnemo);
856 /* Print the operand */
866 Chars += fprintf (F, "%*sa", 9-Chars, "");
872 Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
878 /* zeropage and absolute */
879 Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
884 /* zeropage,X and absolute,X */
885 Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
890 Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
895 Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
900 Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
905 Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
910 Target = E->JumpTo? E->JumpTo->Name : E->Arg;
911 Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
915 Internal ("Invalid addressing mode");
919 /* Print usage info if requested by the debugging flag */
924 "%*s; USE: %-20s CHG: %-20s SIZE: %u\n",
926 RegInfoDesc (E->Use, Use),
927 RegInfoDesc (E->Chg, Chg),
930 /* Terminate the line */