Permalink
Browse files

upconvert pbc_to_exe to use the new API. it builds, but segfaults bui…

…lding parrot_nci_thunk_gen
  • Loading branch information...
1 parent 5c2c3d4 commit 46b7a9d88ff6724cf168f1bac4a61d1304617ad5 @Whiteknight Whiteknight committed Nov 24, 2010
Showing with 103 additions and 46 deletions.
  1. +22 −0 include/parrot/api.h
  2. +7 −6 include/parrot/library.h
  3. +18 −0 src/embed/api.c
  4. +5 −4 src/main.c
  5. +51 −36 tools/dev/pbc_to_exe.pir
View
@@ -9,11 +9,16 @@
#ifndef PARROT_API_H_GUARD
#define PARROT_API_H_GUARD
+#include <stdlib.h>
#include "parrot/compiler.h"
#include "parrot/config.h"
#include "parrot/core_types.h"
+
#define PARROT_API PARROT_EXPORT
+#define GET_INIT_STRUCT(i) do {\
+ (i) = (Parrot_Init_Args*)calloc(sizeof(Parrot_Init_Args), 0); \
+ } while(0)
/* having a modified version of PARROT_ASSERT which resolves as an integer
* rvalue lets us put ASSERT_ARGS() at the top of the list of local variables.
@@ -125,6 +130,18 @@ Parrot_Int Parrot_api_get_runtime_path(
__attribute__nonnull__(2)
FUNC_MODIFIES(*runtime);
+PARROT_API
+Parrot_Int Parrot_api_load_bytecode_bytes(
+ ARGMOD(PMC *interp_pmc),
+ ARGIN(const unsigned char * const pbc),
+ Parrot_Int bytecode_size,
+ ARGOUT(PMC **pbcpmc))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2)
+ __attribute__nonnull__(4)
+ FUNC_MODIFIES(*interp_pmc)
+ FUNC_MODIFIES(*pbcpmc);
+
PARROT_API
Parrot_Int Parrot_api_load_bytecode_file(
ARGMOD(PMC *interp_pmc),
@@ -225,6 +242,11 @@ Parrot_Int Parrot_api_set_warnings(
, PARROT_ASSERT_ARG(errmsg))
#define ASSERT_ARGS_Parrot_api_get_runtime_path __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(runtime))
+#define ASSERT_ARGS_Parrot_api_load_bytecode_bytes \
+ __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp_pmc) \
+ , PARROT_ASSERT_ARG(pbc) \
+ , PARROT_ASSERT_ARG(pbcpmc))
#define ASSERT_ARGS_Parrot_api_load_bytecode_file __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp_pmc) \
, PARROT_ASSERT_ARG(filename) \
View
@@ -57,6 +57,10 @@ void Parrot_lib_add_path_from_cstring(PARROT_INTERP,
__attribute__nonnull__(1)
__attribute__nonnull__(2);
+PARROT_EXPORT
+void Parrot_lib_update_paths_from_config_hash(PARROT_INTERP)
+ __attribute__nonnull__(1);
+
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
@@ -80,9 +84,6 @@ STRING* Parrot_locate_runtime_file_str(PARROT_INTERP,
void parrot_init_library_paths(PARROT_INTERP)
__attribute__nonnull__(1);
-void Parrot_lib_update_paths_from_config_hash(PARROT_INTERP)
- __attribute__nonnull__(1);
-
PARROT_IGNORABLE_RESULT
PARROT_CANNOT_RETURN_NULL
STRING * parrot_split_path_ext(PARROT_INTERP,
@@ -106,6 +107,9 @@ STRING * parrot_split_path_ext(PARROT_INTERP,
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(path))
+#define ASSERT_ARGS_Parrot_lib_update_paths_from_config_hash \
+ __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_locate_runtime_file __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(file_name))
@@ -115,9 +119,6 @@ STRING * parrot_split_path_ext(PARROT_INTERP,
, PARROT_ASSERT_ARG(file))
#define ASSERT_ARGS_parrot_init_library_paths __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
-#define ASSERT_ARGS_Parrot_lib_update_paths_from_config_hash \
- __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
- PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_parrot_split_path_ext __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(in) \
View
@@ -146,11 +146,29 @@ Parrot_api_load_bytecode_file(ARGMOD(PMC *interp_pmc), ARGIN(const char *filenam
PackFile * const pf = Parrot_pbc_read(interp, filename, 0);
if (!pf)
Parrot_ex_throw_from_c_args(interp, NULL, 1, "Could not load packfile");
+ do_sub_pragmas(interp, pf->cur_cs, PBC_PBC, NULL);
*pbc = Parrot_pmc_new(interp, enum_class_UnManagedStruct);
VTABLE_set_pointer(interp, *pbc, pf);
EMBED_API_CALLOUT(interp_pmc, interp)
}
+PARROT_API
+Parrot_Int
+Parrot_api_load_bytecode_bytes(ARGMOD(PMC *interp_pmc), ARGIN(const unsigned char * const pbc), Parrot_Int bytecode_size, ARGOUT(PMC **pbcpmc))
+{
+ EMBED_API_CALLIN(interp_pmc, interp);
+ PackFile * const pf = PackFile_new(interp, 0);
+ if (!pf)
+ Parrot_ex_throw_from_c_args(interp, NULL, 1, "Could not create packfile");
+
+ if (!PackFile_unpack(interp, pf, (const opcode_t *)pbc, bytecode_size))
+ Parrot_ex_throw_from_c_args(interp, NULL, 1, "could not unpack packfile");
+ do_sub_pragmas(interp, pf->cur_cs, PBC_PBC, NULL);
+ *pbcpmc = Parrot_pmc_new(interp, enum_class_UnManagedStruct);
+ VTABLE_set_pointer(interp, *pbcpmc, pf);
+ EMBED_API_CALLOUT(interp_pmc, interp);
+}
+
PARROT_API
Parrot_Int
Parrot_api_run_bytecode(ARGMOD(PMC *interp_pmc), ARGIN(PMC *pbc), ARGIN(PMC *mainargs))
View
@@ -120,10 +120,10 @@ main(int argc, const char *argv[])
int pir_argc;
const char **pir_argv;
const char *core = "slow";
-
- Parrot_Init_Args *initargs = (Parrot_Init_Args*)calloc(sizeof(Parrot_Init_Args), 0);
+ Parrot_Init_Args *initargs;
Parrot_Int trace = 0;
+ GET_INIT_STRUCT(initargs);
/* internationalization setup */
/* setlocale(LC_ALL, ""); */
//PARROT_BINDTEXTDOMAIN(PACKAGE, LOCALEDIR);
@@ -171,11 +171,12 @@ get_last_error(Parrot_PMC interp)
Parrot_String errmsg;
char * errmsg_raw;
if (Parrot_api_get_last_error(interp, &errmsg) &&
- Parrot_api_string_export_ascii(interp, errmsg, &errmsg_raw))
+ Parrot_api_string_export_ascii(interp, errmsg, &errmsg_raw)) {
fprintf(stderr, "PARROT VM: Catastrophic error. Cannot recover\n");
+ Parrot_api_string_free_exported_ascii(interp, errmsg_raw);
+ }
else
fprintf(stderr, "PARROT VM: %s\n", errmsg_raw);
-
}
/*
View
@@ -60,9 +60,13 @@ Compile bytecode to executable.
outfh.'open'(cfile, 'w')
unless outfh goto err_outfh
print outfh, <<'HEADER'
-#include "parrot/parrot.h"
-#include "parrot/embed.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "parrot/api.h"
const void * get_program_code(void);
+int Parrot_set_config_hash(Parrot_PMC interp_pmc);
+static void get_last_error(Parrot_PMC interp);
+ #define TRACE 0
HEADER
print outfh, codestring
@@ -74,43 +78,54 @@ HEADER
print outfh, <<'MAIN'
int main(int argc, const char *argv[])
{
- PackFile *pf;
- Parrot_Interp interp;
+ PMC * interp;
+ PMC * pbc;
+ PMC * argsarray;
const unsigned char *program_code_addr;
+ Parrot_Init_Args *initargs;
+ GET_INIT_STRUCT(initargs);
program_code_addr = (const unsigned char *)get_program_code();
if (!program_code_addr)
- return 1;
-
- Parrot_set_config_hash();
-
- interp = Parrot_new( NULL );
- if (!interp)
- return 1;
-
- Parrot_init_stacktop(interp, &interp);
- Parrot_set_executable_name(interp,
- Parrot_str_new(interp, argv[0], 0));
- Parrot_set_run_core(interp, (Parrot_Run_core_t)RUNCORE);
- Parrot_set_flag(interp, PARROT_DESTROY_FLAG);
-
- pf = PackFile_new(interp, 0);
- if (!pf)
- return 1;
-
- if (!PackFile_unpack(interp, pf,
- (const opcode_t *)program_code_addr, bytecode_size))
- return 1;
-
- do_sub_pragmas(interp, pf->cur_cs, PBC_PBC, NULL);
-
- Parrot_pbc_load(interp, pf);
+ exit(EXIT_FAILURE);
+
+ if (!(Parrot_api_make_interpreter(NULL, 0, initargs, &interp) &&
+ Parrot_set_config_hash(interp) &&
+ Parrot_api_set_executable_name(interp, argv[0]) &&
+ Parrot_api_set_runcore(interp, RUNCORE, TRACE))) {
+ fprintf(stderr, "PARROT VM: Could not initialize new interpreter");
+ get_last_error(interp);
+ exit(EXIT_FAILURE);
+ }
+
+ //Parrot_set_flag(interp, PARROT_DESTROY_FLAG);
+
+ if (!(Parrot_api_load_bytecode_bytes(interp, program_code, sizeof(program_code), &pbc) &&
+ Parrot_api_build_argv_array(interp, argc, argv, &argsarray) &&
+ Parrot_api_run_bytecode(interp, pbc, argsarray))) {
+ fprintf(stderr, "PARROT VM: Execution failed");
+ get_last_error(interp);
+ exit(EXIT_FAILURE);
+ }
+
+ Parrot_api_destroy_interpreter(interp);
+ exit(EXIT_SUCCESS);
+ }
- PackFile_fixup_subs(interp, PBC_MAIN, NULL);
- Parrot_runcode(interp, argc, argv);
- Parrot_destroy(interp);
- Parrot_exit(interp, 0);
+ static void
+ get_last_error(Parrot_PMC interp)
+ {
+ Parrot_String errmsg;
+ char * errmsg_raw;
+ if (Parrot_api_get_last_error(interp, &errmsg) &&
+ Parrot_api_string_export_ascii(interp, errmsg, &errmsg_raw)) {
+ fprintf(stderr, "PARROT VM: Catastrophic error. Cannot recover\n");
+ Parrot_api_string_free_exported_ascii(interp, errmsg_raw);
+ }
+ else
+ fprintf(stderr, "PARROT VM: %s\n", errmsg_raw);
}
+
MAIN
@@ -224,15 +239,15 @@ HELP
.local int runcore_code
unless runcore == 'slow' goto end_slow_core
- runcore_code = .PARROT_SLOW_CORE
+ runcore_code = 'slow'
goto done_runcore
end_slow_core:
unless runcore == 'fast' goto end_fast_core
- runcore_code = .PARROT_FAST_CORE
+ runcore_code = 'fast'
goto done_runcore
end_fast_core:
unless runcore == '' goto end_unspecified_core
- runcore_code = .PARROT_FAST_CORE
+ runcore_code = 'fast'
goto done_runcore
end_unspecified_core:
# invalid runcore name

0 comments on commit 46b7a9d

Please sign in to comment.