1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-24 11:31:31 +00:00

More polishing to get the line info right

git-svn-id: svn://svn.cc65.org/cc65/trunk@742 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2001-05-22 16:54:47 +00:00
parent 12ec031f9a
commit 3a272d93af
7 changed files with 190 additions and 140 deletions

View File

@ -357,19 +357,21 @@ void OutputCodeEntry (const CodeEntry* E, FILE* F)
} }
/* Print usage info if requested by the debugging flag */ /* Print usage info if requested by the debugging flag */
// if (Debug) { if (Debug) {
Chars += fprintf (F, fprintf (F,
"%*s; USE: %c%c%c CHG: %c%c%c SIZE: %u\n", "%*s; USE: %c%c%c CHG: %c%c%c SIZE: %u\n",
30-Chars, "", 30-Chars, "",
(E->Use & REG_A)? 'A' : '_', (E->Use & REG_A)? 'A' : '_',
(E->Use & REG_X)? 'X' : '_', (E->Use & REG_X)? 'X' : '_',
(E->Use & REG_Y)? 'Y' : '_', (E->Use & REG_Y)? 'Y' : '_',
(E->Chg & REG_A)? 'A' : '_', (E->Chg & REG_A)? 'A' : '_',
(E->Chg & REG_X)? 'X' : '_', (E->Chg & REG_X)? 'X' : '_',
(E->Chg & REG_Y)? 'Y' : '_', (E->Chg & REG_Y)? 'Y' : '_',
E->Size); E->Size);
// } } else {
/* Terminate the line */
fprintf (F, "\n");
}
} }

View File

@ -949,12 +949,7 @@ void OutputCodeSeg (const CodeSeg* S, FILE* F)
if (E->LI != LI) { if (E->LI != LI) {
LI = E->LI; LI = E->LI;
if (AddSource) { if (AddSource) {
/* Skip spaces to make the output somewhat more readable */ fprintf (F, ";\n; %s\n;\n", LI->Line);
const char* Line = LI->Line;
while (IsBlank (*Line)) {
++Line;
}
fprintf (F, ";\n; %s\n;\n", Line);
} }
} }
/* Output the code */ /* Output the code */

View File

@ -3128,9 +3128,6 @@ void test (unsigned label, int cond)
/* Load the value into the primary register */ /* Load the value into the primary register */
exprhs (CF_FORCECHAR, k, &lval); exprhs (CF_FORCECHAR, k, &lval);
/* Check for the closing brace */
ConsumeRParen ();
/* Generate the jump */ /* Generate the jump */
if (cond) { if (cond) {
g_truejump (CF_NONE, label); g_truejump (CF_NONE, label);
@ -3143,6 +3140,9 @@ void test (unsigned label, int cond)
g_falsejump (CF_NONE, label); g_falsejump (CF_NONE, label);
} }
} }
/* Check for the closing brace */
ConsumeRParen ();
} }

View File

@ -47,7 +47,7 @@
void DoGoto (void) void GotoStatement (void)
/* Process a goto statement. */ /* Process a goto statement. */
{ {
/* Eat the "goto" */ /* Eat the "goto" */

View File

@ -44,7 +44,7 @@
void DoGoto (void); void GotoStatement (void);
/* Process a goto statement. */ /* Process a goto statement. */
void DoLabel (void); void DoLabel (void);

View File

@ -36,10 +36,12 @@
#include <string.h> #include <string.h>
/* common */ /* common */
#include "chartype.h"
#include "check.h" #include "check.h"
#include "xmalloc.h" #include "xmalloc.h"
/* cc65 */ /* cc65 */
#include "global.h"
#include "input.h" #include "input.h"
#include "lineinfo.h" #include "lineinfo.h"
@ -65,11 +67,20 @@ static LineInfo* CurLineInfo = 0;
static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Line) static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
/* Create and return a new line info. Ref count will be 1. */ /* Create and return a new line info. Ref count will be 1. */
{ {
unsigned Len;
LineInfo* LI;
char* S;
/* Skip leading spaces in Line */
while (IsBlank (*Line)) {
++Line;
}
/* Calculate the length of the line */ /* Calculate the length of the line */
unsigned Len = strlen (Line); Len = strlen (Line);
/* Allocate memory */ /* Allocate memory */
LineInfo* LI = xmalloc (sizeof (LineInfo) + Len); LI = xmalloc (sizeof (LineInfo) + Len);
/* Initialize the fields */ /* Initialize the fields */
LI->RefCount = 1; LI->RefCount = 1;
@ -77,6 +88,18 @@ static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Lin
LI->LineNum = LineNum; LI->LineNum = LineNum;
memcpy (LI->Line, Line, Len+1); memcpy (LI->Line, Line, Len+1);
/* Replace tabs by spaces in the given line since tabs will give rather
* arbitrary results when used in the output later, and if we do it here,
* we won't need another copy.
*/
S = LI->Line;
while (*S) {
if (*S == '\t') {
*S = ' ';
}
++S;
}
/* Return the new struct */ /* Return the new struct */
return LI; return LI;
} }
@ -133,6 +156,13 @@ void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
ReleaseLineInfo (CurLineInfo); ReleaseLineInfo (CurLineInfo);
} }
/* If we have intermixed assembly switched off, use an empty line instead
* of the supplied one to save some memory.
*/
if (!AddSource) {
Line = "";
}
/* Create a new line info */ /* Create a new line info */
CurLineInfo = NewLineInfo (F, LineNum, Line); CurLineInfo = NewLineInfo (F, LineNum, Line);
} }

View File

@ -45,33 +45,38 @@
/*****************************************************************************/
/* Forwards */
/*****************************************************************************/
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
static int DoIf (void) static int IfStatement (void)
/* Handle an 'if' statement */ /* Handle an 'if' statement */
{ {
int flab1; unsigned Label1;
int flab2; int GotBreak;
int gotbreak;
/* Skip the if */ /* Skip the if */
NextToken (); NextToken ();
/* Generate a jump label and parse the condition */ /* Generate a jump label and parse the condition */
flab1 = GetLocalLabel (); Label1 = GetLocalLabel ();
test (flab1, 0); test (Label1, 0);
/* Parse the if body */ /* Parse the if body */
gotbreak = Statement (); GotBreak = Statement ();
/* Else clause present? */ /* Else clause present? */
if (CurTok.Tok != TOK_ELSE) { if (CurTok.Tok != TOK_ELSE) {
g_defcodelabel (flab1); g_defcodelabel (Label1);
/* Since there's no else clause, we're not sure, if the a break /* Since there's no else clause, we're not sure, if the a break
* statement is really executed. * statement is really executed.
*/ */
@ -79,36 +84,30 @@ static int DoIf (void)
} else { } else {
/* Skip the else */ /* Generate a jump around the else branch */
unsigned Label2 = GetLocalLabel ();
g_jump (Label2);
/* Skip the else */
NextToken (); NextToken ();
/* If we had some sort of break statement at the end of the if clause, /* Define the target for the first test */
* there's no need to generate an additional jump around the else g_defcodelabel (Label1);
* clause, since the jump is never reached.
*/ /* Total break only if both branches had a break. */
if (!gotbreak) { GotBreak &= Statement ();
flab2 = GetLocalLabel ();
g_jump (flab2);
} else {
/* Mark the label as unused */
flab2 = 0;
}
g_defcodelabel (flab1);
gotbreak &= Statement ();
/* Generate the label for the else clause */ /* Generate the label for the else clause */
if (flab2) { g_defcodelabel (Label2);
g_defcodelabel (flab2);
}
/* Done */ /* Done */
return gotbreak; return GotBreak;
} }
} }
static void DoDo (void) static void DoStatement (void)
/* Handle the 'do' statement */ /* Handle the 'do' statement */
{ {
/* Get the loop control labels */ /* Get the loop control labels */
@ -141,7 +140,7 @@ static void DoDo (void)
static void DoWhile (void) static void WhileStatement (void)
/* Handle the 'while' statement */ /* Handle the 'while' statement */
{ {
/* Get the loop control labels */ /* Get the loop control labels */
@ -184,7 +183,7 @@ static void DoWhile (void)
static void DoReturn (void) static void ReturnStatement (void)
/* Handle the 'return' statement */ /* Handle the 'return' statement */
{ {
struct expent lval; struct expent lval;
@ -215,7 +214,7 @@ static void DoReturn (void)
static void DoBreak (void) static void BreakStatement (void)
/* Handle the 'break' statement */ /* Handle the 'break' statement */
{ {
LoopDesc* L; LoopDesc* L;
@ -242,7 +241,7 @@ static void DoBreak (void)
static void DoContinue (void) static void ContinueStatement (void)
/* Handle the 'continue' statement */ /* Handle the 'continue' statement */
{ {
LoopDesc* L; LoopDesc* L;
@ -284,8 +283,8 @@ static void DoContinue (void)
static void CascadeSwitch (struct expent* eval) static void CascadeSwitch (struct expent* eval)
/* Handle a switch statement for chars with a cmp cascade for the selector */ /* Handle a switch statement for chars with a cmp cascade for the selector */
{ {
unsigned ExitLab; /* Exit label */ unsigned ExitLab; /* Exit label */
unsigned NextLab; /* Next case label */ unsigned NextLab; /* Next case label */
unsigned CodeLab; /* Label that starts the actual selector code */ unsigned CodeLab; /* Label that starts the actual selector code */
int HaveBreak; /* Remember if we exited with break */ int HaveBreak; /* Remember if we exited with break */
int HaveDefault; /* Remember if we had a default label */ int HaveDefault; /* Remember if we had a default label */
@ -352,29 +351,29 @@ static void CascadeSwitch (struct expent* eval)
case T_SCHAR: case T_SCHAR:
/* Signed char */ /* Signed char */
if (Val < -128 || Val > 127) { if (Val < -128 || Val > 127) {
Error ("Range error"); Error ("Range error");
} }
break; break;
case T_UCHAR: case T_UCHAR:
if (Val < 0 || Val > 255) { if (Val < 0 || Val > 255) {
Error ("Range error"); Error ("Range error");
} }
break; break;
case T_INT: case T_INT:
if (Val < -32768 || Val > 32767) { if (Val < -32768 || Val > 32767) {
Error ("Range error"); Error ("Range error");
} }
break; break;
case T_UINT: case T_UINT:
if (Val < 0 || Val > 65535) { if (Val < 0 || Val > 65535) {
Error ("Range error"); Error ("Range error");
} }
break; break;
default: default:
Internal ("Invalid type: %02X", *eval->e_tptr & 0xFF); Internal ("Invalid type: %02X", *eval->e_tptr & 0xFF);
} }
@ -385,17 +384,17 @@ static void CascadeSwitch (struct expent* eval)
* the condition is true. * the condition is true.
*/ */
if (CurTok.Tok == TOK_CASE) { if (CurTok.Tok == TOK_CASE) {
/* Create a code label if needed */ /* Create a code label if needed */
if (CodeLab == 0) { if (CodeLab == 0) {
CodeLab = GetLocalLabel (); CodeLab = GetLocalLabel ();
} }
g_falsejump (CF_NONE, CodeLab); g_falsejump (CF_NONE, CodeLab);
} else if (CurTok.Tok != TOK_DEFAULT) { } else if (CurTok.Tok != TOK_DEFAULT) {
/* No case follows, jump to next selector */ /* No case follows, jump to next selector */
if (NextLab == 0) { if (NextLab == 0) {
NextLab = GetLocalLabel (); NextLab = GetLocalLabel ();
} }
g_truejump (CF_NONE, NextLab); g_truejump (CF_NONE, NextLab);
} }
/* Skip the colon */ /* Skip the colon */
@ -475,7 +474,7 @@ static void TableSwitch (struct expent* eval)
int lcount; /* Label count */ int lcount; /* Label count */
int HaveBreak; /* Last statement has a break */ int HaveBreak; /* Last statement has a break */
int HaveDefault; /* Remember if we had a default label */ int HaveDefault; /* Remember if we had a default label */
unsigned Flags; /* Code generator flags */ unsigned Flags; /* Code generator flags */
struct expent lval; /* Case label expression */ struct expent lval; /* Case label expression */
struct swent *p; struct swent *p;
struct swent *swtab; struct swent *swtab;
@ -569,7 +568,7 @@ static void TableSwitch (struct expent* eval)
static void DoSwitch (void) static void SwitchStatement (void)
/* Handle a 'switch' statement */ /* Handle a 'switch' statement */
{ {
struct expent eval; /* Switch statement expression */ struct expent eval; /* Switch statement expression */
@ -595,98 +594,122 @@ static void DoSwitch (void)
static void DoFor (void) static void ForStatement (void)
/* Handle a 'for' statement */ /* Handle a 'for' statement */
{ {
int loop;
int lab;
int linc;
int lstat;
struct expent lval1; struct expent lval1;
struct expent lval2; struct expent lval2;
struct expent lval3; struct expent lval3;
/* Get several local labels needed later */
unsigned TestLabel = GetLocalLabel ();
unsigned lab = GetLocalLabel ();
unsigned IncLabel = GetLocalLabel ();
unsigned lstat = GetLocalLabel ();
/* Skip the FOR token */
NextToken (); NextToken ();
loop = GetLocalLabel ();
lab = GetLocalLabel (); /* Add the loop to the loop stack */
linc = GetLocalLabel (); AddLoop (oursp, TestLabel, lab, IncLabel, lstat);
lstat = GetLocalLabel ();
AddLoop (oursp, loop, lab, linc, lstat); /* Skip the opening paren */
ConsumeLParen (); ConsumeLParen ();
if (CurTok.Tok != TOK_SEMI) { /* exp1 */
/* Parse the initializer expression */
if (CurTok.Tok != TOK_SEMI) {
expression (&lval1); expression (&lval1);
} }
ConsumeSemi (); ConsumeSemi ();
g_defcodelabel (loop);
if (CurTok.Tok != TOK_SEMI) { /* exp2 */ /* Label for the test expressions */
boolexpr (&lval2); g_defcodelabel (TestLabel);
g_truejump (CF_NONE, lstat);
g_jump (lab); /* Parse the text expression */
if (CurTok.Tok != TOK_SEMI) {
boolexpr (&lval2);
g_truejump (CF_NONE, lstat);
g_jump (lab);
} else { } else {
g_jump (lstat); g_jump (lstat);
} }
ConsumeSemi (); ConsumeSemi ();
g_defcodelabel (linc);
if (CurTok.Tok != TOK_RPAREN) { /* exp3 */ /* Label for the increment expression */
g_defcodelabel (IncLabel);
/* Parse the increment expression */
if (CurTok.Tok != TOK_RPAREN) {
expression (&lval3); expression (&lval3);
} }
/* Jump to the test */
g_jump (TestLabel);
/* Skip the closing paren */
ConsumeRParen (); ConsumeRParen ();
g_jump (loop);
/* Loop body */
g_defcodelabel (lstat); g_defcodelabel (lstat);
Statement (); Statement ();
g_jump (linc);
/* Jump back to the increment expression */
g_jump (IncLabel);
/* Declare the break label */
g_defcodelabel (lab); g_defcodelabel (lab);
/* Remove the loop from the loop stack */
DelLoop (); DelLoop ();
} }
static int CompoundStatement (void) static int CompoundStatement (void)
/* Compound statement. Allow any number of statements inside braces. */ /* Compound statement. Allow any number of statements inside braces. The
* function returns true if the last statement was a break or return.
*/
{ {
int isbrk; int GotBreak;
int oldsp;
/* eat LCURLY */
NextToken ();
/* Remember the stack at block entry */ /* Remember the stack at block entry */
oldsp = oursp; int OldStack = oursp;
/* Enter a new lexical level */ /* Enter a new lexical level */
EnterBlockLevel (); EnterBlockLevel ();
/* Skip the rcurly */
NextToken ();
/* Parse local variable declarations if any */ /* Parse local variable declarations if any */
DeclareLocals (); DeclareLocals ();
/* Now process statements in this block */ /* Now process statements in this block */
isbrk = 0; GotBreak = 0;
while (CurTok.Tok != TOK_RCURLY) { while (CurTok.Tok != TOK_RCURLY) {
if (CurTok.Tok != TOK_CEOF) { if (CurTok.Tok != TOK_CEOF) {
isbrk = Statement (); GotBreak = Statement ();
} else { } else {
break; break;
} }
} }
/* Clean up the stack. */
if (!GotBreak) {
g_space (oursp - OldStack);
}
oursp = OldStack;
/* Skip the closing brace */
ConsumeRCurly ();
/* Emit references to imports/exports for this block */ /* Emit references to imports/exports for this block */
EmitExternals (); EmitExternals ();
/* Clean up the stack. */
if (isbrk) {
oursp = oldsp;
} else {
g_space (oursp - oldsp);
oursp = oldsp;
}
/* Leave the lexical level */ /* Leave the lexical level */
LeaveBlockLevel (); LeaveBlockLevel ();
/* Eat closing brace */ return GotBreak;
ConsumeRCurly ();
return isbrk;
} }
@ -698,7 +721,7 @@ int Statement (void)
{ {
struct expent lval; struct expent lval;
/* */ /* Check for a label */
if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) { if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
/* Special handling for a label */ /* Special handling for a label */
@ -709,44 +732,44 @@ int Statement (void)
switch (CurTok.Tok) { switch (CurTok.Tok) {
case TOK_LCURLY: case TOK_LCURLY:
return CompoundStatement (); return CompoundStatement ();
case TOK_IF: case TOK_IF:
return DoIf (); return IfStatement ();
case TOK_WHILE: case TOK_WHILE:
DoWhile (); WhileStatement ();
break; break;
case TOK_DO: case TOK_DO:
DoDo (); DoStatement ();
break; break;
case TOK_SWITCH: case TOK_SWITCH:
DoSwitch (); SwitchStatement ();
break; break;
case TOK_RETURN: case TOK_RETURN:
DoReturn (); ReturnStatement ();
ConsumeSemi (); ConsumeSemi ();
return 1; return 1;
case TOK_BREAK: case TOK_BREAK:
DoBreak (); BreakStatement ();
ConsumeSemi (); ConsumeSemi ();
return 1; return 1;
case TOK_CONTINUE: case TOK_CONTINUE:
DoContinue (); ContinueStatement ();
ConsumeSemi (); ConsumeSemi ();
return 1; return 1;
case TOK_FOR: case TOK_FOR:
DoFor (); ForStatement ();
break; break;
case TOK_GOTO: case TOK_GOTO:
DoGoto (); GotoStatement ();
ConsumeSemi (); ConsumeSemi ();
return 1; return 1;