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

    r72 r86  
    1515  TTargetC = class(TBFTarget)
    1616  private
     17    function GetMemoryCell: string;
    1718  public
    1819    constructor Create; override;
     
    4647end;
    4748
     49function TTargetC.GetMemoryCell: string;
     50begin
     51  Result := 'Memory[Pos';
     52  if FProgram[FProgramIndex].RelIndex > 0 then
     53    Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
     54  else if FProgram[FProgramIndex].RelIndex < 0 then
     55    Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
     56  Result := Result + ']';
     57end;
     58
    4859procedure TTargetC.Compile;
    4960begin
     
    6778      cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    6879      cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    69       cmInc: AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    70       cmDec: AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
    71       cmOutput: AddLine('putchar(Memory[Pos]);');
    72       cmInput: AddLine('Memory[Pos] = getchar();');
    73       cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     80      cmInc: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     81      cmDec: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     82      cmOutput: AddLine('putchar(' + GetMemoryCell + ');');
     83      cmInput: AddLine(GetMemoryCell + ' = getchar();');
     84      cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
     85      cmMultipy: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + 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.