diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-03-22 19:57:29 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-03-25 18:20:57 +0000 |
| commit | d51fb33b030447682d47087be59cd8925516ec24 (patch) | |
| tree | e3752025e08b2d5d5816ba18d5a8117113507c1b /src/main/java | |
| parent | 2fad94a05e879072af65ed1a995939d3249c2443 (diff) | |
Serialize Patio instructions to flatbuffers
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java | 1563 |
1 files changed, 1547 insertions, 16 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java b/src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java index d12465ce..acbace10 100644 --- a/src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java +++ b/src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java @@ -3,15 +3,1537 @@ package org.shedlang.compiler.patio.serialization; import com.google.flatbuffers.FlatBufferBuilder; import com.google.flatbuffers.IntVector; import org.shedlang.compiler.patio.flatbuffers.*; +import org.shedlang.compiler.patio.instructions.*; import org.shedlang.compiler.patio.operands.*; import org.shedlang.compiler.patio.types.*; import org.shedlang.compiler.patio.values.*; +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.sources.serialization.SourceToFlatbuffers; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.function.Function; public class PatioToFlatbuffers { + // == Instructions == + + public static int serializeInstruction( + FlatBufferBuilder builder, + PatioInstruction instruction, + Source parentSource + ) { + return switch (instruction) { + case PatioBlock block -> { + var blockFbs = serializeBlock(builder, block, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioBlockFbs); + PatioInstructionFbs.addValue(builder, blockFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioBoolToInt boolToInt -> { + var boolToIntFbs = serializeBoolToInt(builder, boolToInt, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioBoolToIntFbs); + PatioInstructionFbs.addValue(builder, boolToIntFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioCall call -> { + var callFbs = serializeCall(builder, call, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioCallFbs); + PatioInstructionFbs.addValue(builder, callFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioCallPointer callPointer -> { + var callPointerFbs = serializeCallPointer(builder, callPointer, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioCallPointerFbs); + PatioInstructionFbs.addValue(builder, callPointerFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioDataPtrToInt dataPtrToInt -> { + var dataPtrToIntFbs = serializeDataPtrToInt(builder, dataPtrToInt, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioDataPtrToIntFbs); + PatioInstructionFbs.addValue(builder, dataPtrToIntFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioIf if_ -> { + var ifFbs = serializeIf(builder, if_, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioIfFbs); + PatioInstructionFbs.addValue(builder, ifFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioIntAdd intAdd -> { + var intAddFbs = serializeIntAdd(builder, intAdd, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioIntAddFbs); + PatioInstructionFbs.addValue(builder, intAddFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioIntEquals intEquals -> { + var intEqualsFbs = serializeIntEquals(builder, intEquals, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioIntEqualsFbs); + PatioInstructionFbs.addValue(builder, intEqualsFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioIntNotEqual intNotEqual -> { + var intNotEqualFbs = serializeIntNotEqual(builder, intNotEqual, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioIntNotEqualFbs); + PatioInstructionFbs.addValue(builder, intNotEqualFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioIntSub intSub -> { + var intSubFbs = serializeIntSub(builder, intSub, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioIntSubFbs); + PatioInstructionFbs.addValue(builder, intSubFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioIntToBool intToBool -> { + var intToBoolFbs = serializeIntToBool(builder, intToBool, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioIntToBoolFbs); + PatioInstructionFbs.addValue(builder, intToBoolFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioIntToDataPtr intToDataPtr -> { + var intToDataPtrFbs = serializeIntToDataPtr(builder, intToDataPtr, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioIntToDataPtrFbs); + PatioInstructionFbs.addValue(builder, intToDataPtrFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioLocalStore localStore -> { + var localStoreFbs = serializeLocalStore(builder, localStore, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioLocalStoreFbs); + PatioInstructionFbs.addValue(builder, localStoreFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioLoop loop -> { + var loopFbs = serializeLoop(builder, loop, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioLoopFbs); + PatioInstructionFbs.addValue(builder, loopFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioLoopBreak loopBreak -> { + var loopBreakFbs = serializeLoopBreak(builder, loopBreak, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioLoopBreakFbs); + PatioInstructionFbs.addValue(builder, loopBreakFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioLoopContinue loopContinue -> { + var loopContinueFbs = serializeLoopContinue(builder, loopContinue, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioLoopContinueFbs); + PatioInstructionFbs.addValue(builder, loopContinueFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioMemoryLoad memoryLoad -> { + var memoryLoadFbs = serializeMemoryLoad(builder, memoryLoad, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioMemoryLoadFbs); + PatioInstructionFbs.addValue(builder, memoryLoadFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioMemoryStore memoryStore -> { + var memoryStoreFbs = serializeMemoryStore(builder, memoryStore, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioMemoryStoreFbs); + PatioInstructionFbs.addValue(builder, memoryStoreFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioResume resume -> { + var resumeFbs = serializeResume(builder, resume, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioResumeFbs); + PatioInstructionFbs.addValue(builder, resumeFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioReturn return_ -> { + var returnFbs = serializeReturn(builder, return_, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioReturnFbs); + PatioInstructionFbs.addValue(builder, returnFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioReturnVoid returnVoid -> { + var returnVoidFbs = serializeReturnVoid(builder, returnVoid, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioReturnVoidFbs); + PatioInstructionFbs.addValue(builder, returnVoidFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioSwitch switch_ -> { + var switchFbs = serializeSwitch(builder, switch_, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioSwitchFbs); + PatioInstructionFbs.addValue(builder, switchFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioThrow throw_ -> { + var throwFbs = serializeThrow(builder, throw_, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioThrowFbs); + PatioInstructionFbs.addValue(builder, throwFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioTry try_ -> { + var tryFbs = serializeTry(builder, try_, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioTryFbs); + PatioInstructionFbs.addValue(builder, tryFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioUnreachable unreachable -> { + var unreachableFbs = serializeUnreachable(builder, unreachable, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioUnreachableFbs); + PatioInstructionFbs.addValue(builder, unreachableFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioVarLoad varLoad -> { + var varLoadFbs = serializeVarLoad(builder, varLoad, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioVarLoadFbs); + PatioInstructionFbs.addValue(builder, varLoadFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + + case PatioVarStore varStore -> { + var varStoreFbs = serializeVarStore(builder, varStore, parentSource); + + PatioInstructionFbs.startPatioInstructionFbs(builder); + PatioInstructionFbs.addValueType(builder, PatioInstructionValueFbs.PatioVarStoreFbs); + PatioInstructionFbs.addValue(builder, varStoreFbs); + yield PatioInstructionFbs.endPatioInstructionFbs(builder); + } + }; + } + + public static PatioInstruction deserializeInstruction( + PatioInstructionFbs instructionFbs, + Source parentSource + ) { + return switch (instructionFbs.valueType()) { + case PatioInstructionValueFbs.PatioBlockFbs -> + deserializeBlock( + (PatioBlockFbs) instructionFbs.value(new PatioBlockFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioBoolToIntFbs -> + deserializeBoolToInt( + (PatioBoolToIntFbs) instructionFbs.value(new PatioBoolToIntFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioCallFbs -> + deserializeCall( + (PatioCallFbs) instructionFbs.value(new PatioCallFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioCallPointerFbs -> + deserializeCallPointer( + (PatioCallPointerFbs) instructionFbs.value(new PatioCallPointerFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioDataPtrToIntFbs -> + deserializeDataPtrToInt( + (PatioDataPtrToIntFbs) instructionFbs.value(new PatioDataPtrToIntFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioIfFbs -> + deserializeIf( + (PatioIfFbs) instructionFbs.value(new PatioIfFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioIntAddFbs -> + deserializeIntAdd( + (PatioIntAddFbs) instructionFbs.value(new PatioIntAddFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioIntEqualsFbs -> + deserializeIntEquals( + (PatioIntEqualsFbs) instructionFbs.value(new PatioIntEqualsFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioIntNotEqualFbs -> + deserializeIntNotEqual( + (PatioIntNotEqualFbs) instructionFbs.value(new PatioIntNotEqualFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioIntSubFbs -> + deserializeIntSub( + (PatioIntSubFbs) instructionFbs.value(new PatioIntSubFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioIntToBoolFbs -> + deserializeIntToBool( + (PatioIntToBoolFbs) instructionFbs.value(new PatioIntToBoolFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioIntToDataPtrFbs -> + deserializeIntToDataPtr( + (PatioIntToDataPtrFbs) instructionFbs.value(new PatioIntToDataPtrFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioLocalStoreFbs -> + deserializeLocalStore( + (PatioLocalStoreFbs) instructionFbs.value(new PatioLocalStoreFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioLoopFbs -> + deserializeLoop( + (PatioLoopFbs) instructionFbs.value(new PatioLoopFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioLoopBreakFbs -> + deserializeLoopBreak( + (PatioLoopBreakFbs) instructionFbs.value(new PatioLoopBreakFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioLoopContinueFbs -> + deserializeLoopContinue( + (PatioLoopContinueFbs) instructionFbs.value(new PatioLoopContinueFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioMemoryLoadFbs -> + deserializeMemoryLoad( + (PatioMemoryLoadFbs) instructionFbs.value(new PatioMemoryLoadFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioMemoryStoreFbs -> + deserializeMemoryStore( + (PatioMemoryStoreFbs) instructionFbs.value(new PatioMemoryStoreFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioResumeFbs -> + deserializeResume( + (PatioResumeFbs) instructionFbs.value(new PatioResumeFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioReturnFbs -> + deserializeReturn( + (PatioReturnFbs) instructionFbs.value(new PatioReturnFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioReturnVoidFbs -> + deserializeReturnVoid( + (PatioReturnVoidFbs) instructionFbs.value(new PatioReturnVoidFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioSwitchFbs -> + deserializeSwitch( + (PatioSwitchFbs) instructionFbs.value(new PatioSwitchFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioThrowFbs -> + deserializeThrow( + (PatioThrowFbs) instructionFbs.value(new PatioThrowFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioTryFbs -> + deserializeTry( + (PatioTryFbs) instructionFbs.value(new PatioTryFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioUnreachableFbs -> + deserializeUnreachable( + (PatioUnreachableFbs) instructionFbs.value(new PatioUnreachableFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioVarLoadFbs -> + deserializeVarLoad( + (PatioVarLoadFbs) instructionFbs.value(new PatioVarLoadFbs()), + parentSource + ); + + case PatioInstructionValueFbs.PatioVarStoreFbs -> + deserializeVarStore( + (PatioVarStoreFbs) instructionFbs.value(new PatioVarStoreFbs()), + parentSource + ); + + default -> + throw new UnsupportedOperationException("TODO"); + }; + } + + public static int serializeBlock( + FlatBufferBuilder builder, + PatioBlock instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var bodyOffsetsFbs = instruction.body().stream() + .mapToInt(bodyInstruction -> serializeInstruction(builder, bodyInstruction, instruction.source())) + .toArray(); + var bodyVectorFbs = PatioBlockFbs.createBodyVector(builder, bodyOffsetsFbs); + + var resultsOffsetsFbs = instruction.results().stream() + .mapToInt(result -> serializeOperand(builder, result)) + .toArray(); + var resultsVectorFbs = PatioBlockFbs.createResultsVector(builder, resultsOffsetsFbs); + + PatioBlockFbs.startPatioBlockFbs(builder); + PatioBlockFbs.addBody(builder, bodyVectorFbs); + PatioBlockFbs.addResults(builder, resultsVectorFbs); + PatioBlockFbs.addSource(builder, sourceFbs); + return PatioBlockFbs.endPatioBlockFbs(builder); + } + + private static PatioBlock deserializeBlock(PatioBlockFbs instructionFbs, Source parentSource) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var body = deserializeVector( + instructionFbs::body, + instructionFbs.bodyLength(), + bodyInstructionFbs -> deserializeInstruction(bodyInstructionFbs, source) + ); + + var results = deserializeVector( + instructionFbs::results, + instructionFbs.resultsLength(), + resultFbs -> deserializeOperand(resultFbs) + ); + + return new PatioBlock( + body, + results, + source + ); + } + + public static int serializeBoolToInt( + FlatBufferBuilder builder, + PatioBoolToInt instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioBoolToIntFbs.startPatioBoolToIntFbs(builder); + PatioBoolToIntFbs.addSource(builder, sourceFbs); + PatioBoolToIntFbs.addDestination(builder, destinationFbs); + PatioBoolToIntFbs.addValue(builder, valueFbs); + return PatioBoolToIntFbs.endPatioBoolToIntFbs(builder); + } + + private static PatioBoolToInt deserializeBoolToInt( + PatioBoolToIntFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var value = deserializeOperand(instructionFbs.value()); + + return new PatioBoolToInt(destination, value, source); + } + + public static int serializeCall( + FlatBufferBuilder builder, + PatioCall instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var destinationFbs = instruction.destination() + .map(destination -> serializeLocalRef(builder, destination)); + + var functionNameFbs = builder.createString(instruction.functionName()); + + var argsOffsetsFbs = instruction.args().stream() + .mapToInt(arg -> serializeOperand(builder, arg)) + .toArray(); + var argsVectorFbs = PatioCallFbs.createArgsVector(builder, argsOffsetsFbs); + + PatioCallFbs.startPatioCallFbs(builder); + PatioCallFbs.addSource(builder, sourceFbs); + if (destinationFbs.isPresent()) { + PatioCallFbs.addDestination(builder, destinationFbs.get()); + } + PatioCallFbs.addFunctionName(builder, functionNameFbs); + PatioCallFbs.addArgs(builder, argsVectorFbs); + return PatioCallFbs.endPatioCallFbs(builder); + } + + private static PatioCall deserializeCall( + PatioCallFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = Optional.ofNullable(instructionFbs.destination()) + .map(destinationFbs -> deserializeLocalRef(destinationFbs)); + + var functionName = instructionFbs.functionName(); + + var args = deserializeVector( + instructionFbs::args, + instructionFbs.argsLength(), + argFbs -> deserializeOperand(argFbs) + ); + + return new PatioCall(destination, functionName, args, source); + } + + public static int serializeCallPointer( + FlatBufferBuilder builder, + PatioCallPointer instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + + var functionPointerFbs = serializeOperand(builder, instruction.functionPointer()); + + var argsOffsetsFbs = instruction.args().stream() + .mapToInt(arg -> serializeOperand(builder, arg)) + .toArray(); + var argsVectorFbs = PatioCallPointerFbs.createArgsVector(builder, argsOffsetsFbs); + + PatioCallPointerFbs.startPatioCallPointerFbs(builder); + PatioCallPointerFbs.addSource(builder, sourceFbs); + PatioCallPointerFbs.addDestination(builder, destinationFbs); + PatioCallPointerFbs.addFunctionPointer(builder, functionPointerFbs); + PatioCallPointerFbs.addArgs(builder, argsVectorFbs); + return PatioCallPointerFbs.endPatioCallPointerFbs(builder); + } + + private static PatioCallPointer deserializeCallPointer( + PatioCallPointerFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + + var functionPointer = deserializeOperand(instructionFbs.functionPointer()); + + var args = deserializeVector( + instructionFbs::args, + instructionFbs.argsLength(), + argFbs -> deserializeOperand(argFbs) + ); + + return new PatioCallPointer(destination, functionPointer, args, source); + } + + public static int serializeDataPtrToInt( + FlatBufferBuilder builder, + PatioDataPtrToInt instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioDataPtrToIntFbs.startPatioDataPtrToIntFbs(builder); + PatioDataPtrToIntFbs.addSource(builder, sourceFbs); + PatioDataPtrToIntFbs.addDestination(builder, destinationFbs); + PatioDataPtrToIntFbs.addValue(builder, valueFbs); + return PatioDataPtrToIntFbs.endPatioDataPtrToIntFbs(builder); + } + + private static PatioDataPtrToInt deserializeDataPtrToInt( + PatioDataPtrToIntFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var value = deserializeOperand(instructionFbs.value()); + + return new PatioDataPtrToInt(destination, value, source); + } + + public static int serializeIf( + FlatBufferBuilder builder, + PatioIf instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var destinationsOffsetsFbs = instruction.destinations().stream() + .mapToInt(destination -> serializeLocalRef(builder, destination)) + .toArray(); + var destinationsVectorFbs = PatioIfFbs.createDestinationsVector(builder, destinationsOffsetsFbs); + + var conditionFbs = serializeOperand(builder, instruction.condition()); + + var trueBody = serializeBlock(builder, instruction.trueBody(), instruction.source()); + var falseBody = serializeBlock(builder, instruction.falseBody(), instruction.source()); + + PatioIfFbs.startPatioIfFbs(builder); + PatioIfFbs.addSource(builder, sourceFbs); + PatioIfFbs.addDestinations(builder, destinationsVectorFbs); + PatioIfFbs.addCondition(builder, conditionFbs); + PatioIfFbs.addTrueBody(builder, trueBody); + PatioIfFbs.addFalseBody(builder, falseBody); + return PatioIfFbs.endPatioIfFbs(builder); + } + + private static PatioIf deserializeIf( + PatioIfFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destinations = deserializeVector( + instructionFbs::destinations, + instructionFbs.destinationsLength(), + PatioToFlatbuffers::deserializeLocalRef + ); + var condition = deserializeOperand(instructionFbs.condition()); + var trueBody = deserializeBlock(instructionFbs.trueBody(), source); + var falseBody = deserializeBlock(instructionFbs.falseBody(), source); + + return new PatioIf(destinations, condition, trueBody, falseBody, source); + } + + public static int serializeIntAdd( + FlatBufferBuilder builder, + PatioIntAdd instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var leftFbs = serializeOperand(builder, instruction.left()); + var rightFbs = serializeOperand(builder, instruction.right()); + + PatioIntAddFbs.startPatioIntAddFbs(builder); + PatioIntAddFbs.addSource(builder, sourceFbs); + PatioIntAddFbs.addDestination(builder, destinationFbs); + PatioIntAddFbs.addLeft(builder, leftFbs); + PatioIntAddFbs.addRight(builder, rightFbs); + return PatioIntAddFbs.endPatioIntAddFbs(builder); + } + + private static PatioIntAdd deserializeIntAdd( + PatioIntAddFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var left = deserializeOperand(instructionFbs.left()); + var right = deserializeOperand(instructionFbs.right()); + + return new PatioIntAdd(destination, left, right, source); + } + + public static int serializeIntEquals( + FlatBufferBuilder builder, + PatioIntEquals instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var leftFbs = serializeOperand(builder, instruction.left()); + var rightFbs = serializeOperand(builder, instruction.right()); + + PatioIntEqualsFbs.startPatioIntEqualsFbs(builder); + PatioIntEqualsFbs.addSource(builder, sourceFbs); + PatioIntEqualsFbs.addDestination(builder, destinationFbs); + PatioIntEqualsFbs.addLeft(builder, leftFbs); + PatioIntEqualsFbs.addRight(builder, rightFbs); + return PatioIntEqualsFbs.endPatioIntEqualsFbs(builder); + } + + private static PatioIntEquals deserializeIntEquals( + PatioIntEqualsFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var left = deserializeOperand(instructionFbs.left()); + var right = deserializeOperand(instructionFbs.right()); + + return new PatioIntEquals(destination, left, right, source); + } + + public static int serializeIntNotEqual( + FlatBufferBuilder builder, + PatioIntNotEqual instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var leftFbs = serializeOperand(builder, instruction.left()); + var rightFbs = serializeOperand(builder, instruction.right()); + + PatioIntNotEqualFbs.startPatioIntNotEqualFbs(builder); + PatioIntNotEqualFbs.addSource(builder, sourceFbs); + PatioIntNotEqualFbs.addDestination(builder, destinationFbs); + PatioIntNotEqualFbs.addLeft(builder, leftFbs); + PatioIntNotEqualFbs.addRight(builder, rightFbs); + return PatioIntNotEqualFbs.endPatioIntNotEqualFbs(builder); + } + + private static PatioIntNotEqual deserializeIntNotEqual( + PatioIntNotEqualFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var left = deserializeOperand(instructionFbs.left()); + var right = deserializeOperand(instructionFbs.right()); + + return new PatioIntNotEqual(destination, left, right, source); + } + + public static int serializeIntSub( + FlatBufferBuilder builder, + PatioIntSub instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var leftFbs = serializeOperand(builder, instruction.left()); + var rightFbs = serializeOperand(builder, instruction.right()); + + PatioIntSubFbs.startPatioIntSubFbs(builder); + PatioIntSubFbs.addSource(builder, sourceFbs); + PatioIntSubFbs.addDestination(builder, destinationFbs); + PatioIntSubFbs.addLeft(builder, leftFbs); + PatioIntSubFbs.addRight(builder, rightFbs); + return PatioIntSubFbs.endPatioIntSubFbs(builder); + } + + private static PatioIntSub deserializeIntSub( + PatioIntSubFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var left = deserializeOperand(instructionFbs.left()); + var right = deserializeOperand(instructionFbs.right()); + + return new PatioIntSub(destination, left, right, source); + } + + public static int serializeIntToBool( + FlatBufferBuilder builder, + PatioIntToBool instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioIntToBoolFbs.startPatioIntToBoolFbs(builder); + PatioIntToBoolFbs.addSource(builder, sourceFbs); + PatioIntToBoolFbs.addDestination(builder, destinationFbs); + PatioIntToBoolFbs.addValue(builder, valueFbs); + return PatioIntToBoolFbs.endPatioIntToBoolFbs(builder); + } + + private static PatioIntToBool deserializeIntToBool( + PatioIntToBoolFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var value = deserializeOperand(instructionFbs.value()); + + return new PatioIntToBool(destination, value, source); + } + + public static int serializeIntToDataPtr( + FlatBufferBuilder builder, + PatioIntToDataPtr instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioIntToDataPtrFbs.startPatioIntToDataPtrFbs(builder); + PatioIntToDataPtrFbs.addSource(builder, sourceFbs); + PatioIntToDataPtrFbs.addDestination(builder, destinationFbs); + PatioIntToDataPtrFbs.addValue(builder, valueFbs); + return PatioIntToDataPtrFbs.endPatioIntToDataPtrFbs(builder); + } + + private static PatioIntToDataPtr deserializeIntToDataPtr( + PatioIntToDataPtrFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var value = deserializeOperand(instructionFbs.value()); + + return new PatioIntToDataPtr(destination, value, source); + } + + public static int serializeLocalStore( + FlatBufferBuilder builder, + PatioLocalStore instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioLocalStoreFbs.startPatioLocalStoreFbs(builder); + PatioLocalStoreFbs.addSource(builder, sourceFbs); + PatioLocalStoreFbs.addDestination(builder, destinationFbs); + PatioLocalStoreFbs.addValue(builder, valueFbs); + return PatioLocalStoreFbs.endPatioLocalStoreFbs(builder); + } + + private static PatioLocalStore deserializeLocalStore( + PatioLocalStoreFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var value = deserializeOperand(instructionFbs.value()); + + return new PatioLocalStore(destination, value, source); + } + + public static int serializeLoopBreak( + FlatBufferBuilder builder, + PatioLoopBreak instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + PatioLoopBreakFbs.startPatioLoopBreakFbs(builder); + PatioLoopBreakFbs.addSource(builder, sourceFbs); + return PatioLoopBreakFbs.endPatioLoopBreakFbs(builder); + } + + private static PatioLoopBreak deserializeLoopBreak( + PatioLoopBreakFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + return new PatioLoopBreak(source); + } + + public static int serializeLoop( + FlatBufferBuilder builder, + PatioLoop instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var bodyOffsetsFbs = instruction.body().stream() + .mapToInt(bodyInstruction -> serializeInstruction(builder, bodyInstruction, instruction.source())) + .toArray(); + var bodyVectorFbs = PatioLoopFbs.createBodyVector(builder, bodyOffsetsFbs); + + PatioLoopFbs.startPatioLoopFbs(builder); + PatioLoopFbs.addSource(builder, sourceFbs); + PatioLoopFbs.addBody(builder, bodyVectorFbs); + return PatioLoopFbs.endPatioLoopFbs(builder); + } + + private static PatioLoop deserializeLoop( + PatioLoopFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + instructionFbs.source(), + parentSource + ); + + var body = deserializeVector( + instructionFbs::body, + instructionFbs.bodyLength(), + bodyInstructionFbs -> deserializeInstruction(bodyInstructionFbs, source) + ); + + return new PatioLoop(body, source); + } + + public static int serializeLoopContinue( + FlatBufferBuilder builder, + PatioLoopContinue instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + PatioLoopContinueFbs.startPatioLoopContinueFbs(builder); + PatioLoopContinueFbs.addSource(builder, sourceFbs); + return PatioLoopContinueFbs.endPatioLoopContinueFbs(builder); + } + + private static PatioLoopContinue deserializeLoopContinue( + PatioLoopContinueFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + return new PatioLoopContinue(source); + } + + public static int serializeMemoryLoad( + FlatBufferBuilder builder, + PatioMemoryLoad instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var pointerFbs = serializeOperand(builder, instruction.pointer()); + var layoutFbs = serializeDataLayout(builder, instruction.layout()); + var offsetFbs = PatioMemoryLoadFbs.createOffsetVector( + builder, + serializeListOfInts(instruction.offset()) + ); + + PatioMemoryLoadFbs.startPatioMemoryLoadFbs(builder); + PatioMemoryLoadFbs.addSource(builder, sourceFbs); + PatioMemoryLoadFbs.addDestination(builder, destinationFbs); + PatioMemoryLoadFbs.addPointer(builder, pointerFbs); + PatioMemoryLoadFbs.addLayout(builder, layoutFbs); + PatioMemoryLoadFbs.addOffset(builder, offsetFbs); + return PatioMemoryLoadFbs.endPatioMemoryLoadFbs(builder); + } + + private static PatioMemoryLoad deserializeMemoryLoad( + PatioMemoryLoadFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destination = deserializeLocalRef(instructionFbs.destination()); + var pointer = deserializeOperand(instructionFbs.pointer()); + var layout = deserializeDataLayout(instructionFbs.layout()); + var offset = deserializeListOfInts(instructionFbs.offsetVector()); + + return new PatioMemoryLoad(destination, pointer, layout, offset, source); + } + + public static int serializeMemoryStore( + FlatBufferBuilder builder, + PatioMemoryStore instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var pointerFbs = serializeOperand(builder, instruction.pointer()); + var layoutFbs = serializeDataLayout(builder, instruction.layout()); + var offsetFbs = PatioMemoryStoreFbs.createOffsetVector( + builder, + serializeListOfInts(instruction.offset()) + ); + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioMemoryStoreFbs.startPatioMemoryStoreFbs(builder); + PatioMemoryStoreFbs.addSource(builder, sourceFbs); + PatioMemoryStoreFbs.addPointer(builder, pointerFbs); + PatioMemoryStoreFbs.addLayout(builder, layoutFbs); + PatioMemoryStoreFbs.addOffset(builder, offsetFbs); + PatioMemoryStoreFbs.addValue(builder, valueFbs); + return PatioMemoryStoreFbs.endPatioMemoryStoreFbs(builder); + } + + private static PatioMemoryStore deserializeMemoryStore( + PatioMemoryStoreFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var pointer = deserializeOperand(instructionFbs.pointer()); + var layout = deserializeDataLayout(instructionFbs.layout()); + var offset = deserializeListOfInts(instructionFbs.offsetVector()); + var value = deserializeOperand(instructionFbs.value()); + + return new PatioMemoryStore(pointer, layout, offset, value, source); + } + + public static int serializeResume( + FlatBufferBuilder builder, + PatioResume instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioResumeFbs.startPatioResumeFbs(builder); + PatioResumeFbs.addSource(builder, sourceFbs); + PatioResumeFbs.addValue(builder, valueFbs); + return PatioResumeFbs.endPatioResumeFbs(builder); + } + + private static PatioResume deserializeResume( + PatioResumeFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + instructionFbs.source(), + parentSource + ); + + var value = deserializeOperand(instructionFbs.value()); + + return new PatioResume(value, source); + } + + public static int serializeReturn( + FlatBufferBuilder builder, + PatioReturn instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioReturnFbs.startPatioReturnFbs(builder); + PatioReturnFbs.addSource(builder, sourceFbs); + PatioResumeFbs.addValue(builder, valueFbs); + return PatioReturnFbs.endPatioReturnFbs(builder); + } + + private static PatioReturn deserializeReturn( + PatioReturnFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + instructionFbs.source(), + parentSource + ); + + var value = deserializeOperand(instructionFbs.value()); + + return new PatioReturn(value, source); + } + + public static int serializeReturnVoid( + FlatBufferBuilder builder, + PatioReturnVoid instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + PatioReturnVoidFbs.startPatioReturnVoidFbs(builder); + PatioReturnVoidFbs.addSource(builder, sourceFbs); + return PatioReturnVoidFbs.endPatioReturnVoidFbs(builder); + } + + private static PatioReturnVoid deserializeReturnVoid( + PatioReturnVoidFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + return new PatioReturnVoid(source); + } + + public static int serializeSwitch( + FlatBufferBuilder builder, + PatioSwitch instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var destinationsOffsetsFbs = instruction.destinations().stream() + .mapToInt(destination -> serializeLocalRef(builder, destination)) + .toArray(); + var destinationsVectorFbs = PatioSwitchFbs.createDestinationsVector(builder, destinationsOffsetsFbs); + + var valueFbs = serializeOperand(builder, instruction.value()); + + var casesOffsetsFbs = instruction.cases().stream() + .mapToInt(case_ -> serializeSwitchCase(builder, case_, instruction.source())) + .toArray(); + var casesVectorFbs = PatioSwitchFbs.createCasesVector(builder, casesOffsetsFbs); + + var defaultBodyFbs = serializeBlock(builder, instruction.defaultBody(), instruction.source()); + + PatioSwitchFbs.startPatioSwitchFbs(builder); + PatioSwitchFbs.addSource(builder, sourceFbs); + PatioSwitchFbs.addDestinations(builder, destinationsVectorFbs); + PatioSwitchFbs.addValue(builder, valueFbs); + PatioSwitchFbs.addCases(builder, casesVectorFbs); + PatioSwitchFbs.addDefaultBody(builder, defaultBodyFbs); + return PatioSwitchFbs.endPatioSwitchFbs(builder); + } + + private static PatioSwitch deserializeSwitch( + PatioSwitchFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + instructionFbs.source(), + parentSource + ); + + var destinations = deserializeVector( + instructionFbs::destinations, + instructionFbs.destinationsLength(), + PatioToFlatbuffers::deserializeLocalRef + ); + + var value = deserializeOperand(instructionFbs.value()); + + var cases = deserializeVector( + instructionFbs::cases, + instructionFbs.casesLength(), + caseFbs -> deserializeSwitchCase(caseFbs, source) + ); + + var defaultBody = deserializeBlock(instructionFbs.defaultBody(), source); + + return new PatioSwitch(destinations, value, cases, defaultBody, source); + } + + private static int serializeSwitchCase( + FlatBufferBuilder builder, + PatioSwitch.Case case_, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + case_.source(), + parentSource + ); + + var bodyFbs = serializeBlock(builder, case_.body(), case_.source()); + + PatioSwitchCaseFbs.startPatioSwitchCaseFbs(builder); + PatioSwitchCaseFbs.addSource(builder, sourceFbs); + PatioSwitchCaseFbs.addValue(builder, case_.value()); + PatioSwitchCaseFbs.addBody(builder, bodyFbs); + return PatioSwitchCaseFbs.endPatioSwitchCaseFbs(builder); + } + + private static PatioSwitch.Case deserializeSwitchCase( + PatioSwitchCaseFbs caseFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + caseFbs.source(), + parentSource + ); + + var body = deserializeBlock(caseFbs.body(), source); + + return new PatioSwitch.Case((int) caseFbs.value(), body, source); + } + + public static int serializeThrow( + FlatBufferBuilder builder, + PatioThrow instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var exceptionTypeNameFbs = builder.createString(instruction.exceptionTypeName()); + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioThrowFbs.startPatioThrowFbs(builder); + PatioThrowFbs.addSource(builder, sourceFbs); + PatioThrowFbs.addExceptionTypeName(builder, exceptionTypeNameFbs); + PatioThrowFbs.addValue(builder, valueFbs); + return PatioThrowFbs.endPatioThrowFbs(builder); + } + + private static PatioThrow deserializeThrow( + PatioThrowFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + instructionFbs.source(), + parentSource + ); + + var value = deserializeOperand(instructionFbs.value()); + + return new PatioThrow( + instructionFbs.exceptionTypeName(), + value, + source + ); + } + + public static int serializeTry( + FlatBufferBuilder builder, + PatioTry instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var destinationsOffsetsFbs = instruction.destinations().stream() + .mapToInt(destination -> serializeLocalRef(builder, destination)) + .toArray(); + var destinationsVectorFbs = PatioTryFbs.createDestinationsVector(builder, destinationsOffsetsFbs); + + var catchClausesOffsetsFbs = instruction.catchClauses().stream() + .mapToInt(catchClauseFbs -> serializeCatchClause(builder, catchClauseFbs, instruction.source())) + .toArray(); + var catchClausesVectorFbs = PatioTryFbs.createCatchClausesVector(builder, catchClausesOffsetsFbs); + + var bodyFbs = serializeBlock(builder, instruction.body(), instruction.source()); + + PatioTryFbs.startPatioTryFbs(builder); + PatioTryFbs.addSource(builder, sourceFbs); + PatioTryFbs.addDestinations(builder, destinationsVectorFbs); + PatioTryFbs.addCatchClauses(builder, catchClausesVectorFbs); + PatioTryFbs.addBody(builder, bodyFbs); + return PatioTryFbs.endPatioTryFbs(builder); + } + + private static PatioTry deserializeTry( + PatioTryFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + var destinations = deserializeVector( + instructionFbs::destinations, + instructionFbs.destinationsLength(), + PatioToFlatbuffers::deserializeLocalRef + ); + + var catchClauses = deserializeVector( + instructionFbs::catchClauses, + instructionFbs.catchClausesLength(), + catchClauseFbs -> deserializeCatchClause(catchClauseFbs, source) + ); + + var body = deserializeBlock(instructionFbs.body(), source); + + return new PatioTry(destinations, catchClauses, body, source); + } + + private static int serializeCatchClause( + FlatBufferBuilder builder, + PatioCatchClause catchClause, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + catchClause.source(), + parentSource + ); + + var exceptionTypeNameFbs = builder.createString(catchClause.exceptionTypeName()); + + var destinationFbs = serializeLocalRef(builder, catchClause.destination()); + + var bodyFbs = serializeBlock(builder, catchClause.body(), catchClause.source()); + + PatioCatchClauseFbs.startPatioCatchClauseFbs(builder); + PatioCatchClauseFbs.addSource(builder, sourceFbs); + PatioCatchClauseFbs.addExceptionTypeName(builder, exceptionTypeNameFbs); + PatioCatchClauseFbs.addDestination(builder, destinationFbs); + PatioCatchClauseFbs.addBody(builder, bodyFbs); + return PatioCatchClauseFbs.endPatioCatchClauseFbs(builder); + } + + private static PatioCatchClause deserializeCatchClause( + PatioCatchClauseFbs catchClauseFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(catchClauseFbs.source(), parentSource); + + var destination = deserializeLocalRef(catchClauseFbs.destination()); + + var body = deserializeBlock(catchClauseFbs.body(), source); + + return new PatioCatchClause( + catchClauseFbs.exceptionTypeName(), + destination, + body, + source + ); + } + + public static int serializeUnreachable( + FlatBufferBuilder builder, + PatioUnreachable instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + PatioUnreachableFbs.startPatioUnreachableFbs(builder); + PatioUnreachableFbs.addSource(builder, sourceFbs); + return PatioUnreachableFbs.endPatioUnreachableFbs(builder); + } + + private static PatioUnreachable deserializeUnreachable( + PatioUnreachableFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource(instructionFbs.source(), parentSource); + + return new PatioUnreachable(source); + } + + public static int serializeVarLoad( + FlatBufferBuilder builder, + PatioVarLoad instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var destinationFbs = serializeLocalRef(builder, instruction.destination()); + var varFbs = serializeLocalRef(builder, instruction.var()); + + PatioVarLoadFbs.startPatioVarLoadFbs(builder); + PatioVarLoadFbs.addSource(builder, sourceFbs); + PatioVarLoadFbs.addDestination(builder, destinationFbs); + PatioVarLoadFbs.addVar(builder, varFbs); + return PatioVarLoadFbs.endPatioVarLoadFbs(builder); + } + + private static PatioVarLoad deserializeVarLoad( + PatioVarLoadFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + instructionFbs.source(), + parentSource + ); + + var destination = deserializeLocalRef(instructionFbs.destination()); + + var var = deserializeLocalRef(instructionFbs.var()); + + return new PatioVarLoad(destination, var, source); + } + + public static int serializeVarStore( + FlatBufferBuilder builder, + PatioVarStore instruction, + Source parentSource + ) { + var sourceFbs = SourceToFlatbuffers.serializeSource( + builder, + instruction.source(), + parentSource + ); + + var varFbs = serializeLocalRef(builder, instruction.var()); + + var valueFbs = serializeOperand(builder, instruction.value()); + + PatioVarStoreFbs.startPatioVarStoreFbs(builder); + PatioVarStoreFbs.addSource(builder, sourceFbs); + PatioVarStoreFbs.addVar(builder, varFbs); + PatioVarStoreFbs.addValue(builder, valueFbs); + return PatioVarStoreFbs.endPatioVarStoreFbs(builder); + } + + private static PatioVarStore deserializeVarStore( + PatioVarStoreFbs instructionFbs, + Source parentSource + ) { + var source = SourceToFlatbuffers.deserializeSource( + instructionFbs.source(), + parentSource + ); + + var var = deserializeLocalRef(instructionFbs.var()); + + var value = deserializeOperand(instructionFbs.value()); + + return new PatioVarStore(var, value, source); + } + // == Operands == public static int serializeOperand( @@ -68,11 +1590,7 @@ public class PatioToFlatbuffers { } case PatioLocalRef localRef -> { - var nameFbs = builder.createString(localRef.name()); - - PatioLocalRefFbs.startPatioLocalRefFbs(builder); - PatioLocalRefFbs.addName(builder, nameFbs); - var localRefFbs = PatioLocalRefFbs.endPatioLocalRefFbs(builder); + var localRefFbs = serializeLocalRef(builder, localRef); PatioOperandFbs.startPatioOperandFbs(builder); PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioLocalRefFbs); @@ -105,9 +1623,6 @@ public class PatioToFlatbuffers { PatioOperandFbs.addValue(builder, int32ConstFbs); yield PatioOperandFbs.endPatioOperandFbs(builder); } - - default -> - throw new UnsupportedOperationException("TODO"); }; } @@ -158,7 +1673,7 @@ public class PatioToFlatbuffers { case PatioOperandValueFbs.PatioLocalRefFbs -> { var localRefFbs = (PatioLocalRefFbs) operandFbs.value(new PatioLocalRefFbs()); - yield new PatioLocalRef(localRefFbs.name()); + yield deserializeLocalRef(localRefFbs); } default -> @@ -166,6 +1681,18 @@ public class PatioToFlatbuffers { }; } + private static int serializeLocalRef(FlatBufferBuilder builder, PatioLocalRef localRef) { + var nameFbs = builder.createString(localRef.name()); + + PatioLocalRefFbs.startPatioLocalRefFbs(builder); + PatioLocalRefFbs.addName(builder, nameFbs); + return PatioLocalRefFbs.endPatioLocalRefFbs(builder); + } + + private static PatioLocalRef deserializeLocalRef(PatioLocalRefFbs localRefFbs) { + return new PatioLocalRef(localRefFbs.name()); + } + // == Data ptr values == public static int serializeDataPtrValue( @@ -222,7 +1749,7 @@ public class PatioToFlatbuffers { var layoutFbs = serializeDataLayout(builder, byteOffsetOf.layout()); var offsetFbs = PatioIntValueByteOffsetOfFbs.createOffsetVector( builder, - intListToLongArray(byteOffsetOf.offset()) + serializeListOfInts(byteOffsetOf.offset()) ); var byteOffsetOfFbs = PatioIntValueByteOffsetOfFbs.createPatioIntValueByteOffsetOfFbs( @@ -465,7 +1992,7 @@ public class PatioToFlatbuffers { case PatioDataLayoutValueFbs.PatioDataLayoutStructFbs -> { var structFbs = (PatioDataLayoutStructFbs) layoutFbs.value(new PatioDataLayoutStructFbs()); - var elements = mapVector( + var elements = deserializeVector( structFbs::elements, structFbs.elementsLength(), PatioToFlatbuffers::deserializeDataLayout @@ -587,7 +2114,7 @@ public class PatioToFlatbuffers { var elementLayout = deserializeDataLayout(arrayFbs.elementLayout()); - var elements = mapVector( + var elements = deserializeVector( arrayFbs::elements, arrayFbs.elementsLength(), PatioToFlatbuffers::deserializeDataValue @@ -635,7 +2162,7 @@ public class PatioToFlatbuffers { case PatioDataValueValueFbs.PatioDataValueStructFbs -> { var structFbs = (PatioDataValueStructFbs) valueFbs.value(new PatioDataValueStructFbs()); - var elements = mapVector( + var elements = deserializeVector( structFbs::elements, structFbs.elementsLength(), PatioToFlatbuffers::deserializeDataValue @@ -651,17 +2178,21 @@ public class PatioToFlatbuffers { // == Flatbuffers == - private static <T, R> List<R> mapVector(Function<Integer, T> get, int length, Function<T, R> map) { + private static <T, R> List<R> deserializeVector( + Function<Integer, T> get, + int length, + Function<T, R> deserializeElement + ) { var result = new ArrayList<R>(); for (var index = 0; index < length; index++) { var elementFbs = get.apply(index); - var element = map.apply(elementFbs); + var element = deserializeElement.apply(elementFbs); result.add(element); } return result; } - private static long[] intListToLongArray(List<Integer> list) { + private static long[] serializeListOfInts(List<Integer> list) { var array = new long[list.size()]; for (var index = 0; index < list.size(); index++) { array[index] = list.get(index); |
