2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-10-11 22:37:41 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Arch
|
|
|
|
|
|
|
|
#
|
2005-12-30 20:26:06 +00:00
|
|
|
# Everything here is mostly stolen from vlad's perl sparc stuff
|
2005-10-11 22:37:41 +00:00
|
|
|
#
|
|
|
|
module Sparc
|
|
|
|
|
|
|
|
#
|
|
|
|
# Register number constants
|
|
|
|
#
|
|
|
|
RegisterNumber =
|
|
|
|
{
|
|
|
|
'g0' => 0, 'g1' => 1, 'g2' => 2, 'g3' => 3,
|
|
|
|
'g4' => 4, 'g5' => 5, 'g6' => 6, 'g7' => 7,
|
|
|
|
'o0' => 8, 'o1' => 9, 'o2' => 10, 'o3' => 11,
|
|
|
|
'o4' => 12, 'o5' => 13, 'o6' => 14, 'o7' => 15,
|
|
|
|
'l0' => 16, 'l1' => 17, 'l2' => 18, 'l3' => 19,
|
|
|
|
'l4' => 20, 'l5' => 21, 'l6' => 22, 'l7' => 23,
|
|
|
|
'i0' => 24, 'i1' => 25, 'i2' => 26, 'i3' => 27,
|
|
|
|
'i4' => 28, 'i5' => 29, 'i6' => 30, 'i7' => 31,
|
2005-12-30 20:26:06 +00:00
|
|
|
'sp' => 14, 'fp' => 30,
|
2005-11-02 23:03:02 +00:00
|
|
|
} # :nodoc:
|
2005-10-11 22:37:41 +00:00
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
2005-12-30 20:26:06 +00:00
|
|
|
# Encodes a SETHI instruction with the value 'constant' being put into 'dst' register
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
2005-10-11 22:37:41 +00:00
|
|
|
def self.sethi(constant, dst)
|
|
|
|
[
|
|
|
|
(RegisterNumber[dst] << 25) |
|
|
|
|
(4 << 22) |
|
|
|
|
(constant >> 10)
|
|
|
|
].pack('N')
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
2005-12-30 20:26:06 +00:00
|
|
|
# Encodes an OR instruction with the value 'constant' being OR'ed with the 'src' register into the 'dst' register
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
2005-10-11 22:37:41 +00:00
|
|
|
def self.ori(src, constant, dst)
|
|
|
|
[
|
|
|
|
(2 << 30) |
|
|
|
|
(RegisterNumber[dst] << 25) |
|
|
|
|
(2 << 19) |
|
|
|
|
(RegisterNumber[src] << 14) |
|
|
|
|
(1 << 13) |
|
|
|
|
(constant & 0x1fff)
|
|
|
|
].pack('N')
|
|
|
|
end
|
|
|
|
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
2005-12-30 20:26:06 +00:00
|
|
|
# Puts 'constant' into the 'dst' register using as few instructions as possible by checking the size of the value.
|
|
|
|
# XXX: signedness support
|
2005-11-02 23:03:02 +00:00
|
|
|
#
|
2005-10-11 22:37:41 +00:00
|
|
|
def self.set(constant, dst)
|
2005-12-30 20:26:06 +00:00
|
|
|
if (constant <= 4095 and constant >= 0)
|
2005-10-11 22:37:41 +00:00
|
|
|
ori('g0', constant, dst)
|
2007-07-07 03:12:42 +00:00
|
|
|
elsif (constant & 0x3ff != 0)
|
2005-10-11 22:37:41 +00:00
|
|
|
set_dword(constant, dst)
|
|
|
|
else
|
|
|
|
sethi(constant, dst)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-12-30 20:26:06 +00:00
|
|
|
#
|
|
|
|
# Puts 'constant' into the 'dst' register using both sethi and ori (necessary to use both uncessarily in some cases with encoders)
|
|
|
|
#
|
2005-10-11 22:37:41 +00:00
|
|
|
def self.set_dword(constant, dst)
|
|
|
|
sethi(constant, dst) + ori(dst, constant & 0x3ff, dst)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end end
|