authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-05 22:58:05+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-07-05 22:58:05+00:00
logabcd4ea5d88541a21062e63fe427b2d2ea16a041
tree98fdd5e33229ced5fd12c67e9223b612517a2b3f
parenta0a93f20919e523cb68fc414221b5babe2586825
parent4a63189bf1275aed3f3f170e46eff10b15e61157
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5793 from pfgithub/stage-2-testing

stage2 + operator and @as builtin

3 files changed, 54 insertions(+), 4 deletions(-)

src-self-hosted/Module.zig+28-4
...@@ -1297,10 +1297,28 @@ fn astGenExpr(self: *Module, scope: *Scope, ast_node: *ast.Node) InnerError!*zir...@@ -1297,10 +1297,28 @@ fn astGenExpr(self: *Module, scope: *Scope, ast_node: *ast.Node) InnerError!*zir
1297 .Unreachable => return self.astGenUnreachable(scope, @fieldParentPtr(ast.Node.Unreachable, "base", ast_node)),1297 .Unreachable => return self.astGenUnreachable(scope, @fieldParentPtr(ast.Node.Unreachable, "base", ast_node)),
1298 .ControlFlowExpression => return self.astGenControlFlowExpression(scope, @fieldParentPtr(ast.Node.ControlFlowExpression, "base", ast_node)),1298 .ControlFlowExpression => return self.astGenControlFlowExpression(scope, @fieldParentPtr(ast.Node.ControlFlowExpression, "base", ast_node)),
1299 .If => return self.astGenIf(scope, @fieldParentPtr(ast.Node.If, "base", ast_node)),1299 .If => return self.astGenIf(scope, @fieldParentPtr(ast.Node.If, "base", ast_node)),
1300 .InfixOp => return self.astGenInfixOp(scope, @fieldParentPtr(ast.Node.InfixOp, "base", ast_node)),
1300 else => return self.failNode(scope, ast_node, "TODO implement astGenExpr for {}", .{@tagName(ast_node.id)}),1301 else => return self.failNode(scope, ast_node, "TODO implement astGenExpr for {}", .{@tagName(ast_node.id)}),
1301 }1302 }
1302}1303}
13031304
1305fn astGenInfixOp(self: *Module, scope: *Scope, infix_node: *ast.Node.InfixOp) InnerError!*zir.Inst {
1306 switch (infix_node.op) {
1307 .Add => {
1308 const lhs = try self.astGenExpr(scope, infix_node.lhs);
1309 const rhs = try self.astGenExpr(scope, infix_node.rhs);
1310
1311 const tree = scope.tree();
1312 const src = tree.token_locs[infix_node.op_token].start;
1313
1314 return self.addZIRInst(scope, src, zir.Inst.Add, .{ .lhs = lhs, .rhs = rhs }, .{});
1315 },
1316 else => |op| {
1317 return self.failNode(scope, &infix_node.base, "TODO implement infix operator {}", .{op});
1318 },
1319 }
1320}
1321
1304fn astGenIf(self: *Module, scope: *Scope, if_node: *ast.Node.If) InnerError!*zir.Inst {1322fn astGenIf(self: *Module, scope: *Scope, if_node: *ast.Node.If) InnerError!*zir.Inst {
1305 if (if_node.payload) |payload| {1323 if (if_node.payload) |payload| {
1306 return self.failNode(scope, payload, "TODO implement astGenIf for optionals", .{});1324 return self.failNode(scope, payload, "TODO implement astGenIf for optionals", .{});
...@@ -1496,7 +1514,12 @@ fn astGenBuiltinCall(self: *Module, scope: *Scope, call: *ast.Node.BuiltinCall)...@@ -1496,7 +1514,12 @@ fn astGenBuiltinCall(self: *Module, scope: *Scope, call: *ast.Node.BuiltinCall)
1496 const arg_count: ?usize = if (positionals.fields[0].field_type == []*zir.Inst) null else positionals.fields.len;1514 const arg_count: ?usize = if (positionals.fields[0].field_type == []*zir.Inst) null else positionals.fields.len;
1497 if (arg_count) |some| {1515 if (arg_count) |some| {
1498 if (call.params_len != some) {1516 if (call.params_len != some) {
1499 return self.failTok(scope, call.builtin_token, "expected {} parameter, found {}", .{ some, call.params_len });1517 return self.failTok(
1518 scope,
1519 call.builtin_token,
1520 "expected {} parameter{}, found {}",
1521 .{ some, if (some == 1) "" else "s", call.params_len },
1522 );
1500 }1523 }
1501 const params = call.params();1524 const params = call.params();
1502 inline for (positionals.fields) |p, i| {1525 inline for (positionals.fields) |p, i| {
...@@ -2902,7 +2925,9 @@ fn analyzeInstAdd(self: *Module, scope: *Scope, inst: *zir.Inst.Add) InnerError!...@@ -2902,7 +2925,9 @@ fn analyzeInstAdd(self: *Module, scope: *Scope, inst: *zir.Inst.Add) InnerError!
2902 const lhs = try self.resolveInst(scope, inst.positionals.lhs);2925 const lhs = try self.resolveInst(scope, inst.positionals.lhs);
2903 const rhs = try self.resolveInst(scope, inst.positionals.rhs);2926 const rhs = try self.resolveInst(scope, inst.positionals.rhs);
29042927
2905 if (lhs.ty.zigTypeTag() == .Int and rhs.ty.zigTypeTag() == .Int) {2928 if ((lhs.ty.zigTypeTag() == .Int or lhs.ty.zigTypeTag() == .ComptimeInt) and
2929 (rhs.ty.zigTypeTag() == .Int or rhs.ty.zigTypeTag() == .ComptimeInt))
2930 {
2906 if (!lhs.ty.eql(rhs.ty)) {2931 if (!lhs.ty.eql(rhs.ty)) {
2907 return self.fail(scope, inst.base.src, "TODO implement peer type resolution", .{});2932 return self.fail(scope, inst.base.src, "TODO implement peer type resolution", .{});
2908 }2933 }
...@@ -2946,8 +2971,7 @@ fn analyzeInstAdd(self: *Module, scope: *Scope, inst: *zir.Inst.Add) InnerError!...@@ -2946,8 +2971,7 @@ fn analyzeInstAdd(self: *Module, scope: *Scope, inst: *zir.Inst.Add) InnerError!
2946 .rhs = rhs,2971 .rhs = rhs,
2947 });2972 });
2948 }2973 }
29492974 return self.fail(scope, inst.base.src, "TODO analyze add for {} + {}", .{ lhs.ty.zigTypeTag(), rhs.ty.zigTypeTag() });
2950 return self.fail(scope, inst.base.src, "TODO implement more analyze add", .{});
2951}2975}
29522976
2953fn analyzeInstDeref(self: *Module, scope: *Scope, deref: *zir.Inst.Deref) InnerError!*Inst {2977fn analyzeInstDeref(self: *Module, scope: *Scope, deref: *zir.Inst.Deref) InnerError!*Inst {
src-self-hosted/zir.zig+1
...@@ -290,6 +290,7 @@ pub const Inst = struct {...@@ -290,6 +290,7 @@ pub const Inst = struct {
290290
291 pub const As = struct {291 pub const As = struct {
292 pub const base_tag = Tag.as;292 pub const base_tag = Tag.as;
293 pub const builtin_name = "@as";
293 base: Inst,294 base: Inst,
294295
295 positionals: struct {296 positionals: struct {
test/stage2/compare_output.zig+25
...@@ -118,4 +118,29 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -118,4 +118,29 @@ pub fn addCases(ctx: *TestContext) !void {
118 \\118 \\
119 );119 );
120 }120 }
121
122 {
123 var case = ctx.exe("adding numbers", linux_x64);
124 case.addCompareOutput(
125 \\export fn _start() noreturn {
126 \\ asm volatile ("syscall"
127 \\ :
128 \\ : [number] "{rax}" (1),
129 \\ [arg1] "{rdi}" (1),
130 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
131 \\ [arg3] "{rdx}" (10 + 4)
132 \\ : "rcx", "r11", "memory"
133 \\ );
134 \\ asm volatile ("syscall"
135 \\ :
136 \\ : [number] "{rax}" (@as(usize, 230) + @as(usize, 1)),
137 \\ [arg1] "{rdi}" (0)
138 \\ : "rcx", "r11", "memory"
139 \\ );
140 \\ unreachable;
141 \\}
142 ,
143 "Hello, World!\n",
144 );
145 }
121}146}