Ignore:
Timestamp:
Dec 4, 2014, 2:59:28 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Now commands cmInc, cmDec, cmPointerInc, cmPointerDec and cmSet use numeric parameter to merge multiple small steps to one unary operation with numeric parameter. Optimization is done on TTarget side and not on each specific targets.
  • Added: Optimization to eliminate redundant source code.
  • Added: Optimization level option in Options dialog.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Target/UTargetInterpretter.pas

    r52 r64  
    3333    FThread: TTargetInterpretterThread;
    3434    FStepCount: Integer;
    35     FCommandTable: array[TBrainFuckCommand] of TCommandHandler;
     35    FCommandTable: array[TMachineCommand] of TCommandHandler;
    3636    function GetMemorySize: Integer;
    3737    procedure SetMemorySize(AValue: Integer);
     
    4646    procedure CommandLoopStart;
    4747    procedure CommandLoopEnd;
     48    procedure CommandSet;
    4849    procedure PrepareBreakPoints;
    4950  protected
     
    8182
    8283const
    83   BrainFuckCommandText: array[TBrainFuckCommand] of Char = (
    84     ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@');
     84  BrainFuckCommandText: array[TMachineCommand] of Char = (
     85    ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=');
    8586
    8687
     
    9596  SMemoryCellOutOfRange = 'Memory cell %s value out of range';
    9697  SProgramNotRunning = 'Program not running';
     98  SUnsupportedCommand = 'Unsupported command';
    9799
    98100{ TTargetInterpretterThread }
     
    111113          SetStateSafe(rsPaused);
    112114        end else begin
    113           FCommandTable[FProgram[FProgramIndex]];
     115          if Assigned(FCommandTable[FProgram[FProgramIndex].Command]) then
     116            FCommandTable[FProgram[FProgramIndex].Command]
     117            else raise Exception.Create(SUnsupportedCommand);
    114118          Inc(FProgramIndex);
    115119          Inc(FStepCount);
     
    177181  SetLength(Loop, 0);
    178182  for I := 0 to Length(FProgram) - 1 do begin
    179     case FProgram[I] of
     183    case FProgram[I].Command of
    180184      cmLoopStart: begin
    181185        SetLength(Loop, Length(Loop) + 1);
     
    194198end;
    195199
    196 procedure TTargetInterpretter.CommandInc;
    197 begin
    198   Memory[MemoryPosition] := ((Memory[MemoryPosition] + 1) mod CellSize);
    199 end;
    200 
    201 procedure TTargetInterpretter.CommandDec;
    202 begin
    203   Memory[MemoryPosition] := ((Memory[MemoryPosition] - 1) mod CellSize);
    204 end;
    205 
    206 procedure TTargetInterpretter.CommandPointerInc;
    207 begin
    208   if MemoryPosition < MemorySize then Inc(MemoryPosition)
    209     else raise Exception.Create(SProgramUpperLimit);
    210 end;
    211 
    212 procedure TTargetInterpretter.CommandPointerDec;
    213 begin
    214   if MemoryPosition > 0 then Dec(MemoryPosition)
    215     else raise Exception.Create(SProgramLowerLimit);
    216 end;
    217 
    218200procedure TTargetInterpretter.CommandInput;
    219201begin
     
    245227  if Memory[MemoryPosition] > 0 then
    246228    FProgramIndex := SourceJump[FProgramIndex] - 1;
     229end;
     230
     231procedure TTargetInterpretter.CommandInc;
     232begin
     233  Memory[MemoryPosition] := ((Memory[MemoryPosition] + FProgram[FProgramIndex].Parameter) mod CellSize);
     234end;
     235
     236procedure TTargetInterpretter.CommandDec;
     237begin
     238  Memory[MemoryPosition] := ((Memory[MemoryPosition] - FProgram[FProgramIndex].Parameter) mod CellSize);
     239end;
     240
     241procedure TTargetInterpretter.CommandPointerInc;
     242begin
     243  if MemoryPosition < MemorySize then Inc(MemoryPosition, FProgram[FProgramIndex].Parameter)
     244    else raise Exception.Create(SProgramUpperLimit);
     245end;
     246
     247procedure TTargetInterpretter.CommandPointerDec;
     248begin
     249  if MemoryPosition > 0 then Dec(MemoryPosition, FProgram[FProgramIndex].Parameter)
     250    else raise Exception.Create(SProgramLowerLimit);
     251end;
     252
     253procedure TTargetInterpretter.CommandSet;
     254begin
     255  Memory[MemoryPosition] := FProgram[FProgramIndex].Parameter mod CellSize;
    247256end;
    248257
     
    286295  I: Integer;
    287296begin
    288   SetLength(Result, Length(FProgram));
    289   for I := 0 to Length(FProgram) - 1 do
    290     Result[I + 1] := BrainFuckCommandText[FProgram[I]];
     297  Result := '';
     298  for I := 0 to Length(FProgram) - 1 do begin
     299    Result := Result + BrainFuckCommandText[FProgram[I].Command];
     300    if FProgram[I].Command in [cmInc, cmDec, cmSet, cmPointerInc, cmPointerDec] then begin
     301      if FProgram[I].Parameter > 1 then
     302        Result := Result + IntToStr(FProgram[I].Parameter);
     303    end;
     304  end;
    291305end;
    292306
     
    402416  MemorySize := 30000;
    403417  CellSize := 256;
     418  // Base commands
    404419  FCommandTable[cmInc] := CommandInc;
    405420  FCommandTable[cmDec] := CommandDec;
     
    410425  FCommandTable[cmLoopStart] := CommandLoopStart;
    411426  FCommandTable[cmLoopEnd] := CommandLoopEnd;
     427  // Extended commands
     428  FCommandTable[cmSet] := CommandSet;
    412429end;
    413430
Note: See TracChangeset for help on using the changeset viewer.