authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-01-28 20:46:15+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-08 00:33:07+02:00
logc22f010fdd2c5ead80cbdc902738a9d023322436
tree681b888df2b3de60678bfbc31984c6bcd9cd9ba4
parentca20d0ea26bba603229377f4843eb8772aeb749d
signaturelock-open Commit is signed but in an unrecognized format.

stage2 cbe: regular optional types


1 files changed, 30 insertions(+), 11 deletions(-)

src/codegen/c.zig+30-11
......@@ -207,10 +207,12 @@ pub const DeclGen = struct {
207207 if (t.isPtrLikeOptional()) {
208208 return dg.renderValue(writer, child_type, val);
209209 }
210 try writer.writeByte('(');
211 try dg.renderType(writer, t);
210212 if (val.tag() == .null_value) {
211 try writer.writeAll("{ .is_null = true }");
213 try writer.writeAll("){ .is_null = true }");
212214 } else {
213 try writer.writeAll("{ .is_null = false, .payload = ");
215 try writer.writeAll("){ .is_null = false, .payload = ");
214216 try dg.renderValue(writer, child_type, val);
215217 try writer.writeAll(" }");
216218 }
......@@ -319,7 +321,11 @@ pub const DeclGen = struct {
319321 if (t.isPtrLikeOptional()) {
320322 return dg.renderType(w, child_type);
321323 }
322 return dg.fail(dg.decl.src(), "TODO: C backend: more optional types", .{});
324
325 // TODO this needs to be typedeffed since different structs are different types.
326 try w.writeAll("struct { ");
327 try dg.renderType(w, child_type);
328 try w.writeAll(" payload; bool is_null; }");
323329 },
324330 .Null, .Undefined => unreachable, // must be const or comptime
325331 else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement type {s}", .{
......@@ -834,6 +840,7 @@ fn genAsm(o: *Object, as: *Inst.Assembly) !CValue {
834840fn genIsNull(o: *Object, inst: *Inst.UnOp) !CValue {
835841 const writer = o.writer();
836842 const invert_logic = inst.base.tag == .is_non_null or inst.base.tag == .is_non_null_ptr;
843 const operator = if (invert_logic) "!=" else "==";
837844 const maybe_deref = if (inst.base.tag == .is_null_ptr or inst.base.tag == .is_non_null_ptr) "[0]" else "";
838845 const operand = try o.resolveInst(inst.operand);
839846
......@@ -843,10 +850,8 @@ fn genIsNull(o: *Object, inst: *Inst.UnOp) !CValue {
843850
844851 if (inst.operand.ty.isPtrLikeOptional()) {
845852 // operand is a regular pointer, test `operand !=/== NULL`
846 const operator = if (invert_logic) "!=" else "==";
847853 try writer.print("){s} {s} NULL;\n", .{ maybe_deref, operator });
848854 } else {
849 const operator = if (invert_logic) "!=" else "==";
850855 try writer.print("){s}.is_null {s} true;\n", .{ maybe_deref, operator });
851856 }
852857 return local;
......@@ -856,17 +861,26 @@ fn genOptionalPayload(o: *Object, inst: *Inst.UnOp) !CValue {
856861 const writer = o.writer();
857862 const operand = try o.resolveInst(inst.operand);
858863
864 const opt_ty = if (inst.operand.ty.zigTypeTag() == .Pointer)
865 inst.operand.ty.elemType()
866 else
867 inst.operand.ty;
868
859869 if (opt_ty.isPtrLikeOptional()) {
860870 // the operand is just a regular pointer, no need to do anything special.
871 // *?*T -> **T and ?*T -> *T are **T -> **T and *T -> *T in C
861872 return operand;
862873 }
863874
864 const opt_ty = if (inst.operand.ty.zigTypeTag() == .Pointer)
865 inst.operand.ty.elemType()
866 else
867 inst.operand.ty;
875 const maybe_deref = if (inst.operand.ty.zigTypeTag() == .Pointer) "->" else ".";
876 const maybe_addrof = if (inst.base.ty.zigTypeTag() == .Pointer) "&" else "";
877
878 const local = try o.allocLocal(inst.base.ty, .Const);
879 try writer.print(" = {s}(", .{maybe_addrof});
880 try o.writeCValue(writer, operand);
868881
869 return o.dg.fail(o.dg.decl.src(), "TODO: C backend: genOptionalPayload non ptr-like optionals", .{});
882 try writer.print("){s}payload;\n", .{maybe_deref});
883 return local;
870884}
871885
872886fn genWrapOptional(o: *Object, inst: *Inst.UnOp) !CValue {
......@@ -878,7 +892,12 @@ fn genWrapOptional(o: *Object, inst: *Inst.UnOp) !CValue {
878892 return operand;
879893 }
880894
881 return o.dg.fail(o.dg.decl.src(), "TODO: C backend: genWrapOptional non ptr-like optionals", .{});
895 // .wrap_optional is used to convert non-optionals into optionals so it can never be null.
896 const local = try o.allocLocal(inst.base.ty, .Const);
897 try writer.writeAll(" = { .is_null = false, .payload =");
898 try o.writeCValue(writer, operand);
899 try writer.writeAll("};\n");
900 return local;
882901}
883902
884903fn IndentWriter(comptime UnderlyingWriter: type) type {