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

    r72 r86  
    1414  TTargetPHP = class(TBFTarget)
    1515  private
     16    function GetMemoryCell: string;
    1617  public
    1718    constructor Create; override;
     
    4445end;
    4546
     47function TTargetPHP.GetMemoryCell: string;
     48begin
     49  Result := '$Memory[$Position';
     50  if FProgram[FProgramIndex].RelIndex > 0 then
     51    Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
     52  else if FProgram[FProgramIndex].RelIndex < 0 then
     53    Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
     54  Result := Result + ']';
     55end;
     56
    4657procedure TTargetPHP.Compile;
    4758begin
     
    5970      cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    6071      cmPointerDec: AddLine('$Position = $Position - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    61       cmInc: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    62       cmDec: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    63       cmOutput: AddLine('echo($Memory[$Position]);');
    64       cmInput: AddLine('$Memory[$Position] = fgetc(STDIN);');
    65       cmSet: AddLine('$Memory[$Position] = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     72      cmInc: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ' +
     73        IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     74      cmDec: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') - ' +
     75        IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     76      cmOutput: AddLine('echo(' + GetMemoryCell + ');');
     77      cmInput: AddLine(GetMemoryCell + ' = fgetc(STDIN);');
     78      cmSet: AddLine(GetMemoryCell + ' = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     79      cmMultipy: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ord($Memory[$Position]) * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    6680      cmLoopStart: begin
    67         AddLine('while($Memory[$Position] != "\0") {');
     81        AddLine('while(' + GetMemoryCell + ' != "\0") {');
    6882        Inc(Indent);
    6983      end;
Note: See TracChangeset for help on using the changeset viewer.