authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-08-22 02:13:18-04:00
committergravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-08-30 22:54:33-04:00
log353eec73cad82ed0783fe76837cbb94095f79f9d
tree8d41af9a0c1a28303e65ca7ef61d18c9ace328a7
parent861b784454aa2ed3365b31dc18f9f569bc637703

stage2: add array concatenation


2 files changed, 89 insertions(+), 2 deletions(-)

src/Sema.zig+80-2
...@@ -5512,8 +5512,86 @@ fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr...@@ -5512,8 +5512,86 @@ fn zirArrayCat(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
5512 const tracy = trace(@src());5512 const tracy = trace(@src());
5513 defer tracy.end();5513 defer tracy.end();
55145514
5515 _ = inst;5515 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
5516 return sema.mod.fail(&block.base, sema.src, "TODO implement zirArrayCat", .{});5516 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
5517 const lhs = sema.resolveInst(extra.lhs);
5518 const rhs = sema.resolveInst(extra.rhs);
5519 const lhs_ty = sema.typeOf(lhs);
5520 const rhs_ty = sema.typeOf(rhs);
5521 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
5522 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
5523
5524 const lhs_info = getArrayCatInfo(lhs_ty) orelse
5525 return sema.mod.fail(&block.base, lhs_src, "expected array, found '{}'", .{lhs_ty});
5526 const rhs_info = getArrayCatInfo(rhs_ty) orelse
5527 return sema.mod.fail(&block.base, rhs_src, "expected array, found '{}'", .{rhs_ty});
5528 if (!lhs_info.elem_type.eql(rhs_info.elem_type)) {
5529 return sema.mod.fail(&block.base, rhs_src, "expected array of type '{}', found '{}'", .{ lhs_info.elem_type, rhs_ty });
5530 }
5531
5532 // When there is a sentinel mismatch, no sentinel on the result. The type system
5533 // will catch this if it is a problem.
5534 var res_sent: ?Value = null;
5535 if (rhs_info.sentinel != null and lhs_info.sentinel != null) {
5536 if (rhs_info.sentinel.?.eql(lhs_info.sentinel.?, lhs_info.elem_type)) {
5537 res_sent = lhs_info.sentinel.?;
5538 }
5539 }
5540
5541 if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| {
5542 if (try sema.resolveDefinedValue(block, rhs_src, rhs)) |rhs_val| {
5543 const final_len = lhs_info.len + rhs_info.len;
5544 if (lhs_ty.zigTypeTag() == .Pointer) {
5545 var anon_decl = try block.startAnonDecl();
5546 defer anon_decl.deinit();
5547
5548 const lhs_sub_val = (try lhs_val.pointerDeref(anon_decl.arena())).?;
5549 const rhs_sub_val = (try rhs_val.pointerDeref(anon_decl.arena())).?;
5550 const buf = try anon_decl.arena().alloc(Value, final_len);
5551 {
5552 var i: u64 = 0;
5553 while (i < lhs_info.len) : (i += 1) {
5554 const val = try lhs_sub_val.elemValue(sema.arena, i);
5555 buf[i] = try val.copy(anon_decl.arena());
5556 }
5557 }
5558 {
5559 var i: u64 = 0;
5560 while (i < rhs_info.len) : (i += 1) {
5561 const val = try rhs_sub_val.elemValue(sema.arena, i);
5562 buf[lhs_info.len + i] = try val.copy(anon_decl.arena());
5563 }
5564 }
5565 const ty = if (res_sent) |rs|
5566 try Type.Tag.array_sentinel.create(anon_decl.arena(), .{ .len = final_len, .elem_type = lhs_info.elem_type, .sentinel = rs })
5567 else
5568 try Type.Tag.array.create(anon_decl.arena(), .{ .len = final_len, .elem_type = lhs_info.elem_type });
5569 const val = try Value.Tag.array.create(anon_decl.arena(), buf);
5570 return sema.analyzeDeclRef(try anon_decl.finish(
5571 ty,
5572 val,
5573 ));
5574 }
5575 return sema.mod.fail(&block.base, lhs_src, "TODO array_cat more types of Values", .{});
5576 } else {
5577 return sema.mod.fail(&block.base, lhs_src, "TODO runtime array_cat", .{});
5578 }
5579 } else {
5580 return sema.mod.fail(&block.base, lhs_src, "TODO runtime array_cat", .{});
5581 }
5582}
5583
5584fn getArrayCatInfo(t: Type) ?Type.ArrayInfo {
5585 return switch (t.zigTypeTag()) {
5586 .Array => t.arrayInfo(),
5587 .Pointer => blk: {
5588 const ptrinfo = t.ptrInfo().data;
5589 if (ptrinfo.pointee_type.zigTypeTag() != .Array) return null;
5590 if (ptrinfo.size != .One) return null;
5591 break :blk ptrinfo.pointee_type.arrayInfo();
5592 },
5593 else => null,
5594 };
5517}5595}
55185596
5519fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {5597fn zirArrayMul(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
src/type.zig+9
...@@ -273,6 +273,15 @@ pub const Type = extern union {...@@ -273,6 +273,15 @@ pub const Type = extern union {
273 };273 };
274 }274 }
275275
276 pub const ArrayInfo = struct { elem_type: Type, sentinel: ?Value = null, len: u64 };
277 pub fn arrayInfo(self: Type) ArrayInfo {
278 return .{
279 .len = self.arrayLen(),
280 .sentinel = self.sentinel(),
281 .elem_type = self.elemType(),
282 };
283 }
284
276 pub fn ptrInfo(self: Type) Payload.Pointer {285 pub fn ptrInfo(self: Type) Payload.Pointer {
277 switch (self.tag()) {286 switch (self.tag()) {
278 .single_const_pointer_to_comptime_int => return .{ .data = .{287 .single_const_pointer_to_comptime_int => return .{ .data = .{