Merge pull request #10323 from ethereum/ewasm-polyfill-bswap

[ewasm] Polyfill: Rename endian_swap -> bswap.
This commit is contained in:
chriseth 2020-11-18 11:23:47 +01:00 committed by GitHub
commit ae39c96c28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 81 deletions

View File

@ -59,20 +59,20 @@ function u256_to_address(x1, x2, x3, x4) -> r1, r2, r3 {
r3 := x4
}
function endian_swap_16(x) -> y {
function bswap16(x) -> y {
let hi := i64.and(i64.shl(x, 8), 0xff00)
let lo := i64.and(i64.shr_u(x, 8), 0xff)
y := i64.or(hi, lo)
}
function endian_swap_32(x) -> y {
let hi := i64.shl(endian_swap_16(x), 16)
let lo := endian_swap_16(i64.shr_u(x, 16))
function bswap32(x) -> y {
let hi := i64.shl(bswap16(x), 16)
let lo := bswap16(i64.shr_u(x, 16))
y := i64.or(hi, lo)
}
function endian_swap(x) -> y {
let hi := i64.shl(endian_swap_32(x), 32)
let lo := endian_swap_32(i64.shr_u(x, 32))
function bswap64(x) -> y {
let hi := i64.shl(bswap32(x), 32)
let lo := bswap32(i64.shr_u(x, 32))
y := i64.or(hi, lo)
}

View File

@ -161,10 +161,10 @@ function mload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
}
function mload_internal(pos:i32) -> z1, z2, z3, z4 {
z1 := endian_swap(i64.load(pos))
z2 := endian_swap(i64.load(i32.add(pos, 8:i32)))
z3 := endian_swap(i64.load(i32.add(pos, 16:i32)))
z4 := endian_swap(i64.load(i32.add(pos, 24:i32)))
z1 := bswap64(i64.load(pos))
z2 := bswap64(i64.load(i32.add(pos, 8:i32)))
z3 := bswap64(i64.load(i32.add(pos, 16:i32)))
z4 := bswap64(i64.load(i32.add(pos, 24:i32)))
}
function mstore(x1, x2, x3, x4, y1, y2, y3, y4) {
@ -172,10 +172,10 @@ function mstore(x1, x2, x3, x4, y1, y2, y3, y4) {
}
function mstore_internal(pos:i32, y1, y2, y3, y4) {
i64.store(pos, endian_swap(y1))
i64.store(i32.add(pos, 8:i32), endian_swap(y2))
i64.store(i32.add(pos, 16:i32), endian_swap(y3))
i64.store(i32.add(pos, 24:i32), endian_swap(y4))
i64.store(pos, bswap64(y1))
i64.store(i32.add(pos, 8:i32), bswap64(y2))
i64.store(i32.add(pos, 16:i32), bswap64(y3))
i64.store(i32.add(pos, 24:i32), bswap64(y4))
}
function mstore_address(pos:i32, a1, a2, a3, a4) {

View File

@ -19,26 +19,26 @@ object "object" {
mstore_internal(32:i32, _1, _1, _1, 1)
eth.storageStore(0:i32, 32:i32)
}
function endian_swap_16(x) -> y
function bswap16(x) -> y
{
y := i64.or(i64.and(i64.shl(x, 8), 0xff00), i64.and(i64.shr_u(x, 8), 0xff))
}
function endian_swap_32(x) -> y
function bswap32(x) -> y
{
let hi := i64.shl(endian_swap_16(x), 16)
y := i64.or(hi, endian_swap_16(i64.shr_u(x, 16)))
let hi := i64.shl(bswap16(x), 16)
y := i64.or(hi, bswap16(i64.shr_u(x, 16)))
}
function endian_swap(x) -> y
function bswap64(x) -> y
{
let hi := i64.shl(endian_swap_32(x), 32)
y := i64.or(hi, endian_swap_32(i64.shr_u(x, 32)))
let hi := i64.shl(bswap32(x), 32)
y := i64.or(hi, bswap32(i64.shr_u(x, 32)))
}
function mstore_internal(pos:i32, y1, y2, y3, y4)
{
i64.store(pos, endian_swap(y1))
i64.store(i32.add(pos, 8:i32), endian_swap(y2))
i64.store(i32.add(pos, 16:i32), endian_swap(y3))
i64.store(i32.add(pos, 24:i32), endian_swap(y4))
i64.store(pos, bswap64(y1))
i64.store(i32.add(pos, 8:i32), bswap64(y2))
i64.store(i32.add(pos, 16:i32), bswap64(y3))
i64.store(i32.add(pos, 24:i32), bswap64(y4))
}
}
}
@ -63,7 +63,7 @@ Text representation:
)
)
(func $endian_swap_16
(func $bswap16
(param $x i64)
(result i64)
(local $y i64)
@ -74,27 +74,27 @@ Text representation:
(local.get $y)
)
(func $endian_swap_32
(func $bswap32
(param $x i64)
(result i64)
(local $y i64)
(local $hi i64)
(block $label__2
(local.set $hi (i64.shl (call $endian_swap_16 (local.get $x)) (i64.const 16)))
(local.set $y (i64.or (local.get $hi) (call $endian_swap_16 (i64.shr_u (local.get $x) (i64.const 16)))))
(local.set $hi (i64.shl (call $bswap16 (local.get $x)) (i64.const 16)))
(local.set $y (i64.or (local.get $hi) (call $bswap16 (i64.shr_u (local.get $x) (i64.const 16)))))
)
(local.get $y)
)
(func $endian_swap
(func $bswap64
(param $x i64)
(result i64)
(local $y i64)
(local $hi i64)
(block $label__3
(local.set $hi (i64.shl (call $endian_swap_32 (local.get $x)) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (call $endian_swap_32 (i64.shr_u (local.get $x) (i64.const 32)))))
(local.set $hi (i64.shl (call $bswap32 (local.get $x)) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (call $bswap32 (i64.shr_u (local.get $x) (i64.const 32)))))
)
(local.get $y)
@ -107,10 +107,10 @@ Text representation:
(param $y3 i64)
(param $y4 i64)
(block $label__4
(i64.store (local.get $pos) (call $endian_swap (local.get $y1)))
(i64.store (i32.add (local.get $pos) (i32.const 8)) (call $endian_swap (local.get $y2)))
(i64.store (i32.add (local.get $pos) (i32.const 16)) (call $endian_swap (local.get $y3)))
(i64.store (i32.add (local.get $pos) (i32.const 24)) (call $endian_swap (local.get $y4)))
(i64.store (local.get $pos) (call $bswap64 (local.get $y1)))
(i64.store (i32.add (local.get $pos) (i32.const 8)) (call $bswap64 (local.get $y2)))
(i64.store (i32.add (local.get $pos) (i32.const 16)) (call $bswap64 (local.get $y3)))
(i64.store (i32.add (local.get $pos) (i32.const 24)) (call $bswap64 (local.get $y4)))
)
)

View File

@ -106,29 +106,29 @@ object "object" {
default { z := 1:i32 }
z4 := i64.extend_i32_u(z)
}
function endian_swap_16(x) -> y
function bswap16(x) -> y
{
y := i64.or(i64.and(i64.shl(x, 8), 0xff00), i64.and(i64.shr_u(x, 8), 0xff))
}
function endian_swap_32(x) -> y
function bswap32(x) -> y
{
let hi := i64.shl(endian_swap_16(x), 16)
y := i64.or(hi, endian_swap_16(i64.shr_u(x, 16)))
let hi := i64.shl(bswap16(x), 16)
y := i64.or(hi, bswap16(i64.shr_u(x, 16)))
}
function endian_swap(x) -> y
function bswap64(x) -> y
{
let hi := i64.shl(endian_swap_32(x), 32)
y := i64.or(hi, endian_swap_32(i64.shr_u(x, 32)))
let hi := i64.shl(bswap32(x), 32)
y := i64.or(hi, bswap32(i64.shr_u(x, 32)))
}
function calldataload(x1, x2, x3, x4) -> z1, z2, z3, z4
{
if i64.ne(0, i64.or(i64.or(x1, x2), x3)) { unreachable() }
if i64.ne(0, i64.shr_u(x4, 32)) { unreachable() }
eth.callDataCopy(0:i32, i32.wrap_i64(x4), 32:i32)
let z1_1 := endian_swap(i64.load(0:i32))
let z2_1 := endian_swap(i64.load(i32.add(0:i32, 8:i32)))
let z3_1 := endian_swap(i64.load(i32.add(0:i32, 16:i32)))
let z4_1 := endian_swap(i64.load(i32.add(0:i32, 24:i32)))
let z1_1 := bswap64(i64.load(0:i32))
let z2_1 := bswap64(i64.load(i32.add(0:i32, 8:i32)))
let z3_1 := bswap64(i64.load(i32.add(0:i32, 16:i32)))
let z4_1 := bswap64(i64.load(i32.add(0:i32, 24:i32)))
z1 := z1_1
z2 := z2_1
z3 := z3_1
@ -136,10 +136,10 @@ object "object" {
}
function mstore_internal(pos:i32, y1, y2, y3, y4)
{
i64.store(pos, endian_swap(y1))
i64.store(i32.add(pos, 8:i32), endian_swap(y2))
i64.store(i32.add(pos, 16:i32), endian_swap(y3))
i64.store(i32.add(pos, 24:i32), endian_swap(y4))
i64.store(pos, bswap64(y1))
i64.store(i32.add(pos, 8:i32), bswap64(y2))
i64.store(i32.add(pos, 16:i32), bswap64(y3))
i64.store(i32.add(pos, 24:i32), bswap64(y4))
}
function sstore(x1, x2, x3, x4, y1, y2, y3, y4)
{
@ -437,7 +437,7 @@ Text representation:
(local.get $z4)
)
(func $endian_swap_16
(func $bswap16
(param $x i64)
(result i64)
(local $y i64)
@ -448,27 +448,27 @@ Text representation:
(local.get $y)
)
(func $endian_swap_32
(func $bswap32
(param $x i64)
(result i64)
(local $y i64)
(local $hi i64)
(block $label__16
(local.set $hi (i64.shl (call $endian_swap_16 (local.get $x)) (i64.const 16)))
(local.set $y (i64.or (local.get $hi) (call $endian_swap_16 (i64.shr_u (local.get $x) (i64.const 16)))))
(local.set $hi (i64.shl (call $bswap16 (local.get $x)) (i64.const 16)))
(local.set $y (i64.or (local.get $hi) (call $bswap16 (i64.shr_u (local.get $x) (i64.const 16)))))
)
(local.get $y)
)
(func $endian_swap
(func $bswap64
(param $x i64)
(result i64)
(local $y i64)
(local $hi i64)
(block $label__17
(local.set $hi (i64.shl (call $endian_swap_32 (local.get $x)) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (call $endian_swap_32 (i64.shr_u (local.get $x) (i64.const 32)))))
(local.set $hi (i64.shl (call $bswap32 (local.get $x)) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (call $bswap32 (i64.shr_u (local.get $x) (i64.const 32)))))
)
(local.get $y)
@ -494,10 +494,10 @@ Text representation:
(if (i64.ne (i64.const 0) (i64.shr_u (local.get $x4) (i64.const 32))) (then
(unreachable)))
(call $eth.callDataCopy (i32.const 0) (i32.wrap_i64 (local.get $x4)) (i32.const 32))
(local.set $z1_1 (call $endian_swap (i64.load (i32.const 0))))
(local.set $z2_1 (call $endian_swap (i64.load (i32.add (i32.const 0) (i32.const 8)))))
(local.set $z3_1 (call $endian_swap (i64.load (i32.add (i32.const 0) (i32.const 16)))))
(local.set $z4_1 (call $endian_swap (i64.load (i32.add (i32.const 0) (i32.const 24)))))
(local.set $z1_1 (call $bswap64 (i64.load (i32.const 0))))
(local.set $z2_1 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 8)))))
(local.set $z3_1 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 16)))))
(local.set $z4_1 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 24)))))
(local.set $z1 (local.get $z1_1))
(local.set $z2 (local.get $z2_1))
(local.set $z3 (local.get $z3_1))
@ -517,10 +517,10 @@ Text representation:
(param $y3 i64)
(param $y4 i64)
(block $label__19
(i64.store (local.get $pos) (call $endian_swap (local.get $y1)))
(i64.store (i32.add (local.get $pos) (i32.const 8)) (call $endian_swap (local.get $y2)))
(i64.store (i32.add (local.get $pos) (i32.const 16)) (call $endian_swap (local.get $y3)))
(i64.store (i32.add (local.get $pos) (i32.const 24)) (call $endian_swap (local.get $y4)))
(i64.store (local.get $pos) (call $bswap64 (local.get $y1)))
(i64.store (i32.add (local.get $pos) (i32.const 8)) (call $bswap64 (local.get $y2)))
(i64.store (i32.add (local.get $pos) (i32.const 16)) (call $bswap64 (local.get $y3)))
(i64.store (i32.add (local.get $pos) (i32.const 24)) (call $bswap64 (local.get $y4)))
)
)

View File

@ -1,6 +1,6 @@
{"contracts":{"A":{"C":{"ewasm":{"wasm":"0061736d01000000013a0860000060017e017e60047e7e7e7e017f60087e7e7e7e7e7e7e7e00600c7e7e7e7e7e7e7e7e7e7e7e7e0060017f0060027f7f0060037f7f7f0002510408657468657265756d08636f6465436f7079000708657468657265756d06726576657274000608657468657265756d0c67657443616c6c56616c7565000508657468657265756d0666696e6973680006030a090002020101010403030503010001060100071102066d656d6f72790200046d61696e0004009d030c435f325f6465706c6f7965640061736d0100000001160460000060017e017e60047e7e7e7e017f60027f7f0002130108657468657265756d067265766572740003030504000201010503010001060100071102066d656d6f72790200046d61696e00010ab60204ca0104017e027f057e037f02404200210020002000200042c00010022101200141c0006a210220022001490440000b20001003421086210320032000421088100384422086210420042000422088100484210520022005370000200241086a2005370000200241106a20053700004280011003421086210620064280014210881003844220862107200241186a2007428001422088100484370000200020002000200010022108200020002000200010022109200941c0006a210a200a2009490440000b200a200810000b0b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100342108621022002200042108810038421010b20010b0aec0309dc0103017e027f057e02404200210020002000200042c00010052101200141c0006a210220022001490440000b20001009210320022003370000200241086a2003370000200241106a2003370000200241186a428001100937000041001002410029000010092104410041086a29000010092105410041106a2900001009210620042005842006410041186a290000100984845045044020002000200020002000200020002000100c0b4290032107200020002000200020002000200042ce012000200020002007100a20002000200020002000200020002007100b0b0b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b2601027f0240200020012002200310052105200541c0006a210420042005490440000b0b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100742108621022002200042108810078421010b20010b1e01027e02402000100842208621022002200042208810088421010b20010b25000240200020012002200310062004200520062007100520082009200a200b100510000b0b1b000240200020012002200310062004200520062007100510030b0b1b000240200020012002200310062004200520062007100510010b0b","wast":"(module
;; custom section for sub-module
;; The Keccak-256 hash of the text representation of \"C_2_deployed\": f03f5b9154b9eb6803a947177e38e92e2860de95e90ba0e75eb71a58f18ed589
;; The Keccak-256 hash of the text representation of \"C_2_deployed\": 0289c074ac70ccfdbeb7817862087cc066a9f7707de1a981bb8b5b12dd2ce4e9
;; (@custom \"C_2_deployed\" \"0061736d0100000001160460000060017e017e60047e7e7e7e017f60027f7f0002130108657468657265756d067265766572740003030504000201010503010001060100071102066d656d6f72790200046d61696e00010ab60204ca0104017e027f057e037f02404200210020002000200042c00010022101200141c0006a210220022001490440000b20001003421086210320032000421088100384422086210420042000422088100484210520022005370000200241086a2005370000200241106a20053700004280011003421086210620064280014210881003844220862107200241186a2007428001422088100484370000200020002000200010022108200020002000200010022109200941c0006a210a200a2009490440000b200a200810000b0b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100342108621022002200042108810038421010b20010b\")
(import \"ethereum\" \"codeCopy\" (func $eth.codeCopy (param i32 i32 i32)))
(import \"ethereum\" \"revert\" (func $eth.revert (param i32 i32)))
@ -24,16 +24,16 @@
(local.set $r (i32.add (local.get $p) (i32.const 64)))
(if (i32.lt_u (local.get $r) (local.get $p)) (then
(unreachable)))
(local.set $_2 (call $endian_swap (local.get $_1)))
(local.set $_2 (call $bswap64 (local.get $_1)))
(i64.store (local.get $r) (local.get $_2))
(i64.store (i32.add (local.get $r) (i32.const 8)) (local.get $_2))
(i64.store (i32.add (local.get $r) (i32.const 16)) (local.get $_2))
(i64.store (i32.add (local.get $r) (i32.const 24)) (call $endian_swap (i64.const 128)))
(i64.store (i32.add (local.get $r) (i32.const 24)) (call $bswap64 (i64.const 128)))
(call $eth.getCallValue (i32.const 0))
(local.set $z1 (call $endian_swap (i64.load (i32.const 0))))
(local.set $z2 (call $endian_swap (i64.load (i32.add (i32.const 0) (i32.const 8)))))
(local.set $z3 (call $endian_swap (i64.load (i32.add (i32.const 0) (i32.const 16)))))
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $z1) (local.get $z2)) (i64.or (local.get $z3) (call $endian_swap (i64.load (i32.add (i32.const 0) (i32.const 24)))))))) (then
(local.set $z1 (call $bswap64 (i64.load (i32.const 0))))
(local.set $z2 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 8)))))
(local.set $z3 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 16)))))
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $z1) (local.get $z2)) (i64.or (local.get $z3) (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 24)))))))) (then
(call $revert (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1))))
(local.set $_3 (datasize \"C_2_deployed\"))
(call $codecopy (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1) (dataoffset \"C_2_deployed\") (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_3))
@ -77,7 +77,7 @@
(local.get $r)
)
(func $endian_swap_16
(func $bswap16
(param $x i64)
(result i64)
(local $y i64)
@ -88,27 +88,27 @@
(local.get $y)
)
(func $endian_swap_32
(func $bswap32
(param $x i64)
(result i64)
(local $y i64)
(local $hi i64)
(block $label__4
(local.set $hi (i64.shl (call $endian_swap_16 (local.get $x)) (i64.const 16)))
(local.set $y (i64.or (local.get $hi) (call $endian_swap_16 (i64.shr_u (local.get $x) (i64.const 16)))))
(local.set $hi (i64.shl (call $bswap16 (local.get $x)) (i64.const 16)))
(local.set $y (i64.or (local.get $hi) (call $bswap16 (i64.shr_u (local.get $x) (i64.const 16)))))
)
(local.get $y)
)
(func $endian_swap
(func $bswap64
(param $x i64)
(result i64)
(local $y i64)
(local $hi i64)
(block $label__5
(local.set $hi (i64.shl (call $endian_swap_32 (local.get $x)) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (call $endian_swap_32 (i64.shr_u (local.get $x) (i64.const 32)))))
(local.set $hi (i64.shl (call $bswap32 (local.get $x)) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (call $bswap32 (i64.shr_u (local.get $x) (i64.const 32)))))
)
(local.get $y)