* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / yjit_utils.c
blobcbc50a493254145b055979f594b7ad5778718b80
1 // This file is a fragment of the yjit.o compilation unit. See yjit.c.
3 // Save caller-save registers on the stack before a C call
4 static void
5 push_regs(codeblock_t *cb)
7 push(cb, RAX);
8 push(cb, RCX);
9 push(cb, RDX);
10 push(cb, RSI);
11 push(cb, RDI);
12 push(cb, R8);
13 push(cb, R9);
14 push(cb, R10);
15 push(cb, R11);
16 pushfq(cb);
19 // Restore caller-save registers from the after a C call
20 static void
21 pop_regs(codeblock_t *cb)
23 popfq(cb);
24 pop(cb, R11);
25 pop(cb, R10);
26 pop(cb, R9);
27 pop(cb, R8);
28 pop(cb, RDI);
29 pop(cb, RSI);
30 pop(cb, RDX);
31 pop(cb, RCX);
32 pop(cb, RAX);
35 static void
36 print_int_cfun(int64_t val)
38 fprintf(stderr, "%lld\n", (long long int)val);
41 RBIMPL_ATTR_MAYBE_UNUSED()
42 static void
43 print_int(codeblock_t *cb, x86opnd_t opnd)
45 push_regs(cb);
47 if (opnd.num_bits < 64 && opnd.type != OPND_IMM)
48 movsx(cb, RDI, opnd);
49 else
50 mov(cb, RDI, opnd);
52 // Call the print function
53 mov(cb, RAX, const_ptr_opnd((void*)&print_int_cfun));
54 call(cb, RAX);
56 pop_regs(cb);
59 static void
60 print_ptr_cfun(void *val)
62 fprintf(stderr, "%p\n", val);
65 RBIMPL_ATTR_MAYBE_UNUSED()
66 static void
67 print_ptr(codeblock_t *cb, x86opnd_t opnd)
69 assert (opnd.num_bits == 64);
71 push_regs(cb);
73 mov(cb, RDI, opnd);
74 mov(cb, RAX, const_ptr_opnd((void*)&print_ptr_cfun));
75 call(cb, RAX);
77 pop_regs(cb);
80 static void
81 print_str_cfun(const char *str)
83 fprintf(stderr, "%s\n", str);
86 // Print a constant string to stdout
87 static void
88 print_str(codeblock_t *cb, const char *str)
90 //as.comment("printStr(\"" ~ str ~ "\")");
91 size_t len = strlen(str);
93 push_regs(cb);
95 // Load the string address and jump over the string data
96 lea(cb, RDI, mem_opnd(8, RIP, 5));
97 jmp32(cb, (int32_t)len + 1);
99 // Write the string chars and a null terminator
100 for (size_t i = 0; i < len; ++i)
101 cb_write_byte(cb, (uint8_t)str[i]);
102 cb_write_byte(cb, 0);
104 // Call the print function
105 mov(cb, RAX, const_ptr_opnd((void*)&print_str_cfun));
106 call(cb, RAX);
108 pop_regs(cb);