Changeset 64 for trunk/Target


Ignore:
Timestamp:
Dec 4, 2014, 2:59:28 PM (9 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.
Location:
trunk/Target
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Target/UTargetC.pas

    r60 r64  
    6565  FProgramIndex := 0;
    6666  while (FProgramIndex < Length(FProgram)) do begin
    67     case FProgram[FProgramIndex] of
    68       cmPointerInc: begin
    69         Sum := CheckOccurence(cmPointerInc);
    70         AddLine('Pos = Pos + ' + IntToStr(Sum) + ';');
    71       end;
    72       cmPointerDec: begin
    73         Sum := CheckOccurence(cmPointerDec);
    74         AddLine('Pos = Pos - ' + IntToStr(Sum) + ';');
    75       end;
    76       cmInc: begin
    77         Sum := CheckOccurence(cmInc);
    78         AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(Sum) + ';');
    79       end;
    80       cmDec: begin
    81         Sum := CheckOccurence(cmDec);
    82         AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(Sum) + ';');
    83       end;
     67    case FProgram[FProgramIndex].Command of
     68      cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     69      cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     70      cmInc: AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     71      cmDec: AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    8472      cmOutput: AddLine('putchar(Memory[Pos]);');
    8573      cmInput: AddLine('Memory[Pos] = getchar();');
     74      cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    8675      cmLoopStart: begin
    87         if CheckClear then begin
    88           AddLine('Memory[Pos] = 0;');
    89           Inc(FProgramIndex, 2);
    90         end else begin
    91           AddLine('while(Memory[Pos] != 0)');
    92           AddLine('{');
    93           Inc(Indent);
    94         end;
     76        AddLine('while(Memory[Pos] != 0)');
     77        AddLine('{');
     78        Inc(Indent);
    9579      end;
    9680      cmLoopEnd: begin
  • trunk/Target/UTargetDelphi.pas

    r60 r64  
    5959  FProgramIndex := 0;
    6060  while (FProgramIndex < Length(FProgram)) do begin
    61     case FProgram[FProgramIndex] of
    62       cmPointerInc: begin
    63         Sum := CheckOccurence(cmPointerInc);
    64         AddLine('Inc(Pos, ' + IntToStr(Sum) + ');');
    65       end;
    66       cmPointerDec: begin
    67         Sum := CheckOccurence(cmPointerDec);
    68         AddLine('Dec(Pos, ' + IntToStr(Sum) + ');');
    69       end;
    70       cmInc: begin
    71         Sum := CheckOccurence(cmInc);
    72         AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';');
    73       end;
    74       cmDec: begin
    75         Sum := CheckOccurence(cmDec);
    76         AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';');
    77       end;
     61    case FProgram[FProgramIndex].Command of
     62      cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     63      cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     64      cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     65      cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     66      cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    7867      cmOutput: AddLine('Write(Chr(Memory[Pos]));');
    7968      cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);');
    8069      cmLoopStart: begin
    81         if CheckClear then begin
    82           AddLine('Memory[Pos] := 0;');
    83           Inc(FProgramIndex, 2);
    84         end else begin
    85           AddLine('while Memory[Pos] <> 0 do begin');
    86           Inc(Indent);
    87         end;
     70        AddLine('while Memory[Pos] <> 0 do begin');
     71        Inc(Indent);
    8872      end;
    8973      cmLoopEnd: begin
  • trunk/Target/UTargetFPC.pas

    r60 r64  
    4444
    4545procedure TTargetFPC.Compile;
    46 var
    47   Sum: Integer;
    4846begin
    4947  inherited;
     
    6260  FProgramIndex := 0;
    6361  while (FProgramIndex < Length(FProgram)) do begin
    64     case FProgram[FProgramIndex] of
    65       cmPointerInc: begin
    66         Sum := CheckOccurence(cmPointerInc);
    67         AddLine('Inc(Pos, ' + IntToStr(Sum) + ');');
    68       end;
    69       cmPointerDec: begin
    70         Sum := CheckOccurence(cmPointerDec);
    71         AddLine('Dec(Pos, ' + IntToStr(Sum) + ');');
    72       end;
    73       cmInc: begin
    74         Sum := CheckOccurence(cmInc);
    75         AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';');
    76       end;
    77       cmDec: begin
    78         Sum := CheckOccurence(cmDec);
    79         AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';');
    80       end;
     62    case FProgram[FProgramIndex].Command of
     63      cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     64      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) + ';');
    8168      cmOutput: AddLine('Write(Chr(Memory[Pos]));');
    8269      cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);');
    8370      cmLoopStart: begin
    84         if CheckClear then begin
    85           AddLine('Memory[Pos] := 0;');
    86           Inc(FProgramIndex, 2);
    87         end else begin
    88           AddLine('while Memory[Pos] <> 0 do begin');
    89           Inc(Indent);
    90         end;
     71        AddLine('while Memory[Pos] <> 0 do begin');
     72        Inc(Indent);
    9173      end;
    9274      cmLoopEnd: begin
  • 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
  • trunk/Target/UTargetJava.pas

    r60 r64  
    6666  FProgramIndex := 0;
    6767  while (FProgramIndex < Length(FProgram)) do begin
    68     case FProgram[FProgramIndex] of
    69       cmPointerInc: begin
    70         Sum := CheckOccurence(cmPointerInc);
    71         AddLine('Pos = Pos + ' + IntToStr(Sum) + ';');
    72       end;
    73       cmPointerDec: begin
    74         Sum := CheckOccurence(cmPointerDec);
    75         AddLine('Pos = Pos - ' + IntToStr(Sum) + ';');
    76       end;
    77       cmInc: begin
    78         Sum := CheckOccurence(cmInc);
    79         AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(Sum) + ');');
    80       end;
    81       cmDec: begin
    82         Sum := CheckOccurence(cmDec);
    83         AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(Sum) + ');');
    84       end;
     68    case FProgram[FProgramIndex].Command of
     69      cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     70      cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     71      cmInc: AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     72      cmDec: AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    8573      cmOutput: AddLine('System.out.print(Memory[Pos]);');
    8674      cmInput: AddLine('Memory[Pos] = (char)System.in.read();');
     75      cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    8776      cmLoopStart: begin
    88         if CheckClear then begin
    89           AddLine('Memory[Pos] = 0;');
    90           Inc(FProgramIndex, 2);
    91         end else begin
    92           AddLine('while(Memory[Pos] != 0)');
    93           AddLine('{');
    94           Inc(Indent);
    95         end;
     77        AddLine('while(Memory[Pos] != 0)');
     78        AddLine('{');
     79        Inc(Indent);
    9680      end;
    9781      cmLoopEnd: begin
  • trunk/Target/UTargetPHP.pas

    r60 r64  
    5858  FProgramIndex := 0;
    5959  while (FProgramIndex < Length(FProgram)) do begin
    60     case FProgram[FProgramIndex] of
    61       cmPointerInc: begin
    62         Sum := CheckOccurence(cmPointerInc);
    63         AddLine('$Position = $Position + ' + IntToStr(Sum) + ';');
    64       end;
    65       cmPointerDec: begin
    66         Sum := CheckOccurence(cmPointerDec);
    67         AddLine('$Position = $Position - ' + IntToStr(Sum) + ';');
    68       end;
    69       cmInc: begin
    70         Sum := CheckOccurence(cmInc);
    71         AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(Sum) + ');');
    72       end;
    73       cmDec: begin
    74         Sum := CheckOccurence(cmDec);
    75         AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(Sum) + ');');
    76       end;
     60    case FProgram[FProgramIndex].Command of
     61      cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     62      cmPointerDec: AddLine('$Position = $Position - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     63      cmInc: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     64      cmDec: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    7765      cmOutput: AddLine('echo($Memory[$Position]);');
    7866      cmInput: AddLine('$Memory[$Position] = fgetc(STDIN);');
     67      cmSet: AddLine('$Memory[$Position] = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    7968      cmLoopStart: begin
    80         if CheckClear then begin
    81           AddLine('$Memory[$Position] = "\0";');
    82           Inc(FProgramIndex, 2);
    83         end else begin
    84           AddLine('while($Memory[$Position] != "\0") {');
    85           Inc(Indent);
    86         end;
     69        AddLine('while($Memory[$Position] != "\0") {');
     70        Inc(Indent);
    8771      end;
    8872      cmLoopEnd: begin
Note: See TracChangeset for help on using the changeset viewer.