[JAVA] Trüffel Tutorial Folien Persönliches Übersetzungsprotokoll ①

Trüffel-Tutorial-Folien (30-60p) Dies ist mein eigenes Übersetzungsprotokoll.

Wir garantieren keine Fehlübersetzungen oder Auslassungen.

Wenn es ein Problem gibt, löschen Sie es.

Highlights von SL (Eine einfache Sprache)

Schimmel

SL-Typ Wert Implementierung in Java?(Java Type in Implementation)
Number Ganzzahl mit beliebiger Genauigkeit Lang innerhalb von 64 Bit
Bei Überlauf Java.lang.BigInteger
Boolean richtig oder falsch boolean
String Unicode-Zeichenfolge java.lang.String
Function Verweis auf Funktion SLFunction
Object Schlüssel und Wert DynamicObject
Null null SLNull.SINGLETON

beste Übung

Syntax

C-ähnliche Steuerungssyntax

Operator

wörtlich

Eingebaute Funktion? (Eingebaute Funktionen)

Parsing

Abkürzung

SL Beispiel

Abkürzung

Getting Started

Abkürzung

AST-Dolmetscher

Es tut mir leid, dass ich weglasse

Trüffelknoten und Bäume

Class Node

Basisklasse des Trüffelknotens? (Basisklasse aller Trüffelbaumknoten)

NodeUtil bietet praktische Dienstprogrammmethoden.

public abstract class Node implements Cloneable {
    public final Node getParent() { ... }
    public final Iterable<Node> getChildren() { ... }
    public final <T extends Node> T replace(T newNode) { ... }
    public Node copy() { ... }
    public SourceSection getSourceSection();
} 

Wenn Syntax

public final class SLIfNode extends SLStatementNode {
    
    @Child private SLExpressionNode conditionNode;
    @Child private SLStatementNode thenPartNode;
    @Child private SLStatementNode elsePartNode;
    
    public SLIfNode(SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) {
        this.conditionNode = conditionNode;
        this.thenPartNode = thenPartNode;
        this.elsePartNode = elsePartNode;
    }
    
    public void executeVoid(VirtualFrame frame) {
        if (conditionNode.executeBoolean(frame)) {
            thenPartNode.executeVoid(frame);
        }else{
            elsePartNode.executeVoid(frame);
        }
    }
}

Kommentieren Sie die Felder der untergeordneten Knoten mit @Child und machen Sie sie nicht endgültig.

Wenn Syntax mit Profilerstellung

public final class SLIfNode extends SLStatementNode {
    
    @Child private SLExpressionNode conditionNode;
    @Child private SLStatementNode thenPartNode;
    @Child private SLStatementNode elsePartNode;
    private final ConditionProfile condition = ConditionProfile.createCountingProfile();
    
    public SLIfNode(SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) {
        this.conditionNode = conditionNode;
        this.thenPartNode = thenPartNode;
        this.elsePartNode = elsePartNode;
    }
    
    public void executeVoid(VirtualFrame frame) {
        if (condition.profile(conditionNode.executeBoolean(frame))) {
            thenPartNode.executeVoid(frame);
        }else{
            elsePartNode.executeVoid(frame);
        }
    }
}

Durch die Profilerstellung mit einem Interpreter kann der Compiler besseren Code generieren.

Block

public final class SLBlockNode extends SLStatementNode {
    
    @Children private final SLStatementNode[] bodyNodes;
    
    public SLBlockNode(SLStatementNode[] bodyNodes) {
        this.bodyNodes =  bodyNodes;
    }
    
    @ExplodeLoop public void executeVoid(VirtualFrame frame) {
        for (SLStatementNode statement : bodyNodes) {
            statement.executeVoid(frame);
        }
    }
}

Rückgabesyntax: Kontrollfluss zwischen Knoten

public final class SLReturnNode extends SLStatementNode {
    
    @Child private SLExpressionNode valueNode;
    
    ...
    
    public void executeVoid(VirtualFrame frame) {
        throw new SLReturnException(valueNode.executeGeneric(frame));
    }
}
public final class SLReturnException extends ControlFlowException {
    
    private final Object result;
    
    ...
}
public final class SLFunctionBodyNode extends SLExpressionNode {
    
    @Child private SLStatementNode bodyNode;
    
    ...
    
    public Object executeGeneric(VirtualFrame frame) {
        try {
            bodyNode.executeVoid(frame);
        }catch (SLReturnException ex){
            return ex.getResult();
        }
        return SLNull.SINGLETON;
    }
}

Steuerung zwischen Knoten mit Ausnahmen? (Ausnahmen für den Kontrollfluss zwischen Knoten)

Spult die Ausnahme alle Stapelrahmen zurück? (Ausnahme wickelt alle Interpreter-Stack-Frames der Methode ab (Schleifen, Bedingungen, Blöcke, ...))

Zusatz? (Zusatz)

@NodeChildren({@NodeChild("leftNode"), @NodeChild("rightNode")})
public abstract class SLBinaryNode extends SLExpressionNode {
}

public abstract class SLAddNode extends SLBinaryNode {
    
    @Specialization(rewriteOn = ArithmeticException.class)
    protected final long add(long left, long right) {
        return ExactMath.addExact(left, right);
    }
    
    @Specialization
    protected final BigInteger add(BigInteger left, BigInteger right) {
        return left.add(right);
    }
    
    @Specialization(guards = "isString(left, right)")
    protected final String add(Object left, Object right) {
        return left.toString() + right.toString();
    }
    
    protected final boolean isString(Object a, Object b) {
        return a instanceof String || b instanceof String;
    }
} 

Von Truffle DSL generierter Code ①

Von der Factory-Methode generierter Code

@GeneratedBy(SLAddNode.class)
public final class SLAddNodeGen extends SLAddNode {

    public static SLAddNode create(SLExpressionNode leftNode, SLExpressionNode rightNode) {
        ... 
    }
    
    ...
}

Von Truffle DSL generierter Code ②

@GeneratedBy(methodName = "add(long, long)", value = SLAddNode.class)
private static final class Add0Node_ extends BaseNode_ {
    
    @Override public long executeLong(VirtualFrame frameValue) throws UnexpectedResultException {
        long leftNodeValue_;
        try {
            leftNodeValue_ = root.leftNode_.executeLong(frameValue);
        }catch (UnexpectedResultException ex){
            Object rightNodeValue =  executeRightNode_(frameValue);
            return SLTypesGen.expectLong(getNext().execute_(frameValue,  ex.getResult(), rightNodeValue));
        }
        long rightNodeValue_;
        try {
            rightNodeValue_ = root.rightNode_.executeLong(frameValue);
        }catch (UnexpectedResultException ex){
            return SLTypesGen.expectLong(getNext().execute_(frameValue,  leftNodeValue_, ex.getResult()));
        }
        try {
            return root.add(leftNodeValue_, rightNodeValue_);
        } catch (ArithmeticException ex) {
            root.excludeAdd0_ = true;
            return SLTypesGen.expectLong(remove("threw rewrite exception", frameValue, leftNodeValue_, rightNodeValue_));
        }
    }
    
    @Override public Object execute(VirtualFrame frameValue) {
        try {
            return executeLong(frameValue);
        } catch (UnexpectedResultException ex) {
            return ex.getResult();
        }
    } //Der Originalartikel hat diese schließende Klammer nicht
}

Systemdefinition vom Typ Trüffel-DSL

@TypeSystem({long.class, BigInteger.class, boolean.class,              String.class, SLFunction.class, SLNull.class})
public abstract class SLTypes {
    @ImplicitCast
    public BigInteger castBigInteger(long value) {
        return BigInteger.valueOf(value);
    }
}
@TypeSystemReference(SLTypes.class)
public abstract class SLExpressionNode extends SLStatementNode {
    
    public abstract Object executeGeneric(VirtualFrame frame);
    
    public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
        return SLTypesGen.SLTYPES.expectLong(executeGeneric(frame));
    }
    
    public boolean executeBoolean(VirtualFrame frame) ...
} 

UnexpectedResultException

Trüffel-DSL-Workflow

Abkürzung

Rahmenlayout

--Ist die Funktion dem Prolog zugeordnet? (Im Funktionsprolog zugeordnet)

Rahmenverwaltung

--Factory-Methode der TruffleRuntime-Klasse

Rahmenverwaltung

public interface Frame {
    
    FrameDescriptor getFrameDescriptor();
    Object[] getArguments();
    boolean isType(FrameSlot slot);
    
    Type getType(FrameSlot slot) throws FrameSlotTypeException;
    
    void setType(FrameSlot slot, Type value);
    
    Object getValue(FrameSlot slot);
    
    MaterializedFrame materialize();
}

Lokale Variablen

@NodeChild("valueNode")
@NodeField(name = "slot", type = FrameSlot.class)
public abstract class SLWriteLocalVariableNode extends SLExpressionNode {
    
    protected abstract FrameSlot getSlot();
    
    @Specialization(guards = "isLongOrIllegal(frame)")
    protected long writeLong(VirtualFrame frame, long value) {
        getSlot().setKind(FrameSlotKind.Long);
        frame.setLong(getSlot(), value);
        return value;
    }
    
    protected boolean isLongOrIllegal(VirtualFrame frame) {
        return getSlot().getKind() == FrameSlotKind.Long || getSlot().getKind() == FrameSlotKind.Illegal;
    }
    
    ...
     
    @Specialization(contains = {"writeLong", "  writeBoolean"})
    protected Object write(VirtualFrame frame, Object value) {
        getSlot().setKind(FrameSlotKind.Object);
        frame.setObject(getSlot(), value);
        return value;
    }
}
@NodeField(name = "slot", type = FrameSlot.class)
public abstract class SLReadLocalVariableNode extends SLExpressionNode {
    
    protected abstract FrameSlot getSlot();
    
    @Specialization(guards = "isLong(frame)")
    protected long readLong(VirtualFrame frame) {
        return FrameUtil.getLongSafe(frame, getSlot());
    }
    
    protected boolean isLong(VirtualFrame frame) {
        return getSlot().getKind() == FrameSlotKind.Long;
    }
    
    ...
        
    @Specialization(contains = {"readLong", "readBoolean"})
    protected Object readObject(VirtualFrame frame) {
        if (!frame.isObject(getSlot())) {
            CompilerDirectives.transferToInterpreter();
            Object result = frame.getValue(getSlot());
            frame.setObject(getSlot(), result);
            return result;
        }
        return FrameUtil.getObjectSafe(frame, getSlot());
    }
} //Der Originaltext enthält keine schließende Klammer

kompilieren? (Zusammenstellung)

Recommended Posts

Trüffel Tutorial Folien Persönliches Übersetzungsprotokoll ①
Docker-Tutorial (Memo)
[Persönliches Memo] Try-Catch-Zusammenfassung
[Übersetzung] Byte Buddy Tutorial
[Persönliche Notizen] Über das Spring Framework
Persönliches Memo Progate Ruby I (2)
Persönliches Memo Installation des Eclipse-Plug-Ins
[Persönliches Memo] Zahlen-Ratespiel
Persönliches Memo Lomboks typische Anmerkung
Persönliches Memo Progate Ruby I (1)
Zusammenfassung der Übersetzung des Apache Shiro-Tutorials
Java HashMap, entrySet [Persönliches Memo]