array_boundary_opt.sol 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // RUN: --target substrate --emit cfg
  2. contract Array_bound_Test {
  3. // BEGIN-CHECK: Array_bound_Test::Array_bound_Test::function::array_bound__uint256:
  4. function array_bound(
  5. uint256[] b,
  6. uint256 size,
  7. uint32 size32
  8. ) public pure returns (uint256) {
  9. // CHECK: ty:uint32 %1.cse_temp = (trunc uint32 (arg #1))
  10. // CHECK: ty:uint32 %array_length.temp.23 = %1.cse_temp
  11. uint256[] a = new uint256[](size);
  12. // CHECK: ty:uint32 %array_length.temp.24 = (arg #2)
  13. uint256[] c = new uint256[](size32);
  14. // CHECK: ty:uint32 %array_length.temp.25 = uint32 20
  15. uint256[] d = new uint256[](20);
  16. // CHECK: ty:uint32 %array_length.temp.23 = (%1.cse_temp + uint32 1)
  17. a.push();
  18. // CHECK: ty:uint32 %array_length.temp.24 = ((arg #2) - uint32 1)
  19. c.pop();
  20. // CHECK: ty:uint32 %array_length.temp.25 = uint32 21
  21. d.push();
  22. // CHECK: return (zext uint256 (((%array_length.temp.23 + (builtin ArrayLength ((arg #0)))) + ((arg #2) - uint32 1)) + uint32 21))
  23. return a.length + b.length + c.length + d.length;
  24. }
  25. // BEGIN-CHECK: function Array_bound_Test::Array_bound_Test::function::test__bool
  26. function test(bool cond) public returns (uint32) {
  27. bool[] b = new bool[](210);
  28. if (cond) {
  29. // CHECK: ty:uint32 %array_length.temp.30 = uint32 211
  30. b.push(true);
  31. }
  32. // CHECK: return %array_length.temp.30
  33. return b.length;
  34. }
  35. // BEGIN_CHECK: function Array_bound_Test::Array_bound_Test::function::test_for_loop
  36. function test_for_loop() public {
  37. uint256[] a = new uint256[](20);
  38. a.push(1);
  39. uint256 sesa = 0;
  40. // CHECK: branchcond (uint32 20 >= uint32 21), block5, block6
  41. // CHECK: branchcond (unsigned less %i < uint256 21), block1, block4
  42. for (uint256 i = 0; i < a.length; i++) {
  43. sesa = sesa + a[20];
  44. }
  45. assert(sesa == 21);
  46. }
  47. // BEGIN_CHECK: function Array_bound_Test::Array_bound_Test::function::test_without_size
  48. function test_without_size(uint[] dyn_param) public returns (uint32) {
  49. uint256[] vec;
  50. vec.push(3);
  51. uint256[] vec2;
  52. vec2=vec;
  53. uint256[] a = new uint256[](20);
  54. uint256[] b = dyn_param;
  55. uint256[] c = vec;
  56. // CHECK: return ((uint32 21 + (builtin ArrayLength ((arg #0)))) + uint32 1)
  57. return vec2.length + a.length + b.length + c.length;
  58. }
  59. // BEGIN_CHECK: function Array_bound_Test::Array_bound_Test::function::test_loop_2
  60. function test_loop_2() public {
  61. int256[] vec = new int256[](10);
  62. for (int256 i = 0; i < 5; i++) {
  63. // CHECK: branchcond (unsigned more %array_length.temp.40 > uint32 20), block5, block6
  64. if (vec.length > 20) {
  65. break;
  66. }
  67. vec.push(3);
  68. }
  69. // CHECK: branchcond (%array_length.temp.40 == uint32 15), block7, block8
  70. assert(vec.length == 15);
  71. }
  72. }