authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-08-12 22:15:09-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-12 21:58:21-07:00
log1eb5aaa4b5bee902bed354ef6ebfbb643a624440
tree6e3e25027224882a2d4bda89bde67136a343a344
parent5a166cead80d4d77bffba0b116fdeffb5820f5a4

CBE: renderValue pays attention to Type, not Tag


2 files changed, 116 insertions(+), 39 deletions(-)

src-self-hosted/codegen/c.zig+41-39
......@@ -17,41 +17,43 @@ fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {
1717 return allocator.dupe(u8, name);
1818}
1919
20fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !void {
21 if (T.tag() == .usize) {
22 file.need_stddef = true;
23 try writer.writeAll("size_t");
24 } else {
25 switch (T.zigTypeTag()) {
26 .NoReturn => {
27 try writer.writeAll("zig_noreturn void");
28 },
29 .Void => try writer.writeAll("void"),
30 .Int => {
31 if (T.tag() == .u8) {
32 file.need_stdint = true;
33 try writer.writeAll("uint8_t");
34 } else {
35 return file.fail(src, "TODO implement int types", .{});
36 }
37 },
38 else => |e| return file.fail(src, "TODO implement type {}", .{e}),
39 }
20fn renderType(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type) !void {
21 switch (T.zigTypeTag()) {
22 .NoReturn => {
23 try writer.writeAll("zig_noreturn void");
24 },
25 .Void => try writer.writeAll("void"),
26 .Int => {
27 if (T.tag() == .u8) {
28 ctx.file.need_stdint = true;
29 try writer.writeAll("uint8_t");
30 } else if (T.tag() == .usize) {
31 ctx.file.need_stddef = true;
32 try writer.writeAll("size_t");
33 } else {
34 return ctx.file.fail(ctx.decl.src(), "TODO implement int types", .{});
35 }
36 },
37 else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement type {}", .{e}),
4038 }
4139}
4240
43fn renderValue(file: *C, writer: std.ArrayList(u8).Writer, val: Value, src: usize) !void {
44 switch (val.tag()) {
45 .int_u64 => return writer.print("{}", .{val.toUnsignedInt()}),
46 else => |e| return file.fail(src, "TODO implement value {}", .{e}),
41fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Value) !void {
42 switch (T.zigTypeTag()) {
43 .Int => {
44 if (T.isSignedInt())
45 return writer.print("{}", .{val.toSignedInt()});
46 return writer.print("{}", .{val.toUnsignedInt()});
47 },
48 else => |e| return ctx.file.fail(ctx.decl.src(), "TODO implement value {}", .{e}),
4749 }
4850}
4951
50fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
52fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
5153 const tv = decl.typed_value.most_recent.typed_value;
52 try renderType(file, writer, tv.ty.fnReturnType(), decl.src());
53 const name = try map(file.base.allocator, mem.spanZ(decl.name));
54 defer file.base.allocator.free(name);
54 try renderType(ctx, writer, tv.ty.fnReturnType());
55 const name = try map(ctx.file.base.allocator, mem.spanZ(decl.name));
56 defer ctx.file.base.allocator.free(name);
5557 try writer.print(" {}(", .{name});
5658 var param_len = tv.ty.fnParamLen();
5759 if (param_len == 0)
......@@ -62,7 +64,7 @@ fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *De
6264 if (index > 0) {
6365 try writer.writeAll(", ");
6466 }
65 try renderType(file, writer, tv.ty.fnParamType(index), decl.src());
67 try renderType(ctx, writer, tv.ty.fnParamType(index));
6668 try writer.print(" arg{}", .{index});
6769 }
6870 }
......@@ -120,10 +122,6 @@ fn genFn(file: *C, decl: *Decl) !void {
120122 const writer = file.main.writer();
121123 const tv = decl.typed_value.most_recent.typed_value;
122124
123 try renderFunctionSignature(file, writer, decl);
124
125 try writer.writeAll(" {");
126
127125 var ctx = Context{
128126 .file = file,
129127 .decl = decl,
......@@ -131,6 +129,10 @@ fn genFn(file: *C, decl: *Decl) !void {
131129 };
132130 defer ctx.deinit();
133131
132 try renderFunctionSignature(&ctx, writer, decl);
133
134 try writer.writeAll(" {");
135
134136 const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func;
135137 const instructions = func.analysis.success.instructions;
136138 if (instructions.len > 0) {
......@@ -180,9 +182,9 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
180182 const from = ctx.inst_map.get(op) orelse
181183 return ctx.file.fail(ctx.decl.src(), "Internal error in C backend: intCast argument not found in inst_map", .{});
182184 try writer.writeAll(" const ");
183 try renderType(ctx.file, writer, inst.base.ty, ctx.decl.src());
185 try renderType(ctx, writer, inst.base.ty);
184186 try writer.print(" {} = (", .{name});
185 try renderType(ctx.file, writer, inst.base.ty, ctx.decl.src());
187 try renderType(ctx, writer, inst.base.ty);
186188 try writer.print("){};\n", .{from});
187189 return name;
188190}
......@@ -202,7 +204,7 @@ fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
202204 const tname = mem.spanZ(target.name);
203205 if (ctx.file.called.get(tname) == null) {
204206 try ctx.file.called.put(tname, void{});
205 try renderFunctionSignature(ctx.file, header, target);
207 try renderFunctionSignature(ctx, header, target);
206208 try header.writeAll(";\n");
207209 }
208210 try writer.print("{}(", .{tname});
......@@ -212,7 +214,7 @@ fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
212214 try writer.writeAll(", ");
213215 }
214216 if (arg.cast(Inst.Constant)) |con| {
215 try renderValue(ctx.file, writer, con.val, ctx.decl.src());
217 try renderValue(ctx, writer, arg.ty, con.val);
216218 } else {
217219 return ctx.file.fail(ctx.decl.src(), "TODO call pass arg {}", .{arg});
218220 }
......@@ -251,11 +253,11 @@ fn genAsm(ctx: *Context, as: *Inst.Assembly) !?[]u8 {
251253 const reg = i[1 .. i.len - 1];
252254 const arg = as.args[index];
253255 try writer.writeAll("register ");
254 try renderType(ctx.file, writer, arg.ty, ctx.decl.src());
256 try renderType(ctx, writer, arg.ty);
255257 try writer.print(" {}_constant __asm__(\"{}\") = ", .{ reg, reg });
256258 // TODO merge constant handling into inst_map as well
257259 if (arg.castTag(.constant)) |c| {
258 try renderValue(ctx.file, writer, c.val, ctx.decl.src());
260 try renderValue(ctx, writer, arg.ty, c.val);
259261 try writer.writeAll(";\n ");
260262 } else {
261263 const gop = try ctx.inst_map.getOrPut(arg);
src-self-hosted/value.zig+75
......@@ -568,6 +568,81 @@ pub const Value = extern union {
568568 }
569569 }
570570
571 /// Asserts the value is an integer and it fits in a i64
572 pub fn toSignedInt(self: Value) i64 {
573 switch (self.tag()) {
574 .ty,
575 .int_type,
576 .u8_type,
577 .i8_type,
578 .u16_type,
579 .i16_type,
580 .u32_type,
581 .i32_type,
582 .u64_type,
583 .i64_type,
584 .usize_type,
585 .isize_type,
586 .c_short_type,
587 .c_ushort_type,
588 .c_int_type,
589 .c_uint_type,
590 .c_long_type,
591 .c_ulong_type,
592 .c_longlong_type,
593 .c_ulonglong_type,
594 .c_longdouble_type,
595 .f16_type,
596 .f32_type,
597 .f64_type,
598 .f128_type,
599 .c_void_type,
600 .bool_type,
601 .void_type,
602 .type_type,
603 .anyerror_type,
604 .comptime_int_type,
605 .comptime_float_type,
606 .noreturn_type,
607 .null_type,
608 .undefined_type,
609 .fn_noreturn_no_args_type,
610 .fn_void_no_args_type,
611 .fn_naked_noreturn_no_args_type,
612 .fn_ccc_void_no_args_type,
613 .single_const_pointer_to_comptime_int_type,
614 .const_slice_u8_type,
615 .null_value,
616 .function,
617 .ref_val,
618 .decl_ref,
619 .elem_ptr,
620 .bytes,
621 .repeated,
622 .float_16,
623 .float_32,
624 .float_64,
625 .float_128,
626 .void_value,
627 .unreachable_value,
628 .empty_array,
629 => unreachable,
630
631 .undef => unreachable,
632
633 .zero,
634 .bool_false,
635 => return 0,
636
637 .bool_true => return 1,
638
639 .int_u64 => return @intCast(i64, self.cast(Payload.Int_i64).?.int),
640 .int_i64 => return self.cast(Payload.Int_i64).?.int,
641 .int_big_positive => return self.cast(Payload.IntBigPositive).?.asBigInt().to(i64) catch unreachable,
642 .int_big_negative => return self.cast(Payload.IntBigNegative).?.asBigInt().to(i64) catch unreachable,
643 }
644 }
645
571646 pub fn toBool(self: Value) bool {
572647 return switch (self.tag()) {
573648 .bool_true => true,