Read in a bytecode, unpack it into a C<PackFile> structure, and do fixups. DEPRECATED: Use Parrot_pf_read_pbc_file instead. =cut */ PARROT_EXPORT PARROT_CAN_RETURN_NULL Parrot_PackFile Parrot_pbc_read(PARROT_INTERP, ARGIN_NULLOK(const char *fullname), const int debug) { ASSERT_ARGS(Parrot_pbc_read) STRING * const str = Parrot_str_new(interp, fullname, 0); PackFile * const pf = Parrot_pf_read_pbc_file(interp, str); PMC * const pfpmc = Parrot_pf_get_packfile_pmc(interp, pf); UNUSED(debug); return (Parrot_PackFile)pfpmc; } /* =item C<void Parrot_pbc_load(PARROT_INTERP, Parrot_PackFile pf)> Loads the C<PackFile> returned by C<Parrot_pbc_read()>. DEPRECATED: Use Parrot_pf_set_current_packfile instead. =cut
=cut */ /* TODO: This only works with the initial bytecode. After this we should use Parrot_append_bytecode or something similar */ PARROT_API Parrot_Int Parrot_api_load_bytecode_file(Parrot_PMC interp_pmc, ARGIN(Parrot_String filename), ARGOUT(Parrot_PMC * pbc)) { ASSERT_ARGS(Parrot_api_load_bytecode_file) EMBED_API_CALLIN(interp_pmc, interp) PackFile * const pf = Parrot_pf_read_pbc_file(interp, filename); *pbc = Parrot_pf_get_packfile_pmc(interp, pf, filename); EMBED_API_CALLOUT(interp_pmc, interp) } /* =item C<Parrot_Int Parrot_api_load_bytecode_bytes(Parrot_PMC interp_pmc, const unsigned char * const pbc, Parrot_Int bytecode_size, Parrot_PMC * pbcpmc)> Unpacks a bytecode from a buffer C<pbc> of a C<bytecode_size> size, and stores the resulting bytecode in C<pbcpmc>. This function returns a true value if this call is successful and false value otherwise. =cut
int main(int argc, const char **argv) { Parrot_PackFile pfpmc; PackFile *pf; Interp *interp; Parrot_String infilename; const char *file = NULL; int terse = 0; int disas = 0; int convert = 0; int nums_only = 0; int options = PFOPT_UTILS; struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT; int status; if (argc < 2) help(); interp = Parrot_new(NULL); /* init and set top of stack */ Parrot_init_stacktop(interp, &status); while ((status = longopt_get(argc, argv, opt_options, &opt)) > 0) { switch (opt.opt_id) { case 'h': options += PFOPT_HEADERONLY; break; case 't': terse = 1; break; case 'd': disas = 1; break; case 'o': file = opt.opt_arg; convert = 1; break; case 'n': nums_only = 1; break; case '?': default: help(); break; } } if (status == -1) help(); argc -= opt.opt_index; argv += opt.opt_index; infilename = Parrot_str_new(interp, *argv, 0); pf = Parrot_pf_read_pbc_file(interp, infilename); if (pf == NULL) { printf("Can't read PBC\n"); return 1; } pfpmc = Parrot_pf_get_packfile_pmc(interp, pf); Parrot_pf_set_current_packfile(interp, pfpmc); if (convert) { size_t size = PackFile_pack_size(interp, interp->code->base.pf) * sizeof (opcode_t); opcode_t *pack = (opcode_t *)Parrot_gc_allocate_memory_chunk(interp, size); FILE *fp; if (!pack) { printf("out of mem\n"); exit(EXIT_FAILURE); } PackFile_pack(interp, interp->code->base.pf, pack); if (STREQ(file, "-")) fp = stdout; else if ((fp = fopen(file, "wb")) == 0) { printf("Couldn't open %s\n", file); exit(EXIT_FAILURE); } if ((1 != fwrite(pack, size, 1, fp))) { printf("Couldn't write %s\n", file); exit(EXIT_FAILURE); } fclose(fp); Parrot_gc_free_memory_chunk(interp, pack); Parrot_x_exit(interp, 0); } if (!nums_only) PackFile_header_dump(interp, pf); if (options & PFOPT_HEADERONLY) Parrot_x_exit(interp, 0); /* install a dumper function */ if (!terse) { pf->PackFuncs[PF_CONST_SEG].dump = const_dump; } if (disas) pf->PackFuncs[PF_BYTEC_SEG].dump = disas_dump; if (nums_only) { int i; for (i = PF_DIR_SEG + 1; i < PF_MAX_SEG; ++i) pf->PackFuncs[i].dump = null_dump; pf->PackFuncs[PF_DIR_SEG].dump = null_dir_dump; pf->PackFuncs[PF_BYTEC_SEG].dump = nums_dump; } /* do a directory dump, which dumps segs then */ PackFile_Segment_dump(interp, &pf->directory.base); Parrot_x_exit(interp, 0); }
int main(int argc, const char *argv[]) { int nextarg; Parrot_Interp interp; PDB_t *pdb; const char *scriptname = NULL; interp = Parrot_interp_new(NULL); Parrot_debugger_init(interp); pdb = interp->pdb; pdb->state = PDB_ENTER; Parrot_block_GC_mark(interp); Parrot_block_GC_sweep(interp); nextarg = 1; if (argv[nextarg] && strcmp(argv[nextarg], "--script") == 0) { scriptname = argv [++nextarg]; ++nextarg; } if (argv[nextarg]) { const char * const filename = argv[nextarg]; if (*filename == '-') { fprintf(stderr, "parrot_debugger takes no -x or --xxxx flag arguments\n"); exit(1); } else { STRING * const filename_str = Parrot_str_new(interp, filename, 0); PackFile * const pfraw = Parrot_pf_read_pbc_file(interp, filename_str); Parrot_PackFile pf; if (pfraw == NULL) return 1; pf = Parrot_pf_get_packfile_pmc(interp, pfraw, filename_str); if (pf == NULL) return 1; Parrot_pf_set_current_packfile(interp, pf); Parrot_pf_prepare_packfile_init(interp, pf); } } else { /* Generate some code to be able to enter into runloop */ STRING * const compiler_s = Parrot_str_new_constant(interp, "PIR"); PMC * const compiler = Parrot_interp_get_compiler(interp, compiler_s); STRING * const source = Parrot_str_new_constant(interp, ".sub aux :main\nexit 0\n.end\n"); PMC * const code = Parrot_interp_compile_string(interp, compiler, source); if (PMC_IS_NULL(code)) Parrot_warn(interp, PARROT_WARNINGS_NONE_FLAG, "Unexpected compiler problem at debugger start"); } Parrot_unblock_GC_mark(interp); Parrot_unblock_GC_sweep(interp); if (scriptname) PDB_script_file(interp, scriptname); else PDB_printwelcome(); Parrot_runcore_switch(interp, Parrot_str_new_constant(interp, "debugger")); PDB_run_code(interp, argc - nextarg, argv + nextarg); Parrot_x_exit(interp, 0); }