numeric_literals.carbon 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. fn F() {
  7. // 8 and 9 trigger special behavior in APInt when mishandling signed versus
  8. // unsigned, so we pay extra attention to those.
  9. var ints: [i32; 6] = (
  10. 8,
  11. 9,
  12. 0x8,
  13. 0b1000,
  14. 2147483647,
  15. 0x7FFFFFFF,
  16. );
  17. var floats: [f64; 7] = (
  18. 0.9,
  19. 8.0,
  20. 80.0,
  21. 1.0e7,
  22. 1.0e8,
  23. 1.0e-8,
  24. 39999999999999999993.0e39999999999999999993,
  25. );
  26. }
  27. // CHECK:STDOUT: --- numeric_literals.carbon
  28. // CHECK:STDOUT:
  29. // CHECK:STDOUT: constants {
  30. // CHECK:STDOUT: %.1: i32 = int_literal 6 [template]
  31. // CHECK:STDOUT: %.2: type = array_type %.1, i32 [template]
  32. // CHECK:STDOUT: %.3: type = ptr_type [i32; 6] [template]
  33. // CHECK:STDOUT: %.4: i32 = int_literal 8 [template]
  34. // CHECK:STDOUT: %.5: i32 = int_literal 9 [template]
  35. // CHECK:STDOUT: %.6: i32 = int_literal 2147483647 [template]
  36. // CHECK:STDOUT: %.7: type = tuple_type (i32, i32, i32, i32, i32, i32) [template]
  37. // CHECK:STDOUT: %.8: i32 = int_literal 0 [template]
  38. // CHECK:STDOUT: %.9: i32 = int_literal 1 [template]
  39. // CHECK:STDOUT: %.10: i32 = int_literal 2 [template]
  40. // CHECK:STDOUT: %.11: i32 = int_literal 3 [template]
  41. // CHECK:STDOUT: %.12: i32 = int_literal 4 [template]
  42. // CHECK:STDOUT: %.13: i32 = int_literal 5 [template]
  43. // CHECK:STDOUT: %.14: [i32; 6] = tuple_value (%.4, %.5, %.4, %.4, %.6, %.6) [template]
  44. // CHECK:STDOUT: %.15: i32 = int_literal 7 [template]
  45. // CHECK:STDOUT: %.16: type = array_type %.15, f64 [template]
  46. // CHECK:STDOUT: %.17: type = ptr_type [f64; 7] [template]
  47. // CHECK:STDOUT: %.18: f64 = real_literal 9e-1 [template]
  48. // CHECK:STDOUT: %.19: f64 = real_literal 80e-1 [template]
  49. // CHECK:STDOUT: %.20: f64 = real_literal 800e-1 [template]
  50. // CHECK:STDOUT: %.21: f64 = real_literal 10e6 [template]
  51. // CHECK:STDOUT: %.22: f64 = real_literal 10e7 [template]
  52. // CHECK:STDOUT: %.23: f64 = real_literal 10e-9 [template]
  53. // CHECK:STDOUT: %.24: f64 = real_literal 399999999999999999930e39999999999999999992 [template]
  54. // CHECK:STDOUT: %.25: type = tuple_type (f64, f64, f64, f64, f64, f64, f64) [template]
  55. // CHECK:STDOUT: %.26: [f64; 7] = tuple_value (%.18, %.19, %.20, %.21, %.22, %.23, %.24) [template]
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .F = %F
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %F: <function> = fn_decl @F [template] {}
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: fn @F() {
  66. // CHECK:STDOUT: !entry:
  67. // CHECK:STDOUT: %.loc10_19: i32 = int_literal 6 [template = constants.%.1]
  68. // CHECK:STDOUT: %.loc10_20: type = array_type %.loc10_19, i32 [template = constants.%.2]
  69. // CHECK:STDOUT: %ints.var: ref [i32; 6] = var ints
  70. // CHECK:STDOUT: %ints: ref [i32; 6] = bind_name ints, %ints.var
  71. // CHECK:STDOUT: %.loc11: i32 = int_literal 8 [template = constants.%.4]
  72. // CHECK:STDOUT: %.loc12: i32 = int_literal 9 [template = constants.%.5]
  73. // CHECK:STDOUT: %.loc13: i32 = int_literal 8 [template = constants.%.4]
  74. // CHECK:STDOUT: %.loc14: i32 = int_literal 8 [template = constants.%.4]
  75. // CHECK:STDOUT: %.loc15: i32 = int_literal 2147483647 [template = constants.%.6]
  76. // CHECK:STDOUT: %.loc16: i32 = int_literal 2147483647 [template = constants.%.6]
  77. // CHECK:STDOUT: %.loc17_3.1: (i32, i32, i32, i32, i32, i32) = tuple_literal (%.loc11, %.loc12, %.loc13, %.loc14, %.loc15, %.loc16)
  78. // CHECK:STDOUT: %.loc17_3.2: i32 = int_literal 0 [template = constants.%.8]
  79. // CHECK:STDOUT: %.loc17_3.3: ref i32 = array_index %ints.var, %.loc17_3.2
  80. // CHECK:STDOUT: %.loc17_3.4: init i32 = initialize_from %.loc11 to %.loc17_3.3 [template = constants.%.4]
  81. // CHECK:STDOUT: %.loc17_3.5: i32 = int_literal 1 [template = constants.%.9]
  82. // CHECK:STDOUT: %.loc17_3.6: ref i32 = array_index %ints.var, %.loc17_3.5
  83. // CHECK:STDOUT: %.loc17_3.7: init i32 = initialize_from %.loc12 to %.loc17_3.6 [template = constants.%.5]
  84. // CHECK:STDOUT: %.loc17_3.8: i32 = int_literal 2 [template = constants.%.10]
  85. // CHECK:STDOUT: %.loc17_3.9: ref i32 = array_index %ints.var, %.loc17_3.8
  86. // CHECK:STDOUT: %.loc17_3.10: init i32 = initialize_from %.loc13 to %.loc17_3.9 [template = constants.%.4]
  87. // CHECK:STDOUT: %.loc17_3.11: i32 = int_literal 3 [template = constants.%.11]
  88. // CHECK:STDOUT: %.loc17_3.12: ref i32 = array_index %ints.var, %.loc17_3.11
  89. // CHECK:STDOUT: %.loc17_3.13: init i32 = initialize_from %.loc14 to %.loc17_3.12 [template = constants.%.4]
  90. // CHECK:STDOUT: %.loc17_3.14: i32 = int_literal 4 [template = constants.%.12]
  91. // CHECK:STDOUT: %.loc17_3.15: ref i32 = array_index %ints.var, %.loc17_3.14
  92. // CHECK:STDOUT: %.loc17_3.16: init i32 = initialize_from %.loc15 to %.loc17_3.15 [template = constants.%.6]
  93. // CHECK:STDOUT: %.loc17_3.17: i32 = int_literal 5 [template = constants.%.13]
  94. // CHECK:STDOUT: %.loc17_3.18: ref i32 = array_index %ints.var, %.loc17_3.17
  95. // CHECK:STDOUT: %.loc17_3.19: init i32 = initialize_from %.loc16 to %.loc17_3.18 [template = constants.%.6]
  96. // CHECK:STDOUT: %.loc17_3.20: init [i32; 6] = array_init (%.loc17_3.4, %.loc17_3.7, %.loc17_3.10, %.loc17_3.13, %.loc17_3.16, %.loc17_3.19) to %ints.var [template = constants.%.14]
  97. // CHECK:STDOUT: %.loc17_3.21: init [i32; 6] = converted %.loc17_3.1, %.loc17_3.20 [template = constants.%.14]
  98. // CHECK:STDOUT: assign %ints.var, %.loc17_3.21
  99. // CHECK:STDOUT: %.loc18_21: i32 = int_literal 7 [template = constants.%.15]
  100. // CHECK:STDOUT: %.loc18_22: type = array_type %.loc18_21, f64 [template = constants.%.16]
  101. // CHECK:STDOUT: %floats.var: ref [f64; 7] = var floats
  102. // CHECK:STDOUT: %floats: ref [f64; 7] = bind_name floats, %floats.var
  103. // CHECK:STDOUT: %.loc19: f64 = real_literal 9e-1 [template = constants.%.18]
  104. // CHECK:STDOUT: %.loc20: f64 = real_literal 80e-1 [template = constants.%.19]
  105. // CHECK:STDOUT: %.loc21: f64 = real_literal 800e-1 [template = constants.%.20]
  106. // CHECK:STDOUT: %.loc22: f64 = real_literal 10e6 [template = constants.%.21]
  107. // CHECK:STDOUT: %.loc23: f64 = real_literal 10e7 [template = constants.%.22]
  108. // CHECK:STDOUT: %.loc24: f64 = real_literal 10e-9 [template = constants.%.23]
  109. // CHECK:STDOUT: %.loc25: f64 = real_literal 399999999999999999930e39999999999999999992 [template = constants.%.24]
  110. // CHECK:STDOUT: %.loc26_3.1: (f64, f64, f64, f64, f64, f64, f64) = tuple_literal (%.loc19, %.loc20, %.loc21, %.loc22, %.loc23, %.loc24, %.loc25)
  111. // CHECK:STDOUT: %.loc26_3.2: i32 = int_literal 0 [template = constants.%.8]
  112. // CHECK:STDOUT: %.loc26_3.3: ref f64 = array_index %floats.var, %.loc26_3.2
  113. // CHECK:STDOUT: %.loc26_3.4: init f64 = initialize_from %.loc19 to %.loc26_3.3 [template = constants.%.18]
  114. // CHECK:STDOUT: %.loc26_3.5: i32 = int_literal 1 [template = constants.%.9]
  115. // CHECK:STDOUT: %.loc26_3.6: ref f64 = array_index %floats.var, %.loc26_3.5
  116. // CHECK:STDOUT: %.loc26_3.7: init f64 = initialize_from %.loc20 to %.loc26_3.6 [template = constants.%.19]
  117. // CHECK:STDOUT: %.loc26_3.8: i32 = int_literal 2 [template = constants.%.10]
  118. // CHECK:STDOUT: %.loc26_3.9: ref f64 = array_index %floats.var, %.loc26_3.8
  119. // CHECK:STDOUT: %.loc26_3.10: init f64 = initialize_from %.loc21 to %.loc26_3.9 [template = constants.%.20]
  120. // CHECK:STDOUT: %.loc26_3.11: i32 = int_literal 3 [template = constants.%.11]
  121. // CHECK:STDOUT: %.loc26_3.12: ref f64 = array_index %floats.var, %.loc26_3.11
  122. // CHECK:STDOUT: %.loc26_3.13: init f64 = initialize_from %.loc22 to %.loc26_3.12 [template = constants.%.21]
  123. // CHECK:STDOUT: %.loc26_3.14: i32 = int_literal 4 [template = constants.%.12]
  124. // CHECK:STDOUT: %.loc26_3.15: ref f64 = array_index %floats.var, %.loc26_3.14
  125. // CHECK:STDOUT: %.loc26_3.16: init f64 = initialize_from %.loc23 to %.loc26_3.15 [template = constants.%.22]
  126. // CHECK:STDOUT: %.loc26_3.17: i32 = int_literal 5 [template = constants.%.13]
  127. // CHECK:STDOUT: %.loc26_3.18: ref f64 = array_index %floats.var, %.loc26_3.17
  128. // CHECK:STDOUT: %.loc26_3.19: init f64 = initialize_from %.loc24 to %.loc26_3.18 [template = constants.%.23]
  129. // CHECK:STDOUT: %.loc26_3.20: i32 = int_literal 6 [template = constants.%.1]
  130. // CHECK:STDOUT: %.loc26_3.21: ref f64 = array_index %floats.var, %.loc26_3.20
  131. // CHECK:STDOUT: %.loc26_3.22: init f64 = initialize_from %.loc25 to %.loc26_3.21 [template = constants.%.24]
  132. // CHECK:STDOUT: %.loc26_3.23: init [f64; 7] = array_init (%.loc26_3.4, %.loc26_3.7, %.loc26_3.10, %.loc26_3.13, %.loc26_3.16, %.loc26_3.19, %.loc26_3.22) to %floats.var [template = constants.%.26]
  133. // CHECK:STDOUT: %.loc26_3.24: init [f64; 7] = converted %.loc26_3.1, %.loc26_3.23 [template = constants.%.26]
  134. // CHECK:STDOUT: assign %floats.var, %.loc26_3.24
  135. // CHECK:STDOUT: return
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: