aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioVars.java
blob: 6aa1df90a812fc372c0746a4e3621c371fb73e72 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.shedlang.compiler.shidtopatio;

import org.shedlang.compiler.patio.instructions.PatioMemoryLoad;
import org.shedlang.compiler.patio.instructions.PatioMemoryStore;
import org.shedlang.compiler.patio.operands.PatioLocalRef;
import org.shedlang.compiler.patio.operands.PatioOperand;
import org.shedlang.compiler.patio.types.PatioDataLayout;
import org.shedlang.compiler.patio.types.PatioDataLayouts;
import org.shedlang.compiler.patio.types.PatioTypes;
import org.shedlang.compiler.sources.Source;

import java.util.List;

public class ShidToPatioVars {
    private ShidToPatioVars() {
    }

    private static class VarRuntimeType implements RuntimeType {
        private static final PatioDataLayout LAYOUT =
            ShidToPatioGc.gcObjectDataLayout(
                PatioDataLayouts.struct(List.of(
                    PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE)
                ))
            );

        @Override
        public PatioDataLayout layout() {
            return LAYOUT;
        }

        private static final List<Integer> GC_CHILDREN_OFFSET =
            ShidToPatioGc.gcObjectOffset(List.of());

        @Override
        public List<Integer> gcChildrenOffset() {
            return GC_CHILDREN_OFFSET;
        }

        @Override
        public int gcChildrenCount() {
            return 1;
        }

        private static final List<Integer> VALUE_OFFSET = ShidToPatioGc.gcObjectOffset(List.of(0));

        public List<Integer> valueOffset() {
            return VALUE_OFFSET;
        }
    }

    private static final VarRuntimeType VAR_RUNTIME_TYPE = new VarRuntimeType();

    public static ShidToPatioContext alloc(
        String shidName,
        Source source,
        ShidToPatioContext context
    ) {
        var patioLocalResult = context.declareShidVal(
            shidName,
            PatioTypes.DATA_PTR
        );
        var patioLocal = patioLocalResult.value();
        context = patioLocalResult.context();

        context = ShidToPatioGc.alloc(
            patioLocal,
            VAR_RUNTIME_TYPE,
            source,
            context
        );

        context = context.initShidLocal(shidName);

        return context;
    }

    public static ShidToPatioContext store(
        String shidName,
        PatioOperand value,
        Source source,
        ShidToPatioContext context
    ) {
        return context.addInstruction(new PatioMemoryStore(
            context.shidLocalNameToLocal(shidName),
            VAR_RUNTIME_TYPE.layout(),
            VAR_RUNTIME_TYPE.valueOffset(),
            value,
            source
        ));
    }

    public static ShidToPatioContext load(
        PatioLocalRef destination,
        String shidName,
        Source source,
        ShidToPatioContext context
    ) {
        return context.addInstruction(new PatioMemoryLoad(
            destination,
            context.shidLocalNameToLocal(shidName),
            VAR_RUNTIME_TYPE.layout(),
            VAR_RUNTIME_TYPE.valueOffset(),
            source
        ));
    }
}