Skip to content

Commit ff6bb12

Browse files
authored
Merge pull request #5750 from marxin/singlepass-jmp_on_condition
Singlepass: refactor condition jumps to single fn (`jmp_on_condition`)
2 parents 3189527 + d58cb47 commit ff6bb12

File tree

4 files changed

+99
-78
lines changed

4 files changed

+99
-78
lines changed

lib/compiler-singlepass/src/codegen.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
common_decl::*,
88
config::Singlepass,
99
location::{Location, Reg},
10-
machine::{Label, Machine, MachineStackOffset, NATIVE_PAGE_SIZE},
10+
machine::{Label, Machine, MachineStackOffset, UnsignedCondition, NATIVE_PAGE_SIZE},
1111
unwind::UnwindFrame,
1212
};
1313
#[cfg(feature = "unwind")]
@@ -2818,10 +2818,13 @@ impl<'a, M: Machine> FuncGen<'a, M> {
28182818
)?;
28192819
}
28202820

2821-
self.machine
2822-
.location_cmp(Size::S32, func_index, Location::GPR(table_count))?;
2823-
self.machine
2824-
.jmp_on_belowequal(self.special_labels.table_access_oob)?;
2821+
self.machine.jmp_on_condition(
2822+
UnsignedCondition::BelowEqual,
2823+
Size::S32,
2824+
func_index,
2825+
Location::GPR(table_count),
2826+
self.special_labels.table_access_oob,
2827+
)?;
28252828
self.machine
28262829
.move_location(Size::S32, func_index, Location::GPR(table_count))?;
28272830
self.machine.emit_imul_imm32(
@@ -2843,13 +2846,13 @@ impl<'a, M: Machine> FuncGen<'a, M> {
28432846
Location::GPR(table_count),
28442847
)?;
28452848
// Trap if the FuncRef is null
2846-
self.machine.location_cmp(
2849+
self.machine.jmp_on_condition(
2850+
UnsignedCondition::Equal,
28472851
Size::S64,
28482852
Location::Imm32(0),
28492853
Location::GPR(table_count),
2854+
self.special_labels.indirect_call_null,
28502855
)?;
2851-
self.machine
2852-
.jmp_on_equal(self.special_labels.indirect_call_null)?;
28532856
self.machine.move_location(
28542857
Size::S64,
28552858
Location::Memory(
@@ -2860,17 +2863,16 @@ impl<'a, M: Machine> FuncGen<'a, M> {
28602863
)?;
28612864

28622865
// Trap if signature mismatches.
2863-
self.machine.location_cmp(
2866+
self.machine.jmp_on_condition(
2867+
UnsignedCondition::NotEqual,
28642868
Size::S32,
28652869
Location::GPR(sigidx),
28662870
Location::Memory(
28672871
table_count,
28682872
(self.vmoffsets.vmcaller_checked_anyfunc_type_index() as usize) as i32,
28692873
),
2874+
self.special_labels.bad_signature,
28702875
)?;
2871-
self.machine
2872-
.jmp_on_different(self.special_labels.bad_signature)?;
2873-
28742876
self.machine.release_gpr(sigidx);
28752877
self.machine.release_gpr(table_count);
28762878
self.machine.release_gpr(table_base);
@@ -2981,9 +2983,13 @@ impl<'a, M: Machine> FuncGen<'a, M> {
29812983
state_diff_id: self.get_state_diff(),
29822984
};
29832985
self.control_stack.push(frame);
2984-
self.machine
2985-
.emit_relaxed_cmp(Size::S32, Location::Imm32(0), cond)?;
2986-
self.machine.jmp_on_equal(label_else)?;
2986+
self.machine.jmp_on_condition(
2987+
UnsignedCondition::Equal,
2988+
Size::S32,
2989+
Location::Imm32(0),
2990+
cond,
2991+
label_else,
2992+
)?;
29872993
}
29882994
Operator::Else => {
29892995
let frame = self.control_stack.last_mut().unwrap();
@@ -3051,9 +3057,13 @@ impl<'a, M: Machine> FuncGen<'a, M> {
30513057
let end_label = self.machine.get_label();
30523058
let zero_label = self.machine.get_label();
30533059

3054-
self.machine
3055-
.emit_relaxed_cmp(Size::S32, Location::Imm32(0), cond)?;
3056-
self.machine.jmp_on_equal(zero_label)?;
3060+
self.machine.jmp_on_condition(
3061+
UnsignedCondition::Equal,
3062+
Size::S32,
3063+
Location::Imm32(0),
3064+
cond,
3065+
zero_label,
3066+
)?;
30573067
match cncl {
30583068
Some((Some(fp), _))
30593069
if self.machine.arch_supports_canonicalize_nan()
@@ -4051,9 +4061,13 @@ impl<'a, M: Machine> FuncGen<'a, M> {
40514061
Operator::BrIf { relative_depth } => {
40524062
let after = self.machine.get_label();
40534063
let cond = self.pop_value_released()?;
4054-
self.machine
4055-
.emit_relaxed_cmp(Size::S32, Location::Imm32(0), cond)?;
4056-
self.machine.jmp_on_equal(after)?;
4064+
self.machine.jmp_on_condition(
4065+
UnsignedCondition::Equal,
4066+
Size::S32,
4067+
Location::Imm32(0),
4068+
cond,
4069+
after,
4070+
)?;
40574071

40584072
let frame =
40594073
&self.control_stack[self.control_stack.len() - 1 - (relative_depth as usize)];
@@ -4096,12 +4110,13 @@ impl<'a, M: Machine> FuncGen<'a, M> {
40964110
let table_label = self.machine.get_label();
40974111
let mut table: Vec<Label> = vec![];
40984112
let default_br = self.machine.get_label();
4099-
self.machine.emit_relaxed_cmp(
4113+
self.machine.jmp_on_condition(
4114+
UnsignedCondition::AboveEqual,
41004115
Size::S32,
41014116
Location::Imm32(targets.len() as u32),
41024117
cond,
4118+
default_br,
41034119
)?;
4104-
self.machine.jmp_on_aboveequal(default_br)?;
41054120

41064121
self.machine.emit_jmp_to_jumptable(table_label, cond)?;
41074122

lib/compiler-singlepass/src/machine.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ pub const NATIVE_PAGE_SIZE: usize = 4096;
5858

5959
pub struct MachineStackOffset(pub usize);
6060

61+
#[allow(dead_code)]
62+
pub enum UnsignedCondition {
63+
Equal,
64+
NotEqual,
65+
Above,
66+
AboveEqual,
67+
Below,
68+
BelowEqual,
69+
}
70+
6171
#[allow(unused)]
6272
pub trait Machine {
6373
type GPR: Copy + Eq + Debug + Reg;
@@ -382,24 +392,16 @@ pub trait Machine {
382392

383393
/// jmp without condidtion
384394
fn jmp_unconditionnal(&mut self, label: Label) -> Result<(), CompileError>;
385-
/// jmp on equal (src==dst)
386-
/// like Equal set on x86_64
387-
fn jmp_on_equal(&mut self, label: Label) -> Result<(), CompileError>;
388-
/// jmp on different (src!=dst)
389-
/// like NotEqual set on x86_64
390-
fn jmp_on_different(&mut self, label: Label) -> Result<(), CompileError>;
391-
/// jmp on above (src>dst)
392-
/// like Above set on x86_64
393-
fn jmp_on_above(&mut self, label: Label) -> Result<(), CompileError>;
394-
/// jmp on above (src>=dst)
395-
/// like Above or Equal set on x86_64
396-
fn jmp_on_aboveequal(&mut self, label: Label) -> Result<(), CompileError>;
397-
/// jmp on above (src<=dst)
398-
/// like Below or Equal set on x86_64
399-
fn jmp_on_belowequal(&mut self, label: Label) -> Result<(), CompileError>;
400-
/// jmp on overflow
401-
/// like Carry set on x86_64
402-
fn jmp_on_overflow(&mut self, label: Label) -> Result<(), CompileError>;
395+
396+
/// jmp to label if the provided condition is true (when comparing source and dest)
397+
fn jmp_on_condition(
398+
&mut self,
399+
cond: UnsignedCondition,
400+
size: Size,
401+
source: Location<Self::GPR, Self::SIMD>,
402+
dest: Location<Self::GPR, Self::SIMD>,
403+
label: Label,
404+
) -> Result<(), CompileError>;
403405

404406
/// jmp using a jump table at lable with cond as the indice
405407
fn emit_jmp_to_jumptable(

lib/compiler-singlepass/src/machine_arm64.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,23 +2604,25 @@ impl Machine for MachineARM64 {
26042604
fn jmp_unconditionnal(&mut self, label: Label) -> Result<(), CompileError> {
26052605
self.assembler.emit_b_label(label)
26062606
}
2607-
fn jmp_on_equal(&mut self, label: Label) -> Result<(), CompileError> {
2608-
self.assembler.emit_bcond_label_far(Condition::Eq, label)
2609-
}
2610-
fn jmp_on_different(&mut self, label: Label) -> Result<(), CompileError> {
2611-
self.assembler.emit_bcond_label_far(Condition::Ne, label)
2612-
}
2613-
fn jmp_on_above(&mut self, label: Label) -> Result<(), CompileError> {
2614-
self.assembler.emit_bcond_label_far(Condition::Hi, label)
2615-
}
2616-
fn jmp_on_aboveequal(&mut self, label: Label) -> Result<(), CompileError> {
2617-
self.assembler.emit_bcond_label_far(Condition::Cs, label)
2618-
}
2619-
fn jmp_on_belowequal(&mut self, label: Label) -> Result<(), CompileError> {
2620-
self.assembler.emit_bcond_label_far(Condition::Ls, label)
2621-
}
2622-
fn jmp_on_overflow(&mut self, label: Label) -> Result<(), CompileError> {
2623-
self.assembler.emit_bcond_label_far(Condition::Cs, label)
2607+
2608+
fn jmp_on_condition(
2609+
&mut self,
2610+
cond: UnsignedCondition,
2611+
size: Size,
2612+
source: AbstractLocation<Self::GPR, Self::SIMD>,
2613+
dest: AbstractLocation<Self::GPR, Self::SIMD>,
2614+
label: Label,
2615+
) -> Result<(), CompileError> {
2616+
self.emit_relaxed_binop(Assembler::emit_cmp, size, source, dest, false)?;
2617+
let cond = match cond {
2618+
UnsignedCondition::Equal => Condition::Eq,
2619+
UnsignedCondition::NotEqual => Condition::Ne,
2620+
UnsignedCondition::Above => Condition::Hi,
2621+
UnsignedCondition::AboveEqual => Condition::Cs,
2622+
UnsignedCondition::Below => Condition::Cc,
2623+
UnsignedCondition::BelowEqual => Condition::Ls,
2624+
};
2625+
self.assembler.emit_bcond_label_far(cond, label)
26242626
}
26252627

26262628
// jmp table

lib/compiler-singlepass/src/machine_x64.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl MachineX86_64 {
682682
},
683683
)?;
684684

685-
self.jmp_on_different(retry)?;
685+
self.assembler.emit_jmp(Condition::NotEqual, retry)?;
686686

687687
self.assembler.emit_pop(Size::S64, Location::GPR(value))?;
688688
self.release_gpr(compare);
@@ -2751,28 +2751,30 @@ impl Machine for MachineX86_64 {
27512751
) -> Result<(), CompileError> {
27522752
self.assembler.emit_cmp(size, source, dest)
27532753
}
2754-
// (un)conditionnal jmp
2755-
// (un)conditionnal jmp
2754+
2755+
// unconditionnal jmp
27562756
fn jmp_unconditionnal(&mut self, label: Label) -> Result<(), CompileError> {
27572757
self.assembler.emit_jmp(Condition::None, label)
27582758
}
2759-
fn jmp_on_equal(&mut self, label: Label) -> Result<(), CompileError> {
2760-
self.assembler.emit_jmp(Condition::Equal, label)
2761-
}
2762-
fn jmp_on_different(&mut self, label: Label) -> Result<(), CompileError> {
2763-
self.assembler.emit_jmp(Condition::NotEqual, label)
2764-
}
2765-
fn jmp_on_above(&mut self, label: Label) -> Result<(), CompileError> {
2766-
self.assembler.emit_jmp(Condition::Above, label)
2767-
}
2768-
fn jmp_on_aboveequal(&mut self, label: Label) -> Result<(), CompileError> {
2769-
self.assembler.emit_jmp(Condition::AboveEqual, label)
2770-
}
2771-
fn jmp_on_belowequal(&mut self, label: Label) -> Result<(), CompileError> {
2772-
self.assembler.emit_jmp(Condition::BelowEqual, label)
2773-
}
2774-
fn jmp_on_overflow(&mut self, label: Label) -> Result<(), CompileError> {
2775-
self.assembler.emit_jmp(Condition::Carry, label)
2759+
2760+
fn jmp_on_condition(
2761+
&mut self,
2762+
cond: UnsignedCondition,
2763+
size: Size,
2764+
source: AbstractLocation<Self::GPR, Self::SIMD>,
2765+
dest: AbstractLocation<Self::GPR, Self::SIMD>,
2766+
label: Label,
2767+
) -> Result<(), CompileError> {
2768+
self.assembler.emit_cmp(size, source, dest)?;
2769+
let cond = match cond {
2770+
UnsignedCondition::Equal => Condition::Equal,
2771+
UnsignedCondition::NotEqual => Condition::NotEqual,
2772+
UnsignedCondition::Above => Condition::Above,
2773+
UnsignedCondition::AboveEqual => Condition::AboveEqual,
2774+
UnsignedCondition::Below => Condition::Below,
2775+
UnsignedCondition::BelowEqual => Condition::BelowEqual,
2776+
};
2777+
self.assembler.emit_jmp(cond, label)
27762778
}
27772779

27782780
// jmp table

0 commit comments

Comments
 (0)