Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
yjit_utils.c
1// This file is a fragment of the yjit.o compilation unit. See yjit.c.
2
3// Save caller-save registers on the stack before a C call
4static void
5push_regs(codeblock_t *cb)
6{
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);
17}
18
19// Restore caller-save registers from the after a C call
20static void
21pop_regs(codeblock_t *cb)
22{
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);
33}
34
35static void
36print_int_cfun(int64_t val)
37{
38 fprintf(stderr, "%lld\n", (long long int)val);
39}
40
42static void
43print_int(codeblock_t *cb, x86opnd_t opnd)
44{
45 push_regs(cb);
46
47 if (opnd.num_bits < 64 && opnd.type != OPND_IMM)
48 movsx(cb, RDI, opnd);
49 else
50 mov(cb, RDI, opnd);
51
52 // Call the print function
53 mov(cb, RAX, const_ptr_opnd((void*)&print_int_cfun));
54 call(cb, RAX);
55
56 pop_regs(cb);
57}
58
59static void
60print_ptr_cfun(void *val)
61{
62 fprintf(stderr, "%p\n", val);
63}
64
66static void
67print_ptr(codeblock_t *cb, x86opnd_t opnd)
68{
69 assert (opnd.num_bits == 64);
70
71 push_regs(cb);
72
73 mov(cb, RDI, opnd);
74 mov(cb, RAX, const_ptr_opnd((void*)&print_ptr_cfun));
75 call(cb, RAX);
76
77 pop_regs(cb);
78}
79
80static void
81print_str_cfun(const char *str)
82{
83 fprintf(stderr, "%s\n", str);
84}
85
86// Print a constant string to stdout
87static void
88print_str(codeblock_t *cb, const char *str)
89{
90 //as.comment("printStr(\"" ~ str ~ "\")");
91 size_t len = strlen(str);
92
93 push_regs(cb);
94
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);
98
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);
103
104 // Call the print function
105 mov(cb, RAX, const_ptr_opnd((void*)&print_str_cfun));
106 call(cb, RAX);
107
108 pop_regs(cb);
109}
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]]
Definition: maybe_unused.h:33