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 {...@@ -183,6 +183,10 @@ pub const Inst = struct {
183 shl,183 shl,
184 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.184 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.
185 shr,185 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,
186 /// Write a value to a pointer. For loading, see `deref`.190 /// Write a value to a pointer. For loading, see `deref`.
187 store,191 store,
188 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.192 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
...@@ -228,6 +232,8 @@ pub const Inst = struct {...@@ -228,6 +232,8 @@ pub const Inst = struct {
228 .ref,232 .ref,
229 .bitcast_lvalue,233 .bitcast_lvalue,
230 .typeof,234 .typeof,
235 .single_const_ptr_type,
236 .single_mut_ptr_type,
231 => UnOp,237 => UnOp,
232238
233 .add,239 .add,
...@@ -348,6 +354,8 @@ pub const Inst = struct {...@@ -348,6 +354,8 @@ pub const Inst = struct {
348 .ret_type,354 .ret_type,
349 .shl,355 .shl,
350 .shr,356 .shr,
357 .single_const_ptr_type,
358 .single_mut_ptr_type,
351 .store,359 .store,
352 .str,360 .str,
353 .sub,361 .sub,
...@@ -2095,6 +2103,25 @@ const EmitZIR = struct {...@@ -2095,6 +2103,25 @@ const EmitZIR = struct {
2095 };2103 };
2096 return self.emitUnnamedDecl(&inttype_inst.base);2104 return self.emitUnnamedDecl(&inttype_inst.base);
2097 },2105 },
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 },
2098 else => std.debug.panic("TODO implement emitType for {}", .{ty}),2125 else => std.debug.panic("TODO implement emitType for {}", .{ty}),
2099 },2126 },
2100 }2127 }
src-self-hosted/zir_sema.zig+14
...@@ -50,6 +50,8 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!...@@ -50,6 +50,8 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
50 .ref => return analyzeInstRef(mod, scope, old_inst.castTag(.ref).?),50 .ref => return analyzeInstRef(mod, scope, old_inst.castTag(.ref).?),
51 .ret_ptr => return analyzeInstRetPtr(mod, scope, old_inst.castTag(.ret_ptr).?),51 .ret_ptr => return analyzeInstRetPtr(mod, scope, old_inst.castTag(.ret_ptr).?),
52 .ret_type => return analyzeInstRetType(mod, scope, old_inst.castTag(.ret_type).?),52 .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).?),
53 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),55 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),
54 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),56 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),
55 .int => {57 .int => {
...@@ -1137,3 +1139,15 @@ fn analyzeDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerErr...@@ -1137,3 +1139,15 @@ fn analyzeDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerErr
11371139
1138 return decl;1140 return decl;
1139}1141}
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}