Skip to content

Commit 07df963

Browse files
HerrCai0907atc-github
authored andcommitted
chore(frontend): split linked API to multiple files (#121)
1 parent 36747a6 commit 07df963

File tree

9 files changed

+1527
-1423
lines changed

9 files changed

+1527
-1423
lines changed

frontend/CompilerImpl.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,16 @@ FrontendCompiler::FrontendCompiler(Config const &config)
271271
if (config.ascWasmPath) [[unlikely]] {
272272
support::PerfRAII const r{support::PerfItemKind::CompilationHIR_PrepareWASMModule};
273273
std::string const wasmBytes = readBinaryFile(*config.ascWasmPath);
274-
m.initFromBytecode(
275-
vb::Span<const uint8_t>{reinterpret_cast<uint8_t const *>(wasmBytes.data()), wasmBytes.size()},
276-
vb::Span<vb::NativeSymbol const>{warpo::frontend::linkedAPI.data(), warpo::frontend::linkedAPI.size()}, true);
274+
m.initFromBytecode(vb::Span<const uint8_t>{reinterpret_cast<uint8_t const *>(wasmBytes.data()), wasmBytes.size()},
275+
vb::Span<vb::NativeSymbol const>{warpo::frontend::getLinkedAPI().data(),
276+
warpo::frontend::getLinkedAPI().size()},
277+
true);
277278
} else {
278279
support::PerfRAII const r{support::PerfItemKind::CompilationHIR_PrepareWASMModule};
279-
static vb::WasmModule::CompileResult const embedJitCode = m.compile(
280-
vb::Span<const uint8_t>{embed_asc_wasm.data(), embed_asc_wasm.size()},
281-
vb::Span<vb::NativeSymbol const>{warpo::frontend::linkedAPI.data(), warpo::frontend::linkedAPI.size()});
280+
static vb::WasmModule::CompileResult const embedJitCode =
281+
m.compile(vb::Span<const uint8_t>{embed_asc_wasm.data(), embed_asc_wasm.size()},
282+
vb::Span<vb::NativeSymbol const>{warpo::frontend::getLinkedAPI().data(),
283+
warpo::frontend::getLinkedAPI().size()});
282284
m.initFromCompiledBinary(
283285
vb::Span<uint8_t const>{embedJitCode.getModule().data(), embedJitCode.getModule().size()},
284286
vb::Span<vb::NativeSymbol const>{},
@@ -365,10 +367,10 @@ warpo::frontend::CompilationResult FrontendCompiler::compile(std::vector<std::st
365367
int32_t const compiled = m.callExportedFunctionWithName<1>(stackTop, "compile", program)[0].i32;
366368
if (checkDiag(program, config.useColorfulDiagMessage))
367369
return {.m = {}, .errorMessage = errorMessage_};
368-
wasm::Module *binaryen_module = reinterpret_cast<wasm::Module *>(
369-
m.callExportedFunctionWithName<1>(stackTop, "getBinaryenModuleRef", compiled)[0].i64);
370+
asModule_.set(BinaryenModule{reinterpret_cast<wasm::Module *>(
371+
m.callExportedFunctionWithName<1>(stackTop, "getBinaryenModuleRef", compiled)[0].i64)});
370372
compileStat.release();
371-
return {.m = AsModule{binaryen_module}, .errorMessage = errorMessage_};
373+
return {.m = std::move(asModule_), .errorMessage = errorMessage_};
372374
} catch (vb::TrapException const &e) {
373375
logger << "Error: " << e.what() << vb::endStatement;
374376
m.printStacktrace(logger);

frontend/CompilerImpl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <string_view>
1515
#include <vector>
1616

17+
#include "warpo/common/AsModule.hpp"
1718
#include "warpo/frontend/Compiler.hpp"
1819
#include "wasm-compiler/src/WasmModule/WasmModule.hpp"
1920
#include "wasm-compiler/src/utils/STDCompilerLogger.hpp"
@@ -29,6 +30,7 @@ class FrontendCompiler {
2930
std::map<std::string, std::filesystem::path> packageRootMap_{};
3031
size_t errorCount_ = 0;
3132
std::string errorMessage_;
33+
AsModule asModule_;
3234

3335
int32_t allocString(std::string_view str);
3436
std::u16string utf8ToUtf16(std::string const &utf8Str);

frontend/LinkedAPI.cpp

Lines changed: 12 additions & 1408 deletions
Large diffs are not rendered by default.

frontend/LinkedAPI.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
namespace warpo::frontend {
88

9-
extern const std::vector<vb::NativeSymbol> linkedAPI;
9+
std::vector<vb::NativeSymbol> createAssemblyscriptAPI();
10+
std::vector<vb::NativeSymbol> createCppWrapperAPI();
11+
std::vector<vb::NativeSymbol> createBinaryenLinkedAPI();
1012

11-
}
13+
std::vector<vb::NativeSymbol> const &getLinkedAPI();
14+
15+
} // namespace warpo::frontend
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <cassert>
2+
#include <cstddef>
3+
#include <cstdint>
4+
#include <cstdlib>
5+
#include <cstring>
6+
#include <fmt/base.h>
7+
#include <iostream>
8+
#include <sstream>
9+
#include <utility>
10+
#include <vector>
11+
12+
#include "LinkedAPI.hpp"
13+
#include "wasm-compiler/src/WasmModule/WasmModule.hpp"
14+
#include "wasm-compiler/src/core/common/NativeSymbol.hpp"
15+
#include "wasm-compiler/src/core/common/function_traits.hpp"
16+
17+
namespace warpo::frontend {
18+
19+
namespace {
20+
21+
std::string getAsString(uint32_t ptr, vb::WasmModule *ctx) {
22+
if (ptr == 0U)
23+
return "<<NULL>>";
24+
uint8_t const *header = ctx->getLinearMemoryRegion(ptr - 20U, 20);
25+
uint32_t size = 0;
26+
std::memcpy(&size, header + 16, sizeof(size));
27+
uint8_t const *content = ctx->getLinearMemoryRegion(ptr, size);
28+
size /= 2U;
29+
30+
std::stringstream ss{};
31+
for (uint32_t i = 0; i < size; ++i) {
32+
ss << content[i * 2U];
33+
}
34+
return std::move(ss).str();
35+
}
36+
37+
void abortForLink(uint32_t messagePtr, uint32_t fileNamePtr, uint32_t lineNumber, uint32_t columnNumber,
38+
vb::WasmModule *ctx) {
39+
std::stringstream ss{};
40+
ss << "abort: " << getAsString(messagePtr, ctx) << " in " << getAsString(fileNamePtr, ctx) << ":" << lineNumber << ":"
41+
<< columnNumber;
42+
std::cerr << std::move(ss).str() << std::endl;
43+
ctx->requestInterruption(vb::TrapCode::BUILTIN_TRAP);
44+
}
45+
46+
void traceForLink(uint32_t ptr, uint32_t n, double d1, double d2, double d3, double d4, double d5,
47+
vb::WasmModule *ctx) {
48+
std::stringstream ss{};
49+
ss << getAsString(ptr, ctx);
50+
for (size_t i = 0; i < n; i++) {
51+
switch (i) {
52+
case 1:
53+
ss << " " << d1;
54+
break;
55+
case 2:
56+
ss << " " << d2;
57+
break;
58+
case 3:
59+
ss << " " << d3;
60+
break;
61+
case 4:
62+
ss << " " << d4;
63+
break;
64+
case 5:
65+
ss << " " << d5;
66+
break;
67+
}
68+
}
69+
std::cout << std::move(ss).str() << std::endl;
70+
}
71+
72+
} // namespace
73+
74+
std::vector<vb::NativeSymbol> createAssemblyscriptAPI() {
75+
return std::vector<vb::NativeSymbol>{
76+
STATIC_LINK("env", "abort", abortForLink),
77+
STATIC_LINK("env", "trace", traceForLink),
78+
};
79+
}
80+
81+
} // namespace warpo::frontend

0 commit comments

Comments
 (0)