print_read.carbon 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/builtins/print_read.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/builtins/print_read.carbon
  10. import Core library "io";
  11. fn PrintChar(c: i8) -> i32 = "print.char";
  12. fn ReadChar() -> i32 = "read.char";
  13. fn Main() {
  14. Core.Print(1);
  15. let EOF: i32 = -1;
  16. while (ReadChar() != EOF) {
  17. // "Hi"
  18. if (PrintChar(0x48) != EOF) {
  19. PrintChar(0x69);
  20. }
  21. }
  22. }
  23. // CHECK:STDOUT: ; ModuleID = 'print_read.carbon'
  24. // CHECK:STDOUT: source_filename = "print_read.carbon"
  25. // CHECK:STDOUT:
  26. // CHECK:STDOUT: @printf.int.format = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
  27. // CHECK:STDOUT:
  28. // CHECK:STDOUT: define void @_CMain.Main() !dbg !4 {
  29. // CHECK:STDOUT: entry:
  30. // CHECK:STDOUT: %print.int = call i32 (ptr, ...) @printf(ptr @printf.int.format, i32 1), !dbg !7
  31. // CHECK:STDOUT: br label %while.cond, !dbg !8
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: while.cond: ; preds = %if.else, %entry
  34. // CHECK:STDOUT: %read.char = call i32 @getchar(), !dbg !9
  35. // CHECK:STDOUT: %int.neq.loc20 = icmp ne i32 %read.char, -1, !dbg !9
  36. // CHECK:STDOUT: br i1 %int.neq.loc20, label %while.body, label %while.done, !dbg !8
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: while.body: ; preds = %while.cond
  39. // CHECK:STDOUT: %print.char.loc22 = call i32 @putchar(i32 72), !dbg !10
  40. // CHECK:STDOUT: %int.neq.loc22 = icmp ne i32 %print.char.loc22, -1, !dbg !10
  41. // CHECK:STDOUT: br i1 %int.neq.loc22, label %if.then, label %if.else, !dbg !11
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: if.then: ; preds = %while.body
  44. // CHECK:STDOUT: %print.char.loc23 = call i32 @putchar(i32 105), !dbg !12
  45. // CHECK:STDOUT: br label %if.else, !dbg !13
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: if.else: ; preds = %if.then, %while.body
  48. // CHECK:STDOUT: br label %while.cond, !dbg !14
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: while.done: ; preds = %while.cond
  51. // CHECK:STDOUT: ret void, !dbg !15
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: declare i32 @printf(ptr, ...)
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: declare i32 @getchar()
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: declare i32 @putchar(i32)
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: ; uselistorder directives
  61. // CHECK:STDOUT: uselistorder ptr @putchar, { 1, 0 }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  64. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  67. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  68. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  69. // CHECK:STDOUT: !3 = !DIFile(filename: "print_read.carbon", directory: "")
  70. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Main", linkageName: "_CMain.Main", scope: null, file: !3, line: 16, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  71. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  72. // CHECK:STDOUT: !6 = !{}
  73. // CHECK:STDOUT: !7 = !DILocation(line: 17, column: 3, scope: !4)
  74. // CHECK:STDOUT: !8 = !DILocation(line: 20, column: 9, scope: !4)
  75. // CHECK:STDOUT: !9 = !DILocation(line: 20, column: 10, scope: !4)
  76. // CHECK:STDOUT: !10 = !DILocation(line: 22, column: 9, scope: !4)
  77. // CHECK:STDOUT: !11 = !DILocation(line: 22, column: 8, scope: !4)
  78. // CHECK:STDOUT: !12 = !DILocation(line: 23, column: 7, scope: !4)
  79. // CHECK:STDOUT: !13 = !DILocation(line: 22, column: 5, scope: !4)
  80. // CHECK:STDOUT: !14 = !DILocation(line: 20, column: 3, scope: !4)
  81. // CHECK:STDOUT: !15 = !DILocation(line: 16, column: 1, scope: !4)