X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Fcodeseg.c;h=fede7ae6215b12c694a0afd8668e9a77a752aefa;hb=f7dfcbcc3daf8426770842b5e6ed3634e0d50c82;hp=bbc21337a4170e0328a1d9317c96e9db108ffb43;hpb=2435aa63b5dcab10267aca7cf0d6f236d2d5b37c;p=cc65 diff --git a/src/cc65/codeseg.c b/src/cc65/codeseg.c index bbc21337a..fede7ae62 100644 --- a/src/cc65/codeseg.c +++ b/src/cc65/codeseg.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ +/* (C) 2001-2002 Ullrich von Bassewitz */ /* Wacholderweg 14 */ /* D-70597 Stuttgart */ /* EMail: uz@cc65.org */ @@ -39,6 +39,7 @@ /* common */ #include "chartype.h" #include "check.h" +#include "debugflag.h" #include "global.h" #include "hashstr.h" #include "strutil.h" @@ -51,6 +52,7 @@ #include "codeinfo.h" #include "datatype.h" #include "error.h" +#include "ident.h" #include "symentry.h" #include "codeseg.h" @@ -62,6 +64,27 @@ +static void CS_PrintFunctionHeader (const CodeSeg* S, FILE* F) +/* Print a comment with the function signature to the given file */ +{ + /* Get the associated function */ + const SymEntry* Func = S->Func; + + /* If this is a global code segment, do nothing */ + if (Func) { + fprintf (F, + "; ---------------------------------------------------------------\n" + "; "); + PrintFuncSig (F, Func->Name, Func->Type); + fprintf (F, + "\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. @@ -161,7 +184,7 @@ static void CS_RemoveLabelFromHash (CodeSeg* S, CodeLabel* L) static CodeLabel* CS_AddLabelInternal (CodeSeg* S, const char* Name, - void (*ErrorFunc) (const char*, ...)) + void (*ErrorFunc) (const char*, ...)) /* Add a code label for the next instruction to follow */ { /* Calculate the hash from the name */ @@ -256,13 +279,13 @@ static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L) * white space, for example. */ { - char Mnemo[64]; - const OPCDesc* OPC; - am_t AM = 0; /* Initialize to keep gcc silent */ - char Arg[64]; + 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))); @@ -506,7 +529,7 @@ 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]; @@ -516,7 +539,7 @@ void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap) L = SkipSpace (Buf); /* Check which type of instruction we have */ - E = 0; /* Assume no insn created */ + E = 0; /* Assume no insn created */ switch (*L) { case '\0': @@ -572,6 +595,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 */ @@ -725,6 +750,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 */ { @@ -806,6 +861,7 @@ 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 @@ -817,12 +873,25 @@ void CS_MergeLabels (CodeSeg* S) CodeLabel** L = &S->LabelHash[I]; while (*L) { if ((*L)->Owner == 0) { - /* The label does not have an owner, remove it from the chain */ + + /* 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 */ + 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 */ @@ -1028,6 +1097,161 @@ void CS_DelCodeAfter (CodeSeg* S, unsigned Last) +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, FILE* F) +/* 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) { + CS_PrintFunctionHeader (S, F); + fprintf (F, ".segment\t\"%s\"\n\n.proc\t_%s\n\n", S->SegName, Func->Name); + } + +} + + + +void CS_OutputEpilogue (const CodeSeg* S, FILE* F) +/* 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) { + fprintf (F, "\n.endproc\n\n"); + } +} + + + void CS_Output (const CodeSeg* S, FILE* F) /* Output the code segment data to a file */ { @@ -1045,11 +1269,6 @@ void CS_Output (const CodeSeg* S, FILE* F) /* Output the segment directive */ fprintf (F, ".segment\t\"%s\"\n\n", S->SegName); - /* 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 all entries, prepended by the line information if it has changed */ LI = 0; for (I = 0; I < Count; ++I) { @@ -1063,9 +1282,21 @@ void CS_Output (const CodeSeg* S, FILE* F) /* Line info has changed, remember the new line info */ LI = E->LI; - /* Add the source line as a comment */ + /* Add the source line as a comment. Beware: When line continuation + * was used, the line may contain newlines. + */ if (AddSource) { - fprintf (F, ";\n; %s\n;\n", LI->Line); + const char* L = LI->Line; + fputs (";\n; ", F); + while (*L) { + if (*L == '\n') { + fputs ("\n; ", F); + } else { + fputc (*L, F); + } + ++L; + } + fputs ("\n;\n", F); } /* Add line debug info */ @@ -1082,11 +1313,6 @@ void CS_Output (const CodeSeg* S, FILE* F) if (DebugInfo) { fprintf (F, "\t.dbg\tline\n"); } - - /* If this is a segment for a function, leave the function */ - if (S->Func) { - fprintf (F, "\n.endproc\n\n"); - } } @@ -1119,7 +1345,7 @@ void CS_GenRegInfo (CodeSeg* S) /* Assume we're done after this run */ Done = 1; - + /* On entry, the register contents are unknown */ RC_Invalidate (&Regs); CurrentRegs = &Regs; @@ -1178,49 +1404,52 @@ void CS_GenRegInfo (CodeSeg* S) break; } if (J->RI->Out2.RegA != Regs.RegA) { - Regs.RegA = -1; + Regs.RegA = UNKNOWN_REGVAL; } if (J->RI->Out2.RegX != Regs.RegX) { - Regs.RegX = -1; + Regs.RegX = UNKNOWN_REGVAL; } if (J->RI->Out2.RegY != Regs.RegY) { - Regs.RegY = -1; + Regs.RegY = UNKNOWN_REGVAL; } if (J->RI->Out2.SRegLo != Regs.SRegLo) { - Regs.SRegLo = -1; + Regs.SRegLo = UNKNOWN_REGVAL; } if (J->RI->Out2.SRegHi != Regs.SRegHi) { - Regs.SRegHi = -1; + Regs.SRegHi = UNKNOWN_REGVAL; + } + if (J->RI->Out2.Tmp1 != Regs.Tmp1) { + Regs.Tmp1 = UNKNOWN_REGVAL; } ++Entry; } - + /* Use this register info */ CurrentRegs = &Regs; - + } - + /* 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; /* 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: @@ -1237,7 +1466,7 @@ void CS_GenRegInfo (CodeSeg* S) 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. @@ -1250,7 +1479,7 @@ void CS_GenRegInfo (CodeSeg* S) } } break; - + case OP65_CPX: /* If this is an immidiate compare, the X register has * the value of the compare later. @@ -1263,7 +1492,7 @@ void CS_GenRegInfo (CodeSeg* S) } } break; - + case OP65_CPY: /* If this is an immidiate compare, the Y register has * the value of the compare later. @@ -1276,7 +1505,7 @@ void CS_GenRegInfo (CodeSeg* S) } } break; - + case OP65_DEX: case OP65_INX: case OP65_LDX: @@ -1288,7 +1517,7 @@ void CS_GenRegInfo (CodeSeg* S) E->RI->Out.RegX = 0; } break; - + case OP65_DEY: case OP65_INY: case OP65_LDY: @@ -1300,7 +1529,7 @@ void CS_GenRegInfo (CodeSeg* S) 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 @@ -1313,7 +1542,7 @@ void CS_GenRegInfo (CodeSeg* S) 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 @@ -1326,10 +1555,10 @@ void CS_GenRegInfo (CodeSeg* S) E->RI->Out.RegA = E->RI->Out.RegY = 0; } break; - + default: break; - + } } }