arithmetic.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/operators/arithmetic.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/operators/arithmetic.carbon
  12. // Test each of the arithmetic operations, signed and unsigned values may lower
  13. // differently.
  14. fn add_i32(a: i32, b: i32) -> i32 { return a + b; }
  15. fn add_u32(a: u32, b: u32) -> u32 { return a + b; }
  16. fn div_i32(a: i32, b: i32) -> i32 { return a / b; }
  17. fn div_u32(a: u32, b: u32) -> u32 { return a / b; }
  18. fn mod_i32(a: i32, b: i32) -> i32 { return a % b; }
  19. fn mod_u32(a: u32, b: u32) -> u32 { return a % b; }
  20. fn mul_i32(a: i32, b: i32) -> i32 { return a * b; }
  21. fn mul_u32(a: u32, b: u32) -> u32 { return a * b; }
  22. fn neg_i32(a: i32) -> i32 { return -a; }
  23. fn neg_u32(a: u32) -> u32 { return -a; }
  24. fn sub_i32(a: i32, b: i32) -> i32 { return a - b; }
  25. fn sub_u32(a: u32, b: u32) -> u32 { return a - b; }
  26. // One test for non-32 bit, verify the size is correct.
  27. fn div_i16(a: i16, b: i16) -> i16 { return a / b; }
  28. fn div_u16(a: u16, b: u16) -> u16 { return a / b; }
  29. // CHECK:STDOUT: ; ModuleID = 'arithmetic.carbon'
  30. // CHECK:STDOUT: source_filename = "arithmetic.carbon"
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: ; Function Attrs: nounwind
  33. // CHECK:STDOUT: define i32 @_Cadd_i32.Main(i32 %a, i32 %b) #0 !dbg !4 {
  34. // CHECK:STDOUT: entry:
  35. // CHECK:STDOUT: %Int.as.AddWith.impl.Op.call = add i32 %a, %b, !dbg !7
  36. // CHECK:STDOUT: ret i32 %Int.as.AddWith.impl.Op.call, !dbg !8
  37. // CHECK:STDOUT: }
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: ; Function Attrs: nounwind
  40. // CHECK:STDOUT: define i32 @_Cadd_u32.Main(i32 %a, i32 %b) #0 !dbg !9 {
  41. // CHECK:STDOUT: entry:
  42. // CHECK:STDOUT: %UInt.as.AddWith.impl.Op.call = add i32 %a, %b, !dbg !10
  43. // CHECK:STDOUT: ret i32 %UInt.as.AddWith.impl.Op.call, !dbg !11
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: ; Function Attrs: nounwind
  47. // CHECK:STDOUT: define i32 @_Cdiv_i32.Main(i32 %a, i32 %b) #0 !dbg !12 {
  48. // CHECK:STDOUT: entry:
  49. // CHECK:STDOUT: %Int.as.DivWith.impl.Op.call = sdiv i32 %a, %b, !dbg !13
  50. // CHECK:STDOUT: ret i32 %Int.as.DivWith.impl.Op.call, !dbg !14
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: ; Function Attrs: nounwind
  54. // CHECK:STDOUT: define i32 @_Cdiv_u32.Main(i32 %a, i32 %b) #0 !dbg !15 {
  55. // CHECK:STDOUT: entry:
  56. // CHECK:STDOUT: %UInt.as.DivWith.impl.Op.call = udiv i32 %a, %b, !dbg !16
  57. // CHECK:STDOUT: ret i32 %UInt.as.DivWith.impl.Op.call, !dbg !17
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: ; Function Attrs: nounwind
  61. // CHECK:STDOUT: define i32 @_Cmod_i32.Main(i32 %a, i32 %b) #0 !dbg !18 {
  62. // CHECK:STDOUT: entry:
  63. // CHECK:STDOUT: %Int.as.ModWith.impl.Op.call = srem i32 %a, %b, !dbg !19
  64. // CHECK:STDOUT: ret i32 %Int.as.ModWith.impl.Op.call, !dbg !20
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: ; Function Attrs: nounwind
  68. // CHECK:STDOUT: define i32 @_Cmod_u32.Main(i32 %a, i32 %b) #0 !dbg !21 {
  69. // CHECK:STDOUT: entry:
  70. // CHECK:STDOUT: %UInt.as.ModWith.impl.Op.call = urem i32 %a, %b, !dbg !22
  71. // CHECK:STDOUT: ret i32 %UInt.as.ModWith.impl.Op.call, !dbg !23
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: ; Function Attrs: nounwind
  75. // CHECK:STDOUT: define i32 @_Cmul_i32.Main(i32 %a, i32 %b) #0 !dbg !24 {
  76. // CHECK:STDOUT: entry:
  77. // CHECK:STDOUT: %Int.as.MulWith.impl.Op.call = mul i32 %a, %b, !dbg !25
  78. // CHECK:STDOUT: ret i32 %Int.as.MulWith.impl.Op.call, !dbg !26
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: ; Function Attrs: nounwind
  82. // CHECK:STDOUT: define i32 @_Cmul_u32.Main(i32 %a, i32 %b) #0 !dbg !27 {
  83. // CHECK:STDOUT: entry:
  84. // CHECK:STDOUT: %UInt.as.MulWith.impl.Op.call = mul i32 %a, %b, !dbg !28
  85. // CHECK:STDOUT: ret i32 %UInt.as.MulWith.impl.Op.call, !dbg !29
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: ; Function Attrs: nounwind
  89. // CHECK:STDOUT: define i32 @_Cneg_i32.Main(i32 %a) #0 !dbg !30 {
  90. // CHECK:STDOUT: entry:
  91. // CHECK:STDOUT: %Int.as.Negate.impl.Op.call = sub i32 0, %a, !dbg !31
  92. // CHECK:STDOUT: ret i32 %Int.as.Negate.impl.Op.call, !dbg !32
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: ; Function Attrs: nounwind
  96. // CHECK:STDOUT: define i32 @_Cneg_u32.Main(i32 %a) #0 !dbg !33 {
  97. // CHECK:STDOUT: entry:
  98. // CHECK:STDOUT: %UInt.as.Negate.impl.Op.call = sub i32 0, %a, !dbg !34
  99. // CHECK:STDOUT: ret i32 %UInt.as.Negate.impl.Op.call, !dbg !35
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: ; Function Attrs: nounwind
  103. // CHECK:STDOUT: define i32 @_Csub_i32.Main(i32 %a, i32 %b) #0 !dbg !36 {
  104. // CHECK:STDOUT: entry:
  105. // CHECK:STDOUT: %Int.as.SubWith.impl.Op.call = sub i32 %a, %b, !dbg !37
  106. // CHECK:STDOUT: ret i32 %Int.as.SubWith.impl.Op.call, !dbg !38
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: ; Function Attrs: nounwind
  110. // CHECK:STDOUT: define i32 @_Csub_u32.Main(i32 %a, i32 %b) #0 !dbg !39 {
  111. // CHECK:STDOUT: entry:
  112. // CHECK:STDOUT: %UInt.as.SubWith.impl.Op.call = sub i32 %a, %b, !dbg !40
  113. // CHECK:STDOUT: ret i32 %UInt.as.SubWith.impl.Op.call, !dbg !41
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: ; Function Attrs: nounwind
  117. // CHECK:STDOUT: define i16 @_Cdiv_i16.Main(i16 %a, i16 %b) #0 !dbg !42 {
  118. // CHECK:STDOUT: entry:
  119. // CHECK:STDOUT: %Int.as.DivWith.impl.Op.call = sdiv i16 %a, %b, !dbg !43
  120. // CHECK:STDOUT: ret i16 %Int.as.DivWith.impl.Op.call, !dbg !44
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: ; Function Attrs: nounwind
  124. // CHECK:STDOUT: define i16 @_Cdiv_u16.Main(i16 %a, i16 %b) #0 !dbg !45 {
  125. // CHECK:STDOUT: entry:
  126. // CHECK:STDOUT: %UInt.as.DivWith.impl.Op.call = udiv i16 %a, %b, !dbg !46
  127. // CHECK:STDOUT: ret i16 %UInt.as.DivWith.impl.Op.call, !dbg !47
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: attributes #0 = { nounwind }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  133. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  136. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  137. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  138. // CHECK:STDOUT: !3 = !DIFile(filename: "arithmetic.carbon", directory: "")
  139. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "add_i32", linkageName: "_Cadd_i32.Main", scope: null, file: !3, line: 16, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  140. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  141. // CHECK:STDOUT: !6 = !{}
  142. // CHECK:STDOUT: !7 = !DILocation(line: 16, column: 44, scope: !4)
  143. // CHECK:STDOUT: !8 = !DILocation(line: 16, column: 37, scope: !4)
  144. // CHECK:STDOUT: !9 = distinct !DISubprogram(name: "add_u32", linkageName: "_Cadd_u32.Main", scope: null, file: !3, line: 17, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  145. // CHECK:STDOUT: !10 = !DILocation(line: 17, column: 44, scope: !9)
  146. // CHECK:STDOUT: !11 = !DILocation(line: 17, column: 37, scope: !9)
  147. // CHECK:STDOUT: !12 = distinct !DISubprogram(name: "div_i32", linkageName: "_Cdiv_i32.Main", scope: null, file: !3, line: 19, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  148. // CHECK:STDOUT: !13 = !DILocation(line: 19, column: 44, scope: !12)
  149. // CHECK:STDOUT: !14 = !DILocation(line: 19, column: 37, scope: !12)
  150. // CHECK:STDOUT: !15 = distinct !DISubprogram(name: "div_u32", linkageName: "_Cdiv_u32.Main", scope: null, file: !3, line: 20, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  151. // CHECK:STDOUT: !16 = !DILocation(line: 20, column: 44, scope: !15)
  152. // CHECK:STDOUT: !17 = !DILocation(line: 20, column: 37, scope: !15)
  153. // CHECK:STDOUT: !18 = distinct !DISubprogram(name: "mod_i32", linkageName: "_Cmod_i32.Main", scope: null, file: !3, line: 22, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  154. // CHECK:STDOUT: !19 = !DILocation(line: 22, column: 44, scope: !18)
  155. // CHECK:STDOUT: !20 = !DILocation(line: 22, column: 37, scope: !18)
  156. // CHECK:STDOUT: !21 = distinct !DISubprogram(name: "mod_u32", linkageName: "_Cmod_u32.Main", scope: null, file: !3, line: 23, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  157. // CHECK:STDOUT: !22 = !DILocation(line: 23, column: 44, scope: !21)
  158. // CHECK:STDOUT: !23 = !DILocation(line: 23, column: 37, scope: !21)
  159. // CHECK:STDOUT: !24 = distinct !DISubprogram(name: "mul_i32", linkageName: "_Cmul_i32.Main", scope: null, file: !3, line: 25, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  160. // CHECK:STDOUT: !25 = !DILocation(line: 25, column: 44, scope: !24)
  161. // CHECK:STDOUT: !26 = !DILocation(line: 25, column: 37, scope: !24)
  162. // CHECK:STDOUT: !27 = distinct !DISubprogram(name: "mul_u32", linkageName: "_Cmul_u32.Main", scope: null, file: !3, line: 26, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  163. // CHECK:STDOUT: !28 = !DILocation(line: 26, column: 44, scope: !27)
  164. // CHECK:STDOUT: !29 = !DILocation(line: 26, column: 37, scope: !27)
  165. // CHECK:STDOUT: !30 = distinct !DISubprogram(name: "neg_i32", linkageName: "_Cneg_i32.Main", scope: null, file: !3, line: 28, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  166. // CHECK:STDOUT: !31 = !DILocation(line: 28, column: 36, scope: !30)
  167. // CHECK:STDOUT: !32 = !DILocation(line: 28, column: 29, scope: !30)
  168. // CHECK:STDOUT: !33 = distinct !DISubprogram(name: "neg_u32", linkageName: "_Cneg_u32.Main", scope: null, file: !3, line: 29, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  169. // CHECK:STDOUT: !34 = !DILocation(line: 29, column: 36, scope: !33)
  170. // CHECK:STDOUT: !35 = !DILocation(line: 29, column: 29, scope: !33)
  171. // CHECK:STDOUT: !36 = distinct !DISubprogram(name: "sub_i32", linkageName: "_Csub_i32.Main", scope: null, file: !3, line: 31, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  172. // CHECK:STDOUT: !37 = !DILocation(line: 31, column: 44, scope: !36)
  173. // CHECK:STDOUT: !38 = !DILocation(line: 31, column: 37, scope: !36)
  174. // CHECK:STDOUT: !39 = distinct !DISubprogram(name: "sub_u32", linkageName: "_Csub_u32.Main", scope: null, file: !3, line: 32, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  175. // CHECK:STDOUT: !40 = !DILocation(line: 32, column: 44, scope: !39)
  176. // CHECK:STDOUT: !41 = !DILocation(line: 32, column: 37, scope: !39)
  177. // CHECK:STDOUT: !42 = distinct !DISubprogram(name: "div_i16", linkageName: "_Cdiv_i16.Main", scope: null, file: !3, line: 36, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  178. // CHECK:STDOUT: !43 = !DILocation(line: 36, column: 44, scope: !42)
  179. // CHECK:STDOUT: !44 = !DILocation(line: 36, column: 37, scope: !42)
  180. // CHECK:STDOUT: !45 = distinct !DISubprogram(name: "div_u16", linkageName: "_Cdiv_u16.Main", scope: null, file: !3, line: 37, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  181. // CHECK:STDOUT: !46 = !DILocation(line: 37, column: 44, scope: !45)
  182. // CHECK:STDOUT: !47 = !DILocation(line: 37, column: 37, scope: !45)