diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 7901f46d33b..4639956c8b1 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -82,6 +82,9 @@ void traceNameOrNULL(const char *name) { std::map functionTypes; std::map expressions; std::map functions; +std::map globals; +std::map imports; +std::map exports; std::map relooperBlocks; size_t noteExpression(BinaryenExpressionRef expression) { @@ -91,6 +94,27 @@ size_t noteExpression(BinaryenExpressionRef expression) { return id; } +size_t noteGlobal(BinaryenGlobalRef global) { + auto id = globals.size(); + assert(globals.find(global) == globals.end()); + globals[global] = id; + return id; +} + +size_t noteImport(BinaryenImportRef import) { + auto id = imports.size(); + assert(imports.find(import) == imports.end()); + imports[import] = id; + return id; +} + +size_t noteExport(BinaryenExportRef export_) { + auto id = exports.size(); + assert(exports.find(export_) == exports.end()); + exports[export_] = id; + return id; +} + extern "C" { // @@ -106,6 +130,37 @@ BinaryenType BinaryenFloat32(void) { return f32; } BinaryenType BinaryenFloat64(void) { return f64; } BinaryenType BinaryenUndefined(void) { return uint32_t(-1); } +// Expression ids + +BinaryenExpressionId BinaryenInvalidId(void) { return Expression::Id::InvalidId; } +BinaryenExpressionId BinaryenBlockId(void) { return Expression::Id::BlockId; } +BinaryenExpressionId BinaryenIfId(void) { return Expression::Id::IfId; } +BinaryenExpressionId BinaryenLoopId(void) { return Expression::Id::LoopId; } +BinaryenExpressionId BinaryenBreakId(void) { return Expression::Id::BreakId; } +BinaryenExpressionId BinaryenSwitchId(void) { return Expression::Id::SwitchId; } +BinaryenExpressionId BinaryenCallId(void) { return Expression::Id::CallId; } +BinaryenExpressionId BinaryenCallImportId(void) { return Expression::Id::CallImportId; } +BinaryenExpressionId BinaryenCallIndirectId(void) { return Expression::Id::CallIndirectId; } +BinaryenExpressionId BinaryenGetLocalId(void) { return Expression::Id::GetLocalId; } +BinaryenExpressionId BinaryenSetLocalId(void) { return Expression::Id::SetLocalId; } +BinaryenExpressionId BinaryenGetGlobalId(void) { return Expression::Id::GetGlobalId; } +BinaryenExpressionId BinaryenSetGlobalId(void) { return Expression::Id::SetGlobalId; } +BinaryenExpressionId BinaryenLoadId(void) { return Expression::Id::LoadId; } +BinaryenExpressionId BinaryenStoreId(void) { return Expression::Id::StoreId; } +BinaryenExpressionId BinaryenConstId(void) { return Expression::Id::ConstId; } +BinaryenExpressionId BinaryenUnaryId(void) { return Expression::Id::UnaryId; } +BinaryenExpressionId BinaryenBinaryId(void) { return Expression::Id::BinaryId; } +BinaryenExpressionId BinaryenSelectId(void) { return Expression::Id::SelectId; } +BinaryenExpressionId BinaryenDropId(void) { return Expression::Id::DropId; } +BinaryenExpressionId BinaryenReturnId(void) { return Expression::Id::ReturnId; } +BinaryenExpressionId BinaryenHostId(void) { return Expression::Id::HostId; } +BinaryenExpressionId BinaryenNopId(void) { return Expression::Id::NopId; } +BinaryenExpressionId BinaryenUnreachableId(void) { return Expression::Id::UnreachableId; } +BinaryenExpressionId BinaryenAtomicCmpxchgId(void) { return Expression::Id::AtomicCmpxchgId; } +BinaryenExpressionId BinaryenAtomicRMWId(void) { return Expression::Id::AtomicRMWId; } +BinaryenExpressionId BinaryenAtomicWaitId(void) { return Expression::Id::AtomicWaitId; } +BinaryenExpressionId BinaryenAtomicWakeId(void) { return Expression::Id::AtomicWakeId; } + // Modules BinaryenModuleRef BinaryenModuleCreate(void) { @@ -123,10 +178,16 @@ void BinaryenModuleDispose(BinaryenModuleRef module) { std::cout << " functionTypes.clear();\n"; std::cout << " expressions.clear();\n"; std::cout << " functions.clear();\n"; + std::cout << " globals.clear();\n"; + std::cout << " imports.clear();\n"; + std::cout << " exports.clear();\n"; std::cout << " relooperBlocks.clear();\n"; functionTypes.clear(); expressions.clear(); functions.clear(); + globals.clear(); + imports.clear(); + exports.clear(); relooperBlocks.clear(); } @@ -172,6 +233,54 @@ BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const return ret; } +BinaryenFunctionTypeRef BinaryenGetFunctionTypeAt(BinaryenModuleRef module, BinaryenIndex index) { + if (tracing) { + std::cout << " BinaryenGetFunctionTypeAt(the_module, " << index << ");\n"; + } + + auto* wasm = (Module*)module; + if (index < wasm->functionTypes.size()) { + return wasm->functionTypes.at(index).get(); + } + return NULL; +} + +const char* BinaryenFunctionTypeGetName(BinaryenFunctionTypeRef ftype) { + if (tracing) { + std::cout << " BinaryenFunctionTypeGetName(functionTypes[" << functionTypes[ftype] << "]);\n"; + } + + return ((FunctionType*)ftype)->name.c_str(); +} + +void BinaryenFunctionTypeSetName(BinaryenFunctionTypeRef ftype, const char* newName) { + if (tracing) { + std::cout << " BinaryenFunctionTypeSetName(functionTypes[" << functionTypes[ftype] << "], \"" << newName << "\");\n"; + } + + ((FunctionType*)ftype)->name.set(newName); +} + +BinaryenType BinaryenFunctionTypeGetParamAt(BinaryenFunctionTypeRef ftype, BinaryenIndex index) { + if (tracing) { + std::cout << " BinaryenFunctionTypeGetParamAt(functionTypes[" << functionTypes[ftype] << "], " << index << ");\n"; + } + + auto ft = (FunctionType*)ftype; + if (index < ft->params.size()) { + return ft->params.at(index); + } + return uint32_t(-1); +} + +BinaryenType BinaryenFunctionTypeGetResult(BinaryenFunctionTypeRef ftype) { + if (tracing) { + std::cout << " BinaryenFunctionTypeGetResult(functionTypes[" << functionTypes[ftype] << "]);\n"; + } + + return ((FunctionType*)ftype)->result; +} + BinaryenLiteral BinaryenLiteralInt32(int32_t x) { return toBinaryenLiteral(Literal(x)); } BinaryenLiteral BinaryenLiteralInt64(int64_t x) { return toBinaryenLiteral(Literal(x)); } BinaryenLiteral BinaryenLiteralFloat32(float x) { return toBinaryenLiteral(Literal(x)); } @@ -696,6 +805,22 @@ BinaryenExpressionRef BinaryenUnreachable(BinaryenModuleRef module) { return static_cast(ret); } +BinaryenExpressionId BinaryenExpressionGetId(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenExpressionGetId(expressions[" << expressions[expr] << "]);\n"; + } + + return ((Expression*)expr)->_id; +} + +BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenExpressionGetType(expressions[" << expressions[expr] << "]);\n"; + } + + return ((Expression*)expr)->type; +} + void BinaryenExpressionPrint(BinaryenExpressionRef expr) { if (tracing) { std::cout << " BinaryenExpressionPrint(expressions[" << expressions[expr] << "]);\n"; @@ -705,6 +830,66 @@ void BinaryenExpressionPrint(BinaryenExpressionRef expr) { std::cout << '\n'; } +int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenConstGetValueI32(expressions[" << expressions[expr] << "]);\n"; + } + + auto* expression = (Expression*)expr; + assert(expression->is() && expression->type == i32); + return static_cast(expression)->value.geti32(); +} + +int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenConstGetValueI64(expressions[" << expressions[expr] << "]);\n"; + } + + auto* expression = (Expression*)expr; + assert(expression->is() && expression->type == i64); + return static_cast(expression)->value.geti64(); +} + +int32_t BinaryenConstGetValueI64Low(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenConstGetValueI64Low(expressions[" << expressions[expr] << "]);\n"; + } + + auto* expression = (Expression*)expr; + assert(expression->is()); + return (int32_t)(static_cast(expression)->value.geti64() & 0xffffffff); +} + +int32_t BinaryenConstGetValueI64High(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenConstGetValueI64High(expressions[" << expressions[expr] << "]);\n"; + } + + auto* expression = (Expression*)expr; + assert(expression->is()); + return (int32_t)(static_cast(expression)->value.geti64() >> 32); +} + +float BinaryenConstGetValueF32(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenConstGetValueF32(expressions[" << expressions[expr] << "]);\n"; + } + + auto* expression = (Expression*)expr; + assert(expression->is()); + return static_cast(expression)->value.getf32(); +} + +double BinaryenConstGetValueF64(BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenConstGetValueF64(expressions[" << expressions[expr] << "]);\n"; + } + + auto* expression = (Expression*)expr; + assert(expression->is()); + return static_cast(expression)->value.getf64(); +} + // Functions BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* name, BinaryenFunctionTypeRef type, BinaryenType* varTypes, BinaryenIndex numVarTypes, BinaryenExpressionRef body) { @@ -746,11 +931,71 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* na return ret; } -BinaryenImportRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name, BinaryenType type, int8_t mutable_, BinaryenExpressionRef init) { +BinaryenFunctionRef BinaryenGetFunctionAt(BinaryenModuleRef module, BinaryenIndex index) { + if (tracing) { + std::cout << " BinaryenGetFunctionAt(the_module, " << index << ");\n"; + } + + auto* wasm = (Module*)module; + if (index < wasm->functions.size()) { + return wasm->functions.at(index).get(); + } + return NULL; +} + +const char* BinaryenFunctionGetName(BinaryenFunctionRef func) { + if (tracing) { + std::cout << " BinaryenFunctionGetName(functions[" << functions[func] << "]);\n"; + } + + return ((Function*)func)->name.c_str(); +} + +void BinaryenFunctionSetName(BinaryenFunctionRef func, const char* newName) { + if (tracing) { + std::cout << " BinaryenFunctionSetName(functions[" << functions[func] << "], \"" << newName << "\");\n"; + } + + ((Function*)func)->name.set(newName); +} + +BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func) { + if (tracing) { + std::cout << " BinaryenFunctionGetBody(functions[" << functions[func] << "]);\n"; + } + + return ((Function*)func)->body; +} + +void BinaryenFunctionSetBody(BinaryenFunctionRef func, BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenFunctionSetBody(functions[" << functions[func] << "], expressions[" << expressions[expr] << ");\n"; + } + + ((Function*)func)->body = (Expression*)expr; +} + +BinaryenType BinaryenFunctionGetParamAt(BinaryenFunctionRef func, BinaryenIndex index) { if (tracing) { - std::cout << " BinaryenAddGlobal(the_module, \"" << name << "\", types[" << type << "], " << mutable_ << ", " << expressions[init] << ");\n"; + std::cout << " BinaryenFunctionGetParamAt(functions[" << functions[func] << "]);\n"; } + Function* fn = (Function*)func; + if (index < fn->params.size()) { + return fn->params.at(index); + } + return uint32_t(-1); +} + +BinaryenType BinaryenFunctionGetResult(BinaryenFunctionRef func) { + if (tracing) { + std::cout << " BinaryenFunctionGetResult(functions[" << functions[func] << "]);\n"; + } + + return ((Function*)func)->result; +} + +BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name, BinaryenType type, int8_t mutable_, BinaryenExpressionRef init) { auto* wasm = (Module*)module; auto* ret = new Global(); ret->name = name; @@ -758,16 +1003,94 @@ BinaryenImportRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name, ret->mutable_ = !!mutable_; ret->init = (Expression*)init; wasm->addGlobal(ret); + + if (tracing) { + auto id = noteGlobal(ret); + std::cout << " globals[" << id << "] = BinaryenAddGlobal(the_module, \"" << name << "\", types[" << type << "], " << mutable_ << ", " << expressions[init] << ");\n"; + } + return ret; } -// Imports +BinaryenGlobalRef BinaryenGetGlobalAt(BinaryenModuleRef module, BinaryenIndex index) { + if (tracing) { + std::cout << " BinaryenGetGlobalAt(the_module, " << index << ");"; + } -BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef type) { + auto* wasm = (Module*)module; + if (index < wasm->globals.size()) { + return wasm->globals.at(index).get(); + } + return NULL; +} + +const char* BinaryenGlobalGetName(BinaryenGlobalRef global) { if (tracing) { - std::cout << " BinaryenAddImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\", functionTypes[" << functionTypes[type] << "]);\n"; + std::cout << " BinaryenGlobalGetName(globals[" << globals[global] << "]);"; } + return ((Global*)global)->name.c_str(); +} + +void BinaryenGlobalSetName(BinaryenGlobalRef global, const char* newName) { + if (tracing) { + std::cout << " BinaryenGlobalGetName(globals[" << globals[global] << "], \"" << newName << "\");"; + } + + ((Global*)global)->name.set(newName); +} + +BinaryenType BinaryenGlobalGetType(BinaryenGlobalRef global) { + if (tracing) { + std::cout << " BinaryenGlobalGetType(globals[" << globals[global] << "]);"; + } + + return ((Global*)global)->type; +} + +void BinaryenGlobalSetType(BinaryenGlobalRef global, BinaryenType type) { + if (tracing) { + std::cout << " BinaryenGlobalSetType(globals[" << globals[global] << "], types[" << type << "]);"; + } + + ((Global*)global)->type = WasmType(type); +} + +int BinaryenGlobalGetMutable(BinaryenGlobalRef global) { + if (tracing) { + std::cout << " BinaryenGlobalGetMutable(globals[" << globals[global] << "]);"; + } + + return ((Global*)global)->mutable_ ? 1 : 0; +} + +void BinaryenGlobalSetMutable(BinaryenGlobalRef global, int mutable_) { + if (tracing) { + std::cout << " BinaryenGlobalSetMutable(globals[" << globals[global] << "], " << mutable_ << ");"; + } + + ((Global*)global)->mutable_ = mutable_ ? true : false; +} + +BinaryenExpressionRef BinaryenGlobalGetInit(BinaryenGlobalRef global) { + if (tracing) { + std::cout << " BinaryenGlobalGetInit(globals[" << globals[global] << "]);"; + } + + return ((Global*)global)->init; +} + +void BinaryenGlobalSetInit(BinaryenGlobalRef global, BinaryenExpressionRef expr) { + if (tracing) { + std::cout << " BinaryenGlobalSetInit(globals[" << globals[global] << "], expressions[" << expr << "]);"; + } + + ((Global*)global)->init = (Expression*)expr; +} + +// Imports + +BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef type) { auto* wasm = (Module*)module; auto* ret = new Import(); ret->name = internalName; @@ -776,42 +1099,154 @@ BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* intern ret->functionType = ((FunctionType*)type)->name; ret->kind = ExternalKind::Function; wasm->addImport(ret); + + if (tracing) { + auto id = noteImport(ret); + std::cout << " imports[" << id << "] = BinaryenAddImport(the_module, \"" << internalName << "\", \"" << externalModuleName << "\", \"" << externalBaseName << "\", functionTypes[" << functionTypes[type] << "]);\n"; + } + return ret; } -void BinaryenRemoveImport(BinaryenModuleRef module, const char* internalName) { +void BinaryenRemoveImportByName(BinaryenModuleRef module, const char* internalName) { if (tracing) { - std::cout << " BinaryenRemoveImport(the_module, \"" << internalName << "\");\n"; + std::cout << " BinaryenRemoveImportByName(the_module, \"" << internalName << "\");\n"; } auto* wasm = (Module*)module; wasm->removeImport(internalName); } -// Exports +BinaryenImportRef BinaryenGetImportAt(BinaryenModuleRef module, BinaryenIndex index) { + if (tracing) { + std::cout << " BinaryenGetImportAt(the_module, " << index << ");\n"; + } -BinaryenExportRef BinaryenAddExport(BinaryenModuleRef module, const char* internalName, const char* externalName) { + auto* wasm = (Module*)module; + if (index < wasm->imports.size()) { + return wasm->imports.at(index).get(); + } + return NULL; +} + +const char* BinaryenImportGetName(BinaryenImportRef import) { + if (tracing) { + std::cout << " BinaryenImportGetName(imports[" << imports[import] << "]);\n"; + } + + return ((Import*)import)->name.c_str(); +} + +void BinaryenImportSetName(BinaryenImportRef import, const char* newName) { + if (tracing) { + std::cout << " BinaryenImportSetName(imports[" << imports[import] << "], \"" << newName << "\");\n"; + } + + ((Import*)import)->name.set(newName); +} + +const char* BinaryenImportGetModule(BinaryenImportRef import) { + if (tracing) { + std::cout << " BinaryenImportGetModule(imports[" << imports[import] << "]);\n"; + } + + return ((Import*)import)->module.c_str(); +} + +void BinaryenImportSetModule(BinaryenImportRef import, const char* newName) { + if (tracing) { + std::cout << " BinaryenImportSetModule(imports[" << imports[import] << "], \"" << newName << "\");\n"; + } + + ((Import*)import)->module.set(newName); +} + +const char* BinaryenImportGetBase(BinaryenImportRef import) { + if (tracing) { + std::cout << " BinaryenImportGetBase(imports[" << imports[import] << "]);\n"; + } + + return ((Import*)import)->base.c_str(); +} + +void BinaryenImportSetBase(BinaryenImportRef import, const char* newName) { if (tracing) { - std::cout << " BinaryenAddExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n"; + std::cout << " BinaryenImportSetBase(imports[" << imports[import] << "], \"" << newName << "\");\n"; } + ((Import*)import)->base.set(newName); +} + +// Exports + +BinaryenExportRef BinaryenAddExport(BinaryenModuleRef module, const char* internalName, const char* externalName) { auto* wasm = (Module*)module; auto* ret = new Export(); ret->value = internalName; ret->name = externalName; wasm->addExport(ret); + + if (tracing) { + auto id = noteExport(ret); + std::cout << " exports[" << id << "] = BinaryenAddExport(the_module, \"" << internalName << "\", \"" << externalName << "\");\n"; + } + return ret; } -void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName) { +BinaryenExportRef BinaryenGetExportAt(BinaryenModuleRef module, BinaryenIndex index) { + if (tracing) { + std::cout << " BinaryenGetExportAt(the_module, " << index << ");\n"; + } + + auto* wasm = (Module*)module; + if (index < wasm->exports.size()) { + return wasm->exports.at(index).get(); + } + return NULL; +} + +void BinaryenRemoveExportByName(BinaryenModuleRef module, const char* externalName) { if (tracing) { - std::cout << " BinaryenRemoveExport(the_module, \"" << externalName << "\");\n"; + std::cout << " BinaryenRemoveExportByName(the_module, \"" << externalName << "\");\n"; } auto* wasm = (Module*)module; wasm->removeExport(externalName); } +const char* BinaryenExportGetValue(BinaryenExportRef export_) { + if (tracing) { + std::cout << " BinaryenExportGetValue(exports[" << exports[export_] << "]);\n"; + } + + return ((Export*)export_)->value.c_str(); +} + +void BinaryenExportSetValue(BinaryenExportRef export_, const char* newName) { + if (tracing) { + std::cout << " BinaryenExportSetValue(exports[" << exports[export_] << "], \"" << newName << "\");\n"; + } + + ((Export*)export_)->value.set(newName); +} + +const char* BinaryenExportGetName(BinaryenExportRef export_) { + if (tracing) { + std::cout << " BinaryenExportGetName(exports[" << exports[export_] << "]);\n"; + } + + return ((Export*)export_)->name.c_str(); +} + +void BinaryenExportSetName(BinaryenExportRef export_, const char* newName) { + if (tracing) { + std::cout << " BinaryenExportSetName(exports[" << exports[export_] << "], \"" << newName << "\");\n"; + } + + ((Export*)export_)->name.set(newName); +} + // Function table. One per module void BinaryenSetFunctionTable(BinaryenModuleRef module, BinaryenFunctionRef* funcs, BinaryenIndex numFuncs) { @@ -894,6 +1329,72 @@ void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, Binaryen } } +void BinaryenSetMemoryImported(BinaryenModuleRef module, const char* externalModuleName, const char* externalBaseName) { + if (tracing) { + std::cout << " BinaryenSetMemoryImported(the_module, \"" << externalModuleName << "\", \"" << externalBaseName << "\");\n"; + } + + auto* wasm = (Module*)module; + assert(wasm->memory.exists); + wasm->memory.imported = true; + auto memoryImport = make_unique(); + memoryImport->module = externalModuleName; + memoryImport->base = externalBaseName; + memoryImport->name = Name::fromInt(0); + memoryImport->kind = ExternalKind::Memory; + wasm->addImport(memoryImport.release()); +} + +int BinaryenHasMemory(BinaryenModuleRef module) { + if (tracing) { + std::cout << " BinaryenHasMemory(the_module);\n"; + } + + auto* wasm = (Module*)module; + return wasm->memory.exists ? 1 : 0; +} + +BinaryenMemorySegmentRef BinaryenGetMemorySegmentAt(BinaryenModuleRef module, BinaryenIndex index) { + if (tracing) { + std::cout << " BinaryenGetMemorySegmentAt(the_module, " << index << ");\n"; + } + + auto* wasm = (Module*)module; + assert(wasm->memory.exists); + + if (index < wasm->memory.segments.size()) { + return &wasm->memory.segments.at(index); + } + return NULL; +} + +BinaryenExpressionRef BinaryenMemorySegmentGetOffset(BinaryenMemorySegmentRef segment) { + // if (tracing) { + // std::cout << " BinaryenMemorySegmentGetOffset(memorySegments[" << memorySegments[segment] << "]);\n"; + // } + + auto* seg = (Memory::Segment*)segment; + return seg->offset; +} + +size_t BinaryenMemorySegmentGetDataSize(BinaryenMemorySegmentRef segment) { + // if (tracing) { + // std::cout << " BinaryenMemorySegmentGetDataSize(memorySegments[" << memorySegments[segment] << "]);\n"; + // } + + auto* seg = (Memory::Segment*)segment; + return seg->data.size(); +} + +const char* BinaryenMemorySegmentGetData(BinaryenMemorySegmentRef segment) { + // if (tracing) { + // std::cout << " BinaryenMemorySegmentGetData(memorySegments[" << memorySegments[segment] << "]);\n"; + // } + + auto* seg = (Memory::Segment*)segment; + return seg->data.data(); +} + // Start function. One per module void BinaryenSetStart(BinaryenModuleRef module, BinaryenFunctionRef start) { @@ -1147,6 +1648,9 @@ void BinaryenSetAPITracing(int on) { " std::map functionTypes;\n" " std::map expressions;\n" " std::map functions;\n" + " std::map globals;\n" + " std::map imports;\n" + " std::map exports;\n" " std::map relooperBlocks;\n" " BinaryenModuleRef the_module = NULL;\n" " RelooperRef the_relooper = NULL;\n"; diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 0b3f5cc8500..2ac955653b2 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -76,6 +76,39 @@ BinaryenType BinaryenFloat64(void); // the API figure out the type instead of providing one. BinaryenType BinaryenUndefined(void); +// Expression ids (call to get the value of each; you can cache them) + +typedef uint32_t BinaryenExpressionId; + +BinaryenExpressionId BinaryenInvalidId(void); +BinaryenExpressionId BinaryenBlockId(void); +BinaryenExpressionId BinaryenIfId(void); +BinaryenExpressionId BinaryenLoopId(void); +BinaryenExpressionId BinaryenBreakId(void); +BinaryenExpressionId BinaryenSwitchId(void); +BinaryenExpressionId BinaryenCallId(void); +BinaryenExpressionId BinaryenCallImportId(void); +BinaryenExpressionId BinaryenCallIndirectId(void); +BinaryenExpressionId BinaryenGetLocalId(void); +BinaryenExpressionId BinaryenSetLocalId(void); +BinaryenExpressionId BinaryenGetGlobalId(void); +BinaryenExpressionId BinaryenSetGlobalId(void); +BinaryenExpressionId BinaryenLoadId(void); +BinaryenExpressionId BinaryenStoreId(void); +BinaryenExpressionId BinaryenConstId(void); +BinaryenExpressionId BinaryenUnaryId(void); +BinaryenExpressionId BinaryenBinaryId(void); +BinaryenExpressionId BinaryenSelectId(void); +BinaryenExpressionId BinaryenDropId(void); +BinaryenExpressionId BinaryenReturnId(void); +BinaryenExpressionId BinaryenHostId(void); +BinaryenExpressionId BinaryenNopId(void); +BinaryenExpressionId BinaryenUnreachableId(void); +BinaryenExpressionId BinaryenAtomicCmpxchgId(void); +BinaryenExpressionId BinaryenAtomicRMWId(void); +BinaryenExpressionId BinaryenAtomicWaitId(void); +BinaryenExpressionId BinaryenAtomicWakeId(void); + // Modules // // Modules contain lists of functions, imports, exports, function types. The @@ -103,6 +136,17 @@ typedef void* BinaryenFunctionTypeRef; // Add a new function type. This is thread-safe. // Note: name can be NULL, in which case we auto-generate a name BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const char* name, BinaryenType result, BinaryenType* paramTypes, BinaryenIndex numParams); +// Gets the function type at the specified index. Returns `NULL` when there are no more function types. +BinaryenFunctionTypeRef BinaryenGetFunctionTypeAt(BinaryenModuleRef module, BinaryenIndex index); +// Gets the name of the specified function type. +const char* BinaryenFunctionTypeGetName(BinaryenFunctionTypeRef ftype); +// Sets the name of the specified function type. +void BinaryenFunctionTypeSetName(BinaryenFunctionTypeRef ftype, const char* newName); +// Gets the parameter type at the specified index of the specified function type. Returns `BinaryenUndefined()` +// when there are no more parameters. +BinaryenType BinaryenFunctionTypeGetParamAt(BinaryenFunctionTypeRef ftype, BinaryenIndex index); +// Gets the result type of the specified function type. +BinaryenType BinaryenFunctionTypeGetResult(BinaryenFunctionTypeRef ftype); // Literals. These are passed by value. @@ -321,9 +365,24 @@ BinaryenExpressionRef BinaryenReturn(BinaryenModuleRef module, BinaryenExpressio BinaryenExpressionRef BinaryenHost(BinaryenModuleRef module, BinaryenOp op, const char* name, BinaryenExpressionRef* operands, BinaryenIndex numOperands); BinaryenExpressionRef BinaryenNop(BinaryenModuleRef module); BinaryenExpressionRef BinaryenUnreachable(BinaryenModuleRef module); - -// Print an expression to stdout. Useful for debugging. +// Gets the id (kind) of the specified expression. +BinaryenExpressionId BinaryenExpressionGetId(BinaryenExpressionRef expr); +// Gets the type of the specified expression. +BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr); +// Prints an expression to stdout. Useful for debugging. void BinaryenExpressionPrint(BinaryenExpressionRef expr); +// Gets the 32-bit integer value of the specified known-to-be-constant expression. +int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr); +// Gets the 64-bit integer value of the specified known-to-be-constant expression. +int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr); +// Gets the low 32-bits of a 64-bit integer value of the specified known-to-be-constant expression. +int32_t BinaryenConstGetValueI64Low(BinaryenExpressionRef expr); +// Gets the high 32-bits of a 64-bit integer value of the specified known-to-be-constant expression. +int32_t BinaryenConstGetValueI64High(BinaryenExpressionRef expr); +// Gets the 32-bit float value of the specified known-to-be-constant expression. +float BinaryenConstGetValueF32(BinaryenExpressionRef expr); +// Gets the 64-bit float value of the specified known-to-be-constant expression. +double BinaryenConstGetValueF64(BinaryenExpressionRef expr); // Functions @@ -338,24 +397,85 @@ typedef void* BinaryenFunctionRef; // 0 (and written $0), and if you also have 2 vars they will be // at indexes 1 and 2, etc., that is, they share an index space. BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* name, BinaryenFunctionTypeRef type, BinaryenType* varTypes, BinaryenIndex numVarTypes, BinaryenExpressionRef body); +// Gets the function at the specified index. Returns `NULL` when there are no more functions. +BinaryenFunctionRef BinaryenGetFunctionAt(BinaryenModuleRef module, BinaryenIndex index); +// Gets the name of the specified function. +const char* BinaryenFunctionGetName(BinaryenFunctionRef func); +// Sets the name of the specified function. +void BinaryenFunctionSetName(BinaryenFunctionRef func, const char* newName); +// Gets the body of the specified function. +BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func); +// Sets the body of the specified function. +void BinaryenFunctionSetBody(BinaryenFunctionRef func, BinaryenExpressionRef expr); +// Gets the parameter type at the specified index of the specified function. Returns `BinaryenUndefined()` +// when there are no more parameters. +BinaryenType BinaryenFunctionGetParamAt(BinaryenFunctionRef func, BinaryenIndex index); +// Gets the result type of the specified function. +BinaryenType BinaryenFunctionGetResult(BinaryenFunctionRef func); // Imports typedef void* BinaryenImportRef; BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef type); -void BinaryenRemoveImport(BinaryenModuleRef module, const char* internalName); +// Removes the import of the specified internal name from the module. +void BinaryenRemoveImportByName(BinaryenModuleRef module, const char* internalName); +// Gets the import at the specified index from the specified module. Returns `NULL` when there are no more imports. +BinaryenImportRef BinaryenGetImportAt(BinaryenModuleRef module, BinaryenIndex index); +// Gets the name of the specified import. This is the name referenced by other internal elements. +const char* BinaryenImportGetName(BinaryenImportRef import); +// Sets the name of the specified import. This is the name referenced by other internal elements. +void BinaryenImportSetName(BinaryenImportRef import, const char* newName); +// Gets the module of the specified import. This is the name of the module being imported from. +const char* BinaryenImportGetModule(BinaryenImportRef import); +// Sets the module of the specified import. This is the name of the module being imported from. +void BinaryenImportSetModule(BinaryenImportRef import, const char* newName); +// Gets the base of the specified import. This is the name of the element within the module being imported from. +const char* BinaryenImportGetBase(BinaryenImportRef import); +// Sets the base of the specified import. This is the name of the element within the module being imported from. +void BinaryenImportSetBase(BinaryenImportRef import, const char* newName); // Exports typedef void* BinaryenExportRef; +// Adds an export to the module, linking the internal name of the exported +// element to an external name then exposed to the embedder. BinaryenExportRef BinaryenAddExport(BinaryenModuleRef module, const char* internalName, const char* externalName); -void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName); +// Removes the export of the specified name from the module. +void BinaryenRemoveExportByName(BinaryenModuleRef module, const char* externalName); +// Gets the value of the specified export. This is the name referenced by other internal elements. +const char* BinaryenExportGetValue(BinaryenExportRef export_); +// Sets the value of the specified export. This is the name referenced by other internal elements. +void BinaryenExportSetValue(BinaryenExportRef export_, const char* newName); +// Gets the name of the specified export. This is the name exposed to the embedder. +const char* BinaryenExportGetName(BinaryenExportRef export_); +// Sets the name of the specified export. This is the name exposed to the embedder. +void BinaryenExportSetName(BinaryenExportRef export_, const char* newName); // Globals -BinaryenImportRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name, BinaryenType type, int8_t mutable_, BinaryenExpressionRef init); +typedef void* BinaryenGlobalRef; + +BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name, BinaryenType type, int8_t mutable_, BinaryenExpressionRef init); +// Gets the global at the specified index. Returns `NULL` when there are no more globals. +BinaryenGlobalRef BinaryenGetGlobalAt(BinaryenModuleRef module, BinaryenIndex index); +// Gets the name of the specified global. +const char* BinaryenGlobalGetName(BinaryenGlobalRef global); +// Sets the name of the specified global. +void BinaryenGlobalSetName(BinaryenGlobalRef global, const char* newName); +// Gets the type of the specified global. +BinaryenType BinaryenGlobalGetType(BinaryenGlobalRef global); +// Sets the type of the specified global. +void BinaryenGlobalSetType(BinaryenGlobalRef global, BinaryenType type); +// Gets whether the specified global is mutable (`1`) or not (`0`). +int BinaryenGlobalGetMutable(BinaryenGlobalRef global); +// Sets whether the specified global is mutable (`1`) or not (`0`). +void BinaryenGlobalSetMutable(BinaryenGlobalRef global, int mutable_); +// Gets the initializer expression of the specified global. +BinaryenExpressionRef BinaryenGlobalGetInit(BinaryenGlobalRef global); +// Sets the initilizer expression of the specified global. +void BinaryenGlobalSetInit(BinaryenGlobalRef global, BinaryenExpressionRef expr); // Function table. One per module @@ -363,9 +483,23 @@ void BinaryenSetFunctionTable(BinaryenModuleRef module, BinaryenFunctionRef* fun // Memory. One per module +typedef void* BinaryenMemorySegmentRef; + // Each segment has data in segments, a start offset in segmentOffsets, and a size in segmentSizes. // exportName can be NULL void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char **segments, BinaryenExpressionRef* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments); +// Makes the current memory an import. Memory must have been set through `BinaryenSetMemory` previously. +void BinaryenSetMemoryImported(BinaryenModuleRef module, const char* externalModuleName, const char* externalBaseName); +// Tests if the module has a memory (`1`) or not (`0`). +int BinaryenHasMemory(BinaryenModuleRef module); +// Gets the memory segment at the specified index. Returns `NULL` when there are no more memory segments. +BinaryenMemorySegmentRef BinaryenGetMemorySegmentAt(BinaryenModuleRef module, BinaryenIndex index); +// Gets the offset expression of the specified memory segment. +BinaryenExpressionRef BinaryenMemorySegmentGetOffset(BinaryenMemorySegmentRef segment); +// Gets the size of the data of the specified memory segment. +size_t BinaryenMemorySegmentGetDataSize(BinaryenMemorySegmentRef segment); +// Gets a pointer to the data of the specified memory segment. +const char* BinaryenMemorySegmentGetData(BinaryenMemorySegmentRef segment); // Start function. One per module diff --git a/test/binaryen.js/kitchen-sink.js.txt b/test/binaryen.js/kitchen-sink.js.txt index 1a4e6c417ce..d7b6e6db1f3 100644 --- a/test/binaryen.js/kitchen-sink.js.txt +++ b/test/binaryen.js/kitchen-sink.js.txt @@ -1068,7 +1068,7 @@ module loaded from binary form: ) ) -[wasm-validator error in function $func] 1 != 2: set_local type must match function, on +[wasm-validator error in function $func] 1 != 2: set_local type must match function, on [none] (set_local $0 [i64] (i64.const 1234) ) @@ -1091,6 +1091,9 @@ int main() { std::map functionTypes; std::map expressions; std::map functions; + std::map globals; + std::map imports; + std::map exports; std::map relooperBlocks; BinaryenModuleRef the_module = NULL; RelooperRef the_relooper = NULL; @@ -1391,8 +1394,8 @@ int main() { BinaryenType paramTypes[] = { 1, 4 }; functionTypes[1] = BinaryenAddFunctionType(the_module, "fiF", 3, paramTypes, 2); } - BinaryenAddImport(the_module, "an-imported", "module", "base", functionTypes[1]); - BinaryenAddExport(the_module, "kitchen()sinker", "kitchen_sinker"); + imports[0] = BinaryenAddImport(the_module, "an-imported", "module", "base", functionTypes[1]); + exports[0] = BinaryenAddExport(the_module, "kitchen()sinker", "kitchen_sinker"); { BinaryenFunctionRef funcs[] = { functions[0] }; BinaryenSetFunctionTable(the_module, funcs, 1); @@ -1957,6 +1960,9 @@ int main() { functionTypes.clear(); expressions.clear(); functions.clear(); + globals.clear(); + imports.clear(); + exports.clear(); relooperBlocks.clear(); the_module = BinaryenModuleCreate(); expressions[size_t(NULL)] = BinaryenExpressionRef(NULL); @@ -1968,7 +1974,7 @@ int main() { BinaryenType paramTypes[] = { 1 }; functionTypes[1] = BinaryenAddFunctionType(the_module, "vi", 0, paramTypes, 1); } - BinaryenAddImport(the_module, "check", "module", "check", functionTypes[1]); + imports[1] = BinaryenAddImport(the_module, "check", "module", "check", functionTypes[1]); the_relooper = RelooperCreate(); expressions[1] = BinaryenConst(the_module, BinaryenLiteralInt32(1337)); { @@ -2919,6 +2925,9 @@ optimized: functionTypes.clear(); expressions.clear(); functions.clear(); + globals.clear(); + imports.clear(); + exports.clear(); relooperBlocks.clear(); return 0; } diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index d08a204a9f3..f75fa74b67d 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -1091,6 +1091,9 @@ int main() { std::map functionTypes; std::map expressions; std::map functions; + std::map globals; + std::map imports; + std::map exports; std::map relooperBlocks; BinaryenModuleRef the_module = NULL; RelooperRef the_relooper = NULL; @@ -1390,8 +1393,8 @@ int main() { BinaryenType paramTypes[] = { 1, 4 }; functionTypes[1] = BinaryenAddFunctionType(the_module, "fiF", 3, paramTypes, 2); } - BinaryenAddImport(the_module, "an-imported", "module", "base", functionTypes[1]); - BinaryenAddExport(the_module, "kitchen()sinker", "kitchen_sinker"); + imports[0] = BinaryenAddImport(the_module, "an-imported", "module", "base", functionTypes[1]); + exports[0] = BinaryenAddExport(the_module, "kitchen()sinker", "kitchen_sinker"); { BinaryenFunctionRef funcs[] = { functions[0] }; BinaryenSetFunctionTable(the_module, funcs, 1); @@ -1955,6 +1958,9 @@ int main() { functionTypes.clear(); expressions.clear(); functions.clear(); + globals.clear(); + imports.clear(); + exports.clear(); relooperBlocks.clear(); the_module = BinaryenModuleCreate(); expressions[size_t(NULL)] = BinaryenExpressionRef(NULL); @@ -1966,7 +1972,7 @@ int main() { BinaryenType paramTypes[] = { 1 }; functionTypes[1] = BinaryenAddFunctionType(the_module, "vi", 0, paramTypes, 1); } - BinaryenAddImport(the_module, "check", "module", "check", functionTypes[1]); + imports[0] = BinaryenAddImport(the_module, "check", "module", "check", functionTypes[1]); the_relooper = RelooperCreate(); expressions[1] = BinaryenConst(the_module, BinaryenLiteralInt32(1337)); { @@ -2910,6 +2916,9 @@ optimized: functionTypes.clear(); expressions.clear(); functions.clear(); + globals.clear(); + imports.clear(); + exports.clear(); relooperBlocks.clear(); return 0; } diff --git a/test/example/c-api-relooper-unreachable-if.cpp b/test/example/c-api-relooper-unreachable-if.cpp index b6f7f8b046c..0f0ca8d0133 100644 --- a/test/example/c-api-relooper-unreachable-if.cpp +++ b/test/example/c-api-relooper-unreachable-if.cpp @@ -6,6 +6,9 @@ int main() { std::map functionTypes; std::map expressions; std::map functions; + std::map globals; + std::map imports; + std::map exports; std::map relooperBlocks; BinaryenModuleRef the_module = NULL; RelooperRef the_relooper = NULL; @@ -42,7 +45,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 2 }; functions[0] = BinaryenAddFunction(the_module, "tinycore::eh_personality", functionTypes[0], varTypes, 3, expressions[9]); } - BinaryenAddExport(the_module, "tinycore::eh_personality", "tinycore::eh_personality"); + exports[0] = BinaryenAddExport(the_module, "tinycore::eh_personality", "tinycore::eh_personality"); the_relooper = RelooperCreate(); expressions[10] = BinaryenGetLocal(the_module, 0, 1); expressions[11] = BinaryenConst(the_module, BinaryenLiteralInt32(0)); @@ -63,7 +66,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 2 }; functions[1] = BinaryenAddFunction(the_module, "tinycore::eh_unwind_resume", functionTypes[0], varTypes, 3, expressions[18]); } - BinaryenAddExport(the_module, "tinycore::eh_unwind_resume", "tinycore::eh_unwind_resume"); + exports[1] = BinaryenAddExport(the_module, "tinycore::eh_unwind_resume", "tinycore::eh_unwind_resume"); the_relooper = RelooperCreate(); { BinaryenExpressionRef children[] = { 0 }; @@ -91,7 +94,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 2 }; functions[2] = BinaryenAddFunction(the_module, "tinycore::panic_fmt", functionTypes[1], varTypes, 3, expressions[24]); } - BinaryenAddExport(the_module, "tinycore::panic_fmt", "tinycore::panic_fmt"); + exports[2] = BinaryenAddExport(the_module, "tinycore::panic_fmt", "tinycore::panic_fmt"); the_relooper = RelooperCreate(); expressions[25] = BinaryenGetLocal(the_module, 0, 1); expressions[26] = BinaryenConst(the_module, BinaryenLiteralInt32(0)); @@ -112,7 +115,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 2 }; functions[3] = BinaryenAddFunction(the_module, "tinycore::rust_eh_register_frames", functionTypes[0], varTypes, 3, expressions[33]); } - BinaryenAddExport(the_module, "tinycore::rust_eh_register_frames", "tinycore::rust_eh_register_frames"); + exports[3] = BinaryenAddExport(the_module, "tinycore::rust_eh_register_frames", "tinycore::rust_eh_register_frames"); the_relooper = RelooperCreate(); expressions[34] = BinaryenGetLocal(the_module, 0, 1); expressions[35] = BinaryenConst(the_module, BinaryenLiteralInt32(0)); @@ -133,7 +136,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 2 }; functions[4] = BinaryenAddFunction(the_module, "tinycore::rust_eh_unregister_frames", functionTypes[0], varTypes, 3, expressions[42]); } - BinaryenAddExport(the_module, "tinycore::rust_eh_unregister_frames", "tinycore::rust_eh_unregister_frames"); + exports[4] = BinaryenAddExport(the_module, "tinycore::rust_eh_unregister_frames", "tinycore::rust_eh_unregister_frames"); the_relooper = RelooperCreate(); expressions[43] = BinaryenGetLocal(the_module, 0, 1); expressions[44] = BinaryenSetLocal(the_module, 1, expressions[43]); @@ -143,7 +146,7 @@ int main() { BinaryenType paramTypes[] = { 1 }; functionTypes[2] = BinaryenAddFunctionType(the_module, "print_i32", 0, paramTypes, 1); } - BinaryenAddImport(the_module, "print_i32", "spectest", "print", functionTypes[2]); + imports[0] = BinaryenAddImport(the_module, "print_i32", "spectest", "print", functionTypes[2]); expressions[47] = BinaryenGetLocal(the_module, 2, 1); { BinaryenExpressionRef operands[] = { expressions[47] }; @@ -178,7 +181,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 1, 1, 2 }; functions[5] = BinaryenAddFunction(the_module, "wasm::print_i32", functionTypes[3], varTypes, 5, expressions[58]); } - BinaryenAddExport(the_module, "wasm::print_i32", "wasm::print_i32"); + exports[5] = BinaryenAddExport(the_module, "wasm::print_i32", "wasm::print_i32"); the_relooper = RelooperCreate(); expressions[59] = BinaryenConst(the_module, BinaryenLiteralInt32(1)); expressions[60] = BinaryenSetLocal(the_module, 0, expressions[59]); @@ -247,7 +250,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 1, 1, 1, 1, 1, 1, 2 }; functions[6] = BinaryenAddFunction(the_module, "real_main", functionTypes[4], varTypes, 9, expressions[104]); } - BinaryenAddExport(the_module, "real_main", "real_main"); + exports[6] = BinaryenAddExport(the_module, "real_main", "real_main"); the_relooper = RelooperCreate(); expressions[105] = BinaryenGetLocal(the_module, 0, 1); expressions[106] = BinaryenSetLocal(the_module, 2, expressions[105]); @@ -338,7 +341,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 }; functions[7] = BinaryenAddFunction(the_module, "main", functionTypes[5], varTypes, 10, expressions[156]); } - BinaryenAddExport(the_module, "main", "main"); + exports[7] = BinaryenAddExport(the_module, "main", "main"); { BinaryenType paramTypes[] = { 0 }; functionTypes[6] = BinaryenAddFunctionType(the_module, "__wasm_start", 0, paramTypes, 0); @@ -363,7 +366,7 @@ int main() { BinaryenExpressionRef children[] = { expressions[159], expressions[163] }; expressions[164] = BinaryenBlock(the_module, NULL, children, 2, BinaryenUndefined()); } - BinaryenAddExport(the_module, "__wasm_start", "rust_entry"); + exports[8] = BinaryenAddExport(the_module, "__wasm_start", "rust_entry"); { BinaryenType varTypes[] = { 0 }; functions[8] = BinaryenAddFunction(the_module, "__wasm_start", functionTypes[6], varTypes, 0, expressions[164]); @@ -437,7 +440,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 1, 1, 1, 1, 1, 1, 2 }; functions[9] = BinaryenAddFunction(the_module, "_isize_as_tinycore::Add_::add", functionTypes[7], varTypes, 9, expressions[210]); } - BinaryenAddExport(the_module, "_isize_as_tinycore::Add_::add", "_isize_as_tinycore::Add_::add"); + exports[9] = BinaryenAddExport(the_module, "_isize_as_tinycore::Add_::add", "_isize_as_tinycore::Add_::add"); the_relooper = RelooperCreate(); expressions[211] = BinaryenGetLocal(the_module, 0, 1); expressions[212] = BinaryenSetLocal(the_module, 1, expressions[211]); @@ -470,7 +473,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 1, 1, 1, 2 }; functions[10] = BinaryenAddFunction(the_module, "_bool_as_tinycore::Not_::not", functionTypes[8], varTypes, 6, expressions[227]); } - BinaryenAddExport(the_module, "_bool_as_tinycore::Not_::not", "_bool_as_tinycore::Not_::not"); + exports[10] = BinaryenAddExport(the_module, "_bool_as_tinycore::Not_::not", "_bool_as_tinycore::Not_::not"); the_relooper = RelooperCreate(); expressions[228] = BinaryenGetLocal(the_module, 0, 1); expressions[229] = BinaryenSetLocal(the_module, 2, expressions[228]); @@ -508,7 +511,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 1, 1, 1, 1, 1, 2 }; functions[11] = BinaryenAddFunction(the_module, "_i16_as_tinycore::PartialEq_::eq", functionTypes[9], varTypes, 8, expressions[249]); } - BinaryenAddExport(the_module, "_i16_as_tinycore::PartialEq_::eq", "_i16_as_tinycore::PartialEq_::eq"); + exports[11] = BinaryenAddExport(the_module, "_i16_as_tinycore::PartialEq_::eq", "_i16_as_tinycore::PartialEq_::eq"); the_relooper = RelooperCreate(); expressions[250] = BinaryenGetLocal(the_module, 0, 1); expressions[251] = BinaryenSetLocal(the_module, 2, expressions[250]); @@ -546,12 +549,15 @@ int main() { BinaryenType varTypes[] = { 1, 1, 2, 2, 1, 1, 1, 2 }; functions[12] = BinaryenAddFunction(the_module, "_i64_as_tinycore::PartialEq_::eq", functionTypes[10], varTypes, 8, expressions[271]); } - BinaryenAddExport(the_module, "_i64_as_tinycore::PartialEq_::eq", "_i64_as_tinycore::PartialEq_::eq"); + exports[12] = BinaryenAddExport(the_module, "_i64_as_tinycore::PartialEq_::eq", "_i64_as_tinycore::PartialEq_::eq"); BinaryenModuleValidate(the_module); BinaryenModuleDispose(the_module); functionTypes.clear(); expressions.clear(); functions.clear(); + globals.clear(); + imports.clear(); + exports.clear(); relooperBlocks.clear(); } diff --git a/test/example/c-api-unused-mem.cpp b/test/example/c-api-unused-mem.cpp index a0de5637b8a..a5fd8b7bbe9 100644 --- a/test/example/c-api-unused-mem.cpp +++ b/test/example/c-api-unused-mem.cpp @@ -7,6 +7,9 @@ int main() { std::map functionTypes; std::map expressions; std::map functions; + std::map globals; + std::map imports; + std::map exports; std::map relooperBlocks; BinaryenModuleRef the_module = NULL; RelooperRef the_relooper = NULL; @@ -49,7 +52,7 @@ int main() { BinaryenType varTypes[] = { 1, 1, 2 }; functions[0] = BinaryenAddFunction(the_module, "main", functionTypes[0], varTypes, 3, expressions[10]); } - BinaryenAddExport(the_module, "main", "main"); + exports[0] = BinaryenAddExport(the_module, "main", "main"); { BinaryenType paramTypes[] = { 0 }; functionTypes[1] = BinaryenAddFunctionType(the_module, "__wasm_start", 0, paramTypes, 0); @@ -71,7 +74,7 @@ int main() { BinaryenExpressionRef children[] = { expressions[13], expressions[14] }; expressions[15] = BinaryenBlock(the_module, NULL, children, 2, BinaryenUndefined()); } - BinaryenAddExport(the_module, "__wasm_start", "rust_entry"); + exports[1] = BinaryenAddExport(the_module, "__wasm_start", "rust_entry"); { BinaryenType varTypes[] = { 0 }; functions[1] = BinaryenAddFunction(the_module, "__wasm_start", functionTypes[1], varTypes, 0, expressions[15]);