| ... | ... | @@ -3526,6 +3526,14 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3526 | 3526 | return CValue.none; |
| 3527 | 3527 | } |
| 3528 | 3528 | |
| 3529 | fn 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 | |
| 3529 | 3537 | fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3530 | 3538 | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 3531 | 3539 | const extra = f.air.extraData(Air.Asm, ty_pl.payload); |
| ... | ... | @@ -3551,10 +3559,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3551 | 3559 | break :local local; |
| 3552 | 3560 | } else .none; |
| 3553 | 3561 | |
| 3554 | | const output_locals_begin = f.next_local_index; |
| 3555 | | f.next_local_index += outputs.len; |
| 3562 | const locals_begin = f.next_local_index; |
| 3556 | 3563 | const constraints_extra_begin = extra_i; |
| 3557 | | for (outputs) |output, index| { |
| 3564 | for (outputs) |output| { |
| 3558 | 3565 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| 3559 | 3566 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| 3560 | 3567 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| ... | ... | @@ -3562,12 +3569,17 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3562 | 3569 | // for the string, we still use the next u32 for the null terminator. |
| 3563 | 3570 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 3564 | 3571 | |
| 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(); |
| 3567 | 3581 | 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); |
| 3571 | 3583 | try writer.writeAll(" __asm(\""); |
| 3572 | 3584 | try writer.writeAll(constraint["={".len .. constraint.len - "}".len]); |
| 3573 | 3585 | try writer.writeAll("\")"); |
| ... | ... | @@ -3576,13 +3588,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3576 | 3588 | try f.writeCValue(writer, .{ .undef = output_ty }, .Initializer); |
| 3577 | 3589 | } |
| 3578 | 3590 | try writer.writeAll(";\n"); |
| 3579 | | } else if (constraint.len < 2 or constraint[0] != '=') { |
| 3580 | | return f.fail("CBE: constraint not supported: '{s}'", .{constraint}); |
| 3581 | 3591 | } |
| 3582 | 3592 | } |
| 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| { |
| 3586 | 3594 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| 3587 | 3595 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| 3588 | 3596 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| ... | ... | @@ -3590,30 +3598,27 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3590 | 3598 | // for the string, we still use the next u32 for the null terminator. |
| 3591 | 3599 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 3592 | 3600 | |
| 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 | { |
| 3615 | 3604 | return f.fail("CBE: constraint not supported: '{s}'", .{constraint}); |
| 3616 | 3605 | } |
| 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 | } |
| 3617 | 3622 | } |
| 3618 | 3623 | { |
| 3619 | 3624 | var clobber_i: u32 = 0; |
| ... | ... | @@ -3631,6 +3636,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3631 | 3636 | try writer.print("({s}", .{fmtStringLiteral(asm_source)}); |
| 3632 | 3637 | |
| 3633 | 3638 | extra_i = constraints_extra_begin; |
| 3639 | var locals_index = locals_begin; |
| 3634 | 3640 | try writer.writeByte(':'); |
| 3635 | 3641 | for (outputs) |output, index| { |
| 3636 | 3642 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| ... | ... | @@ -3642,11 +3648,13 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3642 | 3648 | |
| 3643 | 3649 | if (index > 0) try writer.writeByte(','); |
| 3644 | 3650 | 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; |
| 3648 | 3657 | } else { |
| 3649 | | try writer.print("{s}(", .{fmtStringLiteral(constraint)}); |
| 3650 | 3658 | try f.writeCValueDeref(writer, try f.resolveInst(output)); |
| 3651 | 3659 | } |
| 3652 | 3660 | try writer.writeByte(')'); |
| ... | ... | @@ -3662,17 +3670,16 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3662 | 3670 | |
| 3663 | 3671 | if (index > 0) try writer.writeByte(','); |
| 3664 | 3672 | 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); |
| 3676 | 3683 | try writer.writeByte(')'); |
| 3677 | 3684 | } |
| 3678 | 3685 | try writer.writeByte(':'); |
| ... | ... | @@ -3693,7 +3700,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3693 | 3700 | try writer.writeAll(");\n"); |
| 3694 | 3701 | |
| 3695 | 3702 | extra_i = constraints_extra_begin; |
| 3696 | | for (outputs) |output, index| { |
| 3703 | locals_index = locals_begin; |
| 3704 | for (outputs) |output| { |
| 3697 | 3705 | const extra_bytes = std.mem.sliceAsBytes(f.air.extra[extra_i..]); |
| 3698 | 3706 | const constraint = std.mem.sliceTo(extra_bytes, 0); |
| 3699 | 3707 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| ... | ... | @@ -3701,13 +3709,15 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3701 | 3709 | // for the string, we still use the next u32 for the null terminator. |
| 3702 | 3710 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 3703 | 3711 | |
| 3704 | | if (constraint[1] == '{') { |
| 3712 | const is_reg = constraint[1] == '{'; |
| 3713 | if (is_reg) { |
| 3705 | 3714 | try f.writeCValueDeref(writer, if (output == .none) |
| 3706 | 3715 | CValue{ .local_ref = local.local } |
| 3707 | 3716 | else |
| 3708 | 3717 | try f.resolveInst(output)); |
| 3709 | 3718 | 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; |
| 3711 | 3721 | try writer.writeAll(";\n"); |
| 3712 | 3722 | } |
| 3713 | 3723 | } |