From 6f4e1ae90880d94d23219a0a97774b2df4e04b56 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 21 Jul 2023 12:25:13 +0100 Subject: Add direct call instruction --- .../compiler/patio/ast/PatioSerializer.java | 21 +++++++++++++++++++++ .../compiler/patio/ast/instructions/PatioCall.java | 20 ++++++++++++++++++++ .../patio/ast/instructions/PatioInstruction.java | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCall.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java index 09768a6e..25552eb2 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -6,6 +6,7 @@ import org.shedlang.compiler.patio.ast.types.*; import org.shedlang.compiler.treetext.Tree; import java.util.List; +import java.util.Optional; import static org.shedlang.compiler.treetext.TreeSerializer.serializeTree; import static org.shedlang.compiler.treetext.Trees.*; @@ -117,6 +118,18 @@ public class PatioSerializer { dataLayoutToTree(alloc.layout()) ); + case PatioCall call -> + treeList( + treeIdentifier("call"), + optionalOperandToTree(call.target()), + treeString(call.functionName()), + treeList( + call.args().stream() + .map(arg -> operandToTree(arg)) + .toList() + ) + ); + case PatioCallPointer callPointer -> treeList( treeIdentifier("call-pointer"), @@ -204,6 +217,14 @@ public class PatioSerializer { return treeList(treeString(local.name()), typeToTree(local.type())); } + private static Tree optionalOperandToTree(Optional target) { + if (target.isEmpty()) { + return treeIdentifier("_"); + } else { + return operandToTree(target.get()); + } + } + private static Tree operandToTree(PatioOperand operand) { return switch (operand) { case PatioBool bool -> diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCall.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCall.java new file mode 100644 index 00000000..c74613bb --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCall.java @@ -0,0 +1,20 @@ +package org.shedlang.compiler.patio.ast.instructions; + +import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; +import org.shedlang.compiler.patio.ast.operands.PatioOperand; +import org.shedlang.compiler.sources.Source; + +import java.util.List; +import java.util.Optional; + +/** + * Call the function with the given name, and store the returned value in the + * target local. + */ +public record PatioCall( + Optional target, + String functionName, + List args, + Source source +) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java index eb9e5ea5..9edeb532 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java @@ -2,6 +2,6 @@ package org.shedlang.compiler.patio.ast.instructions; import org.shedlang.compiler.sources.Source; -public sealed interface PatioInstruction permits PatioAlloc, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntToDataPtr, PatioLocalStore, PatioMemoryLoad, PatioMemoryStore, PatioReturn { +public sealed interface PatioInstruction permits PatioAlloc, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntToDataPtr, PatioLocalStore, PatioMemoryLoad, PatioMemoryStore, PatioReturn { Source source(); } -- cgit v1.2.3