package org.shedlang.compiler.shidtopatio; import org.shedlang.compiler.patio.PatioDataElementMutableDeclaration; import org.shedlang.compiler.patio.PatioDataElementMutableDefinition; import org.shedlang.compiler.patio.PatioVisibility; import org.shedlang.compiler.patio.instructions.PatioMemoryLoad; import org.shedlang.compiler.patio.instructions.PatioMemoryStore; import org.shedlang.compiler.patio.operands.PatioDataPtrConst; import org.shedlang.compiler.patio.operands.PatioLocalRef; import org.shedlang.compiler.patio.operands.PatioOperand; import org.shedlang.compiler.patio.types.PatioDataLayouts; import org.shedlang.compiler.patio.types.PatioDataValues; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.types.SimpleConcreteType; import java.text.MessageFormat; import java.util.List; import java.util.Optional; import static org.shedlang.compiler.shidtopatio.ShidToPatioNamespaces.namespaceNameToPatioIdentifier; public class ShidToPatioMethods { private ShidToPatioMethods() { } public static ShidToPatioContext methodLoad( PatioLocalRef destination, SimpleConcreteType type, String methodName, Source source, ShidToPatioContext context ) { if (!context.namespaceName().equals(Optional.of(type.namespaceName()))) { context = context.ensureCompilationUnitElement(new PatioDataElementMutableDeclaration( globalNameMethod(type, methodName), PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE), // TODO: set source (need to compare compilation unit elements without considering source) Sources.NULL )); } context = context.addInstruction(new PatioMemoryLoad( destination, globalOperandMethod(type, methodName), PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE), List.of(), source )); return context; } public static ShidToPatioContext methodStore( SimpleConcreteType type, String methodName, PatioOperand methodValue, Source source, ShidToPatioContext context ) { context = context.addCompilationUnitElement(new PatioDataElementMutableDefinition( globalNameMethod(type, methodName), PatioVisibility.PUBLIC, PatioDataValues.int_(0), source )); context = context.addInstruction(new PatioMemoryStore( globalOperandMethod(type, methodName), PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE), List.of(), methodValue, source )); return context; } private static PatioOperand globalOperandMethod(SimpleConcreteType type, String methodName) { return PatioDataPtrConst.dataElement(globalNameMethod(type, methodName)); } private static String globalNameMethod(SimpleConcreteType type, String methodName) { return MessageFormat.format( "shed__method__{0}__{1}__{2}", namespaceNameToPatioIdentifier(type.namespaceName()), type.name(), methodName ); } }