authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-16 21:51:05+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-17 19:21:03+03:00
log13b2f1e90ba9e373c655fc881836209c4fa381fa
treee9a84642b41805a0c46e54d7e9718b88971051c7
parentece4a2fc512bffba3845bf611370f5ea436c57b4
signaturelock-open Commit is signed but in an unrecognized format.

address review feedback


5 files changed, 40 insertions(+), 21 deletions(-)

src-self-hosted/Module.zig-1
...@@ -2433,7 +2433,6 @@ fn wrapOptional(self: *Module, scope: *Scope, dest_type: Type, inst: *Inst) !*In...@@ -2433,7 +2433,6 @@ fn wrapOptional(self: *Module, scope: *Scope, dest_type: Type, inst: *Inst) !*In
2433 return self.constInst(scope, inst.src, .{ .ty = dest_type, .val = val });2433 return self.constInst(scope, inst.src, .{ .ty = dest_type, .val = val });
2434 }2434 }
24352435
2436 // TODO how do we get the result location
2437 const b = try self.requireRuntimeBlock(scope, inst.src);2436 const b = try self.requireRuntimeBlock(scope, inst.src);
2438 return self.addUnOp(b, inst.src, dest_type, .wrap_optional, inst);2437 return self.addUnOp(b, inst.src, dest_type, .wrap_optional, inst);
2439}2438}
src-self-hosted/astgen.zig+12-13
...@@ -453,8 +453,6 @@ fn boolNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerErr...@@ -453,8 +453,6 @@ fn boolNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerErr
453}453}
454454
455fn addressOf(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {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);456 return expr(mod, scope, .lvalue, node.rhs);
459}457}
460458
...@@ -490,8 +488,6 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir...@@ -490,8 +488,6 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir
490 .single_const_ptr_type, child_type);488 .single_const_ptr_type, child_type);
491 }489 }
492490
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 = .{};491 var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, "kw_args").field_type = .{};
496 kw_args.@"allowzero" = node.ptr_info.allowzero_token != null;492 kw_args.@"allowzero" = node.ptr_info.allowzero_token != null;
497 if (node.ptr_info.align_info) |some| {493 if (node.ptr_info.align_info) |some| {
...@@ -504,7 +500,12 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir...@@ -504,7 +500,12 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir
504 kw_args.@"const" = node.ptr_info.const_token != null;500 kw_args.@"const" = node.ptr_info.const_token != null;
505 kw_args.@"volatile" = node.ptr_info.volatile_token != null;501 kw_args.@"volatile" = node.ptr_info.volatile_token != null;
506 if (node.ptr_info.sentinel) |some| {502 if (node.ptr_info.sentinel) |some| {
507 kw_args.sentinel = try expr(mod, scope, .{ .ty = child_type }, some);503 kw_args.sentinel = try expr(mod, scope, .none, some);
504 }
505
506 const child_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs);
507 if (kw_args.sentinel) |some| {
508 kw_args.sentinel = try addZIRBinOp(mod, scope, some.src, .as, child_type, some);
508 }509 }
509510
510 return addZIRInst(mod, scope, src, zir.Inst.PtrType, .{ .child_type = child_type }, kw_args);511 return addZIRInst(mod, scope, src, zir.Inst.PtrType, .{ .child_type = child_type }, kw_args);
...@@ -629,7 +630,9 @@ const CondKind = union(enum) {...@@ -629,7 +630,9 @@ const CondKind = union(enum) {
629 const payload = payload_node.?.castTag(.PointerPayload).?;630 const payload = payload_node.?.castTag(.PointerPayload).?;
630 const is_ptr = payload.ptr_token != null;631 const is_ptr = payload.ptr_token != null;
631 const ident_node = payload.value_symbol.castTag(.Identifier).?;632 const ident_node = payload.value_symbol.castTag(.Identifier).?;
632 const ident_name = try identifierTokenString(mod, &then_scope.base, ident_node.token);633
634 // This intentionally does not support @"_" syntax.
635 const ident_name = then_scope.base.tree().tokenSlice(ident_node.token);
633 if (mem.eql(u8, ident_name, "_")) {636 if (mem.eql(u8, ident_name, "_")) {
634 if (is_ptr)637 if (is_ptr)
635 return mod.failTok(&then_scope.base, payload.ptr_token.?, "pointer modifier invalid on discard", .{});638 return mod.failTok(&then_scope.base, payload.ptr_token.?, "pointer modifier invalid on discard", .{});
...@@ -646,7 +649,9 @@ const CondKind = union(enum) {...@@ -646,7 +649,9 @@ const CondKind = union(enum) {
646649
647 const payload = payload_node.?.castTag(.Payload).?;650 const payload = payload_node.?.castTag(.Payload).?;
648 const ident_node = payload.error_symbol.castTag(.Identifier).?;651 const ident_node = payload.error_symbol.castTag(.Identifier).?;
649 const ident_name = try identifierTokenString(mod, &else_scope.base, ident_node.token);652
653 // This intentionally does not support @"_" syntax.
654 const ident_name = else_scope.base.tree().tokenSlice(ident_node.token);
650 if (mem.eql(u8, ident_name, "_")) {655 if (mem.eql(u8, ident_name, "_")) {
651 return &else_scope.base;656 return &else_scope.base;
652 }657 }
...@@ -660,9 +665,6 @@ fn ifExpr(mod: *Module, scope: *Scope, rl: ResultLoc, if_node: *ast.Node.If) Inn...@@ -660,9 +665,6 @@ fn ifExpr(mod: *Module, scope: *Scope, rl: ResultLoc, if_node: *ast.Node.If) Inn
660 if (if_node.payload) |_| cond_kind = .{ .optional = null };665 if (if_node.payload) |_| cond_kind = .{ .optional = null };
661 if (if_node.@"else") |else_node| {666 if (if_node.@"else") |else_node| {
662 if (else_node.payload) |payload| {667 if (else_node.payload) |payload| {
663 if (cond_kind != .optional) {
664 return mod.failNode(scope, payload, "else payload invalid on bool conditions", .{});
665 }
666 cond_kind = .{ .err_union = null };668 cond_kind = .{ .err_union = null };
667 }669 }
668 }670 }
...@@ -760,9 +762,6 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W...@@ -760,9 +762,6 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W
760 if (while_node.payload) |_| cond_kind = .{ .optional = null };762 if (while_node.payload) |_| cond_kind = .{ .optional = null };
761 if (while_node.@"else") |else_node| {763 if (while_node.@"else") |else_node| {
762 if (else_node.payload) |payload| {764 if (else_node.payload) |payload| {
763 if (cond_kind != .optional) {
764 return mod.failNode(scope, payload, "else payload invalid on bool conditions", .{});
765 }
766 cond_kind = .{ .err_union = null };765 cond_kind = .{ .err_union = null };
767 }766 }
768 }767 }
src-self-hosted/codegen.zig+2-2
...@@ -2052,7 +2052,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -2052,7 +2052,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
2052 return mcv;2052 return mcv;
2053 }2053 }
20542054
2055 fn genTypedValue(self: *Self, src: usize, typed_value: TypedValue) error{ CodegenFail, OutOfMemory }!MCValue {2055 fn genTypedValue(self: *Self, src: usize, typed_value: TypedValue) InnerError!MCValue {
2056 if (typed_value.val.isUndef())2056 if (typed_value.val.isUndef())
2057 return MCValue{ .undef = {} };2057 return MCValue{ .undef = {} };
2058 const ptr_bits = self.target.cpu.arch.ptrBitWidth();2058 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
...@@ -2199,7 +2199,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -2199,7 +2199,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
2199 };2199 };
2200 }2200 }
22012201
2202 fn fail(self: *Self, src: usize, comptime format: []const u8, args: anytype) error{ CodegenFail, OutOfMemory } {2202 fn fail(self: *Self, src: usize, comptime format: []const u8, args: anytype) InnerError {
2203 @setCold(true);2203 @setCold(true);
2204 assert(self.err_msg == null);2204 assert(self.err_msg == null);
2205 self.err_msg = try ErrorMsg.create(self.bin_file.base.allocator, src, format, args);2205 self.err_msg = try ErrorMsg.create(self.bin_file.base.allocator, src, format, args);
src-self-hosted/type.zig+25-3
...@@ -362,7 +362,7 @@ pub const Type = extern union {...@@ -362,7 +362,7 @@ pub const Type = extern union {
362 .single_mut_pointer,362 .single_mut_pointer,
363 .optional_single_mut_pointer,363 .optional_single_mut_pointer,
364 .optional_single_const_pointer,364 .optional_single_const_pointer,
365 => return self.copyPayloadSingleField(allocator, Payload.Pointer, "pointee_type"),365 => return self.copyPayloadSingleField(allocator, Payload.Pointer, "pointee_type"),
366 }366 }
367 }367 }
368368
...@@ -1086,14 +1086,14 @@ pub const Type = extern union {...@@ -1086,14 +1086,14 @@ pub const Type = extern union {
1086 .optional_single_mut_pointer => {1086 .optional_single_mut_pointer => {
1087 buf.* = .{1087 buf.* = .{
1088 .base = .{ .tag = .single_mut_pointer },1088 .base = .{ .tag = .single_mut_pointer },
1089 .pointee_type = self.castPointer().?.pointee_type1089 .pointee_type = self.castPointer().?.pointee_type,
1090 };1090 };
1091 return Type.initPayload(&buf.base);1091 return Type.initPayload(&buf.base);
1092 },1092 },
1093 .optional_single_const_pointer => {1093 .optional_single_const_pointer => {
1094 buf.* = .{1094 buf.* = .{
1095 .base = .{ .tag = .single_const_pointer },1095 .base = .{ .tag = .single_const_pointer },
1096 .pointee_type = self.castPointer().?.pointee_type1096 .pointee_type = self.castPointer().?.pointee_type,
1097 };1097 };
1098 return Type.initPayload(&buf.base);1098 return Type.initPayload(&buf.base);
1099 },1099 },
...@@ -1101,6 +1101,28 @@ pub const Type = extern union {...@@ -1101,6 +1101,28 @@ pub const Type = extern union {
1101 };1101 };
1102 }1102 }
11031103
1104 /// Asserts that the type is an optional.
1105 /// Same as `optionalChild` but allocates the buffer if needed.
1106 pub fn optionalChildAlloc(self: Type, allocator: *Allocator) !Type {
1107 return switch (self.tag()) {
1108 .optional => self.cast(Payload.Optional).?.child_type,
1109 .optional_single_mut_pointer, .optional_single_const_pointer => {
1110 const payload = try allocator.create(Payload.Pointer);
1111 payload.* = .{
1112 .base = .{
1113 .tag = if (self.tag() == .optional_single_const_pointer)
1114 .single_const_pointer
1115 else
1116 .single_mut_pointer,
1117 },
1118 .pointee_type = self.castPointer().?.pointee_type,
1119 };
1120 return Type.initPayload(&payload.base);
1121 },
1122 else => unreachable,
1123 };
1124 }
1125
1104 /// Asserts the type is an array or vector.1126 /// Asserts the type is an array or vector.
1105 pub fn arrayLen(self: Type) u64 {1127 pub fn arrayLen(self: Type) u64 {
1106 return switch (self.tag()) {1128 return switch (self.tag()) {
src-self-hosted/zir_sema.zig+1-2
...@@ -710,8 +710,7 @@ fn analyzeInstUnwrapOptional(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp...@@ -710,8 +710,7 @@ fn analyzeInstUnwrapOptional(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp
710 return mod.fail(scope, unwrap.base.src, "expected optional type, found {}", .{operand.ty.elemType()});710 return mod.fail(scope, unwrap.base.src, "expected optional type, found {}", .{operand.ty.elemType()});
711 }711 }
712712
713 var buf: Type.Payload.Pointer = undefined;713 const child_type = try operand.ty.elemType().optionalChildAlloc(scope.arena());
714 const child_type = try operand.ty.elemType().optionalChild(&buf).copy(scope.arena());
715 const child_pointer = try mod.singlePtrType(scope, unwrap.base.src, operand.ty.isConstPtr(), child_type);714 const child_pointer = try mod.singlePtrType(scope, unwrap.base.src, operand.ty.isConstPtr(), child_type);
716715
717 if (operand.value()) |val| {716 if (operand.value()) |val| {