aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java
blob: d39cae99c11e80f4632d9a898477ea34c92e235c (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
package org.shedlang.compiler.backends.wasm;

import org.pcollections.PVector;
import org.shedlang.compiler.backends.Backend;
import org.shedlang.compiler.backends.PatioBackends;
import org.shedlang.compiler.backends.paths.PartialTargetPath;
import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.backends.targets.WasmTarget;
import org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormat;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmCall;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmDrop;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule;
import org.shedlang.compiler.config.ProfileConfig;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.patio.PatioCompilationUnit;
import org.shedlang.compiler.runtime.ShedRuntime;
import org.shedlang.compiler.shid.ShidNamespace;
import org.shedlang.compiler.typechecker.TypeInfos;
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;
import java.util.ArrayList;
import java.util.List;

import static org.shedlang.compiler.util.processes.ProcessRunner.runInheritIo;

public class WasmBackend implements Backend {
    public static final WasmBackend WASM32 = new WasmBackend(WasmTarget.WASM32);
    public static final WasmBackend WASM64 = new WasmBackend(WasmTarget.WASM64);
    public static final String EXECUTABLE_EXTENSION = ".wasm";

    public static WasmBackend forTarget(WasmTarget target) {
        return new WasmBackend(target);
    }

    private final WasmTarget target;

    private WasmBackend(WasmTarget target) {
        this.target = target;
    }

    @Override
    public String targetName() {
        return this.target.targetName();
    }

    @Override
    public void compile(
        ProjectConfig config,
        ProfileConfig profileConfig,
        List<ShidNamespace> namespaces,
        TypeInfos typeInfos,
        TargetDir targetDir
    ) throws IOException, InterruptedException {
        PatioBackends.compile(
            config,
            profileConfig,
            namespaces,
            typeInfos,
            new PatioToWasmBinary(targetDir, P.vector())
        );
    }

    private class PatioToWasmBinary implements PatioBackends.PatioToTarget {
        private final TargetDir targetDir;
        private final PVector<Path> objectFilePaths;

        public PatioToWasmBinary(TargetDir targetDir, PVector<Path> objectFilePaths) {
            this.targetDir = targetDir;
            this.objectFilePaths = objectFilePaths;
        }

        @Override
        public PatioBackends.PatioToTarget namespace(
            NamespaceName namespaceName,
            PatioCompilationUnit patioCompilationUnit
        ) throws IOException {
            var wasmModule = PatioToWasm.generateCompilationUnit(
                patioCompilationUnit,
                target
            );

            var objectPath = writeObjectFile(
                wasmModule,
                this.targetDir.namespace(namespaceName)
            );

            return new PatioToWasmBinary(
                this.targetDir,
                this.objectFilePaths.plus(objectPath)
            );
        }

        @Override
        public void bin(
            String binName,
            PatioCompilationUnit patioCompilationUnit,
            String mainFunctionName,
            ProfileConfig profileConfig
        ) throws IOException, InterruptedException {
            var linkInputs = new ArrayList<>(objectFilePaths);

            var mainModule = generateMain(patioCompilationUnit, mainFunctionName);
            var mainObjectPath = writeObjectFile(mainModule, targetDir.bin(binName));
            linkInputs.add(mainObjectPath);

            var shedRuntime = ShedRuntime.runtime(
                target.targetName(),
                profileConfig.gc(),
                targetDir
            );

            var outputBinPath = targetDir.topLevel(binName + EXECUTABLE_EXTENSION);
            link(
                linkInputs,
                outputBinPath,
                shedRuntime
            );
            validate(outputBinPath);
        }
    }

    public void link(
        List<Path> inputs,
        Path outputPath,
        ShedRuntime shedRuntime
    ) throws ProcessError, InterruptedException {
        var ldArgs = new ArrayList<String>();
        ldArgs.add("wasm-ld-23");

        ldArgs.addAll(this.target.linkerFlags());

        ldArgs.add("-o");
        ldArgs.add(outputPath.toString());

        for (var input : inputs) {
            ldArgs.add(input.toString());
        }

        ldArgs.add(shedRuntime.dir().resolve("libpatio.a").toString());
        ldArgs.add(shedRuntime.dir().resolve("libshed.a").toString());
        ldArgs.add(shedRuntime.dir().resolve("libwalloc.a").toString());

        runInheritIo(ldArgs);
    }

    private WasmModule generateMain(
        PatioCompilationUnit patioCompilationUnit,
        String mainFunctionName
    ) {
        var wasmModule = PatioToWasm.generateCompilationUnit(
            patioCompilationUnit,
            this.target
        );

        wasmModule = generateNativeRuntime(wasmModule);

        wasmModule = wasmModule.addFunction(new WasmFunction(
            "_start",
            List.of(),
            List.of(),
            List.of(),
            new WasmExpression(List.of(
                new WasmCall(new WasmFuncName(mainFunctionName)),
                new WasmDrop()
            ))
        ));

        return wasmModule;
    }

    /**
     * Generates the parts of the runtime that are written in native WASM.
     */
    public static WasmModule generateNativeRuntime(WasmModule wasmModule) {
        return wasmModule;
    }

    private static Path writeObjectFile(
        WasmModule module,
        PartialTargetPath targetPath
    ) throws IOException {
        var objectPath = targetPath.withExtension(".o");
        Files.createDirectories(objectPath.getParent());
        try (var outputStream = new FileOutputStream(objectPath.toFile())) {
            WasmBinaryFormat.serializeModule(module, outputStream);
        }
        return objectPath;
    }

    private void validate(Path path) throws IOException, InterruptedException {
        var command = P.vector("wasm-validate", "--enable-exceptions")
            .plusAll(this.target.wabtFlags())
            .plus(path.toString());

        runInheritIo(command);
    }
}