authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-14 17:39:18+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-17 14:24:56+03:00
logc52513e25b69cdd28c5af567b8d3d6a0d9fb979e
treef95d653ba510e1996c517054476325ff05d784e7
parent8d8d568854d33be7bcc2bc9874029d1082914af7
signature Commit is signed but in an unrecognized format.

stage2: astgen for ptr types and address of


3 files changed, 76 insertions(+), 2 deletions(-)

src-self-hosted/astgen.zig+49-2
......@@ -113,6 +113,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
113113 .Period => return rlWrap(mod, scope, rl, try field(mod, scope, node.castTag(.Period).?)),
114114 .Deref => return rlWrap(mod, scope, rl, try deref(mod, scope, node.castTag(.Deref).?)),
115115 .BoolNot => return rlWrap(mod, scope, rl, try boolNot(mod, scope, node.castTag(.BoolNot).?)),
116 .AddressOf => return rlWrap(mod, scope, rl, try addressOf(mod, scope, node.castTag(.AddressOf).?)),
116117 .FloatLiteral => return rlWrap(mod, scope, rl, try floatLiteral(mod, scope, node.castTag(.FloatLiteral).?)),
117118 .UndefinedLiteral => return rlWrap(mod, scope, rl, try undefLiteral(mod, scope, node.castTag(.UndefinedLiteral).?)),
118119 .BoolLiteral => return rlWrap(mod, scope, rl, try boolLiteral(mod, scope, node.castTag(.BoolLiteral).?)),
......@@ -122,6 +123,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
122123 .Block => return rlWrapVoid(mod, scope, rl, node, try blockExpr(mod, scope, node.castTag(.Block).?)),
123124 .LabeledBlock => return labeledBlockExpr(mod, scope, rl, node.castTag(.LabeledBlock).?),
124125 .Break => return rlWrap(mod, scope, rl, try breakExpr(mod, scope, node.castTag(.Break).?)),
126 .PtrType => return rlWrap(mod, scope, rl, try ptrType(mod, scope, node.castTag(.PtrType).?)),
125127
126128 .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}),
127129 .Catch => return mod.failNode(scope, node, "TODO implement astgen.expr for .Catch", .{}),
......@@ -131,7 +133,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
131133 .MergeErrorSets => return mod.failNode(scope, node, "TODO implement astgen.expr for .MergeErrorSets", .{}),
132134 .Range => return mod.failNode(scope, node, "TODO implement astgen.expr for .Range", .{}),
133135 .OrElse => return mod.failNode(scope, node, "TODO implement astgen.expr for .OrElse", .{}),
134 .AddressOf => return mod.failNode(scope, node, "TODO implement astgen.expr for .AddressOf", .{}),
135136 .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}),
136137 .BitNot => return mod.failNode(scope, node, "TODO implement astgen.expr for .BitNot", .{}),
137138 .Negation => return mod.failNode(scope, node, "TODO implement astgen.expr for .Negation", .{}),
......@@ -140,7 +141,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
140141 .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}),
141142 .ArrayType => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayType", .{}),
142143 .ArrayTypeSentinel => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayTypeSentinel", .{}),
143 .PtrType => return mod.failNode(scope, node, "TODO implement astgen.expr for .PtrType", .{}),
144144 .SliceType => return mod.failNode(scope, node, "TODO implement astgen.expr for .SliceType", .{}),
145145 .Slice => return mod.failNode(scope, node, "TODO implement astgen.expr for .Slice", .{}),
146146 .ArrayAccess => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayAccess", .{}),
......@@ -452,6 +452,12 @@ fn boolNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerErr
452452 return addZIRUnOp(mod, scope, src, .boolnot, operand);
453453}
454454
455fn addressOf(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {
456 const tree = scope.tree();
457 const src = tree.token_locs[node.op_token].start;
458 return expr(mod, scope, .lvalue, node.rhs);
459}
460
455461fn optionalType(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {
456462 const tree = scope.tree();
457463 const src = tree.token_locs[node.op_token].start;
......@@ -463,6 +469,47 @@ fn optionalType(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) Inn
463469 return addZIRUnOp(mod, scope, src, .optional_type, operand);
464470}
465471
472fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir.Inst {
473 const tree = scope.tree();
474 const src = tree.token_locs[node.op_token].start;
475 const meta_type = try addZIRInstConst(mod, scope, src, .{
476 .ty = Type.initTag(.type),
477 .val = Value.initTag(.type_type),
478 });
479
480 const simple = node.ptr_info.allowzero_token == null and
481 node.ptr_info.align_info == null and
482 node.ptr_info.volatile_token == null and
483 node.ptr_info.sentinel == null;
484
485 if (simple) {
486 const child_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs);
487 return addZIRUnOp(mod, scope, src, if (node.ptr_info.const_token == null)
488 .single_mut_ptr_type
489 else
490 .single_const_ptr_type, child_type);
491 }
492
493 const child_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs);
494
495 var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, "kw_args").field_type = .{};
496 kw_args.@"allowzero" = node.ptr_info.allowzero_token != null;
497 if (node.ptr_info.align_info) |some| {
498 kw_args.@"align" = try expr(mod, scope, .none, some.node);
499 if (some.bit_range) |bit_range| {
500 kw_args.align_bit_start = try expr(mod, scope, .none, bit_range.start);
501 kw_args.align_bit_end = try expr(mod, scope, .none, bit_range.end);
502 }
503 }
504 kw_args.@"const" = node.ptr_info.const_token != null;
505 kw_args.@"volatile" = node.ptr_info.volatile_token != null;
506 if (node.ptr_info.sentinel) |some| {
507 kw_args.sentinel = try expr(mod, scope, .{ .ty = child_type }, some);
508 }
509
510 return addZIRInst(mod, scope, src, zir.Inst.PtrType, .{ .child_type = child_type }, kw_args);
511}
512
466513fn unwrapOptional(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.SimpleSuffixOp) InnerError!*zir.Inst {
467514 const tree = scope.tree();
468515 const src = tree.token_locs[node.rtoken].start;
src-self-hosted/zir.zig+22
......@@ -192,6 +192,8 @@ pub const Inst = struct {
192192 single_const_ptr_type,
193193 /// Create a mutable pointer type based on the element type. `*T`
194194 single_mut_ptr_type,
195 /// Create a pointer type with attributes
196 ptr_type,
195197 /// Write a value to a pointer. For loading, see `deref`.
196198 store,
197199 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
......@@ -305,6 +307,7 @@ pub const Inst = struct {
305307 .fntype => FnType,
306308 .elemptr => ElemPtr,
307309 .condbr => CondBr,
310 .ptr_type => PtrType,
308311 };
309312 }
310313
......@@ -382,6 +385,7 @@ pub const Inst = struct {
382385 .optional_type,
383386 .unwrap_optional_safe,
384387 .unwrap_optional_unsafe,
388 .ptr_type,
385389 => false,
386390
387391 .@"break",
......@@ -811,6 +815,24 @@ pub const Inst = struct {
811815 },
812816 kw_args: struct {},
813817 };
818
819 pub const PtrType = struct {
820 pub const base_tag = Tag.ptr_type;
821 base: Inst,
822
823 positionals: struct {
824 child_type: *Inst,
825 },
826 kw_args: struct {
827 @"allowzero": bool = false,
828 @"align": ?*Inst = null,
829 align_bit_start: ?*Inst = null,
830 align_bit_end: ?*Inst = null,
831 @"const": bool = true,
832 @"volatile": bool = false,
833 sentinel: ?*Inst = null,
834 },
835 };
814836};
815837
816838pub const ErrorMsg = struct {
src-self-hosted/zir_sema.zig+5
......@@ -53,6 +53,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
5353 .ret_type => return analyzeInstRetType(mod, scope, old_inst.castTag(.ret_type).?),
5454 .single_const_ptr_type => return analyzeInstSingleConstPtrType(mod, scope, old_inst.castTag(.single_const_ptr_type).?),
5555 .single_mut_ptr_type => return analyzeInstSingleMutPtrType(mod, scope, old_inst.castTag(.single_mut_ptr_type).?),
56 .ptr_type => return analyzeInstPtrType(mod, scope, old_inst.castTag(.ptr_type).?),
5657 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),
5758 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),
5859 .int => {
......@@ -1287,3 +1288,7 @@ fn analyzeInstSingleMutPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp
12871288 const ty = try mod.singleMutPtrType(scope, inst.base.src, elem_type);
12881289 return mod.constType(scope, inst.base.src, ty);
12891290}
1291
1292fn analyzeInstPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.PtrType) InnerError!*Inst {
1293 return mod.fail(scope, inst.base.src, "TODO implement ptr_type", .{});
1294}