1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#include "internal.h"
#include "ujit_asm.h"
#include "ujit_iface.h"
#include "ujit_core.h"
#include "ujit_codegen.h"
// Table of block versions indexed by (iseq, index) tuples
st_table * version_tbl;
int blockid_cmp(st_data_t arg0, st_data_t arg1)
{
const blockid_t *block0 = (const blockid_t*)arg0;
const blockid_t *block1 = (const blockid_t*)arg1;
return block0->iseq == block1->iseq && block0->idx == block1->idx;
}
st_index_t blockid_hash(st_data_t arg)
{
const blockid_t *blockid = (const blockid_t*)arg;
st_index_t hash0 = st_numhash((st_data_t)blockid->iseq);
st_index_t hash1 = st_numhash((st_data_t)blockid->idx);
// Use XOR to combine the hashes
return hash0 ^ hash1;
}
static const struct st_hash_type hashtype_blockid = {
blockid_cmp,
blockid_hash,
};
// Retrieve a basic block version for an (iseq, idx) tuple
// TODO: we need to add a versioning context here
uint8_t* get_block_version(const rb_iseq_t *iseq, unsigned int idx /*, ctx_t* ctx */)
{
blockid_t blockid = { iseq, idx };
// If there exists a version for this block id
st_data_t st_version;
if (rb_st_lookup(version_tbl, (st_data_t)&blockid, &st_version)) {
return (uint8_t*)st_version;
}
uint8_t* code_ptr = ujit_compile_block(iseq, idx, false);
st_insert(version_tbl, (st_data_t)&blockid, (st_data_t)code_ptr);
return code_ptr;
}
//
// Method to generate stubs for branches
// TODO: get_branch_stub() or get_branch() function
//
// Get the current instruction opcode from the context object
int
ctx_get_opcode(ctx_t *ctx)
{
return opcode_at_pc(ctx->iseq, ctx->pc);
}
// Get an instruction argument from the context object
VALUE
ctx_get_arg(ctx_t* ctx, size_t arg_idx)
{
assert (arg_idx + 1 < insn_len(ctx_get_opcode(ctx)));
return *(ctx->pc + arg_idx + 1);
}
/*
Get an operand for the adjusted stack pointer address
*/
x86opnd_t
ctx_sp_opnd(ctx_t* ctx, int32_t offset_bytes)
{
int32_t offset = (ctx->stack_size) * 8 + offset_bytes;
return mem_opnd(64, REG_SP, offset);
}
/*
Make space on the stack for N values
Return a pointer to the new stack top
*/
x86opnd_t
ctx_stack_push(ctx_t* ctx, size_t n)
{
ctx->stack_size += n;
// SP points just above the topmost value
int32_t offset = (ctx->stack_size - 1) * 8;
return mem_opnd(64, REG_SP, offset);
}
/*
Pop N values off the stack
Return a pointer to the stack top before the pop operation
*/
x86opnd_t
ctx_stack_pop(ctx_t* ctx, size_t n)
{
// SP points just above the topmost value
int32_t offset = (ctx->stack_size - 1) * 8;
x86opnd_t top = mem_opnd(64, REG_SP, offset);
ctx->stack_size -= n;
return top;
}
x86opnd_t
ctx_stack_opnd(ctx_t* ctx, int32_t idx)
{
// SP points just above the topmost value
int32_t offset = (ctx->stack_size - 1 - idx) * 8;
x86opnd_t opnd = mem_opnd(64, REG_SP, offset);
return opnd;
}
void
ujit_init_core(void)
{
// Initialize the version hash table
version_tbl = st_init_table(&hashtype_blockid);
}
|