2021-10-01 18:38:39 -04:00
|
|
|
// This file is a fragment of the yjit.o compilation unit. See yjit.c.
|
2020-09-17 17:09:42 -04:00
|
|
|
|
|
|
|
// Save caller-save registers on the stack before a C call
|
2021-10-01 18:38:39 -04:00
|
|
|
static void
|
|
|
|
push_regs(codeblock_t *cb)
|
2020-09-17 17:09:42 -04:00
|
|
|
{
|
|
|
|
push(cb, RAX);
|
|
|
|
push(cb, RCX);
|
|
|
|
push(cb, RDX);
|
|
|
|
push(cb, RSI);
|
|
|
|
push(cb, RDI);
|
|
|
|
push(cb, R8);
|
|
|
|
push(cb, R9);
|
|
|
|
push(cb, R10);
|
|
|
|
push(cb, R11);
|
|
|
|
pushfq(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore caller-save registers from the after a C call
|
2021-10-01 18:38:39 -04:00
|
|
|
static void
|
|
|
|
pop_regs(codeblock_t *cb)
|
2020-09-17 17:09:42 -04:00
|
|
|
{
|
|
|
|
popfq(cb);
|
|
|
|
pop(cb, R11);
|
|
|
|
pop(cb, R10);
|
|
|
|
pop(cb, R9);
|
|
|
|
pop(cb, R8);
|
|
|
|
pop(cb, RDI);
|
|
|
|
pop(cb, RSI);
|
|
|
|
pop(cb, RDX);
|
|
|
|
pop(cb, RCX);
|
|
|
|
pop(cb, RAX);
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:38:39 -04:00
|
|
|
static void
|
|
|
|
print_int_cfun(int64_t val)
|
2020-09-17 17:09:42 -04:00
|
|
|
{
|
2020-11-05 11:37:55 -05:00
|
|
|
fprintf(stderr, "%lld\n", (long long int)val);
|
2020-09-17 17:09:42 -04:00
|
|
|
}
|
|
|
|
|
2021-10-01 18:38:39 -04:00
|
|
|
RBIMPL_ATTR_MAYBE_UNUSED()
|
|
|
|
static void
|
|
|
|
print_int(codeblock_t *cb, x86opnd_t opnd)
|
2020-09-17 17:09:42 -04:00
|
|
|
{
|
2020-09-18 12:20:43 -04:00
|
|
|
push_regs(cb);
|
2020-09-17 17:09:42 -04:00
|
|
|
|
2020-09-18 12:20:43 -04:00
|
|
|
if (opnd.num_bits < 64 && opnd.type != OPND_IMM)
|
|
|
|
movsx(cb, RDI, opnd);
|
2020-09-17 17:09:42 -04:00
|
|
|
else
|
2020-09-18 12:20:43 -04:00
|
|
|
mov(cb, RDI, opnd);
|
2020-09-17 17:09:42 -04:00
|
|
|
|
|
|
|
// Call the print function
|
2020-10-05 08:09:58 -04:00
|
|
|
mov(cb, RAX, const_ptr_opnd((void*)&print_int_cfun));
|
2020-09-18 12:20:43 -04:00
|
|
|
call(cb, RAX);
|
2020-09-17 17:09:42 -04:00
|
|
|
|
2020-09-18 12:20:43 -04:00
|
|
|
pop_regs(cb);
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:38:39 -04:00
|
|
|
static void
|
|
|
|
print_ptr_cfun(void *val)
|
2020-10-14 14:03:31 -04:00
|
|
|
{
|
2020-11-05 11:37:55 -05:00
|
|
|
fprintf(stderr, "%p\n", val);
|
2020-10-14 14:03:31 -04:00
|
|
|
}
|
|
|
|
|
2021-10-01 18:38:39 -04:00
|
|
|
RBIMPL_ATTR_MAYBE_UNUSED()
|
|
|
|
static void
|
|
|
|
print_ptr(codeblock_t *cb, x86opnd_t opnd)
|
2020-10-14 14:03:31 -04:00
|
|
|
{
|
|
|
|
assert (opnd.num_bits == 64);
|
|
|
|
|
|
|
|
push_regs(cb);
|
|
|
|
|
|
|
|
mov(cb, RDI, opnd);
|
|
|
|
mov(cb, RAX, const_ptr_opnd((void*)&print_ptr_cfun));
|
|
|
|
call(cb, RAX);
|
|
|
|
|
|
|
|
pop_regs(cb);
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:38:39 -04:00
|
|
|
static void
|
|
|
|
print_str_cfun(const char *str)
|
2020-09-18 12:20:43 -04:00
|
|
|
{
|
2020-11-05 11:37:55 -05:00
|
|
|
fprintf(stderr, "%s\n", str);
|
2020-09-17 17:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print a constant string to stdout
|
2021-10-01 18:38:39 -04:00
|
|
|
static void
|
|
|
|
print_str(codeblock_t *cb, const char *str)
|
2020-09-17 17:09:42 -04:00
|
|
|
{
|
|
|
|
//as.comment("printStr(\"" ~ str ~ "\")");
|
|
|
|
size_t len = strlen(str);
|
|
|
|
|
|
|
|
push_regs(cb);
|
|
|
|
|
|
|
|
// Load the string address and jump over the string data
|
|
|
|
lea(cb, RDI, mem_opnd(8, RIP, 5));
|
|
|
|
jmp32(cb, (int32_t)len + 1);
|
|
|
|
|
|
|
|
// Write the string chars and a null terminator
|
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
|
|
cb_write_byte(cb, (uint8_t)str[i]);
|
|
|
|
cb_write_byte(cb, 0);
|
|
|
|
|
|
|
|
// Call the print function
|
2020-10-05 14:30:04 -04:00
|
|
|
mov(cb, RAX, const_ptr_opnd((void*)&print_str_cfun));
|
2020-09-17 17:09:42 -04:00
|
|
|
call(cb, RAX);
|
|
|
|
|
|
|
|
pop_regs(cb);
|
|
|
|
}
|