Skip to content
Browse files

Add a new API function to serialize a packfile PMC into a format suit…

…able for writing a .pbc file. This code stolen in part from compilers/imcc/main.c:imcc_write_pbc
  • Loading branch information...
1 parent 50acdfb commit 31792fae89dcff165d9572b0475f4147c3831c2c @Whiteknight Whiteknight committed
Showing with 44 additions and 0 deletions.
  1. +11 −0 include/parrot/api.h
  2. +33 −0 src/embed/api.c
View
11 include/parrot/api.h
@@ -190,6 +190,14 @@ Parrot_Int Parrot_api_run_bytecode(
Parrot_PMC mainargs);
PARROT_API
+Parrot_Int Parrot_api_serialize_bytecode_pmc(
+ Parrot_PMC interp_pmc,
+ Parrot_PMC pbc,
+ ARGOUT(Parrot_String * bc))
+ __attribute__nonnull__(3)
+ FUNC_MODIFIES(* bc);
+
+PARROT_API
Parrot_Int Parrot_api_set_compiler(
Parrot_PMC interp_pmc,
Parrot_String type,
@@ -277,6 +285,9 @@ Parrot_Int Parrot_api_wrap_imcc_hack(
#define ASSERT_ARGS_Parrot_api_ready_bytecode __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(main_sub))
#define ASSERT_ARGS_Parrot_api_run_bytecode __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
+#define ASSERT_ARGS_Parrot_api_serialize_bytecode_pmc \
+ __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(bc))
#define ASSERT_ARGS_Parrot_api_set_compiler __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_Parrot_api_set_configuration_hash \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
View
33 src/embed/api.c
@@ -495,6 +495,39 @@ Parrot_api_disassemble_bytecode(Parrot_PMC interp_pmc, Parrot_PMC pbc,
/*
+=item C<Parrot_Int Parrot_api_serialize_bytecode_pmc(Parrot_PMC interp_pmc,
+Parrot_PMC pbc, Parrot_String * bc)>
+
+Serialize a packfile PMC into a sequence of bytes suitable for writing out
+to a .pbc file
+
+=cut
+
+*/
+
+PARROT_API
+Parrot_Int
+Parrot_api_serialize_bytecode_pmc(Parrot_PMC interp_pmc, Parrot_PMC pbc,
+ ARGOUT(Parrot_String * bc))
+{
+ ASSERT_ARGS(Parrot_api_serialize_bytecode_pmc)
+ EMBED_API_CALLIN(interp_pmc, interp)
+ PackFile * const pf = (PackFile *)VTABLE_get_pointer(interp, pbc);
+ if (!pf)
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNEXPECTED_NULL,
+ "Could not get packfile.");
+ else {
+ const Parrot_Int size = PackFile_pack_size(interp, pf) * sizeof (opcode_t);
+ opcode_t * const packed = (opcode_t*) mem_sys_allocate(size);
+ PackFile_pack(interp, pf, packed);
+ *bc = Parrot_str_new_init(interp, (const char *)packed, size,
+ Parrot_binary_encoding_ptr, 0);
+ }
+ EMBED_API_CALLOUT(interp_pmc, interp)
+}
+
+/*
+
=item C<Parrot_Int Parrot_api_set_warnings(Parrot_PMC interp_pmc, Parrot_Int
flags)>

0 comments on commit 31792fa

Please sign in to comment.
Something went wrong with that request. Please try again.