X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Fcodeseg.c;h=492a322b969438dac42b5304b59dac928f207168;hb=ed2767e65f05b853eda1b071938062126de6f517;hp=567b2e96ba9690595dcc4962ce855a36c11a1de0;hpb=5d0a52b7accc43672275d6331c576c2512b1074b;p=cc65 diff --git a/src/cc65/codeseg.c b/src/cc65/codeseg.c index 567b2e96b..492a322b9 100644 --- a/src/cc65/codeseg.c +++ b/src/cc65/codeseg.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@cc65.org */ +/* (C) 2001-2009, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -39,20 +39,24 @@ /* common */ #include "chartype.h" #include "check.h" +#include "debugflag.h" #include "global.h" #include "hashstr.h" +#include "strbuf.h" #include "strutil.h" #include "xmalloc.h" -#include "xsprintf.h" /* cc65 */ #include "asmlabel.h" #include "codeent.h" #include "codeinfo.h" +#include "codeseg.h" #include "datatype.h" #include "error.h" +#include "global.h" +#include "ident.h" +#include "output.h" #include "symentry.h" -#include "codeseg.h" @@ -62,6 +66,25 @@ +static void CS_PrintFunctionHeader (const CodeSeg* S) +/* Print a comment with the function signature to the output file */ +{ + /* Get the associated function */ + const SymEntry* Func = S->Func; + + /* If this is a global code segment, do nothing */ + if (Func) { + WriteOutput ("; ---------------------------------------------------------------\n" + "; "); + PrintFuncSig (OutputFile, Func->Name, Func->Type); + WriteOutput ("\n" + "; ---------------------------------------------------------------\n" + "\n"); + } +} + + + static void CS_MoveLabelsToEntry (CodeSeg* S, CodeEntry* E) /* Move all labels from the label pool to the given entry and remove them * from the pool. @@ -178,7 +201,7 @@ static const char* SkipSpace (const char* S) static const char* ReadToken (const char* L, const char* Term, - char* Buf, unsigned BufSize) + char* Buf, unsigned BufSize) /* Read the next token into Buf, return the updated line pointer. The * token is terminated by one of the characters given in term. */ @@ -188,8 +211,14 @@ static const char* ReadToken (const char* L, const char* Term, unsigned ParenCount = 0; while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) { if (I < BufSize-1) { - Buf[I++] = *L; + Buf[I] = *L; + } else if (I == BufSize-1) { + /* Cannot store this character, this is an input error (maybe + * identifier too long or similar). + */ + Error ("ASM code error: syntax error"); } + ++I; if (*L == ')') { --ParenCount; } else if (*L == '(') { @@ -215,16 +244,35 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L) * white space, for example. */ { - char Mnemo[16]; - const OPCDesc* OPC; - am_t AM = 0; /* Initialize to keep gcc silent */ - char Arg[64]; - char Reg; + char Mnemo[IDENTSIZE+10]; + const OPCDesc* OPC; + am_t AM = 0; /* Initialize to keep gcc silent */ + char Arg[IDENTSIZE+10]; + char Reg; CodeEntry* E; - CodeLabel* Label; + CodeLabel* Label; + + /* Read the first token and skip white space after it */ + L = SkipSpace (ReadToken (L, " \t:", Mnemo, sizeof (Mnemo))); + + /* Check if we have a label */ + if (*L == ':') { - /* Mnemonic */ - L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo)); + /* Skip the colon and following white space */ + L = SkipSpace (L+1); + + /* Add the label */ + CS_AddLabel (S, Mnemo); + + /* If we have reached end of line, bail out, otherwise a mnemonic + * may follow. + */ + if (*L == '\0') { + return 0; + } + + L = SkipSpace (ReadToken (L, " \t", Mnemo, sizeof (Mnemo))); + } /* Try to find the opcode description for the mnemonic */ OPC = FindOP65 (Mnemo); @@ -235,16 +283,17 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L) return 0; } - /* Skip separator white space */ - L = SkipSpace (L); - /* Get the addressing mode */ Arg[0] = '\0'; switch (*L) { case '\0': - /* Implicit */ - AM = AM65_IMP; + /* Implicit or accu */ + if (OPC->Info & OF_NOIMP) { + AM = AM65_ACC; + } else { + AM = AM65_IMP; + } break; case '#': @@ -288,13 +337,13 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L) if (*L == ',') { L = SkipSpace (L+1); if (toupper (*L) != 'Y') { - Error ("ASM code error: `Y' expected"); - return 0; + Error ("ASM code error: `Y' expected"); + return 0; } L = SkipSpace (L+1); if (*L != '\0') { - Error ("ASM code error: syntax error"); - return 0; + Error ("ASM code error: syntax error"); + return 0; } AM = AM65_ZP_INDY; } else if (*L == '\0') { @@ -326,6 +375,12 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L) } else if (GetZPInfo(Arg) != 0) { AM = AM65_ZP; } else { + /* Check for subroutine call to local label */ + if ((OPC->Info & OF_CALL) && IsLocalLabelName (Arg)) { + Error ("ASM code error: " + "Cannot use local label `%s' in subroutine call", + Arg); + } AM = AM65_ABS; } } else if (*L == ',') { @@ -338,20 +393,20 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L) Reg = toupper (*L); L = SkipSpace (L+1); if (Reg == 'X') { - if (GetZPInfo(Arg) != 0) { - AM = AM65_ZPX; - } else { - AM = AM65_ABSX; - } + if (GetZPInfo(Arg) != 0) { + AM = AM65_ZPX; + } else { + AM = AM65_ABSX; + } } else if (Reg == 'Y') { - AM = AM65_ABSY; + AM = AM65_ABSY; } else { - Error ("ASM code error: syntax error"); - return 0; + Error ("ASM code error: syntax error"); + return 0; } if (*L != '\0') { - Error ("ASM code error: syntax error"); - return 0; + Error ("ASM code error: syntax error"); + return 0; } } } @@ -360,10 +415,12 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L) } /* If the instruction is a branch, check for the label and generate it - * if it does not exist. Ignore anything but local labels here. + * if it does not exist. This may lead to unused labels (if the label + * is actually an external one) which are removed by the CS_MergeLabels + * function later. */ Label = 0; - if (AM == AM65_BRA && Arg[0] == 'L') { + if (AM == AM65_BRA) { /* Generate the hash over the label, then search for the label */ unsigned Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE; @@ -397,7 +454,7 @@ CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func) /* Create a new code segment, initialize and return it */ { unsigned I; - const type* RetType; + const Type* RetType; /* Allocate memory */ CodeSeg* S = xmalloc (sizeof (CodeSeg)); @@ -424,6 +481,10 @@ CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func) S->ExitRegs = REG_NONE; } + /* Copy the global optimization settings */ + S->Optimize = (unsigned char) IS_Get (&Optimize); + S->CodeSizeFactor = (unsigned) IS_Get (&CodeSizeFactor); + /* Return the new struct */ return S; } @@ -447,17 +508,17 @@ void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap) { const char* L; CodeEntry* E; - char Token[64]; + char Token[IDENTSIZE+10]; /* Format the line */ - char Buf [256]; - xvsprintf (Buf, sizeof (Buf), Format, ap); + StrBuf Buf = STATIC_STRBUF_INITIALIZER; + SB_VPrintf (&Buf, Format, ap); /* Skip whitespace */ - L = SkipSpace (Buf); + L = SkipSpace (SB_GetConstBuf (&Buf)); /* Check which type of instruction we have */ - E = 0; /* Assume no insn created */ + E = 0; /* Assume no insn created */ switch (*L) { case '\0': @@ -483,6 +544,9 @@ void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap) if (E) { CS_AddEntry (S, E); } + + /* Cleanup the string buffer */ + SB_Done (&Buf); } @@ -513,6 +577,8 @@ void CS_DelEntry (CodeSeg* S, unsigned Index) /* Delete an entry from the code segment. This includes moving any associated * labels, removing references to labels and even removing the referenced labels * if the reference count drops to zero. + * Note: Labels are moved forward if possible, that is, they are moved to the + * next insn (not the preceeding one). */ { /* Get the code entry for the given index */ @@ -666,6 +732,36 @@ unsigned CS_GetEntryIndex (CodeSeg* S, struct CodeEntry* E) +int CS_RangeHasLabel (CodeSeg* S, unsigned Start, unsigned Count) +/* Return true if any of the code entries in the given range has a label + * attached. If the code segment does not span the given range, check the + * possible span instead. + */ +{ + unsigned EntryCount = CS_GetEntryCount(S); + + /* Adjust count. We expect at least Start to be valid. */ + CHECK (Start < EntryCount); + if (Start + Count > EntryCount) { + Count = EntryCount - Start; + } + + /* Check each entry. Since we have validated the index above, we may + * use the unchecked access function in the loop which is faster. + */ + while (Count--) { + const CodeEntry* E = CollAtUnchecked (&S->Entries, Start++); + if (CE_HasLabel (E)) { + return 1; + } + } + + /* No label in the complete range */ + return 0; +} + + + CodeLabel* CS_AddLabel (CodeSeg* S, const char* Name) /* Add a code label for the next instruction to follow */ { @@ -678,7 +774,10 @@ CodeLabel* CS_AddLabel (CodeSeg* S, const char* Name) /* Did we find it? */ if (L) { /* We found it - be sure it does not already have an owner */ - CHECK (L->Owner == 0); + if (L->Owner) { + Error ("ASM label `%s' is already defined", Name); + return L; + } } else { /* Not found - create a new one */ L = CS_NewCodeLabel (S, Name, Hash); @@ -686,7 +785,8 @@ CodeLabel* CS_AddLabel (CodeSeg* S, const char* Name) /* Safety. This call is quite costly, but safety is better */ if (CollIndex (&S->Labels, L) >= 0) { - Internal ("AddCodeLabel: Label `%s' already defined", Name); + Error ("ASM label `%s' is already defined", Name); + return L; } /* We do now have a valid label. Remember it for later */ @@ -746,7 +846,7 @@ void CS_DelLabel (CodeSeg* S, CodeLabel* L) /* Get the insn referencing this label */ CodeEntry* E = CollAt (&L->JumpFrom, I); /* Remove the reference */ - E->JumpTo = 0; + CE_ClearJumpTo (E); } CollDeleteAll (&L->JumpFrom); @@ -771,6 +871,47 @@ void CS_MergeLabels (CodeSeg* S) */ { unsigned I; + unsigned J; + + /* First, remove all labels from the label symbol table that don't have an + * owner (this means that they are actually external labels but we didn't + * know that previously since they may have also been forward references). + */ + for (I = 0; I < CS_LABEL_HASH_SIZE; ++I) { + + /* Get the first label in this hash chain */ + CodeLabel** L = &S->LabelHash[I]; + while (*L) { + if ((*L)->Owner == 0) { + + /* The label does not have an owner, remove it from the chain */ + CodeLabel* X = *L; + *L = X->Next; + + /* Cleanup any entries jumping to this label */ + for (J = 0; J < CL_GetRefCount (X); ++J) { + /* Get the entry referencing this label */ + CodeEntry* E = CL_GetRef (X, J); + /* And remove the reference. Do NOT call CE_ClearJumpTo + * here, because this will also clear the label name, + * which is not what we want. + */ + E->JumpTo = 0; + } + + /* Print some debugging output */ + if (Debug) { + printf ("Removing unused global label `%s'", X->Name); + } + + /* And free the label */ + FreeCodeLabel (X); + } else { + /* Label is owned, point to next code label pointer */ + L = &((*L)->Next); + } + } + } /* Walk over all code entries */ for (I = 0; I < CS_GetEntryCount (S); ++I) { @@ -778,39 +919,39 @@ void CS_MergeLabels (CodeSeg* S) CodeLabel* RefLab; unsigned J; - /* Get a pointer to the next entry */ - CodeEntry* E = CS_GetEntry (S, I); + /* Get a pointer to the next entry */ + CodeEntry* E = CS_GetEntry (S, I); /* If this entry has zero labels, continue with the next one */ - unsigned LabelCount = CE_GetLabelCount (E); - if (LabelCount == 0) { - continue; - } + unsigned LabelCount = CE_GetLabelCount (E); + if (LabelCount == 0) { + continue; + } - /* We have at least one label. Use the first one as reference label. */ - RefLab = CE_GetLabel (E, 0); + /* We have at least one label. Use the first one as reference label. */ + RefLab = CE_GetLabel (E, 0); - /* Walk through the remaining labels and change references to these - * labels to a reference to the one and only label. Delete the labels - * that are no longer used. To increase performance, walk backwards - * through the list. - */ + /* Walk through the remaining labels and change references to these + * labels to a reference to the one and only label. Delete the labels + * that are no longer used. To increase performance, walk backwards + * through the list. + */ for (J = LabelCount-1; J >= 1; --J) { - /* Get the next label */ - CodeLabel* L = CE_GetLabel (E, J); + /* Get the next label */ + CodeLabel* L = CE_GetLabel (E, J); - /* Move all references from this label to the reference label */ + /* Move all references from this label to the reference label */ CL_MoveRefs (L, RefLab); /* Remove the label completely. */ CS_DelLabel (S, L); } - /* The reference label is the only remaining label. Check if there - * are any references to this label, and delete it if this is not - * the case. - */ + /* The reference label is the only remaining label. Check if there + * are any references to this label, and delete it if this is not + * the case. + */ if (CollCount (&RefLab->JumpFrom) == 0) { /* Delete the label */ CS_DelLabel (S, RefLab); @@ -878,7 +1019,7 @@ void CS_RemoveLabelRef (CodeSeg* S, struct CodeEntry* E) CollDeleteItem (&L->JumpFrom, E); /* The entry jumps no longer to L */ - E->JumpTo = 0; + CE_ClearJumpTo (E); /* If there are no more references, delete the label */ if (CollCount (&L->JumpFrom) == 0) { @@ -909,6 +1050,86 @@ void CS_MoveLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L) +void CS_DelCodeRange (CodeSeg* S, unsigned First, unsigned Last) +/* Delete all entries between first and last, both inclusive. The function + * can only handle basic blocks (First is the only entry, Last the only exit) + * and no open labels. It will call FAIL if any of these preconditions are + * violated. + */ +{ + unsigned I; + CodeEntry* FirstEntry; + + /* Do some sanity checks */ + CHECK (First <= Last && Last < CS_GetEntryCount (S)); + + /* If Last is actually the last insn, call CS_DelCodeAfter instead, which + * is more flexible in this case. + */ + if (Last == CS_GetEntryCount (S) - 1) { + CS_DelCodeAfter (S, First); + return; + } + + /* Get the first entry and check if it has any labels. If it has, move + * them to the insn following Last. If Last is the last insn of the code + * segment, make them ownerless and move them to the label pool. + */ + FirstEntry = CS_GetEntry (S, First); + if (CE_HasLabel (FirstEntry)) { + /* Get the entry following last */ + CodeEntry* FollowingEntry = CS_GetNextEntry (S, Last); + if (FollowingEntry) { + /* There is an entry after Last - move the labels */ + CS_MoveLabels (S, FirstEntry, FollowingEntry); + } else { + /* Move the labels to the pool and clear the owner pointer */ + CS_MoveLabelsToPool (S, FirstEntry); + } + } + + /* First pass: Delete all references to labels. If the reference count + * for a label drops to zero, delete it. + */ + for (I = Last; I >= First; --I) { + + /* Get the next entry */ + CodeEntry* E = CS_GetEntry (S, I); + + /* Check if this entry has a label reference */ + if (E->JumpTo) { + + /* If the label is a label in the label pool, this is an error */ + CodeLabel* L = E->JumpTo; + CHECK (CollIndex (&S->Labels, L) < 0); + + /* Remove the reference to the label */ + CS_RemoveLabelRef (S, E); + } + } + + /* Second pass: Delete the instructions. If a label attached to an + * instruction still has references, it must be references from outside + * the deleted area, which is an error. + */ + for (I = Last; I >= First; --I) { + + /* Get the next entry */ + CodeEntry* E = CS_GetEntry (S, I); + + /* Check if this entry has a label attached */ + CHECK (!CE_HasLabel (E)); + + /* Delete the pointer to the entry */ + CollDelete (&S->Entries, I); + + /* Delete the entry itself */ + FreeCodeEntry (E); + } +} + + + void CS_DelCodeAfter (CodeSeg* S, unsigned Last) /* Delete all entries including the given one */ { @@ -969,8 +1190,170 @@ void CS_DelCodeAfter (CodeSeg* S, unsigned Last) -void CS_Output (const CodeSeg* S, FILE* F) -/* Output the code segment data to a file */ +void CS_ResetMarks (CodeSeg* S, unsigned First, unsigned Last) +/* Remove all user marks from the entries in the given range */ +{ + while (First <= Last) { + CE_ResetMark (CS_GetEntry (S, First++)); + } +} + + + +int CS_IsBasicBlock (CodeSeg* S, unsigned First, unsigned Last) +/* Check if the given code segment range is a basic block. That is, check if + * First is the only entrance and Last is the only exit. This means that no + * jump/branch inside the block may jump to an insn below First or after(!) + * Last, and that no insn may jump into this block from the outside. + */ +{ + unsigned I; + + /* Don't accept invalid ranges */ + CHECK (First <= Last); + + /* First pass: Walk over the range and remove all marks from the entries */ + CS_ResetMarks (S, First, Last); + + /* Second pass: Walk over the range checking all labels. Note: There may be + * label on the first insn which is ok. + */ + I = First + 1; + while (I <= Last) { + + /* Get the next entry */ + CodeEntry* E = CS_GetEntry (S, I); + + /* Check if this entry has one or more labels, if so, check which + * entries jump to this label. + */ + unsigned LabelCount = CE_GetLabelCount (E); + unsigned LabelIndex; + for (LabelIndex = 0; LabelIndex < LabelCount; ++LabelIndex) { + + /* Get this label */ + CodeLabel* L = CE_GetLabel (E, LabelIndex); + + /* Walk over all entries that jump to this label. Check for each + * of the entries if it is out of the range. + */ + unsigned RefCount = CL_GetRefCount (L); + unsigned RefIndex; + for (RefIndex = 0; RefIndex < RefCount; ++RefIndex) { + + /* Get the code entry that jumps here */ + CodeEntry* Ref = CL_GetRef (L, RefIndex); + + /* Walk over out complete range and check if we find the + * refering entry. This is cheaper than using CS_GetEntryIndex, + * because CS_GetEntryIndex will search the complete code + * segment and not just our range. + */ + unsigned J; + for (J = First; J <= Last; ++J) { + if (Ref == CS_GetEntry (S, J)) { + break; + } + } + if (J > Last) { + /* We did not find the entry. This means that the jump to + * out code segment entry E came from outside the range, + * which in turn means that the given range is not a basic + * block. + */ + CS_ResetMarks (S, First, Last); + return 0; + } + + /* If we come here, we found the entry. Mark it, so we know + * that the branch to the label is in range. + */ + CE_SetMark (Ref); + } + } + + /* Next entry */ + ++I; + } + + /* Third pass: Walk again over the range and check all branches. If we + * find a branch that is not marked, its target is not inside the range + * (since we checked all the labels in the range before). + */ + I = First; + while (I <= Last) { + + /* Get the next entry */ + CodeEntry* E = CS_GetEntry (S, I); + + /* Check if this is a branch and if so, if it has a mark */ + if (E->Info & (OF_UBRA | OF_CBRA)) { + if (!CE_HasMark (E)) { + /* No mark means not a basic block. Before bailing out, be sure + * to remove the marks from the remaining entries. + */ + CS_ResetMarks (S, I+1, Last); + return 0; + } + + /* Remove the mark */ + CE_ResetMark (E); + } + + /* Next entry */ + ++I; + } + + /* Done - this is a basic block */ + return 1; +} + + + +void CS_OutputPrologue (const CodeSeg* S) +/* If the given code segment is a code segment for a function, output the + * assembler prologue into the file. That is: Output a comment header, switch + * to the correct segment and enter the local function scope. If the code + * segment is global, do nothing. + */ +{ + /* Get the function associated with the code segment */ + SymEntry* Func = S->Func; + + /* If the code segment is associated with a function, print a function + * header and enter a local scope. Be sure to switch to the correct + * segment before outputing the function label. + */ + if (Func) { + /* Get the function descriptor */ + CS_PrintFunctionHeader (S); + WriteOutput (".segment\t\"%s\"\n\n.proc\t_%s", S->SegName, Func->Name); + if (IsQualNear (Func->Type)) { + WriteOutput (": near"); + } else if (IsQualFar (Func->Type)) { + WriteOutput (": far"); + } + WriteOutput ("\n\n"); + } + +} + + + +void CS_OutputEpilogue (const CodeSeg* S) +/* If the given code segment is a code segment for a function, output the + * assembler epilogue into the file. That is: Close the local function scope. + */ +{ + if (S->Func) { + WriteOutput ("\n.endproc\n\n"); + } +} + + + +void CS_Output (CodeSeg* S) +/* Output the code segment data to the output file */ { unsigned I; const LineInfo* LI; @@ -983,51 +1366,63 @@ void CS_Output (const CodeSeg* S, FILE* F) return; } - /* Output the segment directive */ - fprintf (F, ".segment\t\"%s\"\n\n", S->SegName); + /* Generate register info */ + CS_GenRegInfo (S); - /* If this is a segment for a function, enter a function */ - if (S->Func) { - fprintf (F, ".proc\t_%s\n\n", S->Func->Name); - } + /* Output the segment directive */ + WriteOutput (".segment\t\"%s\"\n\n", S->SegName); /* Output all entries, prepended by the line information if it has changed */ LI = 0; for (I = 0; I < Count; ++I) { - /* Get the next entry */ - const CodeEntry* E = CollConstAt (&S->Entries, I); - /* Check if the line info has changed. If so, output the source line - * if the option is enabled and output debug line info if the debug - * option is enabled. - */ - if (E->LI != LI) { - /* Line info has changed, remember the new line info */ - LI = E->LI; - - /* Add the source line as a comment */ - if (AddSource) { - fprintf (F, ";\n; %s\n;\n", LI->Line); - } - - /* Add line debug info */ + /* Get the next entry */ + const CodeEntry* E = CollConstAt (&S->Entries, I); + /* Check if the line info has changed. If so, output the source line + * if the option is enabled and output debug line info if the debug + * option is enabled. + */ + if (E->LI != LI) { + /* Line info has changed, remember the new line info */ + LI = E->LI; + + /* Add the source line as a comment. Beware: When line continuation + * was used, the line may contain newlines. + */ + if (AddSource) { + const char* L = LI->Line; + WriteOutput (";\n; "); + while (*L) { + const char* N = strchr (L, '\n'); + if (N) { + /* We have a newline, just write the first part */ + WriteOutput ("%.*s\n; ", (int) (N - L), L); + L = N+1; + } else { + /* No Newline, write as is */ + WriteOutput ("%s\n", L); + break; + } + } + WriteOutput (";\n"); + } + + /* Add line debug info */ if (DebugInfo) { - fprintf (F, "\t.dbg\tline, \"%s\", %u\n", - GetInputName (LI), GetInputLine (LI)); + WriteOutput ("\t.dbg\tline, \"%s\", %u\n", + GetInputName (LI), GetInputLine (LI)); } } /* Output the code */ - CE_Output (E, F); + CE_Output (E); } /* If debug info is enabled, terminate the last line number information */ if (DebugInfo) { - fprintf (F, "\t.dbg\tline\n"); + WriteOutput ("\t.dbg\tline\n"); } - /* If this is a segment for a function, leave the function */ - if (S->Func) { - fprintf (F, "\n.endproc\n\n"); - } + /* Free register info */ + CS_FreeRegInfo (S); } @@ -1047,220 +1442,238 @@ void CS_GenRegInfo (CodeSeg* S) /* Generate register infos for all instructions */ { unsigned I; - RegContents Regs; - RegContents* CurrentRegs; - int WasJump; + RegContents Regs; /* Initial register contents */ + RegContents* CurrentRegs; /* Current register contents */ + int WasJump; /* True if last insn was a jump */ + int Done; /* All runs done flag */ /* Be sure to delete all register infos */ CS_FreeRegInfo (S); - /* On entry, the register contents are unknown */ - RC_Invalidate (&Regs); - CurrentRegs = &Regs; + /* We may need two runs to get back references right */ + do { - /* First pass. Walk over all insns and note just the changes from one - * insn to the next one. - */ - WasJump = 0; - for (I = 0; I < CS_GetEntryCount (S); ++I) { + /* Assume we're done after this run */ + Done = 1; - CodeEntry* P; + /* On entry, the register contents are unknown */ + RC_Invalidate (&Regs); + CurrentRegs = &Regs; - /* Get the next instruction */ - CodeEntry* E = CollAtUnchecked (&S->Entries, I); - - /* If the instruction has a label, we need some special handling */ - unsigned LabelCount = CE_GetLabelCount (E); - if (LabelCount > 0) { - - /* Loop over all entry points that jump here. If these entry - * points already have register info, check if all values are - * known and identical. If all values are identical, and the - * preceeding instruction was not an unconditional branch, check - * if the register value on exit of the preceeding instruction - * is also identical. If all these values are identical, the - * value of a register is known, otherwise it is unknown. - */ - CodeLabel* Label = CE_GetLabel (E, 0); - unsigned Entry; - if (WasJump) { - /* Preceeding insn was an unconditional branch */ - CodeEntry* J = CL_GetRef(Label, 0); - if (J->RI) { - Regs = J->RI->Out2; + /* Walk over all insns and note just the changes from one insn to the + * next one. + */ + WasJump = 0; + for (I = 0; I < CS_GetEntryCount (S); ++I) { + + CodeEntry* P; + + /* Get the next instruction */ + CodeEntry* E = CollAtUnchecked (&S->Entries, I); + + /* If the instruction has a label, we need some special handling */ + unsigned LabelCount = CE_GetLabelCount (E); + if (LabelCount > 0) { + + /* Loop over all entry points that jump here. If these entry + * points already have register info, check if all values are + * known and identical. If all values are identical, and the + * preceeding instruction was not an unconditional branch, check + * if the register value on exit of the preceeding instruction + * is also identical. If all these values are identical, the + * value of a register is known, otherwise it is unknown. + */ + CodeLabel* Label = CE_GetLabel (E, 0); + unsigned Entry; + if (WasJump) { + /* Preceeding insn was an unconditional branch */ + CodeEntry* J = CL_GetRef(Label, 0); + if (J->RI) { + Regs = J->RI->Out2; + } else { + RC_Invalidate (&Regs); + } + Entry = 1; } else { - RC_Invalidate (&Regs); + Regs = *CurrentRegs; + Entry = 0; } - Entry = 1; - } else { - Regs = *CurrentRegs; - Entry = 0; - } - while (Entry < CL_GetRefCount (Label)) { - /* Get this entry */ - CodeEntry* J = CL_GetRef (Label, Entry); - if (J->RI == 0) { - /* No register info for this entry, bail out */ - RC_Invalidate (&Regs); - break; - } - if (J->RI->Out2.RegA != Regs.RegA) { - Regs.RegA = -1; - } - if (J->RI->Out2.RegX != Regs.RegX) { - Regs.RegX = -1; - } - if (J->RI->Out2.RegY != Regs.RegY) { - Regs.RegY = -1; - } - if (J->RI->Out2.SRegLo != Regs.SRegLo) { - Regs.SRegLo = -1; - } - if (J->RI->Out2.SRegHi != Regs.SRegHi) { - Regs.SRegHi = -1; + while (Entry < CL_GetRefCount (Label)) { + /* Get this entry */ + CodeEntry* J = CL_GetRef (Label, Entry); + if (J->RI == 0) { + /* No register info for this entry. This means that the + * instruction that jumps here is at higher addresses and + * the jump is a backward jump. We need a second run to + * get the register info right in this case. Until then, + * assume unknown register contents. + */ + Done = 0; + RC_Invalidate (&Regs); + break; + } + if (J->RI->Out2.RegA != Regs.RegA) { + Regs.RegA = UNKNOWN_REGVAL; + } + if (J->RI->Out2.RegX != Regs.RegX) { + Regs.RegX = UNKNOWN_REGVAL; + } + if (J->RI->Out2.RegY != Regs.RegY) { + Regs.RegY = UNKNOWN_REGVAL; + } + if (J->RI->Out2.SRegLo != Regs.SRegLo) { + Regs.SRegLo = UNKNOWN_REGVAL; + } + if (J->RI->Out2.SRegHi != Regs.SRegHi) { + Regs.SRegHi = UNKNOWN_REGVAL; + } + if (J->RI->Out2.Tmp1 != Regs.Tmp1) { + Regs.Tmp1 = UNKNOWN_REGVAL; + } + ++Entry; } - ++Entry; - } - /* Use this register info */ - CurrentRegs = &Regs; + /* Use this register info */ + CurrentRegs = &Regs; - } + } - /* Generate register info for this instruction */ - CE_GenRegInfo (E, CurrentRegs); + /* Generate register info for this instruction */ + CE_GenRegInfo (E, CurrentRegs); - /* Remember for the next insn if this insn was an uncondition branch */ - WasJump = (E->Info & OF_UBRA) != 0; + /* Remember for the next insn if this insn was an uncondition branch */ + WasJump = (E->Info & OF_UBRA) != 0; - /* Output registers for this insn are input for the next */ - CurrentRegs = &E->RI->Out; + /* Output registers for this insn are input for the next */ + CurrentRegs = &E->RI->Out; - /* If this insn is a branch on zero flag, we may have more info on - * register contents for one of both flow directions, but only if - * there is a previous instruction. - */ - if ((E->Info & OF_ZBRA) != 0 && (P = CS_GetPrevEntry (S, I)) != 0) { - - /* Get the branch condition */ - bc_t BC = GetBranchCond (E->OPC); - - /* Check the previous instruction */ - switch (P->OPC) { - - case OP65_ADC: - case OP65_AND: - case OP65_DEA: - case OP65_EOR: - case OP65_INA: - case OP65_LDA: - case OP65_ORA: - case OP65_PLA: - case OP65_SBC: - /* A is zero in one execution flow direction */ - if (BC == BC_EQ) { - E->RI->Out2.RegA = 0; - } else { - E->RI->Out.RegA = 0; - } - break; - - case OP65_CMP: - /* If this is an immidiate compare, the A register has - * the value of the compare later. - */ - if (CE_KnownImm (P)) { - if (BC == BC_EQ) { - E->RI->Out2.RegA = (unsigned char)P->Num; - } else { - E->RI->Out.RegA = (unsigned char)P->Num; - } - } - break; - - case OP65_CPX: - /* If this is an immidiate compare, the X register has - * the value of the compare later. - */ - if (CE_KnownImm (P)) { - if (BC == BC_EQ) { - E->RI->Out2.RegX = (unsigned char)P->Num; - } else { - E->RI->Out.RegX = (unsigned char)P->Num; - } - } - break; - - case OP65_CPY: - /* If this is an immidiate compare, the Y register has - * the value of the compare later. - */ - if (CE_KnownImm (P)) { - if (BC == BC_EQ) { - E->RI->Out2.RegY = (unsigned char)P->Num; - } else { - E->RI->Out.RegY = (unsigned char)P->Num; - } - } - break; - - case OP65_DEX: - case OP65_INX: - case OP65_LDX: - case OP65_PLX: - /* X is zero in one execution flow direction */ - if (BC == BC_EQ) { - E->RI->Out2.RegX = 0; - } else { - E->RI->Out.RegX = 0; - } - break; - - case OP65_DEY: - case OP65_INY: - case OP65_LDY: - case OP65_PLY: - /* X is zero in one execution flow direction */ - if (BC == BC_EQ) { - E->RI->Out2.RegY = 0; - } else { - E->RI->Out.RegY = 0; - } - break; - - case OP65_TAX: - case OP65_TXA: - /* If the branch is a beq, both A and X are zero at the - * branch target, otherwise they are zero at the next - * insn. - */ - if (BC == BC_EQ) { - E->RI->Out2.RegA = E->RI->Out2.RegX = 0; - } else { - E->RI->Out.RegA = E->RI->Out.RegX = 0; - } - break; - - case OP65_TAY: - case OP65_TYA: - /* If the branch is a beq, both A and Y are zero at the - * branch target, otherwise they are zero at the next - * insn. - */ - if (BC == BC_EQ) { - E->RI->Out2.RegA = E->RI->Out2.RegY = 0; - } else { - E->RI->Out.RegA = E->RI->Out.RegY = 0; - } - break; + /* If this insn is a branch on zero flag, we may have more info on + * register contents for one of both flow directions, but only if + * there is a previous instruction. + */ + if ((E->Info & OF_ZBRA) != 0 && (P = CS_GetPrevEntry (S, I)) != 0) { + + /* Get the branch condition */ + bc_t BC = GetBranchCond (E->OPC); + + /* Check the previous instruction */ + switch (P->OPC) { + + case OP65_ADC: + case OP65_AND: + case OP65_DEA: + case OP65_EOR: + case OP65_INA: + case OP65_LDA: + case OP65_ORA: + case OP65_PLA: + case OP65_SBC: + /* A is zero in one execution flow direction */ + if (BC == BC_EQ) { + E->RI->Out2.RegA = 0; + } else { + E->RI->Out.RegA = 0; + } + break; + + case OP65_CMP: + /* If this is an immidiate compare, the A register has + * the value of the compare later. + */ + if (CE_IsConstImm (P)) { + if (BC == BC_EQ) { + E->RI->Out2.RegA = (unsigned char)P->Num; + } else { + E->RI->Out.RegA = (unsigned char)P->Num; + } + } + break; + + case OP65_CPX: + /* If this is an immidiate compare, the X register has + * the value of the compare later. + */ + if (CE_IsConstImm (P)) { + if (BC == BC_EQ) { + E->RI->Out2.RegX = (unsigned char)P->Num; + } else { + E->RI->Out.RegX = (unsigned char)P->Num; + } + } + break; + + case OP65_CPY: + /* If this is an immidiate compare, the Y register has + * the value of the compare later. + */ + if (CE_IsConstImm (P)) { + if (BC == BC_EQ) { + E->RI->Out2.RegY = (unsigned char)P->Num; + } else { + E->RI->Out.RegY = (unsigned char)P->Num; + } + } + break; + + case OP65_DEX: + case OP65_INX: + case OP65_LDX: + case OP65_PLX: + /* X is zero in one execution flow direction */ + if (BC == BC_EQ) { + E->RI->Out2.RegX = 0; + } else { + E->RI->Out.RegX = 0; + } + break; + + case OP65_DEY: + case OP65_INY: + case OP65_LDY: + case OP65_PLY: + /* X is zero in one execution flow direction */ + if (BC == BC_EQ) { + E->RI->Out2.RegY = 0; + } else { + E->RI->Out.RegY = 0; + } + break; + + case OP65_TAX: + case OP65_TXA: + /* If the branch is a beq, both A and X are zero at the + * branch target, otherwise they are zero at the next + * insn. + */ + if (BC == BC_EQ) { + E->RI->Out2.RegA = E->RI->Out2.RegX = 0; + } else { + E->RI->Out.RegA = E->RI->Out.RegX = 0; + } + break; + + case OP65_TAY: + case OP65_TYA: + /* If the branch is a beq, both A and Y are zero at the + * branch target, otherwise they are zero at the next + * insn. + */ + if (BC == BC_EQ) { + E->RI->Out2.RegA = E->RI->Out2.RegY = 0; + } else { + E->RI->Out.RegA = E->RI->Out.RegY = 0; + } + break; - default: - break; + default: + break; + } } } - } + } while (!Done); + }