authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-28 19:12:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-29 02:29:37-07:00
log2b8e7deeda5d4d0d74f42265a4ddf466f91c8fc2
treed0bec44e92bf4042ec5de3f829daadf54ea0aa36
parent99d2d9bf64aad0cc4b46bdcef43432ba48349c7a

stage2: add ZIR emitType support for simple pointer types


2 files changed, 41 insertions(+), 0 deletions(-)

src-self-hosted/zir.zig+27
......@@ -183,6 +183,10 @@ pub const Inst = struct {
183183 shl,
184184 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.
185185 shr,
186 /// Create a const pointer type based on the element type. `*const T`
187 single_const_ptr_type,
188 /// Create a mutable pointer type based on the element type. `*T`
189 single_mut_ptr_type,
186190 /// Write a value to a pointer. For loading, see `deref`.
187191 store,
188192 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
......@@ -228,6 +232,8 @@ pub const Inst = struct {
228232 .ref,
229233 .bitcast_lvalue,
230234 .typeof,
235 .single_const_ptr_type,
236 .single_mut_ptr_type,
231237 => UnOp,
232238
233239 .add,
......@@ -348,6 +354,8 @@ pub const Inst = struct {
348354 .ret_type,
349355 .shl,
350356 .shr,
357 .single_const_ptr_type,
358 .single_mut_ptr_type,
351359 .store,
352360 .str,
353361 .sub,
......@@ -2095,6 +2103,25 @@ const EmitZIR = struct {
20952103 };
20962104 return self.emitUnnamedDecl(&inttype_inst.base);
20972105 },
2106 .Pointer => {
2107 if (ty.isSinglePointer()) {
2108 const inst = try self.arena.allocator.create(Inst.UnOp);
2109 const tag: Inst.Tag = if (ty.isConstPtr()) .single_const_ptr_type else .single_mut_ptr_type;
2110 inst.* = .{
2111 .base = .{
2112 .src = src,
2113 .tag = tag,
2114 },
2115 .positionals = .{
2116 .operand = (try self.emitType(src, ty.elemType())).inst,
2117 },
2118 .kw_args = .{},
2119 };
2120 return self.emitUnnamedDecl(&inst.base);
2121 } else {
2122 std.debug.panic("TODO implement emitType for {}", .{ty});
2123 }
2124 },
20982125 else => std.debug.panic("TODO implement emitType for {}", .{ty}),
20992126 },
21002127 }
src-self-hosted/zir_sema.zig+14
......@@ -50,6 +50,8 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
5050 .ref => return analyzeInstRef(mod, scope, old_inst.castTag(.ref).?),
5151 .ret_ptr => return analyzeInstRetPtr(mod, scope, old_inst.castTag(.ret_ptr).?),
5252 .ret_type => return analyzeInstRetType(mod, scope, old_inst.castTag(.ret_type).?),
53 .single_const_ptr_type => return analyzeInstSingleConstPtrType(mod, scope, old_inst.castTag(.single_const_ptr_type).?),
54 .single_mut_ptr_type => return analyzeInstSingleMutPtrType(mod, scope, old_inst.castTag(.single_mut_ptr_type).?),
5355 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),
5456 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),
5557 .int => {
......@@ -1137,3 +1139,15 @@ fn analyzeDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerErr
11371139
11381140 return decl;
11391141}
1142
1143fn analyzeInstSingleConstPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1144 const elem_type = try resolveType(mod, scope, inst.positionals.operand);
1145 const ty = try mod.singleConstPtrType(scope, inst.base.src, elem_type);
1146 return mod.constType(scope, inst.base.src, ty);
1147}
1148
1149fn analyzeInstSingleMutPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1150 const elem_type = try resolveType(mod, scope, inst.positionals.operand);
1151 const ty = try mod.singleMutPtrType(scope, inst.base.src, elem_type);
1152 return mod.constType(scope, inst.base.src, ty);
1153}