Skip to content

Commit

Permalink
Implement base for Walrus WASI
Browse files Browse the repository at this point in the history
Implement basic structure of wasi functions and also update test runner
to run wasi tests with the 'wasi' argument.

Signed-off-by: Adam Laszlo Kulcsar <[email protected]>
  • Loading branch information
kulcsaradam committed Jul 12, 2023
1 parent 8f4e6e9 commit 6f0a750
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 111 deletions.
132 changes: 132 additions & 0 deletions src/runtime/SpecTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2023-present Samsung Electronics Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "Walrus.h"

namespace Walrus {

class SpecTestFunctionTypes {
MAKE_STACK_ALLOCATED();

public:
enum Index : uint8_t {
// The R is meant to represent the results, after R are the result types.
NONE = 0,
I32R,
RI32,
I64R,
F32R,
F64R,
I32F32R,
F64F64R,
INVALID,
INDEX_NUM,
};

SpecTestFunctionTypes()
{
m_vector.reserve(INDEX_NUM);
size_t index = 0;

ValueTypeVector* param;
ValueTypeVector* result;

{
// NONE
param = new ValueTypeVector();
result = new ValueTypeVector();
m_vector[index++] = new FunctionType(param, result);
}
{
// I32R
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// RI32
param = new ValueTypeVector();
result = new ValueTypeVector();
result->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// I64
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I64);
m_vector[index++] = new FunctionType(param, result);
}
{
// F32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::F32);
m_vector[index++] = new FunctionType(param, result);
}
{
// F64
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::F64);
m_vector[index++] = new FunctionType(param, result);
}
{
// I32F32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I32);
param->push_back(Value::Type::F32);
m_vector[index++] = new FunctionType(param, result);
}
{
// F64F64
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::F64);
param->push_back(Value::Type::F64);
m_vector[index++] = new FunctionType(param, result);
}
{
// INVALID
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::Void);
m_vector[index++] = new FunctionType(param, result);
}

ASSERT(index == INDEX_NUM);
}

~SpecTestFunctionTypes()
{
for (size_t i = 0; i < m_vector.size(); i++) {
delete m_vector[i];
}
}

FunctionType* operator[](const size_t idx)
{
ASSERT(idx < m_vector.size());
return m_vector[idx];
}

private:
FunctionTypeVector m_vector;
};

} // namespace Walrus
133 changes: 23 additions & 110 deletions src/shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "runtime/Tag.h"
#include "runtime/Trap.h"
#include "parser/WASMParser.h"
#include "wasi/Wasi.h"


#include "wabt/wast-lexer.h"
#include "wabt/wast-parser.h"
Expand Down Expand Up @@ -101,108 +103,6 @@ static void printF64(double v)
printf("%s : f64\n", formatDecmialString(ss.str()).c_str());
}

class SpecTestFunctionTypes {
MAKE_STACK_ALLOCATED();

public:
enum Index : uint8_t {
NONE = 0,
I32,
I64,
F32,
F64,
I32F32,
F64F64,
INVALID,
INDEX_NUM,
};

SpecTestFunctionTypes()
{
m_vector.reserve(INDEX_NUM);
size_t index = 0;

ValueTypeVector* param;
ValueTypeVector* result;

{
// NONE
param = new ValueTypeVector();
result = new ValueTypeVector();
m_vector[index++] = new FunctionType(param, result);
}
{
// I32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// I64
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I64);
m_vector[index++] = new FunctionType(param, result);
}
{
// F32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::F32);
m_vector[index++] = new FunctionType(param, result);
}
{
// F64
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::F64);
m_vector[index++] = new FunctionType(param, result);
}
{
// I32F32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I32);
param->push_back(Value::Type::F32);
m_vector[index++] = new FunctionType(param, result);
}
{
// F64F64
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::F64);
param->push_back(Value::Type::F64);
m_vector[index++] = new FunctionType(param, result);
}
{
// INVALID
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::Void);
m_vector[index++] = new FunctionType(param, result);
}

ASSERT(index == INDEX_NUM);
}

~SpecTestFunctionTypes()
{
for (size_t i = 0; i < m_vector.size(); i++) {
delete m_vector[i];
}
}

FunctionType* operator[](const size_t idx)
{
ASSERT(idx < m_vector.size());
return m_vector[idx];
}

private:
FunctionTypeVector m_vector;
};

static Trap::TrapResult executeWASM(Store* store, const std::string& filename, const std::vector<uint8_t>& src, SpecTestFunctionTypes& functionTypes,
std::map<std::string, Instance*>* registeredInstanceMap = nullptr)
{
Expand Down Expand Up @@ -237,6 +137,8 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
(func (export "print_f64_f64") (param f64 f64))
)
*/
Walrus::WASI wasi = Walrus::WASI();
wasi.fillWasiFuncTable();

for (size_t i = 0; i < importTypes.size(); i++) {
auto import = importTypes[i];
Expand All @@ -250,7 +152,7 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
},
nullptr));
} else if (import->fieldName() == "print_i32") {
auto ft = functionTypes[SpecTestFunctionTypes::I32];
auto ft = functionTypes[SpecTestFunctionTypes::I32R];
importValues.push_back(ImportedFunction::createImportedFunction(
store,
ft,
Expand All @@ -259,7 +161,7 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
},
nullptr));
} else if (import->fieldName() == "print_i64") {
auto ft = functionTypes[SpecTestFunctionTypes::I64];
auto ft = functionTypes[SpecTestFunctionTypes::I64R];
importValues.push_back(ImportedFunction::createImportedFunction(
store,
ft,
Expand All @@ -268,7 +170,7 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
},
nullptr));
} else if (import->fieldName() == "print_f32") {
auto ft = functionTypes[SpecTestFunctionTypes::F32];
auto ft = functionTypes[SpecTestFunctionTypes::F32R];
importValues.push_back(ImportedFunction::createImportedFunction(
store,
ft,
Expand All @@ -277,7 +179,7 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
},
nullptr));
} else if (import->fieldName() == "print_f64") {
auto ft = functionTypes[SpecTestFunctionTypes::F64];
auto ft = functionTypes[SpecTestFunctionTypes::F64R];
importValues.push_back(ImportedFunction::createImportedFunction(
store,
ft,
Expand All @@ -286,7 +188,7 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
},
nullptr));
} else if (import->fieldName() == "print_i32_f32") {
auto ft = functionTypes[SpecTestFunctionTypes::I32F32];
auto ft = functionTypes[SpecTestFunctionTypes::I32F32R];
importValues.push_back(ImportedFunction::createImportedFunction(
store,
ft,
Expand All @@ -296,7 +198,7 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
},
nullptr));
} else if (import->fieldName() == "print_f64_f64") {
auto ft = functionTypes[SpecTestFunctionTypes::F64F64];
auto ft = functionTypes[SpecTestFunctionTypes::F64F64R];
importValues.push_back(ImportedFunction::createImportedFunction(
store,
ft,
Expand Down Expand Up @@ -330,7 +232,7 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
} else if (import->moduleName() == "wasi_snapshot_preview1") {
// TODO wasi
if (import->fieldName() == "proc_exit") {
auto ft = functionTypes[SpecTestFunctionTypes::I32];
auto ft = functionTypes[SpecTestFunctionTypes::I32R];
importValues.push_back(ImportedFunction::createImportedFunction(
store,
ft,
Expand All @@ -341,7 +243,18 @@ static Trap::TrapResult executeWASM(Store* store, const std::string& filename, c
},
nullptr));
}
} else if (registeredInstanceMap) {
Walrus::WASI::WasiFunc* wasiImportFunc = wasi.find(import->fieldName());
FunctionType* fn = functionTypes[wasiImportFunc->functionType];
if (wasiImportFunc != nullptr && fn->equals(import->functionType())) {
importValues.push_back(ImportedFunction::createImportedFunction(
store,
const_cast<FunctionType*>(import->functionType()),
wasiImportFunc->ptr,
nullptr));
}
}

else if (registeredInstanceMap) {
auto iter = registeredInstanceMap->find(import->moduleName());
if (iter != registeredInstanceMap->end()) {
Instance* instance = iter->second;
Expand Down
Loading

0 comments on commit 6f0a750

Please sign in to comment.