expression.sol 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // RUN: --target solana --emit cfg -Onone --no-cse
  2. uint128 constant global_cte = 5;
  3. contract testing {
  4. // BEGIN-CHECK: testing::testing::function::boolLiteral
  5. function boolLiteral() public pure {
  6. assembly {
  7. // CHECK: ty:uint256 %x = uint256(true)
  8. let x := true
  9. // CHECK: ty:bool %ss = (uint256 0 != uint256 5)
  10. let ss : bool := 5
  11. // CHECK: ty:uint256 %y = uint256(false)
  12. let y := false
  13. // CHECK: uint64(false)
  14. let r : u64 := false
  15. // CHECK: uint256 1
  16. let s := true :u128
  17. // CHECK: ty:uint32 %p = uint32 0
  18. let p :u32 := false :u64
  19. }
  20. }
  21. // BEGIN-CHECK: testing::testing::function::numberLiteral
  22. function numberLiteral() public pure {
  23. assembly {
  24. // CHECK: ty:int32 %x = int32 90
  25. let x : s32 := 90
  26. // CHECK: ty:uint256 %y = uint256 128
  27. let y := 0x80
  28. // ty:uint256 %y = uint256 128
  29. let z := 0x80 : u8
  30. }
  31. }
  32. // BEGIN-CHECK: testing::testing::function::stringLiteral
  33. function stringLiteral() public pure {
  34. assembly {
  35. // CHECK: ty:uint256 %x = uint256 427070744165
  36. let x := "coffe"
  37. // CHECK: ty:uint256 %y = uint256 831486
  38. let y := hex"0caffe"
  39. }
  40. }
  41. struct LocalTest {
  42. uint32 c;
  43. }
  44. // BEGIN-CHECK: testing::testing::function::yulLocalVariable__uint256
  45. function yulLocalVariable(uint256 a) public pure {
  46. assembly {
  47. // CHECK: ty:uint256 %x = (uint256 2 + (arg #0))
  48. let x := add(2, a)
  49. // CHECK: ty:uint256 %y = (uint256 2 + (arg #0))
  50. let y := x
  51. }
  52. }
  53. uint32 constant contract_cte = 787;
  54. // BEGIN-CHECK: testing::testing::function::constantVariable
  55. function constantVariable() public pure {
  56. assembly {
  57. // CHECK: ty:uint256 %p1 = uint256 5
  58. let p1 := global_cte
  59. // CHECK: ty:uint256 %p2 = uint256 787
  60. let p2 := contract_cte
  61. }
  62. }
  63. // BEGIN-CHECK: testing::testing::function::solidityLocalVariable
  64. function solidityLocalVariable() public pure {
  65. int32 a = 1;
  66. int[] vec;
  67. int[] memory mem_vec = vec;
  68. int[4] cte_vec = [int(1), 2, 3, 4];
  69. int[4] memory mem_cte_vec = [int(1), 2, 3, 4];
  70. string b = "abc";
  71. LocalTest struct_test = LocalTest({c: 2});
  72. LocalTest memory mem_struct_test = LocalTest({c: 2});
  73. assembly {
  74. // CHECK: ty:uint256 %k = uint256 1
  75. let k := a
  76. // CHECK: ty:uint256 %l = (zext uint256 uint64(%vec))
  77. let l := vec
  78. // CHECK: ty:uint256 %m = (zext uint256 uint64(%mem_vec))
  79. let m := mem_vec
  80. // CHECK: ty:uint256 %n = (zext uint256 uint64(%cte_vec))
  81. let n := cte_vec
  82. // CHECK: ty:uint256 %o = (zext uint256 uint64(%mem_cte_vec))
  83. let o := mem_cte_vec
  84. // CHECK: ty:uint256 %p = (zext uint256 uint64(%b))
  85. let p := b
  86. // CHECK: ty:uint256 %r = (zext uint256 uint64(%struct_test))
  87. let r := struct_test
  88. // CHECK: ty:uint256 %s = (zext uint256 uint64(%mem_struct_test))
  89. let s := mem_struct_test
  90. }
  91. }
  92. int[] storage_vec;
  93. int[] storage_vec_2;
  94. // BEGIN-CHECK: testing::testing::function::memberAccess__uint64:_uint64:3
  95. function memberAccess(uint64[] calldata vl, uint64[3] calldata vl_2) public view {
  96. int[] storage l_storage_vec = storage_vec_2;
  97. function () external fPtr = this.solidityLocalVariable;
  98. assembly {
  99. // CHECK: ty:uint256 %k = uint256 16
  100. let k := storage_vec.slot
  101. // CHECK: ty:uint256 %l = uint32 20
  102. let l := l_storage_vec.slot
  103. // CHECK: ty:uint256 %m = uint256 0
  104. let m := storage_vec.offset
  105. // CHECK: ty:uint256 %n = uint256 0
  106. let n := l_storage_vec.offset
  107. // CHECK: ty:uint256 %o = uint256((arg #0))
  108. let o := vl.offset
  109. // CHECK: ty:uint256 %p = (zext uint256 (builtin ArrayLength ((arg #0))))
  110. let p := vl.length
  111. // CHECK: ty:uint256 %q = uint256((load (struct %fPtr field 1)))
  112. let q := fPtr.address
  113. // CHECK: ty:uint256 %r = (zext uint256 bytes8((load (struct %fPtr field 0))))
  114. let r := fPtr.selector
  115. // CHECK: ty:uint256 %s = (zext uint256 uint64((arg #1)))
  116. let s := vl_2
  117. }
  118. }
  119. // BEGIN-CHECK: testing::testing::function::exprFuncCall__uint32
  120. function exprFuncCall(uint32 a) public pure {
  121. assembly {
  122. function get(a) -> ret {
  123. ret := sub(a, 2)
  124. }
  125. function doSmth() {
  126. let y := 9
  127. }
  128. // CHECK: ty:int32 %k = (trunc int32 ((zext uint256 (arg #0)) + uint256 2))
  129. let k : s32 := add(a, 2)
  130. // CHECK: %ret.temp.58 = call testing::yul_function_0::get (sext uint256 (trunc int32 ((zext uint256 (arg #0)) + uint256 2)))
  131. let x := get(k)
  132. // CHECK: ty:uint256 %x = %ret.temp.58
  133. // CHECK: %ret1.temp.59, %ret2.temp.60 = call testing::yul_function_2::multipleReturns %x, (trunc int32 ((zext uint256 (arg #0)) + uint256 2))
  134. let l, m := multipleReturns(x, k)
  135. // CHECK: ty:uint256 %l = (zext uint256 %ret1.temp.59)
  136. // CHECK: ty:uint256 %m = (sext uint256 %ret2.temp.60)
  137. // CHECK: = call testing::yul_function_1::doSmth
  138. doSmth()
  139. function multipleReturns(a, v : s32) -> ret1 : u8, ret2 : s64 {
  140. ret1 := add(a, v)
  141. ret2 := mul(a, v)
  142. }
  143. }
  144. }
  145. }