authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-28 14:55:04+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-09-03 15:05:46+03:00
log2a628fd401bf057a71175c8b723375fd4f375a84
treef4775061f46aa18edc86fd13aaa3efbcfb0ce294
parentff7c6e1e3cea86e130e15a720c729a05763b5f08
signature Commit is signed but in an unrecognized format.

stage2: astgen slice


3 files changed, 63 insertions(+), 1 deletions(-)

src-self-hosted/astgen.zig+31-1
...@@ -275,6 +275,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr...@@ -275,6 +275,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
275 .ErrorType => return rlWrap(mod, scope, rl, try errorType(mod, scope, node.castTag(.ErrorType).?)),275 .ErrorType => return rlWrap(mod, scope, rl, try errorType(mod, scope, node.castTag(.ErrorType).?)),
276 .For => return forExpr(mod, scope, rl, node.castTag(.For).?),276 .For => return forExpr(mod, scope, rl, node.castTag(.For).?),
277 .ArrayAccess => return arrayAccess(mod, scope, rl, node.castTag(.ArrayAccess).?),277 .ArrayAccess => return arrayAccess(mod, scope, rl, node.castTag(.ArrayAccess).?),
278 .Slice => return rlWrap(mod, scope, rl, try sliceExpr(mod, scope, node.castTag(.Slice).?)),
278 .Catch => return catchExpr(mod, scope, rl, node.castTag(.Catch).?),279 .Catch => return catchExpr(mod, scope, rl, node.castTag(.Catch).?),
279 .Comptime => return comptimeKeyword(mod, scope, rl, node.castTag(.Comptime).?),280 .Comptime => return comptimeKeyword(mod, scope, rl, node.castTag(.Comptime).?),
280 .OrElse => return orelseExpr(mod, scope, rl, node.castTag(.OrElse).?),281 .OrElse => return orelseExpr(mod, scope, rl, node.castTag(.OrElse).?),
...@@ -284,7 +285,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr...@@ -284,7 +285,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
284 .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}),285 .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}),
285 .Resume => return mod.failNode(scope, node, "TODO implement astgen.expr for .Resume", .{}),286 .Resume => return mod.failNode(scope, node, "TODO implement astgen.expr for .Resume", .{}),
286 .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}),287 .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}),
287 .Slice => return mod.failNode(scope, node, "TODO implement astgen.expr for .Slice", .{}),
288 .ArrayInitializer => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayInitializer", .{}),288 .ArrayInitializer => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayInitializer", .{}),
289 .ArrayInitializerDot => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayInitializerDot", .{}),289 .ArrayInitializerDot => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayInitializerDot", .{}),
290 .StructInitializer => return mod.failNode(scope, node, "TODO implement astgen.expr for .StructInitializer", .{}),290 .StructInitializer => return mod.failNode(scope, node, "TODO implement astgen.expr for .StructInitializer", .{}),
...@@ -951,6 +951,36 @@ fn arrayAccess(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Array...@@ -951,6 +951,36 @@ fn arrayAccess(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Array
951 return rlWrapPtr(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.ElemPtr, .{ .array_ptr = array_ptr, .index = index }, .{}));951 return rlWrapPtr(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.ElemPtr, .{ .array_ptr = array_ptr, .index = index }, .{}));
952}952}
953953
954fn sliceExpr(mod: *Module, scope: *Scope, node: *ast.Node.Slice) InnerError!*zir.Inst {
955 const tree = scope.tree();
956 const src = tree.token_locs[node.rtoken].start;
957
958 const usize_type = try addZIRInstConst(mod, scope, src, .{
959 .ty = Type.initTag(.type),
960 .val = Value.initTag(.usize_type),
961 });
962
963 const array_ptr = try expr(mod, scope, .ref, node.lhs);
964 const start = try expr(mod, scope, .{ .ty = usize_type }, node.start);
965
966 if (node.end == null and node.sentinel == null) {
967 return try addZIRBinOp(mod, scope, src, .slice_start, array_ptr, start);
968 }
969
970 const end = if (node.end) |end| try expr(mod, scope, .{ .ty = usize_type }, end) else null;
971 // we could get the child type here, but it is easier to just do it in semantic analysis.
972 const sentinel = if (node.sentinel) |sentinel| try expr(mod, scope, .none, sentinel) else null;
973
974 return try addZIRInst(
975 mod,
976 scope,
977 src,
978 zir.Inst.Slice,
979 .{ .array_ptr = array_ptr, .start = start },
980 .{ .end = end, .sentinel = sentinel },
981 );
982}
983
954fn deref(mod: *Module, scope: *Scope, node: *ast.Node.SimpleSuffixOp) InnerError!*zir.Inst {984fn deref(mod: *Module, scope: *Scope, node: *ast.Node.SimpleSuffixOp) InnerError!*zir.Inst {
955 const tree = scope.tree();985 const tree = scope.tree();
956 const src = tree.token_locs[node.rtoken].start;986 const src = tree.token_locs[node.rtoken].start;
src-self-hosted/zir.zig+22
...@@ -231,6 +231,10 @@ pub const Inst = struct {...@@ -231,6 +231,10 @@ pub const Inst = struct {
231 const_slice_type,231 const_slice_type,
232 /// Create a pointer type with attributes232 /// Create a pointer type with attributes
233 ptr_type,233 ptr_type,
234 /// Slice operation `array_ptr[start..end:sentinel]`
235 slice,
236 /// Slice operation with just start `lhs[rhs..]`
237 slice_start,
234 /// Write a value to a pointer. For loading, see `deref`.238 /// Write a value to a pointer. For loading, see `deref`.
235 store,239 store,
236 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.240 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
...@@ -343,6 +347,7 @@ pub const Inst = struct {...@@ -343,6 +347,7 @@ pub const Inst = struct {
343 .xor,347 .xor,
344 .error_union_type,348 .error_union_type,
345 .merge_error_sets,349 .merge_error_sets,
350 .slice_start,
346 => BinOp,351 => BinOp,
347352
348 .block,353 .block,
...@@ -380,6 +385,7 @@ pub const Inst = struct {...@@ -380,6 +385,7 @@ pub const Inst = struct {
380 .ptr_type => PtrType,385 .ptr_type => PtrType,
381 .enum_literal => EnumLiteral,386 .enum_literal => EnumLiteral,
382 .error_set => ErrorSet,387 .error_set => ErrorSet,
388 .slice => Slice,
383 };389 };
384 }390 }
385391
...@@ -481,6 +487,8 @@ pub const Inst = struct {...@@ -481,6 +487,8 @@ pub const Inst = struct {
481 .error_union_type,487 .error_union_type,
482 .bitnot,488 .bitnot,
483 .error_set,489 .error_set,
490 .slice,
491 .slice_start,
484 => false,492 => false,
485493
486 .@"break",494 .@"break",
...@@ -961,6 +969,20 @@ pub const Inst = struct {...@@ -961,6 +969,20 @@ pub const Inst = struct {
961 },969 },
962 kw_args: struct {},970 kw_args: struct {},
963 };971 };
972
973 pub const Slice = struct {
974 pub const base_tag = Tag.slice;
975 base: Inst,
976
977 positionals: struct {
978 array_ptr: *Inst,
979 start: *Inst,
980 },
981 kw_args: struct {
982 end: ?*Inst = null,
983 sentinel: ?*Inst = null,
984 },
985 };
964};986};
965987
966pub const ErrorMsg = struct {988pub const ErrorMsg = struct {
src-self-hosted/zir_sema.zig+10
...@@ -132,6 +132,8 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!...@@ -132,6 +132,8 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
132 .error_union_type => return analyzeInstErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?),132 .error_union_type => return analyzeInstErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?),
133 .anyframe_type => return analyzeInstAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?),133 .anyframe_type => return analyzeInstAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?),
134 .error_set => return analyzeInstErrorSet(mod, scope, old_inst.castTag(.error_set).?),134 .error_set => return analyzeInstErrorSet(mod, scope, old_inst.castTag(.error_set).?),
135 .slice => return analyzeInstSlice(mod, scope, old_inst.castTag(.slice).?),
136 .slice_start => return analyzeInstSliceStart(mod, scope, old_inst.castTag(.slice_start).?),
135 }137 }
136}138}
137139
...@@ -1172,6 +1174,14 @@ fn analyzeInstElemPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.ElemPtr) Inne...@@ -1172,6 +1174,14 @@ fn analyzeInstElemPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.ElemPtr) Inne
1172 return mod.fail(scope, inst.base.src, "TODO implement more analyze elemptr", .{});1174 return mod.fail(scope, inst.base.src, "TODO implement more analyze elemptr", .{});
1173}1175}
11741176
1177fn analyzeInstSlice(mod: *Module, scope: *Scope, inst: *zir.Inst.Slice) InnerError!*Inst {
1178 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstSlice", .{});
1179}
1180
1181fn analyzeInstSliceStart(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1182 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstSliceStart", .{});
1183}
1184
1175fn analyzeInstShl(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {1185fn analyzeInstShl(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1176 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstShl", .{});1186 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstShl", .{});
1177}1187}