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
|
package org.shedlang.compiler.backends.wasm;
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.WasmExpression;
import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmInstruction;
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.binary.WasmBinaryFormatTesting;
import org.shedlang.compiler.patio.PatioCompilationUnit;
import org.shedlang.compiler.patio.PatioExampleTests;
import org.shedlang.compiler.runtime.ShedRuntime;
import org.shedlang.compiler.testing.ExecutionResult;
import org.shedlang.compiler.testing.TestProcessRunner;
import org.shedlang.compiler.util.Lists;
import org.shedlang.compiler.util.P;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public abstract class PatioToWasmTests extends PatioExampleTests {
@Override
protected ExecutionResult executeCompilationUnit(PatioCompilationUnit patio) throws IOException, InterruptedException {
var wasmModule = PatioToWasm.generateCompilationUnit(patio, wasmTarget());
wasmModule = WasmBackend.generateNativeRuntime(wasmModule);
var body = P.<WasmInstruction>vector(new WasmCall(new WasmFuncName(FUNCTION_NAME)));
wasmModule = wasmModule.addFunction(new WasmFunction(
"_start",
List.of(),
List.of(),
List.of(),
new WasmExpression(body)
));
Files.createDirectories(Path.of("tests-tmp"));
var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "PatioToWasmTests");
var objectPath = workingDirectory.resolve("main.o");
try (var outputStream = new FileOutputStream(objectPath.toFile())) {
WasmBinaryFormat.serializeModule(wasmModule, outputStream);
}
var executablePath = workingDirectory.resolve("main.wasm");
var runtime = ShedRuntime.defaultRuntime(wasmTarget().targetName());
WasmBackend.forTarget(wasmTarget()).link(
List.of(
objectPath,
runtime.dir().resolve("libpatio_test.a")
),
executablePath,
ShedRuntime.defaultRuntime(wasmTarget().targetName())
);
WasmBinaryFormatTesting.validate(executablePath, wasmTarget());
return TestProcessRunner.runCaptureOutput(Lists.concat(
WasmTesting.patioTestBinaryRunner(wasmTarget()),
List.of(
executablePath.toString()
)
));
}
protected abstract WasmTarget wasmTarget();
}
|