Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaojx committed Oct 4, 2023
1 parent 3f29592 commit 69feaf9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ namespace wvm
const auto &descriptor = executor.getEngineData()->rtFuncDescriptor.at(idx);
auto paramCount = descriptor.funcType->first.size();
auto rtLocals = descriptor.localsDefault; // Copied.
std::cout << "doCall rtLocals.size: " << rtLocals.size() << std::endl;

// Set up func parameters.
if (paramCount > 0)
{
Expand All @@ -666,6 +668,7 @@ namespace wvm
&descriptor.funcType->second));
// Redirection.
executor.setPC(descriptor.codeEntry);
std::cout << "doCall executor.setPC: " << descriptor.codeEntry << std::endl;
}
void Interpreter::doCallIndirect(Executor &executor, op_handler_info_t _)
{
Expand Down
15 changes: 15 additions & 0 deletions src/instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ namespace wvm
const auto codeEntry = module_ptr->funcDefs.at(i).body.data();
// Wasm types -> RT types (value).
runtime_ptr->rtFuncDescriptor.emplace_back(&funcType, codeEntry);

runtime_ptr->expandWasmTypesToRTValues(
runtime_ptr->rtFuncDescriptor.back().localsDefault,
// 参数类型
funcType.first,
// 内部变量
module_ptr->funcDefs.at(i).locals);
}

/* entry point */
Expand All @@ -37,6 +44,14 @@ namespace wvm
{
runtime_ptr->rtEntryIdx = funcIdx;
}
const auto &inputFuncArgTypes = runtime_ptr->rtFuncDescriptor.at(funcIdx).funcType->first;

runtime_ptr->stack.push_back(
Runtime::RTValueFrame{
runtime_ptr->convertStrToRTVal("1", inputFuncArgTypes[0])});
runtime_ptr->stack.push_back(
Runtime::RTValueFrame{
runtime_ptr->convertStrToRTVal("2", inputFuncArgTypes[1])});
return runtime_ptr;
};

Expand Down
54 changes: 54 additions & 0 deletions src/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ constexpr uint8_t EXT_KIND_FUNC = 0x0;

namespace wvm
{
enum class ValueTypes : uint8_t
{
I32 = 0x7f,
I64 = 0x7e,
F32 = 0x7d,
F64 = 0x7c,
};
struct Runtime
{
enum class STVariantIndex : int8_t
Expand Down Expand Up @@ -69,6 +76,53 @@ namespace wvm
RTMemHolder(size_t size, uint8_t *ptr, uint32_t maximumPages)
: size(size), ptr(ptr), maximumPages(maximumPages) {}
};
Runtime::runtime_value_t convertStrToRTVal(const std::string &str, uint8_t type)
{
switch (static_cast<ValueTypes>(type))
{
case ValueTypes::I32:
return static_cast<Runtime::rt_i32_t>(std::stoi(str));
case ValueTypes::I64:
return static_cast<Runtime::rt_i64_t>(std::stol(str));
case ValueTypes::F32:
return static_cast<Runtime::rt_f32_t>(std::stof(str));
case ValueTypes::F64:
return static_cast<Runtime::rt_f64_t>(std::stod(str));
}
}
template <typename T>
void expandVTypesToRTVals(std::vector<Runtime::runtime_value_t> &container, T &t)
{
if constexpr (
std::is_same_v<std::decay_t<T>, std::vector<uint8_t>>)
{
for (const auto i : t)
{
switch (static_cast<ValueTypes>(i))
{
case ValueTypes::I32:
container.push_back(Runtime::rt_i32_t());
break;
case ValueTypes::I64:
container.push_back(Runtime::rt_i64_t());
break;
case ValueTypes::F32:
container.push_back(Runtime::rt_f32_t());
break;
case ValueTypes::F64:
container.push_back(Runtime::rt_f64_t());
break;
}
}
}
}
template <typename... Args>
void expandWasmTypesToRTValues(
std::vector<Runtime::runtime_value_t> &container,
Args &...args)
{
(expandVTypesToRTVals(container, args), ...);
}
shared_module_t module;
std::vector<RTMemHolder> rtMems;
std::vector<std::vector<std::optional<uint32_t>>> rtTables; // Func idx inside.
Expand Down

0 comments on commit 69feaf9

Please sign in to comment.