smod.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // --- int_div.carbon
  7. fn Mod(a: i32, b: i32) -> i32 = "int.smod";
  8. var arr: [i32; Mod(5, 3)];
  9. let arr_p: [i32; 2]* = &arr;
  10. fn RuntimeCall(a: i32, b: i32) -> i32 {
  11. return Mod(a, b);
  12. }
  13. // --- fail_overflow.carbon
  14. package FailOverflow api;
  15. fn Mod(a: i32, b: i32) -> i32 = "int.smod";
  16. fn Sub(a: i32, b: i32) -> i32 = "int.ssub";
  17. fn Negate(a: i32) -> i32 = "int.snegate";
  18. // -0x7FFF_FFFF % -1 is OK.
  19. let a: i32 = Mod(Negate(0x7FFF_FFFF), Negate(1));
  20. // -0x8000_0000 % 1 is OK.
  21. let b: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), 1);
  22. // -0x8000_0000 / -1 overflows, so -0x8000_0000 % -1 is disallowed, even though
  23. // its result is representable.
  24. // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:14: ERROR: Integer overflow in calculation -2147483648 % -1.
  25. // CHECK:STDERR: let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
  26. // CHECK:STDERR: ^~~~
  27. // CHECK:STDERR:
  28. let c: i32 = Mod(Sub(Negate(0x7FFF_FFFF), 1), Negate(1));
  29. // --- fail_div_by_zero.carbon
  30. package FailDivByZero api;
  31. fn Mod(a: i32, b: i32) -> i32 = "int.smod";
  32. // Remainder of division by zero is not defined.
  33. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+4]]:14: ERROR: Division by zero.
  34. // CHECK:STDERR: let a: i32 = Mod(1, 0);
  35. // CHECK:STDERR: ^~~~
  36. // CHECK:STDERR:
  37. let a: i32 = Mod(1, 0);
  38. // CHECK:STDERR: fail_div_by_zero.carbon:[[@LINE+3]]:14: ERROR: Division by zero.
  39. // CHECK:STDERR: let b: i32 = Mod(0, 0);
  40. // CHECK:STDERR: ^~~~
  41. let b: i32 = Mod(0, 0);
  42. // CHECK:STDOUT: --- int_div.carbon
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: constants {
  45. // CHECK:STDOUT: %Mod: type = fn_type @Mod [template]
  46. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  47. // CHECK:STDOUT: %struct.1: Mod = struct_value () [template]
  48. // CHECK:STDOUT: %.2: i32 = int_literal 5 [template]
  49. // CHECK:STDOUT: %.3: i32 = int_literal 3 [template]
  50. // CHECK:STDOUT: %.4: i32 = int_literal 2 [template]
  51. // CHECK:STDOUT: %.5: type = array_type %.4, i32 [template]
  52. // CHECK:STDOUT: %.6: type = ptr_type [i32; 2] [template]
  53. // CHECK:STDOUT: %RuntimeCall: type = fn_type @RuntimeCall [template]
  54. // CHECK:STDOUT: %struct.2: RuntimeCall = struct_value () [template]
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: file {
  58. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  59. // CHECK:STDOUT: .Core = %Core
  60. // CHECK:STDOUT: .Mod = %Mod.decl
  61. // CHECK:STDOUT: .arr = %arr
  62. // CHECK:STDOUT: .RuntimeCall = %RuntimeCall.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  65. // CHECK:STDOUT: %Mod.decl: Mod = fn_decl @Mod [template = constants.%struct.1] {
  66. // CHECK:STDOUT: %a.loc2_8.1: i32 = param a
  67. // CHECK:STDOUT: @Mod.%a: i32 = bind_name a, %a.loc2_8.1
  68. // CHECK:STDOUT: %b.loc2_16.1: i32 = param b
  69. // CHECK:STDOUT: @Mod.%b: i32 = bind_name b, %b.loc2_16.1
  70. // CHECK:STDOUT: @Mod.%return: ref i32 = var <return slot>
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Mod.ref: Mod = name_ref Mod, %Mod.decl [template = constants.%struct.1]
  73. // CHECK:STDOUT: %.loc4_20: i32 = int_literal 5 [template = constants.%.2]
  74. // CHECK:STDOUT: %.loc4_23: i32 = int_literal 3 [template = constants.%.3]
  75. // CHECK:STDOUT: %int.smod: init i32 = call %Mod.ref(%.loc4_20, %.loc4_23) [template = constants.%.4]
  76. // CHECK:STDOUT: %.loc4_25: type = array_type %int.smod, i32 [template = constants.%.5]
  77. // CHECK:STDOUT: %arr.var: ref [i32; 2] = var arr
  78. // CHECK:STDOUT: %arr: ref [i32; 2] = bind_name arr, %arr.var
  79. // CHECK:STDOUT: %.loc5_18: i32 = int_literal 2 [template = constants.%.4]
  80. // CHECK:STDOUT: %.loc5_19: type = array_type %.loc5_18, i32 [template = constants.%.5]
  81. // CHECK:STDOUT: %.loc5_20: type = ptr_type [i32; 2] [template = constants.%.6]
  82. // CHECK:STDOUT: %arr.ref: ref [i32; 2] = name_ref arr, %arr
  83. // CHECK:STDOUT: %.loc5_24: [i32; 2]* = addr_of %arr.ref
  84. // CHECK:STDOUT: %arr_p: [i32; 2]* = bind_name arr_p, %.loc5_24
  85. // CHECK:STDOUT: %RuntimeCall.decl: RuntimeCall = fn_decl @RuntimeCall [template = constants.%struct.2] {
  86. // CHECK:STDOUT: %a.loc7_16.1: i32 = param a
  87. // CHECK:STDOUT: @RuntimeCall.%a: i32 = bind_name a, %a.loc7_16.1
  88. // CHECK:STDOUT: %b.loc7_24.1: i32 = param b
  89. // CHECK:STDOUT: @RuntimeCall.%b: i32 = bind_name b, %b.loc7_24.1
  90. // CHECK:STDOUT: @RuntimeCall.%return: ref i32 = var <return slot>
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: fn @Mod(%a: i32, %b: i32) -> i32 = "int.smod";
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: fn @RuntimeCall(%a: i32, %b: i32) -> i32 {
  97. // CHECK:STDOUT: !entry:
  98. // CHECK:STDOUT: %Mod.ref: Mod = name_ref Mod, file.%Mod.decl [template = constants.%struct.1]
  99. // CHECK:STDOUT: %a.ref: i32 = name_ref a, %a
  100. // CHECK:STDOUT: %b.ref: i32 = name_ref b, %b
  101. // CHECK:STDOUT: %int.smod: init i32 = call %Mod.ref(%a.ref, %b.ref)
  102. // CHECK:STDOUT: %.loc8_19.1: i32 = value_of_initializer %int.smod
  103. // CHECK:STDOUT: %.loc8_19.2: i32 = converted %int.smod, %.loc8_19.1
  104. // CHECK:STDOUT: return %.loc8_19.2
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: --- fail_overflow.carbon
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: constants {
  110. // CHECK:STDOUT: %Mod: type = fn_type @Mod [template]
  111. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  112. // CHECK:STDOUT: %struct.1: Mod = struct_value () [template]
  113. // CHECK:STDOUT: %Sub: type = fn_type @Sub [template]
  114. // CHECK:STDOUT: %struct.2: Sub = struct_value () [template]
  115. // CHECK:STDOUT: %Negate: type = fn_type @Negate [template]
  116. // CHECK:STDOUT: %struct.3: Negate = struct_value () [template]
  117. // CHECK:STDOUT: %.2: i32 = int_literal 2147483647 [template]
  118. // CHECK:STDOUT: %.3: i32 = int_literal -2147483647 [template]
  119. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  120. // CHECK:STDOUT: %.5: i32 = int_literal -1 [template]
  121. // CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
  122. // CHECK:STDOUT: %.7: i32 = int_literal -2147483648 [template]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: file {
  126. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  127. // CHECK:STDOUT: .Core = %Core
  128. // CHECK:STDOUT: .Mod = %Mod.decl
  129. // CHECK:STDOUT: .Sub = %Sub.decl
  130. // CHECK:STDOUT: .Negate = %Negate.decl
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  133. // CHECK:STDOUT: %Mod.decl: Mod = fn_decl @Mod [template = constants.%struct.1] {
  134. // CHECK:STDOUT: %a.loc4_8.1: i32 = param a
  135. // CHECK:STDOUT: @Mod.%a: i32 = bind_name a, %a.loc4_8.1
  136. // CHECK:STDOUT: %b.loc4_16.1: i32 = param b
  137. // CHECK:STDOUT: @Mod.%b: i32 = bind_name b, %b.loc4_16.1
  138. // CHECK:STDOUT: @Mod.%return: ref i32 = var <return slot>
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: %Sub.decl: Sub = fn_decl @Sub [template = constants.%struct.2] {
  141. // CHECK:STDOUT: %a.loc5_8.1: i32 = param a
  142. // CHECK:STDOUT: @Sub.%a: i32 = bind_name a, %a.loc5_8.1
  143. // CHECK:STDOUT: %b.loc5_16.1: i32 = param b
  144. // CHECK:STDOUT: @Sub.%b: i32 = bind_name b, %b.loc5_16.1
  145. // CHECK:STDOUT: @Sub.%return: ref i32 = var <return slot>
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: %Negate.decl: Negate = fn_decl @Negate [template = constants.%struct.3] {
  148. // CHECK:STDOUT: %a.loc6_11.1: i32 = param a
  149. // CHECK:STDOUT: @Negate.%a: i32 = bind_name a, %a.loc6_11.1
  150. // CHECK:STDOUT: @Negate.%return: ref i32 = var <return slot>
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT: %Mod.ref.loc9: Mod = name_ref Mod, %Mod.decl [template = constants.%struct.1]
  153. // CHECK:STDOUT: %Negate.ref.loc9_18: Negate = name_ref Negate, %Negate.decl [template = constants.%struct.3]
  154. // CHECK:STDOUT: %.loc9_25: i32 = int_literal 2147483647 [template = constants.%.2]
  155. // CHECK:STDOUT: %int.snegate.loc9_24: init i32 = call %Negate.ref.loc9_18(%.loc9_25) [template = constants.%.3]
  156. // CHECK:STDOUT: %Negate.ref.loc9_39: Negate = name_ref Negate, %Negate.decl [template = constants.%struct.3]
  157. // CHECK:STDOUT: %.loc9_46: i32 = int_literal 1 [template = constants.%.4]
  158. // CHECK:STDOUT: %int.snegate.loc9_45: init i32 = call %Negate.ref.loc9_39(%.loc9_46) [template = constants.%.5]
  159. // CHECK:STDOUT: %.loc9_17.1: i32 = value_of_initializer %int.snegate.loc9_24 [template = constants.%.3]
  160. // CHECK:STDOUT: %.loc9_17.2: i32 = converted %int.snegate.loc9_24, %.loc9_17.1 [template = constants.%.3]
  161. // CHECK:STDOUT: %.loc9_17.3: i32 = value_of_initializer %int.snegate.loc9_45 [template = constants.%.5]
  162. // CHECK:STDOUT: %.loc9_17.4: i32 = converted %int.snegate.loc9_45, %.loc9_17.3 [template = constants.%.5]
  163. // CHECK:STDOUT: %int.smod.loc9: init i32 = call %Mod.ref.loc9(%.loc9_17.2, %.loc9_17.4) [template = constants.%.6]
  164. // CHECK:STDOUT: %.loc9_49.1: i32 = value_of_initializer %int.smod.loc9 [template = constants.%.6]
  165. // CHECK:STDOUT: %.loc9_49.2: i32 = converted %int.smod.loc9, %.loc9_49.1 [template = constants.%.6]
  166. // CHECK:STDOUT: %a.loc9: i32 = bind_name a, %.loc9_49.2
  167. // CHECK:STDOUT: %Mod.ref.loc12: Mod = name_ref Mod, %Mod.decl [template = constants.%struct.1]
  168. // CHECK:STDOUT: %Sub.ref.loc12: Sub = name_ref Sub, %Sub.decl [template = constants.%struct.2]
  169. // CHECK:STDOUT: %Negate.ref.loc12: Negate = name_ref Negate, %Negate.decl [template = constants.%struct.3]
  170. // CHECK:STDOUT: %.loc12_29: i32 = int_literal 2147483647 [template = constants.%.2]
  171. // CHECK:STDOUT: %int.snegate.loc12: init i32 = call %Negate.ref.loc12(%.loc12_29) [template = constants.%.3]
  172. // CHECK:STDOUT: %.loc12_43: i32 = int_literal 1 [template = constants.%.4]
  173. // CHECK:STDOUT: %.loc12_21.1: i32 = value_of_initializer %int.snegate.loc12 [template = constants.%.3]
  174. // CHECK:STDOUT: %.loc12_21.2: i32 = converted %int.snegate.loc12, %.loc12_21.1 [template = constants.%.3]
  175. // CHECK:STDOUT: %int.ssub.loc12: init i32 = call %Sub.ref.loc12(%.loc12_21.2, %.loc12_43) [template = constants.%.7]
  176. // CHECK:STDOUT: %.loc12_47: i32 = int_literal 1 [template = constants.%.4]
  177. // CHECK:STDOUT: %.loc12_17.1: i32 = value_of_initializer %int.ssub.loc12 [template = constants.%.7]
  178. // CHECK:STDOUT: %.loc12_17.2: i32 = converted %int.ssub.loc12, %.loc12_17.1 [template = constants.%.7]
  179. // CHECK:STDOUT: %int.smod.loc12: init i32 = call %Mod.ref.loc12(%.loc12_17.2, %.loc12_47) [template = constants.%.6]
  180. // CHECK:STDOUT: %.loc12_49.1: i32 = value_of_initializer %int.smod.loc12 [template = constants.%.6]
  181. // CHECK:STDOUT: %.loc12_49.2: i32 = converted %int.smod.loc12, %.loc12_49.1 [template = constants.%.6]
  182. // CHECK:STDOUT: %b.loc12: i32 = bind_name b, %.loc12_49.2
  183. // CHECK:STDOUT: %Mod.ref.loc20: Mod = name_ref Mod, %Mod.decl [template = constants.%struct.1]
  184. // CHECK:STDOUT: %Sub.ref.loc20: Sub = name_ref Sub, %Sub.decl [template = constants.%struct.2]
  185. // CHECK:STDOUT: %Negate.ref.loc20_22: Negate = name_ref Negate, %Negate.decl [template = constants.%struct.3]
  186. // CHECK:STDOUT: %.loc20_29: i32 = int_literal 2147483647 [template = constants.%.2]
  187. // CHECK:STDOUT: %int.snegate.loc20_28: init i32 = call %Negate.ref.loc20_22(%.loc20_29) [template = constants.%.3]
  188. // CHECK:STDOUT: %.loc20_43: i32 = int_literal 1 [template = constants.%.4]
  189. // CHECK:STDOUT: %.loc20_21.1: i32 = value_of_initializer %int.snegate.loc20_28 [template = constants.%.3]
  190. // CHECK:STDOUT: %.loc20_21.2: i32 = converted %int.snegate.loc20_28, %.loc20_21.1 [template = constants.%.3]
  191. // CHECK:STDOUT: %int.ssub.loc20: init i32 = call %Sub.ref.loc20(%.loc20_21.2, %.loc20_43) [template = constants.%.7]
  192. // CHECK:STDOUT: %Negate.ref.loc20_47: Negate = name_ref Negate, %Negate.decl [template = constants.%struct.3]
  193. // CHECK:STDOUT: %.loc20_54: i32 = int_literal 1 [template = constants.%.4]
  194. // CHECK:STDOUT: %int.snegate.loc20_53: init i32 = call %Negate.ref.loc20_47(%.loc20_54) [template = constants.%.5]
  195. // CHECK:STDOUT: %.loc20_17.1: i32 = value_of_initializer %int.ssub.loc20 [template = constants.%.7]
  196. // CHECK:STDOUT: %.loc20_17.2: i32 = converted %int.ssub.loc20, %.loc20_17.1 [template = constants.%.7]
  197. // CHECK:STDOUT: %.loc20_17.3: i32 = value_of_initializer %int.snegate.loc20_53 [template = constants.%.5]
  198. // CHECK:STDOUT: %.loc20_17.4: i32 = converted %int.snegate.loc20_53, %.loc20_17.3 [template = constants.%.5]
  199. // CHECK:STDOUT: %int.smod.loc20: init i32 = call %Mod.ref.loc20(%.loc20_17.2, %.loc20_17.4) [template = constants.%.6]
  200. // CHECK:STDOUT: %.loc20_57.1: i32 = value_of_initializer %int.smod.loc20 [template = constants.%.6]
  201. // CHECK:STDOUT: %.loc20_57.2: i32 = converted %int.smod.loc20, %.loc20_57.1 [template = constants.%.6]
  202. // CHECK:STDOUT: %c: i32 = bind_name c, %.loc20_57.2
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: fn @Mod(%a: i32, %b: i32) -> i32 = "int.smod";
  206. // CHECK:STDOUT:
  207. // CHECK:STDOUT: fn @Sub(%a: i32, %b: i32) -> i32 = "int.ssub";
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: fn @Negate(%a: i32) -> i32 = "int.snegate";
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: --- fail_div_by_zero.carbon
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: constants {
  214. // CHECK:STDOUT: %Mod: type = fn_type @Mod [template]
  215. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  216. // CHECK:STDOUT: %struct: Mod = struct_value () [template]
  217. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  218. // CHECK:STDOUT: %.3: i32 = int_literal 0 [template]
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: file {
  222. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  223. // CHECK:STDOUT: .Core = %Core
  224. // CHECK:STDOUT: .Mod = %Mod.decl
  225. // CHECK:STDOUT: }
  226. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  227. // CHECK:STDOUT: %Mod.decl: Mod = fn_decl @Mod [template = constants.%struct] {
  228. // CHECK:STDOUT: %a.loc4_8.1: i32 = param a
  229. // CHECK:STDOUT: @Mod.%a: i32 = bind_name a, %a.loc4_8.1
  230. // CHECK:STDOUT: %b.loc4_16.1: i32 = param b
  231. // CHECK:STDOUT: @Mod.%b: i32 = bind_name b, %b.loc4_16.1
  232. // CHECK:STDOUT: @Mod.%return: ref i32 = var <return slot>
  233. // CHECK:STDOUT: }
  234. // CHECK:STDOUT: %Mod.ref.loc12: Mod = name_ref Mod, %Mod.decl [template = constants.%struct]
  235. // CHECK:STDOUT: %.loc12_18: i32 = int_literal 1 [template = constants.%.2]
  236. // CHECK:STDOUT: %.loc12_21: i32 = int_literal 0 [template = constants.%.3]
  237. // CHECK:STDOUT: %int.smod.loc12: init i32 = call %Mod.ref.loc12(%.loc12_18, %.loc12_21) [template = <error>]
  238. // CHECK:STDOUT: %.loc12_23.1: i32 = value_of_initializer %int.smod.loc12 [template = <error>]
  239. // CHECK:STDOUT: %.loc12_23.2: i32 = converted %int.smod.loc12, %.loc12_23.1 [template = <error>]
  240. // CHECK:STDOUT: %a.loc12: i32 = bind_name a, %.loc12_23.2
  241. // CHECK:STDOUT: %Mod.ref.loc17: Mod = name_ref Mod, %Mod.decl [template = constants.%struct]
  242. // CHECK:STDOUT: %.loc17_18: i32 = int_literal 0 [template = constants.%.3]
  243. // CHECK:STDOUT: %.loc17_21: i32 = int_literal 0 [template = constants.%.3]
  244. // CHECK:STDOUT: %int.smod.loc17: init i32 = call %Mod.ref.loc17(%.loc17_18, %.loc17_21) [template = <error>]
  245. // CHECK:STDOUT: %.loc17_23.1: i32 = value_of_initializer %int.smod.loc17 [template = <error>]
  246. // CHECK:STDOUT: %.loc17_23.2: i32 = converted %int.smod.loc17, %.loc17_23.1 [template = <error>]
  247. // CHECK:STDOUT: %b.loc17: i32 = bind_name b, %.loc17_23.2
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT:
  250. // CHECK:STDOUT: fn @Mod(%a: i32, %b: i32) -> i32 = "int.smod";
  251. // CHECK:STDOUT: