Skip to content

Commit

Permalink
optimizations, tail IF, tail CASE
Browse files Browse the repository at this point in the history
  • Loading branch information
tebe6502 committed Jan 23, 2025
1 parent 1e2213e commit 54f2d8d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 82 deletions.
Binary file modified bin/windows/mp.exe
Binary file not shown.
6 changes: 4 additions & 2 deletions src/Optimize.pas
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ procedure OptimizeTemporaryBuf;

{$i include/opt6502/opt_TEMP_MOVE.inc}
{$i include/opt6502/opt_TEMP_FILL.inc}
{$i include/opt6502/opt_TEMP_TAIL.inc}
{$i include/opt6502/opt_TEMP_TAIL_IF.inc}
{$i include/opt6502/opt_TEMP_TAIL_CASE.inc}
{$i include/opt6502/opt_TEMP.inc}
{$i include/opt6502/opt_TEMP_CMP.inc}
{$i include/opt6502/opt_TEMP_CMP_0.inc}
Expand Down Expand Up @@ -359,7 +360,8 @@ procedure OptimizeTemporaryBuf;
opt_TEMP_FILL;

opt_TEMP_IFTMP;
opt_TEMP_TAIL;
opt_TEMP_TAIL_IF;
opt_TEMP_TAIL_CASE;


// #asm
Expand Down
5 changes: 5 additions & 0 deletions src/include/cmd_temporary.inc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
Result := pos('b_', TemporaryBuf[i]) = 1
end;

function LAB_S(i: integer): Boolean;
begin
Result := pos('s_', TemporaryBuf[i]) = 1
end;

function INC_(i: integer): Boolean;
begin
Result := pos(#9'inc ', TemporaryBuf[i]) = 1
Expand Down
68 changes: 68 additions & 0 deletions src/include/opt6502/opt_TEMP_TAIL_CASE.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

// -----------------------------------------------------------------------------
// === Common head/tail Sequence coalescing
// -----------------------------------------------------------------------------
// CASE STATEMENT
// -----------------------------------------------------------------------------

procedure opt_TEMP_TAIL_CASE;
var i, p, k: integer;
yes: Boolean;
lab, tmp: string;
begin

i:=100;

if (pos(#9'jmp a_', TemporaryBuf[i]) > 0) then // jmp a_xxxx
begin

//if lab_s(i+1) then TemporaryBuf[i+1]:= '~';

tmp:=TemporaryBuf[i]; // jmp a_xxxx
lab:=copy(tmp, 6, 256); // a_xxxx

yes:=false;

for p:=i+1 to High(TemporaryBuf) do
if (TemporaryBuf[p] = tmp) and // jmp a_xxxx
lab_s(p+1) then //s_xxxx
begin
yes:=true;
Break;
end;


if yes then begin

lab:=TemporaryBuf[p+1]; //s_xxxx

TemporaryBuf[i] := #9'jmp ' + lab;

TemporaryBuf[p] := lab;
TemporaryBuf[p+1] := tmp;

writeln(p-i);

// writeln(TemporaryBuf[i] ,',', TemporaryBuf[p]) ;

for k:=1 to i-1 do
if (TemporaryBuf[i-k] = TemporaryBuf[p-k]) or
(lab_l(i-k) and lab_l(p-k)) or
(opti(i-k) and opti(p-k)) then
begin

TemporaryBuf[i-k] := '~';

tmp := TemporaryBuf[p-k];

TemporaryBuf[p-k] := TemporaryBuf[p-k+1];
TemporaryBuf[p-k+1] := tmp;

end else
exit;

end; // if yes

end;

end; // procedure
Original file line number Diff line number Diff line change
@@ -1,84 +1,16 @@

// -----------------------------------------------------------------------------
// === Common head/tail Sequence coalescing
// === Common head/tail Sequence coalescing
// -----------------------------------------------------------------------------
// IF THEN STATEMENT
// -----------------------------------------------------------------------------

procedure opt_TEMP_TAIL;
procedure opt_TEMP_TAIL_IF;
var i, p, k: integer;
yes: Boolean;
lab, tmp: string;
begin


// -----------------------------------------------------------------------------
// CASE STATEMENT
// -----------------------------------------------------------------------------
(*
if pos(#9'jmp a_', TemporaryBuf[60]) then
begin
lab:=copy(TemporaryBuf[60], 6, 256); // a_xxxx
yes:=false;
for p:=61 to High(TemporaryBuf) do
if TemporaryBuf[p] = lab then begin yes:=true; Break end;
if yes then begin
while lab_l(p-1) do begin // l_xxxx
tmp:=TemporaryBuf[p-1]; // l_yyyy
// l_zzzz
TemporaryBuf[p-1] := TemporaryBuf[p];
TemporaryBuf[p] := tmp;
dec(p);
end;
if (TemporaryBuf[22] = #9'beq *+5') then
i:=19
else
i:=20;
{
writeln(p);
for k:=0 to 19 do
writeln(TemporaryBuf[p-1-k]);
writeln('-------');
}
if sta(i) or jsr(i) then begin
for k:=0 to 19 do
if TemporaryBuf[i-k] <> TemporaryBuf[p-1-k] then
exit
else begin
TemporaryBuf[i-k] := '~';
TemporaryBuf[p-k] := TemporaryBuf[p-1-k];
TemporaryBuf[p-1-k] := lab;
end;
end;
end; // if yes
end;
*)

// -----------------------------------------------------------------------------
// IF THEN STATEMENT
// -----------------------------------------------------------------------------


if (
(sta(20) or jsr(20)) and // sta|jsr ; 20
lab_l(21) and //l_xxxx ; 21
Expand Down Expand Up @@ -136,6 +68,7 @@ writeln('-------');

for k:=0 to 19 do
if (TemporaryBuf[i-k] = TemporaryBuf[p-1-k]) or
(lab_l(i-k) and lab_l(p-1-k)) or
(opti(i-k) and opti(p-1-k)) then
begin

Expand Down
16 changes: 8 additions & 8 deletions src/mp.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3725,20 +3725,20 @@ procedure GenerateForToDoCondition(ValType: Byte; Down: Boolean; IdentIndex: int
begin

if ValType in [SHORTINTTOK, SMALLINTTOK, INTEGERTOK] then
asm65(#9'bpl *+5', '; >=')
asm65(#9'bpl *+5')
else
asm65(#9'bcs *+5', '; >=');
asm65(#9'bcs *+5');

end

else
begin

if ValType in [SHORTINTTOK, SMALLINTTOK, INTEGERTOK] then begin
asm65(#9'bmi *+7', '; <=');
asm65(#9'bmi *+7');
asm65(#9'beq *+5');
end else begin
asm65(#9'bcc *+7', '; <=');
asm65(#9'bcc *+7');
asm65(#9'beq *+5');
end;

Expand Down Expand Up @@ -3902,7 +3902,7 @@ procedure GenerateCaseStatementEpilog(cnt: integer);

asm65(#9'jmp a_'+IntToHex(cnt,4));

// asm65('l_' + IntToHex(CodeSize, 4));
asm65('s_'+IntToHex(CodeSize, 4)); // opt_TEMP_TAIL_CASE


StoredCodeSize := CodeSize;
Expand Down Expand Up @@ -5513,7 +5513,7 @@ procedure GenerateRelationString(rel: Byte; LeftValType, RightValType: Byte);

Gen;

asm65(#9'ldy #1', '; true');
asm65(#9'ldy #1');

Gen;

Expand Down Expand Up @@ -13419,7 +13419,7 @@ function CompileStatement(i: Integer; isAsm: Boolean = false): Integer;
Error(i, 'BREAK not allowed');

// asm65;
asm65(#9'jmp b_'+IntToHex(BreakPosStack[BreakPosStackTop].ptr, 4), '; break');
asm65(#9'jmp b_'+IntToHex(BreakPosStack[BreakPosStackTop].ptr, 4));

BreakPosStack[BreakPosStackTop].brk := true;

Expand All @@ -13435,7 +13435,7 @@ function CompileStatement(i: Integer; isAsm: Boolean = false): Integer;
Error(i, 'CONTINUE not allowed');

// asm65;
asm65(#9'jmp c_'+IntToHex(BreakPosStack[BreakPosStackTop].ptr, 4), '; continue');
asm65(#9'jmp c_'+IntToHex(BreakPosStack[BreakPosStackTop].ptr, 4));

BreakPosStack[BreakPosStackTop].cnt := true;

Expand Down

0 comments on commit 54f2d8d

Please sign in to comment.