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 ce210dd commit 771aeec
Show file tree
Hide file tree
Showing 12 changed files with 1,060 additions and 970 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,29 @@ A toy WebAssembly virtual machine.

## Getting Started
```bash
make run
make build && ./wvm ./hello-world.wasm
```
```bash
-> parse
-> parseMagicNumber: [ 0 97 115 109 ]
-> parseVersion: [ 1 0 0 0 ]
-> parseSection
-> parseSectionId: 1
-> parseSectionId: 3
-> parseSectionId: 7
-> parseSectionId: 10
-> instantiate
-> func
-> entryFuc: add
-> execute
-> call: 0,paramCount: 2
-> local.get: 0
-> local.get: 1
-> call: 1,paramCount: 2
-> local.get: 0
-> local.get: 1
-> op.I32Add
-> x: 2,y: 1
-> ret: 3
ret: 3
```
1 change: 0 additions & 1 deletion src/decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace wvm
if (readable_.gcount() < static_cast<std::streamsize>(n))
{
std::cerr << "Error: Unable to read " << n << " bytes from the file." << std::endl;
// 可以在这里抛出异常或者进行其他错误处理操作
}

return buffer; // 返回读取到的字节数据
Expand Down
943 changes: 4 additions & 939 deletions src/executor.cc

Large diffs are not rendered by default.

21 changes: 5 additions & 16 deletions src/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

#include <optional>
#include <unordered_map>
#include <tuple>

#include "instance.h"
#include "opcodes.h"
#include "module.h"
#include "log.h"

constexpr uint8_t MAGIC_BYTES_COUNT = 4;
constexpr uint8_t VER_BYTES_COUNT = 4;
Expand All @@ -29,12 +31,6 @@ constexpr auto SCREEN_ARG_SETW_OFFSET = 23;
constexpr auto INPUT_ENTRY_KEY_NAME = "entryFuncName";
constexpr auto INPUT_ENTRY_KEY_ARG = "entryArgs";

#define DECLARE_OPCODE_HANDLER_VALID(NAME) \
static void do##NAME(Executor &, op_handler_info_t = std::nullopt);
#define DECLARE_OPCODE_HANDLER_INVALID(NAME)
#define DECLARE_OPCODE_HANDLER(NAME, OP, VALDITI) \
DECLARE_OPCODE_HANDLER_##VALDITI(NAME)

namespace wvm
{
class Executor
Expand Down Expand Up @@ -171,8 +167,6 @@ namespace wvm
{
try
{

std::cout << "rtIns->stack.size: " << rtIns->stack.size() << std::endl;
return std::get<T>(
rtIns->stack.at(rtIns->stack.size() - 1 - pos));
}
Expand Down Expand Up @@ -351,9 +345,12 @@ namespace wvm
{
auto &x = std::get<Runtime::RTValueFrame>(rtIns->stack.back()); // "c2".
auto &y = std::get<Runtime::RTValueFrame>(rtIns->stack.at(rtIns->stack.size() - 2)); // "c1".
LOG(" -> x: ", std::get<int>(x.value), ",y: ", std::get<int>(y.value));

auto ret = handler(std::get<T>(y.value), std::get<T>(x.value));
rtIns->stack.pop_back(); // Keep "c1" on the stage.
y.value = ret;
LOG(" -> ret: ", std::get<int>(y.value));
}
catch (...)
{
Expand All @@ -378,14 +375,6 @@ namespace wvm
engine_result_t postProcess();
engine_result_t execute(std::optional<uint32_t> = {});
};

struct Interpreter
{
using op_handler_proto_t = void (*)(Executor &, std::optional<uint32_t>);
using op_handler_info_t = std::optional<uint32_t>;
static std::array<op_handler_proto_t, sizeof(uint8_t) * 1 << 8> opTokenHandlers;
ITERATE_ALL_OPCODE(DECLARE_OPCODE_HANDLER)
};
}

#endif // SRC_EXECUTOR_H_
6 changes: 5 additions & 1 deletion src/instance.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "instance.h"

#include "log.h"
namespace wvm
{
Instance::Instance(std::shared_ptr<Module> module_ptr) : module_ptr(module_ptr)
Expand All @@ -12,8 +12,11 @@ namespace wvm

std::shared_ptr<wvm::Runtime> Instance::instantiate()
{
LOG("-> instantiate");
std::shared_ptr<wvm::Runtime> runtime_ptr = std::make_shared<wvm::Runtime>(module_ptr);

/* func */
LOG(" -> func");
for (auto i = 0; i < module_ptr->funcDefs.size(); ++i)
{
const auto typeIdx = module_ptr->funcTypesIndices.at(i);
Expand All @@ -38,6 +41,7 @@ namespace wvm
{
return item.name == "add" && item.extKind == EXT_KIND_FUNC;
});
LOG(" -> entryFuc: ", entryFunc->name);

const auto funcIdx = entryFunc->extIdx;
if (entryFunc != module_ptr->exports.end())
Expand Down
Loading

0 comments on commit 771aeec

Please sign in to comment.