aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java
blob: 1fa9b41d04467a766a183963f153e8d627013902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
        );
    }
}