Ignore:
Timestamp:
Aug 29, 2017, 5:12:18 PM (7 years ago)
Author:
chronos
Message:
  • Added: New optimization using relative indexes to eliminate lots of pointer inc/dec operations.
  • Added: New command "multiply" extended command to replace while loops used for cell multiplication. Also added related optimization.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Target/UTargetFPC.pas

    r66 r86  
    1414  TTargetFPC = class(TBFTarget)
    1515  private
     16    function GetMemoryCell: string;
    1617  public
    1718    constructor Create; override;
     
    4344end;
    4445
     46function TTargetFPC.GetMemoryCell: string;
     47begin
     48  Result := 'Memory[Pos';
     49  if FProgram[FProgramIndex].RelIndex > 0 then
     50    Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
     51  else if FProgram[FProgramIndex].RelIndex < 0 then
     52    Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
     53  Result := Result + ']';
     54end;
     55
    4556procedure TTargetFPC.Compile;
    4657begin
     
    5061
    5162  AddLine('program ' + ProgramName + ';');
    52   AddLine('');
    5363  AddLine('var');
    5464  AddLine('  Memory: array[0..30000] of Byte;');
     
    6373      cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    6474      cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    65       cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    66       cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    67       cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    68       cmOutput: AddLine('Write(Chr(Memory[Pos]));');
    69       cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);');
     75      cmInc: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     76      cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     77      cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     78      cmMultipy: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     79      cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));');
     80      cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);');
    7081      cmLoopStart: begin
    71         AddLine('while Memory[Pos] <> 0 do begin');
     82        AddLine('while ' + GetMemoryCell + ' <> 0 do begin');
    7283        Inc(Indent);
    7384      end;
Note: See TracChangeset for help on using the changeset viewer.