Permalink
Browse files

Borrow code from pbc_to_exe to create a custom build script to write …

…prt0.pbc to a .c file. Link that in from frontend/parrot2/main.c to avoid an extra file load, extra API calls, and extra GCables
  • Loading branch information...
1 parent d0b907d commit 7721cef8c4fcae71c89934cb02ce9db6aad5e14f @Whiteknight Whiteknight committed Aug 9, 2011
Showing with 81 additions and 5 deletions.
  1. +3 −0 .gitignore
  2. +5 −2 config/gen/makefiles/root.in
  3. +72 −0 frontend/parrot2/build.pir
  4. +1 −3 frontend/parrot2/main.c
View
@@ -210,6 +210,9 @@
/ext/Parrot-Embed/t/*.pbc
/ext/nqp-rx/src/stage0/nqp-setting.pir
/frontend/parrot/main.o
+/frontend/parrot2/main.o
+/frontend/parrot2/prt0.pbc
+/frontend/parrot2/prt0.pbc.c
/frontend/parrot_debugger/main.o
/frontend/pbc_disassemble/main.o
/frontend/pbc_dump/main.o
@@ -861,11 +861,14 @@ runtime/parrot/include/config.fpmc : myconfig config_lib.pir \
frontend/parrot2/prt0.pbc : frontend/parrot2/prt0.pir $(MINIPARROT)
$(MINIPARROT) -Iruntime/parrot/include -o frontend/parrot2/prt0.pbc frontend/parrot2/prt0.pir
+frontend/parrot2/prt0.pbc.c : frontend/parrot2/prt0.pbc frontend/parrot2/build.pir $(MINIPARROT)
+ $(MINIPARROT) frontend/parrot2/build.pir frontend/parrot2/prt0.pbc
+
$(PARROT) : frontend/parrot2/main$(O) $(GEN_HEADERS) $(LIBPARROT) \
src/parrot_config$(O) src/longopt$(O) \
- $(MINIPARROT)
+ $(MINIPARROT) frontend/parrot2/prt0.pbc.c
$(LINK) @ld_out@$@ \
- frontend/parrot2/main$(O) src/parrot_config$(O) src/longopt$(O) \
+ frontend/parrot2/main$(O) frontend/parrot2/prt0.pbc.c src/parrot_config$(O) src/longopt$(O) \
$(RPATH_BLIB) $(ALL_PARROT_LIBS) $(LINKFLAGS) $(LINK_DYNAMIC)
#IF(win32): if exist $@.manifest mt.exe -nologo -manifest $@.manifest -outputresource:$@;1
@@ -0,0 +1,72 @@
+.sub 'main' :main
+ .param pmc args
+ .local pmc outfh, ifh
+ .local string infile, outfile, program_name
+ .local int size
+ program_name = shift args
+ infile = shift args
+ outfile = infile . ".c"
+ say outfile
+ ifh = new ['FileHandle']
+ ifh.'open'(infile, 'rb')
+ outfh = new ['FileHandle']
+ outfh.'open'(outfile, 'wb')
+ print outfh, "#include <stdlib.h>\n\n"
+ print outfh, "const unsigned char program_code[] = {"
+ size = 0
+
+ read_loop:
+ .local string pbcstring
+ .local int pbclength
+
+ pbcstring = ifh.'read'(16384)
+ pbclength = length pbcstring
+ unless pbclength > 0 goto read_done
+
+ .local int pos
+ pos = 0
+ code_loop:
+ unless pos < pbclength goto code_done
+ $I0 = ord pbcstring, pos
+ $S0 = $I0
+ print outfh, $S0
+ print outfh, ','
+ inc pos
+ inc size
+ $I0 = size % 32
+ unless $I0 == 0 goto code_loop
+ print outfh, "\n"
+ goto code_loop
+ code_done:
+ goto read_loop
+
+ read_done:
+ say "Done reading input"
+ ifh.'close'()
+
+ say "writing coda"
+ print outfh, "\n};\n\nconst size_t bytecode_size = "
+ $S0 = size
+ print outfh, $S0
+ print outfh, ";\n"
+ print outfh, <<'END_OF_FUNCTION'
+ const void * get_program_code(void)
+ {
+ return program_code;
+ }
+
+ const size_t get_program_code_size(void)
+ {
+ return bytecode_size;
+ }
+END_OF_FUNCTION
+
+ outfh.'flush'()
+ outfh.'close'()
+ .return ()
+
+ err_infile:
+ die "cannot open infile"
+.end
+
+
@@ -132,7 +132,6 @@ int
main(int argc, const char *argv[])
{
Parrot_PMC interp, bytecodepmc, sysargs, pirargs;
- Parrot_String prt0_filename;
Parrot_Init_Args *initargs;
struct init_args_t parsed_flags;
@@ -168,8 +167,7 @@ main(int argc, const char *argv[])
if (!(Parrot_api_pmc_wrap_string_array(interp, parsed_flags.sysargc, parsed_flags.sysargv, &sysargs) &&
Parrot_api_pmc_wrap_string_array(interp, parsed_flags.progargc, parsed_flags.progargv, &pirargs) &&
- Parrot_api_string_import(interp, "frontend/parrot2/prt0.pbc", &prt0_filename) &&
- Parrot_api_load_bytecode_file(interp, prt0_filename, &bytecodepmc) &&
+ Parrot_api_load_bytecode_bytes(interp, get_program_code(), get_program_code_size(), &bytecodepmc) &&
Parrot_api_run_bytecode(interp, bytecodepmc, sysargs, pirargs)))
show_last_error_and_exit(interp);

0 comments on commit 7721cef

Please sign in to comment.