Changeset 86 for trunk/UBFTarget.pas
- Timestamp:
- Aug 29, 2017, 5:12:18 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.