/* */
/* */
/* */
-/* (C) 2001-2004 Ullrich von Bassewitz */
-/* Römerstrasse 52 */
-/* D-70794 Filderstadt */
-/* 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 */
#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 "error.h"
#include "global.h"
#include "ident.h"
+#include "output.h"
#include "symentry.h"
-static void CS_PrintFunctionHeader (const CodeSeg* S, FILE* F)
-/* Print a comment with the function signature to the given file */
+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) {
- fprintf (F,
- "; ---------------------------------------------------------------\n"
- "; ");
- PrintFuncSig (F, Func->Name, Func->Type);
- fprintf (F,
- "\n"
- "; ---------------------------------------------------------------\n"
- "\n");
+ WriteOutput ("; ---------------------------------------------------------------\n"
+ "; ");
+ PrintFuncSig (OutputFile, Func->Name, Func->Type);
+ WriteOutput ("\n"
+ "; ---------------------------------------------------------------\n"
+ "\n");
}
}
if (*L == ',') {
L = SkipSpace (L+1);
if (toupper (*L) != 'Y') {
- Error ("ASM code error: `Y' expected");
+ 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') {
} 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 == ',') {
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;
}
}
}
/* Create a new code segment, initialize and return it */
{
unsigned I;
- const type* RetType;
+ const Type* RetType;
/* Allocate memory */
CodeSeg* S = xmalloc (sizeof (CodeSeg));
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 */
if (E) {
CS_AddEntry (S, E);
}
+
+ /* Cleanup the string buffer */
+ SB_Done (&Buf);
}
+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 */
{
-void CS_OutputPrologue (const CodeSeg* S, FILE* F)
+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
*/
if (Func) {
/* Get the function descriptor */
- const FuncDesc* D = GetFuncDesc (Func->Type);
- CS_PrintFunctionHeader (S, F);
- fprintf (F, ".segment\t\"%s\"\n\n.proc\t_%s", S->SegName, Func->Name);
- if (D->Flags & FD_NEAR) {
- fputs (": near", F);
- } else if (D->Flags & FD_FAR) {
- fputs (": far", F);
+ 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");
}
- fputs ("\n\n", F);
+ WriteOutput ("\n\n");
}
}
-void CS_OutputEpilogue (const CodeSeg* S, FILE* F)
+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) {
- fputs ("\n.endproc\n\n", F);
+ WriteOutput ("\n.endproc\n\n");
}
}
-void CS_Output (CodeSeg* S, FILE* F)
-/* Output the code segment data to a file */
+void CS_Output (CodeSeg* S)
+/* Output the code segment data to the output file */
{
unsigned I;
const LineInfo* LI;
CS_GenRegInfo (S);
/* Output the segment directive */
- fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
+ 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. Beware: When line continuation
+ /* 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) {
+ if (AddSource) {
const char* L = LI->Line;
- fputs (";\n; ", F);
+ WriteOutput (";\n; ");
while (*L) {
- if (*L == '\n') {
- fputs ("\n; ", F);
+ 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 {
- fputc (*L, F);
+ /* No Newline, write as is */
+ WriteOutput ("%s\n", L);
+ break;
}
- ++L;
}
- fputs ("\n;\n", F);
- }
+ WriteOutput (";\n");
+ }
- /* Add line debug info */
+ /* 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) {
- fputs ("\t.dbg\tline\n", F);
+ WriteOutput ("\t.dbg\tline\n");
}
/* Free register info */
/* If this is an immidiate compare, the A register has
* the value of the compare later.
*/
- if (CE_KnownImm (P)) {
+ if (CE_IsConstImm (P)) {
if (BC == BC_EQ) {
E->RI->Out2.RegA = (unsigned char)P->Num;
} else {
/* If this is an immidiate compare, the X register has
* the value of the compare later.
*/
- if (CE_KnownImm (P)) {
+ if (CE_IsConstImm (P)) {
if (BC == BC_EQ) {
E->RI->Out2.RegX = (unsigned char)P->Num;
} else {
/* If this is an immidiate compare, the Y register has
* the value of the compare later.
*/
- if (CE_KnownImm (P)) {
+ if (CE_IsConstImm (P)) {
if (BC == BC_EQ) {
E->RI->Out2.RegY = (unsigned char)P->Num;
} else {