| ... | ... | @@ -1,7 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const mem = std.mem; |
| 3 | 3 | const log = std.log.scoped(.c); |
| 4 | | const Writer = std.ArrayList(u8).Writer; |
| 5 | 4 | |
| 6 | 5 | const link = @import("../link.zig"); |
| 7 | 6 | const Module = @import("../Module.zig"); |
| ... | ... | @@ -42,6 +41,7 @@ pub const Object = struct { |
| 42 | 41 | value_map: CValueMap, |
| 43 | 42 | next_arg_index: usize = 0, |
| 44 | 43 | next_local_index: usize = 0, |
| 44 | indent_writer: std.io.AutoIndentingStream(std.ArrayList(u8).Writer), |
| 45 | 45 | |
| 46 | 46 | fn resolveInst(o: *Object, inst: *Inst) !CValue { |
| 47 | 47 | if (inst.value()) |_| { |
| ... | ... | @@ -58,31 +58,28 @@ pub const Object = struct { |
| 58 | 58 | |
| 59 | 59 | fn allocLocal(o: *Object, ty: Type, mutability: Mutability) !CValue { |
| 60 | 60 | const local_value = o.allocLocalValue(); |
| 61 | | try o.renderTypeAndName(o.code.writer(), ty, local_value, mutability); |
| 61 | try o.renderTypeAndName(o.writer(), ty, local_value, mutability); |
| 62 | 62 | return local_value; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | | fn indent(o: *Object) !void { |
| 66 | | const indent_size = 4; |
| 67 | | const indent_level = 1; |
| 68 | | const indent_amt = indent_size * indent_level; |
| 69 | | try o.code.writer().writeByteNTimes(' ', indent_amt); |
| 65 | fn writer(o: *Object) std.io.AutoIndentingStream(std.ArrayList(u8).Writer).Writer { |
| 66 | return o.indent_writer.writer(); |
| 70 | 67 | } |
| 71 | 68 | |
| 72 | | fn writeCValue(o: *Object, writer: Writer, c_value: CValue) !void { |
| 69 | fn writeCValue(o: *Object, w: anytype, c_value: CValue) !void { |
| 73 | 70 | switch (c_value) { |
| 74 | 71 | .none => unreachable, |
| 75 | | .local => |i| return writer.print("t{d}", .{i}), |
| 76 | | .local_ref => |i| return writer.print("&t{d}", .{i}), |
| 77 | | .constant => |inst| return o.dg.renderValue(writer, inst.ty, inst.value().?), |
| 78 | | .arg => |i| return writer.print("a{d}", .{i}), |
| 79 | | .decl => |decl| return writer.writeAll(mem.span(decl.name)), |
| 72 | .local => |i| return w.print("t{d}", .{i}), |
| 73 | .local_ref => |i| return w.print("&t{d}", .{i}), |
| 74 | .constant => |inst| return o.dg.renderValue(w, inst.ty, inst.value().?), |
| 75 | .arg => |i| return w.print("a{d}", .{i}), |
| 76 | .decl => |decl| return w.writeAll(mem.span(decl.name)), |
| 80 | 77 | } |
| 81 | 78 | } |
| 82 | 79 | |
| 83 | 80 | fn renderTypeAndName( |
| 84 | 81 | o: *Object, |
| 85 | | writer: Writer, |
| 82 | w: anytype, |
| 86 | 83 | ty: Type, |
| 87 | 84 | name: CValue, |
| 88 | 85 | mutability: Mutability, |
| ... | ... | @@ -98,15 +95,15 @@ pub const Object = struct { |
| 98 | 95 | render_ty = render_ty.elemType(); |
| 99 | 96 | } |
| 100 | 97 | |
| 101 | | try o.dg.renderType(writer, render_ty); |
| 98 | try o.dg.renderType(w, render_ty); |
| 102 | 99 | |
| 103 | 100 | const const_prefix = switch (mutability) { |
| 104 | 101 | .Const => "const ", |
| 105 | 102 | .Mut => "", |
| 106 | 103 | }; |
| 107 | | try writer.print(" {s}", .{const_prefix}); |
| 108 | | try o.writeCValue(writer, name); |
| 109 | | try writer.writeAll(suffix.items); |
| 104 | try w.print(" {s}", .{const_prefix}); |
| 105 | try o.writeCValue(w, name); |
| 106 | try w.writeAll(suffix.items); |
| 110 | 107 | } |
| 111 | 108 | }; |
| 112 | 109 | |
| ... | ... | @@ -127,7 +124,7 @@ pub const DeclGen = struct { |
| 127 | 124 | |
| 128 | 125 | fn renderValue( |
| 129 | 126 | dg: *DeclGen, |
| 130 | | writer: Writer, |
| 127 | writer: anytype, |
| 131 | 128 | t: Type, |
| 132 | 129 | val: Value, |
| 133 | 130 | ) error{ OutOfMemory, AnalysisFail }!void { |
| ... | ... | @@ -204,7 +201,7 @@ pub const DeclGen = struct { |
| 204 | 201 | } |
| 205 | 202 | } |
| 206 | 203 | |
| 207 | | fn renderFunctionSignature(dg: *DeclGen, w: Writer, is_global: bool) !void { |
| 204 | fn renderFunctionSignature(dg: *DeclGen, w: anytype, is_global: bool) !void { |
| 208 | 205 | if (!is_global) { |
| 209 | 206 | try w.writeAll("static "); |
| 210 | 207 | } |
| ... | ... | @@ -228,7 +225,7 @@ pub const DeclGen = struct { |
| 228 | 225 | try w.writeByte(')'); |
| 229 | 226 | } |
| 230 | 227 | |
| 231 | | fn renderType(dg: *DeclGen, w: Writer, t: Type) error{ OutOfMemory, AnalysisFail }!void { |
| 228 | fn renderType(dg: *DeclGen, w: anytype, t: Type) error{ OutOfMemory, AnalysisFail }!void { |
| 232 | 229 | switch (t.zigTypeTag()) { |
| 233 | 230 | .NoReturn => { |
| 234 | 231 | try w.writeAll("zig_noreturn void"); |
| ... | ... | @@ -325,20 +322,19 @@ pub fn genDecl(o: *Object) !void { |
| 325 | 322 | try fwd_decl_writer.writeAll(";\n"); |
| 326 | 323 | |
| 327 | 324 | const func: *Module.Fn = func_payload.data; |
| 328 | | const writer = o.code.writer(); |
| 329 | | try writer.writeAll("\n"); |
| 330 | | try o.dg.renderFunctionSignature(writer, is_global); |
| 325 | try o.indent_writer.insertNewline(); |
| 326 | try o.dg.renderFunctionSignature(o.writer(), is_global); |
| 331 | 327 | |
| 332 | 328 | try genBody(o, func.body); |
| 333 | 329 | |
| 334 | | try writer.writeAll("\n"); |
| 330 | try o.indent_writer.insertNewline(); |
| 335 | 331 | } else if (tv.val.tag() == .extern_fn) { |
| 336 | | const writer = o.code.writer(); |
| 332 | const writer = o.writer(); |
| 337 | 333 | try writer.writeAll("ZIG_EXTERN_C "); |
| 338 | 334 | try o.dg.renderFunctionSignature(writer, true); |
| 339 | 335 | try writer.writeAll(";\n"); |
| 340 | 336 | } else { |
| 341 | | const writer = o.code.writer(); |
| 337 | const writer = o.writer(); |
| 342 | 338 | try writer.writeAll("static "); |
| 343 | 339 | |
| 344 | 340 | // TODO ask the Decl if it is const |
| ... | ... | @@ -374,15 +370,15 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { |
| 374 | 370 | } |
| 375 | 371 | |
| 376 | 372 | pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!void { |
| 377 | | const writer = o.code.writer(); |
| 373 | const writer = o.writer(); |
| 378 | 374 | if (body.instructions.len == 0) { |
| 379 | 375 | try writer.writeAll(" {}"); |
| 380 | 376 | return; |
| 381 | 377 | } |
| 382 | 378 | |
| 383 | | try writer.writeAll(" {"); |
| 379 | try writer.writeAll(" {\n"); |
| 380 | o.indent_writer.pushIndent(); |
| 384 | 381 | |
| 385 | | try writer.writeAll("\n"); |
| 386 | 382 | for (body.instructions) |inst| { |
| 387 | 383 | const result_value = switch (inst.tag) { |
| 388 | 384 | .add => try genBinOp(o, inst.castTag(.add).?, " + "), |
| ... | ... | @@ -416,14 +412,14 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi |
| 416 | 412 | } |
| 417 | 413 | } |
| 418 | 414 | |
| 415 | o.indent_writer.popIndent(); |
| 419 | 416 | try writer.writeAll("}"); |
| 420 | 417 | } |
| 421 | 418 | |
| 422 | 419 | fn genAlloc(o: *Object, alloc: *Inst.NoOp) !CValue { |
| 423 | | const writer = o.code.writer(); |
| 420 | const writer = o.writer(); |
| 424 | 421 | |
| 425 | 422 | // First line: the variable used as data storage. |
| 426 | | try o.indent(); |
| 427 | 423 | const elem_type = alloc.base.ty.elemType(); |
| 428 | 424 | const mutability: Mutability = if (alloc.base.ty.isConstPtr()) .Const else .Mut; |
| 429 | 425 | const local = try o.allocLocal(elem_type, mutability); |
| ... | ... | @@ -439,15 +435,13 @@ fn genArg(o: *Object) CValue { |
| 439 | 435 | } |
| 440 | 436 | |
| 441 | 437 | fn genRetVoid(o: *Object) !CValue { |
| 442 | | try o.indent(); |
| 443 | | try o.code.writer().print("return;\n", .{}); |
| 438 | try o.writer().print("return;\n", .{}); |
| 444 | 439 | return CValue.none; |
| 445 | 440 | } |
| 446 | 441 | |
| 447 | 442 | fn genLoad(o: *Object, inst: *Inst.UnOp) !CValue { |
| 448 | 443 | const operand = try o.resolveInst(inst.operand); |
| 449 | | const writer = o.code.writer(); |
| 450 | | try o.indent(); |
| 444 | const writer = o.writer(); |
| 451 | 445 | const local = try o.allocLocal(inst.base.ty, .Const); |
| 452 | 446 | switch (operand) { |
| 453 | 447 | .local_ref => |i| { |
| ... | ... | @@ -467,8 +461,7 @@ fn genLoad(o: *Object, inst: *Inst.UnOp) !CValue { |
| 467 | 461 | |
| 468 | 462 | fn genRet(o: *Object, inst: *Inst.UnOp) !CValue { |
| 469 | 463 | const operand = try o.resolveInst(inst.operand); |
| 470 | | try o.indent(); |
| 471 | | const writer = o.code.writer(); |
| 464 | const writer = o.writer(); |
| 472 | 465 | try writer.writeAll("return "); |
| 473 | 466 | try o.writeCValue(writer, operand); |
| 474 | 467 | try writer.writeAll(";\n"); |
| ... | ... | @@ -481,8 +474,7 @@ fn genIntCast(o: *Object, inst: *Inst.UnOp) !CValue { |
| 481 | 474 | |
| 482 | 475 | const from = try o.resolveInst(inst.operand); |
| 483 | 476 | |
| 484 | | try o.indent(); |
| 485 | | const writer = o.code.writer(); |
| 477 | const writer = o.writer(); |
| 486 | 478 | const local = try o.allocLocal(inst.base.ty, .Const); |
| 487 | 479 | try writer.writeAll(" = ("); |
| 488 | 480 | try o.dg.renderType(writer, inst.base.ty); |
| ... | ... | @@ -497,8 +489,7 @@ fn genStore(o: *Object, inst: *Inst.BinOp) !CValue { |
| 497 | 489 | const dest_ptr = try o.resolveInst(inst.lhs); |
| 498 | 490 | const src_val = try o.resolveInst(inst.rhs); |
| 499 | 491 | |
| 500 | | try o.indent(); |
| 501 | | const writer = o.code.writer(); |
| 492 | const writer = o.writer(); |
| 502 | 493 | switch (dest_ptr) { |
| 503 | 494 | .local_ref => |i| { |
| 504 | 495 | const dest: CValue = .{ .local = i }; |
| ... | ... | @@ -525,8 +516,7 @@ fn genBinOp(o: *Object, inst: *Inst.BinOp, operator: []const u8) !CValue { |
| 525 | 516 | const lhs = try o.resolveInst(inst.lhs); |
| 526 | 517 | const rhs = try o.resolveInst(inst.rhs); |
| 527 | 518 | |
| 528 | | try o.indent(); |
| 529 | | const writer = o.code.writer(); |
| 519 | const writer = o.writer(); |
| 530 | 520 | const local = try o.allocLocal(inst.base.ty, .Const); |
| 531 | 521 | |
| 532 | 522 | try writer.writeAll(" = "); |
| ... | ... | @@ -552,8 +542,7 @@ fn genCall(o: *Object, inst: *Inst.Call) !CValue { |
| 552 | 542 | const unused_result = inst.base.isUnused(); |
| 553 | 543 | var result_local: CValue = .none; |
| 554 | 544 | |
| 555 | | try o.indent(); |
| 556 | | const writer = o.code.writer(); |
| 545 | const writer = o.writer(); |
| 557 | 546 | if (unused_result) { |
| 558 | 547 | if (ret_ty.hasCodeGenBits()) { |
| 559 | 548 | try writer.print("(void)", .{}); |
| ... | ... | @@ -596,8 +585,7 @@ fn genBlock(o: *Object, inst: *Inst.Block) !CValue { |
| 596 | 585 | fn genBitcast(o: *Object, inst: *Inst.UnOp) !CValue { |
| 597 | 586 | const operand = try o.resolveInst(inst.operand); |
| 598 | 587 | |
| 599 | | const writer = o.code.writer(); |
| 600 | | try o.indent(); |
| 588 | const writer = o.writer(); |
| 601 | 589 | if (inst.base.ty.zigTypeTag() == .Pointer and inst.operand.ty.zigTypeTag() == .Pointer) { |
| 602 | 590 | const local = try o.allocLocal(inst.base.ty, .Const); |
| 603 | 591 | try writer.writeAll(" = ("); |
| ... | ... | @@ -611,7 +599,6 @@ fn genBitcast(o: *Object, inst: *Inst.UnOp) !CValue { |
| 611 | 599 | |
| 612 | 600 | const local = try o.allocLocal(inst.base.ty, .Mut); |
| 613 | 601 | try writer.writeAll(";\n"); |
| 614 | | try o.indent(); |
| 615 | 602 | |
| 616 | 603 | try writer.writeAll("memcpy(&"); |
| 617 | 604 | try o.writeCValue(writer, local); |
| ... | ... | @@ -625,22 +612,19 @@ fn genBitcast(o: *Object, inst: *Inst.UnOp) !CValue { |
| 625 | 612 | } |
| 626 | 613 | |
| 627 | 614 | fn genBreakpoint(o: *Object, inst: *Inst.NoOp) !CValue { |
| 628 | | try o.indent(); |
| 629 | | try o.code.writer().writeAll("zig_breakpoint();\n"); |
| 615 | try o.writer().writeAll("zig_breakpoint();\n"); |
| 630 | 616 | return CValue.none; |
| 631 | 617 | } |
| 632 | 618 | |
| 633 | 619 | fn genUnreach(o: *Object, inst: *Inst.NoOp) !CValue { |
| 634 | | try o.indent(); |
| 635 | | try o.code.writer().writeAll("zig_unreachable();\n"); |
| 620 | try o.writer().writeAll("zig_unreachable();\n"); |
| 636 | 621 | return CValue.none; |
| 637 | 622 | } |
| 638 | 623 | |
| 639 | 624 | fn genLoop(o: *Object, inst: *Inst.Loop) !CValue { |
| 640 | | try o.indent(); |
| 641 | | try o.code.writer().writeAll("while (true)"); |
| 625 | try o.writer().writeAll("while (true)"); |
| 642 | 626 | try genBody(o, inst.body); |
| 643 | | try o.code.writer().writeAll("\n"); |
| 627 | try o.indent_writer.insertNewline(); |
| 644 | 628 | return CValue.none; |
| 645 | 629 | } |
| 646 | 630 | |
| ... | ... | @@ -648,13 +632,12 @@ fn genAsm(o: *Object, as: *Inst.Assembly) !CValue { |
| 648 | 632 | if (as.base.isUnused() and !as.is_volatile) |
| 649 | 633 | return CValue.none; |
| 650 | 634 | |
| 651 | | const writer = o.code.writer(); |
| 635 | const writer = o.writer(); |
| 652 | 636 | for (as.inputs) |i, index| { |
| 653 | 637 | if (i[0] == '{' and i[i.len - 1] == '}') { |
| 654 | 638 | const reg = i[1 .. i.len - 1]; |
| 655 | 639 | const arg = as.args[index]; |
| 656 | 640 | const arg_c_value = try o.resolveInst(arg); |
| 657 | | try o.indent(); |
| 658 | 641 | try writer.writeAll("register "); |
| 659 | 642 | try o.dg.renderType(writer, arg.ty); |
| 660 | 643 | |
| ... | ... | @@ -665,7 +648,6 @@ fn genAsm(o: *Object, as: *Inst.Assembly) !CValue { |
| 665 | 648 | return o.dg.fail(o.dg.decl.src(), "TODO non-explicit inline asm regs", .{}); |
| 666 | 649 | } |
| 667 | 650 | } |
| 668 | | try o.indent(); |
| 669 | 651 | const volatile_string: []const u8 = if (as.is_volatile) "volatile " else ""; |
| 670 | 652 | try writer.print("__asm {s}(\"{s}\"", .{ volatile_string, as.asm_source }); |
| 671 | 653 | if (as.output) |_| { |