aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
blob: a913c286bf0a3d81d7fe4fb298024a032d9b493f (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package org.shedlang.compiler.shidtopatio;

import org.shedlang.compiler.patio.PatioFunctionDeclaration;
import org.shedlang.compiler.patio.PatioLocal;
import org.shedlang.compiler.patio.instructions.PatioCall;
import org.shedlang.compiler.patio.instructions.PatioMemoryStore;
import org.shedlang.compiler.patio.operands.*;
import org.shedlang.compiler.patio.types.*;
import org.shedlang.compiler.patio.values.PatioDataPtrValueNull;
import org.shedlang.compiler.patio.values.PatioIntValueByteSizeOf;
import org.shedlang.compiler.sources.Source;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.util.Lists;

import java.util.List;

public class ShidToPatioGc {
    private ShidToPatioGc() {
    }

    public static final PatioType RUNTIME_TYPE_TAG_TYPE = PatioTypes.INT32;

    public static PatioDataLayout gcObjectDataLayout(PatioDataLayout valueLayout) {
        return PatioDataLayouts.struct(List.of(
            PatioDataLayouts.struct(List.of(
                PatioDataLayouts.DATA_PTR, // next
                PatioDataLayouts.INT32, // mark
                PatioDataLayouts.fromType(RUNTIME_TYPE_TAG_TYPE) // runtime type tag
            )),
            valueLayout
        ));
    }

    public static List<Integer> runtimeTypeTagOffset() {
        return List.of(0, 2);
    }

    public static List<Integer> gcObjectOffset(List<Integer> valueOffset) {
        return Lists.concat(List.of(1), valueOffset);
    }

    /**
     * Creates a data value for GC objects with no GC children that should never
     * be garbage collected. Such a value does not need to be added to the set
     * of GC roots.
     */
    public static PatioDataValue gcObjectStaticDataValue(PatioDataValue value) {
        return PatioDataValues.struct(List.of(
            PatioDataValues.struct(List.of(
                PatioDataValues.int_(0),
                // Static values are always marked: see gc.c
                PatioDataValues.int32(0b110),
                PatioDataValues.int32(0)
            )),
            value
        ));
    }

    // == Roots ==

    public static final PatioType GC_ROOT_STACK_INDEX_TYPE = PatioTypes.INT;

    private static final PatioFunctionDeclaration GET_GC_ROOT_STACK_INDEX = new PatioFunctionDeclaration(
        "shed__runtime__gc_get_root_stack_index",
        List.of(),
        List.of(GC_ROOT_STACK_INDEX_TYPE),
        Sources.RUNTIME
    );

    public static ShidToPatioContext getGcRootStackIndex(
        PatioLocalRef destination,
        Source source,
        ShidToPatioContext context
    ) {
        return context
            .ensureCompilationUnitElement(GET_GC_ROOT_STACK_INDEX)
            .addInstruction(new PatioCall(
                List.of(destination),
                GET_GC_ROOT_STACK_INDEX.name(),
                List.of(),
                source
            ));
    }

    private static final PatioFunctionDeclaration SET_GC_ROOT_STACK_INDEX = new PatioFunctionDeclaration(
        "shed__runtime__gc_set_root_stack_index",
        List.of(PatioLocal.param("gc_root_stack_index", GC_ROOT_STACK_INDEX_TYPE)),
        List.of(),
        Sources.RUNTIME
    );

    public static ShidToPatioContext setGcRootStackIndex(
        PatioOperand gcRootIndex,
        Source source,
        ShidToPatioContext context
    ) {
        return context
            .ensureCompilationUnitElement(SET_GC_ROOT_STACK_INDEX)
            .addInstruction(new PatioCall(
                List.of(),
                SET_GC_ROOT_STACK_INDEX.name(),
                List.of(gcRootIndex),
                source
            ));
    }

    private static final PatioFunctionDeclaration PUSH_GC_ROOT = new PatioFunctionDeclaration(
        "shed__runtime__gc_push_root",
        List.of(PatioLocal.param("gc_root", ShidToPatioTypes.VALUE_TYPE)),
        List.of(),
        Sources.RUNTIME
    );

    public static ShidToPatioContext pushGcRoot(
        PatioOperand escapee,
        Source source,
        ShidToPatioContext context
    ) {
        return context
            .ensureCompilationUnitElement(PUSH_GC_ROOT)
            .addInstruction(new PatioCall(
                List.of(),
                PUSH_GC_ROOT.name(),
                List.of(escapee),
                source
            ));
    }

    // == Initialization ==

    private static final PatioFunctionDeclaration INIT = new PatioFunctionDeclaration(
        "shed__runtime__gc_init",
        List.of(),
        List.of(),
        Sources.RUNTIME
    );

    public static ShidToPatioContext init(
        Source source,
        ShidToPatioContext context
    ) {
        return context
            .ensureCompilationUnitElement(INIT)
            .addInstruction(new PatioCall(
                List.of(),
                INIT.name(),
                List.of(),
                source
            ));
    }

    // == Allocation ==

    private static final PatioFunctionDeclaration ALLOC = new PatioFunctionDeclaration(
        "shed__runtime__gc_alloc",
        List.of(
            PatioLocal.param("size", PatioTypes.INT),
            PatioLocal.param("runtime_type_tag", PatioTypes.INT32)
        ),
        List.of(PatioTypes.DATA_PTR),
        Sources.RUNTIME
    );

    public static ShidToPatioContext alloc(
        PatioLocalRef destination,
        RuntimeType runtimeType,
        Source source,
        ShidToPatioContext context
    ) {
        var layout = runtimeType.layout();
        var childrenOffset = runtimeType.gcChildrenOffset();
        var childrenCount = runtimeType.gcChildrenCount();

        var runtimeTypeTagResult = context.runtimeTypeTag(runtimeType);
        var runtimeTypeTag = runtimeTypeTagResult.value();
        context = runtimeTypeTagResult.context();

        context = context.ensureCompilationUnitElement(ALLOC);

        var size = new PatioIntConst(new PatioIntValueByteSizeOf(layout));

        context = context.addInstruction(new PatioCall(
            List.of(destination),
            ALLOC.name(),
            List.of(size, PatioInt32Const.of(runtimeTypeTag)),
            source
        ));

        for (var childIndex = 0; childIndex < childrenCount; childIndex++) {
            var childOffset = Lists.concat(childrenOffset, List.of(childIndex));
            var childDataLayout = PatioOffsets.offsetDataLayout(layout, childOffset);

            var zero = switch (childDataLayout) {
                case PatioDataLayoutDataPtr patioDataLayoutDataPtr ->
                    new PatioDataPtrConst(new PatioDataPtrValueNull());

                case PatioDataLayoutInt dataLayoutInt ->
                    PatioIntConst.of(0);

                default ->
                    // TODO: better error
                    throw new IllegalArgumentException("unsupported child");
            };

            context = context.addInstruction(new PatioMemoryStore(
                destination,
                layout,
                childOffset,
                zero,
                source
            ));
        }

        return context;
    }

    // == Collection  ==

    private static final PatioFunctionDeclaration COLLECT = new PatioFunctionDeclaration(
        "shed__runtime__gc_collect",
        List.of(),
        List.of(),
        Sources.RUNTIME
    );

    public static ShidToPatioContext collect(
        Source source,
        ShidToPatioContext context
    ) {
        return context
            .ensureCompilationUnitElement(COLLECT)
            .addInstruction(new PatioCall(
                List.of(),
                COLLECT.name(),
                List.of(),
                source
            ));
    }

    // == Save/Restore ==

    static ShidToPatioContext saveGcRootStack(Source source, ShidToPatioContext context) {
        var gcRootStackIndexResult = context.allocateVal(
            ShidToPatioLocalSpace.GC_ROOT_STACK_INDEX,
            ShidToPatioGc.GC_ROOT_STACK_INDEX_TYPE
        );
        var gcRootStackIndex = gcRootStackIndexResult.value();
        context = gcRootStackIndexResult.context();

        context = ShidToPatioGc.getGcRootStackIndex(
            gcRootStackIndex,
            source,
            context
        );

        context = context.stashGcRootStackIndex(gcRootStackIndex);

        return context;
    }

    static ShidToPatioContext restoreGcRootStack(
        List<PatioOperand> escapees,
        Source source,
        ShidToPatioContext context
    ) {
        context = ShidToPatioGc.setGcRootStackIndex(
            context.stashedGcRootStackIndex(),
            source,
            context
        );

        for (var escapee : escapees) {
            context = ShidToPatioGc.pushGcRoot(escapee, source, context);
        }

        return context;
    }

    // == Clear ==

    private static final PatioFunctionDeclaration CLEAR = new PatioFunctionDeclaration(
        "shed__runtime__gc_clear",
        List.of(),
        List.of(),
        Sources.RUNTIME
    );

    public static ShidToPatioContext clear(
        Source source,
        ShidToPatioContext context
    ) {
        return context
            .ensureCompilationUnitElement(CLEAR)
            .addInstruction(new PatioCall(
                List.of(),
                CLEAR.name(),
                List.of(),
                source
            ));
    }
}