authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-19 19:12:01+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-19 16:12:29-07:00
log5fdcb1a792e271c67f3aec36f8aca9e6c5503013
tree201b9923a86a011cbdee2c2767ceda2b742fdc18
parent9801047bdb26c86da430310aa5d043f6899adbfe

stage2: emit zir variable fix, array type and enum literal support


2 files changed, 75 insertions(+), 1 deletions(-)

src-self-hosted/value.zig+10-1
...@@ -218,7 +218,7 @@ pub const Value = extern union {...@@ -218,7 +218,7 @@ pub const Value = extern union {
218 };218 };
219 return Value{ .ptr_otherwise = &new_payload.base };219 return Value{ .ptr_otherwise = &new_payload.base };
220 },220 },
221 .enum_literal, .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes),221 .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes),
222 .repeated => {222 .repeated => {
223 const payload = @fieldParentPtr(Payload.Repeated, "base", self.ptr_otherwise);223 const payload = @fieldParentPtr(Payload.Repeated, "base", self.ptr_otherwise);
224 const new_payload = try allocator.create(Payload.Repeated);224 const new_payload = try allocator.create(Payload.Repeated);
...@@ -232,6 +232,15 @@ pub const Value = extern union {...@@ -232,6 +232,15 @@ pub const Value = extern union {
232 .float_32 => return self.copyPayloadShallow(allocator, Payload.Float_32),232 .float_32 => return self.copyPayloadShallow(allocator, Payload.Float_32),
233 .float_64 => return self.copyPayloadShallow(allocator, Payload.Float_64),233 .float_64 => return self.copyPayloadShallow(allocator, Payload.Float_64),
234 .float_128 => return self.copyPayloadShallow(allocator, Payload.Float_128),234 .float_128 => return self.copyPayloadShallow(allocator, Payload.Float_128),
235 .enum_literal => {
236 const payload = @fieldParentPtr(Payload.Bytes, "base", self.ptr_otherwise);
237 const new_payload = try allocator.create(Payload.Bytes);
238 new_payload.* = .{
239 .base = payload.base,
240 .data = try allocator.dupe(u8, payload.data),
241 };
242 return Value{ .ptr_otherwise = &new_payload.base };
243 },
235 }244 }
236 }245 }
237246
src-self-hosted/zir.zig+65
...@@ -1878,6 +1878,11 @@ const EmitZIR = struct {...@@ -1878,6 +1878,11 @@ const EmitZIR = struct {
1878 if (typed_value.val.cast(Value.Payload.DeclRef)) |decl_ref| {1878 if (typed_value.val.cast(Value.Payload.DeclRef)) |decl_ref| {
1879 const decl = decl_ref.decl;1879 const decl = decl_ref.decl;
1880 return try self.emitUnnamedDecl(try self.emitDeclRef(src, decl));1880 return try self.emitUnnamedDecl(try self.emitDeclRef(src, decl));
1881 } else if (typed_value.val.cast(Value.Payload.Variable)) |variable| {
1882 return self.emitTypedValue(src, .{
1883 .ty = typed_value.ty,
1884 .val = variable.variable.value.?,
1885 });
1881 }1886 }
1882 if (typed_value.val.isUndef()) {1887 if (typed_value.val.isUndef()) {
1883 const as_inst = try self.arena.allocator.create(Inst.BinOp);1888 const as_inst = try self.arena.allocator.create(Inst.BinOp);
...@@ -1967,6 +1972,21 @@ const EmitZIR = struct {...@@ -1967,6 +1972,21 @@ const EmitZIR = struct {
1967 return self.emitPrimitive(src, .@"true")1972 return self.emitPrimitive(src, .@"true")
1968 else1973 else
1969 return self.emitPrimitive(src, .@"false"),1974 return self.emitPrimitive(src, .@"false"),
1975 .EnumLiteral => {
1976 const enum_literal = @fieldParentPtr(Value.Payload.Bytes, "base", typed_value.val.ptr_otherwise);
1977 const inst = try self.arena.allocator.create(Inst.Str);
1978 inst.* = .{
1979 .base = .{
1980 .src = src,
1981 .tag = .enum_literal,
1982 },
1983 .positionals = .{
1984 .bytes = enum_literal.data,
1985 },
1986 .kw_args = .{},
1987 };
1988 return self.emitUnnamedDecl(&inst.base);
1989 },
1970 else => |t| std.debug.panic("TODO implement emitTypedValue for {}", .{@tagName(t)}),1990 else => |t| std.debug.panic("TODO implement emitTypedValue for {}", .{@tagName(t)}),
1971 }1991 }
1972 }1992 }
...@@ -2437,6 +2457,51 @@ const EmitZIR = struct {...@@ -2437,6 +2457,51 @@ const EmitZIR = struct {
2437 };2457 };
2438 return self.emitUnnamedDecl(&inst.base);2458 return self.emitUnnamedDecl(&inst.base);
2439 },2459 },
2460 .Array => {
2461 var len_pl = Value.Payload.Int_u64{ .int = ty.arrayLen() };
2462 const len = Value.initPayload(&len_pl.base);
2463
2464 const inst = if (ty.arraySentinel()) |sentinel| blk: {
2465 const inst = try self.arena.allocator.create(Inst.ArrayTypeSentinel);
2466 inst.* = .{
2467 .base = .{
2468 .src = src,
2469 .tag = .array_type,
2470 },
2471 .positionals = .{
2472 .len = (try self.emitTypedValue(src, .{
2473 .ty = Type.initTag(.usize),
2474 .val = len,
2475 })).inst,
2476 .sentinel = (try self.emitTypedValue(src, .{
2477 .ty = ty.elemType(),
2478 .val = sentinel,
2479 })).inst,
2480 .elem_type = (try self.emitType(src, ty.elemType())).inst,
2481 },
2482 .kw_args = .{},
2483 };
2484 break :blk &inst.base;
2485 } else blk: {
2486 const inst = try self.arena.allocator.create(Inst.BinOp);
2487 inst.* = .{
2488 .base = .{
2489 .src = src,
2490 .tag = .array_type,
2491 },
2492 .positionals = .{
2493 .lhs = (try self.emitTypedValue(src, .{
2494 .ty = Type.initTag(.usize),
2495 .val = len,
2496 })).inst,
2497 .rhs = (try self.emitType(src, ty.elemType())).inst,
2498 },
2499 .kw_args = .{},
2500 };
2501 break :blk &inst.base;
2502 };
2503 return self.emitUnnamedDecl(inst);
2504 },
2440 else => std.debug.panic("TODO implement emitType for {}", .{ty}),2505 else => std.debug.panic("TODO implement emitType for {}", .{ty}),
2441 },2506 },
2442 }2507 }