Skip to content

Commit

Permalink
feat: irem
Browse files Browse the repository at this point in the history
  • Loading branch information
nullishamy committed Oct 24, 2023
1 parent db30a62 commit 3f47418
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
9 changes: 6 additions & 3 deletions sources/interpreter_two/src/bytecode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ pub fn decode_instruction(_vm: &VM, bytes: &mut BytesMut) -> Result<Box<dyn Inst
value: RuntimeValue::Floating((1.0_f64).into()),
}),
0x10 => b(ops::PushConst {
value: RuntimeValue::Integral(bytes.try_get_i8()?.into()),
value: RuntimeValue::Integral((bytes.try_get_i8()? as i32).into())
}),
0x11 => b(ops::PushConst {
// The intermediate value is then sign-extended to an int value.
value: RuntimeValue::Integral((bytes.try_get_i16()? as i32).into())
}),
// 0x11 => Opcode::SIPUSH(bytes.try_get_i16()?),
0x12 => b(ops::Ldc {
index: bytes.try_get_u8()?
}),
Expand Down Expand Up @@ -171,7 +174,7 @@ pub fn decode_instruction(_vm: &VM, bytes: &mut BytesMut) -> Result<Box<dyn Inst
// 0x6d => Opcode::LDIV,
// 0x6e => Opcode::FDIV,
// 0x6f => Opcode::DDIV,
// 0x70 => Opcode::IREM,
0x70 => b(ops::Irem),
// 0x71 => Opcode::LREM,
// 0x72 => Opcode::FREM,
// 0x73 => Opcode::DREM,
Expand Down
55 changes: 40 additions & 15 deletions sources/interpreter_two/src/bytecode/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ macro_rules! nop {
};
}

macro_rules! pop {
($ctx: expr) => {
$ctx
.operands
.pop()
.context("no value to pop from the operand stack")?
};
}

macro_rules! arg {
($ctx: expr, $side: expr => int) => {{
let val = pop!($ctx);

let val = val.as_integral().context(format!("{} was not an integral", $side))?;
if val.ty != IntegralType::Int {
return Err(anyhow!(format!("{} was not an int", $side)));
}

val.clone()
}}
}

nop!(Nop, Return);

#[derive(Debug)]
Expand Down Expand Up @@ -88,10 +110,9 @@ impl Instruction for Ldc {
ConstantEntry::Integer(data) => {
ctx.operands
.push(RuntimeValue::Integral((data.bytes as i32).into()));
},
}
ConstantEntry::Float(data) => {
ctx.operands
.push(RuntimeValue::Floating(data.bytes.into()));
ctx.operands.push(RuntimeValue::Floating(data.bytes.into()));
}
v => return Err(anyhow!("cannot load {:#?} with ldc", v)),
};
Expand All @@ -105,24 +126,28 @@ pub struct Isub;

impl Instruction for Isub {
fn handle(&self, _vm: &mut VM, ctx: &mut Context) -> Result<i32> {
let rhs = ctx.operands.pop().context("no rhs for isub")?;
let lhs = ctx.operands.pop().context("no lhs for isub")?;
let rhs = arg!(ctx, "rhs" => int);
let lhs = arg!(ctx, "lhs" => int);

let result: i32 = (lhs.value as i32).wrapping_sub(rhs.value as i32);
ctx.operands.push(RuntimeValue::Integral(result.into()));

let rhs = rhs.as_integral().context("rhs was not an int")?;
let lhs = lhs.as_integral().context("lhs was not an int")?;
Ok(ctx.pc)
}
}

if rhs.ty != IntegralType::Int {
return Err(anyhow!("rhs was not an int, got {:#?}", rhs.ty))
}
if lhs.ty != IntegralType::Int {
return Err(anyhow!("lhs was not an int, got {:#?}", lhs.ty))
}
#[derive(Debug)]
pub struct Irem;

impl Instruction for Irem {
fn handle(&self, _vm: &mut VM, ctx: &mut Context) -> Result<i32> {
let rhs = arg!(ctx, "rhs" => int);
let lhs = arg!(ctx, "lhs" => int);

dbg!(&lhs);
dbg!(&rhs);

let result: i32 = (lhs.value as i32).wrapping_sub(rhs.value as i32);
let result: i32 = (lhs.value as i32) % (rhs.value as i32);
ctx.operands.push(RuntimeValue::Integral(result.into()));

Ok(ctx.pc)
Expand Down
30 changes: 30 additions & 0 deletions tests/instruction/IRem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: javac %s -d %t
// RUN: kate --test --cp %t IRem | filecheck %s
class IRem {
public static native void print(int i);

public static void main(String[] args) {
int x = 3;
int y = 2;
int a = -40;
int b = 5601;

// CHECK: 1
print(x%y);

// CHECK: 0
print(x%1);

// CHECK: 0
print(27%x);

// CHECK: 1
print(28%x);

// CHECK: -40
print(a%b);

// CHECK: 1
print(b%a);
}
}

0 comments on commit 3f47418

Please sign in to comment.