Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import/export rework #97

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"crates/polkavm-derive",
"crates/polkavm-derive-impl",
"crates/polkavm-derive-impl-macro",
"crates/polkavm-linker",
"crates/polkavm-assembler",
"crates/polkavm-common",
Expand All @@ -20,21 +21,22 @@ members = [
]

[workspace.package]
version = "0.7.0"
version = "0.8.0"
authors = ["Jan Bujak <[email protected]>", "Parity Technologies <[email protected]>"]
license = "MIT/Apache-2.0"
edition = "2021"
rust-version = "1.70.0"
repository = "https://github.com/koute/polkavm"

[workspace.dependencies]
polkavm = { version = "0.7.0", path = "crates/polkavm" }
polkavm-assembler = { version = "0.7.0", path = "crates/polkavm-assembler" }
polkavm-common = { version = "0.7.0", path = "crates/polkavm-common" }
polkavm-derive = { version = "0.7.0", path = "crates/polkavm-derive" }
polkavm-derive-impl = { version = "0.7.0", path = "crates/polkavm-derive-impl" }
polkavm-linker = { version = "0.7.0", path = "crates/polkavm-linker" }
polkavm-linux-raw = { version = "0.7.0", path = "crates/polkavm-linux-raw" }
polkavm = { version = "0.8.0", path = "crates/polkavm" }
polkavm-assembler = { version = "0.8.0", path = "crates/polkavm-assembler" }
polkavm-common = { version = "0.8.0", path = "crates/polkavm-common" }
polkavm-derive = { version = "0.8.0", path = "crates/polkavm-derive" }
polkavm-derive-impl = { version = "0.8.0", path = "crates/polkavm-derive-impl" }
polkavm-derive-impl-macro = { version = "0.8.0", path = "crates/polkavm-derive-impl-macro" }
polkavm-linker = { version = "0.8.0", path = "crates/polkavm-linker" }
polkavm-linux-raw = { version = "0.8.0", path = "crates/polkavm-linux-raw" }

clap = "4.4.6"
env_logger = { version = "0.10.0", default-features = false }
Expand Down
141 changes: 57 additions & 84 deletions capi/polkavm_guest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@
#define POLKAVM_JOIN(X,Y) POLKAVM_JOIN_IMPL(X, Y)
#define POLKAVM_UNIQUE(X) POLKAVM_JOIN(X, __COUNTER__)

#define POLKAVM_EXTERN_TY_void 0
#define POLKAVM_EXTERN_TY_i32 1
#define POLKAVM_EXTERN_TY_i64 2

#define POLKAVM_EXTERN_TY_int8_t POLKAVM_EXTERN_TY_i32
#define POLKAVM_EXTERN_TY_uint8_t POLKAVM_EXTERN_TY_i32
#define POLKAVM_EXTERN_TY_int16_t POLKAVM_EXTERN_TY_i32
#define POLKAVM_EXTERN_TY_uint16_t POLKAVM_EXTERN_TY_i32
#define POLKAVM_EXTERN_TY_int32_t POLKAVM_EXTERN_TY_i32
#define POLKAVM_EXTERN_TY_uint32_t POLKAVM_EXTERN_TY_i32
#define POLKAVM_EXTERN_TY_int64_t POLKAVM_EXTERN_TY_i64
#define POLKAVM_EXTERN_TY_uint64_t POLKAVM_EXTERN_TY_i64
#define POLKAVM_EXTERN_TY_int POLKAVM_EXTERN_TY_i32
#define POLKAVM_REGS_FOR_TY_void 0
#define POLKAVM_REGS_FOR_TY_i32 1
#define POLKAVM_REGS_FOR_TY_i64 2

#define POLKAVM_REGS_FOR_TY_int8_t POLKAVM_REGS_FOR_TY_i32
#define POLKAVM_REGS_FOR_TY_uint8_t POLKAVM_REGS_FOR_TY_i32
#define POLKAVM_REGS_FOR_TY_int16_t POLKAVM_REGS_FOR_TY_i32
#define POLKAVM_REGS_FOR_TY_uint16_t POLKAVM_REGS_FOR_TY_i32
#define POLKAVM_REGS_FOR_TY_int32_t POLKAVM_REGS_FOR_TY_i32
#define POLKAVM_REGS_FOR_TY_uint32_t POLKAVM_REGS_FOR_TY_i32
#define POLKAVM_REGS_FOR_TY_int64_t POLKAVM_REGS_FOR_TY_i64
#define POLKAVM_REGS_FOR_TY_uint64_t POLKAVM_REGS_FOR_TY_i64
#define POLKAVM_REGS_FOR_TY_int POLKAVM_REGS_FOR_TY_i32

#ifdef _LP64
#define POLKAVM_EXTERN_TY_size_t POLKAVM_EXTERN_TY_i64
#define POLKAVM_EXTERN_TY_long POLKAVM_EXTERN_TY_i64
#define POLKAVM_REGS_FOR_TY_size_t POLKAVM_REGS_FOR_TY_i64
#define POLKAVM_REGS_FOR_TY_long POLKAVM_REGS_FOR_TY_i64
#else
#define POLKAVM_EXTERN_TY_size_t POLKAVM_EXTERN_TY_i32
#define POLKAVM_EXTERN_TY_long POLKAVM_EXTERN_TY_i32
#define POLKAVM_REGS_FOR_TY_size_t POLKAVM_REGS_FOR_TY_i32
#define POLKAVM_REGS_FOR_TY_long POLKAVM_REGS_FOR_TY_i32
#endif

#define POLKAVM_COUNT_ARGS(...) POLKAVM_COUNT_ARGS_IMPL(0, ## __VA_ARGS__, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define POLKAVM_COUNT_ARGS_IMPL(_0, _1_, _2_, _3_, _4_, _5_, _6_, _7_, _8_, _9_, _10_, _11_, _12_, _13_, _14_, _15_, _16_, _17_, _18_, _19_, _20_, _21_, _22_, _23_, _24_, _25_, _26_, _27_, _28_, _29_, _30_, _31_, _32_, count, ...) count

#define POLKAVM_EXTERN_TY(X) POLKAVM_JOIN(POLKAVM_EXTERN_TY_, X)
#define POLKAVM_REGS_FOR_TY(X) POLKAVM_JOIN(POLKAVM_REGS_FOR_TY_, X)

#define POLKAVM_ARG_ASM_4_0()
#define POLKAVM_ARG_ASM_4_1(A) \
Expand Down Expand Up @@ -65,38 +65,24 @@
#define POLKAVM_ARG_ASM_2(N, ...) POLKAVM_ARG_ASM_3(N, ## __VA_ARGS__)
#define POLKAVM_ARG_ASM(...) POLKAVM_ARG_ASM_2(POLKAVM_COUNT_ARGS(__VA_ARGS__), ## __VA_ARGS__)

#define POLKAVM_ARG_TY_4_0()
#define POLKAVM_ARG_TY_4_1(A) \
[arg_1] "i" (POLKAVM_EXTERN_TY(A)),
#define POLKAVM_ARG_TY_4_2(A, B) \
[arg_1] "i" (POLKAVM_EXTERN_TY(A)), \
[arg_2] "i" (POLKAVM_EXTERN_TY(B)),
#define POLKAVM_ARG_TY_4_3(A, B, C) \
[arg_1] "i" (POLKAVM_EXTERN_TY(A)), \
[arg_2] "i" (POLKAVM_EXTERN_TY(B)), \
[arg_3] "i" (POLKAVM_EXTERN_TY(C)),
#define POLKAVM_ARG_TY_4_4(A, B, C, D) \
[arg_1] "i" (POLKAVM_EXTERN_TY(A)), \
[arg_2] "i" (POLKAVM_EXTERN_TY(B)), \
[arg_3] "i" (POLKAVM_EXTERN_TY(C)), \
[arg_4] "i" (POLKAVM_EXTERN_TY(D)),
#define POLKAVM_ARG_TY_4_5(A, B, C, D, E) \
[arg_1] "i" (POLKAVM_EXTERN_TY(A)), \
[arg_2] "i" (POLKAVM_EXTERN_TY(B)), \
[arg_3] "i" (POLKAVM_EXTERN_TY(C)), \
[arg_4] "i" (POLKAVM_EXTERN_TY(D)), \
[arg_5] "i" (POLKAVM_EXTERN_TY(E)),
#define POLKAVM_ARG_TY_4_6(A, B, C, D, E, F) \
[arg_1] "i" (POLKAVM_EXTERN_TY(A)), \
[arg_2] "i" (POLKAVM_EXTERN_TY(B)), \
[arg_3] "i" (POLKAVM_EXTERN_TY(C)), \
[arg_4] "i" (POLKAVM_EXTERN_TY(D)), \
[arg_5] "i" (POLKAVM_EXTERN_TY(E)), \
[arg_6] "i" (POLKAVM_EXTERN_TY(F)),

#define POLKAVM_ARG_TY_3(N, ...) POLKAVM_ARG_TY_4_ ## N(__VA_ARGS__)
#define POLKAVM_ARG_TY_2(N, ...) POLKAVM_ARG_TY_3(N, ## __VA_ARGS__)
#define POLKAVM_ARG_TY(...) POLKAVM_ARG_TY_2(POLKAVM_COUNT_ARGS(__VA_ARGS__), ## __VA_ARGS__)
#define POLKAVM_COUNT_REGS_4_0() \
0
#define POLKAVM_COUNT_REGS_4_1(A) \
POLKAVM_REGS_FOR_TY(A)
#define POLKAVM_COUNT_REGS_4_2(A, B) \
POLKAVM_REGS_FOR_TY(A) + POLKAVM_REGS_FOR_TY(B)
#define POLKAVM_COUNT_REGS_4_3(A, B, C) \
POLKAVM_REGS_FOR_TY(A) + POLKAVM_REGS_FOR_TY(B) + POLKAVM_REGS_FOR_TY(C)
#define POLKAVM_COUNT_REGS_4_4(A, B, C, D) \
POLKAVM_REGS_FOR_TY(A) + POLKAVM_REGS_FOR_TY(B) + POLKAVM_REGS_FOR_TY(C) + POLKAVM_REGS_FOR_TY(D)
#define POLKAVM_COUNT_REGS_4_5(A, B, C, D, E) \
POLKAVM_REGS_FOR_TY(A) + POLKAVM_REGS_FOR_TY(B) + POLKAVM_REGS_FOR_TY(C) + POLKAVM_REGS_FOR_TY(D) + POLKAVM_REGS_FOR_TY(E)
#define POLKAVM_COUNT_REGS_4_6(A, B, C, D, E, F) \
POLKAVM_REGS_FOR_TY(A) + POLKAVM_REGS_FOR_TY(B) + POLKAVM_REGS_FOR_TY(C) + POLKAVM_REGS_FOR_TY(D) + POLKAVM_REGS_FOR_TY(E) + POLKAVM_REGS_FOR_TY(F)

#define POLKAVM_COUNT_REGS_3(N, ...) POLKAVM_COUNT_REGS_4_ ## N(__VA_ARGS__)
#define POLKAVM_COUNT_REGS_2(N, ...) POLKAVM_COUNT_REGS_3(N, ## __VA_ARGS__)
#define POLKAVM_COUNT_REGS(...) POLKAVM_COUNT_REGS_2(POLKAVM_COUNT_ARGS(__VA_ARGS__), ## __VA_ARGS__)

#define POLKAVM_IMPORT_ARGS_IMPL_4_0()
#define POLKAVM_IMPORT_ARGS_IMPL_4_1(A0) A0 a0
Expand All @@ -110,59 +96,46 @@
#define POLKAVM_IMPORT_ARGS_IMPL_2(N, ...) POLKAVM_IMPORT_ARGS_IMPL_3(N, ## __VA_ARGS__)
#define POLKAVM_IMPORT_ARGS_IMPL(...) POLKAVM_IMPORT_ARGS_IMPL_2(POLKAVM_COUNT_ARGS(__VA_ARGS__), ## __VA_ARGS__)

#define POLKAVM_IMPORT_EXPORT_ASM_IMPL(fn_name, ...) \
".word %[name_length]\n" \
".ascii \"" #fn_name "\"\n" \
".byte %[return_ty]\n" \
".byte %[arg_count]\n" \
POLKAVM_ARG_ASM(__VA_ARGS__)

#define POLKAVM_IMPORT_EXPORT_INPUTS_IMPL(arg_return_ty, fn_name, ...) \
[name_length] "i" ((sizeof(#fn_name) - 1)), \
[return_ty] "i" (POLKAVM_EXTERN_TY(arg_return_ty)), \
[arg_count] "i" (POLKAVM_COUNT_ARGS(__VA_ARGS__)), \
POLKAVM_ARG_TY(__VA_ARGS__)
struct PolkaVM_Metadata {
unsigned char version;
unsigned int flags;
unsigned int symbol_length;
const char * symbol;
unsigned char input_regs;
unsigned char output_regs;
} __attribute__ ((packed));

#define POLKAVM_EXPORT(arg_return_ty, fn_name, ...) \
static struct PolkaVM_Metadata POLKAVM_JOIN(fn_name, __EXPORT_METADATA) __attribute__ ((section(".polkavm_metadata"))) = { \
1, 0, sizeof(#fn_name) - 1, #fn_name, POLKAVM_COUNT_REGS(__VA_ARGS__), POLKAVM_COUNT_REGS(arg_return_ty) \
}; \
static void __attribute__ ((naked, used)) POLKAVM_UNIQUE(polkavm_export_dummy)() { \
__asm__( \
".pushsection .polkavm_exports,\"\",@progbits\n" \
".pushsection .polkavm_exports,\"R\",@note\n" \
".byte 1\n" \
".word %[address]\n" \
POLKAVM_IMPORT_EXPORT_ASM_IMPL(fn_name, ## __VA_ARGS__) \
".word %[metadata]\n" \
".word %[function]\n" \
".popsection\n" \
: \
: \
POLKAVM_IMPORT_EXPORT_INPUTS_IMPL(arg_return_ty, fn_name, ## __VA_ARGS__) \
[address] "i" (fn_name) \
[metadata] "i" (&POLKAVM_JOIN(fn_name, __EXPORT_METADATA)), \
[function] "i" (fn_name) \
: "memory" \
); \
}

#define POLKAVM_IMPORT(arg_return_ty, fn_name, ...) \
static void __attribute__ ((naked, used)) POLKAVM_UNIQUE(polkavm_import_dummy)() { \
__asm__( \
".pushsection .polkavm_imports." #fn_name ",\"a\",@progbits\n" \
".hidden __polkavm_import_" #fn_name "\n" \
"__polkavm_import_" #fn_name ":\n" \
".byte 1\n" \
".byte 0\n" \
POLKAVM_IMPORT_EXPORT_ASM_IMPL(fn_name, ## __VA_ARGS__) \
".popsection\n" \
: \
: \
POLKAVM_IMPORT_EXPORT_INPUTS_IMPL(arg_return_ty, fn_name, ## __VA_ARGS__) \
[dummy] "i" (0) \
: \
); \
} \
static struct PolkaVM_Metadata POLKAVM_JOIN(fn_name, __IMPORT_METADATA) __attribute__ ((section(".polkavm_metadata"))) = { \
1, 0, sizeof(#fn_name) - 1, #fn_name, POLKAVM_COUNT_REGS(__VA_ARGS__), POLKAVM_COUNT_REGS(arg_return_ty) \
}; \
static arg_return_ty __attribute__ ((naked)) fn_name(POLKAVM_IMPORT_ARGS_IMPL(__VA_ARGS__)) { \
__asm__( \
".word 0x0000000b\n" \
".word __polkavm_import_" #fn_name "\n" \
".word %[metadata]\n" \
"ret\n" \
: \
: \
[metadata] "i" (&POLKAVM_JOIN(fn_name, __IMPORT_METADATA)) \
: "memory" \
); \
}
Expand Down
3 changes: 0 additions & 3 deletions crates/polkavm-common/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ pub const VM_MAXIMUM_IMPORT_COUNT: u32 = 1024;
/// The maximum number of functions the program can export.
pub const VM_MAXIMUM_EXPORT_COUNT: u32 = 1024;

/// The maximum number of arguments that can be used in imported functions.
pub const VM_MAXIMUM_EXTERN_ARG_COUNT: usize = 6;

/// The minimum required alignment of runtime code pointers.
// TODO: Support the C extension in the linker and lower this to 2.
pub const VM_CODE_ADDRESS_ALIGNMENT: u32 = 4;
Expand Down
Loading