Compare commits

...

64 Commits

Author SHA1 Message Date
CHatingPython 3b98f77c08 chore: flat out the file structure 2026-09-11 18:48:16 +02:00
CHatingPython 959d0a7773 feat(furc): implement SSA destruction 2026-09-11 18:12:54 +02:00
CHatingPython 70375af068 test(furc/ssa): implement tests for SSA 2026-09-10 21:49:25 +02:00
CHatingPython 46d0de759a feat(furc/ssa): implement SSA stage 2026-09-10 21:48:45 +02:00
CHatingPython 01964a495c feat(furc/ir): add equality comparison to ir classes 2026-09-10 21:47:47 +02:00
CHatingPython 21fb587e36 fix(furc/ir): use separate counter for variables 2026-09-09 14:11:45 +02:00
CHatingPython fe65a09af9 feat(furlang): add serialization 2026-09-06 15:51:51 +02:00
CHatingPython 712a0d7047 chore: change clang-tidy 2026-09-06 15:09:50 +02:00
CHatingPython 35d7fc4970 refactor(furlang): improve results 2026-09-06 15:09:34 +02:00
CHatingPython 9bf4da4fd9 feat(furvm): add string type and improve assigning
Closes: #61
2026-09-06 01:21:14 +02:00
CHatingPython 6c7d16631d feat(furvm): allow setting thing's value 2026-09-04 21:28:28 +02:00
CHatingPython 24a9530abc refactor(furvm): improve thing value access 2026-09-04 21:28:18 +02:00
CHatingPython 0354ec1e79 refactor(furvm): move thing type to types.hpp 2026-09-04 20:59:14 +02:00
CHatingPython ddb60ca93a fix(furdb): add slices to furdb 2026-09-04 18:17:55 +02:00
CHatingPython 3aaed55485 feat(furvm): introduce thing iterators
Refs: #38
2026-09-04 18:17:45 +02:00
CHatingPython cfefca49ee test(furvm): add array and slice tests
Refs: #38
2026-09-04 00:16:58 +02:00
CHatingPython 21e963168e feat(furas): add slice type and instruction 2026-09-03 23:56:19 +02:00
CHatingPython b067bbb4ea feat(furvm): introduce slice instruction 2026-09-03 23:55:57 +02:00
CHatingPython fe37bf8058 feat(furvm): introduce slice type
Refs: #63
2026-09-03 23:55:31 +02:00
CHatingPython 977b22d8f5 fix(furvm): add stack.hpp
I forgot to add it earlier
2026-09-03 22:42:59 +02:00
CHatingPython 30a06186db feat(furvm): introduce stack storage
Closes: #62
2026-09-01 22:33:18 +02:00
CHatingPython 5e425d7fe4 feat(furvm): implement constants
Closes: #13
2026-09-01 17:18:01 +02:00
CHatingPython 12a131c2b8 feat(furc/SSA): insert phi nodes 2026-08-18 17:50:34 +02:00
CHatingPython 5403c0e1a4 feat(furc): actually use SSA 2026-08-18 17:49:43 +02:00
CHatingPython a1dac8ae3f chore(clangd): remove optional access checking 2026-08-18 17:49:20 +02:00
CHatingPython a311795915 fix(furc/IR): fix terminate function 2026-08-18 17:48:51 +02:00
CHatingPython 090d8ad81e feat(furc/SSA): compute dominance frontiers 2026-08-18 13:55:39 +02:00
CHatingPython 5efcf1b9b0 feat(furc/SSA): compute dominance tree 2026-08-18 00:16:32 +02:00
CHatingPython 136663aeab feat(furc/IR): add global variables 2026-08-18 00:15:43 +02:00
CHatingPython 56ea4ba2ab feat(furdb): add stack info 2026-08-17 21:14:10 +02:00
CHatingPython dab7f73216 feat(furvm): expose the stack 2026-08-17 21:13:02 +02:00
CHatingPython 1d4e8bb1d0 feat(furdb): add isocline for better command line 2026-08-17 12:14:30 +02:00
CHatingPython ed84a65757 fix(furdb): improve printing 2026-08-17 11:39:35 +02:00
CHatingPython 26efd110f1 feat(furdb): add continue and next commands 2026-08-17 11:35:49 +02:00
CHatingPython dd96f95d77 fix(furvm): add JustHit executor flag 2026-08-17 11:34:37 +02:00
CHatingPython 70cbb003f2 feat(furdb): introduce info command 2026-08-17 11:21:22 +02:00
CHatingPython 665982049d fix(furdb): improve program execution 2026-08-17 11:20:44 +02:00
CHatingPython 195d154474 fix(furvm): improve executor flags 2026-08-17 11:20:14 +02:00
CHatingPython 26d935e22a feat(furdb): add function for printing instruction 2026-08-17 10:59:00 +02:00
CHatingPython a4966a057c feat(furdb): add break command 2026-08-16 20:01:52 +02:00
CHatingPython b3ce4fa0ed feat(furvm): add breakpoints 2026-08-16 19:59:46 +02:00
CHatingPython 29dd9abb3f fix(furvm): more fixing 2026-08-15 23:13:45 +02:00
CHatingPython ffe1208775 refactor(furvm): cleanup some bugs
Or we could just pretend that there were no bugs clueless
2026-08-15 22:41:32 +02:00
CHatingPython a382b92cf0 refactor(furvm): improve things
Refs: #62
2026-08-15 22:15:35 +02:00
CHatingPython c59f0252ed refactor(furvm): remove thing arena from context 2026-08-15 12:14:24 +02:00
CHatingPython 5c841a1428 feat(furvm): add header to thing data
Refs: #62
2026-08-15 12:10:38 +02:00
CHatingPython 6d176e58f3 refactor(furvm): remove thing allocator
Refs: #62
2026-08-15 12:00:18 +02:00
CHatingPython 6737ed86b0 fix(furvm, furas): fix function signature matching 2026-08-15 00:32:57 +02:00
CHatingPython 4ede6f3476 feat(disfuras): add global variables 2026-08-15 00:32:02 +02:00
CHatingPython c841427eff feat(furas): introduce global variables 2026-08-14 13:58:29 +02:00
CHatingPython a685ecf226 feat(furvm): introduce global variables
Closes: #59
2026-08-14 13:56:10 +02:00
CHatingPython 84b70077c1 refactor(furvm): remove thing handles 2026-08-13 22:08:59 +02:00
CHatingPython 21e92dcb4b refactor(furvm): remove executor handle 2026-08-13 14:10:43 +02:00
CHatingPython 713aec199f feat(furvm): add new frame callback to executor 2026-08-12 23:44:15 +02:00
CHatingPython 15218d7530 feat(furvm): add frames function to executor 2026-08-12 23:05:37 +02:00
CHatingPython d419b62225 chore(furdb): initialize furdb 2026-08-12 14:17:16 +02:00
CHatingPython c68763f785 chore(disfuras): implement basic disfuras 2026-08-11 16:18:40 +02:00
CHatingPython dcefe20d47 refactor(furas): use new instructions from furvm 2026-08-11 16:18:17 +02:00
CHatingPython 989fcb7199 refactor(furvm): use new instructions in executor 2026-08-11 16:17:24 +02:00
CHatingPython 4cffd27a33 refactor(furvm): use furlang views 2026-08-11 16:16:30 +02:00
CHatingPython f9b910ae13 feat(furlang): add view
Add a basic view (span from C++20).
2026-08-11 16:15:48 +02:00
CHatingPython 7a7a1ec64c feat(furvm): improve instruction abstraction
Add reading and writing to instruction abstraction.
2026-08-11 13:56:34 +02:00
CHatingPython 99dba99db2 refactor(furvm): change instruction definition
Improve instruction definition for instruction abstraction.
2026-08-11 13:36:47 +02:00
CHatingPython cc88d6b6a0 refactor(furc): move IR to middle-end 2026-08-10 19:57:56 +02:00
87 changed files with 4912 additions and 1948 deletions
+3 -1
View File
@@ -17,6 +17,7 @@ Checks: >
-readability-redundant-access-specifiers, -readability-redundant-access-specifiers,
-readability-use-anyofallof, -readability-use-anyofallof,
-readability-named-parameter, -readability-named-parameter,
-readability-convert-member-functions-to-static,
-performance-enum-size, -performance-enum-size,
-cppcoreguidelines-avoid-magic-numbers, -cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-bounds-pointer-arithmetic,
@@ -33,7 +34,8 @@ Checks: >
-cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-use-enum-class, -cppcoreguidelines-use-enum-class,
-bugprone-forward-declaration-namespace, -bugprone-forward-declaration-namespace,
-bugprone-tagged-union-member-count -bugprone-tagged-union-member-count,
-bugprone-unchecked-optional-access
WarningsAsErrors: "*" WarningsAsErrors: "*"
+5 -4
View File
@@ -4,16 +4,16 @@ set -e
BUILD_DIR="build" BUILD_DIR="build"
echo "Running clang-format..." FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|hpp)$' | grep -v 'deps/' || true)
FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|hpp)$' || true)
if [ -z "$FILES" ]; then if [ -z "$FILES" ]; then
echo "No C/C++ files to check" echo "No C/C++ files to check"
exit 0 exit 0
fi fi
git diff -U0 --cached | \ echo "Running clang-format..."
git diff -U0 --cached -- $FILES | \
python3 <(curl -s https://raw.githubusercontent.com/llvm/llvm-project/refs/heads/main/clang/tools/clang-format/clang-format-diff.py) -p1 -i python3 <(curl -s https://raw.githubusercontent.com/llvm/llvm-project/refs/heads/main/clang/tools/clang-format/clang-format-diff.py) -p1 -i
echo "Running clang-tidy..." echo "Running clang-tidy..."
@@ -21,6 +21,7 @@ echo "Running clang-tidy..."
for file in $FILES; do for file in $FILES; do
clang-tidy \ clang-tidy \
"$file" \ "$file" \
--header-filter="^(?!.*deps/).*" \
-p "$BUILD_DIR" -p "$BUILD_DIR"
done done
+3
View File
@@ -0,0 +1,3 @@
[submodule "deps/isocline"]
path = deps/isocline
url = https://github.com/daanx/isocline
+10 -4
View File
@@ -11,10 +11,16 @@ find_package(Doxygen)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(furlang) add_subdirectory(src/furlang)
add_subdirectory(furvm) add_subdirectory(src/furvm)
add_subdirectory(furc) add_subdirectory(src/furc)
add_subdirectory(furas) add_subdirectory(src/furas)
add_subdirectory(src/disfuras)
add_subdirectory(src/furdb)
# Dependencies
set(IC_USE_CXX ON)
add_subdirectory(${CMAKE_SOURCE_DIR}/deps/isocline)
if(DOXYGEN_FOUND) if(DOXYGEN_FOUND)
set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/docs) set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/docs)
+1
View File
@@ -0,0 +1 @@
Checks: '-*'
Vendored Submodule
+1
Submodule deps/isocline added at 8d6dc1ef95
@@ -1,6 +1,7 @@
type arr = array $s8 10 type arr = array $s8 10
type arrSlice = slice $s8
func println $arr = native println func println $arrSlice = native println
public func main = #main public func main = #main
main: main:
@@ -30,5 +31,8 @@ body:
end: end:
drop drop
load %0 load %0
call $arr println push $u32 0
load %1
slice
call $arrSlice println
ret ret
@@ -1,22 +1,24 @@
func println $u32 = native println func println $u32 = native println
allocate uwu
public func main = #main public func main = #main
main: main:
push $u32 0 push $u32 0
store %0 storeg %uwu
loop_header: loop_header:
load %0 loadg %uwu
push $u32 10 push $u32 10
ge ge
jnz #loop_end jnz #loop_end
loop_body: loop_body:
load %0 loadg %uwu
call $u32 println call $u32 println
load %0 loadg %uwu
push $u32 1 push $u32 1
add add
store %0 storeg %uwu
jmp #loop_header jmp #loop_header
loop_end: loop_end:
+7
View File
@@ -0,0 +1,7 @@
func println $s32 = native println
public func main = #main
main:
push $s32 69
call $s32 println
ret
-5
View File
@@ -1,5 +0,0 @@
file(GLOB_RECURSE FURAS_SRCS "src/**.cpp")
file(GLOB_RECURSE FURAS_HDRS "include/**.hpp")
add_executable(furas ${FURAS_SRCS} ${FURAS_HDRS})
target_include_directories(furas PUBLIC include/)
target_link_libraries(furas PRIVATE furlang libfurvm)
View File
-16
View File
@@ -1,16 +0,0 @@
file(GLOB_RECURSE FURC_SRCS "src/**.cpp")
file(GLOB_RECURSE FURC_HDRS "include/**.hpp")
add_library(libfurc ${FURC_SRCS} ${FURC_HDRS})
target_include_directories(libfurc PUBLIC include/)
target_compile_definitions(libfurc PRIVATE LIBFURC)
set_target_properties(libfurc PROPERTIES PREFIX "")
target_link_libraries(libfurc PUBLIC furlang libfurvm)
add_executable(furc src/main.cpp)
target_link_libraries(furc PRIVATE libfurc)
include(GoogleTest)
file(GLOB_RECURSE FURC_TESTS "test/**.cpp")
add_executable(furc_tests ${FURC_TESTS})
target_link_libraries(furc_tests PRIVATE libfurc GTest::gtest_main)
# gtest_discover_tests(furc_tests)
-4
View File
@@ -1,4 +0,0 @@
file(GLOB_RECURSE FURLANG_SRCS "src/**.cpp")
file(GLOB_RECURSE FURLANG_HDRS "include/**.hpp")
add_library(furlang STATIC ${FURLANG_SRCS} ${FURLANG_HDRS})
target_include_directories(furlang PUBLIC include/)
-76
View File
@@ -1,76 +0,0 @@
#ifndef FURVM_CONSTANT_HPP
#define FURVM_CONSTANT_HPP
#include "furvm/exceptions.hpp"
#include "furvm/fwd.hpp"
#include <string_view>
namespace furvm {
enum class constant_t : std::uint8_t {
String, /**< String constant. */
};
class constant {
public:
using string_type = std::string_view; /**< String constant type. */
public:
/**
* @brief Construct a new string constant.
*
* @param string String.
*/
constant(string_type string)
: m_type(constant_t::String), m_value(string) {}
~constant() = default;
/**
* @brief Move constructor.
*/
constant(constant&&) = default;
/**
* @brief Move constructor.
*/
constant& operator=(constant&&) = default;
constant(const constant&) = delete;
constant& operator=(const constant&) = delete;
public:
/**
* @brief Returns this constant's type.
* @see constant_t
*
* @return The constant type.
*/
constexpr constant_t type() const { return m_type; }
/**
* @brief Returns this constant's string value.
* @throws bad_constant_access if this constant's type is not constant_t::String.
*
* @return The string value.
*/
constexpr string_type string() const {
require_type(constant_t::String);
return m_value.string;
}
private:
void require_type(constant_t type) const {
if (m_type != type) throw bad_constant_access();
}
private:
constant_t m_type{};
union value {
string_type string;
value(string_type sv)
: string(sv) {}
} m_value;
};
} // namespace furvm
#endif // FURVM_CONSTANT_HPP
-142
View File
@@ -1,142 +0,0 @@
#ifndef FURVM_CONTEXT_HPP
#define FURVM_CONTEXT_HPP
#include "furlang/arena.hpp"
#include "furvm/executor.hpp"
#include "furvm/fwd.hpp"
#include "furvm/handle.hpp"
#include "furvm/module.hpp" // IWYU pragma: keep
#include "furvm/thing.hpp" // IWYU pragma: keep
#include "furvm/thing_allocator.hpp"
#include <cstddef>
#include <utility>
namespace furvm {
class context : public handle_container<mod_h> {
public:
friend class executor;
public:
/**
* @brief Constructs a context.
*/
context()
: m_thingAllocator(m_thingArena) {}
~context() = default;
/**
* @brief Move constructor.
*/
context(context&&) noexcept = default;
/**
* @brief Move constructor.
*/
context& operator=(context&&) noexcept = default;
context(const context&) = delete;
context& operator=(const context&) = delete;
public:
/**
* @brief Emplaces an executor in the context.
*
* @param args Arguments forwarded to executor's constructor.
* @return A handle to the emplaced executor.
*/
template <typename... Args>
auto emplace_executor(Args&&... args) {
return m_executors.emplace_back(std::forward<Args>(args)...);
}
/**
* @brief Returns an executor from the context.
*
* @param args Id of the executor.
* @return A handle to the executor.
*/
template <typename... Args>
auto executor_at(Args&&... args) {
return m_executors.at(std::forward<Args>(args)...);
}
/**
* @brief Returns an executor from the context.
*
* @param args Id of the executor.
* @return A handle to the executor.
*/
template <typename... Args>
auto executor_at(Args&&... args) const {
return m_executors.at(std::forward<Args>(args)...);
}
/**
* @brief Erases an executor from the context.
*
* @param args Id of the executor.
*/
template <typename... Args>
void erase_executor(Args&&... args) {
m_executors.erase(std::forward<Args>(args)...);
}
public:
template <typename... Args>
auto emplace_thing(Args&&... args) {
return m_things.emplace_back(std::forward<Args>(args)...);
}
/**
* @brief Returns a thing from the context.
*
* @param args Id of the thing.
* @return A handle to the thing.
*/
template <typename... Args>
auto thing_at(Args&&... args) {
return m_things.at(std::forward<Args>(args)...);
}
/**
* @brief Returns a thing from the context.
*
* @param args Id of the thing.
* @return A handle to the thing.
*/
template <typename... Args>
auto thing_at(Args&&... args) const {
return m_things.at(std::forward<Args>(args)...);
}
/**
* @brief Erases a thing from the context.
*
* @param args Id of the thing.
*/
template <typename... Args>
void erase_thing(Args&&... args) {
m_things.erase(std::forward<Args>(args)...);
}
/**
* @brief Returns context's thing allocator.
*
* @return The thing allocator.
*/
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
thing_type_store& tt_store() { return m_thingTypeStore; }
private:
handle_container<mod_h> m_modules;
handle_container<thing_h> m_things;
handle_container<executor_h> m_executors;
furlang::arena m_thingArena;
thing_allocator<std::byte> m_thingAllocator;
class thing_type_store m_thingTypeStore;
};
} // namespace furvm
#endif // FURVM_CONTEXT_HPP
-221
View File
@@ -1,221 +0,0 @@
#ifndef FURVM_INSTRUCTION_HPP
#define FURVM_INSTRUCTION_HPP
#include "furvm/fwd.hpp"
namespace furvm {
enum class instruction_t : byte {
/**
* @brief No operation.
*/
NoOperation = 0,
/**
* @brief Pushes an s8 integer from a byte onto the stack.
*/
PushS8,
/**
* @brief Pushes an u8 integer from a byte onto the stack.
*/
PushU8,
/**
* @brief Pushes an s16 integer from two byte onto the stack.
*/
PushS16,
/**
* @brief Pushes an u16 integer from two byte onto the stack.
*/
PushU16,
/**
* @brief Pushes an s32 integer from a byte onto the stack.
*/
PushS32,
/**
* @brief Pushes an u32 integer from a byte onto the stack.
*/
PushU32,
/**
* @brief Pushes a constant onto the stack.
*
* Pushes a constant from the constant pool denoted by two next bytes in little-endian onto the stack.
*/
PushConstant,
/**
* @brief Pushes a new array onto the stack.
*
* Type is the next 4 bytes in little-endian.
* If the type is dynamic the array's size will be popped off of the stack.
*/
Array,
/**
* @brief Pushes an element from an array onto the stack.
*/
Get,
/**
* @brief Sets an array element.
*/
Set,
/**
* @brief Pops top element from the stack.
*/
Drop,
/**
* @brief Duplicates top element on the stack.
*/
Duplicate,
/**
* @brief Swaps two top elements of the stack.
*/
Swap,
/**
* @brief Clones top element on the stack.
*/
Clone,
/**
* @brief Pushes a new reference onto the stack.
*
* Pops the top thing from the stack and pushes its reference.
*/
Reference,
/**
* @brief Adds two things together on the stack.
*/
Add,
/**
* @brief Subtracts two things together on the stack.
*/
Sub,
/**
* @brief Multiplies two things together on the stack.
*/
Mul,
/**
* @brief Divides two things together on the stack.
*/
Div,
/**
* @brief Modulos two things together on the stack.
*/
Mod,
/**
* @brief Compares two top-most things from the stack for equality.
*/
Equals,
/**
* @brief Compares two top-most things from the stack for inequality.
*/
NotEquals,
/**
* @brief Compares if the first top-most thing is less than the second top-most thing.
*/
LessThan,
/**
* @brief Compares if the first top-most thing is greater than the second top-most thing.
*/
GreaterThan,
/**
* @brief Compares if the first top-most thing is less than or equal to the second top-most thing.
*/
LessEqual,
/**
* @brief Compares if the first top-most thing is greater than or equal to the second top-most thing.
*/
GreaterEqual,
/**
* @brief Pushes a pointer of popped-off thing onto the stack.
*/
Pointerof,
/**
* @brief Pushes a size of popped-off thing onto the stack.
*/
Sizeof,
/**
* @brief Pushes a length of popped-off thing onto the stack.
*/
Lengthof,
/**
* @brief Pushes a variable onto the stack.
*
* Fetches a variable denoted by next two bytes in little-endian and pushes it onto the stack.
*/
Load,
/**
* @brief Stores an element from the stack in a variable.
*
* Pops a thing from the stack and stores it in a variable denoted by next two bytes in little-endian.
*/
Store,
/**
* @brief Calls a function.
*
* Calls a function denoted by next two bytes in little-endian from current frame's module.
*/
Call,
/**
* @brief Jumps to an instruction relative to the current instruction.
*
* Jumps to an instruction relative to the current instruction with offset denoted by next byte.
*/
Jump,
/**
* @brief Jumps to an instruction relative to the current instruction if top thing on the stack is not zero.
*
* Jumps to an instruction relative to the current instruction with offset denoted by next byte if the top thing on
* the stack is not zero (is true).
*/
JumpNotZero,
/**
* @brief Pops the current call frame.
*/
Return,
};
struct instruction {
instruction_t type; /**< Instruction type. */
/**
* @brief Instruction value.
*/
union value {
constant_index constant; /**< Constant instruction argument. */
} value; /**< Instruction value. */
};
} // namespace furvm
#endif // FURVM_INSTRUCTION_HPP
-738
View File
@@ -1,738 +0,0 @@
#ifndef FURVM_THING_HPP
#define FURVM_THING_HPP
#include "furlang/arena.hpp"
#include "furlang/utility/hash.hpp"
#include "furvm/exceptions.hpp"
#include "furvm/fwd.hpp"
#include "furvm/thing_allocator.hpp" // IWYU pragma: keep
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <functional>
#include <limits>
#include <new>
#include <stdexcept>
#include <type_traits>
#include <unordered_map>
#include <utility>
namespace furvm {
struct thing_type {
using s8 = std::int8_t;
using s16 = std::int16_t;
using s32 = std::int32_t;
using s64 = std::int64_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
struct array_value {
thing_type* type;
std::size_t size;
};
enum type { // NOLINT
S8 = 0,
S16,
S32,
S64,
U8,
U16,
U32,
U64,
Ptr,
Ref,
Array,
Count,
} type;
union value {
std::nullptr_t null = nullptr;
thing_type* typeRef;
array_value array;
value() = default;
value(thing_type* type)
: typeRef(type) {}
value(thing_type* type, std::size_t size)
: array({}) {
array.type = type;
array.size = size;
}
} value;
static constexpr thing_type_id INVALID_ID = std::numeric_limits<thing_type_id>::max();
thing_type_id id = INVALID_ID;
bool operator==(const thing_type& other) const {
if (type != other.type) return false;
switch (type) {
case S8:
case S16:
case S32:
case S64:
case U8:
case U16:
case U32:
case U64: return true;
case Ptr:
case Ref: return *value.typeRef == *other.value.typeRef;
case Array: return *value.array.type == *other.value.array.type && value.array.size == other.value.array.size;
case Count: break;
}
return false;
}
bool operator!=(const thing_type& other) const { return !this->operator==(other); }
static bool is_primitive(enum type type) {
switch (type) {
case S8:
case S16:
case S32:
case S64:
case U8:
case U16:
case U32:
case U64: return true;
case Ptr:
case Ref:
case Array: return false;
case Count: break;
}
throw std::runtime_error("unreachable");
}
static std::size_t primitive_size(enum type type) {
switch (type) {
case thing_type::S8: return sizeof(s8);
case thing_type::S16: return sizeof(s16);
case thing_type::S32: return sizeof(s32);
case thing_type::S64: return sizeof(s64);
case thing_type::U8: return sizeof(u8);
case thing_type::U16: return sizeof(u16);
case thing_type::U32: return sizeof(u32);
case thing_type::U64: return sizeof(u64);
case Ptr:
case Ref:
case Array: return 0;
case Count: break;
}
throw std::runtime_error("unreachable");
}
};
namespace detail {
struct thing_type_hash {
std::size_t operator()(const thing_type& type) const {
std::size_t seed = std::hash<decltype(type.type)>{}(type.type);
switch (type.type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64: return seed;
case thing_type::Ptr:
case thing_type::Ref: return furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.typeRef));
case thing_type::Array:
seed = furlang::utility::hash_combine(seed, thing_type_hash{}(*type.value.array.type));
seed = furlang::utility::hash_combine(seed,
std::hash<decltype(type.value.array.size)>{}(type.value.array.size));
return seed;
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
};
} // namespace detail
class thing_type_store {
public:
thing_type* insert(thing_type& type) {
if (auto it = m_typeMap.find(type); it != m_typeMap.end()) return m_map[type.id = it->second];
if (type.id == thing_type::INVALID_ID) type.id = m_counter++;
thing_type* ptr = m_arena.allocate<thing_type>(type);
m_map[type.id] = ptr;
m_typeMap[type] = type.id;
return ptr;
}
thing_type* insert(const thing_type& type) {
if (auto it = m_typeMap.find(type); it != m_typeMap.end()) return m_map[it->second];
thing_type_id id = m_counter++;
thing_type* ptr = m_arena.allocate<thing_type>(type);
m_map[id] = ptr;
m_typeMap[type] = id;
return ptr;
}
thing_type* at(thing_type_id id) const {
if (auto it = m_map.find(id); it != m_map.end()) return it->second;
return nullptr;
}
private:
furlang::arena m_arena;
std::unordered_map<thing_type_id, thing_type*> m_map;
std::unordered_map<thing_type, thing_type_id, detail::thing_type_hash> m_typeMap;
thing_type_id m_counter = 0;
};
template <template <typename> typename Allocator>
class thing final {
friend class executor;
public:
using allocator_type = Allocator<std::byte>; /**< Allocator type. */
public:
struct dynamic_array {
std::size_t size;
std::byte* data;
};
public:
/**
* @brief Constructs a thing.
*
* @param type Thing type.
* @param allocator Allocator for the thing's data.
*/
thing(const thing_type& type, const allocator_type& allocator = {})
: m_type(type), m_size(compute_size(type)), m_allocator(allocator) {
if (m_type.type == thing_type::Ref) return;
// TODO: Account for alignment
m_data = m_allocator.allocate(m_size);
std::memset(m_data, 0, m_size);
}
/**
* @brief Destructs a thing.
*/
~thing() {
if (m_type.type != thing_type::Ref && m_data != nullptr && m_size > 0) m_allocator.deallocate(m_data, m_size);
}
/**
* @brief Move constructor.
*/
thing(thing&& other) noexcept
: m_type(other.m_type), m_data(other.m_data), m_size(other.m_size), m_allocator(std::move(other.m_allocator)) {
other.m_type.type = thing_type::Count;
other.m_data = nullptr;
other.m_size = 0;
}
/**
* @brief Move constructor.
*/
thing& operator=(thing&& other) noexcept {
if (this == &other) return *this;
m_type = other.m_type;
m_data = other.m_data;
m_allocator = std::move(other.m_allocator);
other.m_type.type = thing_type::Count;
other.m_data = nullptr;
other.m_size = 0;
return *this;
}
thing(const thing&) = delete;
thing& operator=(const thing&) = delete;
public:
/**
* @brief Returns a clone of the thing.
*
* @return A clone of this thing.
*/
thing clone() const {
thing res(m_type, m_allocator);
switch (m_type.type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
case thing_type::Ptr: std::memcpy(res.m_data, m_data, m_size); return std::move(res);
case thing_type::Array: copy_list(m_type, res.m_data, m_data); return std::move(res);
case thing_type::Ref: throw std::runtime_error("cannot clone references");
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
public:
/**
* @brief Returns the thing's type.
*
* @return The type.
*/
constexpr thing_type type() const { return m_type; }
/**
* @brief Returns the thing's true type.
*
* If the thing is a reference, returns the referenced type.
*
* @return The true type.
*/
constexpr thing_type true_type() const { return (m_type.type == thing_type::Ref) ? *m_type.value.typeRef : m_type; }
/**
* @brief Checks if the thing is of a specified type.
*
* Compares the true type.
*
* @param type Type to compare.
* @return true if the types match.
*/
constexpr bool is(enum thing_type::type type) const { return true_type().type == type; }
public:
/**
* @brief Returns a raw data pointer.
*
* @return The data pointer.
*/
void* raw() { return m_data; }
/**
* @brief Returns a raw data pointer.
*
* @return The data pointer.
*/
const void* raw() const { return m_data; }
public:
/**
* @brief Returns the thing's value.
*
* @return The value.
*/
template <typename T>
T& get() {
if (compute_size_na(m_type) != sizeof(T)) throw bad_thing_access();
return *std::launder(reinterpret_cast<T*>(m_data));
}
/**
* @brief Returns the thing's value.
*
* @return The value.
*/
template <typename T>
const T& get() const {
if (compute_size_na(m_type) != sizeof(T)) throw bad_thing_access();
return *std::launder(reinterpret_cast<const T*>(m_data));
}
public:
/**
* @brief Returns a sum of two things.
*
* @param rhs Thing to sum with this thing (right-hand-side).
* @return The sum.
*/
thing add(const thing& rhs) const { return binary_op(rhs, std::plus<>{}); }
/**
* @brief Returns a difference of two things.
*
* @param rhs Thing to subtract from this thing (right-hand-side).
* @return The sum.
*/
thing sub(const thing& rhs) const { return binary_op(rhs, std::minus<>{}); }
/**
* @brief Returns a product of two things.
*
* @param rhs Thing to multiply with this thing (right-hand-side).
* @return The product.
*/
thing mul(const thing& rhs) const { return binary_op(rhs, std::multiplies<>{}); }
/**
* @brief Returns a quotient of two things.
*
* @param rhs Thing to divide this thing by (right-hand-side).
* @return The quotient.
*/
thing div(const thing& rhs) const { return binary_op(rhs, std::divides<>{}); }
/**
* @brief Returns a remainder of two things.
*
* @param rhs Thing to divide this thing by (right-hand-side).
* @return The remainder.
*/
thing mod(const thing& rhs) const { return binary_op(rhs, std::modulus<>{}); }
/**
* @brief Compares two things for equality.
*
* @param rhs Thing to compare this thing with (right-hand-side).
* @return A boolean result of the comparison in a thing form.
*/
thing equals(const thing& rhs) const { return binary_op(rhs, std::equal_to<>{}); }
/**
* @brief Compares two things for inequality.
*
* @param rhs Thing to compare this thing with (right-hand-side).
* @return A boolean result of the comparison in a thing form.
*/
thing not_equals(const thing& rhs) const { return binary_op(rhs, std::not_equal_to<>{}); }
/**
* @brief Compares if this thing is less than an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing less_than(const thing& rhs) const { return binary_op(rhs, std::less<>{}); }
/**
* @brief Compares if this thing is greater than an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing greater_than(const thing& rhs) const { return binary_op(rhs, std::greater<>{}); }
/**
* @brief Compares if this thing is less than or equal to an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing less_equals(const thing& rhs) const { return binary_op(rhs, std::less_equal<>{}); }
/**
* @brief Compares if this thing is greater than or equal to an another thing.
*
* @param rhs The another thing.
* @return A boolean result of the comparison in a thing form.
*/
thing greater_equals(const thing& rhs) const { return binary_op(rhs, std::greater_equal<>{}); }
public:
/**
* @brief Returns a largest integer value of the thing.
*
* @return The integer value.
*/
thing_type::s64 integer() const {
switch (true_type().type) {
case thing_type::S8: return get<thing_type::s8>();
case thing_type::S16: return get<thing_type::s16>();
case thing_type::S32: return get<thing_type::s32>();
case thing_type::S64: return get<thing_type::s64>();
case thing_type::U8: return get<thing_type::u8>();
case thing_type::U16: return get<thing_type::u16>();
case thing_type::U32: return get<thing_type::u32>();
case thing_type::U64: return get<thing_type::u64>();
default: throw std::runtime_error("unreachable");
}
}
void resize(thing_type::u64 newSize) {
if (!is(thing_type::Array)) throw bad_thing_access();
if (true_type().value.array.size > 0) throw std::runtime_error("cannot resize a static array");
auto& array = get<dynamic_array>();
if (newSize < 0 || newSize == array.size) return;
std::size_t innerSize = compute_size_na(*true_type().value.array.type);
std::byte* newData = new std::byte[innerSize * newSize];
std::memcpy(newData, array.data, innerSize * std::min(static_cast<thing_type::u64>(array.size), newSize));
array.size = newSize;
delete[] array.data;
array.data = newData;
}
thing at(thing_type::u64 index) const {
if (!is(thing_type::Array)) throw bad_thing_access();
std::size_t elementSize = compute_size_na(*m_type.value.array.type);
if (m_type.value.array.size == 0) {
auto& array = get<dynamic_array>();
if (index < 0 || index >= array.size) throw std::out_of_range("index out of range");
thing ref = { { thing_type::Ref, m_type.value.array.type }, m_allocator };
ref.m_data = array.data + (index * elementSize);
return ref;
}
if (index < 0 || index >= m_type.value.array.size) throw std::out_of_range("index out of range");
thing ref = { { thing_type::Ref, m_type.value.array.type }, m_allocator };
ref.m_data = m_data + (index * elementSize);
return ref;
}
thing_type::u64 length() const {
if (!is(thing_type::Array)) throw bad_thing_access();
return true_type().value.array.size == 0 ? get<dynamic_array>().size : true_type().value.array.size;
}
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
T cast_to() const {
return visit_primitive([](auto value) { return static_cast<T>(value); });
}
/**
* @brief Changes reference thing's referenced thing.
*
* Yes.
*
* @param thing Thing.
*/
void reference(const thing& thing) {
if (m_type.type != thing_type::Ref || *m_type.value.typeRef != thing.type()) throw bad_thing_access();
m_data = thing.m_data;
}
/**
* @brief Self-explainatory.
*
* TODO: Document
*/
void assign(thing&& thing) {
class thing rhs = std::move(thing);
if (true_type() != rhs.true_type()) throw std::runtime_error("thing type mismatch");
// TODO: Move this to another function
switch (true_type().type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64: std::memcpy(m_data, rhs.m_data, m_size); return;
case thing_type::Ptr:
case thing_type::Ref:
case thing_type::Array: throw std::runtime_error("unimplemented");
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
private:
static void copy_list(const thing_type& arrayType, void* dst, const void* src) {
if (arrayType.type != thing_type::Array || arrayType.value.array.type == nullptr)
throw std::runtime_error("invalid type");
const auto& innerType = *arrayType.value.array.type;
std::size_t elementSize = compute_size_na(innerType);
std::size_t size = 0;
if (arrayType.value.array.size == 0) {
const dynamic_array& srcDynArr = *std::launder(reinterpret_cast<const dynamic_array*>(src));
dynamic_array& dstDynArr = *std::launder(reinterpret_cast<dynamic_array*>(dst));
size = dstDynArr.size = srcDynArr.size;
if (dstDynArr.size < 0) {
dstDynArr.data = nullptr;
return;
}
src = srcDynArr.data;
dst = dstDynArr.data = new std::byte[dstDynArr.size];
} else {
size = arrayType.value.array.size;
}
switch (innerType.type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
case thing_type::Ptr:
case thing_type::Ref: std::memcpy(dst, src, size * elementSize); return;
case thing_type::Array:
for (std::size_t i = 0; i < size; ++i) {
copy_list(*innerType.value.array.type,
reinterpret_cast<std::byte*>(dst) + (i * elementSize),
reinterpret_cast<const std::byte*>(src) + (i * elementSize));
}
return;
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
private:
static std::size_t compute_size_na(const thing_type& type) {
switch (type.type) {
case thing_type::S8: return sizeof(thing_type::s8);
case thing_type::S16: return sizeof(thing_type::s16);
case thing_type::S32: return sizeof(thing_type::s32);
case thing_type::S64: return sizeof(thing_type::s64);
case thing_type::U8: return sizeof(thing_type::u8);
case thing_type::U16: return sizeof(thing_type::u16);
case thing_type::U32: return sizeof(thing_type::u32);
case thing_type::U64: return sizeof(thing_type::u64);
case thing_type::Ptr: return sizeof(void*);
case thing_type::Ref: return compute_size_na(*type.value.typeRef);
case thing_type::Array:
return type.value.array.size == 0 ? sizeof(dynamic_array)
: compute_size_na(*type.value.array.type) * type.value.array.size;
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
// NOTE: Align to 4 bytes
static std::size_t compute_size(const thing_type& type) { return (compute_size_na(type) + 3) & ~3; }
private:
template <typename Func>
decltype(auto) visit_primitive(Func&& func) const {
switch (true_type().type) {
case thing_type::S8: return std::forward<Func>(func)(get<thing_type::s8>());
case thing_type::S16: return std::forward<Func>(func)(get<thing_type::s16>());
case thing_type::S32: return std::forward<Func>(func)(get<thing_type::s32>());
case thing_type::S64: return std::forward<Func>(func)(get<thing_type::s64>());
case thing_type::U8: return std::forward<Func>(func)(get<thing_type::u8>());
case thing_type::U16: return std::forward<Func>(func)(get<thing_type::u16>());
case thing_type::U32: return std::forward<Func>(func)(get<thing_type::u32>());
case thing_type::U64: return std::forward<Func>(func)(get<thing_type::u64>());
default: throw bad_thing_access();
}
}
template <typename Op>
thing binary_op(const thing& rhs, const Op& op) const {
if (thing_type::is_primitive(true_type().type) && thing_type::is_primitive(true_type().type)) {
static constexpr enum thing_type::type promotions[8 * 8] = {
// S8
thing_type::S8,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::U64,
// S16
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::U64,
// S32
thing_type::S32,
thing_type::S32,
thing_type::S32,
thing_type::S64,
thing_type::S32,
thing_type::S32,
thing_type::U32,
thing_type::U64,
// S64
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::S64,
thing_type::U64,
// U8
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::U8,
thing_type::U16,
thing_type::U32,
thing_type::U64,
// U16
thing_type::S16,
thing_type::S16,
thing_type::S32,
thing_type::S64,
thing_type::U16,
thing_type::U16,
thing_type::U32,
thing_type::U64,
// U32
thing_type::S32,
thing_type::S32,
thing_type::U32,
thing_type::S64,
thing_type::U32,
thing_type::U32,
thing_type::U32,
thing_type::U64,
// U64
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
thing_type::U64,
};
enum thing_type::type resultType = promotions[true_type().type + (rhs.true_type().type * 8)];
thing res = { thing_type{ resultType }, m_allocator };
switch (resultType) {
case thing_type::S8:
res.get<thing_type::s8>() = Op{}(cast_to<thing_type::s8>(), rhs.cast_to<thing_type::s8>());
return res;
case thing_type::S16:
res.get<thing_type::s16>() = Op{}(cast_to<thing_type::s16>(), rhs.cast_to<thing_type::s16>());
return res;
case thing_type::S32:
res.get<thing_type::s32>() = Op{}(cast_to<thing_type::s32>(), rhs.cast_to<thing_type::s32>());
return res;
case thing_type::S64:
res.get<thing_type::s64>() = Op{}(cast_to<thing_type::s64>(), rhs.cast_to<thing_type::s64>());
return res;
case thing_type::U8:
res.get<thing_type::u8>() = Op{}(cast_to<thing_type::u8>(), rhs.cast_to<thing_type::u8>());
return res;
case thing_type::U16:
res.get<thing_type::u16>() = Op{}(cast_to<thing_type::u16>(), rhs.cast_to<thing_type::u16>());
return res;
case thing_type::U32:
res.get<thing_type::u32>() = Op{}(cast_to<thing_type::u32>(), rhs.cast_to<thing_type::u32>());
return res;
case thing_type::U64:
res.get<thing_type::u64>() = Op{}(cast_to<thing_type::u64>(), rhs.cast_to<thing_type::u64>());
return res;
case thing_type::Ptr: // TODO: Pointer arithmetics
case thing_type::Ref:
case thing_type::Array:
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
throw std::runtime_error("unexpected operation");
}
private:
thing_type m_type;
std::size_t m_size;
std::byte* m_data;
allocator_type m_allocator;
};
} // namespace furvm
#endif // FURVM_THING_HPP
-111
View File
@@ -1,111 +0,0 @@
#ifndef FURVM_THING_ALLOCATOR_HPP
#define FURVM_THING_ALLOCATOR_HPP
#include "furlang/arena.hpp"
#include <vector>
namespace furvm {
template <typename T>
class thing_allocator {
template <typename>
friend class thing_allocator;
using dead_things = std::vector<std::pair<T*, std::size_t>>;
public:
using value_type = T; /**< Value type. */
public:
/**
* @brief Constructs a thing allocator.
*
* @param arena Base arena allocator.
*/
explicit thing_allocator(furlang::arena& arena) noexcept
: m_arena(&arena), m_deadThings(std::make_shared<dead_things>()) {}
/**
* @brief Move constructor.
*/
template <typename U>
thing_allocator(thing_allocator<U>&& other) noexcept
: m_arena(std::move(other.m_arena)), m_deadThings(std::move(other.m_deadThings)) {}
/**
* @brief Move constructor.
*/
template <typename U>
thing_allocator& operator=(thing_allocator<U>&& other) noexcept {
if (this == &other) return *this;
m_arena = std::move(other.m_arena);
m_deadThings = std::move(other.m_deadThings);
return *this;
}
/**
* @brief Copy constructor.
*/
template <typename U>
thing_allocator(const thing_allocator<U>& other) noexcept
: m_arena(other.m_arena), m_deadThings(other.m_deadThings) {}
/**
* @brief Copy constructor.
*/
template <typename U>
thing_allocator& operator=(const thing_allocator<U>& other) noexcept {
if (this == &other) return *this;
m_arena = other.m_arena;
m_deadThings = other.m_deadThings;
return *this;
}
public:
/**
* @brief Returns a free chunk of memory.
*
* @param count Count of the things that must fit inside the chunk.
* @return The chunk.
*/
[[nodiscard]] T* allocate(std::size_t count = 1) {
for (auto it = m_deadThings->begin(); it != m_deadThings->end(); ++it) {
if (it->second != count) continue;
T* data = it->first;
m_deadThings->erase(it);
return data;
}
return m_arena->allocate<T>(count);
}
/**
* @brief Recycles the pointer.
*/
void deallocate(T* ptr, std::size_t count) noexcept { m_deadThings->emplace_back(ptr, count); }
public:
/**
* @brief Compares two thing allocators for equality.
*
* @return true if the two things are equal.
*/
template <typename U>
bool operator==(const thing_allocator<U>& other) const noexcept {
return m_arena == other.m_arena && m_deadThings == other.m_deadThings;
}
/**
* @brief Compares two thing allocators for inequality.
*
* @return true if the two things are not equal.
*/
template <typename U>
bool operator!=(const thing_allocator<U>& other) const noexcept {
return m_arena != other.m_arena || m_deadThings != other.m_deadThings;
}
private:
furlang::arena* m_arena;
std::shared_ptr<dead_things> m_deadThings;
};
} // namespace furvm
#endif // FURVM_THING_ALLOCATOR_HPP
-342
View File
@@ -1,342 +0,0 @@
#include "furvm/executor.hpp"
#include "furvm/context.hpp" // IWYU pragma: keep
#include "furvm/exceptions.hpp"
#include "furvm/function.hpp" // IWYU pragma: keep
#include "furvm/fwd.hpp"
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <utility>
#include <vector>
namespace furvm {
thing_type executor::thing_type_impl(mod_h mod, mod_type type) const {
while (type.type == mod_type::Import) {
auto imprt = std::move(type.value.imprt);
mod = m_context->at(imprt.modId);
type = *mod->type_at(imprt.typeId);
}
switch (static_cast<enum thing_type::type>(type.type)) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64: return { static_cast<enum thing_type::type>(type.type) };
case thing_type::Ptr: return { thing_type::Ptr, mod_to_thing_type(mod, *mod->type_at(type.value.typeRef)) };
case thing_type::Array: {
return { static_cast<enum thing_type::type>(type.type),
{ mod_to_thing_type(mod, *mod->type_at(type.value.array.typeId)), type.value.array.size } };
}
default: throw std::runtime_error("invalid thing type");
}
}
thing_type* executor::mod_to_thing_type(const mod_h& mod, const mod_type& type) const {
struct thing_type thingType = thing_type_impl(mod, type);
return m_context->tt_store().insert(thingType);
}
void executor::push_frame(const mod_h& mod, function function) {
mod_h modInst = mod;
while (function.type() == function_t::Import) {
modInst = m_context->at(function.imp().mod);
function = *modInst->function_at(function.imp().function);
}
auto signature = function.signature();
std::vector<thing_h> args;
args.reserve(signature.params.size());
for (const auto& param : signature.params) {
auto arg = pop_thing();
if (arg->type() != *mod_to_thing_type(mod, *param)) throw std::runtime_error("function argument type mismatch");
args.push_back(std::move(arg));
}
struct thing_type* returnType = nullptr;
if (function.signature().returnType.has_value())
returnType = mod_to_thing_type(mod, *function.signature().returnType.value()); // NOLINT
switch (function.type()) {
case function_t::Normal: {
m_frames.emplace(
(struct executor::frame){ mod, function.position(), m_stack.size(), returnType, std::move(args) });
} break;
case function_t::Native: {
m_frames.emplace((struct executor::frame){ mod, 0, m_stack.size(), returnType, std::move(args) });
modInst->get_native_function(function.native())(*this);
pop_frame();
} break;
default: throw std::runtime_error("unexpected function type");
}
}
struct executor::frame executor::pop_frame() {
if (m_frames.empty()) throw stack_underflow();
struct executor::frame frame = m_frames.top();
m_frames.pop();
thing_h returnValue;
if (frame.returnType != nullptr) returnValue = pop_thing();
if (m_stack.size() != frame.stackBase) throw std::runtime_error("unexhausted stack");
if (frame.returnType != nullptr) push_thing(std::move(returnValue));
return frame;
}
struct executor::frame executor::frame() const {
return m_frames.top();
}
thing_h executor::push_thing(::furvm::thing<>&& thing) {
return m_stack.emplace(m_context->emplace_thing(std::move(thing)));
}
thing_h executor::pop_thing() {
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
thing_h top = std::move(m_stack.top());
m_stack.pop();
return std::move(top);
}
thing_h executor::thing() const {
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
return m_stack.top();
}
void executor::store_thing(variable_t variable, const thing_h& thing) {
auto& frame = m_frames.top();
if (frame.variables.size() <= variable) frame.variables.resize(variable + 1);
frame.variables[variable] = thing;
}
void executor::store_thing(variable_t variable, thing_h&& thing) {
auto& frame = m_frames.top();
if (frame.variables.size() <= variable) frame.variables.resize(variable + 1);
frame.variables[variable] = std::move(thing);
}
thing_h executor::load_thing(variable_t variable) const {
const auto& frame = m_frames.top();
return { frame.variables[variable] };
}
void executor::step() {
if ((m_flags & executor_flags::Suspended) == executor_flags::Suspended) return;
struct frame& frame = m_frames.top();
instruction_t instr = static_cast<instruction_t>((*frame.mod).byte_at(frame.position++));
switch (instr) {
case instruction_t::NoOperation: break;
case instruction_t::PushS8: {
push_thing({ (struct thing_type){ thing_type::S8 }, m_context->thing_alloc() })->get<thing_type::s8>() =
static_cast<thing_type::s8>(frame.mod->byte_at(frame.position++));
} break;
case instruction_t::PushU8: {
push_thing({ (struct thing_type){ thing_type::U8 }, m_context->thing_alloc() })->get<thing_type::u8>() =
static_cast<thing_type::u8>(frame.mod->byte_at(frame.position++));
} break;
case instruction_t::PushS16: {
thing_type::u16 value = frame.mod->byte_at(frame.position++);
value |= static_cast<thing_type::u16>(frame.mod->byte_at(frame.position++) << 8);
push_thing({ (struct thing_type){ thing_type::S16 }, m_context->thing_alloc() })->get<thing_type::s16>() =
static_cast<thing_type::s16>(value);
} break;
case instruction_t::PushU16: {
thing_type::u16 value = frame.mod->byte_at(frame.position++);
value |= static_cast<thing_type::u16>(frame.mod->byte_at(frame.position++) << 8);
push_thing({ (struct thing_type){ thing_type::U16 }, m_context->thing_alloc() })->get<thing_type::u16>() =
value;
} break;
case instruction_t::PushS32: {
push_thing({ (struct thing_type){ thing_type::S32 }, m_context->thing_alloc() })->get<thing_type::s32>() =
static_cast<thing_type::s32>(frame.mod->byte_at(frame.position++));
} break;
case instruction_t::PushU32: {
push_thing({ (struct thing_type){ thing_type::U32 }, m_context->thing_alloc() })->get<thing_type::u32>() =
static_cast<thing_type::u32>(frame.mod->byte_at(frame.position++));
} break;
case instruction_t::Array: {
mod_type_id typeId = static_cast<mod_type_id>(frame.mod->byte_at(frame.position)) |
(static_cast<mod_type_id>(frame.mod->byte_at(frame.position + 1)) << 8) |
(static_cast<mod_type_id>(frame.mod->byte_at(frame.position + 2)) << 16) |
(static_cast<mod_type_id>(frame.mod->byte_at(frame.position + 3)) << 24);
frame.position += 4;
const auto& type = *mod_to_thing_type(frame.mod, *frame.mod->type_at(typeId));
if (type.type != thing_type::Array || type.value.array.type == nullptr || type.value.array.type == &type)
throw std::runtime_error("invalid array type");
auto array = push_thing({ type, m_context->thing_alloc() });
if (type.value.array.size == 0) {
auto sizeThing = pop_thing();
std::int64_t size = sizeThing->integer();
array->resize(size);
}
} break;
case instruction_t::Get: {
auto index = pop_thing();
auto array = pop_thing();
push_thing(array->at(index->integer()));
} break;
case instruction_t::Set: {
auto element = pop_thing();
auto index = pop_thing();
auto array = pop_thing();
array->at(index->integer()).assign(std::move(element->clone()));
} break;
case instruction_t::Drop: {
pop_thing();
} break;
case instruction_t::Duplicate: {
push_thing(thing());
} break;
case instruction_t::Swap: {
auto thing1 = pop_thing();
auto thing2 = pop_thing();
push_thing(std::move(thing1));
push_thing(std::move(thing2));
} break;
case instruction_t::Clone: {
push_thing(std::move(thing()->clone()));
} break;
case instruction_t::Reference: {
auto thing = pop_thing();
push_thing({ (struct thing_type){ thing_type::Ref, m_context->tt_store().insert(thing->type()) },
m_context->thing_alloc() })
->reference(*thing);
} break;
case instruction_t::Add: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->add(*rhs));
} break;
case instruction_t::Sub: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->sub(*rhs));
} break;
case instruction_t::Mul: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->mul(*rhs));
} break;
case instruction_t::Div: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->div(*rhs));
} break;
case instruction_t::Mod: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->mod(*rhs));
} break;
case instruction_t::Equals: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->equals(*rhs));
} break;
case instruction_t::NotEquals: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->not_equals(*rhs));
} break;
case instruction_t::LessThan: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->less_than(*rhs));
} break;
case instruction_t::GreaterThan: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->greater_than(*rhs));
} break;
case instruction_t::LessEqual: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->less_equals(*rhs));
} break;
case instruction_t::GreaterEqual: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs->greater_equals(*rhs));
} break;
case instruction_t::Pointerof: {
auto thing = pop_thing();
auto ptr = push_thing({ (struct thing_type){ thing_type::Ptr, m_context->tt_store().at(thing->type().id) },
m_context->thing_alloc() });
ptr->get<void*>() = thing->raw();
} break;
case instruction_t::Sizeof: {
auto thing = pop_thing();
auto size = push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() });
switch (thing->type().type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
size->get<thing_type::u64>() = static_cast<thing_type::u64>(thing_type::primitive_size(thing->type().type));
break;
case thing_type::Ptr: size->get<thing_type::u64>() = static_cast<thing_type::u64>(sizeof(void*)); break;
case thing_type::Array:
/* TODO: Return actual memory size of the array
* By the memory size I mean the length times sizeof single element.
*/
size->get<thing_type::u64>() = thing->length();
break;
default: throw std::runtime_error("unreachable");
}
} break;
case instruction_t::Lengthof: {
auto thing = pop_thing();
auto length = push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() });
length->get<thing_type::u64>() = thing->length();
} break;
case instruction_t::Load: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte_at(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte_at(frame.position + 1)) << 8);
frame.position += 2;
push_thing(load_thing(variable));
} break;
case instruction_t::Store: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte_at(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte_at(frame.position + 1)) << 8);
frame.position += 2;
store_thing(variable, std::move(pop_thing()));
} break;
case instruction_t::Call: {
function_id funcId = static_cast<std::uint16_t>(frame.mod->byte_at(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte_at(frame.position + 1)) << 8);
frame.position += 2;
push_frame(frame.mod, *frame.mod->function_at(funcId));
} break;
case instruction_t::Jump: {
std::int8_t offset = static_cast<std::int8_t>(frame.mod->byte_at(frame.position++));
frame.position += offset;
} break;
case instruction_t::JumpNotZero: {
byte offset = frame.mod->byte_at(frame.position++);
auto cond = pop_thing();
if (cond->integer() != 0) frame.position += (std::int8_t)offset;
} break;
case instruction_t::Return: {
pop_frame();
if (m_frames.empty()) m_flags = m_flags | executor_flags::Done;
} break;
case instruction_t::PushConstant: throw std::runtime_error("unimplemented");
default: throw std::runtime_error("unknown instruction");
}
}
} // namespace furvm
-28
View File
@@ -1,28 +0,0 @@
#include "furlang/arena.hpp"
#include "furvm/furvm.hpp"
#include "furvm/thing.hpp"
#include "furvm/thing_allocator.hpp"
#include "gtest/gtest.h" // IWYU pragma: keep
namespace {
// TODO: Basic program tests (e.g. for loops)
TEST(Things, Ops) {
furlang::arena arena;
furvm::thing_allocator<std::byte> alloc{ arena };
furvm::thing lhs{ furvm::thing_type{ furvm::thing_type::U32 }, alloc };
lhs.get<furvm::thing_type::u32>() = 6;
furvm::thing rhs{ furvm::thing_type{ furvm::thing_type::U32 }, alloc };
rhs.get<furvm::thing_type::u32>() = 7;
auto res = lhs.add(rhs);
ASSERT_EQ(res.type().type, furvm::thing_type::U32);
EXPECT_EQ(lhs.get<furvm::thing_type::u32>(), 6);
EXPECT_EQ(rhs.get<furvm::thing_type::u32>(), 7);
EXPECT_EQ(res.get<furvm::thing_type::u32>(), 6 + 7);
}
} // namespace
@@ -24,7 +24,7 @@ struct lexer_error {
std::string message; std::string message;
}; };
using token_r = furlang::result<token, lexer_error>; using token_r = furlang::result<lexer_error, token>;
class lexer { class lexer {
public: public:
@@ -29,10 +29,12 @@ struct token {
Import, /**< `import` keyword for importing functions and types. */ Import, /**< `import` keyword for importing functions and types. */
Public, /**< `public` access specifier. */ Public, /**< `public` access specifier. */
Private, /**< `private` access specifier. */ Private, /**< `private` access specifier. */
Allocate, /**< `allocate` keyword for global variables. */
// Instructions // Instructions
Push, Push,
Array, Array,
Slice,
Get, Get,
Set, Set,
Drop, Drop,
@@ -56,6 +58,8 @@ struct token {
Lenof, Lenof,
Load, Load,
Store, Store,
LoadGlobal,
StoreGlobal,
Call, Call,
Jmp, Jmp,
Jnz, Jnz,
+16
View File
@@ -0,0 +1,16 @@
#ifndef FURC_BACK_FURVM_HPP
#define FURC_BACK_FURVM_HPP
#include "furc/middle/ir.hpp"
#include "furvm/module.hpp"
namespace furc {
class furvm_generator final {
public:
static furvm::mod generate(const ir_module& mod);
};
} // namespace furc
#endif // FURC_BACK_FURVM_HPP
@@ -1,5 +1,5 @@
#ifndef FURC_BACK_IR_HPP #ifndef FURC_MIDDLE_IR_HPP
#define FURC_BACK_IR_HPP #define FURC_MIDDLE_IR_HPP
#include "furc/front/ast.hpp" #include "furc/front/ast.hpp"
#include "furlang/arena.hpp" #include "furlang/arena.hpp"
@@ -9,6 +9,7 @@
#include <initializer_list> #include <initializer_list>
#include <optional> #include <optional>
#include <stack> #include <stack>
#include <stdexcept>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <unordered_map> #include <unordered_map>
@@ -22,6 +23,7 @@ struct ir_operand {
Integer = 0, Integer = 0,
Register, Register,
Variable, Variable,
Global,
Function, Function,
Block, Block,
BlockPair, BlockPair,
@@ -32,8 +34,19 @@ struct ir_operand {
struct register_s { struct register_s {
std::uint64_t name : 54; std::uint64_t name : 54;
std::uint64_t ver : 10; std::uint64_t ver : 10;
register_s() = default;
register_s(std::uint64_t id)
: name((id >> 10) & ((1ULL << 54) - 1)), ver((id >> 0) & ((1 << 10) - 1)) {}
register_s(std::uint64_t name, std::uint64_t ver)
: name(name), ver(ver) {}
operator std::uint64_t() const { return name << 10 | ver; }
} reg; } reg;
std::uint16_t variable; std::uint16_t variable;
std::uint16_t global;
std::uint64_t function; std::uint64_t function;
std::uint64_t block; std::uint64_t block;
struct block_pair_s { struct block_pair_s {
@@ -53,6 +66,9 @@ struct ir_operand {
value_u(std::uint16_t variable) value_u(std::uint16_t variable)
: variable(variable) {} : variable(variable) {}
value_u(register_s reg)
: reg(reg) {}
value_u(std::uint64_t first, std::uint64_t second) value_u(std::uint64_t first, std::uint64_t second)
: blockPair({ first, second }) {} : blockPair({ first, second }) {}
@@ -63,6 +79,30 @@ struct ir_operand {
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<value_u, Args...>>> template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<value_u, Args...>>>
ir_operand(type_e type, Args&&... args) ir_operand(type_e type, Args&&... args)
: type(type), value(std::forward<Args>(args)...) {} : type(type), value(std::forward<Args>(args)...) {}
static ir_operand reg(std::uint64_t name, std::uint64_t ver) {
return { Register, value_u::register_s{ name, ver } };
}
bool operator==(const ir_operand& rhs) const {
if (type != rhs.type) return false;
switch (type) {
case Integer: return value.integer == rhs.value.integer;
case Register: return value.reg.name == rhs.value.reg.name && value.reg.ver == rhs.value.reg.ver;
case Variable: return value.variable == rhs.value.variable;
case Global: return value.global == rhs.value.global;
case Function: return value.function == rhs.value.function;
case Block: return value.block == rhs.value.block;
case BlockPair:
return value.blockPair.first == rhs.value.blockPair.first &&
value.blockPair.second == rhs.value.blockPair.second;
case PhiPair:
return value.phiPair.block == rhs.value.phiPair.block &&
value.phiPair.reg.name == rhs.value.phiPair.reg.name &&
value.phiPair.reg.ver == rhs.value.phiPair.reg.ver;
}
throw std::runtime_error("unreachable");
}
}; };
struct ir_type { struct ir_type {
@@ -137,6 +177,10 @@ struct ir_instruction {
default: return false; default: return false;
} }
} }
bool operator==(const ir_instruction& rhs) const {
return type == rhs.type && destination == rhs.destination && sources == rhs.sources;
}
}; };
struct ir_basic_block { struct ir_basic_block {
@@ -172,7 +216,7 @@ struct ir_module_variable : ir_variable {
std::uint16_t name; std::uint16_t name;
ir_operand operand() const final { return { ir_operand::Variable, name }; } ir_operand operand() const final { return { ir_operand::Global, name }; }
}; };
struct ir_function_variable : ir_variable { struct ir_function_variable : ir_variable {
@@ -181,7 +225,7 @@ struct ir_function_variable : ir_variable {
std::uint64_t name; std::uint64_t name;
ir_operand operand() const final { return { ir_operand::Register, name }; } ir_operand operand() const final { return { ir_operand::Variable, name }; }
}; };
struct ir_scope { struct ir_scope {
@@ -223,9 +267,10 @@ struct ir_function : ir_scope {
std::vector<ir_basic_block> blocks; std::vector<ir_basic_block> blocks;
std::uint64_t regCount = 0; std::uint64_t regCount = 0;
std::uint64_t varCount = 0;
const ir_variable* allocate(furlang::arena& arena, const std::string& name, ir_type type) final { const ir_variable* allocate(furlang::arena& arena, const std::string& name, ir_type type) final {
return variables[name] = arena.allocate<ir_function_variable>(type, regCount++); return variables[name] = arena.allocate<ir_function_variable>(type, varCount++);
} }
static ir_function from_name(std::string&& name) { static ir_function from_name(std::string&& name) {
@@ -302,7 +347,11 @@ struct ir_context {
void terminate() { add_instr(ir_instruction::Return); } void terminate() { add_instr(ir_instruction::Return); }
void terminate(ir_operand value) { add_instr(ir_instruction::Return, value); } void terminate(ir_operand value) {
ir_instruction instr = { ir_instruction::Return };
instr.sources.emplace_back(value);
add_instr(std::move(instr));
}
void terminate(std::uint64_t block) { add_instr(ir_instruction::Branch, ir_operand{ ir_operand::Block, block }); } void terminate(std::uint64_t block) { add_instr(ir_instruction::Branch, ir_operand{ ir_operand::Block, block }); }
@@ -403,4 +452,4 @@ private:
} // namespace furc } // namespace furc
#endif // FURC_BACK_IR_HPP #endif // FURC_MIDDLE_IR_HPP
+177
View File
@@ -0,0 +1,177 @@
/**
* SSA destruction (out-of-SSA phase) for register-based targets based on "Mechanizing Conventional SSA for a
* Verified Destruction with Coalescing" by Delphine Demange and Yon Fernandez de Retana
* (https://dl.acm.org/doi/pdf/10.1145/2892208.2892222 09/11/2026).
*/
#ifndef FURC_MIDDLE_REG_GEN_HPP
#define FURC_MIDDLE_REG_GEN_HPP
#include "furc/middle/ir.hpp"
#include "furc/middle/ssa.hpp"
#include <cassert>
#include <cstdint>
#include <unordered_set>
#include <vector>
namespace furc {
class reg_gen {
public:
class disjoint_set {
public:
std::uint64_t find(std::uint64_t var) {
if (m_parents.find(var) == m_parents.end()) m_parents.emplace(var, var);
if (m_parents[var] == var) return var;
return find(m_parents[var]);
}
void unite(std::uint64_t var1, std::uint64_t var2) {
std::uint64_t rep1 = find(var1);
std::uint64_t rep2 = find(var2);
if (rep1 != rep2) m_parents[rep1] = rep2;
}
private:
std::unordered_map<std::uint64_t, std::uint64_t> m_parents;
};
public:
struct block_info {
std::unordered_set<std::uint64_t> defs;
std::unordered_set<std::uint64_t> uses;
std::unordered_set<std::uint64_t> liveIn;
std::unordered_set<std::uint64_t> liveOut;
};
public:
reg_gen(ir_function& func, ssa& ssa) {
std::vector<block_info> lifeBlocks;
live_analysis(lifeBlocks, func.blocks, ssa.cfgBlocks);
remove_interference(func.blocks, lifeBlocks);
merge(func.blocks);
}
public:
static void live_analysis(std::vector<block_info>& lifeBlocks,
const std::vector<ir_basic_block>& irBlocks,
const std::vector<ssa::cfg_block>& cfgBlocks) {
lifeBlocks.resize(irBlocks.size());
for (std::uint64_t i = 0; i < irBlocks.size(); ++i) {
const auto& irBlock = irBlocks[i];
auto& block = lifeBlocks[i];
for (const auto& instr : irBlock.instructions) {
for (const auto& op : instr.sources) {
if (op.type != ir_operand::Register) continue;
if (block.defs.find(op.value.reg) != block.defs.end()) continue;
block.uses.insert(op.value.reg);
}
if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
block.defs.insert(instr.destination->value.reg);
}
}
bool changed = true;
while (changed) {
changed = false;
for (std::uint64_t i = 0; i < irBlocks.size(); ++i) {
const auto& irBlock = irBlocks[i];
auto& block = lifeBlocks[i];
std::unordered_set<std::uint64_t> newSet;
for (auto succ : cfgBlocks[i].sucs) {
newSet.insert(lifeBlocks[succ].liveIn.begin(), lifeBlocks[succ].liveIn.end());
}
if (newSet != block.liveOut) {
block.liveOut = newSet;
changed = true;
}
newSet.clear();
newSet.insert(block.uses.begin(), block.uses.end());
for (const auto& var : block.liveOut) {
if (block.defs.find(var) != block.defs.end()) continue;
newSet.insert(var);
}
if (newSet != block.liveIn) {
block.liveIn = newSet;
changed = true;
}
}
}
}
static void remove_interference(std::vector<ir_basic_block>& irBlocks, const std::vector<block_info>& lifeBlocks) {
for (std::uint64_t i = 0; i < irBlocks.size(); ++i) {
auto& irBlock = irBlocks[i];
for (auto it = irBlock.instructions.begin(), end = irBlock.instructions.end();
it != end && it->type == ir_instruction::Phi;
++it) {
assert(it->destination.has_value() && it->destination->type == ir_operand::Register);
const auto& phiDst = it->destination->value.reg;
for (auto& op : it->sources) {
assert(op.type == ir_operand::PhiPair);
const auto& predBlock = lifeBlocks[op.value.phiPair.block];
auto& phiArg = op.value.phiPair.reg;
if (predBlock.liveOut.count(phiArg) == 0 || phiArg == phiDst) continue;
auto oldArg = phiArg;
phiArg.ver = 0; // TODO: Allocate temporary registers
auto& irBlock = irBlocks[op.value.phiPair.block];
assert(!irBlock.instructions.empty());
auto it = irBlock.instructions.end() - 1;
if (ir_instruction::is_terminating(it->type)) --it;
irBlock.instructions.emplace(it,
ir_instruction{ ir_instruction::Move,
ir_operand::reg(phiArg.name, phiArg.ver),
{ ir_operand::reg(oldArg.name, oldArg.ver) } });
}
}
}
}
static void merge(std::vector<ir_basic_block>& irBlocks) {
disjoint_set dj;
for (const auto& block : irBlocks) {
for (const auto& instr : block.instructions) {
if (instr.type != ir_instruction::Phi) break;
assert(instr.destination.has_value() && instr.destination->type == ir_operand::Register);
const auto& phiDst = instr.destination->value.reg;
dj.find(phiDst);
for (const auto& op : instr.sources) {
assert(op.type == ir_operand::PhiPair);
const auto& predBlock = op.value.phiPair.block;
const auto& phiArg = op.value.phiPair.reg;
dj.unite(phiDst, phiArg);
}
}
}
for (auto& block : irBlocks) {
auto it = block.instructions.begin();
while (it != block.instructions.end() && it->type == ir_instruction::Phi) {
it = block.instructions.erase(it);
}
for (; it != block.instructions.end(); ++it) {
for (auto& op : it->sources) {
if (op.type != ir_operand::Register) continue;
op.value.reg = dj.find(op.value.reg);
}
if (!it->destination.has_value() || it->destination->type != ir_operand::Register) continue;
it->destination->value.reg = dj.find(it->destination->value.reg);
}
}
}
};
} // namespace furc
#endif // FURC_MIDDLE_REG_GEN_HPP
+101
View File
@@ -0,0 +1,101 @@
#ifndef FURC_MIDDLE_SSA_HPP
#define FURC_MIDDLE_SSA_HPP
#include "furc/middle/ir.hpp"
#include <cassert>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace furc {
class ssa {
public:
struct cfg_block {
std::unordered_set<std::uint64_t> preds;
std::unordered_set<std::uint64_t> sucs;
};
struct ssa_block {
std::size_t order = 0;
std::uint64_t idom = -1;
std::unordered_set<std::uint64_t> children; // Children of the block in dominator tree
// Dominance Frontiers
std::unordered_set<std::uint64_t> df;
};
struct register_info {
std::unordered_set<std::uint64_t> sites; // Definition Sites
};
public:
ssa(ir_function& func) {
registers.resize(func.regCount);
compute_cfg(func.blocks, cfgBlocks);
collect_registers(func.blocks, registers, globals);
std::vector<std::uint64_t> order;
compute_rpo(cfgBlocks, ssaBlocks, order);
build_dtree(cfgBlocks, ssaBlocks, order);
compute_dfrontiers(cfgBlocks, ssaBlocks);
ssaification(func.blocks, cfgBlocks, ssaBlocks, registers, globals);
rename(func.blocks, func.regCount, cfgBlocks, ssaBlocks, order);
}
public:
static void compute_cfg(const std::vector<ir_basic_block>& irBlocks, std::vector<cfg_block>& cfgBlocks);
static void collect_registers(const std::vector<ir_basic_block>& irBlocks,
std::vector<register_info>& registers,
std::unordered_set<std::uint64_t>& globals);
static void build_dtree(const std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
const std::vector<std::size_t>& order);
static void compute_dfrontiers(const std::vector<cfg_block>& cfgBlocks, std::vector<ssa_block>& ssaBlocks);
static void compute_rpo(std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
std::vector<std::size_t>& order);
static void ssaification(std::vector<ir_basic_block>& irBlocks,
const std::vector<cfg_block>& cfgBlocks,
const std::vector<ssa_block>& ssaBlocks,
const std::vector<register_info>& registers,
const std::unordered_set<std::uint64_t>& globals);
static void rename(std::vector<ir_basic_block>& irBlocks,
std::size_t regCount,
const std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
const std::vector<std::uint64_t>& order);
private:
static void rename_rec(std::vector<std::uint64_t>& counters,
std::vector<std::stack<std::uint64_t>>& stacks,
std::vector<ir_basic_block>& irBlocks,
const std::vector<cfg_block>& cfgBlocks,
const std::vector<ssa_block>& ssaBlocks,
std::size_t blockIdx);
private:
static void rpo_dfs(std::unordered_set<std::size_t>& visited,
std::vector<std::size_t>& order,
std::size_t block,
const std::vector<cfg_block>& blocks);
static std::size_t intersect(std::vector<ssa_block>& m_blocks, std::size_t b1, std::size_t b2);
public:
std::vector<cfg_block> cfgBlocks;
std::vector<ssa_block> ssaBlocks;
std::vector<register_info> registers;
std::unordered_set<std::uint64_t> globals;
};
} // namespace furc
#endif // FURC_MIDDLE_SSA_HPP
@@ -2,7 +2,9 @@
#define FURLANG_RESULT_HPP #define FURLANG_RESULT_HPP
#include <exception> #include <exception>
#include <optional>
#include <ostream> #include <ostream>
#include <type_traits>
#include <utility> #include <utility>
namespace furlang { namespace furlang {
@@ -43,6 +45,8 @@ public:
const char* what() const noexcept override { return "bad result access"; } const char* what() const noexcept override { return "bad result access"; }
}; };
struct error_tag {};
/** /**
* @brief Result. * @brief Result.
* *
@@ -51,7 +55,7 @@ public:
* @tparam R Value type. * @tparam R Value type.
* @tparam E Error type. * @tparam E Error type.
*/ */
template <typename R, typename E> template <typename E, typename R = void>
class result { class result {
public: public:
using value_type = std::remove_reference_t<R>; /**< Value type. */ using value_type = std::remove_reference_t<R>; /**< Value type. */
@@ -63,6 +67,10 @@ public:
using error_reference = error_type&; /**< Error reference type. */ using error_reference = error_type&; /**< Error reference type. */
using error_const_reference = const error_type&; /**< Error const reference type. */ using error_const_reference = const error_type&; /**< Error const reference type. */
public: public:
template <typename Other>
result(const result<E, Other>& error)
: result(error_tag{}, error.error()) {}
/** /**
* @brief Construct a new result. * @brief Construct a new result.
* *
@@ -82,7 +90,7 @@ public:
* *
* @param args Variadic arguments to construct the value with. * @param args Variadic arguments to construct the value with.
*/ */
template <typename... Args> template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<value_type, Args...>>>
result(Args&&... args) { result(Args&&... args) {
new (&m_value.result) value_type(std::forward<Args>(args)...); new (&m_value.result) value_type(std::forward<Args>(args)...);
} }
@@ -92,7 +100,7 @@ public:
* *
* @param error Error to copy. * @param error Error to copy.
*/ */
explicit result(const error_type& error) result(error_tag tag, const error_type& error)
: m_error(true) { : m_error(true) {
new (&m_value.error) error_type(error); new (&m_value.error) error_type(error);
} }
@@ -102,7 +110,7 @@ public:
* *
* @param error Error to move. * @param error Error to move.
*/ */
explicit result(error_type&& error) result(error_tag tag, error_type&& error)
: m_error(true) { : m_error(true) {
new (&m_value.error) error_type(std::move(error)); new (&m_value.error) error_type(std::move(error));
} }
@@ -166,6 +174,16 @@ public:
} }
return *this; return *this;
} }
public:
template <typename ResultFwd, typename = std::enable_if_t<std::is_constructible_v<R, ResultFwd>>>
static result ok(ResultFwd&& value) {
return { std::forward<ResultFwd>(value) };
}
template <typename ErrorFwd, typename = std::enable_if_t<std::is_constructible_v<E, ErrorFwd>>>
static result error(ErrorFwd&& value) {
return { error_tag{}, std::forward<ErrorFwd>(value) };
}
public: public:
/** /**
* @brief Checks if this result contains a value. * @brief Checks if this result contains a value.
@@ -364,6 +382,34 @@ private:
bool m_error = false; bool m_error = false;
}; };
template <typename E>
class result<E, void> {
public:
using value_type = std::remove_reference_t<E>;
using reference = value_type&;
using const_reference = const value_type&;
public:
result() = default;
result(const value_type& value)
: m_error(true), m_value(value) {}
result(value_type&& value)
: m_error(true), m_value(std::move(value)) {}
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<value_type, Args...>>>
result(Args&&... args)
: m_error(true), m_value(std::forward<Args>(args)...) {}
public:
bool has_value() const { return !m_error; }
bool has_error() const { return m_error; }
const value_type& error() const { return *m_value; }
private:
std::optional<value_type> m_value;
bool m_error = false;
};
} // namespace furlang } // namespace furlang
#endif // FURLANG_RESULT_HPP #endif // FURLANG_RESULT_HPP
+58
View File
@@ -0,0 +1,58 @@
#ifndef FURLANG_SERIALIZATION_CODEC_HPP
#define FURLANG_SERIALIZATION_CODEC_HPP
#include "furlang/result.hpp"
#include "furlang/serialization/io.hpp"
#include <type_traits>
#include <utility>
namespace furlang {
namespace serialization {
template <typename Codec, typename T>
using codec_encode_result_t = decltype(std::declval<Codec>().encode(std::declval<writer&>(), std::declval<const T&>()));
template <typename Codec, typename T>
using codec_decode_result_t = decltype(std::declval<Codec>().decode(std::declval<reader&>()));
template <typename Codec, typename T, typename = void>
struct is_codec : std::false_type {};
template <typename Codec, typename T>
struct is_codec<Codec, T, std::void_t<codec_encode_result_t<Codec, T>, codec_decode_result_t<Codec, T>>>
: std::true_type {};
template <typename Codec, typename T>
constexpr bool is_codec_v = is_codec<Codec, T>::value;
template <typename Codec, typename T, typename = std::enable_if_t<is_codec_v<Codec, T>>>
result<error> encode(Codec& codec, writer& writer, const T& value) {
return codec.encode(writer, value);
}
template <typename Codec, typename T, typename = std::enable_if_t<is_codec_v<Codec, T>>>
result<error, T> decode(Codec& codec, reader& reader) {
return codec.decode(reader);
}
template <typename T, typename = void>
class codec;
template <typename T>
struct codec<T, std::enable_if_t<std::is_integral_v<T>>> {
result<error> encode(writer& writer, const T& value) { return writer.write_int(value); }
result<error, T> decode(reader& reader) { return reader.read_int(T{}); }
};
template <>
struct codec<std::string> {
result<error> encode(writer& writer, const std::string& value) { return writer.write_string(value); }
result<error, std::string> decode(reader& reader) { return reader.read_string(); }
};
} // namespace serialization
} // namespace furlang
#endif // FURLANG_SERIALIZATION_CODEC_HPP
+33
View File
@@ -0,0 +1,33 @@
#ifndef FURLANG_SERIALIZATION_ERROR_HPP
#define FURLANG_SERIALIZATION_ERROR_HPP
#include <cstddef>
#include <string>
namespace furlang {
namespace serialization {
enum class error_code {
EndOfFile,
InvalidData,
InvalidTag,
InvalidVersion,
IntegerOverflow,
SizeLimit,
DuplicateId,
UnknownId,
TypeMismatch,
Unsupported,
};
struct error {
error_code code;
std::string message;
std::size_t offset = 0;
};
} // namespace serialization
} // namespace furlang
#endif // FURLANG_SERIALIZATION_ERROR_HPP
+190
View File
@@ -0,0 +1,190 @@
#ifndef FURLANG_SERIALIZATION_IO_HPP
#define FURLANG_SERIALIZATION_IO_HPP
#include "furlang/result.hpp"
#include "furlang/serialization/error.hpp"
#include <cstdint>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
namespace furlang {
namespace serialization {
class writer {
public:
writer() = default;
virtual ~writer() = default;
writer(writer&&) noexcept = default;
writer& operator=(writer&&) noexcept = default;
writer(const writer&) = default;
writer& operator=(const writer&) = default;
public:
virtual result<error> write_s8(std::int8_t value) = 0;
virtual result<error> write_u8(std::uint8_t value) = 0;
virtual result<error> write_s16(std::int16_t value) = 0;
virtual result<error> write_u16(std::uint16_t value) = 0;
virtual result<error> write_s32(std::int32_t value) = 0;
virtual result<error> write_u32(std::uint32_t value) = 0;
virtual result<error> write_s64(std::int64_t value) = 0;
virtual result<error> write_u64(std::uint64_t value) = 0;
result<error> write_int(std::int8_t value) { return write_s8(value); }
result<error> write_int(std::uint8_t value) { return write_u8(value); }
result<error> write_int(std::int16_t value) { return write_s16(value); }
result<error> write_int(std::uint16_t value) { return write_u16(value); }
result<error> write_int(std::int32_t value) { return write_s32(value); }
result<error> write_int(std::uint32_t value) { return write_u32(value); }
result<error> write_int(std::int64_t value) { return write_s64(value); }
result<error> write_int(std::uint64_t value) { return write_u64(value); }
virtual result<error> write_string(const char* string) = 0;
virtual result<error> write_string(std::string_view string) = 0;
virtual result<error> write_string(const std::string& string) = 0;
};
class reader {
public:
reader() = default;
virtual ~reader() = default;
reader(reader&&) noexcept = default;
reader& operator=(reader&&) noexcept = default;
reader(const reader&) = default;
reader& operator=(const reader&) = default;
public:
virtual result<error, std::int8_t> read_s8() = 0;
virtual result<error, std::uint8_t> read_u8() = 0;
virtual result<error, std::int16_t> read_s16() = 0;
virtual result<error, std::uint16_t> read_u16() = 0;
virtual result<error, std::int32_t> read_s32() = 0;
virtual result<error, std::uint32_t> read_u32() = 0;
virtual result<error, std::int64_t> read_s64() = 0;
virtual result<error, std::uint64_t> read_u64() = 0;
result<error, std::int8_t> read_int(std::int8_t) { return read_s8(); }
result<error, std::uint8_t> read_int(std::uint8_t) { return read_u8(); }
result<error, std::int16_t> read_int(std::int16_t) { return read_s16(); }
result<error, std::uint16_t> read_int(std::uint16_t) { return read_u16(); }
result<error, std::int32_t> read_int(std::int32_t) { return read_s32(); }
result<error, std::uint32_t> read_int(std::uint32_t) { return read_u32(); }
result<error, std::int64_t> read_int(std::int64_t) { return read_s64(); }
result<error, std::uint64_t> read_int(std::uint64_t) { return read_u64(); }
virtual result<error, std::string> read_string() = 0;
virtual std::size_t offset() const = 0;
};
enum class endianness {
Little = 0,
Big = 1,
};
class byte_writer : public writer {
public:
byte_writer(endianness endianness = endianness::Big)
: m_endianness(endianness) {}
public:
result<error> write_s8(std::int8_t value) override;
result<error> write_u8(std::uint8_t value) override;
result<error> write_s16(std::int16_t value) override;
result<error> write_u16(std::uint16_t value) override;
result<error> write_s32(std::int32_t value) override;
result<error> write_u32(std::uint32_t value) override;
result<error> write_s64(std::int64_t value) override;
result<error> write_u64(std::uint64_t value) override;
result<error> write_string(const char* string) override;
result<error> write_string(std::string_view string) override;
result<error> write_string(const std::string& string) override;
private:
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
result<error> write_integral_le(T value) {
return write_integral_le(value, std::make_index_sequence<sizeof(T)>{});
}
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>, std::size_t... I>
result<error> write_integral_le(T value, std::index_sequence<I...>) {
auto usig = static_cast<std::make_unsigned_t<T>>(value);
(m_bytes.push_back(usig >> (I * 8)), ...);
return {};
}
private:
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
void write_integral_be(T value) {
write_integral_be(value, std::make_index_sequence<sizeof(T)>{});
}
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>, std::size_t... I>
void write_integral_be(T value, std::index_sequence<I...>) {
auto usig = static_cast<std::make_unsigned_t<T>>(value);
(m_bytes.push_back(usig >> ((sizeof(T) - 1 - I) * 8)), ...);
}
private:
endianness m_endianness;
std::vector<std::uint8_t> m_bytes;
};
class byte_reader : public reader {
public:
byte_reader(const std::uint8_t* bytes, std::size_t length, endianness endianness = endianness::Big)
: m_endianness(endianness), m_bytes(bytes), m_length(length) {}
public:
result<error, std::int8_t> read_s8() override;
result<error, std::uint8_t> read_u8() override;
result<error, std::int16_t> read_s16() override;
result<error, std::uint16_t> read_u16() override;
result<error, std::int32_t> read_s32() override;
result<error, std::uint32_t> read_u32() override;
result<error, std::int64_t> read_s64() override;
result<error, std::uint64_t> read_u64() override;
result<error, std::string> read_string() override;
std::size_t offset() const override;
private:
result<error, std::uint8_t> read_byte() {
if (m_offset >= m_length)
return result<error, std::uint8_t>::error(error{ error_code::EndOfFile, "", m_offset });
return { m_bytes[m_offset++] };
}
private:
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
result<error, T> read_integral_le() {
using U = std::make_unsigned_t<T>;
U usig = 0;
for (std::size_t i = 0; i < sizeof(T); ++i) {
auto res = read_byte();
if (res.has_error()) return res;
usig |= static_cast<U>(res.value()) << (i * 8);
}
return { static_cast<T>(usig) };
}
private:
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
result<error, T> read_integral_be() {
using U = std::make_unsigned_t<T>;
U usig = 0;
for (std::size_t i = 0; i < sizeof(T); ++i) {
auto res = read_byte();
if (res.has_error()) return res;
usig |= static_cast<U>(res.value()) << ((sizeof(T) - 1 - i) * 8);
}
return { static_cast<T>(usig) };
}
private:
endianness m_endianness;
const std::uint8_t* m_bytes;
std::size_t m_length;
std::size_t m_offset = 0;
};
} // namespace serialization
} // namespace furlang
#endif // FURLANG_SERIALIZATION_IO_HPP
+38
View File
@@ -0,0 +1,38 @@
#ifndef FURLANG_VIEW_HPP
#define FURLANG_VIEW_HPP
#include <algorithm>
#include <cstddef>
#include <limits>
#include <stdexcept>
namespace furlang {
template <typename T>
class view {
public:
constexpr view() noexcept = default;
constexpr view(const T* data, std::size_t size) noexcept
: m_data(data), m_size(size) {}
public:
constexpr view subview(std::size_t offset, std::size_t count = std::numeric_limits<std::size_t>::max()) {
if (count > 0 && offset >= m_size) throw std::runtime_error("offset too large");
return { m_data + offset, std::min(m_size - offset, count) };
}
const T& operator[](std::size_t offset) const {
if (offset >= m_size) throw std::runtime_error("out of bounds");
return m_data[offset];
}
constexpr const T* data() const { return m_data; }
constexpr std::size_t size() const { return m_size; }
private:
const T* m_data = nullptr;
std::size_t m_size = 0;
};
} // namespace furlang
#endif // FURLANG_VIEW_HPP
+31
View File
@@ -0,0 +1,31 @@
#ifndef FURVM_CONSTANT_HPP
#define FURVM_CONSTANT_HPP
#include "furvm/fwd.hpp"
#include <cstdint>
#include <string_view>
namespace furvm {
// TODO: Array constants
struct constant {
enum type_e {
S32 = 0,
U32,
S64,
U64,
String,
} type = S32;
union {
std::int32_t s32;
std::uint32_t u32;
std::int64_t s64;
std::uint64_t u64;
std::string_view string;
};
};
} // namespace furvm
#endif // FURVM_CONSTANT_HPP
+90
View File
@@ -0,0 +1,90 @@
#ifndef FURVM_CONTEXT_HPP
#define FURVM_CONTEXT_HPP
#include "furvm/executor.hpp"
#include "furvm/fwd.hpp"
#include "furvm/handle.hpp"
#include "furvm/module.hpp" // IWYU pragma: keep
#include "furvm/thing.hpp" // IWYU pragma: keep
#include <cstddef>
#include <utility>
#include <vector>
namespace furvm {
class context : public handle_container<mod_h> {
public:
friend class executor;
public:
/**
* @brief Constructs a context.
*/
context() {}
~context() = default;
/**
* @brief Move constructor.
*/
context(context&&) noexcept = default;
/**
* @brief Move constructor.
*/
context& operator=(context&&) noexcept = default;
context(const context&) = delete;
context& operator=(const context&) = delete;
public:
template <typename... Args>
auto& allocate_executor() {
executor executor(this);
return m_executors.emplace_back(std::move(executor));
}
/**
* @brief Returns an executor from the context.
*
* @param args Id of the executor.
* @return A handle to the executor.
*/
template <typename... Args>
auto& executor_at(Args&&... args) {
return m_executors.at(std::forward<Args>(args)...);
}
/**
* @brief Returns an executor from the context.
*
* @param args Id of the executor.
* @return A handle to the executor.
*/
template <typename... Args>
const auto& executor_at(Args&&... args) const {
return m_executors.at(std::forward<Args>(args)...);
}
const std::vector<executor>& executors() const { return m_executors; }
public:
thing_type_store& tt_store() { return m_thingTypeStore; }
public:
template <typename... Args>
thing<> allocate_thing(Args&&... args) {
thing<> thing = { std::forward<Args>(args)... };
m_heap.push_back(thing.raw());
return std::move(thing);
}
private:
handle_container<mod_h> m_modules;
std::vector<executor> m_executors;
class thing_type_store m_thingTypeStore;
// A list of things on the heap
std::vector<std::byte*> m_heap;
};
} // namespace furvm
#endif // FURVM_CONTEXT_HPP
@@ -3,11 +3,11 @@
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/module.hpp" // IWYU pragma: keep #include "furvm/module.hpp" // IWYU pragma: keep
#include "furvm/stack.hpp"
#include "furvm/thing.hpp" // IWYU pragma: keep #include "furvm/thing.hpp" // IWYU pragma: keep
#include <optional> #include <functional>
#include <stack> #include <stack>
#include <utility>
#include <vector> #include <vector>
namespace furvm { namespace furvm {
@@ -15,6 +15,8 @@ namespace furvm {
enum class executor_flags : std::uint32_t { enum class executor_flags : std::uint32_t {
Suspended = (1 << 0), /**< Execution suspended. */ Suspended = (1 << 0), /**< Execution suspended. */
Done = (1 << 1), /**< Execution is finished. */ Done = (1 << 1), /**< Execution is finished. */
JustHit = (1 << 16), /**< Executor just hit a breakpoint. */
}; };
static inline executor_flags operator|(executor_flags lhs, executor_flags rhs) { static inline executor_flags operator|(executor_flags lhs, executor_flags rhs) {
@@ -30,6 +32,16 @@ static inline executor_flags operator~(executor_flags flags) {
} }
class executor { class executor {
friend class context;
private:
executor(context* context)
: m_context(context) {}
public:
static constexpr executor_flags STATE_FLAGS = executor_flags::JustHit;
using new_frame_callback = std::function<void(executor&)>;
using stack_thing = thing<stack_allocator>;
public: public:
/** /**
* @brief Executor frame. * @brief Executor frame.
@@ -42,17 +54,9 @@ public:
std::size_t stackBase; /**< Snapshot of the stack size before this frame. */ std::size_t stackBase; /**< Snapshot of the stack size before this frame. */
thing_type* returnType; /**< Return type. */ thing_type* returnType; /**< Return type. */
std::vector<thing_h> variables; /**< Frame variables. */ std::vector<stack_thing> variables; /**< Frame variables. */
}; };
public: public:
/**
* @brief Returns a new executor.
*
* @param context Context.
*/
executor(const context_p& context)
: m_context(context) {}
~executor() = default; ~executor() = default;
/** /**
@@ -74,6 +78,11 @@ public:
* @brief Copy constructor. * @brief Copy constructor.
*/ */
executor& operator=(const executor&) = default; executor& operator=(const executor&) = default;
public:
template <typename CallbackFwd>
void set_new_frame_callback(CallbackFwd&& callback) {
m_newFrameCb = std::forward<CallbackFwd>(callback);
}
public: public:
/** /**
* @brief Returns flags of this executor. * @brief Returns flags of this executor.
@@ -81,6 +90,17 @@ public:
* @return The flags. * @return The flags.
*/ */
executor_flags flags() const { return m_flags; } executor_flags flags() const { return m_flags; }
bool done() const { return (m_flags & executor_flags::Done) == executor_flags::Done; }
bool suspended() const { return (m_flags & executor_flags::Suspended) == executor_flags::Suspended; }
void unsuspend() { m_flags = m_flags & ~executor_flags::Suspended; }
void clear_flags() {
m_flags = m_flags & STATE_FLAGS;
m_flags = m_frames.empty() ? executor_flags::Done : furvm::executor_flags{ 0 };
}
public: public:
/** /**
* @brief Pushes a new frame. * @brief Pushes a new frame.
@@ -102,18 +122,10 @@ public:
* *
* @return The frame. * @return The frame.
*/ */
frame frame() const; frame top_frame() const;
public:
/**
* @brief Pushes a thing handle onto the stack.
*
* @param handle Thing handle.
*/
template <typename HandleFwd>
void push_thing(HandleFwd&& handle) {
m_stack.emplace(std::forward<HandleFwd>(handle));
}
const std::stack<frame>& frames() const { return m_frames; }
public:
/** /**
* @brief Pushes a thing onto the stack. * @brief Pushes a thing onto the stack.
* *
@@ -122,21 +134,27 @@ public:
* @param thing Thing. * @param thing Thing.
* @return The pushed handle. * @return The pushed handle.
*/ */
thing_h push_thing(class thing<>&& thing); stack_thing& push_thing(stack_thing&& thing);
stack_thing& push_thing(const stack_thing& thing);
/** /**
* @brief Pops a thing from the stack. * @brief Pops a thing from the stack.
* *
* @return A handle to the popped thing. * @return A handle to the popped thing.
*/ */
thing_h pop_thing(); stack_thing pop_thing();
/** /**
* @brief Returns the top thing on the stack. * @brief Returns the top thing on the stack.
* *
* @return A handle to the top thing. * @return A handle to the top thing.
*/ */
thing_h thing() const; stack_thing& top_thing();
const stack_thing& top_thing() const;
const std::vector<stack_thing>& stack() const { return m_stack; }
public: public:
/** /**
* @brief Stores a thing in a frame variable. * @brief Stores a thing in a frame variable.
@@ -144,7 +162,7 @@ public:
* @param variable Id of the variable in which the handle will be put. * @param variable Id of the variable in which the handle will be put.
* @param thing Thing handle. * @param thing Thing handle.
*/ */
void store_thing(variable_t variable, const thing_h& thing); void store_thing(variable_t variable, const stack_thing& thing);
/** /**
* @brief Stores a thing in a frame variable. * @brief Stores a thing in a frame variable.
@@ -152,7 +170,7 @@ public:
* @param variable Id of the variable in which the handle will be put. * @param variable Id of the variable in which the handle will be put.
* @param thing Thing handle. * @param thing Thing handle.
*/ */
void store_thing(variable_t variable, thing_h&& thing); void store_thing(variable_t variable, stack_thing&& thing);
/** /**
* @brief Returns a thing stored in a variable. * @brief Returns a thing stored in a variable.
@@ -160,7 +178,9 @@ public:
* @param variable Id of the variable from which the handle will be fetched. * @param variable Id of the variable from which the handle will be fetched.
* @return A handle stored in the variable. * @return A handle stored in the variable.
*/ */
thing_h load_thing(variable_t variable) const; stack_thing& load_thing(variable_t variable);
const stack_thing& load_thing(variable_t variable) const;
public: public:
/** /**
* @brief Executes next instruction. * @brief Executes next instruction.
@@ -171,11 +191,16 @@ private:
thing_type* mod_to_thing_type(const mod_h& mod, const mod_type& type) const; thing_type* mod_to_thing_type(const mod_h& mod, const mod_type& type) const;
private: private:
executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization) static bool compare_thing_types(const thing_type& lhs, const thing_type& rhs);
context_p m_context; private:
executor_flags m_flags = executor_flags::Done;
context* m_context;
furvm::stack<std::byte> m_stackStorage;
std::stack<struct frame> m_frames; std::stack<frame> m_frames;
std::stack<thing_h> m_stack; std::vector<stack_thing> m_stack;
new_frame_callback m_newFrameCb = nullptr;
}; };
} // namespace furvm } // namespace furvm
@@ -1,7 +1,6 @@
#ifndef FURVM_FUNCTION_HPP #ifndef FURVM_FUNCTION_HPP
#define FURVM_FUNCTION_HPP #define FURVM_FUNCTION_HPP
#include "furlang/utility/hash.hpp"
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/handle.hpp" // IWYU pragma: keep #include "furvm/handle.hpp" // IWYU pragma: keep
@@ -75,11 +75,7 @@ class constant;
// instruction.hpp // instruction.hpp
/** struct instruction_argument;
* @enum instruction_t
* @brief Furvm's instruction type.
*/
enum class instruction_t : byte;
/** /**
* @struct instruction * @struct instruction
@@ -144,11 +140,6 @@ using mod_id = std::string;
*/ */
using mod_h = handle<mod, refcount_header<mod_id>>; using mod_h = handle<mod, refcount_header<mod_id>>;
// thing_allocator.hpp
template <typename T>
class thing_allocator;
// thing.hpp // thing.hpp
/** /**
@@ -165,7 +156,7 @@ using thing_type_id = std::uint32_t;
* *
* A stack element. Think of it like of a value in C++ or I guess a class in java. * A stack element. Think of it like of a value in C++ or I guess a class in java.
*/ */
template <template <typename> typename Allocator = thing_allocator> template <template <typename> typename Allocator = std::allocator>
class thing; class thing;
/** /**
@@ -173,11 +164,6 @@ class thing;
*/ */
using thing_id = std::uint32_t; using thing_id = std::uint32_t;
/**
* @brief A handle to a thing.
*/
using thing_h = handle<thing<thing_allocator>, refcount_header<thing_id>>;
// executor.hpp // executor.hpp
/** /**
@@ -199,21 +185,11 @@ enum class executor_flags : std::uint32_t;
*/ */
class executor; class executor;
/**
* @brief An alias to a executor shared pointer.
*/
using executor_p = std::shared_ptr<executor>;
/** /**
* @brief Furvm executor's index. * @brief Furvm executor's index.
*/ */
using executor_id = std::uint32_t; using executor_id = std::uint32_t;
/**
* @brief A handle to an executor.
*/
using executor_h = handle<executor, refcount_header<executor_id>>;
// context.hpp // context.hpp
/** /**
+104
View File
@@ -0,0 +1,104 @@
#ifndef FURVM_INSTRUCTION_HPP
#define FURVM_INSTRUCTION_HPP
#include "furlang/view.hpp"
#include "furvm/fwd.hpp"
#include <cstddef>
#include <vector>
namespace furvm {
struct instruction_argument {
enum type_e {
None = 0,
S8,
U8,
S16,
U16,
S32,
U32,
Constant,
Type,
Variable,
GlobalVariable,
Function,
Offset,
Count,
} type;
union {
std::int8_t s8;
std::uint8_t u8;
std::int16_t s16;
std::uint16_t u16;
std::int32_t s32;
std::uint32_t u32;
};
static const std::size_t s_sizes[Count];
static const bool s_signedness[Count];
std::size_t size() const { return s_sizes[type]; }
bool is_signed() const { return s_signedness[type]; }
};
using instruction_argument_t = instruction_argument::type_e;
struct instruction {
enum type_e : byte {
NoOperation = 0,
PushS8,
PushU8,
PushS16,
PushU16,
PushS32,
PushU32,
PushConstant,
Array,
Slice,
Get,
Set,
Drop,
Duplicate,
Swap,
Clone,
Reference,
Add,
Sub,
Mul,
Div,
Mod,
Equals,
NotEquals,
LessThan,
GreaterThan,
LessEqual,
GreaterEqual,
Pointerof,
Sizeof,
Lengthof,
Load,
Store,
LoadGlobal,
StoreGlobal,
Call,
Jump,
JumpNotZero,
Return,
Count,
} type;
instruction_argument arg;
static const instruction_argument_t s_arguments[Count];
std::size_t read(furlang::view<std::uint8_t> in);
std::size_t write(std::vector<std::uint8_t>& out) const;
};
using instruction_t = instruction::type_e;
} // namespace furvm
#endif // FURVM_INSTRUCTION_HPP
@@ -2,13 +2,17 @@
#define FURVM_MODULE_HPP #define FURVM_MODULE_HPP
#include "furlang/utility/hash.hpp" #include "furlang/utility/hash.hpp"
#include "furlang/view.hpp"
#include "furvm/constant.hpp"
#include "furvm/function.hpp" #include "furvm/function.hpp"
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/handle.hpp" #include "furvm/handle.hpp"
#include "furvm/thing.hpp"
#include <functional> #include <functional>
#include <istream> #include <istream>
#include <ostream> #include <ostream>
#include <stdexcept>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <unordered_map> #include <unordered_map>
@@ -23,6 +27,10 @@ struct mod_type {
std::size_t size; std::size_t size;
}; };
struct slice_value {
mod_type_id typeId;
};
struct import_value { struct import_value {
mod_id modId; mod_id modId;
mod_type_id typeId; mod_type_id typeId;
@@ -40,6 +48,7 @@ struct mod_type {
Ptr, Ptr,
Ref, Ref,
Array, Array,
Slice,
Import, Import,
Count, Count,
@@ -48,6 +57,7 @@ struct mod_type {
std::nullptr_t null = nullptr; std::nullptr_t null = nullptr;
mod_type_id typeRef; mod_type_id typeRef;
array_value array; array_value array;
slice_value slice;
import_value imprt; import_value imprt;
value() = default; value() = default;
@@ -92,6 +102,7 @@ struct mod_type {
~mod_type() { ~mod_type() {
switch (type) { switch (type) {
case Array: value.array.~array_value(); break; case Array: value.array.~array_value(); break;
case Slice: value.slice.~slice_value(); break;
case Import: value.imprt.~import_value(); break; case Import: value.imprt.~import_value(); break;
default: break; default: break;
} }
@@ -101,6 +112,7 @@ struct mod_type {
: type(other.type) { : type(other.type) {
switch (type) { switch (type) {
case Array: new (&value.array) array_value(other.value.array); break; case Array: new (&value.array) array_value(other.value.array); break;
case Slice: new (&value.slice) slice_value(other.value.slice); break;
case Import: new (&value.imprt) import_value(std::move(other.value.imprt)); break; case Import: new (&value.imprt) import_value(std::move(other.value.imprt)); break;
default: break; default: break;
} }
@@ -112,6 +124,7 @@ struct mod_type {
type = other.type; type = other.type;
switch (type) { switch (type) {
case Array: new (&value.array) array_value(other.value.array); break; case Array: new (&value.array) array_value(other.value.array); break;
case Slice: new (&value.slice) slice_value(other.value.slice); break;
case Import: new (&value.imprt) import_value(std::move(other.value.imprt)); break; case Import: new (&value.imprt) import_value(std::move(other.value.imprt)); break;
default: break; default: break;
} }
@@ -123,6 +136,7 @@ struct mod_type {
: type(other.type) { : type(other.type) {
switch (type) { switch (type) {
case Array: new (&value.array) array_value(other.value.array); break; case Array: new (&value.array) array_value(other.value.array); break;
case Slice: new (&value.slice) slice_value(other.value.slice); break;
case Import: new (&value.imprt) import_value(other.value.imprt); break; case Import: new (&value.imprt) import_value(other.value.imprt); break;
default: break; default: break;
} }
@@ -133,6 +147,7 @@ struct mod_type {
type = other.type; type = other.type;
switch (type) { switch (type) {
case Array: new (&value.array) array_value(other.value.array); break; case Array: new (&value.array) array_value(other.value.array); break;
case Slice: new (&value.slice) slice_value(other.value.slice); break;
case Import: new (&value.imprt) import_value(other.value.imprt); break; case Import: new (&value.imprt) import_value(other.value.imprt); break;
default: break; default: break;
} }
@@ -140,6 +155,11 @@ struct mod_type {
} }
}; };
struct breakpoint {
std::function<void(executor&, void*)> callback;
void* data = nullptr;
};
class mod { class mod {
friend class function; friend class function;
friend class serializer; friend class serializer;
@@ -195,7 +215,7 @@ public:
* *
* @return A constant reference to the bytecode. * @return A constant reference to the bytecode.
*/ */
constexpr const bytecode_t& bytecode() const { return m_bytecode; } furlang::view<std::uint8_t> bytecode_view() const { return { m_bytecode.data(), m_bytecode.size() }; }
public: public:
/** /**
* @brief Emplaces a function in the module's function container. * @brief Emplaces a function in the module's function container.
@@ -284,6 +304,10 @@ public:
m_functionMap.erase(it); m_functionMap.erase(it);
} }
} }
const handle_container<function_h>& functions() const { return m_functions; }
const auto& function_map() const { return m_functionMap; }
public: public:
template <typename NameFwd, typename Func> template <typename NameFwd, typename Func>
void set_native_function(NameFwd&& name, Func&& func) { void set_native_function(NameFwd&& name, Func&& func) {
@@ -341,6 +365,51 @@ public:
void erase_type(Args&&... args) { void erase_type(Args&&... args) {
m_types.erase(std::forward<Args>(args)...); m_types.erase(std::forward<Args>(args)...);
} }
const handle_container<mod_type_h>& types() const { return m_types; }
public:
void set_global_variable_count(std::uint16_t count) {
m_globalVariables.resize(count);
m_globalVariables.shrink_to_fit();
}
std::uint16_t get_global_variable_count() const { return static_cast<std::uint16_t>(m_globalVariables.size()); }
void store_global_variable(std::uint16_t var, thing<>&& thing) {
if (var >= m_globalVariables.size()) throw std::runtime_error("invalid slot");
m_globalVariables.emplace(m_globalVariables.cbegin() + var, std::move(thing));
}
void store_global_variable(std::uint16_t var, const thing<>& thing) {
if (var >= m_globalVariables.size()) throw std::runtime_error("invalid slot");
m_globalVariables.emplace(m_globalVariables.cbegin() + var, thing);
}
thing<>& load_global_variable(std::uint16_t var) {
if (var >= m_globalVariables.size()) throw std::runtime_error("invalid slot");
return m_globalVariables[var];
}
const thing<>& load_global_variable(std::uint16_t var) const {
if (var >= m_globalVariables.size()) throw std::runtime_error("invalid slot");
return m_globalVariables[var];
}
public:
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<constant, Args...>>>
void emplace_constant(Args&&... args) {
m_constants.emplace_back(std::forward<Args>(args)...);
}
const constant& constant_at(constant_index index) const { return m_constants.at(index); }
public:
template <typename Fwd, typename = std::enable_if_t<std::is_constructible_v<breakpoint, Fwd>>>
void set_breakpoint(bytecode_pos pos, Fwd&& breakpoint) {
m_breakpoints[pos] = std::forward<Fwd>(breakpoint);
}
bool has_breakpoint(bytecode_pos pos) const { return m_breakpoints.find(pos) != m_breakpoints.end(); }
const breakpoint& breakpoint_at(bytecode_pos pos) const { return m_breakpoints.at(pos); }
public: public:
/** /**
* @brief Prints the module in a bytecode form to an output stream. * @brief Prints the module in a bytecode form to an output stream.
@@ -369,7 +438,13 @@ private:
handle_container<mod_type_h> m_types; handle_container<mod_type_h> m_types;
std::vector<thing<>> m_globalVariables;
std::vector<constant> m_constants;
std::unordered_map<std::string, native_function> m_nativeFunctions; std::unordered_map<std::string, native_function> m_nativeFunctions;
std::unordered_map<bytecode_pos, breakpoint> m_breakpoints;
}; };
} // namespace furvm } // namespace furvm
+56
View File
@@ -0,0 +1,56 @@
#ifndef FURVM_STACK_HPP
#define FURVM_STACK_HPP
#include <cstddef>
#include <new>
#include <stack>
namespace furvm {
template <typename T>
struct stack {
stack(std::size_t capacity = (1024ULL * 1024ULL) / sizeof(T))
: begin(new T[capacity]()), cursor(begin), capacity(capacity) {}
T* begin;
T* cursor;
std::size_t capacity;
std::stack<T*> frames;
void push_frame() { frames.push(cursor); }
void pop_frame() {
cursor = frames.top();
frames.pop();
}
};
template <typename T>
class stack_allocator {
public:
stack_allocator() = default;
stack_allocator(stack<T>& stack)
: m_ref(&stack) {}
template <typename U>
constexpr stack_allocator(const stack_allocator<U>& other) noexcept
: m_ref(other.m_ref) {}
public:
T* allocate(std::size_t n) {
if (m_ref == nullptr) throw std::bad_alloc();
if (m_ref->capacity - (m_ref->cursor - m_ref->begin) < n) throw std::bad_alloc();
T* ptr = m_ref->cursor;
m_ref->cursor += n;
return ptr;
}
void deallocate(T* ptr, std::size_t n) {}
private:
stack<T>* m_ref = nullptr;
};
} // namespace furvm
#endif // FURVM_STACK_HPP
File diff suppressed because it is too large Load Diff
+226
View File
@@ -0,0 +1,226 @@
#ifndef FURVM_TYPES_HPP
#define FURVM_TYPES_HPP
#include "furvm/fwd.hpp"
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace furvm {
using s8 = std::int8_t;
using s16 = std::int16_t;
using s32 = std::int32_t;
using s64 = std::int64_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
struct thing_type {
struct array_value {
thing_type* type;
std::size_t size;
};
struct slice_value {
thing_type* type;
};
enum type { // NOLINT
S8 = 0,
S16,
S32,
S64,
U8,
U16,
U32,
U64,
String,
Ptr,
Ref,
Array,
Slice,
Count,
} type = Count;
union value {
std::nullptr_t null = nullptr;
thing_type* typeRef;
array_value array;
slice_value slice;
value() = default;
value(thing_type* type)
: typeRef(type) {}
value(thing_type* type, std::size_t size)
: array({}) {
array.type = type;
array.size = size;
}
} value;
static constexpr thing_type_id INVALID_ID = std::numeric_limits<thing_type_id>::max();
thing_type_id id = INVALID_ID;
bool operator==(const thing_type& other) const {
if (type != other.type) return false;
switch (type) {
case S8:
case S16:
case S32:
case S64:
case U8:
case U16:
case U32:
case U64:
case String: return true;
case Ptr:
case Ref: return *value.typeRef == *other.value.typeRef;
case Array: return *value.array.type == *other.value.array.type && value.array.size == other.value.array.size;
case Slice: return *value.slice.type == *other.value.slice.type;
case Count: break;
}
return false;
}
bool operator!=(const thing_type& other) const { return !this->operator==(other); }
static bool is_primitive(enum type type) {
switch (type) {
case S8:
case S16:
case S32:
case S64:
case U8:
case U16:
case U32:
case U64: return true;
case String:
case Ptr:
case Ref:
case Array:
case Slice: return false;
case Count: break;
}
throw std::runtime_error("unreachable");
}
static std::size_t primitive_size(enum type type) {
switch (type) {
case thing_type::S8: return sizeof(s8);
case thing_type::S16: return sizeof(s16);
case thing_type::S32: return sizeof(s32);
case thing_type::S64: return sizeof(s64);
case thing_type::U8: return sizeof(u8);
case thing_type::U16: return sizeof(u16);
case thing_type::U32: return sizeof(u32);
case thing_type::U64: return sizeof(u64);
case thing_type::String:
case Ptr:
case Ref:
case Array:
case Slice: return 0;
case Count: break;
}
throw std::runtime_error("unreachable");
}
};
namespace detail {
template <typename T, typename = void>
struct overrides_thing_type_matching : std::false_type {};
template <typename T>
struct overrides_thing_type_matching<T, std::void_t<decltype(T::matches(std::declval<const thing_type&>()))>>
: std::is_same<decltype(T::matches(std::declval<const thing_type&>())), bool> {};
template <typename T>
struct thing_traits {
bool operator()(const thing_type& type) const {
if constexpr (overrides_thing_type_matching<T>::value) {
return T::matches(type);
} else {
return false;
}
}
};
template <>
struct thing_traits<s8> {
bool operator()(const thing_type& type) const { return type.type == thing_type::S8; }
};
template <>
struct thing_traits<u8> {
bool operator()(const thing_type& type) const { return type.type == thing_type::U8; }
};
template <>
struct thing_traits<s16> {
bool operator()(const thing_type& type) const { return type.type == thing_type::S16; }
};
template <>
struct thing_traits<u16> {
bool operator()(const thing_type& type) const { return type.type == thing_type::U16; }
};
template <>
struct thing_traits<s32> {
bool operator()(const thing_type& type) const { return type.type == thing_type::S32; }
};
template <>
struct thing_traits<u32> {
bool operator()(const thing_type& type) const { return type.type == thing_type::U32; }
};
template <>
struct thing_traits<s64> {
bool operator()(const thing_type& type) const { return type.type == thing_type::S64; }
};
template <>
struct thing_traits<u64> {
bool operator()(const thing_type& type) const { return type.type == thing_type::U64; }
};
template <typename Inner>
struct thing_traits<Inner*> {
bool operator()(const thing_type& type) const {
return (type.type == thing_type::Ptr || type.type == thing_type::Ref) &&
thing_traits<Inner>{}(*type.value.typeRef);
}
};
template <typename T, typename Thing, typename = void>
struct cassignable_to_thing : std::false_type {};
template <typename T, typename Thing>
struct cassignable_to_thing<T,
Thing,
std::void_t<decltype(std::declval<thing_traits<T>>().assign_to(std::declval<Thing&>(), std::declval<const T&>()))>>
: std::true_type {};
template <typename T, typename Thing, typename = void>
struct massignable_to_thing : std::false_type {};
template <typename T, typename Thing>
struct massignable_to_thing<T,
Thing,
std::void_t<decltype(std::declval<thing_traits<T>>().assign_to(std::declval<Thing&>(), std::declval<T&&>()))>>
: std::true_type {};
} // namespace detail
} // namespace furvm
#endif // FURVM_TYPES_HPP
+4
View File
@@ -0,0 +1,4 @@
file(GLOB_RECURSE DISFURAS_SRCS "${CMAKE_SOURCE_DIR}/src/disfuras/**.cpp")
add_executable(disfuras ${DISFURAS_SRCS})
target_include_directories(disfuras PRIVATE ${CMAKE_SOURCE_DIR}/include/)
target_link_libraries(disfuras PUBLIC furlang libfurc libfurvm)
+250
View File
@@ -0,0 +1,250 @@
#include "furvm/instruction.hpp"
#include "furvm/module.hpp"
#include <cassert>
#include <exception>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
using namespace std::string_literals;
void print_type(const furvm::mod_type& type, const furvm::mod& mod) {
switch (type.type) {
case furvm::mod_type::S8: std::cout << "$s8"; return;
case furvm::mod_type::S16: std::cout << "$s16"; return;
case furvm::mod_type::S32: std::cout << "$s32"; return;
case furvm::mod_type::S64: std::cout << "$s64"; return;
case furvm::mod_type::U8: std::cout << "$u8"; return;
case furvm::mod_type::U16: std::cout << "$u16"; return;
case furvm::mod_type::U32: std::cout << "$u32"; return;
case furvm::mod_type::U64: std::cout << "$u64"; return;
case furvm::mod_type::Ptr:
std::cout << "ptr ";
print_type(*mod.type_at(type.value.typeRef), mod);
return;
case furvm::mod_type::Ref:
std::cout << "ref ";
print_type(*mod.type_at(type.value.typeRef), mod);
return;
case furvm::mod_type::Array:
std::cout << "array ";
print_type(*mod.type_at(type.value.array.typeId), mod);
if (type.value.array.size == 0) {
std::cout << " dynamic";
} else {
std::cout << ' ' << type.value.array.size;
}
return;
case furvm::mod_type::Import: std::cout << "import " << type.value.imprt.modId << "::t" << type.value.imprt.typeId;
case furvm::mod_type::Count: break;
}
assert(false);
}
static const char* typeNames[furvm::instruction::Count] = {
// NoOperation:
"nop",
// PushS8:
"push $__t0",
// PushU8:
"push $__t1",
// PushS16:
"push $__t2",
// PushU16:
"push $__t3",
// PushS32:
"push $__t4",
// PushU32:
"push $__t5",
// PushConstant:
"push",
// Array:
"array",
// Get:
"get",
// Set:
"set",
// Drop:
"drop",
// Duplicate:
"dup",
// Swap:
"swap",
// Clone:
"clone",
// Reference:
"ref",
// Add:
"add",
// Sub:
"sub",
// Mul:
"mul",
// Div:
"div",
// Mod:
"mod",
// Equals:
"eq",
// NotEquals:
"ne",
// LessThan:
"lt",
// GreaterThan:
"gt",
// LessEqual:
"le",
// GreaterEqual:
"ge",
// Pointerof:
"pointerof",
// Sizeof:
"sizeof",
// Lengthof:
"lengthof",
// Load:
"load",
// Store:
"store",
// LoadGlobal:
"loadg",
// StoreGlobal:
"storeg",
// Call:
"call",
// Jump:
"jmp",
// JumpNotZero:
"jnz",
// Return:
"ret",
};
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <module.fmod>\n";
return 1;
}
try {
std::ifstream file(argv[1], std::ios::binary | std::ios::in);
furvm::mod mod;
try {
mod = furvm::mod::load(file);
} catch (const std::exception& ex) {
std::cerr << "Failed to load module " << argv[1] << ": " << ex.what() << '\n';
return 1;
}
std::cout << "; Generated with disfuras\n";
std::cout << "; Types:\n";
for (const auto& ptr : mod.types()) {
const auto& [header, type] = *ptr;
std::cout << "type __t" << header.id() << " = ";
print_type(type, mod);
std::cout << '\n';
}
std::unordered_map<furvm::function_id, std::string> funcNames;
std::size_t unnamedCounter = 0;
std::unordered_map<std::size_t, std::string> labels;
std::unordered_set<std::size_t> deadLabels;
std::size_t labelCounter = 0;
std::cout << "\n; Functions:\n";
for (const auto& ptr : mod.functions()) {
const auto& [header, func] = *ptr;
auto it = mod.function_map().find(header.id());
if (it != mod.function_map().end()) {
std::cout << "public func " << (funcNames[header.id()] = it->second.first);
} else {
std::cout << "func " << (funcNames[header.id()] = "__f"s + std::to_string(unnamedCounter++));
}
for (const auto& param : func.signature().params) {
std::cout << " $__t" << param.id();
}
std::cout << " = ";
if (func.signature().returnType.has_value())
std::cout << "$__t" << func.signature().returnType->id() << ' '; // NOLINT
switch (func.type()) {
case furvm::function_t::Normal: {
auto it = labels.find(func.position());
if (it == labels.end()) it = labels.emplace(func.position(), funcNames[header.id()]).first;
deadLabels.insert(func.position());
std::cout << '#' << it->second;
} break;
case furvm::function_t::Native: {
std::cout << "native " << func.native();
} break;
case furvm::function_t::Import: {
std::cout << "import " << func.imp().mod << "::" << func.imp().function;
} break;
}
std::cout << '\n';
}
if (mod.get_global_variable_count() > 0) {
std::cout << "\n; Global Variables:\n";
for (std::uint16_t i = 0; i < mod.get_global_variable_count(); ++i) {
std::cout << "allocate __g" << i << '\n';
}
}
std::cout << "\n; Bytecode:\n";
// Label pass:
for (std::size_t off = 0; off < mod.bytecode().size();) {
furvm::instruction instr{};
off += instr.read(mod.bytecode_view().subview(off));
if (instr.type != furvm::instruction::Jump && instr.type != furvm::instruction::JumpNotZero) continue;
if (labels.find(off + instr.arg.s8) == labels.end())
labels[off + instr.arg.s8] = "__l"s + std::to_string(labelCounter++);
deadLabels.insert(off + instr.arg.s8);
}
// Actual printing pass:
for (std::size_t off = 0; off < mod.bytecode().size();) {
if (auto it = labels.find(off); it != labels.end()) {
std::cout << it->second << ":\n";
deadLabels.erase(off);
}
furvm::instruction instr{};
off += instr.read(mod.bytecode_view().subview(off));
std::cout << " " << typeNames[instr.type];
switch (instr.arg.type) {
case furvm::instruction_argument::None: break;
case furvm::instruction_argument::S8: std::cout << ' ' << std::to_string(instr.arg.s8); break;
case furvm::instruction_argument::U8: std::cout << ' ' << std::to_string(instr.arg.u8); break;
case furvm::instruction_argument::S16: std::cout << ' ' << instr.arg.s16; break;
case furvm::instruction_argument::U16: std::cout << ' ' << instr.arg.u16; break;
case furvm::instruction_argument::S32: std::cout << ' ' << std::to_string(instr.arg.s8); break;
case furvm::instruction_argument::U32: std::cout << ' ' << std::to_string(instr.arg.u8); break;
case furvm::instruction_argument::Constant: throw std::runtime_error("unimplemented");
case furvm::instruction_argument::Type: std::cout << " $__t" << instr.arg.u16; break;
case furvm::instruction_argument::Variable: std::cout << " %" << instr.arg.u16; break;
case furvm::instruction_argument::GlobalVariable: std::cout << " %__g" << instr.arg.u16; break;
case furvm::instruction_argument::Function: std::cout << ' ' << funcNames[instr.arg.s16]; break;
case furvm::instruction_argument::Offset: std::cout << " #" << labels[instr.arg.s16]; break;
case furvm::instruction_argument::Count: break;
}
std::cout << '\n';
}
if (!deadLabels.empty()) {
std::cerr << "Malformed module: invalid jumps\n";
return 1;
}
} catch (const std::exception& ex) {
std::cerr << "Exception uncaught: " << ex.what() << '\n';
return 1;
}
return 0;
}
// TODO: Disassemble into FIR (furlang's IR)
+5
View File
@@ -0,0 +1,5 @@
file(GLOB_RECURSE FURAS_SRCS "${CMAKE_SOURCE_DIR}/src/furas/**.cpp")
file(GLOB_RECURSE FURAS_HDRS "${CMAKE_SOURCE_DIR}/include/furas/**.hpp")
add_executable(furas ${FURAS_SRCS} ${FURAS_HDRS})
target_include_directories(furas PUBLIC ${CMAKE_SOURCE_DIR}/include/)
target_link_libraries(furas PRIVATE furlang libfurvm)
+198 -134
View File
@@ -4,6 +4,7 @@
#include "furvm/function.hpp" #include "furvm/function.hpp"
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include "furvm/instruction.hpp" #include "furvm/instruction.hpp"
#include "furvm/module.hpp"
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
@@ -21,48 +22,39 @@ using namespace std::string_literals;
namespace { namespace {
struct instruction {
furvm::instruction_t fur{};
enum arg_type {
None = 0,
Type,
Constant,
Variable,
Function,
Label,
} arg = None;
};
// NOLINTBEGIN // NOLINTBEGIN
std::unordered_map<enum token::type, instruction> instructions = { std::unordered_map<enum token::type, furvm::instruction_t> instructions = {
{ token::Array, { furvm::instruction_t::Array, instruction::Type } }, { token::Array, furvm::instruction_t::Array },
{ token::Get, { furvm::instruction_t::Get } }, { token::Slice, furvm::instruction_t::Slice },
{ token::Set, { furvm::instruction_t::Set } }, { token::Get, furvm::instruction_t::Get },
{ token::Drop, { furvm::instruction_t::Drop } }, { token::Set, furvm::instruction_t::Set },
{ token::Dup, { furvm::instruction_t::Duplicate } }, { token::Drop, furvm::instruction_t::Drop },
{ token::Swap, { furvm::instruction_t::Swap } }, { token::Dup, furvm::instruction_t::Duplicate },
{ token::Clone, { furvm::instruction_t::Clone } }, { token::Swap, furvm::instruction_t::Swap },
{ token::Ref, { furvm::instruction_t::Reference } }, { token::Clone, furvm::instruction_t::Clone },
{ token::Add, { furvm::instruction_t::Add } }, { token::Ref, furvm::instruction_t::Reference },
{ token::Sub, { furvm::instruction_t::Sub } }, { token::Add, furvm::instruction_t::Add },
{ token::Mul, { furvm::instruction_t::Mul } }, { token::Sub, furvm::instruction_t::Sub },
{ token::Div, { furvm::instruction_t::Div } }, { token::Mul, furvm::instruction_t::Mul },
{ token::Mod, { furvm::instruction_t::Mod } }, { token::Div, furvm::instruction_t::Div },
{ token::Eq, { furvm::instruction_t::Equals } }, { token::Mod, furvm::instruction_t::Mod },
{ token::Neq, { furvm::instruction_t::NotEquals } }, { token::Eq, furvm::instruction_t::Equals },
{ token::Lt, { furvm::instruction_t::LessThan } }, { token::Neq, furvm::instruction_t::NotEquals },
{ token::Gt, { furvm::instruction_t::GreaterThan } }, { token::Lt, furvm::instruction_t::LessThan },
{ token::Le, { furvm::instruction_t::LessEqual } }, { token::Gt, furvm::instruction_t::GreaterThan },
{ token::Ge, { furvm::instruction_t::GreaterEqual } }, { token::Le, furvm::instruction_t::LessEqual },
{ token::Ptrof, { furvm::instruction_t::Pointerof } }, { token::Ge, furvm::instruction_t::GreaterEqual },
{ token::Sizeof, { furvm::instruction_t::Sizeof } }, { token::Ptrof, furvm::instruction_t::Pointerof },
{ token::Lenof, { furvm::instruction_t::Lengthof } }, { token::Sizeof, furvm::instruction_t::Sizeof },
{ token::Load, { furvm::instruction_t::Load, instruction::Variable } }, { token::Lenof, furvm::instruction_t::Lengthof },
{ token::Store, { furvm::instruction_t::Store, instruction::Variable } }, { token::Load, furvm::instruction_t::Load },
{ token::Call, { furvm::instruction_t::Call, instruction::Function } }, { token::Store, furvm::instruction_t::Store },
{ token::Jmp, { furvm::instruction_t::Jump, instruction::Label } }, { token::LoadGlobal, furvm::instruction_t::LoadGlobal },
{ token::Jnz, { furvm::instruction_t::JumpNotZero, instruction::Label } }, { token::StoreGlobal, furvm::instruction_t::StoreGlobal },
{ token::Ret, { furvm::instruction_t::Return } }, { token::Call, furvm::instruction_t::Call },
{ token::Jmp, furvm::instruction_t::Jump },
{ token::Jnz, furvm::instruction_t::JumpNotZero },
{ token::Ret, furvm::instruction_t::Return },
}; };
// NOLINTEND // NOLINTEND
@@ -84,8 +76,10 @@ const char* token_type(enum token::type type) {
case token::Import: return "import"; case token::Import: return "import";
case token::Public: return "public"; case token::Public: return "public";
case token::Private: return "private"; case token::Private: return "private";
case token::Allocate: return "allocate";
case token::Push: return "push"; case token::Push: return "push";
case token::Array: return "array"; case token::Array: return "array";
case token::Slice: return "slice";
case token::Get: return "get"; case token::Get: return "get";
case token::Set: return "set"; case token::Set: return "set";
case token::Drop: return "drop"; case token::Drop: return "drop";
@@ -109,6 +103,8 @@ const char* token_type(enum token::type type) {
case token::Lenof: return "lenof"; case token::Lenof: return "lenof";
case token::Load: return "load"; case token::Load: return "load";
case token::Store: return "store"; case token::Store: return "store";
case token::LoadGlobal: return "loadg";
case token::StoreGlobal: return "storeg";
case token::Call: return "call"; case token::Call: return "call";
case token::Jmp: return "jmp"; case token::Jmp: return "jmp";
case token::Jnz: return "jnz"; case token::Jnz: return "jnz";
@@ -134,12 +130,9 @@ struct mod_context {
}; };
furvm::mod mod; furvm::mod mod;
std::unordered_map<std::pair<std::string, furvm::function_sig>, std::unordered_map<std::string, std::vector<furvm::function_h>> functions;
furvm::function_h,
furlang::utility::
pair_hash<std::string, furvm::function_sig, std::hash<std::string>, furvm::detail::function_sig_hash>>
functions;
std::unordered_map<std::string, furvm::mod_type_h> types; std::unordered_map<std::string, furvm::mod_type_h> types;
std::unordered_map<std::string, std::uint16_t> variables;
std::unordered_map<std::string, label_context> labels; std::unordered_map<std::string, label_context> labels;
@@ -153,6 +146,48 @@ struct mod_context {
bool operator!() const { return error.type != generator_error::Success; } bool operator!() const { return error.type != generator_error::Success; }
}; };
bool compare_types(const furvm::mod_type_h& lhs, const furvm::mod_type_h& rhs) const {
if (lhs->type != rhs->type) return false;
switch (lhs->type) {
case furvm::mod_type::S8:
case furvm::mod_type::S16:
case furvm::mod_type::S32:
case furvm::mod_type::S64:
case furvm::mod_type::U8:
case furvm::mod_type::U16:
case furvm::mod_type::U32:
case furvm::mod_type::U64: return true;
case furvm::mod_type::Ptr:
case furvm::mod_type::Ref:
return compare_types(mod.type_at(lhs->value.typeRef), mod.type_at(rhs->value.typeRef));
case furvm::mod_type::Array:
return lhs->value.array.size == rhs->value.array.size &&
compare_types(mod.type_at(lhs->value.array.typeId), mod.type_at(rhs->value.array.typeId));
case furvm::mod_type::Slice:
return compare_types(mod.type_at(lhs->value.slice.typeId), mod.type_at(rhs->value.slice.typeId));
case furvm::mod_type::Import:
return lhs->value.imprt.modId == rhs->value.imprt.modId &&
lhs->value.imprt.typeId == rhs->value.imprt.typeId;
case furvm::mod_type::Count: break;
}
throw std::runtime_error("unreachable");
}
furvm::function_h find_function(const std::string& name, const furvm::function_sig& signature) const {
if (auto it = functions.find(name); it != functions.end()) {
for (const auto& func : it->second) {
if (func->signature().params.size() != signature.params.size()) continue;
std::size_t idx = 0;
while (idx < signature.params.size()) {
if (!compare_types(func->signature().params[idx], signature.params[idx])) break;
++idx;
}
if (idx == signature.params.size()) return func;
}
}
return {};
}
mod_context() { mod_context() {
types.emplace("s8", mod.emplace_type(furvm::mod_type::S8)); types.emplace("s8", mod.emplace_type(furvm::mod_type::S8));
types.emplace("u8", mod.emplace_type(furvm::mod_type::U8)); types.emplace("u8", mod.emplace_type(furvm::mod_type::U8));
@@ -188,6 +223,53 @@ struct mod_context {
return { { generator_error::Success }, token.value }; return { { generator_error::Success }, token.value };
} }
furvm::mod_type_h eat_type(lexer& lexer, const token& tok) {
switch (tok.type) {
case token::Dolar: {
auto result = eat_token(lexer, token::Identifier);
if (auto it = types.find(std::string(result->value.string)); it != types.end()) {
return it->second;
}
throw std::runtime_error("unknown type");
}
case token::Import: {
throw std::runtime_error("unimplemented");
// auto typeNameRes = eat_token(lexer, token::Identifier);
// if (!typeNameRes) return typeNameRes.error;
// return { generator_error::Success };
}
case token::Ref: {
auto result = next_token(lexer);
if (!result) throw std::runtime_error("error");
auto inner = eat_type(lexer, result.value);
return mod.emplace_type(furvm::mod_type::Ref, inner.id());
}
case token::Array: {
auto result = next_token(lexer);
if (!result) throw std::runtime_error("error");
auto inner = eat_type(lexer, result.value);
auto size = eat_token(lexer, token::Unsigned);
if (!size) throw std::runtime_error("error");
return mod.emplace_type(inner.id(), size->value.uint);
}
case token::Slice: {
auto result = next_token(lexer);
if (!result) throw std::runtime_error("error");
auto inner = eat_type(lexer, result.value);
return mod.emplace_type(furvm::mod_type::Slice, inner.id());
}
default: throw std::runtime_error("error");
}
}
generator_error generate(lexer& lexer) { generator_error generate(lexer& lexer) {
auto result = next_token(lexer); auto result = next_token(lexer);
if (result.error.type == generator_error::UnexpectedEof) return { generator_error::Eof }; if (result.error.type == generator_error::UnexpectedEof) return { generator_error::Eof };
@@ -209,23 +291,31 @@ struct mod_context {
(func.pub ? mod.emplace_function(func.name, std::move(func.signature), label.offset) (func.pub ? mod.emplace_function(func.name, std::move(func.signature), label.offset)
: mod.emplace_function(std::move(func.signature), label.offset)); : mod.emplace_function(std::move(func.signature), label.offset));
functions.emplace(std::make_pair(std::move(func.name), std::move(func.signature)), handle); functions[std::move(func.name)].push_back(handle);
handle.dispatch(); handle.dispatch();
} }
for (auto unknown : label.unknowns) { for (auto unknown : label.unknowns) {
std::ptrdiff_t jmpOff = static_cast<std::ptrdiff_t>(label.offset - unknown); const auto jmpOff = static_cast<std::ptrdiff_t>(label.offset) - static_cast<std::ptrdiff_t>(unknown);
if (jmpOff < std::numeric_limits<std::int8_t>::min() || if (jmpOff < std::numeric_limits<std::int8_t>::min() ||
jmpOff > std::numeric_limits<std::int8_t>::max()) { jmpOff > std::numeric_limits<std::int8_t>::max()) {
assert(false); // TODO: Further jumps are not implemented assert(false); // TODO: Further jumps are not implemented
} }
mod.bytecode()[unknown - 1] = jmpOff; mod.bytecode()[unknown - 1] = static_cast<std::int8_t>(jmpOff);
} }
label.functions = {}; label.functions = {};
label.unknowns = {}; label.unknowns = {};
return { generator_error::Success }; return { generator_error::Success };
} }
case token::Allocate: {
result = eat_token(lexer, token::Identifier);
if (!result) return result.error;
std::string name = std::string(result->value.string);
variables.emplace(name, variables.size());
mod.set_global_variable_count(variables.size());
return { generator_error::Success };
}
case token::Func: case token::Func:
case token::Type: case token::Type:
case token::Public: case token::Public:
@@ -248,14 +338,8 @@ struct mod_context {
result = next_token(lexer); result = next_token(lexer);
if (!result) return result.error; if (!result) return result.error;
while (result->type == token::Dolar) { while (result->type != token::EqSign) {
result = eat_token(lexer, token::Identifier); signature.params.push_back(eat_type(lexer, result.value));
if (!result) return result.error;
if (auto it = types.find(std::string(result->value.string)); it != types.end()) {
signature.params.push_back(it->second);
} else {
return { generator_error::UnknownType, "Unknown type "s + std::string(result->value.string) };
}
result = next_token(lexer); result = next_token(lexer);
if (!result) return result.error; if (!result) return result.error;
@@ -277,13 +361,15 @@ struct mod_context {
if (!result) return result.error; if (!result) return result.error;
} }
std::string name = std::string(nameRes->value.string);
if (!find_function(name, signature).empty()) throw std::runtime_error("function already defined");
switch (result->type) { switch (result->type) {
case token::Sha256: { case token::Sha256: {
result = eat_token(lexer, token::Identifier); result = eat_token(lexer, token::Identifier);
if (!result) return result.error; if (!result) return result.error;
std::string name = std::string(nameRes->value.string);
std::string labelName = std::string(result->value.string); std::string labelName = std::string(result->value.string);
std::size_t offset = 0; std::size_t offset = 0;
if (auto it = labels.find(labelName); it != labels.end()) { if (auto it = labels.find(labelName); it != labels.end()) {
@@ -296,10 +382,9 @@ struct mod_context {
furvm::function_h handle = furvm::function_h handle =
(pub ? mod.emplace_function(name, signature, offset) : mod.emplace_function(signature, offset)); (pub ? mod.emplace_function(name, signature, offset) : mod.emplace_function(signature, offset));
functions[name].push_back(handle);
functions.emplace(std::make_pair(name, std::move(signature)), handle);
handle.dispatch(); handle.dispatch();
return { generator_error::Success }; return { generator_error::Success };
} }
case token::Native: { case token::Native: {
@@ -312,10 +397,9 @@ struct mod_context {
furvm::function_h handle = (pub ? mod.emplace_function(name, signature, std::move(nativeName)) furvm::function_h handle = (pub ? mod.emplace_function(name, signature, std::move(nativeName))
: mod.emplace_function(signature, std::move(nativeName))); : mod.emplace_function(signature, std::move(nativeName)));
functions[name].push_back(handle);
functions.emplace(std::make_pair(name, std::move(signature)), handle);
handle.dispatch(); handle.dispatch();
return { generator_error::Success }; return { generator_error::Success };
} }
case token::Import: { case token::Import: {
@@ -337,42 +421,11 @@ struct mod_context {
result = next_token(lexer); result = next_token(lexer);
if (!result) return result.error; if (!result) return result.error;
switch (result->type) {
case token::Import: { auto type = eat_type(lexer, result.value);
auto typeNameRes = eat_token(lexer, token::Identifier); types.emplace(std::string(nameRes->value.string), std::move(type));
if (!typeNameRes) return typeNameRes.error;
return { generator_error::Success }; return { generator_error::Success };
} }
case token::Array: {
result = eat_token(lexer, token::Dolar);
if (!result) return result.error;
auto typeNameRes = eat_token(lexer, token::Identifier);
if (!typeNameRes) return typeNameRes.error;
auto size = eat_token(lexer, token::Unsigned);
if (!size) return size.error;
furvm::mod_type_id innerId = 0;
if (auto it = types.find(std::string(typeNameRes->value.string)); it != types.end()) {
innerId = it->second.id();
} else {
return { generator_error::UnknownType,
"Unknown type "s + std::string(typeNameRes->value.string) };
}
auto type = mod.emplace_type(innerId, size->value.uint);
types.emplace(std::string(nameRes->value.string), type);
type.dispatch();
return { generator_error::Success };
}
default:
return { generator_error::UnexpectedToken,
"Unexpected token "s + token_type(result->type) +
", expected either type, `import`, or `array`" };
}
}
return { generator_error::UnexpectedToken, return { generator_error::UnexpectedToken,
"Unexpected token "s + token_type(result->type) + ", expected either `func` or `type`" }; "Unexpected token "s + token_type(result->type) + ", expected either `func` or `type`" };
@@ -424,6 +477,7 @@ struct mod_context {
} }
case token::Array: case token::Array:
case token::Slice:
case token::Get: case token::Get:
case token::Set: case token::Set:
case token::Drop: case token::Drop:
@@ -447,16 +501,19 @@ struct mod_context {
case token::Lenof: case token::Lenof:
case token::Load: case token::Load:
case token::Store: case token::Store:
case token::LoadGlobal:
case token::StoreGlobal:
case token::Call: case token::Call:
case token::Jmp: case token::Jmp:
case token::Jnz: case token::Jnz:
case token::Ret: { case token::Ret: {
auto it = instructions.find(result->type); auto it = instructions.find(result->type);
assert(it != instructions.end()); assert(it != instructions.end());
mod.bytecode().push_back(static_cast<furvm::byte>(it->second.fur)); furvm::instruction instr{ it->second };
switch (it->second.arg) { instr.arg.type = furvm::instruction::s_arguments[instr.type];
case instruction::None: break; switch (instr.arg.type) {
case instruction::Type: { case furvm::instruction_argument::None: break;
case furvm::instruction_argument::Type: {
result = eat_token(lexer, token::Dolar); result = eat_token(lexer, token::Dolar);
if (!result) return result.error; if (!result) return result.error;
result = eat_token(lexer, token::Identifier); result = eat_token(lexer, token::Identifier);
@@ -464,46 +521,43 @@ struct mod_context {
auto type = types.find(std::string(result->value.string)); auto type = types.find(std::string(result->value.string));
if (type == types.end()) if (type == types.end())
return { generator_error::UnknownType, "Unknown type "s + std::string(result->value.string) }; return { generator_error::UnknownType, "Unknown type "s + std::string(result->value.string) };
auto id = type->second.id(); instr.arg.u32 = type->second.id();
mod.bytecode().push_back((id >> 0) & 0xFF);
mod.bytecode().push_back((id >> 8) & 0xFF);
mod.bytecode().push_back((id >> 16) & 0xFF);
mod.bytecode().push_back((id >> 24) & 0xFF);
} break; } break;
case instruction::Constant: { case furvm::instruction_argument::Constant: {
assert(false); // TODO: Unimplemented assert(false); // TODO: Unimplemented
} break; } break;
case instruction::Variable: { case furvm::instruction_argument::Variable: {
result = eat_token(lexer, token::Percent); result = eat_token(lexer, token::Percent);
if (!result) return result.error; if (!result) return result.error;
result = eat_token(lexer, token::Unsigned); result = eat_token(lexer, token::Unsigned);
if (!result) return result.error; if (!result) return result.error;
std::uint16_t var = result->value.uint; instr.arg.u16 = result->value.uint;
mod.bytecode().push_back((var >> 0) & 0xFF);
mod.bytecode().push_back((var >> 8) & 0xFF);
} break; } break;
case instruction::Function: { case furvm::instruction_argument::GlobalVariable: {
furvm::function_sig signature; result = eat_token(lexer, token::Percent);
while ((result = next_token(lexer)).error.type == generator_error::Success && if (!result) return result.error;
result->type == token::Dolar) {
result = eat_token(lexer, token::Identifier); result = eat_token(lexer, token::Identifier);
if (!result) return result.error; if (!result) return result.error;
auto type = types.find(std::string(result->value.string)); if (auto it = variables.find(std::string(result->value.string)); it != variables.end())
if (type == types.end()) instr.arg.u16 = it->second;
return { generator_error::UnknownType, "Unknown type "s + std::string(result->value.string) }; else
signature.params.push_back(type->second); throw std::runtime_error("Unknown global variable");
} break;
case furvm::instruction_argument::Function: {
furvm::function_sig signature;
while ((result = next_token(lexer)).error.type == generator_error::Success &&
result->type != token::Identifier) {
signature.params.push_back(eat_type(lexer, result.value));
} }
if (!result || result->type != token::Identifier) return result.error; if (!result || result->type != token::Identifier) return result.error;
std::string name(result->value.string); std::string name(result->value.string);
auto func = functions.find(std::make_pair(name, signature)); auto func = find_function(name, signature);
if (func == functions.end()) if (func.empty())
return { generator_error::UnknownType, "Unknown type "s + std::string(result->value.string) }; return { generator_error::UnknownType, "Unknown function "s + std::string(result->value.string) };
auto id = func->second.id(); instr.arg.u16 = func.id();
mod.bytecode().push_back((id >> 0) & 0xFF);
mod.bytecode().push_back((id >> 8) & 0xFF);
} break; } break;
case instruction::Label: { case furvm::instruction_argument::Offset: {
result = eat_token(lexer, token::Sha256); result = eat_token(lexer, token::Sha256);
if (!result) return result.error; if (!result) return result.error;
result = eat_token(lexer, token::Identifier); result = eat_token(lexer, token::Identifier);
@@ -511,18 +565,28 @@ struct mod_context {
auto& label = labels[std::string(result->value.string)]; auto& label = labels[std::string(result->value.string)];
auto offset = label.offset; auto offset = label.offset;
if (offset == label_context::INVALID) { if (offset == label_context::INVALID) {
label.unknowns.push_back(mod.bytecode().size() + 1); label.unknowns.push_back(mod.bytecode().size() + 2);
mod.bytecode().push_back(0); instr.arg.s8 = 0;
return { generator_error::Success }; break;
} }
std::ptrdiff_t jmpOff = static_cast<std::ptrdiff_t>(offset - mod.bytecode().size() - 1); const auto jmpOff =
static_cast<std::ptrdiff_t>(offset) - static_cast<std::ptrdiff_t>(mod.bytecode().size()) - 2;
if (jmpOff < std::numeric_limits<std::int8_t>::min() || if (jmpOff < std::numeric_limits<std::int8_t>::min() ||
jmpOff > std::numeric_limits<std::int8_t>::max()) { jmpOff > std::numeric_limits<std::int8_t>::max()) {
assert(false); // TODO: Further jumps are not implemented assert(false); // TODO: Further jumps are not implemented
} }
mod.bytecode().push_back(jmpOff); instr.arg.s8 = static_cast<std::int8_t>(jmpOff);
} break; } break;
case furvm::instruction_argument::S8:
case furvm::instruction_argument::U8:
case furvm::instruction_argument::S16:
case furvm::instruction_argument::U16:
case furvm::instruction_argument::S32:
case furvm::instruction_argument::U32:
case furvm::instruction_argument::Count:
default: throw std::runtime_error("unreachable");
} }
instr.write(mod.bytecode());
return { generator_error::Success }; return { generator_error::Success };
} }
+8 -4
View File
@@ -19,7 +19,7 @@ token_r lexer::next_token() {
// TODO: Add support for single-line comments // TODO: Add support for single-line comments
// TODO: Add support for multi-line comments // TODO: Add support for multi-line comments
if (m_cursor >= m_content.size()) return token_r{ lexer_error{ lexer_error::EndOfFile, location() } }; if (m_cursor >= m_content.size()) return token_r::error(lexer_error{ lexer_error::EndOfFile, location() });
// TODO: Add support for negative integers (I am positive thanks to stasiu :v:) // TODO: Add support for negative integers (I am positive thanks to stasiu :v:)
// TODO: Add support for hexadecimal and binary numeric literals // TODO: Add support for hexadecimal and binary numeric literals
@@ -39,9 +39,11 @@ token_r lexer::next_token() {
{ "import", token::Import }, { "import", token::Import },
{ "public", token::Public }, { "public", token::Public },
{ "private", token::Private }, { "private", token::Private },
{ "allocate", token::Allocate },
{ "push", token::Push }, { "push", token::Push },
{ "array", token::Array }, { "array", token::Array },
{ "slice", token::Slice },
{ "get", token::Get }, { "get", token::Get },
{ "set", token::Set }, { "set", token::Set },
{ "drop", token::Drop }, { "drop", token::Drop },
@@ -65,6 +67,8 @@ token_r lexer::next_token() {
{ "lenof", token::Lenof }, { "lenof", token::Lenof },
{ "load", token::Load }, { "load", token::Load },
{ "store", token::Store }, { "store", token::Store },
{ "loadg", token::LoadGlobal },
{ "storeg", token::StoreGlobal },
{ "call", token::Call }, { "call", token::Call },
{ "jmp", token::Jmp }, { "jmp", token::Jmp },
{ "jnz", token::Jnz }, { "jnz", token::Jnz },
@@ -89,9 +93,9 @@ token_r lexer::next_token() {
case '.': ++m_cursor; return { token::Dot }; case '.': ++m_cursor; return { token::Dot };
case ':': ++m_cursor; return { token::Colon }; case ':': ++m_cursor; return { token::Colon };
default: default:
return token_r{ return token_r::error(lexer_error{ lexer_error::UnknownCharacter,
lexer_error{ lexer_error::UnknownCharacter, location(), "Unknown character '"s + m_content[m_cursor] + "'" } location(),
}; "Unknown character '"s + m_content[m_cursor] + "'" });
} }
} }
+15
View File
@@ -0,0 +1,15 @@
file(GLOB_RECURSE FURC_SRCS "${CMAKE_SOURCE_DIR}/src/furc/**.cpp")
file(GLOB_RECURSE FURC_HDRS "${CMAKE_SOURCE_DIR}/include/furc/**.hpp")
add_library(libfurc ${FURC_SRCS} ${FURC_HDRS})
target_include_directories(libfurc PUBLIC ${CMAKE_SOURCE_DIR}/include/)
target_compile_definitions(libfurc PRIVATE LIBFURC)
set_target_properties(libfurc PROPERTIES PREFIX "")
target_link_libraries(libfurc PUBLIC furlang libfurvm)
add_executable(furc "${CMAKE_SOURCE_DIR}/src/furc/main.cpp")
target_link_libraries(furc PRIVATE libfurc)
include(GoogleTest)
add_executable(furc_tests "${CMAKE_SOURCE_DIR}/test/furc/ssa.cpp")
target_link_libraries(furc_tests PRIVATE libfurc GTest::gtest_main)
gtest_discover_tests(furc_tests)
+20
View File
@@ -0,0 +1,20 @@
#include "furc/back/furvm/target.hpp"
#include "furvm/function.hpp"
#include <stdexcept>
namespace furc {
furvm::mod furvm_generator::generate(const ir_module& mod) {
if (!mod.variables.empty()) throw std::runtime_error("global variables are not supported for furvm yet");
furvm::mod furvmMod;
for (const auto& func : mod.functions) {
furvmMod.emplace_function(furvm::function_sig{ {}, {} }, furvmMod.bytecode().size());
}
return furvmMod;
}
} // namespace furc
+11 -1
View File
@@ -1,6 +1,10 @@
#include "furc/back/ir.hpp" #ifndef LIBFURC
#include "furc/front/lexer.hpp" #include "furc/front/lexer.hpp"
#include "furc/front/parser.hpp" #include "furc/front/parser.hpp"
#include "furc/middle/ir.hpp"
#include "furc/middle/reg_gen.hpp"
#include "furc/middle/ssa.hpp"
#include "furlang/arena.hpp" #include "furlang/arena.hpp"
int main(void) { int main(void) {
@@ -16,6 +20,12 @@ int main(void) {
furc::lexer lexer = { "<AK>", content }; furc::lexer lexer = { "<AK>", content };
furc::parser parser = { std::move(lexer), arena }; furc::parser parser = { std::move(lexer), arena };
furc::ir_module irModule = furc::ir_generator::generate(parser.parse()); furc::ir_module irModule = furc::ir_generator::generate(parser.parse());
for (auto& func : irModule.functions) {
furc::ssa ssa(*func);
furc::reg_gen gen(*func, ssa);
}
return 0; return 0;
} }
#endif // LIBFURC
@@ -1,4 +1,4 @@
#include "furc/back/ir.hpp" #include "furc/middle/ir.hpp"
#include "furc/front/ast.hpp" #include "furc/front/ast.hpp"
+307
View File
@@ -0,0 +1,307 @@
/**
* Sources:
* - Practical Improvements to the Construction and Deconstruction of Static Single Assignment Form:
* https://web.archive.org/web/20100607003509/http://www.cs.rice.edu/~harv/my_papers/ssa.pdf
* - A Simple, Fast Dominance Algorithm:
* https://www.researchgate.net/publication/2569680_A_Simple_Fast_Dominance_Algorithm
*/
#include "furc/middle/ssa.hpp"
#include "furc/middle/ir.hpp"
#include <algorithm>
#include <cstddef>
#include <limits>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace furc {
void ssa::compute_cfg(const std::vector<ir_basic_block>& irBlocks, std::vector<cfg_block>& cfgBlocks) {
cfgBlocks.resize(irBlocks.size());
for (std::size_t i = 0; i < irBlocks.size(); ++i) {
const auto& block = irBlocks[i];
if (block.instructions.empty()) continue;
const auto& termInstr = block.instructions.back();
switch (termInstr.type) {
case ir_instruction::Branch: {
const auto& dst = termInstr.destination.value();
assert(dst.type == ir_operand::Block);
cfgBlocks[dst.value.block].preds.insert(i);
cfgBlocks[i].sucs.insert(dst.value.block);
} break;
case ir_instruction::BranchCond: {
const auto& dst = termInstr.destination.value();
assert(dst.type == ir_operand::BlockPair);
cfgBlocks[dst.value.blockPair.first].preds.insert(i);
cfgBlocks[dst.value.blockPair.second].preds.insert(i);
cfgBlocks[i].sucs.insert(dst.value.blockPair.first);
cfgBlocks[i].sucs.insert(dst.value.blockPair.second);
} break;
default: break;
}
}
}
void ssa::collect_registers(const std::vector<ir_basic_block>& irBlocks,
std::vector<register_info>& registers,
std::unordered_set<std::uint64_t>& globals) {
for (std::size_t i = 0; i < irBlocks.size(); ++i) {
const auto& block = irBlocks[i];
for (const auto& instr : block.instructions) {
for (const auto& src : instr.sources) {
if (src.type != ir_operand::Register) continue;
const auto& reg = registers.at(src.value.reg.name);
if (reg.sites.find(i) != reg.sites.end()) continue;
globals.insert(src.value.reg.name);
}
if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
registers[instr.destination->value.reg.name].sites.insert(i);
}
}
}
void ssa::build_dtree(const std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
const std::vector<std::size_t>& order) {
ssaBlocks[order.front()].idom = order.front();
bool changed = true;
while (changed) {
changed = false;
for (auto it = order.begin() + 1; it != order.end(); ++it) {
static constexpr std::uint64_t INVALID = std::numeric_limits<std::uint64_t>::max();
std::uint64_t newIdom = -1;
bool found = false;
for (std::uint64_t pred : cfgBlocks[*it].preds) {
if (ssaBlocks[pred].idom == INVALID) continue;
newIdom = found ? intersect(ssaBlocks, pred, newIdom) : pred;
found = true;
}
if (ssaBlocks[*it].idom != newIdom) {
ssaBlocks[*it].idom = newIdom;
changed = true;
}
}
}
}
void ssa::compute_dfrontiers(const std::vector<cfg_block>& cfgBlocks, std::vector<ssa_block>& ssaBlocks) {
for (std::uint64_t i = 0; i < ssaBlocks.size(); ++i) {
if (cfgBlocks[i].preds.size() < 2) continue;
const auto& cfgBlock = cfgBlocks[i];
auto& ssaBlock = ssaBlocks[i];
for (std::uint64_t worker : cfgBlock.preds) {
while (worker != ssaBlock.idom) {
ssaBlocks[worker].df.insert(i);
worker = ssaBlocks[worker].idom;
}
}
}
}
void ssa::compute_rpo(std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
std::vector<std::size_t>& order) {
std::unordered_set<std::size_t> visited;
if (!cfgBlocks.empty()) rpo_dfs(visited, order, 0, cfgBlocks);
std::reverse(order.begin(), order.end());
ssaBlocks.resize(cfgBlocks.size());
for (std::size_t i = 0; i < order.size(); ++i) {
ssaBlocks[order[i]].order = i;
}
}
void ssa::ssaification(std::vector<ir_basic_block>& irBlocks,
const std::vector<cfg_block>& cfgBlocks,
const std::vector<ssa_block>& ssaBlocks,
const std::vector<register_info>& registers,
const std::unordered_set<std::uint64_t>& globals) {
std::vector<std::uint64_t> worklist;
for (std::uint64_t i = 0; i < registers.size(); ++i) {
const auto& reg = registers[i];
if (reg.sites.size() < 2 || globals.find(i) == globals.end()) continue;
worklist.insert(worklist.end(), reg.sites.begin(), reg.sites.end());
std::unordered_set<std::uint64_t> done;
while (!worklist.empty()) {
const auto blockIdx = worklist.back();
worklist.pop_back();
for (auto frontier : ssaBlocks[blockIdx].df) {
if (done.find(frontier) != done.end()) continue;
done.insert(frontier);
auto& target = irBlocks[frontier];
ir_instruction instr = { ir_instruction::Phi, ir_operand{ ir_operand::Register, i } };
for (const auto& pred : cfgBlocks[frontier].preds)
instr.sources.emplace_back(ir_operand::PhiPair, i, pred);
target.instructions.emplace(target.instructions.begin(), std::move(instr));
if (reg.sites.find(frontier) == reg.sites.end()) worklist.push_back(frontier);
}
}
}
}
void ssa::rename(std::vector<ir_basic_block>& irBlocks,
std::size_t regCount,
const std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
const std::vector<std::uint64_t>& order) {
std::vector<std::uint64_t> counters;
std::vector<std::stack<std::uint64_t>> stacks;
counters.resize(regCount);
stacks.resize(regCount);
for (auto it = order.begin() + 1; it != order.end(); ++it) {
std::uint64_t parent = ssaBlocks[*it].idom;
if (parent != std::numeric_limits<std::uint64_t>::max()) ssaBlocks[parent].children.emplace(*it);
}
rename_rec(counters, stacks, irBlocks, cfgBlocks, ssaBlocks, order.front());
}
void ssa::rename_rec(std::vector<std::uint64_t>& counters,
std::vector<std::stack<std::uint64_t>>& stacks,
std::vector<ir_basic_block>& irBlocks,
const std::vector<cfg_block>& cfgBlocks,
const std::vector<ssa_block>& ssaBlocks,
std::size_t blockIdx) {
std::unordered_map<std::uint64_t, std::size_t> pushed;
auto& block = irBlocks[blockIdx];
for (auto& instr : block.instructions) {
if (instr.type == ir_instruction::Phi) {
auto reg = instr.destination->value.reg.name;
stacks[reg].push(instr.destination->value.reg.ver = counters[reg]++);
++pushed[reg];
continue;
}
for (auto& op : instr.sources) {
if (op.type != ir_operand::Register) continue;
auto reg = op.value.reg.name;
op.value.reg.ver = stacks[reg].top();
}
if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
auto reg = instr.destination->value.reg.name;
stacks[reg].push(instr.destination->value.reg.ver = counters[reg]++);
++pushed[reg];
}
for (auto succIdx : cfgBlocks[blockIdx].sucs) {
auto& succ = irBlocks[succIdx];
for (auto& instr : succ.instructions) {
if (instr.type != ir_instruction::Phi) break;
for (auto& op : instr.sources) {
if (op.value.phiPair.block != blockIdx) continue;
op.value.phiPair.reg.ver = stacks[op.value.phiPair.reg.name].top();
}
}
}
for (std::uint64_t child : ssaBlocks[blockIdx].children)
rename_rec(counters, stacks, irBlocks, cfgBlocks, ssaBlocks, child);
for (auto [reg, count] : pushed)
while ((count--) > 0)
stacks[reg].pop();
}
void ssa::rpo_dfs(std::unordered_set<std::size_t>& visited,
std::vector<std::size_t>& order,
std::size_t block,
const std::vector<cfg_block>& blocks) {
visited.insert(block);
for (auto succ : blocks[block].sucs) {
if (visited.find(succ) != visited.end()) continue;
rpo_dfs(visited, order, succ, blocks);
}
order.push_back(block);
}
std::size_t ssa::intersect(std::vector<ssa_block>& m_blocks, std::size_t b1, std::size_t b2) {
while (b1 != b2) {
while (m_blocks[b1].order > m_blocks[b2].order)
b1 = m_blocks[b1].idom;
while (m_blocks[b2].order > m_blocks[b1].order)
b2 = m_blocks[b2].idom;
}
return b1;
}
// // 5. Renaming
// std::vector<std::uint64_t> counters;
// std::vector<std::stack<std::uint64_t>> stacks;
//
// counters.resize(func.regCount);
// stacks.resize(func.regCount);
//
// for (std::size_t i = 1; i < order.size(); ++i) {
// std::size_t parent = blocks[order[i]].idom;
// if (parent != std::numeric_limits<std::size_t>::max()) blocks[parent].children.emplace(order[i]);
// }
//
// auto rename = [&counters, &stacks, &blocks, &func](auto& self, std::size_t blockIdx) -> void {
// std::unordered_map<std::size_t, std::size_t> pushed;
//
// auto& block = func.blocks[blockIdx];
// for (auto& instr : block.instructions) {
// if (instr.type == ir_instruction::Phi) {
// auto reg = instr.destination->value.reg.name;
// auto idx = counters[reg]++;
// instr.destination->value.reg.ver = idx;
// stacks[reg].push(idx);
// ++pushed[reg];
// continue;
// }
//
// for (auto& op : instr.sources) {
// if (op.type != ir_operand::Register) continue;
// auto reg = op.value.reg.name;
// op.value.reg.ver = stacks[reg].top();
// }
//
// if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
// auto reg = instr.destination->value.reg.name;
// auto idx = counters[reg]++;
// instr.destination->value.reg.ver = idx;
// stacks[reg].push(idx);
// ++pushed[reg];
// }
//
// for (auto succIdx : blocks[blockIdx].sucs) {
// auto& succ = func.blocks[succIdx];
// for (auto& instr : succ.instructions) {
// if (instr.type != ir_instruction::Phi) break;
// for (auto& op : instr.sources) {
// if (op.value.phiPair.block != blockIdx) continue;
// op.value.phiPair.reg.ver = stacks[op.value.phiPair.reg.name].top();
// }
// }
// }
//
// for (std::size_t child : blocks[blockIdx].children)
// self(self, child);
//
// for (auto [reg, count] : pushed)
// while (count--)
// stacks[reg].pop();
// };
// rename(rename, order.front());
// }
} // namespace furc
+5
View File
@@ -0,0 +1,5 @@
file(GLOB_RECURSE FURDB_SRCS "${CMAKE_SOURCE_DIR}/src/furdb/**.cpp")
file(GLOB_RECURSE FURDB_HDRS "${CMAKE_SOURCE_DIR}/include/furdb/**.hpp")
add_executable(furdb ${FURDB_SRCS} ${FURDB_HDRS})
target_include_directories(furdb PRIVATE ${CMAKE_SOURCE_DIR}/include/)
target_link_libraries(furdb PUBLIC furlang libfurc libfurvm isocline)
+214
View File
@@ -0,0 +1,214 @@
#include "command.hpp"
#include "furvm/executor.hpp"
#include "furvm/function.hpp"
#include "furvm/instruction.hpp"
#include "furvm/module.hpp"
#include "furvm/thing.hpp"
#include <cctype>
#include <cstddef>
#include <iostream>
#include <random>
#include <string>
#include <utility>
command_info command::parse(std::string_view line) {
command_info info;
while (!line.empty() && std::isspace(line.front()) != 0)
line = line.substr(1);
std::size_t length = 0;
while (line.size() > length && std::isalnum(line.at(length)) != 0)
++length;
info.commandName = line.substr(0, length);
line = line.substr(length);
while (true) {
while (!line.empty() && std::isspace(line.front()) != 0)
line = line.substr(1);
if (line.empty()) break;
length = 0;
while (line.size() > length && std::isalnum(line.at(length)) != 0)
++length;
info.args.push_back(line.substr(0, length));
line = line.substr(length);
}
return std::move(info);
}
void quit_command::execute(context& ctx, const command_info& info) {
static std::random_device s_rd;
static std::mt19937 s_gen(s_rd());
static std::uniform_int_distribution<> s_dis(1, 1000);
int roll = s_dis(s_gen);
std::cout << "Farewell";
if (roll == 1) {
std::cout << ", stasiu pensive";
} else if (roll <= 100) {
std::cout << ", kromek-man";
}
std::cout << "!\n";
ctx.running = false;
}
void run_command::execute(context& ctx, const command_info& info) {
if (!ctx.executor->done()) {
std::cout << "A program is currently running, do you want to kill it? (y/N) ";
std::string answer;
if (!std::getline(std::cin, answer)) {
ctx.running = false;
return;
}
if (answer != "y" && answer != "Y" && answer != "yes") return;
ctx.kill();
}
std::cout << "Running the program\n";
ctx.run();
}
void continue_command::execute(context& ctx, const command_info& info) {
ctx.run();
}
void next_command::execute(context& ctx, const command_info& info) {
if (ctx.executor->done()) {
std::cout << "No program's currently running\n";
return;
}
ctx.executor->unsuspend();
ctx.executor->step();
ctx.print_instruction();
}
static void print_type(const furvm::thing_type& type) {
switch (type.type) {
case furvm::thing_type::S8: std::cout << "s8"; break;
case furvm::thing_type::S16: std::cout << "s16"; break;
case furvm::thing_type::S32: std::cout << "s32"; break;
case furvm::thing_type::S64: std::cout << "s64"; break;
case furvm::thing_type::U8: std::cout << "u8"; break;
case furvm::thing_type::U16: std::cout << "u16"; break;
case furvm::thing_type::U32: std::cout << "u32"; break;
case furvm::thing_type::U64: std::cout << "u64"; break;
case furvm::thing_type::Ptr: std::cout << "ptr"; break;
case furvm::thing_type::Ref: std::cout << "ref"; break;
case furvm::thing_type::Array:
std::cout << "array(";
print_type(*type.value.array.type);
std::cout << ", ";
if (type.value.array.size == 0)
std::cout << "dynamic";
else
std::cout << type.value.array.size;
std::cout << ")";
break;
case furvm::thing_type::Slice:
std::cout << "slice(";
print_type(*type.value.slice.type);
std::cout << ")";
break;
case furvm::thing_type::Count: break;
}
}
static void print_thing(const furvm::thing<>& thing) {
std::cout << '(';
print_type(thing.type());
std::cout << ") ";
switch (thing.type().type) {
case furvm::thing_type::S8: std::cout << std::to_string(thing.get<furvm::s8>()); break;
case furvm::thing_type::S16: std::cout << thing.get<furvm::s16>(); break;
case furvm::thing_type::S32: std::cout << thing.get<furvm::s32>(); break;
case furvm::thing_type::S64: std::cout << thing.get<furvm::s64>(); break;
case furvm::thing_type::U8: std::cout << std::to_string(thing.get<furvm::u8>()); break;
case furvm::thing_type::U16: std::cout << thing.get<furvm::u16>(); break;
case furvm::thing_type::U32: std::cout << thing.get<furvm::u32>(); break;
case furvm::thing_type::U64: std::cout << thing.get<furvm::u64>(); break;
case furvm::thing_type::Ptr: std::cout << thing.get<const void*>(); break;
case furvm::thing_type::Array:
case furvm::thing_type::Slice: {
if (thing.length() == 0) {
std::cout << "{}";
break;
}
std::cout << '(' << thing.length() << ") { ";
for (const auto& el : thing) {
print_thing(el);
std::cout << ", ";
}
std::cout << " }";
} break;
case furvm::thing_type::Ref:
case furvm::thing_type::Count: break;
}
}
static void breakpoint_hit(furvm::executor& executor, void* data) {
std::cout << "Breakpoint hit\n";
context* ctx = reinterpret_cast<context*>(data);
ctx->halt = true;
ctx->print_instruction();
}
void break_command::execute(context& ctx, const command_info& info) {
if (info.args.empty()) {
std::cerr << "Usage: " << info.commandName << " <function name>\n";
return;
}
std::size_t count = 0;
for (const auto& [id, sigPair] : ctx.mod->function_map()) {
if (sigPair.first != info.args[0]) continue;
const auto& func = ctx.mod->function_at(id);
if (func->type() != furvm::function_t::Normal) continue;
count += 1;
ctx.mod->set_breakpoint(func->position(), furvm::breakpoint{ breakpoint_hit, &ctx });
}
if (count == 0) {
std::cerr << "No function \"" << info.args[0] << "\" found!\n";
return;
}
std::cout << "Breakpoint set in " << count << " places\n";
}
void info_command::execute(context& ctx, const command_info& info) {
if (info.args.empty()) {
std::cout << "Possible arguments:\n";
std::cout << "- variables\n";
std::cout << "- stack\n";
} else if (info.args[0] == "variables") {
if (ctx.executor->done()) {
std::cerr << "Not running\n";
return;
}
std::cout << "Variables:\n";
const auto& frame = ctx.executor->top_frame();
for (std::size_t i = 0; i < frame.variables.size(); ++i) {
// TODO: Add debug information to modules
std::cout << "- %" << i << " = ";
print_thing(frame.variables[i]);
std::cout << '\n';
}
} else if (info.args[0] == "stack") {
if (ctx.executor->done()) {
std::cerr << "Not running\n";
return;
}
std::cout << "Stack from top to bottom:\n";
const auto& frame = ctx.executor->top_frame();
for (std::size_t begin = frame.stackBase, i = ctx.executor->stack().size(); i > begin;) {
std::cout << "- ";
print_thing(ctx.executor->stack()[--i]);
std::cout << '\n';
}
} else {
std::cerr << "Unexpected argument \"" << info.args[0] << "\"\n";
}
}
+59
View File
@@ -0,0 +1,59 @@
#ifndef COMMAND_HPP
#define COMMAND_HPP
#include "context.hpp"
#include <string_view>
#include <vector>
struct command_info {
std::string_view commandName;
std::vector<std::string_view> args;
};
class command {
public:
command() = default;
virtual ~command() = default;
command(command&&) noexcept = default;
command& operator=(command&&) noexcept = default;
command(const command&) = delete;
command& operator=(const command&) = delete;
public:
virtual void execute(context& ctx, const command_info& info) = 0;
public:
static command_info parse(std::string_view line);
};
class quit_command final : public command {
public:
void execute(context& ctx, const command_info& info) override;
};
class run_command final : public command {
public:
void execute(context& ctx, const command_info& info) override;
};
class continue_command final : public command {
public:
void execute(context& ctx, const command_info& info) override;
};
class next_command final : public command {
public:
void execute(context& ctx, const command_info& info) override;
};
class break_command final : public command {
public:
void execute(context& ctx, const command_info& info) override;
};
class info_command final : public command {
public:
void execute(context& ctx, const command_info& info) override;
};
#endif // COMMAND_HPP
+137
View File
@@ -0,0 +1,137 @@
#include "context.hpp"
#include <furvm/executor.hpp>
#include <furvm/instruction.hpp>
#include <iostream>
#include <stdexcept>
void context::kill() const {
while (!executor->done())
executor->pop_frame();
}
void context::init() {
kill();
executor->push_frame(mod, *mainFunction);
executor->clear_flags();
}
void context::run() {
if (executor->done()) init();
executor->unsuspend();
halt = false;
while (!executor->done() && !halt)
executor->step();
if (executor->done()) {
std::cout << "Execution finished\n";
halt = true;
}
}
void context::print_instruction() const {
static const char* s_instrNames[furvm::instruction::Count] = {
// NoOperation
"nop",
// PushS8
"push $s8",
// PushU8
"push $u8",
// PushS16
"push $s16",
// PushU16
"push $U16",
// PushS32
"push $S32",
// PushU32
"push $U32",
// PushConstant
"push",
// Array
"array",
// Slice
"slice",
// Get
"get",
// Set
"set",
// Drop
"drop",
// Duplicate
"dup",
// Swap
"swap",
// Clone
"clone",
// Reference
"ref",
// Add
"add",
// Sub
"sub",
// Mul
"mul",
// Div
"div",
// Mod
"mod",
// Equals
"eq",
// NotEquals
"ne",
// LessThan
"lt",
// GreaterThan
"gt",
// LessEqual
"le",
// GreaterEqual
"ge",
// Pointerof
"pointerof",
// Sizeof
"sizeof",
// Lengthof
"lenof",
// Load
"load",
// Store
"store",
// LoadGlobal
"loadg",
// StoreGlobal
"storeg",
// Call
"call",
// Jump
"jmp",
// JumpNotZero
"jnz",
// Return
"ret",
};
furvm::instruction instr{};
instr.read(mod->bytecode_view().subview(executor->top_frame().position));
std::cout << s_instrNames[instr.type];
if (instr.arg.type != furvm::instruction_argument_t::None) {
std::cout << ' ';
switch (instr.arg.type) {
case furvm::instruction_argument::S8: std::cout << std::to_string(instr.arg.s8); break;
case furvm::instruction_argument::U8: std::cout << std::to_string(instr.arg.u8); break;
case furvm::instruction_argument::S16: std::cout << instr.arg.s16; break;
case furvm::instruction_argument::U16: std::cout << instr.arg.u16; break;
case furvm::instruction_argument::S32: std::cout << instr.arg.s32; break;
case furvm::instruction_argument::U32: std::cout << instr.arg.u32; break;
case furvm::instruction_argument::None:
case furvm::instruction_argument::Count: break;
case furvm::instruction_argument::Constant: throw std::runtime_error("unimplemented");
case furvm::instruction_argument::Type: std::cout << "$__t" << instr.arg.u32; break;
case furvm::instruction_argument::Variable: std::cout << "%" << instr.arg.u16; break;
case furvm::instruction_argument::GlobalVariable: std::cout << "%__g" << instr.arg.u16; break;
case furvm::instruction_argument::Function: std::cout << "<a function>"; break;
case furvm::instruction_argument::Offset: std::cout << std::to_string(instr.arg.s8); break;
}
}
std::cout << '\n';
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef CONTEXT_HPP
#define CONTEXT_HPP
#include <furvm/context.hpp>
#include <furvm/executor.hpp>
#include <furvm/fwd.hpp>
#include <furvm/module.hpp>
struct context {
furvm::context_p context = std::make_shared<furvm::context>();
furvm::mod_h mod;
furvm::executor* executor = nullptr;
furvm::function_h mainFunction;
bool running = true;
bool halt = true;
void kill() const;
void init();
void run();
void print_instruction() const;
};
#endif // CONTEXT_HPP
+102
View File
@@ -0,0 +1,102 @@
#include "command.hpp"
#include "context.hpp"
#include "furvm/function.hpp"
#include "isocline.h"
#include <fstream>
#include <furvm/fwd.hpp>
#include <iostream>
#include <string>
#include <unordered_map>
#include <utility>
// taken from furvm uwu :3 ^^ nya~ ngh~
static void print_thing(const furvm::thing<>& thing) {
using namespace furvm;
switch (thing.type().type) {
case thing_type::S8: std::cout << thing.cast_to<s16>(); break;
case thing_type::S16: std::cout << thing.get<s16>(); break;
case thing_type::S32: std::cout << thing.get<s32>(); break;
case thing_type::S64: std::cout << thing.get<s64>(); break;
case thing_type::U8: std::cout << thing.get<u8>(); break;
case thing_type::U16: std::cout << thing.get<u16>(); break;
case thing_type::U32: std::cout << thing.get<u32>(); break;
case thing_type::U64: std::cout << thing.get<u64>(); break;
case thing_type::Array:
std::cout << "{ ";
for (u64 i = 0; i < thing.length(); ++i) {
if (i > 0) std::cout << ", ";
print_thing(thing.at(i));
}
std::cout << " }";
break;
default: std::cerr << "{Type not recognized}";
}
}
// TODO: Argument parsing
// TODO: Multiple modules
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <module.fmod>\n";
return 1;
}
static std::unordered_map<std::string_view, command*> s_commands;
s_commands["exit"] = s_commands["quit"] = s_commands["q"] = new quit_command();
s_commands["run"] = s_commands["r"] = new run_command();
s_commands["continue"] = s_commands["c"] = new continue_command();
s_commands["next"] = s_commands["n"] = new next_command();
s_commands["break"] = s_commands["b"] = new break_command();
s_commands["info"] = s_commands["i"] = new info_command();
ic_set_history(nullptr, -1);
try {
std::ifstream file(argv[1], std::ios::binary | std::ios::in);
context ctx;
try {
ctx.mod = ctx.context->emplace(argv[1], furvm::mod::load(file));
} catch (std::exception ex) {
std::cerr << "Failed to load module " << argv[1] << ": " << ex.what() << '\n';
return 1;
}
ctx.executor = &ctx.context->allocate_executor();
ctx.mainFunction = ctx.mod->function_at("main", furvm::function_sig{});
ctx.mod->set_native_function("println", [](furvm::executor& executor) {
print_thing(executor.load_thing(0));
std::cout << '\n';
});
std::string inputLine;
std::string prevInput;
while (ctx.running) {
std::swap(inputLine, prevInput);
char* inputRaw = ic_readline("furdb");
if (inputRaw == nullptr) break;
inputLine = inputRaw;
free(inputRaw); // NOLINT
if (inputLine.empty()) {
inputLine = prevInput;
}
auto info = command::parse(inputLine);
auto it = s_commands.find(info.commandName);
if (it == s_commands.end()) {
std::cerr << "Unknown command \"" << info.commandName << "\"\n";
continue;
}
it->second->execute(ctx, info);
}
} catch (const std::exception& ex) {
std::cerr << "Uncaught exception: " << ex.what() << '\n';
return 1;
}
return 0;
}
+4
View File
@@ -0,0 +1,4 @@
file(GLOB_RECURSE FURLANG_SRCS "${CMAKE_SOURCE_DIR}/src/furlang/**.cpp")
file(GLOB_RECURSE FURLANG_HDRS "${CMAKE_SOURCE_DIR}/include/furlang/**.hpp")
add_library(furlang STATIC ${FURLANG_SRCS} ${FURLANG_HDRS})
target_include_directories(furlang PUBLIC ${CMAKE_SOURCE_DIR}/include/)
+137
View File
@@ -0,0 +1,137 @@
#include "furlang/serialization/io.hpp"
#include <cstring>
namespace furlang::serialization {
result<error> byte_writer::write_s8(std::int8_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_u8(std::uint8_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_s16(std::int16_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_u16(std::uint16_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_s32(std::int32_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_u32(std::uint32_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_s64(std::int64_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_u64(std::uint64_t value) {
if (m_endianness == endianness::Little)
write_integral_le(value);
else
write_integral_be(value);
return {};
}
result<error> byte_writer::write_string(const char* string) {
write_u16(std::strlen(string));
m_bytes.insert(m_bytes.end(), string, string + std::strlen(string));
return {};
}
result<error> byte_writer::write_string(std::string_view string) {
write_u16(string.length());
m_bytes.insert(m_bytes.end(), string.begin(), string.end());
return {};
}
result<error> byte_writer::write_string(const std::string& string) {
write_u16(string.length());
m_bytes.insert(m_bytes.end(), string.begin(), string.end());
return {};
}
result<error, std::int8_t> byte_reader::read_s8() {
return m_endianness == endianness::Little ? read_integral_le<std::int8_t>() : read_integral_be<std::int8_t>();
}
result<error, std::uint8_t> byte_reader::read_u8() {
return m_endianness == endianness::Little ? read_integral_le<std::uint8_t>() : read_integral_be<std::uint8_t>();
}
result<error, std::int16_t> byte_reader::read_s16() {
return m_endianness == endianness::Little ? read_integral_le<std::int16_t>() : read_integral_be<std::int16_t>();
}
result<error, std::uint16_t> byte_reader::read_u16() {
return m_endianness == endianness::Little ? read_integral_le<std::uint16_t>() : read_integral_be<std::uint16_t>();
}
result<error, std::int32_t> byte_reader::read_s32() {
return m_endianness == endianness::Little ? read_integral_le<std::int32_t>() : read_integral_be<std::int32_t>();
}
result<error, std::uint32_t> byte_reader::read_u32() {
return m_endianness == endianness::Little ? read_integral_le<std::uint32_t>() : read_integral_be<std::uint32_t>();
}
result<error, std::int64_t> byte_reader::read_s64() {
return m_endianness == endianness::Little ? read_integral_le<std::int64_t>() : read_integral_be<std::int64_t>();
}
result<error, std::uint64_t> byte_reader::read_u64() {
return m_endianness == endianness::Little ? read_integral_le<std::uint64_t>() : read_integral_be<std::uint64_t>();
}
result<error, std::string> byte_reader::read_string() {
std::string str;
auto length = read_u16();
if (length.has_error()) return length;
str.resize(length.value());
if (m_offset + str.length() >= m_length)
return result<error, std::string>::error(error{ error_code::EndOfFile, "", m_length });
str.assign(reinterpret_cast<const char*>(m_bytes) + m_offset, str.length());
m_offset += str.length();
return str;
}
std::size_t byte_reader::offset() const {
return m_offset;
}
} // namespace furlang::serialization
@@ -1,16 +1,16 @@
file(GLOB_RECURSE FURVM_SRCS "src/**.cpp") file(GLOB_RECURSE FURVM_SRCS "${CMAKE_SOURCE_DIR}/src/furvm/**.cpp")
file(GLOB_RECURSE FURVM_HDRS "include/**.hpp") file(GLOB_RECURSE FURVM_HDRS "${CMAKE_SOURCE_DIR}/include/furvm/**.hpp")
add_library(libfurvm ${FURVM_SRCS} ${FURVM_HDRS}) add_library(libfurvm ${FURVM_SRCS} ${FURVM_HDRS})
target_include_directories(libfurvm PUBLIC include/) target_include_directories(libfurvm PUBLIC ${CMAKE_SOURCE_DIR}/include/)
target_compile_definitions(libfurvm PRIVATE LIBFURVM) target_compile_definitions(libfurvm PRIVATE LIBFURVM)
set_target_properties(libfurvm PROPERTIES PREFIX "") set_target_properties(libfurvm PROPERTIES PREFIX "")
target_link_libraries(libfurvm PUBLIC furlang) target_link_libraries(libfurvm PUBLIC furlang)
add_executable(furvm src/main.cpp) add_executable(furvm "${CMAKE_SOURCE_DIR}/src/furvm/main.cpp")
target_link_libraries(furvm PRIVATE libfurvm) target_link_libraries(furvm PRIVATE libfurvm)
include(GoogleTest) include(GoogleTest)
file(GLOB_RECURSE FURVM_TESTS "test/**.cpp") file(GLOB_RECURSE FURVM_TESTS "${CMAKE_SOURCE_DIR}/test/furvm/**.cpp")
add_executable(furvm_tests ${FURVM_TESTS}) add_executable(furvm_tests ${FURVM_TESTS})
target_link_libraries(furvm_tests PRIVATE libfurvm GTest::gtest_main) target_link_libraries(furvm_tests PRIVATE libfurvm GTest::gtest_main)
gtest_discover_tests(furvm_tests) gtest_discover_tests(furvm_tests)
+421
View File
@@ -0,0 +1,421 @@
#include "furvm/executor.hpp"
#include "furlang/view.hpp"
#include "furvm/context.hpp" // IWYU pragma: keep
#include "furvm/exceptions.hpp"
#include "furvm/function.hpp" // IWYU pragma: keep
#include "furvm/fwd.hpp"
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
#include <cassert>
#include <cstdint>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
namespace furvm {
thing_type executor::thing_type_impl(mod_h mod, mod_type type) const {
while (type.type == mod_type::Import) {
auto imprt = std::move(type.value.imprt);
mod = m_context->at(imprt.modId);
type = *mod->type_at(imprt.typeId);
}
switch (static_cast<enum thing_type::type>(type.type)) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
case thing_type::String: return { static_cast<enum thing_type::type>(type.type) };
case thing_type::Ptr: return { thing_type::Ptr, mod_to_thing_type(mod, *mod->type_at(type.value.typeRef)) };
case thing_type::Ref: return { thing_type::Ref, mod_to_thing_type(mod, *mod->type_at(type.value.typeRef)) };
case thing_type::Array: {
return { static_cast<enum thing_type::type>(type.type),
{ mod_to_thing_type(mod, *mod->type_at(type.value.array.typeId)), type.value.array.size } };
}
case thing_type::Slice:
return { thing_type::Slice, mod_to_thing_type(mod, *mod->type_at(type.value.slice.typeId)) };
case thing_type::Count: break;
}
throw std::runtime_error("invalid thing type");
}
thing_type* executor::mod_to_thing_type(const mod_h& mod, const mod_type& type) const {
struct thing_type thingType = thing_type_impl(mod, type);
return m_context->tt_store().insert(thingType);
}
bool executor::compare_thing_types(const thing_type& lhs, const thing_type& rhs) {
if (lhs.type != rhs.type) return false;
switch (lhs.type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64:
case thing_type::String: return true;
case thing_type::Ptr:
case thing_type::Ref: return compare_thing_types(*lhs.value.typeRef, *rhs.value.typeRef);
case thing_type::Array:
return lhs.value.array.size == rhs.value.array.size &&
compare_thing_types(*lhs.value.array.type, *rhs.value.array.type);
case thing_type::Slice: return compare_thing_types(*lhs.value.slice.type, *rhs.value.slice.type);
case thing_type::Count: break;
}
throw std::runtime_error("unreachable");
}
void executor::push_frame(const mod_h& mod, function function) {
m_stackStorage.push_frame();
mod_h modInst = mod;
while (function.type() == function_t::Import) {
modInst = m_context->at(function.imp().mod);
function = *modInst->function_at(function.imp().function);
}
auto signature = function.signature();
std::vector<stack_thing> args;
args.reserve(signature.params.size());
for (const auto& param : signature.params) {
auto arg = pop_thing();
if (!compare_thing_types(arg.type(), *mod_to_thing_type(mod, *param)))
throw std::runtime_error("function argument type mismatch");
args.emplace_back(std::move(arg));
}
struct thing_type* returnType = nullptr;
if (function.signature().returnType.has_value())
returnType = mod_to_thing_type(mod, *function.signature().returnType.value()); // NOLINT
switch (function.type()) {
case function_t::Normal: {
m_frames.emplace(
(struct executor::frame){ mod, function.position(), m_stack.size(), returnType, std::move(args) });
} break;
case function_t::Native: {
m_frames.emplace((struct executor::frame){ mod, 0, m_stack.size(), returnType, std::move(args) });
modInst->get_native_function(function.native())(*this);
pop_frame();
return;
}
default: throw std::runtime_error("unexpected function type");
}
if (m_newFrameCb) m_newFrameCb(*this);
if (!m_frames.empty()) {
m_flags = m_flags & ~executor_flags::Done;
} else {
m_flags = m_flags | executor_flags::Done;
}
m_flags = m_flags & ~executor_flags::JustHit;
}
struct executor::frame executor::pop_frame() {
if (m_frames.empty()) throw stack_underflow();
struct executor::frame frame = m_frames.top();
m_frames.pop();
std::optional<stack_thing> returnValue;
if (frame.returnType != nullptr) {
returnValue = pop_thing();
if (returnValue->type() != *frame.returnType) throw std::runtime_error("function return type mismatch");
}
if (m_stack.size() != frame.stackBase) throw std::runtime_error("unexhausted stack");
if (returnValue.has_value()) push_thing(std::move(returnValue.value()));
if (!m_frames.empty()) {
m_flags = m_flags & ~executor_flags::Done;
} else {
m_flags = m_flags | executor_flags::Done;
}
m_flags = m_flags & ~executor_flags::JustHit;
m_stackStorage.pop_frame();
return frame;
}
struct executor::frame executor::top_frame() const {
return m_frames.top();
}
executor::stack_thing& executor::push_thing(stack_thing&& thing) {
return m_stack.emplace_back(std::move(thing));
}
executor::stack_thing& executor::push_thing(const stack_thing& thing) {
return m_stack.emplace_back(thing);
}
executor::stack_thing executor::pop_thing() {
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
auto top = std::move(m_stack.back());
m_stack.pop_back();
return std::move(top);
}
executor::stack_thing& executor::top_thing() {
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
return m_stack.back();
}
const executor::stack_thing& executor::top_thing() const {
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
return m_stack.back();
}
void executor::store_thing(variable_t variable, const stack_thing& thing) {
auto& frame = m_frames.top();
if (frame.variables.size() <= variable) frame.variables.resize(variable + 1);
frame.variables[variable] = thing;
}
void executor::store_thing(variable_t variable, stack_thing&& thing) {
auto& frame = m_frames.top();
if (frame.variables.size() <= variable) frame.variables.resize(variable + 1);
frame.variables[variable] = std::move(thing);
}
executor::stack_thing& executor::load_thing(variable_t variable) {
auto& frame = m_frames.top();
return frame.variables[variable];
}
const executor::stack_thing& executor::load_thing(variable_t variable) const {
const auto& frame = m_frames.top();
return frame.variables[variable];
}
void executor::step() {
if ((m_flags & executor_flags::Suspended) == executor_flags::Suspended) return;
struct frame& frame = m_frames.top();
if ((m_flags & executor_flags::JustHit) != executor_flags::JustHit && frame.mod->has_breakpoint(frame.position)) {
m_flags = m_flags | executor_flags::Suspended | executor_flags::JustHit;
const auto& bp = frame.mod->breakpoint_at(frame.position);
bp.callback(*this, bp.data);
return;
}
m_flags = m_flags & ~executor_flags::JustHit;
instruction instr{};
frame.position += instr.read(frame.mod->bytecode_view().subview(frame.position));
switch (instr.type) {
case instruction_t::NoOperation: break;
case instruction_t::PushS8: {
push_thing({ (struct thing_type){ thing_type::S8 }, { m_stackStorage } }).get<s8>() = instr.arg.s8;
} break;
case instruction_t::PushU8: {
push_thing({ (struct thing_type){ thing_type::U8 }, { m_stackStorage } }).get<u8>() = instr.arg.u8;
} break;
case instruction_t::PushS16: {
push_thing({ (struct thing_type){ thing_type::S16 }, { m_stackStorage } }).get<s16>() = instr.arg.s16;
} break;
case instruction_t::PushU16: {
push_thing({ (struct thing_type){ thing_type::U16 }, { m_stackStorage } }).get<u16>() = instr.arg.u16;
} break;
case instruction_t::PushS32: {
push_thing({ (struct thing_type){ thing_type::S32 }, { m_stackStorage } }).get<s32>() = instr.arg.s8; // NOLINT
} break;
case instruction_t::PushU32: {
push_thing({ (struct thing_type){ thing_type::U32 }, { m_stackStorage } }).get<u32>() =
static_cast<u32>(instr.arg.u8);
} break;
case instruction_t::PushConstant: {
auto constant = frame.mod->constant_at(instr.arg.u16);
switch (constant.type) {
case constant::S32:
push_thing({ (struct thing_type){ thing_type::S32 }, { m_stackStorage } }).get<s32>() =
constant.s32; // NOLINT
break;
case constant::U32:
push_thing({ (struct thing_type){ thing_type::U32 }, { m_stackStorage } }).get<u32>() =
constant.u32; // NOLINT
break;
case constant::S64:
push_thing({ (struct thing_type){ thing_type::S64 }, { m_stackStorage } }).get<s64>() =
constant.s64; // NOLINT
break;
case constant::U64:
push_thing({ (struct thing_type){ thing_type::U64 }, { m_stackStorage } }).get<u64>() =
constant.u64; // NOLINT
break;
case constant::String: throw std::runtime_error("unimplemented");
default: throw std::runtime_error("invalid constant");
}
} break;
case instruction_t::Array: {
const auto& type = *mod_to_thing_type(frame.mod, *frame.mod->type_at(instr.arg.u32));
if (type.type != thing_type::Array || type.value.array.type == nullptr || type.value.array.type == &type)
throw std::runtime_error("invalid array type");
auto& array = push_thing({ type, { m_stackStorage } });
if (type.value.array.size == 0) {
auto sizeThing = pop_thing();
std::int64_t size = sizeThing.integer();
array.resize(size);
}
} break;
case instruction_t::Slice: {
auto length = pop_thing();
auto start = pop_thing();
auto array = pop_thing();
push_thing(array.slice(start.integer(), length.integer()));
} break;
case instruction_t::Get: {
auto index = pop_thing();
auto array = pop_thing();
push_thing(array.at(index.integer()));
} break;
case instruction_t::Set: {
auto element = pop_thing();
auto index = pop_thing();
auto array = pop_thing();
array.at(index.integer()).assign(std::move(element));
} break;
case instruction_t::Drop: {
pop_thing();
} break;
case instruction_t::Duplicate: {
push_thing(top_thing());
} break;
case instruction_t::Swap: {
auto thing1 = pop_thing();
auto thing2 = pop_thing();
push_thing(std::move(thing1));
push_thing(std::move(thing2));
} break;
case instruction_t::Clone: {
push_thing(top_thing());
} break;
case instruction_t::Reference: {
push_thing(stack_thing::make_reference(pop_thing(), { m_stackStorage }));
} break;
case instruction_t::Add: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.add(rhs));
} break;
case instruction_t::Sub: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.sub(rhs));
} break;
case instruction_t::Mul: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.mul(rhs));
} break;
case instruction_t::Div: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.div(rhs));
} break;
case instruction_t::Mod: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.mod(rhs));
} break;
case instruction_t::Equals: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.equals(rhs));
} break;
case instruction_t::NotEquals: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.not_equals(rhs));
} break;
case instruction_t::LessThan: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.less_than(rhs));
} break;
case instruction_t::GreaterThan: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.greater_than(rhs));
} break;
case instruction_t::LessEqual: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.less_equals(rhs));
} break;
case instruction_t::GreaterEqual: {
auto rhs = pop_thing();
auto lhs = pop_thing();
push_thing(lhs.greater_equals(rhs));
} break;
case instruction_t::Pointerof: {
auto thing = pop_thing();
push_thing(
{ (struct thing_type){ thing_type::Ptr, m_context->tt_store().at(thing.type().id) }, { m_stackStorage } })
.get<void*>() = thing.raw();
} break;
case instruction_t::Sizeof: {
auto thing = pop_thing();
auto& size = push_thing({ (struct thing_type){ thing_type::U64 }, { m_stackStorage } });
switch (thing.type().type) {
case thing_type::S8:
case thing_type::S16:
case thing_type::S32:
case thing_type::S64:
case thing_type::U8:
case thing_type::U16:
case thing_type::U32:
case thing_type::U64: size.get<u64>() = static_cast<u64>(thing_type::primitive_size(thing.type().type)); break;
case thing_type::Ptr: size.get<u64>() = static_cast<u64>(sizeof(void*)); break;
case thing_type::Array:
/* TODO: Return actual memory size of the array
* By the memory size I mean the length times sizeof single element.
*/
size.get<u64>() = thing.length();
break;
default: throw std::runtime_error("unreachable");
}
} break;
case instruction_t::Lengthof: {
auto thing = pop_thing();
push_thing({ (struct thing_type){ thing_type::U64 }, { m_stackStorage } }).get<u64>() = thing.length();
} break;
case instruction_t::Load: {
push_thing(std::move(stack_thing::make_reference(load_thing(instr.arg.u16), { m_stackStorage })));
} break;
case instruction_t::Store: {
store_thing(instr.arg.u16, std::move(pop_thing()));
} break;
case instruction_t::LoadGlobal: {
push_thing(stack_thing::make_reference(frame.mod->load_global_variable(instr.arg.u16), { m_stackStorage }));
} break;
case instruction_t::StoreGlobal: {
frame.mod->store_global_variable(instr.arg.u16, pop_thing());
} break;
case instruction_t::Call: {
push_frame(frame.mod, *frame.mod->function_at(instr.arg.u16));
} break;
case instruction_t::Jump: {
frame.position += instr.arg.s8;
} break;
case instruction_t::JumpNotZero: {
if (pop_thing().integer() != 0) frame.position += instr.arg.s8;
} break;
case instruction_t::Return: {
pop_frame();
if (m_frames.empty()) m_flags = m_flags | executor_flags::Done;
} break;
default: throw std::runtime_error("unknown instruction");
}
}
} // namespace furvm
@@ -1,5 +1,6 @@
#include "furvm/function.hpp" #include "furvm/function.hpp"
#include "furlang/utility/hash.hpp"
#include "furvm/furvm.hpp" #include "furvm/furvm.hpp"
#include <utility> #include <utility>
@@ -100,8 +101,35 @@ function& function::operator=(const function& other) {
namespace detail { namespace detail {
struct mod_type_hash {
std::size_t operator()(const mod_type_h& type) const {
std::size_t hash = std::hash<decltype(type->type)>{}(type->type);
switch (type->type) {
case mod_type::S8:
case mod_type::S16:
case mod_type::S32:
case mod_type::S64:
case mod_type::U8:
case mod_type::U16:
case mod_type::U32:
case mod_type::U64: return hash;
case mod_type::Ptr:
case mod_type::Ref: return furlang::utility::hash_combine(hash, type->value.typeRef);
case mod_type::Array:
hash = furlang::utility::hash_combine(hash, type->value.array.size);
return furlang::utility::hash_combine(hash, type->value.array.typeId);
case mod_type::Import:
hash = furlang::utility::hash_combine(hash,
std::hash<decltype(type->value.imprt.modId)>{}(type->value.imprt.modId));
return furlang::utility::hash_combine(hash, type->value.imprt.typeId);
case mod_type::Count: break;
}
return hash;
}
};
std::size_t function_sig_hash::operator()(const function_sig& signature) const { std::size_t function_sig_hash::operator()(const function_sig& signature) const {
return furlang::utility::vector_hash<mod_type_h, detail::handle_hash<mod_type_h>>{}(signature.params); return furlang::utility::vector_hash<mod_type_h, mod_type_hash>{}(signature.params);
} }
} // namespace detail } // namespace detail
+188
View File
@@ -0,0 +1,188 @@
#include "furvm/instruction.hpp"
#include "furlang/view.hpp"
#include <stdexcept>
namespace furvm {
const std::size_t instruction_argument::s_sizes[instruction_argument::Count] = {
// None:
0,
// S8:
1,
// U8:
1,
// S16:
2,
// U16:
2,
// S32
1,
// U32
1,
// Constant:
2,
// Type:
4,
// Variable:
2,
// GlobalVariable:
2,
// Function:
4,
// Offset:
1,
};
const bool instruction_argument::s_signedness[instruction_argument::Count] = {
// None:
false,
// S8:
true,
// U8:
false,
// S16:
true,
// U16:
false,
// S32
true,
// U32:
false,
// Constant:
false,
// Type:
false,
// Variable:
false,
// GlobalVariable:
false,
// Function:
false,
// Offset:
true,
};
const instruction_argument_t instruction::s_arguments[instruction::Count] = {
// NoOperation:
instruction_argument::None,
// PushS8:
instruction_argument::S8,
// PushU8:
instruction_argument::U8,
// PushS16:
instruction_argument::S16,
// PushU16:
instruction_argument::U16,
// PushS32:
instruction_argument::S32,
// PushU32:
instruction_argument::U32,
// PushConstant:
instruction_argument::Constant,
// Array:
instruction_argument::Type,
// Slice:
instruction_argument::None,
// Get:
instruction_argument::None,
// Set:
instruction_argument::None,
// Drop:
instruction_argument::None,
// Duplicate:
instruction_argument::None,
// Swap:
instruction_argument::None,
// Clone:
instruction_argument::None,
// Reference:
instruction_argument::None,
// Add:
instruction_argument::None,
// Sub:
instruction_argument::None,
// Mul:
instruction_argument::None,
// Div:
instruction_argument::None,
// Mod:
instruction_argument::None,
// Equals:
instruction_argument::None,
// NotEquals:
instruction_argument::None,
// LessThan:
instruction_argument::None,
// GreaterThan:
instruction_argument::None,
// LessEqual:
instruction_argument::None,
// GreaterEqual:
instruction_argument::None,
// Pointerof:
instruction_argument::None,
// Sizeof:
instruction_argument::None,
// Lengthof:
instruction_argument::None,
// Load:
instruction_argument::Variable,
// Store:
instruction_argument::Variable,
// LoadGlobal:
instruction_argument::GlobalVariable,
// StoreGlobal:
instruction_argument::GlobalVariable,
// Call:
instruction_argument::Function,
// Jump:
instruction_argument::Offset,
// JumpNotZero:
instruction_argument::Offset,
// Return:
instruction_argument::None,
};
std::size_t instruction::read(furlang::view<std::uint8_t> in) {
type = static_cast<type_e>(in[0]);
if (type >= Count) throw std::runtime_error("invalid instruction");
arg.type = s_arguments[type];
furlang::view argView = in.subview(1, arg.size());
if (argView.size() != arg.size()) throw std::runtime_error("invalid bytecode");
switch (argView.size()) {
case 0: break;
case 1: arg.u8 = argView[0]; break;
case 2: arg.u16 = argView[0] | (argView[1] << 8); break;
case 4: arg.u32 = argView[0] | (argView[1] << 8) | (argView[2] << 16) | (argView[3] << 24); break;
default: throw std::runtime_error("unreachable");
}
return 1 + argView.size();
}
std::size_t instruction::write(std::vector<std::uint8_t>& out) const {
if (type >= Count) throw std::runtime_error("invalid instruction");
if (s_arguments[type] != arg.type) throw std::runtime_error("malformed instruction");
out.push_back(static_cast<char>(type));
switch (arg.size()) {
case 0: break;
case 1: out.push_back(arg.u8); break;
case 2:
out.push_back(arg.u16 & 0xFF);
out.push_back((arg.u16 >> 8) & 0xFF);
break;
case 4:
out.push_back(arg.u32 & 0xFF);
out.push_back((arg.u32 >> 8) & 0xFF);
out.push_back((arg.u32 >> 16) & 0xFF);
out.push_back((arg.u32 >> 24) & 0xFF);
break;
default: throw std::runtime_error("unreachable");
}
return 0;
}
} // namespace furvm
+24 -17
View File
@@ -10,21 +10,29 @@
#include <memory> #include <memory>
#include <sstream> #include <sstream>
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) { static void print_thing(const furvm::thing<>& thing) {
using namespace furvm; using namespace furvm;
switch (thing.true_type().type) { switch (thing.type().type) {
case thing_type::S8: std::cout << thing.cast_to<thing_type::s16>(); break; case thing_type::S8: std::cout << thing.cast_to<s16>(); break;
case thing_type::S16: std::cout << thing.get<thing_type::s16>(); break; case thing_type::S16: std::cout << thing.get<s16>(); break;
case thing_type::S32: std::cout << thing.get<thing_type::s32>(); break; case thing_type::S32: std::cout << thing.get<s32>(); break;
case thing_type::S64: std::cout << thing.get<thing_type::s64>(); break; case thing_type::S64: std::cout << thing.get<s64>(); break;
case thing_type::U8: std::cout << thing.get<thing_type::u8>(); break; case thing_type::U8: std::cout << thing.get<u8>(); break;
case thing_type::U16: std::cout << thing.get<thing_type::u16>(); break; case thing_type::U16: std::cout << thing.get<u16>(); break;
case thing_type::U32: std::cout << thing.get<thing_type::u32>(); break; case thing_type::U32: std::cout << thing.get<u32>(); break;
case thing_type::U64: std::cout << thing.get<thing_type::u64>(); break; case thing_type::U64: std::cout << thing.get<u64>(); break;
case thing_type::Array: case thing_type::Array:
std::cout << "{ "; std::cout << "{ ";
for (thing_type::u64 i = 0; i < thing.length(); ++i) { for (u64 i = 0; i < thing.length(); ++i) {
if (i > 0) std::cout << ", ";
print_thing(thing.at(i));
}
std::cout << " }";
break;
case thing_type::Slice:
std::cout << "Slice [" << thing.length() << "] { ";
for (u64 i = 0; i < thing.length(); ++i) {
if (i > 0) std::cout << ", "; if (i > 0) std::cout << ", ";
print_thing(thing.at(i)); print_thing(thing.at(i));
} }
@@ -75,16 +83,15 @@ int main(int argc, char** argv) {
mod->emplace_function(furvm::function_sig{ { u64Type }, u64Type }, "print").dispatch(); mod->emplace_function(furvm::function_sig{ { u64Type }, u64Type }, "print").dispatch();
#endif #endif
mod->set_native_function("println", [](furvm::executor& executor) { mod->set_native_function("println", [](furvm::executor& executor) {
auto arg = executor.load_thing(0); print_thing(executor.load_thing(0));
print_thing(*arg);
std::cout << '\n'; std::cout << '\n';
}); });
furvm::executor_h executor = context->emplace_executor(context); auto& executor = context->allocate_executor();
executor->push_frame(mod, *mainFunc); executor.push_frame(mod, *mainFunc);
while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) { while ((executor.flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) {
executor->step(); executor.step();
} }
return 0; return 0;
+49 -1
View File
@@ -16,6 +16,19 @@ std::ostream& mod::serialize(std::ostream& os) const {
os.write(MAGIC, sizeof(MAGIC)); os.write(MAGIC, sizeof(MAGIC));
detail::serialize(os, std::uint32_t(0)); // version detail::serialize(os, std::uint32_t(0)); // version
detail::serialize(os, static_cast<constant_index>(m_constants.size()));
for (const auto& constant : m_constants) {
detail::serialize(os, constant.type);
switch (constant.type) {
case constant::S32:
case constant::U32: detail::serialize(os, constant.u32); break;
case constant::S64:
case constant::U64: detail::serialize(os, constant.u64); break;
case constant::String: throw std::runtime_error("unimplemented");
default: throw std::runtime_error("unreachable");
}
}
mod_type_id typeCount = mod_type_id(m_types.cend() - m_types.cbegin()); mod_type_id typeCount = mod_type_id(m_types.cend() - m_types.cbegin());
detail::serialize(os, typeCount); detail::serialize(os, typeCount);
for (mod_type_id id = 0; id < typeCount; ++id) { for (mod_type_id id = 0; id < typeCount; ++id) {
@@ -41,6 +54,9 @@ std::ostream& mod::serialize(std::ostream& os) const {
detail::serialize(os, type.value.array.typeId); detail::serialize(os, type.value.array.typeId);
detail::serialize(os, type.value.array.size); detail::serialize(os, type.value.array.size);
} break; } break;
case mod_type::Slice: {
detail::serialize(os, type.value.slice.typeId);
} break;
case mod_type::Import: { case mod_type::Import: {
detail::serialize(os, type.value.imprt.modId); detail::serialize(os, type.value.imprt.modId);
detail::serialize(os, type.value.imprt.typeId); detail::serialize(os, type.value.imprt.typeId);
@@ -88,6 +104,8 @@ std::ostream& mod::serialize(std::ostream& os) const {
} }
} }
detail::serialize(os, static_cast<std::uint16_t>(m_globalVariables.size()));
detail::serialize(os, std::uint64_t(m_bytecode.size())); detail::serialize(os, std::uint64_t(m_bytecode.size()));
return os.write(reinterpret_cast<const char*>(m_bytecode.data()), static_cast<std::streamsize>(m_bytecode.size())); return os.write(reinterpret_cast<const char*>(m_bytecode.data()), static_cast<std::streamsize>(m_bytecode.size()));
} }
@@ -102,6 +120,22 @@ mod mod::load(std::istream& is) {
mod mod; mod mod;
constant_index constantCount = 0;
detail::load(is, constantCount);
mod.m_constants.reserve(constantCount);
for (; constantCount != 0; --constantCount) {
constant constant{};
detail::load(is, reinterpret_cast<std::uint32_t&>(constant.type));
switch (constant.type) {
case constant::S32:
case constant::U32: detail::load(is, constant.u32); break;
case constant::S64:
case constant::U64: detail::load(is, constant.u64); break;
case constant::String: throw std::runtime_error("unimplemented");
default: throw std::runtime_error("unreachable");
}
}
mod_type_id typeCount = 0; mod_type_id typeCount = 0;
detail::load(is, typeCount); detail::load(is, typeCount);
for (mod_type_id id = 0; id < typeCount; ++id) { for (mod_type_id id = 0; id < typeCount; ++id) {
@@ -124,7 +158,12 @@ mod mod::load(std::istream& is) {
case mod_type::Ptr: { case mod_type::Ptr: {
mod_type_id typeId = 0; mod_type_id typeId = 0;
detail::load(is, typeId); detail::load(is, typeId);
mod.emplace_type(id, typeId).dispatch(); mod.emplace_type(id, mod_type::Ptr, typeId).dispatch();
} break;
case mod_type::Ref: {
mod_type_id typeId = 0;
detail::load(is, typeId);
mod.emplace_type(id, mod_type::Ref, typeId).dispatch();
} break; } break;
case mod_type::Array: { case mod_type::Array: {
mod_type_id typeId = 0; mod_type_id typeId = 0;
@@ -133,6 +172,11 @@ mod mod::load(std::istream& is) {
detail::load(is, size); detail::load(is, size);
mod.emplace_type(id, typeId, size).dispatch(); mod.emplace_type(id, typeId, size).dispatch();
} break; } break;
case mod_type::Slice: {
mod_type_id typeId = 0;
detail::load(is, typeId);
mod.emplace_type(id, mod_type::Slice, typeId).dispatch();
} break;
case mod_type::Import: { case mod_type::Import: {
std::string modName; std::string modName;
detail::load(is, modName); detail::load(is, modName);
@@ -192,6 +236,10 @@ mod mod::load(std::istream& is) {
} }
} }
std::uint16_t globalVariables = 0;
detail::load(is, globalVariables);
mod.set_global_variable_count(globalVariables);
std::uint64_t bytecodeLength = 0; std::uint64_t bytecodeLength = 0;
detail::load(is, bytecodeLength); detail::load(is, bytecodeLength);
mod.bytecode().resize(bytecodeLength); mod.bytecode().resize(bytecodeLength);
+141
View File
@@ -0,0 +1,141 @@
// NOLINTBEGIN(readability-identifier-naming)
#include "gtest/gtest.h"
#include <furc/middle/ir.hpp>
#include <furc/middle/ssa.hpp>
#include <unordered_set>
#include <vector>
namespace furc::test {
class SsaCfg : public testing::Test {
public:
SsaCfg()
: p_context(&p_function) {}
protected:
void compute() { ssa::compute_cfg(p_function.blocks, p_cfgBlocks); }
protected:
std::vector<ssa::cfg_block> p_cfgBlocks;
ir_function p_function;
ir_context p_context;
};
TEST_F(SsaCfg, Diamond) {
ir_operand null = { ir_operand::Integer, static_cast<std::uint64_t>(0) };
// Entry
p_context.terminate(null, 1, 2);
p_context.new_next();
// A
p_context.terminate(3);
p_context.new_next();
// B
p_context.terminate(3);
p_context.new_next();
// C
p_context.terminate(4);
p_context.new_next();
// Exit
p_context.terminate();
compute();
EXPECT_EQ(p_cfgBlocks[0].sucs.size(), 2);
EXPECT_NE(p_cfgBlocks[0].sucs.find(1), p_cfgBlocks[0].sucs.end());
EXPECT_NE(p_cfgBlocks[0].sucs.find(2), p_cfgBlocks[0].sucs.end());
EXPECT_EQ(p_cfgBlocks[3].preds, p_cfgBlocks[0].sucs);
EXPECT_EQ(p_cfgBlocks[3].sucs.size(), 1);
EXPECT_NE(p_cfgBlocks[3].sucs.find(4), p_cfgBlocks[3].sucs.end());
}
class SsaDom : public testing::Test {
public:
SsaDom()
: p_context(&p_function) {}
protected:
void compute() {
ssa::compute_cfg(p_function.blocks, p_cfgBlocks);
p_ssaRegisters.resize(p_function.regCount);
ssa::collect_registers(p_function.blocks, p_ssaRegisters, p_ssaGlobals);
std::vector<std::uint64_t> order;
ssa::compute_rpo(p_cfgBlocks, p_ssaBlocks, order);
ssa::build_dtree(p_cfgBlocks, p_ssaBlocks, order);
ssa::compute_dfrontiers(p_cfgBlocks, p_ssaBlocks);
ssa::ssaification(p_function.blocks, p_cfgBlocks, p_ssaBlocks, p_ssaRegisters, p_ssaGlobals);
ssa::rename(p_function.blocks, p_function.regCount, p_cfgBlocks, p_ssaBlocks, order);
}
protected:
std::vector<ssa::cfg_block> p_cfgBlocks;
std::vector<ssa::ssa_block> p_ssaBlocks;
std::vector<ssa::register_info> p_ssaRegisters;
std::unordered_set<std::uint64_t> p_ssaGlobals;
ir_function p_function;
ir_context p_context;
};
TEST_F(SsaDom, Diamond) {
ir_operand null = { ir_operand::Integer, static_cast<std::uint64_t>(0) };
// Entry
p_context.terminate(null, 1, 2);
p_context.new_next();
// A
p_context.add_instr(ir_instruction{ ir_instruction::Move,
ir_operand{ ir_operand::Register, static_cast<std::uint64_t>(0) },
{ null } });
p_context.terminate(3);
p_context.new_next();
// B
p_context.add_instr(ir_instruction{ ir_instruction::Move,
ir_operand{ ir_operand::Register, static_cast<std::uint64_t>(0) },
{ null } });
p_context.terminate(3);
p_context.new_next();
// C
p_context.add_instr(ir_instruction{ ir_instruction::Add,
ir_operand{ ir_operand::Register, static_cast<std::uint64_t>(0) },
{ ir_operand{ ir_operand::Register, static_cast<std::uint64_t>(0) } } });
p_context.new_next();
// Exit
p_context.terminate();
p_function.regCount = 1;
compute();
EXPECT_TRUE(p_ssaBlocks[0].df.empty());
EXPECT_EQ(p_ssaBlocks[1].idom, 0);
EXPECT_EQ(p_ssaBlocks[1].df.size(), 1);
EXPECT_NE(p_ssaBlocks[1].df.find(3), p_ssaBlocks[1].df.end());
EXPECT_EQ(p_ssaBlocks[2].idom, 0);
EXPECT_EQ(p_ssaBlocks[2].df.size(), 1);
EXPECT_NE(p_ssaBlocks[2].df.find(3), p_ssaBlocks[2].df.end());
EXPECT_EQ(p_ssaBlocks[3].idom, 0);
EXPECT_TRUE(p_ssaBlocks[3].df.empty());
EXPECT_EQ(p_ssaBlocks[4].idom, 3);
EXPECT_TRUE(p_ssaBlocks[4].df.empty());
// Renaming
EXPECT_EQ(p_function.blocks[1].instructions.front(),
(ir_instruction{ ir_instruction::Move, ir_operand::reg(0, 3), { null } }));
EXPECT_EQ(p_function.blocks[2].instructions.front(),
(ir_instruction{ ir_instruction::Move, ir_operand::reg(0, 2), { null } }));
EXPECT_EQ(p_function.blocks[3].instructions.front(),
(ir_instruction{ ir_instruction::Phi,
ir_operand::reg(0, 0),
{ ir_operand{ ir_operand::PhiPair, ir_operand::value_u::register_s{ 0, 2 }, 2 },
ir_operand{ ir_operand::PhiPair, ir_operand::value_u::register_s{ 0, 3 }, 1 } } }));
}
} // namespace furc::test
// NOLINTEND(readability-identifier-naming)
+110
View File
@@ -0,0 +1,110 @@
#include "furvm/exceptions.hpp"
#include "furvm/furvm.hpp"
#include "furvm/thing.hpp"
#include "gtest/gtest.h" // IWYU pragma: keep
#include <array>
namespace {
// TODO: Basic program tests (e.g. for loops)
TEST(ThingOps, Add) {
furvm::thing lhs{ furvm::thing_type{ furvm::thing_type::U32 } };
lhs.get<furvm::u32>() = 6;
furvm::thing rhs{ furvm::thing_type{ furvm::thing_type::U32 } };
rhs.get<furvm::u32>() = 7;
auto res = lhs.add(rhs);
ASSERT_EQ(res.type().type, furvm::thing_type::U32);
EXPECT_EQ(lhs.get<furvm::u32>(), 6);
EXPECT_EQ(rhs.get<furvm::u32>(), 7);
EXPECT_EQ(res.get<furvm::u32>(), 6 + 7);
}
TEST(ThingOps, Array) {
furvm::thing_type innerType = { furvm::thing_type::U32 };
static constexpr std::size_t LENGTH = 10;
static constexpr std::array<furvm::u32, LENGTH> values = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 7 };
furvm::thing array{ furvm::thing_type{ furvm::thing_type::Array, { &innerType, LENGTH } } };
EXPECT_EQ(array.length(), LENGTH);
for (std::size_t i = 0; i < LENGTH; ++i) {
auto el = array.at(i);
el.get<furvm::u32>() = values[i];
EXPECT_EQ(array.at(i).integer(), values[i]);
}
}
TEST(ThingOps, Slice) {
furvm::thing_type innerType = { furvm::thing_type::U32 };
static constexpr std::size_t LENGTH = 10;
static constexpr std::array<furvm::u32, LENGTH> values = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 7 };
furvm::thing array{ furvm::thing_type{ furvm::thing_type::Array, { &innerType, LENGTH } } };
EXPECT_EQ(array.length(), LENGTH);
furvm::thing slice = array.slice(0, LENGTH);
EXPECT_EQ(slice.length(), LENGTH);
for (std::size_t i = 0; i < LENGTH; ++i) {
auto el = slice.at(i);
el.get<furvm::u32>() = values[i];
EXPECT_EQ(array.at(i).integer(), values[i]);
EXPECT_EQ(slice.at(i).integer(), array.at(i).integer());
}
}
TEST(ThingOps, String) {
static constexpr std::string_view STRING = "femboje na topie";
static constexpr auto LENGTH = STRING.length();
furvm::thing string{ furvm::thing_type{ furvm::thing_type::String } };
string.assign(STRING);
EXPECT_EQ(string.length(), LENGTH);
furvm::thing slice = string.slice(0, LENGTH);
EXPECT_EQ(slice.length(), LENGTH);
for (std::size_t i = 0; i < LENGTH; ++i) {
auto el = slice.at(i);
EXPECT_EQ(slice.at(i).get<furvm::u8>(), STRING[i]);
}
}
TEST(ThingOps, Iterators) {
furvm::thing_type innerType = { furvm::thing_type::U32 };
static constexpr std::size_t LENGTH = 10;
static constexpr std::array<furvm::u32, LENGTH> values = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 7 };
furvm::thing array{ furvm::thing_type{ furvm::thing_type::Array, { &innerType, LENGTH } } };
EXPECT_EQ(array.length(), LENGTH);
for (std::size_t i = 0; i < LENGTH; ++i) {
auto el = array.at(i);
el.get<furvm::u32>() = values[i];
EXPECT_EQ(array.at(i).integer(), values[i]);
}
std::size_t itCount = 0;
for (auto it = array.begin(); it != array.end(); ++it, ++itCount) {
auto idx = it - array.begin();
ASSERT_EQ(idx, itCount);
ASSERT_LT(idx, LENGTH);
EXPECT_EQ(it->integer(), values[idx]);
}
}
TEST(ThingOps, Access) {
furvm::thing thing{ furvm::thing_type{ furvm::thing_type::U8 } };
EXPECT_NO_THROW(thing.get<furvm::u8>());
EXPECT_THROW(thing.get<furvm::s8>(), furvm::bad_thing_access);
EXPECT_THROW(thing.get<furvm::s32>(), furvm::bad_thing_access);
EXPECT_THROW(thing.get<void*>(), furvm::bad_thing_access);
EXPECT_NO_THROW(thing.assign<furvm::u8>(67));
EXPECT_THROW(thing.assign<furvm::s32>(1337), furvm::bad_thing_access);
EXPECT_EQ(thing.get<furvm::u8>(), 67);
EXPECT_NO_THROW(thing.set<furvm::u8>(67)); // He talkin' 'bout sum 6-7 while I want sixty ni-
EXPECT_THROW(thing.set<furvm::s32>(1337), furvm::bad_thing_access);
}
} // namespace