dymokomi/luce-base
Source of release 0.45.0
0.45.0 / src / back / native / x86_64 / inline_asm.lucb
#==============================================================================================
#
# x86_64/inline_asm - Inline assembly
#
# DESCRIPTION:
# An `asm` block's operands placed in the registers it names, its text written, and
# its outputs stored (ยง8.9).
#
#==============================================================================================
extend Generator:
# mark: Inline assembly ====================================================================
## An asm block, with its outputs. The callee-saved registers its operands name
## (`set_reg` before it, `get_reg` after it, a discarded output among them) are pushed
## first, since the block may write them while the allocator keeps values there. After
## the body, an output that lives in a pushed register is pushed as well, the saved
## registers are reloaded from the stack, and only then is every output stored, so the
## addresses of the output places, which may live in those registers, are sound.
func inline_asm(n: ir.Instr) -> !:
let arch = self.program_unit.texts.get((usize)n.a.index).bytes
if arch != "x86_64":
return
self.pending = 0
var k = self.at
while k > 0 and self.f.instrs.get(k - 1).op == ir.Op.set_reg:
k -= 1
try self.note_named(self.f.instrs.get(k))
let first_output = self.at + 1
var last = first_output
while last < self.f.instrs.count and self.f.instrs.get(last).op == ir.Op.get_reg:
try self.note_named(self.f.instrs.get(last))
last += 1
var s: usize = 0
while s < self.pending:
try self.writer.line2("pushq ", self.saved[s])
s += 1
let body = self.program_unit.texts.get((usize)n.b.index).bytes
try self.writer.put("\n")
try self.writer.put(body)
# the outputs that live in a saved register, pushed above the saves
var values: usize = 0
var value_index: usize[16] = ---
k = first_output
while k < last:
let o = self.f.instrs.get(k)
value_index[k - first_output] = 16
if o.a.kind != ir.RefKind.none_:
let reg = wide_name(with_percent(self.program_unit.texts.get((usize)o.b.index).bytes))
if self.saved_index(reg) < self.pending:
try self.writer.line2("pushq ", reg)
value_index[k - first_output] = values
values += 1
k += 1
s = 0
while s < self.pending:
try self.writer.line("movq ")
try self.writer.put_u64((u64)(8 * (values + (self.pending - 1 - s))))
try self.writer.put("(%rsp), ")
try self.writer.put(self.saved[s])
s += 1
k = first_output
while k < last:
let o = self.f.instrs.get(k)
if o.a.kind != ir.RefKind.none_:
var reg = with_percent(self.program_unit.texts.get((usize)o.b.index).bytes)
let j = value_index[k - first_output]
if j < 16:
try self.writer.line("movq ")
try self.writer.put_u64((u64)(8 * (values - 1 - j)))
try self.writer.put("(%rsp), %rax")
reg = "%rax" if o.cls == ir.Class.l else "%eax"
try self.address_operand("%r10", o.a)
try self.writer.line3(move_of(o.cls), reg, ", (%r10)")
k += 1
if self.pending + values > 0:
try self.writer.line("addq $")
try self.writer.put_u64((u64)(8 * (self.pending + values)))
try self.writer.put(", %rsp")
self.pending = 0
## Remember the register `operand` names when the allocator may keep a value in it.
func note_named(operand: ir.Instr) -> !:
let reg = wide_name(with_percent(self.program_unit.texts.get((usize)operand.b.index).bytes))
if self.saved_index(reg) < self.pending:
return
var c: usize = 1
while c <= 5:
if self.q_name((u8)c) == reg and self.pending < 8:
self.saved[self.pending] = reg
self.pending += 1
return
c += 1
## Where `reg` sits among the saved registers, or `pending` when it does not.
func saved_index(reg: str) -> usize:
var s: usize = 0
while s < self.pending:
if self.saved[s] == reg:
return s
s += 1
return self.pending