Skip to content

Commit dcef00d

Browse files
authored
Merge pull request #212 from peterdell/way2js4
Way2js4 - Fix VarDataSize computation for ENUM and add trace file
2 parents a8ab57c + 82b614f commit dcef00d

16 files changed

+1155
-732
lines changed

origin/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.bak
2+
*.exe
3+
*.xex
4+
*.tmp
5+
*.log
6+
*.lps
7+
backup/
8+
lib/

origin/Common.pas

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ TIdentifier = record
567567
NumDefines: integer = 1; // NumDefines = AddDefines
568568

569569
NumTok, NumIdent, NumTypes, NumPredefIdent, NumStaticStrChars, NumUnits, NumBlocks, NumProc,
570-
BlockStackTop, CodeSize, CodePosStackTop, BreakPosStackTop, VarDataSize, Pass, ShrShlCnt,
570+
BlockStackTop, CodeSize, CodePosStackTop, BreakPosStackTop, _VarDataSize, Pass, ShrShlCnt,
571571
NumStaticStrCharsTmp, AsmBlockIndex, IfCnt, CaseCnt, IfdefLevel, run_func: Integer;
572572

573573
iOut: integer = -1;
@@ -675,12 +675,64 @@ TIdentifier = record
675675

676676
function StrToInt(const a: string): Int64;
677677

678+
type TTokenIndex = Integer;
679+
procedure IncVarDataSize(const tokenIndex: TTokenIndex; const size: Integer);
680+
681+
function GetVarDataSize: Integer;
682+
procedure SetVarDataSize(const tokenIndex: TTokenIndex; const size: Integer);
683+
684+
var TraceFile: TextFile;
685+
procedure LogTrace(message: String);
686+
678687
// ----------------------------------------------------------------------------
679688

680689
implementation
681690

682691
uses SysUtils, Messages;
683692

693+
procedure LogTrace(message: String);
694+
begin
695+
{$IFDEF USETRACEFILE}
696+
Writeln(traceFile, message);
697+
{$ENDIF}
698+
end;
699+
700+
// ----------------------------------------------------------------------------
701+
// ----------------------------------------------------------------------------
702+
703+
function GetVarDataSize: Integer;
704+
begin
705+
Result := _VarDataSize;
706+
end;
707+
708+
709+
procedure SetVarDataSize(const tokenIndex: TTokenIndex; const size: Integer);
710+
var token: TToken;
711+
// var GetSourceFileLocationString: String;
712+
713+
begin
714+
_VarDataSize := size;
715+
token:= Tok[tokenIndex];
716+
717+
(*
718+
GetSourceFileLocationString := UnitName[ token.UnitIndex].Path;
719+
720+
if (token.line>0) then
721+
begin
722+
GetSourceFileLocationString:=GetSourceFileLocationString+ ' ( line ' + IntToStr(token.Line) + ', column ' + IntToStr(token.Column) + ')';
723+
end;
724+
725+
726+
// LogTrace(Format('SetVarDataSize: TokenIndex=%d: %s %s VarDataSize=%d', [tokenIndex, GetSourceFileLocationString,'TODO', _VarDataSize]));
727+
*)
728+
end;
729+
730+
731+
procedure IncVarDataSize(const tokenIndex: TTokenIndex; const size: Integer);
732+
begin
733+
SetVarDataSize(tokenIndex, _VarDataSize + size);
734+
end;
735+
684736
// ----------------------------------------------------------------------------
685737

686738

origin/Parser.pas

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ procedure SaveToDataSegment(ConstDataSize: integer; ConstVal: Int64; ConstValTyp
455455
var ftmp: TFloat;
456456
begin
457457

458+
// LogTrace(Format('SaveToDataSegment(index=%d, value=%d, valueDataType=%d', [ConstDataSize, ConstVal, ConstValType]));
459+
460+
458461
if (ConstDataSize < 0) or (ConstDataSize > $FFFF) then begin writeln('SaveToDataSegment: ', ConstDataSize); halt end;
459462

460463
ftmp:=Default(TFloat);
@@ -1747,10 +1750,10 @@ procedure DefineIdent(ErrTokenIndex: Integer; Name: TString; Kind: Byte; DataTyp
17471750
if Ident[NumIdent].isAbsolute then
17481751
Ident[NumIdent].Value := Data - 1
17491752
else
1750-
Ident[NumIdent].Value := DATAORIGIN + VarDataSize; // Variable address
1753+
Ident[NumIdent].Value := DATAORIGIN + GetVarDataSize; // Variable address
17511754

17521755
if not OutputDisabled then
1753-
VarDataSize := VarDataSize + DataSize[DataType];
1756+
IncVarDataSize( ErrTokenIndex,DataSize[DataType]);
17541757

17551758
Ident[NumIdent].NumAllocElements := NumAllocElements; // Number of array elements (0 for single variable)
17561759
Ident[NumIdent].NumAllocElements_ := NumAllocElements_;
@@ -1760,28 +1763,28 @@ procedure DefineIdent(ErrTokenIndex: Integer; Name: TString; Kind: Byte; DataTyp
17601763
if not OutputDisabled then begin
17611764

17621765
if (DataType = POINTERTOK) and (AllocElementType in [RECORDTOK, OBJECTTOK]) and (NumAllocElements_ = 0) then
1763-
inc(VarDataSize, DataSize[POINTERTOK])
1766+
IncVarDataSize( ErrTokenIndex, DataSize[POINTERTOK])
17641767
else
17651768

17661769
if DataType in [ENUMTYPE] then
1767-
inc(VarDataSize)
1770+
IncVarDataSize( ErrTokenIndex,1)
17681771
else
17691772
if (DataType in [RECORDTOK, OBJECTTOK]) and (NumAllocElements > 0) then
1770-
VarDataSize := VarDataSize + 0
1773+
IncVarDataSize( ErrTokenIndex, 0)
17711774
else
17721775
if (DataType in [FILETOK, TEXTFILETOK]) and (NumAllocElements > 0) then
1773-
VarDataSize := VarDataSize + 12
1776+
IncVarDataSize( ErrTokenIndex, 12)
17741777
else begin
17751778

17761779
if (Ident[NumIdent].idType = ARRAYTOK) and (Ident[NumIdent].isAbsolute = false) and (Elements(NumIdent) = 1) then // [0..0] ; [0..0, 0..0]
17771780

17781781
else
1779-
VarDataSize := VarDataSize + integer(Elements(NumIdent) * DataSize[AllocElementType]);
1782+
IncVarDataSize( ErrTokenIndex, integer(Elements(NumIdent) * DataSize[AllocElementType]));
17801783

17811784
end;
17821785

17831786

1784-
if NumAllocElements > 0 then dec(VarDataSize, DataSize[DataType]);
1787+
if NumAllocElements > 0 then IncVarDataSize( ErrTokenIndex,-DataSize[DataType]);
17851788

17861789
end;
17871790

origin/Scanner.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ procedure TokenizeProgramInitialization;
4747
SetLength(msgWarning, 1);
4848
SetLength(msgNote, 1);
4949

50-
NumBlocks := 0; BlockStackTop := 0; CodeSize := 0; CodePosStackTop := 0; VarDataSize := 0;
50+
NumBlocks := 0; BlockStackTop := 0; CodeSize := 0; CodePosStackTop := 0; SetVarDataSize(0,0);
5151
CaseCnt := 0; IfCnt := 0; ShrShlCnt := 0; NumTypes := 0; run_func := 0; NumProc := 0;
5252
NumTok := 0; NumIdent := 0;
5353

origin/define.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
//{$DEFINE WHILEDO}
22

33
//{$DEFINE USEOPTFILE}
4+
//{$DEFINE USETRACEFILE}
45

56
{$DEFINE OPTIMIZECODE}
67

8+
// The origin version still relies on accessing the element [0] of arrays starting at 1.
9+
{$RANGECHECKS OFF}
10+
711
{$I+}

0 commit comments

Comments
 (0)