Skip to content

Commit

Permalink
Switch to using Core.BigInt as the type of the size of a type liter…
Browse files Browse the repository at this point in the history
…al. (#4450)

This removes one of the few ways in which `i32` is special and gets us
closer to removing it as a special case.
  • Loading branch information
zygoloid authored Oct 28, 2024
1 parent 7ec8cea commit df68bf9
Show file tree
Hide file tree
Showing 29 changed files with 485 additions and 448 deletions.
6 changes: 3 additions & 3 deletions core/prelude/types.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export import library "prelude/types/bool";

fn BigInt() -> type = "big_int.make_type";
fn Int32() -> type = "int.make_type_32";
fn Int(size: i32) -> type = "int.make_type_signed";
fn UInt(size: i32) -> type = "int.make_type_unsigned";
fn Float(size: i32) -> type = "float.make_type";
fn Int(size: BigInt()) -> type = "int.make_type_signed";
fn UInt(size: BigInt()) -> type = "int.make_type_unsigned";
fn Float(size: BigInt()) -> type = "float.make_type";
17 changes: 15 additions & 2 deletions toolchain/check/handle_literal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ static auto MakeI32Literal(Context& context, Parse::NodeId node_id,
.int_id = context.ints().Add(i32_val)});
}

// Forms an IntLiteral instruction with type `BigInt` for a given literal
// integer value, which is assumed to be unsigned.
static auto MakeBigIntLiteral(Context& context, Parse::NodeId node_id,
IntId int_id) -> SemIR::InstId {
// TODO: `IntId`s with different bit-widths are considered different values
// here. Decide how we want to canonicalize these. For now this is only used
// by type literals, so we rely on the lexer picking some consistent rule.
return context.AddInst<SemIR::IntLiteral>(
node_id,
{.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::BigIntType),
.int_id = int_id});
}

auto HandleParseNode(Context& context, Parse::IntLiteralId node_id) -> bool {
// Convert the literal to i32.
// TODO: Form an integer literal value and a corresponding type here instead.
Expand Down Expand Up @@ -134,7 +147,7 @@ static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
node_id, IntWidthNotMultipleOf8, int_kind.is_signed(),
llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
}
auto width_id = MakeI32Literal(context, node_id, size_id);
auto width_id = MakeBigIntLiteral(context, node_id, size_id);
auto fn_inst_id = context.LookupNameInCore(
node_id, int_kind == SemIR::IntKind::Signed ? "Int" : "UInt");
auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {width_id});
Expand Down Expand Up @@ -175,7 +188,7 @@ auto HandleParseNode(Context& context, Parse::FloatTypeLiteralId node_id)
}
auto tok_id = context.parse_tree().node_token(node_id);
auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
auto width_id = MakeI32Literal(context, node_id, size_id);
auto width_id = MakeBigIntLiteral(context, node_id, size_id);
auto fn_inst_id = context.LookupNameInCore(node_id, "Float");
auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {width_id});
context.node_stack().Push(node_id, type_inst_id);
Expand Down
8 changes: 4 additions & 4 deletions toolchain/check/testdata/array/base.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var c: [(); 5] = ((), (), (), (), (),);
// CHECK:STDOUT: %.5: type = tuple_type (i32) [template]
// CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
// CHECK:STDOUT: %array.1: %.3 = tuple_value (%.2) [template]
// CHECK:STDOUT: %.7: i32 = int_literal 64 [template]
// CHECK:STDOUT: %.7: Core.BigInt = int_literal 64 [template]
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
// CHECK:STDOUT: %.8: i32 = int_literal 2 [template]
Expand Down Expand Up @@ -58,7 +58,7 @@ var c: [(); 5] = ((), (), (), (), (),);
// CHECK:STDOUT: import Core//prelude/types/bool
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -76,7 +76,7 @@ var c: [(); 5] = ((), (), (), (), (),);
// CHECK:STDOUT: %.loc11_15: type = array_type %.loc11_14, i32 [template = constants.%.3]
// CHECK:STDOUT: %a.var: ref %.3 = var a
// CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
// CHECK:STDOUT: %.loc12_9.1: i32 = int_literal 64 [template = constants.%.7]
// CHECK:STDOUT: %.loc12_9.1: Core.BigInt = int_literal 64 [template = constants.%.7]
// CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc12_9.1) [template = f64]
// CHECK:STDOUT: %.loc12_14: i32 = int_literal 2 [template = constants.%.8]
// CHECK:STDOUT: %.loc12_9.2: type = value_of_initializer %float.make_type [template = f64]
Expand All @@ -94,7 +94,7 @@ var c: [(); 5] = ((), (), (), (), (),);
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
Expand Down
8 changes: 4 additions & 4 deletions toolchain/check/testdata/basics/builtin_types.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var test_type: type = i32;
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
// CHECK:STDOUT: %.2: i32 = int_literal 0 [template]
// CHECK:STDOUT: %.3: i32 = int_literal 64 [template]
// CHECK:STDOUT: %.3: Core.BigInt = int_literal 64 [template]
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
// CHECK:STDOUT: %.4: f64 = float_literal 0.10000000000000001 [template]
Expand All @@ -42,7 +42,7 @@ var test_type: type = i32;
// CHECK:STDOUT: import Core//prelude/types/bool
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -59,7 +59,7 @@ var test_type: type = i32;
// CHECK:STDOUT: %.loc11_15.2: type = converted %int.make_type_32, %.loc11_15.1 [template = i32]
// CHECK:STDOUT: %test_i32.var: ref i32 = var test_i32
// CHECK:STDOUT: %test_i32: ref i32 = bind_name test_i32, %test_i32.var
// CHECK:STDOUT: %.loc12_15.1: i32 = int_literal 64 [template = constants.%.3]
// CHECK:STDOUT: %.loc12_15.1: Core.BigInt = int_literal 64 [template = constants.%.3]
// CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc12_15.1) [template = f64]
// CHECK:STDOUT: %.loc12_15.2: type = value_of_initializer %float.make_type [template = f64]
// CHECK:STDOUT: %.loc12_15.3: type = converted %float.make_type, %.loc12_15.2 [template = f64]
Expand All @@ -71,7 +71,7 @@ var test_type: type = i32;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let e: f64 = 5.0e39999999999999999993;
// CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
// CHECK:STDOUT: %.2: i32 = int_literal 64 [template]
// CHECK:STDOUT: %.2: Core.BigInt = int_literal 64 [template]
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
// CHECK:STDOUT: }
Expand All @@ -62,7 +62,7 @@ let e: f64 = 5.0e39999999999999999993;
// CHECK:STDOUT: import Core//prelude/types/bool
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -84,19 +84,19 @@ let e: f64 = 5.0e39999999999999999993;
// CHECK:STDOUT: %int.make_type_32.loc27: init type = call constants.%Int32() [template = i32]
// CHECK:STDOUT: %.loc27_8.1: type = value_of_initializer %int.make_type_32.loc27 [template = i32]
// CHECK:STDOUT: %.loc27_8.2: type = converted %int.make_type_32.loc27, %.loc27_8.1 [template = i32]
// CHECK:STDOUT: %.loc33_8.1: i32 = int_literal 64 [template = constants.%.2]
// CHECK:STDOUT: %.loc33_8.1: Core.BigInt = int_literal 64 [template = constants.%.2]
// CHECK:STDOUT: %float.make_type.loc33: init type = call constants.%Float(%.loc33_8.1) [template = f64]
// CHECK:STDOUT: %.loc33_8.2: type = value_of_initializer %float.make_type.loc33 [template = f64]
// CHECK:STDOUT: %.loc33_8.3: type = converted %float.make_type.loc33, %.loc33_8.2 [template = f64]
// CHECK:STDOUT: %.loc38_8.1: i32 = int_literal 64 [template = constants.%.2]
// CHECK:STDOUT: %.loc38_8.1: Core.BigInt = int_literal 64 [template = constants.%.2]
// CHECK:STDOUT: %float.make_type.loc38: init type = call constants.%Float(%.loc38_8.1) [template = f64]
// CHECK:STDOUT: %.loc38_8.2: type = value_of_initializer %float.make_type.loc38 [template = f64]
// CHECK:STDOUT: %.loc38_8.3: type = converted %float.make_type.loc38, %.loc38_8.2 [template = f64]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
Expand Down
8 changes: 4 additions & 4 deletions toolchain/check/testdata/basics/numeric_literals.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn F() {
// CHECK:STDOUT: %.13: i32 = int_literal 4 [template]
// CHECK:STDOUT: %.14: i32 = int_literal 5 [template]
// CHECK:STDOUT: %array.1: %.3 = tuple_value (%.5, %.6, %.5, %.5, %.7, %.7) [template]
// CHECK:STDOUT: %.15: i32 = int_literal 64 [template]
// CHECK:STDOUT: %.15: Core.BigInt = int_literal 64 [template]
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
// CHECK:STDOUT: %.16: type = array_type %.2, f64 [template]
Expand Down Expand Up @@ -80,7 +80,7 @@ fn F() {
// CHECK:STDOUT: import Core//prelude/types/bool
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand Down Expand Up @@ -129,7 +129,7 @@ fn F() {
// CHECK:STDOUT: %.loc21_3.20: init %.3 = array_init (%.loc21_3.4, %.loc21_3.7, %.loc21_3.10, %.loc21_3.13, %.loc21_3.16, %.loc21_3.19) to %ints.var [template = constants.%array.1]
// CHECK:STDOUT: %.loc21_4: init %.3 = converted %.loc21_3.1, %.loc21_3.20 [template = constants.%array.1]
// CHECK:STDOUT: assign %ints.var, %.loc21_4
// CHECK:STDOUT: %.loc22_16.1: i32 = int_literal 64 [template = constants.%.15]
// CHECK:STDOUT: %.loc22_16.1: Core.BigInt = int_literal 64 [template = constants.%.15]
// CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc22_16.1) [template = f64]
// CHECK:STDOUT: %.loc22_21: i32 = int_literal 6 [template = constants.%.2]
// CHECK:STDOUT: %.loc22_16.2: type = value_of_initializer %float.make_type [template = f64]
Expand Down Expand Up @@ -170,5 +170,5 @@ fn F() {
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
// CHECK:STDOUT:
Loading

0 comments on commit df68bf9

Please sign in to comment.