Skip to content

Commit 65a00a4

Browse files
authored
Load an immediate into a register in the backend (#38)
1 parent 302abf5 commit 65a00a4

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

zjit/src/backend/x86_64/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ impl Assembler
217217
(Opnd::Mem(_) | Opnd::Reg(_), _) => {
218218
*left = asm.load(*left);
219219
},
220+
// The first operand can't be an immediate value
221+
(Opnd::Value(_), _) => {
222+
*left = asm.load(*left);
223+
}
220224
_ => {}
221225
};
222226

@@ -244,9 +248,15 @@ impl Assembler
244248
}
245249
},
246250
Insn::Test { left, right } => {
247-
if let (Opnd::Mem(_), Opnd::Mem(_)) = (&left, &right) {
248-
let loaded = asm.load(*right);
249-
*right = loaded;
251+
match (&left, &right) {
252+
(Opnd::Mem(_), Opnd::Mem(_)) => {
253+
*right = asm.load(*right);
254+
}
255+
// The first operand can't be an immediate value
256+
(Opnd::UImm(_) | Opnd::Imm(_), _) => {
257+
*left = asm.load(*left);
258+
}
259+
_ => {}
250260
}
251261
asm.push_insn(insn);
252262
},

zjit/src/codegen.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,8 @@ fn gen_fixnum_add(jit: &mut JITState, asm: &mut Assembler, left: InsnId, right:
146146
let left_opnd = jit.get_opnd(left)?;
147147
let right_opnd = jit.get_opnd(right)?;
148148

149-
// Load left into a register if left is a constant. The backend doesn't support sub(imm, imm).
150-
let left_reg = match left_opnd {
151-
Opnd::Value(_) => asm.load(left_opnd),
152-
_ => left_opnd,
153-
};
154-
155149
// Add arg0 + arg1 and test for overflow
156-
let left_untag = asm.sub(left_reg, Opnd::Imm(1));
150+
let left_untag = asm.sub(left_opnd, Opnd::Imm(1));
157151
let out_val = asm.add(left_untag, right_opnd);
158152
asm.jo(Target::SideExit(state.clone()));
159153

@@ -164,14 +158,8 @@ fn gen_fixnum_add(jit: &mut JITState, asm: &mut Assembler, left: InsnId, right:
164158
fn gen_guard_type(jit: &mut JITState, asm: &mut Assembler, val: InsnId, guard_type: Type, state: &FrameState) -> Option<lir::Opnd> {
165159
let opnd = jit.get_opnd(val)?;
166160
if guard_type.is_subtype(Fixnum) {
167-
// Load opnd into a register if opnd is a constant. The backend doesn't support test(imm, imm) yet.
168-
let opnd_reg = match opnd {
169-
Opnd::Value(_) => asm.load(opnd),
170-
_ => opnd,
171-
};
172-
173161
// Check if opnd is Fixnum
174-
asm.test(opnd_reg, Opnd::UImm(RUBY_FIXNUM_FLAG as u64));
162+
asm.test(opnd, Opnd::UImm(RUBY_FIXNUM_FLAG as u64));
175163
asm.jz(Target::SideExit(state.clone()));
176164
} else {
177165
unimplemented!("unsupported type: {guard_type}");

0 commit comments

Comments
 (0)