Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/interp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Bytecode interpreter and the per-thread run loop.

import {
INITIALIZED, JArray, JClass, JMethod, JObject, isWide, newArrayData,
INITIALIZED, JArray, JClass, JMethod, JObject, isWide, longToFloat, newArrayData,
} from "./runtime.ts";
import {
Frame, JThread, JavaThrow, NativeFrame, PendingThrow, RUNNABLE, TERMINATED, WAITING, fromNative,
Expand Down Expand Up @@ -149,9 +149,12 @@ export function interpret(t: JThread, f: Frame, budget: number): number {
case 0x47: case 0x48: case 0x49: case 0x4a: sp -= 2; locals[code[pc] - 0x47] = stack[sp]; pc++; break;
case 0x4b: case 0x4c: case 0x4d: case 0x4e: locals[code[pc] - 0x4b] = stack[--sp]; pc++; break;
// array stores
case 0x4f: case 0x51: case 0x54: case 0x55: case 0x56:
case 0x4f: case 0x51: case 0x55: case 0x56:
v = stack[--sp]; idx = stack[--sp]; o = stack[--sp];
arr(o, idx)[idx] = v; pc++; break;
case 0x54:
v = stack[--sp]; idx = stack[--sp]; o = stack[--sp];
arr(o, idx)[idx] = o!.cls.name === "[Z" ? v & 1 : v; pc++; break;
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
case 0x50: case 0x52:
sp -= 2; v = stack[sp]; idx = stack[--sp]; o = stack[--sp];
arr(o, idx)[idx] = v; pc++; break;
Expand Down Expand Up @@ -230,7 +233,7 @@ export function interpret(t: JThread, f: Frame, budget: number): number {
case 0x86: stack[sp - 1] = Math.fround(stack[sp - 1]); pc++; break;
case 0x87: stack[sp++] = undefined; pc++; break;
case 0x88: sp--; stack[sp - 1] = Number(BigInt.asIntN(32, stack[sp - 1])); pc++; break;
case 0x89: sp--; stack[sp - 1] = Math.fround(Number(stack[sp - 1])); pc++; break;
case 0x89: sp--; stack[sp - 1] = longToFloat(stack[sp - 1]); pc++; break;
case 0x8a: stack[sp - 2] = Number(stack[sp - 2]); pc++; break;
case 0x8b: stack[sp - 1] = f2i(stack[sp - 1]); pc++; break;
case 0x8c: stack[sp - 1] = f2l(stack[sp - 1]); stack[sp++] = undefined; pc++; break;
Expand Down
12 changes: 8 additions & 4 deletions src/jit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Tag, utf8 } from "./classfile.ts";
import {
ACC_FINAL, INITIALIZED, JArray, JClass, JField, JMethod, JObject, isWide, newArrayData, parseMethodDesc,
ACC_FINAL, INITIALIZED, JArray, JClass, JField, JMethod, JObject, isWide, longToFloat, newArrayData, parseMethodDesc,
} from "./runtime.ts";
import { memberTarget } from "./natives/invoke.ts";
import { concatStrings } from "./natives/strings.ts";
Expand Down Expand Up @@ -223,6 +223,7 @@ const runtime = {
},
PUSHED,
INTERPRET,
longToFloat,
npe: npeAt,
arr(o: JArray | null, i: number, pc?: number): any {
if (o === null) throw at(new PendingThrow("java/lang/NullPointerException"), pc);
Expand Down Expand Up @@ -744,9 +745,12 @@ function insn(g: Gen, b: Body, pc: number) {
case 0x43: case 0x44: case 0x45: case 0x46: emit(`${L(op - 0x43)}=${top()};`); break;
case 0x47: case 0x48: case 0x49: case 0x4a: emit(`${L(op - 0x47)}=${top(1)};`); break;
case 0x4b: case 0x4c: case 0x4d: case 0x4e: emit(`${L(op - 0x4b)}=${top()};`); break;
case 0x4f: case 0x51: case 0x54: case 0x55: case 0x56:
case 0x4f: case 0x51: case 0x55: case 0x56:
emit(`if(${top(2)}===null)rt.npe(${pc});a=${top(2)}.a;if(${top(1)}>>>0>=a.length)rt.arr(${top(2)},${top(1)},${pc});a[${top(1)}]=${top()};`);
break;
case 0x54:
emit(`if(${top(2)}===null)rt.npe(${pc});a=${top(2)}.a;if(${top(1)}>>>0>=a.length)rt.arr(${top(2)},${top(1)},${pc});a[${top(1)}]=${top(2)}.cls.name==="[Z"?${top()}&1:${top()};`);
break;
case 0x50: case 0x52:
emit(`if(${top(3)}===null)rt.npe(${pc});a=${top(3)}.a;if(${top(2)}>>>0>=a.length)rt.arr(${top(3)},${top(2)},${pc});a[${top(2)}]=${top(1)};`);
break;
Expand Down Expand Up @@ -800,7 +804,7 @@ function insn(g: Gen, b: Body, pc: number) {
case 0x86: emit(`${top()}=Math.fround(${top()});`); break;
case 0x87: break;
case 0x88: emit(`${top(1)}=Number(BigInt.asIntN(32,${top(1)}));`); break;
case 0x89: emit(`${top(1)}=Math.fround(Number(${top(1)}));`); break;
case 0x89: emit(`${top(1)}=rt.longToFloat(${top(1)});`); break;
case 0x8a: emit(`${top(1)}=Number(${top(1)});`); break;
case 0x8b: emit(`${top()}=rt.f2i(${top()});`); break;
case 0x8c: emit(`${top()}=rt.f2l(${top()});`); break;
Expand Down Expand Up @@ -1039,7 +1043,7 @@ const BINARY: Record<number, (a: string, b: string) => string> = {
const UNARY: Record<number, (a: string) => string> = {
0x74: (a) => `((-${a})|0)`, 0x75: (a) => `BigInt.asIntN(64,-${a})`, 0x76: (a) => `(-${a})`, 0x77: (a) => `(-${a})`,
0x85: (a) => `BigInt(${a})`, 0x86: (a) => `Math.fround(${a})`, 0x87: (a) => a, 0x88: (a) => `Number(BigInt.asIntN(32,${a}))`,
0x89: (a) => `Math.fround(Number(${a}))`, 0x8a: (a) => `Number(${a})`, 0x8b: (a) => `rt.f2i(${a})`, 0x8c: (a) => `rt.f2l(${a})`,
0x89: (a) => `rt.longToFloat(${a})`, 0x8a: (a) => `Number(${a})`, 0x8b: (a) => `rt.f2i(${a})`, 0x8c: (a) => `rt.f2l(${a})`,
0x8d: (a) => a, 0x8e: (a) => `rt.f2i(${a})`, 0x8f: (a) => `rt.f2l(${a})`, 0x90: (a) => `Math.fround(${a})`,
0x91: (a) => `((${a}<<24)>>24)`, 0x92: (a) => `(${a}&65535)`, 0x93: (a) => `((${a}<<16)>>16)`,
};
Expand Down
13 changes: 11 additions & 2 deletions src/jvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,17 @@ export class JVM {
c.initThread = t;
try {
if (!c.isInterface && c.superclass && c.superclass.state !== INITIALIZED) yield* this.initGen(t, c.superclass);
for (const i of c.interfaces) {
if (i.state !== INITIALIZED && i.methods.some((m) => !m.isAbstract && !m.isStatic)) yield* this.initGen(t, i);
if (!c.isInterface && c.interfaces.length) {
const seen = new Set<JClass>();
const interfaces: JClass[] = [];
const visit = (i: JClass) => {
if (seen.has(i)) return;
seen.add(i);
for (const parent of i.interfaces) visit(parent);
if (i.methods.some((m) => !m.isAbstract && !m.isStatic)) interfaces.push(i);
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
};
for (const i of c.interfaces) visit(i);
for (const i of interfaces) if (i.state !== INITIALIZED) yield* this.initGen(t, i);
}
this.applyConstantValues(c);
const clinit = c.methodMap.get("<clinit>()V");
Expand Down
9 changes: 9 additions & 0 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ export type ArrayData =
| Int8Array | Uint16Array | Int16Array | Int32Array | Float32Array | Float64Array
| BigInt64Array | (JObject | null)[];

// Preserve discarded bits as a sticky bit before rounding a long to float.
export function longToFloat(v: bigint): number {
const negative = v < 0n;
const magnitude = negative ? -v : v;
if (magnitude < (1n << 53n)) return Math.fround(Number(v));
const rounded = (magnitude >> 11n) | ((magnitude & 2047n) === 0n ? 0n : 1n);
return Math.fround(Number(negative ? -rounded : rounded) * 2048);
}

export class JArray extends JObject {
a: ArrayData;
constructor(cls: JClass, a: ArrayData) {
Expand Down
42 changes: 42 additions & 0 deletions test/java/BooleanStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class BooleanStore {
public static class Stores {
public static int store(boolean[] values) {
values[0] = true;
return values[0] ? 1 : 0;
}
}

static class Loader extends ClassLoader {
Class<?> define(byte[] bytes) { return defineClass(null, bytes, 0, bytes.length); }
}

public static void main(String[] args) throws Exception {
try {
Stores.store(null);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} catch (NullPointerException e) {
System.out.println("null store: " + e.getClass().getName());
}
byte[] bytes;
try (var in = BooleanStore.class.getResourceAsStream("BooleanStore$Stores.class")) {
bytes = in.readAllBytes();
}
int changes = 0;
for (int i = 0; i < bytes.length - 1; i++) {
if (bytes[i] == 4 && bytes[i + 1] == 0x54) {
bytes[i] = 5;
changes++;
}
}
if (changes != 1) throw new AssertionError(changes);
var method = new Loader().define(bytes).getMethod("store", boolean[].class);
for (int i = 0; i < 1000; i++) {
int value = (int) method.invoke(null, new boolean[] { true });
if (i == 0 || i == 999) System.out.println(value);
}
try {
method.invoke(null, (Object) null);
} catch (java.lang.reflect.InvocationTargetException e) {
System.out.println("hot null store: " + e.getCause().getClass().getName());
}
}
}
44 changes: 44 additions & 0 deletions test/java/InterfaceInitialization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class InterfaceInitialization {
static String mark(String name) {
System.out.println(name);
return name;
}

interface Root {
String VALUE = mark("root");
default void root() {}
}
interface Middle extends Root {}
interface Left extends Middle {
String VALUE = mark("left");
default void left() {}
}
interface Right extends Root {
String VALUE = mark("right");
default void right() {}
}
static class Impl implements Left, Right {
static String VALUE = mark("class");
}

interface Parent {
String VALUE = mark("parent");
default void parent() {}
}
interface Child extends Parent {
String VALUE = mark("child");
}
interface PrivateOnly {
String VALUE = mark("private interface");
private void helper() {}
}
static class PrivateImpl implements PrivateOnly {
static String VALUE = mark("private class");
}

public static void main(String[] args) {
System.out.println(Child.VALUE);
System.out.println(Impl.VALUE);
System.out.println(PrivateImpl.VALUE);
}
}
15 changes: 15 additions & 0 deletions test/java/LongToFloat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class LongToFloat {
static float convert(long value) { return (float) value; }

public static void main(String[] args) {
long midpoint = (1L << 62) + (1L << 38);
long[] values = {0, 1, -1, midpoint - 1, midpoint, midpoint + 1,
-midpoint - 1, -midpoint, -midpoint + 1, Long.MIN_VALUE, Long.MAX_VALUE};
for (int run = 0; run < 1000; run++) {
for (long value : values) {
int bits = Float.floatToRawIntBits(convert(value));
if (run == 0 || run == 999) System.out.println(bits);
}
}
}
}
Loading
⌂ Proxy