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

    r72 r86  
    1414  TTargetJava = class(TBFTarget)
    1515  private
     16    function GetMemoryCell: string;
    1617  public
    1718    constructor Create; override;
     
    4445end;
    4546
     47function TTargetJava.GetMemoryCell: string;
     48begin
     49  Result := 'Memory[Pos';
     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 TTargetJava.Compile;
    4758begin
     
    6778      cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    6879      cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    69       cmInc: AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    70       cmDec: AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    71       cmOutput: AddLine('System.out.print(Memory[Pos]);');
    72       cmInput: AddLine('Memory[Pos] = (char)System.in.read();');
    73       cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     80      cmInc: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     81      cmDec: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
     82      cmOutput: AddLine('System.out.print(' + GetMemoryCell + ');');
     83      cmInput: AddLine(GetMemoryCell + ' = (char)System.in.read();');
     84      cmSet: AddLine(GetMemoryCell + ' = (char)' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     85      cmMultipy: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + (int)Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
    7486      cmLoopStart: begin
    75         AddLine('while(Memory[Pos] != 0)');
     87        AddLine('while(' + GetMemoryCell + ' != 0)');
    7688        AddLine('{');
    7789        Inc(Indent);
Note: See TracChangeset for help on using the changeset viewer.