diff options
Diffstat (limited to 'src/main/java/org/shedlang')
4 files changed, 147 insertions, 2862 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java b/src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java index 75794092..31fdb1af 100644 --- a/src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java @@ -1,18 +1,17 @@ package org.shedlang.compiler.backends.cranelift; -import com.google.flatbuffers.FlatBufferBuilder; import org.pcollections.PVector; import org.shedlang.compiler.backends.Backend; import org.shedlang.compiler.backends.PatioBackends; import org.shedlang.compiler.backends.llvm.LlvmBackend; -import org.shedlang.compiler.backends.targets.NativeTarget; import org.shedlang.compiler.backends.paths.PartialTargetPath; import org.shedlang.compiler.backends.paths.TargetDir; +import org.shedlang.compiler.backends.targets.NativeTarget; import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.filesystem.ShedCompilerFiles; import org.shedlang.compiler.patio.PatioCompilationUnit; -import org.shedlang.compiler.patio.serialization.PatioToFlatbuffers; +import org.shedlang.compiler.patio.serialization.PatioToTransient0; import org.shedlang.compiler.runtime.ShedRuntime; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.typechecker.TypeInfos; @@ -20,6 +19,7 @@ import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.util.P; import org.shedlang.compiler.util.processes.ProcessError; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -62,14 +62,11 @@ public class CraneliftBackend implements Backend { Optional<String> mainFunctionName, PartialTargetPath partialTargetPath ) throws IOException, InterruptedException { - var fbsBuilder = new FlatBufferBuilder(); - var compilationUnitOffset = PatioToFlatbuffers.serializeCompilationUnit(fbsBuilder, patio); - fbsBuilder.finish(compilationUnitOffset); - - // TODO: avoid the copy var patioPath = partialTargetPath.withExtension(".patio"); Files.createDirectories(patioPath.getParent()); - Files.write(patioPath, fbsBuilder.sizedByteArray()); + try (var outputStream = new FileOutputStream(patioPath.toFile())) { + PatioToTransient0.serializeCompilationUnit(patio, outputStream); + } var patioToCraneliftArgs = new ArrayList<String>(); patioToCraneliftArgs.add( diff --git a/src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java b/src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java deleted file mode 100644 index 1f2e34c8..00000000 --- a/src/main/java/org/shedlang/compiler/patio/serialization/PatioToFlatbuffers.java +++ /dev/null @@ -1,2712 +0,0 @@ -package org.shedlang.compiler.patio.serialization; - -import com.google.flatbuffers.FlatBufferBuilder; -import com.google.flatbuffers.IntVector; -import org.shedlang.compiler.patio.*; -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 { - // == Compilation units == - - public static int serializeCompilationUnit( - FlatBufferBuilder builder, - PatioCompilationUnit compilationUnit - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - compilationUnit.source(), - Optional.empty() - ); - - var elementsOffsetsFbs = compilationUnit.elements().stream() - .mapToInt(element -> serializeCompilationUnitElement(builder, element, compilationUnit.source())) - .toArray(); - var elementsVectorFbs = PatioCompilationUnitFbs.createElementsVector(builder, elementsOffsetsFbs); - - PatioCompilationUnitFbs.startPatioCompilationUnitFbs(builder); - PatioCompilationUnitFbs.addSource(builder, sourceFbs); - PatioCompilationUnitFbs.addElements(builder, elementsVectorFbs); - return PatioCompilationUnitFbs.endPatioCompilationUnitFbs(builder); - } - - public static PatioCompilationUnit deserializeCompilationUnit( - PatioCompilationUnitFbs compilationUnitFbs - ) { - var source = SourceToFlatbuffers.deserializeSource( - compilationUnitFbs.source(), - Optional.empty() - ); - - var elements = deserializeVector( - compilationUnitFbs::elements, - compilationUnitFbs.elementsLength(), - elementFbs -> deserializeCompilationUnitElement(elementFbs, source) - ); - - return new PatioCompilationUnit(elements, source); - } - - // == Compilation unit elements == - - public static int serializeCompilationUnitElement( - FlatBufferBuilder builder, - PatioCompilationUnitElement element, - Source parentSource - ) { - return switch (element) { - case PatioDataElementImmutableDefinition dataElementImmutableDefinition -> { - var valueFbs = serializeDataElementImmutableDefinition(builder, dataElementImmutableDefinition, parentSource); - - PatioCompilationUnitElementFbs.startPatioCompilationUnitElementFbs(builder); - PatioCompilationUnitElementFbs.addValueType( - builder, - PatioCompilationUnitElementValueFbs.PatioDataElementImmutableDefinitionFbs - ); - PatioCompilationUnitElementFbs.addValue(builder, valueFbs); - yield PatioCompilationUnitElementFbs.endPatioCompilationUnitElementFbs(builder); - } - - case PatioDataElementMutableDeclaration dataElementMutableDeclaration -> { - var valueFbs = serializeDataElementMutableDeclaration(builder, dataElementMutableDeclaration, parentSource); - - PatioCompilationUnitElementFbs.startPatioCompilationUnitElementFbs(builder); - PatioCompilationUnitElementFbs.addValueType( - builder, - PatioCompilationUnitElementValueFbs.PatioDataElementMutableDeclarationFbs - ); - PatioCompilationUnitElementFbs.addValue(builder, valueFbs); - yield PatioCompilationUnitElementFbs.endPatioCompilationUnitElementFbs(builder); - } - - case PatioDataElementMutableDefinition dataElementMutableDefinition -> { - var valueFbs = serializeDataElementMutableDefinition(builder, dataElementMutableDefinition, parentSource); - - PatioCompilationUnitElementFbs.startPatioCompilationUnitElementFbs(builder); - PatioCompilationUnitElementFbs.addValueType( - builder, - PatioCompilationUnitElementValueFbs.PatioDataElementMutableDefinitionFbs - ); - PatioCompilationUnitElementFbs.addValue(builder, valueFbs); - yield PatioCompilationUnitElementFbs.endPatioCompilationUnitElementFbs(builder); - } - - case PatioExceptionTypeDefinition exceptionTypeDefinition -> { - var valueFbs = serializeExceptionTypeDefinition(builder, exceptionTypeDefinition, parentSource); - - PatioCompilationUnitElementFbs.startPatioCompilationUnitElementFbs(builder); - PatioCompilationUnitElementFbs.addValueType( - builder, - PatioCompilationUnitElementValueFbs.PatioExceptionTypeDefinitionFbs - ); - PatioCompilationUnitElementFbs.addValue(builder, valueFbs); - yield PatioCompilationUnitElementFbs.endPatioCompilationUnitElementFbs(builder); - } - - case PatioFunctionDeclaration functionDeclaration -> { - var valueFbs = serializeFunctionDeclaration(builder, functionDeclaration, parentSource); - - PatioCompilationUnitElementFbs.startPatioCompilationUnitElementFbs(builder); - PatioCompilationUnitElementFbs.addValueType( - builder, - PatioCompilationUnitElementValueFbs.PatioFunctionDeclarationFbs - ); - PatioCompilationUnitElementFbs.addValue(builder, valueFbs); - yield PatioCompilationUnitElementFbs.endPatioCompilationUnitElementFbs(builder); - } - - case PatioFunctionDefinition functionDefinition -> { - var valueFbs = serializeFunctionDefinition(builder, functionDefinition, parentSource); - - PatioCompilationUnitElementFbs.startPatioCompilationUnitElementFbs(builder); - PatioCompilationUnitElementFbs.addValueType( - builder, - PatioCompilationUnitElementValueFbs.PatioFunctionDefinitionFbs - ); - PatioCompilationUnitElementFbs.addValue(builder, valueFbs); - yield PatioCompilationUnitElementFbs.endPatioCompilationUnitElementFbs(builder); - } - }; - } - - public static PatioCompilationUnitElement deserializeCompilationUnitElement( - PatioCompilationUnitElementFbs elementFbs, - Source parentSource - ) { - return switch (elementFbs.valueType()) { - case PatioCompilationUnitElementValueFbs.PatioDataElementImmutableDefinitionFbs -> { - var dataElementImmutableDefinitionFbs = - (PatioDataElementImmutableDefinitionFbs) elementFbs.value(new PatioDataElementImmutableDefinitionFbs()); - yield deserializeDataElementImmutableDefinition(dataElementImmutableDefinitionFbs, parentSource); - } - - case PatioCompilationUnitElementValueFbs.PatioDataElementMutableDeclarationFbs -> { - var dataElementMutableDeclarationFbs = - (PatioDataElementMutableDeclarationFbs) elementFbs.value(new PatioDataElementMutableDeclarationFbs()); - yield deserializeDataElementMutableDeclaration(dataElementMutableDeclarationFbs, parentSource); - } - - case PatioCompilationUnitElementValueFbs.PatioDataElementMutableDefinitionFbs -> { - var dataElementMutableDefinitionFbs = - (PatioDataElementMutableDefinitionFbs) elementFbs.value(new PatioDataElementMutableDefinitionFbs()); - yield deserializeDataElementMutableDefinition(dataElementMutableDefinitionFbs, parentSource); - } - - case PatioCompilationUnitElementValueFbs.PatioExceptionTypeDefinitionFbs -> { - var exceptionTypeDefinitionFbs = - (PatioExceptionTypeDefinitionFbs) elementFbs.value(new PatioExceptionTypeDefinitionFbs()); - yield deserializeExceptionTypeDefinition(exceptionTypeDefinitionFbs, parentSource); - } - - case PatioCompilationUnitElementValueFbs.PatioFunctionDeclarationFbs -> { - var functionDeclarationFbs = - (PatioFunctionDeclarationFbs) elementFbs.value(new PatioFunctionDeclarationFbs()); - yield deserializeFunctionDeclaration(functionDeclarationFbs, parentSource); - } - - case PatioCompilationUnitElementValueFbs.PatioFunctionDefinitionFbs -> { - var functionDefinitionFbs = - (PatioFunctionDefinitionFbs) elementFbs.value(new PatioFunctionDefinitionFbs()); - yield deserializeFunctionDefinition(functionDefinitionFbs, parentSource); - } - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - private static int serializeDataElementImmutableDefinition( - FlatBufferBuilder builder, - PatioDataElementImmutableDefinition dataElementImmutableDefinition, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - dataElementImmutableDefinition.source(), - parentSource - ); - - var nameFbs = builder.createString(dataElementImmutableDefinition.name()); - - var visibilityFbs = serializeVisibility(dataElementImmutableDefinition.visibility()); - - var valueFbs = serializeDataValue(builder, dataElementImmutableDefinition.value()); - - PatioDataElementImmutableDefinitionFbs.startPatioDataElementImmutableDefinitionFbs(builder); - PatioDataElementImmutableDefinitionFbs.addSource(builder, sourceFbs); - PatioDataElementImmutableDefinitionFbs.addName(builder, nameFbs); - PatioDataElementImmutableDefinitionFbs.addVisibility( - builder, - visibilityFbs - ); - PatioDataElementImmutableDefinitionFbs.addValue(builder, valueFbs); - return PatioDataElementImmutableDefinitionFbs.endPatioDataElementImmutableDefinitionFbs(builder); - } - - private static PatioDataElementImmutableDefinition deserializeDataElementImmutableDefinition( - PatioDataElementImmutableDefinitionFbs dataElementImmutableDefinitionFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - dataElementImmutableDefinitionFbs.source(), - parentSource - ); - - var name = dataElementImmutableDefinitionFbs.name(); - var visibility = deserializeVisibility(dataElementImmutableDefinitionFbs.visibility()); - var value = deserializeDataValue(dataElementImmutableDefinitionFbs.value()); - - return new PatioDataElementImmutableDefinition(name, visibility, value, source); - } - - private static int serializeDataElementMutableDeclaration( - FlatBufferBuilder builder, - PatioDataElementMutableDeclaration dataElementMutableDeclaration, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - dataElementMutableDeclaration.source(), - parentSource - ); - - var nameFbs = builder.createString(dataElementMutableDeclaration.name()); - - var layoutFbs = serializeDataLayout(builder, dataElementMutableDeclaration.layout()); - - PatioDataElementMutableDeclarationFbs.startPatioDataElementMutableDeclarationFbs(builder); - PatioDataElementMutableDeclarationFbs.addSource(builder, sourceFbs); - PatioDataElementMutableDeclarationFbs.addName(builder, nameFbs); - PatioDataElementMutableDeclarationFbs.addLayout(builder, layoutFbs); - return PatioDataElementMutableDeclarationFbs.endPatioDataElementMutableDeclarationFbs(builder); - } - - private static PatioDataElementMutableDeclaration deserializeDataElementMutableDeclaration( - PatioDataElementMutableDeclarationFbs dataElementMutableDeclarationFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - dataElementMutableDeclarationFbs.source(), - parentSource - ); - - var name = dataElementMutableDeclarationFbs.name(); - var layout = deserializeDataLayout(dataElementMutableDeclarationFbs.layout()); - - return new PatioDataElementMutableDeclaration(name, layout, source); - } - - private static int serializeDataElementMutableDefinition( - FlatBufferBuilder builder, - PatioDataElementMutableDefinition dataElementMutableDefinition, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - dataElementMutableDefinition.source(), - parentSource - ); - - var nameFbs = builder.createString(dataElementMutableDefinition.name()); - - var visibilityFbs = serializeVisibility(dataElementMutableDefinition.visibility()); - - var initialValueFbs = serializeDataValue(builder, dataElementMutableDefinition.initialValue()); - - PatioDataElementMutableDefinitionFbs.startPatioDataElementMutableDefinitionFbs(builder); - PatioDataElementMutableDefinitionFbs.addSource(builder, sourceFbs); - PatioDataElementMutableDefinitionFbs.addName(builder, nameFbs); - PatioDataElementMutableDefinitionFbs.addVisibility( - builder, - visibilityFbs - ); - PatioDataElementMutableDefinitionFbs.addInitialValue(builder, initialValueFbs); - return PatioDataElementMutableDefinitionFbs.endPatioDataElementMutableDefinitionFbs(builder); - } - - private static PatioDataElementMutableDefinition deserializeDataElementMutableDefinition( - PatioDataElementMutableDefinitionFbs dataElementMutableDefinitionFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - dataElementMutableDefinitionFbs.source(), - parentSource - ); - - var name = dataElementMutableDefinitionFbs.name(); - var visibility = deserializeVisibility(dataElementMutableDefinitionFbs.visibility()); - var initialValue = deserializeDataValue(dataElementMutableDefinitionFbs.initialValue()); - - return new PatioDataElementMutableDefinition(name, visibility, initialValue, source); - } - - private static int serializeExceptionTypeDefinition( - FlatBufferBuilder builder, - PatioExceptionTypeDefinition exceptionTypeDefinition, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - exceptionTypeDefinition.source(), - parentSource - ); - - var nameFbs = builder.createString(exceptionTypeDefinition.name()); - var valueTypeFbs = serializeType(builder, exceptionTypeDefinition.valueType()); - - PatioExceptionTypeDefinitionFbs.startPatioExceptionTypeDefinitionFbs(builder); - PatioExceptionTypeDefinitionFbs.addSource(builder, sourceFbs); - PatioExceptionTypeDefinitionFbs.addName(builder, nameFbs); - PatioExceptionTypeDefinitionFbs.addValueType(builder, valueTypeFbs); - return PatioExceptionTypeDefinitionFbs.endPatioExceptionTypeDefinitionFbs(builder); - } - - private static PatioExceptionTypeDefinition deserializeExceptionTypeDefinition( - PatioExceptionTypeDefinitionFbs exceptionTypeDefinitionFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - exceptionTypeDefinitionFbs.source(), - parentSource - ); - - var name = exceptionTypeDefinitionFbs.name(); - var valueType = deserializeType(exceptionTypeDefinitionFbs.valueType()); - - return new PatioExceptionTypeDefinition(name, valueType, source); - } - - private static int serializeFunctionDeclaration( - FlatBufferBuilder builder, - PatioFunctionDeclaration functionDeclaration, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - functionDeclaration.source(), - parentSource - ); - - var nameFbs = builder.createString(functionDeclaration.name()); - - var paramsOffsetsFbs = functionDeclaration.params().stream() - .mapToInt(param -> serializeLocal(builder, param)) - .toArray(); - var paramsVectorFbs = PatioFunctionDeclarationFbs.createParamsVector(builder, paramsOffsetsFbs); - - var returnTypesOffsetsFbs = functionDeclaration.returnTypes().stream() - .mapToInt(returnType -> serializeType(builder, returnType)) - .toArray(); - var returnTypesVectorFbs = PatioFunctionDeclarationFbs.createReturnTypesVector(builder, returnTypesOffsetsFbs); - - PatioFunctionDeclarationFbs.startPatioFunctionDeclarationFbs(builder); - PatioFunctionDeclarationFbs.addSource(builder, sourceFbs); - PatioFunctionDeclarationFbs.addName(builder, nameFbs); - PatioFunctionDeclarationFbs.addParams(builder, paramsVectorFbs); - PatioFunctionDeclarationFbs.addReturnTypes(builder, returnTypesVectorFbs); - return PatioFunctionDeclarationFbs.endPatioFunctionDeclarationFbs(builder); - } - - private static PatioFunctionDeclaration deserializeFunctionDeclaration( - PatioFunctionDeclarationFbs functionDeclarationFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - functionDeclarationFbs.source(), - parentSource - ); - - var name = functionDeclarationFbs.name(); - var params = deserializeVector( - functionDeclarationFbs::params, - functionDeclarationFbs.paramsLength(), - PatioToFlatbuffers::deserializeLocal - ); - var returnTypes = deserializeVector( - functionDeclarationFbs::returnTypes, - functionDeclarationFbs.returnTypesLength(), - PatioToFlatbuffers::deserializeType - ); - - return new PatioFunctionDeclaration(name, params, returnTypes, source); - } - - private static int serializeFunctionDefinition( - FlatBufferBuilder builder, - PatioFunctionDefinition functionDefinition, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - functionDefinition.source(), - parentSource - ); - - var nameFbs = builder.createString(functionDefinition.name()); - - var visibilityFbs = serializeVisibility(functionDefinition.visibility()); - - var localsOffsetsFbs = functionDefinition.locals().stream() - .mapToInt(local -> serializeLocal(builder, local)) - .toArray(); - var localsVectorFbs = PatioFunctionDefinitionFbs.createLocalsVector(builder, localsOffsetsFbs); - - var returnTypesOffsetsFbs = functionDefinition.returnTypes().stream() - .mapToInt(returnType -> serializeType(builder, returnType)) - .toArray(); - var returnTypesVectorFbs = PatioFunctionDefinitionFbs.createReturnTypesVector(builder, returnTypesOffsetsFbs); - - var bodyOffsetsFbs = functionDefinition.body().stream() - .mapToInt(bodyInstruction -> serializeInstruction(builder, bodyInstruction, functionDefinition.source())) - .toArray(); - var bodyVectorFbs = PatioFunctionDefinitionFbs.createBodyVector(builder, bodyOffsetsFbs); - - PatioFunctionDefinitionFbs.startPatioFunctionDefinitionFbs(builder); - PatioFunctionDefinitionFbs.addSource(builder, sourceFbs); - PatioFunctionDefinitionFbs.addName(builder, nameFbs); - PatioFunctionDefinitionFbs.addVisibility( - builder, - visibilityFbs - ); - PatioFunctionDefinitionFbs.addLocals(builder, localsVectorFbs); - PatioFunctionDefinitionFbs.addReturnTypes(builder, returnTypesVectorFbs); - PatioFunctionDefinitionFbs.addBody(builder, bodyVectorFbs); - return PatioFunctionDefinitionFbs.endPatioFunctionDefinitionFbs(builder); - } - - private static PatioFunctionDefinition deserializeFunctionDefinition( - PatioFunctionDefinitionFbs functionDefinitionFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - functionDefinitionFbs.source(), - parentSource - ); - - var name = functionDefinitionFbs.name(); - var visibility = deserializeVisibility(functionDefinitionFbs.visibility()); - var locals = deserializeVector( - functionDefinitionFbs::locals, - functionDefinitionFbs.localsLength(), - PatioToFlatbuffers::deserializeLocal - ); - var returnTypes = deserializeVector( - functionDefinitionFbs::returnTypes, - functionDefinitionFbs.returnTypesLength(), - PatioToFlatbuffers::deserializeType - ); - var body = deserializeVector( - functionDefinitionFbs::body, - functionDefinitionFbs.bodyLength(), - instructionFbs -> deserializeInstruction(instructionFbs, source) - ); - - return new PatioFunctionDefinition(name, visibility, locals, returnTypes, body, source); - } - - // == Visibility == - - private static int serializeVisibility(PatioVisibility visibility) { - return switch (visibility) { - case PRIVATE -> - PatioVisibilityFbs.PRIVATE; - - case PUBLIC -> - PatioVisibilityFbs.PUBLIC; - }; - } - - private static PatioVisibility deserializeVisibility(int visibilityFbs) { - return switch (visibilityFbs) { - case PatioVisibilityFbs.PRIVATE -> - PatioVisibility.PRIVATE; - - case PatioVisibilityFbs.PUBLIC -> - PatioVisibility.PUBLIC; - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - // == Locals == - - private static int serializeLocal(FlatBufferBuilder builder, PatioLocal local) { - var nameFbs = builder.createString(local.name()); - var typeFbs = serializeType(builder, local.type()); - var localTypeFbs = serializeLocalType(local.localType()); - - PatioLocalFbs.startPatioLocalFbs(builder); - PatioLocalFbs.addName(builder, nameFbs); - PatioLocalFbs.addType(builder, typeFbs); - PatioLocalFbs.addLocalType(builder, localTypeFbs); - return PatioLocalFbs.endPatioLocalFbs(builder); - } - - private static PatioLocal deserializeLocal(PatioLocalFbs localFbs) { - var name = localFbs.name(); - var type = deserializeType(localFbs.type()); - var localType = deserializeLocalType(localFbs.localType()); - - return new PatioLocal(name, type, localType); - } - - private static int serializeLocalType(PatioLocalType localType) { - return switch (localType) { - case PARAM -> - PatioLocalTypeFbs.PARAM; - - case VAL -> - PatioLocalTypeFbs.VAL; - - case VAR -> - PatioLocalTypeFbs.VAR; - }; - } - - private static PatioLocalType deserializeLocalType(int localTypeFbs) { - return switch (localTypeFbs) { - case PatioLocalTypeFbs.PARAM -> - PatioLocalType.PARAM; - - case PatioLocalTypeFbs.VAL -> - PatioLocalType.VAL; - - case PatioLocalTypeFbs.VAR -> - PatioLocalType.VAR; - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - // == 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 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 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.PatioReturnFbs -> - deserializeReturn( - (PatioReturnFbs) instructionFbs.value(new PatioReturnFbs()), - 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 destinationsOffsetsFbs = instruction.destinations().stream() - .mapToInt(destination -> serializeLocalRef(builder, destination)) - .toArray(); - var destinationsVectorFbs = PatioCallFbs.createDestinationsVector(builder, destinationsOffsetsFbs); - - 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); - PatioCallFbs.addDestinations(builder, destinationsVectorFbs); - 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 destinations = deserializeVector( - instructionFbs::destinations, - instructionFbs.destinationsLength(), - PatioToFlatbuffers::deserializeLocalRef - ); - - var functionName = instructionFbs.functionName(); - - var args = deserializeVector( - instructionFbs::args, - instructionFbs.argsLength(), - argFbs -> deserializeOperand(argFbs) - ); - - return new PatioCall(destinations, functionName, args, source); - } - - public static int serializeCallPointer( - FlatBufferBuilder builder, - PatioCallPointer instruction, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - instruction.source(), - parentSource - ); - - var destinationsOffsetsFbs = instruction.destinations().stream() - .mapToInt(destination -> serializeLocalRef(builder, destination)) - .toArray(); - var destinationsVectorFbs = PatioCallFbs.createDestinationsVector(builder, destinationsOffsetsFbs); - - 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.addDestinations(builder, destinationsVectorFbs); - 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 destinations = deserializeVector( - instructionFbs::destinations, - instructionFbs.destinationsLength(), - PatioToFlatbuffers::deserializeLocalRef - ); - - var functionPointer = deserializeOperand(instructionFbs.functionPointer()); - - var args = deserializeVector( - instructionFbs::args, - instructionFbs.argsLength(), - argFbs -> deserializeOperand(argFbs) - ); - - return new PatioCallPointer(destinations, 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 elementOffsetsFb = PatioMemoryLoadFbs.createElementOffsetsVector( - builder, - serializeListOfInts(instruction.elementOffsets()) - ); - - PatioMemoryLoadFbs.startPatioMemoryLoadFbs(builder); - PatioMemoryLoadFbs.addSource(builder, sourceFbs); - PatioMemoryLoadFbs.addDestination(builder, destinationFbs); - PatioMemoryLoadFbs.addPointer(builder, pointerFbs); - PatioMemoryLoadFbs.addLayout(builder, layoutFbs); - PatioMemoryLoadFbs.addElementOffsets(builder, elementOffsetsFb); - 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 elementOffsets = deserializeListOfInts(instructionFbs.elementOffsetsVector()); - - return new PatioMemoryLoad(destination, pointer, layout, elementOffsets, 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 elementOffsetsFbs = PatioMemoryStoreFbs.createElementOffsetsVector( - builder, - serializeListOfInts(instruction.elementOffsets()) - ); - var valueFbs = serializeOperand(builder, instruction.value()); - - PatioMemoryStoreFbs.startPatioMemoryStoreFbs(builder); - PatioMemoryStoreFbs.addSource(builder, sourceFbs); - PatioMemoryStoreFbs.addPointer(builder, pointerFbs); - PatioMemoryStoreFbs.addLayout(builder, layoutFbs); - PatioMemoryStoreFbs.addElementOffsets(builder, elementOffsetsFbs); - 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 elementOffsets = deserializeListOfInts(instructionFbs.elementOffsetsVector()); - var value = deserializeOperand(instructionFbs.value()); - - return new PatioMemoryStore(pointer, layout, elementOffsets, value, source); - } - - public static int serializeReturn( - FlatBufferBuilder builder, - PatioReturn instruction, - Source parentSource - ) { - var sourceFbs = SourceToFlatbuffers.serializeSource( - builder, - instruction.source(), - parentSource - ); - - var valuesOffsetsFbs = instruction.values().stream() - .mapToInt(value -> serializeOperand(builder, value)) - .toArray(); - var valuesVectorFbs = PatioReturnFbs.createValuesVector(builder, valuesOffsetsFbs); - - PatioReturnFbs.startPatioReturnFbs(builder); - PatioReturnFbs.addSource(builder, sourceFbs); - PatioReturnFbs.addValues(builder, valuesVectorFbs); - return PatioReturnFbs.endPatioReturnFbs(builder); - } - - private static PatioReturn deserializeReturn( - PatioReturnFbs instructionFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - instructionFbs.source(), - parentSource - ); - - var values = deserializeVector( - instructionFbs::values, - instructionFbs.valuesLength(), - PatioToFlatbuffers::deserializeOperand - ); - - return new PatioReturn(values, 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, - PatioSwitchCase 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 PatioSwitchCase deserializeSwitchCase( - PatioSwitchCaseFbs caseFbs, - Source parentSource - ) { - var source = SourceToFlatbuffers.deserializeSource( - caseFbs.source(), - parentSource - ); - - var body = deserializeBlock(caseFbs.body(), source); - - return new PatioSwitchCase((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( - FlatBufferBuilder builder, - PatioOperand operand - ) { - return switch (operand) { - case PatioBoolConst boolConst -> { - PatioBoolConstFbs.startPatioBoolConstFbs(builder); - PatioBoolConstFbs.addValue(builder, boolConst.value()); - var boolConstFbs = PatioBoolConstFbs.endPatioBoolConstFbs(builder); - - PatioOperandFbs.startPatioOperandFbs(builder); - PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioBoolConstFbs); - PatioOperandFbs.addValue(builder, boolConstFbs); - yield PatioOperandFbs.endPatioOperandFbs(builder); - } - - case PatioByteConst byteConst -> { - PatioByteConstFbs.startPatioByteConstFbs(builder); - PatioByteConstFbs.addValue(builder, byteConst.value()); - var byteConstFbs = PatioByteConstFbs.endPatioByteConstFbs(builder); - - PatioOperandFbs.startPatioOperandFbs(builder); - PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioByteConstFbs); - PatioOperandFbs.addValue(builder, byteConstFbs); - yield PatioOperandFbs.endPatioOperandFbs(builder); - } - - case PatioDataPtrConst dataPtrConst -> { - var dataPtrValueFbs = serializeDataPtrValue(builder, dataPtrConst.value()); - - PatioDataPtrConstFbs.startPatioDataPtrConstFbs(builder); - PatioDataPtrConstFbs.addValue(builder, dataPtrValueFbs); - var dataPtrConstFbs = PatioDataPtrConstFbs.endPatioDataPtrConstFbs(builder); - - PatioOperandFbs.startPatioOperandFbs(builder); - PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioDataPtrConstFbs); - PatioOperandFbs.addValue(builder, dataPtrConstFbs); - yield PatioOperandFbs.endPatioOperandFbs(builder); - } - - case PatioFunctionPtrConst functionPtrConst -> { - var nameFbs = builder.createString(functionPtrConst.name()); - - PatioFunctionPtrConstFbs.startPatioFunctionPtrConstFbs(builder); - PatioFunctionPtrConstFbs.addName(builder, nameFbs); - var functionPtrConstFbs = PatioFunctionPtrConstFbs.endPatioFunctionPtrConstFbs(builder); - - PatioOperandFbs.startPatioOperandFbs(builder); - PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioFunctionPtrConstFbs); - PatioOperandFbs.addValue(builder, functionPtrConstFbs); - yield PatioOperandFbs.endPatioOperandFbs(builder); - } - - case PatioLocalRef localRef -> { - var localRefFbs = serializeLocalRef(builder, localRef); - - PatioOperandFbs.startPatioOperandFbs(builder); - PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioLocalRefFbs); - PatioOperandFbs.addValue(builder, localRefFbs); - yield PatioOperandFbs.endPatioOperandFbs(builder); - } - - case PatioIntConst intConst -> { - var intValueFbs = serializeIntValue(builder, intConst.value()); - - PatioIntConstFbs.startPatioIntConstFbs(builder); - PatioIntConstFbs.addValue(builder, intValueFbs); - var intConstFbs = PatioIntConstFbs.endPatioIntConstFbs(builder); - - PatioOperandFbs.startPatioOperandFbs(builder); - PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioIntConstFbs); - PatioOperandFbs.addValue(builder, intConstFbs); - yield PatioOperandFbs.endPatioOperandFbs(builder); - } - - case PatioInt32Const int32Const -> { - var intValueFbs = serializeIntValue(builder, int32Const.value()); - - PatioInt32ConstFbs.startPatioInt32ConstFbs(builder); - PatioInt32ConstFbs.addValue(builder, intValueFbs); - var int32ConstFbs = PatioInt32ConstFbs.endPatioInt32ConstFbs(builder); - - PatioOperandFbs.startPatioOperandFbs(builder); - PatioOperandFbs.addValueType(builder, PatioOperandValueFbs.PatioInt32ConstFbs); - PatioOperandFbs.addValue(builder, int32ConstFbs); - yield PatioOperandFbs.endPatioOperandFbs(builder); - } - }; - } - - public static PatioOperand deserializeOperand(PatioOperandFbs operandFbs) { - return switch (operandFbs.valueType()) { - case PatioOperandValueFbs.PatioBoolConstFbs -> { - var boolConstFbs = (PatioBoolConstFbs) operandFbs.value(new PatioBoolConstFbs()); - - yield new PatioBoolConst(boolConstFbs.value()); - } - - case PatioOperandValueFbs.PatioByteConstFbs -> { - var byteConstFbs = (PatioByteConstFbs) operandFbs.value(new PatioByteConstFbs()); - - yield new PatioByteConst((byte) byteConstFbs.value()); - } - - case PatioOperandValueFbs.PatioDataPtrConstFbs -> { - var dataPtrConstFbs = (PatioDataPtrConstFbs) operandFbs.value(new PatioDataPtrConstFbs()); - - var dataPtrValue = deserializeDataPtrValue(dataPtrConstFbs.value()); - - yield new PatioDataPtrConst(dataPtrValue); - } - - case PatioOperandValueFbs.PatioFunctionPtrConstFbs -> { - var functionPtrConstFbs = (PatioFunctionPtrConstFbs) operandFbs.value(new PatioFunctionPtrConstFbs()); - - yield new PatioFunctionPtrConst(functionPtrConstFbs.name()); - } - - case PatioOperandValueFbs.PatioIntConstFbs -> { - var intConstFbs = (PatioIntConstFbs) operandFbs.value(new PatioIntConstFbs()); - - var intValue = deserializeIntValue(intConstFbs.value()); - - yield new PatioIntConst(intValue); - } - - case PatioOperandValueFbs.PatioInt32ConstFbs -> { - var int32ConstFbs = (PatioInt32ConstFbs) operandFbs.value(new PatioInt32ConstFbs()); - - var intValue = deserializeIntValue(int32ConstFbs.value()); - - yield new PatioInt32Const(intValue); - } - - case PatioOperandValueFbs.PatioLocalRefFbs -> { - var localRefFbs = (PatioLocalRefFbs) operandFbs.value(new PatioLocalRefFbs()); - - yield deserializeLocalRef(localRefFbs); - } - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - 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( - FlatBufferBuilder builder, - PatioDataPtrValue value - ) { - switch (value) { - case PatioDataPtrValueDataElement dataElement -> { - var nameFbs = builder.createString(dataElement.name()); - - var dataElementFbs = PatioDataPtrValueDataElementFbs - .createPatioDataPtrValueDataElementFbs(builder, nameFbs); - - return PatioDataPtrValueFbs.createPatioDataPtrValueFbs( - builder, - PatioDataPtrValueValueFbs.PatioDataPtrValueDataElementFbs, - dataElementFbs - ); - } - - case PatioDataPtrValueNull _ -> { - PatioDataPtrValueNullFbs.startPatioDataPtrValueNullFbs(builder); - var nullFbs = PatioDataPtrValueNullFbs.endPatioDataPtrValueNullFbs(builder); - - return PatioDataPtrValueFbs.createPatioDataPtrValueFbs( - builder, - PatioDataPtrValueValueFbs.PatioDataPtrValueNullFbs, - nullFbs - ); - } - } - } - - public static PatioDataPtrValue deserializeDataPtrValue(PatioDataPtrValueFbs valueFbs) { - return switch (valueFbs.valueType()) { - case PatioDataPtrValueValueFbs.PatioDataPtrValueDataElementFbs -> { - var dataElementFbs = (PatioDataPtrValueDataElementFbs) valueFbs.value(new PatioDataPtrValueDataElementFbs()); - yield new PatioDataPtrValueDataElement(dataElementFbs.name()); - } - - case PatioDataPtrValueValueFbs.PatioDataPtrValueNullFbs -> - new PatioDataPtrValueNull(); - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - // == Int values == - - public static int serializeIntValue(FlatBufferBuilder builder, PatioIntValue value) { - return switch (value) { - case PatioIntValueByteOffsetOf byteOffsetOf -> { - var layoutFbs = serializeDataLayout(builder, byteOffsetOf.layout()); - var elementOffsetsFbs = PatioIntValueByteOffsetOfFbs.createElementOffsetsVector( - builder, - serializeListOfInts(byteOffsetOf.elementOffsets()) - ); - - var byteOffsetOfFbs = PatioIntValueByteOffsetOfFbs.createPatioIntValueByteOffsetOfFbs( - builder, - layoutFbs, - elementOffsetsFbs - ); - - PatioIntValueFbs.startPatioIntValueFbs(builder); - PatioIntValueFbs.addValueType(builder, PatioIntValueValueFbs.PatioIntValueByteOffsetOfFbs); - PatioIntValueFbs.addValue(builder, byteOffsetOfFbs); - yield PatioIntValueFbs.endPatioIntValueFbs(builder); - } - - case PatioIntValueByteSizeOf byteSizeOf -> { - var layoutFbs = serializeDataLayout(builder, byteSizeOf.layout()); - - var byteSizeOfFbs = PatioIntValueByteSizeOfFbs.createPatioIntValueByteSizeOfFbs(builder, layoutFbs); - - PatioIntValueFbs.startPatioIntValueFbs(builder); - PatioIntValueFbs.addValueType(builder, PatioIntValueValueFbs.PatioIntValueByteSizeOfFbs); - PatioIntValueFbs.addValue(builder, byteSizeOfFbs); - yield PatioIntValueFbs.endPatioIntValueFbs(builder); - } - - case PatioIntValueLiteral literal -> { - var literalFbs = PatioIntValueLiteralFbs.createPatioIntValueLiteralFbs(builder, literal.value()); - - PatioIntValueFbs.startPatioIntValueFbs(builder); - PatioIntValueFbs.addValueType(builder, PatioIntValueValueFbs.PatioIntValueLiteralFbs); - PatioIntValueFbs.addValue(builder, literalFbs); - yield PatioIntValueFbs.endPatioIntValueFbs(builder); - } - }; - } - - public static PatioIntValue deserializeIntValue(PatioIntValueFbs valueFbs) { - return switch (valueFbs.valueType()) { - case PatioIntValueValueFbs.PatioIntValueByteOffsetOfFbs -> { - var byteOffsetOfFbs = (PatioIntValueByteOffsetOfFbs) valueFbs.value(new PatioIntValueByteOffsetOfFbs()); - var layout = deserializeDataLayout(byteOffsetOfFbs.layout()); - var elementOffsets = deserializeListOfInts(byteOffsetOfFbs.elementOffsetsVector()); - yield new PatioIntValueByteOffsetOf(layout, elementOffsets); - } - - case PatioIntValueValueFbs.PatioIntValueByteSizeOfFbs -> { - var byteSizeOfFbs = (PatioIntValueByteSizeOfFbs) valueFbs.value(new PatioIntValueByteSizeOfFbs()); - var layout = deserializeDataLayout(byteSizeOfFbs.layout()); - yield new PatioIntValueByteSizeOf(layout); - } - - case PatioIntValueValueFbs.PatioIntValueLiteralFbs -> { - var literalFbs = (PatioIntValueLiteralFbs) valueFbs.value(new PatioIntValueLiteralFbs()); - yield new PatioIntValueLiteral(literalFbs.value()); - } - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - // == Types == - - public static int serializeType( - FlatBufferBuilder builder, - PatioType type - ) { - return switch (type) { - case PatioBoolType _ -> { - PatioBoolTypeFbs.startPatioBoolTypeFbs(builder); - var boolTypeFbs = PatioBoolTypeFbs.endPatioBoolTypeFbs(builder); - - PatioTypeFbs.startPatioTypeFbs(builder); - PatioTypeFbs.addValueType(builder, PatioTypeValueFbs.PatioBoolTypeFbs); - PatioTypeFbs.addValue(builder, boolTypeFbs); - yield PatioTypeFbs.endPatioTypeFbs(builder); - } - - case PatioByteType _ -> { - PatioByteTypeFbs.startPatioByteTypeFbs(builder); - var byteTypeFbs = PatioByteTypeFbs.endPatioByteTypeFbs(builder); - - PatioTypeFbs.startPatioTypeFbs(builder); - PatioTypeFbs.addValueType(builder, PatioTypeValueFbs.PatioByteTypeFbs); - PatioTypeFbs.addValue(builder, byteTypeFbs); - yield PatioTypeFbs.endPatioTypeFbs(builder); - } - - case PatioDataPtrType _ -> { - PatioDataPtrTypeFbs.startPatioDataPtrTypeFbs(builder); - var dataPtrTypeFbs = PatioDataPtrTypeFbs.endPatioDataPtrTypeFbs(builder); - - PatioTypeFbs.startPatioTypeFbs(builder); - PatioTypeFbs.addValueType(builder, PatioTypeValueFbs.PatioDataPtrTypeFbs); - PatioTypeFbs.addValue(builder, dataPtrTypeFbs); - yield PatioTypeFbs.endPatioTypeFbs(builder); - } - - case PatioFunctionPtrType _ -> { - PatioFunctionPtrTypeFbs.startPatioFunctionPtrTypeFbs(builder); - var functionPtrTypeFbs = PatioFunctionPtrTypeFbs.endPatioFunctionPtrTypeFbs(builder); - - PatioTypeFbs.startPatioTypeFbs(builder); - PatioTypeFbs.addValueType(builder, PatioTypeValueFbs.PatioFunctionPtrTypeFbs); - PatioTypeFbs.addValue(builder, functionPtrTypeFbs); - yield PatioTypeFbs.endPatioTypeFbs(builder); - } - - case PatioIntType _ -> { - PatioIntTypeFbs.startPatioIntTypeFbs(builder); - var intTypeFbs = PatioIntTypeFbs.endPatioIntTypeFbs(builder); - - PatioTypeFbs.startPatioTypeFbs(builder); - PatioTypeFbs.addValueType(builder, PatioTypeValueFbs.PatioIntTypeFbs); - PatioTypeFbs.addValue(builder, intTypeFbs); - yield PatioTypeFbs.endPatioTypeFbs(builder); - } - - case PatioInt32Type _ -> { - PatioInt32TypeFbs.startPatioInt32TypeFbs(builder); - var int32TypeFbs = PatioInt32TypeFbs.endPatioInt32TypeFbs(builder); - - PatioTypeFbs.startPatioTypeFbs(builder); - PatioTypeFbs.addValueType(builder, PatioTypeValueFbs.PatioInt32TypeFbs); - PatioTypeFbs.addValue(builder, int32TypeFbs); - yield PatioTypeFbs.endPatioTypeFbs(builder); - } - }; - } - - public static PatioType deserializeType(PatioTypeFbs typeFbs) { - return switch (typeFbs.valueType()) { - case PatioTypeValueFbs.PatioBoolTypeFbs -> - PatioBoolType.INSTANCE; - - case PatioTypeValueFbs.PatioByteTypeFbs -> - PatioByteType.INSTANCE; - - case PatioTypeValueFbs.PatioDataPtrTypeFbs -> - PatioDataPtrType.INSTANCE; - - case PatioTypeValueFbs.PatioFunctionPtrTypeFbs -> - PatioFunctionPtrType.INSTANCE; - - case PatioTypeValueFbs.PatioIntTypeFbs -> - PatioIntType.INSTANCE; - - case PatioTypeValueFbs.PatioInt32TypeFbs -> - PatioInt32Type.INSTANCE; - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - // == Data layouts == - - public static int serializeDataLayout( - FlatBufferBuilder builder, - PatioDataLayout layout - ) { - return switch (layout) { - case PatioDataLayoutArray array -> { - var elementFbs = serializeDataLayout(builder, array.element()); - - PatioDataLayoutArrayFbs.startPatioDataLayoutArrayFbs(builder); - PatioDataLayoutArrayFbs.addElement(builder, elementFbs); - PatioDataLayoutArrayFbs.addSize(builder, array.size()); - var arrayFbs = PatioDataLayoutArrayFbs.endPatioDataLayoutArrayFbs(builder); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutArrayFbs); - PatioDataLayoutFbs.addValue(builder, arrayFbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - - case PatioDataLayoutBool _ -> { - PatioDataLayoutBoolFbs.startPatioDataLayoutBoolFbs(builder); - var boolFbs = PatioDataLayoutBoolFbs.endPatioDataLayoutBoolFbs(builder); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutBoolFbs); - PatioDataLayoutFbs.addValue(builder, boolFbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - - case PatioDataLayoutByte _ -> { - PatioDataLayoutByteFbs.startPatioDataLayoutByteFbs(builder); - var byteFbs = PatioDataLayoutByteFbs.endPatioDataLayoutByteFbs(builder); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutByteFbs); - PatioDataLayoutFbs.addValue(builder, byteFbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - - case PatioDataLayoutDataPtr _ -> { - PatioDataLayoutDataPtrFbs.startPatioDataLayoutDataPtrFbs(builder); - var dataPtrFbs = PatioDataLayoutDataPtrFbs.endPatioDataLayoutDataPtrFbs(builder); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutDataPtrFbs); - PatioDataLayoutFbs.addValue(builder, dataPtrFbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - - case PatioDataLayoutFunctionPtr _ -> { - PatioDataLayoutFunctionPtrFbs.startPatioDataLayoutFunctionPtrFbs(builder); - var functionPtrFbs = PatioDataLayoutFunctionPtrFbs.endPatioDataLayoutFunctionPtrFbs(builder); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutFunctionPtrFbs); - PatioDataLayoutFbs.addValue(builder, functionPtrFbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - - case PatioDataLayoutInt _ -> { - PatioDataLayoutIntFbs.startPatioDataLayoutIntFbs(builder); - var intFbs = PatioDataLayoutIntFbs.endPatioDataLayoutIntFbs(builder); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutIntFbs); - PatioDataLayoutFbs.addValue(builder, intFbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - - case PatioDataLayoutInt32 _ -> { - PatioDataLayoutInt32Fbs.startPatioDataLayoutInt32Fbs(builder); - var int32Fbs = PatioDataLayoutInt32Fbs.endPatioDataLayoutInt32Fbs(builder); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutInt32Fbs); - PatioDataLayoutFbs.addValue(builder, int32Fbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - - case PatioDataLayoutStruct struct -> { - var elementsOffsetsFbs = struct.elements().stream() - .mapToInt(element -> serializeDataLayout(builder, element)) - .toArray(); - var elementsVectorFbs = PatioDataLayoutStructFbs.createElementsVector(builder, elementsOffsetsFbs); - - var structFbs = PatioDataLayoutStructFbs.createPatioDataLayoutStructFbs(builder, elementsVectorFbs); - - PatioDataLayoutFbs.startPatioDataLayoutFbs(builder); - PatioDataLayoutFbs.addValueType(builder, PatioDataLayoutValueFbs.PatioDataLayoutStructFbs); - PatioDataLayoutFbs.addValue(builder, structFbs); - yield PatioDataLayoutFbs.endPatioDataLayoutFbs(builder); - } - }; - } - - public static PatioDataLayout deserializeDataLayout(PatioDataLayoutFbs layoutFbs) { - return switch (layoutFbs.valueType()) { - case PatioDataLayoutValueFbs.PatioDataLayoutArrayFbs -> { - var arrayFbs = (PatioDataLayoutArrayFbs) layoutFbs.value(new PatioDataLayoutArrayFbs()); - var element = deserializeDataLayout(arrayFbs.element()); - var size = (int) arrayFbs.size(); - yield new PatioDataLayoutArray(element, size); - } - - case PatioDataLayoutValueFbs.PatioDataLayoutBoolFbs -> - PatioDataLayoutBool.INSTANCE; - - case PatioDataLayoutValueFbs.PatioDataLayoutByteFbs -> - PatioDataLayoutByte.INSTANCE; - - case PatioDataLayoutValueFbs.PatioDataLayoutDataPtrFbs -> - PatioDataLayoutDataPtr.INSTANCE; - - case PatioDataLayoutValueFbs.PatioDataLayoutFunctionPtrFbs -> - PatioDataLayoutFunctionPtr.INSTANCE; - - case PatioDataLayoutValueFbs.PatioDataLayoutIntFbs -> - PatioDataLayoutInt.INSTANCE; - - case PatioDataLayoutValueFbs.PatioDataLayoutInt32Fbs -> - PatioDataLayoutInt32.INSTANCE; - - case PatioDataLayoutValueFbs.PatioDataLayoutStructFbs -> { - var structFbs = (PatioDataLayoutStructFbs) layoutFbs.value(new PatioDataLayoutStructFbs()); - var elements = deserializeVector( - structFbs::elements, - structFbs.elementsLength(), - PatioToFlatbuffers::deserializeDataLayout - ); - yield new PatioDataLayoutStruct(elements); - } - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - // == Data values == - - public static int serializeDataValue(FlatBufferBuilder builder, PatioDataValue value) { - return switch (value) { - case PatioDataValueArray array -> { - var elementLayoutFbs = serializeDataLayout(builder, array.elementLayout()); - - var elementsOffsetsFbs = array.elements().stream() - .mapToInt(element -> serializeDataValue(builder, element)) - .toArray(); - var elementsVectorFbs = PatioDataValueArrayFbs.createElementsVector(builder, elementsOffsetsFbs); - - PatioDataValueArrayFbs.startPatioDataValueArrayFbs(builder); - PatioDataValueArrayFbs.addElementLayout(builder, elementLayoutFbs); - PatioDataValueArrayFbs.addElements(builder, elementsVectorFbs); - var arrayFbs = PatioDataValueArrayFbs.endPatioDataValueArrayFbs(builder); - - PatioDataValueFbs.startPatioDataValueFbs(builder); - PatioDataValueFbs.addValueType(builder, PatioDataValueValueFbs.PatioDataValueArrayFbs); - PatioDataValueFbs.addValue(builder, arrayFbs); - yield PatioDataValueFbs.endPatioDataValueFbs(builder); - } - - case PatioDataValueBool bool -> { - PatioDataValueBoolFbs.startPatioDataValueBoolFbs(builder); - PatioDataValueBoolFbs.addValue(builder, bool.value()); - var boolFbs = PatioDataValueBoolFbs.endPatioDataValueBoolFbs(builder); - - PatioDataValueFbs.startPatioDataValueFbs(builder); - PatioDataValueFbs.addValueType(builder, PatioDataValueValueFbs.PatioDataValueBoolFbs); - PatioDataValueFbs.addValue(builder, boolFbs); - yield PatioDataValueFbs.endPatioDataValueFbs(builder); - } - - case PatioDataValueByte byte_ -> { - PatioDataValueByteFbs.startPatioDataValueByteFbs(builder); - PatioDataValueByteFbs.addValue(builder, byte_.value()); - var byteFbs = PatioDataValueByteFbs.endPatioDataValueByteFbs(builder); - - PatioDataValueFbs.startPatioDataValueFbs(builder); - PatioDataValueFbs.addValueType(builder, PatioDataValueValueFbs.PatioDataValueByteFbs); - PatioDataValueFbs.addValue(builder, byteFbs); - yield PatioDataValueFbs.endPatioDataValueFbs(builder); - } - - case PatioDataValueDataPtr dataPtr -> { - var dataPtrValueFbs = serializeDataPtrValue(builder, dataPtr.value()); - - PatioDataValueDataPtrFbs.startPatioDataValueDataPtrFbs(builder); - PatioDataValueDataPtrFbs.addValue(builder, dataPtrValueFbs); - var dataPtrFbs = PatioDataValueDataPtrFbs.endPatioDataValueDataPtrFbs(builder); - - PatioDataValueFbs.startPatioDataValueFbs(builder); - PatioDataValueFbs.addValueType(builder, PatioDataValueValueFbs.PatioDataValueDataPtrFbs); - PatioDataValueFbs.addValue(builder, dataPtrFbs); - yield PatioDataValueFbs.endPatioDataValueFbs(builder); - } - - case PatioDataValueInt int_ -> { - var intValueFbs = serializeIntValue(builder, int_.value()); - - PatioDataValueIntFbs.startPatioDataValueIntFbs(builder); - PatioDataValueIntFbs.addValue(builder, intValueFbs); - var intFbs = PatioDataValueIntFbs.endPatioDataValueIntFbs(builder); - - PatioDataValueFbs.startPatioDataValueFbs(builder); - PatioDataValueFbs.addValueType(builder, PatioDataValueValueFbs.PatioDataValueIntFbs); - PatioDataValueFbs.addValue(builder, intFbs); - yield PatioDataValueFbs.endPatioDataValueFbs(builder); - } - - case PatioDataValueInt32 int32 -> { - var intValueFbs = serializeIntValue(builder, int32.value()); - - PatioDataValueInt32Fbs.startPatioDataValueInt32Fbs(builder); - PatioDataValueInt32Fbs.addValue(builder, intValueFbs); - var int32Fbs = PatioDataValueInt32Fbs.endPatioDataValueInt32Fbs(builder); - - PatioDataValueFbs.startPatioDataValueFbs(builder); - PatioDataValueFbs.addValueType(builder, PatioDataValueValueFbs.PatioDataValueInt32Fbs); - PatioDataValueFbs.addValue(builder, int32Fbs); - yield PatioDataValueFbs.endPatioDataValueFbs(builder); - } - - case PatioDataValueStruct struct -> { - var elementsOffsetsFbs = struct.elements().stream() - .mapToInt(element -> serializeDataValue(builder, element)) - .toArray(); - var elementsVectorFbs = PatioDataValueStructFbs.createElementsVector(builder, elementsOffsetsFbs); - - PatioDataValueStructFbs.startPatioDataValueStructFbs(builder); - PatioDataValueStructFbs.addElements(builder, elementsVectorFbs); - var structFbs = PatioDataValueStructFbs.endPatioDataValueStructFbs(builder); - - PatioDataValueFbs.startPatioDataValueFbs(builder); - PatioDataValueFbs.addValueType(builder, PatioDataValueValueFbs.PatioDataValueStructFbs); - PatioDataValueFbs.addValue(builder, structFbs); - yield PatioDataValueFbs.endPatioDataValueFbs(builder); - } - }; - } - - public static PatioDataValue deserializeDataValue(PatioDataValueFbs valueFbs) { - return switch (valueFbs.valueType()) { - case PatioDataValueValueFbs.PatioDataValueArrayFbs -> { - var arrayFbs = (PatioDataValueArrayFbs) valueFbs.value(new PatioDataValueArrayFbs()); - - var elementLayout = deserializeDataLayout(arrayFbs.elementLayout()); - - var elements = deserializeVector( - arrayFbs::elements, - arrayFbs.elementsLength(), - PatioToFlatbuffers::deserializeDataValue - ); - - yield new PatioDataValueArray(elementLayout, elements); - } - - case PatioDataValueValueFbs.PatioDataValueBoolFbs -> { - var boolFbs = (PatioDataValueBoolFbs) valueFbs.value(new PatioDataValueBoolFbs()); - - yield new PatioDataValueBool(boolFbs.value()); - } - - case PatioDataValueValueFbs.PatioDataValueByteFbs -> { - var byteFbs = (PatioDataValueByteFbs) valueFbs.value(new PatioDataValueByteFbs()); - - yield new PatioDataValueByte((byte) byteFbs.value()); - } - - case PatioDataValueValueFbs.PatioDataValueDataPtrFbs -> { - var dataPtrFbs = (PatioDataValueDataPtrFbs) valueFbs.value(new PatioDataValueDataPtrFbs()); - - var value = deserializeDataPtrValue(dataPtrFbs.value()); - - yield new PatioDataValueDataPtr(value); - } - - case PatioDataValueValueFbs.PatioDataValueIntFbs -> { - var intFbs = (PatioDataValueIntFbs) valueFbs.value(new PatioDataValueIntFbs()); - - var value = deserializeIntValue(intFbs.value()); - - yield new PatioDataValueInt(value); - } - - case PatioDataValueValueFbs.PatioDataValueInt32Fbs -> { - var int32Fbs = (PatioDataValueInt32Fbs) valueFbs.value(new PatioDataValueInt32Fbs()); - - var value = deserializeIntValue(int32Fbs.value()); - - yield new PatioDataValueInt32(value); - } - - case PatioDataValueValueFbs.PatioDataValueStructFbs -> { - var structFbs = (PatioDataValueStructFbs) valueFbs.value(new PatioDataValueStructFbs()); - - var elements = deserializeVector( - structFbs::elements, - structFbs.elementsLength(), - PatioToFlatbuffers::deserializeDataValue - ); - - yield new PatioDataValueStruct(elements); - } - - default -> - throw new UnsupportedOperationException("TODO"); - }; - } - - // == Flatbuffers == - - 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 = deserializeElement.apply(elementFbs); - result.add(element); - } - return result; - } - - 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); - } - return array; - } - - private static List<Integer> deserializeListOfInts(IntVector vector) { - var result = new ArrayList<Integer>(); - for (var index = 0; index < vector.length(); index++) { - result.add(vector.get(index)); - } - return result; - } -} diff --git a/src/main/java/org/shedlang/compiler/patio/serialization/PatioToTransient0.java b/src/main/java/org/shedlang/compiler/patio/serialization/PatioToTransient0.java new file mode 100644 index 00000000..666100dc --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/serialization/PatioToTransient0.java @@ -0,0 +1,141 @@ +package org.shedlang.compiler.patio.serialization; + +import org.shedlang.compiler.patio.PatioCompilationUnit; +import org.shedlang.compiler.patio.PatioCompilationUnitElement; +import org.shedlang.compiler.patio.instructions.PatioInstruction; +import org.shedlang.compiler.patio.instructions.transient0.HobgoblinTransient0Instructions; +import org.shedlang.compiler.patio.operands.PatioOperand; +import org.shedlang.compiler.patio.operands.transient0.HobgoblinTransient0Operands; +import org.shedlang.compiler.patio.transient0.HobgoblinTransient0Patio; +import org.shedlang.compiler.patio.types.PatioDataLayout; +import org.shedlang.compiler.patio.types.PatioDataValue; +import org.shedlang.compiler.patio.types.PatioType; +import org.shedlang.compiler.patio.types.transient0.HobgoblinTransient0Types; +import org.shedlang.compiler.patio.values.PatioDataPtrValue; +import org.shedlang.compiler.patio.values.PatioIntValue; +import org.shedlang.compiler.patio.values.transient0.HobgoblinTransient0Values; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; + +public class PatioToTransient0 { + public static void serializeCompilationUnit( + PatioCompilationUnit value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Patio.encodePatioCompilationUnit(value, outputStream, new HashMap<>()); + } + + public static PatioCompilationUnit deserializeCompilationUnit( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Patio.decodePatioCompilationUnit(inputStream, new ArrayList<>()); + } + + static void serializeCompilationUnitElement( + PatioCompilationUnitElement value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Patio.encodePatioCompilationUnitElement(value, outputStream, new HashMap<>()); + } + + static PatioCompilationUnitElement deserializeCompilationUnitElement( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Patio.decodePatioCompilationUnitElement(inputStream, new ArrayList<>()); + } + + static void serializeInstruction( + PatioInstruction value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Instructions.encodePatioInstruction(value, outputStream, new HashMap<>()); + } + + static PatioInstruction deserializeInstruction( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Instructions.decodePatioInstruction(inputStream, new ArrayList<>()); + } + + static void serializeOperand( + PatioOperand value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Operands.encodePatioOperand(value, outputStream, new HashMap<>()); + } + + static PatioOperand deserializeOperand( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Operands.decodePatioOperand(inputStream, new ArrayList<>()); + } + + static void serializeDataPtrValue( + PatioDataPtrValue value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Values.encodePatioDataPtrValue(value, outputStream, new HashMap<>()); + } + + static PatioDataPtrValue deserializeDataPtrValue( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Values.decodePatioDataPtrValue(inputStream, new ArrayList<>()); + } + + static void serializeIntValue( + PatioIntValue value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Values.encodePatioIntValue(value, outputStream, new HashMap<>()); + } + + static PatioIntValue deserializeIntValue( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Values.decodePatioIntValue(inputStream, new ArrayList<>()); + } + + static void serializeType( + PatioType value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Types.encodePatioType(value, outputStream, new HashMap<>()); + } + + static PatioType deserializeType( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Types.decodePatioType(inputStream, new ArrayList<>()); + } + + static void serializeDataLayout( + PatioDataLayout value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Types.encodePatioDataLayout(value, outputStream, new HashMap<>()); + } + + static PatioDataLayout deserializeDataLayout( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Types.decodePatioDataLayout(inputStream, new ArrayList<>()); + } + + static void serializeDataValue( + PatioDataValue value, + OutputStream outputStream + ) throws IOException { + HobgoblinTransient0Types.encodePatioDataValue(value, outputStream, new HashMap<>()); + } + + static PatioDataValue deserializeDataValue( + InputStream inputStream + ) throws IOException { + return HobgoblinTransient0Types.decodePatioDataValue(inputStream, new ArrayList<>()); + } +} diff --git a/src/main/java/org/shedlang/compiler/sources/serialization/SourceToFlatbuffers.java b/src/main/java/org/shedlang/compiler/sources/serialization/SourceToFlatbuffers.java deleted file mode 100644 index 7c1bb074..00000000 --- a/src/main/java/org/shedlang/compiler/sources/serialization/SourceToFlatbuffers.java +++ /dev/null @@ -1,141 +0,0 @@ -package org.shedlang.compiler.sources.serialization; - -import com.google.flatbuffers.FlatBufferBuilder; -import org.shedlang.compiler.sources.*; -import org.shedlang.compiler.sources.flatbuffers.*; -import org.zwobble.sourcetext.SourcePosition; -import org.zwobble.sourcetext.SourceRange; -import org.zwobble.sourcetext.SourceText; - -import java.util.Optional; - -public class SourceToFlatbuffers { - private SourceToFlatbuffers() { - } - - public static int serializeSource( - FlatBufferBuilder builder, - Source source, - Source parentSource - ) { - return serializeSource(builder, source, Optional.of(parentSource)); - } - - public static int serializeSource( - FlatBufferBuilder builder, - Source source, - Optional<Source> parentSource - ) { - switch (source) { - case NullSource nullSource -> { - NullSourceFbs.startNullSourceFbs(builder); - var nullSourceFbs = NullSourceFbs.endNullSourceFbs(builder); - - SourceFbs.startSourceFbs(builder); - SourceFbs.addValueType(builder, SourceValueFbs.NullSourceFbs); - SourceFbs.addValue(builder, nullSourceFbs); - return SourceFbs.endSourceFbs(builder); - } - case RuntimeSource runtimeSource -> { - RuntimeSourceFbs.startRuntimeSourceFbs(builder); - var runtimeSourceFbs = RuntimeSourceFbs.endRuntimeSourceFbs(builder); - - SourceFbs.startSourceFbs(builder); - SourceFbs.addValueType(builder, SourceValueFbs.RuntimeSourceFbs); - SourceFbs.addValue(builder, runtimeSourceFbs); - return SourceFbs.endSourceFbs(builder); - } - case FileFragmentSource fileFragmentSource -> { - var fileFragmentSourceFbs = serializeFileFragmentSource(builder, fileFragmentSource, parentSource); - - SourceFbs.startSourceFbs(builder); - SourceFbs.addValueType(builder, SourceValueFbs.FileFragmentSourceFbs); - SourceFbs.addValue( - builder, - fileFragmentSourceFbs - ); - return SourceFbs.endSourceFbs(builder); - } - default -> { - throw new UnsupportedOperationException("Unhandled source: " + source); - } - } - } - - public static Source deserializeSource( - SourceFbs sourceFbs, - Source parentSource - ) { - return deserializeSource(sourceFbs, Optional.of(parentSource)); - } - - public static Source deserializeSource( - SourceFbs sourceFbs, - Optional<Source> parentSource - ) { - return switch (sourceFbs.valueType()) { - case SourceValueFbs.NullSourceFbs -> - Sources.NULL; - - case SourceValueFbs.RuntimeSourceFbs -> - Sources.RUNTIME; - - case SourceValueFbs.FileFragmentSourceFbs -> - deserializeFileFragmentSource( - (FileFragmentSourceFbs) sourceFbs.value(new FileFragmentSourceFbs()), - parentSource - ); - - default -> { - throw new UnsupportedOperationException("TODO"); - } - }; - } - - private static int serializeFileFragmentSource( - FlatBufferBuilder builder, - FileFragmentSource source, - Optional<Source> parentSource - ) { - var sourceTextName = source.sourceRange().sourceText().name(); - if (!( - parentSource.isPresent() && - parentSource.get() instanceof FileFragmentSource( - SourceRange parentSourceRange) && - parentSourceRange.sourceText().name().equals(sourceTextName) - )) { - var sourceTextNameFbs = builder.createString(sourceTextName); - var sourceTextContentsFbs = builder.createString(source.sourceRange().sourceText().charSequence()); - var sourceTextFbs = SourceTextFbs.createSourceTextFbs(builder, sourceTextNameFbs, sourceTextContentsFbs); - FileFragmentSourceFbs.startFileFragmentSourceFbs(builder); - FileFragmentSourceFbs.addSourceText(builder, sourceTextFbs); - } else { - FileFragmentSourceFbs.startFileFragmentSourceFbs(builder); - } - - FileFragmentSourceFbs.addStartCharacterIndex(builder, source.sourceRange().start().characterIndex()); - FileFragmentSourceFbs.addEndCharacterIndex(builder, source.sourceRange().end().characterIndex()); - - return FileFragmentSourceFbs.endFileFragmentSourceFbs(builder); - } - - private static FileFragmentSource deserializeFileFragmentSource( - FileFragmentSourceFbs sourceFbs, - Optional<Source> parentSource - ) { - SourceText sourceText; - if (sourceFbs.sourceText() != null) { - sourceText = SourceText.fromString( - sourceFbs.sourceText().name(), - sourceFbs.sourceText().contents() - ); - } else { - // TODO: handle missing parent - sourceText = ((FileFragmentSource) parentSource.orElseThrow()).sourceRange().sourceText(); - } - - var start = new SourcePosition(sourceText, (int) sourceFbs.startCharacterIndex()); - var end = new SourcePosition(sourceText, (int) sourceFbs.endCharacterIndex()); - return new FileFragmentSource(new SourceRange(sourceText, start, end)); - } -} |
