From 52ce8dce00fbb8879f24537f6cfb12c4d6a9a825 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Wed, 11 Aug 2021 10:02:05 +0200 Subject: Adds Wyrd generic instruction compilers. --- src/core/src/tonkadur/wyrd/v1/Base.java | 6 ++ .../v1/compiler/fate/v1/InstructionCompiler.java | 8 +- .../v1/instruction/GenericInstructionCompiler.java | 106 +++++++++++++++++++++ .../v1/instruction/generic/AllocateCompiler.java | 75 +++++++++++++++ 4 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java create mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java diff --git a/src/core/src/tonkadur/wyrd/v1/Base.java b/src/core/src/tonkadur/wyrd/v1/Base.java index 739cf3a..c111ef2 100644 --- a/src/core/src/tonkadur/wyrd/v1/Base.java +++ b/src/core/src/tonkadur/wyrd/v1/Base.java @@ -3,6 +3,7 @@ package tonkadur.wyrd.v1; import tonkadur.TonkadurPlugin; import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.instruction.GenericInstructionCompiler; public class Base { @@ -12,5 +13,10 @@ public class Base ( GenericComputationCompiler.class ); + + TonkadurPlugin.register_as_loadable_superclass + ( + GenericInstructionCompiler.class + ); } } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java index 97b212a..80f0e29 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java @@ -72,6 +72,8 @@ import tonkadur.wyrd.v1.compiler.util.Sort; import tonkadur.wyrd.v1.compiler.util.SubList; import tonkadur.wyrd.v1.compiler.util.While; +import tonkadur.wyrd.v1.compiler.fate.v1.instruction.GenericInstructionCompiler; + public class InstructionCompiler implements tonkadur.fate.v1.lang.meta.InstructionVisitor { @@ -3942,6 +3944,10 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) throws Throwable { - // TODO: implement. + final InstructionCompiler ic; + + ic = GenericInstructionCompiler.handle(compiler, n); + + this.result.addAll(ic.result); } } diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java new file mode 100644 index 0000000..936a83a --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java @@ -0,0 +1,106 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.instruction; + +import java.util.Map; +import java.util.HashMap; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.InstructionCompiler; + +public abstract class GenericInstructionCompiler extends InstructionCompiler +{ + protected static final Map COMPILERS; + + static + { + COMPILERS = new HashMap(); + } + + public static void register (final Class c) + { + try + { + COMPILERS.put + ( + (Class) c.getDeclaredMethod("get_target_class").invoke + ( + /* object = */ null + ), + c + ); + } + catch (final Throwable t) + { + System.err.println + ( + "[F] Could not get target class for Wyrd Generic Instruction" + + " Compiler '" + + c.getName() + + "':" + ); + + t.printStackTrace(); + } + } + + public static InstructionCompiler handle + ( + final Compiler compiler, + final tonkadur.fate.v1.lang.instruction.GenericInstruction instruction + ) + throws Throwable + { + final Class registered_class; + final GenericInstructionCompiler gic; + + registered_class = COMPILERS.get(instruction.getClass()); + + if (registered_class == null) + { + System.err.println + ( + "[F] No Wyrd compilation process registered for generic Fate" + + " instruction \"" + + instruction.getClass().getName() + + "\"." + ); + + System.err.println("Registered compilers:"); + + for (final Class c: COMPILERS.keySet()) + { + System.err.println + ( + "- " + + COMPILERS.get(c).getName() + + " for " + + c.getName() + + "." + ); + } + + System.exit(-1); + } + + gic = + (GenericInstructionCompiler) + registered_class.getDeclaredConstructor(Compiler.class).newInstance + ( + compiler + ); + + gic.compile(instruction); + + return gic; + } + + protected abstract void compile + ( + final tonkadur.fate.v1.lang.instruction.GenericInstruction instruction + ) + throws Throwable; + + protected GenericInstructionCompiler (final Compiler compiler) + { + super(compiler); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java new file mode 100644 index 0000000..dbf4a98 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java @@ -0,0 +1,75 @@ +package tonkadur.wyrd.v1.compiler.fate.v1.instruction.generic; + +import tonkadur.fate.v1.lang.instruction.generic.Allocate; + +import tonkadur.wyrd.v1.lang.computation.Address; +import tonkadur.wyrd.v1.lang.computation.New; + +import tonkadur.wyrd.v1.lang.instruction.SetValue; + +import tonkadur.wyrd.v1.compiler.fate.v1.Compiler; +import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler; +import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler; + +import tonkadur.wyrd.v1.compiler.fate.v1.instruction.GenericInstructionCompiler; + + +public class AllocateCompiler extends GenericInstructionCompiler +{ + public static Class get_target_class () + { + return Allocate.class; + } + + public AllocateCompiler (final Compiler compiler) + { + super(compiler); + } + + public void compile + ( + final tonkadur.fate.v1.lang.instruction.GenericInstruction instruction + ) + throws Throwable + { + final Allocate source; + final ComputationCompiler cc; + final Address target; + + source = (Allocate) instruction; + + cc = new ComputationCompiler(compiler); + + source.get_target().get_visited_by(cc); + + if (cc.has_init()) + { + result.add(cc.get_init()); + } + + target = cc.get_address(); + + if (target == null) + { + System.err.println + ( + "[P] Argument in (allocate " + + source.get_target() + + ") did not compile to a address." + ); + + System.exit(-1); + } + + result.add + ( + new SetValue + ( + target, + new New(TypeCompiler.compile(compiler, source.get_allocated_type())) + ) + ); + + cc.release_registers(result); + } +} -- cgit v1.2.3-70-g09d2