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
8 changes: 4 additions & 4 deletions src/jit/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void JITCompiler::buildVariables(uint32_t requiredStackSize)
for (auto it : tryBlocks()[nextTryBlock].catchBlocks) {
if (it.tagIndex != std::numeric_limits<uint32_t>::max()) {
TagType* tagType = module()->tagType(it.tagIndex);
variableCount += module()->functionType(tagType->sigIndex())->param().size();
variableCount += tagType->functionType()->param().size();
}
}

Expand Down Expand Up @@ -370,7 +370,7 @@ void JITCompiler::buildVariables(uint32_t requiredStackSize)
dependencyCtx.update(it.u.handler->m_dependencyStart, label->id());
} else {
TagType* tagType = module()->tagType(it.tagIndex);
const TypeVector& param = module()->functionType(tagType->sigIndex())->param();
const TypeVector& param = tagType->functionType()->param();
Label* catchLabel = it.u.handler;

m_variableList->pushCatchUpdate(catchLabel, param.size());
Expand Down Expand Up @@ -454,7 +454,7 @@ void JITCompiler::buildVariables(uint32_t requiredStackSize)
dependencyCtx.update(it.u.handler->m_dependencyStart, instr->id());
} else {
TagType* tagType = module()->tagType(it.tagIndex);
const TypeVector& param = module()->functionType(tagType->sigIndex())->param();
const TypeVector& param = tagType->functionType()->param();
Label* catchLabel = it.u.handler;

dependencyCtx.update(catchLabel->m_dependencyStart, catchLabel->id(),
Expand Down Expand Up @@ -788,7 +788,7 @@ void JITCompiler::buildVariables(uint32_t requiredStackSize)
case ByteCode::ThrowOpcode: {
Throw* throwTag = reinterpret_cast<Throw*>(instr->byteCode());
TagType* tagType = module()->tagType(throwTag->tagIndex());
types = &module()->functionType(tagType->sigIndex())->param();
types = &tagType->functionType()->param();
break;
}
default: {
Expand Down
2 changes: 1 addition & 1 deletion src/jit/ByteCodeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ static void compileFunction(JITCompiler* compiler)
case ByteCode::ThrowOpcode: {
Throw* throwTag = reinterpret_cast<Throw*>(byteCode);
TagType* tagType = compiler->module()->tagType(throwTag->tagIndex());
uint32_t size = compiler->module()->functionType(tagType->sigIndex())->param().size();
uint32_t size = tagType->functionType()->param().size();

Instruction* instr = compiler->append(byteCode, Instruction::Any, opcode, size, 0);
Operand* param = instr->params();
Expand Down
2 changes: 1 addition & 1 deletion src/jit/TryCatchInl.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ static void emitThrow(sljit_compiler* compiler, Instruction* instr)
CompileContext* context = CompileContext::get(compiler);
Throw* throwTag = reinterpret_cast<Throw*>(instr->byteCode());
TagType* tagType = context->compiler->module()->tagType(throwTag->tagIndex());
const TypeVector& types = context->compiler->module()->functionType(tagType->sigIndex())->param();
const TypeVector& types = tagType->functionType()->param();

emitStoreOntoStack(compiler, instr->params(), throwTag->dataOffsets(), types, false);

Expand Down
157 changes: 75 additions & 82 deletions src/parser/WASMComponentParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,20 @@ namespace wabt {
class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate {
private:
struct CoreInstanceType {
static constexpr size_t NotInline = ~static_cast<size_t>(0);

CoreInstanceType(Walrus::Module* module)
CoreInstanceType(Walrus::Module* module, uint32_t index)
: module(module)
, inlineIndex(NotInline)
, index(index)
{
ASSERT(module != nullptr);
}

CoreInstanceType(size_t inlineIndex)
CoreInstanceType(uint32_t index)
: module(nullptr)
, inlineIndex(inlineIndex)
, index(index)
{
}

Walrus::Module* module;
size_t inlineIndex;
uint32_t index;
};

// Depth data for each component.
Expand All @@ -53,15 +50,16 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
: parent(parent)
, parentComponentType(parentComponentType)
, parentComponent(parentComponent)
, coreInstanceCounter(0)
{
}

ComponentTypeInfo* parent;
Walrus::ComponentType* parentComponentType;
Walrus::Component* parentComponent;
uint32_t coreInstanceCounter;
std::vector<Walrus::FunctionType*> coreFuncTypes;
std::vector<bool> coreMemories;
std::vector<Walrus::Module*> coreModuleTypes;
std::vector<CoreInstanceType> coreInstanceTypes;
std::vector<Walrus::ComponentTypeFunc*> funcTypes;
std::vector<Walrus::ComponentType*> componentTypes;
Expand Down Expand Up @@ -286,29 +284,31 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
if (!result.second.empty()) {
m_walrusParseError = result.second;
}
// Module has been already added to store.
Walrus::Module* module = result.first.value();
m_currentComponent->pushDeclaration(new Walrus::ComponentCoreModule(module));
m_currentInfo->coreModuleTypes.push_back(module);
// Module has already been added to store.
m_currentComponent->pushModule(result.first.value());
}

void BeginComponent(uint32_t version, size_t depth)
{
ASSERT(m_currentInfo != nullptr || (m_current == nullptr && m_currentComponent == nullptr));
Walrus::Component* parentComponent = m_currentComponent;
m_currentInfo = new ComponentTypeInfo(m_currentInfo, m_current, m_currentComponent);
m_currentComponent = new Walrus::Component();
m_currentComponent = new Walrus::Component(m_store);
m_current = m_currentComponent->type();

if (parentComponent != nullptr) {
parentComponent->pushDeclaration(m_currentComponent);
parentComponent->pushComponent(m_currentComponent);
m_currentInfo->parent->componentTypes.push_back(m_current);
}
}

void EndComponent()
{
ComponentTypeInfo* info = m_currentInfo;
if (m_currentComponent->coreInlineExportsStarts().size() > 0) {
// Extra value for coreInlineExportsEnd() calls.
m_currentComponent->coreInlineExportsStarts().push_back(m_currentComponent->coreInlineExports().size());
}
// Keep the last component.
if (m_currentInfo->parentComponent != nullptr) {
m_currentComponent = m_currentInfo->parentComponent;
Expand All @@ -322,16 +322,17 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
void BeginCoreInstance(Index moduleIndex,
uint32_t argumentCount)
{
m_currentInfo->coreInstanceTypes.push_back(CoreInstanceType(m_currentInfo->coreModuleTypes[moduleIndex]));
m_currentInfo->coreInstanceTypes.push_back(CoreInstanceType(m_currentComponent->modules()[moduleIndex], m_currentInfo->coreInstanceCounter++));
m_currentComponent->pushDeclaration(new Walrus::ComponentCoreInstantiate(moduleIndex));
ASSERT(~static_cast<uint32_t>(m_currentComponent->coreInlineExportsStarts().size()) >= m_currentInfo->coreInstanceCounter);
}

void OnCoreInstanceArg(const ComponentStringLoc& name,
ComponentSort sort,
Index index)
{
Walrus::ComponentCoreInstantiate* instance = m_currentComponent->declarations().back()->asCoreInstantiate();
instance->arguments().push_back(Walrus::ComponentCoreInstantiate::Argument{ name.str.to_string(), index });
instance->arguments().push_back(Walrus::ComponentCoreInstantiate::Argument{ name.str.to_string(), m_currentInfo->coreInstanceTypes[index].index });
}

void EndCoreInstance()
Expand All @@ -340,59 +341,21 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate

void BeginInlineCoreInstance(uint32_t argumentCount)
{
m_currentInfo->coreInstanceTypes.push_back(CoreInstanceType(m_currentComponent->declarations().size()));
m_currentComponent->pushDeclaration(new Walrus::ComponentCoreInstantiateInline());
ASSERT(m_names.size() == 0);
uint32_t index = static_cast<uint32_t>(m_currentComponent->coreInlineExportsStarts().size());
m_currentComponent->coreInlineExportsStarts().push_back(m_currentComponent->coreInlineExports().size());
ASSERT(~index > m_currentInfo->coreInstanceCounter);
m_currentInfo->coreInstanceTypes.push_back(CoreInstanceType(~index));
}

void OnInlineCoreInstanceArg(const ComponentStringLoc& name,
ComponentSort sort,
Index index)
{
Walrus::ComponentCoreInstantiateInline* instance = m_currentComponent->declarations().back()->asCoreInstantiateInline();
instance->arguments().push_back(Walrus::ComponentCoreInstantiateInline::Argument{ getSort(sort), index });
m_names.push_back(name.str.to_string());
m_currentComponent->coreInlineExports().push_back(Walrus::Component::InlineExport{ name.str.to_string(), getSort(sort), index });
}

void EndInlineCoreInstance()
{
Walrus::ComponentCoreInstantiateInline* instance = m_currentComponent->declarations().back()->asCoreInstantiateInline();
Walrus::WASMParsingResult result;
size_t size = instance->arguments().size();
uint32_t funcIndex = 0;
uint32_t tableIndex = 0;
uint32_t memoryIndex = 0;
uint32_t globalIndex = 0;
ASSERT(m_names.size() == size);

for (size_t i = 0; i < size; i++) {
Walrus::ExportType::Type exportType;
uint32_t index;
switch (instance->arguments()[i].sort) {
case Walrus::ComponentSort::CoreFunc:
index = funcIndex++;
exportType = Walrus::ExportType::Function;
break;
case Walrus::ComponentSort::CoreTable:
index = tableIndex++;
exportType = Walrus::ExportType::Table;
break;
case Walrus::ComponentSort::CoreMemory:
index = memoryIndex++;
exportType = Walrus::ExportType::Memory;
break;
default:
ASSERT(instance->arguments()[i].sort == Walrus::ComponentSort::CoreGlobal);
index = globalIndex++;
exportType = Walrus::ExportType::Global;
break;
}
result.m_exports.push_back(new Walrus::ExportType(exportType, m_names[i], index));
}
Walrus::Module* module = new Walrus::Module(m_store, result);
m_currentInfo->coreInstanceTypes.back().module = module;
instance->setModule(module);
m_names.clear();
}

void BeginInstance(Index componentIndex,
Expand Down Expand Up @@ -430,8 +393,8 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
}
type->addRef();
Walrus::ComponentInstantiateInline* instance = m_currentComponent->declarations().back()->asInstantiateInline();
instance->type()->exports().push_back(Walrus::ComponentType::External{ name.str.to_string(), type });
instance->arguments().push_back(Walrus::ComponentInstantiateInline::Argument{ getSort(sort), index });
instance->type()->exports().push_back(Walrus::ComponentType::External{ name.str.to_string(), type, getSort(sort), kInvalidIndex });
instance->arguments().push_back(index);
}

void EndInlineInstance()
Expand All @@ -442,6 +405,10 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
Index instanceIndex,
const ComponentStringLoc& name)
{
if (sort != ComponentSort::Type) {
m_currentComponent->pushDeclaration(new Walrus::ComponentAliasExport(Walrus::ComponentDeclaration::AliasExportKind, name.str.to_string(), getSort(sort), instanceIndex));
}

for (auto it : m_currentInfo->instanceTypes[instanceIndex]->exports()) {
if (name.str == it.name) {
switch (sort) {
Expand Down Expand Up @@ -471,25 +438,26 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
Index coreInstanceIndex,
const ComponentStringLoc& name)
{
size_t exportIndex = 0;
const CoreInstanceType& type = m_currentInfo->coreInstanceTypes[coreInstanceIndex];
const Walrus::VectorWithFixedSize<Walrus::ExportType*, std::allocator<Walrus::ExportType*>>& exports = type.module->exports();

while (true) {
if (exportIndex >= exports.size()) {
m_walrusParseError = "export not found";
return;
}
if (type.module == nullptr) {
size_t exportIndex = m_currentComponent->coreInlineExportsStart(~type.index);
size_t end = m_currentComponent->coreInlineExportsEnd(~type.index);
std::vector<Walrus::Component::InlineExport>& exports = m_currentComponent->coreInlineExports();

if (name.str == exports[exportIndex]->name()) {
break;
}
exportIndex++;
}
while (true) {
if (exportIndex >= end) {
m_walrusParseError = "export not found";
return;
}

if (type.inlineIndex != CoreInstanceType::NotInline) {
exportIndex = m_currentComponent->declarations()[type.inlineIndex]->asCoreInstantiateInline()->arguments()[exportIndex].index;
if (name.str == exports[exportIndex].name) {
break;
}
exportIndex++;
}

exportIndex = exports[exportIndex].index;
switch (sort) {
case ComponentSort::CoreFunc:
m_currentInfo->coreFuncTypes.push_back(m_currentInfo->coreFuncTypes[exportIndex]);
Expand All @@ -500,9 +468,28 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
default:
break;
}

if (sort != ComponentSort::CoreType) {
m_currentComponent->pushDeclaration(new Walrus::ComponentAliasInline(getSort(sort), exportIndex));
}
return;
}

size_t exportIndex = 0;
const Walrus::VectorWithFixedSize<Walrus::ExportType*, std::allocator<Walrus::ExportType*>>& exports = type.module->exports();

while (true) {
if (exportIndex >= exports.size()) {
m_walrusParseError = "export not found";
return;
}

if (name.str == exports[exportIndex]->name()) {
break;
}
exportIndex++;
}

uint32_t itemIndex = exports[exportIndex]->itemIndex();

switch (sort) {
Expand All @@ -515,6 +502,10 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
default:
break;
}

if (sort != ComponentSort::CoreType) {
m_currentComponent->pushDeclaration(new Walrus::ComponentAliasExport(Walrus::ComponentDeclaration::AliasCoreExportKind, name.str.to_string(), getSort(sort), type.index));
}
}

void OnAliasOuter(ComponentSort sort,
Expand Down Expand Up @@ -783,7 +774,7 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
if (m_currentComponent != nullptr) {
m_currentComponent->pushDeclaration(new Walrus::ComponentImport(static_cast<uint32_t>(m_current->imports().size())));
}
m_current->imports().push_back(Walrus::ComponentType::External{ name.str.to_string(), pushExternalType(externalInfo) });
m_current->imports().push_back(Walrus::ComponentType::External{ name.str.to_string(), pushExternalType(externalInfo), getSort(externalInfo.sort), kInvalidIndex });
}

void OnExport(const ComponentStringLoc& name,
Expand All @@ -792,12 +783,13 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
ComponentExportInfo* exportInfo)
{
if (externalInfo != nullptr) {
m_current->exports().push_back(Walrus::ComponentType::External{ name.str.to_string(), pushExternalType(*externalInfo) });
uint32_t exportIndex = exportInfo != nullptr ? exportInfo->index.index : kInvalidIndex;
m_current->exports().push_back(Walrus::ComponentType::External{ name.str.to_string(), pushExternalType(*externalInfo), getSort(externalInfo->sort), exportIndex });
return;
}

ComponentExternalInfo info{ exportInfo->sort, ComponentExternalDesc::Unused, exportInfo->index };
m_current->exports().push_back(Walrus::ComponentType::External{ name.str.to_string(), pushExternalType(info) });
m_current->exports().push_back(Walrus::ComponentType::External{ name.str.to_string(), pushExternalType(info), getSort(exportInfo->sort), exportInfo->index.index });
}

Walrus::Component* parsingResult()
Expand All @@ -814,7 +806,6 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
Walrus::ComponentType* m_current;
Walrus::Component* m_currentComponent;
ComponentTypeInfo* m_currentInfo;
std::vector<std::string> m_names;
};

} // namespace wabt
Expand All @@ -827,11 +818,13 @@ std::pair<Optional<Component*>, std::string> WASMComponentParser::parseBinary(St

std::string error = ReadWasmComponentBinary(data, len, &delegate);
if (error.length()) {
if (delegate.parsingResult() != nullptr) {
delete delegate.parsingResult();
}
return std::make_pair(nullptr, error);
}

delete delegate.parsingResult();
return std::make_pair(nullptr, std::string());
return std::make_pair(delegate.parsingResult(), std::string());
}

} // namespace Walrus
Loading
Loading