Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaojx committed Oct 3, 2023
0 parents commit 1f3acce
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_base": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"vector": "cpp",
"__bits": "cpp",
"clocale": "cpp",
"compare": "cpp",
"concepts": "cpp",
"deque": "cpp",
"fstream": "cpp",
"iomanip": "cpp",
"stack": "cpp",
"variant": "cpp",
"map": "cpp",
"__tree": "cpp"
}
}
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
run:
make build && ./wvm ./hello-world.wasm
build:
g++ src/*.cc -o wvm -pthread -std=c++17
Binary file added hello-world.wasm
Binary file not shown.
13 changes: 13 additions & 0 deletions hello-world.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(module
(type (;0;) (func (param i32 i32) (result i32)))
(func (;0;) (type 0) (param i32 i32) (result i32)
local.get 0
local.get 1
call $inner_add)
(func $inner_add (type 0) (param i32 i32) (result i32)
local.get 0
local.get 1
i32.const 10
drop
i32.add)
(export "add" (func 0)))
9 changes: 9 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SRC_CONSTANTS_H_
#define SRC_CONSTANTS_H_

#include <vector>

#define MAGIC_NUMBER std::vector<char>({0x00, 0x61, 0x73, 0x6d})
#define WASM_VERSION std::vector<char>({0x01, 0x00, 0x00, 0x00})

#endif // SRC_CONSTANTS_H_
41 changes: 41 additions & 0 deletions src/decoder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <string>
#include <fstream>
#include <iostream>

#include "decoder.h"

namespace wvm
{
Decoder::Decoder(std::ifstream &readable) : readable_(readable)
{
}

Decoder::~Decoder()
{
}

std::ifstream &Decoder::readable()
{
return readable_;
}

std::vector<char> Decoder::readBytes(char n)
{
std::vector<char> buffer(n); // 创建一个存储字节的缓冲区
readable_.read(buffer.data(), n); // 从文件流中读取 n 个字节到缓冲区

// 如果未能读取足够的字节,可以根据需要进行处理(例如抛出异常)
if (readable_.gcount() < static_cast<std::streamsize>(n))
{
std::cerr << "Error: Unable to read " << n << " bytes from the file." << std::endl;
// 可以在这里抛出异常或者进行其他错误处理操作
}

return buffer; // 返回读取到的字节数据P
}

uint8_t Decoder::readByte()
{
return static_cast<uint8_t>(readable_.get());
}
}
120 changes: 120 additions & 0 deletions src/decoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef SRC_DECODE_H_
#define SRC_DECODE_H_

#include <string>
#include <fstream>
#include <iostream>
#include <memory>
#include <vector>

#include "decoder.h"

namespace wvm
{
class Decoder
{
private:
std::ifstream &readable_;

public:
Decoder(std::ifstream &readable);
~Decoder();

std::ifstream &readable();
std::vector<char> readBytes(char n);

uint8_t readByte();

template <typename U>
static std::vector<uint8_t> retrievePackedLEB128Bytes(U &&in)
{
std::vector<uint8_t> v = {};
while (true)
{
uint8_t byte;
if constexpr (std::is_same_v<typename std::decay<U>::type, uint8_t *>)
{
byte = *in++;
}
else
{
byte = static_cast<uint8_t>(in.get());
}
v.emplace_back(byte);
if (!(byte & 0x80))
{
break;
}
}
return v;
}

template <typename T>
static std::vector<uint8_t> encodeVaruint(T in)
{
std::vector<uint8_t> v;
do
{
uint8_t byte = in & 0x7f;
in >>= 7;
if (in != 0)
byte |= 0x80;
v.push_back(byte);
} while (in != 0);
return v;
}
template <typename T, typename U>
static T decodeVaruint(U &&in)
{
const auto v = retrievePackedLEB128Bytes(
std::forward<U>(in));
T val = 0;
unsigned shift = 0;
for (auto byte : v)
{
val |= (static_cast<T>(byte & 0x7f) << shift);
shift += 7;
}
return val;
}
template <typename T, typename U>
static T decodeVarint(U &&in)
{
const auto v = retrievePackedLEB128Bytes(
std::forward<U>(in));
T val = 0;
unsigned shift = 0;
uint8_t b = 0;
for (auto byte : v)
{
b = byte;
val |= (static_cast<T>(byte & 0x7f) << shift);
shift += 7;
}
if ((shift < sizeof(T) * 8) && (b & 0x40))
{
// Sign extend.
val |= (~0 << shift);
}
return val;
}

#define WALK_FUNC_DEF(name, type, suffix) \
type read##name() \
{ \
return decodeVar##suffix<type>(readable_); \
}

#define DEFINE_WALK_FUNCS(V) \
V(U8, uint8_t, uint) \
V(U16, uint16_t, uint) \
V(U32, uint32_t, uint) \
V(I8, int8_t, int) \
V(I16, int16_t, int) \
V(I32, int32_t, int)

DEFINE_WALK_FUNCS(WALK_FUNC_DEF)
};
}

#endif // SRC_DECODE_H_
18 changes: 18 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef SRC_LOG_H_
#define SRC_LOG_H_

#include <iostream>

// 重载输出运算符以支持 std::vector<char> 类型的输出
std::ostream &operator<<(std::ostream &os, const std::vector<char> &vec)
{
for (const auto &ch : vec)
{
os << ch;
}
return os;
}

#define LOG(...) std::cout << "[WVM]: " << (__VA_ARGS__) << std::endl

#endif // SRC_LOG_H_
Loading

0 comments on commit 1f3acce

Please sign in to comment.