Contenu connexe Similaire à GraalVMで使われている、他言語をJVM上に実装する仕組みを学ぼう (20) Plus de Koichi Sakata (20) GraalVMで使われている、他言語をJVM上に実装する仕組みを学ぼう2. About Me
• 阪田 浩一 @jyukutyo
• 関西Javaエンジニアの会 会長
• Javaチャンピオン
• ポノス株式会社(京都)
• JVMになりたい人
7. Custom Part in GraalVM
• Graal
– JIT Compiler
• Truffle
– Language Implementation Framework
• SubstrateVM
– Runtime Library andTools
for Java AOT Compiled Code
11. Top 10ThingsTo DoWith GraalVM
1. High-performance modern Java
2. Low-footprint, fast-startup Java
3. Combine JavaScript, Java, Ruby,
and R
4. Run native languages on the
JVM
5. Tools that work across all
languages
6. Extend a JVM-based application
7. Extend a native application
8. Java code as a native library
9. Polyglot in the database
10. Create your own language
13. Top 10ThingsTo DoWith GraalVM
1. High-performance modern Java
2. Low-footprint, fast-startup Java
3. Combine JavaScript, Java, Ruby,
and R
4. Run native languages on the
JVM
5. Tools that work across all
languages
6. Extend a JVM-based application
7. Extend a native application
8. Java code as a native library
9. Polyglot in the database
10. Create your own language
16. Truffleでの言語実装
• TruffleRuby (Ruby)
– https://github.com/graalvm/truffleruby
• FastR (GNU R)
– https://github.com/graalvm/fastr
• Graal.js (JavaScript)
– ECMAScript 2018/Node.js 10.15.0
– https://github.com/graalvm/graaljs
• SimpleLanguage (学習用)
– https://github.com/graalvm/simplelanguage
52. 数値ノード
@NodeInfo(shortName = "const")
public class LongNode extends JvmMathLangNode {
private final long value;
public LongNode(long l) {
this.value = l;
}
public long executeLong(VirtualFrame frame) {
return this.value; }
@Override
public Object executeGeneric(VirtualFrame frame) {
return this.value;
}
}
53. 加算ノード
@NodeInfo(shortName = "+")
public abstract class AddNode extends BinaryNode {
@Specialization(rewriteOn = ArithmeticException.class)
@TruffleBoundary
protected long add(long left, long right) {
return Math.addExact(left, right);
}
@Specialization
@TruffleBoundary
protected BigDecimal add(BigDecimal left, BigDecimal right) {
return left.add(right);
}
64. 言語規定クラス
@TruffleLanguage.Registration(id = "jvmmathlang",
name = "JVM Math Language", version = "0.0.2")
public class JvmMathLang
extends TruffleLanguage<JvmMathLangContext> {
@Override
protected CallTarget parse(ParsingRequest r) throws Exception {
JvmMathLangRootNode main = parseSource(r.getSource());
return Truffle.getRuntime().createCallTarget(main);
}
69. メインクラス
public class JvmMathLangMain {
public static void main(String[] args) throws IOException {
Context context = Context.create("jvmmathlang");
Object answer = runCode(context, [PROGRAM_FOR_RUN]);
}
private static Object runCode(Context context, String program)
throws IOException {
Source source = Source.newBuilder("jvmmathlang", program,
"MATH").build();
Value value = context.eval(source);
return value;
}
}
85. 加算ノード
@NodeInfo(shortName = "+")
public abstract class AddNode extends BinaryNode {
@Specialization
@TruffleBoundary
protected BigDecimalTruffleObject add(Object left, Object right) {
BigDecimal l = left instanceof BigDecimalTruffleObject ?
((BigDecimalTruffleObject) left).getValue() : BigDecimal.valueOf((long)
left);
BigDecimal r = right instanceof BigDecimalTruffleObject ?
((BigDecimalTruffleObject) right).getValue() :
BigDecimal.valueOf((long) right);
return new BigDecimalTruffleObject(l.add(r));
}
}