authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-20 02:04:40-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-25 05:11:29-04:00
log3d90ee50ff184dd7a94005f2769d055e460ad290
treee8bf4cbe145ed49f283296febaac91ab3d2ec009
parent4fdac5f1c9cad90450cbad5a8dd276a597aba870

cbe: allow immediate and register asm constraints in naked functions


1 files changed, 63 insertions(+), 53 deletions(-)

src/codegen/c.zig+63-53
......@@ -3526,6 +3526,14 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
35263526 return CValue.none;
35273527}
35283528
3529fn asmInputNeedsLocal(constraint: []const u8, value: CValue) bool {
3530 return switch (constraint[0]) {
3531 '{' => true,
3532 'i', 'r' => false,
3533 else => value == .constant,
3534 };
3535}
3536
35293537fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
35303538 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
35313539 const extra = f.air.extraData(Air.Asm, ty_pl.payload);
......@@ -3551,10 +3559,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
35513559 break :local local;
35523560 } else .none;
35533561
3554 const output_locals_begin = f.next_local_index;
3555 f.next_local_index += outputs.len;
3562 const locals_begin = f.next_local_index;
35563563 const constraints_extra_begin = extra_i;
3557 for (outputs) |output, index| {
3564 for (outputs) |output| {
35583565 const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]);
35593566 const constraint = std.mem.sliceTo(extra_bytes, 0);
35603567 const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0);
......@@ -3562,12 +3569,17 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
35623569 // for the string, we still use the next u32 for the null terminator.
35633570 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
35643571
3565 const output_ty = if (output == .none) inst_ty else f.air.typeOf(output).childType();
3566 if (std.mem.startsWith(u8, constraint, "={") and std.mem.endsWith(u8, constraint, "}")) {
3572 if (constraint.len < 2 or constraint[0] != '=' or
3573 (constraint[1] == '{' and constraint[constraint.len - 1] != '}'))
3574 {
3575 return f.fail("CBE: constraint not supported: '{s}'", .{constraint});
3576 }
3577
3578 const is_reg = constraint[1] == '{';
3579 if (is_reg) {
3580 const output_ty = if (output == .none) inst_ty else f.air.typeOf(output).childType();
35673581 try writer.writeAll("register ");
3568 try f.object.dg.renderTypeAndName(writer, output_ty, .{
3569 .local = output_locals_begin + index,
3570 }, .Mut, 0);
3582 _ = try f.allocLocal(output_ty, .Mut);
35713583 try writer.writeAll(" __asm(\"");
35723584 try writer.writeAll(constraint["={".len .. constraint.len - "}".len]);
35733585 try writer.writeAll("\")");
......@@ -3576,13 +3588,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
35763588 try f.writeCValue(writer, .{ .undef = output_ty }, .Initializer);
35773589 }
35783590 try writer.writeAll(";\n");
3579 } else if (constraint.len < 2 or constraint[0] != '=') {
3580 return f.fail("CBE: constraint not supported: '{s}'", .{constraint});
35813591 }
35823592 }
3583 const input_locals_begin = f.next_local_index;
3584 f.next_local_index += inputs.len;
3585 for (inputs) |input, index| {
3593 for (inputs) |input| {
35863594 const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]);
35873595 const constraint = std.mem.sliceTo(extra_bytes, 0);
35883596 const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0);
......@@ -3590,30 +3598,27 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
35903598 // for the string, we still use the next u32 for the null terminator.
35913599 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
35923600
3593 const input_ty = f.air.typeOf(input);
3594 if (std.mem.startsWith(u8, constraint, "{") and std.mem.endsWith(u8, constraint, "}")) {
3595 try writer.writeAll("register ");
3596 try f.object.dg.renderTypeAndName(writer, input_ty, .{
3597 .local = input_locals_begin + index,
3598 }, .Const, 0);
3599 try writer.writeAll(" __asm(\"");
3600 try writer.writeAll(constraint["{".len .. constraint.len - "}".len]);
3601 try writer.writeAll("\") = ");
3602 try f.writeCValue(writer, try f.resolveInst(input), .Initializer);
3603 try writer.writeAll(";\n");
3604 } else if (constraint.len >= 1 and std.mem.indexOfScalar(u8, "=+&%", constraint[0]) == null) {
3605 const input_val = try f.resolveInst(input);
3606 if (input_val == .constant) {
3607 try f.object.dg.renderTypeAndName(writer, input_ty, .{
3608 .local = input_locals_begin + index,
3609 }, .Const, 0);
3610 try writer.writeAll(" = ");
3611 try f.writeCValue(writer, input_val, .Initializer);
3612 try writer.writeAll(";\n");
3613 }
3614 } else {
3601 if (constraint.len < 1 or std.mem.indexOfScalar(u8, "=+&%", constraint[0]) != null or
3602 (constraint[0] == '{' and constraint[constraint.len - 1] != '}'))
3603 {
36153604 return f.fail("CBE: constraint not supported: '{s}'", .{constraint});
36163605 }
3606
3607 const is_reg = constraint[0] == '{';
3608 const input_val = try f.resolveInst(input);
3609 if (asmInputNeedsLocal(constraint, input_val)) {
3610 const input_ty = f.air.typeOf(input);
3611 if (is_reg) try writer.writeAll("register ");
3612 _ = try f.allocLocal(input_ty, .Const);
3613 if (is_reg) {
3614 try writer.writeAll(" __asm(\"");
3615 try writer.writeAll(constraint["{".len .. constraint.len - "}".len]);
3616 try writer.writeAll("\")");
3617 }
3618 try writer.writeAll(" = ");
3619 try f.writeCValue(writer, input_val, .Initializer);
3620 try writer.writeAll(";\n");
3621 }
36173622 }
36183623 {
36193624 var clobber_i: u32 = 0;
......@@ -3631,6 +3636,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
36313636 try writer.print("({s}", .{fmtStringLiteral(asm_source)});
36323637
36333638 extra_i = constraints_extra_begin;
3639 var locals_index = locals_begin;
36343640 try writer.writeByte(':');
36353641 for (outputs) |output, index| {
36363642 const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]);
......@@ -3642,11 +3648,13 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
36423648
36433649 if (index > 0) try writer.writeByte(',');
36443650 try writer.writeByte(' ');
3645 if (constraint[1] == '{') {
3646 try writer.print("{s}(", .{fmtStringLiteral("=r")});
3647 try f.writeCValue(writer, .{ .local = output_locals_begin + index }, .Other);
3651 if (!std.mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name});
3652 const is_reg = constraint[1] == '{';
3653 try writer.print("{s}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint)});
3654 if (is_reg) {
3655 try f.writeCValue(writer, .{ .local = locals_index }, .Other);
3656 locals_index += 1;
36483657 } else {
3649 try writer.print("{s}(", .{fmtStringLiteral(constraint)});
36503658 try f.writeCValueDeref(writer, try f.resolveInst(output));
36513659 }
36523660 try writer.writeByte(')');
......@@ -3662,17 +3670,16 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
36623670
36633671 if (index > 0) try writer.writeByte(',');
36643672 try writer.writeByte(' ');
3665 if (constraint[0] == '{') {
3666 try writer.print("{s}(", .{fmtStringLiteral("r")});
3667 try f.writeCValue(writer, .{ .local = input_locals_begin + index }, .Other);
3668 } else {
3669 const input_val = try f.resolveInst(input);
3670 try writer.print("{s}(", .{fmtStringLiteral(constraint)});
3671 try f.writeCValue(writer, if (input_val == .constant)
3672 CValue{ .local = input_locals_begin + index }
3673 else
3674 input_val, .Other);
3675 }
3673 if (!std.mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name});
3674
3675 const is_reg = constraint[0] == '{';
3676 const input_val = try f.resolveInst(input);
3677 try writer.print("{s}(", .{fmtStringLiteral(if (is_reg) "r" else constraint)});
3678 try f.writeCValue(writer, if (asmInputNeedsLocal(constraint, input_val)) local: {
3679 const input_local = CValue{ .local = locals_index };
3680 locals_index += 1;
3681 break :local input_local;
3682 } else input_val, .Other);
36763683 try writer.writeByte(')');
36773684 }
36783685 try writer.writeByte(':');
......@@ -3693,7 +3700,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
36933700 try writer.writeAll(");\n");
36943701
36953702 extra_i = constraints_extra_begin;
3696 for (outputs) |output, index| {
3703 locals_index = locals_begin;
3704 for (outputs) |output| {
36973705 const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]);
36983706 const constraint = std.mem.sliceTo(extra_bytes, 0);
36993707 const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0);
......@@ -3701,13 +3709,15 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
37013709 // for the string, we still use the next u32 for the null terminator.
37023710 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
37033711
3704 if (constraint[1] == '{') {
3712 const is_reg = constraint[1] == '{';
3713 if (is_reg) {
37053714 try f.writeCValueDeref(writer, if (output == .none)
37063715 CValue{ .local_ref = local.local }
37073716 else
37083717 try f.resolveInst(output));
37093718 try writer.writeAll(" = ");
3710 try f.writeCValue(writer, .{ .local = output_locals_begin + index }, .Other);
3719 try f.writeCValue(writer, .{ .local = locals_index }, .Other);
3720 locals_index += 1;
37113721 try writer.writeAll(";\n");
37123722 }
37133723 }