Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
GordonSmith committed Mar 19, 2024
1 parent 6157a44 commit 47aa027
Show file tree
Hide file tree
Showing 21 changed files with 1,850 additions and 1,361 deletions.
16 changes: 0 additions & 16 deletions .clang-format

This file was deleted.

57 changes: 0 additions & 57 deletions .cmake-format

This file was deleted.

17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ This repository contains a C++ ABI implementation of the WebAssembly Component M

## Features

### OS
- [x] Linux
- [ ] MacOS
- [ ] Windows

### Host Data Types
- [x] Bool
- [x] S8
- [x] U8
Expand Down Expand Up @@ -43,7 +49,16 @@ This repository contains a C++ ABI implementation of the WebAssembly Component M
- [ ] Flags
- [ ] Own
- [ ] Borrow
- [x] lower_flat

### Host Functions
- [x] lower_values
- [ ] lift_values

### Tests
- [ ] ABI
- [ ] WasmTime
- [ ] Wamr
- [ ] WasmEdge

## Usage

Expand Down
15 changes: 0 additions & 15 deletions all/CMakeLists.txt

This file was deleted.

16 changes: 14 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SRC
cmcpp.hpp
cmcpp.cpp
context.hpp
context.cpp
flatten.hpp
flatten.cpp
lift.hpp
lift.cpp
load.hpp
load.cpp
lower.hpp
lower.cpp
store.hpp
store.cpp
util.hpp
util.cpp
val.hpp
val.cpp
)
Expand Down
21 changes: 12 additions & 9 deletions src/cmcpp.cpp → src/context.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cmcpp.hpp"
#include "context.hpp"

#include <cstring>
#include <optional> // Include the necessary header file
Expand All @@ -10,7 +10,9 @@ class CanonicalOptionsImpl : public CanonicalOptions
const GuestPostReturn &_post_return;

public:
CanonicalOptionsImpl(const GuestMemory &memory, HostEncoding string_encoding, const GuestRealloc &realloc, const GuestPostReturn &post_return) : _realloc(realloc), _post_return(post_return)
CanonicalOptionsImpl(const GuestMemory &memory, HostEncoding string_encoding,
const GuestRealloc &realloc, const GuestPostReturn &post_return)
: _realloc(realloc), _post_return(post_return)
{
this->memory = memory;
this->string_encoding = string_encoding;
Expand All @@ -31,21 +33,22 @@ class CanonicalOptionsImpl : public CanonicalOptions
}
};

CanonicalOptionsPtr createCanonicalOptions(const GuestMemory &memory, const GuestRealloc &realloc, HostEncoding encoding, const GuestPostReturn &post_return)
CanonicalOptionsPtr createCanonicalOptions(const GuestMemory &memory, const GuestRealloc &realloc,
HostEncoding encoding,
const GuestPostReturn &post_return)
{
return std::make_shared<CanonicalOptionsImpl>(memory, encoding, realloc, post_return);
}

class CallContextImpl : public CallContext
{
public:
CallContextImpl(CanonicalOptionsPtr options)
{
opts = options;
}
CallContextImpl(CanonicalOptionsPtr options) { opts = options; }
};

CallContextPtr createCallContext(const GuestMemory &memory, const GuestRealloc &realloc, HostEncoding encoding, const GuestPostReturn &post_return)
CallContextPtr createCallContext(const GuestMemory &memory, const GuestRealloc &realloc,
HostEncoding encoding, const GuestPostReturn &post_return)
{
return std::make_shared<CallContextImpl>(createCanonicalOptions(memory, realloc, encoding, post_return));
return std::make_shared<CallContextImpl>(
createCanonicalOptions(memory, realloc, encoding, post_return));
}
File renamed without changes.
146 changes: 146 additions & 0 deletions src/flatten.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include "flatten.hpp"

// #include <any>
// #include <cassert>
// #include <cmath>
// #include <cstring>
// #include <map>
// #include <random>
// #include <sstream>

const int MAX_FLAT_PARAMS = 16;
const int MAX_FLAT_RESULTS = 1;

std::vector<std::string> flatten_type(ValType kind);
std::vector<std::string> flatten_types(const std::vector<ValType> &ts);

// CoreFuncType flatten_functype(FuncTypePtr ft, std::string context)
// {
// std::vector<std::string> flat_params = flatten_types(ft->param_types());
// if (flat_params.size() > MAX_FLAT_PARAMS)
// {
// flat_params = {"i32"};
// }

// std::vector<std::string> flat_results = flatten_types(ft->result_types());
// if (flat_results.size() > MAX_FLAT_RESULTS)
// {
// if (context == "lift")
// {
// flat_results = {"i32"};
// }
// else if (context == "lower")
// {
// flat_params.push_back("i32");
// flat_results = {};
// }
// }

// return CoreFuncType(flat_params, flat_results);
// }

std::vector<std::string> flatten_types(const std::vector<Val> &vs)
{
std::vector<std::string> result;
for (Val v : vs)
{
std::vector<std::string> flattened = flatten_type(v.kind());
result.insert(result.end(), flattened.begin(), flattened.end());
}
return result;
}

std::vector<std::string> flatten_types(const std::vector<ValType> &ts)
{
std::vector<std::string> result;
for (ValType t : ts)
{
std::vector<std::string> flattened = flatten_type(t);
result.insert(result.end(), flattened.begin(), flattened.end());
}
return result;
}

std::vector<std::string> flatten_record(const std::vector<Field> &fields);
std::vector<std::string> flatten_variant(const std::vector<Case> &cases);

std::vector<std::string> flatten_type(ValType kind)
{
switch (kind)
{
case ValType::Bool:
return {"i32"};
case ValType::U8:
case ValType::U16:
case ValType::U32:
return {"i32"};
case ValType::U64:
return {"i64"};
case ValType::S8:
case ValType::S16:
case ValType::S32:
return {"i32"};
case ValType::S64:
return {"i64"};
case ValType::Float32:
return {"f32"};
case ValType::Float64:
return {"f64"};
case ValType::Char:
return {"i32"};
case ValType::String:
return {"i32", "i32"};
case ValType::List:
return {"i32", "i32"};
// case ValType::Record:
// return flatten_record(static_cast<const Record &>(t).fields);
// case ValType::Variant:
// return flatten_variant(static_cast<const Variant &>(t).cases);
// case ValType::Flags:
// return std::vector<std::string>(num_i32_flags(static_cast<const Flags &>(t).labels),
// "i32");
// case ValType::Own:
// case ValType::Borrow:
// return {"i32"};
default:
throw std::runtime_error("Invalid type");
}
}

std::vector<std::string> flatten_record(const std::vector<Field> &fields)
{
std::vector<std::string> flat;
for (const Field &f : fields)
{
auto flattened = flatten_type(f.v.value().kind());
flat.insert(flat.end(), flattened.begin(), flattened.end());
}
return flat;
}

// std::string join(const std::string &a, const std::string &b);
// std::vector<std::string> flatten_variant(const std::vector<Case> &cases)
// {
// std::vector<std::string> flat;
// for (const auto &c : cases)
// {
// if (c.t.has_value())
// {
// auto flattened = flatten_type(c.t.value());
// for (size_t i = 0; i < flattened.size(); ++i)
// {
// if (i < flat.size())
// {
// flat[i] = join(flat[i], flattened[i]);
// }
// else
// {
// flat.push_back(flattened[i]);
// }
// }
// }
// }
// auto discriminantFlattened = flatten_type(discriminant_type(cases));
// flat.insert(flat.begin(), discriminantFlattened.begin(), discriminantFlattened.end());
// return flat;
// }
7 changes: 7 additions & 0 deletions src/flatten.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef FLATTEN_HPP
#define FLATTEN_HPP

#include "context.hpp"
#include "val.hpp"

#endif
Loading

0 comments on commit 47aa027

Please sign in to comment.