authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-06-19 20:45:57+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-22 23:19:12-04:00
logd98aed6eff61628f97597e1fa68335db5b2fd466
tree72a80c2fa34bacc8f77a1ac6f04d96006e241c44
parentc9a0ec25e0baae7f128a8f7efe4d308af7fad753

self-hosted: generalize `astGenBuiltinCall`


2 files changed, 27 insertions(+), 9 deletions(-)

src-self-hosted/Module.zig+26-9
...@@ -1384,18 +1384,35 @@ fn astGenAsm(self: *Module, scope: *Scope, asm_node: *ast.Node.Asm) InnerError!*...@@ -1384,18 +1384,35 @@ fn astGenAsm(self: *Module, scope: *Scope, asm_node: *ast.Node.Asm) InnerError!*
1384fn astGenBuiltinCall(self: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {1384fn astGenBuiltinCall(self: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
1385 const tree = scope.tree();1385 const tree = scope.tree();
1386 const builtin_name = tree.tokenSlice(call.builtin_token);1386 const builtin_name = tree.tokenSlice(call.builtin_token);
1387 const src = tree.token_locs[call.builtin_token].start;
1388
1389 inline for (std.meta.declarations(zir.Inst)) |inst| {
1390 if (inst.data != .Type) continue;
1391 const T = inst.data.Type;
1392 if (!@hasDecl(T, "builtin_name")) continue;
1393 if (std.mem.eql(u8, builtin_name, T.builtin_name)) {
1394 var value: T = undefined;
1395 const positionals = @typeInfo(std.meta.fieldInfo(T, "positionals").field_type).Struct;
1396 if (positionals.fields.len == 0) {
1397 return self.addZIRInst(scope, src, T, value.positionals, value.kw_args);
1398 }
1399 const arg_count: ?usize = if (positionals.fields[0].field_type == []*zir.Inst) null else positionals.fields.len;
1400 if (arg_count) |some| {
1401 if (call.params_len != some) {
1402 return self.failTok(scope, call.builtin_token, "expected {} parameter, found {}", .{ some, call.params_len });
1403 }
1404 const params = call.params();
1405 inline for (positionals.fields) |p, i| {
1406 @field(value.positionals, p.name) = try self.astGenExpr(scope, params[i]);
1407 }
1408 } else {
1409 return self.failTok(scope, call.builtin_token, "TODO var args builtin '{}'", .{builtin_name});
1410 }
13871411
1388 if (mem.eql(u8, builtin_name, "@ptrToInt")) {1412 return self.addZIRInst(scope, src, T, value.positionals, .{});
1389 if (call.params_len != 1) {
1390 return self.failTok(scope, call.builtin_token, "expected 1 parameter, found {}", .{call.params_len});
1391 }1413 }
1392 const src = tree.token_locs[call.builtin_token].start;
1393 return self.addZIRInst(scope, src, zir.Inst.PtrToInt, .{
1394 .ptr = try self.astGenExpr(scope, call.params()[0]),
1395 }, .{});
1396 } else {
1397 return self.failTok(scope, call.builtin_token, "TODO implement builtin call for '{}'", .{builtin_name});
1398 }1414 }
1415 return self.failTok(scope, call.builtin_token, "TODO implement builtin call for '{}'", .{builtin_name});
1399}1416}
14001417
1401fn astGenCall(self: *Module, scope: *Scope, call: *ast.Node.Call) InnerError!*zir.Inst {1418fn astGenCall(self: *Module, scope: *Scope, call: *ast.Node.Call) InnerError!*zir.Inst {
src-self-hosted/zir.zig+1
...@@ -201,6 +201,7 @@ pub const Inst = struct {...@@ -201,6 +201,7 @@ pub const Inst = struct {
201 };201 };
202202
203 pub const PtrToInt = struct {203 pub const PtrToInt = struct {
204 pub const builtin_name = "@ptrToInt";
204 pub const base_tag = Tag.ptrtoint;205 pub const base_tag = Tag.ptrtoint;
205 base: Inst,206 base: Inst,
206207