Skip to content

Commit

Permalink
Update dynamic argument calls to avoid macro
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Jul 18, 2024
1 parent d73b371 commit 10ca535
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 51 deletions.
3 changes: 0 additions & 3 deletions engine/scripts/src/gameplay.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "events.hpp"
#include <api.h>
using namespace api;
extern void benchmark_multiprocessing();

int main()
{
Expand Down Expand Up @@ -84,8 +83,6 @@ PUBLIC(void benchmarks())
measure("Dynamic call handler x4 (call)", opaque_dyncall_handler);

measure("Allocate 1024-bytes, and free it", bench_alloc_free);

//benchmark_multiprocessing();
}

struct C
Expand Down
12 changes: 3 additions & 9 deletions engine/scripts/src/level1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ extern void do_threads_stuff();
extern void do_remote_stuff();
static void do_benchmarks();

static inline void
my_dynamic_call(long val, float fval, const std::string& str)
{
DYNCALL("Test::my_dynamic_call", val, fval, str);
}

/* This is the function that gets called at the start.
See: engine/src/main.cpp */
PUBLIC(void start())
Expand All @@ -21,7 +15,7 @@ PUBLIC(void start())

print("** Fully dynamic system calls **\n");

my_dynamic_call(1234, 5678.0, "nine-ten-eleven-twelve!");
DynamicCall("Test::my_dynamic_call")(1234, 5678.0, "nine-ten-eleven-twelve!");

if (Game::setting("benchmarks").value_or(false))
do_benchmarks();
Expand Down Expand Up @@ -63,14 +57,14 @@ void do_benchmarks()
"Dynamic arguments (no arguments)",
[]
{
DYNCALL("Test::void");
DynamicCall("Test::void")();
});

measure(
"Dynamic arguments (4x arguments)",
[]
{
DYNCALL("Test::void", -4096, 5678.0, 524287, 8765.0);
DynamicCall("Test::void")(-4096, 5678.0, 524287, 8765.0);
});

measure(
Expand Down
6 changes: 0 additions & 6 deletions engine/src/script/script_syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ APICALL(api_write)
const uint32_t len_g = std::min(4096u, (uint32_t)len);

auto& scr = script(machine);
if (scr.machine().is_multiprocessing())
{
machine.set_result(-1);
return;
}

if (scr.stdout_enabled())
{
std::array<riscv::vBuffer, 16> buffers;
Expand Down
2 changes: 1 addition & 1 deletion ext/libriscv
82 changes: 52 additions & 30 deletions programs/micro/api/api_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,36 +264,33 @@ dynamic_call(const uint32_t hash, const char* name, Args&&... args)
std::array<uintptr_t, 8> gpr {};
std::array<float, 8> fpr {};

(
[&]
([&] {
if constexpr (std::is_integral_v<std::remove_reference_t<Args>>)
{
if constexpr (std::is_integral_v<std::remove_reference_t<Args>>)
{
gpr[argc] = args;
type[argc] = 0b001;
argc++;
}
else if constexpr (std::is_floating_point_v<
std::remove_reference_t<Args>>)
{
fpr[argc] = args;
type[argc] = 0b010;
argc++;
}
else if constexpr (is_stdstring<std::remove_cvref_t<Args>>::value)
{
gpr[argc] = (uintptr_t)args.data();
type[argc] = 0b111;
argc++;
}
else if constexpr (is_string<Args>::value)
{
gpr[argc] = (uintptr_t) const_cast<const char*>(args);
type[argc] = 0b111;
argc++;
}
}(),
...);
gpr[argc] = args;
type[argc] = 0b001;
argc++;
}
else if constexpr (std::is_floating_point_v<
std::remove_reference_t<Args>>)
{
fpr[argc] = args;
type[argc] = 0b010;
argc++;
}
else if constexpr (is_stdstring<std::remove_cvref_t<Args>>::value)
{
gpr[argc] = (uintptr_t)args.data();
type[argc] = 0b111;
argc++;
}
else if constexpr (is_string<Args>::value)
{
gpr[argc] = (uintptr_t) const_cast<const char*>(args);
type[argc] = 0b111;
argc++;
}
}(), ...);

register long a0 asm("a0");
register float fa0 asm("fa0");
Expand Down Expand Up @@ -334,4 +331,29 @@ dynamic_call(const uint32_t hash, const char* name, Args&&... args)
: "r"(name_hash), "m"(*name_ptr), "r"(name_ptr));
}

#define DYNCALL(name, ...) dynamic_call(crc32(name), name, ##__VA_ARGS__)
struct DynamicCall
{
const uint32_t hash;
const char* const name;

template <typename... Args>
constexpr void operator()(Args&&... args)
{
dynamic_call(hash, name, std::forward<Args>(args)...);
}

constexpr operator uint32_t() const
{
return hash;
}
constexpr operator const char*() const
{
return name;
}

template <size_t N>
constexpr DynamicCall(const char (&name)[N])
: hash(crc32ct(name)), name(name)
{
}
};
2 changes: 0 additions & 2 deletions programs/micro/libc/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@ extern "C" long sys_write(const void*, size_t);
extern "C" void (*farcall_helper) ();
extern "C" void (*direct_farcall_helper) ();
extern "C" void sys_interrupt (uint32_t, uint32_t, const void*, size_t);
extern "C" unsigned sys_multiprocess(unsigned);
extern "C" uint32_t sys_multiprocess_wait();

0 comments on commit 10ca535

Please sign in to comment.