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/UTargetInterpretter.pas

    r80 r86  
    4444    procedure CommandLoopEnd;
    4545    procedure CommandSet;
     46    procedure CommandMultiply;
    4647    procedure PrepareBreakPoints;
    4748  protected
     
    7576const
    7677  BrainFuckCommandText: array[TMachineCommand] of Char = (
    77     ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=');
     78    ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=', '*');
    7879
    7980
     
    189190  end;
    190191  if InputPosition <= Length(Input) then begin
    191     Memory[MemoryPosition] := Ord(Input[InputPosition]);
     192    Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] :=
     193      Ord(Input[InputPosition]);
    192194    Inc(InputPosition);
    193195  end;
     
    198200  if OutputPosition > Length(Output) then
    199201    SetLength(Output, Length(Output) + 1 + Length(Output) div 4);
    200   Output[OutputPosition] := Char(Memory[MemoryPosition]);
     202  Output[OutputPosition] := Char(Memory[MemoryPosition +
     203    FProgram[FProgramIndex].RelIndex]);
    201204  Inc(OutputPosition);
    202205end;
     
    204207procedure TTargetInterpretter.CommandLoopStart;
    205208begin
    206   if Memory[MemoryPosition] = 0 then
     209  if Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] = 0 then
    207210    FProgramIndex := FProgram[FProgramIndex].Parameter;
    208211end;
     
    210213procedure TTargetInterpretter.CommandLoopEnd;
    211214begin
    212   if Memory[MemoryPosition] > 0 then
     215  if Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] > 0 then
    213216    FProgramIndex := FProgram[FProgramIndex].Parameter - 1;
    214217end;
     
    216219procedure TTargetInterpretter.CommandInc;
    217220begin
    218   Memory[MemoryPosition] := ((Memory[MemoryPosition] + FProgram[FProgramIndex].Parameter) mod CellSize);
     221  Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] :=
     222    ((Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] +
     223    FProgram[FProgramIndex].Parameter) mod CellSize);
    219224end;
    220225
    221226procedure TTargetInterpretter.CommandDec;
    222227begin
    223   Memory[MemoryPosition] := ((Memory[MemoryPosition] - FProgram[FProgramIndex].Parameter) mod CellSize);
     228  Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] :=
     229    ((Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] -
     230    FProgram[FProgramIndex].Parameter) mod CellSize);
    224231end;
    225232
     
    240247procedure TTargetInterpretter.CommandSet;
    241248begin
    242   Memory[MemoryPosition] := FProgram[FProgramIndex].Parameter mod CellSize;
     249  Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] :=
     250    FProgram[FProgramIndex].Parameter mod CellSize;
     251end;
     252
     253procedure TTargetInterpretter.CommandMultiply;
     254begin
     255  Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] :=
     256    (Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] +
     257    Memory[MemoryPosition] * FProgram[FProgramIndex].Parameter) mod CellSize;
    243258end;
    244259
     
    411426  // Extended commands
    412427  FCommandTable[cmSet] := CommandSet;
     428  FCommandTable[cmMultipy] := CommandMultiply;
    413429end;
    414430
Note: See TracChangeset for help on using the changeset viewer.