Explorar o código

Simplify encode_ty() a little

Signed-off-by: Sean Young <sean@mess.org>
Sean Young %!s(int64=5) %!d(string=hai) anos
pai
achega
adc2cb2e1a
Modificáronse 4 ficheiros con 23 adicións e 49 borrados
  1. 9 11
      src/emit/ethabiencoder.rs
  2. 1 9
      src/emit/ewasm.rs
  3. 1 9
      src/emit/sabre.rs
  4. 12 20
      src/emit/substrate.rs

+ 9 - 11
src/emit/ethabiencoder.rs

@@ -20,6 +20,12 @@ impl EthAbiEncoder {
         arg: BasicValueEnum,
         data: &mut PointerValue<'a>,
     ) {
+        let arg = if ty.is_reference_type() {
+            contract.builder.build_load(arg.into_pointer_value(), "")
+        } else {
+            arg
+        };
+
         match &ty {
             resolver::Type::Bool
             | resolver::Type::Address
@@ -50,7 +56,7 @@ impl EthAbiEncoder {
                             .const_int(d.to_u64().unwrap(), false),
                         data,
                         |index, data| {
-                            let mut elem = unsafe {
+                            let elem = unsafe {
                                 contract.builder.build_gep(
                                     arg.into_pointer_value(),
                                     &[contract.context.i32_type().const_zero(), index],
@@ -60,11 +66,7 @@ impl EthAbiEncoder {
 
                             let ty = ty.array_deref();
 
-                            if ty.is_reference_type() {
-                                elem = contract.builder.build_load(elem, "").into_pointer_value();
-                            }
-
-                            self.encode_ty(contract, function, &ty, elem.into(), data);
+                            self.encode_ty(contract, function, &ty.deref(), elem.into(), data);
                         },
                     );
                 } else {
@@ -73,7 +75,7 @@ impl EthAbiEncoder {
             }
             resolver::Type::Struct(n) => {
                 for (i, field) in contract.ns.structs[*n].fields.iter().enumerate() {
-                    let mut elem = unsafe {
+                    let elem = unsafe {
                         contract.builder.build_gep(
                             arg.into_pointer_value(),
                             &[
@@ -84,10 +86,6 @@ impl EthAbiEncoder {
                         )
                     };
 
-                    if field.ty.is_reference_type() {
-                        elem = contract.builder.build_load(elem, "").into_pointer_value();
-                    }
-
                     self.encode_ty(contract, function, &field.ty, elem.into(), data);
                 }
             }

+ 1 - 9
src/emit/ewasm.rs

@@ -575,16 +575,8 @@ impl TargetRuntime for EwasmTarget {
         let mut data = encoded_data;
 
         for (i, arg) in spec.returns.iter().enumerate() {
-            let val = if arg.ty.is_reference_type() {
-                contract
-                    .builder
-                    .build_load(args[i].into_pointer_value(), "")
-            } else {
-                args[i]
-            };
-
             self.abi
-                .encode_ty(contract, function, &arg.ty, val, &mut data);
+                .encode_ty(contract, function, &arg.ty, args[i], &mut data);
         }
 
         (encoded_data, length)

+ 1 - 9
src/emit/sabre.rs

@@ -506,16 +506,8 @@ impl TargetRuntime for SabreTarget {
         let mut data = encoded_data;
 
         for (i, arg) in spec.returns.iter().enumerate() {
-            let val = if arg.ty.is_reference_type() {
-                contract
-                    .builder
-                    .build_load(args[i].into_pointer_value(), "")
-            } else {
-                args[i]
-            };
-
             self.abi
-                .encode_ty(contract, function, &arg.ty, val, &mut data);
+                .encode_ty(contract, function, &arg.ty, args[i], &mut data);
         }
 
         (encoded_data, length)

+ 12 - 20
src/emit/substrate.rs

@@ -604,6 +604,12 @@ impl SubstrateTarget {
         arg: BasicValueEnum<'a>,
         data: &mut PointerValue<'a>,
     ) {
+        let arg = if ty.is_reference_type() {
+            contract.builder.build_load(arg.into_pointer_value(), "")
+        } else {
+            arg
+        };
+
         match &ty {
             resolver::Type::Bool
             | resolver::Type::Address
@@ -634,7 +640,7 @@ impl SubstrateTarget {
                             .const_int(d.to_u64().unwrap(), false),
                         data,
                         |index, data| {
-                            let mut elem = unsafe {
+                            let elem = unsafe {
                                 contract.builder.build_gep(
                                     arg.into_pointer_value(),
                                     &[contract.context.i32_type().const_zero(), index],
@@ -644,11 +650,7 @@ impl SubstrateTarget {
 
                             let ty = ty.array_deref();
 
-                            if ty.is_reference_type() {
-                                elem = contract.builder.build_load(elem, "").into_pointer_value()
-                            }
-
-                            self.encode_ty(contract, function, &ty, elem.into(), data);
+                            self.encode_ty(contract, function, &ty.deref(), elem.into(), data);
                         },
                     );
                 } else {
@@ -657,7 +659,7 @@ impl SubstrateTarget {
             }
             resolver::Type::Struct(n) => {
                 for (i, field) in contract.ns.structs[*n].fields.iter().enumerate() {
-                    let mut elem = unsafe {
+                    let elem = unsafe {
                         contract.builder.build_gep(
                             arg.into_pointer_value(),
                             &[
@@ -668,10 +670,6 @@ impl SubstrateTarget {
                         )
                     };
 
-                    if field.ty.is_reference_type() {
-                        elem = contract.builder.build_load(elem, "").into_pointer_value();
-                    }
-
                     self.encode_ty(contract, function, &field.ty, elem.into(), data);
                 }
             }
@@ -1098,6 +1096,7 @@ impl TargetRuntime for SubstrateTarget {
         args: &[BasicValueEnum<'b>],
         spec: &resolver::FunctionDecl,
     ) -> (PointerValue<'b>, IntValue<'b>) {
+        // first calculate how much memory we need to allocate
         let mut length = contract.context.i32_type().const_zero();
 
         for (i, field) in spec.returns.iter().enumerate() {
@@ -1128,18 +1127,11 @@ impl TargetRuntime for SubstrateTarget {
             .unwrap()
             .into_pointer_value();
 
+        // now encode each of the arguments
         let mut argsdata = data;
 
         for (i, arg) in spec.returns.iter().enumerate() {
-            let val = if arg.ty.is_reference_type() {
-                contract
-                    .builder
-                    .build_load(args[i].into_pointer_value(), "")
-            } else {
-                args[i]
-            };
-
-            self.encode_ty(contract, function, &arg.ty, val, &mut argsdata);
+            self.encode_ty(contract, function, &arg.ty, args[i], &mut argsdata);
         }
 
         let length = contract