/* */
/* */
/* */
-/* (C) 2001-2003 Ullrich von Bassewitz */
-/* Römerstrasse 52 */
-/* D-70794 Filderstadt */
-/* EMail: uz@cc65.org */
+/* (C) 2001-2005, Ullrich von Bassewitz */
+/* Römerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
* lookup the information about this function and use it. The jump itself
* does not change any registers, so we don't need to use the data from D.
*/
- if ((E->Info & (OF_BRA | OF_CALL)) != 0 && E->JumpTo == 0) {
+ if ((E->Info & (OF_UBRA | OF_CALL)) != 0 && E->JumpTo == 0) {
/* A subroutine call or jump to external symbol (function exit) */
GetFuncInfo (E->Arg, &E->Use, &E->Chg);
} else {
int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
/* Check if both code entries are equal */
{
- return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
+ return (E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0);
}
+void CE_ClearJumpTo (CodeEntry* E)
+/* Clear the JumpTo entry and the argument (which contained the name of the
+ * label). Note: The function will not clear the backpointer from the label,
+ * so use it with care.
+ */
+{
+ /* Clear the JumpTo entry */
+ E->JumpTo = 0;
+
+ /* Clear the argument and assign the empty one */
+ FreeArg (E->Arg);
+ E->Arg = EmptyArg;
+}
+
+
+
void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
/* Move the code label L from it's former owner to the code entry E. */
{
+void CE_SetArg (CodeEntry* E, const char* Arg)
+/* Replace the argument by the new one. */
+{
+ /* Free the old argument */
+ FreeArg (E->Arg);
+
+ /* Assign the new one */
+ E->Arg = GetArgCopy (Arg);
+}
+
+
+
void CE_SetNumArg (CodeEntry* E, long Num)
/* Set a new numeric argument for the given code entry that must already
* have a numeric argument.
Internal ("Invalid instruction size in CE_SetNumArg");
}
- /* Free the old argument */
- FreeArg (E->Arg);
-
- /* Assign the new one */
- E->Arg = GetArgCopy (Buf);
+ /* Replace the argument by the new one */
+ CE_SetArg (E, Buf);
/* Use the new numerical value */
E->Num = Num;
-int CE_KnownImm (const CodeEntry* E)
-/* Return true if the argument of E is a known immediate value */
+int CE_IsConstImm (const CodeEntry* E)
+/* Return true if the argument of E is a constant immediate value */
{
return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
}
+int CE_IsKnownImm (const CodeEntry* E, unsigned long Num)
+/* Return true if the argument of E is a constant immediate value that is
+ * equal to Num.
+ */
+{
+ return E->AM == AM65_IMM &&
+ (E->Flags & CEF_NUMARG) != 0 &&
+ E->Num == Num;
+}
+
+
+
int CE_UseLoadFlags (const CodeEntry* E)
/* Return true if the instruction uses any flags that are set by a load of
* a register (N and Z).
*/
{
+ /* Follow unconditional branches, but beware of endless loops. After this,
+ * E will point to the first entry that is not a branch.
+ */
+ if (E->Info & OF_UBRA) {
+ Collection C = AUTO_COLLECTION_INITIALIZER;
+
+ /* Follow the chain */
+ while (E->Info & OF_UBRA) {
+
+ /* Remember the entry so we can detect loops */
+ CollAppend (&C, (void*) E);
+
+ /* Check the target */
+ if (E->JumpTo == 0 || CollIndex (&C, E->JumpTo->Owner) >= 0) {
+ /* Unconditional jump to external symbol, or endless loop. */
+ DoneCollection (&C);
+ return 0; /* Flags not used */
+ }
+
+ /* Follow the chain */
+ E = E->JumpTo->Owner;
+ }
+
+ /* Delete the collection */
+ DoneCollection (&C);
+ }
+
/* A branch will use the flags */
if (E->Info & OF_FBRA) {
return 1;
/* Free an existing register info struct */
{
if (E->RI) {
- FreeRegInfo (E->RI);
+ FreeRegInfo (E->RI);
E->RI = 0;
}
}
case OP65_AND:
if (RegValIsKnown (In->RegA)) {
- if (CE_KnownImm (E)) {
+ if (CE_IsConstImm (E)) {
Out->RegA = In->RegA & (short) E->Num;
} else if (E->AM == AM65_ZP) {
switch (GetKnownReg (E->Use & REG_ZP, In)) {
case OP65_EOR:
if (RegValIsKnown (In->RegA)) {
- if (CE_KnownImm (E)) {
+ if (CE_IsConstImm (E)) {
Out->RegA = In->RegA ^ (short) E->Num;
} else if (E->AM == AM65_ZP) {
switch (GetKnownReg (E->Use & REG_ZP, In)) {
break;
case OP65_LDA:
- if (CE_KnownImm (E)) {
+ if (CE_IsConstImm (E)) {
Out->RegA = (unsigned char) E->Num;
} else if (E->AM == AM65_ZP) {
switch (GetKnownReg (E->Use & REG_ZP, In)) {
break;
case OP65_LDX:
- if (CE_KnownImm (E)) {
+ if (CE_IsConstImm (E)) {
Out->RegX = (unsigned char) E->Num;
} else if (E->AM == AM65_ZP) {
switch (GetKnownReg (E->Use & REG_ZP, In)) {
break;
case OP65_LDY:
- if (CE_KnownImm (E)) {
+ if (CE_IsConstImm (E)) {
Out->RegY = (unsigned char) E->Num;
} else if (E->AM == AM65_ZP) {
switch (GetKnownReg (E->Use & REG_ZP, In)) {
case OP65_ORA:
if (RegValIsKnown (In->RegA)) {
- if (CE_KnownImm (E)) {
+ if (CE_IsConstImm (E)) {
Out->RegA = In->RegA | (short) E->Num;
} else if (E->AM == AM65_ZP) {
switch (GetKnownReg (E->Use & REG_ZP, In)) {
+static char* RegContentDesc (const RegContents* RC, char* Buf)
+/* Return a string containing register contents */
+{
+ char* B = Buf;
+
+ if (RegValIsUnknown (RC->RegA)) {
+ strcpy (B, "A:XX ");
+ } else {
+ sprintf (B, "A:%02X ", RC->RegA);
+ }
+ B += 5;
+ if (RegValIsUnknown (RC->RegX)) {
+ strcpy (B, "X:XX ");
+ } else {
+ sprintf (B, "X:%02X ", RC->RegX);
+ }
+ B += 5;
+ if (RegValIsUnknown (RC->RegY)) {
+ strcpy (B, "Y:XX");
+ } else {
+ sprintf (B, "Y:%02X", RC->RegY);
+ }
+ B += 4;
+
+ return Buf;
+}
+
+
+
void CE_Output (const CodeEntry* E, FILE* F)
/* Output the code entry to a file */
{
/* Print the operand */
switch (E->AM) {
- case AM_IMP:
case AM65_IMP:
/* implicit */
break;
Chars += fprintf (F, "%*sa", 9-Chars, "");
break;
- case AM_IMM:
case AM65_IMM:
/* immidiate */
Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
break;
- case AM_ABS:
case AM65_ZP:
case AM65_ABS:
/* zeropage and absolute */
char Use [128];
char Chg [128];
fprintf (F,
- "%*s; USE: %-12s CHG: %-12s SIZE: %u\n",
+ "%*s; USE: %-12s CHG: %-12s SIZE: %u",
30-Chars, "",
- RegInfoDesc (E->Use, Use),
- RegInfoDesc (E->Chg, Chg),
+ RegInfoDesc (E->Use, Use),
+ RegInfoDesc (E->Chg, Chg),
E->Size);
- } else {
- /* Terminate the line */
- fprintf (F, "\n");
+
+ if (E->RI) {
+ char RegIn[32];
+ char RegOut[32];
+ fprintf (F,
+ " In %s Out %s",
+ RegContentDesc (&E->RI->In, RegIn),
+ RegContentDesc (&E->RI->Out, RegOut));
+ }
}
+
+ /* Terminate the line */
+ fprintf (F, "\n");
}