global.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/int.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/var/global.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/var/global.carbon
  12. // TODO: We should initialize all of these variables statically rather than on startup.
  13. // --- simple.carbon
  14. library "[[@TEST_NAME]]";
  15. var a: i32 = 1;
  16. fn GetA() -> i32* { return &a; }
  17. // --- var_in_tuple.carbon
  18. library "[[@TEST_NAME]]";
  19. let (var b: i32, var c: i32) = (2, 3);
  20. fn GetB() -> i32* { return &b; }
  21. fn GetC() -> i32* { return &c; }
  22. // --- tuple_in_var.carbon
  23. library "[[@TEST_NAME]]";
  24. var (d: i32, e: i32) = (4, 5);
  25. fn GetD() -> i32* { return &d; }
  26. fn GetE() -> i32* { return &e; }
  27. // --- unnamed.carbon
  28. library "[[@TEST_NAME]]";
  29. var (_: i32, x: i32) = (1, 2);
  30. var (_: i32, _: i32) = (3, 4);
  31. // CHECK:STDOUT: ; ModuleID = 'simple.carbon'
  32. // CHECK:STDOUT: source_filename = "simple.carbon"
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: @_Ca.Main = global i32 0
  35. // CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: define ptr @_CGetA.Main() !dbg !4 {
  38. // CHECK:STDOUT: entry:
  39. // CHECK:STDOUT: ret ptr @_Ca.Main, !dbg !7
  40. // CHECK:STDOUT: }
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: define void @_C__global_init.Main() !dbg !8 {
  43. // CHECK:STDOUT: entry:
  44. // CHECK:STDOUT: store i32 1, ptr @_Ca.Main, align 4, !dbg !9
  45. // CHECK:STDOUT: ret void, !dbg !10
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  49. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  52. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  53. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  54. // CHECK:STDOUT: !3 = !DIFile(filename: "simple.carbon", directory: "")
  55. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "GetA", linkageName: "_CGetA.Main", scope: null, file: !3, line: 5, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  56. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  57. // CHECK:STDOUT: !6 = !{}
  58. // CHECK:STDOUT: !7 = !DILocation(line: 5, column: 21, scope: !4)
  59. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  60. // CHECK:STDOUT: !9 = !DILocation(line: 4, column: 1, scope: !8)
  61. // CHECK:STDOUT: !10 = !DILocation(line: 0, scope: !8)
  62. // CHECK:STDOUT: ; ModuleID = 'var_in_tuple.carbon'
  63. // CHECK:STDOUT: source_filename = "var_in_tuple.carbon"
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: @_Cb.Main = global i32 0
  66. // CHECK:STDOUT: @_Cc.Main = global i32 0
  67. // CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: define ptr @_CGetB.Main() !dbg !4 {
  70. // CHECK:STDOUT: entry:
  71. // CHECK:STDOUT: ret ptr @_Cb.Main, !dbg !7
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: define ptr @_CGetC.Main() !dbg !8 {
  75. // CHECK:STDOUT: entry:
  76. // CHECK:STDOUT: ret ptr @_Cc.Main, !dbg !9
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: define void @_C__global_init.Main() !dbg !10 {
  80. // CHECK:STDOUT: entry:
  81. // CHECK:STDOUT: store i32 2, ptr @_Cb.Main, align 4, !dbg !11
  82. // CHECK:STDOUT: store i32 3, ptr @_Cc.Main, align 4, !dbg !12
  83. // CHECK:STDOUT: ret void, !dbg !13
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  87. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  90. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  91. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  92. // CHECK:STDOUT: !3 = !DIFile(filename: "var_in_tuple.carbon", directory: "")
  93. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "GetB", linkageName: "_CGetB.Main", scope: null, file: !3, line: 6, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  94. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  95. // CHECK:STDOUT: !6 = !{}
  96. // CHECK:STDOUT: !7 = !DILocation(line: 6, column: 21, scope: !4)
  97. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "GetC", linkageName: "_CGetC.Main", scope: null, file: !3, line: 7, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  98. // CHECK:STDOUT: !9 = !DILocation(line: 7, column: 21, scope: !8)
  99. // CHECK:STDOUT: !10 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  100. // CHECK:STDOUT: !11 = !DILocation(line: 4, column: 6, scope: !10)
  101. // CHECK:STDOUT: !12 = !DILocation(line: 4, column: 18, scope: !10)
  102. // CHECK:STDOUT: !13 = !DILocation(line: 0, scope: !10)
  103. // CHECK:STDOUT: ; ModuleID = 'tuple_in_var.carbon'
  104. // CHECK:STDOUT: source_filename = "tuple_in_var.carbon"
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: @_Cd.Main = global { i32, i32 } zeroinitializer
  107. // CHECK:STDOUT: @tuple.loc4_1 = internal constant { i32, i32 } { i32 4, i32 5 }
  108. // CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: define ptr @_CGetD.Main() !dbg !4 {
  111. // CHECK:STDOUT: entry:
  112. // CHECK:STDOUT: ret ptr @_Cd.Main, !dbg !7
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: define ptr @_CGetE.Main() !dbg !8 {
  116. // CHECK:STDOUT: entry:
  117. // CHECK:STDOUT: ret ptr getelementptr inbounds nuw ({ i32, i32 }, ptr @_Cd.Main, i32 0, i32 1), !dbg !9
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: define void @_C__global_init.Main() !dbg !10 {
  121. // CHECK:STDOUT: entry:
  122. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 @_Cd.Main, ptr align 4 @tuple.loc4_1, i64 8, i1 false), !dbg !11
  123. // CHECK:STDOUT: ret void, !dbg !12
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  127. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #0
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: ; uselistorder directives
  130. // CHECK:STDOUT: uselistorder ptr @_Cd.Main, { 0, 2, 1 }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  135. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  138. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  139. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  140. // CHECK:STDOUT: !3 = !DIFile(filename: "tuple_in_var.carbon", directory: "")
  141. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "GetD", linkageName: "_CGetD.Main", scope: null, file: !3, line: 6, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  142. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  143. // CHECK:STDOUT: !6 = !{}
  144. // CHECK:STDOUT: !7 = !DILocation(line: 6, column: 21, scope: !4)
  145. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "GetE", linkageName: "_CGetE.Main", scope: null, file: !3, line: 7, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  146. // CHECK:STDOUT: !9 = !DILocation(line: 7, column: 21, scope: !8)
  147. // CHECK:STDOUT: !10 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  148. // CHECK:STDOUT: !11 = !DILocation(line: 4, column: 1, scope: !10)
  149. // CHECK:STDOUT: !12 = !DILocation(line: 0, scope: !10)
  150. // CHECK:STDOUT: ; ModuleID = 'unnamed.carbon'
  151. // CHECK:STDOUT: source_filename = "unnamed.carbon"
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: @_Cx.Main = global { i32, i32 } zeroinitializer
  154. // CHECK:STDOUT: @var.anon.var_patt.loc5 = internal global { i32, i32 } zeroinitializer
  155. // CHECK:STDOUT: @tuple.21c.loc4_1 = internal constant { i32, i32 } { i32 1, i32 2 }
  156. // CHECK:STDOUT: @tuple.ffd.loc5_1 = internal constant { i32, i32 } { i32 3, i32 4 }
  157. // CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: define void @_C__global_init.Main() !dbg !4 {
  160. // CHECK:STDOUT: entry:
  161. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 @_Cx.Main, ptr align 4 @tuple.21c.loc4_1, i64 8, i1 false), !dbg !7
  162. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 @var.anon.var_patt.loc5, ptr align 4 @tuple.ffd.loc5_1, i64 8, i1 false), !dbg !8
  163. // CHECK:STDOUT: ret void, !dbg !9
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  167. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #0
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: ; uselistorder directives
  170. // CHECK:STDOUT: uselistorder ptr @llvm.memcpy.p0.p0.i64, { 1, 0 }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  175. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  178. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  179. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  180. // CHECK:STDOUT: !3 = !DIFile(filename: "unnamed.carbon", directory: "")
  181. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  182. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  183. // CHECK:STDOUT: !6 = !{}
  184. // CHECK:STDOUT: !7 = !DILocation(line: 4, column: 1, scope: !4)
  185. // CHECK:STDOUT: !8 = !DILocation(line: 5, column: 1, scope: !4)
  186. // CHECK:STDOUT: !9 = !DILocation(line: 0, scope: !4)