authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-27 14:06:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-27 14:19:53-07:00
loga8e964eadd3496330043985cacaaee7db92886c6
tree57768a2f87b76b9f80231ab924d2bdb481a22d2a
parentba71b96fe6c0e01b8445a2f7bd49541a07c360db

stage2: `zig test` now works with the LLVM backend

Frontend improvements: * When compiling in `zig test` mode, put a task on the work queue to analyze the main package root file. Normally, start code does `_ = import("root");` to make Zig analyze the user's code, however in the case of `zig test`, the root source file is the test runner. Without this change, no tests are picked up. * In the main pipeline, once semantic analysis is finished, if there are no compile errors, populate the `test_functions` Decl with the set of test functions picked up from semantic analysis. * Value: add `array` and `slice` Tags. LLVM backend improvements: * Fix incremental updates of globals. Previously the value of a global would not get replaced with a new value. * Fix LLVM type of arrays. They were incorrectly sending the ABI size as the element count. * Remove the FuncGen parameter from genTypedValue. This function is for generating global constants and there is no function available when it is being called. - The `ref_val` case is now commented out. I'd like to eliminate `ref_val` as one of the possible Value Tags. Instead it should always be done via `decl_ref`. * Implement constant value generation for slices, arrays, and structs. * Constant value generation for functions supports the `decl_ref` tag.

6 files changed, 332 insertions(+), 82 deletions(-)

src/Compilation.zig+15-19
......@@ -1709,7 +1709,9 @@ pub fn update(self: *Compilation) !void {
17091709 // in the start code, but when using the stage1 backend that won't happen,
17101710 // so in order to run AstGen on the root source file we put it into the
17111711 // import_table here.
1712 if (use_stage1) {
1712 // Likewise, in the case of `zig test`, the test runner is the root source file,
1713 // and so there is nothing to import the main file.
1714 if (use_stage1 or self.bin_file.options.is_test) {
17131715 _ = try module.importPkg(module.main_pkg);
17141716 }
17151717
......@@ -1725,6 +1727,9 @@ pub fn update(self: *Compilation) !void {
17251727
17261728 if (!use_stage1) {
17271729 try self.work_queue.writeItem(.{ .analyze_pkg = std_pkg });
1730 if (self.bin_file.options.is_test) {
1731 try self.work_queue.writeItem(.{ .analyze_pkg = module.main_pkg });
1732 }
17281733 }
17291734 }
17301735
......@@ -2053,24 +2058,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
20532058 assert(decl.has_tv);
20542059 assert(decl.ty.hasCodeGenBits());
20552060
2056 self.bin_file.updateDecl(module, decl) catch |err| switch (err) {
2057 error.OutOfMemory => return error.OutOfMemory,
2058 error.AnalysisFail => {
2059 decl.analysis = .codegen_failure;
2060 continue;
2061 },
2062 else => {
2063 try module.failed_decls.ensureUnusedCapacity(gpa, 1);
2064 module.failed_decls.putAssumeCapacityNoClobber(decl, try Module.ErrorMsg.create(
2065 gpa,
2066 decl.srcLoc(),
2067 "unable to codegen: {s}",
2068 .{@errorName(err)},
2069 ));
2070 decl.analysis = .codegen_failure_retryable;
2071 continue;
2072 },
2073 };
2061 try module.linkerUpdateDecl(decl);
20742062 },
20752063 },
20762064 .codegen_func => |func| switch (func.owner_decl.analysis) {
......@@ -2396,6 +2384,14 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
23962384 };
23972385 },
23982386 };
2387
2388 if (self.bin_file.options.is_test and self.totalErrorCount() == 0) {
2389 // The `test_functions` decl has been intentionally postponed until now,
2390 // at which point we must populate it with the list of test functions that
2391 // have been discovered and not filtered out.
2392 const mod = self.bin_file.options.module.?;
2393 try mod.populateTestFunctions();
2394 }
23992395}
24002396
24012397const AstGenSrc = union(enum) {
src/Module.zig+122-13
......@@ -112,6 +112,8 @@ compile_log_text: ArrayListUnmanaged(u8) = .{},
112112
113113emit_h: ?*GlobalEmitH,
114114
115test_functions: std.AutoArrayHashMapUnmanaged(*Decl, void) = .{},
116
115117/// A `Module` has zero or one of these depending on whether `-femit-h` is enabled.
116118pub const GlobalEmitH = struct {
117119 /// Where to put the output.
......@@ -282,6 +284,7 @@ pub const Decl = struct {
282284 pub fn destroy(decl: *Decl, module: *Module) void {
283285 const gpa = module.gpa;
284286 log.debug("destroy {*} ({s})", .{ decl, decl.name });
287 _ = module.test_functions.swapRemove(decl);
285288 if (decl.deletion_flag) {
286289 assert(module.deletion_set.swapRemove(decl));
287290 }
......@@ -3319,6 +3322,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
33193322 // the test name filter.
33203323 if (!mod.comp.bin_file.options.is_test) break :blk false;
33213324 if (decl_pkg != mod.main_pkg) break :blk false;
3325 try mod.test_functions.put(gpa, new_decl, {});
33223326 break :blk true;
33233327 },
33243328 else => blk: {
......@@ -3326,6 +3330,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
33263330 if (!mod.comp.bin_file.options.is_test) break :blk false;
33273331 if (decl_pkg != mod.main_pkg) break :blk false;
33283332 // TODO check the name against --test-filter
3333 try mod.test_functions.put(gpa, new_decl, {});
33293334 break :blk true;
33303335 },
33313336 };
......@@ -3765,17 +3770,38 @@ pub fn createAnonymousDeclNamed(
37653770 scope: *Scope,
37663771 typed_value: TypedValue,
37673772 name: [:0]u8,
3773) !*Decl {
3774 return mod.createAnonymousDeclFromDeclNamed(scope.ownerDecl().?, typed_value, name);
3775}
3776
3777pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl {
3778 return mod.createAnonymousDeclFromDecl(scope.ownerDecl().?, typed_value);
3779}
3780
3781pub fn createAnonymousDeclFromDecl(mod: *Module, owner_decl: *Decl, tv: TypedValue) !*Decl {
3782 const name_index = mod.getNextAnonNameIndex();
3783 const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{
3784 owner_decl.name, name_index,
3785 });
3786 return mod.createAnonymousDeclFromDeclNamed(owner_decl, tv, name);
3787}
3788
3789/// Takes ownership of `name` even if it returns an error.
3790pub fn createAnonymousDeclFromDeclNamed(
3791 mod: *Module,
3792 owner_decl: *Decl,
3793 typed_value: TypedValue,
3794 name: [:0]u8,
37683795) !*Decl {
37693796 errdefer mod.gpa.free(name);
37703797
3771 const scope_decl = scope.ownerDecl().?;
3772 const namespace = scope_decl.namespace;
3798 const namespace = owner_decl.namespace;
37733799 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);
37743800
3775 const new_decl = try mod.allocateNewDecl(namespace, scope_decl.src_node);
3801 const new_decl = try mod.allocateNewDecl(namespace, owner_decl.src_node);
37763802
37773803 new_decl.name = name;
3778 new_decl.src_line = scope_decl.src_line;
3804 new_decl.src_line = owner_decl.src_line;
37793805 new_decl.ty = typed_value.ty;
37803806 new_decl.val = typed_value.val;
37813807 new_decl.has_tv = true;
......@@ -3796,15 +3822,6 @@ pub fn createAnonymousDeclNamed(
37963822 return new_decl;
37973823}
37983824
3799pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl {
3800 const scope_decl = scope.ownerDecl().?;
3801 const name_index = mod.getNextAnonNameIndex();
3802 const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{
3803 scope_decl.name, name_index,
3804 });
3805 return mod.createAnonymousDeclNamed(scope, typed_value, name);
3806}
3807
38083825pub fn getNextAnonNameIndex(mod: *Module) usize {
38093826 return @atomicRmw(usize, &mod.next_anon_name_index, .Add, 1, .Monotonic);
38103827}
......@@ -4801,3 +4818,95 @@ pub fn processExports(mod: *Module) !void {
48014818 };
48024819 }
48034820}
4821
4822pub fn populateTestFunctions(mod: *Module) !void {
4823 const gpa = mod.gpa;
4824 const builtin_pkg = mod.main_pkg.table.get("builtin").?;
4825 const builtin_file = (mod.importPkg(builtin_pkg) catch unreachable).file;
4826 const builtin_namespace = builtin_file.root_decl.?.namespace;
4827 const decl = builtin_namespace.decls.get("test_functions").?;
4828 var buf: Type.Payload.ElemType = undefined;
4829 const tmp_test_fn_ty = decl.ty.slicePtrFieldType(&buf).elemType();
4830
4831 const array_decl = d: {
4832 // Add mod.test_functions to an array decl then make the test_functions
4833 // decl reference it as a slice.
4834 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
4835 errdefer new_decl_arena.deinit();
4836 const arena = &new_decl_arena.allocator;
4837
4838 const test_fn_vals = try arena.alloc(Value, mod.test_functions.count());
4839 const array_decl = try mod.createAnonymousDeclFromDecl(decl, .{
4840 .ty = try Type.Tag.array.create(arena, .{
4841 .len = test_fn_vals.len,
4842 .elem_type = try tmp_test_fn_ty.copy(arena),
4843 }),
4844 .val = try Value.Tag.array.create(arena, test_fn_vals),
4845 });
4846 for (mod.test_functions.keys()) |test_decl, i| {
4847 const test_name_slice = mem.sliceTo(test_decl.name, 0);
4848 const test_name_decl = n: {
4849 var name_decl_arena = std.heap.ArenaAllocator.init(gpa);
4850 errdefer name_decl_arena.deinit();
4851 const bytes = try name_decl_arena.allocator.dupe(u8, test_name_slice);
4852 const test_name_decl = try mod.createAnonymousDeclFromDecl(array_decl, .{
4853 .ty = try Type.Tag.array_u8.create(&name_decl_arena.allocator, bytes.len),
4854 .val = try Value.Tag.bytes.create(&name_decl_arena.allocator, bytes),
4855 });
4856 try test_name_decl.finalizeNewArena(&name_decl_arena);
4857 break :n test_name_decl;
4858 };
4859 try mod.linkerUpdateDecl(test_name_decl);
4860
4861 const field_vals = try arena.create([3]Value);
4862 field_vals.* = .{
4863 try Value.Tag.slice.create(arena, .{
4864 .ptr = try Value.Tag.decl_ref.create(arena, test_name_decl),
4865 .len = try Value.Tag.int_u64.create(arena, test_name_slice.len),
4866 }), // name
4867 try Value.Tag.decl_ref.create(arena, test_decl), // func
4868 Value.initTag(.null_value), // async_frame_size
4869 };
4870 test_fn_vals[i] = try Value.Tag.@"struct".create(arena, field_vals);
4871 }
4872
4873 try array_decl.finalizeNewArena(&new_decl_arena);
4874 break :d array_decl;
4875 };
4876 try mod.linkerUpdateDecl(array_decl);
4877
4878 {
4879 var arena_instance = decl.value_arena.?.promote(gpa);
4880 defer decl.value_arena.?.* = arena_instance.state;
4881 const arena = &arena_instance.allocator;
4882
4883 decl.ty = try Type.Tag.const_slice.create(arena, try tmp_test_fn_ty.copy(arena));
4884 decl.val = try Value.Tag.slice.create(arena, .{
4885 .ptr = try Value.Tag.decl_ref.create(arena, array_decl),
4886 .len = try Value.Tag.int_u64.create(arena, mod.test_functions.count()),
4887 });
4888 }
4889 try mod.linkerUpdateDecl(decl);
4890}
4891
4892pub fn linkerUpdateDecl(mod: *Module, decl: *Decl) !void {
4893 mod.comp.bin_file.updateDecl(mod, decl) catch |err| switch (err) {
4894 error.OutOfMemory => return error.OutOfMemory,
4895 error.AnalysisFail => {
4896 decl.analysis = .codegen_failure;
4897 return;
4898 },
4899 else => {
4900 const gpa = mod.gpa;
4901 try mod.failed_decls.ensureUnusedCapacity(gpa, 1);
4902 mod.failed_decls.putAssumeCapacityNoClobber(decl, try ErrorMsg.create(
4903 gpa,
4904 decl.srcLoc(),
4905 "unable to codegen: {s}",
4906 .{@errorName(err)},
4907 ));
4908 decl.analysis = .codegen_failure_retryable;
4909 return;
4910 },
4911 };
4912}
src/codegen/llvm.zig+91-43
......@@ -500,7 +500,18 @@ pub const DeclGen = struct {
500500 } else if (decl.val.castTag(.extern_fn)) |extern_fn| {
501501 _ = try self.resolveLlvmFunction(extern_fn.data);
502502 } else {
503 _ = try self.resolveGlobalDecl(decl);
503 const global = try self.resolveGlobalDecl(decl);
504 assert(decl.has_tv);
505 const init_val = if (decl.val.castTag(.variable)) |payload| init_val: {
506 const variable = payload.data;
507 break :init_val variable.init;
508 } else init_val: {
509 global.setGlobalConstant(.True);
510 break :init_val decl.val;
511 };
512
513 const llvm_init = try self.genTypedValue(.{ .ty = decl.ty, .val = init_val });
514 llvm.setInitializer(global, llvm_init);
504515 }
505516 }
506517
......@@ -548,25 +559,11 @@ pub const DeclGen = struct {
548559 }
549560
550561 fn resolveGlobalDecl(self: *DeclGen, decl: *Module.Decl) error{ OutOfMemory, CodegenFail }!*const llvm.Value {
551 if (self.llvmModule().getNamedGlobal(decl.name)) |val| return val;
552
553 assert(decl.has_tv);
554
562 const llvm_module = self.object.llvm_module;
563 if (llvm_module.getNamedGlobal(decl.name)) |val| return val;
555564 // TODO: remove this redundant `llvmType`, it is also called in `genTypedValue`.
556565 const llvm_type = try self.llvmType(decl.ty);
557 const global = self.llvmModule().addGlobal(llvm_type, decl.name);
558 const init_val = if (decl.val.castTag(.variable)) |payload| init_val: {
559 const variable = payload.data;
560 break :init_val variable.init;
561 } else init_val: {
562 global.setGlobalConstant(.True);
563 break :init_val decl.val;
564 };
565
566 const llvm_init = try self.genTypedValue(.{ .ty = decl.ty, .val = init_val }, null);
567 llvm.setInitializer(global, llvm_init);
568
569 return global;
566 return llvm_module.addGlobal(llvm_type, decl.name);
570567 }
571568
572569 fn llvmType(self: *DeclGen, t: Type) error{ OutOfMemory, CodegenFail }!*const llvm.Type {
......@@ -596,7 +593,8 @@ pub const DeclGen = struct {
596593 },
597594 .Array => {
598595 const elem_type = try self.llvmType(t.elemType());
599 return elem_type.arrayType(@intCast(c_uint, t.abiSize(self.module.getTarget())));
596 const total_len = t.arrayLen() + @boolToInt(t.sentinel() != null);
597 return elem_type.arrayType(@intCast(c_uint, total_len));
600598 },
601599 .Optional => {
602600 if (!t.isPtrLikeOptional()) {
......@@ -674,8 +672,7 @@ pub const DeclGen = struct {
674672 }
675673 }
676674
677 // TODO: figure out a way to remove the FuncGen argument
678 fn genTypedValue(self: *DeclGen, tv: TypedValue, fg: ?*FuncGen) error{ OutOfMemory, CodegenFail }!*const llvm.Value {
675 fn genTypedValue(self: *DeclGen, tv: TypedValue) error{ OutOfMemory, CodegenFail }!*const llvm.Value {
679676 const llvm_type = try self.llvmType(tv.ty);
680677
681678 if (tv.val.isUndef())
......@@ -711,20 +708,36 @@ pub const DeclGen = struct {
711708 usize_type.constNull(),
712709 };
713710
714 // TODO: consider using buildInBoundsGEP2 for opaque pointers
715 return fg.?.builder.buildInBoundsGEP(val, &indices, 2, "");
711 return val.constInBoundsGEP(&indices, indices.len);
716712 },
717713 .ref_val => {
718 const elem_value = tv.val.castTag(.ref_val).?.data;
719 const elem_type = tv.ty.castPointer().?.data;
720 const alloca = fg.?.buildAlloca(try self.llvmType(elem_type));
721 _ = fg.?.builder.buildStore(try self.genTypedValue(.{ .ty = elem_type, .val = elem_value }, fg), alloca);
722 return alloca;
714 //const elem_value = tv.val.castTag(.ref_val).?.data;
715 //const elem_type = tv.ty.castPointer().?.data;
716 //const alloca = fg.?.buildAlloca(try self.llvmType(elem_type));
717 //_ = fg.?.builder.buildStore(try self.genTypedValue(.{ .ty = elem_type, .val = elem_value }, fg), alloca);
718 //return alloca;
719 // TODO eliminate the ref_val Value Tag
720 return self.todo("implement const of pointer tag ref_val", .{});
723721 },
724722 .variable => {
725723 const variable = tv.val.castTag(.variable).?.data;
726724 return self.resolveGlobalDecl(variable.owner_decl);
727725 },
726 .slice => {
727 const slice = tv.val.castTag(.slice).?.data;
728 var buf: Type.Payload.ElemType = undefined;
729 const fields: [2]*const llvm.Value = .{
730 try self.genTypedValue(.{
731 .ty = tv.ty.slicePtrFieldType(&buf),
732 .val = slice.ptr,
733 }),
734 try self.genTypedValue(.{
735 .ty = Type.initTag(.usize),
736 .val = slice.len,
737 }),
738 };
739 return self.context.constStruct(&fields, fields.len, .False);
740 },
728741 else => |tag| return self.todo("implement const of pointer type '{}' ({})", .{ tv.ty, tag }),
729742 },
730743 .Array => {
......@@ -734,10 +747,28 @@ pub const DeclGen = struct {
734747 return self.todo("handle other sentinel values", .{});
735748 } else false;
736749
737 return self.context.constString(payload.data.ptr, @intCast(c_uint, payload.data.len), llvm.Bool.fromBool(!zero_sentinel));
738 } else {
739 return self.todo("handle more array values", .{});
750 return self.context.constString(
751 payload.data.ptr,
752 @intCast(c_uint, payload.data.len),
753 llvm.Bool.fromBool(!zero_sentinel),
754 );
755 }
756 if (tv.val.castTag(.array)) |payload| {
757 const gpa = self.gpa;
758 const elem_ty = tv.ty.elemType();
759 const elem_vals = payload.data;
760 const llvm_elems = try gpa.alloc(*const llvm.Value, elem_vals.len);
761 defer gpa.free(llvm_elems);
762 for (elem_vals) |elem_val, i| {
763 llvm_elems[i] = try self.genTypedValue(.{ .ty = elem_ty, .val = elem_val });
764 }
765 const llvm_elem_ty = try self.llvmType(elem_ty);
766 return llvm_elem_ty.constArray(
767 llvm_elems.ptr,
768 @intCast(c_uint, llvm_elems.len),
769 );
740770 }
771 return self.todo("handle more array values", .{});
741772 },
742773 .Optional => {
743774 if (!tv.ty.isPtrLikeOptional()) {
......@@ -750,26 +781,25 @@ pub const DeclGen = struct {
750781 llvm_child_type.constNull(),
751782 self.context.intType(1).constNull(),
752783 };
753 return self.context.constStruct(&optional_values, 2, .False);
784 return self.context.constStruct(&optional_values, optional_values.len, .False);
754785 } else {
755786 var optional_values: [2]*const llvm.Value = .{
756 try self.genTypedValue(.{ .ty = child_type, .val = tv.val }, fg),
787 try self.genTypedValue(.{ .ty = child_type, .val = tv.val }),
757788 self.context.intType(1).constAllOnes(),
758789 };
759 return self.context.constStruct(&optional_values, 2, .False);
790 return self.context.constStruct(&optional_values, optional_values.len, .False);
760791 }
761792 } else {
762793 return self.todo("implement const of optional pointer", .{});
763794 }
764795 },
765796 .Fn => {
766 const fn_decl = if (tv.val.castTag(.extern_fn)) |extern_fn|
767 extern_fn.data
768 else if (tv.val.castTag(.function)) |func_payload|
769 func_payload.data.owner_decl
770 else
771 unreachable;
772
797 const fn_decl = switch (tv.val.tag()) {
798 .extern_fn => tv.val.castTag(.extern_fn).?.data,
799 .function => tv.val.castTag(.function).?.data.owner_decl,
800 .decl_ref => tv.val.castTag(.decl_ref).?.data,
801 else => unreachable,
802 };
773803 return self.resolveLlvmFunction(fn_decl);
774804 },
775805 .ErrorSet => {
......@@ -793,11 +823,29 @@ pub const DeclGen = struct {
793823
794824 if (!payload_type.hasCodeGenBits()) {
795825 // We use the error type directly as the type.
796 return self.genTypedValue(.{ .ty = error_type, .val = sub_val }, fg);
826 return self.genTypedValue(.{ .ty = error_type, .val = sub_val });
797827 }
798828
799829 return self.todo("implement error union const of type '{}'", .{tv.ty});
800830 },
831 .Struct => {
832 const fields_len = tv.ty.structFieldCount();
833 const field_vals = tv.val.castTag(.@"struct").?.data;
834 const gpa = self.gpa;
835 const llvm_fields = try gpa.alloc(*const llvm.Value, fields_len);
836 defer gpa.free(llvm_fields);
837 for (llvm_fields) |*llvm_field, i| {
838 llvm_field.* = try self.genTypedValue(.{
839 .ty = tv.ty.structFieldType(i),
840 .val = field_vals[i],
841 });
842 }
843 return self.context.constStruct(
844 llvm_fields.ptr,
845 @intCast(c_uint, llvm_fields.len),
846 .False,
847 );
848 },
801849 else => return self.todo("implement const of type '{}'", .{tv.ty}),
802850 }
803851 }
......@@ -869,7 +917,7 @@ pub const FuncGen = struct {
869917
870918 fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) !*const llvm.Value {
871919 if (self.air.value(inst)) |val| {
872 return self.dg.genTypedValue(.{ .ty = self.air.typeOf(inst), .val = val }, self);
920 return self.dg.genTypedValue(.{ .ty = self.air.typeOf(inst), .val = val });
873921 }
874922 const inst_index = Air.refToIndex(inst).?;
875923 if (self.func_inst_table.get(inst_index)) |value| return value;
src/codegen/llvm/bindings.zig+14-2
......@@ -49,7 +49,12 @@ pub const Context = opaque {
4949 extern fn LLVMConstStringInContext(C: *const Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) *const Value;
5050
5151 pub const constStruct = LLVMConstStructInContext;
52 extern fn LLVMConstStructInContext(C: *const Context, ConstantVals: [*]*const Value, Count: c_uint, Packed: Bool) *const Value;
52 extern fn LLVMConstStructInContext(
53 C: *const Context,
54 ConstantVals: [*]const *const Value,
55 Count: c_uint,
56 Packed: Bool,
57 ) *const Value;
5358
5459 pub const createBasicBlock = LLVMCreateBasicBlockInContext;
5560 extern fn LLVMCreateBasicBlockInContext(C: *const Context, Name: [*:0]const u8) *const BasicBlock;
......@@ -100,6 +105,13 @@ pub const Value = opaque {
100105
101106 pub const setAliasee = LLVMAliasSetAliasee;
102107 extern fn LLVMAliasSetAliasee(Alias: *const Value, Aliasee: *const Value) void;
108
109 pub const constInBoundsGEP = LLVMConstInBoundsGEP;
110 extern fn LLVMConstInBoundsGEP(
111 ConstantVal: *const Value,
112 ConstantIndices: [*]const *const Value,
113 NumIndices: c_uint,
114 ) *const Value;
103115};
104116
105117pub const Type = opaque {
......@@ -113,7 +125,7 @@ pub const Type = opaque {
113125 extern fn LLVMConstInt(IntTy: *const Type, N: c_ulonglong, SignExtend: Bool) *const Value;
114126
115127 pub const constArray = LLVMConstArray;
116 extern fn LLVMConstArray(ElementTy: *const Type, ConstantVals: ?[*]*const Value, Length: c_uint) *const Value;
128 extern fn LLVMConstArray(ElementTy: *const Type, ConstantVals: [*]*const Value, Length: c_uint) *const Value;
117129
118130 pub const getUndef = LLVMGetUndef;
119131 extern fn LLVMGetUndef(Ty: *const Type) *const Value;
src/type.zig+22
......@@ -1526,6 +1526,8 @@ pub const Type = extern union {
15261526 .var_args_param => unreachable,
15271527
15281528 .@"struct" => {
1529 const s = self.castTag(.@"struct").?.data;
1530 assert(s.status == .have_layout);
15291531 @panic("TODO abiSize struct");
15301532 },
15311533 .enum_simple, .enum_full, .enum_nonexhaustive => {
......@@ -2768,6 +2770,26 @@ pub const Type = extern union {
27682770 }
27692771 }
27702772
2773 pub fn structFieldCount(ty: Type) usize {
2774 switch (ty.tag()) {
2775 .@"struct" => {
2776 const struct_obj = ty.castTag(.@"struct").?.data;
2777 return struct_obj.fields.count();
2778 },
2779 else => unreachable,
2780 }
2781 }
2782
2783 pub fn structFieldType(ty: Type, index: usize) Type {
2784 switch (ty.tag()) {
2785 .@"struct" => {
2786 const struct_obj = ty.castTag(.@"struct").?.data;
2787 return struct_obj.fields.values()[index].ty;
2788 },
2789 else => unreachable,
2790 }
2791 }
2792
27712793 pub fn declSrcLoc(ty: Type) Module.SrcLoc {
27722794 switch (ty.tag()) {
27732795 .enum_full, .enum_nonexhaustive => {
src/value.zig+68-5
......@@ -112,6 +112,10 @@ pub const Value = extern union {
112112 /// This value is repeated some number of times. The amount of times to repeat
113113 /// is stored externally.
114114 repeated,
115 /// Each element stored as a `Value`.
116 array,
117 /// Pointer and length as sub `Value` objects.
118 slice,
115119 float_16,
116120 float_32,
117121 float_64,
......@@ -217,6 +221,9 @@ pub const Value = extern union {
217221 .enum_literal,
218222 => Payload.Bytes,
219223
224 .array => Payload.Array,
225 .slice => Payload.Slice,
226
220227 .enum_field_index => Payload.U32,
221228
222229 .ty => Payload.Ty,
......@@ -442,6 +449,28 @@ pub const Value = extern union {
442449 };
443450 return Value{ .ptr_otherwise = &new_payload.base };
444451 },
452 .array => {
453 const payload = self.castTag(.array).?;
454 const new_payload = try allocator.create(Payload.Array);
455 new_payload.* = .{
456 .base = payload.base,
457 .data = try allocator.alloc(Value, payload.data.len),
458 };
459 std.mem.copy(Value, new_payload.data, payload.data);
460 return Value{ .ptr_otherwise = &new_payload.base };
461 },
462 .slice => {
463 const payload = self.castTag(.slice).?;
464 const new_payload = try allocator.create(Payload.Slice);
465 new_payload.* = .{
466 .base = payload.base,
467 .data = .{
468 .ptr = try payload.data.ptr.copy(allocator),
469 .len = try payload.data.len.copy(allocator),
470 },
471 };
472 return Value{ .ptr_otherwise = &new_payload.base };
473 },
445474 .float_16 => return self.copyPayloadShallow(allocator, Payload.Float_16),
446475 .float_32 => return self.copyPayloadShallow(allocator, Payload.Float_32),
447476 .float_64 => return self.copyPayloadShallow(allocator, Payload.Float_64),
......@@ -605,6 +634,8 @@ pub const Value = extern union {
605634 try out_stream.writeAll("(repeated) ");
606635 val = val.castTag(.repeated).?.data;
607636 },
637 .array => return out_stream.writeAll("(array)"),
638 .slice => return out_stream.writeAll("(slice)"),
608639 .float_16 => return out_stream.print("{}", .{val.castTag(.float_16).?.data}),
609640 .float_32 => return out_stream.print("{}", .{val.castTag(.float_32).?.data}),
610641 .float_64 => return out_stream.print("{}", .{val.castTag(.float_64).?.data}),
......@@ -729,6 +760,8 @@ pub const Value = extern union {
729760 .field_ptr,
730761 .bytes,
731762 .repeated,
763 .array,
764 .slice,
732765 .float_16,
733766 .float_32,
734767 .float_64,
......@@ -1075,6 +1108,8 @@ pub const Value = extern union {
10751108 return orderAgainstZero(lhs).compare(op);
10761109 }
10771110
1111 /// TODO we can't compare value equality without also knowing the type to treat
1112 /// the values as
10781113 pub fn eql(a: Value, b: Value) bool {
10791114 const a_tag = a.tag();
10801115 const b_tag = b.tag();
......@@ -1109,6 +1144,8 @@ pub const Value = extern union {
11091144 return @truncate(u32, self.hash());
11101145 }
11111146
1147 /// TODO we can't hash without also knowing the type of the value.
1148 /// we have to hash as if there were a canonical value memory layout.
11121149 pub fn hash(self: Value) u64 {
11131150 var hasher = std.hash.Wyhash.init(0);
11141151
......@@ -1203,6 +1240,15 @@ pub const Value = extern union {
12031240 const payload = self.castTag(.bytes).?;
12041241 hasher.update(payload.data);
12051242 },
1243 .repeated => {
1244 @panic("TODO Value.hash for repeated");
1245 },
1246 .array => {
1247 @panic("TODO Value.hash for array");
1248 },
1249 .slice => {
1250 @panic("TODO Value.hash for slice");
1251 },
12061252 .int_u64 => {
12071253 const payload = self.castTag(.int_u64).?;
12081254 std.hash.autoHash(&hasher, payload.data);
......@@ -1211,10 +1257,6 @@ pub const Value = extern union {
12111257 const payload = self.castTag(.int_i64).?;
12121258 std.hash.autoHash(&hasher, payload.data);
12131259 },
1214 .repeated => {
1215 const payload = self.castTag(.repeated).?;
1216 std.hash.autoHash(&hasher, payload.data.hash());
1217 },
12181260 .ref_val => {
12191261 const payload = self.castTag(.ref_val).?;
12201262 std.hash.autoHash(&hasher, payload.data.hash());
......@@ -1340,6 +1382,8 @@ pub const Value = extern union {
13401382 return switch (val.tag()) {
13411383 .empty_array => 0,
13421384 .bytes => val.castTag(.bytes).?.data.len,
1385 .array => val.castTag(.array).?.data.len,
1386 .slice => val.castTag(.slice).?.data.len.toUnsignedInt(),
13431387 .ref_val => sliceLen(val.castTag(.ref_val).?.data),
13441388 .decl_ref => {
13451389 const decl = val.castTag(.decl_ref).?.data;
......@@ -1364,6 +1408,9 @@ pub const Value = extern union {
13641408 // No matter the index; all the elements are the same!
13651409 .repeated => return self.castTag(.repeated).?.data,
13661410
1411 .array => return self.castTag(.array).?.data[index],
1412 .slice => return self.castTag(.slice).?.data.ptr.elemValue(allocator, index),
1413
13671414 else => unreachable,
13681415 }
13691416 }
......@@ -1450,7 +1497,8 @@ pub const Value = extern union {
14501497 }
14511498
14521499 /// Valid for all types. Asserts the value is not undefined.
1453 pub fn isType(self: Value) bool {
1500 /// TODO this function is a code smell and should be deleted
1501 fn isType(self: Value) bool {
14541502 return switch (self.tag()) {
14551503 .ty,
14561504 .int_type,
......@@ -1528,6 +1576,8 @@ pub const Value = extern union {
15281576 .field_ptr,
15291577 .bytes,
15301578 .repeated,
1579 .array,
1580 .slice,
15311581 .float_16,
15321582 .float_32,
15331583 .float_64,
......@@ -1638,6 +1688,19 @@ pub const Value = extern union {
16381688 data: []const u8,
16391689 };
16401690
1691 pub const Array = struct {
1692 base: Payload,
1693 data: []Value,
1694 };
1695
1696 pub const Slice = struct {
1697 base: Payload,
1698 data: struct {
1699 ptr: Value,
1700 len: Value,
1701 },
1702 };
1703
16411704 pub const Ty = struct {
16421705 base: Payload,
16431706 data: Type,