1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 13:55:55 +03:00

[java-truffle] tweaks for native image compilation

GraalVM native image compilation wasn't working due to
some missing @TruffleBoundary annotations, and calls
from partially evaluated code into methods that are
black-listed for runtime compilation.

With these changes, a GraalVM native image should be
producable from every Mal step.
This commit is contained in:
mmcgill 2021-06-05 21:12:11 -04:00 committed by Joel Martin
parent 593290d601
commit 20b6677551
5 changed files with 28 additions and 8 deletions

View File

@ -1393,6 +1393,7 @@ abstract class EvalBuiltin extends BuiltinNode {
protected EvalBuiltin() { super("eval"); }
@Specialization
@TruffleBoundary
protected Object eval(Object ast) {
return language.evalForm(ast).call();
}
@ -1404,11 +1405,13 @@ abstract class ThrowBuiltin extends BuiltinNode {
protected ThrowBuiltin() { super("throw"); }
@TruffleBoundary
@Specialization
protected Object throwException(String obj) {
throw new MalException(obj);
}
@TruffleBoundary
@Fallback
protected Object throwException(Object obj) {
throw new MalException(obj);

View File

@ -277,6 +277,7 @@ class LexicalScope {
final LexicalScope parent;
final int depth;
final Map<MalSymbol, EnvSlot> slots;
private int staticBindingCount;
final Map<MalSymbol, Assumption> notDynamicallyBound;
LexicalScope() {
@ -287,6 +288,7 @@ class LexicalScope {
this.parent = parent;
this.depth = parent == null? 0 : parent.depth+1;
this.slots = new HashMap<>();
this.staticBindingCount = 0;
this.notDynamicallyBound = new HashMap<>();
}
@ -309,6 +311,7 @@ class LexicalScope {
public EnvSlot allocateSlot(MalSymbol symbol) {
var slot = new EnvSlot(0, slots.size(), getNotDynamicallyBound(symbol));
slots.put(symbol, slot);
staticBindingCount++;
return slot;
}
@ -354,7 +357,7 @@ class LexicalScope {
}
public int getStaticBindingCount() {
return slots.size();
return staticBindingCount;
}
static class EnvSlot {

View File

@ -241,6 +241,15 @@ class MalVector extends MalValue implements TruffleObject, Iterable<Object>, Met
return new MalVector(vector.append(obj), this.meta);
}
@TruffleBoundary
public MalVector concat(Object[] objs) {
var v = vector.mutable();
for (int i=0; i < objs.length; ++i) {
v.append(objs[i]);
}
return new MalVector(v.immutable(), meta);
}
@TruffleBoundary
public MalVector concat(Iterable<? extends Object> objs) {
return new MalVector(vector.concat(objs), meta);

View File

@ -3,7 +3,6 @@ package truffle.mal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
@ -15,6 +14,7 @@ import org.graalvm.polyglot.Value;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.TruffleObject;
@ -137,9 +137,9 @@ public class step2_eval {
@Override
public Object executeGeneric(VirtualFrame frame) {
var elements = new ArrayList<>(elementNodes.length);
var elements = new Object[elementNodes.length];
for (int i=0; i < elementNodes.length; i++) {
elements.add(elementNodes[i].executeGeneric(frame));
elements[i] = elementNodes[i].executeGeneric(frame);
}
return MalVector.EMPTY.concat(elements);
}
@ -174,14 +174,19 @@ public class step2_eval {
this.symbol = symbol;
}
@Override
public Object executeGeneric(VirtualFrame frame) {
@TruffleBoundary
private Object lookup() {
var result = replEnv.get(symbol);
if (result == null) {
throw new MalException(symbol+" not found");
}
return result;
}
@Override
public Object executeGeneric(VirtualFrame frame) {
return lookup();
}
}
static class ApplyNode extends MalNode {

View File

@ -204,9 +204,9 @@ public class stepE_macros {
@ExplodeLoop
@Override
public Object executeGeneric(VirtualFrame frame, MalEnv env) {
var elements = new ArrayList<>(elementNodes.length);
var elements = new Object[elementNodes.length];
for (int i=0; i < elementNodes.length; i++) {
elements.add(elementNodes[i].executeGeneric(frame, env));
elements[i] = elementNodes[i].executeGeneric(frame, env);
}
return MalVector.EMPTY.concat(elements);
}