Changeset 114 for trunk


Ignore:
Timestamp:
May 18, 2019, 12:13:44 AM (6 years ago)
Author:
chronos
Message:
  • Modified: Improved stepping through source and target code. Each step has source, program and target index.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r113 r114  
    226226    AProgramShowExecutionPoint.Execute;
    227227  if Core.CurrentTarget.State = rsStopped then
    228   Core.CurrentTarget.Messages.AddMessage(SProgramStopped);
     228    Core.CurrentTarget.Messages.AddMessage(SProgramStopped);
    229229end;
    230230
     
    487487      Core.ThemeManager.UseTheme(Self);
    488488      FormSourceCode.UpdateTheme;
     489      Core.SaveToRegistry(Core.ApplicationInfo.GetRegistryContext);
     490      SaveToRegistry(Core.ApplicationInfo.GetRegistryContext);
    489491    end;
    490492  finally
     
    523525  DebugStep := Core.CurrentTarget.DebugSteps.SearchBySourcePos(FormSourceCode.SynEditSource.SelStart - 1);
    524526  if Assigned(DebugStep) then begin
    525     BreakPoint := Core.CurrentTarget.BreakPoints.SearchByTargetPos(DebugStep.TargetPosition);
     527    BreakPoint := Core.CurrentTarget.BreakPoints.SearchByTargetPos(DebugStep.ProgramPosition);
    526528    if Assigned(BreakPoint) then
    527529      Core.CurrentTarget.BreakPoints.Delete(Core.CurrentTarget.BreakPoints.IndexOf(BreakPoint))
    528       else Core.CurrentTarget.BreakPoints.AddItem(DebugStep.TargetPosition);
     530      else Core.CurrentTarget.BreakPoints.AddItem(DebugStep.ProgramPosition);
    529531  end;
    530532end;
     
    555557    if not Core.CurrentTarget.Compiled then AProgramCompile.Execute;
    556558    DebugStep := Core.CurrentTarget.DebugSteps.SearchBySourcePos(FormSourceCode.SynEditSource.SelStart - 1);
    557     Core.CurrentTarget.BreakPoints.SetSystem(DebugStep.TargetPosition);
     559    Core.CurrentTarget.BreakPoints.SetSystem(DebugStep.ProgramPosition);
    558560    AProgramRun.Execute;
    559561  end else begin
    560562    DebugStep := Core.CurrentTarget.DebugSteps.SearchBySourcePos(FormSourceCode.SynEditSource.SelStart - 1);
    561     Core.CurrentTarget.RunToCursor(DebugStep.TargetPosition);
     563    Core.CurrentTarget.RunToCursor(DebugStep.ProgramPosition);
    562564  end;
    563565end;
     
    567569  DebugStep: TDebugStep;
    568570begin
    569   DebugStep := Core.CurrentTarget.DebugSteps.SearchByTargetPos(Core.CurrentTarget.ExecutionPosition);
     571  DebugStep := Core.CurrentTarget.DebugSteps.SearchByProgramPos(Core.CurrentTarget.ExecutionPosition);
    570572  FormSourceCode.SynEditSource.SelStart := DebugStep.SourcePosition + 1;
    571573  FormTargetCode.SynEditTarget.SelStart := DebugStep.TargetPosition + 1;
     
    578580  if Core.CurrentTarget.State = rsStopped then begin
    579581    if not Core.CurrentTarget.Compiled then AProgramCompile.Execute;
    580     Core.CurrentTarget.BreakPoints.SetSystem(TDebugStep(Core.CurrentTarget.DebugSteps.First).TargetPosition);
     582    Core.CurrentTarget.BreakPoints.SetSystem(TDebugStep(Core.CurrentTarget.DebugSteps.First).ProgramPosition);
    581583    AProgramRun.Execute;
    582584  end else Core.CurrentTarget.StepInto;
     
    592594  if Core.CurrentTarget.State = rsStopped then begin
    593595    if not Core.CurrentTarget.Compiled then AProgramCompile.Execute;
    594     Core.CurrentTarget.BreakPoints.SetSystem(TDebugStep(Core.CurrentTarget.DebugSteps.First).TargetPosition);
     596    Core.CurrentTarget.BreakPoints.SetSystem(TDebugStep(Core.CurrentTarget.DebugSteps.First).ProgramPosition);
    595597    AProgramRun.Execute;
    596598  end else Core.CurrentTarget.StepOver;
  • trunk/Target/UTargetInterpretter.pas

    r112 r114  
    7676  end;
    7777
    78 const
    79   BrainFuckCommandText: array[TMachineCommand] of Char = (
    80     ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=', '*');
    81 
    8278
    8379implementation
     
    321317  Result := '';
    322318  for I := 0 to Length(FProgram) - 1 do begin
    323     Result := Result + BrainFuckCommandText[FProgram[I].Command];
    324     if FProgram[I].Command in [cmInc, cmDec, cmPointerInc, cmPointerDec,
    325       cmSet, cmMultipy] then begin
    326       if FProgram[I].Parameter <> 1 then
    327         Result := Result + IntToStr(FProgram[I].Parameter);
    328     end;
    329     if FProgram[I].RelIndex <> 0 then
    330       Result := Result + 'R' + IntToStr(FProgram[I].RelIndex);
     319    Result := Result + GetOperationText(FProgram[I]);
    331320  end;
    332321end;
     
    369358begin
    370359  if State = rsPaused then begin
    371     Step := DebugSteps.SearchByTargetPos(FProgramIndex);
     360    Step := DebugSteps.SearchByProgramPos(FProgramIndex);
    372361    if Step.Operation = soStepOut then begin
    373       BreakPoints.SetSystem(Step.TargetPosition + 1);
    374       Step := DebugSteps.SearchByTargetPos(FProgram[Step.TargetPosition].Parameter);
    375       BreakPoints.AddSystem(Step.TargetPosition);
     362      BreakPoints.SetSystem(Step.ProgramPosition + 1);
     363      Step := DebugSteps.SearchByProgramPos(FProgram[Step.ProgramPosition].Parameter);
     364      BreakPoints.AddSystem(Step.ProgramPosition);
    376365    end else
    377366    if Step.Operation = soStepIn then begin
    378       BreakPoints.SetSystem(Step.TargetPosition + 1);
    379       Step := DebugSteps.SearchByTargetPos(FProgram[Step.TargetPosition].Parameter);
    380       BreakPoints.AddSystem(Step.TargetPosition);
    381     end else BreakPoints.SetSystem(Step.TargetPosition + 1);
     367      BreakPoints.SetSystem(Step.ProgramPosition + 1);
     368      Step := DebugSteps.SearchByProgramPos(FProgram[Step.ProgramPosition].Parameter);
     369      BreakPoints.AddSystem(Step.ProgramPosition);
     370    end else BreakPoints.SetSystem(Step.ProgramPosition + 1);
    382371    Run;
    383372  end else raise Exception.Create(SProgramNotRunning);
     
    389378begin
    390379  if State = rsPaused then begin
    391     Step := DebugSteps.SearchByTargetPos(FProgramIndex);
     380    Step := DebugSteps.SearchByProgramPos(FProgramIndex);
    392381    if Step.Operation = soStepOut then begin
    393       BreakPoints.SetSystem(Step.TargetPosition + 1);
    394       Step := DebugSteps.SearchByTargetPos(FProgram[Step.TargetPosition].Parameter);
    395       BreakPoints.AddSystem(Step.TargetPosition);
     382      BreakPoints.SetSystem(Step.ProgramPosition + 1);
     383      Step := DebugSteps.SearchByProgramPos(FProgram[Step.ProgramPosition].Parameter);
     384      BreakPoints.AddSystem(Step.ProgramPosition);
    396385    end else
    397386    if Step.Operation = soStepIn then begin
    398       Step := DebugSteps.SearchByTargetPos(FProgram[Step.TargetPosition].Parameter);
    399       BreakPoints.SetSystem(Step.TargetPosition + 1);
    400     end else BreakPoints.SetSystem(Step.TargetPosition + 1);
     387      Step := DebugSteps.SearchByProgramPos(FProgram[Step.ProgramPosition].Parameter);
     388      BreakPoints.SetSystem(Step.ProgramPosition + 1);
     389    end else BreakPoints.SetSystem(Step.ProgramPosition + 1);
    401390    Run;
    402391  end else raise Exception.Create(SProgramNotRunning);
     
    410399begin
    411400  if State = rsPaused then begin
    412     Step := DebugSteps.SearchByTargetPos(FProgramIndex);
     401    Step := DebugSteps.SearchByProgramPos(FProgramIndex);
    413402    StepIndex := DebugSteps.IndexOf(Step);
    414403    Nesting := 1;
     
    419408    end;
    420409    if StepIndex < DebugSteps.Count then begin
    421       Breakpoints.SetSystem(TDebugStep(DebugSteps[StepIndex]).TargetPosition);
     410      Breakpoints.SetSystem(TDebugStep(DebugSteps[StepIndex]).ProgramPosition);
    422411    end;
    423412    Run;
  • trunk/UBFTarget.pas

    r112 r114  
    4444    FProgram: array of TMachineOperation;
    4545    FProgramIndex: Integer;
     46    function GetOperationText(Operation: TMachineOperation): string; virtual;
    4647    procedure LoadProgram; override;
    4748  public
     
    5556  end;
    5657
     58const
     59  BrainFuckCommandText: array[TMachineCommand] of Char = (
     60    ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=', '*');
     61
    5762
    5863implementation
     
    98103  NewProgram: array of TMachineOperation;
    99104  NewProgramIndex: Integer;
     105  NewTargetPos: Integer;
     106  FirstIndex: Integer;
    100107begin
    101108  NewProgramIndex := 0;
     109  NewTargetPos := 0;
    102110  SetLength(NewProgram, Length(FProgram));
    103111
    104112  FProgramIndex := 0;
    105113  while (FProgramIndex < Length(FProgram)) do begin
     114    FirstIndex := FProgramIndex;
    106115    case FProgram[FProgramIndex].Command of
    107116      cmPointerInc: begin
     
    123132      else NewProgram[NewProgramIndex] := FProgram[FProgramIndex];
    124133    end;
    125     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     134    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTargetPos);
     135    Inc(NewTargetPos, Length(GetOperationText(NewProgram[NewProgramIndex])));
    126136    Inc(FProgramIndex);
    127137    Inc(NewProgramIndex);
     
    139149  NewProgramIndex: Integer;
    140150  PreviousCommand: TMachineCommand;
     151  FirstIndex: Integer;
     152  NewTextIndex: Integer;
    141153begin
    142154  // Merge together cmInc, cmDec, cmSet
     
    147159
    148160  FProgramIndex := 0;
     161  NewTextIndex := 0;
    149162  while (FProgramIndex < Length(FProgram)) do begin
     163    FirstIndex := FProgramIndex;
    150164    case FProgram[FProgramIndex].Command of
    151165      cmPointerInc: begin
     
    258272    end;
    259273    PreviousCommand := FProgram[FProgramIndex].Command;
    260     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     274    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTextIndex);
     275    Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgramIndex])));
    261276    Inc(FProgramIndex);
    262277    Inc(NewProgramIndex);
     
    281296  NewProgramIndex: Integer;
    282297  RelIndex: Integer;
     298  FirstIndex: Integer;
     299  NewTextIndex: Integer;
    283300begin
    284301  NewProgramIndex := 0;
     
    287304  RelIndex := 0;
    288305  FProgramIndex := 0;
     306  NewTextIndex := 0;
    289307  while (FProgramIndex < Length(FProgram)) do begin
     308    FirstIndex := FProgramIndex;
    290309    case FProgram[FProgramIndex].Command of
    291310      cmPointerInc: begin
     
    319338      else raise Exception.Create(Format('Unsupported command %d', [FProgram[FProgramIndex].Command]));
    320339    end;
    321     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     340    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTextIndex);
     341    Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgramIndex])));
    322342    Inc(FProgramIndex);
    323343    Inc(NewProgramIndex);
     
    340360  LoopStartIndex: Integer;
    341361  LoopStartIndexNew: Integer;
     362  FirstIndex: Integer;
     363  NewTextIndex: Integer;
    342364begin
    343365  NewProgramIndex := 0;
     
    347369  ProcessLoop := False;
    348370  FProgramIndex := 0;
     371  NewTextIndex := 0;
    349372  PointerChange := 0;
    350373  while (FProgramIndex < Length(FProgram)) do begin
     374    FirstIndex := FProgramIndex;
    351375    case FProgram[FProgramIndex].Command of
    352376      cmPointerInc: begin
     
    418442      else raise Exception.Create(Format('Unsupported command %d', [FProgram[FProgramIndex].Command]));
    419443    end;
    420     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     444    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTextIndex);
     445    Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgramIndex])));
    421446    Inc(FProgramIndex);
    422447    Inc(NewProgramIndex);
     
    428453  Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) *
    429454    Length(NewProgram));
     455end;
     456
     457function TBFTarget.GetOperationText(Operation: TMachineOperation): string;
     458begin
     459  Result := BrainFuckCommandText[Operation.Command];
     460  if Operation.Command in [cmInc, cmDec, cmPointerInc, cmPointerDec,
     461    cmSet, cmMultipy] then begin
     462  if Operation.Parameter <> 1 then
     463    Result := Result + IntToStr(Operation.Parameter);
     464  end;
     465  if Operation.RelIndex <> 0 then
     466    Result := Result + 'R' + IntToStr(Operation.RelIndex);
    430467end;
    431468
  • trunk/UTarget.pas

    r111 r114  
    2222  TDebugStep = class
    2323    SourcePosition: Integer;
     24    ProgramPosition: Integer;
    2425    TargetPosition: Integer;
    2526    Operation: TStepOperation;
     
    3031  TDebugStepList = class(TFPGObjectList<TDebugStep>)
    3132    function SearchBySourcePos(Pos: Integer): TDebugStep;
     33    function SearchByProgramPos(Pos: Integer): TDebugStep;
    3234    function SearchByTargetPos(Pos: Integer): TDebugStep;
     35    function SearchIndexByProgramPos(Pos: Integer): Integer;
    3336    procedure AddStep(SourcePos, TargetPos: Integer; Operation: TStepOperation);
    34     procedure UpdateTargetPos(Old, New: Integer);
     37    procedure UpdateTargetPos(OldProgramFrom, OldProgramTo, NewProgram, NewTarget: Integer);
    3538  end;
    3639
     
    233236end;
    234237
     238function TDebugStepList.SearchByProgramPos(Pos: Integer): TDebugStep;
     239var
     240  I: Integer;
     241begin
     242  I := 0;
     243  while (I < Count) and (TDebugStep(Items[I]).ProgramPosition < Pos) do Inc(I);
     244  if I < Count then Result := TDebugStep(Items[I])
     245    else Result := nil;
     246end;
     247
    235248function TDebugStepList.SearchByTargetPos(Pos: Integer
    236249  ): TDebugStep;
     
    244257end;
    245258
     259function TDebugStepList.SearchIndexByProgramPos(Pos: Integer): Integer;
     260var
     261  I: Integer;
     262begin
     263  I := 0;
     264  while (I < Count) and (TDebugStep(Items[I]).ProgramPosition < Pos) do Inc(I);
     265  if I < Count then Result := I
     266    else Result := -1;
     267end;
     268
    246269procedure TDebugStepList.AddStep(SourcePos, TargetPos: Integer;
    247270  Operation: TStepOperation);
     
    251274  NewItem := TDebugStep.Create;
    252275  NewItem.SourcePosition := SourcePos;
     276  NewItem.ProgramPosition := TargetPos;
    253277  NewItem.TargetPosition := TargetPos;
    254278  NewItem.Operation := Operation;
     
    256280end;
    257281
    258 procedure TDebugStepList.UpdateTargetPos(Old, New: Integer);
    259 var
    260   I: Integer;
    261 begin
    262   I := 0;
    263   while (I < Count) do begin
    264     if TDebugStep(Items[I]).TargetPosition = Old then
    265       TDebugStep(Items[I]).TargetPosition := New;
    266     Inc(I);
    267   end;
     282procedure TDebugStepList.UpdateTargetPos(OldProgramFrom, OldProgramTo, NewProgram, NewTarget: Integer);
     283var
     284  I: Integer;
     285  First: Integer;
     286  Last: Integer;
     287begin
     288  First := SearchIndexByProgramPos(OldProgramFrom);
     289  Last := SearchIndexByProgramPos(OldProgramTo);
     290  for I := Last downto First + 1 do Delete(I);
     291  TDebugStep(Items[First]).ProgramPosition := NewProgram;
     292  TDebugStep(Items[First]).TargetPosition := NewTarget;
    268293end;
    269294
Note: See TracChangeset for help on using the changeset viewer.