authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-14 10:27:44-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-14 10:27:44-05:00
loga8b36fbe34e4acfea1fcb348fbed321b05611fd3
tree23f489f85c427a003577f5c40b74201ba8c85ddb
parentcdc5070f216a924d24588b8d0fe06400e036e6bf
parent40b9db7cad6f876bb3e8fa32d7b32bbd4bc983ea
signaturelock-open Commit is signed but in an unrecognized format.

Merge remote-tracking branch 'origin/master' into llvm10


68 files changed, 3303 insertions(+), 1752 deletions(-)

CMakeLists.txt+3-13
...@@ -5,18 +5,6 @@ if(NOT CMAKE_BUILD_TYPE)...@@ -5,18 +5,6 @@ if(NOT CMAKE_BUILD_TYPE)
5 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)5 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
6endif()6endif()
77
8set(_list "None;Debug;Release;RelWithDebInfo;MinSizeRel")
9list(FIND _list ${CMAKE_BUILD_TYPE} _index)
10if(${_index} EQUAL -1)
11 string(REPLACE ";" ", " _list_pretty "${_list}")
12 message("::")
13 message(":: ERROR: Invalid build type: ${CMAKE_BUILD_TYPE}")
14 message("::")
15 message(":: valid types: { ${_list_pretty} }")
16 message("::")
17 message(FATAL_ERROR)
18endif()
19
20if(NOT CMAKE_INSTALL_PREFIX)8if(NOT CMAKE_INSTALL_PREFIX)
21 set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE STRING9 set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE STRING
22 "Directory to install zig to" FORCE)10 "Directory to install zig to" FORCE)
...@@ -256,7 +244,7 @@ set(ZIG_MAIN_SRC "${CMAKE_SOURCE_DIR}/src/main.cpp")...@@ -256,7 +244,7 @@ set(ZIG_MAIN_SRC "${CMAKE_SOURCE_DIR}/src/main.cpp")
256set(ZIG0_SHIM_SRC "${CMAKE_SOURCE_DIR}/src/userland.cpp")244set(ZIG0_SHIM_SRC "${CMAKE_SOURCE_DIR}/src/userland.cpp")
257245
258if(ZIG_ENABLE_MEM_PROFILE)246if(ZIG_ENABLE_MEM_PROFILE)
259 set(ZIG_SOURCES_MEM_PROFILE "${CMAKE_SOURCE_DIR}/src/memory_profiling.cpp")247 set(ZIG_SOURCES_MEM_PROFILE "${CMAKE_SOURCE_DIR}/src/mem_profile.cpp")
260endif()248endif()
261249
262set(ZIG_SOURCES250set(ZIG_SOURCES
...@@ -272,10 +260,12 @@ set(ZIG_SOURCES...@@ -272,10 +260,12 @@ set(ZIG_SOURCES
272 "${CMAKE_SOURCE_DIR}/src/errmsg.cpp"260 "${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
273 "${CMAKE_SOURCE_DIR}/src/error.cpp"261 "${CMAKE_SOURCE_DIR}/src/error.cpp"
274 "${CMAKE_SOURCE_DIR}/src/glibc.cpp"262 "${CMAKE_SOURCE_DIR}/src/glibc.cpp"
263 "${CMAKE_SOURCE_DIR}/src/heap.cpp"
275 "${CMAKE_SOURCE_DIR}/src/ir.cpp"264 "${CMAKE_SOURCE_DIR}/src/ir.cpp"
276 "${CMAKE_SOURCE_DIR}/src/ir_print.cpp"265 "${CMAKE_SOURCE_DIR}/src/ir_print.cpp"
277 "${CMAKE_SOURCE_DIR}/src/libc_installation.cpp"266 "${CMAKE_SOURCE_DIR}/src/libc_installation.cpp"
278 "${CMAKE_SOURCE_DIR}/src/link.cpp"267 "${CMAKE_SOURCE_DIR}/src/link.cpp"
268 "${CMAKE_SOURCE_DIR}/src/mem.cpp"
279 "${CMAKE_SOURCE_DIR}/src/os.cpp"269 "${CMAKE_SOURCE_DIR}/src/os.cpp"
280 "${CMAKE_SOURCE_DIR}/src/parser.cpp"270 "${CMAKE_SOURCE_DIR}/src/parser.cpp"
281 "${CMAKE_SOURCE_DIR}/src/range_set.cpp"271 "${CMAKE_SOURCE_DIR}/src/range_set.cpp"
lib/std/array_list.zig+3-11
...@@ -244,10 +244,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {...@@ -244,10 +244,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
244}244}
245245
246test "std.ArrayList.init" {246test "std.ArrayList.init" {
247 var bytes: [1024]u8 = undefined;247 var list = ArrayList(i32).init(testing.allocator);
248 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
249
250 var list = ArrayList(i32).init(allocator);
251 defer list.deinit();248 defer list.deinit();
252249
253 testing.expect(list.len == 0);250 testing.expect(list.len == 0);
...@@ -255,19 +252,14 @@ test "std.ArrayList.init" {...@@ -255,19 +252,14 @@ test "std.ArrayList.init" {
255}252}
256253
257test "std.ArrayList.initCapacity" {254test "std.ArrayList.initCapacity" {
258 var bytes: [1024]u8 = undefined;255 var list = try ArrayList(i8).initCapacity(testing.allocator, 200);
259 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
260 var list = try ArrayList(i8).initCapacity(allocator, 200);
261 defer list.deinit();256 defer list.deinit();
262 testing.expect(list.len == 0);257 testing.expect(list.len == 0);
263 testing.expect(list.capacity() >= 200);258 testing.expect(list.capacity() >= 200);
264}259}
265260
266test "std.ArrayList.basic" {261test "std.ArrayList.basic" {
267 var bytes: [1024]u8 = undefined;262 var list = ArrayList(i32).init(testing.allocator);
268 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
269
270 var list = ArrayList(i32).init(allocator);
271 defer list.deinit();263 defer list.deinit();
272264
273 // setting on empty list is out of bounds265 // setting on empty list is out of bounds
lib/std/ascii.zig+2-3
...@@ -236,9 +236,8 @@ pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8)...@@ -236,9 +236,8 @@ pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8)
236}236}
237237
238test "allocLowerString" {238test "allocLowerString" {
239 var buf: [100]u8 = undefined;239 const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
240 const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;240 defer std.testing.allocator.free(result);
241 const result = try allocLowerString(allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
242 std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result));241 std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result));
243}242}
244243
lib/std/c/tokenizer.zig+13-3
...@@ -651,6 +651,7 @@ pub const Tokenizer = struct {...@@ -651,6 +651,7 @@ pub const Tokenizer = struct {
651 state = .StringLiteral;651 state = .StringLiteral;
652 },652 },
653 else => {653 else => {
654 self.index -= 1;
654 state = .Identifier;655 state = .Identifier;
655 },656 },
656 },657 },
...@@ -660,6 +661,7 @@ pub const Tokenizer = struct {...@@ -660,6 +661,7 @@ pub const Tokenizer = struct {
660 state = .StringLiteral;661 state = .StringLiteral;
661 },662 },
662 else => {663 else => {
664 self.index -= 1;
663 state = .Identifier;665 state = .Identifier;
664 },666 },
665 },667 },
...@@ -673,6 +675,7 @@ pub const Tokenizer = struct {...@@ -673,6 +675,7 @@ pub const Tokenizer = struct {
673 state = .StringLiteral;675 state = .StringLiteral;
674 },676 },
675 else => {677 else => {
678 self.index -= 1;
676 state = .Identifier;679 state = .Identifier;
677 },680 },
678 },681 },
...@@ -686,6 +689,7 @@ pub const Tokenizer = struct {...@@ -686,6 +689,7 @@ pub const Tokenizer = struct {
686 state = .StringLiteral;689 state = .StringLiteral;
687 },690 },
688 else => {691 else => {
692 self.index -= 1;
689 state = .Identifier;693 state = .Identifier;
690 },694 },
691 },695 },
...@@ -1079,6 +1083,9 @@ pub const Tokenizer = struct {...@@ -1079,6 +1083,9 @@ pub const Tokenizer = struct {
1079 'x', 'X' => {1083 'x', 'X' => {
1080 state = .IntegerLiteralHex;1084 state = .IntegerLiteralHex;
1081 },1085 },
1086 '.' => {
1087 state = .FloatFraction;
1088 },
1082 else => {1089 else => {
1083 state = .IntegerSuffix;1090 state = .IntegerSuffix;
1084 self.index -= 1;1091 self.index -= 1;
...@@ -1261,13 +1268,16 @@ pub const Tokenizer = struct {...@@ -1261,13 +1268,16 @@ pub const Tokenizer = struct {
1261 .UnicodeEscape,1268 .UnicodeEscape,
1262 .MultiLineComment,1269 .MultiLineComment,
1263 .MultiLineCommentAsterisk,1270 .MultiLineCommentAsterisk,
1264 .FloatFraction,
1265 .FloatFractionHex,
1266 .FloatExponent,1271 .FloatExponent,
1267 .FloatExponentDigits,
1268 .MacroString,1272 .MacroString,
1269 => result.id = .Invalid,1273 => result.id = .Invalid,
12701274
1275 .FloatExponentDigits => result.id = if (counter == 0) .Invalid else .{ .FloatLiteral = .None },
1276
1277 .FloatFraction,
1278 .FloatFractionHex,
1279 => result.id = .{ .FloatLiteral = .None },
1280
1271 .IntegerLiteralOct,1281 .IntegerLiteralOct,
1272 .IntegerLiteralBinary,1282 .IntegerLiteralBinary,
1273 .IntegerLiteralHex,1283 .IntegerLiteralHex,
lib/std/cstr.zig+2-3
...@@ -41,9 +41,8 @@ pub fn addNullByte(allocator: *mem.Allocator, slice: []const u8) ![:0]u8 {...@@ -41,9 +41,8 @@ pub fn addNullByte(allocator: *mem.Allocator, slice: []const u8) ![:0]u8 {
41}41}
4242
43test "addNullByte" {43test "addNullByte" {
44 var buf: [30]u8 = undefined;44 const slice = try addNullByte(std.testing.allocator, "hello"[0..4]);
45 const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;45 defer std.testing.allocator.free(slice);
46 const slice = try addNullByte(allocator, "hello"[0..4]);
47 testing.expect(slice.len == 4);46 testing.expect(slice.len == 4);
48 testing.expect(slice[4] == 0);47 testing.expect(slice[4] == 0);
49}48}
lib/std/fmt.zig+50-6
...@@ -69,12 +69,12 @@ fn peekIsAlign(comptime fmt: []const u8) bool {...@@ -69,12 +69,12 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
69///69///
70/// If a formatted user type contains a function of the type70/// If a formatted user type contains a function of the type
71/// ```71/// ```
72/// fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, context: var, comptime Errors: type, output: fn (@TypeOf(context), []const u8) Errors!void) Errors!void72/// fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, context: var, comptime Errors: type, comptime output: fn (@TypeOf(context), []const u8) Errors!void) Errors!void
73/// ```73/// ```
74/// with `?` being the type formatted, this function will be called instead of the default implementation.74/// with `?` being the type formatted, this function will be called instead of the default implementation.
75/// This allows user types to be formatted in a logical manner instead of dumping all fields of the type.75/// This allows user types to be formatted in a logical manner instead of dumping all fields of the type.
76///76///
77/// A user type may be a `struct`, `union` or `enum` type.77/// A user type may be a `struct`, `vector`, `union` or `enum` type.
78pub fn format(78pub fn format(
79 context: var,79 context: var,
80 comptime Errors: type,80 comptime Errors: type,
...@@ -373,11 +373,11 @@ pub fn formatType(...@@ -373,11 +373,11 @@ pub fn formatType(
373 try output(context, @typeName(T));373 try output(context, @typeName(T));
374 if (enumInfo.is_exhaustive) {374 if (enumInfo.is_exhaustive) {
375 try output(context, ".");375 try output(context, ".");
376 return formatType(@tagName(value), "", options, context, Errors, output, max_depth);376 try output(context, @tagName(value));
377 } else {377 } else {
378 // TODO: when @tagName works on exhaustive enums print known enum strings378 // TODO: when @tagName works on exhaustive enums print known enum strings
379 try output(context, "(");379 try output(context, "(");
380 try formatType(@enumToInt(value), "", options, context, Errors, output, max_depth);380 try formatType(@enumToInt(value), fmt, options, context, Errors, output, max_depth);
381 try output(context, ")");381 try output(context, ")");
382 }382 }
383 },383 },
...@@ -397,7 +397,7 @@ pub fn formatType(...@@ -397,7 +397,7 @@ pub fn formatType(
397 try output(context, " = ");397 try output(context, " = ");
398 inline for (info.fields) |u_field| {398 inline for (info.fields) |u_field| {
399 if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) {399 if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) {
400 try formatType(@field(value, u_field.name), "", options, context, Errors, output, max_depth - 1);400 try formatType(@field(value, u_field.name), fmt, options, context, Errors, output, max_depth - 1);
401 }401 }
402 }402 }
403 try output(context, " }");403 try output(context, " }");
...@@ -424,7 +424,7 @@ pub fn formatType(...@@ -424,7 +424,7 @@ pub fn formatType(
424 }424 }
425 try output(context, @memberName(T, field_i));425 try output(context, @memberName(T, field_i));
426 try output(context, " = ");426 try output(context, " = ");
427 try formatType(@field(value, @memberName(T, field_i)), "", options, context, Errors, output, max_depth - 1);427 try formatType(@field(value, @memberName(T, field_i)), fmt, options, context, Errors, output, max_depth - 1);
428 }428 }
429 try output(context, " }");429 try output(context, " }");
430 },430 },
...@@ -474,6 +474,18 @@ pub fn formatType(...@@ -474,6 +474,18 @@ pub fn formatType(
474 });474 });
475 return formatType(@as(Slice, &value), fmt, options, context, Errors, output, max_depth);475 return formatType(@as(Slice, &value), fmt, options, context, Errors, output, max_depth);
476 },476 },
477 .Vector => {
478 const len = @typeInfo(T).Vector.len;
479 try output(context, "{ ");
480 var i: usize = 0;
481 while (i < len) : (i += 1) {
482 try formatValue(value[i], fmt, options, context, Errors, output);
483 if (i < len - 1) {
484 try output(context, ", ");
485 }
486 }
487 try output(context, " }");
488 },
477 .Fn => {489 .Fn => {
478 return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });490 return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
479 },491 },
...@@ -500,6 +512,7 @@ fn formatValue(...@@ -500,6 +512,7 @@ fn formatValue(
500 switch (@typeId(T)) {512 switch (@typeId(T)) {
501 .Float => return formatFloatValue(value, fmt, options, context, Errors, output),513 .Float => return formatFloatValue(value, fmt, options, context, Errors, output),
502 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output),514 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output),
515 .Bool => return output(context, if (value) "true" else "false"),
503 else => comptime unreachable,516 else => comptime unreachable,
504 }517 }
505}518}
...@@ -1343,6 +1356,20 @@ test "enum" {...@@ -1343,6 +1356,20 @@ test "enum" {
1343 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});1356 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});
1344}1357}
13451358
1359test "non-exhaustive enum" {
1360 const Enum = enum(u16) {
1361 One = 0x000f,
1362 Two = 0xbeef,
1363 _,
1364 };
1365 try testFmt("enum: Enum(15)\n", "enum: {}\n", .{Enum.One});
1366 try testFmt("enum: Enum(48879)\n", "enum: {}\n", .{Enum.Two});
1367 try testFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)});
1368 try testFmt("enum: Enum(f)\n", "enum: {x}\n", .{Enum.One});
1369 try testFmt("enum: Enum(beef)\n", "enum: {x}\n", .{Enum.Two});
1370 try testFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)});
1371}
1372
1346test "float.scientific" {1373test "float.scientific" {
1347 try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)});1374 try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)});
1348 try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)});1375 try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)});
...@@ -1699,3 +1726,20 @@ test "positional with specifier" {...@@ -1699,3 +1726,20 @@ test "positional with specifier" {
1699test "positional/alignment/width/precision" {1726test "positional/alignment/width/precision" {
1700 try testFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)});1727 try testFmt("10.0", "{0d: >3.1}", .{@as(f64, 9.999)});
1701}1728}
1729
1730test "vector" {
1731 // https://github.com/ziglang/zig/issues/3317
1732 if (builtin.arch == .mipsel) return error.SkipZigTest;
1733
1734 const vbool: @Vector(4, bool) = [_]bool{ true, false, true, false };
1735 const vi64: @Vector(4, i64) = [_]i64{ -2, -1, 0, 1 };
1736 const vu64: @Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 };
1737
1738 try testFmt("{ true, false, true, false }", "{}", .{vbool});
1739 try testFmt("{ -2, -1, 0, 1 }", "{}", .{vi64});
1740 try testFmt("{ - 2, - 1, + 0, + 1 }", "{d:5}", .{vi64});
1741 try testFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64});
1742 try testFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64});
1743 try testFmt("{ 1kB, 2kB, 3kB, 4kB }", "{B}", .{vu64});
1744 try testFmt("{ 1000B, 1.953125KiB, 2.9296875KiB, 3.90625KiB }", "{Bi}", .{vu64});
1745}
lib/std/fs/get_app_data_dir.zig+2-4
...@@ -56,9 +56,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD...@@ -56,9 +56,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
56}56}
5757
58test "getAppDataDir" {58test "getAppDataDir" {
59 var buf: [512]u8 = undefined;
60 const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
61
62 // We can't actually validate the result59 // We can't actually validate the result
63 _ = getAppDataDir(allocator, "zig") catch return;60 const dir = getAppDataDir(std.testing.allocator, "zig") catch return;
61 defer std.testing.allocator.free(dir);
64}62}
lib/std/fs/path.zig+4-6
...@@ -89,16 +89,14 @@ pub fn joinPosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -89,16 +89,14 @@ pub fn joinPosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
89}89}
9090
91fn testJoinWindows(paths: []const []const u8, expected: []const u8) void {91fn testJoinWindows(paths: []const []const u8, expected: []const u8) void {
92 var buf: [1024]u8 = undefined;92 const actual = joinWindows(testing.allocator, paths) catch @panic("fail");
93 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;93 defer testing.allocator.free(actual);
94 const actual = joinWindows(a, paths) catch @panic("fail");
95 testing.expectEqualSlices(u8, expected, actual);94 testing.expectEqualSlices(u8, expected, actual);
96}95}
9796
98fn testJoinPosix(paths: []const []const u8, expected: []const u8) void {97fn testJoinPosix(paths: []const []const u8, expected: []const u8) void {
99 var buf: [1024]u8 = undefined;98 const actual = joinPosix(testing.allocator, paths) catch @panic("fail");
100 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;99 defer testing.allocator.free(actual);
101 const actual = joinPosix(a, paths) catch @panic("fail");
102 testing.expectEqualSlices(u8, expected, actual);100 testing.expectEqualSlices(u8, expected, actual);
103}101}
104102
lib/std/heap.zig+1-1
...@@ -533,7 +533,7 @@ pub const ArenaAllocator = struct {...@@ -533,7 +533,7 @@ pub const ArenaAllocator = struct {
533 };533 };
534 }534 }
535535
536 pub fn deinit(self: *ArenaAllocator) void {536 pub fn deinit(self: ArenaAllocator) void {
537 var it = self.buffer_list.first;537 var it = self.buffer_list.first;
538 while (it) |node| {538 while (it) |node| {
539 // this has to occur before the free because the free frees node539 // this has to occur before the free because the free frees node
lib/std/http/headers.zig+22-26
...@@ -83,12 +83,8 @@ const HeaderEntry = struct {...@@ -83,12 +83,8 @@ const HeaderEntry = struct {
83 }83 }
84};84};
8585
86var test_memory: [32 * 1024]u8 = undefined;
87var test_fba_state = std.heap.FixedBufferAllocator.init(&test_memory);
88const test_allocator = &test_fba_state.allocator;
89
90test "HeaderEntry" {86test "HeaderEntry" {
91 var e = try HeaderEntry.init(test_allocator, "foo", "bar", null);87 var e = try HeaderEntry.init(testing.allocator, "foo", "bar", null);
92 defer e.deinit();88 defer e.deinit();
93 testing.expectEqualSlices(u8, "foo", e.name);89 testing.expectEqualSlices(u8, "foo", e.name);
94 testing.expectEqualSlices(u8, "bar", e.value);90 testing.expectEqualSlices(u8, "bar", e.value);
...@@ -368,7 +364,7 @@ pub const Headers = struct {...@@ -368,7 +364,7 @@ pub const Headers = struct {
368};364};
369365
370test "Headers.iterator" {366test "Headers.iterator" {
371 var h = Headers.init(test_allocator);367 var h = Headers.init(testing.allocator);
372 defer h.deinit();368 defer h.deinit();
373 try h.append("foo", "bar", null);369 try h.append("foo", "bar", null);
374 try h.append("cookie", "somevalue", null);370 try h.append("cookie", "somevalue", null);
...@@ -390,7 +386,7 @@ test "Headers.iterator" {...@@ -390,7 +386,7 @@ test "Headers.iterator" {
390}386}
391387
392test "Headers.contains" {388test "Headers.contains" {
393 var h = Headers.init(test_allocator);389 var h = Headers.init(testing.allocator);
394 defer h.deinit();390 defer h.deinit();
395 try h.append("foo", "bar", null);391 try h.append("foo", "bar", null);
396 try h.append("cookie", "somevalue", null);392 try h.append("cookie", "somevalue", null);
...@@ -400,7 +396,7 @@ test "Headers.contains" {...@@ -400,7 +396,7 @@ test "Headers.contains" {
400}396}
401397
402test "Headers.delete" {398test "Headers.delete" {
403 var h = Headers.init(test_allocator);399 var h = Headers.init(testing.allocator);
404 defer h.deinit();400 defer h.deinit();
405 try h.append("foo", "bar", null);401 try h.append("foo", "bar", null);
406 try h.append("baz", "qux", null);402 try h.append("baz", "qux", null);
...@@ -428,7 +424,7 @@ test "Headers.delete" {...@@ -428,7 +424,7 @@ test "Headers.delete" {
428}424}
429425
430test "Headers.orderedRemove" {426test "Headers.orderedRemove" {
431 var h = Headers.init(test_allocator);427 var h = Headers.init(testing.allocator);
432 defer h.deinit();428 defer h.deinit();
433 try h.append("foo", "bar", null);429 try h.append("foo", "bar", null);
434 try h.append("baz", "qux", null);430 try h.append("baz", "qux", null);
...@@ -451,7 +447,7 @@ test "Headers.orderedRemove" {...@@ -451,7 +447,7 @@ test "Headers.orderedRemove" {
451}447}
452448
453test "Headers.swapRemove" {449test "Headers.swapRemove" {
454 var h = Headers.init(test_allocator);450 var h = Headers.init(testing.allocator);
455 defer h.deinit();451 defer h.deinit();
456 try h.append("foo", "bar", null);452 try h.append("foo", "bar", null);
457 try h.append("baz", "qux", null);453 try h.append("baz", "qux", null);
...@@ -474,7 +470,7 @@ test "Headers.swapRemove" {...@@ -474,7 +470,7 @@ test "Headers.swapRemove" {
474}470}
475471
476test "Headers.at" {472test "Headers.at" {
477 var h = Headers.init(test_allocator);473 var h = Headers.init(testing.allocator);
478 defer h.deinit();474 defer h.deinit();
479 try h.append("foo", "bar", null);475 try h.append("foo", "bar", null);
480 try h.append("cookie", "somevalue", null);476 try h.append("cookie", "somevalue", null);
...@@ -494,7 +490,7 @@ test "Headers.at" {...@@ -494,7 +490,7 @@ test "Headers.at" {
494}490}
495491
496test "Headers.getIndices" {492test "Headers.getIndices" {
497 var h = Headers.init(test_allocator);493 var h = Headers.init(testing.allocator);
498 defer h.deinit();494 defer h.deinit();
499 try h.append("foo", "bar", null);495 try h.append("foo", "bar", null);
500 try h.append("set-cookie", "x=1", null);496 try h.append("set-cookie", "x=1", null);
...@@ -506,27 +502,27 @@ test "Headers.getIndices" {...@@ -506,27 +502,27 @@ test "Headers.getIndices" {
506}502}
507503
508test "Headers.get" {504test "Headers.get" {
509 var h = Headers.init(test_allocator);505 var h = Headers.init(testing.allocator);
510 defer h.deinit();506 defer h.deinit();
511 try h.append("foo", "bar", null);507 try h.append("foo", "bar", null);
512 try h.append("set-cookie", "x=1", null);508 try h.append("set-cookie", "x=1", null);
513 try h.append("set-cookie", "y=2", null);509 try h.append("set-cookie", "y=2", null);
514510
515 {511 {
516 const v = try h.get(test_allocator, "not-present");512 const v = try h.get(testing.allocator, "not-present");
517 testing.expect(null == v);513 testing.expect(null == v);
518 }514 }
519 {515 {
520 const v = (try h.get(test_allocator, "foo")).?;516 const v = (try h.get(testing.allocator, "foo")).?;
521 defer test_allocator.free(v);517 defer testing.allocator.free(v);
522 const e = v[0];518 const e = v[0];
523 testing.expectEqualSlices(u8, "foo", e.name);519 testing.expectEqualSlices(u8, "foo", e.name);
524 testing.expectEqualSlices(u8, "bar", e.value);520 testing.expectEqualSlices(u8, "bar", e.value);
525 testing.expectEqual(false, e.never_index);521 testing.expectEqual(false, e.never_index);
526 }522 }
527 {523 {
528 const v = (try h.get(test_allocator, "set-cookie")).?;524 const v = (try h.get(testing.allocator, "set-cookie")).?;
529 defer test_allocator.free(v);525 defer testing.allocator.free(v);
530 {526 {
531 const e = v[0];527 const e = v[0];
532 testing.expectEqualSlices(u8, "set-cookie", e.name);528 testing.expectEqualSlices(u8, "set-cookie", e.name);
...@@ -543,30 +539,30 @@ test "Headers.get" {...@@ -543,30 +539,30 @@ test "Headers.get" {
543}539}
544540
545test "Headers.getCommaSeparated" {541test "Headers.getCommaSeparated" {
546 var h = Headers.init(test_allocator);542 var h = Headers.init(testing.allocator);
547 defer h.deinit();543 defer h.deinit();
548 try h.append("foo", "bar", null);544 try h.append("foo", "bar", null);
549 try h.append("set-cookie", "x=1", null);545 try h.append("set-cookie", "x=1", null);
550 try h.append("set-cookie", "y=2", null);546 try h.append("set-cookie", "y=2", null);
551547
552 {548 {
553 const v = try h.getCommaSeparated(test_allocator, "not-present");549 const v = try h.getCommaSeparated(testing.allocator, "not-present");
554 testing.expect(null == v);550 testing.expect(null == v);
555 }551 }
556 {552 {
557 const v = (try h.getCommaSeparated(test_allocator, "foo")).?;553 const v = (try h.getCommaSeparated(testing.allocator, "foo")).?;
558 defer test_allocator.free(v);554 defer testing.allocator.free(v);
559 testing.expectEqualSlices(u8, "bar", v);555 testing.expectEqualSlices(u8, "bar", v);
560 }556 }
561 {557 {
562 const v = (try h.getCommaSeparated(test_allocator, "set-cookie")).?;558 const v = (try h.getCommaSeparated(testing.allocator, "set-cookie")).?;
563 defer test_allocator.free(v);559 defer testing.allocator.free(v);
564 testing.expectEqualSlices(u8, "x=1,y=2", v);560 testing.expectEqualSlices(u8, "x=1,y=2", v);
565 }561 }
566}562}
567563
568test "Headers.sort" {564test "Headers.sort" {
569 var h = Headers.init(test_allocator);565 var h = Headers.init(testing.allocator);
570 defer h.deinit();566 defer h.deinit();
571 try h.append("foo", "bar", null);567 try h.append("foo", "bar", null);
572 try h.append("cookie", "somevalue", null);568 try h.append("cookie", "somevalue", null);
...@@ -587,7 +583,7 @@ test "Headers.sort" {...@@ -587,7 +583,7 @@ test "Headers.sort" {
587}583}
588584
589test "Headers.format" {585test "Headers.format" {
590 var h = Headers.init(test_allocator);586 var h = Headers.init(testing.allocator);
591 defer h.deinit();587 defer h.deinit();
592 try h.append("foo", "bar", null);588 try h.append("foo", "bar", null);
593 try h.append("cookie", "somevalue", null);589 try h.append("cookie", "somevalue", null);
lib/std/io.zig+4-8
...@@ -223,15 +223,13 @@ test "io.BufferedInStream" {...@@ -223,15 +223,13 @@ test "io.BufferedInStream" {
223 }223 }
224 };224 };
225225
226 var buf: [100]u8 = undefined;
227 const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
228
229 const str = "This is a test";226 const str = "This is a test";
230 var one_byte_stream = OneByteReadInStream.init(str);227 var one_byte_stream = OneByteReadInStream.init(str);
231 var buf_in_stream = BufferedInStream(OneByteReadInStream.Error).init(&one_byte_stream.stream);228 var buf_in_stream = BufferedInStream(OneByteReadInStream.Error).init(&one_byte_stream.stream);
232 const stream = &buf_in_stream.stream;229 const stream = &buf_in_stream.stream;
233230
234 const res = try stream.readAllAlloc(allocator, str.len + 1);231 const res = try stream.readAllAlloc(testing.allocator, str.len + 1);
232 defer testing.allocator.free(res);
235 testing.expectEqualSlices(u8, str, res);233 testing.expectEqualSlices(u8, str, res);
236}234}
237235
...@@ -874,10 +872,8 @@ pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 {...@@ -874,10 +872,8 @@ pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 {
874}872}
875873
876test "io.readLineFrom" {874test "io.readLineFrom" {
877 var bytes: [128]u8 = undefined;875 var buf = try std.Buffer.initSize(testing.allocator, 0);
878 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;876 defer buf.deinit();
879
880 var buf = try std.Buffer.initSize(allocator, 0);
881 var mem_stream = SliceInStream.init(877 var mem_stream = SliceInStream.init(
882 \\Line 1878 \\Line 1
883 \\Line 22879 \\Line 22
lib/std/io/test.zig+4-9
...@@ -11,9 +11,6 @@ const fs = std.fs;...@@ -11,9 +11,6 @@ const fs = std.fs;
11const File = std.fs.File;11const File = std.fs.File;
1212
13test "write a file, read it, then delete it" {13test "write a file, read it, then delete it" {
14 var raw_bytes: [200 * 1024]u8 = undefined;
15 var allocator = &std.heap.FixedBufferAllocator.init(raw_bytes[0..]).allocator;
16
17 const cwd = fs.cwd();14 const cwd = fs.cwd();
1815
19 var data: [1024]u8 = undefined;16 var data: [1024]u8 = undefined;
...@@ -53,8 +50,8 @@ test "write a file, read it, then delete it" {...@@ -53,8 +50,8 @@ test "write a file, read it, then delete it" {
53 var file_in_stream = file.inStream();50 var file_in_stream = file.inStream();
54 var buf_stream = io.BufferedInStream(File.ReadError).init(&file_in_stream.stream);51 var buf_stream = io.BufferedInStream(File.ReadError).init(&file_in_stream.stream);
55 const st = &buf_stream.stream;52 const st = &buf_stream.stream;
56 const contents = try st.readAllAlloc(allocator, 2 * 1024);53 const contents = try st.readAllAlloc(std.testing.allocator, 2 * 1024);
57 defer allocator.free(contents);54 defer std.testing.allocator.free(contents);
5855
59 expect(mem.eql(u8, contents[0.."begin".len], "begin"));56 expect(mem.eql(u8, contents[0.."begin".len], "begin"));
60 expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], &data));57 expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], &data));
...@@ -64,10 +61,8 @@ test "write a file, read it, then delete it" {...@@ -64,10 +61,8 @@ test "write a file, read it, then delete it" {
64}61}
6562
66test "BufferOutStream" {63test "BufferOutStream" {
67 var bytes: [100]u8 = undefined;64 var buffer = try std.Buffer.initSize(std.testing.allocator, 0);
68 var allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;65 defer buffer.deinit();
69
70 var buffer = try std.Buffer.initSize(allocator, 0);
71 var buf_stream = &std.io.BufferOutStream.init(&buffer).stream;66 var buf_stream = &std.io.BufferOutStream.init(&buffer).stream;
7267
73 const x: i32 = 42;68 const x: i32 = 42;
lib/std/json.zig+20-22
...@@ -1495,10 +1495,7 @@ fn unescapeString(output: []u8, input: []const u8) !void {...@@ -1495,10 +1495,7 @@ fn unescapeString(output: []u8, input: []const u8) !void {
1495}1495}
14961496
1497test "json.parser.dynamic" {1497test "json.parser.dynamic" {
1498 var memory: [1024 * 16]u8 = undefined;1498 var p = Parser.init(testing.allocator, false);
1499 var buf_alloc = std.heap.FixedBufferAllocator.init(&memory);
1500
1501 var p = Parser.init(&buf_alloc.allocator, false);
1502 defer p.deinit();1499 defer p.deinit();
15031500
1504 const s =1501 const s =
...@@ -1588,10 +1585,10 @@ test "write json then parse it" {...@@ -1588,10 +1585,10 @@ test "write json then parse it" {
15881585
1589 try jw.endObject();1586 try jw.endObject();
15901587
1591 var mem_buffer: [1024 * 20]u8 = undefined;1588 var parser = Parser.init(testing.allocator, false);
1592 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;1589 defer parser.deinit();
1593 var parser = Parser.init(allocator, false);1590 var tree = try parser.parse(slice_out_stream.getWritten());
1594 const tree = try parser.parse(slice_out_stream.getWritten());1591 defer tree.deinit();
15951592
1596 testing.expect(tree.root.Object.get("f").?.value.Bool == false);1593 testing.expect(tree.root.Object.get("f").?.value.Bool == false);
1597 testing.expect(tree.root.Object.get("t").?.value.Bool == true);1594 testing.expect(tree.root.Object.get("t").?.value.Bool == true);
...@@ -1601,21 +1598,21 @@ test "write json then parse it" {...@@ -1601,21 +1598,21 @@ test "write json then parse it" {
1601 testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));1598 testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
1602}1599}
16031600
1604fn test_parse(memory: []u8, json_str: []const u8) !Value {1601fn test_parse(arena_allocator: *std.mem.Allocator, json_str: []const u8) !Value {
1605 // buf_alloc goes out of scope, but we don't use it after parsing1602 var p = Parser.init(arena_allocator, false);
1606 var buf_alloc = std.heap.FixedBufferAllocator.init(memory);
1607 var p = Parser.init(&buf_alloc.allocator, false);
1608 return (try p.parse(json_str)).root;1603 return (try p.parse(json_str)).root;
1609}1604}
16101605
1611test "parsing empty string gives appropriate error" {1606test "parsing empty string gives appropriate error" {
1612 var memory: [1024 * 4]u8 = undefined;1607 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1613 testing.expectError(error.UnexpectedEndOfJson, test_parse(&memory, ""));1608 defer arena_allocator.deinit();
1609 testing.expectError(error.UnexpectedEndOfJson, test_parse(&arena_allocator.allocator, ""));
1614}1610}
16151611
1616test "integer after float has proper type" {1612test "integer after float has proper type" {
1617 var memory: [1024 * 8]u8 = undefined;1613 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1618 const json = try test_parse(&memory,1614 defer arena_allocator.deinit();
1615 const json = try test_parse(&arena_allocator.allocator,
1619 \\{1616 \\{
1620 \\ "float": 3.14,1617 \\ "float": 3.14,
1621 \\ "ints": [1, 2, 3]1618 \\ "ints": [1, 2, 3]
...@@ -1625,7 +1622,8 @@ test "integer after float has proper type" {...@@ -1625,7 +1622,8 @@ test "integer after float has proper type" {
1625}1622}
16261623
1627test "escaped characters" {1624test "escaped characters" {
1628 var memory: [1024 * 16]u8 = undefined;1625 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1626 defer arena_allocator.deinit();
1629 const input =1627 const input =
1630 \\{1628 \\{
1631 \\ "backslash": "\\",1629 \\ "backslash": "\\",
...@@ -1641,7 +1639,7 @@ test "escaped characters" {...@@ -1641,7 +1639,7 @@ test "escaped characters" {
1641 \\}1639 \\}
1642 ;1640 ;
16431641
1644 const obj = (try test_parse(&memory, input)).Object;1642 const obj = (try test_parse(&arena_allocator.allocator, input)).Object;
16451643
1646 testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\");1644 testing.expectEqualSlices(u8, obj.get("backslash").?.value.String, "\\");
1647 testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/");1645 testing.expectEqualSlices(u8, obj.get("forwardslash").?.value.String, "/");
...@@ -1665,13 +1663,13 @@ test "string copy option" {...@@ -1665,13 +1663,13 @@ test "string copy option" {
1665 \\}1663 \\}
1666 ;1664 ;
16671665
1668 var mem_buffer: [1024 * 16]u8 = undefined;1666 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
1669 var buf_alloc = std.heap.FixedBufferAllocator.init(&mem_buffer);1667 defer arena_allocator.deinit();
16701668
1671 const tree_nocopy = try Parser.init(&buf_alloc.allocator, false).parse(input);1669 const tree_nocopy = try Parser.init(&arena_allocator.allocator, false).parse(input);
1672 const obj_nocopy = tree_nocopy.root.Object;1670 const obj_nocopy = tree_nocopy.root.Object;
16731671
1674 const tree_copy = try Parser.init(&buf_alloc.allocator, true).parse(input);1672 const tree_copy = try Parser.init(&arena_allocator.allocator, true).parse(input);
1675 const obj_copy = tree_copy.root.Object;1673 const obj_copy = tree_copy.root.Object;
16761674
1677 for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| {1675 for ([_][]const u8{ "noescape", "simple", "unicode", "surrogatepair" }) |field_name| {
lib/std/json/test.zig+14-17
...@@ -8,19 +8,18 @@ const std = @import("../std.zig");...@@ -8,19 +8,18 @@ const std = @import("../std.zig");
8fn ok(comptime s: []const u8) void {8fn ok(comptime s: []const u8) void {
9 std.testing.expect(std.json.validate(s));9 std.testing.expect(std.json.validate(s));
1010
11 var mem_buffer: [1024 * 20]u8 = undefined;11 var p = std.json.Parser.init(std.testing.allocator, false);
12 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;12 defer p.deinit();
13 var p = std.json.Parser.init(allocator, false);
1413
15 _ = p.parse(s) catch unreachable;14 var tree = p.parse(s) catch unreachable;
15 defer tree.deinit();
16}16}
1717
18fn err(comptime s: []const u8) void {18fn err(comptime s: []const u8) void {
19 std.testing.expect(!std.json.validate(s));19 std.testing.expect(!std.json.validate(s));
2020
21 var mem_buffer: [1024 * 20]u8 = undefined;21 var p = std.json.Parser.init(std.testing.allocator, false);
22 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;22 defer p.deinit();
23 var p = std.json.Parser.init(allocator, false);
2423
25 if (p.parse(s)) |_| {24 if (p.parse(s)) |_| {
26 unreachable;25 unreachable;
...@@ -30,9 +29,8 @@ fn err(comptime s: []const u8) void {...@@ -30,9 +29,8 @@ fn err(comptime s: []const u8) void {
30fn utf8Error(comptime s: []const u8) void {29fn utf8Error(comptime s: []const u8) void {
31 std.testing.expect(!std.json.validate(s));30 std.testing.expect(!std.json.validate(s));
3231
33 var mem_buffer: [1024 * 20]u8 = undefined;32 var p = std.json.Parser.init(std.testing.allocator, false);
34 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;33 defer p.deinit();
35 var p = std.json.Parser.init(allocator, false);
3634
37 if (p.parse(s)) |_| {35 if (p.parse(s)) |_| {
38 unreachable;36 unreachable;
...@@ -44,19 +42,18 @@ fn utf8Error(comptime s: []const u8) void {...@@ -44,19 +42,18 @@ fn utf8Error(comptime s: []const u8) void {
44fn any(comptime s: []const u8) void {42fn any(comptime s: []const u8) void {
45 _ = std.json.validate(s);43 _ = std.json.validate(s);
4644
47 var mem_buffer: [1024 * 20]u8 = undefined;45 var p = std.json.Parser.init(std.testing.allocator, false);
48 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;46 defer p.deinit();
49 var p = std.json.Parser.init(allocator, false);
5047
51 _ = p.parse(s) catch {};48 var tree = p.parse(s) catch return;
49 defer tree.deinit();
52}50}
5351
54fn anyStreamingErrNonStreaming(comptime s: []const u8) void {52fn anyStreamingErrNonStreaming(comptime s: []const u8) void {
55 _ = std.json.validate(s);53 _ = std.json.validate(s);
5654
57 var mem_buffer: [1024 * 20]u8 = undefined;55 var p = std.json.Parser.init(std.testing.allocator, false);
58 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buffer).allocator;56 defer p.deinit();
59 var p = std.json.Parser.init(allocator, false);
6057
61 if (p.parse(s)) |_| {58 if (p.parse(s)) |_| {
62 unreachable;59 unreachable;
lib/std/json/write_stream.zig+3-3
...@@ -254,11 +254,11 @@ test "json write stream" {...@@ -254,11 +254,11 @@ test "json write stream" {
254 var slice_stream = std.io.SliceOutStream.init(&out_buf);254 var slice_stream = std.io.SliceOutStream.init(&out_buf);
255 const out = &slice_stream.stream;255 const out = &slice_stream.stream;
256256
257 var mem_buf: [1024 * 10]u8 = undefined;257 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
258 const allocator = &std.heap.FixedBufferAllocator.init(&mem_buf).allocator;258 defer arena_allocator.deinit();
259259
260 var w = std.json.WriteStream(@TypeOf(out).Child, 10).init(out);260 var w = std.json.WriteStream(@TypeOf(out).Child, 10).init(out);
261 try w.emitJson(try getJson(allocator));261 try w.emitJson(try getJson(&arena_allocator.allocator));
262262
263 const result = slice_stream.getWritten();263 const result = slice_stream.getWritten();
264 const expected =264 const expected =
lib/std/math/big/int.zig+503-262
...@@ -137,10 +137,9 @@ pub const Int = struct {...@@ -137,10 +137,9 @@ pub const Int = struct {
137 }137 }
138138
139 /// Frees all memory associated with an Int.139 /// Frees all memory associated with an Int.
140 pub fn deinit(self: *Int) void {140 pub fn deinit(self: Int) void {
141 self.assertWritable();141 self.assertWritable();
142 self.allocator.?.free(self.limbs);142 self.allocator.?.free(self.limbs);
143 self.* = undefined;
144 }143 }
145144
146 /// Clones an Int and returns a new Int with the same value. The new Int is a deep copy and145 /// Clones an Int and returns a new Int with the same value. The new Int is a deep copy and
...@@ -1361,13 +1360,10 @@ pub const Int = struct {...@@ -1361,13 +1360,10 @@ pub const Int = struct {
1361// They will still run on larger than this and should pass, but the multi-limb code-paths1360// They will still run on larger than this and should pass, but the multi-limb code-paths
1362// may be untested in some cases.1361// may be untested in some cases.
13631362
1364var buffer: [64 * 8192]u8 = undefined;
1365var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
1366const al = &fixed.allocator;
1367
1368test "big.int comptime_int set" {1363test "big.int comptime_int set" {
1369 comptime var s = 0xefffffff00000001eeeeeeefaaaaaaab;1364 comptime var s = 0xefffffff00000001eeeeeeefaaaaaaab;
1370 var a = try Int.initSet(al, s);1365 var a = try Int.initSet(testing.allocator, s);
1366 defer a.deinit();
13711367
1372 const s_limb_count = 128 / Limb.bit_count;1368 const s_limb_count = 128 / Limb.bit_count;
13731369
...@@ -1381,39 +1377,45 @@ test "big.int comptime_int set" {...@@ -1381,39 +1377,45 @@ test "big.int comptime_int set" {
1381}1377}
13821378
1383test "big.int comptime_int set negative" {1379test "big.int comptime_int set negative" {
1384 var a = try Int.initSet(al, -10);1380 var a = try Int.initSet(testing.allocator, -10);
1381 defer a.deinit();
13851382
1386 testing.expect(a.limbs[0] == 10);1383 testing.expect(a.limbs[0] == 10);
1387 testing.expect(a.isPositive() == false);1384 testing.expect(a.isPositive() == false);
1388}1385}
13891386
1390test "big.int int set unaligned small" {1387test "big.int int set unaligned small" {
1391 var a = try Int.initSet(al, @as(u7, 45));1388 var a = try Int.initSet(testing.allocator, @as(u7, 45));
1389 defer a.deinit();
13921390
1393 testing.expect(a.limbs[0] == 45);1391 testing.expect(a.limbs[0] == 45);
1394 testing.expect(a.isPositive() == true);1392 testing.expect(a.isPositive() == true);
1395}1393}
13961394
1397test "big.int comptime_int to" {1395test "big.int comptime_int to" {
1398 const a = try Int.initSet(al, 0xefffffff00000001eeeeeeefaaaaaaab);1396 const a = try Int.initSet(testing.allocator, 0xefffffff00000001eeeeeeefaaaaaaab);
1397 defer a.deinit();
13991398
1400 testing.expect((try a.to(u128)) == 0xefffffff00000001eeeeeeefaaaaaaab);1399 testing.expect((try a.to(u128)) == 0xefffffff00000001eeeeeeefaaaaaaab);
1401}1400}
14021401
1403test "big.int sub-limb to" {1402test "big.int sub-limb to" {
1404 const a = try Int.initSet(al, 10);1403 const a = try Int.initSet(testing.allocator, 10);
1404 defer a.deinit();
14051405
1406 testing.expect((try a.to(u8)) == 10);1406 testing.expect((try a.to(u8)) == 10);
1407}1407}
14081408
1409test "big.int to target too small error" {1409test "big.int to target too small error" {
1410 const a = try Int.initSet(al, 0xffffffff);1410 const a = try Int.initSet(testing.allocator, 0xffffffff);
1411 defer a.deinit();
14111412
1412 testing.expectError(error.TargetTooSmall, a.to(u8));1413 testing.expectError(error.TargetTooSmall, a.to(u8));
1413}1414}
14141415
1415test "big.int normalize" {1416test "big.int normalize" {
1416 var a = try Int.init(al);1417 var a = try Int.init(testing.allocator);
1418 defer a.deinit();
1417 try a.ensureCapacity(8);1419 try a.ensureCapacity(8);
14181420
1419 a.limbs[0] = 1;1421 a.limbs[0] = 1;
...@@ -1440,7 +1442,8 @@ test "big.int normalize" {...@@ -1440,7 +1442,8 @@ test "big.int normalize" {
1440}1442}
14411443
1442test "big.int normalize multi" {1444test "big.int normalize multi" {
1443 var a = try Int.init(al);1445 var a = try Int.init(testing.allocator);
1446 defer a.deinit();
1444 try a.ensureCapacity(8);1447 try a.ensureCapacity(8);
14451448
1446 a.limbs[0] = 1;1449 a.limbs[0] = 1;
...@@ -1469,7 +1472,9 @@ test "big.int normalize multi" {...@@ -1469,7 +1472,9 @@ test "big.int normalize multi" {
1469}1472}
14701473
1471test "big.int parity" {1474test "big.int parity" {
1472 var a = try Int.init(al);1475 var a = try Int.init(testing.allocator);
1476 defer a.deinit();
1477
1473 try a.set(0);1478 try a.set(0);
1474 testing.expect(a.isEven());1479 testing.expect(a.isEven());
1475 testing.expect(!a.isOdd());1480 testing.expect(!a.isOdd());
...@@ -1480,7 +1485,8 @@ test "big.int parity" {...@@ -1480,7 +1485,8 @@ test "big.int parity" {
1480}1485}
14811486
1482test "big.int bitcount + sizeInBase" {1487test "big.int bitcount + sizeInBase" {
1483 var a = try Int.init(al);1488 var a = try Int.init(testing.allocator);
1489 defer a.deinit();
14841490
1485 try a.set(0b100);1491 try a.set(0b100);
1486 testing.expect(a.bitCountAbs() == 3);1492 testing.expect(a.bitCountAbs() == 3);
...@@ -1507,7 +1513,8 @@ test "big.int bitcount + sizeInBase" {...@@ -1507,7 +1513,8 @@ test "big.int bitcount + sizeInBase" {
1507}1513}
15081514
1509test "big.int bitcount/to" {1515test "big.int bitcount/to" {
1510 var a = try Int.init(al);1516 var a = try Int.init(testing.allocator);
1517 defer a.deinit();
15111518
1512 try a.set(0);1519 try a.set(0);
1513 testing.expect(a.bitCountTwosComp() == 0);1520 testing.expect(a.bitCountTwosComp() == 0);
...@@ -1537,7 +1544,8 @@ test "big.int bitcount/to" {...@@ -1537,7 +1544,8 @@ test "big.int bitcount/to" {
1537}1544}
15381545
1539test "big.int fits" {1546test "big.int fits" {
1540 var a = try Int.init(al);1547 var a = try Int.init(testing.allocator);
1548 defer a.deinit();
15411549
1542 try a.set(0);1550 try a.set(0);
1543 testing.expect(a.fits(u0));1551 testing.expect(a.fits(u0));
...@@ -1564,82 +1572,100 @@ test "big.int fits" {...@@ -1564,82 +1572,100 @@ test "big.int fits" {
1564}1572}
15651573
1566test "big.int string set" {1574test "big.int string set" {
1567 var a = try Int.init(al);1575 var a = try Int.init(testing.allocator);
1568 try a.setString(10, "120317241209124781241290847124");1576 defer a.deinit();
15691577
1578 try a.setString(10, "120317241209124781241290847124");
1570 testing.expect((try a.to(u128)) == 120317241209124781241290847124);1579 testing.expect((try a.to(u128)) == 120317241209124781241290847124);
1571}1580}
15721581
1573test "big.int string negative" {1582test "big.int string negative" {
1574 var a = try Int.init(al);1583 var a = try Int.init(testing.allocator);
1584 defer a.deinit();
1585
1575 try a.setString(10, "-1023");1586 try a.setString(10, "-1023");
1576 testing.expect((try a.to(i32)) == -1023);1587 testing.expect((try a.to(i32)) == -1023);
1577}1588}
15781589
1579test "big.int string set bad char error" {1590test "big.int string set bad char error" {
1580 var a = try Int.init(al);1591 var a = try Int.init(testing.allocator);
1592 defer a.deinit();
1581 testing.expectError(error.InvalidCharForDigit, a.setString(10, "x"));1593 testing.expectError(error.InvalidCharForDigit, a.setString(10, "x"));
1582}1594}
15831595
1584test "big.int string set bad base error" {1596test "big.int string set bad base error" {
1585 var a = try Int.init(al);1597 var a = try Int.init(testing.allocator);
1598 defer a.deinit();
1586 testing.expectError(error.InvalidBase, a.setString(45, "10"));1599 testing.expectError(error.InvalidBase, a.setString(45, "10"));
1587}1600}
15881601
1589test "big.int string to" {1602test "big.int string to" {
1590 const a = try Int.initSet(al, 120317241209124781241290847124);1603 const a = try Int.initSet(testing.allocator, 120317241209124781241290847124);
1604 defer a.deinit();
15911605
1592 const as = try a.toString(al, 10);1606 const as = try a.toString(testing.allocator, 10);
1607 defer testing.allocator.free(as);
1593 const es = "120317241209124781241290847124";1608 const es = "120317241209124781241290847124";
15941609
1595 testing.expect(mem.eql(u8, as, es));1610 testing.expect(mem.eql(u8, as, es));
1596}1611}
15971612
1598test "big.int string to base base error" {1613test "big.int string to base base error" {
1599 const a = try Int.initSet(al, 0xffffffff);1614 const a = try Int.initSet(testing.allocator, 0xffffffff);
1615 defer a.deinit();
16001616
1601 testing.expectError(error.InvalidBase, a.toString(al, 45));1617 testing.expectError(error.InvalidBase, a.toString(testing.allocator, 45));
1602}1618}
16031619
1604test "big.int string to base 2" {1620test "big.int string to base 2" {
1605 const a = try Int.initSet(al, -0b1011);1621 const a = try Int.initSet(testing.allocator, -0b1011);
1622 defer a.deinit();
16061623
1607 const as = try a.toString(al, 2);1624 const as = try a.toString(testing.allocator, 2);
1625 defer testing.allocator.free(as);
1608 const es = "-1011";1626 const es = "-1011";
16091627
1610 testing.expect(mem.eql(u8, as, es));1628 testing.expect(mem.eql(u8, as, es));
1611}1629}
16121630
1613test "big.int string to base 16" {1631test "big.int string to base 16" {
1614 const a = try Int.initSet(al, 0xefffffff00000001eeeeeeefaaaaaaab);1632 const a = try Int.initSet(testing.allocator, 0xefffffff00000001eeeeeeefaaaaaaab);
1633 defer a.deinit();
16151634
1616 const as = try a.toString(al, 16);1635 const as = try a.toString(testing.allocator, 16);
1636 defer testing.allocator.free(as);
1617 const es = "efffffff00000001eeeeeeefaaaaaaab";1637 const es = "efffffff00000001eeeeeeefaaaaaaab";
16181638
1619 testing.expect(mem.eql(u8, as, es));1639 testing.expect(mem.eql(u8, as, es));
1620}1640}
16211641
1622test "big.int neg string to" {1642test "big.int neg string to" {
1623 const a = try Int.initSet(al, -123907434);1643 const a = try Int.initSet(testing.allocator, -123907434);
1644 defer a.deinit();
16241645
1625 const as = try a.toString(al, 10);1646 const as = try a.toString(testing.allocator, 10);
1647 defer testing.allocator.free(as);
1626 const es = "-123907434";1648 const es = "-123907434";
16271649
1628 testing.expect(mem.eql(u8, as, es));1650 testing.expect(mem.eql(u8, as, es));
1629}1651}
16301652
1631test "big.int zero string to" {1653test "big.int zero string to" {
1632 const a = try Int.initSet(al, 0);1654 const a = try Int.initSet(testing.allocator, 0);
1655 defer a.deinit();
16331656
1634 const as = try a.toString(al, 10);1657 const as = try a.toString(testing.allocator, 10);
1658 defer testing.allocator.free(as);
1635 const es = "0";1659 const es = "0";
16361660
1637 testing.expect(mem.eql(u8, as, es));1661 testing.expect(mem.eql(u8, as, es));
1638}1662}
16391663
1640test "big.int clone" {1664test "big.int clone" {
1641 var a = try Int.initSet(al, 1234);1665 var a = try Int.initSet(testing.allocator, 1234);
1666 defer a.deinit();
1642 const b = try a.clone();1667 const b = try a.clone();
1668 defer b.deinit();
16431669
1644 testing.expect((try a.to(u32)) == 1234);1670 testing.expect((try a.to(u32)) == 1234);
1645 testing.expect((try b.to(u32)) == 1234);1671 testing.expect((try b.to(u32)) == 1234);
...@@ -1650,8 +1676,10 @@ test "big.int clone" {...@@ -1650,8 +1676,10 @@ test "big.int clone" {
1650}1676}
16511677
1652test "big.int swap" {1678test "big.int swap" {
1653 var a = try Int.initSet(al, 1234);1679 var a = try Int.initSet(testing.allocator, 1234);
1654 var b = try Int.initSet(al, 5678);1680 defer a.deinit();
1681 var b = try Int.initSet(testing.allocator, 5678);
1682 defer b.deinit();
16551683
1656 testing.expect((try a.to(u32)) == 1234);1684 testing.expect((try a.to(u32)) == 1234);
1657 testing.expect((try b.to(u32)) == 5678);1685 testing.expect((try b.to(u32)) == 5678);
...@@ -1663,53 +1691,65 @@ test "big.int swap" {...@@ -1663,53 +1691,65 @@ test "big.int swap" {
1663}1691}
16641692
1665test "big.int to negative" {1693test "big.int to negative" {
1666 var a = try Int.initSet(al, -10);1694 var a = try Int.initSet(testing.allocator, -10);
1695 defer a.deinit();
16671696
1668 testing.expect((try a.to(i32)) == -10);1697 testing.expect((try a.to(i32)) == -10);
1669}1698}
16701699
1671test "big.int compare" {1700test "big.int compare" {
1672 var a = try Int.initSet(al, -11);1701 var a = try Int.initSet(testing.allocator, -11);
1673 var b = try Int.initSet(al, 10);1702 defer a.deinit();
1703 var b = try Int.initSet(testing.allocator, 10);
1704 defer b.deinit();
16741705
1675 testing.expect(a.cmpAbs(b) == 1);1706 testing.expect(a.cmpAbs(b) == 1);
1676 testing.expect(a.cmp(b) == -1);1707 testing.expect(a.cmp(b) == -1);
1677}1708}
16781709
1679test "big.int compare similar" {1710test "big.int compare similar" {
1680 var a = try Int.initSet(al, 0xffffffffeeeeeeeeffffffffeeeeeeee);1711 var a = try Int.initSet(testing.allocator, 0xffffffffeeeeeeeeffffffffeeeeeeee);
1681 var b = try Int.initSet(al, 0xffffffffeeeeeeeeffffffffeeeeeeef);1712 defer a.deinit();
1713 var b = try Int.initSet(testing.allocator, 0xffffffffeeeeeeeeffffffffeeeeeeef);
1714 defer b.deinit();
16821715
1683 testing.expect(a.cmpAbs(b) == -1);1716 testing.expect(a.cmpAbs(b) == -1);
1684 testing.expect(b.cmpAbs(a) == 1);1717 testing.expect(b.cmpAbs(a) == 1);
1685}1718}
16861719
1687test "big.int compare different limb size" {1720test "big.int compare different limb size" {
1688 var a = try Int.initSet(al, maxInt(Limb) + 1);1721 var a = try Int.initSet(testing.allocator, maxInt(Limb) + 1);
1689 var b = try Int.initSet(al, 1);1722 defer a.deinit();
1723 var b = try Int.initSet(testing.allocator, 1);
1724 defer b.deinit();
16901725
1691 testing.expect(a.cmpAbs(b) == 1);1726 testing.expect(a.cmpAbs(b) == 1);
1692 testing.expect(b.cmpAbs(a) == -1);1727 testing.expect(b.cmpAbs(a) == -1);
1693}1728}
16941729
1695test "big.int compare multi-limb" {1730test "big.int compare multi-limb" {
1696 var a = try Int.initSet(al, -0x7777777799999999ffffeeeeffffeeeeffffeeeef);1731 var a = try Int.initSet(testing.allocator, -0x7777777799999999ffffeeeeffffeeeeffffeeeef);
1697 var b = try Int.initSet(al, 0x7777777799999999ffffeeeeffffeeeeffffeeeee);1732 defer a.deinit();
1733 var b = try Int.initSet(testing.allocator, 0x7777777799999999ffffeeeeffffeeeeffffeeeee);
1734 defer b.deinit();
16981735
1699 testing.expect(a.cmpAbs(b) == 1);1736 testing.expect(a.cmpAbs(b) == 1);
1700 testing.expect(a.cmp(b) == -1);1737 testing.expect(a.cmp(b) == -1);
1701}1738}
17021739
1703test "big.int equality" {1740test "big.int equality" {
1704 var a = try Int.initSet(al, 0xffffffff1);1741 var a = try Int.initSet(testing.allocator, 0xffffffff1);
1705 var b = try Int.initSet(al, -0xffffffff1);1742 defer a.deinit();
1743 var b = try Int.initSet(testing.allocator, -0xffffffff1);
1744 defer b.deinit();
17061745
1707 testing.expect(a.eqAbs(b));1746 testing.expect(a.eqAbs(b));
1708 testing.expect(!a.eq(b));1747 testing.expect(!a.eq(b));
1709}1748}
17101749
1711test "big.int abs" {1750test "big.int abs" {
1712 var a = try Int.initSet(al, -5);1751 var a = try Int.initSet(testing.allocator, -5);
1752 defer a.deinit();
17131753
1714 a.abs();1754 a.abs();
1715 testing.expect((try a.to(u32)) == 5);1755 testing.expect((try a.to(u32)) == 5);
...@@ -1719,7 +1759,8 @@ test "big.int abs" {...@@ -1719,7 +1759,8 @@ test "big.int abs" {
1719}1759}
17201760
1721test "big.int negate" {1761test "big.int negate" {
1722 var a = try Int.initSet(al, 5);1762 var a = try Int.initSet(testing.allocator, 5);
1763 defer a.deinit();
17231764
1724 a.negate();1765 a.negate();
1725 testing.expect((try a.to(i32)) == -5);1766 testing.expect((try a.to(i32)) == -5);
...@@ -1729,20 +1770,26 @@ test "big.int negate" {...@@ -1729,20 +1770,26 @@ test "big.int negate" {
1729}1770}
17301771
1731test "big.int add single-single" {1772test "big.int add single-single" {
1732 var a = try Int.initSet(al, 50);1773 var a = try Int.initSet(testing.allocator, 50);
1733 var b = try Int.initSet(al, 5);1774 defer a.deinit();
1775 var b = try Int.initSet(testing.allocator, 5);
1776 defer b.deinit();
17341777
1735 var c = try Int.init(al);1778 var c = try Int.init(testing.allocator);
1779 defer c.deinit();
1736 try c.add(a, b);1780 try c.add(a, b);
17371781
1738 testing.expect((try c.to(u32)) == 55);1782 testing.expect((try c.to(u32)) == 55);
1739}1783}
17401784
1741test "big.int add multi-single" {1785test "big.int add multi-single" {
1742 var a = try Int.initSet(al, maxInt(Limb) + 1);1786 var a = try Int.initSet(testing.allocator, maxInt(Limb) + 1);
1743 var b = try Int.initSet(al, 1);1787 defer a.deinit();
1788 var b = try Int.initSet(testing.allocator, 1);
1789 defer b.deinit();
17441790
1745 var c = try Int.init(al);1791 var c = try Int.init(testing.allocator);
1792 defer c.deinit();
17461793
1747 try c.add(a, b);1794 try c.add(a, b);
1748 testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);1795 testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
...@@ -1754,20 +1801,26 @@ test "big.int add multi-single" {...@@ -1754,20 +1801,26 @@ test "big.int add multi-single" {
1754test "big.int add multi-multi" {1801test "big.int add multi-multi" {
1755 const op1 = 0xefefefef7f7f7f7f;1802 const op1 = 0xefefefef7f7f7f7f;
1756 const op2 = 0xfefefefe9f9f9f9f;1803 const op2 = 0xfefefefe9f9f9f9f;
1757 var a = try Int.initSet(al, op1);1804 var a = try Int.initSet(testing.allocator, op1);
1758 var b = try Int.initSet(al, op2);1805 defer a.deinit();
1806 var b = try Int.initSet(testing.allocator, op2);
1807 defer b.deinit();
17591808
1760 var c = try Int.init(al);1809 var c = try Int.init(testing.allocator);
1810 defer c.deinit();
1761 try c.add(a, b);1811 try c.add(a, b);
17621812
1763 testing.expect((try c.to(u128)) == op1 + op2);1813 testing.expect((try c.to(u128)) == op1 + op2);
1764}1814}
17651815
1766test "big.int add zero-zero" {1816test "big.int add zero-zero" {
1767 var a = try Int.initSet(al, 0);1817 var a = try Int.initSet(testing.allocator, 0);
1768 var b = try Int.initSet(al, 0);1818 defer a.deinit();
1819 var b = try Int.initSet(testing.allocator, 0);
1820 defer b.deinit();
17691821
1770 var c = try Int.init(al);1822 var c = try Int.init(testing.allocator);
1823 defer c.deinit();
1771 try c.add(a, b);1824 try c.add(a, b);
17721825
1773 testing.expect((try c.to(u32)) == 0);1826 testing.expect((try c.to(u32)) == 0);
...@@ -1775,8 +1828,10 @@ test "big.int add zero-zero" {...@@ -1775,8 +1828,10 @@ test "big.int add zero-zero" {
17751828
1776test "big.int add alias multi-limb nonzero-zero" {1829test "big.int add alias multi-limb nonzero-zero" {
1777 const op1 = 0xffffffff777777771;1830 const op1 = 0xffffffff777777771;
1778 var a = try Int.initSet(al, op1);1831 var a = try Int.initSet(testing.allocator, op1);
1779 var b = try Int.initSet(al, 0);1832 defer a.deinit();
1833 var b = try Int.initSet(testing.allocator, 0);
1834 defer b.deinit();
17801835
1781 try a.add(a, b);1836 try a.add(a, b);
17821837
...@@ -1784,12 +1839,17 @@ test "big.int add alias multi-limb nonzero-zero" {...@@ -1784,12 +1839,17 @@ test "big.int add alias multi-limb nonzero-zero" {
1784}1839}
17851840
1786test "big.int add sign" {1841test "big.int add sign" {
1787 var a = try Int.init(al);1842 var a = try Int.init(testing.allocator);
17881843 defer a.deinit();
1789 const one = try Int.initSet(al, 1);1844
1790 const two = try Int.initSet(al, 2);1845 const one = try Int.initSet(testing.allocator, 1);
1791 const neg_one = try Int.initSet(al, -1);1846 defer one.deinit();
1792 const neg_two = try Int.initSet(al, -2);1847 const two = try Int.initSet(testing.allocator, 2);
1848 defer two.deinit();
1849 const neg_one = try Int.initSet(testing.allocator, -1);
1850 defer neg_one.deinit();
1851 const neg_two = try Int.initSet(testing.allocator, -2);
1852 defer neg_two.deinit();
17931853
1794 try a.add(one, two);1854 try a.add(one, two);
1795 testing.expect((try a.to(i32)) == 3);1855 testing.expect((try a.to(i32)) == 3);
...@@ -1805,20 +1865,26 @@ test "big.int add sign" {...@@ -1805,20 +1865,26 @@ test "big.int add sign" {
1805}1865}
18061866
1807test "big.int sub single-single" {1867test "big.int sub single-single" {
1808 var a = try Int.initSet(al, 50);1868 var a = try Int.initSet(testing.allocator, 50);
1809 var b = try Int.initSet(al, 5);1869 defer a.deinit();
1870 var b = try Int.initSet(testing.allocator, 5);
1871 defer b.deinit();
18101872
1811 var c = try Int.init(al);1873 var c = try Int.init(testing.allocator);
1874 defer c.deinit();
1812 try c.sub(a, b);1875 try c.sub(a, b);
18131876
1814 testing.expect((try c.to(u32)) == 45);1877 testing.expect((try c.to(u32)) == 45);
1815}1878}
18161879
1817test "big.int sub multi-single" {1880test "big.int sub multi-single" {
1818 var a = try Int.initSet(al, maxInt(Limb) + 1);1881 var a = try Int.initSet(testing.allocator, maxInt(Limb) + 1);
1819 var b = try Int.initSet(al, 1);1882 defer a.deinit();
1883 var b = try Int.initSet(testing.allocator, 1);
1884 defer b.deinit();
18201885
1821 var c = try Int.init(al);1886 var c = try Int.init(testing.allocator);
1887 defer c.deinit();
1822 try c.sub(a, b);1888 try c.sub(a, b);
18231889
1824 testing.expect((try c.to(Limb)) == maxInt(Limb));1890 testing.expect((try c.to(Limb)) == maxInt(Limb));
...@@ -1828,32 +1894,43 @@ test "big.int sub multi-multi" {...@@ -1828,32 +1894,43 @@ test "big.int sub multi-multi" {
1828 const op1 = 0xefefefefefefefefefefefef;1894 const op1 = 0xefefefefefefefefefefefef;
1829 const op2 = 0xabababababababababababab;1895 const op2 = 0xabababababababababababab;
18301896
1831 var a = try Int.initSet(al, op1);1897 var a = try Int.initSet(testing.allocator, op1);
1832 var b = try Int.initSet(al, op2);1898 defer a.deinit();
1899 var b = try Int.initSet(testing.allocator, op2);
1900 defer b.deinit();
18331901
1834 var c = try Int.init(al);1902 var c = try Int.init(testing.allocator);
1903 defer c.deinit();
1835 try c.sub(a, b);1904 try c.sub(a, b);
18361905
1837 testing.expect((try c.to(u128)) == op1 - op2);1906 testing.expect((try c.to(u128)) == op1 - op2);
1838}1907}
18391908
1840test "big.int sub equal" {1909test "big.int sub equal" {
1841 var a = try Int.initSet(al, 0x11efefefefefefefefefefefef);1910 var a = try Int.initSet(testing.allocator, 0x11efefefefefefefefefefefef);
1842 var b = try Int.initSet(al, 0x11efefefefefefefefefefefef);1911 defer a.deinit();
1912 var b = try Int.initSet(testing.allocator, 0x11efefefefefefefefefefefef);
1913 defer b.deinit();
18431914
1844 var c = try Int.init(al);1915 var c = try Int.init(testing.allocator);
1916 defer c.deinit();
1845 try c.sub(a, b);1917 try c.sub(a, b);
18461918
1847 testing.expect((try c.to(u32)) == 0);1919 testing.expect((try c.to(u32)) == 0);
1848}1920}
18491921
1850test "big.int sub sign" {1922test "big.int sub sign" {
1851 var a = try Int.init(al);1923 var a = try Int.init(testing.allocator);
18521924 defer a.deinit();
1853 const one = try Int.initSet(al, 1);1925
1854 const two = try Int.initSet(al, 2);1926 const one = try Int.initSet(testing.allocator, 1);
1855 const neg_one = try Int.initSet(al, -1);1927 defer one.deinit();
1856 const neg_two = try Int.initSet(al, -2);1928 const two = try Int.initSet(testing.allocator, 2);
1929 defer two.deinit();
1930 const neg_one = try Int.initSet(testing.allocator, -1);
1931 defer neg_one.deinit();
1932 const neg_two = try Int.initSet(testing.allocator, -2);
1933 defer neg_two.deinit();
18571934
1858 try a.sub(one, two);1935 try a.sub(one, two);
1859 testing.expect((try a.to(i32)) == -1);1936 testing.expect((try a.to(i32)) == -1);
...@@ -1872,20 +1949,26 @@ test "big.int sub sign" {...@@ -1872,20 +1949,26 @@ test "big.int sub sign" {
1872}1949}
18731950
1874test "big.int mul single-single" {1951test "big.int mul single-single" {
1875 var a = try Int.initSet(al, 50);1952 var a = try Int.initSet(testing.allocator, 50);
1876 var b = try Int.initSet(al, 5);1953 defer a.deinit();
1954 var b = try Int.initSet(testing.allocator, 5);
1955 defer b.deinit();
18771956
1878 var c = try Int.init(al);1957 var c = try Int.init(testing.allocator);
1958 defer c.deinit();
1879 try c.mul(a, b);1959 try c.mul(a, b);
18801960
1881 testing.expect((try c.to(u64)) == 250);1961 testing.expect((try c.to(u64)) == 250);
1882}1962}
18831963
1884test "big.int mul multi-single" {1964test "big.int mul multi-single" {
1885 var a = try Int.initSet(al, maxInt(Limb));1965 var a = try Int.initSet(testing.allocator, maxInt(Limb));
1886 var b = try Int.initSet(al, 2);1966 defer a.deinit();
1967 var b = try Int.initSet(testing.allocator, 2);
1968 defer b.deinit();
18871969
1888 var c = try Int.init(al);1970 var c = try Int.init(testing.allocator);
1971 defer c.deinit();
1889 try c.mul(a, b);1972 try c.mul(a, b);
18901973
1891 testing.expect((try c.to(DoubleLimb)) == 2 * maxInt(Limb));1974 testing.expect((try c.to(DoubleLimb)) == 2 * maxInt(Limb));
...@@ -1894,18 +1977,23 @@ test "big.int mul multi-single" {...@@ -1894,18 +1977,23 @@ test "big.int mul multi-single" {
1894test "big.int mul multi-multi" {1977test "big.int mul multi-multi" {
1895 const op1 = 0x998888efefefefefefefef;1978 const op1 = 0x998888efefefefefefefef;
1896 const op2 = 0x333000abababababababab;1979 const op2 = 0x333000abababababababab;
1897 var a = try Int.initSet(al, op1);1980 var a = try Int.initSet(testing.allocator, op1);
1898 var b = try Int.initSet(al, op2);1981 defer a.deinit();
1982 var b = try Int.initSet(testing.allocator, op2);
1983 defer b.deinit();
18991984
1900 var c = try Int.init(al);1985 var c = try Int.init(testing.allocator);
1986 defer c.deinit();
1901 try c.mul(a, b);1987 try c.mul(a, b);
19021988
1903 testing.expect((try c.to(u256)) == op1 * op2);1989 testing.expect((try c.to(u256)) == op1 * op2);
1904}1990}
19051991
1906test "big.int mul alias r with a" {1992test "big.int mul alias r with a" {
1907 var a = try Int.initSet(al, maxInt(Limb));1993 var a = try Int.initSet(testing.allocator, maxInt(Limb));
1908 var b = try Int.initSet(al, 2);1994 defer a.deinit();
1995 var b = try Int.initSet(testing.allocator, 2);
1996 defer b.deinit();
19091997
1910 try a.mul(a, b);1998 try a.mul(a, b);
19111999
...@@ -1913,8 +2001,10 @@ test "big.int mul alias r with a" {...@@ -1913,8 +2001,10 @@ test "big.int mul alias r with a" {
1913}2001}
19142002
1915test "big.int mul alias r with b" {2003test "big.int mul alias r with b" {
1916 var a = try Int.initSet(al, maxInt(Limb));2004 var a = try Int.initSet(testing.allocator, maxInt(Limb));
1917 var b = try Int.initSet(al, 2);2005 defer a.deinit();
2006 var b = try Int.initSet(testing.allocator, 2);
2007 defer b.deinit();
19182008
1919 try a.mul(b, a);2009 try a.mul(b, a);
19202010
...@@ -1922,7 +2012,8 @@ test "big.int mul alias r with b" {...@@ -1922,7 +2012,8 @@ test "big.int mul alias r with b" {
1922}2012}
19232013
1924test "big.int mul alias r with a and b" {2014test "big.int mul alias r with a and b" {
1925 var a = try Int.initSet(al, maxInt(Limb));2015 var a = try Int.initSet(testing.allocator, maxInt(Limb));
2016 defer a.deinit();
19262017
1927 try a.mul(a, a);2018 try a.mul(a, a);
19282019
...@@ -1930,31 +2021,41 @@ test "big.int mul alias r with a and b" {...@@ -1930,31 +2021,41 @@ test "big.int mul alias r with a and b" {
1930}2021}
19312022
1932test "big.int mul a*0" {2023test "big.int mul a*0" {
1933 var a = try Int.initSet(al, 0xefefefefefefefef);2024 var a = try Int.initSet(testing.allocator, 0xefefefefefefefef);
1934 var b = try Int.initSet(al, 0);2025 defer a.deinit();
2026 var b = try Int.initSet(testing.allocator, 0);
2027 defer b.deinit();
19352028
1936 var c = try Int.init(al);2029 var c = try Int.init(testing.allocator);
2030 defer c.deinit();
1937 try c.mul(a, b);2031 try c.mul(a, b);
19382032
1939 testing.expect((try c.to(u32)) == 0);2033 testing.expect((try c.to(u32)) == 0);
1940}2034}
19412035
1942test "big.int mul 0*0" {2036test "big.int mul 0*0" {
1943 var a = try Int.initSet(al, 0);2037 var a = try Int.initSet(testing.allocator, 0);
1944 var b = try Int.initSet(al, 0);2038 defer a.deinit();
2039 var b = try Int.initSet(testing.allocator, 0);
2040 defer b.deinit();
19452041
1946 var c = try Int.init(al);2042 var c = try Int.init(testing.allocator);
2043 defer c.deinit();
1947 try c.mul(a, b);2044 try c.mul(a, b);
19482045
1949 testing.expect((try c.to(u32)) == 0);2046 testing.expect((try c.to(u32)) == 0);
1950}2047}
19512048
1952test "big.int div single-single no rem" {2049test "big.int div single-single no rem" {
1953 var a = try Int.initSet(al, 50);2050 var a = try Int.initSet(testing.allocator, 50);
1954 var b = try Int.initSet(al, 5);2051 defer a.deinit();
19552052 var b = try Int.initSet(testing.allocator, 5);
1956 var q = try Int.init(al);2053 defer b.deinit();
1957 var r = try Int.init(al);2054
2055 var q = try Int.init(testing.allocator);
2056 defer q.deinit();
2057 var r = try Int.init(testing.allocator);
2058 defer r.deinit();
1958 try Int.divTrunc(&q, &r, a, b);2059 try Int.divTrunc(&q, &r, a, b);
19592060
1960 testing.expect((try q.to(u32)) == 10);2061 testing.expect((try q.to(u32)) == 10);
...@@ -1962,11 +2063,15 @@ test "big.int div single-single no rem" {...@@ -1962,11 +2063,15 @@ test "big.int div single-single no rem" {
1962}2063}
19632064
1964test "big.int div single-single with rem" {2065test "big.int div single-single with rem" {
1965 var a = try Int.initSet(al, 49);2066 var a = try Int.initSet(testing.allocator, 49);
1966 var b = try Int.initSet(al, 5);2067 defer a.deinit();
19672068 var b = try Int.initSet(testing.allocator, 5);
1968 var q = try Int.init(al);2069 defer b.deinit();
1969 var r = try Int.init(al);2070
2071 var q = try Int.init(testing.allocator);
2072 defer q.deinit();
2073 var r = try Int.init(testing.allocator);
2074 defer r.deinit();
1970 try Int.divTrunc(&q, &r, a, b);2075 try Int.divTrunc(&q, &r, a, b);
19712076
1972 testing.expect((try q.to(u32)) == 9);2077 testing.expect((try q.to(u32)) == 9);
...@@ -1977,11 +2082,15 @@ test "big.int div multi-single no rem" {...@@ -1977,11 +2082,15 @@ test "big.int div multi-single no rem" {
1977 const op1 = 0xffffeeeeddddcccc;2082 const op1 = 0xffffeeeeddddcccc;
1978 const op2 = 34;2083 const op2 = 34;
19792084
1980 var a = try Int.initSet(al, op1);2085 var a = try Int.initSet(testing.allocator, op1);
1981 var b = try Int.initSet(al, op2);2086 defer a.deinit();
2087 var b = try Int.initSet(testing.allocator, op2);
2088 defer b.deinit();
19822089
1983 var q = try Int.init(al);2090 var q = try Int.init(testing.allocator);
1984 var r = try Int.init(al);2091 defer q.deinit();
2092 var r = try Int.init(testing.allocator);
2093 defer r.deinit();
1985 try Int.divTrunc(&q, &r, a, b);2094 try Int.divTrunc(&q, &r, a, b);
19862095
1987 testing.expect((try q.to(u64)) == op1 / op2);2096 testing.expect((try q.to(u64)) == op1 / op2);
...@@ -1992,11 +2101,15 @@ test "big.int div multi-single with rem" {...@@ -1992,11 +2101,15 @@ test "big.int div multi-single with rem" {
1992 const op1 = 0xffffeeeeddddcccf;2101 const op1 = 0xffffeeeeddddcccf;
1993 const op2 = 34;2102 const op2 = 34;
19942103
1995 var a = try Int.initSet(al, op1);2104 var a = try Int.initSet(testing.allocator, op1);
1996 var b = try Int.initSet(al, op2);2105 defer a.deinit();
2106 var b = try Int.initSet(testing.allocator, op2);
2107 defer b.deinit();
19972108
1998 var q = try Int.init(al);2109 var q = try Int.init(testing.allocator);
1999 var r = try Int.init(al);2110 defer q.deinit();
2111 var r = try Int.init(testing.allocator);
2112 defer r.deinit();
2000 try Int.divTrunc(&q, &r, a, b);2113 try Int.divTrunc(&q, &r, a, b);
20012114
2002 testing.expect((try q.to(u64)) == op1 / op2);2115 testing.expect((try q.to(u64)) == op1 / op2);
...@@ -2007,11 +2120,15 @@ test "big.int div multi>2-single" {...@@ -2007,11 +2120,15 @@ test "big.int div multi>2-single" {
2007 const op1 = 0xfefefefefefefefefefefefefefefefe;2120 const op1 = 0xfefefefefefefefefefefefefefefefe;
2008 const op2 = 0xefab8;2121 const op2 = 0xefab8;
20092122
2010 var a = try Int.initSet(al, op1);2123 var a = try Int.initSet(testing.allocator, op1);
2011 var b = try Int.initSet(al, op2);2124 defer a.deinit();
2125 var b = try Int.initSet(testing.allocator, op2);
2126 defer b.deinit();
20122127
2013 var q = try Int.init(al);2128 var q = try Int.init(testing.allocator);
2014 var r = try Int.init(al);2129 defer q.deinit();
2130 var r = try Int.init(testing.allocator);
2131 defer r.deinit();
2015 try Int.divTrunc(&q, &r, a, b);2132 try Int.divTrunc(&q, &r, a, b);
20162133
2017 testing.expect((try q.to(u128)) == op1 / op2);2134 testing.expect((try q.to(u128)) == op1 / op2);
...@@ -2019,11 +2136,15 @@ test "big.int div multi>2-single" {...@@ -2019,11 +2136,15 @@ test "big.int div multi>2-single" {
2019}2136}
20202137
2021test "big.int div single-single q < r" {2138test "big.int div single-single q < r" {
2022 var a = try Int.initSet(al, 0x0078f432);2139 var a = try Int.initSet(testing.allocator, 0x0078f432);
2023 var b = try Int.initSet(al, 0x01000000);2140 defer a.deinit();
20242141 var b = try Int.initSet(testing.allocator, 0x01000000);
2025 var q = try Int.init(al);2142 defer b.deinit();
2026 var r = try Int.init(al);2143
2144 var q = try Int.init(testing.allocator);
2145 defer q.deinit();
2146 var r = try Int.init(testing.allocator);
2147 defer r.deinit();
2027 try Int.divTrunc(&q, &r, a, b);2148 try Int.divTrunc(&q, &r, a, b);
20282149
2029 testing.expect((try q.to(u64)) == 0);2150 testing.expect((try q.to(u64)) == 0);
...@@ -2031,11 +2152,15 @@ test "big.int div single-single q < r" {...@@ -2031,11 +2152,15 @@ test "big.int div single-single q < r" {
2031}2152}
20322153
2033test "big.int div single-single q == r" {2154test "big.int div single-single q == r" {
2034 var a = try Int.initSet(al, 10);2155 var a = try Int.initSet(testing.allocator, 10);
2035 var b = try Int.initSet(al, 10);2156 defer a.deinit();
20362157 var b = try Int.initSet(testing.allocator, 10);
2037 var q = try Int.init(al);2158 defer b.deinit();
2038 var r = try Int.init(al);2159
2160 var q = try Int.init(testing.allocator);
2161 defer q.deinit();
2162 var r = try Int.init(testing.allocator);
2163 defer r.deinit();
2039 try Int.divTrunc(&q, &r, a, b);2164 try Int.divTrunc(&q, &r, a, b);
20402165
2041 testing.expect((try q.to(u64)) == 1);2166 testing.expect((try q.to(u64)) == 1);
...@@ -2043,8 +2168,10 @@ test "big.int div single-single q == r" {...@@ -2043,8 +2168,10 @@ test "big.int div single-single q == r" {
2043}2168}
20442169
2045test "big.int div q=0 alias" {2170test "big.int div q=0 alias" {
2046 var a = try Int.initSet(al, 3);2171 var a = try Int.initSet(testing.allocator, 3);
2047 var b = try Int.initSet(al, 10);2172 defer a.deinit();
2173 var b = try Int.initSet(testing.allocator, 10);
2174 defer b.deinit();
20482175
2049 try Int.divTrunc(&a, &b, a, b);2176 try Int.divTrunc(&a, &b, a, b);
20502177
...@@ -2055,11 +2182,15 @@ test "big.int div q=0 alias" {...@@ -2055,11 +2182,15 @@ test "big.int div q=0 alias" {
2055test "big.int div multi-multi q < r" {2182test "big.int div multi-multi q < r" {
2056 const op1 = 0x1ffffffff0078f432;2183 const op1 = 0x1ffffffff0078f432;
2057 const op2 = 0x1ffffffff01000000;2184 const op2 = 0x1ffffffff01000000;
2058 var a = try Int.initSet(al, op1);2185 var a = try Int.initSet(testing.allocator, op1);
2059 var b = try Int.initSet(al, op2);2186 defer a.deinit();
20602187 var b = try Int.initSet(testing.allocator, op2);
2061 var q = try Int.init(al);2188 defer b.deinit();
2062 var r = try Int.init(al);2189
2190 var q = try Int.init(testing.allocator);
2191 defer q.deinit();
2192 var r = try Int.init(testing.allocator);
2193 defer r.deinit();
2063 try Int.divTrunc(&q, &r, a, b);2194 try Int.divTrunc(&q, &r, a, b);
20642195
2065 testing.expect((try q.to(u128)) == 0);2196 testing.expect((try q.to(u128)) == 0);
...@@ -2070,11 +2201,15 @@ test "big.int div trunc single-single +/+" {...@@ -2070,11 +2201,15 @@ test "big.int div trunc single-single +/+" {
2070 const u: i32 = 5;2201 const u: i32 = 5;
2071 const v: i32 = 3;2202 const v: i32 = 3;
20722203
2073 var a = try Int.initSet(al, u);2204 var a = try Int.initSet(testing.allocator, u);
2074 var b = try Int.initSet(al, v);2205 defer a.deinit();
2206 var b = try Int.initSet(testing.allocator, v);
2207 defer b.deinit();
20752208
2076 var q = try Int.init(al);2209 var q = try Int.init(testing.allocator);
2077 var r = try Int.init(al);2210 defer q.deinit();
2211 var r = try Int.init(testing.allocator);
2212 defer r.deinit();
2078 try Int.divTrunc(&q, &r, a, b);2213 try Int.divTrunc(&q, &r, a, b);
20792214
2080 // n = q * d + r2215 // n = q * d + r
...@@ -2090,11 +2225,15 @@ test "big.int div trunc single-single -/+" {...@@ -2090,11 +2225,15 @@ test "big.int div trunc single-single -/+" {
2090 const u: i32 = -5;2225 const u: i32 = -5;
2091 const v: i32 = 3;2226 const v: i32 = 3;
20922227
2093 var a = try Int.initSet(al, u);2228 var a = try Int.initSet(testing.allocator, u);
2094 var b = try Int.initSet(al, v);2229 defer a.deinit();
2230 var b = try Int.initSet(testing.allocator, v);
2231 defer b.deinit();
20952232
2096 var q = try Int.init(al);2233 var q = try Int.init(testing.allocator);
2097 var r = try Int.init(al);2234 defer q.deinit();
2235 var r = try Int.init(testing.allocator);
2236 defer r.deinit();
2098 try Int.divTrunc(&q, &r, a, b);2237 try Int.divTrunc(&q, &r, a, b);
20992238
2100 // n = q * d + r2239 // n = q * d + r
...@@ -2110,11 +2249,15 @@ test "big.int div trunc single-single +/-" {...@@ -2110,11 +2249,15 @@ test "big.int div trunc single-single +/-" {
2110 const u: i32 = 5;2249 const u: i32 = 5;
2111 const v: i32 = -3;2250 const v: i32 = -3;
21122251
2113 var a = try Int.initSet(al, u);2252 var a = try Int.initSet(testing.allocator, u);
2114 var b = try Int.initSet(al, v);2253 defer a.deinit();
2254 var b = try Int.initSet(testing.allocator, v);
2255 defer b.deinit();
21152256
2116 var q = try Int.init(al);2257 var q = try Int.init(testing.allocator);
2117 var r = try Int.init(al);2258 defer q.deinit();
2259 var r = try Int.init(testing.allocator);
2260 defer r.deinit();
2118 try Int.divTrunc(&q, &r, a, b);2261 try Int.divTrunc(&q, &r, a, b);
21192262
2120 // n = q * d + r2263 // n = q * d + r
...@@ -2130,11 +2273,15 @@ test "big.int div trunc single-single -/-" {...@@ -2130,11 +2273,15 @@ test "big.int div trunc single-single -/-" {
2130 const u: i32 = -5;2273 const u: i32 = -5;
2131 const v: i32 = -3;2274 const v: i32 = -3;
21322275
2133 var a = try Int.initSet(al, u);2276 var a = try Int.initSet(testing.allocator, u);
2134 var b = try Int.initSet(al, v);2277 defer a.deinit();
2278 var b = try Int.initSet(testing.allocator, v);
2279 defer b.deinit();
21352280
2136 var q = try Int.init(al);2281 var q = try Int.init(testing.allocator);
2137 var r = try Int.init(al);2282 defer q.deinit();
2283 var r = try Int.init(testing.allocator);
2284 defer r.deinit();
2138 try Int.divTrunc(&q, &r, a, b);2285 try Int.divTrunc(&q, &r, a, b);
21392286
2140 // n = q * d + r2287 // n = q * d + r
...@@ -2150,11 +2297,15 @@ test "big.int div floor single-single +/+" {...@@ -2150,11 +2297,15 @@ test "big.int div floor single-single +/+" {
2150 const u: i32 = 5;2297 const u: i32 = 5;
2151 const v: i32 = 3;2298 const v: i32 = 3;
21522299
2153 var a = try Int.initSet(al, u);2300 var a = try Int.initSet(testing.allocator, u);
2154 var b = try Int.initSet(al, v);2301 defer a.deinit();
2302 var b = try Int.initSet(testing.allocator, v);
2303 defer b.deinit();
21552304
2156 var q = try Int.init(al);2305 var q = try Int.init(testing.allocator);
2157 var r = try Int.init(al);2306 defer q.deinit();
2307 var r = try Int.init(testing.allocator);
2308 defer r.deinit();
2158 try Int.divFloor(&q, &r, a, b);2309 try Int.divFloor(&q, &r, a, b);
21592310
2160 // n = q * d + r2311 // n = q * d + r
...@@ -2170,11 +2321,15 @@ test "big.int div floor single-single -/+" {...@@ -2170,11 +2321,15 @@ test "big.int div floor single-single -/+" {
2170 const u: i32 = -5;2321 const u: i32 = -5;
2171 const v: i32 = 3;2322 const v: i32 = 3;
21722323
2173 var a = try Int.initSet(al, u);2324 var a = try Int.initSet(testing.allocator, u);
2174 var b = try Int.initSet(al, v);2325 defer a.deinit();
2326 var b = try Int.initSet(testing.allocator, v);
2327 defer b.deinit();
21752328
2176 var q = try Int.init(al);2329 var q = try Int.init(testing.allocator);
2177 var r = try Int.init(al);2330 defer q.deinit();
2331 var r = try Int.init(testing.allocator);
2332 defer r.deinit();
2178 try Int.divFloor(&q, &r, a, b);2333 try Int.divFloor(&q, &r, a, b);
21792334
2180 // n = q * d + r2335 // n = q * d + r
...@@ -2190,11 +2345,15 @@ test "big.int div floor single-single +/-" {...@@ -2190,11 +2345,15 @@ test "big.int div floor single-single +/-" {
2190 const u: i32 = 5;2345 const u: i32 = 5;
2191 const v: i32 = -3;2346 const v: i32 = -3;
21922347
2193 var a = try Int.initSet(al, u);2348 var a = try Int.initSet(testing.allocator, u);
2194 var b = try Int.initSet(al, v);2349 defer a.deinit();
2350 var b = try Int.initSet(testing.allocator, v);
2351 defer b.deinit();
21952352
2196 var q = try Int.init(al);2353 var q = try Int.init(testing.allocator);
2197 var r = try Int.init(al);2354 defer q.deinit();
2355 var r = try Int.init(testing.allocator);
2356 defer r.deinit();
2198 try Int.divFloor(&q, &r, a, b);2357 try Int.divFloor(&q, &r, a, b);
21992358
2200 // n = q * d + r2359 // n = q * d + r
...@@ -2210,11 +2369,15 @@ test "big.int div floor single-single -/-" {...@@ -2210,11 +2369,15 @@ test "big.int div floor single-single -/-" {
2210 const u: i32 = -5;2369 const u: i32 = -5;
2211 const v: i32 = -3;2370 const v: i32 = -3;
22122371
2213 var a = try Int.initSet(al, u);2372 var a = try Int.initSet(testing.allocator, u);
2214 var b = try Int.initSet(al, v);2373 defer a.deinit();
2374 var b = try Int.initSet(testing.allocator, v);
2375 defer b.deinit();
22152376
2216 var q = try Int.init(al);2377 var q = try Int.init(testing.allocator);
2217 var r = try Int.init(al);2378 defer q.deinit();
2379 var r = try Int.init(testing.allocator);
2380 defer r.deinit();
2218 try Int.divFloor(&q, &r, a, b);2381 try Int.divFloor(&q, &r, a, b);
22192382
2220 // n = q * d + r2383 // n = q * d + r
...@@ -2227,11 +2390,15 @@ test "big.int div floor single-single -/-" {...@@ -2227,11 +2390,15 @@ test "big.int div floor single-single -/-" {
2227}2390}
22282391
2229test "big.int div multi-multi with rem" {2392test "big.int div multi-multi with rem" {
2230 var a = try Int.initSet(al, 0x8888999911110000ffffeeeeddddccccbbbbaaaa9999);2393 var a = try Int.initSet(testing.allocator, 0x8888999911110000ffffeeeeddddccccbbbbaaaa9999);
2231 var b = try Int.initSet(al, 0x99990000111122223333);2394 defer a.deinit();
22322395 var b = try Int.initSet(testing.allocator, 0x99990000111122223333);
2233 var q = try Int.init(al);2396 defer b.deinit();
2234 var r = try Int.init(al);2397
2398 var q = try Int.init(testing.allocator);
2399 defer q.deinit();
2400 var r = try Int.init(testing.allocator);
2401 defer r.deinit();
2235 try Int.divTrunc(&q, &r, a, b);2402 try Int.divTrunc(&q, &r, a, b);
22362403
2237 testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);2404 testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
...@@ -2239,11 +2406,15 @@ test "big.int div multi-multi with rem" {...@@ -2239,11 +2406,15 @@ test "big.int div multi-multi with rem" {
2239}2406}
22402407
2241test "big.int div multi-multi no rem" {2408test "big.int div multi-multi no rem" {
2242 var a = try Int.initSet(al, 0x8888999911110000ffffeeeedb4fec200ee3a4286361);2409 var a = try Int.initSet(testing.allocator, 0x8888999911110000ffffeeeedb4fec200ee3a4286361);
2243 var b = try Int.initSet(al, 0x99990000111122223333);2410 defer a.deinit();
22442411 var b = try Int.initSet(testing.allocator, 0x99990000111122223333);
2245 var q = try Int.init(al);2412 defer b.deinit();
2246 var r = try Int.init(al);2413
2414 var q = try Int.init(testing.allocator);
2415 defer q.deinit();
2416 var r = try Int.init(testing.allocator);
2417 defer r.deinit();
2247 try Int.divTrunc(&q, &r, a, b);2418 try Int.divTrunc(&q, &r, a, b);
22482419
2249 testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);2420 testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
...@@ -2251,11 +2422,15 @@ test "big.int div multi-multi no rem" {...@@ -2251,11 +2422,15 @@ test "big.int div multi-multi no rem" {
2251}2422}
22522423
2253test "big.int div multi-multi (2 branch)" {2424test "big.int div multi-multi (2 branch)" {
2254 var a = try Int.initSet(al, 0x866666665555555588888887777777761111111111111111);2425 var a = try Int.initSet(testing.allocator, 0x866666665555555588888887777777761111111111111111);
2255 var b = try Int.initSet(al, 0x86666666555555554444444433333333);2426 defer a.deinit();
22562427 var b = try Int.initSet(testing.allocator, 0x86666666555555554444444433333333);
2257 var q = try Int.init(al);2428 defer b.deinit();
2258 var r = try Int.init(al);2429
2430 var q = try Int.init(testing.allocator);
2431 defer q.deinit();
2432 var r = try Int.init(testing.allocator);
2433 defer r.deinit();
2259 try Int.divTrunc(&q, &r, a, b);2434 try Int.divTrunc(&q, &r, a, b);
22602435
2261 testing.expect((try q.to(u128)) == 0x10000000000000000);2436 testing.expect((try q.to(u128)) == 0x10000000000000000);
...@@ -2263,11 +2438,15 @@ test "big.int div multi-multi (2 branch)" {...@@ -2263,11 +2438,15 @@ test "big.int div multi-multi (2 branch)" {
2263}2438}
22642439
2265test "big.int div multi-multi (3.1/3.3 branch)" {2440test "big.int div multi-multi (3.1/3.3 branch)" {
2266 var a = try Int.initSet(al, 0x11111111111111111111111111111111111111111111111111111111111111);2441 var a = try Int.initSet(testing.allocator, 0x11111111111111111111111111111111111111111111111111111111111111);
2267 var b = try Int.initSet(al, 0x1111111111111111111111111111111111111111171);2442 defer a.deinit();
22682443 var b = try Int.initSet(testing.allocator, 0x1111111111111111111111111111111111111111171);
2269 var q = try Int.init(al);2444 defer b.deinit();
2270 var r = try Int.init(al);2445
2446 var q = try Int.init(testing.allocator);
2447 defer q.deinit();
2448 var r = try Int.init(testing.allocator);
2449 defer r.deinit();
2271 try Int.divTrunc(&q, &r, a, b);2450 try Int.divTrunc(&q, &r, a, b);
22722451
2273 testing.expect((try q.to(u128)) == 0xfffffffffffffffffff);2452 testing.expect((try q.to(u128)) == 0xfffffffffffffffffff);
...@@ -2275,145 +2454,189 @@ test "big.int div multi-multi (3.1/3.3 branch)" {...@@ -2275,145 +2454,189 @@ test "big.int div multi-multi (3.1/3.3 branch)" {
2275}2454}
22762455
2277test "big.int div multi-single zero-limb trailing" {2456test "big.int div multi-single zero-limb trailing" {
2278 var a = try Int.initSet(al, 0x60000000000000000000000000000000000000000000000000000000000000000);2457 var a = try Int.initSet(testing.allocator, 0x60000000000000000000000000000000000000000000000000000000000000000);
2279 var b = try Int.initSet(al, 0x10000000000000000);2458 defer a.deinit();
22802459 var b = try Int.initSet(testing.allocator, 0x10000000000000000);
2281 var q = try Int.init(al);2460 defer b.deinit();
2282 var r = try Int.init(al);2461
2462 var q = try Int.init(testing.allocator);
2463 defer q.deinit();
2464 var r = try Int.init(testing.allocator);
2465 defer r.deinit();
2283 try Int.divTrunc(&q, &r, a, b);2466 try Int.divTrunc(&q, &r, a, b);
22842467
2285 var expected = try Int.initSet(al, 0x6000000000000000000000000000000000000000000000000);2468 var expected = try Int.initSet(testing.allocator, 0x6000000000000000000000000000000000000000000000000);
2469 defer expected.deinit();
2286 testing.expect(q.eq(expected));2470 testing.expect(q.eq(expected));
2287 testing.expect(r.eqZero());2471 testing.expect(r.eqZero());
2288}2472}
22892473
2290test "big.int div multi-multi zero-limb trailing (with rem)" {2474test "big.int div multi-multi zero-limb trailing (with rem)" {
2291 var a = try Int.initSet(al, 0x86666666555555558888888777777776111111111111111100000000000000000000000000000000);2475 var a = try Int.initSet(testing.allocator, 0x86666666555555558888888777777776111111111111111100000000000000000000000000000000);
2292 var b = try Int.initSet(al, 0x8666666655555555444444443333333300000000000000000000000000000000);2476 defer a.deinit();
22932477 var b = try Int.initSet(testing.allocator, 0x8666666655555555444444443333333300000000000000000000000000000000);
2294 var q = try Int.init(al);2478 defer b.deinit();
2295 var r = try Int.init(al);2479
2480 var q = try Int.init(testing.allocator);
2481 defer q.deinit();
2482 var r = try Int.init(testing.allocator);
2483 defer r.deinit();
2296 try Int.divTrunc(&q, &r, a, b);2484 try Int.divTrunc(&q, &r, a, b);
22972485
2298 testing.expect((try q.to(u128)) == 0x10000000000000000);2486 testing.expect((try q.to(u128)) == 0x10000000000000000);
22992487
2300 const rs = try r.toString(al, 16);2488 const rs = try r.toString(testing.allocator, 16);
2489 defer testing.allocator.free(rs);
2301 testing.expect(std.mem.eql(u8, rs, "4444444344444443111111111111111100000000000000000000000000000000"));2490 testing.expect(std.mem.eql(u8, rs, "4444444344444443111111111111111100000000000000000000000000000000"));
2302}2491}
23032492
2304test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-limb count > divisor zero-limb count" {2493test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-limb count > divisor zero-limb count" {
2305 var a = try Int.initSet(al, 0x8666666655555555888888877777777611111111111111110000000000000000);2494 var a = try Int.initSet(testing.allocator, 0x8666666655555555888888877777777611111111111111110000000000000000);
2306 var b = try Int.initSet(al, 0x8666666655555555444444443333333300000000000000000000000000000000);2495 defer a.deinit();
23072496 var b = try Int.initSet(testing.allocator, 0x8666666655555555444444443333333300000000000000000000000000000000);
2308 var q = try Int.init(al);2497 defer b.deinit();
2309 var r = try Int.init(al);2498
2499 var q = try Int.init(testing.allocator);
2500 defer q.deinit();
2501 var r = try Int.init(testing.allocator);
2502 defer r.deinit();
2310 try Int.divTrunc(&q, &r, a, b);2503 try Int.divTrunc(&q, &r, a, b);
23112504
2312 testing.expect((try q.to(u128)) == 0x1);2505 testing.expect((try q.to(u128)) == 0x1);
23132506
2314 const rs = try r.toString(al, 16);2507 const rs = try r.toString(testing.allocator, 16);
2508 defer testing.allocator.free(rs);
2315 testing.expect(std.mem.eql(u8, rs, "444444434444444311111111111111110000000000000000"));2509 testing.expect(std.mem.eql(u8, rs, "444444434444444311111111111111110000000000000000"));
2316}2510}
23172511
2318test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-limb count < divisor zero-limb count" {2512test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-limb count < divisor zero-limb count" {
2319 var a = try Int.initSet(al, 0x86666666555555558888888777777776111111111111111100000000000000000000000000000000);2513 var a = try Int.initSet(testing.allocator, 0x86666666555555558888888777777776111111111111111100000000000000000000000000000000);
2320 var b = try Int.initSet(al, 0x866666665555555544444444333333330000000000000000);2514 defer a.deinit();
23212515 var b = try Int.initSet(testing.allocator, 0x866666665555555544444444333333330000000000000000);
2322 var q = try Int.init(al);2516 defer b.deinit();
2323 var r = try Int.init(al);2517
2518 var q = try Int.init(testing.allocator);
2519 defer q.deinit();
2520 var r = try Int.init(testing.allocator);
2521 defer r.deinit();
2324 try Int.divTrunc(&q, &r, a, b);2522 try Int.divTrunc(&q, &r, a, b);
23252523
2326 const qs = try q.toString(al, 16);2524 const qs = try q.toString(testing.allocator, 16);
2525 defer testing.allocator.free(qs);
2327 testing.expect(std.mem.eql(u8, qs, "10000000000000000820820803105186f"));2526 testing.expect(std.mem.eql(u8, qs, "10000000000000000820820803105186f"));
23282527
2329 const rs = try r.toString(al, 16);2528 const rs = try r.toString(testing.allocator, 16);
2529 defer testing.allocator.free(rs);
2330 testing.expect(std.mem.eql(u8, rs, "4e11f2baa5896a321d463b543d0104e30000000000000000"));2530 testing.expect(std.mem.eql(u8, rs, "4e11f2baa5896a321d463b543d0104e30000000000000000"));
2331}2531}
23322532
2333test "big.int div multi-multi fuzz case #1" {2533test "big.int div multi-multi fuzz case #1" {
2334 var a = try Int.init(al);2534 var a = try Int.init(testing.allocator);
2335 var b = try Int.init(al);2535 defer a.deinit();
2536 var b = try Int.init(testing.allocator);
2537 defer b.deinit();
23362538
2337 try a.setString(16, "ffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");2539 try a.setString(16, "ffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
2338 try b.setString(16, "3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffc000000000000000000000000000000007fffffffffff");2540 try b.setString(16, "3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffc000000000000000000000000000000007fffffffffff");
23392541
2340 var q = try Int.init(al);2542 var q = try Int.init(testing.allocator);
2341 var r = try Int.init(al);2543 defer q.deinit();
2544 var r = try Int.init(testing.allocator);
2545 defer r.deinit();
2342 try Int.divTrunc(&q, &r, a, b);2546 try Int.divTrunc(&q, &r, a, b);
23432547
2344 const qs = try q.toString(al, 16);2548 const qs = try q.toString(testing.allocator, 16);
2549 defer testing.allocator.free(qs);
2345 testing.expect(std.mem.eql(u8, qs, "3ffffffffffffffffffffffffffff0000000000000000000000000000000000001ffffffffffffffffffffffffffff7fffffffe000000000000000000000000000180000000000000000000003fffffbfffffffdfffffffffffffeffff800000100101000000100000000020003fffffdfbfffffe3ffffffffffffeffff7fffc00800a100000017ffe000002000400007efbfff7fe9f00000037ffff3fff7fffa004006100000009ffe00000190038200bf7d2ff7fefe80400060000f7d7f8fbf9401fe38e0403ffc0bdffffa51102c300d7be5ef9df4e5060007b0127ad3fa69f97d0f820b6605ff617ddf7f32ad7a05c0d03f2e7bc78a6000e087a8bbcdc59e07a5a079128a7861f553ddebed7e8e56701756f9ead39b48cd1b0831889ea6ec1fddf643d0565b075ff07e6caea4e2854ec9227fd635ed60a2f5eef2893052ffd54718fa08604acbf6a15e78a467c4a3c53c0278af06c4416573f925491b195e8fd79302cb1aaf7caf4ecfc9aec1254cc969786363ac729f914c6ddcc26738d6b0facd54eba026580aba2eb6482a088b0d224a8852420b91ec1"));2550 testing.expect(std.mem.eql(u8, qs, "3ffffffffffffffffffffffffffff0000000000000000000000000000000000001ffffffffffffffffffffffffffff7fffffffe000000000000000000000000000180000000000000000000003fffffbfffffffdfffffffffffffeffff800000100101000000100000000020003fffffdfbfffffe3ffffffffffffeffff7fffc00800a100000017ffe000002000400007efbfff7fe9f00000037ffff3fff7fffa004006100000009ffe00000190038200bf7d2ff7fefe80400060000f7d7f8fbf9401fe38e0403ffc0bdffffa51102c300d7be5ef9df4e5060007b0127ad3fa69f97d0f820b6605ff617ddf7f32ad7a05c0d03f2e7bc78a6000e087a8bbcdc59e07a5a079128a7861f553ddebed7e8e56701756f9ead39b48cd1b0831889ea6ec1fddf643d0565b075ff07e6caea4e2854ec9227fd635ed60a2f5eef2893052ffd54718fa08604acbf6a15e78a467c4a3c53c0278af06c4416573f925491b195e8fd79302cb1aaf7caf4ecfc9aec1254cc969786363ac729f914c6ddcc26738d6b0facd54eba026580aba2eb6482a088b0d224a8852420b91ec1"));
23462551
2347 const rs = try r.toString(al, 16);2552 const rs = try r.toString(testing.allocator, 16);
2553 defer testing.allocator.free(rs);
2348 testing.expect(std.mem.eql(u8, rs, "310d1d4c414426b4836c2635bad1df3a424e50cbdd167ffccb4dfff57d36b4aae0d6ca0910698220171a0f3373c1060a046c2812f0027e321f72979daa5e7973214170d49e885de0c0ecc167837d44502430674a82522e5df6a0759548052420b91ec1"));2554 testing.expect(std.mem.eql(u8, rs, "310d1d4c414426b4836c2635bad1df3a424e50cbdd167ffccb4dfff57d36b4aae0d6ca0910698220171a0f3373c1060a046c2812f0027e321f72979daa5e7973214170d49e885de0c0ecc167837d44502430674a82522e5df6a0759548052420b91ec1"));
2349}2555}
23502556
2351test "big.int div multi-multi fuzz case #2" {2557test "big.int div multi-multi fuzz case #2" {
2352 var a = try Int.init(al);2558 var a = try Int.init(testing.allocator);
2353 var b = try Int.init(al);2559 defer a.deinit();
2560 var b = try Int.init(testing.allocator);
2561 defer b.deinit();
23542562
2355 try a.setString(16, "3ffffffffe00000000000000000000000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000001fffffffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffc000000000000000000000000000000000000000000000000000000000000000");2563 try a.setString(16, "3ffffffffe00000000000000000000000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000001fffffffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffc000000000000000000000000000000000000000000000000000000000000000");
2356 try b.setString(16, "ffc0000000000000000000000000000000000000000000000000");2564 try b.setString(16, "ffc0000000000000000000000000000000000000000000000000");
23572565
2358 var q = try Int.init(al);2566 var q = try Int.init(testing.allocator);
2359 var r = try Int.init(al);2567 defer q.deinit();
2568 var r = try Int.init(testing.allocator);
2569 defer r.deinit();
2360 try Int.divTrunc(&q, &r, a, b);2570 try Int.divTrunc(&q, &r, a, b);
23612571
2362 const qs = try q.toString(al, 16);2572 const qs = try q.toString(testing.allocator, 16);
2573 defer testing.allocator.free(qs);
2363 testing.expect(std.mem.eql(u8, qs, "40100400fe3f8fe3f8fe3f8fe3f8fe3f8fe4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f91e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4992649926499264991e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4792e4b92e4b92e4b92e4b92a4a92a4a92a4"));2574 testing.expect(std.mem.eql(u8, qs, "40100400fe3f8fe3f8fe3f8fe3f8fe3f8fe4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f93e4f91e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4992649926499264991e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4791e4792e4b92e4b92e4b92e4b92a4a92a4a92a4"));
23642575
2365 const rs = try r.toString(al, 16);2576 const rs = try r.toString(testing.allocator, 16);
2577 defer testing.allocator.free(rs);
2366 testing.expect(std.mem.eql(u8, rs, "a900000000000000000000000000000000000000000000000000"));2578 testing.expect(std.mem.eql(u8, rs, "a900000000000000000000000000000000000000000000000000"));
2367}2579}
23682580
2369test "big.int shift-right single" {2581test "big.int shift-right single" {
2370 var a = try Int.initSet(al, 0xffff0000);2582 var a = try Int.initSet(testing.allocator, 0xffff0000);
2583 defer a.deinit();
2371 try a.shiftRight(a, 16);2584 try a.shiftRight(a, 16);
23722585
2373 testing.expect((try a.to(u32)) == 0xffff);2586 testing.expect((try a.to(u32)) == 0xffff);
2374}2587}
23752588
2376test "big.int shift-right multi" {2589test "big.int shift-right multi" {
2377 var a = try Int.initSet(al, 0xffff0000eeee1111dddd2222cccc3333);2590 var a = try Int.initSet(testing.allocator, 0xffff0000eeee1111dddd2222cccc3333);
2591 defer a.deinit();
2378 try a.shiftRight(a, 67);2592 try a.shiftRight(a, 67);
23792593
2380 testing.expect((try a.to(u64)) == 0x1fffe0001dddc222);2594 testing.expect((try a.to(u64)) == 0x1fffe0001dddc222);
2381}2595}
23822596
2383test "big.int shift-left single" {2597test "big.int shift-left single" {
2384 var a = try Int.initSet(al, 0xffff);2598 var a = try Int.initSet(testing.allocator, 0xffff);
2599 defer a.deinit();
2385 try a.shiftLeft(a, 16);2600 try a.shiftLeft(a, 16);
23862601
2387 testing.expect((try a.to(u64)) == 0xffff0000);2602 testing.expect((try a.to(u64)) == 0xffff0000);
2388}2603}
23892604
2390test "big.int shift-left multi" {2605test "big.int shift-left multi" {
2391 var a = try Int.initSet(al, 0x1fffe0001dddc222);2606 var a = try Int.initSet(testing.allocator, 0x1fffe0001dddc222);
2607 defer a.deinit();
2392 try a.shiftLeft(a, 67);2608 try a.shiftLeft(a, 67);
23932609
2394 testing.expect((try a.to(u128)) == 0xffff0000eeee11100000000000000000);2610 testing.expect((try a.to(u128)) == 0xffff0000eeee11100000000000000000);
2395}2611}
23962612
2397test "big.int shift-right negative" {2613test "big.int shift-right negative" {
2398 var a = try Int.init(al);2614 var a = try Int.init(testing.allocator);
2615 defer a.deinit();
23992616
2400 try a.shiftRight(try Int.initSet(al, -20), 2);2617 try a.shiftRight(try Int.initSet(testing.allocator, -20), 2);
2618 defer a.deinit();
2401 testing.expect((try a.to(i32)) == -20 >> 2);2619 testing.expect((try a.to(i32)) == -20 >> 2);
24022620
2403 try a.shiftRight(try Int.initSet(al, -5), 10);2621 try a.shiftRight(try Int.initSet(testing.allocator, -5), 10);
2622 defer a.deinit();
2404 testing.expect((try a.to(i32)) == -5 >> 10);2623 testing.expect((try a.to(i32)) == -5 >> 10);
2405}2624}
24062625
2407test "big.int shift-left negative" {2626test "big.int shift-left negative" {
2408 var a = try Int.init(al);2627 var a = try Int.init(testing.allocator);
2628 defer a.deinit();
24092629
2410 try a.shiftRight(try Int.initSet(al, -10), 1232);2630 try a.shiftRight(try Int.initSet(testing.allocator, -10), 1232);
2631 defer a.deinit();
2411 testing.expect((try a.to(i32)) == -10 >> 1232);2632 testing.expect((try a.to(i32)) == -10 >> 1232);
2412}2633}
24132634
2414test "big.int bitwise and simple" {2635test "big.int bitwise and simple" {
2415 var a = try Int.initSet(al, 0xffffffff11111111);2636 var a = try Int.initSet(testing.allocator, 0xffffffff11111111);
2416 var b = try Int.initSet(al, 0xeeeeeeee22222222);2637 defer a.deinit();
2638 var b = try Int.initSet(testing.allocator, 0xeeeeeeee22222222);
2639 defer b.deinit();
24172640
2418 try a.bitAnd(a, b);2641 try a.bitAnd(a, b);
24192642
...@@ -2421,8 +2644,10 @@ test "big.int bitwise and simple" {...@@ -2421,8 +2644,10 @@ test "big.int bitwise and simple" {
2421}2644}
24222645
2423test "big.int bitwise and multi-limb" {2646test "big.int bitwise and multi-limb" {
2424 var a = try Int.initSet(al, maxInt(Limb) + 1);2647 var a = try Int.initSet(testing.allocator, maxInt(Limb) + 1);
2425 var b = try Int.initSet(al, maxInt(Limb));2648 defer a.deinit();
2649 var b = try Int.initSet(testing.allocator, maxInt(Limb));
2650 defer b.deinit();
24262651
2427 try a.bitAnd(a, b);2652 try a.bitAnd(a, b);
24282653
...@@ -2430,8 +2655,10 @@ test "big.int bitwise and multi-limb" {...@@ -2430,8 +2655,10 @@ test "big.int bitwise and multi-limb" {
2430}2655}
24312656
2432test "big.int bitwise xor simple" {2657test "big.int bitwise xor simple" {
2433 var a = try Int.initSet(al, 0xffffffff11111111);2658 var a = try Int.initSet(testing.allocator, 0xffffffff11111111);
2434 var b = try Int.initSet(al, 0xeeeeeeee22222222);2659 defer a.deinit();
2660 var b = try Int.initSet(testing.allocator, 0xeeeeeeee22222222);
2661 defer b.deinit();
24352662
2436 try a.bitXor(a, b);2663 try a.bitXor(a, b);
24372664
...@@ -2439,8 +2666,10 @@ test "big.int bitwise xor simple" {...@@ -2439,8 +2666,10 @@ test "big.int bitwise xor simple" {
2439}2666}
24402667
2441test "big.int bitwise xor multi-limb" {2668test "big.int bitwise xor multi-limb" {
2442 var a = try Int.initSet(al, maxInt(Limb) + 1);2669 var a = try Int.initSet(testing.allocator, maxInt(Limb) + 1);
2443 var b = try Int.initSet(al, maxInt(Limb));2670 defer a.deinit();
2671 var b = try Int.initSet(testing.allocator, maxInt(Limb));
2672 defer b.deinit();
24442673
2445 try a.bitXor(a, b);2674 try a.bitXor(a, b);
24462675
...@@ -2448,8 +2677,10 @@ test "big.int bitwise xor multi-limb" {...@@ -2448,8 +2677,10 @@ test "big.int bitwise xor multi-limb" {
2448}2677}
24492678
2450test "big.int bitwise or simple" {2679test "big.int bitwise or simple" {
2451 var a = try Int.initSet(al, 0xffffffff11111111);2680 var a = try Int.initSet(testing.allocator, 0xffffffff11111111);
2452 var b = try Int.initSet(al, 0xeeeeeeee22222222);2681 defer a.deinit();
2682 var b = try Int.initSet(testing.allocator, 0xeeeeeeee22222222);
2683 defer b.deinit();
24532684
2454 try a.bitOr(a, b);2685 try a.bitOr(a, b);
24552686
...@@ -2457,8 +2688,10 @@ test "big.int bitwise or simple" {...@@ -2457,8 +2688,10 @@ test "big.int bitwise or simple" {
2457}2688}
24582689
2459test "big.int bitwise or multi-limb" {2690test "big.int bitwise or multi-limb" {
2460 var a = try Int.initSet(al, maxInt(Limb) + 1);2691 var a = try Int.initSet(testing.allocator, maxInt(Limb) + 1);
2461 var b = try Int.initSet(al, maxInt(Limb));2692 defer a.deinit();
2693 var b = try Int.initSet(testing.allocator, maxInt(Limb));
2694 defer b.deinit();
24622695
2463 try a.bitOr(a, b);2696 try a.bitOr(a, b);
24642697
...@@ -2467,11 +2700,19 @@ test "big.int bitwise or multi-limb" {...@@ -2467,11 +2700,19 @@ test "big.int bitwise or multi-limb" {
2467}2700}
24682701
2469test "big.int var args" {2702test "big.int var args" {
2470 var a = try Int.initSet(al, 5);2703 var a = try Int.initSet(testing.allocator, 5);
2704 defer a.deinit();
24712705
2472 try a.add(a, try Int.initSet(al, 6));2706 const b = try Int.initSet(testing.allocator, 6);
2707 defer b.deinit();
2708 try a.add(a, b);
2473 testing.expect((try a.to(u64)) == 11);2709 testing.expect((try a.to(u64)) == 11);
24742710
2475 testing.expect(a.cmp(try Int.initSet(al, 11)) == 0);2711 const c = try Int.initSet(testing.allocator, 11);
2476 testing.expect(a.cmp(try Int.initSet(al, 14)) <= 0);2712 defer c.deinit();
2713 testing.expect(a.cmp(c) == 0);
2714
2715 const d = try Int.initSet(testing.allocator, 14);
2716 defer d.deinit();
2717 testing.expect(a.cmp(d) <= 0);
2477}2718}
lib/std/math/big/rational.zig+98-53
...@@ -587,14 +587,13 @@ fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void {...@@ -587,14 +587,13 @@ fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void {
587 r.swap(&x);587 r.swap(&x);
588}588}
589589
590var buffer: [64 * 8192]u8 = undefined;
591var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
592var al = &fixed.allocator;
593
594test "big.rational gcd non-one small" {590test "big.rational gcd non-one small" {
595 var a = try Int.initSet(al, 17);591 var a = try Int.initSet(testing.allocator, 17);
596 var b = try Int.initSet(al, 97);592 defer a.deinit();
597 var r = try Int.init(al);593 var b = try Int.initSet(testing.allocator, 97);
594 defer b.deinit();
595 var r = try Int.init(testing.allocator);
596 defer r.deinit();
598597
599 try gcd(&r, a, b);598 try gcd(&r, a, b);
600599
...@@ -602,9 +601,12 @@ test "big.rational gcd non-one small" {...@@ -602,9 +601,12 @@ test "big.rational gcd non-one small" {
602}601}
603602
604test "big.rational gcd non-one small" {603test "big.rational gcd non-one small" {
605 var a = try Int.initSet(al, 4864);604 var a = try Int.initSet(testing.allocator, 4864);
606 var b = try Int.initSet(al, 3458);605 defer a.deinit();
607 var r = try Int.init(al);606 var b = try Int.initSet(testing.allocator, 3458);
607 defer b.deinit();
608 var r = try Int.init(testing.allocator);
609 defer r.deinit();
608610
609 try gcd(&r, a, b);611 try gcd(&r, a, b);
610612
...@@ -612,9 +614,12 @@ test "big.rational gcd non-one small" {...@@ -612,9 +614,12 @@ test "big.rational gcd non-one small" {
612}614}
613615
614test "big.rational gcd non-one large" {616test "big.rational gcd non-one large" {
615 var a = try Int.initSet(al, 0xffffffffffffffff);617 var a = try Int.initSet(testing.allocator, 0xffffffffffffffff);
616 var b = try Int.initSet(al, 0xffffffffffffffff7777);618 defer a.deinit();
617 var r = try Int.init(al);619 var b = try Int.initSet(testing.allocator, 0xffffffffffffffff7777);
620 defer b.deinit();
621 var r = try Int.init(testing.allocator);
622 defer r.deinit();
618623
619 try gcd(&r, a, b);624 try gcd(&r, a, b);
620625
...@@ -622,9 +627,12 @@ test "big.rational gcd non-one large" {...@@ -622,9 +627,12 @@ test "big.rational gcd non-one large" {
622}627}
623628
624test "big.rational gcd large multi-limb result" {629test "big.rational gcd large multi-limb result" {
625 var a = try Int.initSet(al, 0x12345678123456781234567812345678123456781234567812345678);630 var a = try Int.initSet(testing.allocator, 0x12345678123456781234567812345678123456781234567812345678);
626 var b = try Int.initSet(al, 0x12345671234567123456712345671234567123456712345671234567);631 defer a.deinit();
627 var r = try Int.init(al);632 var b = try Int.initSet(testing.allocator, 0x12345671234567123456712345671234567123456712345671234567);
633 defer b.deinit();
634 var r = try Int.init(testing.allocator);
635 defer r.deinit();
628636
629 try gcd(&r, a, b);637 try gcd(&r, a, b);
630638
...@@ -632,9 +640,12 @@ test "big.rational gcd large multi-limb result" {...@@ -632,9 +640,12 @@ test "big.rational gcd large multi-limb result" {
632}640}
633641
634test "big.rational gcd one large" {642test "big.rational gcd one large" {
635 var a = try Int.initSet(al, 1897056385327307);643 var a = try Int.initSet(testing.allocator, 1897056385327307);
636 var b = try Int.initSet(al, 2251799813685248);644 defer a.deinit();
637 var r = try Int.init(al);645 var b = try Int.initSet(testing.allocator, 2251799813685248);
646 defer b.deinit();
647 var r = try Int.init(testing.allocator);
648 defer r.deinit();
638649
639 try gcd(&r, a, b);650 try gcd(&r, a, b);
640651
...@@ -661,7 +672,8 @@ fn extractLowBits(a: Int, comptime T: type) T {...@@ -661,7 +672,8 @@ fn extractLowBits(a: Int, comptime T: type) T {
661}672}
662673
663test "big.rational extractLowBits" {674test "big.rational extractLowBits" {
664 var a = try Int.initSet(al, 0x11112222333344441234567887654321);675 var a = try Int.initSet(testing.allocator, 0x11112222333344441234567887654321);
676 defer a.deinit();
665677
666 const a1 = extractLowBits(a, u8);678 const a1 = extractLowBits(a, u8);
667 testing.expect(a1 == 0x21);679 testing.expect(a1 == 0x21);
...@@ -680,7 +692,8 @@ test "big.rational extractLowBits" {...@@ -680,7 +692,8 @@ test "big.rational extractLowBits" {
680}692}
681693
682test "big.rational set" {694test "big.rational set" {
683 var a = try Rational.init(al);695 var a = try Rational.init(testing.allocator);
696 defer a.deinit();
684697
685 try a.setInt(5);698 try a.setInt(5);
686 testing.expect((try a.p.to(u32)) == 5);699 testing.expect((try a.p.to(u32)) == 5);
...@@ -708,7 +721,8 @@ test "big.rational set" {...@@ -708,7 +721,8 @@ test "big.rational set" {
708}721}
709722
710test "big.rational setFloat" {723test "big.rational setFloat" {
711 var a = try Rational.init(al);724 var a = try Rational.init(testing.allocator);
725 defer a.deinit();
712726
713 try a.setFloat(f64, 2.5);727 try a.setFloat(f64, 2.5);
714 testing.expect((try a.p.to(i32)) == 5);728 testing.expect((try a.p.to(i32)) == 5);
...@@ -732,7 +746,8 @@ test "big.rational setFloat" {...@@ -732,7 +746,8 @@ test "big.rational setFloat" {
732}746}
733747
734test "big.rational setFloatString" {748test "big.rational setFloatString" {
735 var a = try Rational.init(al);749 var a = try Rational.init(testing.allocator);
750 defer a.deinit();
736751
737 try a.setFloatString("72.14159312071241458852455252781510353");752 try a.setFloatString("72.14159312071241458852455252781510353");
738753
...@@ -742,7 +757,8 @@ test "big.rational setFloatString" {...@@ -742,7 +757,8 @@ test "big.rational setFloatString" {
742}757}
743758
744test "big.rational toFloat" {759test "big.rational toFloat" {
745 var a = try Rational.init(al);760 var a = try Rational.init(testing.allocator);
761 defer a.deinit();
746762
747 // = 3.14159297943115234375763 // = 3.14159297943115234375
748 try a.setRatio(3294199, 1048576);764 try a.setRatio(3294199, 1048576);
...@@ -754,7 +770,8 @@ test "big.rational toFloat" {...@@ -754,7 +770,8 @@ test "big.rational toFloat" {
754}770}
755771
756test "big.rational set/to Float round-trip" {772test "big.rational set/to Float round-trip" {
757 var a = try Rational.init(al);773 var a = try Rational.init(testing.allocator);
774 defer a.deinit();
758 var prng = std.rand.DefaultPrng.init(0x5EED);775 var prng = std.rand.DefaultPrng.init(0x5EED);
759 var i: usize = 0;776 var i: usize = 0;
760 while (i < 512) : (i += 1) {777 while (i < 512) : (i += 1) {
...@@ -765,23 +782,29 @@ test "big.rational set/to Float round-trip" {...@@ -765,23 +782,29 @@ test "big.rational set/to Float round-trip" {
765}782}
766783
767test "big.rational copy" {784test "big.rational copy" {
768 var a = try Rational.init(al);785 var a = try Rational.init(testing.allocator);
786 defer a.deinit();
769787
770 const b = try Int.initSet(al, 5);788 const b = try Int.initSet(testing.allocator, 5);
789 defer b.deinit();
771790
772 try a.copyInt(b);791 try a.copyInt(b);
773 testing.expect((try a.p.to(u32)) == 5);792 testing.expect((try a.p.to(u32)) == 5);
774 testing.expect((try a.q.to(u32)) == 1);793 testing.expect((try a.q.to(u32)) == 1);
775794
776 const c = try Int.initSet(al, 7);795 const c = try Int.initSet(testing.allocator, 7);
777 const d = try Int.initSet(al, 3);796 defer c.deinit();
797 const d = try Int.initSet(testing.allocator, 3);
798 defer d.deinit();
778799
779 try a.copyRatio(c, d);800 try a.copyRatio(c, d);
780 testing.expect((try a.p.to(u32)) == 7);801 testing.expect((try a.p.to(u32)) == 7);
781 testing.expect((try a.q.to(u32)) == 3);802 testing.expect((try a.q.to(u32)) == 3);
782803
783 const e = try Int.initSet(al, 9);804 const e = try Int.initSet(testing.allocator, 9);
784 const f = try Int.initSet(al, 3);805 defer e.deinit();
806 const f = try Int.initSet(testing.allocator, 3);
807 defer f.deinit();
785808
786 try a.copyRatio(e, f);809 try a.copyRatio(e, f);
787 testing.expect((try a.p.to(u32)) == 3);810 testing.expect((try a.p.to(u32)) == 3);
...@@ -789,7 +812,8 @@ test "big.rational copy" {...@@ -789,7 +812,8 @@ test "big.rational copy" {
789}812}
790813
791test "big.rational negate" {814test "big.rational negate" {
792 var a = try Rational.init(al);815 var a = try Rational.init(testing.allocator);
816 defer a.deinit();
793817
794 try a.setInt(-50);818 try a.setInt(-50);
795 testing.expect((try a.p.to(i32)) == -50);819 testing.expect((try a.p.to(i32)) == -50);
...@@ -805,7 +829,8 @@ test "big.rational negate" {...@@ -805,7 +829,8 @@ test "big.rational negate" {
805}829}
806830
807test "big.rational abs" {831test "big.rational abs" {
808 var a = try Rational.init(al);832 var a = try Rational.init(testing.allocator);
833 defer a.deinit();
809834
810 try a.setInt(-50);835 try a.setInt(-50);
811 testing.expect((try a.p.to(i32)) == -50);836 testing.expect((try a.p.to(i32)) == -50);
...@@ -821,8 +846,10 @@ test "big.rational abs" {...@@ -821,8 +846,10 @@ test "big.rational abs" {
821}846}
822847
823test "big.rational swap" {848test "big.rational swap" {
824 var a = try Rational.init(al);849 var a = try Rational.init(testing.allocator);
825 var b = try Rational.init(al);850 defer a.deinit();
851 var b = try Rational.init(testing.allocator);
852 defer b.deinit();
826853
827 try a.setRatio(50, 23);854 try a.setRatio(50, 23);
828 try b.setRatio(17, 3);855 try b.setRatio(17, 3);
...@@ -843,8 +870,10 @@ test "big.rational swap" {...@@ -843,8 +870,10 @@ test "big.rational swap" {
843}870}
844871
845test "big.rational cmp" {872test "big.rational cmp" {
846 var a = try Rational.init(al);873 var a = try Rational.init(testing.allocator);
847 var b = try Rational.init(al);874 defer a.deinit();
875 var b = try Rational.init(testing.allocator);
876 defer b.deinit();
848877
849 try a.setRatio(500, 231);878 try a.setRatio(500, 231);
850 try b.setRatio(18903, 8584);879 try b.setRatio(18903, 8584);
...@@ -856,8 +885,10 @@ test "big.rational cmp" {...@@ -856,8 +885,10 @@ test "big.rational cmp" {
856}885}
857886
858test "big.rational add single-limb" {887test "big.rational add single-limb" {
859 var a = try Rational.init(al);888 var a = try Rational.init(testing.allocator);
860 var b = try Rational.init(al);889 defer a.deinit();
890 var b = try Rational.init(testing.allocator);
891 defer b.deinit();
861892
862 try a.setRatio(500, 231);893 try a.setRatio(500, 231);
863 try b.setRatio(18903, 8584);894 try b.setRatio(18903, 8584);
...@@ -869,9 +900,12 @@ test "big.rational add single-limb" {...@@ -869,9 +900,12 @@ test "big.rational add single-limb" {
869}900}
870901
871test "big.rational add" {902test "big.rational add" {
872 var a = try Rational.init(al);903 var a = try Rational.init(testing.allocator);
873 var b = try Rational.init(al);904 defer a.deinit();
874 var r = try Rational.init(al);905 var b = try Rational.init(testing.allocator);
906 defer b.deinit();
907 var r = try Rational.init(testing.allocator);
908 defer r.deinit();
875909
876 try a.setRatio(78923, 23341);910 try a.setRatio(78923, 23341);
877 try b.setRatio(123097, 12441414);911 try b.setRatio(123097, 12441414);
...@@ -882,9 +916,12 @@ test "big.rational add" {...@@ -882,9 +916,12 @@ test "big.rational add" {
882}916}
883917
884test "big.rational sub" {918test "big.rational sub" {
885 var a = try Rational.init(al);919 var a = try Rational.init(testing.allocator);
886 var b = try Rational.init(al);920 defer a.deinit();
887 var r = try Rational.init(al);921 var b = try Rational.init(testing.allocator);
922 defer b.deinit();
923 var r = try Rational.init(testing.allocator);
924 defer r.deinit();
888925
889 try a.setRatio(78923, 23341);926 try a.setRatio(78923, 23341);
890 try b.setRatio(123097, 12441414);927 try b.setRatio(123097, 12441414);
...@@ -895,9 +932,12 @@ test "big.rational sub" {...@@ -895,9 +932,12 @@ test "big.rational sub" {
895}932}
896933
897test "big.rational mul" {934test "big.rational mul" {
898 var a = try Rational.init(al);935 var a = try Rational.init(testing.allocator);
899 var b = try Rational.init(al);936 defer a.deinit();
900 var r = try Rational.init(al);937 var b = try Rational.init(testing.allocator);
938 defer b.deinit();
939 var r = try Rational.init(testing.allocator);
940 defer r.deinit();
901941
902 try a.setRatio(78923, 23341);942 try a.setRatio(78923, 23341);
903 try b.setRatio(123097, 12441414);943 try b.setRatio(123097, 12441414);
...@@ -908,9 +948,12 @@ test "big.rational mul" {...@@ -908,9 +948,12 @@ test "big.rational mul" {
908}948}
909949
910test "big.rational div" {950test "big.rational div" {
911 var a = try Rational.init(al);951 var a = try Rational.init(testing.allocator);
912 var b = try Rational.init(al);952 defer a.deinit();
913 var r = try Rational.init(al);953 var b = try Rational.init(testing.allocator);
954 defer b.deinit();
955 var r = try Rational.init(testing.allocator);
956 defer r.deinit();
914957
915 try a.setRatio(78923, 23341);958 try a.setRatio(78923, 23341);
916 try b.setRatio(123097, 12441414);959 try b.setRatio(123097, 12441414);
...@@ -921,8 +964,10 @@ test "big.rational div" {...@@ -921,8 +964,10 @@ test "big.rational div" {
921}964}
922965
923test "big.rational div" {966test "big.rational div" {
924 var a = try Rational.init(al);967 var a = try Rational.init(testing.allocator);
925 var r = try Rational.init(al);968 defer a.deinit();
969 var r = try Rational.init(testing.allocator);
970 defer r.deinit();
926971
927 try a.setRatio(78923, 23341);972 try a.setRatio(78923, 23341);
928 a.invert();973 a.invert();
lib/std/mem.zig+30-14
...@@ -1011,11 +1011,21 @@ pub fn join(allocator: *Allocator, separator: []const u8, slices: []const []cons...@@ -1011,11 +1011,21 @@ pub fn join(allocator: *Allocator, separator: []const u8, slices: []const []cons
1011}1011}
10121012
1013test "mem.join" {1013test "mem.join" {
1014 var buf: [1024]u8 = undefined;1014 {
1015 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;1015 const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });
1016 testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "b", "c" }), "a,b,c"));1016 defer testing.allocator.free(str);
1017 testing.expect(eql(u8, try join(a, ",", &[_][]const u8{"a"}), "a"));1017 testing.expect(eql(u8, str, "a,b,c"));
1018 testing.expect(eql(u8, try join(a, ",", &[_][]const u8{ "a", "", "b", "", "c" }), "a,,b,,c"));1018 }
1019 {
1020 const str = try join(testing.allocator, ",", &[_][]const u8{"a"});
1021 defer testing.allocator.free(str);
1022 testing.expect(eql(u8, str, "a"));
1023 }
1024 {
1025 const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "", "b", "", "c" });
1026 defer testing.allocator.free(str);
1027 testing.expect(eql(u8, str, "a,,b,,c"));
1028 }
1019}1029}
10201030
1021/// Copies each T from slices into a new slice that exactly holds all the elements.1031/// Copies each T from slices into a new slice that exactly holds all the elements.
...@@ -1044,15 +1054,21 @@ pub fn concat(allocator: *Allocator, comptime T: type, slices: []const []const T...@@ -1044,15 +1054,21 @@ pub fn concat(allocator: *Allocator, comptime T: type, slices: []const []const T
1044}1054}
10451055
1046test "concat" {1056test "concat" {
1047 var buf: [1024]u8 = undefined;1057 {
1048 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;1058 const str = try concat(testing.allocator, u8, &[_][]const u8{ "abc", "def", "ghi" });
1049 testing.expect(eql(u8, try concat(a, u8, &[_][]const u8{ "abc", "def", "ghi" }), "abcdefghi"));1059 defer testing.allocator.free(str);
1050 testing.expect(eql(u32, try concat(a, u32, &[_][]const u32{1060 testing.expect(eql(u8, str, "abcdefghi"));
1051 &[_]u32{ 0, 1 },1061 }
1052 &[_]u32{ 2, 3, 4 },1062 {
1053 &[_]u32{},1063 const str = try concat(testing.allocator, u32, &[_][]const u32{
1054 &[_]u32{5},1064 &[_]u32{ 0, 1 },
1055 }), &[_]u32{ 0, 1, 2, 3, 4, 5 }));1065 &[_]u32{ 2, 3, 4 },
1066 &[_]u32{},
1067 &[_]u32{5},
1068 });
1069 defer testing.allocator.free(str);
1070 testing.expect(eql(u32, str, &[_]u32{ 0, 1, 2, 3, 4, 5 }));
1071 }
1056}1072}
10571073
1058test "testStringEquality" {1074test "testStringEquality" {
lib/std/meta.zig+64-59
...@@ -7,13 +7,12 @@ const testing = std.testing;...@@ -7,13 +7,12 @@ const testing = std.testing;
77
8pub const trait = @import("meta/trait.zig");8pub const trait = @import("meta/trait.zig");
99
10const TypeId = builtin.TypeId;
11const TypeInfo = builtin.TypeInfo;10const TypeInfo = builtin.TypeInfo;
1211
13pub fn tagName(v: var) []const u8 {12pub fn tagName(v: var) []const u8 {
14 const T = @TypeOf(v);13 const T = @TypeOf(v);
15 switch (@typeInfo(T)) {14 switch (@typeInfo(T)) {
16 TypeId.ErrorSet => return @errorName(v),15 .ErrorSet => return @errorName(v),
17 else => return @tagName(v),16 else => return @tagName(v),
18 }17 }
19}18}
...@@ -55,7 +54,7 @@ test "std.meta.tagName" {...@@ -55,7 +54,7 @@ test "std.meta.tagName" {
5554
56pub fn stringToEnum(comptime T: type, str: []const u8) ?T {55pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
57 inline for (@typeInfo(T).Enum.fields) |enumField| {56 inline for (@typeInfo(T).Enum.fields) |enumField| {
58 if (std.mem.eql(u8, str, enumField.name)) {57 if (mem.eql(u8, str, enumField.name)) {
59 return @field(T, enumField.name);58 return @field(T, enumField.name);
60 }59 }
61 }60 }
...@@ -74,9 +73,9 @@ test "std.meta.stringToEnum" {...@@ -74,9 +73,9 @@ test "std.meta.stringToEnum" {
7473
75pub fn bitCount(comptime T: type) comptime_int {74pub fn bitCount(comptime T: type) comptime_int {
76 return switch (@typeInfo(T)) {75 return switch (@typeInfo(T)) {
77 TypeId.Bool => 1,76 .Bool => 1,
78 TypeId.Int => |info| info.bits,77 .Int => |info| info.bits,
79 TypeId.Float => |info| info.bits,78 .Float => |info| info.bits,
80 else => @compileError("Expected bool, int or float type, found '" ++ @typeName(T) ++ "'"),79 else => @compileError("Expected bool, int or float type, found '" ++ @typeName(T) ++ "'"),
81 };80 };
82}81}
...@@ -88,7 +87,7 @@ test "std.meta.bitCount" {...@@ -88,7 +87,7 @@ test "std.meta.bitCount" {
8887
89pub fn alignment(comptime T: type) comptime_int {88pub fn alignment(comptime T: type) comptime_int {
90 //@alignOf works on non-pointer types89 //@alignOf works on non-pointer types
91 const P = if (comptime trait.is(TypeId.Pointer)(T)) T else *T;90 const P = if (comptime trait.is(.Pointer)(T)) T else *T;
92 return @typeInfo(P).Pointer.alignment;91 return @typeInfo(P).Pointer.alignment;
93}92}
9493
...@@ -102,9 +101,9 @@ test "std.meta.alignment" {...@@ -102,9 +101,9 @@ test "std.meta.alignment" {
102101
103pub fn Child(comptime T: type) type {102pub fn Child(comptime T: type) type {
104 return switch (@typeInfo(T)) {103 return switch (@typeInfo(T)) {
105 TypeId.Array => |info| info.child,104 .Array => |info| info.child,
106 TypeId.Pointer => |info| info.child,105 .Pointer => |info| info.child,
107 TypeId.Optional => |info| info.child,106 .Optional => |info| info.child,
108 else => @compileError("Expected pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"),107 else => @compileError("Expected pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"),
109 };108 };
110}109}
...@@ -118,9 +117,9 @@ test "std.meta.Child" {...@@ -118,9 +117,9 @@ test "std.meta.Child" {
118117
119pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout {118pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout {
120 return switch (@typeInfo(T)) {119 return switch (@typeInfo(T)) {
121 TypeId.Struct => |info| info.layout,120 .Struct => |info| info.layout,
122 TypeId.Enum => |info| info.layout,121 .Enum => |info| info.layout,
123 TypeId.Union => |info| info.layout,122 .Union => |info| info.layout,
124 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),123 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
125 };124 };
126}125}
...@@ -148,22 +147,22 @@ test "std.meta.containerLayout" {...@@ -148,22 +147,22 @@ test "std.meta.containerLayout" {
148 a: u8,147 a: u8,
149 };148 };
150149
151 testing.expect(containerLayout(E1) == TypeInfo.ContainerLayout.Auto);150 testing.expect(containerLayout(E1) == .Auto);
152 testing.expect(containerLayout(E2) == TypeInfo.ContainerLayout.Packed);151 testing.expect(containerLayout(E2) == .Packed);
153 testing.expect(containerLayout(E3) == TypeInfo.ContainerLayout.Extern);152 testing.expect(containerLayout(E3) == .Extern);
154 testing.expect(containerLayout(S1) == TypeInfo.ContainerLayout.Auto);153 testing.expect(containerLayout(S1) == .Auto);
155 testing.expect(containerLayout(S2) == TypeInfo.ContainerLayout.Packed);154 testing.expect(containerLayout(S2) == .Packed);
156 testing.expect(containerLayout(S3) == TypeInfo.ContainerLayout.Extern);155 testing.expect(containerLayout(S3) == .Extern);
157 testing.expect(containerLayout(U1) == TypeInfo.ContainerLayout.Auto);156 testing.expect(containerLayout(U1) == .Auto);
158 testing.expect(containerLayout(U2) == TypeInfo.ContainerLayout.Packed);157 testing.expect(containerLayout(U2) == .Packed);
159 testing.expect(containerLayout(U3) == TypeInfo.ContainerLayout.Extern);158 testing.expect(containerLayout(U3) == .Extern);
160}159}
161160
162pub fn declarations(comptime T: type) []TypeInfo.Declaration {161pub fn declarations(comptime T: type) []TypeInfo.Declaration {
163 return switch (@typeInfo(T)) {162 return switch (@typeInfo(T)) {
164 TypeId.Struct => |info| info.decls,163 .Struct => |info| info.decls,
165 TypeId.Enum => |info| info.decls,164 .Enum => |info| info.decls,
166 TypeId.Union => |info| info.decls,165 .Union => |info| info.decls,
167 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),166 else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
168 };167 };
169}168}
...@@ -232,17 +231,17 @@ test "std.meta.declarationInfo" {...@@ -232,17 +231,17 @@ test "std.meta.declarationInfo" {
232}231}
233232
234pub fn fields(comptime T: type) switch (@typeInfo(T)) {233pub fn fields(comptime T: type) switch (@typeInfo(T)) {
235 TypeId.Struct => []TypeInfo.StructField,234 .Struct => []TypeInfo.StructField,
236 TypeId.Union => []TypeInfo.UnionField,235 .Union => []TypeInfo.UnionField,
237 TypeId.ErrorSet => []TypeInfo.Error,236 .ErrorSet => []TypeInfo.Error,
238 TypeId.Enum => []TypeInfo.EnumField,237 .Enum => []TypeInfo.EnumField,
239 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),238 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
240} {239} {
241 return switch (@typeInfo(T)) {240 return switch (@typeInfo(T)) {
242 TypeId.Struct => |info| info.fields,241 .Struct => |info| info.fields,
243 TypeId.Union => |info| info.fields,242 .Union => |info| info.fields,
244 TypeId.Enum => |info| info.fields,243 .Enum => |info| info.fields,
245 TypeId.ErrorSet => |errors| errors.?, // must be non global error set244 .ErrorSet => |errors| errors.?, // must be non global error set
246 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),245 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
247 };246 };
248}247}
...@@ -277,10 +276,10 @@ test "std.meta.fields" {...@@ -277,10 +276,10 @@ test "std.meta.fields" {
277}276}
278277
279pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) {278pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) {
280 TypeId.Struct => TypeInfo.StructField,279 .Struct => TypeInfo.StructField,
281 TypeId.Union => TypeInfo.UnionField,280 .Union => TypeInfo.UnionField,
282 TypeId.ErrorSet => TypeInfo.Error,281 .ErrorSet => TypeInfo.Error,
283 TypeId.Enum => TypeInfo.EnumField,282 .Enum => TypeInfo.EnumField,
284 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),283 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
285} {284} {
286 inline for (comptime fields(T)) |field| {285 inline for (comptime fields(T)) |field| {
...@@ -318,8 +317,8 @@ test "std.meta.fieldInfo" {...@@ -318,8 +317,8 @@ test "std.meta.fieldInfo" {
318317
319pub fn TagType(comptime T: type) type {318pub fn TagType(comptime T: type) type {
320 return switch (@typeInfo(T)) {319 return switch (@typeInfo(T)) {
321 TypeId.Enum => |info| info.tag_type,320 .Enum => |info| info.tag_type,
322 TypeId.Union => |info| if (info.tag_type) |Tag| Tag else null,321 .Union => |info| if (info.tag_type) |Tag| Tag else null,
323 else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"),322 else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"),
324 };323 };
325}324}
...@@ -365,7 +364,7 @@ test "std.meta.activeTag" {...@@ -365,7 +364,7 @@ test "std.meta.activeTag" {
365///Given a tagged union type, and an enum, return the type of the union364///Given a tagged union type, and an enum, return the type of the union
366/// field corresponding to the enum tag.365/// field corresponding to the enum tag.
367pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {366pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {
368 testing.expect(trait.is(builtin.TypeId.Union)(U));367 testing.expect(trait.is(.Union)(U));
369368
370 const info = @typeInfo(U).Union;369 const info = @typeInfo(U).Union;
371370
...@@ -387,30 +386,26 @@ test "std.meta.TagPayloadType" {...@@ -387,30 +386,26 @@ test "std.meta.TagPayloadType" {
387 testing.expect(MovedEvent == @TypeOf(e.Moved));386 testing.expect(MovedEvent == @TypeOf(e.Moved));
388}387}
389388
390///Compares two of any type for equality. Containers are compared on a field-by-field basis,389/// Compares two of any type for equality. Containers are compared on a field-by-field basis,
391/// where possible. Pointers are not followed.390/// where possible. Pointers are not followed.
392pub fn eql(a: var, b: @TypeOf(a)) bool {391pub fn eql(a: var, b: @TypeOf(a)) bool {
393 const T = @TypeOf(a);392 const T = @TypeOf(a);
394393
395 switch (@typeId(T)) {394 switch (@typeInfo(T)) {
396 builtin.TypeId.Struct => {395 .Struct => |info| {
397 const info = @typeInfo(T).Struct;
398
399 inline for (info.fields) |field_info| {396 inline for (info.fields) |field_info| {
400 if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;397 if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;
401 }398 }
402 return true;399 return true;
403 },400 },
404 builtin.TypeId.ErrorUnion => {401 .ErrorUnion => {
405 if (a) |a_p| {402 if (a) |a_p| {
406 if (b) |b_p| return eql(a_p, b_p) else |_| return false;403 if (b) |b_p| return eql(a_p, b_p) else |_| return false;
407 } else |a_e| {404 } else |a_e| {
408 if (b) |_| return false else |b_e| return a_e == b_e;405 if (b) |_| return false else |b_e| return a_e == b_e;
409 }406 }
410 },407 },
411 builtin.TypeId.Union => {408 .Union => |info| {
412 const info = @typeInfo(T).Union;
413
414 if (info.tag_type) |_| {409 if (info.tag_type) |_| {
415 const tag_a = activeTag(a);410 const tag_a = activeTag(a);
416 const tag_b = activeTag(b);411 const tag_b = activeTag(b);
...@@ -427,23 +422,26 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {...@@ -427,23 +422,26 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {
427422
428 @compileError("cannot compare untagged union type " ++ @typeName(T));423 @compileError("cannot compare untagged union type " ++ @typeName(T));
429 },424 },
430 builtin.TypeId.Array => {425 .Array => {
431 if (a.len != b.len) return false;426 if (a.len != b.len) return false;
432 for (a) |e, i|427 for (a) |e, i|
433 if (!eql(e, b[i])) return false;428 if (!eql(e, b[i])) return false;
434 return true;429 return true;
435 },430 },
436 builtin.TypeId.Pointer => {431 .Vector => |info| {
437 const info = @typeInfo(T).Pointer;432 var i: usize = 0;
438 switch (info.size) {433 while (i < info.len) : (i += 1) {
439 builtin.TypeInfo.Pointer.Size.One,434 if (!eql(a[i], b[i])) return false;
440 builtin.TypeInfo.Pointer.Size.Many,
441 builtin.TypeInfo.Pointer.Size.C,
442 => return a == b,
443 builtin.TypeInfo.Pointer.Size.Slice => return a.ptr == b.ptr and a.len == b.len,
444 }435 }
436 return true;
445 },437 },
446 builtin.TypeId.Optional => {438 .Pointer => |info| {
439 return switch (info.size) {
440 .One, .Many, .C, => a == b,
441 .Slice => a.ptr == b.ptr and a.len == b.len,
442 };
443 },
444 .Optional => {
447 if (a == null and b == null) return true;445 if (a == null and b == null) return true;
448 if (a == null or b == null) return false;446 if (a == null or b == null) return false;
449 return eql(a.?, b.?);447 return eql(a.?, b.?);
...@@ -510,6 +508,13 @@ test "std.meta.eql" {...@@ -510,6 +508,13 @@ test "std.meta.eql" {
510 testing.expect(eql(EU.tst(true), EU.tst(true)));508 testing.expect(eql(EU.tst(true), EU.tst(true)));
511 testing.expect(eql(EU.tst(false), EU.tst(false)));509 testing.expect(eql(EU.tst(false), EU.tst(false)));
512 testing.expect(!eql(EU.tst(false), EU.tst(true)));510 testing.expect(!eql(EU.tst(false), EU.tst(true)));
511
512 var v1 = @splat(4, @as(u32, 1));
513 var v2 = @splat(4, @as(u32, 1));
514 var v3 = @splat(4, @as(u32, 2));
515
516 testing.expect(eql(v1, v2));
517 testing.expect(!eql(v1, v3));
513}518}
514519
515test "intToEnum with error return" {520test "intToEnum with error return" {
lib/std/meta/trait.zig+43-64
...@@ -7,17 +7,11 @@ const warn = debug.warn;...@@ -7,17 +7,11 @@ const warn = debug.warn;
77
8const meta = @import("../meta.zig");8const meta = @import("../meta.zig");
99
10//This is necessary if we want to return generic functions directly because of how the10pub const TraitFn = fn (type) bool;
11// the type erasure works. see: #1375
12fn traitFnWorkaround(comptime T: type) bool {
13 return false;
14}
15
16pub const TraitFn = @TypeOf(traitFnWorkaround);
1711
18//////Trait generators12//////Trait generators
1913
20//Need TraitList because compiler can't do varargs at comptime yet14// TODO convert to tuples when #4335 is done
21pub const TraitList = []const TraitFn;15pub const TraitList = []const TraitFn;
22pub fn multiTrait(comptime traits: TraitList) TraitFn {16pub fn multiTrait(comptime traits: TraitList) TraitFn {
23 const Closure = struct {17 const Closure = struct {
...@@ -60,8 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {...@@ -60,8 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {
60 if (!comptime isContainer(T)) return false;54 if (!comptime isContainer(T)) return false;
61 if (!comptime @hasDecl(T, name)) return false;55 if (!comptime @hasDecl(T, name)) return false;
62 const DeclType = @TypeOf(@field(T, name));56 const DeclType = @TypeOf(@field(T, name));
63 const decl_type_id = @typeId(DeclType);57 return @typeId(DeclType) == .Fn;
64 return decl_type_id == builtin.TypeId.Fn;
65 }58 }
66 };59 };
67 return Closure.trait;60 return Closure.trait;
...@@ -80,11 +73,10 @@ test "std.meta.trait.hasFn" {...@@ -80,11 +73,10 @@ test "std.meta.trait.hasFn" {
80pub fn hasField(comptime name: []const u8) TraitFn {73pub fn hasField(comptime name: []const u8) TraitFn {
81 const Closure = struct {74 const Closure = struct {
82 pub fn trait(comptime T: type) bool {75 pub fn trait(comptime T: type) bool {
83 const info = @typeInfo(T);76 const fields = switch (@typeInfo(T)) {
84 const fields = switch (info) {77 .Struct => |s| s.fields,
85 builtin.TypeId.Struct => |s| s.fields,78 .Union => |u| u.fields,
86 builtin.TypeId.Union => |u| u.fields,79 .Enum => |e| e.fields,
87 builtin.TypeId.Enum => |e| e.fields,
88 else => return false,80 else => return false,
89 };81 };
9082
...@@ -120,11 +112,11 @@ pub fn is(comptime id: builtin.TypeId) TraitFn {...@@ -120,11 +112,11 @@ pub fn is(comptime id: builtin.TypeId) TraitFn {
120}112}
121113
122test "std.meta.trait.is" {114test "std.meta.trait.is" {
123 testing.expect(is(builtin.TypeId.Int)(u8));115 testing.expect(is(.Int)(u8));
124 testing.expect(!is(builtin.TypeId.Int)(f32));116 testing.expect(!is(.Int)(f32));
125 testing.expect(is(builtin.TypeId.Pointer)(*u8));117 testing.expect(is(.Pointer)(*u8));
126 testing.expect(is(builtin.TypeId.Void)(void));118 testing.expect(is(.Void)(void));
127 testing.expect(!is(builtin.TypeId.Optional)(anyerror));119 testing.expect(!is(.Optional)(anyerror));
128}120}
129121
130pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {122pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {
...@@ -138,9 +130,9 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {...@@ -138,9 +130,9 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {
138}130}
139131
140test "std.meta.trait.isPtrTo" {132test "std.meta.trait.isPtrTo" {
141 testing.expect(!isPtrTo(builtin.TypeId.Struct)(struct {}));133 testing.expect(!isPtrTo(.Struct)(struct {}));
142 testing.expect(isPtrTo(builtin.TypeId.Struct)(*struct {}));134 testing.expect(isPtrTo(.Struct)(*struct {}));
143 testing.expect(!isPtrTo(builtin.TypeId.Struct)(**struct {}));135 testing.expect(!isPtrTo(.Struct)(**struct {}));
144}136}
145137
146///////////Strait trait Fns138///////////Strait trait Fns
...@@ -149,12 +141,10 @@ test "std.meta.trait.isPtrTo" {...@@ -149,12 +141,10 @@ test "std.meta.trait.isPtrTo" {
149// Somewhat limited since we can't apply this logic to normal variables, fields, or141// Somewhat limited since we can't apply this logic to normal variables, fields, or
150// Fns yet. Should be isExternType?142// Fns yet. Should be isExternType?
151pub fn isExtern(comptime T: type) bool {143pub fn isExtern(comptime T: type) bool {
152 const Extern = builtin.TypeInfo.ContainerLayout.Extern;144 return switch (@typeInfo(T)) {
153 const info = @typeInfo(T);145 .Struct => |s| s.layout == .Extern,
154 return switch (info) {146 .Union => |u| u.layout == .Extern,
155 builtin.TypeId.Struct => |s| s.layout == Extern,147 .Enum => |e| e.layout == .Extern,
156 builtin.TypeId.Union => |u| u.layout == Extern,
157 builtin.TypeId.Enum => |e| e.layout == Extern,
158 else => false,148 else => false,
159 };149 };
160}150}
...@@ -169,12 +159,10 @@ test "std.meta.trait.isExtern" {...@@ -169,12 +159,10 @@ test "std.meta.trait.isExtern" {
169}159}
170160
171pub fn isPacked(comptime T: type) bool {161pub fn isPacked(comptime T: type) bool {
172 const Packed = builtin.TypeInfo.ContainerLayout.Packed;162 return switch (@typeInfo(T)) {
173 const info = @typeInfo(T);163 .Struct => |s| s.layout == .Packed,
174 return switch (info) {164 .Union => |u| u.layout == .Packed,
175 builtin.TypeId.Struct => |s| s.layout == Packed,165 .Enum => |e| e.layout == .Packed,
176 builtin.TypeId.Union => |u| u.layout == Packed,
177 builtin.TypeId.Enum => |e| e.layout == Packed,
178 else => false,166 else => false,
179 };167 };
180}168}
...@@ -189,8 +177,8 @@ test "std.meta.trait.isPacked" {...@@ -189,8 +177,8 @@ test "std.meta.trait.isPacked" {
189}177}
190178
191pub fn isUnsignedInt(comptime T: type) bool {179pub fn isUnsignedInt(comptime T: type) bool {
192 return switch (@typeId(T)) {180 return switch (@typeInfo(T)) {
193 builtin.TypeId.Int => !@typeInfo(T).Int.is_signed,181 .Int => |i| !i.is_signed,
194 else => false,182 else => false,
195 };183 };
196}184}
...@@ -203,9 +191,9 @@ test "isUnsignedInt" {...@@ -203,9 +191,9 @@ test "isUnsignedInt" {
203}191}
204192
205pub fn isSignedInt(comptime T: type) bool {193pub fn isSignedInt(comptime T: type) bool {
206 return switch (@typeId(T)) {194 return switch (@typeInfo(T)) {
207 builtin.TypeId.ComptimeInt => true,195 .ComptimeInt => true,
208 builtin.TypeId.Int => @typeInfo(T).Int.is_signed,196 .Int => |i| i.is_signed,
209 else => false,197 else => false,
210 };198 };
211}199}
...@@ -218,9 +206,8 @@ test "isSignedInt" {...@@ -218,9 +206,8 @@ test "isSignedInt" {
218}206}
219207
220pub fn isSingleItemPtr(comptime T: type) bool {208pub fn isSingleItemPtr(comptime T: type) bool {
221 if (comptime is(builtin.TypeId.Pointer)(T)) {209 if (comptime is(.Pointer)(T)) {
222 const info = @typeInfo(T);210 return @typeInfo(T).Pointer.size == .One;
223 return info.Pointer.size == builtin.TypeInfo.Pointer.Size.One;
224 }211 }
225 return false;212 return false;
226}213}
...@@ -233,9 +220,8 @@ test "std.meta.trait.isSingleItemPtr" {...@@ -233,9 +220,8 @@ test "std.meta.trait.isSingleItemPtr" {
233}220}
234221
235pub fn isManyItemPtr(comptime T: type) bool {222pub fn isManyItemPtr(comptime T: type) bool {
236 if (comptime is(builtin.TypeId.Pointer)(T)) {223 if (comptime is(.Pointer)(T)) {
237 const info = @typeInfo(T);224 return @typeInfo(T).Pointer.size == .Many;
238 return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Many;
239 }225 }
240 return false;226 return false;
241}227}
...@@ -249,9 +235,8 @@ test "std.meta.trait.isManyItemPtr" {...@@ -249,9 +235,8 @@ test "std.meta.trait.isManyItemPtr" {
249}235}
250236
251pub fn isSlice(comptime T: type) bool {237pub fn isSlice(comptime T: type) bool {
252 if (comptime is(builtin.TypeId.Pointer)(T)) {238 if (comptime is(.Pointer)(T)) {
253 const info = @typeInfo(T);239 return @typeInfo(T).Pointer.size == .Slice;
254 return info.Pointer.size == builtin.TypeInfo.Pointer.Size.Slice;
255 }240 }
256 return false;241 return false;
257}242}
...@@ -264,15 +249,13 @@ test "std.meta.trait.isSlice" {...@@ -264,15 +249,13 @@ test "std.meta.trait.isSlice" {
264}249}
265250
266pub fn isIndexable(comptime T: type) bool {251pub fn isIndexable(comptime T: type) bool {
267 if (comptime is(builtin.TypeId.Pointer)(T)) {252 if (comptime is(.Pointer)(T)) {
268 const info = @typeInfo(T);253 if (@typeInfo(T).Pointer.size == .One) {
269 if (info.Pointer.size == builtin.TypeInfo.Pointer.Size.One) {254 return (comptime is(.Array)(meta.Child(T)));
270 if (comptime is(builtin.TypeId.Array)(meta.Child(T))) return true;
271 return false;
272 }255 }
273 return true;256 return true;
274 }257 }
275 return comptime is(builtin.TypeId.Array)(T);258 return comptime is(.Array)(T);
276}259}
277260
278test "std.meta.trait.isIndexable" {261test "std.meta.trait.isIndexable" {
...@@ -287,7 +270,7 @@ test "std.meta.trait.isIndexable" {...@@ -287,7 +270,7 @@ test "std.meta.trait.isIndexable" {
287270
288pub fn isNumber(comptime T: type) bool {271pub fn isNumber(comptime T: type) bool {
289 return switch (@typeId(T)) {272 return switch (@typeId(T)) {
290 builtin.TypeId.Int, builtin.TypeId.Float, builtin.TypeId.ComptimeInt, builtin.TypeId.ComptimeFloat => true,273 .Int, .Float, .ComptimeInt, .ComptimeFloat => true,
291 else => false,274 else => false,
292 };275 };
293}276}
...@@ -307,9 +290,8 @@ test "std.meta.trait.isNumber" {...@@ -307,9 +290,8 @@ test "std.meta.trait.isNumber" {
307}290}
308291
309pub fn isConstPtr(comptime T: type) bool {292pub fn isConstPtr(comptime T: type) bool {
310 if (!comptime is(builtin.TypeId.Pointer)(T)) return false;293 if (!comptime is(.Pointer)(T)) return false;
311 const info = @typeInfo(T);294 return @typeInfo(T).Pointer.is_const;
312 return info.Pointer.is_const;
313}295}
314296
315test "std.meta.trait.isConstPtr" {297test "std.meta.trait.isConstPtr" {
...@@ -322,11 +304,8 @@ test "std.meta.trait.isConstPtr" {...@@ -322,11 +304,8 @@ test "std.meta.trait.isConstPtr" {
322}304}
323305
324pub fn isContainer(comptime T: type) bool {306pub fn isContainer(comptime T: type) bool {
325 const info = @typeInfo(T);307 return switch (@typeId(T)) {
326 return switch (info) {308 .Struct, .Union, .Enum => true,
327 builtin.TypeId.Struct => true,
328 builtin.TypeId.Union => true,
329 builtin.TypeId.Enum => true,
330 else => false,309 else => false,
331 };310 };
332}311}
lib/std/net/test.zig+1-3
...@@ -67,10 +67,8 @@ test "resolve DNS" {...@@ -67,10 +67,8 @@ test "resolve DNS" {
67 // DNS resolution not implemented on Windows yet.67 // DNS resolution not implemented on Windows yet.
68 return error.SkipZigTest;68 return error.SkipZigTest;
69 }69 }
70 var buf: [1000 * 10]u8 = undefined;
71 const a = &std.heap.FixedBufferAllocator.init(&buf).allocator;
7270
73 const address_list = net.getAddressList(a, "example.com", 80) catch |err| switch (err) {71 const address_list = net.getAddressList(testing.allocator, "example.com", 80) catch |err| switch (err) {
74 // The tests are required to work even when there is no Internet connection,72 // The tests are required to work even when there is no Internet connection,
75 // so some of these errors we must accept and skip the test.73 // so some of these errors we must accept and skip the test.
76 error.UnknownHostName => return error.SkipZigTest,74 error.UnknownHostName => return error.SkipZigTest,
lib/std/os/test.zig+10-14
...@@ -95,8 +95,6 @@ test "cpu count" {...@@ -95,8 +95,6 @@ test "cpu count" {
95}95}
9696
97test "AtomicFile" {97test "AtomicFile" {
98 var buffer: [1024]u8 = undefined;
99 const allocator = &std.heap.FixedBufferAllocator.init(buffer[0..]).allocator;
100 const test_out_file = "tmp_atomic_file_test_dest.txt";98 const test_out_file = "tmp_atomic_file_test_dest.txt";
101 const test_content =99 const test_content =
102 \\ hello!100 \\ hello!
...@@ -108,7 +106,8 @@ test "AtomicFile" {...@@ -108,7 +106,8 @@ test "AtomicFile" {
108 try af.file.write(test_content);106 try af.file.write(test_content);
109 try af.finish();107 try af.finish();
110 }108 }
111 const content = try io.readFileAlloc(allocator, test_out_file);109 const content = try io.readFileAlloc(testing.allocator, test_out_file);
110 defer testing.allocator.free(content);
112 expect(mem.eql(u8, content, test_content));111 expect(mem.eql(u8, content, test_content));
113112
114 try fs.cwd().deleteFile(test_out_file);113 try fs.cwd().deleteFile(test_out_file);
...@@ -276,8 +275,11 @@ test "mmap" {...@@ -276,8 +275,11 @@ test "mmap" {
276 testing.expectEqual(@as(usize, 1234), data.len);275 testing.expectEqual(@as(usize, 1234), data.len);
277276
278 // By definition the data returned by mmap is zero-filled277 // By definition the data returned by mmap is zero-filled
279 std.mem.set(u8, data[0 .. data.len - 1], 0x55);278 testing.expect(mem.eql(u8, data, &[_]u8{0x00} ** 1234));
280 testing.expect(mem.indexOfScalar(u8, data, 0).? == 1234 - 1);279
280 // Make sure the memory is writeable as requested
281 std.mem.set(u8, data, 0x55);
282 testing.expect(mem.eql(u8, data, &[_]u8{0x55} ** 1234));
281 }283 }
282284
283 const test_out_file = "os_tmp_test";285 const test_out_file = "os_tmp_test";
...@@ -300,10 +302,7 @@ test "mmap" {...@@ -300,10 +302,7 @@ test "mmap" {
300302
301 // Map the whole file303 // Map the whole file
302 {304 {
303 const file = try fs.cwd().createFile(test_out_file, .{305 const file = try fs.cwd().openFile(test_out_file, .{});
304 .read = true,
305 .truncate = false,
306 });
307 defer file.close();306 defer file.close();
308307
309 const data = try os.mmap(308 const data = try os.mmap(
...@@ -327,15 +326,12 @@ test "mmap" {...@@ -327,15 +326,12 @@ test "mmap" {
327326
328 // Map the upper half of the file327 // Map the upper half of the file
329 {328 {
330 const file = try fs.cwd().createFile(test_out_file, .{329 const file = try fs.cwd().openFile(test_out_file, .{});
331 .read = true,
332 .truncate = false,
333 });
334 defer file.close();330 defer file.close();
335331
336 const data = try os.mmap(332 const data = try os.mmap(
337 null,333 null,
338 alloc_size,334 alloc_size / 2,
339 os.PROT_READ,335 os.PROT_READ,
340 os.MAP_PRIVATE,336 os.MAP_PRIVATE,
341 file.handle,337 file.handle,
lib/std/process.zig+2-4
...@@ -27,10 +27,8 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {...@@ -27,10 +27,8 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
27}27}
2828
29test "getCwdAlloc" {29test "getCwdAlloc" {
30 // at least call it so it gets compiled30 const cwd = try getCwdAlloc(testing.allocator);
31 var buf: [1000]u8 = undefined;31 testing.allocator.free(cwd);
32 const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
33 _ = getCwdAlloc(allocator) catch undefined;
34}32}
3533
36/// Caller must free result when done.34/// Caller must free result when done.
lib/std/sort.zig+4-4
...@@ -1220,16 +1220,16 @@ test "sort fuzz testing" {...@@ -1220,16 +1220,16 @@ test "sort fuzz testing" {
1220 const test_case_count = 10;1220 const test_case_count = 10;
1221 var i: usize = 0;1221 var i: usize = 0;
1222 while (i < test_case_count) : (i += 1) {1222 while (i < test_case_count) : (i += 1) {
1223 fuzzTest(&prng.random);1223 try fuzzTest(&prng.random);
1224 }1224 }
1225}1225}
12261226
1227var fixed_buffer_mem: [100 * 1024]u8 = undefined;1227var fixed_buffer_mem: [100 * 1024]u8 = undefined;
12281228
1229fn fuzzTest(rng: *std.rand.Random) void {1229fn fuzzTest(rng: *std.rand.Random) !void {
1230 const array_size = rng.range(usize, 0, 1000);1230 const array_size = rng.range(usize, 0, 1000);
1231 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);1231 var array = try testing.allocator.alloc(IdAndValue, array_size);
1232 var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable;1232 defer testing.allocator.free(array);
1233 // populate with random data1233 // populate with random data
1234 for (array) |*item, index| {1234 for (array) |*item, index| {
1235 item.id = index;1235 item.id = index;
lib/std/special/compiler_rt.zig+1-1
...@@ -149,7 +149,7 @@ comptime {...@@ -149,7 +149,7 @@ comptime {
149149
150 @export(@import("compiler_rt/clzsi2.zig").__clzsi2, .{ .name = "__clzsi2", .linkage = linkage });150 @export(@import("compiler_rt/clzsi2.zig").__clzsi2, .{ .name = "__clzsi2", .linkage = linkage });
151151
152 if (builtin.arch.isARM() and !is_test) {152 if ((builtin.arch.isARM() or builtin.arch.isThumb()) and !is_test) {
153 @export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr0, .{ .name = "__aeabi_unwind_cpp_pr0", .linkage = linkage });153 @export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr0, .{ .name = "__aeabi_unwind_cpp_pr0", .linkage = linkage });
154 @export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr1, .{ .name = "__aeabi_unwind_cpp_pr1", .linkage = linkage });154 @export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr1, .{ .name = "__aeabi_unwind_cpp_pr1", .linkage = linkage });
155 @export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr2, .{ .name = "__aeabi_unwind_cpp_pr2", .linkage = linkage });155 @export(@import("compiler_rt/arm.zig").__aeabi_unwind_cpp_pr2, .{ .name = "__aeabi_unwind_cpp_pr2", .linkage = linkage });
lib/std/target/riscv.zig-2
...@@ -270,7 +270,6 @@ pub const cpu = struct {...@@ -270,7 +270,6 @@ pub const cpu = struct {
270 .d,270 .d,
271 .f,271 .f,
272 .m,272 .m,
273 .relax,
274 }),273 }),
275 };274 };
276275
...@@ -284,7 +283,6 @@ pub const cpu = struct {...@@ -284,7 +283,6 @@ pub const cpu = struct {
284 .d,283 .d,
285 .f,284 .f,
286 .m,285 .m,
287 .relax,
288 }),286 }),
289 };287 };
290288
lib/std/testing.zig+17-2
...@@ -12,7 +12,7 @@ pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.al...@@ -12,7 +12,7 @@ pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.al
12pub const failing_allocator = &FailingAllocator.init(&base_allocator_instance.allocator, 0).allocator;12pub const failing_allocator = &FailingAllocator.init(&base_allocator_instance.allocator, 0).allocator;
1313
14pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);14pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
15var allocator_mem: [512 * 1024]u8 = undefined;15var allocator_mem: [1024 * 1024]u8 = undefined;
1616
17/// This function is intended to be used only in tests. It prints diagnostics to stderr17/// This function is intended to be used only in tests. It prints diagnostics to stderr
18/// and then aborts when actual_error_union is not expected_error.18/// and then aborts when actual_error_union is not expected_error.
...@@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {...@@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
56 .EnumLiteral,56 .EnumLiteral,
57 .Enum,57 .Enum,
58 .Fn,58 .Fn,
59 .Vector,
60 .ErrorSet,59 .ErrorSet,
61 => {60 => {
62 if (actual != expected) {61 if (actual != expected) {
...@@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {...@@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
8887
89 .Array => |array| expectEqualSlices(array.child, &expected, &actual),88 .Array => |array| expectEqualSlices(array.child, &expected, &actual),
9089
90 .Vector => |vectorType| {
91 var i: usize = 0;
92 while (i < vectorType.len) : (i += 1) {
93 if (!std.meta.eql(expected[i], actual[i])) {
94 std.debug.panic("index {} incorrect. expected {}, found {}", .{ i, expected[i], actual[i] });
95 }
96 }
97 },
98
91 .Struct => |structType| {99 .Struct => |structType| {
92 inline for (structType.fields) |field| {100 inline for (structType.fields) |field| {
93 expectEqual(@field(expected, field.name), @field(actual, field.name));101 expectEqual(@field(expected, field.name), @field(actual, field.name));
...@@ -202,3 +210,10 @@ test "expectEqual nested array" {...@@ -202,3 +210,10 @@ test "expectEqual nested array" {
202210
203 expectEqual(a, b);211 expectEqual(a, b);
204}212}
213
214test "expectEqual vector" {
215 var a = @splat(4, @as(u32, 4));
216 var b = @splat(4, @as(u32, 4));
217
218 expectEqual(a, b);
219}
lib/std/unicode.zig+4-6
...@@ -617,16 +617,14 @@ test "utf8ToUtf16Le" {...@@ -617,16 +617,14 @@ test "utf8ToUtf16Le" {
617617
618test "utf8ToUtf16LeWithNull" {618test "utf8ToUtf16LeWithNull" {
619 {619 {
620 var bytes: [128]u8 = undefined;620 const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "𐐷");
621 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;621 defer testing.allocator.free(utf16);
622 const utf16 = try utf8ToUtf16LeWithNull(allocator, "𐐷");
623 testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..]));622 testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..]));
624 testing.expect(utf16[2] == 0);623 testing.expect(utf16[2] == 0);
625 }624 }
626 {625 {
627 var bytes: [128]u8 = undefined;626 const utf16 = try utf8ToUtf16LeWithNull(testing.allocator, "\u{10FFFF}");
628 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;627 defer testing.allocator.free(utf16);
629 const utf16 = try utf8ToUtf16LeWithNull(allocator, "\u{10FFFF}");
630 testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..]));628 testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..]));
631 testing.expect(utf16[2] == 0);629 testing.expect(utf16[2] == 0);
632 }630 }
lib/std/zig/parser_test.zig+1-2
...@@ -2886,8 +2886,7 @@ fn testCanonical(source: []const u8) !void {...@@ -2886,8 +2886,7 @@ fn testCanonical(source: []const u8) !void {
2886}2886}
28872887
2888fn testError(source: []const u8) !void {2888fn testError(source: []const u8) !void {
2889 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);2889 const tree = try std.zig.parse(std.testing.allocator, source);
2890 const tree = try std.zig.parse(&fixed_allocator.allocator, source);
2891 defer tree.deinit();2890 defer tree.deinit();
28922891
2893 std.testing.expect(tree.errors.len != 0);2892 std.testing.expect(tree.errors.len != 0);
src-self-hosted/translate_c.zig+25-8
...@@ -5123,6 +5123,8 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl...@@ -5123,6 +5123,8 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
5123 cast_node.rparen_token = try appendToken(c, .RParen, ")");5123 cast_node.rparen_token = try appendToken(c, .RParen, ")");
5124 return &cast_node.base;5124 return &cast_node.base;
5125 } else if (tok.id == .FloatLiteral) {5125 } else if (tok.id == .FloatLiteral) {
5126 if (lit_bytes[0] == '.')
5127 lit_bytes = try std.fmt.allocPrint(c.a(), "0{}", .{lit_bytes});
5126 if (tok.id.FloatLiteral == .None) {5128 if (tok.id.FloatLiteral == .None) {
5127 return transCreateNodeFloat(c, lit_bytes);5129 return transCreateNodeFloat(c, lit_bytes);
5128 }5130 }
...@@ -5340,12 +5342,27 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5340,12 +5342,27 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5340 .LParen => {5342 .LParen => {
5341 const inner_node = try parseCExpr(c, it, source, source_loc, scope);5343 const inner_node = try parseCExpr(c, it, source, source_loc, scope);
53425344
5343 if (it.peek().?.id == .RParen) {5345 if (it.next().?.id != .RParen) {
5344 _ = it.next();5346 const first_tok = it.list.at(0);
5345 if (it.peek().?.id != .LParen) {5347 try failDecl(
5346 return inner_node;5348 c,
5347 }5349 source_loc,
5348 _ = it.next();5350 source[first_tok.start..first_tok.end],
5351 "unable to translate C expr: expected ')'' here",
5352 .{},
5353 );
5354 return error.ParseError;
5355 }
5356 var saw_l_paren = false;
5357 switch (it.peek().?.id) {
5358 // (type)(to_cast)
5359 .LParen => {
5360 saw_l_paren = true;
5361 _ = it.next();
5362 },
5363 // (type)identifier
5364 .Identifier => {},
5365 else => return inner_node,
5349 }5366 }
53505367
5351 // hack to get zig fmt to render a comma in builtin calls5368 // hack to get zig fmt to render a comma in builtin calls
...@@ -5353,7 +5370,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5353,7 +5370,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
53535370
5354 const node_to_cast = try parseCExpr(c, it, source, source_loc, scope);5371 const node_to_cast = try parseCExpr(c, it, source, source_loc, scope);
53555372
5356 if (it.next().?.id != .RParen) {5373 if (saw_l_paren and it.next().?.id != .RParen) {
5357 const first_tok = it.list.at(0);5374 const first_tok = it.list.at(0);
5358 try failDecl(5375 try failDecl(
5359 c,5376 c,
...@@ -5494,7 +5511,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5494,7 +5511,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5494 // hack to get zig fmt to render a comma in builtin calls5511 // hack to get zig fmt to render a comma in builtin calls
5495 _ = try appendToken(c, .Comma, ",");5512 _ = try appendToken(c, .Comma, ",");
54965513
5497 const ptr_kind = blk:{5514 const ptr_kind = blk: {
5498 // * token5515 // * token
5499 _ = it.prev();5516 _ = it.prev();
5500 // last token of `node`5517 // last token of `node`
src/all_types.hpp+3-1
...@@ -1999,6 +1999,9 @@ struct CFile {...@@ -1999,6 +1999,9 @@ struct CFile {
19991999
2000// When adding fields, check if they should be added to the hash computation in build_with_cache2000// When adding fields, check if they should be added to the hash computation in build_with_cache
2001struct CodeGen {2001struct CodeGen {
2002 // arena allocator destroyed just prior to codegen emit
2003 heap::ArenaAllocator *pass1_arena;
2004
2002 //////////////////////////// Runtime State2005 //////////////////////////// Runtime State
2003 LLVMModuleRef module;2006 LLVMModuleRef module;
2004 ZigList<ErrorMsg*> errors;2007 ZigList<ErrorMsg*> errors;
...@@ -2279,7 +2282,6 @@ struct ZigVar {...@@ -2279,7 +2282,6 @@ struct ZigVar {
2279 Scope *parent_scope;2282 Scope *parent_scope;
2280 Scope *child_scope;2283 Scope *child_scope;
2281 LLVMValueRef param_value_ref;2284 LLVMValueRef param_value_ref;
2282 IrExecutableSrc *owner_exec;
22832285
2284 Buf *section_name;2286 Buf *section_name;
22852287
src/analyze.cpp+94-99
...@@ -80,7 +80,7 @@ ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node,...@@ -80,7 +80,7 @@ ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node,
80}80}
8181
82ZigType *new_type_table_entry(ZigTypeId id) {82ZigType *new_type_table_entry(ZigTypeId id) {
83 ZigType *entry = allocate<ZigType>(1);83 ZigType *entry = heap::c_allocator.create<ZigType>();
84 entry->id = id;84 entry->id = id;
85 return entry;85 return entry;
86}86}
...@@ -140,7 +140,7 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope...@@ -140,7 +140,7 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope
140static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type,140static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type,
141 ZigType *import, Buf *bare_name)141 ZigType *import, Buf *bare_name)
142{142{
143 ScopeDecls *scope = allocate<ScopeDecls>(1);143 ScopeDecls *scope = heap::c_allocator.create<ScopeDecls>();
144 init_scope(g, &scope->base, ScopeIdDecls, node, parent);144 init_scope(g, &scope->base, ScopeIdDecls, node, parent);
145 scope->decl_table.init(4);145 scope->decl_table.init(4);
146 scope->container_type = container_type;146 scope->container_type = container_type;
...@@ -151,7 +151,7 @@ static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent,...@@ -151,7 +151,7 @@ static ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent,
151151
152ScopeBlock *create_block_scope(CodeGen *g, AstNode *node, Scope *parent) {152ScopeBlock *create_block_scope(CodeGen *g, AstNode *node, Scope *parent) {
153 assert(node->type == NodeTypeBlock);153 assert(node->type == NodeTypeBlock);
154 ScopeBlock *scope = allocate<ScopeBlock>(1);154 ScopeBlock *scope = heap::c_allocator.create<ScopeBlock>();
155 init_scope(g, &scope->base, ScopeIdBlock, node, parent);155 init_scope(g, &scope->base, ScopeIdBlock, node, parent);
156 scope->name = node->data.block.name;156 scope->name = node->data.block.name;
157 return scope;157 return scope;
...@@ -159,20 +159,20 @@ ScopeBlock *create_block_scope(CodeGen *g, AstNode *node, Scope *parent) {...@@ -159,20 +159,20 @@ ScopeBlock *create_block_scope(CodeGen *g, AstNode *node, Scope *parent) {
159159
160ScopeDefer *create_defer_scope(CodeGen *g, AstNode *node, Scope *parent) {160ScopeDefer *create_defer_scope(CodeGen *g, AstNode *node, Scope *parent) {
161 assert(node->type == NodeTypeDefer);161 assert(node->type == NodeTypeDefer);
162 ScopeDefer *scope = allocate<ScopeDefer>(1);162 ScopeDefer *scope = heap::c_allocator.create<ScopeDefer>();
163 init_scope(g, &scope->base, ScopeIdDefer, node, parent);163 init_scope(g, &scope->base, ScopeIdDefer, node, parent);
164 return scope;164 return scope;
165}165}
166166
167ScopeDeferExpr *create_defer_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {167ScopeDeferExpr *create_defer_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {
168 assert(node->type == NodeTypeDefer);168 assert(node->type == NodeTypeDefer);
169 ScopeDeferExpr *scope = allocate<ScopeDeferExpr>(1);169 ScopeDeferExpr *scope = heap::c_allocator.create<ScopeDeferExpr>();
170 init_scope(g, &scope->base, ScopeIdDeferExpr, node, parent);170 init_scope(g, &scope->base, ScopeIdDeferExpr, node, parent);
171 return scope;171 return scope;
172}172}
173173
174Scope *create_var_scope(CodeGen *g, AstNode *node, Scope *parent, ZigVar *var) {174Scope *create_var_scope(CodeGen *g, AstNode *node, Scope *parent, ZigVar *var) {
175 ScopeVarDecl *scope = allocate<ScopeVarDecl>(1);175 ScopeVarDecl *scope = heap::c_allocator.create<ScopeVarDecl>();
176 init_scope(g, &scope->base, ScopeIdVarDecl, node, parent);176 init_scope(g, &scope->base, ScopeIdVarDecl, node, parent);
177 scope->var = var;177 scope->var = var;
178 return &scope->base;178 return &scope->base;
...@@ -180,14 +180,14 @@ Scope *create_var_scope(CodeGen *g, AstNode *node, Scope *parent, ZigVar *var) {...@@ -180,14 +180,14 @@ Scope *create_var_scope(CodeGen *g, AstNode *node, Scope *parent, ZigVar *var) {
180180
181ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent) {181ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent) {
182 assert(node->type == NodeTypeFnCallExpr);182 assert(node->type == NodeTypeFnCallExpr);
183 ScopeCImport *scope = allocate<ScopeCImport>(1);183 ScopeCImport *scope = heap::c_allocator.create<ScopeCImport>();
184 init_scope(g, &scope->base, ScopeIdCImport, node, parent);184 init_scope(g, &scope->base, ScopeIdCImport, node, parent);
185 buf_resize(&scope->buf, 0);185 buf_resize(&scope->buf, 0);
186 return scope;186 return scope;
187}187}
188188
189ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent) {189ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent) {
190 ScopeLoop *scope = allocate<ScopeLoop>(1);190 ScopeLoop *scope = heap::c_allocator.create<ScopeLoop>();
191 init_scope(g, &scope->base, ScopeIdLoop, node, parent);191 init_scope(g, &scope->base, ScopeIdLoop, node, parent);
192 if (node->type == NodeTypeWhileExpr) {192 if (node->type == NodeTypeWhileExpr) {
193 scope->name = node->data.while_expr.name;193 scope->name = node->data.while_expr.name;
...@@ -200,7 +200,7 @@ ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent) {...@@ -200,7 +200,7 @@ ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent) {
200}200}
201201
202Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstSrc *is_comptime) {202Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstSrc *is_comptime) {
203 ScopeRuntime *scope = allocate<ScopeRuntime>(1);203 ScopeRuntime *scope = heap::c_allocator.create<ScopeRuntime>();
204 scope->is_comptime = is_comptime;204 scope->is_comptime = is_comptime;
205 init_scope(g, &scope->base, ScopeIdRuntime, node, parent);205 init_scope(g, &scope->base, ScopeIdRuntime, node, parent);
206 return &scope->base;206 return &scope->base;
...@@ -208,37 +208,37 @@ Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstSrc...@@ -208,37 +208,37 @@ Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstSrc
208208
209ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent) {209ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent) {
210 assert(node->type == NodeTypeSuspend);210 assert(node->type == NodeTypeSuspend);
211 ScopeSuspend *scope = allocate<ScopeSuspend>(1);211 ScopeSuspend *scope = heap::c_allocator.create<ScopeSuspend>();
212 init_scope(g, &scope->base, ScopeIdSuspend, node, parent);212 init_scope(g, &scope->base, ScopeIdSuspend, node, parent);
213 return scope;213 return scope;
214}214}
215215
216ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry) {216ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry) {
217 ScopeFnDef *scope = allocate<ScopeFnDef>(1);217 ScopeFnDef *scope = heap::c_allocator.create<ScopeFnDef>();
218 init_scope(g, &scope->base, ScopeIdFnDef, node, parent);218 init_scope(g, &scope->base, ScopeIdFnDef, node, parent);
219 scope->fn_entry = fn_entry;219 scope->fn_entry = fn_entry;
220 return scope;220 return scope;
221}221}
222222
223Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) {223Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent) {
224 ScopeCompTime *scope = allocate<ScopeCompTime>(1);224 ScopeCompTime *scope = heap::c_allocator.create<ScopeCompTime>();
225 init_scope(g, &scope->base, ScopeIdCompTime, node, parent);225 init_scope(g, &scope->base, ScopeIdCompTime, node, parent);
226 return &scope->base;226 return &scope->base;
227}227}
228228
229Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent) {229Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent) {
230 ScopeTypeOf *scope = allocate<ScopeTypeOf>(1);230 ScopeTypeOf *scope = heap::c_allocator.create<ScopeTypeOf>();
231 init_scope(g, &scope->base, ScopeIdTypeOf, node, parent);231 init_scope(g, &scope->base, ScopeIdTypeOf, node, parent);
232 return &scope->base;232 return &scope->base;
233}233}
234234
235ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {235ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent) {
236 ScopeExpr *scope = allocate<ScopeExpr>(1);236 ScopeExpr *scope = heap::c_allocator.create<ScopeExpr>();
237 init_scope(g, &scope->base, ScopeIdExpr, node, parent);237 init_scope(g, &scope->base, ScopeIdExpr, node, parent);
238 ScopeExpr *parent_expr = find_expr_scope(parent);238 ScopeExpr *parent_expr = find_expr_scope(parent);
239 if (parent_expr != nullptr) {239 if (parent_expr != nullptr) {
240 size_t new_len = parent_expr->children_len + 1;240 size_t new_len = parent_expr->children_len + 1;
241 parent_expr->children_ptr = reallocate_nonzero<ScopeExpr *>(241 parent_expr->children_ptr = heap::c_allocator.reallocate_nonzero<ScopeExpr *>(
242 parent_expr->children_ptr, parent_expr->children_len, new_len);242 parent_expr->children_ptr, parent_expr->children_len, new_len);
243 parent_expr->children_ptr[parent_expr->children_len] = scope;243 parent_expr->children_ptr[parent_expr->children_len] = scope;
244 parent_expr->children_len = new_len;244 parent_expr->children_len = new_len;
...@@ -1104,8 +1104,8 @@ ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *...@@ -1104,8 +1104,8 @@ ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *
1104{1104{
1105 Error err;1105 Error err;
11061106
1107 ZigValue *result = create_const_vals(1);1107 ZigValue *result = g->pass1_arena->create<ZigValue>();
1108 ZigValue *result_ptr = create_const_vals(1);1108 ZigValue *result_ptr = g->pass1_arena->create<ZigValue>();
1109 result->special = ConstValSpecialUndef;1109 result->special = ConstValSpecialUndef;
1110 result->type = (type_entry == nullptr) ? g->builtin_types.entry_var : type_entry;1110 result->type = (type_entry == nullptr) ? g->builtin_types.entry_var : type_entry;
1111 result_ptr->special = ConstValSpecialStatic;1111 result_ptr->special = ConstValSpecialStatic;
...@@ -1122,7 +1122,6 @@ ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *...@@ -1122,7 +1122,6 @@ ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *
1122 {1122 {
1123 return g->invalid_inst_gen->value;1123 return g->invalid_inst_gen->value;
1124 }1124 }
1125 destroy(result_ptr, "ZigValue");
1126 return result;1125 return result;
1127}1126}
11281127
...@@ -1507,7 +1506,7 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, CallingConventio...@@ -1507,7 +1506,7 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, CallingConventio
15071506
1508 fn_type_id->cc = cc;1507 fn_type_id->cc = cc;
1509 fn_type_id->param_count = fn_proto->params.length;1508 fn_type_id->param_count = fn_proto->params.length;
1510 fn_type_id->param_info = allocate<FnTypeParamInfo>(param_count_alloc);1509 fn_type_id->param_info = heap::c_allocator.allocate<FnTypeParamInfo>(param_count_alloc);
1511 fn_type_id->next_param_index = 0;1510 fn_type_id->next_param_index = 0;
1512 fn_type_id->is_var_args = fn_proto->is_var_args;1511 fn_type_id->is_var_args = fn_proto->is_var_args;
1513}1512}
...@@ -2171,7 +2170,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -2171,7 +2170,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
2171 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);2170 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
2172 struct_type->data.structure.resolve_loop_flag_other = true;2171 struct_type->data.structure.resolve_loop_flag_other = true;
21732172
2174 uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;2173 uint32_t *host_int_bytes = packed ? heap::c_allocator.allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;
21752174
2176 size_t packed_bits_offset = 0;2175 size_t packed_bits_offset = 0;
2177 size_t next_offset = 0;2176 size_t next_offset = 0;
...@@ -2657,7 +2656,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2657,7 +2656,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2657 }2656 }
26582657
2659 enum_type->data.enumeration.src_field_count = field_count;2658 enum_type->data.enumeration.src_field_count = field_count;
2660 enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);2659 enum_type->data.enumeration.fields = heap::c_allocator.allocate<TypeEnumField>(field_count);
2661 enum_type->data.enumeration.fields_by_name.init(field_count);2660 enum_type->data.enumeration.fields_by_name.init(field_count);
26622661
2663 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};2662 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};
...@@ -3034,7 +3033,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3034,7 +3033,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3034 return ErrorSemanticAnalyzeFail;3033 return ErrorSemanticAnalyzeFail;
3035 }3034 }
3036 union_type->data.unionation.src_field_count = field_count;3035 union_type->data.unionation.src_field_count = field_count;
3037 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);3036 union_type->data.unionation.fields = heap::c_allocator.allocate<TypeUnionField>(field_count);
3038 union_type->data.unionation.fields_by_name.init(field_count);3037 union_type->data.unionation.fields_by_name.init(field_count);
30393038
3040 Scope *scope = &union_type->data.unionation.decls_scope->base;3039 Scope *scope = &union_type->data.unionation.decls_scope->base;
...@@ -3053,7 +3052,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3053,7 +3052,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3053 if (create_enum_type) {3052 if (create_enum_type) {
3054 occupied_tag_values.init(field_count);3053 occupied_tag_values.init(field_count);
30553054
3056 di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);3055 di_enumerators = heap::c_allocator.allocate<ZigLLVMDIEnumerator*>(field_count);
30573056
3058 ZigType *tag_int_type;3057 ZigType *tag_int_type;
3059 if (enum_type_node != nullptr) {3058 if (enum_type_node != nullptr) {
...@@ -3086,7 +3085,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3086,7 +3085,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3086 tag_type->data.enumeration.decl_node = decl_node;3085 tag_type->data.enumeration.decl_node = decl_node;
3087 tag_type->data.enumeration.layout = ContainerLayoutAuto;3086 tag_type->data.enumeration.layout = ContainerLayoutAuto;
3088 tag_type->data.enumeration.src_field_count = field_count;3087 tag_type->data.enumeration.src_field_count = field_count;
3089 tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);3088 tag_type->data.enumeration.fields = heap::c_allocator.allocate<TypeEnumField>(field_count);
3090 tag_type->data.enumeration.fields_by_name.init(field_count);3089 tag_type->data.enumeration.fields_by_name.init(field_count);
3091 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;3090 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;
3092 } else if (enum_type_node != nullptr) {3091 } else if (enum_type_node != nullptr) {
...@@ -3106,7 +3105,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3106,7 +3105,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3106 return err;3105 return err;
3107 }3106 }
3108 tag_type = enum_type;3107 tag_type = enum_type;
3109 covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count);3108 covered_enum_fields = heap::c_allocator.allocate<bool>(enum_type->data.enumeration.src_field_count);
3110 } else {3109 } else {
3111 tag_type = nullptr;3110 tag_type = nullptr;
3112 }3111 }
...@@ -3244,7 +3243,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3244,7 +3243,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3244 }3243 }
3245 covered_enum_fields[union_field->enum_field->decl_index] = true;3244 covered_enum_fields[union_field->enum_field->decl_index] = true;
3246 } else {3245 } else {
3247 union_field->enum_field = allocate<TypeEnumField>(1);3246 union_field->enum_field = heap::c_allocator.create<TypeEnumField>();
3248 union_field->enum_field->name = field_name;3247 union_field->enum_field->name = field_name;
3249 union_field->enum_field->decl_index = i;3248 union_field->enum_field->decl_index = i;
3250 bigint_init_unsigned(&union_field->enum_field->value, i);3249 bigint_init_unsigned(&union_field->enum_field->value, i);
...@@ -3366,8 +3365,8 @@ static void get_fully_qualified_decl_name(CodeGen *g, Buf *buf, Tld *tld, bool i...@@ -3366,8 +3365,8 @@ static void get_fully_qualified_decl_name(CodeGen *g, Buf *buf, Tld *tld, bool i
3366}3365}
33673366
3368ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value) {3367ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value) {
3369 ZigFn *fn_entry = allocate<ZigFn>(1, "ZigFn");3368 ZigFn *fn_entry = heap::c_allocator.create<ZigFn>();
3370 fn_entry->ir_executable = allocate<IrExecutableSrc>(1, "IrExecutableSrc");3369 fn_entry->ir_executable = heap::c_allocator.create<IrExecutableSrc>();
33713370
3372 fn_entry->prealloc_backward_branch_quota = default_backward_branch_quota;3371 fn_entry->prealloc_backward_branch_quota = default_backward_branch_quota;
33733372
...@@ -3642,7 +3641,7 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope...@@ -3642,7 +3641,7 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope
3642 return;3641 return;
3643 }3642 }
36443643
3645 TldFn *tld_fn = allocate<TldFn>(1);3644 TldFn *tld_fn = heap::c_allocator.create<TldFn>();
3646 init_tld(&tld_fn->base, TldIdFn, test_name, VisibModPrivate, node, &decls_scope->base);3645 init_tld(&tld_fn->base, TldIdFn, test_name, VisibModPrivate, node, &decls_scope->base);
3647 g->resolve_queue.append(&tld_fn->base);3646 g->resolve_queue.append(&tld_fn->base);
3648}3647}
...@@ -3650,7 +3649,7 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope...@@ -3650,7 +3649,7 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope
3650static void preview_comptime_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope) {3649static void preview_comptime_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope) {
3651 assert(node->type == NodeTypeCompTime);3650 assert(node->type == NodeTypeCompTime);
36523651
3653 TldCompTime *tld_comptime = allocate<TldCompTime>(1);3652 TldCompTime *tld_comptime = heap::c_allocator.create<TldCompTime>();
3654 init_tld(&tld_comptime->base, TldIdCompTime, nullptr, VisibModPrivate, node, &decls_scope->base);3653 init_tld(&tld_comptime->base, TldIdCompTime, nullptr, VisibModPrivate, node, &decls_scope->base);
3655 g->resolve_queue.append(&tld_comptime->base);3654 g->resolve_queue.append(&tld_comptime->base);
3656}3655}
...@@ -3673,7 +3672,7 @@ void update_compile_var(CodeGen *g, Buf *name, ZigValue *value) {...@@ -3673,7 +3672,7 @@ void update_compile_var(CodeGen *g, Buf *name, ZigValue *value) {
3673 resolve_top_level_decl(g, tld, tld->source_node, false);3672 resolve_top_level_decl(g, tld, tld->source_node, false);
3674 assert(tld->id == TldIdVar && tld->resolution == TldResolutionOk);3673 assert(tld->id == TldIdVar && tld->resolution == TldResolutionOk);
3675 TldVar *tld_var = (TldVar *)tld;3674 TldVar *tld_var = (TldVar *)tld;
3676 copy_const_val(tld_var->var->const_value, value);3675 copy_const_val(g, tld_var->var->const_value, value);
3677 tld_var->var->var_type = value->type;3676 tld_var->var->var_type = value->type;
3678 tld_var->var->align_bytes = get_abi_alignment(g, value->type);3677 tld_var->var->align_bytes = get_abi_alignment(g, value->type);
3679}3678}
...@@ -3693,7 +3692,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -3693,7 +3692,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
3693 {3692 {
3694 Buf *name = node->data.variable_declaration.symbol;3693 Buf *name = node->data.variable_declaration.symbol;
3695 VisibMod visib_mod = node->data.variable_declaration.visib_mod;3694 VisibMod visib_mod = node->data.variable_declaration.visib_mod;
3696 TldVar *tld_var = allocate<TldVar>(1);3695 TldVar *tld_var = heap::c_allocator.create<TldVar>();
3697 init_tld(&tld_var->base, TldIdVar, name, visib_mod, node, &decls_scope->base);3696 init_tld(&tld_var->base, TldIdVar, name, visib_mod, node, &decls_scope->base);
3698 tld_var->extern_lib_name = node->data.variable_declaration.lib_name;3697 tld_var->extern_lib_name = node->data.variable_declaration.lib_name;
3699 add_top_level_decl(g, decls_scope, &tld_var->base);3698 add_top_level_decl(g, decls_scope, &tld_var->base);
...@@ -3709,7 +3708,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -3709,7 +3708,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
3709 }3708 }
37103709
3711 VisibMod visib_mod = node->data.fn_proto.visib_mod;3710 VisibMod visib_mod = node->data.fn_proto.visib_mod;
3712 TldFn *tld_fn = allocate<TldFn>(1);3711 TldFn *tld_fn = heap::c_allocator.create<TldFn>();
3713 init_tld(&tld_fn->base, TldIdFn, fn_name, visib_mod, node, &decls_scope->base);3712 init_tld(&tld_fn->base, TldIdFn, fn_name, visib_mod, node, &decls_scope->base);
3714 tld_fn->extern_lib_name = node->data.fn_proto.lib_name;3713 tld_fn->extern_lib_name = node->data.fn_proto.lib_name;
3715 add_top_level_decl(g, decls_scope, &tld_fn->base);3714 add_top_level_decl(g, decls_scope, &tld_fn->base);
...@@ -3718,7 +3717,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -3718,7 +3717,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
3718 }3717 }
3719 case NodeTypeUsingNamespace: {3718 case NodeTypeUsingNamespace: {
3720 VisibMod visib_mod = node->data.using_namespace.visib_mod;3719 VisibMod visib_mod = node->data.using_namespace.visib_mod;
3721 TldUsingNamespace *tld_using_namespace = allocate<TldUsingNamespace>(1);3720 TldUsingNamespace *tld_using_namespace = heap::c_allocator.create<TldUsingNamespace>();
3722 init_tld(&tld_using_namespace->base, TldIdUsingNamespace, nullptr, visib_mod, node, &decls_scope->base);3721 init_tld(&tld_using_namespace->base, TldIdUsingNamespace, nullptr, visib_mod, node, &decls_scope->base);
3723 add_top_level_decl(g, decls_scope, &tld_using_namespace->base);3722 add_top_level_decl(g, decls_scope, &tld_using_namespace->base);
3724 decls_scope->use_decls.append(tld_using_namespace);3723 decls_scope->use_decls.append(tld_using_namespace);
...@@ -3845,7 +3844,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf...@@ -3845,7 +3844,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
3845 assert(const_value != nullptr);3844 assert(const_value != nullptr);
3846 assert(var_type != nullptr);3845 assert(var_type != nullptr);
38473846
3848 ZigVar *variable_entry = allocate<ZigVar>(1);3847 ZigVar *variable_entry = heap::c_allocator.create<ZigVar>();
3849 variable_entry->const_value = const_value;3848 variable_entry->const_value = const_value;
3850 variable_entry->var_type = var_type;3849 variable_entry->var_type = var_type;
3851 variable_entry->parent_scope = parent_scope;3850 variable_entry->parent_scope = parent_scope;
...@@ -3984,7 +3983,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {...@@ -3984,7 +3983,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
3984 ZigType *type = explicit_type ? explicit_type : implicit_type;3983 ZigType *type = explicit_type ? explicit_type : implicit_type;
3985 assert(type != nullptr); // should have been caught by the parser3984 assert(type != nullptr); // should have been caught by the parser
39863985
3987 ZigValue *init_val = (init_value != nullptr) ? init_value : create_const_runtime(type);3986 ZigValue *init_val = (init_value != nullptr) ? init_value : create_const_runtime(g, type);
39883987
3989 tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope, var_decl->symbol,3988 tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope, var_decl->symbol,
3990 is_const, init_val, &tld_var->base, type);3989 is_const, init_val, &tld_var->base, type);
...@@ -4491,7 +4490,7 @@ static Error define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {...@@ -4491,7 +4490,7 @@ static Error define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {
4491 }4490 }
44924491
4493 ZigVar *var = add_variable(g, param_decl_node, fn_table_entry->child_scope,4492 ZigVar *var = add_variable(g, param_decl_node, fn_table_entry->child_scope,
4494 param_name, true, create_const_runtime(param_type), nullptr, param_type);4493 param_name, true, create_const_runtime(g, param_type), nullptr, param_type);
4495 var->src_arg_index = i;4494 var->src_arg_index = i;
4496 fn_table_entry->child_scope = var->child_scope;4495 fn_table_entry->child_scope = var->child_scope;
4497 var->shadowable = var->shadowable || is_var_args;4496 var->shadowable = var->shadowable || is_var_args;
...@@ -4786,7 +4785,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {...@@ -4786,7 +4785,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
4786 } else {4785 } else {
4787 return_err_set_type->data.error_set.err_count = inferred_err_set_type->data.error_set.err_count;4786 return_err_set_type->data.error_set.err_count = inferred_err_set_type->data.error_set.err_count;
4788 if (inferred_err_set_type->data.error_set.err_count > 0) {4787 if (inferred_err_set_type->data.error_set.err_count > 0) {
4789 return_err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(inferred_err_set_type->data.error_set.err_count);4788 return_err_set_type->data.error_set.errors = heap::c_allocator.allocate<ErrorTableEntry *>(inferred_err_set_type->data.error_set.err_count);
4790 for (uint32_t i = 0; i < inferred_err_set_type->data.error_set.err_count; i += 1) {4789 for (uint32_t i = 0; i < inferred_err_set_type->data.error_set.err_count; i += 1) {
4791 return_err_set_type->data.error_set.errors[i] = inferred_err_set_type->data.error_set.errors[i];4790 return_err_set_type->data.error_set.errors[i] = inferred_err_set_type->data.error_set.errors[i];
4792 }4791 }
...@@ -4919,7 +4918,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu...@@ -4919,7 +4918,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
4919 Buf *bare_name = buf_alloc();4918 Buf *bare_name = buf_alloc();
4920 os_path_extname(src_basename, bare_name, nullptr);4919 os_path_extname(src_basename, bare_name, nullptr);
49214920
4922 RootStruct *root_struct = allocate<RootStruct>(1);4921 RootStruct *root_struct = heap::c_allocator.create<RootStruct>();
4923 root_struct->package = package;4922 root_struct->package = package;
4924 root_struct->source_code = source_code;4923 root_struct->source_code = source_code;
4925 root_struct->line_offsets = tokenization.line_offsets;4924 root_struct->line_offsets = tokenization.line_offsets;
...@@ -4946,7 +4945,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu...@@ -4946,7 +4945,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
4946 scan_decls(g, import_entry->data.structure.decls_scope, top_level_decl);4945 scan_decls(g, import_entry->data.structure.decls_scope, top_level_decl);
4947 }4946 }
49484947
4949 TldContainer *tld_container = allocate<TldContainer>(1);4948 TldContainer *tld_container = heap::c_allocator.create<TldContainer>();
4950 init_tld(&tld_container->base, TldIdContainer, namespace_name, VisibModPub, root_node, nullptr);4949 init_tld(&tld_container->base, TldIdContainer, namespace_name, VisibModPub, root_node, nullptr);
4951 tld_container->type_entry = import_entry;4950 tld_container->type_entry = import_entry;
4952 tld_container->decls_scope = import_entry->data.structure.decls_scope;4951 tld_container->decls_scope = import_entry->data.structure.decls_scope;
...@@ -5694,14 +5693,14 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {...@@ -5694,14 +5693,14 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
5694 if (entry != nullptr) {5693 if (entry != nullptr) {
5695 return entry->value;5694 return entry->value;
5696 }5695 }
5697 ZigValue *result = create_const_vals(1);5696 ZigValue *result = g->pass1_arena->create<ZigValue>();
5698 result->type = type_entry;5697 result->type = type_entry;
5699 result->special = ConstValSpecialStatic;5698 result->special = ConstValSpecialStatic;
5700 if (result->type->id == ZigTypeIdStruct) {5699 if (result->type->id == ZigTypeIdStruct) {
5701 // The fields array cannot be left unpopulated5700 // The fields array cannot be left unpopulated
5702 const ZigType *struct_type = result->type;5701 const ZigType *struct_type = result->type;
5703 const size_t field_count = struct_type->data.structure.src_field_count;5702 const size_t field_count = struct_type->data.structure.src_field_count;
5704 result->data.x_struct.fields = alloc_const_vals_ptrs(field_count);5703 result->data.x_struct.fields = alloc_const_vals_ptrs(g, field_count);
5705 for (size_t i = 0; i < field_count; i += 1) {5704 for (size_t i = 0; i < field_count; i += 1) {
5706 TypeStructField *field = struct_type->data.structure.fields[i];5705 TypeStructField *field = struct_type->data.structure.fields[i];
5707 ZigType *field_type = resolve_struct_field_type(g, field);5706 ZigType *field_type = resolve_struct_field_type(g, field);
...@@ -5786,7 +5785,7 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str) {...@@ -5786,7 +5785,7 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str) {
5786 }5785 }
57875786
5788 // first we build the underlying array5787 // first we build the underlying array
5789 ZigValue *array_val = create_const_vals(1);5788 ZigValue *array_val = g->pass1_arena->create<ZigValue>();
5790 array_val->special = ConstValSpecialStatic;5789 array_val->special = ConstValSpecialStatic;
5791 array_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str), g->intern.for_zero_byte());5790 array_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str), g->intern.for_zero_byte());
5792 array_val->data.x_array.special = ConstArraySpecialBuf;5791 array_val->data.x_array.special = ConstArraySpecialBuf;
...@@ -5803,7 +5802,7 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str) {...@@ -5803,7 +5802,7 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str) {
5803}5802}
58045803
5805ZigValue *create_const_str_lit(CodeGen *g, Buf *str) {5804ZigValue *create_const_str_lit(CodeGen *g, Buf *str) {
5806 ZigValue *const_val = create_const_vals(1);5805 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5807 init_const_str_lit(g, const_val, str);5806 init_const_str_lit(g, const_val, str);
5808 return const_val;5807 return const_val;
5809}5808}
...@@ -5814,8 +5813,8 @@ void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint)...@@ -5814,8 +5813,8 @@ void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint)
5814 bigint_init_bigint(&const_val->data.x_bigint, bigint);5813 bigint_init_bigint(&const_val->data.x_bigint, bigint);
5815}5814}
58165815
5817ZigValue *create_const_bigint(ZigType *type, const BigInt *bigint) {5816ZigValue *create_const_bigint(CodeGen *g, ZigType *type, const BigInt *bigint) {
5818 ZigValue *const_val = create_const_vals(1);5817 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5819 init_const_bigint(const_val, type, bigint);5818 init_const_bigint(const_val, type, bigint);
5820 return const_val;5819 return const_val;
5821}5820}
...@@ -5828,8 +5827,8 @@ void init_const_unsigned_negative(ZigValue *const_val, ZigType *type, uint64_t x...@@ -5828,8 +5827,8 @@ void init_const_unsigned_negative(ZigValue *const_val, ZigType *type, uint64_t x
5828 const_val->data.x_bigint.is_negative = negative;5827 const_val->data.x_bigint.is_negative = negative;
5829}5828}
58305829
5831ZigValue *create_const_unsigned_negative(ZigType *type, uint64_t x, bool negative) {5830ZigValue *create_const_unsigned_negative(CodeGen *g, ZigType *type, uint64_t x, bool negative) {
5832 ZigValue *const_val = create_const_vals(1);5831 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5833 init_const_unsigned_negative(const_val, type, x, negative);5832 init_const_unsigned_negative(const_val, type, x, negative);
5834 return const_val;5833 return const_val;
5835}5834}
...@@ -5839,7 +5838,7 @@ void init_const_usize(CodeGen *g, ZigValue *const_val, uint64_t x) {...@@ -5839,7 +5838,7 @@ void init_const_usize(CodeGen *g, ZigValue *const_val, uint64_t x) {
5839}5838}
58405839
5841ZigValue *create_const_usize(CodeGen *g, uint64_t x) {5840ZigValue *create_const_usize(CodeGen *g, uint64_t x) {
5842 return create_const_unsigned_negative(g->builtin_types.entry_usize, x, false);5841 return create_const_unsigned_negative(g, g->builtin_types.entry_usize, x, false);
5843}5842}
58445843
5845void init_const_signed(ZigValue *const_val, ZigType *type, int64_t x) {5844void init_const_signed(ZigValue *const_val, ZigType *type, int64_t x) {
...@@ -5848,8 +5847,8 @@ void init_const_signed(ZigValue *const_val, ZigType *type, int64_t x) {...@@ -5848,8 +5847,8 @@ void init_const_signed(ZigValue *const_val, ZigType *type, int64_t x) {
5848 bigint_init_signed(&const_val->data.x_bigint, x);5847 bigint_init_signed(&const_val->data.x_bigint, x);
5849}5848}
58505849
5851ZigValue *create_const_signed(ZigType *type, int64_t x) {5850ZigValue *create_const_signed(CodeGen *g, ZigType *type, int64_t x) {
5852 ZigValue *const_val = create_const_vals(1);5851 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5853 init_const_signed(const_val, type, x);5852 init_const_signed(const_val, type, x);
5854 return const_val;5853 return const_val;
5855}5854}
...@@ -5860,8 +5859,8 @@ void init_const_null(ZigValue *const_val, ZigType *type) {...@@ -5860,8 +5859,8 @@ void init_const_null(ZigValue *const_val, ZigType *type) {
5860 const_val->data.x_optional = nullptr;5859 const_val->data.x_optional = nullptr;
5861}5860}
58625861
5863ZigValue *create_const_null(ZigType *type) {5862ZigValue *create_const_null(CodeGen *g, ZigType *type) {
5864 ZigValue *const_val = create_const_vals(1);5863 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5865 init_const_null(const_val, type);5864 init_const_null(const_val, type);
5866 return const_val;5865 return const_val;
5867}5866}
...@@ -5893,8 +5892,8 @@ void init_const_float(ZigValue *const_val, ZigType *type, double value) {...@@ -5893,8 +5892,8 @@ void init_const_float(ZigValue *const_val, ZigType *type, double value) {
5893 }5892 }
5894}5893}
58955894
5896ZigValue *create_const_float(ZigType *type, double value) {5895ZigValue *create_const_float(CodeGen *g, ZigType *type, double value) {
5897 ZigValue *const_val = create_const_vals(1);5896 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5898 init_const_float(const_val, type, value);5897 init_const_float(const_val, type, value);
5899 return const_val;5898 return const_val;
5900}5899}
...@@ -5905,8 +5904,8 @@ void init_const_enum(ZigValue *const_val, ZigType *type, const BigInt *tag) {...@@ -5905,8 +5904,8 @@ void init_const_enum(ZigValue *const_val, ZigType *type, const BigInt *tag) {
5905 bigint_init_bigint(&const_val->data.x_enum_tag, tag);5904 bigint_init_bigint(&const_val->data.x_enum_tag, tag);
5906}5905}
59075906
5908ZigValue *create_const_enum(ZigType *type, const BigInt *tag) {5907ZigValue *create_const_enum(CodeGen *g, ZigType *type, const BigInt *tag) {
5909 ZigValue *const_val = create_const_vals(1);5908 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5910 init_const_enum(const_val, type, tag);5909 init_const_enum(const_val, type, tag);
5911 return const_val;5910 return const_val;
5912}5911}
...@@ -5919,7 +5918,7 @@ void init_const_bool(CodeGen *g, ZigValue *const_val, bool value) {...@@ -5919,7 +5918,7 @@ void init_const_bool(CodeGen *g, ZigValue *const_val, bool value) {
5919}5918}
59205919
5921ZigValue *create_const_bool(CodeGen *g, bool value) {5920ZigValue *create_const_bool(CodeGen *g, bool value) {
5922 ZigValue *const_val = create_const_vals(1);5921 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5923 init_const_bool(g, const_val, value);5922 init_const_bool(g, const_val, value);
5924 return const_val;5923 return const_val;
5925}5924}
...@@ -5929,8 +5928,8 @@ void init_const_runtime(ZigValue *const_val, ZigType *type) {...@@ -5929,8 +5928,8 @@ void init_const_runtime(ZigValue *const_val, ZigType *type) {
5929 const_val->type = type;5928 const_val->type = type;
5930}5929}
59315930
5932ZigValue *create_const_runtime(ZigType *type) {5931ZigValue *create_const_runtime(CodeGen *g, ZigType *type) {
5933 ZigValue *const_val = create_const_vals(1);5932 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5934 init_const_runtime(const_val, type);5933 init_const_runtime(const_val, type);
5935 return const_val;5934 return const_val;
5936}5935}
...@@ -5942,7 +5941,7 @@ void init_const_type(CodeGen *g, ZigValue *const_val, ZigType *type_value) {...@@ -5942,7 +5941,7 @@ void init_const_type(CodeGen *g, ZigValue *const_val, ZigType *type_value) {
5942}5941}
59435942
5944ZigValue *create_const_type(CodeGen *g, ZigType *type_value) {5943ZigValue *create_const_type(CodeGen *g, ZigType *type_value) {
5945 ZigValue *const_val = create_const_vals(1);5944 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5946 init_const_type(g, const_val, type_value);5945 init_const_type(g, const_val, type_value);
5947 return const_val;5946 return const_val;
5948}5947}
...@@ -5957,7 +5956,7 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,...@@ -5957,7 +5956,7 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
59575956
5958 const_val->special = ConstValSpecialStatic;5957 const_val->special = ConstValSpecialStatic;
5959 const_val->type = get_slice_type(g, ptr_type);5958 const_val->type = get_slice_type(g, ptr_type);
5960 const_val->data.x_struct.fields = alloc_const_vals_ptrs(2);5959 const_val->data.x_struct.fields = alloc_const_vals_ptrs(g, 2);
59615960
5962 init_const_ptr_array(g, const_val->data.x_struct.fields[slice_ptr_index], array_val, start, is_const,5961 init_const_ptr_array(g, const_val->data.x_struct.fields[slice_ptr_index], array_val, start, is_const,
5963 PtrLenUnknown);5962 PtrLenUnknown);
...@@ -5965,7 +5964,7 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,...@@ -5965,7 +5964,7 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
5965}5964}
59665965
5967ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const) {5966ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const) {
5968 ZigValue *const_val = create_const_vals(1);5967 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5969 init_const_slice(g, const_val, array_val, start, len, is_const);5968 init_const_slice(g, const_val, array_val, start, len, is_const);
5970 return const_val;5969 return const_val;
5971}5970}
...@@ -5987,7 +5986,7 @@ void init_const_ptr_array(CodeGen *g, ZigValue *const_val, ZigValue *array_val,...@@ -5987,7 +5986,7 @@ void init_const_ptr_array(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
5987ZigValue *create_const_ptr_array(CodeGen *g, ZigValue *array_val, size_t elem_index, bool is_const,5986ZigValue *create_const_ptr_array(CodeGen *g, ZigValue *array_val, size_t elem_index, bool is_const,
5988 PtrLen ptr_len)5987 PtrLen ptr_len)
5989{5988{
5990 ZigValue *const_val = create_const_vals(1);5989 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
5991 init_const_ptr_array(g, const_val, array_val, elem_index, is_const, ptr_len);5990 init_const_ptr_array(g, const_val, array_val, elem_index, is_const, ptr_len);
5992 return const_val;5991 return const_val;
5993}5992}
...@@ -6000,7 +5999,7 @@ void init_const_ptr_ref(CodeGen *g, ZigValue *const_val, ZigValue *pointee_val,...@@ -6000,7 +5999,7 @@ void init_const_ptr_ref(CodeGen *g, ZigValue *const_val, ZigValue *pointee_val,
6000}5999}
60016000
6002ZigValue *create_const_ptr_ref(CodeGen *g, ZigValue *pointee_val, bool is_const) {6001ZigValue *create_const_ptr_ref(CodeGen *g, ZigValue *pointee_val, bool is_const) {
6003 ZigValue *const_val = create_const_vals(1);6002 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
6004 init_const_ptr_ref(g, const_val, pointee_val, is_const);6003 init_const_ptr_ref(g, const_val, pointee_val, is_const);
6005 return const_val;6004 return const_val;
6006}6005}
...@@ -6017,25 +6016,21 @@ void init_const_ptr_hard_coded_addr(CodeGen *g, ZigValue *const_val, ZigType *po...@@ -6017,25 +6016,21 @@ void init_const_ptr_hard_coded_addr(CodeGen *g, ZigValue *const_val, ZigType *po
6017ZigValue *create_const_ptr_hard_coded_addr(CodeGen *g, ZigType *pointee_type,6016ZigValue *create_const_ptr_hard_coded_addr(CodeGen *g, ZigType *pointee_type,
6018 size_t addr, bool is_const)6017 size_t addr, bool is_const)
6019{6018{
6020 ZigValue *const_val = create_const_vals(1);6019 ZigValue *const_val = g->pass1_arena->create<ZigValue>();
6021 init_const_ptr_hard_coded_addr(g, const_val, pointee_type, addr, is_const);6020 init_const_ptr_hard_coded_addr(g, const_val, pointee_type, addr, is_const);
6022 return const_val;6021 return const_val;
6023}6022}
60246023
6025ZigValue *create_const_vals(size_t count) {6024ZigValue **alloc_const_vals_ptrs(CodeGen *g, size_t count) {
6026 return allocate<ZigValue>(count, "ZigValue");6025 return realloc_const_vals_ptrs(g, nullptr, 0, count);
6027}6026}
60286027
6029ZigValue **alloc_const_vals_ptrs(size_t count) {6028ZigValue **realloc_const_vals_ptrs(CodeGen *g, ZigValue **ptr, size_t old_count, size_t new_count) {
6030 return realloc_const_vals_ptrs(nullptr, 0, count);
6031}
6032
6033ZigValue **realloc_const_vals_ptrs(ZigValue **ptr, size_t old_count, size_t new_count) {
6034 assert(new_count >= old_count);6029 assert(new_count >= old_count);
60356030
6036 size_t new_item_count = new_count - old_count;6031 size_t new_item_count = new_count - old_count;
6037 ZigValue **result = reallocate(ptr, old_count, new_count, "ZigValue*");6032 ZigValue **result = heap::c_allocator.reallocate(ptr, old_count, new_count);
6038 ZigValue *vals = create_const_vals(new_item_count);6033 ZigValue *vals = g->pass1_arena->allocate<ZigValue>(new_item_count);
6039 for (size_t i = old_count; i < new_count; i += 1) {6034 for (size_t i = old_count; i < new_count; i += 1) {
6040 result[i] = &vals[i - old_count];6035 result[i] = &vals[i - old_count];
6041 }6036 }
...@@ -6050,8 +6045,8 @@ TypeStructField **realloc_type_struct_fields(TypeStructField **ptr, size_t old_c...@@ -6050,8 +6045,8 @@ TypeStructField **realloc_type_struct_fields(TypeStructField **ptr, size_t old_c
6050 assert(new_count >= old_count);6045 assert(new_count >= old_count);
60516046
6052 size_t new_item_count = new_count - old_count;6047 size_t new_item_count = new_count - old_count;
6053 TypeStructField **result = reallocate(ptr, old_count, new_count, "TypeStructField*");6048 TypeStructField **result = heap::c_allocator.reallocate(ptr, old_count, new_count);
6054 TypeStructField *vals = allocate<TypeStructField>(new_item_count, "TypeStructField");6049 TypeStructField *vals = heap::c_allocator.allocate<TypeStructField>(new_item_count);
6055 for (size_t i = old_count; i < new_count; i += 1) {6050 for (size_t i = old_count; i < new_count; i += 1) {
6056 result[i] = &vals[i - old_count];6051 result[i] = &vals[i - old_count];
6057 }6052 }
...@@ -6062,7 +6057,7 @@ static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {...@@ -6062,7 +6057,7 @@ static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {
6062 if (orig_fn_type->data.fn.fn_type_id.cc == CallingConventionAsync)6057 if (orig_fn_type->data.fn.fn_type_id.cc == CallingConventionAsync)
6063 return orig_fn_type;6058 return orig_fn_type;
60646059
6065 ZigType *fn_type = allocate_nonzero<ZigType>(1);6060 ZigType *fn_type = heap::c_allocator.allocate_nonzero<ZigType>(1);
6066 *fn_type = *orig_fn_type;6061 *fn_type = *orig_fn_type;
6067 fn_type->data.fn.fn_type_id.cc = CallingConventionAsync;6062 fn_type->data.fn.fn_type_id.cc = CallingConventionAsync;
6068 fn_type->llvm_type = nullptr;6063 fn_type->llvm_type = nullptr;
...@@ -6236,11 +6231,11 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {...@@ -6236,11 +6231,11 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
6236 ZigType *fn_type = get_async_fn_type(g, fn->type_entry);6231 ZigType *fn_type = get_async_fn_type(g, fn->type_entry);
62376232
6238 if (fn->analyzed_executable.need_err_code_spill) {6233 if (fn->analyzed_executable.need_err_code_spill) {
6239 IrInstGenAlloca *alloca_gen = allocate<IrInstGenAlloca>(1);6234 IrInstGenAlloca *alloca_gen = heap::c_allocator.create<IrInstGenAlloca>();
6240 alloca_gen->base.id = IrInstGenIdAlloca;6235 alloca_gen->base.id = IrInstGenIdAlloca;
6241 alloca_gen->base.base.source_node = fn->proto_node;6236 alloca_gen->base.base.source_node = fn->proto_node;
6242 alloca_gen->base.base.scope = fn->child_scope;6237 alloca_gen->base.base.scope = fn->child_scope;
6243 alloca_gen->base.value = allocate<ZigValue>(1, "ZigValue");6238 alloca_gen->base.value = g->pass1_arena->create<ZigValue>();
6244 alloca_gen->base.value->type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);6239 alloca_gen->base.value->type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
6245 alloca_gen->base.base.ref_count = 1;6240 alloca_gen->base.base.ref_count = 1;
6246 alloca_gen->name_hint = "";6241 alloca_gen->name_hint = "";
...@@ -6942,9 +6937,9 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu...@@ -6942,9 +6937,9 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu
6942 return;6937 return;
6943 }6938 }
6944 case ConstArraySpecialNone: {6939 case ConstArraySpecialNone: {
6945 ZigValue *base = &array->data.s_none.elements[start];
6946 assert(base != nullptr);
6947 assert(start + len <= const_val->type->data.array.len);6940 assert(start + len <= const_val->type->data.array.len);
6941 ZigValue *base = &array->data.s_none.elements[start];
6942 assert(len == 0 || base != nullptr);
69486943
6949 buf_appendf(buf, "%s{", buf_ptr(type_name));6944 buf_appendf(buf, "%s{", buf_ptr(type_name));
6950 for (uint64_t i = 0; i < len; i += 1) {6945 for (uint64_t i = 0; i < len; i += 1) {
...@@ -7375,7 +7370,7 @@ static void init_const_undefined(CodeGen *g, ZigValue *const_val) {...@@ -7375,7 +7370,7 @@ static void init_const_undefined(CodeGen *g, ZigValue *const_val) {
73757370
7376 const_val->special = ConstValSpecialStatic;7371 const_val->special = ConstValSpecialStatic;
7377 size_t field_count = wanted_type->data.structure.src_field_count;7372 size_t field_count = wanted_type->data.structure.src_field_count;
7378 const_val->data.x_struct.fields = alloc_const_vals_ptrs(field_count);7373 const_val->data.x_struct.fields = alloc_const_vals_ptrs(g, field_count);
7379 for (size_t i = 0; i < field_count; i += 1) {7374 for (size_t i = 0; i < field_count; i += 1) {
7380 ZigValue *field_val = const_val->data.x_struct.fields[i];7375 ZigValue *field_val = const_val->data.x_struct.fields[i];
7381 field_val->type = resolve_struct_field_type(g, wanted_type->data.structure.fields[i]);7376 field_val->type = resolve_struct_field_type(g, wanted_type->data.structure.fields[i]);
...@@ -7418,7 +7413,7 @@ void expand_undef_array(CodeGen *g, ZigValue *const_val) {...@@ -7418,7 +7413,7 @@ void expand_undef_array(CodeGen *g, ZigValue *const_val) {
7418 return;7413 return;
7419 case ConstArraySpecialUndef: {7414 case ConstArraySpecialUndef: {
7420 const_val->data.x_array.special = ConstArraySpecialNone;7415 const_val->data.x_array.special = ConstArraySpecialNone;
7421 const_val->data.x_array.data.s_none.elements = create_const_vals(elem_count);7416 const_val->data.x_array.data.s_none.elements = g->pass1_arena->allocate<ZigValue>(elem_count);
7422 for (size_t i = 0; i < elem_count; i += 1) {7417 for (size_t i = 0; i < elem_count; i += 1) {
7423 ZigValue *element_val = &const_val->data.x_array.data.s_none.elements[i];7418 ZigValue *element_val = &const_val->data.x_array.data.s_none.elements[i];
7424 element_val->type = elem_type;7419 element_val->type = elem_type;
...@@ -7437,7 +7432,7 @@ void expand_undef_array(CodeGen *g, ZigValue *const_val) {...@@ -7437,7 +7432,7 @@ void expand_undef_array(CodeGen *g, ZigValue *const_val) {
74377432
7438 const_val->data.x_array.special = ConstArraySpecialNone;7433 const_val->data.x_array.special = ConstArraySpecialNone;
7439 assert(elem_count == buf_len(buf));7434 assert(elem_count == buf_len(buf));
7440 const_val->data.x_array.data.s_none.elements = create_const_vals(elem_count);7435 const_val->data.x_array.data.s_none.elements = g->pass1_arena->allocate<ZigValue>(elem_count);
7441 for (size_t i = 0; i < elem_count; i += 1) {7436 for (size_t i = 0; i < elem_count; i += 1) {
7442 ZigValue *this_char = &const_val->data.x_array.data.s_none.elements[i];7437 ZigValue *this_char = &const_val->data.x_array.data.s_none.elements[i];
7443 this_char->special = ConstValSpecialStatic;7438 this_char->special = ConstValSpecialStatic;
...@@ -7609,7 +7604,7 @@ const char *type_id_name(ZigTypeId id) {...@@ -7609,7 +7604,7 @@ const char *type_id_name(ZigTypeId id) {
7609}7604}
76107605
7611LinkLib *create_link_lib(Buf *name) {7606LinkLib *create_link_lib(Buf *name) {
7612 LinkLib *link_lib = allocate<LinkLib>(1);7607 LinkLib *link_lib = heap::c_allocator.create<LinkLib>();
7613 link_lib->name = name;7608 link_lib->name = name;
7614 return link_lib;7609 return link_lib;
7615}7610}
...@@ -8137,7 +8132,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS...@@ -8137,7 +8132,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
81378132
8138 size_t field_count = struct_type->data.structure.src_field_count;8133 size_t field_count = struct_type->data.structure.src_field_count;
8139 // Every field could potentially have a generated padding field after it.8134 // Every field could potentially have a generated padding field after it.
8140 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(field_count * 2);8135 LLVMTypeRef *element_types = heap::c_allocator.allocate<LLVMTypeRef>(field_count * 2);
81418136
8142 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);8137 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
8143 size_t packed_bits_offset = 0;8138 size_t packed_bits_offset = 0;
...@@ -8272,7 +8267,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS...@@ -8272,7 +8267,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
8272 (unsigned)struct_type->data.structure.gen_field_count, packed);8267 (unsigned)struct_type->data.structure.gen_field_count, packed);
8273 }8268 }
82748269
8275 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);8270 ZigLLVMDIType **di_element_types = heap::c_allocator.allocate<ZigLLVMDIType*>(debug_field_count);
8276 size_t debug_field_index = 0;8271 size_t debug_field_index = 0;
8277 for (size_t i = 0; i < field_count; i += 1) {8272 for (size_t i = 0; i < field_count; i += 1) {
8278 TypeStructField *field = struct_type->data.structure.fields[i];8273 TypeStructField *field = struct_type->data.structure.fields[i];
...@@ -8389,7 +8384,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatu...@@ -8389,7 +8384,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatu
8389 uint32_t field_count = enum_type->data.enumeration.src_field_count;8384 uint32_t field_count = enum_type->data.enumeration.src_field_count;
83908385
8391 assert(field_count == 0 || enum_type->data.enumeration.fields != nullptr);8386 assert(field_count == 0 || enum_type->data.enumeration.fields != nullptr);
8392 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);8387 ZigLLVMDIEnumerator **di_enumerators = heap::c_allocator.allocate<ZigLLVMDIEnumerator*>(field_count);
83938388
8394 for (uint32_t i = 0; i < field_count; i += 1) {8389 for (uint32_t i = 0; i < field_count; i += 1) {
8395 TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];8390 TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];
...@@ -8456,7 +8451,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta...@@ -8456,7 +8451,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
8456 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;8451 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
8457 }8452 }
84588453
8459 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);8454 ZigLLVMDIType **union_inner_di_types = heap::c_allocator.allocate<ZigLLVMDIType*>(gen_field_count);
8460 uint32_t field_count = union_type->data.unionation.src_field_count;8455 uint32_t field_count = union_type->data.unionation.src_field_count;
8461 for (uint32_t i = 0; i < field_count; i += 1) {8456 for (uint32_t i = 0; i < field_count; i += 1) {
8462 TypeUnionField *union_field = &union_type->data.unionation.fields[i];8457 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
...@@ -8895,7 +8890,7 @@ static void resolve_llvm_types_fn_type(CodeGen *g, ZigType *fn_type) {...@@ -8895,7 +8890,7 @@ static void resolve_llvm_types_fn_type(CodeGen *g, ZigType *fn_type) {
8895 param_di_types.append(get_llvm_di_type(g, gen_type));8890 param_di_types.append(get_llvm_di_type(g, gen_type));
8896 }8891 }
8897 if (is_async) {8892 if (is_async) {
8898 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(2);8893 fn_type->data.fn.gen_param_info = heap::c_allocator.allocate<FnGenParamInfo>(2);
88998894
8900 ZigType *frame_type = get_any_frame_type(g, fn_type_id->return_type);8895 ZigType *frame_type = get_any_frame_type(g, fn_type_id->return_type);
8901 gen_param_types.append(get_llvm_type(g, frame_type));8896 gen_param_types.append(get_llvm_type(g, frame_type));
...@@ -8912,7 +8907,7 @@ static void resolve_llvm_types_fn_type(CodeGen *g, ZigType *fn_type) {...@@ -8912,7 +8907,7 @@ static void resolve_llvm_types_fn_type(CodeGen *g, ZigType *fn_type) {
8912 fn_type->data.fn.gen_param_info[1].gen_index = 1;8907 fn_type->data.fn.gen_param_info[1].gen_index = 1;
8913 fn_type->data.fn.gen_param_info[1].type = g->builtin_types.entry_usize;8908 fn_type->data.fn.gen_param_info[1].type = g->builtin_types.entry_usize;
8914 } else {8909 } else {
8915 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);8910 fn_type->data.fn.gen_param_info = heap::c_allocator.allocate<FnGenParamInfo>(fn_type_id->param_count);
8916 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {8911 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
8917 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];8912 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
8918 ZigType *type_entry = src_param_info->type;8913 ZigType *type_entry = src_param_info->type;
...@@ -9369,7 +9364,7 @@ bool type_has_optional_repr(ZigType *ty) {...@@ -9369,7 +9364,7 @@ bool type_has_optional_repr(ZigType *ty) {
9369 }9364 }
9370}9365}
93719366
9372void copy_const_val(ZigValue *dest, ZigValue *src) {9367void copy_const_val(CodeGen *g, ZigValue *dest, ZigValue *src) {
9373 uint32_t prev_align = dest->llvm_align;9368 uint32_t prev_align = dest->llvm_align;
9374 ConstParent prev_parent = dest->parent;9369 ConstParent prev_parent = dest->parent;
9375 memcpy(dest, src, sizeof(ZigValue));9370 memcpy(dest, src, sizeof(ZigValue));
...@@ -9378,26 +9373,26 @@ void copy_const_val(ZigValue *dest, ZigValue *src) {...@@ -9378,26 +9373,26 @@ void copy_const_val(ZigValue *dest, ZigValue *src) {
9378 return;9373 return;
9379 dest->parent = prev_parent;9374 dest->parent = prev_parent;
9380 if (dest->type->id == ZigTypeIdStruct) {9375 if (dest->type->id == ZigTypeIdStruct) {
9381 dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);9376 dest->data.x_struct.fields = alloc_const_vals_ptrs(g, dest->type->data.structure.src_field_count);
9382 for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {9377 for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
9383 copy_const_val(dest->data.x_struct.fields[i], src->data.x_struct.fields[i]);9378 copy_const_val(g, dest->data.x_struct.fields[i], src->data.x_struct.fields[i]);
9384 dest->data.x_struct.fields[i]->parent.id = ConstParentIdStruct;9379 dest->data.x_struct.fields[i]->parent.id = ConstParentIdStruct;
9385 dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest;9380 dest->data.x_struct.fields[i]->parent.data.p_struct.struct_val = dest;
9386 dest->data.x_struct.fields[i]->parent.data.p_struct.field_index = i;9381 dest->data.x_struct.fields[i]->parent.data.p_struct.field_index = i;
9387 }9382 }
9388 } else if (dest->type->id == ZigTypeIdArray) {9383 } else if (dest->type->id == ZigTypeIdArray) {
9389 if (dest->data.x_array.special == ConstArraySpecialNone) {9384 if (dest->data.x_array.special == ConstArraySpecialNone) {
9390 dest->data.x_array.data.s_none.elements = create_const_vals(dest->type->data.array.len);9385 dest->data.x_array.data.s_none.elements = g->pass1_arena->allocate<ZigValue>(dest->type->data.array.len);
9391 for (uint64_t i = 0; i < dest->type->data.array.len; i += 1) {9386 for (uint64_t i = 0; i < dest->type->data.array.len; i += 1) {
9392 copy_const_val(&dest->data.x_array.data.s_none.elements[i], &src->data.x_array.data.s_none.elements[i]);9387 copy_const_val(g, &dest->data.x_array.data.s_none.elements[i], &src->data.x_array.data.s_none.elements[i]);
9393 dest->data.x_array.data.s_none.elements[i].parent.id = ConstParentIdArray;9388 dest->data.x_array.data.s_none.elements[i].parent.id = ConstParentIdArray;
9394 dest->data.x_array.data.s_none.elements[i].parent.data.p_array.array_val = dest;9389 dest->data.x_array.data.s_none.elements[i].parent.data.p_array.array_val = dest;
9395 dest->data.x_array.data.s_none.elements[i].parent.data.p_array.elem_index = i;9390 dest->data.x_array.data.s_none.elements[i].parent.data.p_array.elem_index = i;
9396 }9391 }
9397 }9392 }
9398 } else if (type_has_optional_repr(dest->type) && dest->data.x_optional != nullptr) {9393 } else if (type_has_optional_repr(dest->type) && dest->data.x_optional != nullptr) {
9399 dest->data.x_optional = create_const_vals(1);9394 dest->data.x_optional = g->pass1_arena->create<ZigValue>();
9400 copy_const_val(dest->data.x_optional, src->data.x_optional);9395 copy_const_val(g, dest->data.x_optional, src->data.x_optional);
9401 dest->data.x_optional->parent.id = ConstParentIdOptionalPayload;9396 dest->data.x_optional->parent.id = ConstParentIdOptionalPayload;
9402 dest->data.x_optional->parent.data.p_optional_payload.optional_val = dest;9397 dest->data.x_optional->parent.data.p_optional_payload.optional_val = dest;
9403 }9398 }
src/analyze.hpp+10-11
...@@ -128,22 +128,22 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str);...@@ -128,22 +128,22 @@ void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str);
128ZigValue *create_const_str_lit(CodeGen *g, Buf *str);128ZigValue *create_const_str_lit(CodeGen *g, Buf *str);
129129
130void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint);130void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint);
131ZigValue *create_const_bigint(ZigType *type, const BigInt *bigint);131ZigValue *create_const_bigint(CodeGen *g, ZigType *type, const BigInt *bigint);
132132
133void init_const_unsigned_negative(ZigValue *const_val, ZigType *type, uint64_t x, bool negative);133void init_const_unsigned_negative(ZigValue *const_val, ZigType *type, uint64_t x, bool negative);
134ZigValue *create_const_unsigned_negative(ZigType *type, uint64_t x, bool negative);134ZigValue *create_const_unsigned_negative(CodeGen *g, ZigType *type, uint64_t x, bool negative);
135135
136void init_const_signed(ZigValue *const_val, ZigType *type, int64_t x);136void init_const_signed(ZigValue *const_val, ZigType *type, int64_t x);
137ZigValue *create_const_signed(ZigType *type, int64_t x);137ZigValue *create_const_signed(CodeGen *g, ZigType *type, int64_t x);
138138
139void init_const_usize(CodeGen *g, ZigValue *const_val, uint64_t x);139void init_const_usize(CodeGen *g, ZigValue *const_val, uint64_t x);
140ZigValue *create_const_usize(CodeGen *g, uint64_t x);140ZigValue *create_const_usize(CodeGen *g, uint64_t x);
141141
142void init_const_float(ZigValue *const_val, ZigType *type, double value);142void init_const_float(ZigValue *const_val, ZigType *type, double value);
143ZigValue *create_const_float(ZigType *type, double value);143ZigValue *create_const_float(CodeGen *g, ZigType *type, double value);
144144
145void init_const_enum(ZigValue *const_val, ZigType *type, const BigInt *tag);145void init_const_enum(ZigValue *const_val, ZigType *type, const BigInt *tag);
146ZigValue *create_const_enum(ZigType *type, const BigInt *tag);146ZigValue *create_const_enum(CodeGen *g, ZigType *type, const BigInt *tag);
147147
148void init_const_bool(CodeGen *g, ZigValue *const_val, bool value);148void init_const_bool(CodeGen *g, ZigValue *const_val, bool value);
149ZigValue *create_const_bool(CodeGen *g, bool value);149ZigValue *create_const_bool(CodeGen *g, bool value);
...@@ -152,7 +152,7 @@ void init_const_type(CodeGen *g, ZigValue *const_val, ZigType *type_value);...@@ -152,7 +152,7 @@ void init_const_type(CodeGen *g, ZigValue *const_val, ZigType *type_value);
152ZigValue *create_const_type(CodeGen *g, ZigType *type_value);152ZigValue *create_const_type(CodeGen *g, ZigType *type_value);
153153
154void init_const_runtime(ZigValue *const_val, ZigType *type);154void init_const_runtime(ZigValue *const_val, ZigType *type);
155ZigValue *create_const_runtime(ZigType *type);155ZigValue *create_const_runtime(CodeGen *g, ZigType *type);
156156
157void init_const_ptr_ref(CodeGen *g, ZigValue *const_val, ZigValue *pointee_val, bool is_const);157void init_const_ptr_ref(CodeGen *g, ZigValue *const_val, ZigValue *pointee_val, bool is_const);
158ZigValue *create_const_ptr_ref(CodeGen *g, ZigValue *pointee_val, bool is_const);158ZigValue *create_const_ptr_ref(CodeGen *g, ZigValue *pointee_val, bool is_const);
...@@ -172,11 +172,10 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,...@@ -172,11 +172,10 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
172ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const);172ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const);
173173
174void init_const_null(ZigValue *const_val, ZigType *type);174void init_const_null(ZigValue *const_val, ZigType *type);
175ZigValue *create_const_null(ZigType *type);175ZigValue *create_const_null(CodeGen *g, ZigType *type);
176176
177ZigValue *create_const_vals(size_t count);177ZigValue **alloc_const_vals_ptrs(CodeGen *g, size_t count);
178ZigValue **alloc_const_vals_ptrs(size_t count);178ZigValue **realloc_const_vals_ptrs(CodeGen *g, ZigValue **ptr, size_t old_count, size_t new_count);
179ZigValue **realloc_const_vals_ptrs(ZigValue **ptr, size_t old_count, size_t new_count);
180179
181TypeStructField **alloc_type_struct_fields(size_t count);180TypeStructField **alloc_type_struct_fields(size_t count);
182TypeStructField **realloc_type_struct_fields(TypeStructField **ptr, size_t old_count, size_t new_count);181TypeStructField **realloc_type_struct_fields(TypeStructField **ptr, size_t old_count, size_t new_count);
...@@ -275,7 +274,7 @@ Error analyze_import(CodeGen *codegen, ZigType *source_import, Buf *import_targe...@@ -275,7 +274,7 @@ Error analyze_import(CodeGen *codegen, ZigType *source_import, Buf *import_targe
275 ZigType **out_import, Buf **out_import_target_path, Buf *out_full_path);274 ZigType **out_import, Buf **out_import_target_path, Buf *out_full_path);
276ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry);275ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry);
277bool is_anon_container(ZigType *ty);276bool is_anon_container(ZigType *ty);
278void copy_const_val(ZigValue *dest, ZigValue *src);277void copy_const_val(CodeGen *g, ZigValue *dest, ZigValue *src);
279bool type_has_optional_repr(ZigType *ty);278bool type_has_optional_repr(ZigType *ty);
280bool is_opt_err_set(ZigType *ty);279bool is_opt_err_set(ZigType *ty);
281bool type_is_numeric(ZigType *ty);280bool type_is_numeric(ZigType *ty);
src/bigint.cpp+16-16
...@@ -93,7 +93,7 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count)...@@ -93,7 +93,7 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count)
93 if (dest->data.digit == 0) dest->digit_count = 0;93 if (dest->data.digit == 0) dest->digit_count = 0;
94 return;94 return;
95 }95 }
96 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);96 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
97 for (size_t i = 0; i < digits_to_copy; i += 1) {97 for (size_t i = 0; i < digits_to_copy; i += 1) {
98 uint64_t digit = (i < op->digit_count) ? op_digits[i] : 0;98 uint64_t digit = (i < op->digit_count) ? op_digits[i] : 0;
99 dest->data.digits[i] = digit;99 dest->data.digits[i] = digit;
...@@ -174,7 +174,7 @@ void bigint_init_data(BigInt *dest, const uint64_t *digits, size_t digit_count,...@@ -174,7 +174,7 @@ void bigint_init_data(BigInt *dest, const uint64_t *digits, size_t digit_count,
174174
175 dest->digit_count = digit_count;175 dest->digit_count = digit_count;
176 dest->is_negative = is_negative;176 dest->is_negative = is_negative;
177 dest->data.digits = allocate_nonzero<uint64_t>(digit_count);177 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(digit_count);
178 memcpy(dest->data.digits, digits, sizeof(uint64_t) * digit_count);178 memcpy(dest->data.digits, digits, sizeof(uint64_t) * digit_count);
179179
180 bigint_normalize(dest);180 bigint_normalize(dest);
...@@ -191,13 +191,13 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src) {...@@ -191,13 +191,13 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src) {
191 }191 }
192 dest->is_negative = src->is_negative;192 dest->is_negative = src->is_negative;
193 dest->digit_count = src->digit_count;193 dest->digit_count = src->digit_count;
194 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);194 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
195 memcpy(dest->data.digits, src->data.digits, sizeof(uint64_t) * dest->digit_count);195 memcpy(dest->data.digits, src->data.digits, sizeof(uint64_t) * dest->digit_count);
196}196}
197197
198void bigint_deinit(BigInt *bi) {198void bigint_deinit(BigInt *bi) {
199 if (bi->digit_count > 1)199 if (bi->digit_count > 1)
200 deallocate<uint64_t>(bi->data.digits, bi->digit_count);200 heap::c_allocator.deallocate(bi->data.digits, bi->digit_count);
201}201}
202202
203void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {203void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
...@@ -227,7 +227,7 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {...@@ -227,7 +227,7 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
227 f128M_rem(&abs_val, &max_u64, &remainder);227 f128M_rem(&abs_val, &max_u64, &remainder);
228228
229 dest->digit_count = 2;229 dest->digit_count = 2;
230 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);230 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
231 dest->data.digits[0] = f128M_to_ui64(&remainder, softfloat_round_minMag, false);231 dest->data.digits[0] = f128M_to_ui64(&remainder, softfloat_round_minMag, false);
232 dest->data.digits[1] = f128M_to_ui64(&amt, softfloat_round_minMag, false);232 dest->data.digits[1] = f128M_to_ui64(&amt, softfloat_round_minMag, false);
233 bigint_normalize(dest);233 bigint_normalize(dest);
...@@ -345,7 +345,7 @@ void bigint_read_twos_complement(BigInt *dest, const uint8_t *buf, size_t bit_co...@@ -345,7 +345,7 @@ void bigint_read_twos_complement(BigInt *dest, const uint8_t *buf, size_t bit_co
345 if (dest->digit_count == 1) {345 if (dest->digit_count == 1) {
346 digits = &dest->data.digit;346 digits = &dest->data.digit;
347 } else {347 } else {
348 digits = allocate_nonzero<uint64_t>(dest->digit_count);348 digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
349 dest->data.digits = digits;349 dest->data.digits = digits;
350 }350 }
351351
...@@ -464,7 +464,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -464,7 +464,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
464 }464 }
465 size_t i = 1;465 size_t i = 1;
466 uint64_t first_digit = dest->data.digit;466 uint64_t first_digit = dest->data.digit;
467 dest->data.digits = allocate_nonzero<uint64_t>(max(op1->digit_count, op2->digit_count) + 1);467 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(max(op1->digit_count, op2->digit_count) + 1);
468 dest->data.digits[0] = first_digit;468 dest->data.digits[0] = first_digit;
469469
470 for (;;) {470 for (;;) {
...@@ -532,7 +532,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -532,7 +532,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
532 return;532 return;
533 }533 }
534 uint64_t first_digit = dest->data.digit;534 uint64_t first_digit = dest->data.digit;
535 dest->data.digits = allocate_nonzero<uint64_t>(bigger_op->digit_count);535 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(bigger_op->digit_count);
536 dest->data.digits[0] = first_digit;536 dest->data.digits[0] = first_digit;
537 size_t i = 1;537 size_t i = 1;
538538
...@@ -1032,7 +1032,7 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn...@@ -1032,7 +1032,7 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn
1032 if (lhsWords == 1) {1032 if (lhsWords == 1) {
1033 Quotient->data.digit = Make_64(Q[1], Q[0]);1033 Quotient->data.digit = Make_64(Q[1], Q[0]);
1034 } else {1034 } else {
1035 Quotient->data.digits = allocate<uint64_t>(lhsWords);1035 Quotient->data.digits = heap::c_allocator.allocate<uint64_t>(lhsWords);
1036 for (size_t i = 0; i < lhsWords; i += 1) {1036 for (size_t i = 0; i < lhsWords; i += 1) {
1037 Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);1037 Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);
1038 }1038 }
...@@ -1046,7 +1046,7 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn...@@ -1046,7 +1046,7 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn
1046 if (rhsWords == 1) {1046 if (rhsWords == 1) {
1047 Remainder->data.digit = Make_64(R[1], R[0]);1047 Remainder->data.digit = Make_64(R[1], R[0]);
1048 } else {1048 } else {
1049 Remainder->data.digits = allocate<uint64_t>(rhsWords);1049 Remainder->data.digits = heap::c_allocator.allocate<uint64_t>(rhsWords);
1050 for (size_t i = 0; i < rhsWords; i += 1) {1050 for (size_t i = 0; i < rhsWords; i += 1) {
1051 Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);1051 Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);
1052 }1052 }
...@@ -1218,7 +1218,7 @@ void bigint_or(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -1218,7 +1218,7 @@ void bigint_or(BigInt *dest, const BigInt *op1, const BigInt *op2) {
1218 return;1218 return;
1219 }1219 }
1220 dest->digit_count = max(op1->digit_count, op2->digit_count);1220 dest->digit_count = max(op1->digit_count, op2->digit_count);
1221 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);1221 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
1222 for (size_t i = 0; i < dest->digit_count; i += 1) {1222 for (size_t i = 0; i < dest->digit_count; i += 1) {
1223 uint64_t digit = 0;1223 uint64_t digit = 0;
1224 if (i < op1->digit_count) {1224 if (i < op1->digit_count) {
...@@ -1262,7 +1262,7 @@ void bigint_and(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -1262,7 +1262,7 @@ void bigint_and(BigInt *dest, const BigInt *op1, const BigInt *op2) {
1262 }1262 }
12631263
1264 dest->digit_count = max(op1->digit_count, op2->digit_count);1264 dest->digit_count = max(op1->digit_count, op2->digit_count);
1265 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);1265 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
12661266
1267 size_t i = 0;1267 size_t i = 0;
1268 for (; i < op1->digit_count && i < op2->digit_count; i += 1) {1268 for (; i < op1->digit_count && i < op2->digit_count; i += 1) {
...@@ -1308,7 +1308,7 @@ void bigint_xor(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -1308,7 +1308,7 @@ void bigint_xor(BigInt *dest, const BigInt *op1, const BigInt *op2) {
1308 return;1308 return;
1309 }1309 }
1310 dest->digit_count = max(op1->digit_count, op2->digit_count);1310 dest->digit_count = max(op1->digit_count, op2->digit_count);
1311 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);1311 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
1312 size_t i = 0;1312 size_t i = 0;
1313 for (; i < op1->digit_count && i < op2->digit_count; i += 1) {1313 for (; i < op1->digit_count && i < op2->digit_count; i += 1) {
1314 dest->data.digits[i] = op1_digits[i] ^ op2_digits[i];1314 dest->data.digits[i] = op1_digits[i] ^ op2_digits[i];
...@@ -1358,7 +1358,7 @@ void bigint_shl(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -1358,7 +1358,7 @@ void bigint_shl(BigInt *dest, const BigInt *op1, const BigInt *op2) {
1358 uint64_t digit_shift_count = shift_amt / 64;1358 uint64_t digit_shift_count = shift_amt / 64;
1359 uint64_t leftover_shift_count = shift_amt % 64;1359 uint64_t leftover_shift_count = shift_amt % 64;
13601360
1361 dest->data.digits = allocate<uint64_t>(op1->digit_count + digit_shift_count + 1);1361 dest->data.digits = heap::c_allocator.allocate<uint64_t>(op1->digit_count + digit_shift_count + 1);
1362 dest->digit_count = digit_shift_count;1362 dest->digit_count = digit_shift_count;
1363 uint64_t carry = 0;1363 uint64_t carry = 0;
1364 for (size_t i = 0; i < op1->digit_count; i += 1) {1364 for (size_t i = 0; i < op1->digit_count; i += 1) {
...@@ -1421,7 +1421,7 @@ void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -1421,7 +1421,7 @@ void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2) {
1421 if (dest->digit_count == 1) {1421 if (dest->digit_count == 1) {
1422 digits = &dest->data.digit;1422 digits = &dest->data.digit;
1423 } else {1423 } else {
1424 digits = allocate<uint64_t>(dest->digit_count);1424 digits = heap::c_allocator.allocate<uint64_t>(dest->digit_count);
1425 dest->data.digits = digits;1425 dest->data.digits = digits;
1426 }1426 }
14271427
...@@ -1492,7 +1492,7 @@ void bigint_not(BigInt *dest, const BigInt *op, size_t bit_count, bool is_signed...@@ -1492,7 +1492,7 @@ void bigint_not(BigInt *dest, const BigInt *op, size_t bit_count, bool is_signed
1492 }1492 }
1493 dest->digit_count = (bit_count + 63) / 64;1493 dest->digit_count = (bit_count + 63) / 64;
1494 assert(dest->digit_count >= op->digit_count);1494 assert(dest->digit_count >= op->digit_count);
1495 dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);1495 dest->data.digits = heap::c_allocator.allocate_nonzero<uint64_t>(dest->digit_count);
1496 size_t i = 0;1496 size_t i = 0;
1497 for (; i < op->digit_count; i += 1) {1497 for (; i < op->digit_count; i += 1) {
1498 dest->data.digits[i] = ~op_digits[i];1498 dest->data.digits[i] = ~op_digits[i];
src/buffer.hpp+4-5
...@@ -50,7 +50,7 @@ static inline void buf_resize(Buf *buf, size_t new_len) {...@@ -50,7 +50,7 @@ static inline void buf_resize(Buf *buf, size_t new_len) {
50}50}
5151
52static inline Buf *buf_alloc_fixed(size_t size) {52static inline Buf *buf_alloc_fixed(size_t size) {
53 Buf *buf = allocate<Buf>(1);53 Buf *buf = heap::c_allocator.create<Buf>();
54 buf_resize(buf, size);54 buf_resize(buf, size);
55 return buf;55 return buf;
56}56}
...@@ -65,7 +65,7 @@ static inline void buf_deinit(Buf *buf) {...@@ -65,7 +65,7 @@ static inline void buf_deinit(Buf *buf) {
6565
66static inline void buf_destroy(Buf *buf) {66static inline void buf_destroy(Buf *buf) {
67 buf_deinit(buf);67 buf_deinit(buf);
68 free(buf);68 heap::c_allocator.destroy(buf);
69}69}
7070
71static inline void buf_init_from_mem(Buf *buf, const char *ptr, size_t len) {71static inline void buf_init_from_mem(Buf *buf, const char *ptr, size_t len) {
...@@ -85,7 +85,7 @@ static inline void buf_init_from_buf(Buf *buf, Buf *other) {...@@ -85,7 +85,7 @@ static inline void buf_init_from_buf(Buf *buf, Buf *other) {
8585
86static inline Buf *buf_create_from_mem(const char *ptr, size_t len) {86static inline Buf *buf_create_from_mem(const char *ptr, size_t len) {
87 assert(len != SIZE_MAX);87 assert(len != SIZE_MAX);
88 Buf *buf = allocate<Buf>(1);88 Buf *buf = heap::c_allocator.create<Buf>();
89 buf_init_from_mem(buf, ptr, len);89 buf_init_from_mem(buf, ptr, len);
90 return buf;90 return buf;
91}91}
...@@ -108,7 +108,7 @@ static inline Buf *buf_slice(Buf *in_buf, size_t start, size_t end) {...@@ -108,7 +108,7 @@ static inline Buf *buf_slice(Buf *in_buf, size_t start, size_t end) {
108 assert(end != SIZE_MAX);108 assert(end != SIZE_MAX);
109 assert(start < buf_len(in_buf));109 assert(start < buf_len(in_buf));
110 assert(end <= buf_len(in_buf));110 assert(end <= buf_len(in_buf));
111 Buf *out_buf = allocate<Buf>(1);111 Buf *out_buf = heap::c_allocator.create<Buf>();
112 out_buf->list.resize(end - start + 1);112 out_buf->list.resize(end - start + 1);
113 memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start);113 memcpy(buf_ptr(out_buf), buf_ptr(in_buf) + start, end - start);
114 out_buf->list.at(buf_len(out_buf)) = 0;114 out_buf->list.at(buf_len(out_buf)) = 0;
...@@ -211,5 +211,4 @@ static inline void buf_replace(Buf* buf, char from, char to) {...@@ -211,5 +211,4 @@ static inline void buf_replace(Buf* buf, char from, char to) {
211 }211 }
212}212}
213213
214
215#endif214#endif
src/codegen.cpp+40-35
...@@ -21,6 +21,7 @@...@@ -21,6 +21,7 @@
21#include "userland.h"21#include "userland.h"
22#include "dump_analysis.hpp"22#include "dump_analysis.hpp"
23#include "softfloat.hpp"23#include "softfloat.hpp"
24#include "mem_profile.hpp"
2425
25#include <stdio.h>26#include <stdio.h>
26#include <errno.h>27#include <errno.h>
...@@ -57,7 +58,7 @@ static void init_darwin_native(CodeGen *g) {...@@ -57,7 +58,7 @@ static void init_darwin_native(CodeGen *g) {
57}58}
5859
59static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path, const char *pkg_path) {60static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path, const char *pkg_path) {
60 ZigPackage *entry = allocate<ZigPackage>(1);61 ZigPackage *entry = heap::c_allocator.create<ZigPackage>();
61 entry->package_table.init(4);62 entry->package_table.init(4);
62 buf_init_from_str(&entry->root_src_dir, root_src_dir);63 buf_init_from_str(&entry->root_src_dir, root_src_dir);
63 buf_init_from_str(&entry->root_src_path, root_src_path);64 buf_init_from_str(&entry->root_src_path, root_src_path);
...@@ -4323,7 +4324,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrIn...@@ -4323,7 +4324,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutableGen *executable, IrIn
4323 }4324 }
4324 size_t field_count = arg_calc.field_index;4325 size_t field_count = arg_calc.field_index;
43254326
4326 LLVMTypeRef *field_types = allocate_nonzero<LLVMTypeRef>(field_count);4327 LLVMTypeRef *field_types = heap::c_allocator.allocate_nonzero<LLVMTypeRef>(field_count);
4327 LLVMGetStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc)), field_types);4328 LLVMGetStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc)), field_types);
4328 assert(LLVMCountStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc))) == arg_calc_start.field_index);4329 assert(LLVMCountStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc))) == arg_calc_start.field_index);
43294330
...@@ -4676,8 +4677,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, IrExecutableGen *executable, I...@@ -4676,8 +4677,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, IrExecutableGen *executable, I
4676 instruction->return_count;4677 instruction->return_count;
4677 size_t total_index = 0;4678 size_t total_index = 0;
4678 size_t param_index = 0;4679 size_t param_index = 0;
4679 LLVMTypeRef *param_types = allocate<LLVMTypeRef>(input_and_output_count);4680 LLVMTypeRef *param_types = heap::c_allocator.allocate<LLVMTypeRef>(input_and_output_count);
4680 LLVMValueRef *param_values = allocate<LLVMValueRef>(input_and_output_count);4681 LLVMValueRef *param_values = heap::c_allocator.allocate<LLVMValueRef>(input_and_output_count);
4681 for (size_t i = 0; i < asm_expr->output_list.length; i += 1, total_index += 1) {4682 for (size_t i = 0; i < asm_expr->output_list.length; i += 1, total_index += 1) {
4682 AsmOutput *asm_output = asm_expr->output_list.at(i);4683 AsmOutput *asm_output = asm_expr->output_list.at(i);
4683 bool is_return = (asm_output->return_type != nullptr);4684 bool is_return = (asm_output->return_type != nullptr);
...@@ -4919,7 +4920,7 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutableGen *execut...@@ -4919,7 +4920,7 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutableGen *execut
4919 // second vector. These start at -1 and go down, and are easiest to use4920 // second vector. These start at -1 and go down, and are easiest to use
4920 // with the ~ operator. Here we convert between the two formats.4921 // with the ~ operator. Here we convert between the two formats.
4921 IrInstGen *mask = instruction->mask;4922 IrInstGen *mask = instruction->mask;
4922 LLVMValueRef *values = allocate<LLVMValueRef>(len_mask);4923 LLVMValueRef *values = heap::c_allocator.allocate<LLVMValueRef>(len_mask);
4923 for (uint64_t i = 0; i < len_mask; i++) {4924 for (uint64_t i = 0; i < len_mask; i++) {
4924 if (mask->value->data.x_array.data.s_none.elements[i].special == ConstValSpecialUndef) {4925 if (mask->value->data.x_array.data.s_none.elements[i].special == ConstValSpecialUndef) {
4925 values[i] = LLVMGetUndef(LLVMInt32Type());4926 values[i] = LLVMGetUndef(LLVMInt32Type());
...@@ -4931,7 +4932,7 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutableGen *execut...@@ -4931,7 +4932,7 @@ static LLVMValueRef ir_render_shuffle_vector(CodeGen *g, IrExecutableGen *execut
4931 }4932 }
49324933
4933 LLVMValueRef llvm_mask_value = LLVMConstVector(values, len_mask);4934 LLVMValueRef llvm_mask_value = LLVMConstVector(values, len_mask);
4934 free(values);4935 heap::c_allocator.deallocate(values, len_mask);
49354936
4936 return LLVMBuildShuffleVector(g->builder,4937 return LLVMBuildShuffleVector(g->builder,
4937 ir_llvm_value(g, instruction->a),4938 ir_llvm_value(g, instruction->a),
...@@ -4999,8 +5000,8 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutableGen *executable, IrIns...@@ -4999,8 +5000,8 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutableGen *executable, IrIns
4999 }5000 }
50005001
5001 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");5002 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");
5002 LLVMValueRef *incoming_values = allocate<LLVMValueRef>(instruction->incoming_count);5003 LLVMValueRef *incoming_values = heap::c_allocator.allocate<LLVMValueRef>(instruction->incoming_count);
5003 LLVMBasicBlockRef *incoming_blocks = allocate<LLVMBasicBlockRef>(instruction->incoming_count);5004 LLVMBasicBlockRef *incoming_blocks = heap::c_allocator.allocate<LLVMBasicBlockRef>(instruction->incoming_count);
5004 for (size_t i = 0; i < instruction->incoming_count; i += 1) {5005 for (size_t i = 0; i < instruction->incoming_count; i += 1) {
5005 incoming_values[i] = ir_llvm_value(g, instruction->incoming_values[i]);5006 incoming_values[i] = ir_llvm_value(g, instruction->incoming_values[i]);
5006 incoming_blocks[i] = instruction->incoming_blocks[i]->llvm_exit_block;5007 incoming_blocks[i] = instruction->incoming_blocks[i]->llvm_exit_block;
...@@ -5972,12 +5973,12 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutableGen *executable, IrI...@@ -5972,12 +5973,12 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutableGen *executable, IrI
5972 LLVMValueRef shift_amt = LLVMConstInt(get_llvm_type(g, extended_type), 8, false);5973 LLVMValueRef shift_amt = LLVMConstInt(get_llvm_type(g, extended_type), 8, false);
5973 if (is_vector) {5974 if (is_vector) {
5974 extended_type = get_vector_type(g, expr_type->data.vector.len, extended_type);5975 extended_type = get_vector_type(g, expr_type->data.vector.len, extended_type);
5975 LLVMValueRef *values = allocate_nonzero<LLVMValueRef>(expr_type->data.vector.len);5976 LLVMValueRef *values = heap::c_allocator.allocate_nonzero<LLVMValueRef>(expr_type->data.vector.len);
5976 for (uint32_t i = 0; i < expr_type->data.vector.len; i += 1) {5977 for (uint32_t i = 0; i < expr_type->data.vector.len; i += 1) {
5977 values[i] = shift_amt;5978 values[i] = shift_amt;
5978 }5979 }
5979 shift_amt = LLVMConstVector(values, expr_type->data.vector.len);5980 shift_amt = LLVMConstVector(values, expr_type->data.vector.len);
5980 free(values);5981 heap::c_allocator.deallocate(values, expr_type->data.vector.len);
5981 }5982 }
5982 // aabbcc5983 // aabbcc
5983 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, get_llvm_type(g, extended_type), "");5984 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, get_llvm_type(g, extended_type), "");
...@@ -7010,7 +7011,7 @@ check: switch (const_val->special) {...@@ -7010,7 +7011,7 @@ check: switch (const_val->special) {
7010 }7011 }
7011 case ZigTypeIdStruct:7012 case ZigTypeIdStruct:
7012 {7013 {
7013 LLVMValueRef *fields = allocate<LLVMValueRef>(type_entry->data.structure.gen_field_count);7014 LLVMValueRef *fields = heap::c_allocator.allocate<LLVMValueRef>(type_entry->data.structure.gen_field_count);
7014 size_t src_field_count = type_entry->data.structure.src_field_count;7015 size_t src_field_count = type_entry->data.structure.src_field_count;
7015 bool make_unnamed_struct = false;7016 bool make_unnamed_struct = false;
7016 assert(type_entry->data.structure.resolve_status == ResolveStatusLLVMFull);7017 assert(type_entry->data.structure.resolve_status == ResolveStatusLLVMFull);
...@@ -7069,7 +7070,7 @@ check: switch (const_val->special) {...@@ -7069,7 +7070,7 @@ check: switch (const_val->special) {
7069 } else {7070 } else {
7070 const LLVMValueRef AMT = LLVMConstInt(LLVMTypeOf(val), 8, false);7071 const LLVMValueRef AMT = LLVMConstInt(LLVMTypeOf(val), 8, false);
70717072
7072 LLVMValueRef *values = allocate<LLVMValueRef>(size_in_bytes);7073 LLVMValueRef *values = heap::c_allocator.allocate<LLVMValueRef>(size_in_bytes);
7073 for (size_t i = 0; i < size_in_bytes; i++) {7074 for (size_t i = 0; i < size_in_bytes; i++) {
7074 const size_t idx = is_big_endian ? size_in_bytes - 1 - i : i;7075 const size_t idx = is_big_endian ? size_in_bytes - 1 - i : i;
7075 values[idx] = LLVMConstTruncOrBitCast(val, LLVMInt8Type());7076 values[idx] = LLVMConstTruncOrBitCast(val, LLVMInt8Type());
...@@ -7133,7 +7134,7 @@ check: switch (const_val->special) {...@@ -7133,7 +7134,7 @@ check: switch (const_val->special) {
7133 case ConstArraySpecialNone: {7134 case ConstArraySpecialNone: {
7134 uint64_t extra_len_from_sentinel = (type_entry->data.array.sentinel != nullptr) ? 1 : 0;7135 uint64_t extra_len_from_sentinel = (type_entry->data.array.sentinel != nullptr) ? 1 : 0;
7135 uint64_t full_len = len + extra_len_from_sentinel;7136 uint64_t full_len = len + extra_len_from_sentinel;
7136 LLVMValueRef *values = allocate<LLVMValueRef>(full_len);7137 LLVMValueRef *values = heap::c_allocator.allocate<LLVMValueRef>(full_len);
7137 LLVMTypeRef element_type_ref = get_llvm_type(g, type_entry->data.array.child_type);7138 LLVMTypeRef element_type_ref = get_llvm_type(g, type_entry->data.array.child_type);
7138 bool make_unnamed_struct = false;7139 bool make_unnamed_struct = false;
7139 for (uint64_t i = 0; i < len; i += 1) {7140 for (uint64_t i = 0; i < len; i += 1) {
...@@ -7165,7 +7166,7 @@ check: switch (const_val->special) {...@@ -7165,7 +7166,7 @@ check: switch (const_val->special) {
7165 case ConstArraySpecialUndef:7166 case ConstArraySpecialUndef:
7166 return LLVMGetUndef(get_llvm_type(g, type_entry));7167 return LLVMGetUndef(get_llvm_type(g, type_entry));
7167 case ConstArraySpecialNone: {7168 case ConstArraySpecialNone: {
7168 LLVMValueRef *values = allocate<LLVMValueRef>(len);7169 LLVMValueRef *values = heap::c_allocator.allocate<LLVMValueRef>(len);
7169 for (uint64_t i = 0; i < len; i += 1) {7170 for (uint64_t i = 0; i < len; i += 1) {
7170 ZigValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];7171 ZigValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];
7171 values[i] = gen_const_val(g, elem_value, "");7172 values[i] = gen_const_val(g, elem_value, "");
...@@ -7175,7 +7176,7 @@ check: switch (const_val->special) {...@@ -7175,7 +7176,7 @@ check: switch (const_val->special) {
7175 case ConstArraySpecialBuf: {7176 case ConstArraySpecialBuf: {
7176 Buf *buf = const_val->data.x_array.data.s_buf;7177 Buf *buf = const_val->data.x_array.data.s_buf;
7177 assert(buf_len(buf) == len);7178 assert(buf_len(buf) == len);
7178 LLVMValueRef *values = allocate<LLVMValueRef>(len);7179 LLVMValueRef *values = heap::c_allocator.allocate<LLVMValueRef>(len);
7179 for (uint64_t i = 0; i < len; i += 1) {7180 for (uint64_t i = 0; i < len; i += 1) {
7180 values[i] = LLVMConstInt(g->builtin_types.entry_u8->llvm_type, buf_ptr(buf)[i], false);7181 values[i] = LLVMConstInt(g->builtin_types.entry_u8->llvm_type, buf_ptr(buf)[i], false);
7181 }7182 }
...@@ -7377,7 +7378,7 @@ static void generate_error_name_table(CodeGen *g) {...@@ -7377,7 +7378,7 @@ static void generate_error_name_table(CodeGen *g) {
7377 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);7378 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
7378 ZigType *str_type = get_slice_type(g, u8_ptr_type);7379 ZigType *str_type = get_slice_type(g, u8_ptr_type);
73797380
7380 LLVMValueRef *values = allocate<LLVMValueRef>(g->errors_by_index.length);7381 LLVMValueRef *values = heap::c_allocator.allocate<LLVMValueRef>(g->errors_by_index.length);
7381 values[0] = LLVMGetUndef(get_llvm_type(g, str_type));7382 values[0] = LLVMGetUndef(get_llvm_type(g, str_type));
7382 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {7383 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
7383 ErrorTableEntry *err_entry = g->errors_by_index.at(i);7384 ErrorTableEntry *err_entry = g->errors_by_index.at(i);
...@@ -7906,6 +7907,9 @@ static void do_code_gen(CodeGen *g) {...@@ -7906,6 +7907,9 @@ static void do_code_gen(CodeGen *g) {
7906}7907}
79077908
7908static void zig_llvm_emit_output(CodeGen *g) {7909static void zig_llvm_emit_output(CodeGen *g) {
7910 g->pass1_arena->destruct(&heap::c_allocator);
7911 g->pass1_arena = nullptr;
7912
7909 bool is_small = g->build_mode == BuildModeSmallRelease;7913 bool is_small = g->build_mode == BuildModeSmallRelease;
79107914
7911 Buf *output_path = &g->o_file_output_path;7915 Buf *output_path = &g->o_file_output_path;
...@@ -8202,7 +8206,7 @@ static void define_intern_values(CodeGen *g) {...@@ -8202,7 +8206,7 @@ static void define_intern_values(CodeGen *g) {
8202}8206}
82038207
8204static BuiltinFnEntry *create_builtin_fn(CodeGen *g, BuiltinFnId id, const char *name, size_t count) {8208static BuiltinFnEntry *create_builtin_fn(CodeGen *g, BuiltinFnId id, const char *name, size_t count) {
8205 BuiltinFnEntry *builtin_fn = allocate<BuiltinFnEntry>(1);8209 BuiltinFnEntry *builtin_fn = heap::c_allocator.create<BuiltinFnEntry>();
8206 buf_init_from_str(&builtin_fn->name, name);8210 buf_init_from_str(&builtin_fn->name, name);
8207 builtin_fn->id = id;8211 builtin_fn->id = id;
8208 builtin_fn->param_count = count;8212 builtin_fn->param_count = count;
...@@ -8919,16 +8923,16 @@ static void init(CodeGen *g) {...@@ -8919,16 +8923,16 @@ static void init(CodeGen *g) {
8919 define_builtin_types(g);8923 define_builtin_types(g);
8920 define_intern_values(g);8924 define_intern_values(g);
89218925
8922 IrInstGen *sentinel_instructions = allocate<IrInstGen>(2);8926 IrInstGen *sentinel_instructions = heap::c_allocator.allocate<IrInstGen>(2);
8923 g->invalid_inst_gen = &sentinel_instructions[0];8927 g->invalid_inst_gen = &sentinel_instructions[0];
8924 g->invalid_inst_gen->value = allocate<ZigValue>(1, "ZigValue");8928 g->invalid_inst_gen->value = g->pass1_arena->create<ZigValue>();
8925 g->invalid_inst_gen->value->type = g->builtin_types.entry_invalid;8929 g->invalid_inst_gen->value->type = g->builtin_types.entry_invalid;
89268930
8927 g->unreach_instruction = &sentinel_instructions[1];8931 g->unreach_instruction = &sentinel_instructions[1];
8928 g->unreach_instruction->value = allocate<ZigValue>(1, "ZigValue");8932 g->unreach_instruction->value = g->pass1_arena->create<ZigValue>();
8929 g->unreach_instruction->value->type = g->builtin_types.entry_unreachable;8933 g->unreach_instruction->value->type = g->builtin_types.entry_unreachable;
89308934
8931 g->invalid_inst_src = allocate<IrInstSrc>(1);8935 g->invalid_inst_src = heap::c_allocator.create<IrInstSrc>();
89328936
8933 define_builtin_fns(g);8937 define_builtin_fns(g);
8934 Error err;8938 Error err;
...@@ -9010,7 +9014,7 @@ static void detect_libc(CodeGen *g) {...@@ -9010,7 +9014,7 @@ static void detect_libc(CodeGen *g) {
9010 buf_ptr(g->zig_lib_dir), target_os_name(g->zig_target->os));9014 buf_ptr(g->zig_lib_dir), target_os_name(g->zig_target->os));
90119015
9012 g->libc_include_dir_len = 4;9016 g->libc_include_dir_len = 4;
9013 g->libc_include_dir_list = allocate<Buf*>(g->libc_include_dir_len);9017 g->libc_include_dir_list = heap::c_allocator.allocate<Buf*>(g->libc_include_dir_len);
9014 g->libc_include_dir_list[0] = arch_include_dir;9018 g->libc_include_dir_list[0] = arch_include_dir;
9015 g->libc_include_dir_list[1] = generic_include_dir;9019 g->libc_include_dir_list[1] = generic_include_dir;
9016 g->libc_include_dir_list[2] = arch_os_include_dir;9020 g->libc_include_dir_list[2] = arch_os_include_dir;
...@@ -9019,7 +9023,7 @@ static void detect_libc(CodeGen *g) {...@@ -9019,7 +9023,7 @@ static void detect_libc(CodeGen *g) {
9019 }9023 }
90209024
9021 if (g->zig_target->is_native) {9025 if (g->zig_target->is_native) {
9022 g->libc = allocate<ZigLibCInstallation>(1);9026 g->libc = heap::c_allocator.create<ZigLibCInstallation>();
90239027
9024 // search for native_libc.txt in following dirs:9028 // search for native_libc.txt in following dirs:
9025 // - LOCAL_CACHE_DIR9029 // - LOCAL_CACHE_DIR
...@@ -9099,7 +9103,7 @@ static void detect_libc(CodeGen *g) {...@@ -9099,7 +9103,7 @@ static void detect_libc(CodeGen *g) {
9099 size_t want_um_and_shared_dirs = (g->zig_target->os == OsWindows) ? 2 : 0;9103 size_t want_um_and_shared_dirs = (g->zig_target->os == OsWindows) ? 2 : 0;
9100 size_t dir_count = 1 + want_sys_dir + want_um_and_shared_dirs;9104 size_t dir_count = 1 + want_sys_dir + want_um_and_shared_dirs;
9101 g->libc_include_dir_len = 0;9105 g->libc_include_dir_len = 0;
9102 g->libc_include_dir_list = allocate<Buf*>(dir_count);9106 g->libc_include_dir_list = heap::c_allocator.allocate<Buf*>(dir_count);
91039107
9104 g->libc_include_dir_list[g->libc_include_dir_len] = &g->libc->include_dir;9108 g->libc_include_dir_list[g->libc_include_dir_len] = &g->libc->include_dir;
9105 g->libc_include_dir_len += 1;9109 g->libc_include_dir_len += 1;
...@@ -9466,10 +9470,10 @@ static void update_test_functions_builtin_decl(CodeGen *g) {...@@ -9466,10 +9470,10 @@ static void update_test_functions_builtin_decl(CodeGen *g) {
9466 if ((err = type_resolve(g, struct_type, ResolveStatusSizeKnown)))9470 if ((err = type_resolve(g, struct_type, ResolveStatusSizeKnown)))
9467 zig_unreachable();9471 zig_unreachable();
94689472
9469 ZigValue *test_fn_array = create_const_vals(1);9473 ZigValue *test_fn_array = g->pass1_arena->create<ZigValue>();
9470 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length, nullptr);9474 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length, nullptr);
9471 test_fn_array->special = ConstValSpecialStatic;9475 test_fn_array->special = ConstValSpecialStatic;
9472 test_fn_array->data.x_array.data.s_none.elements = create_const_vals(g->test_fns.length);9476 test_fn_array->data.x_array.data.s_none.elements = g->pass1_arena->allocate<ZigValue>(g->test_fns.length);
94739477
9474 for (size_t i = 0; i < g->test_fns.length; i += 1) {9478 for (size_t i = 0; i < g->test_fns.length; i += 1) {
9475 ZigFn *test_fn_entry = g->test_fns.at(i);9479 ZigFn *test_fn_entry = g->test_fns.at(i);
...@@ -9480,7 +9484,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {...@@ -9480,7 +9484,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {
9480 this_val->parent.id = ConstParentIdArray;9484 this_val->parent.id = ConstParentIdArray;
9481 this_val->parent.data.p_array.array_val = test_fn_array;9485 this_val->parent.data.p_array.array_val = test_fn_array;
9482 this_val->parent.data.p_array.elem_index = i;9486 this_val->parent.data.p_array.elem_index = i;
9483 this_val->data.x_struct.fields = alloc_const_vals_ptrs(3);9487 this_val->data.x_struct.fields = alloc_const_vals_ptrs(g, 3);
94849488
9485 ZigValue *name_field = this_val->data.x_struct.fields[0];9489 ZigValue *name_field = this_val->data.x_struct.fields[0];
9486 ZigValue *name_array_val = create_const_str_lit(g, &test_fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;9490 ZigValue *name_array_val = create_const_str_lit(g, &test_fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
...@@ -9499,7 +9503,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {...@@ -9499,7 +9503,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {
9499 frame_size_field->data.x_optional = nullptr;9503 frame_size_field->data.x_optional = nullptr;
95009504
9501 if (fn_is_async(test_fn_entry)) {9505 if (fn_is_async(test_fn_entry)) {
9502 frame_size_field->data.x_optional = create_const_vals(1);9506 frame_size_field->data.x_optional = g->pass1_arena->create<ZigValue>();
9503 frame_size_field->data.x_optional->special = ConstValSpecialStatic;9507 frame_size_field->data.x_optional->special = ConstValSpecialStatic;
9504 frame_size_field->data.x_optional->type = g->builtin_types.entry_usize;9508 frame_size_field->data.x_optional->type = g->builtin_types.entry_usize;
9505 bigint_init_unsigned(&frame_size_field->data.x_optional->data.x_bigint,9509 bigint_init_unsigned(&frame_size_field->data.x_optional->data.x_bigint,
...@@ -9634,7 +9638,7 @@ static Error get_tmp_filename(CodeGen *g, Buf *out, Buf *suffix) {...@@ -9634,7 +9638,7 @@ static Error get_tmp_filename(CodeGen *g, Buf *out, Buf *suffix) {
96349638
9635Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose) {9639Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose) {
9636 Error err;9640 Error err;
9637 CacheHash *cache_hash = allocate<CacheHash>(1);9641 CacheHash *cache_hash = heap::c_allocator.create<CacheHash>();
9638 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(g->cache_dir));9642 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(g->cache_dir));
9639 cache_init(cache_hash, manifest_dir);9643 cache_init(cache_hash, manifest_dir);
96409644
...@@ -10788,7 +10792,8 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget...@@ -10788,7 +10792,8 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget
10788 OutType out_type, BuildMode build_mode, Buf *override_lib_dir,10792 OutType out_type, BuildMode build_mode, Buf *override_lib_dir,
10789 ZigLibCInstallation *libc, Buf *cache_dir, bool is_test_build, Stage2ProgressNode *progress_node)10793 ZigLibCInstallation *libc, Buf *cache_dir, bool is_test_build, Stage2ProgressNode *progress_node)
10790{10794{
10791 CodeGen *g = allocate<CodeGen>(1);10795 CodeGen *g = heap::c_allocator.create<CodeGen>();
10796 g->pass1_arena = heap::ArenaAllocator::construct(&heap::c_allocator, &heap::c_allocator, "pass1");
10792 g->main_progress_node = progress_node;10797 g->main_progress_node = progress_node;
1079310798
10794 codegen_add_time_event(g, "Initialize");10799 codegen_add_time_event(g, "Initialize");
...@@ -10931,35 +10936,35 @@ void codegen_switch_sub_prog_node(CodeGen *g, Stage2ProgressNode *node) {...@@ -10931,35 +10936,35 @@ void codegen_switch_sub_prog_node(CodeGen *g, Stage2ProgressNode *node) {
1093110936
10932ZigValue *CodeGen::Intern::for_undefined() {10937ZigValue *CodeGen::Intern::for_undefined() {
10933#ifdef ZIG_ENABLE_MEM_PROFILE10938#ifdef ZIG_ENABLE_MEM_PROFILE
10934 memprof_intern_count.x_undefined += 1;10939 mem::intern_counters.x_undefined += 1;
10935#endif10940#endif
10936 return &this->x_undefined;10941 return &this->x_undefined;
10937}10942}
1093810943
10939ZigValue *CodeGen::Intern::for_void() {10944ZigValue *CodeGen::Intern::for_void() {
10940#ifdef ZIG_ENABLE_MEM_PROFILE10945#ifdef ZIG_ENABLE_MEM_PROFILE
10941 memprof_intern_count.x_void += 1;10946 mem::intern_counters.x_void += 1;
10942#endif10947#endif
10943 return &this->x_void;10948 return &this->x_void;
10944}10949}
1094510950
10946ZigValue *CodeGen::Intern::for_null() {10951ZigValue *CodeGen::Intern::for_null() {
10947#ifdef ZIG_ENABLE_MEM_PROFILE10952#ifdef ZIG_ENABLE_MEM_PROFILE
10948 memprof_intern_count.x_null += 1;10953 mem::intern_counters.x_null += 1;
10949#endif10954#endif
10950 return &this->x_null;10955 return &this->x_null;
10951}10956}
1095210957
10953ZigValue *CodeGen::Intern::for_unreachable() {10958ZigValue *CodeGen::Intern::for_unreachable() {
10954#ifdef ZIG_ENABLE_MEM_PROFILE10959#ifdef ZIG_ENABLE_MEM_PROFILE
10955 memprof_intern_count.x_unreachable += 1;10960 mem::intern_counters.x_unreachable += 1;
10956#endif10961#endif
10957 return &this->x_unreachable;10962 return &this->x_unreachable;
10958}10963}
1095910964
10960ZigValue *CodeGen::Intern::for_zero_byte() {10965ZigValue *CodeGen::Intern::for_zero_byte() {
10961#ifdef ZIG_ENABLE_MEM_PROFILE10966#ifdef ZIG_ENABLE_MEM_PROFILE
10962 memprof_intern_count.zero_byte += 1;10967 mem::intern_counters.zero_byte += 1;
10963#endif10968#endif
10964 return &this->zero_byte;10969 return &this->zero_byte;
10965}10970}
src/errmsg.cpp+2-2
...@@ -99,7 +99,7 @@ void err_msg_add_note(ErrorMsg *parent, ErrorMsg *note) {...@@ -99,7 +99,7 @@ void err_msg_add_note(ErrorMsg *parent, ErrorMsg *note) {
99ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size_t offset,99ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size_t offset,
100 const char *source, Buf *msg)100 const char *source, Buf *msg)
101{101{
102 ErrorMsg *err_msg = allocate<ErrorMsg>(1);102 ErrorMsg *err_msg = heap::c_allocator.create<ErrorMsg>();
103 err_msg->path = path;103 err_msg->path = path;
104 err_msg->line_start = line;104 err_msg->line_start = line;
105 err_msg->column_start = column;105 err_msg->column_start = column;
...@@ -138,7 +138,7 @@ ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size...@@ -138,7 +138,7 @@ ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size
138ErrorMsg *err_msg_create_with_line(Buf *path, size_t line, size_t column,138ErrorMsg *err_msg_create_with_line(Buf *path, size_t line, size_t column,
139 Buf *source, ZigList<size_t> *line_offsets, Buf *msg)139 Buf *source, ZigList<size_t> *line_offsets, Buf *msg)
140{140{
141 ErrorMsg *err_msg = allocate<ErrorMsg>(1);141 ErrorMsg *err_msg = heap::c_allocator.create<ErrorMsg>();
142 err_msg->path = path;142 err_msg->path = path;
143 err_msg->line_start = line;143 err_msg->line_start = line;
144 err_msg->column_start = column;144 err_msg->column_start = column;
src/glibc.cpp+4-4
...@@ -21,7 +21,7 @@ static const ZigGLibCLib glibc_libs[] = {...@@ -21,7 +21,7 @@ static const ZigGLibCLib glibc_libs[] = {
21Error glibc_load_metadata(ZigGLibCAbi **out_result, Buf *zig_lib_dir, bool verbose) {21Error glibc_load_metadata(ZigGLibCAbi **out_result, Buf *zig_lib_dir, bool verbose) {
22 Error err;22 Error err;
2323
24 ZigGLibCAbi *glibc_abi = allocate<ZigGLibCAbi>(1);24 ZigGLibCAbi *glibc_abi = heap::c_allocator.create<ZigGLibCAbi>();
25 glibc_abi->vers_txt_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "glibc" OS_SEP "vers.txt", buf_ptr(zig_lib_dir));25 glibc_abi->vers_txt_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "glibc" OS_SEP "vers.txt", buf_ptr(zig_lib_dir));
26 glibc_abi->fns_txt_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "glibc" OS_SEP "fns.txt", buf_ptr(zig_lib_dir));26 glibc_abi->fns_txt_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "glibc" OS_SEP "fns.txt", buf_ptr(zig_lib_dir));
27 glibc_abi->abi_txt_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "glibc" OS_SEP "abi.txt", buf_ptr(zig_lib_dir));27 glibc_abi->abi_txt_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "glibc" OS_SEP "abi.txt", buf_ptr(zig_lib_dir));
...@@ -100,10 +100,10 @@ Error glibc_load_metadata(ZigGLibCAbi **out_result, Buf *zig_lib_dir, bool verbo...@@ -100,10 +100,10 @@ Error glibc_load_metadata(ZigGLibCAbi **out_result, Buf *zig_lib_dir, bool verbo
100 Optional<Slice<uint8_t>> opt_line = SplitIterator_next_separate(&it);100 Optional<Slice<uint8_t>> opt_line = SplitIterator_next_separate(&it);
101 if (!opt_line.is_some) break;101 if (!opt_line.is_some) break;
102102
103 ver_list_base = allocate<ZigGLibCVerList>(glibc_abi->all_functions.length);103 ver_list_base = heap::c_allocator.allocate<ZigGLibCVerList>(glibc_abi->all_functions.length);
104 SplitIterator line_it = memSplit(opt_line.value, str(" "));104 SplitIterator line_it = memSplit(opt_line.value, str(" "));
105 for (;;) {105 for (;;) {
106 ZigTarget *target = allocate<ZigTarget>(1);106 ZigTarget *target = heap::c_allocator.create<ZigTarget>();
107 Optional<Slice<uint8_t>> opt_target = SplitIterator_next(&line_it);107 Optional<Slice<uint8_t>> opt_target = SplitIterator_next(&line_it);
108 if (!opt_target.is_some) break;108 if (!opt_target.is_some) break;
109109
...@@ -174,7 +174,7 @@ Error glibc_build_dummies_and_maps(CodeGen *g, const ZigGLibCAbi *glibc_abi, con...@@ -174,7 +174,7 @@ Error glibc_build_dummies_and_maps(CodeGen *g, const ZigGLibCAbi *glibc_abi, con
174 Error err;174 Error err;
175175
176 Buf *cache_dir = get_global_cache_dir();176 Buf *cache_dir = get_global_cache_dir();
177 CacheHash *cache_hash = allocate<CacheHash>(1);177 CacheHash *cache_hash = heap::c_allocator.create<CacheHash>();
178 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));178 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));
179 cache_init(cache_hash, manifest_dir);179 cache_init(cache_hash, manifest_dir);
180180
src/hash_map.hpp+3-3
...@@ -19,7 +19,7 @@ public:...@@ -19,7 +19,7 @@ public:
19 init_capacity(capacity);19 init_capacity(capacity);
20 }20 }
21 void deinit(void) {21 void deinit(void) {
22 free(_entries);22 heap::c_allocator.deallocate(_entries, _capacity);
23 }23 }
2424
25 struct Entry {25 struct Entry {
...@@ -57,7 +57,7 @@ public:...@@ -57,7 +57,7 @@ public:
57 if (old_entry->used)57 if (old_entry->used)
58 internal_put(old_entry->key, old_entry->value);58 internal_put(old_entry->key, old_entry->value);
59 }59 }
60 free(old_entries);60 heap::c_allocator.deallocate(old_entries, old_capacity);
61 }61 }
62 }62 }
6363
...@@ -164,7 +164,7 @@ private:...@@ -164,7 +164,7 @@ private:
164164
165 void init_capacity(int capacity) {165 void init_capacity(int capacity) {
166 _capacity = capacity;166 _capacity = capacity;
167 _entries = allocate<Entry>(_capacity);167 _entries = heap::c_allocator.allocate<Entry>(_capacity);
168 _size = 0;168 _size = 0;
169 _max_distance_from_start_index = 0;169 _max_distance_from_start_index = 0;
170 for (int i = 0; i < _capacity; i += 1) {170 for (int i = 0; i < _capacity; i += 1) {
src/heap.cpp created+377
...@@ -0,0 +1,377 @@
1/*
2 * Copyright (c) 2020 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#include <new>
9#include <string.h>
10
11#include "config.h"
12#include "heap.hpp"
13#include "mem_profile.hpp"
14
15namespace heap {
16
17extern mem::Allocator &bootstrap_allocator;
18
19//
20// BootstrapAllocator implementation is identical to CAllocator minus
21// profile profile functionality. Splitting off to a base interface doesn't
22// seem worthwhile.
23//
24
25void BootstrapAllocator::init(const char *name) {}
26void BootstrapAllocator::deinit() {}
27
28void *BootstrapAllocator::internal_allocate(const mem::TypeInfo &info, size_t count) {
29 return mem::os::calloc(count, info.size);
30}
31
32void *BootstrapAllocator::internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) {
33 return mem::os::malloc(count * info.size);
34}
35
36void *BootstrapAllocator::internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) {
37 auto new_ptr = this->internal_reallocate_nonzero(info, old_ptr, old_count, new_count);
38 if (new_count > old_count)
39 memset(reinterpret_cast<uint8_t *>(new_ptr) + (old_count * info.size), 0, (new_count - old_count) * info.size);
40 return new_ptr;
41}
42
43void *BootstrapAllocator::internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) {
44 return mem::os::realloc(old_ptr, new_count * info.size);
45}
46
47void BootstrapAllocator::internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) {
48 mem::os::free(ptr);
49}
50
51void CAllocator::init(const char *name) {
52#ifdef ZIG_ENABLE_MEM_PROFILE
53 this->profile = bootstrap_allocator.create<mem::Profile>();
54 this->profile->init(name, "CAllocator");
55#endif
56}
57
58void CAllocator::deinit() {
59#ifdef ZIG_ENABLE_MEM_PROFILE
60 assert(this->profile);
61 this->profile->deinit();
62 bootstrap_allocator.destroy(this->profile);
63 this->profile = nullptr;
64#endif
65}
66
67CAllocator *CAllocator::construct(mem::Allocator *allocator, const char *name) {
68 auto p = new(allocator->create<CAllocator>()) CAllocator();
69 p->init(name);
70 return p;
71}
72
73void CAllocator::destruct(mem::Allocator *allocator) {
74 this->deinit();
75 allocator->destroy(this);
76}
77
78#ifdef ZIG_ENABLE_MEM_PROFILE
79void CAllocator::print_report(FILE *file) {
80 this->profile->print_report(file);
81}
82#endif
83
84void *CAllocator::internal_allocate(const mem::TypeInfo &info, size_t count) {
85#ifdef ZIG_ENABLE_MEM_PROFILE
86 this->profile->record_alloc(info, count);
87#endif
88 return mem::os::calloc(count, info.size);
89}
90
91void *CAllocator::internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) {
92#ifdef ZIG_ENABLE_MEM_PROFILE
93 this->profile->record_alloc(info, count);
94#endif
95 return mem::os::malloc(count * info.size);
96}
97
98void *CAllocator::internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) {
99 auto new_ptr = this->internal_reallocate_nonzero(info, old_ptr, old_count, new_count);
100 if (new_count > old_count)
101 memset(reinterpret_cast<uint8_t *>(new_ptr) + (old_count * info.size), 0, (new_count - old_count) * info.size);
102 return new_ptr;
103}
104
105void *CAllocator::internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) {
106#ifdef ZIG_ENABLE_MEM_PROFILE
107 this->profile->record_dealloc(info, old_count);
108 this->profile->record_alloc(info, new_count);
109#endif
110 return mem::os::realloc(old_ptr, new_count * info.size);
111}
112
113void CAllocator::internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) {
114#ifdef ZIG_ENABLE_MEM_PROFILE
115 this->profile->record_dealloc(info, count);
116#endif
117 mem::os::free(ptr);
118}
119
120struct ArenaAllocator::Impl {
121 Allocator *backing;
122
123 // regular allocations bump through a segment of static size
124 struct Segment {
125 static constexpr size_t size = 65536;
126 static constexpr size_t object_threshold = 4096;
127
128 uint8_t data[size];
129 };
130
131 // active segment
132 Segment *segment;
133 size_t segment_offset;
134
135 // keep track of segments
136 struct SegmentTrack {
137 static constexpr size_t size = (4096 - sizeof(SegmentTrack *)) / sizeof(Segment *);
138
139 // null if first
140 SegmentTrack *prev;
141 Segment *segments[size];
142 };
143 static_assert(sizeof(SegmentTrack) <= 4096, "unwanted struct padding");
144
145 // active segment track
146 SegmentTrack *segment_track;
147 size_t segment_track_remain;
148
149 // individual allocations punted to backing allocator
150 struct Object {
151 uint8_t *ptr;
152 size_t len;
153 };
154
155 // keep track of objects
156 struct ObjectTrack {
157 static constexpr size_t size = (4096 - sizeof(ObjectTrack *)) / sizeof(Object);
158
159 // null if first
160 ObjectTrack *prev;
161 Object objects[size];
162 };
163 static_assert(sizeof(ObjectTrack) <= 4096, "unwanted struct padding");
164
165 // active object track
166 ObjectTrack *object_track;
167 size_t object_track_remain;
168
169 ATTRIBUTE_RETURNS_NOALIAS inline void *allocate(const mem::TypeInfo& info, size_t count);
170 inline void *reallocate(const mem::TypeInfo& info, void *old_ptr, size_t old_count, size_t new_count);
171
172 inline void new_segment();
173 inline void track_segment();
174 inline void track_object(Object object);
175};
176
177void *ArenaAllocator::Impl::allocate(const mem::TypeInfo& info, size_t count) {
178#ifndef NDEBUG
179 // make behavior when size == 0 portable
180 if (info.size == 0 || count == 0)
181 return nullptr;
182#endif
183 const size_t nbytes = info.size * count;
184 this->segment_offset = (this->segment_offset + (info.alignment - 1)) & ~(info.alignment - 1);
185 if (nbytes >= Segment::object_threshold) {
186 auto ptr = this->backing->allocate<uint8_t>(nbytes);
187 this->track_object({ptr, nbytes});
188 return ptr;
189 }
190 if (this->segment_offset + nbytes > Segment::size)
191 this->new_segment();
192 auto ptr = &this->segment->data[this->segment_offset];
193 this->segment_offset += nbytes;
194 return ptr;
195}
196
197void *ArenaAllocator::Impl::reallocate(const mem::TypeInfo& info, void *old_ptr, size_t old_count, size_t new_count) {
198#ifndef NDEBUG
199 // make behavior when size == 0 portable
200 if (info.size == 0 && old_ptr == nullptr)
201 return nullptr;
202#endif
203 const size_t new_nbytes = info.size * new_count;
204 if (new_nbytes <= info.size * old_count)
205 return old_ptr;
206 const size_t old_nbytes = info.size * old_count;
207 this->segment_offset = (this->segment_offset + (info.alignment - 1)) & ~(info.alignment - 1);
208 if (new_nbytes >= Segment::object_threshold) {
209 auto new_ptr = this->backing->allocate<uint8_t>(new_nbytes);
210 this->track_object({new_ptr, new_nbytes});
211 memcpy(new_ptr, old_ptr, old_nbytes);
212 return new_ptr;
213 }
214 if (this->segment_offset + new_nbytes > Segment::size)
215 this->new_segment();
216 auto new_ptr = &this->segment->data[this->segment_offset];
217 this->segment_offset += new_nbytes;
218 memcpy(new_ptr, old_ptr, old_nbytes);
219 return new_ptr;
220}
221
222void ArenaAllocator::Impl::new_segment() {
223 this->segment = this->backing->create<Segment>();
224 this->segment_offset = 0;
225 this->track_segment();
226}
227
228void ArenaAllocator::Impl::track_segment() {
229 assert(this->segment != nullptr);
230 if (this->segment_track_remain < 1) {
231 auto prev = this->segment_track;
232 this->segment_track = this->backing->create<SegmentTrack>();
233 this->segment_track->prev = prev;
234 this->segment_track_remain = SegmentTrack::size;
235 }
236 this->segment_track_remain -= 1;
237 this->segment_track->segments[this->segment_track_remain] = this->segment;
238}
239
240void ArenaAllocator::Impl::track_object(Object object) {
241 if (this->object_track_remain < 1) {
242 auto prev = this->object_track;
243 this->object_track = this->backing->create<ObjectTrack>();
244 this->object_track->prev = prev;
245 this->object_track_remain = ObjectTrack::size;
246 }
247 this->object_track_remain -= 1;
248 this->object_track->objects[this->object_track_remain] = object;
249}
250
251void ArenaAllocator::init(Allocator *backing, const char *name) {
252#ifdef ZIG_ENABLE_MEM_PROFILE
253 this->profile = bootstrap_allocator.create<mem::Profile>();
254 this->profile->init(name, "ArenaAllocator");
255#endif
256 this->impl = bootstrap_allocator.create<Impl>();
257 {
258 auto &r = *this->impl;
259 r.backing = backing;
260 r.segment_offset = Impl::Segment::size;
261 }
262}
263
264void ArenaAllocator::deinit() {
265 auto &backing = *this->impl->backing;
266
267 // segments
268 if (this->impl->segment_track) {
269 // active track is not full and bounded by track_remain
270 auto prev = this->impl->segment_track->prev;
271 {
272 auto t = this->impl->segment_track;
273 for (size_t i = this->impl->segment_track_remain; i < Impl::SegmentTrack::size; ++i)
274 backing.destroy(t->segments[i]);
275 backing.destroy(t);
276 }
277
278 // previous tracks are full
279 for (auto t = prev; t != nullptr;) {
280 for (size_t i = 0; i < Impl::SegmentTrack::size; ++i)
281 backing.destroy(t->segments[i]);
282 prev = t->prev;
283 backing.destroy(t);
284 t = prev;
285 }
286 }
287
288 // objects
289 if (this->impl->object_track) {
290 // active track is not full and bounded by track_remain
291 auto prev = this->impl->object_track->prev;
292 {
293 auto t = this->impl->object_track;
294 for (size_t i = this->impl->object_track_remain; i < Impl::ObjectTrack::size; ++i) {
295 auto &obj = t->objects[i];
296 backing.deallocate(obj.ptr, obj.len);
297 }
298 backing.destroy(t);
299 }
300
301 // previous tracks are full
302 for (auto t = prev; t != nullptr;) {
303 for (size_t i = 0; i < Impl::ObjectTrack::size; ++i) {
304 auto &obj = t->objects[i];
305 backing.deallocate(obj.ptr, obj.len);
306 }
307 prev = t->prev;
308 backing.destroy(t);
309 t = prev;
310 }
311 }
312
313#ifdef ZIG_ENABLE_MEM_PROFILE
314 assert(this->profile);
315 this->profile->deinit();
316 bootstrap_allocator.destroy(this->profile);
317 this->profile = nullptr;
318#endif
319}
320
321ArenaAllocator *ArenaAllocator::construct(mem::Allocator *allocator, mem::Allocator *backing, const char *name) {
322 auto p = new(allocator->create<ArenaAllocator>()) ArenaAllocator;
323 p->init(backing, name);
324 return p;
325}
326
327void ArenaAllocator::destruct(mem::Allocator *allocator) {
328 this->deinit();
329 allocator->destroy(this);
330}
331
332#ifdef ZIG_ENABLE_MEM_PROFILE
333void ArenaAllocator::print_report(FILE *file) {
334 this->profile->print_report(file);
335}
336#endif
337
338void *ArenaAllocator::internal_allocate(const mem::TypeInfo &info, size_t count) {
339#ifdef ZIG_ENABLE_MEM_PROFILE
340 this->profile->record_alloc(info, count);
341#endif
342 return this->impl->allocate(info, count);
343}
344
345void *ArenaAllocator::internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) {
346#ifdef ZIG_ENABLE_MEM_PROFILE
347 this->profile->record_alloc(info, count);
348#endif
349 return this->impl->allocate(info, count);
350}
351
352void *ArenaAllocator::internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) {
353 return this->internal_reallocate_nonzero(info, old_ptr, old_count, new_count);
354}
355
356void *ArenaAllocator::internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) {
357#ifdef ZIG_ENABLE_MEM_PROFILE
358 this->profile->record_dealloc(info, old_count);
359 this->profile->record_alloc(info, new_count);
360#endif
361 return this->impl->reallocate(info, old_ptr, old_count, new_count);
362}
363
364void ArenaAllocator::internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) {
365#ifdef ZIG_ENABLE_MEM_PROFILE
366 this->profile->record_dealloc(info, count);
367#endif
368 // noop
369}
370
371BootstrapAllocator bootstrap_allocator_state;
372mem::Allocator &bootstrap_allocator = bootstrap_allocator_state;
373
374CAllocator c_allocator_state;
375mem::Allocator &c_allocator = c_allocator_state;
376
377} // namespace heap
src/heap.hpp created+101
...@@ -0,0 +1,101 @@
1/*
2 * Copyright (c) 2020 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_HEAP_HPP
9#define ZIG_HEAP_HPP
10
11#include "config.h"
12#include "util_base.hpp"
13#include "mem.hpp"
14
15#ifdef ZIG_ENABLE_MEM_PROFILE
16namespace mem {
17 struct Profile;
18}
19#endif
20
21namespace heap {
22
23struct BootstrapAllocator final : mem::Allocator {
24 void init(const char *name);
25 void deinit();
26 void destruct(Allocator *allocator) {}
27
28private:
29 ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate(const mem::TypeInfo &info, size_t count) final;
30 ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) final;
31 void *internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
32 void *internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
33 void internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) final;
34};
35
36struct CAllocator final : mem::Allocator {
37 void init(const char *name);
38 void deinit();
39
40 static CAllocator *construct(mem::Allocator *allocator, const char *name);
41 void destruct(mem::Allocator *allocator) final;
42
43#ifdef ZIG_ENABLE_MEM_PROFILE
44 void print_report(FILE *file = nullptr);
45#endif
46
47private:
48 ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate(const mem::TypeInfo &info, size_t count) final;
49 ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) final;
50 void *internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
51 void *internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
52 void internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) final;
53
54#ifdef ZIG_ENABLE_MEM_PROFILE
55 mem::Profile *profile;
56#endif
57};
58
59//
60// arena allocator
61//
62// - allocations are backed by the underlying allocator's memory
63// - allocations are N:1 relationship to underlying allocations
64// - dellocations are noops
65// - deinit() releases all underlying memory
66//
67struct ArenaAllocator final : mem::Allocator {
68 void init(Allocator *backing, const char *name);
69 void deinit();
70
71 static ArenaAllocator *construct(mem::Allocator *allocator, mem::Allocator *backing, const char *name);
72 void destruct(mem::Allocator *allocator) final;
73
74#ifdef ZIG_ENABLE_MEM_PROFILE
75 void print_report(FILE *file = nullptr);
76#endif
77
78private:
79 ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate(const mem::TypeInfo &info, size_t count) final;
80 ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) final;
81 void *internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
82 void *internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
83 void internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) final;
84
85#ifdef ZIG_ENABLE_MEM_PROFILE
86 mem::Profile *profile;
87#endif
88
89 struct Impl;
90 Impl *impl;
91};
92
93extern BootstrapAllocator bootstrap_allocator_state;
94extern mem::Allocator &bootstrap_allocator;
95
96extern CAllocator c_allocator_state;
97extern mem::Allocator &c_allocator;
98
99} // namespace heap
100
101#endif
src/ir.cpp+557-560
...@@ -267,479 +267,470 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,...@@ -267,479 +267,470 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,
267static ResultLoc *no_result_loc(void);267static ResultLoc *no_result_loc(void);
268static IrInstGen *ir_analyze_test_non_null(IrAnalyze *ira, IrInst *source_inst, IrInstGen *value);268static IrInstGen *ir_analyze_test_non_null(IrAnalyze *ira, IrInst *source_inst, IrInstGen *value);
269static IrInstGen *ir_error_dependency_loop(IrAnalyze *ira, IrInst *source_instr);269static IrInstGen *ir_error_dependency_loop(IrAnalyze *ira, IrInst *source_instr);
270static IrInstGen *ir_const_undef(IrAnalyze *ira, IrInst *source_instruction, ZigType *ty);
270271
271static void destroy_instruction_src(IrInstSrc *inst) {272static void destroy_instruction_src(IrInstSrc *inst) {
272#ifdef ZIG_ENABLE_MEM_PROFILE
273 const char *name = ir_inst_src_type_str(inst->id);
274#else
275 const char *name = nullptr;
276#endif
277 switch (inst->id) {273 switch (inst->id) {
278 case IrInstSrcIdInvalid:274 case IrInstSrcIdInvalid:
279 zig_unreachable();275 zig_unreachable();
280 case IrInstSrcIdReturn:276 case IrInstSrcIdReturn:
281 return destroy(reinterpret_cast<IrInstSrcReturn *>(inst), name);277 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcReturn *>(inst));
282 case IrInstSrcIdConst:278 case IrInstSrcIdConst:
283 return destroy(reinterpret_cast<IrInstSrcConst *>(inst), name);279 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcConst *>(inst));
284 case IrInstSrcIdBinOp:280 case IrInstSrcIdBinOp:
285 return destroy(reinterpret_cast<IrInstSrcBinOp *>(inst), name);281 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBinOp *>(inst));
286 case IrInstSrcIdMergeErrSets:282 case IrInstSrcIdMergeErrSets:
287 return destroy(reinterpret_cast<IrInstSrcMergeErrSets *>(inst), name);283 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcMergeErrSets *>(inst));
288 case IrInstSrcIdDeclVar:284 case IrInstSrcIdDeclVar:
289 return destroy(reinterpret_cast<IrInstSrcDeclVar *>(inst), name);285 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcDeclVar *>(inst));
290 case IrInstSrcIdCall:286 case IrInstSrcIdCall:
291 return destroy(reinterpret_cast<IrInstSrcCall *>(inst), name);287 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCall *>(inst));
292 case IrInstSrcIdCallExtra:288 case IrInstSrcIdCallExtra:
293 return destroy(reinterpret_cast<IrInstSrcCallExtra *>(inst), name);289 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCallExtra *>(inst));
294 case IrInstSrcIdUnOp:290 case IrInstSrcIdUnOp:
295 return destroy(reinterpret_cast<IrInstSrcUnOp *>(inst), name);291 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcUnOp *>(inst));
296 case IrInstSrcIdCondBr:292 case IrInstSrcIdCondBr:
297 return destroy(reinterpret_cast<IrInstSrcCondBr *>(inst), name);293 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCondBr *>(inst));
298 case IrInstSrcIdBr:294 case IrInstSrcIdBr:
299 return destroy(reinterpret_cast<IrInstSrcBr *>(inst), name);295 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBr *>(inst));
300 case IrInstSrcIdPhi:296 case IrInstSrcIdPhi:
301 return destroy(reinterpret_cast<IrInstSrcPhi *>(inst), name);297 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcPhi *>(inst));
302 case IrInstSrcIdContainerInitList:298 case IrInstSrcIdContainerInitList:
303 return destroy(reinterpret_cast<IrInstSrcContainerInitList *>(inst), name);299 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcContainerInitList *>(inst));
304 case IrInstSrcIdContainerInitFields:300 case IrInstSrcIdContainerInitFields:
305 return destroy(reinterpret_cast<IrInstSrcContainerInitFields *>(inst), name);301 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcContainerInitFields *>(inst));
306 case IrInstSrcIdUnreachable:302 case IrInstSrcIdUnreachable:
307 return destroy(reinterpret_cast<IrInstSrcUnreachable *>(inst), name);303 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcUnreachable *>(inst));
308 case IrInstSrcIdElemPtr:304 case IrInstSrcIdElemPtr:
309 return destroy(reinterpret_cast<IrInstSrcElemPtr *>(inst), name);305 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcElemPtr *>(inst));
310 case IrInstSrcIdVarPtr:306 case IrInstSrcIdVarPtr:
311 return destroy(reinterpret_cast<IrInstSrcVarPtr *>(inst), name);307 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcVarPtr *>(inst));
312 case IrInstSrcIdLoadPtr:308 case IrInstSrcIdLoadPtr:
313 return destroy(reinterpret_cast<IrInstSrcLoadPtr *>(inst), name);309 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcLoadPtr *>(inst));
314 case IrInstSrcIdStorePtr:310 case IrInstSrcIdStorePtr:
315 return destroy(reinterpret_cast<IrInstSrcStorePtr *>(inst), name);311 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcStorePtr *>(inst));
316 case IrInstSrcIdTypeOf:312 case IrInstSrcIdTypeOf:
317 return destroy(reinterpret_cast<IrInstSrcTypeOf *>(inst), name);313 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTypeOf *>(inst));
318 case IrInstSrcIdFieldPtr:314 case IrInstSrcIdFieldPtr:
319 return destroy(reinterpret_cast<IrInstSrcFieldPtr *>(inst), name);315 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFieldPtr *>(inst));
320 case IrInstSrcIdSetCold:316 case IrInstSrcIdSetCold:
321 return destroy(reinterpret_cast<IrInstSrcSetCold *>(inst), name);317 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetCold *>(inst));
322 case IrInstSrcIdSetRuntimeSafety:318 case IrInstSrcIdSetRuntimeSafety:
323 return destroy(reinterpret_cast<IrInstSrcSetRuntimeSafety *>(inst), name);319 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetRuntimeSafety *>(inst));
324 case IrInstSrcIdSetFloatMode:320 case IrInstSrcIdSetFloatMode:
325 return destroy(reinterpret_cast<IrInstSrcSetFloatMode *>(inst), name);321 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetFloatMode *>(inst));
326 case IrInstSrcIdArrayType:322 case IrInstSrcIdArrayType:
327 return destroy(reinterpret_cast<IrInstSrcArrayType *>(inst), name);323 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcArrayType *>(inst));
328 case IrInstSrcIdSliceType:324 case IrInstSrcIdSliceType:
329 return destroy(reinterpret_cast<IrInstSrcSliceType *>(inst), name);325 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSliceType *>(inst));
330 case IrInstSrcIdAnyFrameType:326 case IrInstSrcIdAnyFrameType:
331 return destroy(reinterpret_cast<IrInstSrcAnyFrameType *>(inst), name);327 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAnyFrameType *>(inst));
332 case IrInstSrcIdAsm:328 case IrInstSrcIdAsm:
333 return destroy(reinterpret_cast<IrInstSrcAsm *>(inst), name);329 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAsm *>(inst));
334 case IrInstSrcIdSizeOf:330 case IrInstSrcIdSizeOf:
335 return destroy(reinterpret_cast<IrInstSrcSizeOf *>(inst), name);331 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSizeOf *>(inst));
336 case IrInstSrcIdTestNonNull:332 case IrInstSrcIdTestNonNull:
337 return destroy(reinterpret_cast<IrInstSrcTestNonNull *>(inst), name);333 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTestNonNull *>(inst));
338 case IrInstSrcIdOptionalUnwrapPtr:334 case IrInstSrcIdOptionalUnwrapPtr:
339 return destroy(reinterpret_cast<IrInstSrcOptionalUnwrapPtr *>(inst), name);335 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcOptionalUnwrapPtr *>(inst));
340 case IrInstSrcIdPopCount:336 case IrInstSrcIdPopCount:
341 return destroy(reinterpret_cast<IrInstSrcPopCount *>(inst), name);337 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcPopCount *>(inst));
342 case IrInstSrcIdClz:338 case IrInstSrcIdClz:
343 return destroy(reinterpret_cast<IrInstSrcClz *>(inst), name);339 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcClz *>(inst));
344 case IrInstSrcIdCtz:340 case IrInstSrcIdCtz:
345 return destroy(reinterpret_cast<IrInstSrcCtz *>(inst), name);341 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCtz *>(inst));
346 case IrInstSrcIdBswap:342 case IrInstSrcIdBswap:
347 return destroy(reinterpret_cast<IrInstSrcBswap *>(inst), name);343 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBswap *>(inst));
348 case IrInstSrcIdBitReverse:344 case IrInstSrcIdBitReverse:
349 return destroy(reinterpret_cast<IrInstSrcBitReverse *>(inst), name);345 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBitReverse *>(inst));
350 case IrInstSrcIdSwitchBr:346 case IrInstSrcIdSwitchBr:
351 return destroy(reinterpret_cast<IrInstSrcSwitchBr *>(inst), name);347 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSwitchBr *>(inst));
352 case IrInstSrcIdSwitchVar:348 case IrInstSrcIdSwitchVar:
353 return destroy(reinterpret_cast<IrInstSrcSwitchVar *>(inst), name);349 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSwitchVar *>(inst));
354 case IrInstSrcIdSwitchElseVar:350 case IrInstSrcIdSwitchElseVar:
355 return destroy(reinterpret_cast<IrInstSrcSwitchElseVar *>(inst), name);351 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSwitchElseVar *>(inst));
356 case IrInstSrcIdSwitchTarget:352 case IrInstSrcIdSwitchTarget:
357 return destroy(reinterpret_cast<IrInstSrcSwitchTarget *>(inst), name);353 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSwitchTarget *>(inst));
358 case IrInstSrcIdImport:354 case IrInstSrcIdImport:
359 return destroy(reinterpret_cast<IrInstSrcImport *>(inst), name);355 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcImport *>(inst));
360 case IrInstSrcIdRef:356 case IrInstSrcIdRef:
361 return destroy(reinterpret_cast<IrInstSrcRef *>(inst), name);357 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcRef *>(inst));
362 case IrInstSrcIdCompileErr:358 case IrInstSrcIdCompileErr:
363 return destroy(reinterpret_cast<IrInstSrcCompileErr *>(inst), name);359 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCompileErr *>(inst));
364 case IrInstSrcIdCompileLog:360 case IrInstSrcIdCompileLog:
365 return destroy(reinterpret_cast<IrInstSrcCompileLog *>(inst), name);361 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCompileLog *>(inst));
366 case IrInstSrcIdErrName:362 case IrInstSrcIdErrName:
367 return destroy(reinterpret_cast<IrInstSrcErrName *>(inst), name);363 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrName *>(inst));
368 case IrInstSrcIdCImport:364 case IrInstSrcIdCImport:
369 return destroy(reinterpret_cast<IrInstSrcCImport *>(inst), name);365 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCImport *>(inst));
370 case IrInstSrcIdCInclude:366 case IrInstSrcIdCInclude:
371 return destroy(reinterpret_cast<IrInstSrcCInclude *>(inst), name);367 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCInclude *>(inst));
372 case IrInstSrcIdCDefine:368 case IrInstSrcIdCDefine:
373 return destroy(reinterpret_cast<IrInstSrcCDefine *>(inst), name);369 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCDefine *>(inst));
374 case IrInstSrcIdCUndef:370 case IrInstSrcIdCUndef:
375 return destroy(reinterpret_cast<IrInstSrcCUndef *>(inst), name);371 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCUndef *>(inst));
376 case IrInstSrcIdEmbedFile:372 case IrInstSrcIdEmbedFile:
377 return destroy(reinterpret_cast<IrInstSrcEmbedFile *>(inst), name);373 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcEmbedFile *>(inst));
378 case IrInstSrcIdCmpxchg:374 case IrInstSrcIdCmpxchg:
379 return destroy(reinterpret_cast<IrInstSrcCmpxchg *>(inst), name);375 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCmpxchg *>(inst));
380 case IrInstSrcIdFence:376 case IrInstSrcIdFence:
381 return destroy(reinterpret_cast<IrInstSrcFence *>(inst), name);377 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFence *>(inst));
382 case IrInstSrcIdTruncate:378 case IrInstSrcIdTruncate:
383 return destroy(reinterpret_cast<IrInstSrcTruncate *>(inst), name);379 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTruncate *>(inst));
384 case IrInstSrcIdIntCast:380 case IrInstSrcIdIntCast:
385 return destroy(reinterpret_cast<IrInstSrcIntCast *>(inst), name);381 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntCast *>(inst));
386 case IrInstSrcIdFloatCast:382 case IrInstSrcIdFloatCast:
387 return destroy(reinterpret_cast<IrInstSrcFloatCast *>(inst), name);383 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFloatCast *>(inst));
388 case IrInstSrcIdErrSetCast:384 case IrInstSrcIdErrSetCast:
389 return destroy(reinterpret_cast<IrInstSrcErrSetCast *>(inst), name);385 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrSetCast *>(inst));
390 case IrInstSrcIdFromBytes:386 case IrInstSrcIdFromBytes:
391 return destroy(reinterpret_cast<IrInstSrcFromBytes *>(inst), name);387 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFromBytes *>(inst));
392 case IrInstSrcIdToBytes:388 case IrInstSrcIdToBytes:
393 return destroy(reinterpret_cast<IrInstSrcToBytes *>(inst), name);389 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcToBytes *>(inst));
394 case IrInstSrcIdIntToFloat:390 case IrInstSrcIdIntToFloat:
395 return destroy(reinterpret_cast<IrInstSrcIntToFloat *>(inst), name);391 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntToFloat *>(inst));
396 case IrInstSrcIdFloatToInt:392 case IrInstSrcIdFloatToInt:
397 return destroy(reinterpret_cast<IrInstSrcFloatToInt *>(inst), name);393 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFloatToInt *>(inst));
398 case IrInstSrcIdBoolToInt:394 case IrInstSrcIdBoolToInt:
399 return destroy(reinterpret_cast<IrInstSrcBoolToInt *>(inst), name);395 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBoolToInt *>(inst));
400 case IrInstSrcIdIntType:396 case IrInstSrcIdIntType:
401 return destroy(reinterpret_cast<IrInstSrcIntType *>(inst), name);397 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntType *>(inst));
402 case IrInstSrcIdVectorType:398 case IrInstSrcIdVectorType:
403 return destroy(reinterpret_cast<IrInstSrcVectorType *>(inst), name);399 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcVectorType *>(inst));
404 case IrInstSrcIdShuffleVector:400 case IrInstSrcIdShuffleVector:
405 return destroy(reinterpret_cast<IrInstSrcShuffleVector *>(inst), name);401 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcShuffleVector *>(inst));
406 case IrInstSrcIdSplat:402 case IrInstSrcIdSplat:
407 return destroy(reinterpret_cast<IrInstSrcSplat *>(inst), name);403 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSplat *>(inst));
408 case IrInstSrcIdBoolNot:404 case IrInstSrcIdBoolNot:
409 return destroy(reinterpret_cast<IrInstSrcBoolNot *>(inst), name);405 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBoolNot *>(inst));
410 case IrInstSrcIdMemset:406 case IrInstSrcIdMemset:
411 return destroy(reinterpret_cast<IrInstSrcMemset *>(inst), name);407 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcMemset *>(inst));
412 case IrInstSrcIdMemcpy:408 case IrInstSrcIdMemcpy:
413 return destroy(reinterpret_cast<IrInstSrcMemcpy *>(inst), name);409 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcMemcpy *>(inst));
414 case IrInstSrcIdSlice:410 case IrInstSrcIdSlice:
415 return destroy(reinterpret_cast<IrInstSrcSlice *>(inst), name);411 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSlice *>(inst));
416 case IrInstSrcIdMemberCount:412 case IrInstSrcIdMemberCount:
417 return destroy(reinterpret_cast<IrInstSrcMemberCount *>(inst), name);413 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcMemberCount *>(inst));
418 case IrInstSrcIdMemberType:414 case IrInstSrcIdMemberType:
419 return destroy(reinterpret_cast<IrInstSrcMemberType *>(inst), name);415 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcMemberType *>(inst));
420 case IrInstSrcIdMemberName:416 case IrInstSrcIdMemberName:
421 return destroy(reinterpret_cast<IrInstSrcMemberName *>(inst), name);417 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcMemberName *>(inst));
422 case IrInstSrcIdBreakpoint:418 case IrInstSrcIdBreakpoint:
423 return destroy(reinterpret_cast<IrInstSrcBreakpoint *>(inst), name);419 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBreakpoint *>(inst));
424 case IrInstSrcIdReturnAddress:420 case IrInstSrcIdReturnAddress:
425 return destroy(reinterpret_cast<IrInstSrcReturnAddress *>(inst), name);421 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcReturnAddress *>(inst));
426 case IrInstSrcIdFrameAddress:422 case IrInstSrcIdFrameAddress:
427 return destroy(reinterpret_cast<IrInstSrcFrameAddress *>(inst), name);423 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFrameAddress *>(inst));
428 case IrInstSrcIdFrameHandle:424 case IrInstSrcIdFrameHandle:
429 return destroy(reinterpret_cast<IrInstSrcFrameHandle *>(inst), name);425 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFrameHandle *>(inst));
430 case IrInstSrcIdFrameType:426 case IrInstSrcIdFrameType:
431 return destroy(reinterpret_cast<IrInstSrcFrameType *>(inst), name);427 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFrameType *>(inst));
432 case IrInstSrcIdFrameSize:428 case IrInstSrcIdFrameSize:
433 return destroy(reinterpret_cast<IrInstSrcFrameSize *>(inst), name);429 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFrameSize *>(inst));
434 case IrInstSrcIdAlignOf:430 case IrInstSrcIdAlignOf:
435 return destroy(reinterpret_cast<IrInstSrcAlignOf *>(inst), name);431 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAlignOf *>(inst));
436 case IrInstSrcIdOverflowOp:432 case IrInstSrcIdOverflowOp:
437 return destroy(reinterpret_cast<IrInstSrcOverflowOp *>(inst), name);433 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcOverflowOp *>(inst));
438 case IrInstSrcIdTestErr:434 case IrInstSrcIdTestErr:
439 return destroy(reinterpret_cast<IrInstSrcTestErr *>(inst), name);435 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTestErr *>(inst));
440 case IrInstSrcIdUnwrapErrCode:436 case IrInstSrcIdUnwrapErrCode:
441 return destroy(reinterpret_cast<IrInstSrcUnwrapErrCode *>(inst), name);437 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcUnwrapErrCode *>(inst));
442 case IrInstSrcIdUnwrapErrPayload:438 case IrInstSrcIdUnwrapErrPayload:
443 return destroy(reinterpret_cast<IrInstSrcUnwrapErrPayload *>(inst), name);439 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcUnwrapErrPayload *>(inst));
444 case IrInstSrcIdFnProto:440 case IrInstSrcIdFnProto:
445 return destroy(reinterpret_cast<IrInstSrcFnProto *>(inst), name);441 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFnProto *>(inst));
446 case IrInstSrcIdTestComptime:442 case IrInstSrcIdTestComptime:
447 return destroy(reinterpret_cast<IrInstSrcTestComptime *>(inst), name);443 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTestComptime *>(inst));
448 case IrInstSrcIdPtrCast:444 case IrInstSrcIdPtrCast:
449 return destroy(reinterpret_cast<IrInstSrcPtrCast *>(inst), name);445 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcPtrCast *>(inst));
450 case IrInstSrcIdBitCast:446 case IrInstSrcIdBitCast:
451 return destroy(reinterpret_cast<IrInstSrcBitCast *>(inst), name);447 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBitCast *>(inst));
452 case IrInstSrcIdPtrToInt:448 case IrInstSrcIdPtrToInt:
453 return destroy(reinterpret_cast<IrInstSrcPtrToInt *>(inst), name);449 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcPtrToInt *>(inst));
454 case IrInstSrcIdIntToPtr:450 case IrInstSrcIdIntToPtr:
455 return destroy(reinterpret_cast<IrInstSrcIntToPtr *>(inst), name);451 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntToPtr *>(inst));
456 case IrInstSrcIdIntToEnum:452 case IrInstSrcIdIntToEnum:
457 return destroy(reinterpret_cast<IrInstSrcIntToEnum *>(inst), name);453 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntToEnum *>(inst));
458 case IrInstSrcIdIntToErr:454 case IrInstSrcIdIntToErr:
459 return destroy(reinterpret_cast<IrInstSrcIntToErr *>(inst), name);455 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcIntToErr *>(inst));
460 case IrInstSrcIdErrToInt:456 case IrInstSrcIdErrToInt:
461 return destroy(reinterpret_cast<IrInstSrcErrToInt *>(inst), name);457 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrToInt *>(inst));
462 case IrInstSrcIdCheckSwitchProngs:458 case IrInstSrcIdCheckSwitchProngs:
463 return destroy(reinterpret_cast<IrInstSrcCheckSwitchProngs *>(inst), name);459 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCheckSwitchProngs *>(inst));
464 case IrInstSrcIdCheckStatementIsVoid:460 case IrInstSrcIdCheckStatementIsVoid:
465 return destroy(reinterpret_cast<IrInstSrcCheckStatementIsVoid *>(inst), name);461 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCheckStatementIsVoid *>(inst));
466 case IrInstSrcIdTypeName:462 case IrInstSrcIdTypeName:
467 return destroy(reinterpret_cast<IrInstSrcTypeName *>(inst), name);463 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTypeName *>(inst));
468 case IrInstSrcIdTagName:464 case IrInstSrcIdTagName:
469 return destroy(reinterpret_cast<IrInstSrcTagName *>(inst), name);465 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTagName *>(inst));
470 case IrInstSrcIdPtrType:466 case IrInstSrcIdPtrType:
471 return destroy(reinterpret_cast<IrInstSrcPtrType *>(inst), name);467 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcPtrType *>(inst));
472 case IrInstSrcIdDeclRef:468 case IrInstSrcIdDeclRef:
473 return destroy(reinterpret_cast<IrInstSrcDeclRef *>(inst), name);469 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcDeclRef *>(inst));
474 case IrInstSrcIdPanic:470 case IrInstSrcIdPanic:
475 return destroy(reinterpret_cast<IrInstSrcPanic *>(inst), name);471 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcPanic *>(inst));
476 case IrInstSrcIdFieldParentPtr:472 case IrInstSrcIdFieldParentPtr:
477 return destroy(reinterpret_cast<IrInstSrcFieldParentPtr *>(inst), name);473 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFieldParentPtr *>(inst));
478 case IrInstSrcIdByteOffsetOf:474 case IrInstSrcIdByteOffsetOf:
479 return destroy(reinterpret_cast<IrInstSrcByteOffsetOf *>(inst), name);475 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcByteOffsetOf *>(inst));
480 case IrInstSrcIdBitOffsetOf:476 case IrInstSrcIdBitOffsetOf:
481 return destroy(reinterpret_cast<IrInstSrcBitOffsetOf *>(inst), name);477 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBitOffsetOf *>(inst));
482 case IrInstSrcIdTypeInfo:478 case IrInstSrcIdTypeInfo:
483 return destroy(reinterpret_cast<IrInstSrcTypeInfo *>(inst), name);479 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTypeInfo *>(inst));
484 case IrInstSrcIdType:480 case IrInstSrcIdType:
485 return destroy(reinterpret_cast<IrInstSrcType *>(inst), name);481 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcType *>(inst));
486 case IrInstSrcIdHasField:482 case IrInstSrcIdHasField:
487 return destroy(reinterpret_cast<IrInstSrcHasField *>(inst), name);483 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcHasField *>(inst));
488 case IrInstSrcIdTypeId:484 case IrInstSrcIdTypeId:
489 return destroy(reinterpret_cast<IrInstSrcTypeId *>(inst), name);485 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTypeId *>(inst));
490 case IrInstSrcIdSetEvalBranchQuota:486 case IrInstSrcIdSetEvalBranchQuota:
491 return destroy(reinterpret_cast<IrInstSrcSetEvalBranchQuota *>(inst), name);487 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetEvalBranchQuota *>(inst));
492 case IrInstSrcIdAlignCast:488 case IrInstSrcIdAlignCast:
493 return destroy(reinterpret_cast<IrInstSrcAlignCast *>(inst), name);489 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAlignCast *>(inst));
494 case IrInstSrcIdImplicitCast:490 case IrInstSrcIdImplicitCast:
495 return destroy(reinterpret_cast<IrInstSrcImplicitCast *>(inst), name);491 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcImplicitCast *>(inst));
496 case IrInstSrcIdResolveResult:492 case IrInstSrcIdResolveResult:
497 return destroy(reinterpret_cast<IrInstSrcResolveResult *>(inst), name);493 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcResolveResult *>(inst));
498 case IrInstSrcIdResetResult:494 case IrInstSrcIdResetResult:
499 return destroy(reinterpret_cast<IrInstSrcResetResult *>(inst), name);495 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcResetResult *>(inst));
500 case IrInstSrcIdOpaqueType:496 case IrInstSrcIdOpaqueType:
501 return destroy(reinterpret_cast<IrInstSrcOpaqueType *>(inst), name);497 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcOpaqueType *>(inst));
502 case IrInstSrcIdSetAlignStack:498 case IrInstSrcIdSetAlignStack:
503 return destroy(reinterpret_cast<IrInstSrcSetAlignStack *>(inst), name);499 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetAlignStack *>(inst));
504 case IrInstSrcIdArgType:500 case IrInstSrcIdArgType:
505 return destroy(reinterpret_cast<IrInstSrcArgType *>(inst), name);501 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcArgType *>(inst));
506 case IrInstSrcIdTagType:502 case IrInstSrcIdTagType:
507 return destroy(reinterpret_cast<IrInstSrcTagType *>(inst), name);503 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTagType *>(inst));
508 case IrInstSrcIdExport:504 case IrInstSrcIdExport:
509 return destroy(reinterpret_cast<IrInstSrcExport *>(inst), name);505 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcExport *>(inst));
510 case IrInstSrcIdErrorReturnTrace:506 case IrInstSrcIdErrorReturnTrace:
511 return destroy(reinterpret_cast<IrInstSrcErrorReturnTrace *>(inst), name);507 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrorReturnTrace *>(inst));
512 case IrInstSrcIdErrorUnion:508 case IrInstSrcIdErrorUnion:
513 return destroy(reinterpret_cast<IrInstSrcErrorUnion *>(inst), name);509 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcErrorUnion *>(inst));
514 case IrInstSrcIdAtomicRmw:510 case IrInstSrcIdAtomicRmw:
515 return destroy(reinterpret_cast<IrInstSrcAtomicRmw *>(inst), name);511 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAtomicRmw *>(inst));
516 case IrInstSrcIdSaveErrRetAddr:512 case IrInstSrcIdSaveErrRetAddr:
517 return destroy(reinterpret_cast<IrInstSrcSaveErrRetAddr *>(inst), name);513 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSaveErrRetAddr *>(inst));
518 case IrInstSrcIdAddImplicitReturnType:514 case IrInstSrcIdAddImplicitReturnType:
519 return destroy(reinterpret_cast<IrInstSrcAddImplicitReturnType *>(inst), name);515 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAddImplicitReturnType *>(inst));
520 case IrInstSrcIdFloatOp:516 case IrInstSrcIdFloatOp:
521 return destroy(reinterpret_cast<IrInstSrcFloatOp *>(inst), name);517 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFloatOp *>(inst));
522 case IrInstSrcIdMulAdd:518 case IrInstSrcIdMulAdd:
523 return destroy(reinterpret_cast<IrInstSrcMulAdd *>(inst), name);519 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcMulAdd *>(inst));
524 case IrInstSrcIdAtomicLoad:520 case IrInstSrcIdAtomicLoad:
525 return destroy(reinterpret_cast<IrInstSrcAtomicLoad *>(inst), name);521 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAtomicLoad *>(inst));
526 case IrInstSrcIdAtomicStore:522 case IrInstSrcIdAtomicStore:
527 return destroy(reinterpret_cast<IrInstSrcAtomicStore *>(inst), name);523 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAtomicStore *>(inst));
528 case IrInstSrcIdEnumToInt:524 case IrInstSrcIdEnumToInt:
529 return destroy(reinterpret_cast<IrInstSrcEnumToInt *>(inst), name);525 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcEnumToInt *>(inst));
530 case IrInstSrcIdCheckRuntimeScope:526 case IrInstSrcIdCheckRuntimeScope:
531 return destroy(reinterpret_cast<IrInstSrcCheckRuntimeScope *>(inst), name);527 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCheckRuntimeScope *>(inst));
532 case IrInstSrcIdHasDecl:528 case IrInstSrcIdHasDecl:
533 return destroy(reinterpret_cast<IrInstSrcHasDecl *>(inst), name);529 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcHasDecl *>(inst));
534 case IrInstSrcIdUndeclaredIdent:530 case IrInstSrcIdUndeclaredIdent:
535 return destroy(reinterpret_cast<IrInstSrcUndeclaredIdent *>(inst), name);531 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcUndeclaredIdent *>(inst));
536 case IrInstSrcIdAlloca:532 case IrInstSrcIdAlloca:
537 return destroy(reinterpret_cast<IrInstSrcAlloca *>(inst), name);533 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAlloca *>(inst));
538 case IrInstSrcIdEndExpr:534 case IrInstSrcIdEndExpr:
539 return destroy(reinterpret_cast<IrInstSrcEndExpr *>(inst), name);535 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcEndExpr *>(inst));
540 case IrInstSrcIdUnionInitNamedField:536 case IrInstSrcIdUnionInitNamedField:
541 return destroy(reinterpret_cast<IrInstSrcUnionInitNamedField *>(inst), name);537 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcUnionInitNamedField *>(inst));
542 case IrInstSrcIdSuspendBegin:538 case IrInstSrcIdSuspendBegin:
543 return destroy(reinterpret_cast<IrInstSrcSuspendBegin *>(inst), name);539 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSuspendBegin *>(inst));
544 case IrInstSrcIdSuspendFinish:540 case IrInstSrcIdSuspendFinish:
545 return destroy(reinterpret_cast<IrInstSrcSuspendFinish *>(inst), name);541 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSuspendFinish *>(inst));
546 case IrInstSrcIdResume:542 case IrInstSrcIdResume:
547 return destroy(reinterpret_cast<IrInstSrcResume *>(inst), name);543 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcResume *>(inst));
548 case IrInstSrcIdAwait:544 case IrInstSrcIdAwait:
549 return destroy(reinterpret_cast<IrInstSrcAwait *>(inst), name);545 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcAwait *>(inst));
550 case IrInstSrcIdSpillBegin:546 case IrInstSrcIdSpillBegin:
551 return destroy(reinterpret_cast<IrInstSrcSpillBegin *>(inst), name);547 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSpillBegin *>(inst));
552 case IrInstSrcIdSpillEnd:548 case IrInstSrcIdSpillEnd:
553 return destroy(reinterpret_cast<IrInstSrcSpillEnd *>(inst), name);549 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSpillEnd *>(inst));
554 case IrInstSrcIdCallArgs:550 case IrInstSrcIdCallArgs:
555 return destroy(reinterpret_cast<IrInstSrcCallArgs *>(inst), name);551 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCallArgs *>(inst));
556 }552 }
557 zig_unreachable();553 zig_unreachable();
558}554}
559555
560void destroy_instruction_gen(IrInstGen *inst) {556void destroy_instruction_gen(IrInstGen *inst) {
561#ifdef ZIG_ENABLE_MEM_PROFILE
562 const char *name = ir_inst_gen_type_str(inst->id);
563#else
564 const char *name = nullptr;
565#endif
566 switch (inst->id) {557 switch (inst->id) {
567 case IrInstGenIdInvalid:558 case IrInstGenIdInvalid:
568 zig_unreachable();559 zig_unreachable();
569 case IrInstGenIdReturn:560 case IrInstGenIdReturn:
570 return destroy(reinterpret_cast<IrInstGenReturn *>(inst), name);561 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenReturn *>(inst));
571 case IrInstGenIdConst:562 case IrInstGenIdConst:
572 return destroy(reinterpret_cast<IrInstGenConst *>(inst), name);563 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenConst *>(inst));
573 case IrInstGenIdBinOp:564 case IrInstGenIdBinOp:
574 return destroy(reinterpret_cast<IrInstGenBinOp *>(inst), name);565 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBinOp *>(inst));
575 case IrInstGenIdCast:566 case IrInstGenIdCast:
576 return destroy(reinterpret_cast<IrInstGenCast *>(inst), name);567 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenCast *>(inst));
577 case IrInstGenIdCall:568 case IrInstGenIdCall:
578 return destroy(reinterpret_cast<IrInstGenCall *>(inst), name);569 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenCall *>(inst));
579 case IrInstGenIdCondBr:570 case IrInstGenIdCondBr:
580 return destroy(reinterpret_cast<IrInstGenCondBr *>(inst), name);571 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenCondBr *>(inst));
581 case IrInstGenIdBr:572 case IrInstGenIdBr:
582 return destroy(reinterpret_cast<IrInstGenBr *>(inst), name);573 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBr *>(inst));
583 case IrInstGenIdPhi:574 case IrInstGenIdPhi:
584 return destroy(reinterpret_cast<IrInstGenPhi *>(inst), name);575 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenPhi *>(inst));
585 case IrInstGenIdUnreachable:576 case IrInstGenIdUnreachable:
586 return destroy(reinterpret_cast<IrInstGenUnreachable *>(inst), name);577 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenUnreachable *>(inst));
587 case IrInstGenIdElemPtr:578 case IrInstGenIdElemPtr:
588 return destroy(reinterpret_cast<IrInstGenElemPtr *>(inst), name);579 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenElemPtr *>(inst));
589 case IrInstGenIdVarPtr:580 case IrInstGenIdVarPtr:
590 return destroy(reinterpret_cast<IrInstGenVarPtr *>(inst), name);581 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenVarPtr *>(inst));
591 case IrInstGenIdReturnPtr:582 case IrInstGenIdReturnPtr:
592 return destroy(reinterpret_cast<IrInstGenReturnPtr *>(inst), name);583 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenReturnPtr *>(inst));
593 case IrInstGenIdLoadPtr:584 case IrInstGenIdLoadPtr:
594 return destroy(reinterpret_cast<IrInstGenLoadPtr *>(inst), name);585 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenLoadPtr *>(inst));
595 case IrInstGenIdStorePtr:586 case IrInstGenIdStorePtr:
596 return destroy(reinterpret_cast<IrInstGenStorePtr *>(inst), name);587 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenStorePtr *>(inst));
597 case IrInstGenIdVectorStoreElem:588 case IrInstGenIdVectorStoreElem:
598 return destroy(reinterpret_cast<IrInstGenVectorStoreElem *>(inst), name);589 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenVectorStoreElem *>(inst));
599 case IrInstGenIdStructFieldPtr:590 case IrInstGenIdStructFieldPtr:
600 return destroy(reinterpret_cast<IrInstGenStructFieldPtr *>(inst), name);591 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenStructFieldPtr *>(inst));
601 case IrInstGenIdUnionFieldPtr:592 case IrInstGenIdUnionFieldPtr:
602 return destroy(reinterpret_cast<IrInstGenUnionFieldPtr *>(inst), name);593 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenUnionFieldPtr *>(inst));
603 case IrInstGenIdAsm:594 case IrInstGenIdAsm:
604 return destroy(reinterpret_cast<IrInstGenAsm *>(inst), name);595 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAsm *>(inst));
605 case IrInstGenIdTestNonNull:596 case IrInstGenIdTestNonNull:
606 return destroy(reinterpret_cast<IrInstGenTestNonNull *>(inst), name);597 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenTestNonNull *>(inst));
607 case IrInstGenIdOptionalUnwrapPtr:598 case IrInstGenIdOptionalUnwrapPtr:
608 return destroy(reinterpret_cast<IrInstGenOptionalUnwrapPtr *>(inst), name);599 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenOptionalUnwrapPtr *>(inst));
609 case IrInstGenIdPopCount:600 case IrInstGenIdPopCount:
610 return destroy(reinterpret_cast<IrInstGenPopCount *>(inst), name);601 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenPopCount *>(inst));
611 case IrInstGenIdClz:602 case IrInstGenIdClz:
612 return destroy(reinterpret_cast<IrInstGenClz *>(inst), name);603 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenClz *>(inst));
613 case IrInstGenIdCtz:604 case IrInstGenIdCtz:
614 return destroy(reinterpret_cast<IrInstGenCtz *>(inst), name);605 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenCtz *>(inst));
615 case IrInstGenIdBswap:606 case IrInstGenIdBswap:
616 return destroy(reinterpret_cast<IrInstGenBswap *>(inst), name);607 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBswap *>(inst));
617 case IrInstGenIdBitReverse:608 case IrInstGenIdBitReverse:
618 return destroy(reinterpret_cast<IrInstGenBitReverse *>(inst), name);609 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBitReverse *>(inst));
619 case IrInstGenIdSwitchBr:610 case IrInstGenIdSwitchBr:
620 return destroy(reinterpret_cast<IrInstGenSwitchBr *>(inst), name);611 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSwitchBr *>(inst));
621 case IrInstGenIdUnionTag:612 case IrInstGenIdUnionTag:
622 return destroy(reinterpret_cast<IrInstGenUnionTag *>(inst), name);613 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenUnionTag *>(inst));
623 case IrInstGenIdRef:614 case IrInstGenIdRef:
624 return destroy(reinterpret_cast<IrInstGenRef *>(inst), name);615 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenRef *>(inst));
625 case IrInstGenIdErrName:616 case IrInstGenIdErrName:
626 return destroy(reinterpret_cast<IrInstGenErrName *>(inst), name);617 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenErrName *>(inst));
627 case IrInstGenIdCmpxchg:618 case IrInstGenIdCmpxchg:
628 return destroy(reinterpret_cast<IrInstGenCmpxchg *>(inst), name);619 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenCmpxchg *>(inst));
629 case IrInstGenIdFence:620 case IrInstGenIdFence:
630 return destroy(reinterpret_cast<IrInstGenFence *>(inst), name);621 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenFence *>(inst));
631 case IrInstGenIdTruncate:622 case IrInstGenIdTruncate:
632 return destroy(reinterpret_cast<IrInstGenTruncate *>(inst), name);623 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenTruncate *>(inst));
633 case IrInstGenIdShuffleVector:624 case IrInstGenIdShuffleVector:
634 return destroy(reinterpret_cast<IrInstGenShuffleVector *>(inst), name);625 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenShuffleVector *>(inst));
635 case IrInstGenIdSplat:626 case IrInstGenIdSplat:
636 return destroy(reinterpret_cast<IrInstGenSplat *>(inst), name);627 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSplat *>(inst));
637 case IrInstGenIdBoolNot:628 case IrInstGenIdBoolNot:
638 return destroy(reinterpret_cast<IrInstGenBoolNot *>(inst), name);629 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBoolNot *>(inst));
639 case IrInstGenIdMemset:630 case IrInstGenIdMemset:
640 return destroy(reinterpret_cast<IrInstGenMemset *>(inst), name);631 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenMemset *>(inst));
641 case IrInstGenIdMemcpy:632 case IrInstGenIdMemcpy:
642 return destroy(reinterpret_cast<IrInstGenMemcpy *>(inst), name);633 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenMemcpy *>(inst));
643 case IrInstGenIdSlice:634 case IrInstGenIdSlice:
644 return destroy(reinterpret_cast<IrInstGenSlice *>(inst), name);635 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSlice *>(inst));
645 case IrInstGenIdBreakpoint:636 case IrInstGenIdBreakpoint:
646 return destroy(reinterpret_cast<IrInstGenBreakpoint *>(inst), name);637 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBreakpoint *>(inst));
647 case IrInstGenIdReturnAddress:638 case IrInstGenIdReturnAddress:
648 return destroy(reinterpret_cast<IrInstGenReturnAddress *>(inst), name);639 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenReturnAddress *>(inst));
649 case IrInstGenIdFrameAddress:640 case IrInstGenIdFrameAddress:
650 return destroy(reinterpret_cast<IrInstGenFrameAddress *>(inst), name);641 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenFrameAddress *>(inst));
651 case IrInstGenIdFrameHandle:642 case IrInstGenIdFrameHandle:
652 return destroy(reinterpret_cast<IrInstGenFrameHandle *>(inst), name);643 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenFrameHandle *>(inst));
653 case IrInstGenIdFrameSize:644 case IrInstGenIdFrameSize:
654 return destroy(reinterpret_cast<IrInstGenFrameSize *>(inst), name);645 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenFrameSize *>(inst));
655 case IrInstGenIdOverflowOp:646 case IrInstGenIdOverflowOp:
656 return destroy(reinterpret_cast<IrInstGenOverflowOp *>(inst), name);647 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenOverflowOp *>(inst));
657 case IrInstGenIdTestErr:648 case IrInstGenIdTestErr:
658 return destroy(reinterpret_cast<IrInstGenTestErr *>(inst), name);649 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenTestErr *>(inst));
659 case IrInstGenIdUnwrapErrCode:650 case IrInstGenIdUnwrapErrCode:
660 return destroy(reinterpret_cast<IrInstGenUnwrapErrCode *>(inst), name);651 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenUnwrapErrCode *>(inst));
661 case IrInstGenIdUnwrapErrPayload:652 case IrInstGenIdUnwrapErrPayload:
662 return destroy(reinterpret_cast<IrInstGenUnwrapErrPayload *>(inst), name);653 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenUnwrapErrPayload *>(inst));
663 case IrInstGenIdOptionalWrap:654 case IrInstGenIdOptionalWrap:
664 return destroy(reinterpret_cast<IrInstGenOptionalWrap *>(inst), name);655 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenOptionalWrap *>(inst));
665 case IrInstGenIdErrWrapCode:656 case IrInstGenIdErrWrapCode:
666 return destroy(reinterpret_cast<IrInstGenErrWrapCode *>(inst), name);657 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenErrWrapCode *>(inst));
667 case IrInstGenIdErrWrapPayload:658 case IrInstGenIdErrWrapPayload:
668 return destroy(reinterpret_cast<IrInstGenErrWrapPayload *>(inst), name);659 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenErrWrapPayload *>(inst));
669 case IrInstGenIdPtrCast:660 case IrInstGenIdPtrCast:
670 return destroy(reinterpret_cast<IrInstGenPtrCast *>(inst), name);661 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenPtrCast *>(inst));
671 case IrInstGenIdBitCast:662 case IrInstGenIdBitCast:
672 return destroy(reinterpret_cast<IrInstGenBitCast *>(inst), name);663 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBitCast *>(inst));
673 case IrInstGenIdWidenOrShorten:664 case IrInstGenIdWidenOrShorten:
674 return destroy(reinterpret_cast<IrInstGenWidenOrShorten *>(inst), name);665 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenWidenOrShorten *>(inst));
675 case IrInstGenIdPtrToInt:666 case IrInstGenIdPtrToInt:
676 return destroy(reinterpret_cast<IrInstGenPtrToInt *>(inst), name);667 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenPtrToInt *>(inst));
677 case IrInstGenIdIntToPtr:668 case IrInstGenIdIntToPtr:
678 return destroy(reinterpret_cast<IrInstGenIntToPtr *>(inst), name);669 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenIntToPtr *>(inst));
679 case IrInstGenIdIntToEnum:670 case IrInstGenIdIntToEnum:
680 return destroy(reinterpret_cast<IrInstGenIntToEnum *>(inst), name);671 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenIntToEnum *>(inst));
681 case IrInstGenIdIntToErr:672 case IrInstGenIdIntToErr:
682 return destroy(reinterpret_cast<IrInstGenIntToErr *>(inst), name);673 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenIntToErr *>(inst));
683 case IrInstGenIdErrToInt:674 case IrInstGenIdErrToInt:
684 return destroy(reinterpret_cast<IrInstGenErrToInt *>(inst), name);675 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenErrToInt *>(inst));
685 case IrInstGenIdTagName:676 case IrInstGenIdTagName:
686 return destroy(reinterpret_cast<IrInstGenTagName *>(inst), name);677 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenTagName *>(inst));
687 case IrInstGenIdPanic:678 case IrInstGenIdPanic:
688 return destroy(reinterpret_cast<IrInstGenPanic *>(inst), name);679 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenPanic *>(inst));
689 case IrInstGenIdFieldParentPtr:680 case IrInstGenIdFieldParentPtr:
690 return destroy(reinterpret_cast<IrInstGenFieldParentPtr *>(inst), name);681 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenFieldParentPtr *>(inst));
691 case IrInstGenIdAlignCast:682 case IrInstGenIdAlignCast:
692 return destroy(reinterpret_cast<IrInstGenAlignCast *>(inst), name);683 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAlignCast *>(inst));
693 case IrInstGenIdErrorReturnTrace:684 case IrInstGenIdErrorReturnTrace:
694 return destroy(reinterpret_cast<IrInstGenErrorReturnTrace *>(inst), name);685 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenErrorReturnTrace *>(inst));
695 case IrInstGenIdAtomicRmw:686 case IrInstGenIdAtomicRmw:
696 return destroy(reinterpret_cast<IrInstGenAtomicRmw *>(inst), name);687 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAtomicRmw *>(inst));
697 case IrInstGenIdSaveErrRetAddr:688 case IrInstGenIdSaveErrRetAddr:
698 return destroy(reinterpret_cast<IrInstGenSaveErrRetAddr *>(inst), name);689 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSaveErrRetAddr *>(inst));
699 case IrInstGenIdFloatOp:690 case IrInstGenIdFloatOp:
700 return destroy(reinterpret_cast<IrInstGenFloatOp *>(inst), name);691 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenFloatOp *>(inst));
701 case IrInstGenIdMulAdd:692 case IrInstGenIdMulAdd:
702 return destroy(reinterpret_cast<IrInstGenMulAdd *>(inst), name);693 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenMulAdd *>(inst));
703 case IrInstGenIdAtomicLoad:694 case IrInstGenIdAtomicLoad:
704 return destroy(reinterpret_cast<IrInstGenAtomicLoad *>(inst), name);695 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAtomicLoad *>(inst));
705 case IrInstGenIdAtomicStore:696 case IrInstGenIdAtomicStore:
706 return destroy(reinterpret_cast<IrInstGenAtomicStore *>(inst), name);697 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAtomicStore *>(inst));
707 case IrInstGenIdDeclVar:698 case IrInstGenIdDeclVar:
708 return destroy(reinterpret_cast<IrInstGenDeclVar *>(inst), name);699 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenDeclVar *>(inst));
709 case IrInstGenIdArrayToVector:700 case IrInstGenIdArrayToVector:
710 return destroy(reinterpret_cast<IrInstGenArrayToVector *>(inst), name);701 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenArrayToVector *>(inst));
711 case IrInstGenIdVectorToArray:702 case IrInstGenIdVectorToArray:
712 return destroy(reinterpret_cast<IrInstGenVectorToArray *>(inst), name);703 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenVectorToArray *>(inst));
713 case IrInstGenIdPtrOfArrayToSlice:704 case IrInstGenIdPtrOfArrayToSlice:
714 return destroy(reinterpret_cast<IrInstGenPtrOfArrayToSlice *>(inst), name);705 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenPtrOfArrayToSlice *>(inst));
715 case IrInstGenIdAssertZero:706 case IrInstGenIdAssertZero:
716 return destroy(reinterpret_cast<IrInstGenAssertZero *>(inst), name);707 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAssertZero *>(inst));
717 case IrInstGenIdAssertNonNull:708 case IrInstGenIdAssertNonNull:
718 return destroy(reinterpret_cast<IrInstGenAssertNonNull *>(inst), name);709 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAssertNonNull *>(inst));
719 case IrInstGenIdResizeSlice:710 case IrInstGenIdResizeSlice:
720 return destroy(reinterpret_cast<IrInstGenResizeSlice *>(inst), name);711 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenResizeSlice *>(inst));
721 case IrInstGenIdAlloca:712 case IrInstGenIdAlloca:
722 return destroy(reinterpret_cast<IrInstGenAlloca *>(inst), name);713 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAlloca *>(inst));
723 case IrInstGenIdSuspendBegin:714 case IrInstGenIdSuspendBegin:
724 return destroy(reinterpret_cast<IrInstGenSuspendBegin *>(inst), name);715 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSuspendBegin *>(inst));
725 case IrInstGenIdSuspendFinish:716 case IrInstGenIdSuspendFinish:
726 return destroy(reinterpret_cast<IrInstGenSuspendFinish *>(inst), name);717 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSuspendFinish *>(inst));
727 case IrInstGenIdResume:718 case IrInstGenIdResume:
728 return destroy(reinterpret_cast<IrInstGenResume *>(inst), name);719 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenResume *>(inst));
729 case IrInstGenIdAwait:720 case IrInstGenIdAwait:
730 return destroy(reinterpret_cast<IrInstGenAwait *>(inst), name);721 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenAwait *>(inst));
731 case IrInstGenIdSpillBegin:722 case IrInstGenIdSpillBegin:
732 return destroy(reinterpret_cast<IrInstGenSpillBegin *>(inst), name);723 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSpillBegin *>(inst));
733 case IrInstGenIdSpillEnd:724 case IrInstGenIdSpillEnd:
734 return destroy(reinterpret_cast<IrInstGenSpillEnd *>(inst), name);725 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenSpillEnd *>(inst));
735 case IrInstGenIdVectorExtractElem:726 case IrInstGenIdVectorExtractElem:
736 return destroy(reinterpret_cast<IrInstGenVectorExtractElem *>(inst), name);727 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenVectorExtractElem *>(inst));
737 case IrInstGenIdBinaryNot:728 case IrInstGenIdBinaryNot:
738 return destroy(reinterpret_cast<IrInstGenBinaryNot *>(inst), name);729 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenBinaryNot *>(inst));
739 case IrInstGenIdNegation:730 case IrInstGenIdNegation:
740 return destroy(reinterpret_cast<IrInstGenNegation *>(inst), name);731 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenNegation *>(inst));
741 case IrInstGenIdNegationWrapping:732 case IrInstGenIdNegationWrapping:
742 return destroy(reinterpret_cast<IrInstGenNegationWrapping *>(inst), name);733 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenNegationWrapping *>(inst));
743 }734 }
744 zig_unreachable();735 zig_unreachable();
745}736}
...@@ -760,34 +751,19 @@ static void ira_deref(IrAnalyze *ira) {...@@ -760,34 +751,19 @@ static void ira_deref(IrAnalyze *ira) {
760 IrInstSrc *pass1_inst = pass1_bb->instruction_list.items[inst_i];751 IrInstSrc *pass1_inst = pass1_bb->instruction_list.items[inst_i];
761 destroy_instruction_src(pass1_inst);752 destroy_instruction_src(pass1_inst);
762 }753 }
763 destroy(pass1_bb, "IrBasicBlockSrc");754 heap::c_allocator.destroy(pass1_bb);
764 }755 }
765 ira->old_irb.exec->basic_block_list.deinit();756 ira->old_irb.exec->basic_block_list.deinit();
766 ira->old_irb.exec->tld_list.deinit();757 ira->old_irb.exec->tld_list.deinit();
767 // cannot destroy here because of var->owner_exec758 heap::c_allocator.destroy(ira->old_irb.exec);
768 //destroy(ira->old_irb.exec, "IrExecutableSrc");
769 ira->src_implicit_return_type_list.deinit();759 ira->src_implicit_return_type_list.deinit();
770 ira->resume_stack.deinit();760 ira->resume_stack.deinit();
771 destroy(ira, "IrAnalyze");761 heap::c_allocator.destroy(ira);
772}762}
773763
774static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {764static ZigValue *const_ptr_pointee_unchecked_no_isf(CodeGen *g, ZigValue *const_val) {
775 assert(get_src_ptr_type(const_val->type) != nullptr);765 assert(get_src_ptr_type(const_val->type) != nullptr);
776 assert(const_val->special == ConstValSpecialStatic);766 assert(const_val->special == ConstValSpecialStatic);
777 ZigValue *result;
778
779 InferredStructField *isf = const_val->type->data.pointer.inferred_struct_field;
780 if (isf != nullptr) {
781 TypeStructField *field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
782 assert(field != nullptr);
783 if (field->is_comptime) {
784 assert(field->init_val != nullptr);
785 return field->init_val;
786 }
787 assert(const_val->data.x_ptr.special == ConstPtrSpecialRef);
788 ZigValue *struct_val = const_val->data.x_ptr.data.ref.pointee;
789 return struct_val->data.x_struct.fields[field->src_index];
790 }
791767
792 switch (type_has_one_possible_value(g, const_val->type->data.pointer.child_type)) {768 switch (type_has_one_possible_value(g, const_val->type->data.pointer.child_type)) {
793 case OnePossibleValueInvalid:769 case OnePossibleValueInvalid:
...@@ -798,6 +774,7 @@ static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {...@@ -798,6 +774,7 @@ static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {
798 break;774 break;
799 }775 }
800776
777 ZigValue *result;
801 switch (const_val->data.x_ptr.special) {778 switch (const_val->data.x_ptr.special) {
802 case ConstPtrSpecialInvalid:779 case ConstPtrSpecialInvalid:
803 zig_unreachable();780 zig_unreachable();
...@@ -843,6 +820,26 @@ static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {...@@ -843,6 +820,26 @@ static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {
843 return result;820 return result;
844}821}
845822
823static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {
824 assert(get_src_ptr_type(const_val->type) != nullptr);
825 assert(const_val->special == ConstValSpecialStatic);
826
827 InferredStructField *isf = const_val->type->data.pointer.inferred_struct_field;
828 if (isf != nullptr) {
829 TypeStructField *field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
830 assert(field != nullptr);
831 if (field->is_comptime) {
832 assert(field->init_val != nullptr);
833 return field->init_val;
834 }
835 ZigValue *struct_val = const_ptr_pointee_unchecked_no_isf(g, const_val);
836 assert(struct_val->type->id == ZigTypeIdStruct);
837 return struct_val->data.x_struct.fields[field->src_index];
838 }
839
840 return const_ptr_pointee_unchecked_no_isf(g, const_val);
841}
842
846static bool is_tuple(ZigType *type) {843static bool is_tuple(ZigType *type) {
847 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialInferredTuple;844 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialInferredTuple;
848}845}
...@@ -1010,8 +1007,8 @@ static void ir_ref_var(ZigVar *var) {...@@ -1010,8 +1007,8 @@ static void ir_ref_var(ZigVar *var) {
1010static void create_result_ptr(CodeGen *codegen, ZigType *expected_type,1007static void create_result_ptr(CodeGen *codegen, ZigType *expected_type,
1011 ZigValue **out_result, ZigValue **out_result_ptr)1008 ZigValue **out_result, ZigValue **out_result_ptr)
1012{1009{
1013 ZigValue *result = create_const_vals(1);1010 ZigValue *result = codegen->pass1_arena->create<ZigValue>();
1014 ZigValue *result_ptr = create_const_vals(1);1011 ZigValue *result_ptr = codegen->pass1_arena->create<ZigValue>();
1015 result->special = ConstValSpecialUndef;1012 result->special = ConstValSpecialUndef;
1016 result->type = expected_type;1013 result->type = expected_type;
1017 result_ptr->special = ConstValSpecialStatic;1014 result_ptr->special = ConstValSpecialStatic;
...@@ -1043,14 +1040,11 @@ ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {...@@ -1043,14 +1040,11 @@ ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {
1043 assert(result->special != ConstValSpecialRuntime);1040 assert(result->special != ConstValSpecialRuntime);
1044 ZigType *res_type = result->data.x_type;1041 ZigType *res_type = result->data.x_type;
10451042
1046 destroy(result_ptr, "ZigValue");
1047 destroy(result, "ZigValue");
1048
1049 return res_type;1043 return res_type;
1050}1044}
10511045
1052static IrBasicBlockSrc *ir_create_basic_block(IrBuilderSrc *irb, Scope *scope, const char *name_hint) {1046static IrBasicBlockSrc *ir_create_basic_block(IrBuilderSrc *irb, Scope *scope, const char *name_hint) {
1053 IrBasicBlockSrc *result = allocate<IrBasicBlockSrc>(1, "IrBasicBlockSrc");1047 IrBasicBlockSrc *result = heap::c_allocator.create<IrBasicBlockSrc>();
1054 result->scope = scope;1048 result->scope = scope;
1055 result->name_hint = name_hint;1049 result->name_hint = name_hint;
1056 result->debug_id = exec_next_debug_id(irb->exec);1050 result->debug_id = exec_next_debug_id(irb->exec);
...@@ -1059,7 +1053,7 @@ static IrBasicBlockSrc *ir_create_basic_block(IrBuilderSrc *irb, Scope *scope, c...@@ -1059,7 +1053,7 @@ static IrBasicBlockSrc *ir_create_basic_block(IrBuilderSrc *irb, Scope *scope, c
1059}1053}
10601054
1061static IrBasicBlockGen *ir_create_basic_block_gen(IrAnalyze *ira, Scope *scope, const char *name_hint) {1055static IrBasicBlockGen *ir_create_basic_block_gen(IrAnalyze *ira, Scope *scope, const char *name_hint) {
1062 IrBasicBlockGen *result = allocate<IrBasicBlockGen>(1, "IrBasicBlockGen");1056 IrBasicBlockGen *result = heap::c_allocator.create<IrBasicBlockGen>();
1063 result->scope = scope;1057 result->scope = scope;
1064 result->name_hint = name_hint;1058 result->name_hint = name_hint;
1065 result->debug_id = exec_next_debug_id_gen(ira->new_irb.exec);1059 result->debug_id = exec_next_debug_id_gen(ira->new_irb.exec);
...@@ -1976,12 +1970,7 @@ static constexpr IrInstGenId ir_inst_id(IrInstGenConst *) {...@@ -1976,12 +1970,7 @@ static constexpr IrInstGenId ir_inst_id(IrInstGenConst *) {
19761970
1977template<typename T>1971template<typename T>
1978static T *ir_create_instruction(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {1972static T *ir_create_instruction(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {
1979 const char *name = nullptr;1973 T *special_instruction = heap::c_allocator.create<T>();
1980#ifdef ZIG_ENABLE_MEM_PROFILE
1981 T *dummy = nullptr;
1982 name = ir_inst_src_type_str(ir_inst_id(dummy));
1983#endif
1984 T *special_instruction = allocate<T>(1, name);
1985 special_instruction->base.id = ir_inst_id(special_instruction);1974 special_instruction->base.id = ir_inst_id(special_instruction);
1986 special_instruction->base.base.scope = scope;1975 special_instruction->base.base.scope = scope;
1987 special_instruction->base.base.source_node = source_node;1976 special_instruction->base.base.source_node = source_node;
...@@ -1992,29 +1981,19 @@ static T *ir_create_instruction(IrBuilderSrc *irb, Scope *scope, AstNode *source...@@ -1992,29 +1981,19 @@ static T *ir_create_instruction(IrBuilderSrc *irb, Scope *scope, AstNode *source
19921981
1993template<typename T>1982template<typename T>
1994static T *ir_create_inst_gen(IrBuilderGen *irb, Scope *scope, AstNode *source_node) {1983static T *ir_create_inst_gen(IrBuilderGen *irb, Scope *scope, AstNode *source_node) {
1995 const char *name = nullptr;1984 T *special_instruction = heap::c_allocator.create<T>();
1996#ifdef ZIG_ENABLE_MEM_PROFILE
1997 T *dummy = nullptr;
1998 name = ir_inst_gen_type_str(ir_inst_id(dummy));
1999#endif
2000 T *special_instruction = allocate<T>(1, name);
2001 special_instruction->base.id = ir_inst_id(special_instruction);1985 special_instruction->base.id = ir_inst_id(special_instruction);
2002 special_instruction->base.base.scope = scope;1986 special_instruction->base.base.scope = scope;
2003 special_instruction->base.base.source_node = source_node;1987 special_instruction->base.base.source_node = source_node;
2004 special_instruction->base.base.debug_id = exec_next_debug_id_gen(irb->exec);1988 special_instruction->base.base.debug_id = exec_next_debug_id_gen(irb->exec);
2005 special_instruction->base.owner_bb = irb->current_basic_block;1989 special_instruction->base.owner_bb = irb->current_basic_block;
2006 special_instruction->base.value = allocate<ZigValue>(1, "ZigValue");1990 special_instruction->base.value = irb->codegen->pass1_arena->create<ZigValue>();
2007 return special_instruction;1991 return special_instruction;
2008}1992}
20091993
2010template<typename T>1994template<typename T>
2011static T *ir_create_inst_noval(IrBuilderGen *irb, Scope *scope, AstNode *source_node) {1995static T *ir_create_inst_noval(IrBuilderGen *irb, Scope *scope, AstNode *source_node) {
2012 const char *name = nullptr;1996 T *special_instruction = heap::c_allocator.create<T>();
2013#ifdef ZIG_ENABLE_MEM_PROFILE
2014 T *dummy = nullptr;
2015 name = ir_inst_gen_type_str(ir_inst_id(dummy));
2016#endif
2017 T *special_instruction = allocate<T>(1, name);
2018 special_instruction->base.id = ir_inst_id(special_instruction);1997 special_instruction->base.id = ir_inst_id(special_instruction);
2019 special_instruction->base.base.scope = scope;1998 special_instruction->base.base.scope = scope;
2020 special_instruction->base.base.source_node = source_node;1999 special_instruction->base.base.source_node = source_node;
...@@ -2056,11 +2035,11 @@ static T *ir_build_inst_void(IrBuilderGen *irb, Scope *scope, AstNode *source_no...@@ -2056,11 +2035,11 @@ static T *ir_build_inst_void(IrBuilderGen *irb, Scope *scope, AstNode *source_no
2056IrInstGen *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node, ZigFn *fn,2035IrInstGen *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node, ZigFn *fn,
2057 ZigType *var_type, const char *name_hint)2036 ZigType *var_type, const char *name_hint)
2058{2037{
2059 IrInstGenAlloca *alloca_gen = allocate<IrInstGenAlloca>(1);2038 IrInstGenAlloca *alloca_gen = heap::c_allocator.create<IrInstGenAlloca>();
2060 alloca_gen->base.id = IrInstGenIdAlloca;2039 alloca_gen->base.id = IrInstGenIdAlloca;
2061 alloca_gen->base.base.source_node = source_node;2040 alloca_gen->base.base.source_node = source_node;
2062 alloca_gen->base.base.scope = scope;2041 alloca_gen->base.base.scope = scope;
2063 alloca_gen->base.value = allocate<ZigValue>(1, "ZigValue");2042 alloca_gen->base.value = g->pass1_arena->create<ZigValue>();
2064 alloca_gen->base.value->type = get_pointer_to_type(g, var_type, false);2043 alloca_gen->base.value->type = get_pointer_to_type(g, var_type, false);
2065 alloca_gen->base.base.ref_count = 1;2044 alloca_gen->base.base.ref_count = 1;
2066 alloca_gen->name_hint = name_hint;2045 alloca_gen->name_hint = name_hint;
...@@ -2150,7 +2129,7 @@ static IrInstSrc *ir_build_const_undefined(IrBuilderSrc *irb, Scope *scope, AstN...@@ -2150,7 +2129,7 @@ static IrInstSrc *ir_build_const_undefined(IrBuilderSrc *irb, Scope *scope, AstN
21502129
2151static IrInstSrc *ir_build_const_uint(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, uint64_t value) {2130static IrInstSrc *ir_build_const_uint(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, uint64_t value) {
2152 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);2131 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);
2153 const_instruction->value = create_const_vals(1);2132 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2154 const_instruction->value->type = irb->codegen->builtin_types.entry_num_lit_int;2133 const_instruction->value->type = irb->codegen->builtin_types.entry_num_lit_int;
2155 const_instruction->value->special = ConstValSpecialStatic;2134 const_instruction->value->special = ConstValSpecialStatic;
2156 bigint_init_unsigned(&const_instruction->value->data.x_bigint, value);2135 bigint_init_unsigned(&const_instruction->value->data.x_bigint, value);
...@@ -2159,7 +2138,7 @@ static IrInstSrc *ir_build_const_uint(IrBuilderSrc *irb, Scope *scope, AstNode *...@@ -2159,7 +2138,7 @@ static IrInstSrc *ir_build_const_uint(IrBuilderSrc *irb, Scope *scope, AstNode *
21592138
2160static IrInstSrc *ir_build_const_bigint(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, BigInt *bigint) {2139static IrInstSrc *ir_build_const_bigint(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, BigInt *bigint) {
2161 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);2140 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);
2162 const_instruction->value = create_const_vals(1);2141 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2163 const_instruction->value->type = irb->codegen->builtin_types.entry_num_lit_int;2142 const_instruction->value->type = irb->codegen->builtin_types.entry_num_lit_int;
2164 const_instruction->value->special = ConstValSpecialStatic;2143 const_instruction->value->special = ConstValSpecialStatic;
2165 bigint_init_bigint(&const_instruction->value->data.x_bigint, bigint);2144 bigint_init_bigint(&const_instruction->value->data.x_bigint, bigint);
...@@ -2168,7 +2147,7 @@ static IrInstSrc *ir_build_const_bigint(IrBuilderSrc *irb, Scope *scope, AstNode...@@ -2168,7 +2147,7 @@ static IrInstSrc *ir_build_const_bigint(IrBuilderSrc *irb, Scope *scope, AstNode
21682147
2169static IrInstSrc *ir_build_const_bigfloat(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, BigFloat *bigfloat) {2148static IrInstSrc *ir_build_const_bigfloat(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, BigFloat *bigfloat) {
2170 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);2149 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);
2171 const_instruction->value = create_const_vals(1);2150 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2172 const_instruction->value->type = irb->codegen->builtin_types.entry_num_lit_float;2151 const_instruction->value->type = irb->codegen->builtin_types.entry_num_lit_float;
2173 const_instruction->value->special = ConstValSpecialStatic;2152 const_instruction->value->special = ConstValSpecialStatic;
2174 bigfloat_init_bigfloat(&const_instruction->value->data.x_bigfloat, bigfloat);2153 bigfloat_init_bigfloat(&const_instruction->value->data.x_bigfloat, bigfloat);
...@@ -2184,7 +2163,7 @@ static IrInstSrc *ir_build_const_null(IrBuilderSrc *irb, Scope *scope, AstNode *...@@ -2184,7 +2163,7 @@ static IrInstSrc *ir_build_const_null(IrBuilderSrc *irb, Scope *scope, AstNode *
21842163
2185static IrInstSrc *ir_build_const_usize(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, uint64_t value) {2164static IrInstSrc *ir_build_const_usize(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, uint64_t value) {
2186 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);2165 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);
2187 const_instruction->value = create_const_vals(1);2166 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2188 const_instruction->value->type = irb->codegen->builtin_types.entry_usize;2167 const_instruction->value->type = irb->codegen->builtin_types.entry_usize;
2189 const_instruction->value->special = ConstValSpecialStatic;2168 const_instruction->value->special = ConstValSpecialStatic;
2190 bigint_init_unsigned(&const_instruction->value->data.x_bigint, value);2169 bigint_init_unsigned(&const_instruction->value->data.x_bigint, value);
...@@ -2195,7 +2174,7 @@ static IrInstSrc *ir_create_const_type(IrBuilderSrc *irb, Scope *scope, AstNode...@@ -2195,7 +2174,7 @@ static IrInstSrc *ir_create_const_type(IrBuilderSrc *irb, Scope *scope, AstNode
2195 ZigType *type_entry)2174 ZigType *type_entry)
2196{2175{
2197 IrInstSrcConst *const_instruction = ir_create_instruction<IrInstSrcConst>(irb, scope, source_node);2176 IrInstSrcConst *const_instruction = ir_create_instruction<IrInstSrcConst>(irb, scope, source_node);
2198 const_instruction->value = create_const_vals(1);2177 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2199 const_instruction->value->type = irb->codegen->builtin_types.entry_type;2178 const_instruction->value->type = irb->codegen->builtin_types.entry_type;
2200 const_instruction->value->special = ConstValSpecialStatic;2179 const_instruction->value->special = ConstValSpecialStatic;
2201 const_instruction->value->data.x_type = type_entry;2180 const_instruction->value->data.x_type = type_entry;
...@@ -2212,7 +2191,7 @@ static IrInstSrc *ir_build_const_type(IrBuilderSrc *irb, Scope *scope, AstNode *...@@ -2212,7 +2191,7 @@ static IrInstSrc *ir_build_const_type(IrBuilderSrc *irb, Scope *scope, AstNode *
22122191
2213static IrInstSrc *ir_build_const_import(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, ZigType *import) {2192static IrInstSrc *ir_build_const_import(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, ZigType *import) {
2214 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);2193 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);
2215 const_instruction->value = create_const_vals(1);2194 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2216 const_instruction->value->type = irb->codegen->builtin_types.entry_type;2195 const_instruction->value->type = irb->codegen->builtin_types.entry_type;
2217 const_instruction->value->special = ConstValSpecialStatic;2196 const_instruction->value->special = ConstValSpecialStatic;
2218 const_instruction->value->data.x_type = import;2197 const_instruction->value->data.x_type = import;
...@@ -2221,7 +2200,7 @@ static IrInstSrc *ir_build_const_import(IrBuilderSrc *irb, Scope *scope, AstNode...@@ -2221,7 +2200,7 @@ static IrInstSrc *ir_build_const_import(IrBuilderSrc *irb, Scope *scope, AstNode
22212200
2222static IrInstSrc *ir_build_const_bool(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, bool value) {2201static IrInstSrc *ir_build_const_bool(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, bool value) {
2223 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);2202 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);
2224 const_instruction->value = create_const_vals(1);2203 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2225 const_instruction->value->type = irb->codegen->builtin_types.entry_bool;2204 const_instruction->value->type = irb->codegen->builtin_types.entry_bool;
2226 const_instruction->value->special = ConstValSpecialStatic;2205 const_instruction->value->special = ConstValSpecialStatic;
2227 const_instruction->value->data.x_bool = value;2206 const_instruction->value->data.x_bool = value;
...@@ -2230,7 +2209,7 @@ static IrInstSrc *ir_build_const_bool(IrBuilderSrc *irb, Scope *scope, AstNode *...@@ -2230,7 +2209,7 @@ static IrInstSrc *ir_build_const_bool(IrBuilderSrc *irb, Scope *scope, AstNode *
22302209
2231static IrInstSrc *ir_build_const_enum_literal(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, Buf *name) {2210static IrInstSrc *ir_build_const_enum_literal(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, Buf *name) {
2232 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);2211 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, source_node);
2233 const_instruction->value = create_const_vals(1);2212 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2234 const_instruction->value->type = irb->codegen->builtin_types.entry_enum_literal;2213 const_instruction->value->type = irb->codegen->builtin_types.entry_enum_literal;
2235 const_instruction->value->special = ConstValSpecialStatic;2214 const_instruction->value->special = ConstValSpecialStatic;
2236 const_instruction->value->data.x_enum_literal = name;2215 const_instruction->value->data.x_enum_literal = name;
...@@ -2239,7 +2218,7 @@ static IrInstSrc *ir_build_const_enum_literal(IrBuilderSrc *irb, Scope *scope, A...@@ -2239,7 +2218,7 @@ static IrInstSrc *ir_build_const_enum_literal(IrBuilderSrc *irb, Scope *scope, A
22392218
2240static IrInstSrc *ir_create_const_str_lit(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, Buf *str) {2219static IrInstSrc *ir_create_const_str_lit(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, Buf *str) {
2241 IrInstSrcConst *const_instruction = ir_create_instruction<IrInstSrcConst>(irb, scope, source_node);2220 IrInstSrcConst *const_instruction = ir_create_instruction<IrInstSrcConst>(irb, scope, source_node);
2242 const_instruction->value = create_const_vals(1);2221 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
2243 init_const_str_lit(irb->codegen, const_instruction->value, str);2222 init_const_str_lit(irb->codegen, const_instruction->value, str);
22442223
2245 return &const_instruction->base;2224 return &const_instruction->base;
...@@ -5237,7 +5216,7 @@ static IrInstSrc *ir_gen_return(IrBuilderSrc *irb, Scope *scope, AstNode *node,...@@ -5237,7 +5216,7 @@ static IrInstSrc *ir_gen_return(IrBuilderSrc *irb, Scope *scope, AstNode *node,
5237 switch (node->data.return_expr.kind) {5216 switch (node->data.return_expr.kind) {
5238 case ReturnKindUnconditional:5217 case ReturnKindUnconditional:
5239 {5218 {
5240 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1, "ResultLocReturn");5219 ResultLocReturn *result_loc_ret = heap::c_allocator.create<ResultLocReturn>();
5241 result_loc_ret->base.id = ResultLocIdReturn;5220 result_loc_ret->base.id = ResultLocIdReturn;
5242 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);5221 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);
52435222
...@@ -5325,7 +5304,7 @@ static IrInstSrc *ir_gen_return(IrBuilderSrc *irb, Scope *scope, AstNode *node,...@@ -5325,7 +5304,7 @@ static IrInstSrc *ir_gen_return(IrBuilderSrc *irb, Scope *scope, AstNode *node,
5325 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, node, err_val, nullptr));5304 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, node, err_val, nullptr));
5326 IrInstSrcSpillBegin *spill_begin = ir_build_spill_begin_src(irb, scope, node, err_val,5305 IrInstSrcSpillBegin *spill_begin = ir_build_spill_begin_src(irb, scope, node, err_val,
5327 SpillIdRetErrCode);5306 SpillIdRetErrCode);
5328 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1, "ResultLocReturn");5307 ResultLocReturn *result_loc_ret = heap::c_allocator.create<ResultLocReturn>();
5329 result_loc_ret->base.id = ResultLocIdReturn;5308 result_loc_ret->base.id = ResultLocIdReturn;
5330 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);5309 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);
5331 ir_build_end_expr(irb, scope, node, err_val, &result_loc_ret->base);5310 ir_build_end_expr(irb, scope, node, err_val, &result_loc_ret->base);
...@@ -5353,12 +5332,12 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s...@@ -5353,12 +5332,12 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s
5353 Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, IrInstSrc *is_comptime,5332 Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, IrInstSrc *is_comptime,
5354 bool skip_name_check)5333 bool skip_name_check)
5355{5334{
5356 ZigVar *variable_entry = allocate<ZigVar>(1, "ZigVar");5335 ZigVar *variable_entry = heap::c_allocator.create<ZigVar>();
5357 variable_entry->parent_scope = parent_scope;5336 variable_entry->parent_scope = parent_scope;
5358 variable_entry->shadowable = is_shadowable;5337 variable_entry->shadowable = is_shadowable;
5359 variable_entry->is_comptime = is_comptime;5338 variable_entry->is_comptime = is_comptime;
5360 variable_entry->src_arg_index = SIZE_MAX;5339 variable_entry->src_arg_index = SIZE_MAX;
5361 variable_entry->const_value = create_const_vals(1);5340 variable_entry->const_value = codegen->pass1_arena->create<ZigValue>();
53625341
5363 if (is_comptime != nullptr) {5342 if (is_comptime != nullptr) {
5364 is_comptime->base.ref_count += 1;5343 is_comptime->base.ref_count += 1;
...@@ -5418,15 +5397,12 @@ static ZigVar *ir_create_var(IrBuilderSrc *irb, AstNode *node, Scope *scope, Buf...@@ -5418,15 +5397,12 @@ static ZigVar *ir_create_var(IrBuilderSrc *irb, AstNode *node, Scope *scope, Buf
5418 ZigVar *var = create_local_var(irb->codegen, node, scope,5397 ZigVar *var = create_local_var(irb->codegen, node, scope,
5419 (is_underscored ? nullptr : name), src_is_const, gen_is_const,5398 (is_underscored ? nullptr : name), src_is_const, gen_is_const,
5420 (is_underscored ? true : is_shadowable), is_comptime, false);5399 (is_underscored ? true : is_shadowable), is_comptime, false);
5421 if (is_comptime != nullptr || gen_is_const) {
5422 var->owner_exec = irb->exec;
5423 }
5424 assert(var->child_scope);5400 assert(var->child_scope);
5425 return var;5401 return var;
5426}5402}
54275403
5428static ResultLocPeer *create_peer_result(ResultLocPeerParent *peer_parent) {5404static ResultLocPeer *create_peer_result(ResultLocPeerParent *peer_parent) {
5429 ResultLocPeer *result = allocate<ResultLocPeer>(1, "ResultLocPeer");5405 ResultLocPeer *result = heap::c_allocator.create<ResultLocPeer>();
5430 result->base.id = ResultLocIdPeer;5406 result->base.id = ResultLocIdPeer;
5431 result->base.source_instruction = peer_parent->base.source_instruction;5407 result->base.source_instruction = peer_parent->base.source_instruction;
5432 result->parent = peer_parent;5408 result->parent = peer_parent;
...@@ -5465,7 +5441,7 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *...@@ -5465,7 +5441,7 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
5465 scope_block->is_comptime = ir_build_const_bool(irb, parent_scope, block_node,5441 scope_block->is_comptime = ir_build_const_bool(irb, parent_scope, block_node,
5466 ir_should_inline(irb->exec, parent_scope));5442 ir_should_inline(irb->exec, parent_scope));
54675443
5468 scope_block->peer_parent = allocate<ResultLocPeerParent>(1, "ResultLocPeerParent");5444 scope_block->peer_parent = heap::c_allocator.create<ResultLocPeerParent>();
5469 scope_block->peer_parent->base.id = ResultLocIdPeerParent;5445 scope_block->peer_parent->base.id = ResultLocIdPeerParent;
5470 scope_block->peer_parent->base.source_instruction = scope_block->is_comptime;5446 scope_block->peer_parent->base.source_instruction = scope_block->is_comptime;
5471 scope_block->peer_parent->base.allow_write_through_const = result_loc->allow_write_through_const;5447 scope_block->peer_parent->base.allow_write_through_const = result_loc->allow_write_through_const;
...@@ -5555,7 +5531,7 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *...@@ -5555,7 +5531,7 @@ static IrInstSrc *ir_gen_block(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
5555 // only generate unconditional defers5531 // only generate unconditional defers
55565532
5557 ir_mark_gen(ir_build_add_implicit_return_type(irb, child_scope, block_node, result, nullptr));5533 ir_mark_gen(ir_build_add_implicit_return_type(irb, child_scope, block_node, result, nullptr));
5558 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1, "ResultLocReturn");5534 ResultLocReturn *result_loc_ret = heap::c_allocator.create<ResultLocReturn>();
5559 result_loc_ret->base.id = ResultLocIdReturn;5535 result_loc_ret->base.id = ResultLocIdReturn;
5560 ir_build_reset_result(irb, parent_scope, block_node, &result_loc_ret->base);5536 ir_build_reset_result(irb, parent_scope, block_node, &result_loc_ret->base);
5561 ir_mark_gen(ir_build_end_expr(irb, parent_scope, block_node, result, &result_loc_ret->base));5537 ir_mark_gen(ir_build_end_expr(irb, parent_scope, block_node, result, &result_loc_ret->base));
...@@ -5597,7 +5573,7 @@ static IrInstSrc *ir_gen_assign(IrBuilderSrc *irb, Scope *scope, AstNode *node)...@@ -5597,7 +5573,7 @@ static IrInstSrc *ir_gen_assign(IrBuilderSrc *irb, Scope *scope, AstNode *node)
5597 if (lvalue == irb->codegen->invalid_inst_src)5573 if (lvalue == irb->codegen->invalid_inst_src)
5598 return irb->codegen->invalid_inst_src;5574 return irb->codegen->invalid_inst_src;
55995575
5600 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1, "ResultLocInstruction");5576 ResultLocInstruction *result_loc_inst = heap::c_allocator.create<ResultLocInstruction>();
5601 result_loc_inst->base.id = ResultLocIdInstruction;5577 result_loc_inst->base.id = ResultLocIdInstruction;
5602 result_loc_inst->base.source_instruction = lvalue;5578 result_loc_inst->base.source_instruction = lvalue;
5603 ir_ref_instruction(lvalue, irb->current_basic_block);5579 ir_ref_instruction(lvalue, irb->current_basic_block);
...@@ -5669,10 +5645,10 @@ static IrInstSrc *ir_gen_bool_or(IrBuilderSrc *irb, Scope *scope, AstNode *node)...@@ -5669,10 +5645,10 @@ static IrInstSrc *ir_gen_bool_or(IrBuilderSrc *irb, Scope *scope, AstNode *node)
56695645
5670 ir_set_cursor_at_end_and_append_block(irb, true_block);5646 ir_set_cursor_at_end_and_append_block(irb, true_block);
56715647
5672 IrInstSrc **incoming_values = allocate<IrInstSrc *>(2, "IrInstSrc *");5648 IrInstSrc **incoming_values = heap::c_allocator.allocate<IrInstSrc *>(2);
5673 incoming_values[0] = val1;5649 incoming_values[0] = val1;
5674 incoming_values[1] = val2;5650 incoming_values[1] = val2;
5675 IrBasicBlockSrc **incoming_blocks = allocate<IrBasicBlockSrc *>(2, "IrBasicBlockSrc *");5651 IrBasicBlockSrc **incoming_blocks = heap::c_allocator.allocate<IrBasicBlockSrc *>(2);
5676 incoming_blocks[0] = post_val1_block;5652 incoming_blocks[0] = post_val1_block;
5677 incoming_blocks[1] = post_val2_block;5653 incoming_blocks[1] = post_val2_block;
56785654
...@@ -5711,10 +5687,10 @@ static IrInstSrc *ir_gen_bool_and(IrBuilderSrc *irb, Scope *scope, AstNode *node...@@ -5711,10 +5687,10 @@ static IrInstSrc *ir_gen_bool_and(IrBuilderSrc *irb, Scope *scope, AstNode *node
57115687
5712 ir_set_cursor_at_end_and_append_block(irb, false_block);5688 ir_set_cursor_at_end_and_append_block(irb, false_block);
57135689
5714 IrInstSrc **incoming_values = allocate<IrInstSrc *>(2);5690 IrInstSrc **incoming_values = heap::c_allocator.allocate<IrInstSrc *>(2);
5715 incoming_values[0] = val1;5691 incoming_values[0] = val1;
5716 incoming_values[1] = val2;5692 incoming_values[1] = val2;
5717 IrBasicBlockSrc **incoming_blocks = allocate<IrBasicBlockSrc *>(2, "IrBasicBlockSrc *");5693 IrBasicBlockSrc **incoming_blocks = heap::c_allocator.allocate<IrBasicBlockSrc *>(2);
5718 incoming_blocks[0] = post_val1_block;5694 incoming_blocks[0] = post_val1_block;
5719 incoming_blocks[1] = post_val2_block;5695 incoming_blocks[1] = post_val2_block;
57205696
...@@ -5724,7 +5700,7 @@ static IrInstSrc *ir_gen_bool_and(IrBuilderSrc *irb, Scope *scope, AstNode *node...@@ -5724,7 +5700,7 @@ static IrInstSrc *ir_gen_bool_and(IrBuilderSrc *irb, Scope *scope, AstNode *node
5724static ResultLocPeerParent *ir_build_result_peers(IrBuilderSrc *irb, IrInstSrc *cond_br_inst,5700static ResultLocPeerParent *ir_build_result_peers(IrBuilderSrc *irb, IrInstSrc *cond_br_inst,
5725 IrBasicBlockSrc *end_block, ResultLoc *parent, IrInstSrc *is_comptime)5701 IrBasicBlockSrc *end_block, ResultLoc *parent, IrInstSrc *is_comptime)
5726{5702{
5727 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);5703 ResultLocPeerParent *peer_parent = heap::c_allocator.create<ResultLocPeerParent>();
5728 peer_parent->base.id = ResultLocIdPeerParent;5704 peer_parent->base.id = ResultLocIdPeerParent;
5729 peer_parent->base.source_instruction = cond_br_inst;5705 peer_parent->base.source_instruction = cond_br_inst;
5730 peer_parent->base.allow_write_through_const = parent->allow_write_through_const;5706 peer_parent->base.allow_write_through_const = parent->allow_write_through_const;
...@@ -5802,10 +5778,10 @@ static IrInstSrc *ir_gen_orelse(IrBuilderSrc *irb, Scope *parent_scope, AstNode...@@ -5802,10 +5778,10 @@ static IrInstSrc *ir_gen_orelse(IrBuilderSrc *irb, Scope *parent_scope, AstNode
5802 ir_build_br(irb, parent_scope, node, end_block, is_comptime);5778 ir_build_br(irb, parent_scope, node, end_block, is_comptime);
58035779
5804 ir_set_cursor_at_end_and_append_block(irb, end_block);5780 ir_set_cursor_at_end_and_append_block(irb, end_block);
5805 IrInstSrc **incoming_values = allocate<IrInstSrc *>(2);5781 IrInstSrc **incoming_values = heap::c_allocator.allocate<IrInstSrc *>(2);
5806 incoming_values[0] = null_result;5782 incoming_values[0] = null_result;
5807 incoming_values[1] = unwrapped_payload;5783 incoming_values[1] = unwrapped_payload;
5808 IrBasicBlockSrc **incoming_blocks = allocate<IrBasicBlockSrc *>(2, "IrBasicBlockSrc *");5784 IrBasicBlockSrc **incoming_blocks = heap::c_allocator.allocate<IrBasicBlockSrc *>(2);
5809 incoming_blocks[0] = after_null_block;5785 incoming_blocks[0] = after_null_block;
5810 incoming_blocks[1] = after_ok_block;5786 incoming_blocks[1] = after_ok_block;
5811 IrInstSrc *phi = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values, peer_parent);5787 IrInstSrc *phi = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values, peer_parent);
...@@ -5959,7 +5935,7 @@ static void populate_invalid_variable_in_scope(CodeGen *g, Scope *scope, AstNode...@@ -5959,7 +5935,7 @@ static void populate_invalid_variable_in_scope(CodeGen *g, Scope *scope, AstNode
5959 }5935 }
5960 scope = scope->parent;5936 scope = scope->parent;
5961 }5937 }
5962 TldVar *tld_var = allocate<TldVar>(1);5938 TldVar *tld_var = heap::c_allocator.create<TldVar>();
5963 init_tld(&tld_var->base, TldIdVar, var_name, VisibModPub, node, &scope_decls->base);5939 init_tld(&tld_var->base, TldIdVar, var_name, VisibModPub, node, &scope_decls->base);
5964 tld_var->base.resolution = TldResolutionInvalid;5940 tld_var->base.resolution = TldResolutionInvalid;
5965 tld_var->var = add_variable(g, node, &scope_decls->base, var_name, false,5941 tld_var->var = add_variable(g, node, &scope_decls->base, var_name, false,
...@@ -5976,7 +5952,7 @@ static IrInstSrc *ir_gen_symbol(IrBuilderSrc *irb, Scope *scope, AstNode *node,...@@ -5976,7 +5952,7 @@ static IrInstSrc *ir_gen_symbol(IrBuilderSrc *irb, Scope *scope, AstNode *node,
5976 if (buf_eql_str(variable_name, "_")) {5952 if (buf_eql_str(variable_name, "_")) {
5977 if (lval == LValPtr) {5953 if (lval == LValPtr) {
5978 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, node);5954 IrInstSrcConst *const_instruction = ir_build_instruction<IrInstSrcConst>(irb, scope, node);
5979 const_instruction->value = create_const_vals(1);5955 const_instruction->value = irb->codegen->pass1_arena->create<ZigValue>();
5980 const_instruction->value->type = get_pointer_to_type(irb->codegen,5956 const_instruction->value->type = get_pointer_to_type(irb->codegen,
5981 irb->codegen->builtin_types.entry_void, false);5957 irb->codegen->builtin_types.entry_void, false);
5982 const_instruction->value->special = ConstValSpecialStatic;5958 const_instruction->value->special = ConstValSpecialStatic;
...@@ -6170,7 +6146,7 @@ static IrInstSrc *ir_gen_async_call(IrBuilderSrc *irb, Scope *scope, AstNode *aw...@@ -6170,7 +6146,7 @@ static IrInstSrc *ir_gen_async_call(IrBuilderSrc *irb, Scope *scope, AstNode *aw
6170 return fn_ref;6146 return fn_ref;
61716147
6172 size_t arg_count = call_node->data.fn_call_expr.params.length - arg_offset;6148 size_t arg_count = call_node->data.fn_call_expr.params.length - arg_offset;
6173 IrInstSrc **args = allocate<IrInstSrc*>(arg_count);6149 IrInstSrc **args = heap::c_allocator.allocate<IrInstSrc*>(arg_count);
6174 for (size_t i = 0; i < arg_count; i += 1) {6150 for (size_t i = 0; i < arg_count; i += 1) {
6175 AstNode *arg_node = call_node->data.fn_call_expr.params.at(i + arg_offset);6151 AstNode *arg_node = call_node->data.fn_call_expr.params.at(i + arg_offset);
6176 IrInstSrc *arg = ir_gen_node(irb, arg_node, scope);6152 IrInstSrc *arg = ir_gen_node(irb, arg_node, scope);
...@@ -6196,7 +6172,7 @@ static IrInstSrc *ir_gen_fn_call_with_args(IrBuilderSrc *irb, Scope *scope, AstN...@@ -6196,7 +6172,7 @@ static IrInstSrc *ir_gen_fn_call_with_args(IrBuilderSrc *irb, Scope *scope, AstN
61966172
6197 IrInstSrc *fn_type = ir_build_typeof(irb, scope, source_node, fn_ref);6173 IrInstSrc *fn_type = ir_build_typeof(irb, scope, source_node, fn_ref);
61986174
6199 IrInstSrc **args = allocate<IrInstSrc*>(args_len);6175 IrInstSrc **args = heap::c_allocator.allocate<IrInstSrc*>(args_len);
6200 for (size_t i = 0; i < args_len; i += 1) {6176 for (size_t i = 0; i < args_len; i += 1) {
6201 AstNode *arg_node = args_ptr[i];6177 AstNode *arg_node = args_ptr[i];
62026178
...@@ -6381,7 +6357,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -6381,7 +6357,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
6381 }6357 }
6382 case BuiltinFnIdCompileLog:6358 case BuiltinFnIdCompileLog:
6383 {6359 {
6384 IrInstSrc **args = allocate<IrInstSrc*>(actual_param_count);6360 IrInstSrc **args = heap::c_allocator.allocate<IrInstSrc*>(actual_param_count);
63856361
6386 for (size_t i = 0; i < actual_param_count; i += 1) {6362 for (size_t i = 0; i < actual_param_count; i += 1) {
6387 AstNode *arg_node = node->data.fn_call_expr.params.at(i);6363 AstNode *arg_node = node->data.fn_call_expr.params.at(i);
...@@ -7006,7 +6982,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -7006,7 +6982,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
7006 if (dest_type == irb->codegen->invalid_inst_src)6982 if (dest_type == irb->codegen->invalid_inst_src)
7007 return dest_type;6983 return dest_type;
70086984
7009 ResultLocBitCast *result_loc_bit_cast = allocate<ResultLocBitCast>(1);6985 ResultLocBitCast *result_loc_bit_cast = heap::c_allocator.create<ResultLocBitCast>();
7010 result_loc_bit_cast->base.id = ResultLocIdBitCast;6986 result_loc_bit_cast->base.id = ResultLocIdBitCast;
7011 result_loc_bit_cast->base.source_instruction = dest_type;6987 result_loc_bit_cast->base.source_instruction = dest_type;
7012 result_loc_bit_cast->base.allow_write_through_const = result_loc->allow_write_through_const;6988 result_loc_bit_cast->base.allow_write_through_const = result_loc->allow_write_through_const;
...@@ -7555,10 +7531,10 @@ static IrInstSrc *ir_gen_if_bool_expr(IrBuilderSrc *irb, Scope *scope, AstNode *...@@ -7555,10 +7531,10 @@ static IrInstSrc *ir_gen_if_bool_expr(IrBuilderSrc *irb, Scope *scope, AstNode *
7555 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));7531 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
75567532
7557 ir_set_cursor_at_end_and_append_block(irb, endif_block);7533 ir_set_cursor_at_end_and_append_block(irb, endif_block);
7558 IrInstSrc **incoming_values = allocate<IrInstSrc *>(2);7534 IrInstSrc **incoming_values = heap::c_allocator.allocate<IrInstSrc *>(2);
7559 incoming_values[0] = then_expr_result;7535 incoming_values[0] = then_expr_result;
7560 incoming_values[1] = else_expr_result;7536 incoming_values[1] = else_expr_result;
7561 IrBasicBlockSrc **incoming_blocks = allocate<IrBasicBlockSrc *>(2, "IrBasicBlockSrc *");7537 IrBasicBlockSrc **incoming_blocks = heap::c_allocator.allocate<IrBasicBlockSrc *>(2);
7562 incoming_blocks[0] = after_then_block;7538 incoming_blocks[0] = after_then_block;
7563 incoming_blocks[1] = after_else_block;7539 incoming_blocks[1] = after_else_block;
75647540
...@@ -7759,7 +7735,7 @@ static IrInstSrc *ir_gen_union_init_expr(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -7759,7 +7735,7 @@ static IrInstSrc *ir_gen_union_init_expr(IrBuilderSrc *irb, Scope *scope, AstNod
7759 IrInstSrc *field_ptr = ir_build_field_ptr_instruction(irb, scope, source_node, container_ptr,7735 IrInstSrc *field_ptr = ir_build_field_ptr_instruction(irb, scope, source_node, container_ptr,
7760 field_name, true);7736 field_name, true);
77617737
7762 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);7738 ResultLocInstruction *result_loc_inst = heap::c_allocator.create<ResultLocInstruction>();
7763 result_loc_inst->base.id = ResultLocIdInstruction;7739 result_loc_inst->base.id = ResultLocIdInstruction;
7764 result_loc_inst->base.source_instruction = field_ptr;7740 result_loc_inst->base.source_instruction = field_ptr;
7765 ir_ref_instruction(field_ptr, irb->current_basic_block);7741 ir_ref_instruction(field_ptr, irb->current_basic_block);
...@@ -7835,7 +7811,7 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As...@@ -7835,7 +7811,7 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As
7835 nullptr);7811 nullptr);
78367812
7837 size_t field_count = container_init_expr->entries.length;7813 size_t field_count = container_init_expr->entries.length;
7838 IrInstSrcContainerInitFieldsField *fields = allocate<IrInstSrcContainerInitFieldsField>(field_count);7814 IrInstSrcContainerInitFieldsField *fields = heap::c_allocator.allocate<IrInstSrcContainerInitFieldsField>(field_count);
7839 for (size_t i = 0; i < field_count; i += 1) {7815 for (size_t i = 0; i < field_count; i += 1) {
7840 AstNode *entry_node = container_init_expr->entries.at(i);7816 AstNode *entry_node = container_init_expr->entries.at(i);
7841 assert(entry_node->type == NodeTypeStructValueField);7817 assert(entry_node->type == NodeTypeStructValueField);
...@@ -7844,7 +7820,7 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As...@@ -7844,7 +7820,7 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As
7844 AstNode *expr_node = entry_node->data.struct_val_field.expr;7820 AstNode *expr_node = entry_node->data.struct_val_field.expr;
78457821
7846 IrInstSrc *field_ptr = ir_build_field_ptr(irb, scope, entry_node, container_ptr, name, true);7822 IrInstSrc *field_ptr = ir_build_field_ptr(irb, scope, entry_node, container_ptr, name, true);
7847 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);7823 ResultLocInstruction *result_loc_inst = heap::c_allocator.create<ResultLocInstruction>();
7848 result_loc_inst->base.id = ResultLocIdInstruction;7824 result_loc_inst->base.id = ResultLocIdInstruction;
7849 result_loc_inst->base.source_instruction = field_ptr;7825 result_loc_inst->base.source_instruction = field_ptr;
7850 result_loc_inst->base.allow_write_through_const = true;7826 result_loc_inst->base.allow_write_through_const = true;
...@@ -7874,14 +7850,14 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As...@@ -7874,14 +7850,14 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As
7874 IrInstSrc *container_ptr = ir_build_resolve_result(irb, scope, node, child_result_loc,7850 IrInstSrc *container_ptr = ir_build_resolve_result(irb, scope, node, child_result_loc,
7875 nullptr);7851 nullptr);
78767852
7877 IrInstSrc **result_locs = allocate<IrInstSrc *>(item_count);7853 IrInstSrc **result_locs = heap::c_allocator.allocate<IrInstSrc *>(item_count);
7878 for (size_t i = 0; i < item_count; i += 1) {7854 for (size_t i = 0; i < item_count; i += 1) {
7879 AstNode *expr_node = container_init_expr->entries.at(i);7855 AstNode *expr_node = container_init_expr->entries.at(i);
78807856
7881 IrInstSrc *elem_index = ir_build_const_usize(irb, scope, expr_node, i);7857 IrInstSrc *elem_index = ir_build_const_usize(irb, scope, expr_node, i);
7882 IrInstSrc *elem_ptr = ir_build_elem_ptr(irb, scope, expr_node, container_ptr,7858 IrInstSrc *elem_ptr = ir_build_elem_ptr(irb, scope, expr_node, container_ptr,
7883 elem_index, false, PtrLenSingle, init_array_type_source_node);7859 elem_index, false, PtrLenSingle, init_array_type_source_node);
7884 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);7860 ResultLocInstruction *result_loc_inst = heap::c_allocator.create<ResultLocInstruction>();
7885 result_loc_inst->base.id = ResultLocIdInstruction;7861 result_loc_inst->base.id = ResultLocIdInstruction;
7886 result_loc_inst->base.source_instruction = elem_ptr;7862 result_loc_inst->base.source_instruction = elem_ptr;
7887 result_loc_inst->base.allow_write_through_const = true;7863 result_loc_inst->base.allow_write_through_const = true;
...@@ -7907,7 +7883,7 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As...@@ -7907,7 +7883,7 @@ static IrInstSrc *ir_gen_container_init_expr(IrBuilderSrc *irb, Scope *scope, As
7907}7883}
79087884
7909static ResultLocVar *ir_build_var_result_loc(IrBuilderSrc *irb, IrInstSrc *alloca, ZigVar *var) {7885static ResultLocVar *ir_build_var_result_loc(IrBuilderSrc *irb, IrInstSrc *alloca, ZigVar *var) {
7910 ResultLocVar *result_loc_var = allocate<ResultLocVar>(1);7886 ResultLocVar *result_loc_var = heap::c_allocator.create<ResultLocVar>();
7911 result_loc_var->base.id = ResultLocIdVar;7887 result_loc_var->base.id = ResultLocIdVar;
7912 result_loc_var->base.source_instruction = alloca;7888 result_loc_var->base.source_instruction = alloca;
7913 result_loc_var->base.allow_write_through_const = true;7889 result_loc_var->base.allow_write_through_const = true;
...@@ -7921,7 +7897,7 @@ static ResultLocVar *ir_build_var_result_loc(IrBuilderSrc *irb, IrInstSrc *alloc...@@ -7921,7 +7897,7 @@ static ResultLocVar *ir_build_var_result_loc(IrBuilderSrc *irb, IrInstSrc *alloc
7921static ResultLocCast *ir_build_cast_result_loc(IrBuilderSrc *irb, IrInstSrc *dest_type,7897static ResultLocCast *ir_build_cast_result_loc(IrBuilderSrc *irb, IrInstSrc *dest_type,
7922 ResultLoc *parent_result_loc)7898 ResultLoc *parent_result_loc)
7923{7899{
7924 ResultLocCast *result_loc_cast = allocate<ResultLocCast>(1);7900 ResultLocCast *result_loc_cast = heap::c_allocator.create<ResultLocCast>();
7925 result_loc_cast->base.id = ResultLocIdCast;7901 result_loc_cast->base.id = ResultLocIdCast;
7926 result_loc_cast->base.source_instruction = dest_type;7902 result_loc_cast->base.source_instruction = dest_type;
7927 result_loc_cast->base.allow_write_through_const = parent_result_loc->allow_write_through_const;7903 result_loc_cast->base.allow_write_through_const = parent_result_loc->allow_write_through_const;
...@@ -8764,9 +8740,9 @@ static IrInstSrc *ir_gen_asm_expr(IrBuilderSrc *irb, Scope *scope, AstNode *node...@@ -8764,9 +8740,9 @@ static IrInstSrc *ir_gen_asm_expr(IrBuilderSrc *irb, Scope *scope, AstNode *node
8764 nullptr, 0, is_volatile, true);8740 nullptr, 0, is_volatile, true);
8765 }8741 }
87668742
8767 IrInstSrc **input_list = allocate<IrInstSrc *>(asm_expr->input_list.length);8743 IrInstSrc **input_list = heap::c_allocator.allocate<IrInstSrc *>(asm_expr->input_list.length);
8768 IrInstSrc **output_types = allocate<IrInstSrc *>(asm_expr->output_list.length);8744 IrInstSrc **output_types = heap::c_allocator.allocate<IrInstSrc *>(asm_expr->output_list.length);
8769 ZigVar **output_vars = allocate<ZigVar *>(asm_expr->output_list.length);8745 ZigVar **output_vars = heap::c_allocator.allocate<ZigVar *>(asm_expr->output_list.length);
8770 size_t return_count = 0;8746 size_t return_count = 0;
8771 if (!is_volatile && asm_expr->output_list.length == 0) {8747 if (!is_volatile && asm_expr->output_list.length == 0) {
8772 add_node_error(irb->codegen, node,8748 add_node_error(irb->codegen, node,
...@@ -8900,10 +8876,10 @@ static IrInstSrc *ir_gen_if_optional_expr(IrBuilderSrc *irb, Scope *scope, AstNo...@@ -8900,10 +8876,10 @@ static IrInstSrc *ir_gen_if_optional_expr(IrBuilderSrc *irb, Scope *scope, AstNo
8900 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));8876 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
89018877
8902 ir_set_cursor_at_end_and_append_block(irb, endif_block);8878 ir_set_cursor_at_end_and_append_block(irb, endif_block);
8903 IrInstSrc **incoming_values = allocate<IrInstSrc *>(2);8879 IrInstSrc **incoming_values = heap::c_allocator.allocate<IrInstSrc *>(2);
8904 incoming_values[0] = then_expr_result;8880 incoming_values[0] = then_expr_result;
8905 incoming_values[1] = else_expr_result;8881 incoming_values[1] = else_expr_result;
8906 IrBasicBlockSrc **incoming_blocks = allocate<IrBasicBlockSrc *>(2, "IrBasicBlockSrc *");8882 IrBasicBlockSrc **incoming_blocks = heap::c_allocator.allocate<IrBasicBlockSrc *>(2);
8907 incoming_blocks[0] = after_then_block;8883 incoming_blocks[0] = after_then_block;
8908 incoming_blocks[1] = after_else_block;8884 incoming_blocks[1] = after_else_block;
89098885
...@@ -8997,10 +8973,10 @@ static IrInstSrc *ir_gen_if_err_expr(IrBuilderSrc *irb, Scope *scope, AstNode *n...@@ -8997,10 +8973,10 @@ static IrInstSrc *ir_gen_if_err_expr(IrBuilderSrc *irb, Scope *scope, AstNode *n
8997 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));8973 ir_mark_gen(ir_build_br(irb, scope, node, endif_block, is_comptime));
89988974
8999 ir_set_cursor_at_end_and_append_block(irb, endif_block);8975 ir_set_cursor_at_end_and_append_block(irb, endif_block);
9000 IrInstSrc **incoming_values = allocate<IrInstSrc *>(2);8976 IrInstSrc **incoming_values = heap::c_allocator.allocate<IrInstSrc *>(2);
9001 incoming_values[0] = then_expr_result;8977 incoming_values[0] = then_expr_result;
9002 incoming_values[1] = else_expr_result;8978 incoming_values[1] = else_expr_result;
9003 IrBasicBlockSrc **incoming_blocks = allocate<IrBasicBlockSrc *>(2, "IrBasicBlockSrc *");8979 IrBasicBlockSrc **incoming_blocks = heap::c_allocator.allocate<IrBasicBlockSrc *>(2);
9004 incoming_blocks[0] = after_then_block;8980 incoming_blocks[0] = after_then_block;
9005 incoming_blocks[1] = after_else_block;8981 incoming_blocks[1] = after_else_block;
90068982
...@@ -9093,7 +9069,7 @@ static IrInstSrc *ir_gen_switch_expr(IrBuilderSrc *irb, Scope *scope, AstNode *n...@@ -9093,7 +9069,7 @@ static IrInstSrc *ir_gen_switch_expr(IrBuilderSrc *irb, Scope *scope, AstNode *n
90939069
9094 IrInstSrcSwitchElseVar *switch_else_var = nullptr;9070 IrInstSrcSwitchElseVar *switch_else_var = nullptr;
90959071
9096 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);9072 ResultLocPeerParent *peer_parent = heap::c_allocator.create<ResultLocPeerParent>();
9097 peer_parent->base.id = ResultLocIdPeerParent;9073 peer_parent->base.id = ResultLocIdPeerParent;
9098 peer_parent->base.allow_write_through_const = result_loc->allow_write_through_const;9074 peer_parent->base.allow_write_through_const = result_loc->allow_write_through_const;
9099 peer_parent->end_bb = end_block;9075 peer_parent->end_bb = end_block;
...@@ -9255,7 +9231,7 @@ static IrInstSrc *ir_gen_switch_expr(IrBuilderSrc *irb, Scope *scope, AstNode *n...@@ -9255,7 +9231,7 @@ static IrInstSrc *ir_gen_switch_expr(IrBuilderSrc *irb, Scope *scope, AstNode *n
9255 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);9231 ResultLocPeer *this_peer_result_loc = create_peer_result(peer_parent);
92569232
9257 IrBasicBlockSrc *prong_block = ir_create_basic_block(irb, scope, "SwitchProng");9233 IrBasicBlockSrc *prong_block = ir_create_basic_block(irb, scope, "SwitchProng");
9258 IrInstSrc **items = allocate<IrInstSrc *>(prong_item_count);9234 IrInstSrc **items = heap::c_allocator.allocate<IrInstSrc *>(prong_item_count);
92599235
9260 for (size_t item_i = 0; item_i < prong_item_count; item_i += 1) {9236 for (size_t item_i = 0; item_i < prong_item_count; item_i += 1) {
9261 AstNode *item_node = prong_node->data.switch_prong.items.at(item_i);9237 AstNode *item_node = prong_node->data.switch_prong.items.at(item_i);
...@@ -9637,10 +9613,10 @@ static IrInstSrc *ir_gen_catch(IrBuilderSrc *irb, Scope *parent_scope, AstNode *...@@ -9637,10 +9613,10 @@ static IrInstSrc *ir_gen_catch(IrBuilderSrc *irb, Scope *parent_scope, AstNode *
9637 ir_build_br(irb, parent_scope, node, end_block, is_comptime);9613 ir_build_br(irb, parent_scope, node, end_block, is_comptime);
96389614
9639 ir_set_cursor_at_end_and_append_block(irb, end_block);9615 ir_set_cursor_at_end_and_append_block(irb, end_block);
9640 IrInstSrc **incoming_values = allocate<IrInstSrc *>(2);9616 IrInstSrc **incoming_values = heap::c_allocator.allocate<IrInstSrc *>(2);
9641 incoming_values[0] = err_result;9617 incoming_values[0] = err_result;
9642 incoming_values[1] = unwrapped_payload;9618 incoming_values[1] = unwrapped_payload;
9643 IrBasicBlockSrc **incoming_blocks = allocate<IrBasicBlockSrc *>(2, "IrBasicBlockSrc *");9619 IrBasicBlockSrc **incoming_blocks = heap::c_allocator.allocate<IrBasicBlockSrc *>(2);
9644 incoming_blocks[0] = after_err_block;9620 incoming_blocks[0] = after_err_block;
9645 incoming_blocks[1] = after_ok_block;9621 incoming_blocks[1] = after_ok_block;
9646 IrInstSrc *phi = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values, peer_parent);9622 IrInstSrc *phi = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values, peer_parent);
...@@ -9707,7 +9683,7 @@ static IrInstSrc *ir_gen_container_decl(IrBuilderSrc *irb, Scope *parent_scope,...@@ -9707,7 +9683,7 @@ static IrInstSrc *ir_gen_container_decl(IrBuilderSrc *irb, Scope *parent_scope,
9707 scan_decls(irb->codegen, child_scope, child_node);9683 scan_decls(irb->codegen, child_scope, child_node);
9708 }9684 }
97099685
9710 TldContainer *tld_container = allocate<TldContainer>(1);9686 TldContainer *tld_container = heap::c_allocator.create<TldContainer>();
9711 init_tld(&tld_container->base, TldIdContainer, bare_name, VisibModPub, node, parent_scope);9687 init_tld(&tld_container->base, TldIdContainer, bare_name, VisibModPub, node, parent_scope);
9712 tld_container->type_entry = container_type;9688 tld_container->type_entry = container_type;
9713 tld_container->decls_scope = child_scope;9689 tld_container->decls_scope = child_scope;
...@@ -9750,7 +9726,7 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp...@@ -9750,7 +9726,7 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
9750 }9726 }
97519727
9752 err_set_type->data.error_set.err_count = count;9728 err_set_type->data.error_set.err_count = count;
9753 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(count);9729 err_set_type->data.error_set.errors = heap::c_allocator.allocate<ErrorTableEntry *>(count);
97549730
9755 bool need_comma = false;9731 bool need_comma = false;
9756 for (uint32_t i = 0; i < set1->data.error_set.err_count; i += 1) {9732 for (uint32_t i = 0; i < set1->data.error_set.err_count; i += 1) {
...@@ -9797,7 +9773,7 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN...@@ -9797,7 +9773,7 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN
9797 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;9773 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
9798 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;9774 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
9799 err_set_type->data.error_set.err_count = 1;9775 err_set_type->data.error_set.err_count = 1;
9800 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(1);9776 err_set_type->data.error_set.errors = heap::c_allocator.create<ErrorTableEntry *>();
98019777
9802 err_set_type->data.error_set.errors[0] = err_entry;9778 err_set_type->data.error_set.errors[0] = err_entry;
98039779
...@@ -9828,16 +9804,16 @@ static IrInstSrc *ir_gen_err_set_decl(IrBuilderSrc *irb, Scope *parent_scope, As...@@ -9828,16 +9804,16 @@ static IrInstSrc *ir_gen_err_set_decl(IrBuilderSrc *irb, Scope *parent_scope, As
9828 err_set_type->size_in_bits = irb->codegen->builtin_types.entry_global_error_set->size_in_bits;9804 err_set_type->size_in_bits = irb->codegen->builtin_types.entry_global_error_set->size_in_bits;
9829 err_set_type->abi_align = irb->codegen->builtin_types.entry_global_error_set->abi_align;9805 err_set_type->abi_align = irb->codegen->builtin_types.entry_global_error_set->abi_align;
9830 err_set_type->abi_size = irb->codegen->builtin_types.entry_global_error_set->abi_size;9806 err_set_type->abi_size = irb->codegen->builtin_types.entry_global_error_set->abi_size;
9831 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(err_count);9807 err_set_type->data.error_set.errors = heap::c_allocator.allocate<ErrorTableEntry *>(err_count);
98329808
9833 size_t errors_count = irb->codegen->errors_by_index.length + err_count;9809 size_t errors_count = irb->codegen->errors_by_index.length + err_count;
9834 ErrorTableEntry **errors = allocate<ErrorTableEntry *>(errors_count, "ErrorTableEntry *");9810 ErrorTableEntry **errors = heap::c_allocator.allocate<ErrorTableEntry *>(errors_count);
98359811
9836 for (uint32_t i = 0; i < err_count; i += 1) {9812 for (uint32_t i = 0; i < err_count; i += 1) {
9837 AstNode *field_node = node->data.err_set_decl.decls.at(i);9813 AstNode *field_node = node->data.err_set_decl.decls.at(i);
9838 AstNode *symbol_node = ast_field_to_symbol_node(field_node);9814 AstNode *symbol_node = ast_field_to_symbol_node(field_node);
9839 Buf *err_name = symbol_node->data.symbol_expr.symbol;9815 Buf *err_name = symbol_node->data.symbol_expr.symbol;
9840 ErrorTableEntry *err = allocate<ErrorTableEntry>(1);9816 ErrorTableEntry *err = heap::c_allocator.create<ErrorTableEntry>();
9841 err->decl_node = field_node;9817 err->decl_node = field_node;
9842 buf_init_from_buf(&err->name, err_name);9818 buf_init_from_buf(&err->name, err_name);
98439819
...@@ -9862,7 +9838,7 @@ static IrInstSrc *ir_gen_err_set_decl(IrBuilderSrc *irb, Scope *parent_scope, As...@@ -9862,7 +9838,7 @@ static IrInstSrc *ir_gen_err_set_decl(IrBuilderSrc *irb, Scope *parent_scope, As
9862 }9838 }
9863 errors[err->value] = err;9839 errors[err->value] = err;
9864 }9840 }
9865 deallocate(errors, errors_count, "ErrorTableEntry *");9841 heap::c_allocator.deallocate(errors, errors_count);
9866 return ir_build_const_type(irb, parent_scope, node, err_set_type);9842 return ir_build_const_type(irb, parent_scope, node, err_set_type);
9867}9843}
98689844
...@@ -9870,7 +9846,7 @@ static IrInstSrc *ir_gen_fn_proto(IrBuilderSrc *irb, Scope *parent_scope, AstNod...@@ -9870,7 +9846,7 @@ static IrInstSrc *ir_gen_fn_proto(IrBuilderSrc *irb, Scope *parent_scope, AstNod
9870 assert(node->type == NodeTypeFnProto);9846 assert(node->type == NodeTypeFnProto);
98719847
9872 size_t param_count = node->data.fn_proto.params.length;9848 size_t param_count = node->data.fn_proto.params.length;
9873 IrInstSrc **param_types = allocate<IrInstSrc*>(param_count);9849 IrInstSrc **param_types = heap::c_allocator.allocate<IrInstSrc*>(param_count);
98749850
9875 bool is_var_args = false;9851 bool is_var_args = false;
9876 for (size_t i = 0; i < param_count; i += 1) {9852 for (size_t i = 0; i < param_count; i += 1) {
...@@ -10151,7 +10127,7 @@ static IrInstSrc *ir_gen_node_raw(IrBuilderSrc *irb, AstNode *node, Scope *scope...@@ -10151,7 +10127,7 @@ static IrInstSrc *ir_gen_node_raw(IrBuilderSrc *irb, AstNode *node, Scope *scope
10151}10127}
1015210128
10153static ResultLoc *no_result_loc(void) {10129static ResultLoc *no_result_loc(void) {
10154 ResultLocNone *result_loc_none = allocate<ResultLocNone>(1);10130 ResultLocNone *result_loc_none = heap::c_allocator.create<ResultLocNone>();
10155 result_loc_none->base.id = ResultLocIdNone;10131 result_loc_none->base.id = ResultLocIdNone;
10156 return &result_loc_none->base;10132 return &result_loc_none->base;
10157}10133}
...@@ -10240,7 +10216,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutableSrc *ir_e...@@ -10240,7 +10216,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutableSrc *ir_e
10240 if (!instr_is_unreachable(result)) {10216 if (!instr_is_unreachable(result)) {
10241 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->base.source_node, result, nullptr));10217 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->base.source_node, result, nullptr));
10242 // no need for save_err_ret_addr because this cannot return error10218 // no need for save_err_ret_addr because this cannot return error
10243 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1, "ResultLocReturn");10219 ResultLocReturn *result_loc_ret = heap::c_allocator.create<ResultLocReturn>();
10244 result_loc_ret->base.id = ResultLocIdReturn;10220 result_loc_ret->base.id = ResultLocIdReturn;
10245 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);10221 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);
10246 ir_mark_gen(ir_build_end_expr(irb, scope, node, result, &result_loc_ret->base));10222 ir_mark_gen(ir_build_end_expr(irb, scope, node, result, &result_loc_ret->base));
...@@ -10332,7 +10308,7 @@ static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, Ast...@@ -10332,7 +10308,7 @@ static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, Ast
10332 if ((err = ir_read_const_ptr(ira, codegen, source_node, &tmp, ptr_val)))10308 if ((err = ir_read_const_ptr(ira, codegen, source_node, &tmp, ptr_val)))
10333 return err;10309 return err;
10334 ZigValue *child_val = const_ptr_pointee_unchecked(codegen, ptr_val);10310 ZigValue *child_val = const_ptr_pointee_unchecked(codegen, ptr_val);
10335 copy_const_val(child_val, &tmp);10311 copy_const_val(codegen, child_val, &tmp);
10336 return ErrorNone;10312 return ErrorNone;
10337}10313}
1033810314
...@@ -11482,7 +11458,7 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp...@@ -11482,7 +11458,7 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
11482 return set1;11458 return set1;
11483 }11459 }
11484 size_t errors_count = ira->codegen->errors_by_index.length;11460 size_t errors_count = ira->codegen->errors_by_index.length;
11485 ErrorTableEntry **errors = allocate<ErrorTableEntry *>(errors_count, "ErrorTableEntry *");11461 ErrorTableEntry **errors = heap::c_allocator.allocate<ErrorTableEntry *>(errors_count);
11486 populate_error_set_table(errors, set1);11462 populate_error_set_table(errors, set1);
11487 ZigList<ErrorTableEntry *> intersection_list = {};11463 ZigList<ErrorTableEntry *> intersection_list = {};
1148811464
...@@ -11503,7 +11479,7 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp...@@ -11503,7 +11479,7 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
11503 buf_appendf(&err_set_type->name, "%s%s", comma, buf_ptr(&existing_entry_with_docs->name));11479 buf_appendf(&err_set_type->name, "%s%s", comma, buf_ptr(&existing_entry_with_docs->name));
11504 }11480 }
11505 }11481 }
11506 deallocate(errors, errors_count, "ErrorTableEntry *");11482 heap::c_allocator.deallocate(errors, errors_count);
1150711483
11508 err_set_type->data.error_set.err_count = intersection_list.length;11484 err_set_type->data.error_set.err_count = intersection_list.length;
11509 err_set_type->data.error_set.errors = intersection_list.items;11485 err_set_type->data.error_set.errors = intersection_list.items;
...@@ -11552,10 +11528,11 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11552,10 +11528,11 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11552 wanted_ptr_type->data.pointer.sentinel == nullptr ||11528 wanted_ptr_type->data.pointer.sentinel == nullptr ||
11553 (actual_ptr_type->data.pointer.sentinel != nullptr &&11529 (actual_ptr_type->data.pointer.sentinel != nullptr &&
11554 const_values_equal(ira->codegen, wanted_ptr_type->data.pointer.sentinel,11530 const_values_equal(ira->codegen, wanted_ptr_type->data.pointer.sentinel,
11555 actual_ptr_type->data.pointer.sentinel));11531 actual_ptr_type->data.pointer.sentinel)) ||
11532 actual_ptr_type->data.pointer.ptr_len == PtrLenC;
11556 if (!ok_null_term_ptrs) {11533 if (!ok_null_term_ptrs) {
11557 result.id = ConstCastResultIdPtrSentinel;11534 result.id = ConstCastResultIdPtrSentinel;
11558 result.data.bad_ptr_sentinel = allocate_nonzero<ConstCastPtrSentinel>(1);11535 result.data.bad_ptr_sentinel = heap::c_allocator.allocate_nonzero<ConstCastPtrSentinel>(1);
11559 result.data.bad_ptr_sentinel->wanted_type = wanted_ptr_type;11536 result.data.bad_ptr_sentinel->wanted_type = wanted_ptr_type;
11560 result.data.bad_ptr_sentinel->actual_type = actual_ptr_type;11537 result.data.bad_ptr_sentinel->actual_type = actual_ptr_type;
11561 return result;11538 return result;
...@@ -11571,7 +11548,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11571,7 +11548,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11571 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile);11548 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile);
11572 if (!ok_cv_qualifiers) {11549 if (!ok_cv_qualifiers) {
11573 result.id = ConstCastResultIdCV;11550 result.id = ConstCastResultIdCV;
11574 result.data.bad_cv = allocate_nonzero<ConstCastBadCV>(1);11551 result.data.bad_cv = heap::c_allocator.allocate_nonzero<ConstCastBadCV>(1);
11575 result.data.bad_cv->wanted_type = wanted_ptr_type;11552 result.data.bad_cv->wanted_type = wanted_ptr_type;
11576 result.data.bad_cv->actual_type = actual_ptr_type;11553 result.data.bad_cv->actual_type = actual_ptr_type;
11577 return result;11554 return result;
...@@ -11583,7 +11560,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11583,7 +11560,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11583 return child;11560 return child;
11584 if (child.id != ConstCastResultIdOk) {11561 if (child.id != ConstCastResultIdOk) {
11585 result.id = ConstCastResultIdPointerChild;11562 result.id = ConstCastResultIdPointerChild;
11586 result.data.pointer_mismatch = allocate_nonzero<ConstCastPointerMismatch>(1);11563 result.data.pointer_mismatch = heap::c_allocator.allocate_nonzero<ConstCastPointerMismatch>(1);
11587 result.data.pointer_mismatch->child = child;11564 result.data.pointer_mismatch->child = child;
11588 result.data.pointer_mismatch->wanted_child = wanted_ptr_type->data.pointer.child_type;11565 result.data.pointer_mismatch->wanted_child = wanted_ptr_type->data.pointer.child_type;
11589 result.data.pointer_mismatch->actual_child = actual_ptr_type->data.pointer.child_type;11566 result.data.pointer_mismatch->actual_child = actual_ptr_type->data.pointer.child_type;
...@@ -11594,7 +11571,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11594,7 +11571,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11594 (!wanted_allows_zero && !actual_allows_zero);11571 (!wanted_allows_zero && !actual_allows_zero);
11595 if (!ok_allows_zero) {11572 if (!ok_allows_zero) {
11596 result.id = ConstCastResultIdBadAllowsZero;11573 result.id = ConstCastResultIdBadAllowsZero;
11597 result.data.bad_allows_zero = allocate_nonzero<ConstCastBadAllowsZero>(1);11574 result.data.bad_allows_zero = heap::c_allocator.allocate_nonzero<ConstCastBadAllowsZero>(1);
11598 result.data.bad_allows_zero->wanted_type = wanted_type;11575 result.data.bad_allows_zero->wanted_type = wanted_type;
11599 result.data.bad_allows_zero->actual_type = actual_type;11576 result.data.bad_allows_zero->actual_type = actual_type;
11600 return result;11577 return result;
...@@ -11634,7 +11611,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11634,7 +11611,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11634 return child;11611 return child;
11635 if (child.id != ConstCastResultIdOk) {11612 if (child.id != ConstCastResultIdOk) {
11636 result.id = ConstCastResultIdArrayChild;11613 result.id = ConstCastResultIdArrayChild;
11637 result.data.array_mismatch = allocate_nonzero<ConstCastArrayMismatch>(1);11614 result.data.array_mismatch = heap::c_allocator.allocate_nonzero<ConstCastArrayMismatch>(1);
11638 result.data.array_mismatch->child = child;11615 result.data.array_mismatch->child = child;
11639 result.data.array_mismatch->wanted_child = wanted_type->data.array.child_type;11616 result.data.array_mismatch->wanted_child = wanted_type->data.array.child_type;
11640 result.data.array_mismatch->actual_child = actual_type->data.array.child_type;11617 result.data.array_mismatch->actual_child = actual_type->data.array.child_type;
...@@ -11645,7 +11622,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11645,7 +11622,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11645 const_values_equal(ira->codegen, wanted_type->data.array.sentinel, actual_type->data.array.sentinel));11622 const_values_equal(ira->codegen, wanted_type->data.array.sentinel, actual_type->data.array.sentinel));
11646 if (!ok_null_terminated) {11623 if (!ok_null_terminated) {
11647 result.id = ConstCastResultIdSentinelArrays;11624 result.id = ConstCastResultIdSentinelArrays;
11648 result.data.sentinel_arrays = allocate_nonzero<ConstCastBadNullTermArrays>(1);11625 result.data.sentinel_arrays = heap::c_allocator.allocate_nonzero<ConstCastBadNullTermArrays>(1);
11649 result.data.sentinel_arrays->child = child;11626 result.data.sentinel_arrays->child = child;
11650 result.data.sentinel_arrays->wanted_type = wanted_type;11627 result.data.sentinel_arrays->wanted_type = wanted_type;
11651 result.data.sentinel_arrays->actual_type = actual_type;11628 result.data.sentinel_arrays->actual_type = actual_type;
...@@ -11673,7 +11650,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11673,7 +11650,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11673 actual_ptr_type->data.pointer.sentinel));11650 actual_ptr_type->data.pointer.sentinel));
11674 if (!ok_sentinels) {11651 if (!ok_sentinels) {
11675 result.id = ConstCastResultIdPtrSentinel;11652 result.id = ConstCastResultIdPtrSentinel;
11676 result.data.bad_ptr_sentinel = allocate_nonzero<ConstCastPtrSentinel>(1);11653 result.data.bad_ptr_sentinel = heap::c_allocator.allocate_nonzero<ConstCastPtrSentinel>(1);
11677 result.data.bad_ptr_sentinel->wanted_type = wanted_ptr_type;11654 result.data.bad_ptr_sentinel->wanted_type = wanted_ptr_type;
11678 result.data.bad_ptr_sentinel->actual_type = actual_ptr_type;11655 result.data.bad_ptr_sentinel->actual_type = actual_ptr_type;
11679 return result;11656 return result;
...@@ -11690,7 +11667,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11690,7 +11667,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11690 return child;11667 return child;
11691 if (child.id != ConstCastResultIdOk) {11668 if (child.id != ConstCastResultIdOk) {
11692 result.id = ConstCastResultIdSliceChild;11669 result.id = ConstCastResultIdSliceChild;
11693 result.data.slice_mismatch = allocate_nonzero<ConstCastSliceMismatch>(1);11670 result.data.slice_mismatch = heap::c_allocator.allocate_nonzero<ConstCastSliceMismatch>(1);
11694 result.data.slice_mismatch->child = child;11671 result.data.slice_mismatch->child = child;
11695 result.data.slice_mismatch->actual_child = actual_ptr_type->data.pointer.child_type;11672 result.data.slice_mismatch->actual_child = actual_ptr_type->data.pointer.child_type;
11696 result.data.slice_mismatch->wanted_child = wanted_ptr_type->data.pointer.child_type;11673 result.data.slice_mismatch->wanted_child = wanted_ptr_type->data.pointer.child_type;
...@@ -11707,7 +11684,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11707,7 +11684,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11707 return child;11684 return child;
11708 if (child.id != ConstCastResultIdOk) {11685 if (child.id != ConstCastResultIdOk) {
11709 result.id = ConstCastResultIdOptionalChild;11686 result.id = ConstCastResultIdOptionalChild;
11710 result.data.optional = allocate_nonzero<ConstCastOptionalMismatch>(1);11687 result.data.optional = heap::c_allocator.allocate_nonzero<ConstCastOptionalMismatch>(1);
11711 result.data.optional->child = child;11688 result.data.optional->child = child;
11712 result.data.optional->wanted_child = wanted_type->data.maybe.child_type;11689 result.data.optional->wanted_child = wanted_type->data.maybe.child_type;
11713 result.data.optional->actual_child = actual_type->data.maybe.child_type;11690 result.data.optional->actual_child = actual_type->data.maybe.child_type;
...@@ -11723,7 +11700,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11723,7 +11700,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11723 return payload_child;11700 return payload_child;
11724 if (payload_child.id != ConstCastResultIdOk) {11701 if (payload_child.id != ConstCastResultIdOk) {
11725 result.id = ConstCastResultIdErrorUnionPayload;11702 result.id = ConstCastResultIdErrorUnionPayload;
11726 result.data.error_union_payload = allocate_nonzero<ConstCastErrUnionPayloadMismatch>(1);11703 result.data.error_union_payload = heap::c_allocator.allocate_nonzero<ConstCastErrUnionPayloadMismatch>(1);
11727 result.data.error_union_payload->child = payload_child;11704 result.data.error_union_payload->child = payload_child;
11728 result.data.error_union_payload->wanted_payload = wanted_type->data.error_union.payload_type;11705 result.data.error_union_payload->wanted_payload = wanted_type->data.error_union.payload_type;
11729 result.data.error_union_payload->actual_payload = actual_type->data.error_union.payload_type;11706 result.data.error_union_payload->actual_payload = actual_type->data.error_union.payload_type;
...@@ -11735,7 +11712,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11735,7 +11712,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11735 return error_set_child;11712 return error_set_child;
11736 if (error_set_child.id != ConstCastResultIdOk) {11713 if (error_set_child.id != ConstCastResultIdOk) {
11737 result.id = ConstCastResultIdErrorUnionErrorSet;11714 result.id = ConstCastResultIdErrorUnionErrorSet;
11738 result.data.error_union_error_set = allocate_nonzero<ConstCastErrUnionErrSetMismatch>(1);11715 result.data.error_union_error_set = heap::c_allocator.allocate_nonzero<ConstCastErrUnionErrSetMismatch>(1);
11739 result.data.error_union_error_set->child = error_set_child;11716 result.data.error_union_error_set->child = error_set_child;
11740 result.data.error_union_error_set->wanted_err_set = wanted_type->data.error_union.err_set_type;11717 result.data.error_union_error_set->wanted_err_set = wanted_type->data.error_union.err_set_type;
11741 result.data.error_union_error_set->actual_err_set = actual_type->data.error_union.err_set_type;11718 result.data.error_union_error_set->actual_err_set = actual_type->data.error_union.err_set_type;
...@@ -11769,7 +11746,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11769,7 +11746,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11769 }11746 }
1177011747
11771 size_t errors_count = g->errors_by_index.length;11748 size_t errors_count = g->errors_by_index.length;
11772 ErrorTableEntry **errors = allocate<ErrorTableEntry *>(errors_count, "ErrorTableEntry *");11749 ErrorTableEntry **errors = heap::c_allocator.allocate<ErrorTableEntry *>(errors_count);
11773 for (uint32_t i = 0; i < container_set->data.error_set.err_count; i += 1) {11750 for (uint32_t i = 0; i < container_set->data.error_set.err_count; i += 1) {
11774 ErrorTableEntry *error_entry = container_set->data.error_set.errors[i];11751 ErrorTableEntry *error_entry = container_set->data.error_set.errors[i];
11775 assert(errors[error_entry->value] == nullptr);11752 assert(errors[error_entry->value] == nullptr);
...@@ -11781,12 +11758,12 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11781,12 +11758,12 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11781 if (error_entry == nullptr) {11758 if (error_entry == nullptr) {
11782 if (result.id == ConstCastResultIdOk) {11759 if (result.id == ConstCastResultIdOk) {
11783 result.id = ConstCastResultIdErrSet;11760 result.id = ConstCastResultIdErrSet;
11784 result.data.error_set_mismatch = allocate<ConstCastErrSetMismatch>(1);11761 result.data.error_set_mismatch = heap::c_allocator.create<ConstCastErrSetMismatch>();
11785 }11762 }
11786 result.data.error_set_mismatch->missing_errors.append(contained_error_entry);11763 result.data.error_set_mismatch->missing_errors.append(contained_error_entry);
11787 }11764 }
11788 }11765 }
11789 deallocate(errors, errors_count, "ErrorTableEntry *");11766 heap::c_allocator.deallocate(errors, errors_count);
11790 return result;11767 return result;
11791 }11768 }
1179211769
...@@ -11815,7 +11792,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11815,7 +11792,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11815 return child;11792 return child;
11816 if (child.id != ConstCastResultIdOk) {11793 if (child.id != ConstCastResultIdOk) {
11817 result.id = ConstCastResultIdFnReturnType;11794 result.id = ConstCastResultIdFnReturnType;
11818 result.data.return_type = allocate_nonzero<ConstCastOnly>(1);11795 result.data.return_type = heap::c_allocator.allocate_nonzero<ConstCastOnly>(1);
11819 *result.data.return_type = child;11796 *result.data.return_type = child;
11820 return result;11797 return result;
11821 }11798 }
...@@ -11844,7 +11821,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11844,7 +11821,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11844 result.data.fn_arg.arg_index = i;11821 result.data.fn_arg.arg_index = i;
11845 result.data.fn_arg.actual_param_type = actual_param_info->type;11822 result.data.fn_arg.actual_param_type = actual_param_info->type;
11846 result.data.fn_arg.expected_param_type = expected_param_info->type;11823 result.data.fn_arg.expected_param_type = expected_param_info->type;
11847 result.data.fn_arg.child = allocate_nonzero<ConstCastOnly>(1);11824 result.data.fn_arg.child = heap::c_allocator.allocate_nonzero<ConstCastOnly>(1);
11848 *result.data.fn_arg.child = arg_child;11825 *result.data.fn_arg.child = arg_child;
11849 return result;11826 return result;
11850 }11827 }
...@@ -11864,15 +11841,20 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11864,15 +11841,20 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11864 }11841 }
1186511842
11866 if (wanted_type->id == ZigTypeIdInt && actual_type->id == ZigTypeIdInt) {11843 if (wanted_type->id == ZigTypeIdInt && actual_type->id == ZigTypeIdInt) {
11867 result.id = ConstCastResultIdIntShorten;11844 if (wanted_type->data.integral.is_signed != actual_type->data.integral.is_signed ||
11868 result.data.int_shorten = allocate_nonzero<ConstCastIntShorten>(1);11845 wanted_type->data.integral.bit_count != actual_type->data.integral.bit_count)
11869 result.data.int_shorten->wanted_type = wanted_type;11846 {
11870 result.data.int_shorten->actual_type = actual_type;11847 result.id = ConstCastResultIdIntShorten;
11848 result.data.int_shorten = heap::c_allocator.allocate_nonzero<ConstCastIntShorten>(1);
11849 result.data.int_shorten->wanted_type = wanted_type;
11850 result.data.int_shorten->actual_type = actual_type;
11851 return result;
11852 }
11871 return result;11853 return result;
11872 }11854 }
1187311855
11874 result.id = ConstCastResultIdType;11856 result.id = ConstCastResultIdType;
11875 result.data.type_mismatch = allocate_nonzero<ConstCastTypeMismatch>(1);11857 result.data.type_mismatch = heap::c_allocator.allocate_nonzero<ConstCastTypeMismatch>(1);
11876 result.data.type_mismatch->wanted_type = wanted_type;11858 result.data.type_mismatch->wanted_type = wanted_type;
11877 result.data.type_mismatch->actual_type = actual_type;11859 result.data.type_mismatch->actual_type = actual_type;
11878 return result;11860 return result;
...@@ -11881,7 +11863,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11881,7 +11863,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11881static void update_errors_helper(CodeGen *g, ErrorTableEntry ***errors, size_t *errors_count) {11863static void update_errors_helper(CodeGen *g, ErrorTableEntry ***errors, size_t *errors_count) {
11882 size_t old_errors_count = *errors_count;11864 size_t old_errors_count = *errors_count;
11883 *errors_count = g->errors_by_index.length;11865 *errors_count = g->errors_by_index.length;
11884 *errors = reallocate(*errors, old_errors_count, *errors_count);11866 *errors = heap::c_allocator.reallocate(*errors, old_errors_count, *errors_count);
11885}11867}
1188611868
11887static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigType *expected_type,11869static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigType *expected_type,
...@@ -12551,7 +12533,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -12551,7 +12533,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
12551 return ira->codegen->builtin_types.entry_invalid;12533 return ira->codegen->builtin_types.entry_invalid;
12552 }12534 }
1255312535
12554 free(errors);12536 heap::c_allocator.deallocate(errors, errors_count);
1255512537
12556 if (convert_to_const_slice) {12538 if (convert_to_const_slice) {
12557 if (prev_inst->value->type->id == ZigTypeIdPointer) {12539 if (prev_inst->value->type->id == ZigTypeIdPointer) {
...@@ -12630,7 +12612,7 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr,...@@ -12630,7 +12612,7 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr,
12630 case CastOpBitCast:12612 case CastOpBitCast:
12631 zig_panic("TODO");12613 zig_panic("TODO");
12632 case CastOpNoop: {12614 case CastOpNoop: {
12633 copy_const_val(const_val, other_val);12615 copy_const_val(ira->codegen, const_val, other_val);
12634 const_val->type = new_type;12616 const_val->type = new_type;
12635 break;12617 break;
12636 }12618 }
...@@ -12770,13 +12752,19 @@ static IrInstGen *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira, IrI...@@ -12770,13 +12752,19 @@ static IrInstGen *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira, IrI
12770 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, value->value->type));12752 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, value->value->type));
1277112753
12772 if (instr_is_comptime(value)) {12754 if (instr_is_comptime(value)) {
12773 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, value->value, source_instr->source_node);12755 ZigValue *val = ir_resolve_const(ira, value, UndefOk);
12756 if (val == nullptr)
12757 return ira->codegen->invalid_inst_gen;
12758 if (val->special == ConstValSpecialUndef)
12759 return ir_const_undef(ira, source_instr, wanted_type);
12760
12761 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, val, source_instr->source_node);
12774 if (pointee == nullptr)12762 if (pointee == nullptr)
12775 return ira->codegen->invalid_inst_gen;12763 return ira->codegen->invalid_inst_gen;
12776 if (pointee->special != ConstValSpecialRuntime) {12764 if (pointee->special != ConstValSpecialRuntime) {
12777 IrInstGen *result = ir_const(ira, source_instr, wanted_type);12765 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
12778 result->value->data.x_ptr.special = ConstPtrSpecialBaseArray;12766 result->value->data.x_ptr.special = ConstPtrSpecialBaseArray;
12779 result->value->data.x_ptr.mut = value->value->data.x_ptr.mut;12767 result->value->data.x_ptr.mut = val->data.x_ptr.mut;
12780 result->value->data.x_ptr.data.base_array.array_val = pointee;12768 result->value->data.x_ptr.data.base_array.array_val = pointee;
12781 result->value->data.x_ptr.data.base_array.elem_index = 0;12769 result->value->data.x_ptr.data.base_array.elem_index = 0;
12782 return result;12770 return result;
...@@ -13159,7 +13147,7 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,...@@ -13159,7 +13147,7 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13159 if (type_is_invalid(return_ptr->type))13147 if (type_is_invalid(return_ptr->type))
13160 return ErrorSemanticAnalyzeFail;13148 return ErrorSemanticAnalyzeFail;
1316113149
13162 IrExecutableSrc *ir_executable = allocate<IrExecutableSrc>(1, "IrExecutableSrc");13150 IrExecutableSrc *ir_executable = heap::c_allocator.create<IrExecutableSrc>();
13163 ir_executable->source_node = source_node;13151 ir_executable->source_node = source_node;
13164 ir_executable->parent_exec = parent_exec;13152 ir_executable->parent_exec = parent_exec;
13165 ir_executable->name = exec_name;13153 ir_executable->name = exec_name;
...@@ -13183,7 +13171,7 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,...@@ -13183,7 +13171,7 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
13183 ir_print_src(codegen, stderr, ir_executable, 2);13171 ir_print_src(codegen, stderr, ir_executable, 2);
13184 fprintf(stderr, "}\n");13172 fprintf(stderr, "}\n");
13185 }13173 }
13186 IrExecutableGen *analyzed_executable = allocate<IrExecutableGen>(1, "IrExecutableGen");13174 IrExecutableGen *analyzed_executable = heap::c_allocator.create<IrExecutableGen>();
13187 analyzed_executable->source_node = source_node;13175 analyzed_executable->source_node = source_node;
13188 analyzed_executable->parent_exec = parent_exec;13176 analyzed_executable->parent_exec = parent_exec;
13189 analyzed_executable->source_exec = ir_executable;13177 analyzed_executable->source_exec = ir_executable;
...@@ -13384,7 +13372,7 @@ static IrInstGen *ir_analyze_optional_wrap(IrAnalyze *ira, IrInst* source_instr,...@@ -13384,7 +13372,7 @@ static IrInstGen *ir_analyze_optional_wrap(IrAnalyze *ira, IrInst* source_instr,
13384 source_instr->scope, source_instr->source_node);13372 source_instr->scope, source_instr->source_node);
13385 const_instruction->base.value->special = ConstValSpecialStatic;13373 const_instruction->base.value->special = ConstValSpecialStatic;
13386 if (types_have_same_zig_comptime_repr(ira->codegen, wanted_type, payload_type)) {13374 if (types_have_same_zig_comptime_repr(ira->codegen, wanted_type, payload_type)) {
13387 copy_const_val(const_instruction->base.value, val);13375 copy_const_val(ira->codegen, const_instruction->base.value, val);
13388 } else {13376 } else {
13389 const_instruction->base.value->data.x_optional = val;13377 const_instruction->base.value->data.x_optional = val;
13390 }13378 }
...@@ -13425,7 +13413,7 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins...@@ -13425,7 +13413,7 @@ static IrInstGen *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInst* source_ins
13425 if (val == nullptr)13413 if (val == nullptr)
13426 return ira->codegen->invalid_inst_gen;13414 return ira->codegen->invalid_inst_gen;
1342713415
13428 ZigValue *err_set_val = create_const_vals(1);13416 ZigValue *err_set_val = ira->codegen->pass1_arena->create<ZigValue>();
13429 err_set_val->type = err_set_type;13417 err_set_val->type = err_set_type;
13430 err_set_val->special = ConstValSpecialStatic;13418 err_set_val->special = ConstValSpecialStatic;
13431 err_set_val->data.x_err_set = nullptr;13419 err_set_val->data.x_err_set = nullptr;
...@@ -13537,7 +13525,7 @@ static IrInstGen *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInst* source_instr,...@@ -13537,7 +13525,7 @@ static IrInstGen *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInst* source_instr,
13537 if (!val)13525 if (!val)
13538 return ira->codegen->invalid_inst_gen;13526 return ira->codegen->invalid_inst_gen;
1353913527
13540 ZigValue *err_set_val = create_const_vals(1);13528 ZigValue *err_set_val = ira->codegen->pass1_arena->create<ZigValue>();
13541 err_set_val->special = ConstValSpecialStatic;13529 err_set_val->special = ConstValSpecialStatic;
13542 err_set_val->type = wanted_type->data.error_union.err_set_type;13530 err_set_val->type = wanted_type->data.error_union.err_set_type;
13543 err_set_val->data.x_err_set = val->data.x_err_set;13531 err_set_val->data.x_err_set = val->data.x_err_set;
...@@ -13802,7 +13790,7 @@ static IrInstGen *ir_analyze_enum_to_union(IrAnalyze *ira, IrInst* source_instr,...@@ -13802,7 +13790,7 @@ static IrInstGen *ir_analyze_enum_to_union(IrAnalyze *ira, IrInst* source_instr,
13802 result->value->special = ConstValSpecialStatic;13790 result->value->special = ConstValSpecialStatic;
13803 result->value->type = wanted_type;13791 result->value->type = wanted_type;
13804 bigint_init_bigint(&result->value->data.x_union.tag, &val->data.x_enum_tag);13792 bigint_init_bigint(&result->value->data.x_union.tag, &val->data.x_enum_tag);
13805 result->value->data.x_union.payload = create_const_vals(1);13793 result->value->data.x_union.payload = ira->codegen->pass1_arena->create<ZigValue>();
13806 result->value->data.x_union.payload->special = ConstValSpecialStatic;13794 result->value->data.x_union.payload->special = ConstValSpecialStatic;
13807 result->value->data.x_union.payload->type = field_type;13795 result->value->data.x_union.payload->type = field_type;
13808 return result;13796 return result;
...@@ -14107,7 +14095,7 @@ static IrInstGen *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInst* source_instr,...@@ -14107,7 +14095,7 @@ static IrInstGen *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInst* source_instr,
14107 if (pointee == nullptr)14095 if (pointee == nullptr)
14108 return ira->codegen->invalid_inst_gen;14096 return ira->codegen->invalid_inst_gen;
14109 if (pointee->special != ConstValSpecialRuntime) {14097 if (pointee->special != ConstValSpecialRuntime) {
14110 ZigValue *array_val = create_const_vals(1);14098 ZigValue *array_val = ira->codegen->pass1_arena->create<ZigValue>();
14111 array_val->special = ConstValSpecialStatic;14099 array_val->special = ConstValSpecialStatic;
14112 array_val->type = array_type;14100 array_val->type = array_type;
14113 array_val->data.x_array.special = ConstArraySpecialNone;14101 array_val->data.x_array.special = ConstArraySpecialNone;
...@@ -14321,7 +14309,7 @@ static IrInstGen *ir_analyze_array_to_vector(IrAnalyze *ira, IrInst* source_inst...@@ -14321,7 +14309,7 @@ static IrInstGen *ir_analyze_array_to_vector(IrAnalyze *ira, IrInst* source_inst
14321 if (instr_is_comptime(array)) {14309 if (instr_is_comptime(array)) {
14322 // arrays and vectors have the same ZigValue representation14310 // arrays and vectors have the same ZigValue representation
14323 IrInstGen *result = ir_const(ira, source_instr, vector_type);14311 IrInstGen *result = ir_const(ira, source_instr, vector_type);
14324 copy_const_val(result->value, array->value);14312 copy_const_val(ira->codegen, result->value, array->value);
14325 result->value->type = vector_type;14313 result->value->type = vector_type;
14326 return result;14314 return result;
14327 }14315 }
...@@ -14334,7 +14322,7 @@ static IrInstGen *ir_analyze_vector_to_array(IrAnalyze *ira, IrInst* source_inst...@@ -14334,7 +14322,7 @@ static IrInstGen *ir_analyze_vector_to_array(IrAnalyze *ira, IrInst* source_inst
14334 if (instr_is_comptime(vector)) {14322 if (instr_is_comptime(vector)) {
14335 // arrays and vectors have the same ZigValue representation14323 // arrays and vectors have the same ZigValue representation
14336 IrInstGen *result = ir_const(ira, source_instr, array_type);14324 IrInstGen *result = ir_const(ira, source_instr, array_type);
14337 copy_const_val(result->value, vector->value);14325 copy_const_val(ira->codegen, result->value, vector->value);
14338 result->value->type = array_type;14326 result->value->type = array_type;
14339 return result;14327 return result;
14340 }14328 }
...@@ -14634,7 +14622,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,...@@ -14634,7 +14622,7 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
14634 if (wanted_type->id == ZigTypeIdComptimeInt || wanted_type->id == ZigTypeIdInt) {14622 if (wanted_type->id == ZigTypeIdComptimeInt || wanted_type->id == ZigTypeIdInt) {
14635 IrInstGen *result = ir_const(ira, source_instr, wanted_type);14623 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
14636 if (actual_type->id == ZigTypeIdComptimeInt || actual_type->id == ZigTypeIdInt) {14624 if (actual_type->id == ZigTypeIdComptimeInt || actual_type->id == ZigTypeIdInt) {
14637 copy_const_val(result->value, value->value);14625 copy_const_val(ira->codegen, result->value, value->value);
14638 result->value->type = wanted_type;14626 result->value->type = wanted_type;
14639 } else {14627 } else {
14640 float_init_bigint(&result->value->data.x_bigint, value->value);14628 float_init_bigint(&result->value->data.x_bigint, value->value);
...@@ -14826,6 +14814,16 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,...@@ -14826,6 +14814,16 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
14826 }14814 }
14827 }14815 }
1482814816
14817 // @Vector(N,T1) to @Vector(N,T2)
14818 if (actual_type->id == ZigTypeIdVector && wanted_type->id == ZigTypeIdVector) {
14819 if (actual_type->data.vector.len == wanted_type->data.vector.len &&
14820 types_match_const_cast_only(ira, wanted_type->data.vector.elem_type,
14821 actual_type->data.vector.elem_type, source_node, false).id == ConstCastResultIdOk)
14822 {
14823 return ir_analyze_bit_cast(ira, source_instr, value, wanted_type);
14824 }
14825 }
14826
14829 // *@Frame(func) to anyframe->T or anyframe14827 // *@Frame(func) to anyframe->T or anyframe
14830 // *@Frame(func) to ?anyframe->T or ?anyframe14828 // *@Frame(func) to ?anyframe->T or ?anyframe
14831 // *@Frame(func) to E!anyframe->T or E!anyframe14829 // *@Frame(func) to E!anyframe->T or E!anyframe
...@@ -16395,9 +16393,11 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i...@@ -16395,9 +16393,11 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
16395 case ZigTypeIdComptimeInt:16393 case ZigTypeIdComptimeInt:
16396 case ZigTypeIdInt:16394 case ZigTypeIdInt:
16397 case ZigTypeIdFloat:16395 case ZigTypeIdFloat:
16398 case ZigTypeIdVector:
16399 zig_unreachable(); // handled with the type_is_numeric checks above16396 zig_unreachable(); // handled with the type_is_numeric checks above
1640016397
16398 case ZigTypeIdVector:
16399 // Not every case is handled by the type_is_numeric checks above,
16400 // vectors of bool trigger this code path
16401 case ZigTypeIdBool:16401 case ZigTypeIdBool:
16402 case ZigTypeIdMetaType:16402 case ZigTypeIdMetaType:
16403 case ZigTypeIdVoid:16403 case ZigTypeIdVoid:
...@@ -16467,7 +16467,7 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i...@@ -16467,7 +16467,7 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
16467 IrInstGen *result = ir_const(ira, &bin_op_instruction->base.base,16467 IrInstGen *result = ir_const(ira, &bin_op_instruction->base.base,
16468 get_vector_type(ira->codegen, resolved_type->data.vector.len, ira->codegen->builtin_types.entry_bool));16468 get_vector_type(ira->codegen, resolved_type->data.vector.len, ira->codegen->builtin_types.entry_bool));
16469 result->value->data.x_array.data.s_none.elements =16469 result->value->data.x_array.data.s_none.elements =
16470 create_const_vals(resolved_type->data.vector.len);16470 ira->codegen->pass1_arena->allocate<ZigValue>(resolved_type->data.vector.len);
1647116471
16472 expand_undef_array(ira->codegen, result->value);16472 expand_undef_array(ira->codegen, result->value);
16473 for (size_t i = 0;i < resolved_type->data.vector.len;i++) {16473 for (size_t i = 0;i < resolved_type->data.vector.len;i++) {
...@@ -16475,7 +16475,7 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i...@@ -16475,7 +16475,7 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
16475 &op1_val->data.x_array.data.s_none.elements[i],16475 &op1_val->data.x_array.data.s_none.elements[i],
16476 &op2_val->data.x_array.data.s_none.elements[i],16476 &op2_val->data.x_array.data.s_none.elements[i],
16477 bin_op_instruction, op_id, one_possible_value);16477 bin_op_instruction, op_id, one_possible_value);
16478 copy_const_val(&result->value->data.x_array.data.s_none.elements[i], cur_res->value);16478 copy_const_val(ira->codegen, &result->value->data.x_array.data.s_none.elements[i], cur_res->value);
16479 }16479 }
16480 return result;16480 return result;
16481 }16481 }
...@@ -17375,7 +17375,7 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi...@@ -17375,7 +17375,7 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi
17375 ZigValue *out_array_val;17375 ZigValue *out_array_val;
17376 size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index);17376 size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index);
17377 if (op1_type->id == ZigTypeIdPointer || op2_type->id == ZigTypeIdPointer) {17377 if (op1_type->id == ZigTypeIdPointer || op2_type->id == ZigTypeIdPointer) {
17378 out_array_val = create_const_vals(1);17378 out_array_val = ira->codegen->pass1_arena->create<ZigValue>();
17379 out_array_val->special = ConstValSpecialStatic;17379 out_array_val->special = ConstValSpecialStatic;
17380 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);17380 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);
1738117381
...@@ -17387,11 +17387,11 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi...@@ -17387,11 +17387,11 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi
17387 true, false, PtrLenUnknown, 0, 0, 0, false,17387 true, false, PtrLenUnknown, 0, 0, 0, false,
17388 VECTOR_INDEX_NONE, nullptr, sentinel);17388 VECTOR_INDEX_NONE, nullptr, sentinel);
17389 result->value->type = get_slice_type(ira->codegen, ptr_type);17389 result->value->type = get_slice_type(ira->codegen, ptr_type);
17390 out_array_val = create_const_vals(1);17390 out_array_val = ira->codegen->pass1_arena->create<ZigValue>();
17391 out_array_val->special = ConstValSpecialStatic;17391 out_array_val->special = ConstValSpecialStatic;
17392 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);17392 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);
1739317393
17394 out_val->data.x_struct.fields = alloc_const_vals_ptrs(2);17394 out_val->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
1739517395
17396 out_val->data.x_struct.fields[slice_ptr_index]->type = ptr_type;17396 out_val->data.x_struct.fields[slice_ptr_index]->type = ptr_type;
17397 out_val->data.x_struct.fields[slice_ptr_index]->special = ConstValSpecialStatic;17397 out_val->data.x_struct.fields[slice_ptr_index]->special = ConstValSpecialStatic;
...@@ -17408,7 +17408,7 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi...@@ -17408,7 +17408,7 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi
17408 } else {17408 } else {
17409 result->value->type = get_pointer_to_type_extra2(ira->codegen, child_type, true, false, PtrLenUnknown,17409 result->value->type = get_pointer_to_type_extra2(ira->codegen, child_type, true, false, PtrLenUnknown,
17410 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, sentinel);17410 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, sentinel);
17411 out_array_val = create_const_vals(1);17411 out_array_val = ira->codegen->pass1_arena->create<ZigValue>();
17412 out_array_val->special = ConstValSpecialStatic;17412 out_array_val->special = ConstValSpecialStatic;
17413 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);17413 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);
17414 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;17414 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
...@@ -17424,7 +17424,7 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi...@@ -17424,7 +17424,7 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi
17424 }17424 }
1742517425
17426 uint64_t full_len = new_len + ((sentinel != nullptr) ? 1 : 0);17426 uint64_t full_len = new_len + ((sentinel != nullptr) ? 1 : 0);
17427 out_array_val->data.x_array.data.s_none.elements = create_const_vals(full_len);17427 out_array_val->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(full_len);
17428 // TODO handle the buf case here for an optimization17428 // TODO handle the buf case here for an optimization
17429 expand_undef_array(ira->codegen, op1_array_val);17429 expand_undef_array(ira->codegen, op1_array_val);
17430 expand_undef_array(ira->codegen, op2_array_val);17430 expand_undef_array(ira->codegen, op2_array_val);
...@@ -17432,21 +17432,21 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi...@@ -17432,21 +17432,21 @@ static IrInstGen *ir_analyze_array_cat(IrAnalyze *ira, IrInstSrcBinOp *instructi
17432 size_t next_index = 0;17432 size_t next_index = 0;
17433 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {17433 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
17434 ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];17434 ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
17435 copy_const_val(elem_dest_val, &op1_array_val->data.x_array.data.s_none.elements[i]);17435 copy_const_val(ira->codegen, elem_dest_val, &op1_array_val->data.x_array.data.s_none.elements[i]);
17436 elem_dest_val->parent.id = ConstParentIdArray;17436 elem_dest_val->parent.id = ConstParentIdArray;
17437 elem_dest_val->parent.data.p_array.array_val = out_array_val;17437 elem_dest_val->parent.data.p_array.array_val = out_array_val;
17438 elem_dest_val->parent.data.p_array.elem_index = next_index;17438 elem_dest_val->parent.data.p_array.elem_index = next_index;
17439 }17439 }
17440 for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {17440 for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {
17441 ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];17441 ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
17442 copy_const_val(elem_dest_val, &op2_array_val->data.x_array.data.s_none.elements[i]);17442 copy_const_val(ira->codegen, elem_dest_val, &op2_array_val->data.x_array.data.s_none.elements[i]);
17443 elem_dest_val->parent.id = ConstParentIdArray;17443 elem_dest_val->parent.id = ConstParentIdArray;
17444 elem_dest_val->parent.data.p_array.array_val = out_array_val;17444 elem_dest_val->parent.data.p_array.array_val = out_array_val;
17445 elem_dest_val->parent.data.p_array.elem_index = next_index;17445 elem_dest_val->parent.data.p_array.elem_index = next_index;
17446 }17446 }
17447 if (next_index < full_len) {17447 if (next_index < full_len) {
17448 ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];17448 ZigValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
17449 copy_const_val(elem_dest_val, sentinel);17449 copy_const_val(ira->codegen, elem_dest_val, sentinel);
17450 elem_dest_val->parent.id = ConstParentIdArray;17450 elem_dest_val->parent.id = ConstParentIdArray;
17451 elem_dest_val->parent.data.p_array.array_val = out_array_val;17451 elem_dest_val->parent.data.p_array.array_val = out_array_val;
17452 elem_dest_val->parent.data.p_array.elem_index = next_index;17452 elem_dest_val->parent.data.p_array.elem_index = next_index;
...@@ -17525,13 +17525,13 @@ static IrInstGen *ir_analyze_array_mult(IrAnalyze *ira, IrInstSrcBinOp *instruct...@@ -17525,13 +17525,13 @@ static IrInstGen *ir_analyze_array_mult(IrAnalyze *ira, IrInstSrcBinOp *instruct
17525 // TODO optimize the buf case17525 // TODO optimize the buf case
17526 expand_undef_array(ira->codegen, array_val);17526 expand_undef_array(ira->codegen, array_val);
17527 size_t extra_null_term = (array_type->data.array.sentinel != nullptr) ? 1 : 0;17527 size_t extra_null_term = (array_type->data.array.sentinel != nullptr) ? 1 : 0;
17528 out_val->data.x_array.data.s_none.elements = create_const_vals(new_array_len + extra_null_term);17528 out_val->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(new_array_len + extra_null_term);
1752917529
17530 uint64_t i = 0;17530 uint64_t i = 0;
17531 for (uint64_t x = 0; x < mult_amt; x += 1) {17531 for (uint64_t x = 0; x < mult_amt; x += 1) {
17532 for (uint64_t y = 0; y < old_array_len; y += 1) {17532 for (uint64_t y = 0; y < old_array_len; y += 1) {
17533 ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];17533 ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];
17534 copy_const_val(elem_dest_val, &array_val->data.x_array.data.s_none.elements[y]);17534 copy_const_val(ira->codegen, elem_dest_val, &array_val->data.x_array.data.s_none.elements[y]);
17535 elem_dest_val->parent.id = ConstParentIdArray;17535 elem_dest_val->parent.id = ConstParentIdArray;
17536 elem_dest_val->parent.data.p_array.array_val = out_val;17536 elem_dest_val->parent.data.p_array.array_val = out_val;
17537 elem_dest_val->parent.data.p_array.elem_index = i;17537 elem_dest_val->parent.data.p_array.elem_index = i;
...@@ -17542,7 +17542,7 @@ static IrInstGen *ir_analyze_array_mult(IrAnalyze *ira, IrInstSrcBinOp *instruct...@@ -17542,7 +17542,7 @@ static IrInstGen *ir_analyze_array_mult(IrAnalyze *ira, IrInstSrcBinOp *instruct
1754217542
17543 if (array_type->data.array.sentinel != nullptr) {17543 if (array_type->data.array.sentinel != nullptr) {
17544 ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];17544 ZigValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];
17545 copy_const_val(elem_dest_val, array_type->data.array.sentinel);17545 copy_const_val(ira->codegen, elem_dest_val, array_type->data.array.sentinel);
17546 elem_dest_val->parent.id = ConstParentIdArray;17546 elem_dest_val->parent.id = ConstParentIdArray;
17547 elem_dest_val->parent.data.p_array.array_val = out_val;17547 elem_dest_val->parent.data.p_array.array_val = out_val;
17548 elem_dest_val->parent.data.p_array.elem_index = i;17548 elem_dest_val->parent.data.p_array.elem_index = i;
...@@ -17583,14 +17583,14 @@ static IrInstGen *ir_analyze_instruction_merge_err_sets(IrAnalyze *ira,...@@ -17583,14 +17583,14 @@ static IrInstGen *ir_analyze_instruction_merge_err_sets(IrAnalyze *ira,
17583 }17583 }
1758417584
17585 size_t errors_count = ira->codegen->errors_by_index.length;17585 size_t errors_count = ira->codegen->errors_by_index.length;
17586 ErrorTableEntry **errors = allocate<ErrorTableEntry *>(errors_count, "ErrorTableEntry *");17586 ErrorTableEntry **errors = heap::c_allocator.allocate<ErrorTableEntry *>(errors_count);
17587 for (uint32_t i = 0, count = op1_type->data.error_set.err_count; i < count; i += 1) {17587 for (uint32_t i = 0, count = op1_type->data.error_set.err_count; i < count; i += 1) {
17588 ErrorTableEntry *error_entry = op1_type->data.error_set.errors[i];17588 ErrorTableEntry *error_entry = op1_type->data.error_set.errors[i];
17589 assert(errors[error_entry->value] == nullptr);17589 assert(errors[error_entry->value] == nullptr);
17590 errors[error_entry->value] = error_entry;17590 errors[error_entry->value] = error_entry;
17591 }17591 }
17592 ZigType *result_type = get_error_set_union(ira->codegen, errors, op1_type, op2_type, instruction->type_name);17592 ZigType *result_type = get_error_set_union(ira->codegen, errors, op1_type, op2_type, instruction->type_name);
17593 deallocate(errors, errors_count, "ErrorTableEntry *");17593 heap::c_allocator.deallocate(errors, errors_count);
1759417594
17595 return ir_const_type(ira, &instruction->base.base, result_type);17595 return ir_const_type(ira, &instruction->base.base, result_type);
17596}17596}
...@@ -17689,8 +17689,8 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV...@@ -17689,8 +17689,8 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
17689 if (var->gen_is_const) {17689 if (var->gen_is_const) {
17690 var->const_value = init_val;17690 var->const_value = init_val;
17691 } else {17691 } else {
17692 var->const_value = create_const_vals(1);17692 var->const_value = ira->codegen->pass1_arena->create<ZigValue>();
17693 copy_const_val(var->const_value, init_val);17693 copy_const_val(ira->codegen, var->const_value, init_val);
17694 }17694 }
17695 }17695 }
17696 }17696 }
...@@ -17864,7 +17864,7 @@ static IrInstGen *ir_analyze_instruction_export(IrAnalyze *ira, IrInstSrcExport...@@ -17864,7 +17864,7 @@ static IrInstGen *ir_analyze_instruction_export(IrAnalyze *ira, IrInstSrcExport
17864 // It's not clear how all the different types are supposed to be handled.17864 // It's not clear how all the different types are supposed to be handled.
17865 // Need comprehensive tests for exporting one thing in one file and declaring an extern var17865 // Need comprehensive tests for exporting one thing in one file and declaring an extern var
17866 // in another file.17866 // in another file.
17867 TldFn *tld_fn = allocate<TldFn>(1);17867 TldFn *tld_fn = heap::c_allocator.create<TldFn>();
17868 tld_fn->base.id = TldIdFn;17868 tld_fn->base.id = TldIdFn;
17869 tld_fn->base.source_node = instruction->base.base.source_node;17869 tld_fn->base.source_node = instruction->base.base.source_node;
1787017870
...@@ -18093,7 +18093,7 @@ static IrInstGen *ir_analyze_instruction_error_union(IrAnalyze *ira, IrInstSrcEr...@@ -18093,7 +18093,7 @@ static IrInstGen *ir_analyze_instruction_error_union(IrAnalyze *ira, IrInstSrcEr
18093 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);18093 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);
18094 result->value->special = ConstValSpecialLazy;18094 result->value->special = ConstValSpecialLazy;
1809518095
18096 LazyValueErrUnionType *lazy_err_union_type = allocate<LazyValueErrUnionType>(1, "LazyValueErrUnionType");18096 LazyValueErrUnionType *lazy_err_union_type = heap::c_allocator.create<LazyValueErrUnionType>();
18097 lazy_err_union_type->ira = ira; ira_ref(ira);18097 lazy_err_union_type->ira = ira; ira_ref(ira);
18098 result->value->data.x_lazy = &lazy_err_union_type->base;18098 result->value->data.x_lazy = &lazy_err_union_type->base;
18099 lazy_err_union_type->base.id = LazyValueIdErrUnionType;18099 lazy_err_union_type->base.id = LazyValueIdErrUnionType;
...@@ -18114,7 +18114,7 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType...@@ -18114,7 +18114,7 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType
18114{18114{
18115 Error err;18115 Error err;
1811618116
18117 ZigValue *pointee = create_const_vals(1);18117 ZigValue *pointee = ira->codegen->pass1_arena->create<ZigValue>();
18118 pointee->special = ConstValSpecialUndef;18118 pointee->special = ConstValSpecialUndef;
18119 pointee->llvm_align = align;18119 pointee->llvm_align = align;
1812018120
...@@ -18195,8 +18195,8 @@ static bool type_can_bit_cast(ZigType *t) {...@@ -18195,8 +18195,8 @@ static bool type_can_bit_cast(ZigType *t) {
18195 }18195 }
18196}18196}
1819718197
18198static void set_up_result_loc_for_inferred_comptime(IrInstGen *ptr) {18198static void set_up_result_loc_for_inferred_comptime(IrAnalyze *ira, IrInstGen *ptr) {
18199 ZigValue *undef_child = create_const_vals(1);18199 ZigValue *undef_child = ira->codegen->pass1_arena->create<ZigValue>();
18200 undef_child->type = ptr->value->type->data.pointer.child_type;18200 undef_child->type = ptr->value->type->data.pointer.child_type;
18201 undef_child->special = ConstValSpecialUndef;18201 undef_child->special = ConstValSpecialUndef;
18202 ptr->value->special = ConstValSpecialStatic;18202 ptr->value->special = ConstValSpecialStatic;
...@@ -18242,7 +18242,7 @@ static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_sourc...@@ -18242,7 +18242,7 @@ static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_sourc
18242 IrInstGenAlloca *alloca_gen = ir_build_alloca_gen(ira, suspend_source_instr, 0, "");18242 IrInstGenAlloca *alloca_gen = ir_build_alloca_gen(ira, suspend_source_instr, 0, "");
18243 alloca_gen->base.value->type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,18243 alloca_gen->base.value->type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,
18244 PtrLenSingle, 0, 0, 0, false);18244 PtrLenSingle, 0, 0, 0, false);
18245 set_up_result_loc_for_inferred_comptime(&alloca_gen->base);18245 set_up_result_loc_for_inferred_comptime(ira, &alloca_gen->base);
18246 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;18246 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;
18247 if (fn_entry != nullptr && get_scope_typeof(suspend_source_instr->scope) == nullptr) {18247 if (fn_entry != nullptr && get_scope_typeof(suspend_source_instr->scope) == nullptr) {
18248 fn_entry->alloca_gen_list.append(alloca_gen);18248 fn_entry->alloca_gen_list.append(alloca_gen);
...@@ -18306,7 +18306,6 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i...@@ -18306,7 +18306,6 @@ static IrInstGen *ir_resolve_result_raw(IrAnalyze *ira, IrInst *suspend_source_i
18306 ZigVar *new_var = create_local_var(ira->codegen, var->decl_node, var->child_scope,18306 ZigVar *new_var = create_local_var(ira->codegen, var->decl_node, var->child_scope,
18307 buf_create_from_str(var->name), var->src_is_const, var->gen_is_const,18307 buf_create_from_str(var->name), var->src_is_const, var->gen_is_const,
18308 var->shadowable, var->is_comptime, true);18308 var->shadowable, var->is_comptime, true);
18309 new_var->owner_exec = var->owner_exec;
18310 new_var->align_bytes = var->align_bytes;18309 new_var->align_bytes = var->align_bytes;
1831118310
18312 var->next_var = new_var;18311 var->next_var = new_var;
...@@ -18645,15 +18644,15 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18645,15 +18644,15 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18645 if (!val)18644 if (!val)
18646 return ira->codegen->invalid_inst_gen;18645 return ira->codegen->invalid_inst_gen;
18647 field->is_comptime = true;18646 field->is_comptime = true;
18648 field->init_val = create_const_vals(1);18647 field->init_val = ira->codegen->pass1_arena->create<ZigValue>();
18649 copy_const_val(field->init_val, val);18648 copy_const_val(ira->codegen, field->init_val, val);
18650 return result_loc;18649 return result_loc;
18651 }18650 }
1865218651
18653 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);18652 ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
18654 if (instr_is_comptime(result_loc)) {18653 if (instr_is_comptime(result_loc)) {
18655 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);18654 casted_ptr = ir_const(ira, suspend_source_instr, struct_ptr_type);
18656 copy_const_val(casted_ptr->value, result_loc->value);18655 copy_const_val(ira->codegen, casted_ptr->value, result_loc->value);
18657 casted_ptr->value->type = struct_ptr_type;18656 casted_ptr->value->type = struct_ptr_type;
18658 } else {18657 } else {
18659 casted_ptr = result_loc;18658 casted_ptr = result_loc;
...@@ -18666,8 +18665,8 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr...@@ -18666,8 +18665,8 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
18666 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,18665 ZigValue *struct_val = const_ptr_pointee(ira, ira->codegen, ptr_val,
18667 suspend_source_instr->source_node);18666 suspend_source_instr->source_node);
18668 struct_val->special = ConstValSpecialStatic;18667 struct_val->special = ConstValSpecialStatic;
18669 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(struct_val->data.x_struct.fields,18668 struct_val->data.x_struct.fields = realloc_const_vals_ptrs(ira->codegen,
18670 old_field_count, new_field_count);18669 struct_val->data.x_struct.fields, old_field_count, new_field_count);
1867118670
18672 ZigValue *field_val = struct_val->data.x_struct.fields[old_field_count];18671 ZigValue *field_val = struct_val->data.x_struct.fields[old_field_count];
18673 field_val->special = ConstValSpecialUndef;18672 field_val->special = ConstValSpecialUndef;
...@@ -18967,10 +18966,10 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -18967,10 +18966,10 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
18967 if (!arg_val)18966 if (!arg_val)
18968 return false;18967 return false;
18969 } else {18968 } else {
18970 arg_val = create_const_runtime(casted_arg->value->type);18969 arg_val = create_const_runtime(ira->codegen, casted_arg->value->type);
18971 }18970 }
18972 if (arg_part_of_generic_id) {18971 if (arg_part_of_generic_id) {
18973 copy_const_val(&generic_id->params[generic_id->param_count], arg_val);18972 copy_const_val(ira->codegen, &generic_id->params[generic_id->param_count], arg_val);
18974 generic_id->param_count += 1;18973 generic_id->param_count += 1;
18975 }18974 }
1897618975
...@@ -19119,7 +19118,7 @@ static IrInstGen *ir_analyze_store_ptr(IrAnalyze *ira, IrInst* source_instr,...@@ -19119,7 +19118,7 @@ static IrInstGen *ir_analyze_store_ptr(IrAnalyze *ira, IrInst* source_instr,
19119 if (dest_val == nullptr)19118 if (dest_val == nullptr)
19120 return ira->codegen->invalid_inst_gen;19119 return ira->codegen->invalid_inst_gen;
19121 if (dest_val->special != ConstValSpecialRuntime) {19120 if (dest_val->special != ConstValSpecialRuntime) {
19122 copy_const_val(dest_val, value->value);19121 copy_const_val(ira->codegen, dest_val, value->value);
1912319122
19124 if (ptr->value->data.x_ptr.mut == ConstPtrMutComptimeVar &&19123 if (ptr->value->data.x_ptr.mut == ConstPtrMutComptimeVar &&
19125 !ira->new_irb.current_basic_block->must_be_comptime_source_instr)19124 !ira->new_irb.current_basic_block->must_be_comptime_source_instr)
...@@ -19354,8 +19353,6 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19354,8 +19353,6 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19354 {19353 {
19355 return ira->codegen->invalid_inst_gen;19354 return ira->codegen->invalid_inst_gen;
19356 }19355 }
19357 destroy(result_ptr, "ZigValue");
19358 result_ptr = nullptr;
1935919356
19360 if (inferred_err_set_type != nullptr) {19357 if (inferred_err_set_type != nullptr) {
19361 inferred_err_set_type->data.error_set.incomplete = false;19358 inferred_err_set_type->data.error_set.incomplete = false;
...@@ -19363,7 +19360,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19363,7 +19360,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19363 ErrorTableEntry *err = result->data.x_err_union.error_set->data.x_err_set;19360 ErrorTableEntry *err = result->data.x_err_union.error_set->data.x_err_set;
19364 if (err != nullptr) {19361 if (err != nullptr) {
19365 inferred_err_set_type->data.error_set.err_count = 1;19362 inferred_err_set_type->data.error_set.err_count = 1;
19366 inferred_err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(1);19363 inferred_err_set_type->data.error_set.errors = heap::c_allocator.create<ErrorTableEntry *>();
19367 inferred_err_set_type->data.error_set.errors[0] = err;19364 inferred_err_set_type->data.error_set.errors[0] = err;
19368 }19365 }
19369 ZigType *fn_inferred_err_set_type = result->type->data.error_union.err_set_type;19366 ZigType *fn_inferred_err_set_type = result->type->data.error_union.err_set_type;
...@@ -19397,12 +19394,12 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19397,12 +19394,12 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1939719394
19398 size_t new_fn_arg_count = first_arg_1_or_0 + args_len;19395 size_t new_fn_arg_count = first_arg_1_or_0 + args_len;
1939919396
19400 IrInstGen **casted_args = allocate<IrInstGen *>(new_fn_arg_count);19397 IrInstGen **casted_args = heap::c_allocator.allocate<IrInstGen *>(new_fn_arg_count);
1940119398
19402 // Fork a scope of the function with known values for the parameters.19399 // Fork a scope of the function with known values for the parameters.
19403 Scope *parent_scope = fn_entry->fndef_scope->base.parent;19400 Scope *parent_scope = fn_entry->fndef_scope->base.parent;
19404 ZigFn *impl_fn = create_fn(ira->codegen, fn_proto_node);19401 ZigFn *impl_fn = create_fn(ira->codegen, fn_proto_node);
19405 impl_fn->param_source_nodes = allocate<AstNode *>(new_fn_arg_count);19402 impl_fn->param_source_nodes = heap::c_allocator.allocate<AstNode *>(new_fn_arg_count);
19406 buf_init_from_buf(&impl_fn->symbol_name, &fn_entry->symbol_name);19403 buf_init_from_buf(&impl_fn->symbol_name, &fn_entry->symbol_name);
19407 impl_fn->fndef_scope = create_fndef_scope(ira->codegen, impl_fn->body_node, parent_scope, impl_fn);19404 impl_fn->fndef_scope = create_fndef_scope(ira->codegen, impl_fn->body_node, parent_scope, impl_fn);
19408 impl_fn->child_scope = &impl_fn->fndef_scope->base;19405 impl_fn->child_scope = &impl_fn->fndef_scope->base;
...@@ -19413,10 +19410,10 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19413,10 +19410,10 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1941319410
19414 // TODO maybe GenericFnTypeId can be replaced with using the child_scope directly19411 // TODO maybe GenericFnTypeId can be replaced with using the child_scope directly
19415 // as the key in generic_table19412 // as the key in generic_table
19416 GenericFnTypeId *generic_id = allocate<GenericFnTypeId>(1);19413 GenericFnTypeId *generic_id = heap::c_allocator.create<GenericFnTypeId>();
19417 generic_id->fn_entry = fn_entry;19414 generic_id->fn_entry = fn_entry;
19418 generic_id->param_count = 0;19415 generic_id->param_count = 0;
19419 generic_id->params = create_const_vals(new_fn_arg_count);19416 generic_id->params = ira->codegen->pass1_arena->allocate<ZigValue>(new_fn_arg_count);
19420 size_t next_proto_i = 0;19417 size_t next_proto_i = 0;
1942119418
19422 if (first_arg_ptr) {19419 if (first_arg_ptr) {
...@@ -19476,7 +19473,6 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19476,7 +19473,6 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19476 IrInstGenConst *const_instruction = ir_create_inst_noval<IrInstGenConst>(&ira->new_irb,19473 IrInstGenConst *const_instruction = ir_create_inst_noval<IrInstGenConst>(&ira->new_irb,
19477 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);19474 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
19478 const_instruction->base.value = align_result;19475 const_instruction->base.value = align_result;
19479 destroy(result_ptr, "ZigValue");
1948019476
19481 uint32_t align_bytes = 0;19477 uint32_t align_bytes = 0;
19482 ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes);19478 ir_resolve_align(ira, &const_instruction->base, nullptr, &align_bytes);
...@@ -19609,7 +19605,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -19609,7 +19605,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
19609 }19605 }
1961019606
1961119607
19612 IrInstGen **casted_args = allocate<IrInstGen *>(call_param_count);19608 IrInstGen **casted_args = heap::c_allocator.allocate<IrInstGen *>(call_param_count);
19613 size_t next_arg_index = 0;19609 size_t next_arg_index = 0;
19614 if (first_arg_ptr) {19610 if (first_arg_ptr) {
19615 assert(first_arg_ptr->value->type->id == ZigTypeIdPointer);19611 assert(first_arg_ptr->value->type->id == ZigTypeIdPointer);
...@@ -19741,7 +19737,7 @@ static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_ins...@@ -19741,7 +19737,7 @@ static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_ins
19741 return ira->codegen->invalid_inst_gen;19737 return ira->codegen->invalid_inst_gen;
19742 new_stack_src = &call_instruction->new_stack->base;19738 new_stack_src = &call_instruction->new_stack->base;
19743 }19739 }
19744 IrInstGen **args_ptr = allocate<IrInstGen *>(call_instruction->arg_count, "IrInstGen *");19740 IrInstGen **args_ptr = heap::c_allocator.allocate<IrInstGen *>(call_instruction->arg_count);
19745 for (size_t i = 0; i < call_instruction->arg_count; i += 1) {19741 for (size_t i = 0; i < call_instruction->arg_count; i += 1) {
19746 args_ptr[i] = call_instruction->args[i]->child;19742 args_ptr[i] = call_instruction->args[i]->child;
19747 if (type_is_invalid(args_ptr[i]->value->type))19743 if (type_is_invalid(args_ptr[i]->value->type))
...@@ -19757,7 +19753,7 @@ static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_ins...@@ -19757,7 +19753,7 @@ static IrInstGen *ir_analyze_fn_call_src(IrAnalyze *ira, IrInstSrcCall *call_ins
19757 first_arg_ptr, first_arg_ptr_src, modifier, new_stack, new_stack_src,19753 first_arg_ptr, first_arg_ptr_src, modifier, new_stack, new_stack_src,
19758 call_instruction->is_async_call_builtin, args_ptr, call_instruction->arg_count, ret_ptr,19754 call_instruction->is_async_call_builtin, args_ptr, call_instruction->arg_count, ret_ptr,
19759 call_instruction->result_loc);19755 call_instruction->result_loc);
19760 deallocate(args_ptr, call_instruction->arg_count, "IrInstGen *");19756 heap::c_allocator.deallocate(args_ptr, call_instruction->arg_count);
19761 return result;19757 return result;
19762}19758}
1976319759
...@@ -19877,7 +19873,7 @@ static IrInstGen *ir_analyze_instruction_call_extra(IrAnalyze *ira, IrInstSrcCal...@@ -19877,7 +19873,7 @@ static IrInstGen *ir_analyze_instruction_call_extra(IrAnalyze *ira, IrInstSrcCal
1987719873
19878 if (is_tuple(args_type)) {19874 if (is_tuple(args_type)) {
19879 args_len = args_type->data.structure.src_field_count;19875 args_len = args_type->data.structure.src_field_count;
19880 args_ptr = allocate<IrInstGen *>(args_len, "IrInstGen *");19876 args_ptr = heap::c_allocator.allocate<IrInstGen *>(args_len);
19881 for (size_t i = 0; i < args_len; i += 1) {19877 for (size_t i = 0; i < args_len; i += 1) {
19882 TypeStructField *arg_field = args_type->data.structure.fields[i];19878 TypeStructField *arg_field = args_type->data.structure.fields[i];
19883 args_ptr[i] = ir_analyze_struct_value_field_value(ira, &instruction->base.base, args, arg_field);19879 args_ptr[i] = ir_analyze_struct_value_field_value(ira, &instruction->base.base, args, arg_field);
...@@ -19890,12 +19886,12 @@ static IrInstGen *ir_analyze_instruction_call_extra(IrAnalyze *ira, IrInstSrcCal...@@ -19890,12 +19886,12 @@ static IrInstGen *ir_analyze_instruction_call_extra(IrAnalyze *ira, IrInstSrcCal
19890 }19886 }
19891 IrInstGen *result = ir_analyze_call_extra(ira, &instruction->base.base, instruction->options,19887 IrInstGen *result = ir_analyze_call_extra(ira, &instruction->base.base, instruction->options,
19892 instruction->fn_ref, args_ptr, args_len, instruction->result_loc);19888 instruction->fn_ref, args_ptr, args_len, instruction->result_loc);
19893 deallocate(args_ptr, args_len, "IrInstGen *");19889 heap::c_allocator.deallocate(args_ptr, args_len);
19894 return result;19890 return result;
19895}19891}
1989619892
19897static IrInstGen *ir_analyze_instruction_call_args(IrAnalyze *ira, IrInstSrcCallArgs *instruction) {19893static IrInstGen *ir_analyze_instruction_call_args(IrAnalyze *ira, IrInstSrcCallArgs *instruction) {
19898 IrInstGen **args_ptr = allocate<IrInstGen *>(instruction->args_len, "IrInstGen *");19894 IrInstGen **args_ptr = heap::c_allocator.allocate<IrInstGen *>(instruction->args_len);
19899 for (size_t i = 0; i < instruction->args_len; i += 1) {19895 for (size_t i = 0; i < instruction->args_len; i += 1) {
19900 args_ptr[i] = instruction->args_ptr[i]->child;19896 args_ptr[i] = instruction->args_ptr[i]->child;
19901 if (type_is_invalid(args_ptr[i]->value->type))19897 if (type_is_invalid(args_ptr[i]->value->type))
...@@ -19904,7 +19900,7 @@ static IrInstGen *ir_analyze_instruction_call_args(IrAnalyze *ira, IrInstSrcCall...@@ -19904,7 +19900,7 @@ static IrInstGen *ir_analyze_instruction_call_args(IrAnalyze *ira, IrInstSrcCall
1990419900
19905 IrInstGen *result = ir_analyze_call_extra(ira, &instruction->base.base, instruction->options,19901 IrInstGen *result = ir_analyze_call_extra(ira, &instruction->base.base, instruction->options,
19906 instruction->fn_ref, args_ptr, instruction->args_len, instruction->result_loc);19902 instruction->fn_ref, args_ptr, instruction->args_len, instruction->result_loc);
19907 deallocate(args_ptr, instruction->args_len, "IrInstGen *");19903 heap::c_allocator.deallocate(args_ptr, instruction->args_len);
19908 return result;19904 return result;
19909}19905}
1991019906
...@@ -19979,7 +19975,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source...@@ -19979,7 +19975,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1997919975
19980 if (dst_size <= src_size) {19976 if (dst_size <= src_size) {
19981 if (src_size == dst_size && types_have_same_zig_comptime_repr(codegen, out_val->type, pointee->type)) {19977 if (src_size == dst_size && types_have_same_zig_comptime_repr(codegen, out_val->type, pointee->type)) {
19982 copy_const_val(out_val, pointee);19978 copy_const_val(codegen, out_val, pointee);
19983 return ErrorNone;19979 return ErrorNone;
19984 }19980 }
19985 Buf buf = BUF_INIT;19981 Buf buf = BUF_INIT;
...@@ -20047,7 +20043,7 @@ static IrInstGen *ir_analyze_optional_type(IrAnalyze *ira, IrInstSrcUnOp *instru...@@ -20047,7 +20043,7 @@ static IrInstGen *ir_analyze_optional_type(IrAnalyze *ira, IrInstSrcUnOp *instru
20047 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);20043 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);
20048 result->value->special = ConstValSpecialLazy;20044 result->value->special = ConstValSpecialLazy;
2004920045
20050 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1, "LazyValueOptType");20046 LazyValueOptType *lazy_opt_type = heap::c_allocator.create<LazyValueOptType>();
20051 lazy_opt_type->ira = ira; ira_ref(ira);20047 lazy_opt_type->ira = ira; ira_ref(ira);
20052 result->value->data.x_lazy = &lazy_opt_type->base;20048 result->value->data.x_lazy = &lazy_opt_type->base;
20053 lazy_opt_type->base.id = LazyValueIdOptType;20049 lazy_opt_type->base.id = LazyValueIdOptType;
...@@ -20331,7 +20327,7 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i...@@ -20331,7 +20327,7 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i
2033120327
20332 if (value->value->special != ConstValSpecialRuntime) {20328 if (value->value->special != ConstValSpecialRuntime) {
20333 IrInstGen *result = ir_const(ira, &phi_instruction->base.base, nullptr);20329 IrInstGen *result = ir_const(ira, &phi_instruction->base.base, nullptr);
20334 copy_const_val(result->value, value->value);20330 copy_const_val(ira->codegen, result->value, value->value);
20335 return result;20331 return result;
20336 } else {20332 } else {
20337 return value;20333 return value;
...@@ -20345,7 +20341,7 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i...@@ -20345,7 +20341,7 @@ static IrInstGen *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstSrcPhi *phi_i
20345 peer_parent->peers.length >= 2)20341 peer_parent->peers.length >= 2)
20346 {20342 {
20347 if (peer_parent->resolved_type == nullptr) {20343 if (peer_parent->resolved_type == nullptr) {
20348 IrInstGen **instructions = allocate<IrInstGen *>(peer_parent->peers.length);20344 IrInstGen **instructions = heap::c_allocator.allocate<IrInstGen *>(peer_parent->peers.length);
20349 for (size_t i = 0; i < peer_parent->peers.length; i += 1) {20345 for (size_t i = 0; i < peer_parent->peers.length; i += 1) {
20350 ResultLocPeer *this_peer = peer_parent->peers.at(i);20346 ResultLocPeer *this_peer = peer_parent->peers.at(i);
2035120347
...@@ -20718,7 +20714,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20718,7 +20714,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20718 if (index == array_len && array_type->data.array.sentinel != nullptr) {20714 if (index == array_len && array_type->data.array.sentinel != nullptr) {
20719 ZigType *elem_type = array_type->data.array.child_type;20715 ZigType *elem_type = array_type->data.array.child_type;
20720 IrInstGen *sentinel_elem = ir_const(ira, &elem_ptr_instruction->base.base, elem_type);20716 IrInstGen *sentinel_elem = ir_const(ira, &elem_ptr_instruction->base.base, elem_type);
20721 copy_const_val(sentinel_elem->value, array_type->data.array.sentinel);20717 copy_const_val(ira->codegen, sentinel_elem->value, array_type->data.array.sentinel);
20722 return ir_get_ref(ira, &elem_ptr_instruction->base.base, sentinel_elem, true, false);20718 return ir_get_ref(ira, &elem_ptr_instruction->base.base, sentinel_elem, true, false);
20723 }20719 }
20724 if (index >= array_len) {20720 if (index >= array_len) {
...@@ -20782,7 +20778,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20782,7 +20778,7 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20782 {20778 {
20783 if (array_type->id == ZigTypeIdArray || array_type->id == ZigTypeIdVector) {20779 if (array_type->id == ZigTypeIdArray || array_type->id == ZigTypeIdVector) {
20784 array_ptr_val->data.x_array.special = ConstArraySpecialNone;20780 array_ptr_val->data.x_array.special = ConstArraySpecialNone;
20785 array_ptr_val->data.x_array.data.s_none.elements = create_const_vals(array_type->data.array.len);20781 array_ptr_val->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(array_type->data.array.len);
20786 array_ptr_val->special = ConstValSpecialStatic;20782 array_ptr_val->special = ConstValSpecialStatic;
20787 for (size_t i = 0; i < array_type->data.array.len; i += 1) {20783 for (size_t i = 0; i < array_type->data.array.len; i += 1) {
20788 ZigValue *elem_val = &array_ptr_val->data.x_array.data.s_none.elements[i];20784 ZigValue *elem_val = &array_ptr_val->data.x_array.data.s_none.elements[i];
...@@ -20805,11 +20801,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20805,11 +20801,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20805 return ira->codegen->invalid_inst_gen;20801 return ira->codegen->invalid_inst_gen;
20806 }20802 }
2080720803
20808 ZigValue *array_init_val = create_const_vals(1);20804 ZigValue *array_init_val = ira->codegen->pass1_arena->create<ZigValue>();
20809 array_init_val->special = ConstValSpecialStatic;20805 array_init_val->special = ConstValSpecialStatic;
20810 array_init_val->type = actual_array_type;20806 array_init_val->type = actual_array_type;
20811 array_init_val->data.x_array.special = ConstArraySpecialNone;20807 array_init_val->data.x_array.special = ConstArraySpecialNone;
20812 array_init_val->data.x_array.data.s_none.elements = create_const_vals(actual_array_type->data.array.len);20808 array_init_val->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(actual_array_type->data.array.len);
20813 array_init_val->special = ConstValSpecialStatic;20809 array_init_val->special = ConstValSpecialStatic;
20814 for (size_t i = 0; i < actual_array_type->data.array.len; i += 1) {20810 for (size_t i = 0; i < actual_array_type->data.array.len; i += 1) {
20815 ZigValue *elem_val = &array_init_val->data.x_array.data.s_none.elements[i];20811 ZigValue *elem_val = &array_init_val->data.x_array.data.s_none.elements[i];
...@@ -21135,7 +21131,7 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins...@@ -21135,7 +21131,7 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins
21135 if (field->is_comptime) {21131 if (field->is_comptime) {
21136 IrInstGen *elem = ir_const(ira, source_instr, field_type);21132 IrInstGen *elem = ir_const(ira, source_instr, field_type);
21137 memoize_field_init_val(ira->codegen, struct_type, field);21133 memoize_field_init_val(ira->codegen, struct_type, field);
21138 copy_const_val(elem->value, field->init_val);21134 copy_const_val(ira->codegen, elem->value, field->init_val);
21139 return ir_get_ref2(ira, source_instr, elem, field_type, true, false);21135 return ir_get_ref2(ira, source_instr, elem, field_type, true, false);
21140 }21136 }
21141 switch (type_has_one_possible_value(ira->codegen, field_type)) {21137 switch (type_has_one_possible_value(ira->codegen, field_type)) {
...@@ -21183,7 +21179,7 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins...@@ -21183,7 +21179,7 @@ static IrInstGen *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInst* source_ins
21183 if (type_is_invalid(struct_val->type))21179 if (type_is_invalid(struct_val->type))
21184 return ira->codegen->invalid_inst_gen;21180 return ira->codegen->invalid_inst_gen;
21185 if (initializing && struct_val->special == ConstValSpecialUndef) {21181 if (initializing && struct_val->special == ConstValSpecialUndef) {
21186 struct_val->data.x_struct.fields = alloc_const_vals_ptrs(struct_type->data.structure.src_field_count);21182 struct_val->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, struct_type->data.structure.src_field_count);
21187 struct_val->special = ConstValSpecialStatic;21183 struct_val->special = ConstValSpecialStatic;
21188 for (size_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) {21184 for (size_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) {
21189 ZigValue *field_val = struct_val->data.x_struct.fields[i];21185 ZigValue *field_val = struct_val->data.x_struct.fields[i];
...@@ -21225,7 +21221,7 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,...@@ -21225,7 +21221,7 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,
21225 ZigType *container_ptr_type = container_ptr->value->type;21221 ZigType *container_ptr_type = container_ptr->value->type;
21226 ir_assert(container_ptr_type->id == ZigTypeIdPointer, source_instr);21222 ir_assert(container_ptr_type->id == ZigTypeIdPointer, source_instr);
2122721223
21228 InferredStructField *inferred_struct_field = allocate<InferredStructField>(1, "InferredStructField");21224 InferredStructField *inferred_struct_field = heap::c_allocator.create<InferredStructField>();
21229 inferred_struct_field->inferred_struct_type = container_type;21225 inferred_struct_field->inferred_struct_type = container_type;
21230 inferred_struct_field->field_name = field_name;21226 inferred_struct_field->field_name = field_name;
2123121227
...@@ -21245,7 +21241,7 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,...@@ -21245,7 +21241,7 @@ static IrInstGen *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,
21245 } else {21241 } else {
21246 result = ir_const(ira, source_instr, field_ptr_type);21242 result = ir_const(ira, source_instr, field_ptr_type);
21247 }21243 }
21248 copy_const_val(result->value, ptr_val);21244 copy_const_val(ira->codegen, result->value, ptr_val);
21249 result->value->type = field_ptr_type;21245 result->value->type = field_ptr_type;
21250 return result;21246 return result;
21251 }21247 }
...@@ -21316,7 +21312,7 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name...@@ -21316,7 +21312,7 @@ static IrInstGen *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name
21316 return ira->codegen->invalid_inst_gen;21312 return ira->codegen->invalid_inst_gen;
2131721313
21318 if (initializing) {21314 if (initializing) {
21319 ZigValue *payload_val = create_const_vals(1);21315 ZigValue *payload_val = ira->codegen->pass1_arena->create<ZigValue>();
21320 payload_val->special = ConstValSpecialUndef;21316 payload_val->special = ConstValSpecialUndef;
21321 payload_val->type = field_type;21317 payload_val->type = field_type;
21322 payload_val->parent.id = ConstParentIdUnion;21318 payload_val->parent.id = ConstParentIdUnion;
...@@ -21499,7 +21495,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21499,7 +21495,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21499 }21495 }
21500 } else if (is_array_ref(container_type) && !field_ptr_instruction->initializing) {21496 } else if (is_array_ref(container_type) && !field_ptr_instruction->initializing) {
21501 if (buf_eql_str(field_name, "len")) {21497 if (buf_eql_str(field_name, "len")) {
21502 ZigValue *len_val = create_const_vals(1);21498 ZigValue *len_val = ira->codegen->pass1_arena->create<ZigValue>();
21503 if (container_type->id == ZigTypeIdPointer) {21499 if (container_type->id == ZigTypeIdPointer) {
21504 init_const_usize(ira->codegen, len_val, container_type->data.pointer.child_type->data.array.len);21500 init_const_usize(ira->codegen, len_val, container_type->data.pointer.child_type->data.array.len);
21505 } else {21501 } else {
...@@ -21545,7 +21541,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21545,7 +21541,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21545 bool ptr_is_const = true;21541 bool ptr_is_const = true;
21546 bool ptr_is_volatile = false;21542 bool ptr_is_volatile = false;
21547 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,21543 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
21548 create_const_enum(child_type, &field->value), child_type,21544 create_const_enum(ira->codegen, child_type, &field->value), child_type,
21549 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);21545 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
21550 }21546 }
21551 }21547 }
...@@ -21574,7 +21570,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21574,7 +21570,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21574 bool ptr_is_const = true;21570 bool ptr_is_const = true;
21575 bool ptr_is_volatile = false;21571 bool ptr_is_volatile = false;
21576 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,21572 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
21577 create_const_enum(enum_type, &field->enum_field->value), enum_type,21573 create_const_enum(ira->codegen, enum_type, &field->enum_field->value), enum_type,
21578 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);21574 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
21579 }21575 }
21580 }21576 }
...@@ -21592,7 +21588,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21592,7 +21588,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21592 if (existing_entry) {21588 if (existing_entry) {
21593 err_entry = existing_entry->value;21589 err_entry = existing_entry->value;
21594 } else {21590 } else {
21595 err_entry = allocate<ErrorTableEntry>(1);21591 err_entry = heap::c_allocator.create<ErrorTableEntry>();
21596 err_entry->decl_node = field_ptr_instruction->base.base.source_node;21592 err_entry->decl_node = field_ptr_instruction->base.base.source_node;
21597 buf_init_from_buf(&err_entry->name, field_name);21593 buf_init_from_buf(&err_entry->name, field_name);
21598 size_t error_value_count = ira->codegen->errors_by_index.length;21594 size_t error_value_count = ira->codegen->errors_by_index.length;
...@@ -21619,7 +21615,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21619,7 +21615,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21619 }21615 }
21620 err_set_type = child_type;21616 err_set_type = child_type;
21621 }21617 }
21622 ZigValue *const_val = create_const_vals(1);21618 ZigValue *const_val = ira->codegen->pass1_arena->create<ZigValue>();
21623 const_val->special = ConstValSpecialStatic;21619 const_val->special = ConstValSpecialStatic;
21624 const_val->type = err_set_type;21620 const_val->type = err_set_type;
21625 const_val->data.x_err_set = err_entry;21621 const_val->data.x_err_set = err_entry;
...@@ -21633,7 +21629,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21633,7 +21629,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21633 bool ptr_is_const = true;21629 bool ptr_is_const = true;
21634 bool ptr_is_volatile = false;21630 bool ptr_is_volatile = false;
21635 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,21631 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
21636 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,21632 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
21637 child_type->data.integral.bit_count, false),21633 child_type->data.integral.bit_count, false),
21638 ira->codegen->builtin_types.entry_num_lit_int,21634 ira->codegen->builtin_types.entry_num_lit_int,
21639 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);21635 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
...@@ -21655,7 +21651,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21655,7 +21651,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21655 bool ptr_is_const = true;21651 bool ptr_is_const = true;
21656 bool ptr_is_volatile = false;21652 bool ptr_is_volatile = false;
21657 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,21653 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
21658 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,21654 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
21659 child_type->data.floating.bit_count, false),21655 child_type->data.floating.bit_count, false),
21660 ira->codegen->builtin_types.entry_num_lit_int,21656 ira->codegen->builtin_types.entry_num_lit_int,
21661 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);21657 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
...@@ -21682,7 +21678,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21682,7 +21678,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21682 return ira->codegen->invalid_inst_gen;21678 return ira->codegen->invalid_inst_gen;
21683 }21679 }
21684 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,21680 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
21685 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,21681 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
21686 get_ptr_align(ira->codegen, child_type), false),21682 get_ptr_align(ira->codegen, child_type), false),
21687 ira->codegen->builtin_types.entry_num_lit_int,21683 ira->codegen->builtin_types.entry_num_lit_int,
21688 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);21684 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
...@@ -21704,7 +21700,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel...@@ -21704,7 +21700,7 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
21704 bool ptr_is_const = true;21700 bool ptr_is_const = true;
21705 bool ptr_is_volatile = false;21701 bool ptr_is_volatile = false;
21706 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,21702 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
21707 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,21703 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
21708 child_type->data.array.len, false),21704 child_type->data.array.len, false),
21709 ira->codegen->builtin_types.entry_num_lit_int,21705 ira->codegen->builtin_types.entry_num_lit_int,
21710 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);21706 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
...@@ -21984,7 +21980,7 @@ static IrInstGen *ir_analyze_instruction_slice_type(IrAnalyze *ira, IrInstSrcSli...@@ -21984,7 +21980,7 @@ static IrInstGen *ir_analyze_instruction_slice_type(IrAnalyze *ira, IrInstSrcSli
21984 IrInstGen *result = ir_const(ira, &slice_type_instruction->base.base, ira->codegen->builtin_types.entry_type);21980 IrInstGen *result = ir_const(ira, &slice_type_instruction->base.base, ira->codegen->builtin_types.entry_type);
21985 result->value->special = ConstValSpecialLazy;21981 result->value->special = ConstValSpecialLazy;
2198621982
21987 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1, "LazyValueSliceType");21983 LazyValueSliceType *lazy_slice_type = heap::c_allocator.create<LazyValueSliceType>();
21988 lazy_slice_type->ira = ira; ira_ref(ira);21984 lazy_slice_type->ira = ira; ira_ref(ira);
21989 result->value->data.x_lazy = &lazy_slice_type->base;21985 result->value->data.x_lazy = &lazy_slice_type->base;
21990 lazy_slice_type->base.id = LazyValueIdSliceType;21986 lazy_slice_type->base.id = LazyValueIdSliceType;
...@@ -22057,8 +22053,8 @@ static IrInstGen *ir_analyze_instruction_asm(IrAnalyze *ira, IrInstSrcAsm *asm_i...@@ -22057,8 +22053,8 @@ static IrInstGen *ir_analyze_instruction_asm(IrAnalyze *ira, IrInstSrcAsm *asm_i
2205722053
22058 // TODO validate the output types and variable types22054 // TODO validate the output types and variable types
2205922055
22060 IrInstGen **input_list = allocate<IrInstGen *>(asm_expr->input_list.length);22056 IrInstGen **input_list = heap::c_allocator.allocate<IrInstGen *>(asm_expr->input_list.length);
22061 IrInstGen **output_types = allocate<IrInstGen *>(asm_expr->output_list.length);22057 IrInstGen **output_types = heap::c_allocator.allocate<IrInstGen *>(asm_expr->output_list.length);
2206222058
22063 ZigType *return_type = ira->codegen->builtin_types.entry_void;22059 ZigType *return_type = ira->codegen->builtin_types.entry_void;
22064 for (size_t i = 0; i < asm_expr->output_list.length; i += 1) {22060 for (size_t i = 0; i < asm_expr->output_list.length; i += 1) {
...@@ -22097,7 +22093,7 @@ static IrInstGen *ir_analyze_instruction_array_type(IrAnalyze *ira, IrInstSrcArr...@@ -22097,7 +22093,7 @@ static IrInstGen *ir_analyze_instruction_array_type(IrAnalyze *ira, IrInstSrcArr
22097 IrInstGen *result = ir_const(ira, &array_type_instruction->base.base, ira->codegen->builtin_types.entry_type);22093 IrInstGen *result = ir_const(ira, &array_type_instruction->base.base, ira->codegen->builtin_types.entry_type);
22098 result->value->special = ConstValSpecialLazy;22094 result->value->special = ConstValSpecialLazy;
2209922095
22100 LazyValueArrayType *lazy_array_type = allocate<LazyValueArrayType>(1, "LazyValueArrayType");22096 LazyValueArrayType *lazy_array_type = heap::c_allocator.create<LazyValueArrayType>();
22101 lazy_array_type->ira = ira; ira_ref(ira);22097 lazy_array_type->ira = ira; ira_ref(ira);
22102 result->value->data.x_lazy = &lazy_array_type->base;22098 result->value->data.x_lazy = &lazy_array_type->base;
22103 lazy_array_type->base.id = LazyValueIdArrayType;22099 lazy_array_type->base.id = LazyValueIdArrayType;
...@@ -22122,7 +22118,7 @@ static IrInstGen *ir_analyze_instruction_size_of(IrAnalyze *ira, IrInstSrcSizeOf...@@ -22122,7 +22118,7 @@ static IrInstGen *ir_analyze_instruction_size_of(IrAnalyze *ira, IrInstSrcSizeOf
22122 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_num_lit_int);22118 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_num_lit_int);
22123 result->value->special = ConstValSpecialLazy;22119 result->value->special = ConstValSpecialLazy;
2212422120
22125 LazyValueSizeOf *lazy_size_of = allocate<LazyValueSizeOf>(1, "LazyValueSizeOf");22121 LazyValueSizeOf *lazy_size_of = heap::c_allocator.create<LazyValueSizeOf>();
22126 lazy_size_of->ira = ira; ira_ref(ira);22122 lazy_size_of->ira = ira; ira_ref(ira);
22127 result->value->data.x_lazy = &lazy_size_of->base;22123 result->value->data.x_lazy = &lazy_size_of->base;
22128 lazy_size_of->base.id = LazyValueIdSizeOf;22124 lazy_size_of->base.id = LazyValueIdSizeOf;
...@@ -22242,7 +22238,7 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou...@@ -22242,7 +22238,7 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou
22242 return ira->codegen->invalid_inst_gen;22238 return ira->codegen->invalid_inst_gen;
22243 case OnePossibleValueNo:22239 case OnePossibleValueNo:
22244 if (!same_comptime_repr) {22240 if (!same_comptime_repr) {
22245 ZigValue *payload_val = create_const_vals(1);22241 ZigValue *payload_val = ira->codegen->pass1_arena->create<ZigValue>();
22246 payload_val->type = child_type;22242 payload_val->type = child_type;
22247 payload_val->special = ConstValSpecialUndef;22243 payload_val->special = ConstValSpecialUndef;
22248 payload_val->parent.id = ConstParentIdOptionalPayload;22244 payload_val->parent.id = ConstParentIdOptionalPayload;
...@@ -22489,7 +22485,7 @@ static IrInstGen *ir_analyze_instruction_switch_br(IrAnalyze *ira,...@@ -22489,7 +22485,7 @@ static IrInstGen *ir_analyze_instruction_switch_br(IrAnalyze *ira,
22489 }22485 }
22490 }22486 }
2249122487
22492 IrInstGenSwitchBrCase *cases = allocate<IrInstGenSwitchBrCase>(case_count);22488 IrInstGenSwitchBrCase *cases = heap::c_allocator.allocate<IrInstGenSwitchBrCase>(case_count);
22493 for (size_t i = 0; i < case_count; i += 1) {22489 for (size_t i = 0; i < case_count; i += 1) {
22494 IrInstSrcSwitchBrCase *old_case = &switch_br_instruction->cases[i];22490 IrInstSrcSwitchBrCase *old_case = &switch_br_instruction->cases[i];
22495 IrInstGenSwitchBrCase *new_case = &cases[i];22491 IrInstGenSwitchBrCase *new_case = &cases[i];
...@@ -22574,7 +22570,7 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -22574,7 +22570,7 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,
22574 case ZigTypeIdErrorSet: {22570 case ZigTypeIdErrorSet: {
22575 if (pointee_val) {22571 if (pointee_val) {
22576 IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, nullptr);22572 IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, nullptr);
22577 copy_const_val(result->value, pointee_val);22573 copy_const_val(ira->codegen, result->value, pointee_val);
22578 result->value->type = target_type;22574 result->value->type = target_type;
22579 return result;22575 return result;
22580 }22576 }
...@@ -22794,7 +22790,7 @@ static IrInstGen *ir_analyze_instruction_switch_else_var(IrAnalyze *ira,...@@ -22794,7 +22790,7 @@ static IrInstGen *ir_analyze_instruction_switch_else_var(IrAnalyze *ira,
22794 return target_value_ptr;22790 return target_value_ptr;
22795 }22791 }
22796 // Make note of the errors handled by other cases22792 // Make note of the errors handled by other cases
22797 ErrorTableEntry **errors = allocate<ErrorTableEntry *>(ira->codegen->errors_by_index.length);22793 ErrorTableEntry **errors = heap::c_allocator.allocate<ErrorTableEntry *>(ira->codegen->errors_by_index.length);
22798 // We may not have any case in the switch if this is a lone else22794 // We may not have any case in the switch if this is a lone else
22799 const size_t switch_cases = instruction->switch_br ? instruction->switch_br->case_count : 0;22795 const size_t switch_cases = instruction->switch_br ? instruction->switch_br->case_count : 0;
22800 for (size_t case_i = 0; case_i < switch_cases; case_i += 1) {22796 for (size_t case_i = 0; case_i < switch_cases; case_i += 1) {
...@@ -22830,7 +22826,7 @@ static IrInstGen *ir_analyze_instruction_switch_else_var(IrAnalyze *ira,...@@ -22830,7 +22826,7 @@ static IrInstGen *ir_analyze_instruction_switch_else_var(IrAnalyze *ira,
22830 buf_appendf(&err_set_type->name, "%s,", buf_ptr(&error_entry->name));22826 buf_appendf(&err_set_type->name, "%s,", buf_ptr(&error_entry->name));
22831 }22827 }
22832 }22828 }
22833 free(errors);22829 heap::c_allocator.deallocate(errors, ira->codegen->errors_by_index.length);
2283422830
22835 err_set_type->data.error_set.err_count = result_list.length;22831 err_set_type->data.error_set.err_count = result_list.length;
22836 err_set_type->data.error_set.errors = result_list.items;22832 err_set_type->data.error_set.errors = result_list.items;
...@@ -22978,7 +22974,7 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc...@@ -22978,7 +22974,7 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
2297822974
22979 IrInstGen *first_non_const_instruction = nullptr;22975 IrInstGen *first_non_const_instruction = nullptr;
2298022976
22981 AstNode **field_assign_nodes = allocate<AstNode *>(actual_field_count);22977 AstNode **field_assign_nodes = heap::c_allocator.allocate<AstNode *>(actual_field_count);
22982 ZigList<IrInstGen *> const_ptrs = {};22978 ZigList<IrInstGen *> const_ptrs = {};
2298322979
22984 bool is_comptime = ir_should_inline(ira->old_irb.exec, source_instr->scope)22980 bool is_comptime = ir_should_inline(ira->old_irb.exec, source_instr->scope)
...@@ -23049,7 +23045,7 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc...@@ -23049,7 +23045,7 @@ static IrInstGen *ir_analyze_container_init_fields(IrAnalyze *ira, IrInst *sourc
23049 return ira->codegen->invalid_inst_gen;23045 return ira->codegen->invalid_inst_gen;
2305023046
23051 IrInstGen *runtime_inst = ir_const(ira, source_instr, field->init_val->type);23047 IrInstGen *runtime_inst = ir_const(ira, source_instr, field->init_val->type);
23052 copy_const_val(runtime_inst->value, field->init_val);23048 copy_const_val(ira->codegen, runtime_inst->value, field->init_val);
2305323049
23054 IrInstGen *field_ptr = ir_analyze_struct_field_ptr(ira, source_instr, field, result_loc,23050 IrInstGen *field_ptr = ir_analyze_struct_field_ptr(ira, source_instr, field, result_loc,
23055 container_type, true);23051 container_type, true);
...@@ -23313,7 +23309,7 @@ static IrInstGen *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstSrcErrNa...@@ -23313,7 +23309,7 @@ static IrInstGen *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstSrcErrNa
23313 err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true);23309 err->cached_error_name_val = create_const_slice(ira->codegen, array_val, 0, buf_len(&err->name), true);
23314 }23310 }
23315 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);23311 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
23316 copy_const_val(result->value, err->cached_error_name_val);23312 copy_const_val(ira->codegen, result->value, err->cached_error_name_val);
23317 result->value->type = str_type;23313 result->value->type = str_type;
23318 return result;23314 return result;
23319 }23315 }
...@@ -23639,11 +23635,11 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23639,11 +23635,11 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23639 }23635 }
23640 }23636 }
2364123637
23642 ZigValue *declaration_array = create_const_vals(1);23638 ZigValue *declaration_array = ira->codegen->pass1_arena->create<ZigValue>();
23643 declaration_array->special = ConstValSpecialStatic;23639 declaration_array->special = ConstValSpecialStatic;
23644 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, nullptr);23640 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, nullptr);
23645 declaration_array->data.x_array.special = ConstArraySpecialNone;23641 declaration_array->data.x_array.special = ConstArraySpecialNone;
23646 declaration_array->data.x_array.data.s_none.elements = create_const_vals(declaration_count);23642 declaration_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(declaration_count);
23647 init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false);23643 init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false);
2364823644
23649 // Loop through the declarations and generate info.23645 // Loop through the declarations and generate info.
...@@ -23665,7 +23661,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23665,7 +23661,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23665 declaration_val->special = ConstValSpecialStatic;23661 declaration_val->special = ConstValSpecialStatic;
23666 declaration_val->type = type_info_declaration_type;23662 declaration_val->type = type_info_declaration_type;
2366723663
23668 ZigValue **inner_fields = alloc_const_vals_ptrs(3);23664 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3);
23669 ZigValue *name = create_const_str_lit(ira->codegen, curr_entry->key)->data.x_ptr.data.ref.pointee;23665 ZigValue *name = create_const_str_lit(ira->codegen, curr_entry->key)->data.x_ptr.data.ref.pointee;
23670 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true);23666 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(curr_entry->key), true);
23671 inner_fields[1]->special = ConstValSpecialStatic;23667 inner_fields[1]->special = ConstValSpecialStatic;
...@@ -23696,7 +23692,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23696,7 +23692,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23696 // 1: Data.Var: type23692 // 1: Data.Var: type
23697 bigint_init_unsigned(&inner_fields[2]->data.x_union.tag, 1);23693 bigint_init_unsigned(&inner_fields[2]->data.x_union.tag, 1);
2369823694
23699 ZigValue *payload = create_const_vals(1);23695 ZigValue *payload = ira->codegen->pass1_arena->create<ZigValue>();
23700 payload->special = ConstValSpecialStatic;23696 payload->special = ConstValSpecialStatic;
23701 payload->type = ira->codegen->builtin_types.entry_type;23697 payload->type = ira->codegen->builtin_types.entry_type;
23702 payload->data.x_type = var->const_value->type;23698 payload->data.x_type = var->const_value->type;
...@@ -23717,13 +23713,13 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23717,13 +23713,13 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
2371723713
23718 AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto;23714 AstNodeFnProto *fn_node = &fn_entry->proto_node->data.fn_proto;
2371923715
23720 ZigValue *fn_decl_val = create_const_vals(1);23716 ZigValue *fn_decl_val = ira->codegen->pass1_arena->create<ZigValue>();
23721 fn_decl_val->special = ConstValSpecialStatic;23717 fn_decl_val->special = ConstValSpecialStatic;
23722 fn_decl_val->type = type_info_fn_decl_type;23718 fn_decl_val->type = type_info_fn_decl_type;
23723 fn_decl_val->parent.id = ConstParentIdUnion;23719 fn_decl_val->parent.id = ConstParentIdUnion;
23724 fn_decl_val->parent.data.p_union.union_val = inner_fields[2];23720 fn_decl_val->parent.data.p_union.union_val = inner_fields[2];
2372523721
23726 ZigValue **fn_decl_fields = alloc_const_vals_ptrs(9);23722 ZigValue **fn_decl_fields = alloc_const_vals_ptrs(ira->codegen, 9);
23727 fn_decl_val->data.x_struct.fields = fn_decl_fields;23723 fn_decl_val->data.x_struct.fields = fn_decl_fields;
2372823724
23729 // fn_type: type23725 // fn_type: type
...@@ -23761,7 +23757,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23761,7 +23757,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23761 0, 0, 0, false);23757 0, 0, 0, false);
23762 fn_decl_fields[5]->type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));23758 fn_decl_fields[5]->type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));
23763 if (fn_node->is_extern && fn_node->lib_name != nullptr && buf_len(fn_node->lib_name) > 0) {23759 if (fn_node->is_extern && fn_node->lib_name != nullptr && buf_len(fn_node->lib_name) > 0) {
23764 fn_decl_fields[5]->data.x_optional = create_const_vals(1);23760 fn_decl_fields[5]->data.x_optional = ira->codegen->pass1_arena->create<ZigValue>();
23765 ZigValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name)->data.x_ptr.data.ref.pointee;23761 ZigValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name)->data.x_ptr.data.ref.pointee;
23766 init_const_slice(ira->codegen, fn_decl_fields[5]->data.x_optional, lib_name, 0,23762 init_const_slice(ira->codegen, fn_decl_fields[5]->data.x_optional, lib_name, 0,
23767 buf_len(fn_node->lib_name), true);23763 buf_len(fn_node->lib_name), true);
...@@ -23776,12 +23772,12 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23776,12 +23772,12 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23776 // arg_names: [][] const u823772 // arg_names: [][] const u8
23777 ensure_field_index(fn_decl_val->type, "arg_names", 7);23773 ensure_field_index(fn_decl_val->type, "arg_names", 7);
23778 size_t fn_arg_count = fn_entry->variable_list.length;23774 size_t fn_arg_count = fn_entry->variable_list.length;
23779 ZigValue *fn_arg_name_array = create_const_vals(1);23775 ZigValue *fn_arg_name_array = ira->codegen->pass1_arena->create<ZigValue>();
23780 fn_arg_name_array->special = ConstValSpecialStatic;23776 fn_arg_name_array->special = ConstValSpecialStatic;
23781 fn_arg_name_array->type = get_array_type(ira->codegen,23777 fn_arg_name_array->type = get_array_type(ira->codegen,
23782 get_slice_type(ira->codegen, u8_ptr), fn_arg_count, nullptr);23778 get_slice_type(ira->codegen, u8_ptr), fn_arg_count, nullptr);
23783 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;23779 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
23784 fn_arg_name_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);23780 fn_arg_name_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count);
2378523781
23786 init_const_slice(ira->codegen, fn_decl_fields[7], fn_arg_name_array, 0, fn_arg_count, false);23782 init_const_slice(ira->codegen, fn_decl_fields[7], fn_arg_name_array, 0, fn_arg_count, false);
2378723783
...@@ -23808,7 +23804,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23808,7 +23804,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23808 // This is a type.23804 // This is a type.
23809 bigint_init_unsigned(&inner_fields[2]->data.x_union.tag, 0);23805 bigint_init_unsigned(&inner_fields[2]->data.x_union.tag, 0);
2381023806
23811 ZigValue *payload = create_const_vals(1);23807 ZigValue *payload = ira->codegen->pass1_arena->create<ZigValue>();
23812 payload->special = ConstValSpecialStatic;23808 payload->special = ConstValSpecialStatic;
23813 payload->type = ira->codegen->builtin_types.entry_type;23809 payload->type = ira->codegen->builtin_types.entry_type;
23814 payload->data.x_type = type_entry;23810 payload->data.x_type = type_entry;
...@@ -23874,11 +23870,11 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent...@@ -23874,11 +23870,11 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent
23874 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);23870 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);
23875 assertNoError(type_resolve(ira->codegen, type_info_pointer_type, ResolveStatusSizeKnown));23871 assertNoError(type_resolve(ira->codegen, type_info_pointer_type, ResolveStatusSizeKnown));
2387623872
23877 ZigValue *result = create_const_vals(1);23873 ZigValue *result = ira->codegen->pass1_arena->create<ZigValue>();
23878 result->special = ConstValSpecialStatic;23874 result->special = ConstValSpecialStatic;
23879 result->type = type_info_pointer_type;23875 result->type = type_info_pointer_type;
2388023876
23881 ZigValue **fields = alloc_const_vals_ptrs(7);23877 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 7);
23882 result->data.x_struct.fields = fields;23878 result->data.x_struct.fields = fields;
2388323879
23884 // size: Size23880 // size: Size
...@@ -23933,7 +23929,7 @@ static void make_enum_field_val(IrAnalyze *ira, ZigValue *enum_field_val, TypeEn...@@ -23933,7 +23929,7 @@ static void make_enum_field_val(IrAnalyze *ira, ZigValue *enum_field_val, TypeEn
23933 enum_field_val->special = ConstValSpecialStatic;23929 enum_field_val->special = ConstValSpecialStatic;
23934 enum_field_val->type = type_info_enum_field_type;23930 enum_field_val->type = type_info_enum_field_type;
2393523931
23936 ZigValue **inner_fields = alloc_const_vals_ptrs(2);23932 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 2);
23937 inner_fields[1]->special = ConstValSpecialStatic;23933 inner_fields[1]->special = ConstValSpecialStatic;
23938 inner_fields[1]->type = ira->codegen->builtin_types.entry_num_lit_int;23934 inner_fields[1]->type = ira->codegen->builtin_types.entry_num_lit_int;
2393923935
...@@ -23979,11 +23975,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -23979,11 +23975,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
23979 break;23975 break;
23980 case ZigTypeIdInt:23976 case ZigTypeIdInt:
23981 {23977 {
23982 result = create_const_vals(1);23978 result = ira->codegen->pass1_arena->create<ZigValue>();
23983 result->special = ConstValSpecialStatic;23979 result->special = ConstValSpecialStatic;
23984 result->type = ir_type_info_get_type(ira, "Int", nullptr);23980 result->type = ir_type_info_get_type(ira, "Int", nullptr);
2398523981
23986 ZigValue **fields = alloc_const_vals_ptrs(2);23982 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 2);
23987 result->data.x_struct.fields = fields;23983 result->data.x_struct.fields = fields;
2398823984
23989 // is_signed: bool23985 // is_signed: bool
...@@ -24001,11 +23997,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24001,11 +23997,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24001 }23997 }
24002 case ZigTypeIdFloat:23998 case ZigTypeIdFloat:
24003 {23999 {
24004 result = create_const_vals(1);24000 result = ira->codegen->pass1_arena->create<ZigValue>();
24005 result->special = ConstValSpecialStatic;24001 result->special = ConstValSpecialStatic;
24006 result->type = ir_type_info_get_type(ira, "Float", nullptr);24002 result->type = ir_type_info_get_type(ira, "Float", nullptr);
2400724003
24008 ZigValue **fields = alloc_const_vals_ptrs(1);24004 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 1);
24009 result->data.x_struct.fields = fields;24005 result->data.x_struct.fields = fields;
2401024006
24011 // bits: u824007 // bits: u8
...@@ -24025,11 +24021,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24025,11 +24021,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24025 }24021 }
24026 case ZigTypeIdArray:24022 case ZigTypeIdArray:
24027 {24023 {
24028 result = create_const_vals(1);24024 result = ira->codegen->pass1_arena->create<ZigValue>();
24029 result->special = ConstValSpecialStatic;24025 result->special = ConstValSpecialStatic;
24030 result->type = ir_type_info_get_type(ira, "Array", nullptr);24026 result->type = ir_type_info_get_type(ira, "Array", nullptr);
2403124027
24032 ZigValue **fields = alloc_const_vals_ptrs(3);24028 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 3);
24033 result->data.x_struct.fields = fields;24029 result->data.x_struct.fields = fields;
2403424030
24035 // len: usize24031 // len: usize
...@@ -24049,11 +24045,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24049,11 +24045,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24049 break;24045 break;
24050 }24046 }
24051 case ZigTypeIdVector: {24047 case ZigTypeIdVector: {
24052 result = create_const_vals(1);24048 result = ira->codegen->pass1_arena->create<ZigValue>();
24053 result->special = ConstValSpecialStatic;24049 result->special = ConstValSpecialStatic;
24054 result->type = ir_type_info_get_type(ira, "Vector", nullptr);24050 result->type = ir_type_info_get_type(ira, "Vector", nullptr);
2405524051
24056 ZigValue **fields = alloc_const_vals_ptrs(2);24052 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 2);
24057 result->data.x_struct.fields = fields;24053 result->data.x_struct.fields = fields;
2405824054
24059 // len: usize24055 // len: usize
...@@ -24071,11 +24067,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24071,11 +24067,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24071 }24067 }
24072 case ZigTypeIdOptional:24068 case ZigTypeIdOptional:
24073 {24069 {
24074 result = create_const_vals(1);24070 result = ira->codegen->pass1_arena->create<ZigValue>();
24075 result->special = ConstValSpecialStatic;24071 result->special = ConstValSpecialStatic;
24076 result->type = ir_type_info_get_type(ira, "Optional", nullptr);24072 result->type = ir_type_info_get_type(ira, "Optional", nullptr);
2407724073
24078 ZigValue **fields = alloc_const_vals_ptrs(1);24074 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 1);
24079 result->data.x_struct.fields = fields;24075 result->data.x_struct.fields = fields;
2408024076
24081 // child: type24077 // child: type
...@@ -24087,11 +24083,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24087,11 +24083,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24087 break;24083 break;
24088 }24084 }
24089 case ZigTypeIdAnyFrame: {24085 case ZigTypeIdAnyFrame: {
24090 result = create_const_vals(1);24086 result = ira->codegen->pass1_arena->create<ZigValue>();
24091 result->special = ConstValSpecialStatic;24087 result->special = ConstValSpecialStatic;
24092 result->type = ir_type_info_get_type(ira, "AnyFrame", nullptr);24088 result->type = ir_type_info_get_type(ira, "AnyFrame", nullptr);
2409324089
24094 ZigValue **fields = alloc_const_vals_ptrs(1);24090 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 1);
24095 result->data.x_struct.fields = fields;24091 result->data.x_struct.fields = fields;
2409624092
24097 // child: ?type24093 // child: ?type
...@@ -24104,11 +24100,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24104,11 +24100,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24104 }24100 }
24105 case ZigTypeIdEnum:24101 case ZigTypeIdEnum:
24106 {24102 {
24107 result = create_const_vals(1);24103 result = ira->codegen->pass1_arena->create<ZigValue>();
24108 result->special = ConstValSpecialStatic;24104 result->special = ConstValSpecialStatic;
24109 result->type = ir_type_info_get_type(ira, "Enum", nullptr);24105 result->type = ir_type_info_get_type(ira, "Enum", nullptr);
2411024106
24111 ZigValue **fields = alloc_const_vals_ptrs(5);24107 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 5);
24112 result->data.x_struct.fields = fields;24108 result->data.x_struct.fields = fields;
2411324109
24114 // layout: ContainerLayout24110 // layout: ContainerLayout
...@@ -24130,11 +24126,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24130,11 +24126,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24130 }24126 }
24131 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;24127 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
2413224128
24133 ZigValue *enum_field_array = create_const_vals(1);24129 ZigValue *enum_field_array = ira->codegen->pass1_arena->create<ZigValue>();
24134 enum_field_array->special = ConstValSpecialStatic;24130 enum_field_array->special = ConstValSpecialStatic;
24135 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count, nullptr);24131 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count, nullptr);
24136 enum_field_array->data.x_array.special = ConstArraySpecialNone;24132 enum_field_array->data.x_array.special = ConstArraySpecialNone;
24137 enum_field_array->data.x_array.data.s_none.elements = create_const_vals(enum_field_count);24133 enum_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(enum_field_count);
2413824134
24139 init_const_slice(ira->codegen, fields[2], enum_field_array, 0, enum_field_count, false);24135 init_const_slice(ira->codegen, fields[2], enum_field_array, 0, enum_field_count, false);
2414024136
...@@ -24164,7 +24160,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24164,7 +24160,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24164 }24160 }
24165 case ZigTypeIdErrorSet:24161 case ZigTypeIdErrorSet:
24166 {24162 {
24167 result = create_const_vals(1);24163 result = ira->codegen->pass1_arena->create<ZigValue>();
24168 result->special = ConstValSpecialStatic;24164 result->special = ConstValSpecialStatic;
24169 result->type = ir_type_info_get_type(ira, "ErrorSet", nullptr);24165 result->type = ir_type_info_get_type(ira, "ErrorSet", nullptr);
2417024166
...@@ -24179,15 +24175,15 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24179,15 +24175,15 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24179 if ((err = type_resolve(ira->codegen, type_info_error_type, ResolveStatusSizeKnown))) {24175 if ((err = type_resolve(ira->codegen, type_info_error_type, ResolveStatusSizeKnown))) {
24180 zig_unreachable();24176 zig_unreachable();
24181 }24177 }
24182 ZigValue *slice_val = create_const_vals(1);24178 ZigValue *slice_val = ira->codegen->pass1_arena->create<ZigValue>();
24183 result->data.x_optional = slice_val;24179 result->data.x_optional = slice_val;
2418424180
24185 uint32_t error_count = type_entry->data.error_set.err_count;24181 uint32_t error_count = type_entry->data.error_set.err_count;
24186 ZigValue *error_array = create_const_vals(1);24182 ZigValue *error_array = ira->codegen->pass1_arena->create<ZigValue>();
24187 error_array->special = ConstValSpecialStatic;24183 error_array->special = ConstValSpecialStatic;
24188 error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count, nullptr);24184 error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count, nullptr);
24189 error_array->data.x_array.special = ConstArraySpecialNone;24185 error_array->data.x_array.special = ConstArraySpecialNone;
24190 error_array->data.x_array.data.s_none.elements = create_const_vals(error_count);24186 error_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(error_count);
2419124187
24192 init_const_slice(ira->codegen, slice_val, error_array, 0, error_count, false);24188 init_const_slice(ira->codegen, slice_val, error_array, 0, error_count, false);
24193 for (uint32_t error_index = 0; error_index < error_count; error_index++) {24189 for (uint32_t error_index = 0; error_index < error_count; error_index++) {
...@@ -24197,7 +24193,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24197,7 +24193,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24197 error_val->special = ConstValSpecialStatic;24193 error_val->special = ConstValSpecialStatic;
24198 error_val->type = type_info_error_type;24194 error_val->type = type_info_error_type;
2419924195
24200 ZigValue **inner_fields = alloc_const_vals_ptrs(2);24196 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 2);
24201 inner_fields[1]->special = ConstValSpecialStatic;24197 inner_fields[1]->special = ConstValSpecialStatic;
24202 inner_fields[1]->type = ira->codegen->builtin_types.entry_num_lit_int;24198 inner_fields[1]->type = ira->codegen->builtin_types.entry_num_lit_int;
2420324199
...@@ -24219,11 +24215,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24219,11 +24215,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24219 }24215 }
24220 case ZigTypeIdErrorUnion:24216 case ZigTypeIdErrorUnion:
24221 {24217 {
24222 result = create_const_vals(1);24218 result = ira->codegen->pass1_arena->create<ZigValue>();
24223 result->special = ConstValSpecialStatic;24219 result->special = ConstValSpecialStatic;
24224 result->type = ir_type_info_get_type(ira, "ErrorUnion", nullptr);24220 result->type = ir_type_info_get_type(ira, "ErrorUnion", nullptr);
2422524221
24226 ZigValue **fields = alloc_const_vals_ptrs(2);24222 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 2);
24227 result->data.x_struct.fields = fields;24223 result->data.x_struct.fields = fields;
2422824224
24229 // error_set: type24225 // error_set: type
...@@ -24242,11 +24238,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24242,11 +24238,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24242 }24238 }
24243 case ZigTypeIdUnion:24239 case ZigTypeIdUnion:
24244 {24240 {
24245 result = create_const_vals(1);24241 result = ira->codegen->pass1_arena->create<ZigValue>();
24246 result->special = ConstValSpecialStatic;24242 result->special = ConstValSpecialStatic;
24247 result->type = ir_type_info_get_type(ira, "Union", nullptr);24243 result->type = ir_type_info_get_type(ira, "Union", nullptr);
2424824244
24249 ZigValue **fields = alloc_const_vals_ptrs(4);24245 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);
24250 result->data.x_struct.fields = fields;24246 result->data.x_struct.fields = fields;
2425124247
24252 // layout: ContainerLayout24248 // layout: ContainerLayout
...@@ -24263,7 +24259,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24263,7 +24259,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24263 if (union_decl_node->data.container_decl.auto_enum ||24259 if (union_decl_node->data.container_decl.auto_enum ||
24264 union_decl_node->data.container_decl.init_arg_expr != nullptr)24260 union_decl_node->data.container_decl.init_arg_expr != nullptr)
24265 {24261 {
24266 ZigValue *tag_type = create_const_vals(1);24262 ZigValue *tag_type = ira->codegen->pass1_arena->create<ZigValue>();
24267 tag_type->special = ConstValSpecialStatic;24263 tag_type->special = ConstValSpecialStatic;
24268 tag_type->type = ira->codegen->builtin_types.entry_type;24264 tag_type->type = ira->codegen->builtin_types.entry_type;
24269 tag_type->data.x_type = type_entry->data.unionation.tag_type;24265 tag_type->data.x_type = type_entry->data.unionation.tag_type;
...@@ -24279,11 +24275,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24279,11 +24275,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24279 zig_unreachable();24275 zig_unreachable();
24280 uint32_t union_field_count = type_entry->data.unionation.src_field_count;24276 uint32_t union_field_count = type_entry->data.unionation.src_field_count;
2428124277
24282 ZigValue *union_field_array = create_const_vals(1);24278 ZigValue *union_field_array = ira->codegen->pass1_arena->create<ZigValue>();
24283 union_field_array->special = ConstValSpecialStatic;24279 union_field_array->special = ConstValSpecialStatic;
24284 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count, nullptr);24280 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count, nullptr);
24285 union_field_array->data.x_array.special = ConstArraySpecialNone;24281 union_field_array->data.x_array.special = ConstArraySpecialNone;
24286 union_field_array->data.x_array.data.s_none.elements = create_const_vals(union_field_count);24282 union_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(union_field_count);
2428724283
24288 init_const_slice(ira->codegen, fields[2], union_field_array, 0, union_field_count, false);24284 init_const_slice(ira->codegen, fields[2], union_field_array, 0, union_field_count, false);
2428924285
...@@ -24296,14 +24292,14 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24296,14 +24292,14 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24296 union_field_val->special = ConstValSpecialStatic;24292 union_field_val->special = ConstValSpecialStatic;
24297 union_field_val->type = type_info_union_field_type;24293 union_field_val->type = type_info_union_field_type;
2429824294
24299 ZigValue **inner_fields = alloc_const_vals_ptrs(3);24295 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3);
24300 inner_fields[1]->special = ConstValSpecialStatic;24296 inner_fields[1]->special = ConstValSpecialStatic;
24301 inner_fields[1]->type = get_optional_type(ira->codegen, type_info_enum_field_type);24297 inner_fields[1]->type = get_optional_type(ira->codegen, type_info_enum_field_type);
2430224298
24303 if (fields[1]->data.x_optional == nullptr) {24299 if (fields[1]->data.x_optional == nullptr) {
24304 inner_fields[1]->data.x_optional = nullptr;24300 inner_fields[1]->data.x_optional = nullptr;
24305 } else {24301 } else {
24306 inner_fields[1]->data.x_optional = create_const_vals(1);24302 inner_fields[1]->data.x_optional = ira->codegen->pass1_arena->create<ZigValue>();
24307 make_enum_field_val(ira, inner_fields[1]->data.x_optional, union_field->enum_field, type_info_enum_field_type);24303 make_enum_field_val(ira, inner_fields[1]->data.x_optional, union_field->enum_field, type_info_enum_field_type);
24308 }24304 }
2430924305
...@@ -24338,11 +24334,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24338,11 +24334,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24338 break;24334 break;
24339 }24335 }
2434024336
24341 result = create_const_vals(1);24337 result = ira->codegen->pass1_arena->create<ZigValue>();
24342 result->special = ConstValSpecialStatic;24338 result->special = ConstValSpecialStatic;
24343 result->type = ir_type_info_get_type(ira, "Struct", nullptr);24339 result->type = ir_type_info_get_type(ira, "Struct", nullptr);
2434424340
24345 ZigValue **fields = alloc_const_vals_ptrs(3);24341 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 3);
24346 result->data.x_struct.fields = fields;24342 result->data.x_struct.fields = fields;
2434724343
24348 // layout: ContainerLayout24344 // layout: ContainerLayout
...@@ -24359,11 +24355,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24359,11 +24355,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24359 }24355 }
24360 uint32_t struct_field_count = type_entry->data.structure.src_field_count;24356 uint32_t struct_field_count = type_entry->data.structure.src_field_count;
2436124357
24362 ZigValue *struct_field_array = create_const_vals(1);24358 ZigValue *struct_field_array = ira->codegen->pass1_arena->create<ZigValue>();
24363 struct_field_array->special = ConstValSpecialStatic;24359 struct_field_array->special = ConstValSpecialStatic;
24364 struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count, nullptr);24360 struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count, nullptr);
24365 struct_field_array->data.x_array.special = ConstArraySpecialNone;24361 struct_field_array->data.x_array.special = ConstArraySpecialNone;
24366 struct_field_array->data.x_array.data.s_none.elements = create_const_vals(struct_field_count);24362 struct_field_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(struct_field_count);
2436724363
24368 init_const_slice(ira->codegen, fields[1], struct_field_array, 0, struct_field_count, false);24364 init_const_slice(ira->codegen, fields[1], struct_field_array, 0, struct_field_count, false);
2436924365
...@@ -24374,7 +24370,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24374,7 +24370,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24374 struct_field_val->special = ConstValSpecialStatic;24370 struct_field_val->special = ConstValSpecialStatic;
24375 struct_field_val->type = type_info_struct_field_type;24371 struct_field_val->type = type_info_struct_field_type;
2437624372
24377 ZigValue **inner_fields = alloc_const_vals_ptrs(4);24373 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 4);
24378 inner_fields[1]->special = ConstValSpecialStatic;24374 inner_fields[1]->special = ConstValSpecialStatic;
24379 inner_fields[1]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int);24375 inner_fields[1]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int);
2438024376
...@@ -24387,7 +24383,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24387,7 +24383,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24387 inner_fields[1]->data.x_optional = nullptr;24383 inner_fields[1]->data.x_optional = nullptr;
24388 } else {24384 } else {
24389 size_t byte_offset = struct_field->offset;24385 size_t byte_offset = struct_field->offset;
24390 inner_fields[1]->data.x_optional = create_const_vals(1);24386 inner_fields[1]->data.x_optional = ira->codegen->pass1_arena->create<ZigValue>();
24391 inner_fields[1]->data.x_optional->special = ConstValSpecialStatic;24387 inner_fields[1]->data.x_optional->special = ConstValSpecialStatic;
24392 inner_fields[1]->data.x_optional->type = ira->codegen->builtin_types.entry_num_lit_int;24388 inner_fields[1]->data.x_optional->type = ira->codegen->builtin_types.entry_num_lit_int;
24393 bigint_init_unsigned(&inner_fields[1]->data.x_optional->data.x_bigint, byte_offset);24389 bigint_init_unsigned(&inner_fields[1]->data.x_optional->data.x_bigint, byte_offset);
...@@ -24423,11 +24419,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24423,11 +24419,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24423 }24419 }
24424 case ZigTypeIdFn:24420 case ZigTypeIdFn:
24425 {24421 {
24426 result = create_const_vals(1);24422 result = ira->codegen->pass1_arena->create<ZigValue>();
24427 result->special = ConstValSpecialStatic;24423 result->special = ConstValSpecialStatic;
24428 result->type = ir_type_info_get_type(ira, "Fn", nullptr);24424 result->type = ir_type_info_get_type(ira, "Fn", nullptr);
2442924425
24430 ZigValue **fields = alloc_const_vals_ptrs(5);24426 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 5);
24431 result->data.x_struct.fields = fields;24427 result->data.x_struct.fields = fields;
2443224428
24433 // calling_convention: TypeInfo.CallingConvention24429 // calling_convention: TypeInfo.CallingConvention
...@@ -24454,7 +24450,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24454,7 +24450,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24454 if (type_entry->data.fn.fn_type_id.return_type == nullptr)24450 if (type_entry->data.fn.fn_type_id.return_type == nullptr)
24455 fields[3]->data.x_optional = nullptr;24451 fields[3]->data.x_optional = nullptr;
24456 else {24452 else {
24457 ZigValue *return_type = create_const_vals(1);24453 ZigValue *return_type = ira->codegen->pass1_arena->create<ZigValue>();
24458 return_type->special = ConstValSpecialStatic;24454 return_type->special = ConstValSpecialStatic;
24459 return_type->type = ira->codegen->builtin_types.entry_type;24455 return_type->type = ira->codegen->builtin_types.entry_type;
24460 return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;24456 return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;
...@@ -24468,11 +24464,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24468,11 +24464,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24468 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -24464 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -
24469 (is_varargs && type_entry->data.fn.fn_type_id.cc != CallingConventionC);24465 (is_varargs && type_entry->data.fn.fn_type_id.cc != CallingConventionC);
2447024466
24471 ZigValue *fn_arg_array = create_const_vals(1);24467 ZigValue *fn_arg_array = ira->codegen->pass1_arena->create<ZigValue>();
24472 fn_arg_array->special = ConstValSpecialStatic;24468 fn_arg_array->special = ConstValSpecialStatic;
24473 fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count, nullptr);24469 fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count, nullptr);
24474 fn_arg_array->data.x_array.special = ConstArraySpecialNone;24470 fn_arg_array->data.x_array.special = ConstArraySpecialNone;
24475 fn_arg_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);24471 fn_arg_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count);
2447624472
24477 init_const_slice(ira->codegen, fields[4], fn_arg_array, 0, fn_arg_count, false);24473 init_const_slice(ira->codegen, fields[4], fn_arg_array, 0, fn_arg_count, false);
2447824474
...@@ -24486,7 +24482,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24486,7 +24482,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24486 bool arg_is_generic = fn_param_info->type == nullptr;24482 bool arg_is_generic = fn_param_info->type == nullptr;
24487 if (arg_is_generic) assert(is_generic);24483 if (arg_is_generic) assert(is_generic);
2448824484
24489 ZigValue **inner_fields = alloc_const_vals_ptrs(3);24485 ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3);
24490 inner_fields[0]->special = ConstValSpecialStatic;24486 inner_fields[0]->special = ConstValSpecialStatic;
24491 inner_fields[0]->type = ira->codegen->builtin_types.entry_bool;24487 inner_fields[0]->type = ira->codegen->builtin_types.entry_bool;
24492 inner_fields[0]->data.x_bool = arg_is_generic;24488 inner_fields[0]->data.x_bool = arg_is_generic;
...@@ -24499,7 +24495,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24499,7 +24495,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24499 if (arg_is_generic)24495 if (arg_is_generic)
24500 inner_fields[2]->data.x_optional = nullptr;24496 inner_fields[2]->data.x_optional = nullptr;
24501 else {24497 else {
24502 ZigValue *arg_type = create_const_vals(1);24498 ZigValue *arg_type = ira->codegen->pass1_arena->create<ZigValue>();
24503 arg_type->special = ConstValSpecialStatic;24499 arg_type->special = ConstValSpecialStatic;
24504 arg_type->type = ira->codegen->builtin_types.entry_type;24500 arg_type->type = ira->codegen->builtin_types.entry_type;
24505 arg_type->data.x_type = fn_param_info->type;24501 arg_type->data.x_type = fn_param_info->type;
...@@ -24524,7 +24520,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -24524,7 +24520,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
24524 break;24520 break;
24525 }24521 }
24526 case ZigTypeIdFnFrame:24522 case ZigTypeIdFnFrame:
24527 zig_panic("TODO @typeInfo for async function frames");24523 ir_add_error(ira, source_instr,
24524 buf_sprintf("compiler bug: TODO @typeInfo for async function frames. https://github.com/ziglang/zig/issues/3066"));
24525 return ErrorSemanticAnalyzeFail;
24528 }24526 }
2452924527
24530 assert(result != nullptr);24528 assert(result != nullptr);
...@@ -24823,7 +24821,7 @@ static IrInstGen *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstSrcType...@@ -24823,7 +24821,7 @@ static IrInstGen *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstSrcType
24823 type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, type_bare_name(type_entry));24821 type_entry->cached_const_name_val = create_const_str_lit(ira->codegen, type_bare_name(type_entry));
24824 }24822 }
24825 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);24823 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
24826 copy_const_val(result->value, type_entry->cached_const_name_val);24824 copy_const_val(ira->codegen, result->value, type_entry->cached_const_name_val);
24827 return result;24825 return result;
24828}24826}
2482924827
...@@ -24855,7 +24853,6 @@ static IrInstGen *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstSrcCImpo...@@ -24855,7 +24853,6 @@ static IrInstGen *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstSrcCImpo
24855 }24853 }
24856 if (type_is_invalid(cimport_result->type))24854 if (type_is_invalid(cimport_result->type))
24857 return ira->codegen->invalid_inst_gen;24855 return ira->codegen->invalid_inst_gen;
24858 destroy(result_ptr, "ZigValue");
2485924856
24860 ZigPackage *cur_scope_pkg = scope_package(instruction->base.base.scope);24857 ZigPackage *cur_scope_pkg = scope_package(instruction->base.base.scope);
24861 Buf *namespace_name = buf_sprintf("%s.cimport:%" ZIG_PRI_usize ":%" ZIG_PRI_usize,24858 Buf *namespace_name = buf_sprintf("%s.cimport:%" ZIG_PRI_usize ":%" ZIG_PRI_usize,
...@@ -25534,11 +25531,11 @@ static IrInstGen *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstSrcToByt...@@ -25534,11 +25531,11 @@ static IrInstGen *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstSrcToByt
25534 return ira->codegen->invalid_inst_gen;25531 return ira->codegen->invalid_inst_gen;
2553525532
25536 IrInstGen *result = ir_const(ira, &instruction->base.base, dest_slice_type);25533 IrInstGen *result = ir_const(ira, &instruction->base.base, dest_slice_type);
25537 result->value->data.x_struct.fields = alloc_const_vals_ptrs(2);25534 result->value->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
2553825535
25539 ZigValue *ptr_val = result->value->data.x_struct.fields[slice_ptr_index];25536 ZigValue *ptr_val = result->value->data.x_struct.fields[slice_ptr_index];
25540 ZigValue *target_ptr_val = target_val->data.x_struct.fields[slice_ptr_index];25537 ZigValue *target_ptr_val = target_val->data.x_struct.fields[slice_ptr_index];
25541 copy_const_val(ptr_val, target_ptr_val);25538 copy_const_val(ira->codegen, ptr_val, target_ptr_val);
25542 ptr_val->type = dest_ptr_type;25539 ptr_val->type = dest_ptr_type;
2554325540
25544 ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index];25541 ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index];
...@@ -25825,7 +25822,7 @@ static IrInstGen *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInst* source_instr...@@ -25825,7 +25822,7 @@ static IrInstGen *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInst* source_instr
25825 expand_undef_array(ira->codegen, b_val);25822 expand_undef_array(ira->codegen, b_val);
2582625823
25827 IrInstGen *result = ir_const(ira, source_instr, result_type);25824 IrInstGen *result = ir_const(ira, source_instr, result_type);
25828 result->value->data.x_array.data.s_none.elements = create_const_vals(len_mask);25825 result->value->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(len_mask);
25829 for (uint32_t i = 0; i < mask_val->type->data.vector.len; i += 1) {25826 for (uint32_t i = 0; i < mask_val->type->data.vector.len; i += 1) {
25830 ZigValue *mask_elem_val = &mask_val->data.x_array.data.s_none.elements[i];25827 ZigValue *mask_elem_val = &mask_val->data.x_array.data.s_none.elements[i];
25831 ZigValue *result_elem_val = &result->value->data.x_array.data.s_none.elements[i];25828 ZigValue *result_elem_val = &result->value->data.x_array.data.s_none.elements[i];
...@@ -25838,7 +25835,7 @@ static IrInstGen *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInst* source_instr...@@ -25838,7 +25835,7 @@ static IrInstGen *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInst* source_instr
25838 ZigValue *src_elem_val = (v >= 0) ?25835 ZigValue *src_elem_val = (v >= 0) ?
25839 &a->value->data.x_array.data.s_none.elements[v] :25836 &a->value->data.x_array.data.s_none.elements[v] :
25840 &b->value->data.x_array.data.s_none.elements[~v];25837 &b->value->data.x_array.data.s_none.elements[~v];
25841 copy_const_val(result_elem_val, src_elem_val);25838 copy_const_val(ira->codegen, result_elem_val, src_elem_val);
2584225839
25843 ir_assert(result_elem_val->special == ConstValSpecialStatic, source_instr);25840 ir_assert(result_elem_val->special == ConstValSpecialStatic, source_instr);
25844 }25841 }
...@@ -25858,7 +25855,7 @@ static IrInstGen *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInst* source_instr...@@ -25858,7 +25855,7 @@ static IrInstGen *ir_analyze_shuffle_vector(IrAnalyze *ira, IrInst* source_instr
2585825855
25859 IrInstGen *expand_mask = ir_const(ira, &mask->base,25856 IrInstGen *expand_mask = ir_const(ira, &mask->base,
25860 get_vector_type(ira->codegen, len_max, ira->codegen->builtin_types.entry_i32));25857 get_vector_type(ira->codegen, len_max, ira->codegen->builtin_types.entry_i32));
25861 expand_mask->value->data.x_array.data.s_none.elements = create_const_vals(len_max);25858 expand_mask->value->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(len_max);
25862 uint32_t i = 0;25859 uint32_t i = 0;
25863 for (; i < len_min; i += 1)25860 for (; i < len_min; i += 1)
25864 bigint_init_unsigned(&expand_mask->value->data.x_array.data.s_none.elements[i].data.x_bigint, i);25861 bigint_init_unsigned(&expand_mask->value->data.x_array.data.s_none.elements[i].data.x_bigint, i);
...@@ -25928,9 +25925,9 @@ static IrInstGen *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstSrcSplat *i...@@ -25928,9 +25925,9 @@ static IrInstGen *ir_analyze_instruction_splat(IrAnalyze *ira, IrInstSrcSplat *i
25928 return ir_const_undef(ira, &instruction->base.base, return_type);25925 return ir_const_undef(ira, &instruction->base.base, return_type);
2592925926
25930 IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);25927 IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);
25931 result->value->data.x_array.data.s_none.elements = create_const_vals(len_int);25928 result->value->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(len_int);
25932 for (uint32_t i = 0; i < len_int; i += 1) {25929 for (uint32_t i = 0; i < len_int; i += 1) {
25933 copy_const_val(&result->value->data.x_array.data.s_none.elements[i], scalar_val);25930 copy_const_val(ira->codegen, &result->value->data.x_array.data.s_none.elements[i], scalar_val);
25934 }25931 }
25935 return result;25932 return result;
25936 }25933 }
...@@ -26068,7 +26065,7 @@ static IrInstGen *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstSrcMemset...@@ -26068,7 +26065,7 @@ static IrInstGen *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstSrcMemset
26068 }26065 }
2606926066
26070 for (size_t i = start; i < end; i += 1) {26067 for (size_t i = start; i < end; i += 1) {
26071 copy_const_val(&dest_elements[i], byte_val);26068 copy_const_val(ira->codegen, &dest_elements[i], byte_val);
26072 }26069 }
2607326070
26074 return ir_const_void(ira, &instruction->base.base);26071 return ir_const_void(ira, &instruction->base.base);
...@@ -26244,7 +26241,7 @@ static IrInstGen *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstSrcMemcpy...@@ -26244,7 +26241,7 @@ static IrInstGen *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstSrcMemcpy
26244 // TODO check for noalias violations - this should be generalized to work for any function26241 // TODO check for noalias violations - this should be generalized to work for any function
2624526242
26246 for (size_t i = 0; i < count; i += 1) {26243 for (size_t i = 0; i < count; i += 1) {
26247 copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i]);26244 copy_const_val(ira->codegen, &dest_elements[dest_start + i], &src_elements[src_start + i]);
26248 }26245 }
2624926246
26250 return ir_const_void(ira, &instruction->base.base);26247 return ir_const_void(ira, &instruction->base.base);
...@@ -26528,7 +26525,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26528,7 +26525,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
2652826525
26529 IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);26526 IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);
26530 ZigValue *out_val = result->value;26527 ZigValue *out_val = result->value;
26531 out_val->data.x_struct.fields = alloc_const_vals_ptrs(2);26528 out_val->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
2653226529
26533 ZigValue *ptr_val = out_val->data.x_struct.fields[slice_ptr_index];26530 ZigValue *ptr_val = out_val->data.x_struct.fields[slice_ptr_index];
2653426531
...@@ -26823,7 +26820,7 @@ static IrInstGen *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstSrcAlign...@@ -26823,7 +26820,7 @@ static IrInstGen *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstSrcAlign
26823 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_num_lit_int);26820 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_num_lit_int);
26824 result->value->special = ConstValSpecialLazy;26821 result->value->special = ConstValSpecialLazy;
2682526822
26826 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1, "LazyValueAlignOf");26823 LazyValueAlignOf *lazy_align_of = heap::c_allocator.create<LazyValueAlignOf>();
26827 lazy_align_of->ira = ira; ira_ref(ira);26824 lazy_align_of->ira = ira; ira_ref(ira);
26828 result->value->data.x_lazy = &lazy_align_of->base;26825 result->value->data.x_lazy = &lazy_align_of->base;
26829 lazy_align_of->base.id = LazyValueIdAlignOf;26826 lazy_align_of->base.id = LazyValueIdAlignOf;
...@@ -27149,7 +27146,7 @@ static IrInstGen *ir_analyze_unwrap_err_code(IrAnalyze *ira, IrInst* source_inst...@@ -27149,7 +27146,7 @@ static IrInstGen *ir_analyze_unwrap_err_code(IrAnalyze *ira, IrInst* source_inst
27149 return ira->codegen->invalid_inst_gen;27146 return ira->codegen->invalid_inst_gen;
2715027147
27151 if (initializing && err_union_val->special == ConstValSpecialUndef) {27148 if (initializing && err_union_val->special == ConstValSpecialUndef) {
27152 ZigValue *vals = create_const_vals(2);27149 ZigValue *vals = ira->codegen->pass1_arena->allocate<ZigValue>(2);
27153 ZigValue *err_set_val = &vals[0];27150 ZigValue *err_set_val = &vals[0];
27154 ZigValue *payload_val = &vals[1];27151 ZigValue *payload_val = &vals[1];
2715527152
...@@ -27230,7 +27227,7 @@ static IrInstGen *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInst* source...@@ -27230,7 +27227,7 @@ static IrInstGen *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInst* source
27230 if (err_union_val == nullptr)27227 if (err_union_val == nullptr)
27231 return ira->codegen->invalid_inst_gen;27228 return ira->codegen->invalid_inst_gen;
27232 if (initializing && err_union_val->special == ConstValSpecialUndef) {27229 if (initializing && err_union_val->special == ConstValSpecialUndef) {
27233 ZigValue *vals = create_const_vals(2);27230 ZigValue *vals = ira->codegen->pass1_arena->allocate<ZigValue>(2);
27234 ZigValue *err_set_val = &vals[0];27231 ZigValue *err_set_val = &vals[0];
27235 ZigValue *payload_val = &vals[1];27232 ZigValue *payload_val = &vals[1];
2723627233
...@@ -27292,7 +27289,7 @@ static IrInstGen *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstSrcFnPro...@@ -27292,7 +27289,7 @@ static IrInstGen *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstSrcFnPro
27292 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);27289 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);
27293 result->value->special = ConstValSpecialLazy;27290 result->value->special = ConstValSpecialLazy;
2729427291
27295 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1, "LazyValueFnType");27292 LazyValueFnType *lazy_fn_type = heap::c_allocator.create<LazyValueFnType>();
27296 lazy_fn_type->ira = ira; ira_ref(ira);27293 lazy_fn_type->ira = ira; ira_ref(ira);
27297 result->value->data.x_lazy = &lazy_fn_type->base;27294 result->value->data.x_lazy = &lazy_fn_type->base;
27298 lazy_fn_type->base.id = LazyValueIdFnType;27295 lazy_fn_type->base.id = LazyValueIdFnType;
...@@ -27320,7 +27317,7 @@ static IrInstGen *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstSrcFnPro...@@ -27320,7 +27317,7 @@ static IrInstGen *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstSrcFnPro
2732027317
27321 size_t param_count = proto_node->data.fn_proto.params.length;27318 size_t param_count = proto_node->data.fn_proto.params.length;
27322 lazy_fn_type->proto_node = proto_node;27319 lazy_fn_type->proto_node = proto_node;
27323 lazy_fn_type->param_types = allocate<IrInstGen *>(param_count);27320 lazy_fn_type->param_types = heap::c_allocator.allocate<IrInstGen *>(param_count);
2732427321
27325 for (size_t param_index = 0; param_index < param_count; param_index += 1) {27322 for (size_t param_index = 0; param_index < param_count; param_index += 1) {
27326 AstNode *param_node = proto_node->data.fn_proto.params.at(param_index);27323 AstNode *param_node = proto_node->data.fn_proto.params.at(param_index);
...@@ -27475,7 +27472,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -27475,7 +27472,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
27475 }27472 }
2747627473
27477 size_t field_prev_uses_count = ira->codegen->errors_by_index.length;27474 size_t field_prev_uses_count = ira->codegen->errors_by_index.length;
27478 AstNode **field_prev_uses = allocate<AstNode *>(field_prev_uses_count, "AstNode *");27475 AstNode **field_prev_uses = heap::c_allocator.allocate<AstNode *>(field_prev_uses_count);
2747927476
27480 for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {27477 for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
27481 IrInstSrcCheckSwitchProngsRange *range = &instruction->ranges[range_i];27478 IrInstSrcCheckSwitchProngsRange *range = &instruction->ranges[range_i];
...@@ -27532,7 +27529,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -27532,7 +27529,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
27532 }27529 }
27533 }27530 }
2753427531
27535 deallocate(field_prev_uses, field_prev_uses_count, "AstNode *");27532 heap::c_allocator.deallocate(field_prev_uses, field_prev_uses_count);
27536 } else if (switch_type->id == ZigTypeIdInt) {27533 } else if (switch_type->id == ZigTypeIdInt) {
27537 RangeSet rs = {0};27534 RangeSet rs = {0};
27538 for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {27535 for (size_t range_i = 0; range_i < instruction->range_count; range_i += 1) {
...@@ -27725,7 +27722,7 @@ static IrInstGen *ir_align_cast(IrAnalyze *ira, IrInstGen *target, uint32_t alig...@@ -27725,7 +27722,7 @@ static IrInstGen *ir_align_cast(IrAnalyze *ira, IrInstGen *target, uint32_t alig
27725 }27722 }
2772627723
27727 IrInstGen *result = ir_const(ira, &target->base, result_type);27724 IrInstGen *result = ir_const(ira, &target->base, result_type);
27728 copy_const_val(result->value, val);27725 copy_const_val(ira->codegen, result->value, val);
27729 result->value->type = result_type;27726 result->value->type = result_type;
27730 return result;27727 return result;
27731 }27728 }
...@@ -27821,7 +27818,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn...@@ -27821,7 +27818,7 @@ static IrInstGen *ir_analyze_ptr_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
27821 InferredStructField *isf = (val->type->id == ZigTypeIdPointer) ?27818 InferredStructField *isf = (val->type->id == ZigTypeIdPointer) ?
27822 val->type->data.pointer.inferred_struct_field : nullptr;27819 val->type->data.pointer.inferred_struct_field : nullptr;
27823 if (isf == nullptr) {27820 if (isf == nullptr) {
27824 copy_const_val(result->value, val);27821 copy_const_val(ira->codegen, result->value, val);
27825 } else {27822 } else {
27826 // The destination value should have x_ptr struct pointing to underlying struct value27823 // The destination value should have x_ptr struct pointing to underlying struct value
27827 result->value->data.x_ptr.mut = val->data.x_ptr.mut;27824 result->value->data.x_ptr.mut = val->data.x_ptr.mut;
...@@ -27978,7 +27975,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ZigValue *val)...@@ -27978,7 +27975,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ZigValue *val)
27978 while (gen_i < gen_field_count) {27975 while (gen_i < gen_field_count) {
27979 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];27976 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
27980 if (big_int_byte_count > child_buf_len) {27977 if (big_int_byte_count > child_buf_len) {
27981 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);27978 child_buf = heap::c_allocator.allocate_nonzero<uint8_t>(big_int_byte_count);
27982 child_buf_len = big_int_byte_count;27979 child_buf_len = big_int_byte_count;
27983 }27980 }
27984 BigInt big_int;27981 BigInt big_int;
...@@ -28041,7 +28038,7 @@ static Error buf_read_value_bytes_array(IrAnalyze *ira, CodeGen *codegen, AstNod...@@ -28041,7 +28038,7 @@ static Error buf_read_value_bytes_array(IrAnalyze *ira, CodeGen *codegen, AstNod
2804128038
28042 switch (val->data.x_array.special) {28039 switch (val->data.x_array.special) {
28043 case ConstArraySpecialNone:28040 case ConstArraySpecialNone:
28044 val->data.x_array.data.s_none.elements = create_const_vals(len);28041 val->data.x_array.data.s_none.elements = codegen->pass1_arena->allocate<ZigValue>(len);
28045 for (size_t i = 0; i < len; i++) {28042 for (size_t i = 0; i < len; i++) {
28046 ZigValue *elem = &val->data.x_array.data.s_none.elements[i];28043 ZigValue *elem = &val->data.x_array.data.s_none.elements[i];
28047 elem->special = ConstValSpecialStatic;28044 elem->special = ConstValSpecialStatic;
...@@ -28127,7 +28124,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -28127,7 +28124,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
28127 }28124 }
28128 case ContainerLayoutExtern: {28125 case ContainerLayoutExtern: {
28129 size_t src_field_count = val->type->data.structure.src_field_count;28126 size_t src_field_count = val->type->data.structure.src_field_count;
28130 val->data.x_struct.fields = alloc_const_vals_ptrs(src_field_count);28127 val->data.x_struct.fields = alloc_const_vals_ptrs(codegen, src_field_count);
28131 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {28128 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {
28132 ZigValue *field_val = val->data.x_struct.fields[field_i];28129 ZigValue *field_val = val->data.x_struct.fields[field_i];
28133 field_val->special = ConstValSpecialStatic;28130 field_val->special = ConstValSpecialStatic;
...@@ -28144,7 +28141,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -28144,7 +28141,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
28144 }28141 }
28145 case ContainerLayoutPacked: {28142 case ContainerLayoutPacked: {
28146 size_t src_field_count = val->type->data.structure.src_field_count;28143 size_t src_field_count = val->type->data.structure.src_field_count;
28147 val->data.x_struct.fields = alloc_const_vals_ptrs(src_field_count);28144 val->data.x_struct.fields = alloc_const_vals_ptrs(codegen, src_field_count);
28148 size_t gen_field_count = val->type->data.structure.gen_field_count;28145 size_t gen_field_count = val->type->data.structure.gen_field_count;
28149 size_t gen_i = 0;28146 size_t gen_i = 0;
28150 size_t src_i = 0;28147 size_t src_i = 0;
...@@ -28156,7 +28153,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -28156,7 +28153,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
28156 while (gen_i < gen_field_count) {28153 while (gen_i < gen_field_count) {
28157 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];28154 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
28158 if (big_int_byte_count > child_buf_len) {28155 if (big_int_byte_count > child_buf_len) {
28159 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);28156 child_buf = heap::c_allocator.allocate_nonzero<uint8_t>(big_int_byte_count);
28160 child_buf_len = big_int_byte_count;28157 child_buf_len = big_int_byte_count;
28161 }28158 }
28162 BigInt big_int;28159 BigInt big_int;
...@@ -28266,7 +28263,7 @@ static IrInstGen *ir_analyze_bit_cast(IrAnalyze *ira, IrInst* source_instr, IrIn...@@ -28266,7 +28263,7 @@ static IrInstGen *ir_analyze_bit_cast(IrAnalyze *ira, IrInst* source_instr, IrIn
28266 return ira->codegen->invalid_inst_gen;28263 return ira->codegen->invalid_inst_gen;
2826728264
28268 IrInstGen *result = ir_const(ira, source_instr, dest_type);28265 IrInstGen *result = ir_const(ira, source_instr, dest_type);
28269 uint8_t *buf = allocate_nonzero<uint8_t>(src_size_bytes);28266 uint8_t *buf = heap::c_allocator.allocate_nonzero<uint8_t>(src_size_bytes);
28270 buf_write_value_bytes(ira->codegen, buf, val);28267 buf_write_value_bytes(ira->codegen, buf, val);
28271 if ((err = buf_read_value_bytes(ira, ira->codegen, source_instr->source_node, buf, result->value)))28268 if ((err = buf_read_value_bytes(ira, ira->codegen, source_instr->source_node, buf, result->value)))
28272 return ira->codegen->invalid_inst_gen;28269 return ira->codegen->invalid_inst_gen;
...@@ -28408,7 +28405,7 @@ static IrInstGen *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstSrcPtrTy...@@ -28408,7 +28405,7 @@ static IrInstGen *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstSrcPtrTy
28408 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);28405 IrInstGen *result = ir_const(ira, &instruction->base.base, ira->codegen->builtin_types.entry_type);
28409 result->value->special = ConstValSpecialLazy;28406 result->value->special = ConstValSpecialLazy;
2841028407
28411 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1, "LazyValuePtrType");28408 LazyValuePtrType *lazy_ptr_type = heap::c_allocator.create<LazyValuePtrType>();
28412 lazy_ptr_type->ira = ira; ira_ref(ira);28409 lazy_ptr_type->ira = ira; ira_ref(ira);
28413 result->value->data.x_lazy = &lazy_ptr_type->base;28410 result->value->data.x_lazy = &lazy_ptr_type->base;
28414 lazy_ptr_type->base.id = LazyValueIdPtrType;28411 lazy_ptr_type->base.id = LazyValueIdPtrType;
...@@ -29107,11 +29104,11 @@ static IrInstGen *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstSrcBswap *i...@@ -29107,11 +29104,11 @@ static IrInstGen *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstSrcBswap *i
29107 return ir_const_undef(ira, &instruction->base.base, op_type);29104 return ir_const_undef(ira, &instruction->base.base, op_type);
2910829105
29109 IrInstGen *result = ir_const(ira, &instruction->base.base, op_type);29106 IrInstGen *result = ir_const(ira, &instruction->base.base, op_type);
29110 size_t buf_size = int_type->data.integral.bit_count / 8;29107 const size_t buf_size = int_type->data.integral.bit_count / 8;
29111 uint8_t *buf = allocate_nonzero<uint8_t>(buf_size);29108 uint8_t *buf = heap::c_allocator.allocate_nonzero<uint8_t>(buf_size);
29112 if (is_vector) {29109 if (is_vector) {
29113 expand_undef_array(ira->codegen, val);29110 expand_undef_array(ira->codegen, val);
29114 result->value->data.x_array.data.s_none.elements = create_const_vals(op_type->data.vector.len);29111 result->value->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(op_type->data.vector.len);
29115 for (unsigned i = 0; i < op_type->data.vector.len; i += 1) {29112 for (unsigned i = 0; i < op_type->data.vector.len; i += 1) {
29116 ZigValue *op_elem_val = &val->data.x_array.data.s_none.elements[i];29113 ZigValue *op_elem_val = &val->data.x_array.data.s_none.elements[i];
29117 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, instruction->base.base.source_node,29114 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, instruction->base.base.source_node,
...@@ -29135,7 +29132,7 @@ static IrInstGen *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstSrcBswap *i...@@ -29135,7 +29132,7 @@ static IrInstGen *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstSrcBswap *i
29135 bigint_read_twos_complement(&result->value->data.x_bigint, buf, int_type->data.integral.bit_count, false,29132 bigint_read_twos_complement(&result->value->data.x_bigint, buf, int_type->data.integral.bit_count, false,
29136 int_type->data.integral.is_signed);29133 int_type->data.integral.is_signed);
29137 }29134 }
29138 free(buf);29135 heap::c_allocator.deallocate(buf, buf_size);
29139 return result;29136 return result;
29140 }29137 }
2914129138
...@@ -29167,8 +29164,8 @@ static IrInstGen *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstSrcBi...@@ -29167,8 +29164,8 @@ static IrInstGen *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstSrcBi
29167 IrInstGen *result = ir_const(ira, &instruction->base.base, int_type);29164 IrInstGen *result = ir_const(ira, &instruction->base.base, int_type);
29168 size_t num_bits = int_type->data.integral.bit_count;29165 size_t num_bits = int_type->data.integral.bit_count;
29169 size_t buf_size = (num_bits + 7) / 8;29166 size_t buf_size = (num_bits + 7) / 8;
29170 uint8_t *comptime_buf = allocate_nonzero<uint8_t>(buf_size);29167 uint8_t *comptime_buf = heap::c_allocator.allocate_nonzero<uint8_t>(buf_size);
29171 uint8_t *result_buf = allocate_nonzero<uint8_t>(buf_size);29168 uint8_t *result_buf = heap::c_allocator.allocate_nonzero<uint8_t>(buf_size);
29172 memset(comptime_buf,0,buf_size);29169 memset(comptime_buf,0,buf_size);
29173 memset(result_buf,0,buf_size);29170 memset(result_buf,0,buf_size);
2917429171
...@@ -29854,7 +29851,7 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen...@@ -29854,7 +29851,7 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutableSrc *old_exec, IrExecutableGen
29854 assert(old_exec->first_err_trace_msg == nullptr);29851 assert(old_exec->first_err_trace_msg == nullptr);
29855 assert(expected_type == nullptr || !type_is_invalid(expected_type));29852 assert(expected_type == nullptr || !type_is_invalid(expected_type));
2985629853
29857 IrAnalyze *ira = allocate<IrAnalyze>(1, "IrAnalyze");29854 IrAnalyze *ira = heap::c_allocator.create<IrAnalyze>();
29858 ira->ref_count = 1;29855 ira->ref_count = 1;
29859 old_exec->analysis = ira;29856 old_exec->analysis = ira;
29860 ira->codegen = codegen;29857 ira->codegen = codegen;
src/ir.hpp-2
...@@ -37,6 +37,4 @@ ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_va...@@ -37,6 +37,4 @@ ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_va
37void dbg_ir_break(const char *src_file, uint32_t line);37void dbg_ir_break(const char *src_file, uint32_t line);
38void dbg_ir_clear(void);38void dbg_ir_clear(void);
3939
40void destroy_instruction_gen(IrInstGen *inst);
41
42#endif40#endif
src/link.cpp+19-26
...@@ -650,7 +650,7 @@ static const char *build_libunwind(CodeGen *parent, Stage2ProgressNode *progress...@@ -650,7 +650,7 @@ static const char *build_libunwind(CodeGen *parent, Stage2ProgressNode *progress
650 };650 };
651 ZigList<CFile *> c_source_files = {0};651 ZigList<CFile *> c_source_files = {0};
652 for (size_t i = 0; i < array_length(unwind_src); i += 1) {652 for (size_t i = 0; i < array_length(unwind_src); i += 1) {
653 CFile *c_file = allocate<CFile>(1);653 CFile *c_file = heap::c_allocator.create<CFile>();
654 c_file->source_path = path_from_libunwind(parent, unwind_src[i].path);654 c_file->source_path = path_from_libunwind(parent, unwind_src[i].path);
655 switch (unwind_src[i].kind) {655 switch (unwind_src[i].kind) {
656 case SrcC:656 case SrcC:
...@@ -1111,7 +1111,7 @@ static const char *build_musl(CodeGen *parent, Stage2ProgressNode *progress_node...@@ -1111,7 +1111,7 @@ static const char *build_musl(CodeGen *parent, Stage2ProgressNode *progress_node
1111 Buf *full_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "%s",1111 Buf *full_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "%s",
1112 buf_ptr(parent->zig_lib_dir), buf_ptr(src_file));1112 buf_ptr(parent->zig_lib_dir), buf_ptr(src_file));
11131113
1114 CFile *c_file = allocate<CFile>(1);1114 CFile *c_file = heap::c_allocator.create<CFile>();
1115 c_file->source_path = buf_ptr(full_path);1115 c_file->source_path = buf_ptr(full_path);
11161116
1117 musl_add_cc_args(parent, c_file, src_kind == MuslSrcO3);1117 musl_add_cc_args(parent, c_file, src_kind == MuslSrcO3);
...@@ -1127,7 +1127,7 @@ static const char *build_musl(CodeGen *parent, Stage2ProgressNode *progress_node...@@ -1127,7 +1127,7 @@ static const char *build_musl(CodeGen *parent, Stage2ProgressNode *progress_node
1127}1127}
11281128
1129static void add_msvcrt_os_dep(CodeGen *parent, CodeGen *child_gen, const char *src_path) {1129static void add_msvcrt_os_dep(CodeGen *parent, CodeGen *child_gen, const char *src_path) {
1130 CFile *c_file = allocate<CFile>(1);1130 CFile *c_file = heap::c_allocator.create<CFile>();
1131 c_file->source_path = buf_ptr(buf_sprintf("%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "%s",1131 c_file->source_path = buf_ptr(buf_sprintf("%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "%s",
1132 buf_ptr(parent->zig_lib_dir), src_path));1132 buf_ptr(parent->zig_lib_dir), src_path));
1133 c_file->args.append("-DHAVE_CONFIG_H");1133 c_file->args.append("-DHAVE_CONFIG_H");
...@@ -1151,7 +1151,7 @@ static void add_msvcrt_os_dep(CodeGen *parent, CodeGen *child_gen, const char *s...@@ -1151,7 +1151,7 @@ static void add_msvcrt_os_dep(CodeGen *parent, CodeGen *child_gen, const char *s
1151}1151}
11521152
1153static void add_mingwex_os_dep(CodeGen *parent, CodeGen *child_gen, const char *src_path) {1153static void add_mingwex_os_dep(CodeGen *parent, CodeGen *child_gen, const char *src_path) {
1154 CFile *c_file = allocate<CFile>(1);1154 CFile *c_file = heap::c_allocator.create<CFile>();
1155 c_file->source_path = buf_ptr(buf_sprintf("%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "%s",1155 c_file->source_path = buf_ptr(buf_sprintf("%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "%s",
1156 buf_ptr(parent->zig_lib_dir), src_path));1156 buf_ptr(parent->zig_lib_dir), src_path));
1157 c_file->args.append("-DHAVE_CONFIG_H");1157 c_file->args.append("-DHAVE_CONFIG_H");
...@@ -1178,7 +1178,7 @@ static void add_mingwex_os_dep(CodeGen *parent, CodeGen *child_gen, const char *...@@ -1178,7 +1178,7 @@ static void add_mingwex_os_dep(CodeGen *parent, CodeGen *child_gen, const char *
1178static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2ProgressNode *progress_node) {1178static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2ProgressNode *progress_node) {
1179 if (parent->libc == nullptr && parent->zig_target->os == OsWindows) {1179 if (parent->libc == nullptr && parent->zig_target->os == OsWindows) {
1180 if (strcmp(file, "crt2.o") == 0) {1180 if (strcmp(file, "crt2.o") == 0) {
1181 CFile *c_file = allocate<CFile>(1);1181 CFile *c_file = heap::c_allocator.create<CFile>();
1182 c_file->source_path = buf_ptr(buf_sprintf(1182 c_file->source_path = buf_ptr(buf_sprintf(
1183 "%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "crt" OS_SEP "crtexe.c", buf_ptr(parent->zig_lib_dir)));1183 "%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "crt" OS_SEP "crtexe.c", buf_ptr(parent->zig_lib_dir)));
1184 mingw_add_cc_args(parent, c_file);1184 mingw_add_cc_args(parent, c_file);
...@@ -1190,7 +1190,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1190,7 +1190,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1190 //c_file->args.append("-DWPRFLAG=1");1190 //c_file->args.append("-DWPRFLAG=1");
1191 return build_libc_object(parent, "crt2", c_file, progress_node);1191 return build_libc_object(parent, "crt2", c_file, progress_node);
1192 } else if (strcmp(file, "dllcrt2.o") == 0) {1192 } else if (strcmp(file, "dllcrt2.o") == 0) {
1193 CFile *c_file = allocate<CFile>(1);1193 CFile *c_file = heap::c_allocator.create<CFile>();
1194 c_file->source_path = buf_ptr(buf_sprintf(1194 c_file->source_path = buf_ptr(buf_sprintf(
1195 "%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "crt" OS_SEP "crtdll.c", buf_ptr(parent->zig_lib_dir)));1195 "%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "crt" OS_SEP "crtdll.c", buf_ptr(parent->zig_lib_dir)));
1196 mingw_add_cc_args(parent, c_file);1196 mingw_add_cc_args(parent, c_file);
...@@ -1231,7 +1231,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1231,7 +1231,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1231 "mingw" OS_SEP "crt" OS_SEP "cxa_atexit.c",1231 "mingw" OS_SEP "crt" OS_SEP "cxa_atexit.c",
1232 };1232 };
1233 for (size_t i = 0; i < array_length(deps); i += 1) {1233 for (size_t i = 0; i < array_length(deps); i += 1) {
1234 CFile *c_file = allocate<CFile>(1);1234 CFile *c_file = heap::c_allocator.create<CFile>();
1235 c_file->source_path = path_from_libc(parent, deps[i]);1235 c_file->source_path = path_from_libc(parent, deps[i]);
1236 c_file->args.append("-DHAVE_CONFIG_H");1236 c_file->args.append("-DHAVE_CONFIG_H");
1237 c_file->args.append("-D_SYSCRT=1");1237 c_file->args.append("-D_SYSCRT=1");
...@@ -1301,7 +1301,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1301,7 +1301,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1301 }1301 }
1302 } else if (parent->libc == nullptr && target_is_glibc(parent->zig_target)) {1302 } else if (parent->libc == nullptr && target_is_glibc(parent->zig_target)) {
1303 if (strcmp(file, "crti.o") == 0) {1303 if (strcmp(file, "crti.o") == 0) {
1304 CFile *c_file = allocate<CFile>(1);1304 CFile *c_file = heap::c_allocator.create<CFile>();
1305 c_file->source_path = glibc_start_asm_path(parent, "crti.S");1305 c_file->source_path = glibc_start_asm_path(parent, "crti.S");
1306 glibc_add_include_dirs(parent, c_file);1306 glibc_add_include_dirs(parent, c_file);
1307 c_file->args.append("-D_LIBC_REENTRANT");1307 c_file->args.append("-D_LIBC_REENTRANT");
...@@ -1317,7 +1317,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1317,7 +1317,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1317 c_file->args.append("-Wa,--noexecstack");1317 c_file->args.append("-Wa,--noexecstack");
1318 return build_libc_object(parent, "crti", c_file, progress_node);1318 return build_libc_object(parent, "crti", c_file, progress_node);
1319 } else if (strcmp(file, "crtn.o") == 0) {1319 } else if (strcmp(file, "crtn.o") == 0) {
1320 CFile *c_file = allocate<CFile>(1);1320 CFile *c_file = heap::c_allocator.create<CFile>();
1321 c_file->source_path = glibc_start_asm_path(parent, "crtn.S");1321 c_file->source_path = glibc_start_asm_path(parent, "crtn.S");
1322 glibc_add_include_dirs(parent, c_file);1322 glibc_add_include_dirs(parent, c_file);
1323 c_file->args.append("-D_LIBC_REENTRANT");1323 c_file->args.append("-D_LIBC_REENTRANT");
...@@ -1328,7 +1328,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1328,7 +1328,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1328 c_file->args.append("-Wa,--noexecstack");1328 c_file->args.append("-Wa,--noexecstack");
1329 return build_libc_object(parent, "crtn", c_file, progress_node);1329 return build_libc_object(parent, "crtn", c_file, progress_node);
1330 } else if (strcmp(file, "start.os") == 0) {1330 } else if (strcmp(file, "start.os") == 0) {
1331 CFile *c_file = allocate<CFile>(1);1331 CFile *c_file = heap::c_allocator.create<CFile>();
1332 c_file->source_path = glibc_start_asm_path(parent, "start.S");1332 c_file->source_path = glibc_start_asm_path(parent, "start.S");
1333 glibc_add_include_dirs(parent, c_file);1333 glibc_add_include_dirs(parent, c_file);
1334 c_file->args.append("-D_LIBC_REENTRANT");1334 c_file->args.append("-D_LIBC_REENTRANT");
...@@ -1346,7 +1346,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1346,7 +1346,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1346 c_file->args.append("-Wa,--noexecstack");1346 c_file->args.append("-Wa,--noexecstack");
1347 return build_libc_object(parent, "start", c_file, progress_node);1347 return build_libc_object(parent, "start", c_file, progress_node);
1348 } else if (strcmp(file, "abi-note.o") == 0) {1348 } else if (strcmp(file, "abi-note.o") == 0) {
1349 CFile *c_file = allocate<CFile>(1);1349 CFile *c_file = heap::c_allocator.create<CFile>();
1350 c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "csu" OS_SEP "abi-note.S");1350 c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "csu" OS_SEP "abi-note.S");
1351 c_file->args.append("-I");1351 c_file->args.append("-I");
1352 c_file->args.append(path_from_libc(parent, "glibc" OS_SEP "csu"));1352 c_file->args.append(path_from_libc(parent, "glibc" OS_SEP "csu"));
...@@ -1369,7 +1369,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1369,7 +1369,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1369 } else if (strcmp(file, "libc_nonshared.a") == 0) {1369 } else if (strcmp(file, "libc_nonshared.a") == 0) {
1370 CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr, "c_nonshared", progress_node);1370 CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr, "c_nonshared", progress_node);
1371 {1371 {
1372 CFile *c_file = allocate<CFile>(1);1372 CFile *c_file = heap::c_allocator.create<CFile>();
1373 c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "csu" OS_SEP "elf-init.c");1373 c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "csu" OS_SEP "elf-init.c");
1374 c_file->args.append("-std=gnu11");1374 c_file->args.append("-std=gnu11");
1375 c_file->args.append("-fgnu89-inline");1375 c_file->args.append("-fgnu89-inline");
...@@ -1419,7 +1419,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1419,7 +1419,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1419 {"stack_chk_fail_local", "glibc" OS_SEP "debug" OS_SEP "stack_chk_fail_local.c"},1419 {"stack_chk_fail_local", "glibc" OS_SEP "debug" OS_SEP "stack_chk_fail_local.c"},
1420 };1420 };
1421 for (size_t i = 0; i < array_length(deps); i += 1) {1421 for (size_t i = 0; i < array_length(deps); i += 1) {
1422 CFile *c_file = allocate<CFile>(1);1422 CFile *c_file = heap::c_allocator.create<CFile>();
1423 c_file->source_path = path_from_libc(parent, deps[i].path);1423 c_file->source_path = path_from_libc(parent, deps[i].path);
1424 c_file->args.append("-std=gnu11");1424 c_file->args.append("-std=gnu11");
1425 c_file->args.append("-fgnu89-inline");1425 c_file->args.append("-fgnu89-inline");
...@@ -1451,26 +1451,26 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1451,26 +1451,26 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1451 }1451 }
1452 } else if (parent->libc == nullptr && target_is_musl(parent->zig_target)) {1452 } else if (parent->libc == nullptr && target_is_musl(parent->zig_target)) {
1453 if (strcmp(file, "crti.o") == 0) {1453 if (strcmp(file, "crti.o") == 0) {
1454 CFile *c_file = allocate<CFile>(1);1454 CFile *c_file = heap::c_allocator.create<CFile>();
1455 c_file->source_path = musl_start_asm_path(parent, "crti.s");1455 c_file->source_path = musl_start_asm_path(parent, "crti.s");
1456 musl_add_cc_args(parent, c_file, false);1456 musl_add_cc_args(parent, c_file, false);
1457 c_file->args.append("-Qunused-arguments");1457 c_file->args.append("-Qunused-arguments");
1458 return build_libc_object(parent, "crti", c_file, progress_node);1458 return build_libc_object(parent, "crti", c_file, progress_node);
1459 } else if (strcmp(file, "crtn.o") == 0) {1459 } else if (strcmp(file, "crtn.o") == 0) {
1460 CFile *c_file = allocate<CFile>(1);1460 CFile *c_file = heap::c_allocator.create<CFile>();
1461 c_file->source_path = musl_start_asm_path(parent, "crtn.s");1461 c_file->source_path = musl_start_asm_path(parent, "crtn.s");
1462 c_file->args.append("-Qunused-arguments");1462 c_file->args.append("-Qunused-arguments");
1463 musl_add_cc_args(parent, c_file, false);1463 musl_add_cc_args(parent, c_file, false);
1464 return build_libc_object(parent, "crtn", c_file, progress_node);1464 return build_libc_object(parent, "crtn", c_file, progress_node);
1465 } else if (strcmp(file, "crt1.o") == 0) {1465 } else if (strcmp(file, "crt1.o") == 0) {
1466 CFile *c_file = allocate<CFile>(1);1466 CFile *c_file = heap::c_allocator.create<CFile>();
1467 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "crt1.c");1467 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "crt1.c");
1468 musl_add_cc_args(parent, c_file, false);1468 musl_add_cc_args(parent, c_file, false);
1469 c_file->args.append("-fno-stack-protector");1469 c_file->args.append("-fno-stack-protector");
1470 c_file->args.append("-DCRT");1470 c_file->args.append("-DCRT");
1471 return build_libc_object(parent, "crt1", c_file, progress_node);1471 return build_libc_object(parent, "crt1", c_file, progress_node);
1472 } else if (strcmp(file, "Scrt1.o") == 0) {1472 } else if (strcmp(file, "Scrt1.o") == 0) {
1473 CFile *c_file = allocate<CFile>(1);1473 CFile *c_file = heap::c_allocator.create<CFile>();
1474 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "Scrt1.c");1474 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "Scrt1.c");
1475 musl_add_cc_args(parent, c_file, false);1475 musl_add_cc_args(parent, c_file, false);
1476 c_file->args.append("-fPIC");1476 c_file->args.append("-fPIC");
...@@ -1987,7 +1987,7 @@ static const char *get_def_lib(CodeGen *parent, const char *name, Buf *def_in_fi...@@ -1987,7 +1987,7 @@ static const char *get_def_lib(CodeGen *parent, const char *name, Buf *def_in_fi
1987 Buf *def_include_dir = buf_sprintf("%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "def-include",1987 Buf *def_include_dir = buf_sprintf("%s" OS_SEP "libc" OS_SEP "mingw" OS_SEP "def-include",
1988 buf_ptr(parent->zig_lib_dir));1988 buf_ptr(parent->zig_lib_dir));
19891989
1990 CacheHash *cache_hash = allocate<CacheHash>(1);1990 CacheHash *cache_hash = heap::c_allocator.create<CacheHash>();
1991 cache_init(cache_hash, manifest_dir);1991 cache_init(cache_hash, manifest_dir);
19921992
1993 cache_buf(cache_hash, compiler_id);1993 cache_buf(cache_hash, compiler_id);
...@@ -2372,7 +2372,7 @@ static void construct_linker_job_coff(LinkJob *lj) {...@@ -2372,7 +2372,7 @@ static void construct_linker_job_coff(LinkJob *lj) {
23722372
2373 lj->args.append(get_def_lib(g, name, &lib_path));2373 lj->args.append(get_def_lib(g, name, &lib_path));
23742374
2375 free(name);2375 mem::os::free(name);
2376 }2376 }
2377}2377}
23782378
...@@ -2647,13 +2647,6 @@ void codegen_link(CodeGen *g) {...@@ -2647,13 +2647,6 @@ void codegen_link(CodeGen *g) {
2647 lj.rpath_table.init(4);2647 lj.rpath_table.init(4);
2648 lj.codegen = g;2648 lj.codegen = g;
26492649
2650 if (g->verbose_llvm_ir) {
2651 fprintf(stderr, "\nOptimization:\n");
2652 fprintf(stderr, "---------------\n");
2653 fflush(stderr);
2654 LLVMDumpModule(g->module);
2655 }
2656
2657 if (g->out_type == OutTypeObj) {2650 if (g->out_type == OutTypeObj) {
2658 lj.args.append("-r");2651 lj.args.append("-r");
2659 }2652 }
src/list.hpp+2-4
...@@ -13,7 +13,7 @@...@@ -13,7 +13,7 @@
13template<typename T>13template<typename T>
14struct ZigList {14struct ZigList {
15 void deinit() {15 void deinit() {
16 deallocate(items, capacity);16 heap::c_allocator.deallocate(items, capacity);
17 }17 }
18 void append(const T& item) {18 void append(const T& item) {
19 ensure_capacity(length + 1);19 ensure_capacity(length + 1);
...@@ -70,7 +70,7 @@ struct ZigList {...@@ -70,7 +70,7 @@ struct ZigList {
70 better_capacity = better_capacity * 5 / 2 + 8;70 better_capacity = better_capacity * 5 / 2 + 8;
71 } while (better_capacity < new_capacity);71 } while (better_capacity < new_capacity);
7272
73 items = reallocate_nonzero(items, capacity, better_capacity);73 items = heap::c_allocator.reallocate_nonzero(items, capacity, better_capacity);
74 capacity = better_capacity;74 capacity = better_capacity;
75 }75 }
7676
...@@ -91,5 +91,3 @@ struct ZigList {...@@ -91,5 +91,3 @@ struct ZigList {
91};91};
9292
93#endif93#endif
94
95
src/main.cpp+27-21
...@@ -11,12 +11,14 @@...@@ -11,12 +11,14 @@
11#include "compiler.hpp"11#include "compiler.hpp"
12#include "config.h"12#include "config.h"
13#include "error.hpp"13#include "error.hpp"
14#include "heap.hpp"
14#include "os.hpp"15#include "os.hpp"
15#include "target.hpp"16#include "target.hpp"
16#include "libc_installation.hpp"17#include "libc_installation.hpp"
17#include "userland.h"18#include "userland.h"
18#include "glibc.hpp"19#include "glibc.hpp"
19#include "dump_analysis.hpp"20#include "dump_analysis.hpp"
21#include "mem_profile.hpp"
2022
21#include <stdio.h>23#include <stdio.h>
2224
...@@ -243,21 +245,10 @@ int main_exit(Stage2ProgressNode *root_progress_node, int exit_code) {...@@ -243,21 +245,10 @@ int main_exit(Stage2ProgressNode *root_progress_node, int exit_code) {
243 if (root_progress_node != nullptr) {245 if (root_progress_node != nullptr) {
244 stage2_progress_end(root_progress_node);246 stage2_progress_end(root_progress_node);
245 }247 }
246#ifdef ZIG_ENABLE_MEM_PROFILE
247 if (mem_report) {
248 memprof_dump_stats(stderr);
249 }
250#endif
251 return exit_code;248 return exit_code;
252}249}
253250
254int main(int argc, char **argv) {251static int main0(int argc, char **argv) {
255 stage2_attach_segfault_handler();
256
257#ifdef ZIG_ENABLE_MEM_PROFILE
258 memprof_init();
259#endif
260
261 char *arg0 = argv[0];252 char *arg0 = argv[0];
262 Error err;253 Error err;
263254
...@@ -279,9 +270,6 @@ int main(int argc, char **argv) {...@@ -279,9 +270,6 @@ int main(int argc, char **argv) {
279 return ZigClang_main(argc, argv);270 return ZigClang_main(argc, argv);
280 }271 }
281272
282 // Must be before all os.hpp function calls.
283 os_init();
284
285 if (argc == 2 && strcmp(argv[1], "id") == 0) {273 if (argc == 2 && strcmp(argv[1], "id") == 0) {
286 Buf *compiler_id;274 Buf *compiler_id;
287 if ((err = get_compiler_id(&compiler_id))) {275 if ((err = get_compiler_id(&compiler_id))) {
...@@ -440,7 +428,7 @@ int main(int argc, char **argv) {...@@ -440,7 +428,7 @@ int main(int argc, char **argv) {
440 bool enable_doc_generation = false;428 bool enable_doc_generation = false;
441 bool disable_bin_generation = false;429 bool disable_bin_generation = false;
442 const char *cache_dir = nullptr;430 const char *cache_dir = nullptr;
443 CliPkg *cur_pkg = allocate<CliPkg>(1);431 CliPkg *cur_pkg = heap::c_allocator.create<CliPkg>();
444 BuildMode build_mode = BuildModeDebug;432 BuildMode build_mode = BuildModeDebug;
445 ZigList<const char *> test_exec_args = {0};433 ZigList<const char *> test_exec_args = {0};
446 int runtime_args_start = -1;434 int runtime_args_start = -1;
...@@ -636,6 +624,7 @@ int main(int argc, char **argv) {...@@ -636,6 +624,7 @@ int main(int argc, char **argv) {
636 } else if (strcmp(arg, "-fmem-report") == 0) {624 } else if (strcmp(arg, "-fmem-report") == 0) {
637#ifdef ZIG_ENABLE_MEM_PROFILE625#ifdef ZIG_ENABLE_MEM_PROFILE
638 mem_report = true;626 mem_report = true;
627 mem::report_print = true;
639#else628#else
640 fprintf(stderr, "-fmem-report requires configuring with -DZIG_ENABLE_MEM_PROFILE=ON\n");629 fprintf(stderr, "-fmem-report requires configuring with -DZIG_ENABLE_MEM_PROFILE=ON\n");
641 return print_error_usage(arg0);630 return print_error_usage(arg0);
...@@ -696,7 +685,7 @@ int main(int argc, char **argv) {...@@ -696,7 +685,7 @@ int main(int argc, char **argv) {
696 fprintf(stderr, "Expected 2 arguments after --pkg-begin\n");685 fprintf(stderr, "Expected 2 arguments after --pkg-begin\n");
697 return print_error_usage(arg0);686 return print_error_usage(arg0);
698 }687 }
699 CliPkg *new_cur_pkg = allocate<CliPkg>(1);688 CliPkg *new_cur_pkg = heap::c_allocator.create<CliPkg>();
700 i += 1;689 i += 1;
701 new_cur_pkg->name = argv[i];690 new_cur_pkg->name = argv[i];
702 i += 1;691 i += 1;
...@@ -811,7 +800,7 @@ int main(int argc, char **argv) {...@@ -811,7 +800,7 @@ int main(int argc, char **argv) {
811 } else if (strcmp(arg, "--object") == 0) {800 } else if (strcmp(arg, "--object") == 0) {
812 objects.append(argv[i]);801 objects.append(argv[i]);
813 } else if (strcmp(arg, "--c-source") == 0) {802 } else if (strcmp(arg, "--c-source") == 0) {
814 CFile *c_file = allocate<CFile>(1);803 CFile *c_file = heap::c_allocator.create<CFile>();
815 for (;;) {804 for (;;) {
816 if (argv[i][0] == '-') {805 if (argv[i][0] == '-') {
817 c_file->args.append(argv[i]);806 c_file->args.append(argv[i]);
...@@ -991,7 +980,7 @@ int main(int argc, char **argv) {...@@ -991,7 +980,7 @@ int main(int argc, char **argv) {
991 }980 }
992 }981 }
993 if (target_is_glibc(&target)) {982 if (target_is_glibc(&target)) {
994 target.glibc_version = allocate<ZigGLibCVersion>(1);983 target.glibc_version = heap::c_allocator.create<ZigGLibCVersion>();
995984
996 if (target_glibc != nullptr) {985 if (target_glibc != nullptr) {
997 if ((err = target_parse_glibc_version(target.glibc_version, target_glibc))) {986 if ((err = target_parse_glibc_version(target.glibc_version, target_glibc))) {
...@@ -1139,7 +1128,7 @@ int main(int argc, char **argv) {...@@ -1139,7 +1128,7 @@ int main(int argc, char **argv) {
1139 }1128 }
1140 ZigLibCInstallation *libc = nullptr;1129 ZigLibCInstallation *libc = nullptr;
1141 if (libc_txt != nullptr) {1130 if (libc_txt != nullptr) {
1142 libc = allocate<ZigLibCInstallation>(1);1131 libc = heap::c_allocator.create<ZigLibCInstallation>();
1143 if ((err = zig_libc_parse(libc, buf_create_from_str(libc_txt), &target, true))) {1132 if ((err = zig_libc_parse(libc, buf_create_from_str(libc_txt), &target, true))) {
1144 fprintf(stderr, "Unable to parse --libc text file: %s\n", err_str(err));1133 fprintf(stderr, "Unable to parse --libc text file: %s\n", err_str(err));
1145 return main_exit(root_progress_node, EXIT_FAILURE);1134 return main_exit(root_progress_node, EXIT_FAILURE);
...@@ -1270,7 +1259,8 @@ int main(int argc, char **argv) {...@@ -1270,7 +1259,8 @@ int main(int argc, char **argv) {
12701259
1271 if (cmd == CmdRun) {1260 if (cmd == CmdRun) {
1272#ifdef ZIG_ENABLE_MEM_PROFILE1261#ifdef ZIG_ENABLE_MEM_PROFILE
1273 memprof_dump_stats(stderr);1262 if (mem::report_print)
1263 mem::print_report();
1274#endif1264#endif
12751265
1276 const char *exec_path = buf_ptr(&g->output_file_path);1266 const char *exec_path = buf_ptr(&g->output_file_path);
...@@ -1385,4 +1375,20 @@ int main(int argc, char **argv) {...@@ -1385,4 +1375,20 @@ int main(int argc, char **argv) {
1385 case CmdNone:1375 case CmdNone:
1386 return print_full_usage(arg0, stderr, EXIT_FAILURE);1376 return print_full_usage(arg0, stderr, EXIT_FAILURE);
1387 }1377 }
1378 zig_unreachable();
1379}
1380
1381int main(int argc, char **argv) {
1382 stage2_attach_segfault_handler();
1383 os_init();
1384 mem::init();
1385
1386 auto result = main0(argc, argv);
1387
1388#ifdef ZIG_ENABLE_MEM_PROFILE
1389 if (mem::report_print)
1390 mem::intern_counters.print_report();
1391#endif
1392 mem::deinit();
1393 return result;
1388}1394}
src/mem.cpp created+37
...@@ -0,0 +1,37 @@
1/*
2 * Copyright (c) 2020 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#include "config.h"
9#include "mem.hpp"
10#include "mem_profile.hpp"
11#include "heap.hpp"
12
13namespace mem {
14
15void init() {
16 heap::bootstrap_allocator_state.init("heap::bootstrap_allocator");
17 heap::c_allocator_state.init("heap::c_allocator");
18}
19
20void deinit() {
21 heap::c_allocator_state.deinit();
22 heap::bootstrap_allocator_state.deinit();
23}
24
25#ifdef ZIG_ENABLE_MEM_PROFILE
26void print_report(FILE *file) {
27 heap::c_allocator_state.print_report(file);
28 intern_counters.print_report(file);
29}
30#endif
31
32#ifdef ZIG_ENABLE_MEM_PROFILE
33bool report_print = false;
34FILE *report_file{nullptr};
35#endif
36
37} // namespace mem
src/mem.hpp created+149
...@@ -0,0 +1,149 @@
1/*
2 * Copyright (c) 2020 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_MEM_HPP
9#define ZIG_MEM_HPP
10
11#include <stdint.h>
12#include <stdio.h>
13#include <stdlib.h>
14
15#include "config.h"
16#include "util_base.hpp"
17#include "mem_type_info.hpp"
18
19//
20// -- Memory Allocation General Notes --
21//
22// `heap::c_allocator` is the preferred general allocator.
23//
24// `heap::bootstrap_allocator` is an implementation detail for use
25// by allocators themselves when incidental heap may be required for
26// profiling and statistics. It breaks the infinite recursion cycle.
27//
28// `mem::os` contains a raw wrapper for system malloc API used in
29// preference to calling ::{malloc, free, calloc, realloc} directly.
30// This isolates usage and helps with audits:
31//
32// mem::os::malloc
33// mem::os::free
34// mem::os::calloc
35// mem::os::realloc
36//
37namespace mem {
38
39// initialize mem module before any use
40void init();
41
42// deinitialize mem module to free memory and print report
43void deinit();
44
45// isolate system/libc allocators
46namespace os {
47
48ATTRIBUTE_RETURNS_NOALIAS
49inline void *malloc(size_t size) {
50#ifndef NDEBUG
51 // make behavior when size == 0 portable
52 if (size == 0)
53 return nullptr;
54#endif
55 auto ptr = ::malloc(size);
56 if (ptr == nullptr)
57 zig_panic("allocation failed");
58 return ptr;
59}
60
61inline void free(void *ptr) {
62 ::free(ptr);
63}
64
65ATTRIBUTE_RETURNS_NOALIAS
66inline void *calloc(size_t count, size_t size) {
67#ifndef NDEBUG
68 // make behavior when size == 0 portable
69 if (count == 0 || size == 0)
70 return nullptr;
71#endif
72 auto ptr = ::calloc(count, size);
73 if (ptr == nullptr)
74 zig_panic("allocation failed");
75 return ptr;
76}
77
78inline void *realloc(void *old_ptr, size_t size) {
79#ifndef NDEBUG
80 // make behavior when size == 0 portable
81 if (old_ptr == nullptr && size == 0)
82 return nullptr;
83#endif
84 auto ptr = ::realloc(old_ptr, size);
85 if (ptr == nullptr)
86 zig_panic("allocation failed");
87 return ptr;
88}
89
90} // namespace os
91
92struct Allocator {
93 virtual void destruct(Allocator *allocator) = 0;
94
95 template <typename T> ATTRIBUTE_RETURNS_NOALIAS
96 T *allocate(size_t count) {
97 return reinterpret_cast<T *>(this->internal_allocate(TypeInfo::make<T>(), count));
98 }
99
100 template <typename T> ATTRIBUTE_RETURNS_NOALIAS
101 T *allocate_nonzero(size_t count) {
102 return reinterpret_cast<T *>(this->internal_allocate_nonzero(TypeInfo::make<T>(), count));
103 }
104
105 template <typename T>
106 T *reallocate(T *old_ptr, size_t old_count, size_t new_count) {
107 return reinterpret_cast<T *>(this->internal_reallocate(TypeInfo::make<T>(), old_ptr, old_count, new_count));
108 }
109
110 template <typename T>
111 T *reallocate_nonzero(T *old_ptr, size_t old_count, size_t new_count) {
112 return reinterpret_cast<T *>(this->internal_reallocate_nonzero(TypeInfo::make<T>(), old_ptr, old_count, new_count));
113 }
114
115 template<typename T>
116 void deallocate(T *ptr, size_t count) {
117 this->internal_deallocate(TypeInfo::make<T>(), ptr, count);
118 }
119
120 template<typename T>
121 T *create() {
122 return reinterpret_cast<T *>(this->internal_allocate(TypeInfo::make<T>(), 1));
123 }
124
125 template<typename T>
126 void destroy(T *ptr) {
127 this->internal_deallocate(TypeInfo::make<T>(), ptr, 1);
128 }
129
130protected:
131 ATTRIBUTE_RETURNS_NOALIAS virtual void *internal_allocate(const TypeInfo &info, size_t count) = 0;
132 ATTRIBUTE_RETURNS_NOALIAS virtual void *internal_allocate_nonzero(const TypeInfo &info, size_t count) = 0;
133 virtual void *internal_reallocate(const TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) = 0;
134 virtual void *internal_reallocate_nonzero(const TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) = 0;
135 virtual void internal_deallocate(const TypeInfo &info, void *ptr, size_t count) = 0;
136};
137
138#ifdef ZIG_ENABLE_MEM_PROFILE
139void print_report(FILE *file = nullptr);
140
141// global memory report flag
142extern bool report_print;
143// global memory report default destination
144extern FILE *report_file;
145#endif
146
147} // namespace mem
148
149#endif
src/mem_hash_map.hpp created+244
...@@ -0,0 +1,244 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_MEM_HASH_MAP_HPP
9#define ZIG_MEM_HASH_MAP_HPP
10
11#include "mem.hpp"
12
13namespace mem {
14
15template<typename K, typename V, uint32_t (*HashFunction)(K key), bool (*EqualFn)(K a, K b)>
16class HashMap {
17public:
18 void init(Allocator& allocator, int capacity) {
19 init_capacity(allocator, capacity);
20 }
21 void deinit(Allocator& allocator) {
22 allocator.deallocate(_entries, _capacity);
23 }
24
25 struct Entry {
26 K key;
27 V value;
28 bool used;
29 int distance_from_start_index;
30 };
31
32 void clear() {
33 for (int i = 0; i < _capacity; i += 1) {
34 _entries[i].used = false;
35 }
36 _size = 0;
37 _max_distance_from_start_index = 0;
38 _modification_count += 1;
39 }
40
41 int size() const {
42 return _size;
43 }
44
45 void put(Allocator& allocator, const K &key, const V &value) {
46 _modification_count += 1;
47 internal_put(key, value);
48
49 // if we get too full (60%), double the capacity
50 if (_size * 5 >= _capacity * 3) {
51 Entry *old_entries = _entries;
52 int old_capacity = _capacity;
53 init_capacity(allocator, _capacity * 2);
54 // dump all of the old elements into the new table
55 for (int i = 0; i < old_capacity; i += 1) {
56 Entry *old_entry = &old_entries[i];
57 if (old_entry->used)
58 internal_put(old_entry->key, old_entry->value);
59 }
60 allocator.deallocate(old_entries, old_capacity);
61 }
62 }
63
64 Entry *put_unique(Allocator& allocator, const K &key, const V &value) {
65 // TODO make this more efficient
66 Entry *entry = internal_get(key);
67 if (entry)
68 return entry;
69 put(allocator, key, value);
70 return nullptr;
71 }
72
73 const V &get(const K &key) const {
74 Entry *entry = internal_get(key);
75 if (!entry)
76 zig_panic("key not found");
77 return entry->value;
78 }
79
80 Entry *maybe_get(const K &key) const {
81 return internal_get(key);
82 }
83
84 void maybe_remove(const K &key) {
85 if (maybe_get(key)) {
86 remove(key);
87 }
88 }
89
90 void remove(const K &key) {
91 _modification_count += 1;
92 int start_index = key_to_index(key);
93 for (int roll_over = 0; roll_over <= _max_distance_from_start_index; roll_over += 1) {
94 int index = (start_index + roll_over) % _capacity;
95 Entry *entry = &_entries[index];
96
97 if (!entry->used)
98 zig_panic("key not found");
99
100 if (!EqualFn(entry->key, key))
101 continue;
102
103 for (; roll_over < _capacity; roll_over += 1) {
104 int next_index = (start_index + roll_over + 1) % _capacity;
105 Entry *next_entry = &_entries[next_index];
106 if (!next_entry->used || next_entry->distance_from_start_index == 0) {
107 entry->used = false;
108 _size -= 1;
109 return;
110 }
111 *entry = *next_entry;
112 entry->distance_from_start_index -= 1;
113 entry = next_entry;
114 }
115 zig_panic("shifting everything in the table");
116 }
117 zig_panic("key not found");
118 }
119
120 class Iterator {
121 public:
122 Entry *next() {
123 if (_inital_modification_count != _table->_modification_count)
124 zig_panic("concurrent modification");
125 if (_count >= _table->size())
126 return NULL;
127 for (; _index < _table->_capacity; _index += 1) {
128 Entry *entry = &_table->_entries[_index];
129 if (entry->used) {
130 _index += 1;
131 _count += 1;
132 return entry;
133 }
134 }
135 zig_panic("no next item");
136 }
137
138 private:
139 const HashMap * _table;
140 // how many items have we returned
141 int _count = 0;
142 // iterator through the entry array
143 int _index = 0;
144 // used to detect concurrent modification
145 uint32_t _inital_modification_count;
146 Iterator(const HashMap * table) :
147 _table(table), _inital_modification_count(table->_modification_count) {
148 }
149 friend HashMap;
150 };
151
152 // you must not modify the underlying HashMap while this iterator is still in use
153 Iterator entry_iterator() const {
154 return Iterator(this);
155 }
156
157private:
158 Entry *_entries;
159 int _capacity;
160 int _size;
161 int _max_distance_from_start_index;
162 // this is used to detect bugs where a hashtable is edited while an iterator is running.
163 uint32_t _modification_count;
164
165 void init_capacity(Allocator& allocator, int capacity) {
166 _capacity = capacity;
167 _entries = allocator.allocate<Entry>(_capacity);
168 _size = 0;
169 _max_distance_from_start_index = 0;
170 for (int i = 0; i < _capacity; i += 1) {
171 _entries[i].used = false;
172 }
173 }
174
175 void internal_put(K key, V value) {
176 int start_index = key_to_index(key);
177 for (int roll_over = 0, distance_from_start_index = 0;
178 roll_over < _capacity; roll_over += 1, distance_from_start_index += 1)
179 {
180 int index = (start_index + roll_over) % _capacity;
181 Entry *entry = &_entries[index];
182
183 if (entry->used && !EqualFn(entry->key, key)) {
184 if (entry->distance_from_start_index < distance_from_start_index) {
185 // robin hood to the rescue
186 Entry tmp = *entry;
187 if (distance_from_start_index > _max_distance_from_start_index)
188 _max_distance_from_start_index = distance_from_start_index;
189 *entry = {
190 key,
191 value,
192 true,
193 distance_from_start_index,
194 };
195 key = tmp.key;
196 value = tmp.value;
197 distance_from_start_index = tmp.distance_from_start_index;
198 }
199 continue;
200 }
201
202 if (!entry->used) {
203 // adding an entry. otherwise overwriting old value with
204 // same key
205 _size += 1;
206 }
207
208 if (distance_from_start_index > _max_distance_from_start_index)
209 _max_distance_from_start_index = distance_from_start_index;
210 *entry = {
211 key,
212 value,
213 true,
214 distance_from_start_index,
215 };
216 return;
217 }
218 zig_panic("put into a full HashMap");
219 }
220
221
222 Entry *internal_get(const K &key) const {
223 int start_index = key_to_index(key);
224 for (int roll_over = 0; roll_over <= _max_distance_from_start_index; roll_over += 1) {
225 int index = (start_index + roll_over) % _capacity;
226 Entry *entry = &_entries[index];
227
228 if (!entry->used)
229 return NULL;
230
231 if (EqualFn(entry->key, key))
232 return entry;
233 }
234 return NULL;
235 }
236
237 int key_to_index(const K &key) const {
238 return (int)(HashFunction(key) % ((uint32_t)_capacity));
239 }
240};
241
242} // namespace mem
243
244#endif
src/mem_list.hpp created+101
...@@ -0,0 +1,101 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_MEM_LIST_HPP
9#define ZIG_MEM_LIST_HPP
10
11#include "mem.hpp"
12
13namespace mem {
14
15template<typename T>
16struct List {
17 void deinit(Allocator& allocator) {
18 allocator.deallocate<T>(items, capacity);
19 }
20
21 void append(Allocator& allocator, const T& item) {
22 ensure_capacity(allocator, length + 1);
23 items[length++] = item;
24 }
25
26 // remember that the pointer to this item is invalid after you
27 // modify the length of the list
28 const T & at(size_t index) const {
29 assert(index != SIZE_MAX);
30 assert(index < length);
31 return items[index];
32 }
33
34 T & at(size_t index) {
35 assert(index != SIZE_MAX);
36 assert(index < length);
37 return items[index];
38 }
39
40 T pop() {
41 assert(length >= 1);
42 return items[--length];
43 }
44
45 T *add_one() {
46 resize(length + 1);
47 return &last();
48 }
49
50 const T & last() const {
51 assert(length >= 1);
52 return items[length - 1];
53 }
54
55 T & last() {
56 assert(length >= 1);
57 return items[length - 1];
58 }
59
60 void resize(Allocator& allocator, size_t new_length) {
61 assert(new_length != SIZE_MAX);
62 ensure_capacity(allocator, new_length);
63 length = new_length;
64 }
65
66 void clear() {
67 length = 0;
68 }
69
70 void ensure_capacity(Allocator& allocator, size_t new_capacity) {
71 if (capacity >= new_capacity)
72 return;
73
74 size_t better_capacity = capacity;
75 do {
76 better_capacity = better_capacity * 5 / 2 + 8;
77 } while (better_capacity < new_capacity);
78
79 items = allocator.reallocate_nonzero<T>(items, capacity, better_capacity);
80 capacity = better_capacity;
81 }
82
83 T swap_remove(size_t index) {
84 if (length - 1 == index) return pop();
85
86 assert(index != SIZE_MAX);
87 assert(index < length);
88
89 T old_item = items[index];
90 items[index] = pop();
91 return old_item;
92 }
93
94 T *items{nullptr};
95 size_t length{0};
96 size_t capacity{0};
97};
98
99} // namespace mem
100
101#endif
src/mem_profile.cpp created+181
...@@ -0,0 +1,181 @@
1/*
2 * Copyright (c) 2020 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#include "config.h"
9
10#ifdef ZIG_ENABLE_MEM_PROFILE
11
12#include "mem.hpp"
13#include "mem_list.hpp"
14#include "mem_profile.hpp"
15#include "heap.hpp"
16
17namespace mem {
18
19void Profile::init(const char *name, const char *kind) {
20 this->name = name;
21 this->kind = kind;
22 this->usage_table.init(heap::bootstrap_allocator, 1024);
23}
24
25void Profile::deinit() {
26 assert(this->name != nullptr);
27 if (mem::report_print)
28 this->print_report();
29 this->usage_table.deinit(heap::bootstrap_allocator);
30 this->name = nullptr;
31}
32
33void Profile::record_alloc(const TypeInfo &info, size_t count) {
34 if (count == 0) return;
35 auto existing_entry = this->usage_table.put_unique(
36 heap::bootstrap_allocator,
37 UsageKey{info.name_ptr, info.name_len},
38 Entry{info, 1, count, 0, 0} );
39 if (existing_entry != nullptr) {
40 assert(existing_entry->value.info.size == info.size); // allocated name does not match type
41 existing_entry->value.alloc.calls += 1;
42 existing_entry->value.alloc.objects += count;
43 }
44}
45
46void Profile::record_dealloc(const TypeInfo &info, size_t count) {
47 if (count == 0) return;
48 auto existing_entry = this->usage_table.maybe_get(UsageKey{info.name_ptr, info.name_len});
49 if (existing_entry == nullptr) {
50 fprintf(stderr, "deallocated name '");
51 for (size_t i = 0; i < info.name_len; ++i)
52 fputc(info.name_ptr[i], stderr);
53 zig_panic("' (size %zu) not found in allocated table; compromised memory usage stats", info.size);
54 }
55 if (existing_entry->value.info.size != info.size) {
56 fprintf(stderr, "deallocated name '");
57 for (size_t i = 0; i < info.name_len; ++i)
58 fputc(info.name_ptr[i], stderr);
59 zig_panic("' does not match expected type size %zu", info.size);
60 }
61 assert(existing_entry->value.alloc.calls - existing_entry->value.dealloc.calls > 0);
62 assert(existing_entry->value.alloc.objects - existing_entry->value.dealloc.objects >= count);
63 existing_entry->value.dealloc.calls += 1;
64 existing_entry->value.dealloc.objects += count;
65}
66
67static size_t entry_remain_total_bytes(const Profile::Entry *entry) {
68 return (entry->alloc.objects - entry->dealloc.objects) * entry->info.size;
69}
70
71static int entry_compare(const void *a, const void *b) {
72 size_t total_a = entry_remain_total_bytes(*reinterpret_cast<Profile::Entry *const *>(a));
73 size_t total_b = entry_remain_total_bytes(*reinterpret_cast<Profile::Entry *const *>(b));
74 if (total_a > total_b)
75 return -1;
76 if (total_a < total_b)
77 return 1;
78 return 0;
79};
80
81void Profile::print_report(FILE *file) {
82 if (!file) {
83 file = report_file;
84 if (!file)
85 file = stderr;
86 }
87 fprintf(file, "\n--- MEMORY PROFILE REPORT [%s]: %s ---\n", this->kind, this->name);
88
89 List<const Entry *> list;
90 auto it = this->usage_table.entry_iterator();
91 for (;;) {
92 auto entry = it.next();
93 if (!entry)
94 break;
95 list.append(heap::bootstrap_allocator, &entry->value);
96 }
97
98 qsort(list.items, list.length, sizeof(const Entry *), entry_compare);
99
100 size_t total_bytes_alloc = 0;
101 size_t total_bytes_dealloc = 0;
102
103 size_t total_calls_alloc = 0;
104 size_t total_calls_dealloc = 0;
105
106 for (size_t i = 0; i < list.length; i += 1) {
107 const Entry *entry = list.at(i);
108 fprintf(file, " ");
109 for (size_t j = 0; j < entry->info.name_len; ++j)
110 fputc(entry->info.name_ptr[j], file);
111 fprintf(file, ": %zu bytes each", entry->info.size);
112
113 fprintf(file, ", alloc{ %zu calls, %zu objects, total ", entry->alloc.calls, entry->alloc.objects);
114 const auto alloc_num_bytes = entry->alloc.objects * entry->info.size;
115 zig_pretty_print_bytes(file, alloc_num_bytes);
116
117 fprintf(file, " }, dealloc{ %zu calls, %zu objects, total ", entry->dealloc.calls, entry->dealloc.objects);
118 const auto dealloc_num_bytes = entry->dealloc.objects * entry->info.size;
119 zig_pretty_print_bytes(file, dealloc_num_bytes);
120
121 fprintf(file, " }, remain{ %zu calls, %zu objects, total ",
122 entry->alloc.calls - entry->dealloc.calls,
123 entry->alloc.objects - entry->dealloc.objects );
124 const auto remain_num_bytes = alloc_num_bytes - dealloc_num_bytes;
125 zig_pretty_print_bytes(file, remain_num_bytes);
126
127 fprintf(file, " }\n");
128
129 total_bytes_alloc += alloc_num_bytes;
130 total_bytes_dealloc += dealloc_num_bytes;
131
132 total_calls_alloc += entry->alloc.calls;
133 total_calls_dealloc += entry->dealloc.calls;
134 }
135
136 fprintf(file, "\n Total bytes allocated: ");
137 zig_pretty_print_bytes(file, total_bytes_alloc);
138 fprintf(file, ", deallocated: ");
139 zig_pretty_print_bytes(file, total_bytes_dealloc);
140 fprintf(file, ", remaining: ");
141 zig_pretty_print_bytes(file, total_bytes_alloc - total_bytes_dealloc);
142
143 fprintf(file, "\n Total calls alloc: %zu, dealloc: %zu, remain: %zu\n",
144 total_calls_alloc, total_calls_dealloc, (total_calls_alloc - total_calls_dealloc));
145
146 list.deinit(heap::bootstrap_allocator);
147}
148
149uint32_t Profile::usage_hash(UsageKey key) {
150 // FNV 32-bit hash
151 uint32_t h = 2166136261;
152 for (size_t i = 0; i < key.name_len; ++i) {
153 h = h ^ key.name_ptr[i];
154 h = h * 16777619;
155 }
156 return h;
157}
158
159bool Profile::usage_equal(UsageKey a, UsageKey b) {
160 return memcmp(a.name_ptr, b.name_ptr, a.name_len > b.name_len ? a.name_len : b.name_len) == 0;
161}
162
163void InternCounters::print_report(FILE *file) {
164 if (!file) {
165 file = report_file;
166 if (!file)
167 file = stderr;
168 }
169 fprintf(file, "\n--- IR INTERNING REPORT ---\n");
170 fprintf(file, " undefined: interned %zu times\n", intern_counters.x_undefined);
171 fprintf(file, " void: interned %zu times\n", intern_counters.x_void);
172 fprintf(file, " null: interned %zu times\n", intern_counters.x_null);
173 fprintf(file, " unreachable: interned %zu times\n", intern_counters.x_unreachable);
174 fprintf(file, " zero_byte: interned %zu times\n", intern_counters.zero_byte);
175}
176
177InternCounters intern_counters;
178
179} // namespace mem
180
181#endif
src/mem_profile.hpp created+71
...@@ -0,0 +1,71 @@
1/*
2 * Copyright (c) 2020 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_MEM_PROFILE_HPP
9#define ZIG_MEM_PROFILE_HPP
10
11#include "config.h"
12
13#ifdef ZIG_ENABLE_MEM_PROFILE
14
15#include <stdio.h>
16
17#include "mem.hpp"
18#include "mem_hash_map.hpp"
19#include "util.hpp"
20
21namespace mem {
22
23struct Profile {
24 void init(const char *name, const char *kind);
25 void deinit();
26
27 void record_alloc(const TypeInfo &info, size_t count);
28 void record_dealloc(const TypeInfo &info, size_t count);
29
30 void print_report(FILE *file = nullptr);
31
32 struct Entry {
33 TypeInfo info;
34
35 struct Use {
36 size_t calls;
37 size_t objects;
38 } alloc, dealloc;
39 };
40
41private:
42 const char *name;
43 const char *kind;
44
45 struct UsageKey {
46 const char *name_ptr;
47 size_t name_len;
48 };
49
50 static uint32_t usage_hash(UsageKey key);
51 static bool usage_equal(UsageKey a, UsageKey b);
52
53 HashMap<UsageKey, Entry, usage_hash, usage_equal> usage_table;
54};
55
56struct InternCounters {
57 size_t x_undefined;
58 size_t x_void;
59 size_t x_null;
60 size_t x_unreachable;
61 size_t zero_byte;
62
63 void print_report(FILE *file = nullptr);
64};
65
66extern InternCounters intern_counters;
67
68} // namespace mem
69
70#endif
71#endif
src/mem_type_info.hpp created+136
...@@ -0,0 +1,136 @@
1/*
2 * Copyright (c) 2020 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_MEM_TYPE_INFO_HPP
9#define ZIG_MEM_TYPE_INFO_HPP
10
11#include "config.h"
12
13#ifndef ZIG_TYPE_INFO_IMPLEMENTATION
14# ifdef ZIG_ENABLE_MEM_PROFILE
15# define ZIG_TYPE_INFO_IMPLEMENTATION 1
16# else
17# define ZIG_TYPE_INFO_IMPLEMENTATION 0
18# endif
19#endif
20
21namespace mem {
22
23#if ZIG_TYPE_INFO_IMPLEMENTATION == 0
24
25struct TypeInfo {
26 size_t size;
27 size_t alignment;
28
29 template <typename T>
30 static constexpr TypeInfo make() {
31 return {sizeof(T), alignof(T)};
32 }
33};
34
35#elif ZIG_TYPE_INFO_IMPLEMENTATION == 1
36
37//
38// A non-portable way to get a human-readable type-name compatible with
39// non-RTTI C++ compiler mode; eg. `-fno-rtti`.
40//
41// Minimum requirements are c++11 and a compiler that has a constant for the
42// current function's decorated name whereby a template-type name can be
43// computed. eg. `__PRETTY_FUNCTION__` or `__FUNCSIG__`.
44//
45// given the following snippet:
46//
47// | #include <stdio.h>
48// |
49// | struct Top {};
50// | namespace mynamespace {
51// | using custom = unsigned int;
52// | struct Foo {
53// | struct Bar {};
54// | };
55// | };
56// |
57// | template <typename T>
58// | void foobar() {
59// | #ifdef _MSC_VER
60// | fprintf(stderr, "--> %s\n", __FUNCSIG__);
61// | #else
62// | fprintf(stderr, "--> %s\n", __PRETTY_FUNCTION__);
63// | #endif
64// | }
65// |
66// | int main() {
67// | foobar<Top>();
68// | foobar<unsigned int>();
69// | foobar<mynamespace::custom>();
70// | foobar<mynamespace::Foo*>();
71// | foobar<mynamespace::Foo::Bar*>();
72// | }
73//
74// gcc 9.2.0 produces:
75// --> void foobar() [with T = Top]
76// --> void foobar() [with T = unsigned int]
77// --> void foobar() [with T = unsigned int]
78// --> void foobar() [with T = mynamespace::Foo*]
79// --> void foobar() [with T = mynamespace::Foo::Bar*]
80//
81// xcode 11.3.1/clang produces:
82// --> void foobar() [T = Top]
83// --> void foobar() [T = unsigned int]
84// --> void foobar() [T = unsigned int]
85// --> void foobar() [T = mynamespace::Foo *]
86// --> void foobar() [T = mynamespace::Foo::Bar *]
87//
88// VStudio 2019 16.5.0/msvc produces:
89// --> void __cdecl foobar<struct Top>(void)
90// --> void __cdecl foobar<unsigned int>(void)
91// --> void __cdecl foobar<unsigned int>(void)
92// --> void __cdecl foobar<structmynamespace::Foo*>(void)
93// --> void __cdecl foobar<structmynamespace::Foo::Bar*>(void)
94//
95struct TypeInfo {
96 const char *name_ptr;
97 size_t name_len;
98 size_t size;
99 size_t alignment;
100
101 static constexpr TypeInfo to_type_info(const char *str, size_t start, size_t end, size_t size, size_t alignment) {
102 return TypeInfo{str + start, end - start, size, alignment};
103 }
104
105 static constexpr size_t index_of(const char *str, char c) {
106 return *str == c ? 0 : 1 + index_of(str + 1, c);
107 }
108
109 template <typename T>
110 static constexpr const char *decorated_name() {
111#ifdef _MSC_VER
112 return __FUNCSIG__;
113#else
114 return __PRETTY_FUNCTION__;
115#endif
116 }
117
118 static constexpr TypeInfo extract(const char *decorated, size_t size, size_t alignment) {
119#ifdef _MSC_VER
120 return to_type_info(decorated, index_of(decorated, '<') + 1, index_of(decorated, '>'), size, alignment);
121#else
122 return to_type_info(decorated, index_of(decorated, '=') + 2, index_of(decorated, ']'), size, alignment);
123#endif
124 }
125
126 template <typename T>
127 static constexpr TypeInfo make() {
128 return TypeInfo::extract(TypeInfo::decorated_name<T>(), sizeof(T), alignof(T));
129 }
130};
131
132#endif // ZIG_TYPE_INFO_IMPLEMENTATION
133
134} // namespace mem
135
136#endif
src/memory_profiling.cpp deleted-150
...@@ -1,150 +0,0 @@
1#include "memory_profiling.hpp"
2#include "hash_map.hpp"
3#include "list.hpp"
4#include "util.hpp"
5#include <string.h>
6
7#ifdef ZIG_ENABLE_MEM_PROFILE
8
9MemprofInternCount memprof_intern_count;
10
11static bool str_eql_str(const char *a, const char *b) {
12 return strcmp(a, b) == 0;
13}
14
15static uint32_t str_hash(const char *s) {
16 // FNV 32-bit hash
17 uint32_t h = 2166136261;
18 for (; *s; s += 1) {
19 h = h ^ *s;
20 h = h * 16777619;
21 }
22 return h;
23}
24
25struct CountAndSize {
26 size_t item_count;
27 size_t type_size;
28};
29
30ZigList<const char *> unknown_names = {};
31HashMap<const char *, CountAndSize, str_hash, str_eql_str> usage_table = {};
32bool table_active = false;
33
34static const char *get_default_name(const char *name_or_null, size_t type_size) {
35 if (name_or_null != nullptr) return name_or_null;
36 if (type_size >= unknown_names.length) {
37 table_active = false;
38 while (type_size >= unknown_names.length) {
39 unknown_names.append(nullptr);
40 }
41 table_active = true;
42 }
43 if (unknown_names.at(type_size) == nullptr) {
44 char buf[100];
45 sprintf(buf, "Unknown_%zu%c", type_size, 0);
46 unknown_names.at(type_size) = strdup(buf);
47 }
48 return unknown_names.at(type_size);
49}
50
51void memprof_alloc(const char *name, size_t count, size_t type_size) {
52 if (!table_active) return;
53 if (count == 0) return;
54 // temporarily disable during table put
55 table_active = false;
56 name = get_default_name(name, type_size);
57 auto existing_entry = usage_table.put_unique(name, {count, type_size});
58 if (existing_entry != nullptr) {
59 assert(existing_entry->value.type_size == type_size); // allocated name does not match type
60 existing_entry->value.item_count += count;
61 }
62 table_active = true;
63}
64
65void memprof_dealloc(const char *name, size_t count, size_t type_size) {
66 if (!table_active) return;
67 if (count == 0) return;
68 name = get_default_name(name, type_size);
69 auto existing_entry = usage_table.maybe_get(name);
70 if (existing_entry == nullptr) {
71 zig_panic("deallocated name '%s' (size %zu) not found in allocated table; compromised memory usage stats",
72 name, type_size);
73 }
74 if (existing_entry->value.type_size != type_size) {
75 zig_panic("deallocated name '%s' does not match expected type size %zu", name, type_size);
76 }
77 existing_entry->value.item_count -= count;
78}
79
80void memprof_init(void) {
81 usage_table.init(1024);
82 table_active = true;
83}
84
85struct MemItem {
86 const char *type_name;
87 CountAndSize count_and_size;
88};
89
90static size_t get_bytes(const MemItem *item) {
91 return item->count_and_size.item_count * item->count_and_size.type_size;
92}
93
94static int compare_bytes_desc(const void *a, const void *b) {
95 size_t size_a = get_bytes((const MemItem *)(a));
96 size_t size_b = get_bytes((const MemItem *)(b));
97 if (size_a > size_b)
98 return -1;
99 if (size_a < size_b)
100 return 1;
101 return 0;
102}
103
104void memprof_dump_stats(FILE *file) {
105 assert(table_active);
106 // disable modifications from this function
107 table_active = false;
108
109 ZigList<MemItem> list = {};
110
111 auto it = usage_table.entry_iterator();
112 for (;;) {
113 auto *entry = it.next();
114 if (!entry)
115 break;
116
117 list.append({entry->key, entry->value});
118 }
119
120 qsort(list.items, list.length, sizeof(MemItem), compare_bytes_desc);
121
122 size_t total_bytes_used = 0;
123
124 for (size_t i = 0; i < list.length; i += 1) {
125 const MemItem *item = &list.at(i);
126 fprintf(file, "%s: %zu items, %zu bytes each, total ", item->type_name,
127 item->count_and_size.item_count, item->count_and_size.type_size);
128 size_t bytes = get_bytes(item);
129 zig_pretty_print_bytes(file, bytes);
130 fprintf(file, "\n");
131
132 total_bytes_used += bytes;
133 }
134
135 fprintf(stderr, "Total bytes used: ");
136 zig_pretty_print_bytes(file, total_bytes_used);
137 fprintf(file, "\n");
138
139 list.deinit();
140 table_active = true;
141
142 fprintf(stderr, "\n");
143 fprintf(stderr, "undefined: interned %zu times\n", memprof_intern_count.x_undefined);
144 fprintf(stderr, "void: interned %zu times\n", memprof_intern_count.x_void);
145 fprintf(stderr, "null: interned %zu times\n", memprof_intern_count.x_null);
146 fprintf(stderr, "unreachable: interned %zu times\n", memprof_intern_count.x_unreachable);
147 fprintf(stderr, "zero_byte: interned %zu times\n", memprof_intern_count.zero_byte);
148}
149
150#endif
src/memory_profiling.hpp deleted-31
...@@ -1,31 +0,0 @@
1/*
2 * Copyright (c) 2019 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_MEMORY_PROFILING_HPP
9#define ZIG_MEMORY_PROFILING_HPP
10
11#include "config.h"
12
13#include <stddef.h>
14#include <stdio.h>
15
16struct MemprofInternCount {
17 size_t x_undefined;
18 size_t x_void;
19 size_t x_null;
20 size_t x_unreachable;
21 size_t zero_byte;
22};
23extern MemprofInternCount memprof_intern_count;
24
25void memprof_init(void);
26
27void memprof_alloc(const char *name, size_t item_count, size_t type_size);
28void memprof_dealloc(const char *name, size_t item_count, size_t type_size);
29
30void memprof_dump_stats(FILE *file);
31#endif
src/os.cpp+7-7
...@@ -107,7 +107,7 @@ static void populate_termination(Termination *term, int status) {...@@ -107,7 +107,7 @@ static void populate_termination(Termination *term, int status) {
107}107}
108108
109static void os_spawn_process_posix(ZigList<const char *> &args, Termination *term) {109static void os_spawn_process_posix(ZigList<const char *> &args, Termination *term) {
110 const char **argv = allocate<const char *>(args.length + 1);110 const char **argv = heap::c_allocator.allocate<const char *>(args.length + 1);
111 for (size_t i = 0; i < args.length; i += 1) {111 for (size_t i = 0; i < args.length; i += 1) {
112 argv[i] = args.at(i);112 argv[i] = args.at(i);
113 }113 }
...@@ -688,7 +688,7 @@ static Buf os_path_resolve_posix(Buf **paths_ptr, size_t paths_len) {...@@ -688,7 +688,7 @@ static Buf os_path_resolve_posix(Buf **paths_ptr, size_t paths_len) {
688688
689 if (have_abs) {689 if (have_abs) {
690 result_len = max_size;690 result_len = max_size;
691 result_ptr = allocate_nonzero<uint8_t>(result_len);691 result_ptr = heap::c_allocator.allocate_nonzero<uint8_t>(result_len);
692 } else {692 } else {
693 Buf cwd = BUF_INIT;693 Buf cwd = BUF_INIT;
694 int err;694 int err;
...@@ -696,7 +696,7 @@ static Buf os_path_resolve_posix(Buf **paths_ptr, size_t paths_len) {...@@ -696,7 +696,7 @@ static Buf os_path_resolve_posix(Buf **paths_ptr, size_t paths_len) {
696 zig_panic("get cwd failed");696 zig_panic("get cwd failed");
697 }697 }
698 result_len = max_size + buf_len(&cwd) + 1;698 result_len = max_size + buf_len(&cwd) + 1;
699 result_ptr = allocate_nonzero<uint8_t>(result_len);699 result_ptr = heap::c_allocator.allocate_nonzero<uint8_t>(result_len);
700 memcpy(result_ptr, buf_ptr(&cwd), buf_len(&cwd));700 memcpy(result_ptr, buf_ptr(&cwd), buf_len(&cwd));
701 result_index += buf_len(&cwd);701 result_index += buf_len(&cwd);
702 }702 }
...@@ -816,7 +816,7 @@ static Error os_exec_process_posix(ZigList<const char *> &args,...@@ -816,7 +816,7 @@ static Error os_exec_process_posix(ZigList<const char *> &args,
816 if (dup2(stderr_pipe[1], STDERR_FILENO) == -1)816 if (dup2(stderr_pipe[1], STDERR_FILENO) == -1)
817 zig_panic("dup2 failed");817 zig_panic("dup2 failed");
818818
819 const char **argv = allocate<const char *>(args.length + 1);819 const char **argv = heap::c_allocator.allocate<const char *>(args.length + 1);
820 argv[args.length] = nullptr;820 argv[args.length] = nullptr;
821 for (size_t i = 0; i < args.length; i += 1) {821 for (size_t i = 0; i < args.length; i += 1) {
822 argv[i] = args.at(i);822 argv[i] = args.at(i);
...@@ -1134,7 +1134,7 @@ static bool is_stderr_cyg_pty(void) {...@@ -1134,7 +1134,7 @@ static bool is_stderr_cyg_pty(void) {
1134 if (stderr_handle == INVALID_HANDLE_VALUE)1134 if (stderr_handle == INVALID_HANDLE_VALUE)
1135 return false;1135 return false;
11361136
1137 int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;1137 const int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
1138 FILE_NAME_INFO *nameinfo;1138 FILE_NAME_INFO *nameinfo;
1139 WCHAR *p = NULL;1139 WCHAR *p = NULL;
11401140
...@@ -1142,7 +1142,7 @@ static bool is_stderr_cyg_pty(void) {...@@ -1142,7 +1142,7 @@ static bool is_stderr_cyg_pty(void) {
1142 if (GetFileType(stderr_handle) != FILE_TYPE_PIPE) {1142 if (GetFileType(stderr_handle) != FILE_TYPE_PIPE) {
1143 return 0;1143 return 0;
1144 }1144 }
1145 nameinfo = (FILE_NAME_INFO *)allocate<char>(size);1145 nameinfo = reinterpret_cast<FILE_NAME_INFO *>(heap::c_allocator.allocate<char>(size));
1146 if (nameinfo == NULL) {1146 if (nameinfo == NULL) {
1147 return 0;1147 return 0;
1148 }1148 }
...@@ -1179,7 +1179,7 @@ static bool is_stderr_cyg_pty(void) {...@@ -1179,7 +1179,7 @@ static bool is_stderr_cyg_pty(void) {
1179 }1179 }
1180 }1180 }
1181 }1181 }
1182 free(nameinfo);1182 heap::c_allocator.deallocate(reinterpret_cast<char *>(nameinfo), size);
1183 return (p != NULL);1183 return (p != NULL);
1184}1184}
1185#endif1185#endif
src/parser.cpp+3-3
...@@ -147,7 +147,7 @@ static void ast_invalid_token_error(ParseContext *pc, Token *token) {...@@ -147,7 +147,7 @@ static void ast_invalid_token_error(ParseContext *pc, Token *token) {
147}147}
148148
149static AstNode *ast_create_node_no_line_info(ParseContext *pc, NodeType type) {149static AstNode *ast_create_node_no_line_info(ParseContext *pc, NodeType type) {
150 AstNode *node = allocate<AstNode>(1, "AstNode");150 AstNode *node = heap::c_allocator.create<AstNode>();
151 node->type = type;151 node->type = type;
152 node->owner = pc->owner;152 node->owner = pc->owner;
153 return node;153 return node;
...@@ -1966,7 +1966,7 @@ static AsmOutput *ast_parse_asm_output_item(ParseContext *pc) {...@@ -1966,7 +1966,7 @@ static AsmOutput *ast_parse_asm_output_item(ParseContext *pc) {
19661966
1967 expect_token(pc, TokenIdRParen);1967 expect_token(pc, TokenIdRParen);
19681968
1969 AsmOutput *res = allocate<AsmOutput>(1);1969 AsmOutput *res = heap::c_allocator.create<AsmOutput>();
1970 res->asm_symbolic_name = token_buf(sym_name);1970 res->asm_symbolic_name = token_buf(sym_name);
1971 res->constraint = token_buf(str);1971 res->constraint = token_buf(str);
1972 res->variable_name = token_buf(var_name);1972 res->variable_name = token_buf(var_name);
...@@ -2003,7 +2003,7 @@ static AsmInput *ast_parse_asm_input_item(ParseContext *pc) {...@@ -2003,7 +2003,7 @@ static AsmInput *ast_parse_asm_input_item(ParseContext *pc) {
2003 AstNode *expr = ast_expect(pc, ast_parse_expr);2003 AstNode *expr = ast_expect(pc, ast_parse_expr);
2004 expect_token(pc, TokenIdRParen);2004 expect_token(pc, TokenIdRParen);
20052005
2006 AsmInput *res = allocate<AsmInput>(1);2006 AsmInput *res = heap::c_allocator.create<AsmInput>();
2007 res->asm_symbolic_name = token_buf(sym_name);2007 res->asm_symbolic_name = token_buf(sym_name);
2008 res->constraint = token_buf(constraint);2008 res->constraint = token_buf(constraint);
2009 res->expr = expr;2009 res->expr = expr;
src/target.cpp+1-1
...@@ -524,7 +524,7 @@ void get_native_target(ZigTarget *target) {...@@ -524,7 +524,7 @@ void get_native_target(ZigTarget *target) {
524 target->abi = target_default_abi(target->arch, target->os);524 target->abi = target_default_abi(target->arch, target->os);
525 }525 }
526 if (target_is_glibc(target)) {526 if (target_is_glibc(target)) {
527 target->glibc_version = allocate<ZigGLibCVersion>(1);527 target->glibc_version = heap::c_allocator.create<ZigGLibCVersion>();
528 target_init_default_glibc_version(target);528 target_init_default_glibc_version(target);
529#ifdef ZIG_OS_LINUX529#ifdef ZIG_OS_LINUX
530 Error err;530 Error err;
src/tokenizer.cpp+2-2
...@@ -397,10 +397,10 @@ static void invalid_char_error(Tokenize *t, uint8_t c) {...@@ -397,10 +397,10 @@ static void invalid_char_error(Tokenize *t, uint8_t c) {
397void tokenize(Buf *buf, Tokenization *out) {397void tokenize(Buf *buf, Tokenization *out) {
398 Tokenize t = {0};398 Tokenize t = {0};
399 t.out = out;399 t.out = out;
400 t.tokens = out->tokens = allocate<ZigList<Token>>(1);400 t.tokens = out->tokens = heap::c_allocator.create<ZigList<Token>>();
401 t.buf = buf;401 t.buf = buf;
402402
403 out->line_offsets = allocate<ZigList<size_t>>(1);403 out->line_offsets = heap::c_allocator.create<ZigList<size_t>>();
404 out->line_offsets->append(0);404 out->line_offsets->append(0);
405405
406 // Skip the UTF-8 BOM if present406 // Skip the UTF-8 BOM if present
src/userland.cpp+2-2
...@@ -101,7 +101,7 @@ Error stage2_cpu_features_parse(struct Stage2CpuFeatures **out, const char *zig_...@@ -101,7 +101,7 @@ Error stage2_cpu_features_parse(struct Stage2CpuFeatures **out, const char *zig_
101 const char *cpu_name, const char *cpu_features)101 const char *cpu_name, const char *cpu_features)
102{102{
103 if (zig_triple == nullptr) {103 if (zig_triple == nullptr) {
104 Stage2CpuFeatures *result = allocate<Stage2CpuFeatures>(1, "Stage2CpuFeatures");104 Stage2CpuFeatures *result = heap::c_allocator.create<Stage2CpuFeatures>();
105 result->llvm_cpu_name = ZigLLVMGetHostCPUName();105 result->llvm_cpu_name = ZigLLVMGetHostCPUName();
106 result->llvm_cpu_features = ZigLLVMGetNativeFeatures();106 result->llvm_cpu_features = ZigLLVMGetNativeFeatures();
107 result->builtin_str = "arch.getBaselineCpuFeatures();\n";107 result->builtin_str = "arch.getBaselineCpuFeatures();\n";
...@@ -110,7 +110,7 @@ Error stage2_cpu_features_parse(struct Stage2CpuFeatures **out, const char *zig_...@@ -110,7 +110,7 @@ Error stage2_cpu_features_parse(struct Stage2CpuFeatures **out, const char *zig_
110 return ErrorNone;110 return ErrorNone;
111 }111 }
112 if (cpu_name == nullptr && cpu_features == nullptr) {112 if (cpu_name == nullptr && cpu_features == nullptr) {
113 Stage2CpuFeatures *result = allocate<Stage2CpuFeatures>(1, "Stage2CpuFeatures");113 Stage2CpuFeatures *result = heap::c_allocator.create<Stage2CpuFeatures>();
114 result->builtin_str = "arch.getBaselineCpuFeatures();\n";114 result->builtin_str = "arch.getBaselineCpuFeatures();\n";
115 result->cache_hash = "\n\n";115 result->cache_hash = "\n\n";
116 *out = result;116 *out = result;
src/util.hpp+5-127
...@@ -8,69 +8,19 @@...@@ -8,69 +8,19 @@
8#ifndef ZIG_UTIL_HPP8#ifndef ZIG_UTIL_HPP
9#define ZIG_UTIL_HPP9#define ZIG_UTIL_HPP
1010
11#include "memory_profiling.hpp"
12
13#include <stdlib.h>11#include <stdlib.h>
14#include <stdint.h>12#include <stdint.h>
15#include <string.h>13#include <string.h>
16#include <assert.h>
17#include <ctype.h>14#include <ctype.h>
1815
19#if defined(_MSC_VER)16#if defined(_MSC_VER)
20
21#include <intrin.h> 17#include <intrin.h>
22
23#define ATTRIBUTE_COLD __declspec(noinline)
24#define ATTRIBUTE_PRINTF(a, b)
25#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
26#define ATTRIBUTE_NORETURN __declspec(noreturn)
27#define ATTRIBUTE_MUST_USE
28
29#define BREAKPOINT __debugbreak()
30
31#else
32
33#define ATTRIBUTE_COLD __attribute__((cold))
34#define ATTRIBUTE_PRINTF(a, b) __attribute__((format(printf, a, b)))
35#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
36#define ATTRIBUTE_NORETURN __attribute__((noreturn))
37#define ATTRIBUTE_MUST_USE __attribute__((warn_unused_result))
38
39#if defined(__MINGW32__) || defined(__MINGW64__)
40#define BREAKPOINT __debugbreak()
41#elif defined(__i386__) || defined(__x86_64__)
42#define BREAKPOINT __asm__ volatile("int $0x03");
43#elif defined(__clang__)
44#define BREAKPOINT __builtin_debugtrap()
45#elif defined(__GNUC__)
46#define BREAKPOINT __builtin_trap()
47#else
48#include <signal.h>
49#define BREAKPOINT raise(SIGTRAP)
50#endif
51
52#endif
53
54ATTRIBUTE_COLD
55ATTRIBUTE_NORETURN
56ATTRIBUTE_PRINTF(1, 2)
57void zig_panic(const char *format, ...);
58
59static inline void zig_assert(bool ok, const char *file, int line, const char *func) {
60 if (!ok) {
61 zig_panic("Assertion failed at %s:%d in %s. This is a bug in the Zig compiler.", file, line, func);
62 }
63}
64
65#ifdef _WIN32
66#define __func__ __FUNCTION__
67#endif18#endif
6819
69#define zig_unreachable() zig_panic("Unreachable at %s:%d in %s. This is a bug in the Zig compiler.", __FILE__, __LINE__, __func__)20#include "config.h"
7021#include "util_base.hpp"
71// Assertions in stage1 are always on, and they call zig @panic.22#include "heap.hpp"
72#undef assert23#include "mem.hpp"
73#define assert(ok) zig_assert(ok, __FILE__, __LINE__, __func__)
7424
75#if defined(_MSC_VER)25#if defined(_MSC_VER)
76static inline int clzll(unsigned long long mask) {26static inline int clzll(unsigned long long mask) {
...@@ -107,78 +57,6 @@ static inline int ctzll(unsigned long long mask) {...@@ -107,78 +57,6 @@ static inline int ctzll(unsigned long long mask) {
107#define ctzll(x) __builtin_ctzll(x)57#define ctzll(x) __builtin_ctzll(x)
108#endif58#endif
10959
110
111template<typename T>
112ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count, const char *name = nullptr) {
113#ifdef ZIG_ENABLE_MEM_PROFILE
114 memprof_alloc(name, count, sizeof(T));
115#endif
116#ifndef NDEBUG
117 // make behavior when size == 0 portable
118 if (count == 0)
119 return nullptr;
120#endif
121 T *ptr = reinterpret_cast<T*>(malloc(count * sizeof(T)));
122 if (!ptr)
123 zig_panic("allocation failed");
124 return ptr;
125}
126
127template<typename T>
128ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count, const char *name = nullptr) {
129#ifdef ZIG_ENABLE_MEM_PROFILE
130 memprof_alloc(name, count, sizeof(T));
131#endif
132#ifndef NDEBUG
133 // make behavior when size == 0 portable
134 if (count == 0)
135 return nullptr;
136#endif
137 T *ptr = reinterpret_cast<T*>(calloc(count, sizeof(T)));
138 if (!ptr)
139 zig_panic("allocation failed");
140 return ptr;
141}
142
143template<typename T>
144static inline T *reallocate(T *old, size_t old_count, size_t new_count, const char *name = nullptr) {
145 T *ptr = reallocate_nonzero(old, old_count, new_count);
146 if (new_count > old_count) {
147 memset(&ptr[old_count], 0, (new_count - old_count) * sizeof(T));
148 }
149 return ptr;
150}
151
152template<typename T>
153static inline T *reallocate_nonzero(T *old, size_t old_count, size_t new_count, const char *name = nullptr) {
154#ifdef ZIG_ENABLE_MEM_PROFILE
155 memprof_dealloc(name, old_count, sizeof(T));
156 memprof_alloc(name, new_count, sizeof(T));
157#endif
158#ifndef NDEBUG
159 // make behavior when size == 0 portable
160 if (new_count == 0 && old == nullptr)
161 return nullptr;
162#endif
163 T *ptr = reinterpret_cast<T*>(realloc(old, new_count * sizeof(T)));
164 if (!ptr)
165 zig_panic("allocation failed");
166 return ptr;
167}
168
169template<typename T>
170static inline void deallocate(T *old, size_t count, const char *name = nullptr) {
171#ifdef ZIG_ENABLE_MEM_PROFILE
172 memprof_dealloc(name, count, sizeof(T));
173#endif
174 free(old);
175}
176
177template<typename T>
178static inline void destroy(T *old, const char *name = nullptr) {
179 return deallocate(old, 1, name);
180}
181
182template <typename T, size_t n>60template <typename T, size_t n>
183constexpr size_t array_length(const T (&)[n]) {61constexpr size_t array_length(const T (&)[n]) {
184 return n;62 return n;
...@@ -293,7 +171,7 @@ struct Slice {...@@ -293,7 +171,7 @@ struct Slice {
293 }171 }
294172
295 static inline Slice<T> alloc(size_t n) {173 static inline Slice<T> alloc(size_t n) {
296 return {allocate_nonzero<T>(n), n};174 return {heap::c_allocator.allocate_nonzero<T>(n), n};
297 }175 }
298};176};
299177
src/util_base.hpp created+67
...@@ -0,0 +1,67 @@
1/*
2 * Copyright (c) 2015 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_UTIL_BASE_HPP
9#define ZIG_UTIL_BASE_HPP
10
11#include <assert.h>
12
13#if defined(_MSC_VER)
14
15#define ATTRIBUTE_COLD __declspec(noinline)
16#define ATTRIBUTE_PRINTF(a, b)
17#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
18#define ATTRIBUTE_NORETURN __declspec(noreturn)
19#define ATTRIBUTE_MUST_USE
20
21#define BREAKPOINT __debugbreak()
22
23#else
24
25#define ATTRIBUTE_COLD __attribute__((cold))
26#define ATTRIBUTE_PRINTF(a, b) __attribute__((format(printf, a, b)))
27#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
28#define ATTRIBUTE_NORETURN __attribute__((noreturn))
29#define ATTRIBUTE_MUST_USE __attribute__((warn_unused_result))
30
31#if defined(__MINGW32__) || defined(__MINGW64__)
32#define BREAKPOINT __debugbreak()
33#elif defined(__i386__) || defined(__x86_64__)
34#define BREAKPOINT __asm__ volatile("int $0x03");
35#elif defined(__clang__)
36#define BREAKPOINT __builtin_debugtrap()
37#elif defined(__GNUC__)
38#define BREAKPOINT __builtin_trap()
39#else
40#include <signal.h>
41#define BREAKPOINT raise(SIGTRAP)
42#endif
43
44#endif
45
46ATTRIBUTE_COLD
47ATTRIBUTE_NORETURN
48ATTRIBUTE_PRINTF(1, 2)
49void zig_panic(const char *format, ...);
50
51static inline void zig_assert(bool ok, const char *file, int line, const char *func) {
52 if (!ok) {
53 zig_panic("Assertion failed at %s:%d in %s. This is a bug in the Zig compiler.", file, line, func);
54 }
55}
56
57#ifdef _WIN32
58#define __func__ __FUNCTION__
59#endif
60
61#define zig_unreachable() zig_panic("Unreachable at %s:%d in %s. This is a bug in the Zig compiler.", __FILE__, __LINE__, __func__)
62
63// Assertions in stage1 are always on, and they call zig @panic.
64#undef assert
65#define assert(ok) zig_assert(ok, __FILE__, __LINE__, __func__)
66
67#endif
test/compile_errors.zig+16
...@@ -3,6 +3,22 @@ const builtin = @import("builtin");...@@ -3,6 +3,22 @@ const builtin = @import("builtin");
3const Target = @import("std").Target;3const Target = @import("std").Target;
44
5pub fn addCases(cases: *tests.CompileErrorContext) void {5pub fn addCases(cases: *tests.CompileErrorContext) void {
6 cases.addTest("duplicate field in anonymous struct literal",
7 \\export fn entry() void {
8 \\ const anon = .{
9 \\ .inner = .{
10 \\ .a = .{
11 \\ .something = "text",
12 \\ },
13 \\ .a = .{},
14 \\ },
15 \\ };
16 \\}
17 , &[_][]const u8{
18 "tmp.zig:7:13: error: duplicate field",
19 "tmp.zig:4:13: note: other field here",
20 });
21
6 cases.addTest("type mismatch in C prototype with varargs",22 cases.addTest("type mismatch in C prototype with varargs",
7 \\const fn_ty = ?fn ([*c]u8, ...) callconv(.C) void;23 \\const fn_ty = ?fn ([*c]u8, ...) callconv(.C) void;
8 \\extern fn fn_decl(fmt: [*:0]u8, ...) void;24 \\extern fn fn_decl(fmt: [*:0]u8, ...) void;
test/stage1/behavior/atomics.zig+4-4
...@@ -146,10 +146,10 @@ fn testAtomicStore() void {...@@ -146,10 +146,10 @@ fn testAtomicStore() void {
146}146}
147147
148test "atomicrmw with floats" {148test "atomicrmw with floats" {
149 if (builtin.arch == .aarch64 or149 if (builtin.arch == .aarch64 or builtin.arch == .arm or builtin.arch == .riscv64) {
150 builtin.arch == .arm or150 // https://github.com/ziglang/zig/issues/4457
151 builtin.arch == .riscv64)151 return error.SkipZigTest;
152 return;152 }
153 testAtomicRmwFloat();153 testAtomicRmwFloat();
154}154}
155155
test/stage1/behavior/bugs/1851.zig+2-3
...@@ -6,10 +6,9 @@ test "allocation and looping over 3-byte integer" {...@@ -6,10 +6,9 @@ test "allocation and looping over 3-byte integer" {
6 expect(@sizeOf([1]u24) == 4);6 expect(@sizeOf([1]u24) == 4);
7 expect(@alignOf(u24) == 4);7 expect(@alignOf(u24) == 4);
8 expect(@alignOf([1]u24) == 4);8 expect(@alignOf([1]u24) == 4);
9 var buffer: [100]u8 = undefined;
10 const a = &std.heap.FixedBufferAllocator.init(&buffer).allocator;
119
12 var x = a.alloc(u24, 2) catch unreachable;10 var x = try std.testing.allocator.alloc(u24, 2);
11 defer std.testing.allocator.free(x);
13 expect(x.len == 2);12 expect(x.len == 2);
14 x[0] = 0xFFFFFF;13 x[0] = 0xFFFFFF;
15 x[1] = 0xFFFFFF;14 x[1] = 0xFFFFFF;
test/stage1/behavior/cast.zig+27
...@@ -764,3 +764,30 @@ test "variable initialization uses result locations properly with regards to the...@@ -764,3 +764,30 @@ test "variable initialization uses result locations properly with regards to the
764 const x: i32 = if (b) 1 else 2;764 const x: i32 = if (b) 1 else 2;
765 expect(x == 1);765 expect(x == 1);
766}766}
767
768test "cast between [*c]T and ?[*:0]T on fn parameter" {
769 const S = struct {
770 const Handler = ?extern fn ([*c]const u8) void;
771 fn addCallback(handler: Handler) void {}
772
773 fn myCallback(cstr: ?[*:0]const u8) callconv(.C) void {}
774
775 fn doTheTest() void {
776 addCallback(myCallback);
777 }
778 };
779 S.doTheTest();
780}
781
782test "cast between C pointer with different but compatible types" {
783 const S = struct {
784 fn foo(arg: [*]c_ushort) u16 {
785 return arg[0];
786 }
787 fn doTheTest() void {
788 var x = [_]u16{ 4, 2, 1, 3 };
789 expect(foo(@ptrCast([*]u16, &x)) == 4);
790 }
791 };
792 S.doTheTest();
793}
test/stage1/behavior/vector.zig+27
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const mem = std.mem;2const mem = std.mem;
3const expect = std.testing.expect;3const expect = std.testing.expect;
4const expectEqual = std.testing.expectEqual;
4const builtin = @import("builtin");5const builtin = @import("builtin");
56
6test "implicit cast vector to array - bool" {7test "implicit cast vector to array - bool" {
...@@ -250,3 +251,29 @@ test "initialize vector which is a struct field" {...@@ -250,3 +251,29 @@ test "initialize vector which is a struct field" {
250 S.doTheTest();251 S.doTheTest();
251 comptime S.doTheTest();252 comptime S.doTheTest();
252}253}
254
255test "vector comparison operators" {
256 const S = struct {
257 fn doTheTest() void {
258 {
259 const v1: @Vector(4, bool) = [_]bool{ true, false, true, false };
260 const v2: @Vector(4, bool) = [_]bool{ false, true, false, true };
261 expectEqual(@splat(4, true), v1 == v1);
262 expectEqual(@splat(4, false), v1 == v2);
263 expectEqual(@splat(4, true), v1 != v2);
264 expectEqual(@splat(4, false), v2 != v2);
265 }
266 {
267 const v1 = @splat(4, @as(u32, 0xc0ffeeee));
268 const v2: @Vector(4, c_uint) = v1;
269 const v3 = @splat(4, @as(u32, 0xdeadbeef));
270 expectEqual(@splat(4, true), v1 == v2);
271 expectEqual(@splat(4, false), v1 == v3);
272 expectEqual(@splat(4, true), v1 != v3);
273 expectEqual(@splat(4, false), v1 != v2);
274 }
275 }
276 };
277 S.doTheTest();
278 comptime S.doTheTest();
279}
test/translate_c.zig+12
...@@ -621,9 +621,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -621,9 +621,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
621 cases.add("float suffixes",621 cases.add("float suffixes",
622 \\#define foo 3.14f622 \\#define foo 3.14f
623 \\#define bar 16.e-2l623 \\#define bar 16.e-2l
624 \\#define FOO 0.12345
625 \\#define BAR .12345
624 , &[_][]const u8{626 , &[_][]const u8{
625 "pub const foo = @as(f32, 3.14);",627 "pub const foo = @as(f32, 3.14);",
626 "pub const bar = @as(c_longdouble, 16.e-2);",628 "pub const bar = @as(c_longdouble, 16.e-2);",
629 "pub const FOO = 0.12345;",
630 "pub const BAR = 0.12345;",
627 });631 });
628632
629 cases.add("comments",633 cases.add("comments",
...@@ -1358,12 +1362,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1358,12 +1362,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1358 cases.add("basic macro function",1362 cases.add("basic macro function",
1359 \\extern int c;1363 \\extern int c;
1360 \\#define BASIC(c) (c*2)1364 \\#define BASIC(c) (c*2)
1365 \\#define FOO(L,b) (L + b)
1361 , &[_][]const u8{1366 , &[_][]const u8{
1362 \\pub extern var c: c_int;1367 \\pub extern var c: c_int;
1363 ,1368 ,
1364 \\pub inline fn BASIC(c_1: var) @TypeOf(c_1 * 2) {1369 \\pub inline fn BASIC(c_1: var) @TypeOf(c_1 * 2) {
1365 \\ return c_1 * 2;1370 \\ return c_1 * 2;
1366 \\}1371 \\}
1372 ,
1373 \\pub inline fn FOO(L: var, b: var) @TypeOf(L + b) {
1374 \\ return L + b;
1375 \\}
1367 });1376 });
13681377
1369 cases.add("macro defines string literal with hex",1378 cases.add("macro defines string literal with hex",
...@@ -2529,10 +2538,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2529,10 +2538,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25292538
2530 cases.add("macro cast",2539 cases.add("macro cast",
2531 \\#define FOO(bar) baz((void *)(baz))2540 \\#define FOO(bar) baz((void *)(baz))
2541 \\#define BAR (void*) a
2532 , &[_][]const u8{2542 , &[_][]const u8{
2533 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) {2543 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) {
2534 \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));2544 \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));
2535 \\}2545 \\}
2546 ,
2547 \\pub const BAR = if (@typeId(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeId(@TypeOf(a)) == .Int) @intToPtr(*c_void, a) else @as(*c_void, a);
2536 });2548 });
25372549
2538 cases.add("macro conditional operator",2550 cases.add("macro conditional operator",