+#include <string.h>
+
/* common */
#include "check.h"
#include "xmalloc.h"
+/* Empty argument */
+static char EmptyArg[] = "";
+
+
+
+/*****************************************************************************/
+/* Helper functions */
+/*****************************************************************************/
+
+
+
+static void FreeArg (char* Arg)
+/* Free a code entry argument */
+{
+ if (Arg != EmptyArg) {
+ xfree (Arg);
+ }
+}
+
+
+
+static char* GetArgCopy (const char* Arg)
+/* Create an argument copy for assignment */
+{
+ if (Arg && Arg[0] != '\0') {
+ /* Create a copy */
+ return xstrdup (Arg);
+ } else {
+ /* Use the empty argument string */
+ return EmptyArg;
+ }
+}
+
+
+
/*****************************************************************************/
/* Code */
/*****************************************************************************/
E->AM = AM;
E->Size = GetInsnSize (E->OPC, E->AM);
E->Hints = 0;
- E->Arg = (Arg && Arg[0] != '\0')? xstrdup (Arg) : 0;
+ E->Arg = GetArgCopy (Arg);
E->Num = 0;
E->Flags = 0;
E->Info = D->Info;
E->Use = D->Use;
E->Chg = D->Chg;
- if (E->OPC == OPC_JSR && E->Arg) {
- /* A subroutine call */
+ if (E->OPC == OPC_JSR) {
+ /* A subroutine call */
GetFuncInfo (E->Arg, &E->Use, &E->Chg);
} else {
- /* Some other instruction */
+ /* Some other instruction */
E->Use |= GetAMUseInfo (AM);
}
E->JumpTo = JumpTo;
/* If we have a label given, add this entry to the label */
if (JumpTo) {
- CollAppend (&JumpTo->JumpFrom, E);
+ CollAppend (&JumpTo->JumpFrom, E);
}
/* Return the initialized struct */
/* Free the given code entry */
{
/* Free the string argument if we have one */
- xfree (E->Arg);
+ FreeArg (E->Arg);
/* Cleanup the collection */
DoneCollection (&E->Labels);
-int CodeEntryHasLabel (const CodeEntry* E)
-/* Check if the given code entry has labels attached */
+int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
+/* Check if both code entries are equal */
{
- return (CollCount (&E->Labels) > 0);
+ return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
}
-int CodeEntryHasMark (const CodeEntry* E)
-/* Return true if the given code entry has the CEF_USERMARK flag set */
+void AttachCodeLabel (CodeEntry* E, CodeLabel* L)
+/* Attach the label to the entry */
{
- return (E->Flags & CEF_USERMARK) != 0;
+ /* Mark the label as defined */
+ L->Flags |= LF_DEF;
+
+ /* Add it to the entries label list */
+ CollAppend (&E->Labels, L);
+
+ /* Tell the label about it's owner */
+ L->Owner = E;
}
-void CodeEntrySetMark (CodeEntry* E)
-/* Set the CEF_USERMARK flag for the given entry */
+int CodeEntryHasLabel (const CodeEntry* E)
+/* Check if the given code entry has labels attached */
{
- E->Flags |= CEF_USERMARK;
+ return (CollCount (&E->Labels) > 0);
}
-void CodeEntryResetMark (CodeEntry* E)
-/* Reset the CEF_USERMARK flag for the given entry */
+unsigned GetCodeLabelCount (const CodeEntry* E)
+/* Get the number of labels attached to this entry */
{
- E->Flags &= ~CEF_USERMARK;
+ return CollCount (&E->Labels);
}
+int CodeEntryHasMark (const CodeEntry* E)
+/* Return true if the given code entry has the CEF_USERMARK flag set */
+{
+ return (E->Flags & CEF_USERMARK) != 0;
+}
+
+
+
+void CodeEntrySetMark (CodeEntry* E)
+/* Set the CEF_USERMARK flag for the given entry */
+{
+ E->Flags |= CEF_USERMARK;
+}
+
+
+
+void CodeEntryResetMark (CodeEntry* E)
+/* Reset the CEF_USERMARK flag for the given entry */
+{
+ E->Flags &= ~CEF_USERMARK;
+}
+
+
+
+void CodeEntrySetArg (CodeEntry* E, const char* Arg)
+/* Set a new argument for the given code entry. An old string is deleted. */
+{
+ /* Free the old argument */
+ FreeArg (E->Arg);
+
+ /* Assign the new one */
+ E->Arg = GetArgCopy (Arg);
+}
+
+
+
void OutputCodeEntry (const CodeEntry* E, FILE* F)
/* Output the code entry to a file */
{
const OPCDesc* D;
unsigned Chars;
+ const char* Target;
/* If we have a label, print that */
unsigned LabelCount = CollCount (&E->Labels);
case AM_BRA:
/* branch */
- CHECK (E->JumpTo != 0);
- Chars += fprintf (F, "%*s%s", 9-Chars, "", E->JumpTo->Name);
+ Target = E->JumpTo? E->JumpTo->Name : E->Arg;
+ Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
break;
default: