1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
|
# Copyright (C) 2013 Apple Inc. All rights reserved.
# Copyright (C) 2013 Cisco Systems, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS, INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS, INC. OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'risc'
class Node
def sh4SingleHi
doubleOperand = sh4Operand
raise "Bogus register name #{doubleOperand}" unless doubleOperand =~ /^dr/
"fr" + ($~.post_match.to_i).to_s
end
def sh4SingleLo
doubleOperand = sh4Operand
raise "Bogus register name #{doubleOperand}" unless doubleOperand =~ /^dr/
"fr" + ($~.post_match.to_i + 1).to_s
end
end
class SpecialRegister < NoChildren
def sh4Operand
@name
end
def dump
@name
end
def register?
true
end
end
SH4_TMP_GPRS = [ SpecialRegister.new("r3"), SpecialRegister.new("r11"), SpecialRegister.new("r13") ]
SH4_TMP_FPRS = [ SpecialRegister.new("dr10") ]
class RegisterID
def sh4Operand
case name
when "a0"
"r4"
when "a1"
"r5"
when "t0"
"r0"
when "t1"
"r1"
when "t2"
"r2"
when "t3"
"r10"
when "t4"
"r6"
when "cfr"
"r14"
when "sp"
"r15"
when "lr"
"pr"
else
raise "Bad register #{name} for SH4 at #{codeOriginString}"
end
end
end
class FPRegisterID
def sh4Operand
case name
when "ft0", "fr"
"dr0"
when "ft1"
"dr2"
when "ft2"
"dr4"
when "ft3"
"dr6"
when "ft4"
"dr8"
when "fa0"
"dr12"
else
raise "Bad register #{name} for SH4 at #{codeOriginString}"
end
end
end
class Immediate
def sh4Operand
raise "Invalid immediate #{value} at #{codeOriginString}" if value < -128 or value > 127
"##{value}"
end
end
class Address
def sh4Operand
raise "Bad offset #{offset.value} at #{codeOriginString}" if offset.value < 0 or offset.value > 60
if offset.value == 0
"@#{base.sh4Operand}"
else
"@(#{offset.value}, #{base.sh4Operand})"
end
end
def sh4OperandPostInc
raise "Bad offset #{offset.value} for post inc at #{codeOriginString}" unless offset.value == 0
"@#{base.sh4Operand}+"
end
def sh4OperandPreDec
raise "Bad offset #{offset.value} for pre dec at #{codeOriginString}" unless offset.value == 0
"@-#{base.sh4Operand}"
end
end
class BaseIndex
def sh4Operand
raise "Unconverted base index at #{codeOriginString}"
end
end
class AbsoluteAddress
def sh4Operand
raise "Unconverted absolute address at #{codeOriginString}"
end
end
class ConstPool < Node
attr_reader :size
attr_reader :entries
def initialize(codeOrigin, entries, size)
super(codeOrigin)
raise "Invalid size #{size} for ConstPool" unless size == 16 or size == 32
@size = size
@entries = entries
end
def dump
"#{size}: #{entries}"
end
def address?
false
end
def label?
false
end
def immediate?
false
end
def register?
false
end
def lowerSH4
if size == 16
$asm.puts ".balign 2"
else
$asm.puts ".balign 4"
end
entries.map {
|e|
e.label.lower("SH4")
if e.size == 16
$asm.puts ".word #{e.value}"
else
$asm.puts ".long #{e.value}"
end
}
end
end
class ConstPoolEntry < Node
attr_reader :size
attr_reader :value
attr_reader :label
attr_reader :labelref
def initialize(codeOrigin, value, size)
super(codeOrigin)
raise "Invalid size #{size} for ConstPoolEntry" unless size == 16 or size == 32
@size = size
@value = value
@label = LocalLabel.unique("constpool#{size}")
@labelref = LocalLabelReference.new(codeOrigin, label)
end
def dump
"#{value} (#{size} @ #{label})"
end
def ==(other)
other.is_a? ConstPoolEntry and other.value == @value
end
def address?
false
end
def label?
false
end
def immediate?
false
end
def register?
false
end
end
#
# Lowering of shift ops for SH4. For example:
#
# rshifti foo, bar
#
# becomes:
#
# negi foo, tmp
# shad tmp, bar
#
def sh4LowerShiftOps(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "ulshifti", "ulshiftp", "urshifti", "urshiftp", "lshifti", "lshiftp", "rshifti", "rshiftp"
if node.opcode[0, 1] == "u"
type = "l"
direction = node.opcode[1, 1]
else
type = "a"
direction = node.opcode[0, 1]
end
if node.operands[0].is_a? Immediate
maskedImm = Immediate.new(node.operands[0].codeOrigin, node.operands[0].value & 31)
if maskedImm.value == 0
# There is nothing to do here.
elsif maskedImm.value == 1 or (type == "l" and [2, 8, 16].include? maskedImm.value)
newList << Instruction.new(node.codeOrigin, "sh#{type}#{direction}x", [maskedImm, node.operands[1]])
else
tmp = Tmp.new(node.codeOrigin, :gpr)
if direction == "l"
newList << Instruction.new(node.codeOrigin, "move", [maskedImm, tmp])
else
newList << Instruction.new(node.codeOrigin, "move", [Immediate.new(node.operands[0].codeOrigin, -1 * maskedImm.value), tmp])
end
newList << Instruction.new(node.codeOrigin, "sh#{type}d", [tmp, node.operands[1]])
end
else
tmp = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, "move", [Immediate.new(node.operands[0].codeOrigin, 31), tmp])
newList << Instruction.new(node.codeOrigin, "andi", [node.operands[0], tmp])
if direction == "r"
newList << Instruction.new(node.codeOrigin, "negi", [tmp, tmp])
end
newList << Instruction.new(node.codeOrigin, "sh#{type}d", [tmp, node.operands[1]])
end
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowering of simple branch ops for SH4. For example:
#
# baddis foo, bar, baz
#
# will become:
#
# addi foo, bar
# bs bar, baz
#
def sh4LowerSimpleBranchOps(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
annotation = node.annotation
case node.opcode
when /^b(addi|subi|ori|addp)/
op = $1
bc = $~.post_match
case op
when "addi", "addp"
op = "addi"
when "subi", "subp"
op = "subi"
when "ori", "orp"
op = "ori"
end
if bc == "s"
raise "Invalid operands number (#{node.operands.size})" unless node.operands.size == 3
if node.operands[1].is_a? RegisterID or node.operands[1].is_a? SpecialRegister
newList << Instruction.new(node.codeOrigin, op, node.operands[0..1])
newList << Instruction.new(node.codeOrigin, "bs", node.operands[1..2])
else
tmpVal = Tmp.new(node.codeOrigin, :gpr)
tmpPtr = Tmp.new(node.codeOrigin, :gpr)
addr = Address.new(node.codeOrigin, tmpPtr, Immediate.new(node.codeOrigin, 0))
newList << Instruction.new(node.codeOrigin, "leap", [node.operands[1], tmpPtr])
newList << Instruction.new(node.codeOrigin, "loadi", [addr, tmpVal])
newList << Instruction.new(node.codeOrigin, op, [node.operands[0], tmpVal])
newList << Instruction.new(node.codeOrigin, "storei", [tmpVal, addr])
newList << Instruction.new(node.codeOrigin, "bs", [tmpVal, node.operands[2]])
end
elsif bc == "nz"
raise "Invalid operands number (#{node.operands.size})" unless node.operands.size == 3
newList << Instruction.new(node.codeOrigin, op, node.operands[0..1])
newList << Instruction.new(node.codeOrigin, "btinz", node.operands[1..2])
else
newList << node
end
when "bmulio", "bmulpo"
raise "Invalid operands number (#{node.operands.size})" unless node.operands.size == 3
tmp1 = Tmp.new(node.codeOrigin, :gpr)
tmp2 = Tmp.new(node.codeOrigin, :gpr)
newList << Instruction.new(node.codeOrigin, node.opcode, [tmp1, tmp2].concat(node.operands))
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowering of double accesses for SH4. For example:
#
# loadd [foo, bar, 8], baz
#
# becomes:
#
# leap [foo, bar, 8], tmp
# loaddReversedAndIncrementAddress [tmp], baz
#
def sh4LowerDoubleAccesses(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "loadd"
tmp = Tmp.new(codeOrigin, :gpr)
addr = Address.new(codeOrigin, tmp, Immediate.new(codeOrigin, 0))
newList << Instruction.new(codeOrigin, "leap", [node.operands[0], tmp])
newList << Instruction.new(node.codeOrigin, "loaddReversedAndIncrementAddress", [addr, node.operands[1]], node.annotation)
when "stored"
tmp = Tmp.new(codeOrigin, :gpr)
addr = Address.new(codeOrigin, tmp, Immediate.new(codeOrigin, 0))
newList << Instruction.new(codeOrigin, "leap", [node.operands[1].withOffset(8), tmp])
newList << Instruction.new(node.codeOrigin, "storedReversedAndDecrementAddress", [node.operands[0], addr], node.annotation)
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowering of double specials for SH4.
#
def sh4LowerDoubleSpecials(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "bdltun", "bdgtun"
# Handle specific floating point unordered opcodes.
newList << Instruction.new(codeOrigin, "bdnan", [node.operands[0], node.operands[2]])
newList << Instruction.new(codeOrigin, "bdnan", [node.operands[1], node.operands[2]])
newList << Instruction.new(codeOrigin, node.opcode[0..-3], node.operands)
when "bdnequn", "bdgtequn", "bdltequn"
newList << Instruction.new(codeOrigin, node.opcode[0..-3], node.operands)
when "bdneq", "bdgteq", "bdlteq"
# Handle specific floating point ordered opcodes.
outlabel = LocalLabel.unique("out_#{node.opcode}")
outref = LocalLabelReference.new(codeOrigin, outlabel)
newList << Instruction.new(codeOrigin, "bdnan", [node.operands[0], outref])
newList << Instruction.new(codeOrigin, "bdnan", [node.operands[1], outref])
newList << Instruction.new(codeOrigin, node.opcode, node.operands)
newList << outlabel
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Lowering of misplaced labels for SH4.
#
def sh4LowerMisplacedLabels(list)
newList = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "jmp", "call"
if node.operands[0].is_a? LabelReference
tmp = Tmp.new(codeOrigin, :gpr)
newList << Instruction.new(codeOrigin, "move", [node.operands[0], tmp])
newList << Instruction.new(codeOrigin, node.opcode, [tmp])
else
newList << node
end
else
newList << node
end
else
newList << node
end
}
newList
end
#
# Group immediate values outside -128..127 range into constant pools for SH4.
# These constant pools will be placed behind non-return opcodes jmp and ret, for example:
#
# move 1024, foo
# ...
# ret
#
# becomes:
#
# move [label], foo
# ...
# ret
# label: 1024
#
def sh4LowerConstPool(list)
newList = []
currentPool16 = []
currentPool32 = []
list.each {
| node |
if node.is_a? Instruction
case node.opcode
when "jmp", "ret"
newList << node
if not currentPool16.empty?
newList << ConstPool.new(codeOrigin, currentPool16, 16)
currentPool16 = []
end
if not currentPool32.empty?
newList << ConstPool.new(codeOrigin, currentPool32, 32)
currentPool32 = []
end
when "move"
if node.operands[0].is_a? Immediate and not (-128..127).include? node.operands[0].value
poolEntry = nil
if (-32768..32767).include? node.operands[0].value
currentPool16.each { |e|
if e.value == node.operands[0].value
poolEntry = e
end
}
if !poolEntry
poolEntry = ConstPoolEntry.new(codeOrigin, node.operands[0].value, 16)
currentPool16 << poolEntry
end
else
currentPool32.each { |e|
if e.value == node.operands[0].value
poolEntry = e
end
}
if !poolEntry
poolEntry = ConstPoolEntry.new(codeOrigin, node.operands[0].value, 32)
currentPool32 << poolEntry
end
end
newList << Instruction.new(codeOrigin, "move", [poolEntry, node.operands[1]])
elsif node.operands[0].is_a? LabelReference
poolEntry = nil
currentPool32.each { |e|
if e.value == node.operands[0].asmLabel
poolEntry = e
end
}
if !poolEntry
poolEntry = ConstPoolEntry.new(codeOrigin, node.operands[0].asmLabel, 32)
currentPool32 << poolEntry
end
newList << Instruction.new(codeOrigin, "move", [poolEntry, node.operands[1]])
else
newList << node
end
else
newList << node
end
else
newList << node
end
}
if not currentPool16.empty?
newList << ConstPool.new(codeOrigin, currentPool16, 16)
end
if not currentPool32.empty?
newList << ConstPool.new(codeOrigin, currentPool32, 32)
end
newList
end
class Sequence
def getModifiedListSH4
result = @list
# Verify that we will only see instructions and labels.
result.each {
| node |
unless node.is_a? Instruction or
node.is_a? Label or
node.is_a? LocalLabel or
node.is_a? Skip
raise "Unexpected #{node.inspect} at #{node.codeOrigin}"
end
}
result = sh4LowerShiftOps(result)
result = sh4LowerSimpleBranchOps(result)
result = riscLowerMalformedAddresses(result) {
| node, address |
if address.is_a? Address
case node.opcode
when "btbz", "btbnz", "cbeq", "bbeq", "bbneq", "bbb", "loadb", "storeb"
(0..15).include? address.offset.value and
((node.operands[0].is_a? RegisterID and node.operands[0].sh4Operand == "r0") or
(node.operands[1].is_a? RegisterID and node.operands[1].sh4Operand == "r0"))
when "loadh"
(0..30).include? address.offset.value and
((node.operands[0].is_a? RegisterID and node.operands[0].sh4Operand == "r0") or
(node.operands[1].is_a? RegisterID and node.operands[1].sh4Operand == "r0"))
else
(0..60).include? address.offset.value
end
else
false
end
}
result = sh4LowerDoubleAccesses(result)
result = sh4LowerDoubleSpecials(result)
result = riscLowerMisplacedImmediates(result, ["storeb", "storei", "storep", "muli", "mulp", "andi", "ori", "xori",
"cbeq", "cieq", "cpeq", "cineq", "cpneq", "cib", "baddio", "bsubio", "bmulio", "baddis",
"bbeq", "bbneq", "bbb", "bieq", "bpeq", "bineq", "bpneq", "bia", "bpa", "biaeq", "bpaeq", "bib", "bpb",
"bigteq", "bpgteq", "bilt", "bplt", "bigt", "bpgt", "bilteq", "bplteq", "btiz", "btpz", "btinz", "btpnz", "btbz", "btbnz"])
result = riscLowerMalformedImmediates(result, -128..127)
result = sh4LowerMisplacedLabels(result)
result = riscLowerMisplacedAddresses(result)
result = assignRegistersToTemporaries(result, :gpr, SH4_TMP_GPRS)
result = assignRegistersToTemporaries(result, :gpr, SH4_TMP_FPRS)
result = sh4LowerConstPool(result)
return result
end
end
def sh4Operands(operands)
operands.map{|v| v.sh4Operand}.join(", ")
end
def emitSH4Branch(sh4opcode, operand)
$asm.puts "#{sh4opcode} @#{operand.sh4Operand}"
$asm.puts "nop"
end
def emitSH4ShiftImm(val, operand, direction)
tmp = val
while tmp > 0
if tmp >= 16
$asm.puts "shl#{direction}16 #{operand.sh4Operand}"
tmp -= 16
elsif tmp >= 8
$asm.puts "shl#{direction}8 #{operand.sh4Operand}"
tmp -= 8
elsif tmp >= 2
$asm.puts "shl#{direction}2 #{operand.sh4Operand}"
tmp -= 2
else
$asm.puts "shl#{direction} #{operand.sh4Operand}"
tmp -= 1
end
end
end
def emitSH4BranchIfT(label, neg)
outlabel = LocalLabel.unique("branchIfT")
sh4opcode = neg ? "bt" : "bf"
$asm.puts "#{sh4opcode} #{LocalLabelReference.new(codeOrigin, outlabel).asmLabel}"
$asm.puts "bra #{label.asmLabel}"
$asm.puts "nop"
outlabel.lower("SH4")
end
def emitSH4IntCompare(cmpOpcode, operands)
$asm.puts "cmp/#{cmpOpcode} #{sh4Operands([operands[1], operands[0]])}"
end
def emitSH4CondBranch(cmpOpcode, neg, operands)
emitSH4IntCompare(cmpOpcode, operands)
emitSH4BranchIfT(operands[2], neg)
end
def emitSH4CompareSet(cmpOpcode, neg, operands)
emitSH4IntCompare(cmpOpcode, operands)
if !neg
$asm.puts "movt #{operands[2].sh4Operand}"
else
outlabel = LocalLabel.unique("compareSet")
$asm.puts "mov #0, #{operands[2].sh4Operand}"
$asm.puts "bt #{LocalLabelReference.new(codeOrigin, outlabel).asmLabel}"
$asm.puts "mov #1, #{operands[2].sh4Operand}"
outlabel.lower("SH4")
end
end
def emitSH4BranchIfNaN(operands)
raise "Invalid operands number (#{operands.size})" unless operands.size == 2
$asm.puts "fcmp/eq #{sh4Operands([operands[0], operands[0]])}"
$asm.puts "bf #{operands[1].asmLabel}"
end
def emitSH4DoubleCondBranch(cmpOpcode, neg, operands)
if cmpOpcode == "lt"
$asm.puts "fcmp/gt #{sh4Operands([operands[0], operands[1]])}"
else
$asm.puts "fcmp/#{cmpOpcode} #{sh4Operands([operands[1], operands[0]])}"
end
emitSH4BranchIfT(operands[2], neg)
end
class Instruction
def lowerSH4
$asm.comment codeOriginString
case opcode
when "addi", "addp"
if operands.size == 3
if operands[0].sh4Operand == operands[2].sh4Operand
$asm.puts "add #{sh4Operands([operands[1], operands[2]])}"
elsif operands[1].sh4Operand == operands[2].sh4Operand
$asm.puts "add #{sh4Operands([operands[0], operands[2]])}"
else
$asm.puts "mov #{sh4Operands([operands[0], operands[2]])}"
$asm.puts "add #{sh4Operands([operands[1], operands[2]])}"
end
else
$asm.puts "add #{sh4Operands(operands)}"
end
when "subi", "subp"
if operands.size == 3
if operands[1].sh4Operand == operands[2].sh4Operand
$asm.puts "neg #{sh4Operands([operands[2], operands[2]])}"
$asm.puts "add #{sh4Operands([operands[0], operands[2]])}"
else
$asm.puts "mov #{sh4Operands([operands[0], operands[2]])}"
$asm.puts "sub #{sh4Operands([operands[1], operands[2]])}"
end
else
if operands[0].is_a? Immediate
$asm.puts "add #{sh4Operands([Immediate.new(codeOrigin, -1 * operands[0].value), operands[1]])}"
else
$asm.puts "sub #{sh4Operands(operands)}"
end
end
when "muli", "mulp"
$asm.puts "mul.l #{sh4Operands(operands[0..1])}"
$asm.puts "sts macl, #{operands[-1].sh4Operand}"
when "negi", "negp"
if operands.size == 2
$asm.puts "neg #{sh4Operands(operands)}"
else
$asm.puts "neg #{sh4Operands([operands[0], operands[0]])}"
end
when "andi", "andp", "ori", "orp", "xori", "xorp"
raise "#{opcode} with #{operands.size} operands is not handled yet" unless operands.size == 2
sh4opcode = opcode[0..-2]
$asm.puts "#{sh4opcode} #{sh4Operands(operands)}"
when "shllx", "shlrx"
raise "Unhandled parameters for opcode #{opcode}" unless operands[0].is_a? Immediate
if operands[0].value == 1
$asm.puts "shl#{opcode[3, 1]} #{operands[1].sh4Operand}"
else
$asm.puts "shl#{opcode[3, 1]}#{operands[0].value} #{operands[1].sh4Operand}"
end
when "shld", "shad"
$asm.puts "#{opcode} #{sh4Operands(operands)}"
when "loaddReversedAndIncrementAddress"
# As we are little endian, we don't use "fmov @Rm, DRn" here.
$asm.puts "fmov.s #{operands[0].sh4OperandPostInc}, #{operands[1].sh4SingleLo}"
$asm.puts "fmov.s #{operands[0].sh4OperandPostInc}, #{operands[1].sh4SingleHi}"
when "storedReversedAndDecrementAddress"
# As we are little endian, we don't use "fmov DRm, @Rn" here.
$asm.puts "fmov.s #{operands[0].sh4SingleHi}, #{operands[1].sh4OperandPreDec}"
$asm.puts "fmov.s #{operands[0].sh4SingleLo}, #{operands[1].sh4OperandPreDec}"
when "ci2d"
$asm.puts "lds #{operands[0].sh4Operand}, fpul"
$asm.puts "float fpul, #{operands[1].sh4Operand}"
when "fii2d"
$asm.puts "lds #{operands[0].sh4Operand}, fpul"
$asm.puts "fsts fpul, #{operands[2].sh4SingleLo}"
$asm.puts "lds #{operands[1].sh4Operand}, fpul"
$asm.puts "fsts fpul, #{operands[2].sh4SingleHi}"
when "fd2ii"
$asm.puts "flds #{operands[0].sh4SingleLo}, fpul"
$asm.puts "sts fpul, #{operands[1].sh4Operand}"
$asm.puts "flds #{operands[0].sh4SingleHi}, fpul"
$asm.puts "sts fpul, #{operands[2].sh4Operand}"
when "addd", "subd", "muld", "divd"
sh4opcode = opcode[0..-2]
$asm.puts "f#{sh4opcode} #{sh4Operands(operands)}"
when "bcd2i"
$asm.puts "ftrc #{operands[0].sh4Operand}, fpul"
$asm.puts "sts fpul, #{operands[1].sh4Operand}"
$asm.puts "float fpul, #{SH4_TMP_FPRS[0].sh4Operand}"
$asm.puts "fcmp/eq #{sh4Operands([operands[0], SH4_TMP_FPRS[0]])}"
$asm.puts "bf #{operands[2].asmLabel}"
$asm.puts "tst #{sh4Operands([operands[1], operands[1]])}"
$asm.puts "bt #{operands[2].asmLabel}"
when "bdnan"
emitSH4BranchIfNaN(operands)
when "bdneq"
emitSH4DoubleCondBranch("eq", true, operands)
when "bdgteq"
emitSH4DoubleCondBranch("lt", true, operands)
when "bdlt"
emitSH4DoubleCondBranch("lt", false, operands)
when "bdlteq"
emitSH4DoubleCondBranch("gt", true, operands)
when "bdgt"
emitSH4DoubleCondBranch("gt", false, operands)
when "baddio", "baddpo", "bsubio", "bsubpo"
raise "#{opcode} with #{operands.size} operands is not handled yet" unless operands.size == 3
$asm.puts "#{opcode[1, 3]}v #{sh4Operands([operands[0], operands[1]])}"
$asm.puts "bt #{operands[2].asmLabel}"
when "bmulio", "bmulpo"
raise "Invalid operands number (#{operands.size})" unless operands.size == 5
$asm.puts "dmuls.l #{sh4Operands([operands[2], operands[3]])}"
$asm.puts "sts macl, #{operands[3].sh4Operand}"
$asm.puts "cmp/pz #{operands[3].sh4Operand}"
$asm.puts "movt #{operands[1].sh4Operand}"
$asm.puts "add #-1, #{operands[1].sh4Operand}"
$asm.puts "sts mach, #{operands[0].sh4Operand}"
$asm.puts "cmp/eq #{sh4Operands([operands[0], operands[1]])}"
$asm.puts "bf #{operands[4].asmLabel}"
when "btiz", "btpz", "btbz", "btinz", "btpnz", "btbnz"
if operands.size == 3
$asm.puts "tst #{sh4Operands([operands[0], operands[1]])}"
else
if operands[0].sh4Operand == "r0"
$asm.puts "cmp/eq #0, r0"
else
$asm.puts "tst #{sh4Operands([operands[0], operands[0]])}"
end
end
emitSH4BranchIfT(operands[-1], (opcode[-2, 2] == "nz"))
when "cieq", "cpeq", "cbeq"
emitSH4CompareSet("eq", false, operands)
when "cineq", "cpneq", "cbneq"
emitSH4CompareSet("eq", true, operands)
when "cib", "cpb", "cbb"
emitSH4CompareSet("hs", true, operands)
when "bieq", "bpeq", "bbeq"
emitSH4CondBranch("eq", false, operands)
when "bineq", "bpneq", "bbneq"
emitSH4CondBranch("eq", true, operands)
when "bib", "bpb", "bbb"
emitSH4CondBranch("hs", true, operands)
when "bia", "bpa", "bba"
emitSH4CondBranch("hi", false, operands)
when "bibeq", "bpbeq"
emitSH4CondBranch("hi", true, operands)
when "biaeq", "bpaeq"
emitSH4CondBranch("hs", false, operands)
when "bigteq", "bpgteq", "bbgteq"
emitSH4CondBranch("ge", false, operands)
when "bilt", "bplt", "bblt"
emitSH4CondBranch("ge", true, operands)
when "bigt", "bpgt", "bbgt"
emitSH4CondBranch("gt", false, operands)
when "bilteq", "bplteq", "bblteq"
emitSH4CondBranch("gt", true, operands)
when "bs"
$asm.puts "cmp/pz #{operands[0].sh4Operand}"
$asm.puts "bf #{operands[1].asmLabel}"
when "call"
if operands[0].is_a? LocalLabelReference
$asm.puts "bsr #{operands[0].asmLabel}"
$asm.puts "nop"
elsif operands[0].is_a? RegisterID or operands[0].is_a? SpecialRegister
emitSH4Branch("jsr", operands[0])
else
raise "Unhandled parameters for opcode #{opcode} at #{codeOriginString}"
end
when "jmp"
if operands[0].is_a? LocalLabelReference
$asm.puts "bra #{operands[0].asmLabel}"
$asm.puts "nop"
elsif operands[0].is_a? RegisterID or operands[0].is_a? SpecialRegister
emitSH4Branch("jmp", operands[0])
else
raise "Unhandled parameters for opcode #{opcode} at #{codeOriginString}"
end
when "ret"
$asm.puts "rts"
$asm.puts "nop"
when "loadb"
$asm.puts "mov.b #{sh4Operands(operands)}"
$asm.puts "extu.b #{sh4Operands([operands[1], operands[1]])}"
when "storeb"
$asm.puts "mov.b #{sh4Operands(operands)}"
when "loadh"
$asm.puts "mov.w #{sh4Operands(operands)}"
$asm.puts "extu.w #{sh4Operands([operands[1], operands[1]])}"
when "loadi", "loadis", "loadp", "storei", "storep"
$asm.puts "mov.l #{sh4Operands(operands)}"
when "move"
if operands[0].is_a? ConstPoolEntry
if operands[0].size == 16
$asm.puts "mov.w #{operands[0].labelref.asmLabel}, #{operands[1].sh4Operand}"
else
$asm.puts "mov.l #{operands[0].labelref.asmLabel}, #{operands[1].sh4Operand}"
end
else
$asm.puts "mov #{sh4Operands(operands)}"
end
when "leap"
if operands[0].is_a? BaseIndex
biop = operands[0]
$asm.puts "mov #{sh4Operands([biop.index, operands[1]])}"
if biop.scaleShift > 0
emitSH4ShiftImm(biop.scaleShift, operands[1], "l")
end
$asm.puts "add #{sh4Operands([biop.base, operands[1]])}"
if biop.offset.value != 0
$asm.puts "add #{sh4Operands([biop.offset, operands[1]])}"
end
elsif operands[0].is_a? Address
if operands[0].base != operands[1]
$asm.puts "mov #{sh4Operands([operands[0].base, operands[1]])}"
end
if operands[0].offset.value != 0
$asm.puts "add #{sh4Operands([operands[0].offset, operands[1]])}"
end
else
raise "Unhandled parameters for opcode #{opcode} at #{codeOriginString}"
end
when "ldspr"
$asm.puts "lds #{sh4Operands(operands)}, pr"
when "stspr"
$asm.puts "sts pr, #{sh4Operands(operands)}"
when "break"
# This special opcode always generates an illegal instruction exception.
$asm.puts ".word 0xfffd"
else
raise "Unhandled opcode #{opcode} at #{codeOriginString}"
end
end
end
|