- Timestamp:
- Aug 29, 2017, 5:12:18 PM (7 years ago)
- Location:
- trunk
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Target/UTargetC.pas
r72 r86 15 15 TTargetC = class(TBFTarget) 16 16 private 17 function GetMemoryCell: string; 17 18 public 18 19 constructor Create; override; … … 46 47 end; 47 48 49 function TTargetC.GetMemoryCell: string; 50 begin 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 + ']'; 57 end; 58 48 59 procedure TTargetC.Compile; 49 60 begin … … 67 78 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 68 79 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) + ';'); 74 86 cmLoopStart: begin 75 AddLine('while( Memory[Pos]!= 0)');87 AddLine('while(' + GetMemoryCell + ' != 0)'); 76 88 AddLine('{'); 77 89 Inc(Indent); -
trunk/Target/UTargetDelphi.pas
r72 r86 14 14 TTargetDelphi = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 38 39 end; 39 40 41 function TTargetDelphi.GetMemoryCell: string; 42 begin 43 Result := 'Memory[Pos'; 44 if FProgram[FProgramIndex].RelIndex > 0 then 45 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 46 else if FProgram[FProgramIndex].RelIndex < 0 then 47 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 48 Result := Result + ']'; 49 end; 50 40 51 procedure TTargetDelphi.Compile; 41 52 begin … … 60 71 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 61 72 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 62 cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 63 cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 64 cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 65 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 66 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 73 cmInc: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 74 cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 75 cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 76 cmMultipy: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 77 cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));'); 78 cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);'); 67 79 cmLoopStart: begin 68 AddLine('while Memory[Pos]<> 0 do begin');80 AddLine('while ' + GetMemoryCell + ' <> 0 do begin'); 69 81 Inc(Indent); 70 82 end; -
trunk/Target/UTargetFPC.pas
r66 r86 14 14 TTargetFPC = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 43 44 end; 44 45 46 function TTargetFPC.GetMemoryCell: string; 47 begin 48 Result := 'Memory[Pos'; 49 if FProgram[FProgramIndex].RelIndex > 0 then 50 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 51 else if FProgram[FProgramIndex].RelIndex < 0 then 52 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 53 Result := Result + ']'; 54 end; 55 45 56 procedure TTargetFPC.Compile; 46 57 begin … … 50 61 51 62 AddLine('program ' + ProgramName + ';'); 52 AddLine('');53 63 AddLine('var'); 54 64 AddLine(' Memory: array[0..30000] of Byte;'); … … 63 73 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 74 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 65 cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 66 cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 67 cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 68 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 69 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 75 cmInc: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 76 cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 77 cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 78 cmMultipy: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 79 cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));'); 80 cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);'); 70 81 cmLoopStart: begin 71 AddLine('while Memory[Pos]<> 0 do begin');82 AddLine('while ' + GetMemoryCell + ' <> 0 do begin'); 72 83 Inc(Indent); 73 84 end; -
trunk/Target/UTargetInterpretter.pas
r80 r86 44 44 procedure CommandLoopEnd; 45 45 procedure CommandSet; 46 procedure CommandMultiply; 46 47 procedure PrepareBreakPoints; 47 48 protected … … 75 76 const 76 77 BrainFuckCommandText: array[TMachineCommand] of Char = ( 77 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=' );78 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=', '*'); 78 79 79 80 … … 189 190 end; 190 191 if InputPosition <= Length(Input) then begin 191 Memory[MemoryPosition] := Ord(Input[InputPosition]); 192 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 193 Ord(Input[InputPosition]); 192 194 Inc(InputPosition); 193 195 end; … … 198 200 if OutputPosition > Length(Output) then 199 201 SetLength(Output, Length(Output) + 1 + Length(Output) div 4); 200 Output[OutputPosition] := Char(Memory[MemoryPosition]); 202 Output[OutputPosition] := Char(Memory[MemoryPosition + 203 FProgram[FProgramIndex].RelIndex]); 201 204 Inc(OutputPosition); 202 205 end; … … 204 207 procedure TTargetInterpretter.CommandLoopStart; 205 208 begin 206 if Memory[MemoryPosition ] = 0 then209 if Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] = 0 then 207 210 FProgramIndex := FProgram[FProgramIndex].Parameter; 208 211 end; … … 210 213 procedure TTargetInterpretter.CommandLoopEnd; 211 214 begin 212 if Memory[MemoryPosition ] > 0 then215 if Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] > 0 then 213 216 FProgramIndex := FProgram[FProgramIndex].Parameter - 1; 214 217 end; … … 216 219 procedure TTargetInterpretter.CommandInc; 217 220 begin 218 Memory[MemoryPosition] := ((Memory[MemoryPosition] + FProgram[FProgramIndex].Parameter) mod CellSize); 221 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 222 ((Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] + 223 FProgram[FProgramIndex].Parameter) mod CellSize); 219 224 end; 220 225 221 226 procedure TTargetInterpretter.CommandDec; 222 227 begin 223 Memory[MemoryPosition] := ((Memory[MemoryPosition] - FProgram[FProgramIndex].Parameter) mod CellSize); 228 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 229 ((Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] - 230 FProgram[FProgramIndex].Parameter) mod CellSize); 224 231 end; 225 232 … … 240 247 procedure TTargetInterpretter.CommandSet; 241 248 begin 242 Memory[MemoryPosition] := FProgram[FProgramIndex].Parameter mod CellSize; 249 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 250 FProgram[FProgramIndex].Parameter mod CellSize; 251 end; 252 253 procedure TTargetInterpretter.CommandMultiply; 254 begin 255 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 256 (Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] + 257 Memory[MemoryPosition] * FProgram[FProgramIndex].Parameter) mod CellSize; 243 258 end; 244 259 … … 411 426 // Extended commands 412 427 FCommandTable[cmSet] := CommandSet; 428 FCommandTable[cmMultipy] := CommandMultiply; 413 429 end; 414 430 -
trunk/Target/UTargetJava.pas
r72 r86 14 14 TTargetJava = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 44 45 end; 45 46 47 function TTargetJava.GetMemoryCell: string; 48 begin 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 + ']'; 55 end; 56 46 57 procedure TTargetJava.Compile; 47 58 begin … … 67 78 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 68 79 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) + ');'); 74 86 cmLoopStart: begin 75 AddLine('while( Memory[Pos]!= 0)');87 AddLine('while(' + GetMemoryCell + ' != 0)'); 76 88 AddLine('{'); 77 89 Inc(Indent); -
trunk/Target/UTargetPHP.pas
r72 r86 14 14 TTargetPHP = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 44 45 end; 45 46 47 function TTargetPHP.GetMemoryCell: string; 48 begin 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 + ']'; 55 end; 56 46 57 procedure TTargetPHP.Compile; 47 58 begin … … 59 70 cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 60 71 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) + ');'); 66 80 cmLoopStart: begin 67 AddLine('while( $Memory[$Position]!= "\0") {');81 AddLine('while(' + GetMemoryCell + ' != "\0") {'); 68 82 Inc(Indent); 69 83 end; -
trunk/Target/UTargetPython.pas
r82 r86 14 14 TTargetPython = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 42 43 ExecutorPath := '/usr/bin/python'; 43 44 {$ENDIF} 45 end; 46 47 function TTargetPython.GetMemoryCell: string; 48 begin 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 + ']'; 44 55 end; 45 56 … … 97 108 cmPointerInc: AddLine('position += ' + IntToStr(FProgram[FProgramIndex].Parameter)); 98 109 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)); 101 112 cmOutput: begin 102 AddLine('sys.stdout.write(chr( memory[position]))');113 AddLine('sys.stdout.write(chr(' + GetMemoryCell + '))'); 103 114 AddLine('sys.stdout.flush()'); 104 115 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) + ';'); 107 119 cmLoopStart: begin 108 AddLine('while( memory[position]!= 0):');120 AddLine('while(' + GetMemoryCell + ' != 0):'); 109 121 Inc(Indent); 110 122 end; -
trunk/UBFTarget.pas
r80 r86 11 11 12 12 TMachineCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec, 13 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug, cmSet );13 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug, cmSet, cmMultipy); 14 14 15 15 TMachineOperation = record 16 16 Command: TMachineCommand; 17 17 Parameter: Integer; 18 RelIndex: Integer; 18 19 end; 19 20 … … 28 29 procedure OptimizeMerge; 29 30 procedure OptimizeZeroInitMemory; 31 procedure OptimizeRelativeIndexes; 32 procedure OptimizeCopyMultiply; 30 33 protected 31 34 FProgram: array of TMachineOperation; … … 261 264 end; 262 265 266 procedure TBFTarget.OptimizeRelativeIndexes; 267 var 268 NewProgram: array of TMachineOperation; 269 NewProgramIndex: Integer; 270 RelIndex: Integer; 271 begin 272 NewProgramIndex := 0; 273 SetLength(NewProgram, Length(FProgram)); 274 275 RelIndex := 0; 276 FProgramIndex := 0; 277 while (FProgramIndex < Length(FProgram)) do begin 278 case FProgram[FProgramIndex].Command of 279 cmPointerInc: begin 280 RelIndex := RelIndex + FProgram[FProgramIndex].Parameter; 281 Dec(NewProgramIndex); 282 end; 283 cmPointerDec: begin 284 RelIndex := RelIndex - FProgram[FProgramIndex].Parameter; 285 Dec(NewProgramIndex); 286 end; 287 cmInc, cmDec, cmInput, cmOutput, cmSet: begin 288 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 289 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 290 NewProgram[NewProgramIndex].RelIndex := RelIndex; 291 end; 292 cmLoopStart, cmLoopEnd: begin 293 if RelIndex > 0 then begin 294 NewProgram[NewProgramIndex].Command := cmPointerInc; 295 NewProgram[NewProgramIndex].Parameter := RelIndex; 296 NewProgram[NewProgramIndex].RelIndex := 0; 297 Inc(NewProgramIndex); 298 RelIndex := 0; 299 end else 300 if RelIndex < 0 then begin 301 NewProgram[NewProgramIndex].Command := cmPointerDec; 302 NewProgram[NewProgramIndex].Parameter := Abs(RelIndex); 303 NewProgram[NewProgramIndex].RelIndex := 0; 304 Inc(NewProgramIndex); 305 RelIndex := 0; 306 end; 307 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 308 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 309 NewProgram[NewProgramIndex].RelIndex := 0; 310 end; 311 end; 312 DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex); 313 Inc(FProgramIndex); 314 Inc(NewProgramIndex); 315 end; 316 SetLength(NewProgram, NewProgramIndex); 317 318 // Replace old program by new program 319 SetLength(FProgram, Length(NewProgram)); 320 Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) * 321 Length(NewProgram)); 322 end; 323 324 procedure TBFTarget.OptimizeCopyMultiply; 325 var 326 NewProgram: array of TMachineOperation; 327 NewProgramIndex: Integer; 328 ProcessLoop: Boolean; 329 PointerChange: Integer; 330 NumberOfBaseDecrement: Integer; 331 LoopStartIndex: Integer; 332 LoopStartIndexNew: Integer; 333 begin 334 NewProgramIndex := 0; 335 SetLength(NewProgram, Length(FProgram)); 336 337 NumberOfBaseDecrement := 0; 338 ProcessLoop := False; 339 FProgramIndex := 0; 340 PointerChange := 0; 341 while (FProgramIndex < Length(FProgram)) do begin 342 case FProgram[FProgramIndex].Command of 343 cmPointerInc: begin 344 PointerChange := PointerChange + FProgram[FProgramIndex].Parameter; 345 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 346 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 347 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 348 end; 349 cmPointerDec: begin 350 PointerChange := PointerChange - FProgram[FProgramIndex].Parameter; 351 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 352 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 353 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 354 end; 355 cmInc: begin 356 if not ProcessLoop then begin 357 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 358 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 359 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 360 end else begin 361 if ((FProgram[FProgramIndex].RelIndex + PointerChange) <> 0) then begin 362 NewProgram[NewProgramIndex].Command := cmMultipy; 363 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 364 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 365 end else Dec(NewProgramIndex); 366 end; 367 end; 368 cmDec: begin 369 if not ProcessLoop then begin 370 if (PointerChange = 0) and (FProgram[FProgramIndex].RelIndex = 0) and 371 (FProgram[FProgramIndex].Parameter = 1) then 372 Inc(NumberOfBaseDecrement); 373 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 374 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 375 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 376 end else begin 377 if ((FProgram[FProgramIndex].RelIndex + PointerChange) <> 0) then begin 378 NewProgram[NewProgramIndex].Command := cmMultipy; 379 NewProgram[NewProgramIndex].Parameter := -FProgram[FProgramIndex].Parameter; 380 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 381 end else Dec(NewProgramIndex); 382 end; 383 end; 384 cmInput, cmOutput: begin 385 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 386 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 387 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 388 Inc(NumberOfBaseDecrement, 2); 389 end; 390 cmSet: begin 391 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 392 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 393 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 394 Inc(NumberOfBaseDecrement, 2); 395 end; 396 cmLoopStart: begin 397 if not ProcessLoop then begin 398 NumberOfBaseDecrement := 0; 399 PointerChange := 0; 400 LoopStartIndex := FProgramIndex; 401 LoopStartIndexNew := NewProgramIndex; 402 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 403 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 404 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 405 end else begin 406 Dec(NewProgramIndex); 407 end; 408 end; 409 cmLoopEnd: begin 410 if not ProcessLoop then begin 411 if (NumberOfBaseDecrement = 1) and (PointerChange = 0) then begin 412 FProgramIndex := LoopstartIndex - 1; 413 NewProgramIndex := LoopStartIndexNew - 1; 414 ProcessLoop := True; 415 end else begin 416 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 417 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 418 NewProgram[NewProgramIndex].RelIndex := FProgram[FProgramIndex].RelIndex; 419 end; 420 end else begin 421 NewProgram[NewProgramIndex].Command := cmSet; 422 NewProgram[NewProgramIndex].Parameter := 0; 423 NewProgram[NewProgramIndex].RelIndex := 0; 424 ProcessLoop := False; 425 end; 426 end; 427 end; 428 DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex); 429 Inc(FProgramIndex); 430 Inc(NewProgramIndex); 431 end; 432 SetLength(NewProgram, NewProgramIndex); 433 434 // Replace old program by new program 435 SetLength(FProgram, Length(NewProgram)); 436 Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) * 437 Length(NewProgram)); 438 end; 439 263 440 procedure TBFTarget.LoadProgram; 264 441 var … … 274 451 FProgram[FProgramIndex].Command := cmInc; 275 452 FProgram[FProgramIndex].Parameter := 1; 453 FProgram[FProgramIndex].RelIndex := 0; 276 454 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 277 455 end; … … 279 457 FProgram[FProgramIndex].Command := cmDec; 280 458 FProgram[FProgramIndex].Parameter := 1; 459 FProgram[FProgramIndex].RelIndex := 0; 281 460 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 282 461 end; … … 284 463 FProgram[FProgramIndex].Command := cmPointerInc; 285 464 FProgram[FProgramIndex].Parameter := 1; 465 FProgram[FProgramIndex].RelIndex := 0; 286 466 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 287 467 end; … … 289 469 FProgram[FProgramIndex].Command := cmPointerDec; 290 470 FProgram[FProgramIndex].Parameter := 1; 471 FProgram[FProgramIndex].RelIndex := 0; 291 472 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 292 473 end; … … 294 475 FProgram[FProgramIndex].Command := cmInput; 295 476 FProgram[FProgramIndex].Parameter := 0; 477 FProgram[FProgramIndex].RelIndex := 0; 296 478 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 297 479 end; … … 299 481 FProgram[FProgramIndex].Command := cmOutput; 300 482 FProgram[FProgramIndex].Parameter := 0; 483 FProgram[FProgramIndex].RelIndex := 0; 301 484 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 302 485 end; … … 304 487 FProgram[FProgramIndex].Command := cmLoopStart; 305 488 FProgram[FProgramIndex].Parameter := 0; 489 FProgram[FProgramIndex].RelIndex := 0; 306 490 DebugSteps.AddStep(I - 1, FProgramIndex, soStepIn); 307 491 end; … … 309 493 FProgram[FProgramIndex].Command := cmLoopEnd; 310 494 FProgram[FProgramIndex].Parameter := 0; 495 FProgram[FProgramIndex].RelIndex := 0; 311 496 DebugSteps.AddStep(I - 1, FProgramIndex, soStepOut); 312 497 end … … 336 521 until Length(FProgram) = OldLength; 337 522 OptimizeZeroInitMemory; 523 OptimizeRelativeIndexes; 524 OptimizeCopyMultiply; 338 525 end; 339 526 -
trunk/UTarget.pas
r84 r86 410 410 I: Integer; 411 411 begin 412 if CompilerPath = '' then Exit;413 414 412 CompiledFile := ExtractFilePath(ProjectFileName) + 415 413 'compiled' + DirectorySeparator + Name + DirectorySeparator +
Note:
See TracChangeset
for help on using the changeset viewer.