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

    r82 r86  
    1414  TTargetPython = class(TBFTarget)
    1515  private
     16    function GetMemoryCell: string;
    1617  public
    1718    constructor Create; override;
     
    4243  ExecutorPath := '/usr/bin/python';
    4344  {$ENDIF}
     45end;
     46
     47function TTargetPython.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 + ']';
    4455end;
    4556
     
    97108      cmPointerInc: AddLine('position += ' + IntToStr(FProgram[FProgramIndex].Parameter));
    98109      cmPointerDec: AddLine('position -= ' + IntToStr(FProgram[FProgramIndex].Parameter));
    99       cmInc: AddLine('memory[position] += ' + IntToStr(FProgram[FProgramIndex].Parameter));
    100       cmDec: AddLine('memory[position] -= ' + IntToStr(FProgram[FProgramIndex].Parameter));
     110      cmInc: AddLine(GetMemoryCell + ' += ' + IntToStr(FProgram[FProgramIndex].Parameter));
     111      cmDec: AddLine(GetMemoryCell + ' -= ' + IntToStr(FProgram[FProgramIndex].Parameter));
    101112      cmOutput: begin
    102         AddLine('sys.stdout.write(chr(memory[position]))');
     113        AddLine('sys.stdout.write(chr(' + GetMemoryCell + '))');
    103114        AddLine('sys.stdout.flush()');
    104115      end;
    105       cmInput: AddLine('memory[position] = ord(getchar())');
    106       cmSet: AddLine('memory[position] = ' + IntToStr(FProgram[FProgramIndex].Parameter));
     116      cmInput: AddLine(GetMemoryCell + ' = ord(getchar())');
     117      cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter));
     118      cmMultipy: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + memory[position] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    107119      cmLoopStart: begin
    108         AddLine('while(memory[position] != 0):');
     120        AddLine('while(' + GetMemoryCell + ' != 0):');
    109121        Inc(Indent);
    110122      end;
Note: See TracChangeset for help on using the changeset viewer.