-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,94 @@ | ||
package org.apidesign.demo.talk2compiler; | ||
|
||
import com.oracle.truffle.api.CallTarget; | ||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | ||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.Truffle; | ||
import com.oracle.truffle.api.dsl.Fallback; | ||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.NodeChildren; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.nodes.RootNode; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.profiles.BranchProfile; | ||
|
||
public class Main extends RootNode { | ||
static final Main MAIN = new Main(); | ||
static final Main MAIN; | ||
|
||
static Plus newPlus(Compute l, Compute r) { | ||
return MainFactory.PlusNodeGen.create(l, r); | ||
} | ||
|
||
static { | ||
Compute p = newPlus(new Index(0), | ||
newPlus(new Index(2), new Index(1)) | ||
); | ||
MAIN = new Main(p); | ||
} | ||
static final CallTarget CODE = Truffle.getRuntime().createCallTarget(MAIN); | ||
|
||
private Main() { | ||
@Child private Compute program; | ||
|
||
private Main(Compute program) { | ||
super(null); | ||
this.program = program; | ||
} | ||
|
||
public static void main(String... args) { | ||
System.err.println(CODE.call((Object) new Number[] { 5, Math.PI, 11, 15 })); | ||
} | ||
|
||
@Override | ||
public Object execute(VirtualFrame frame) { | ||
final String name = (String) frame.getArguments()[0]; | ||
return formatGreeting("Hello from %s!", name); | ||
public Object execute(VirtualFrame frame) { | ||
return program.executeEval(frame); | ||
} | ||
|
||
@TruffleBoundary | ||
private static String formatGreeting(String msg, String name) { | ||
return String.format(msg, name); | ||
public static abstract class Compute extends Node { | ||
public abstract Object executeEval(VirtualFrame frame); | ||
} | ||
|
||
public static void main(String... args) { | ||
String who = args.length > 0 ? args[0] : "unknown"; | ||
int cnt; | ||
if (Boolean.getBoolean("noigv")) { | ||
cnt = 1; | ||
} else { | ||
cnt = args.length > 1 ? Integer.parseInt(args[1]) : 10000000; | ||
@NodeChildren({ | ||
@NodeChild(value = "left"), | ||
@NodeChild(value = "right")}) | ||
public static abstract class Plus extends Compute { | ||
@Specialization | ||
Number doII(int left, int right) { | ||
return left + right; | ||
} | ||
|
||
@Specialization | ||
Number doDI(double left, int right) { | ||
return left + right; | ||
} | ||
int print = 1; | ||
for (int i = 1; i <= cnt; i++) { | ||
final Object result = CODE.call(who); | ||
if (i >= print) { | ||
System.err.println("run #" + i + " result: " + result); | ||
print *= 2; | ||
} | ||
|
||
@Specialization | ||
Number doID(int left, double right) { | ||
return left + right; | ||
} | ||
|
||
@Specialization | ||
Number doDD(double left, double right) { | ||
return left + right; | ||
} | ||
|
||
@Fallback | ||
Object doFallback(Object leftValue, Object rightValue) { | ||
CompilerDirectives.transferToInterpreter(); | ||
throw new IllegalStateException("Cannot + for " + leftValue + " and " + rightValue); | ||
} | ||
} | ||
|
||
public static final class Index extends Compute { | ||
private final int index; | ||
|
||
public Index(int index) { | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
public Number executeEval(VirtualFrame frame) { | ||
return (Number) frame.getArguments()[index]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters