mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07:45 +00:00
LibJIT: Support jumps to already linked labels
This commit is contained in:
parent
b2d8d0c270
commit
ff265d1900
1 changed files with 26 additions and 21 deletions
|
@ -219,12 +219,14 @@ struct Assembler {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Label {
|
struct Label {
|
||||||
size_t offset_of_label_in_instruction_stream { 0 };
|
Optional<size_t> offset_of_label_in_instruction_stream;
|
||||||
Vector<size_t> jump_slot_offsets_in_instruction_stream;
|
Vector<size_t> jump_slot_offsets_in_instruction_stream;
|
||||||
|
|
||||||
void add_jump(size_t offset)
|
void add_jump(Assembler& assembler, size_t offset)
|
||||||
{
|
{
|
||||||
jump_slot_offsets_in_instruction_stream.append(offset);
|
jump_slot_offsets_in_instruction_stream.append(offset);
|
||||||
|
if (offset_of_label_in_instruction_stream.has_value())
|
||||||
|
link_jump(assembler, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void link(Assembler& assembler)
|
void link(Assembler& assembler)
|
||||||
|
@ -234,24 +236,27 @@ struct Assembler {
|
||||||
|
|
||||||
void link_to(Assembler& assembler, size_t link_offset)
|
void link_to(Assembler& assembler, size_t link_offset)
|
||||||
{
|
{
|
||||||
|
VERIFY(!offset_of_label_in_instruction_stream.has_value());
|
||||||
offset_of_label_in_instruction_stream = link_offset;
|
offset_of_label_in_instruction_stream = link_offset;
|
||||||
for (auto offset_in_instruction_stream : jump_slot_offsets_in_instruction_stream) {
|
for (auto offset_in_instruction_stream : jump_slot_offsets_in_instruction_stream)
|
||||||
auto offset = offset_of_label_in_instruction_stream - offset_in_instruction_stream;
|
link_jump(assembler, offset_in_instruction_stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void link_jump(Assembler& assembler, size_t offset_in_instruction_stream)
|
||||||
|
{
|
||||||
|
auto offset = offset_of_label_in_instruction_stream.value() - offset_in_instruction_stream;
|
||||||
auto jump_slot = offset_in_instruction_stream - 4;
|
auto jump_slot = offset_in_instruction_stream - 4;
|
||||||
assembler.m_output[jump_slot + 0] = (offset >> 0) & 0xff;
|
assembler.m_output[jump_slot + 0] = (offset >> 0) & 0xff;
|
||||||
assembler.m_output[jump_slot + 1] = (offset >> 8) & 0xff;
|
assembler.m_output[jump_slot + 1] = (offset >> 8) & 0xff;
|
||||||
assembler.m_output[jump_slot + 2] = (offset >> 16) & 0xff;
|
assembler.m_output[jump_slot + 2] = (offset >> 16) & 0xff;
|
||||||
assembler.m_output[jump_slot + 3] = (offset >> 24) & 0xff;
|
assembler.m_output[jump_slot + 3] = (offset >> 24) & 0xff;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
[[nodiscard]] Label make_label()
|
[[nodiscard]] Label make_label()
|
||||||
{
|
{
|
||||||
return Label {
|
return Label {};
|
||||||
.offset_of_label_in_instruction_stream = m_output.size(),
|
|
||||||
.jump_slot_offsets_in_instruction_stream = {},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] Label jump()
|
[[nodiscard]] Label jump()
|
||||||
|
@ -260,7 +265,7 @@ struct Assembler {
|
||||||
emit8(0xe9);
|
emit8(0xe9);
|
||||||
emit32(0xdeadbeef);
|
emit32(0xdeadbeef);
|
||||||
auto label = make_label();
|
auto label = make_label();
|
||||||
label.add_jump(m_output.size());
|
label.add_jump(*this, m_output.size());
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,7 +274,7 @@ struct Assembler {
|
||||||
// jmp target (RIP-relative 32-bit offset)
|
// jmp target (RIP-relative 32-bit offset)
|
||||||
emit8(0xe9);
|
emit8(0xe9);
|
||||||
emit32(0xdeadbeef);
|
emit32(0xdeadbeef);
|
||||||
label.add_jump(m_output.size());
|
label.add_jump(*this, m_output.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void jump(Operand op)
|
void jump(Operand op)
|
||||||
|
@ -341,7 +346,7 @@ struct Assembler {
|
||||||
emit8(0x0f);
|
emit8(0x0f);
|
||||||
emit8(0x84);
|
emit8(0x84);
|
||||||
emit32(0xdeadbeef);
|
emit32(0xdeadbeef);
|
||||||
label.add_jump(m_output.size());
|
label.add_jump(*this, m_output.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void jump_if_not_zero(Operand reg, Label& label)
|
void jump_if_not_zero(Operand reg, Label& label)
|
||||||
|
@ -352,7 +357,7 @@ struct Assembler {
|
||||||
emit8(0x0f);
|
emit8(0x0f);
|
||||||
emit8(0x85);
|
emit8(0x85);
|
||||||
emit32(0xdeadbeef);
|
emit32(0xdeadbeef);
|
||||||
label.add_jump(m_output.size());
|
label.add_jump(*this, m_output.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void jump_if_equal(Operand lhs, Operand rhs, Label& label)
|
void jump_if_equal(Operand lhs, Operand rhs, Label& label)
|
||||||
|
@ -368,7 +373,7 @@ struct Assembler {
|
||||||
emit8(0x0f);
|
emit8(0x0f);
|
||||||
emit8(0x84);
|
emit8(0x84);
|
||||||
emit32(0xdeadbeef);
|
emit32(0xdeadbeef);
|
||||||
label.add_jump(m_output.size());
|
label.add_jump(*this, m_output.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void jump_if_not_equal(Operand lhs, Operand rhs, Label& label)
|
void jump_if_not_equal(Operand lhs, Operand rhs, Label& label)
|
||||||
|
@ -384,7 +389,7 @@ struct Assembler {
|
||||||
emit8(0x0f);
|
emit8(0x0f);
|
||||||
emit8(0x85);
|
emit8(0x85);
|
||||||
emit32(0xdeadbeef);
|
emit32(0xdeadbeef);
|
||||||
label.add_jump(m_output.size());
|
label.add_jump(*this, m_output.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void jump_if_less_than(Operand lhs, Operand rhs, Label& label)
|
void jump_if_less_than(Operand lhs, Operand rhs, Label& label)
|
||||||
|
@ -395,7 +400,7 @@ struct Assembler {
|
||||||
emit8(0x0f);
|
emit8(0x0f);
|
||||||
emit8(0x8c);
|
emit8(0x8c);
|
||||||
emit32(0xdeadbeef);
|
emit32(0xdeadbeef);
|
||||||
label.add_jump(m_output.size());
|
label.add_jump(*this, m_output.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void sign_extend_32_to_64_bits(Reg reg)
|
void sign_extend_32_to_64_bits(Reg reg)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue