Skip to content

Commit

Permalink
Merge dda69ad into 45b07d0
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr committed Jan 18, 2017
2 parents 45b07d0 + dda69ad commit ef7f223
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

- Make instrumentation more robust by defining number of arguments of an
operation explicitly.

- Add parse-time specialization of primitives. This enables very early
knowledge about the program, which might be unreliable, but should be good
enough for tooling. (See [Issue #75](https://github.com/smarr/SOMns/issues/75) and [PR #88](https://github.com/smarr/SOMns/pull/88))

## 0.1.0 - 2016-12-15

Expand Down
4 changes: 2 additions & 2 deletions src/som/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public VM(final String[] args, final boolean avoidExitForTesting) throws IOExcep

this.avoidExitForTesting = avoidExitForTesting;
options = new VMOptions(args);
objectSystem = new ObjectSystem(new SourcecodeCompiler(), structuralProbe,
options.platformFile, options.kernelFile);
objectSystem = new ObjectSystem(new SourcecodeCompiler(), structuralProbe);
objectSystem.loadKernelAndPlatform(options.platformFile, options.kernelFile);

if (options.showUsage) {
VMOptions.printUsageAndExit();
Expand Down
3 changes: 1 addition & 2 deletions src/som/interpreter/SNodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import som.interpreter.nodes.ExpressionNode;
import som.interpreter.nodes.InternalObjectArrayNode;
import som.interpreter.nodes.MessageSendNode;
import som.interpreter.nodes.MessageSendNode.AbstractMessageSendNode;
import som.interpreter.nodes.ResolvingImplicitReceiverSend;
import som.interpreter.nodes.ReturnNonLocalNode;
import som.interpreter.nodes.ReturnNonLocalNode.CatchNonLocalReturnNode;
Expand Down Expand Up @@ -75,7 +74,7 @@ public static ExpressionNode createMessageSend(final SSymbol msg,
}
}

public static AbstractMessageSendNode createMessageSend(final SSymbol msg,
public static ExpressionNode createMessageSend(final SSymbol msg,
final List<ExpressionNode> exprs, final SourceSection source) {
return MessageSendNode.createMessageSend(msg,
exprs.toArray(new ExpressionNode[0]), source);
Expand Down
19 changes: 17 additions & 2 deletions src/som/interpreter/nodes/MessageSendNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,24 @@

public final class MessageSendNode {

public static AbstractMessageSendNode createMessageSend(final SSymbol selector,
public static ExpressionNode createMessageSend(final SSymbol selector,
final ExpressionNode[] arguments, final SourceSection source) {
return new UninitializedMessageSendNode(selector, arguments, source);
Primitives prims = VM.getVM().getPrimitives();
Specializer<EagerlySpecializableNode> specializer =
prims.getParserSpecializer(selector, arguments);
if (specializer != null) {
EagerlySpecializableNode newNode = specializer.create(null, arguments, source, !specializer.noWrapper());
for (ExpressionNode exp : arguments) {
unwrapIfNecessary(exp).markAsPrimitiveArgument();
}
if (specializer.noWrapper()) {
return newNode;
} else {
return newNode.wrapInEagerWrapper(newNode, selector, arguments);
}
} else {
return new UninitializedMessageSendNode(selector, arguments, source);
}
}

public static AbstractMessageSendNode adaptSymbol(final SSymbol newSelector,
Expand Down
4 changes: 2 additions & 2 deletions src/som/interpreter/nodes/ResolvingImplicitReceiverSend.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ private PreevaluatedExpression specialize(final Object[] args) {
ExpressionNode[] msgArgNodes = argumentNodes.clone();
msgArgNodes[0] = newReceiverNodeForOuterSend;

replacedBy = MessageSendNode.createMessageSend(selector, msgArgNodes,
replacedBy = (PreevaluatedExpression) MessageSendNode.createMessageSend(selector, msgArgNodes,
getSourceSection());

replace((ExpressionNode) replacedBy);
args[0] = newReceiverNodeForOuterSend.executeEvaluated(args[0]);
} else {
replacedBy = MessageSendNode.createMessageSend(selector, argumentNodes,
replacedBy = (PreevaluatedExpression) MessageSendNode.createMessageSend(selector, argumentNodes,
getSourceSection());
replace((ExpressionNode) replacedBy);
}
Expand Down
5 changes: 4 additions & 1 deletion src/som/interpreter/nodes/specialized/AndMessageNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ protected AndOrSplzr(final Primitive prim, final NodeFactory<BinaryExpressionNod

@Override
public final boolean matches(final Object[] args, final ExpressionNode[] argNodes) {
// XXX: this is the case when doing parse-time specialization
if (args == null) { return true; }

return args[0] instanceof Boolean &&
(args[1] instanceof Boolean ||
unwrapIfNecessary(argNodes[1]) instanceof BlockNode);
Expand All @@ -56,7 +59,7 @@ public final BinaryExpressionNode create(final Object[] arguments,
if (unwrapIfNecessary(argNodes[1]) instanceof BlockNode) {
return fact.createNode(arguments[1], section, argNodes[0], argNodes[1]);
} else {
assert arguments[1] instanceof Boolean;
assert arguments == null || arguments[1] instanceof Boolean;
return boolFact.createNode(section, argNodes[0], argNodes[1]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@GenerateNodeFactory
@Primitive(selector = "to:do:", noWrapper = true, disabled = true,
specializer = ToDoSplzr.class)
specializer = ToDoSplzr.class, inParser = false)
public abstract class IntToDoMessageNode extends TernaryExpressionNode {
public static class ToDoSplzr extends Specializer<IntToDoMessageNode> {
public ToDoSplzr(final Primitive prim, final NodeFactory<IntToDoMessageNode> fact) { super(prim, fact); }
Expand Down
6 changes: 3 additions & 3 deletions src/som/primitives/BlockPrims.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public SAbstractObject doSBlock(final SBlock receiver) {

@GenerateNodeFactory
@ImportStatic(BlockPrims.class)
@Primitive(primitive = "blockValue:", selector = "value",
@Primitive(primitive = "blockValue:", selector = "value", inParser = false,
receiverType = {SBlock.class, Boolean.class})
public abstract static class ValueNonePrim extends UnaryExpressionNode {
public ValueNonePrim(final boolean eagerlyWrapped, final SourceSection source) { super(eagerlyWrapped, source); }
Expand Down Expand Up @@ -102,7 +102,7 @@ public final Object doGeneric(final VirtualFrame frame,

@GenerateNodeFactory
@ImportStatic(BlockPrims.class)
@Primitive(primitive = "blockValue:with:", selector = "value:",
@Primitive(primitive = "blockValue:with:", selector = "value:", inParser = false,
receiverType = SBlock.class)
public abstract static class ValueOnePrim extends BinaryExpressionNode {
protected ValueOnePrim(final boolean eagWrap, final SourceSection source) { super(eagWrap, source); }
Expand Down Expand Up @@ -134,7 +134,7 @@ public final Object doGeneric(final VirtualFrame frame,

@GenerateNodeFactory
@ImportStatic(BlockPrims.class)
@Primitive(primitive = "blockValue:with:with:", selector = "value:with:",
@Primitive(primitive = "blockValue:with:with:", selector = "value:with:", inParser = false,
receiverType = SBlock.class)
public abstract static class ValueTwoPrim extends TernaryExpressionNode {
public ValueTwoPrim(final boolean eagWrap, final SourceSection source) { super(eagWrap, source); }
Expand Down
5 changes: 4 additions & 1 deletion src/som/primitives/DoublePrims.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final long doDouble(final double receiver) {
}

@GenerateNodeFactory
@Primitive(primitive = "doubleAsInteger:", selector = "asInteger",
@Primitive(primitive = "doubleAsInteger:", selector = "asInteger", inParser = false,
receiverType = Double.class)
public abstract static class AsIntPrim extends UnaryBasicOperation {
public AsIntPrim(final boolean eagWrap, final SourceSection source) { super(eagWrap, source); }
Expand All @@ -63,6 +63,9 @@ public static class IsDoubleClass extends Specializer<ExpressionNode> {

@Override
public boolean matches(final Object[] args, final ExpressionNode[] argNodess) {
// XXX: this is the case when doing parse-time specialization
if (args == null) { return true; }

return args[0] == Classes.doubleClass;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/som/primitives/Primitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
/** Selector for eager replacement. */
String selector() default "";

/** Specialize already during parsing. */
boolean inParser() default true;

/**
* Expected type of receiver for eager replacement,
* if given one of the types needs to match.
Expand Down
4 changes: 2 additions & 2 deletions src/som/primitives/SizeAndLengthPrim.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@


@GenerateNodeFactory
@Primitive(primitive = "arraySize:", selector = "size", receiverType = SArray.class)
@Primitive(primitive = "stringLength:", selector = "length", receiverType = String.class)
@Primitive(primitive = "arraySize:", selector = "size", receiverType = SArray.class, inParser = false)
@Primitive(primitive = "stringLength:", selector = "length", receiverType = String.class, inParser = false)
public abstract class SizeAndLengthPrim extends UnaryBasicOperation {
private final ValueProfile storageType = ValueProfile.createClassProfile();

Expand Down
2 changes: 2 additions & 0 deletions src/som/primitives/SystemPrims.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ public static class IsSystemModule extends Specializer<ExpressionNode> {

@Override
public boolean matches(final Object[] args, final ExpressionNode[] argNodes) {
// XXX: this is the case when doing parse-time specialization
if (args == null) { return true; }
return args[0] == SystemPrims.SystemModule;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/som/primitives/actors/PromisePrims.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public static class IsActorModule extends Specializer<ExpressionNode> {

@Override
public boolean matches(final Object[] args, final ExpressionNode[] argNodes) {
// XXX: this is the case when doing parse-time specialization
if (args == null) { return true; }

return args[0] == ActorClasses.ActorModule;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/som/primitives/arrays/AtPrim.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


@GenerateNodeFactory
@Primitive(primitive = "array:at:", selector = "at:", receiverType = SArray.class)
@Primitive(primitive = "array:at:", selector = "at:", receiverType = SArray.class, inParser = false)
public abstract class AtPrim extends BinaryBasicOperation {
private final ValueProfile storageType = ValueProfile.createClassProfile();

Expand Down
2 changes: 1 addition & 1 deletion src/som/primitives/arrays/AtPutPrim.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@GenerateNodeFactory
@ImportStatic(Nil.class)
@Primitive(primitive = "array:at:put:", selector = "at:put:",
receiverType = SArray.class)
receiverType = SArray.class, inParser = false)
public abstract class AtPutPrim extends TernaryExpressionNode {
private final ValueProfile storageType = ValueProfile.createClassProfile();

Expand Down
7 changes: 5 additions & 2 deletions src/som/primitives/arrays/NewImmutableArrayNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@


@GenerateNodeFactory
@Primitive(selector = "new:withAll:",
@Primitive(selector = "new:withAll:", inParser = false,
specializer = NewImmutableArrayNode.IsValueArrayClass.class)
public abstract class NewImmutableArrayNode extends TernaryExpressionNode {
public static class IsValueArrayClass extends Specializer<NewImmutableArrayNode> {
public IsValueArrayClass(final Primitive prim, final NodeFactory<NewImmutableArrayNode> fact) { super(prim, fact); }

@Override
public boolean matches(final Object[] args, final ExpressionNode[] argNodes) {
// XXX: this is the case when doing parse-time specialization
if (args == null) { return true; }

return !VmSettings.DYNAMIC_METRICS && args[0] == Classes.valueArrayClass;
}
}
Expand All @@ -45,7 +48,7 @@ public static boolean isValueArrayClass(final SClass valueArrayClass) {
return Classes.valueArrayClass == valueArrayClass;
}

@Specialization
@Specialization(guards = "isValueArrayClass(valueArrayClass)")
public SImmutableArray create(final VirtualFrame frame,
final SClass valueArrayClass, final long size, final SBlock block) {
if (size <= 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/som/primitives/arrays/NewPrim.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@GenerateNodeFactory
@Primitive(primitive = "array:new:", selector = "new:",
@Primitive(primitive = "array:new:", selector = "new:", inParser = false,
specializer = NewPrim.IsArrayClass.class)
public abstract class NewPrim extends BinaryExpressionNode {
public static class IsArrayClass extends Specializer<NewPrim> {
Expand Down
15 changes: 9 additions & 6 deletions src/som/vm/ObjectSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public final class ObjectSystem {

private final Map<URI, MixinDefinition> loadedModules;

private final MixinDefinition platformModule;
private final MixinDefinition kernelModule;
@CompilationFinal private MixinDefinition platformModule;
@CompilationFinal private MixinDefinition kernelModule;

@CompilationFinal
private SClass platformClass; // is only set after completion of initialize()
Expand All @@ -66,13 +66,16 @@ public final class ObjectSystem {
private CompletableFuture<Object> mainThreadCompleted;

public ObjectSystem(final SourcecodeCompiler compiler,
final StructuralProbe probe, final String platformFilename,
final String kernelFilename) throws IOException {
final StructuralProbe probe) {
last = this;
this.primitives = new Primitives();
this.compiler = compiler;
this.compiler = compiler;
structuralProbe = probe;
loadedModules = new LinkedHashMap<>();
loadedModules = new LinkedHashMap<>();
}

public void loadKernelAndPlatform(final String platformFilename,
final String kernelFilename) throws IOException {
platformModule = loadModule(platformFilename);
kernelModule = loadModule(kernelFilename);
}
Expand Down
17 changes: 16 additions & 1 deletion src/som/vm/Primitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public Primitives() {
initialize();
}

@SuppressWarnings("unchecked")
public Specializer<EagerlySpecializableNode> getParserSpecializer(final SSymbol selector,
final ExpressionNode[] argNodes) {
Specializer<? extends ExpressionNode> specializer = eagerPrimitives.get(selector);
if (specializer != null && specializer.inParser() && specializer.matches(null, argNodes)) {
return (Specializer<EagerlySpecializableNode>) specializer;
}
return null;
}

@SuppressWarnings("unchecked")
public Specializer<EagerlySpecializableNode> getEagerSpecializer(final SSymbol selector,
final Object[] arguments, final ExpressionNode[] argumentNodes) {
Expand Down Expand Up @@ -136,6 +146,10 @@ public Specializer(final som.primitives.Primitive prim, final NodeFactory<T> fac
}
}

public boolean inParser() {
return prim.inParser() && !prim.requiresArguments();
}

public boolean noWrapper() {
return prim.noWrapper();
}
Expand All @@ -145,7 +159,7 @@ public boolean matches(final Object[] args, final ExpressionNode[] argNodes) {
return false;
}

if (prim.receiverType().length == 0) {
if (args == null || prim.receiverType().length == 0) {
// no constraints, so, it matches
return true;
}
Expand All @@ -172,6 +186,7 @@ public T create(final Object[] arguments,
int offset = 2;

if (prim.requiresArguments()) {
assert arguments != null;
ctorArgs[offset] = arguments;
offset += 1;
}
Expand Down
Binary file modified tools/tests/dym/expected-results.tar.bz2
Binary file not shown.

0 comments on commit ef7f223

Please sign in to comment.