Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,10 @@ if (WABT_INSTALL_RULES)
endif ()

if (HAVE_SETJMP_H)
set(WASM_RT_FILES "wasm2c/wasm-rt-impl.h" "wasm2c/wasm-rt-impl.c" "wasm2c/wasm-rt-exceptions-impl.c")
set(WASM_RT_FILES "wasm2c/wasm-rt-impl.h" "wasm2c/wasm-rt-impl.c" "wasm2c/wasm-rt-exceptions-impl.c" "wasm2c/wasm-rt-mem-impl.c")

add_library(wasm-rt-impl STATIC ${WASM_RT_FILES})
target_link_libraries(wasm-rt-impl ${CMAKE_THREAD_LIBS_INIT})
add_library(wabt::wasm-rt-impl ALIAS wasm-rt-impl)
if (WABT_BIG_ENDIAN)
target_compile_definitions(wasm-rt-impl PUBLIC WABT_BIG_ENDIAN=1)
Expand Down
6 changes: 6 additions & 0 deletions src/binary-reader-ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ Result BinaryReaderIR::OnImportMemory(Index import_index,
import->module_name = module_name;
import->field_name = field_name;
import->memory.page_limits = *page_limits;
if (import->memory.page_limits.is_shared) {
module_->features_used.threads = true;
}
module_->AppendField(
std::make_unique<ImportModuleField>(std::move(import), GetLocation()));
return Result::Ok;
Expand Down Expand Up @@ -697,6 +700,9 @@ Result BinaryReaderIR::OnMemory(Index index, const Limits* page_limits) {
auto field = std::make_unique<MemoryModuleField>(GetLocation());
Memory& memory = field->memory;
memory.page_limits = *page_limits;
if (memory.page_limits.is_shared) {
module_->features_used.threads = true;
}
module_->AppendField(std::move(field));
return Result::Ok;
}
Expand Down
60 changes: 41 additions & 19 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ class CWriter {
void WriteGlobal(const Global&, const std::string&);
void WriteGlobalPtr(const Global&, const std::string&);
void WriteMemories();
void WriteMemory(const std::string&);
void WriteMemoryPtr(const std::string&);
void WriteMemory(const std::string&, const Memory& memory);
void WriteMemoryPtr(const std::string&, const Memory& memory);
void WriteTables();
void WriteTable(const std::string&, const wabt::Type&);
void WriteTablePtr(const std::string&, const Table&);
Expand Down Expand Up @@ -1328,6 +1328,15 @@ void CWriter::WriteGetFuncTypeDecl() {
Newline());
}

static std::string GetMemoryTypeString(const Memory& memory) {
return memory.page_limits.is_shared ? "wasm_rt_shared_memory_t"
: "wasm_rt_memory_t";
}

static std::string GetMemoryAPIString(const Memory& memory, std::string api) {
return memory.page_limits.is_shared ? (api + "_shared") : api;
}

void CWriter::WriteInitExpr(const ExprList& expr_list) {
if (expr_list.empty()) {
WABT_UNREACHABLE;
Expand Down Expand Up @@ -1736,7 +1745,7 @@ void CWriter::BeginInstance() {
}

case ExternalKind::Memory: {
Write("wasm_rt_memory_t");
Write(GetMemoryTypeString(cast<MemoryImport>(import)->memory));
break;
}

Expand Down Expand Up @@ -1779,7 +1788,8 @@ void CWriter::BeginInstance() {

case ExternalKind::Memory:
WriteMemory(std::string("*") +
ExportName(import->module_name, import->field_name));
ExportName(import->module_name, import->field_name),
cast<MemoryImport>(import)->memory);
break;

case ExternalKind::Table: {
Expand Down Expand Up @@ -2027,19 +2037,20 @@ void CWriter::WriteMemories() {
bool is_import = memory_index < module_->num_memory_imports;
if (!is_import) {
WriteMemory(
DefineInstanceMemberName(ModuleFieldType::Memory, memory->name));
DefineInstanceMemberName(ModuleFieldType::Memory, memory->name),
*memory);
Write(Newline());
}
++memory_index;
}
}

void CWriter::WriteMemory(const std::string& name) {
Write("wasm_rt_memory_t ", name, ";");
void CWriter::WriteMemory(const std::string& name, const Memory& memory) {
Write(GetMemoryTypeString(memory), " ", name, ";");
}

void CWriter::WriteMemoryPtr(const std::string& name) {
Write("wasm_rt_memory_t* ", name, "(", ModuleInstanceTypeName(),
void CWriter::WriteMemoryPtr(const std::string& name, const Memory& memory) {
Write(GetMemoryTypeString(memory), "* ", name, "(", ModuleInstanceTypeName(),
"* instance)");
}

Expand Down Expand Up @@ -2169,7 +2180,8 @@ void CWriter::WriteDataInitializers() {
max = memory->page_limits.is_64 ? (static_cast<uint64_t>(1) << 48)
: 65536;
}
Write("wasm_rt_allocate_memory(",
std::string func = GetMemoryAPIString(*memory, "wasm_rt_allocate_memory");
Write(func, "(",
ExternalInstancePtr(ModuleFieldType::Memory, memory->name), ", ",
memory->page_limits.initial, ", ", max, ", ",
memory->page_limits.is_64, ");", Newline());
Expand Down Expand Up @@ -2444,7 +2456,7 @@ void CWriter::WriteExports(CWriterPhase kind) {
case ExternalKind::Memory: {
const Memory* memory = module_->GetMemory(export_->var);
internal_name = memory->name;
WriteMemoryPtr(mangled_name);
WriteMemoryPtr(mangled_name, *memory);
break;
}

Expand Down Expand Up @@ -2754,7 +2766,8 @@ void CWriter::WriteFree() {
for (const Memory* memory : module_->memories) {
bool is_import = memory_index < module_->num_memory_imports;
if (!is_import) {
Write("wasm_rt_free_memory(",
std::string func = GetMemoryAPIString(*memory, "wasm_rt_free_memory");
Write(func, "(",
ExternalInstancePtr(ModuleFieldType::Memory, memory->name), ");",
Newline());
}
Expand Down Expand Up @@ -3706,7 +3719,8 @@ void CWriter::Write(const ExprList& exprs) {
Memory* memory = module_->memories[module_->GetMemoryIndex(
cast<MemoryGrowExpr>(&expr)->memidx)];

Write(StackVar(0), " = wasm_rt_grow_memory(",
std::string func = GetMemoryAPIString(*memory, "wasm_rt_grow_memory");
Write(StackVar(0), " = ", func, "(",
ExternalInstancePtr(ModuleFieldType::Memory, memory->name), ", ",
StackVar(0), ");", Newline());
break;
Expand Down Expand Up @@ -4923,7 +4937,7 @@ void CWriter::Write(const ConvertExpr& expr) {
}

void CWriter::Write(const LoadExpr& expr) {
const char* func = nullptr;
std::string func;
// clang-format off
switch (expr.opcode) {
case Opcode::I32Load: func = "i32_load"; break;
Expand Down Expand Up @@ -4954,6 +4968,7 @@ void CWriter::Write(const LoadExpr& expr) {
// clang-format on

Memory* memory = module_->memories[module_->GetMemoryIndex(expr.memidx)];
func = GetMemoryAPIString(*memory, func);

Type result_type = expr.opcode.GetResultType();
Write(StackVar(0, result_type), " = ", func, "(",
Expand All @@ -4967,7 +4982,7 @@ void CWriter::Write(const LoadExpr& expr) {
}

void CWriter::Write(const StoreExpr& expr) {
const char* func = nullptr;
std::string func;
// clang-format off
switch (expr.opcode) {
case Opcode::I32Store: func = "i32_store"; break;
Expand All @@ -4987,6 +5002,7 @@ void CWriter::Write(const StoreExpr& expr) {
// clang-format on

Memory* memory = module_->memories[module_->GetMemoryIndex(expr.memidx)];
func = GetMemoryAPIString(*memory, func);

Write(func, "(", ExternalInstancePtr(ModuleFieldType::Memory, memory->name),
", (u64)(", StackVar(1), ")");
Expand Down Expand Up @@ -5560,7 +5576,7 @@ void CWriter::Write(const LoadZeroExpr& expr) {
}

void CWriter::Write(const AtomicLoadExpr& expr) {
const char* func = nullptr;
std::string func;
// clang-format off
switch (expr.opcode) {
case Opcode::I32AtomicLoad: func = "i32_atomic_load"; break;
Expand All @@ -5577,6 +5593,7 @@ void CWriter::Write(const AtomicLoadExpr& expr) {
// clang-format on

Memory* memory = module_->memories[module_->GetMemoryIndex(expr.memidx)];
func = GetMemoryAPIString(*memory, func);

Type result_type = expr.opcode.GetResultType();
Write(StackVar(0, result_type), " = ", func, "(",
Expand All @@ -5590,7 +5607,7 @@ void CWriter::Write(const AtomicLoadExpr& expr) {
}

void CWriter::Write(const AtomicStoreExpr& expr) {
const char* func = nullptr;
std::string func;
// clang-format off
switch (expr.opcode) {
case Opcode::I32AtomicStore: func = "i32_atomic_store"; break;
Expand All @@ -5607,6 +5624,7 @@ void CWriter::Write(const AtomicStoreExpr& expr) {
// clang-format on

Memory* memory = module_->memories[module_->GetMemoryIndex(expr.memidx)];
func = GetMemoryAPIString(*memory, func);

Write(func, "(", ExternalInstancePtr(ModuleFieldType::Memory, memory->name),
", (u64)(", StackVar(1), ")");
Expand All @@ -5617,7 +5635,7 @@ void CWriter::Write(const AtomicStoreExpr& expr) {
}

void CWriter::Write(const AtomicRmwExpr& expr) {
const char* func = nullptr;
std::string func;
// clang-format off
switch (expr.opcode) {
case Opcode::I32AtomicRmwAdd: func = "i32_atomic_rmw_add"; break;
Expand Down Expand Up @@ -5668,6 +5686,8 @@ void CWriter::Write(const AtomicRmwExpr& expr) {
// clang-format on

Memory* memory = module_->memories[module_->GetMemoryIndex(expr.memidx)];
func = GetMemoryAPIString(*memory, func);

Type result_type = expr.opcode.GetResultType();

Write(StackVar(1, result_type), " = ", func, "(",
Expand All @@ -5681,7 +5701,7 @@ void CWriter::Write(const AtomicRmwExpr& expr) {
}

void CWriter::Write(const AtomicRmwCmpxchgExpr& expr) {
const char* func = nullptr;
std::string func;
// clang-format off
switch(expr.opcode) {
case Opcode::I32AtomicRmwCmpxchg: func = "i32_atomic_rmw_cmpxchg"; break;
Expand All @@ -5697,6 +5717,8 @@ void CWriter::Write(const AtomicRmwCmpxchgExpr& expr) {
// clang-format on

Memory* memory = module_->memories[module_->GetMemoryIndex(expr.memidx)];
func = GetMemoryAPIString(*memory, func);

Type result_type = expr.opcode.GetResultType();

Write(StackVar(2, result_type), " = ", func, "(",
Expand Down
Loading