#ifndef CORE_VM_GUEST_H
#define CORE_VM_GUEST_H
#include <stdint.h>
#include <stdio.h>
#include "polkavm_guest.h"
struct PolkaVM_Metadata_V2 {
uint8_t version;
uint32_t flags;
uint32_t symbol_length;
const char * symbol;
uint8_t input_regs;
uint8_t output_regs;
uint8_t has_index;
uint32_t index;
} __attribute__ ((packed));
#define POLKAVM_IMPORT_V2(arg_index, arg_return_ty, fn_name, ...) \
static struct PolkaVM_Metadata_V2 POLKAVM_JOIN(fn_name, __IMPORT_METADATA) __attribute__ ((section(".polkavm_metadata"))) = { \
.version = 2, \
.flags = 0, \
.symbol_length = sizeof(#fn_name) - 1, \
.symbol = #fn_name, \
.input_regs = POLKAVM_COUNT_REGS(__VA_ARGS__), \
.output_regs = POLKAVM_COUNT_REGS(arg_return_ty), \
.has_index = 1, \
.index = arg_index \
}; \
static arg_return_ty __attribute__ ((naked)) fn_name(POLKAVM_IMPORT_ARGS_IMPL(__VA_ARGS__)) { \
__asm__( \
POLKAVM_IMPORT_DEF() \
"ret\n" \
: \
: \
[metadata] "i" (&POLKAVM_JOIN(fn_name, __IMPORT_METADATA)) \
: "memory" \
); \
}
POLKAVM_IMPORT_V2(0, uint64_t, corevm_gas);
POLKAVM_IMPORT_V2(1, uint64_t, corevm_alloc, uint64_t);
POLKAVM_IMPORT_V2(2, void, corevm_free, uint64_t, uint64_t);
POLKAVM_IMPORT_V2(3, void, corevm_yield_console_data, uint64_t, uint64_t, uint64_t);
POLKAVM_IMPORT_V2(4, void, corevm_yield_video_frame, uint64_t, uint64_t, uint64_t);
POLKAVM_IMPORT_V2(5, void, corevm_video_mode, uint64_t, uint64_t, uint64_t, uint64_t);
enum CorevmVideoFrameFormat {
COREVM_VIDEO_RGB88_INDEXED8 = 1
}
#ifndef COREVM_PRINTF_BUFFER_LEN
#define COREVM_PRINTF_BUFFER_LEN 4096
#endif
#define corevm_printf_impl(stream, format, ...) \
{ \
char buffer[COREVM_PRINTF_BUFFER_LEN]; \
int n = snprintf(buffer, COREVM_PRINTF_BUFFER_LEN, format, ##__VA_ARGS__); \
if (n > 0) { \
if (n == COREVM_PRINTF_BUFFER_LEN) { \
n = COREVM_PRINTF_BUFFER_LEN - 1; \
} \
buffer[n] = 0; \
corevm_yield_console_data(stream, (uint64_t)buffer, (uint64_t)(n + 1)); \
} \
}
#define corevm_printf(format, ...) corevm_printf_impl(1, format, ##__VA_ARGS__)
#define corevm_eprintf(format, ...) corevm_printf_impl(2, format, ##__VA_ARGS__)
#endif