Changeset 153 for branches/virtualcpu2/UMachine.pas
- Timestamp:
- Apr 20, 2018, 10:56:29 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/virtualcpu2/UMachine.pas
r152 r153 9 9 10 10 type 11 Int8 = ShortInt; 12 Int16 = SmallInt; 13 Int32 = Longint; 14 PInt8 = ^Int8; 15 PInt16 = ^Int16; 16 PInt32 = ^Int32; 17 UInt8 = Byte; 18 UInt16 = Word; 19 UInt32 = Cardinal; 20 PUInt8 = ^UInt8; 21 PUInt16 = ^UInt16; 22 PUInt32 = ^UInt16; 23 24 11 25 //TAddrInt = Int8; 12 26 TAddrInt = Int16; 13 27 //TAddrInt = Int32; 14 28 //TAddrInt = Int64; 29 PAddrInt = ^TAddrInt; 15 30 TDataInt = QWord; 16 31 //TDataInt = Int16; … … 19 34 PDataInt = ^TDataInt; 20 35 21 TRegIndex = TAddrInt;36 TRegIndex = Byte; 22 37 TRegSize = Int8; 23 38 … … 40 55 opPop, 41 56 // Skip 42 op SkipZero,43 op SkipNotZero,57 opTestZero, 58 opTestNotZero, 44 59 opSkipCarry, 45 60 opSkipNotCarray, … … 47 62 opCallRel, 48 63 opCallAbs, 64 opCallRelCond, 65 opCallAbsCond, 49 66 opRet, 50 67 // Jump 51 68 opJumpRel, 52 69 opJumpAbs, 70 opJumpRelCond, 71 opJumpAbsCond, 53 72 // Logical operations 54 73 opAnd, … … 103 122 procedure OpcodePop; 104 123 procedure OpcodePush; 124 procedure OpcodePush8; 125 procedure OpcodePush16; 126 procedure OpcodePush32; 127 procedure OpcodePush64; 105 128 procedure OpcodeLdir; 106 129 procedure OpcodeInc; … … 110 133 procedure OpcodeInc64; 111 134 procedure OpcodeDec; 112 procedure Opcode SkipNotZero;113 procedure Opcode SkipZero;135 procedure OpcodeTestNotZero; 136 procedure OpcodeTestZero; 114 137 procedure OpcodeJumpRel; 115 138 procedure OpcodeJumpAbs; 139 procedure OpcodeJumpRelCond; 140 procedure OpcodeJumpRelCond8; 141 procedure OpcodeJumpAbsCond; 142 procedure OpcodeCallRel; 143 procedure OpcodeCallAbs; 144 procedure OpcodeCallRelCond; 145 procedure OpcodeCallAbsCond; 116 146 procedure OpcodeUnsupported; 147 procedure OpcodeSetSize; 148 procedure OpcodeSetSize8; 149 procedure OpcodeSetSize16; 150 procedure OpcodeSetSize32; 151 procedure OpcodeSetSize64; 117 152 function ReadNext: TDataInt; 153 function ReadNextS: TDataInt; 118 154 function ReadNext8: Byte; 119 155 function ReadNextS8: ShortInt; … … 131 167 SP: TAddrInt; 132 168 DataSize: TDataSize; 169 BaseDataSize: TDataSize; 133 170 Terminated: Boolean; 134 SkipNext: Boolean;171 Condition: Boolean; 135 172 procedure Run; 136 173 constructor Create; … … 143 180 Machine: TMachine; 144 181 procedure AddData(Data: TDataInt); 182 procedure AddDataS(Data: Int64); 145 183 procedure AddData8(Data: Byte); 146 184 procedure AddDataS8(Data: ShortInt); 147 185 procedure AddCopyConst(Reg: TRegIndex; Value: TDataInt); 186 procedure AddCopyConst8(Reg: TRegIndex; Value: UInt8); 148 187 procedure AddGetDataSize(Reg: TRegIndex); 149 188 procedure AddCopyFromMem(Reg, Src: TRegIndex); … … 152 191 procedure AddDec(Reg: TRegIndex); 153 192 procedure AddHalt; 154 procedure AddJumpRelNotZero(Reg: TRegIndex; RelAddr: Integer); 193 procedure AddTestNotZero(Reg: TRegIndex); 194 procedure AddJumpRelCond(RelAddr: Integer); 195 procedure AddJumpRelCond8(RelAddr: ShortInt); 155 196 constructor Create; 156 197 end; 157 198 158 199 const 159 DataSizeValue: array[TDataSize] of Byte = (SizeOf(TDataInt), SizeOf( Byte),160 SizeOf( Word), SizeOf(Cardinal), SizeOf(QWord));200 DataSizeValue: array[TDataSize] of Byte = (SizeOf(TDataInt), SizeOf(UInt8), 201 SizeOf(UInt16), SizeOf(UInt32), SizeOf(UInt64)); 161 202 162 203 implementation … … 203 244 SetOpcode(opInc, OpcodeInc64, ds64); 204 245 SetOpcode(opDec, OpcodeDec); 205 SetOpcode(op SkipZero, OpcodeSkipZero);206 SetOpcode(op SkipNotZero, OpcodeSkipNotZero);246 SetOpcode(opTestZero, OpcodeTestZero); 247 SetOpcode(opTestNotZero, OpcodeTestNotZero); 207 248 SetOpcode(opJumpRel, OpcodeJumpRel); 208 249 SetOpcode(opJumpAbs, OpcodeJumpAbs); 250 SetOpcode(opJumpRelCond, OpcodeJumpRelCond); 251 SetOpcode(opJumpRelCond, OpcodeJumpRelCond8, ds8); 252 SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond); 253 SetOpcode(opCallRel, OpcodeCAllRel); 254 SetOpcode(opCallAbs, OpcodeCallAbs); 255 SetOpcode(opCallRelCond, OpcodeCallRelCond); 256 SetOpcode(opCallAbsCond, OpcodeCallAbsCond); 209 257 SetOpcode(opAdd, OpcodeAdd); 210 258 SetOpcode(opSub, OpcodeSub); … … 214 262 SetOpcode(opXor, OpcodeXor); 215 263 SetOpcode(opPush, OpcodePush); 264 SetOpcode(opPush, OpcodePush8, ds8); 265 SetOpcode(opPush, OpcodePush16, ds16); 266 SetOpcode(opPush, OpcodePush32, ds32); 267 SetOpcode(opPush, OpcodePush64, ds64); 216 268 SetOpcode(opPop, OpcodePop); 269 SetOpcode(opSetSize8, OpcodeSetSize8); 217 270 end; 218 271 … … 226 279 if (Opcode <= High(Opcode)) then begin 227 280 OpcodeHandler := OpcodeTable[Opcode, DataSize]; 228 if notAssigned(OpcodeHandler) then OpcodeHandler281 if Assigned(OpcodeHandler) then OpcodeHandler 229 282 else raise Exception.Create('Opcode without handler: ' + IntToHex(Integer(Opcode), 2)); 230 283 end else raise Exception.Create('Unknown opcode: ' + IntToHex(Integer(Opcode), 2)); 284 if (DataSize <> BaseDataSize) and (Opcode <> opSetSize) and (Opcode <> opSetSize8) and 285 (Opcode <> opSetSize16) and (Opcode <> opSetSize32) and (Opcode <> opSetSize64) then 286 DataSize := BaseDataSize; 231 287 end; 232 288 … … 253 309 Dest := ReadNext8; 254 310 Source := ReadNext8; 255 P Byte(@Registers[Dest])^ := PByte(@Registers[Source])^;311 PUInt8(@Registers[Dest])^ := PUInt8(@Registers[Source])^; 256 312 end; 257 313 … … 263 319 Dest := ReadNext8; 264 320 Source := ReadNext8; 265 P Word(@Registers[Dest])^ := PWord(@Registers[Source])^;321 PUInt16(@Registers[Dest])^ := PUInt16(@Registers[Source])^; 266 322 end; 267 323 … … 273 329 Dest := ReadNext8; 274 330 Source := ReadNext8; 275 P Cardinal(@Registers[Dest])^ := PCardinal(@Registers[Source])^;331 PUInt32(@Registers[Dest])^ := PUInt32(@Registers[Source])^; 276 332 end; 277 333 … … 283 339 Dest := ReadNext8; 284 340 Source := ReadNext8; 285 P QWord(@Registers[Dest])^ := PQWord(@Registers[Source])^;341 PUInt64(@Registers[Dest])^ := PUInt64(@Registers[Source])^; 286 342 end; 287 343 … … 328 384 procedure TMachine.OpcodeCopyConst8; 329 385 var 330 Source: Byte;331 Dest: TRegIndex; 332 begin 333 Dest := ReadNext8; 334 Source := ReadNext8; 335 P Byte(@Registers[Dest])^ := Source;386 Source: UInt8; 387 Dest: TRegIndex; 388 begin 389 Dest := ReadNext8; 390 Source := ReadNext8; 391 PUInt8(@Registers[Dest])^ := Source; 336 392 end; 337 393 338 394 procedure TMachine.OpcodeCopyConst16; 339 395 var 340 Source: Word;396 Source: UInt16; 341 397 Dest: TRegIndex; 342 398 begin 343 399 Dest := ReadNext8; 344 400 Source := ReadNext16; 345 P Word(@Registers[Dest])^ := Source;401 PUInt16(@Registers[Dest])^ := Source; 346 402 end; 347 403 348 404 procedure TMachine.OpcodeCopyConst32; 349 405 var 350 Source: Cardinal;406 Source: UInt32; 351 407 Dest: TRegIndex; 352 408 begin 353 409 Dest := ReadNext8; 354 410 Source := ReadNext32; 355 P Cardinal(@Registers[Dest])^ := Source;411 PUInt32(@Registers[Dest])^ := Source; 356 412 end; 357 413 358 414 procedure TMachine.OpcodeCopyConst64; 359 415 var 360 Source: QWord;416 Source: UInt64; 361 417 Dest: TRegIndex; 362 418 begin 363 419 Dest := ReadNext8; 364 420 Source := ReadNext64; 365 P QWord(@Registers[Dest])^ := Source;421 PUInt64(@Registers[Dest])^ := Source; 366 422 end; 367 423 … … 453 509 Dec(SP, SizeOf(TDataInt)); 454 510 PDataInt(@Memory[SP])^ := PDataInt(@Registers[Source])^; 511 end; 512 513 procedure TMachine.OpcodePush8; 514 var 515 Source: TRegIndex; 516 begin 517 Source := ReadNext8; 518 Dec(SP, SizeOf(Byte)); 519 PUInt8(@Memory[SP])^ := PUInt8(@Registers[Source])^; 520 end; 521 522 procedure TMachine.OpcodePush16; 523 var 524 Source: TRegIndex; 525 begin 526 Source := ReadNext8; 527 Dec(SP, SizeOf(Word)); 528 PUInt16(@Memory[SP])^ := PUInt16(@Registers[Source])^; 529 end; 530 531 procedure TMachine.OpcodePush32; 532 var 533 Source: TRegIndex; 534 begin 535 Source := ReadNext8; 536 Dec(SP, SizeOf(Cardinal)); 537 PUInt32(@Memory[SP])^ := PUInt32(@Registers[Source])^; 538 end; 539 540 procedure TMachine.OpcodePush64; 541 var 542 Source: TRegIndex; 543 begin 544 Source := ReadNext8; 545 Dec(SP, SizeOf(QWord)); 546 PUInt64(@Memory[SP])^ := PUInt64(@Registers[Source])^; 455 547 end; 456 548 … … 485 577 begin 486 578 Reg := ReadNext8; 487 P Byte(@Registers[Reg])^ := PByte(@Registers[Reg])^ + 1;579 PUInt8(@Registers[Reg])^ := PUInt8(@Registers[Reg])^ + 1; 488 580 end; 489 581 … … 493 585 begin 494 586 Reg := ReadNext8; 495 P Word(@Registers[Reg])^ := PWord(@Registers[Reg])^ + 1;587 PUInt16(@Registers[Reg])^ := PUInt16(@Registers[Reg])^ + 1; 496 588 end; 497 589 … … 501 593 begin 502 594 Reg := ReadNext8; 503 P Cardinal(@Registers[Reg])^ := PCardinal(@Registers[Reg])^ + 1;595 PUInt32(@Registers[Reg])^ := PUInt32(@Registers[Reg])^ + 1; 504 596 end; 505 597 … … 509 601 begin 510 602 Reg := ReadNext8; 511 P QWord(@Registers[Reg])^ := PQWord(@Registers[Reg])^ + 1;603 PUInt64(@Registers[Reg])^ := PUInt64(@Registers[Reg])^ + 1; 512 604 end; 513 605 … … 520 612 end; 521 613 522 procedure TMachine.Opcode SkipNotZero;614 procedure TMachine.OpcodeTestNotZero; 523 615 var 524 616 Reg: TRegIndex; 525 617 begin 526 618 Reg := ReadNext8; 527 SkipNext:= Registers[Reg] <> 0;528 end; 529 530 procedure TMachine.Opcode SkipZero;619 Condition := Registers[Reg] <> 0; 620 end; 621 622 procedure TMachine.OpcodeTestZero; 531 623 var 532 624 Reg: TRegIndex; 533 625 begin 534 626 Reg := ReadNext8; 535 SkipNext:= Registers[Reg] = 0;627 Condition := Registers[Reg] = 0; 536 628 end; 537 629 … … 540 632 RelAddr: TAddrInt; 541 633 begin 542 OpcodeSkipNotZero; 634 RelAddr := ReadNextS; 635 IP := IP + RelAddr; 636 end; 637 638 procedure TMachine.OpcodeJumpAbs; 639 var 640 Addr: TAddrInt; 641 begin 642 Addr := ReadNext; 643 IP := Addr; 644 end; 645 646 procedure TMachine.OpcodeJumpRelCond; 647 var 648 RelAddr: TAddrInt; 649 begin 650 RelAddr := ReadNextS; 651 if Condition then IP := IP + RelAddr; 652 end; 653 654 procedure TMachine.OpcodeJumpRelCond8; 655 var 656 RelAddr: Int8; 657 begin 543 658 RelAddr := ReadNextS8; 544 if not SkipNextthen IP := IP + RelAddr;545 end; 546 547 procedure TMachine.OpcodeJumpAbs ;659 if Condition then IP := IP + RelAddr; 660 end; 661 662 procedure TMachine.OpcodeJumpAbsCond; 548 663 var 549 664 Addr: TAddrInt; 550 665 begin 551 OpcodeSkipNotZero;552 666 Addr := ReadNext; 553 if not SkipNext then IP := Addr; 667 if Condition then IP := Addr; 668 end; 669 670 procedure TMachine.OpcodeCallRel; 671 var 672 RelAddr: TAddrInt; 673 begin 674 RelAddr := ReadNextS; 675 Dec(SP, SizeOf(TAddrInt)); 676 PAddrInt(@Memory[SP])^ := IP; 677 IP := IP + RelAddr; 678 end; 679 680 procedure TMachine.OpcodeCallAbs; 681 var 682 Addr: TAddrInt; 683 begin 684 Addr := ReadNext; 685 Dec(SP, SizeOf(TAddrInt)); 686 PAddrInt(@Memory[SP])^ := IP; 687 IP := Addr; 688 end; 689 690 procedure TMachine.OpcodeCallRelCond; 691 var 692 RelAddr: TAddrInt; 693 begin 694 RelAddr := ReadNextS; 695 if Condition then begin 696 Dec(SP, SizeOf(TAddrInt)); 697 PAddrInt(@Memory[SP])^ := IP; 698 IP := IP + RelAddr; 699 end; 700 end; 701 702 procedure TMachine.OpcodeCallAbsCond; 703 var 704 Addr: TAddrInt; 705 begin 706 Dec(SP, SizeOf(TAddrInt)); 707 if Condition then begin 708 PAddrInt(@Memory[SP])^ := IP; 709 Addr := ReadNext; 710 IP := Addr; 711 end; 554 712 end; 555 713 … … 557 715 begin 558 716 raise Exception.Create('Unsupported instruction'); 717 end; 718 719 procedure TMachine.OpcodeSetSize; 720 begin 721 DataSize := dsNative; 722 end; 723 724 procedure TMachine.OpcodeSetSize8; 725 begin 726 DataSize := ds8; 727 end; 728 729 procedure TMachine.OpcodeSetSize16; 730 begin 731 DataSize := ds16; 732 end; 733 734 procedure TMachine.OpcodeSetSize32; 735 begin 736 DataSize := ds32; 737 end; 738 739 procedure TMachine.OpcodeSetSize64; 740 begin 741 DataSize := ds64; 559 742 end; 560 743 … … 572 755 end; 573 756 757 function TMachine.ReadNextS: TDataInt; 758 begin 759 CheckIP; 760 Result := PDataInt(@Memory[IP])^; 761 Inc(IP, SizeOf(TDataInt)); 762 end; 763 574 764 function TMachine.ReadNext8: Byte; 575 765 begin … … 615 805 end; 616 806 807 procedure TInstructionWriter.AddDataS(Data: Int64); 808 begin 809 PInt64(@(Machine.Memory[Addr]))^ := Data; 810 Inc(Addr, SizeOf(Int64)); 811 end; 812 617 813 procedure TInstructionWriter.AddData8(Data: Byte); 618 814 begin … … 634 830 end; 635 831 832 procedure TInstructionWriter.AddCopyConst8(Reg: TRegIndex; Value: UInt8); 833 begin 834 AddData8(Integer(opSetSize8)); 835 AddData8(Integer(opCopyConst)); 836 AddData8(Reg); 837 AddData8(Value); 838 end; 839 636 840 procedure TInstructionWriter.AddGetDataSize(Reg: TRegIndex); 637 841 begin … … 671 875 end; 672 876 673 procedure TInstructionWriter.AddJumpRelNotZero(Reg: TRegIndex; RelAddr: Integer 674 ); 675 begin 676 AddData8(Integer(opJumpRel)); 877 procedure TInstructionWriter.AddTestNotZero(Reg: TRegIndex); 878 begin 879 AddData8(Integer(opTestNotZero)); 677 880 AddData8(Reg); 678 AddDataS8(RelAddr - 3); 881 end; 882 883 procedure TInstructionWriter.AddJumpRelCond(RelAddr: Integer); 884 begin 885 AddData8(Integer(opJumpRelCond)); 886 AddDataS(RelAddr - SizeOf(Byte) - SizeOf(TDataInt)); 887 end; 888 889 procedure TInstructionWriter.AddJumpRelCond8(RelAddr: ShortInt); 890 begin 891 AddData8(Integer(opSetSize8)); 892 AddData8(Integer(opJumpRelCond)); 893 AddDataS8(RelAddr - SizeOf(Byte) - SizeOf(TDataInt)); 679 894 end; 680 895
Note:
See TracChangeset
for help on using the changeset viewer.