Skip to content

Commit d4e1aba

Browse files
committed
origin: Minimize differences in Optimize
1 parent 57eb3f4 commit d4e1aba

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

origin/Optimize.pas

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ interface
66

77
// ----------------------------------------------------------------------------
88

9-
var
10-
TemporaryBuf: array [0..511] of String;
11-
9+
procedure ResetOpty;
1210

1311
procedure asm65(a: String = ''; comment: String = ''); // OptimizeASM
1412

15-
procedure OptimizeProgram(MainIndex: Integer);
16-
17-
procedure ResetOpty;
13+
procedure OptimizeProgram(MainProcedureIndex: Integer);
1814

1915
procedure WriteOut(a: String); // OptimizeTemporaryBuf
2016

@@ -25,6 +21,9 @@ procedure FlushTempBuf;
2521
implementation
2622

2723
uses Crt, SysUtils, Common;
24+
var
25+
TemporaryBuf: array [0..511] of String;
26+
2827

2928
// ----------------------------------------------------------------------------
3029

@@ -43,12 +42,12 @@ procedure ResetOpty;
4342
// ----------------------------------------------------------------------------
4443

4544

46-
procedure OptimizeProgram(MainIndex: Integer);
45+
procedure OptimizeProgram(MainProcedureIndex: Integer);
4746
type
48-
TBoolean = array [1..MAXBLOCKS] of Boolean;
47+
TBooleanArray = array [1..MAXBLOCKS] of Boolean;
4948

5049
var
51-
ProcAsBlock: TBoolean; // issue #125 fixed
50+
ProcAsBlock: TBooleanArray; // issue #125 fixed
5251

5352
procedure MarkNotDead(IdentIndex: Integer);
5453
var
@@ -57,7 +56,7 @@ procedure OptimizeProgram(MainIndex: Integer);
5756

5857
Ident[IdentIndex].IsNotDead := True;
5958

60-
ProcAsBlockIndex := Ident[IdentIndex].ProcAsBlock;
59+
ProcAsBlockIndex := IdentifierAt(IdentIndex).ProcAsBlock;
6160

6261
if (ProcAsBlockIndex > 0) and (ProcAsBlock[ProcAsBlockIndex] = False) and
6362
(CallGraph[ProcAsBlockIndex].NumChildren > 0) then
@@ -67,7 +66,7 @@ procedure OptimizeProgram(MainIndex: Integer);
6766

6867
for ChildIndex := 1 to CallGraph[ProcAsBlockIndex].NumChildren do
6968
for ChildIdentIndex := 1 to NumIdent do
70-
if (Ident[ChildIdentIndex].ProcAsBlock > 0) and (Ident[ChildIdentIndex].ProcAsBlock =
69+
if (IdentifierAt(ChildIdentIndex).ProcAsBlock > 0) and (IdentifierAt(ChildIdentIndex).ProcAsBlock =
7170
CallGraph[ProcAsBlockIndex].ChildBlock[ChildIndex]) then
7271
MarkNotDead(ChildIdentIndex);
7372

@@ -77,11 +76,10 @@ procedure OptimizeProgram(MainIndex: Integer);
7776

7877
begin
7978

80-
//fillbyte(ProcAsBlock, sizeof(ProcAsBlock), 0);
81-
ProcAsBlock := Default(TBoolean);
79+
ProcAsBlock := Default(TBooleanArray);
8280

8381
// Perform dead code elimination
84-
MarkNotDead(MainIndex);
82+
MarkNotDead(MainProcedureIndex);
8583

8684
end;
8785

@@ -539,6 +537,7 @@ procedure OptimizeASM;
539537
type
540538
TListing = array [0..1023] of String;
541539
TListing_tmp = array [0..127] of String;
540+
TString0_3_Array = array [0..3] of String;
542541

543542
var
544543
inxUse, found: Boolean;
@@ -551,7 +550,7 @@ procedure OptimizeASM;
551550

552551
a, t, arg0: String;
553552

554-
s: array [0..15, 0..3] of String;
553+
s: array [0..15] of TString0_3_Array;
555554

556555
// -----------------------------------------------------------------------------
557556

@@ -607,12 +606,7 @@ procedure OptimizeASM;
607606

608607
NormVideo;
609608

610-
// FreeTokens;
611-
612-
// CloseFile(OutFile);
613-
// Erase(OutFile);
614-
615-
// Halt(2);
609+
// RaiseHaltException(THaltException.COMPILING_ABORTED);
616610
end;
617611

618612
WriteOut(listing[i]);
@@ -1030,7 +1024,7 @@ procedure OptimizeASM;
10301024
a := listing[j];
10311025

10321026
if a <> '' then
1033-
while not (a[i] in [' ', #9]) and (i <= length(a)) do
1027+
while (i <= length(a)) and not (a[i] in [' ', #9]) do
10341028
begin
10351029
Result := Result + a[i];
10361030
Inc(i);
@@ -1093,7 +1087,7 @@ procedure OptimizeASM;
10931087

10941088
while a[i] in [' ', #9] do Inc(i);
10951089

1096-
while not (a[i] in [' ', #9]) and (i <= length(a)) do
1090+
while (i <= length(a)) and not (a[i] in [' ', #9]) do
10971091
begin
10981092
Result := Result + a[i];
10991093
Inc(i);
@@ -1107,16 +1101,25 @@ procedure OptimizeASM;
11071101

11081102

11091103
function RemoveUnusedSTACK: Boolean;
1104+
const
1105+
last_i = 7;
1106+
const
1107+
last_i_plus_one = last_i + 1;
1108+
const
1109+
min_j = 0;
1110+
const
1111+
last_j = 3;
1112+
type
1113+
TArray0_3Boolean = array[min_j..last_j] of Boolean;
11101114
var
1111-
j: Byte;
1112-
i: Integer;
1113-
cnt_l, // licznik odczytow stosu
1114-
cnt_s: array [0..7 + 1, 0..3] of Boolean; // licznik zapisow stosu
1115+
i, j: Integer;
1116+
cnt_l, // load stack pointer
1117+
cnt_s: array [0..last_i_plus_one] of TArray0_3Boolean; // store stack pointer
11151118

11161119

11171120
procedure Clear;
11181121
var
1119-
i: Byte;
1122+
i, j: Byte;
11201123
begin
11211124

11221125
for i := 0 to 15 do
@@ -1127,8 +1130,14 @@ procedure OptimizeASM;
11271130
s[i][3] := '';
11281131
end;
11291132

1130-
fillchar(cnt_l, sizeof(cnt_l), False);
1131-
fillchar(cnt_s, sizeof(cnt_s), False);
1133+
for i := Low(cnt_l) to High(cnt_l) do
1134+
begin
1135+
for j := min_j to last_j do
1136+
begin
1137+
cnt_l[i][j] := False;
1138+
cnt_s[i][j] := False;
1139+
end;
1140+
end;
11321141

11331142
end;
11341143

@@ -1311,7 +1320,7 @@ procedure OptimizeASM;
13111320

13121321
function PeepholeOptimization_STACK: Boolean;
13131322
var
1314-
i, p: Integer;
1323+
i: Integer;
13151324
tmp: String;
13161325
begin
13171326

@@ -1528,7 +1537,7 @@ procedure OptimizeASM;
15281537

15291538
function PeepholeOptimization_STA: Boolean;
15301539
var
1531-
i, p: Integer;
1540+
i: Integer;
15321541
begin
15331542

15341543
Result := True;
@@ -1612,7 +1621,7 @@ procedure OptimizeASM;
16121621

16131622
function PeepholeOptimization: Boolean;
16141623
var
1615-
p, i: Integer;
1624+
i: Integer;
16161625
begin
16171626

16181627
Result := True;
@@ -3684,7 +3693,7 @@ procedure OptimizeASM;
36843693

36853694
if l > High(listing) then
36863695
begin
3687-
writeln('Out of resources, LISTING');
3696+
WriteLn('Out of resources, LISTING');
36883697
halt;
36893698
end;
36903699

@@ -3813,7 +3822,7 @@ procedure asm65(a: String = ''; comment: String = '');
38133822
begin
38143823

38153824
{$IFDEF OPTIMIZECODE}
3816-
optimize_code := true;
3825+
optimize_code := True;
38173826
{$ELSE}
38183827
optimize_code := False;
38193828
{$ENDIF}

0 commit comments

Comments
 (0)