[svn:parrot] r39176 - trunk/tools/dev

Util at svn.parrot.org Util at svn.parrot.org
Tue May 26 06:55:04 UTC 2009


Author: Util
Date: Tue May 26 06:55:03 2009
New Revision: 39176
URL: https://trac.parrot.org/parrot/changeset/39176

Log:
[pbc_to_exe] TT #688 - performance fixed for GCC.

Modified:
   trunk/tools/dev/pbc_to_exe.pir

Modified: trunk/tools/dev/pbc_to_exe.pir
==============================================================================
--- trunk/tools/dev/pbc_to_exe.pir	Tue May 26 06:20:54 2009	(r39175)
+++ trunk/tools/dev/pbc_to_exe.pir	Tue May 26 06:55:03 2009	(r39176)
@@ -29,11 +29,24 @@
     .local string out
     .local int    closeresult
 
+    .local string gcc
+    .local int    is_gcc
+    $P0 = '_config'()
+    gcc    = $P0['gccversion']
+    $I0    = length gcc
+    is_gcc = $I0 > 0
+
     (infile, cfile, objfile, exefile) = 'handle_args'(argv)
     unless infile > '' goto err_infile
 
     .local string codestring
+    unless is_gcc goto code_for_non_gcc
+  code_for_gcc:
+    codestring = 'generate_code_gcc'(infile)
+    goto code_end
+  code_for_non_gcc:
     codestring = 'generate_code'(infile)
+  code_end:
 
   open_outfile:
     .local pmc outfh
@@ -221,6 +234,91 @@
     die "cannot open infile"
 .end
 
+# The PBC will be represented as a C string, so this sub builds a table
+# of the C representation of each ASCII character, for lookup by ordinal value.
+.sub 'generate_encoding_table'
+    # Use '\%o' for speed, or '\x%02x' for readability
+    .const string encoding_format = '\%o'
+
+    # The 'sprintf' op requires the arglist to be in an array, even when
+    # there is only one arg.
+    .local pmc one_number
+    one_number    = new 'FixedIntegerArray'
+    set one_number, 1
+
+    .local pmc coded_strings
+    coded_strings = new 'FixedStringArray'
+    set coded_strings, 256
+
+    .local int index
+    index = 0
+
+  next_index:
+    one_number[0] = index
+    $S0 = sprintf encoding_format, one_number
+    coded_strings[index] = $S0
+    inc index
+    if index < 256 goto next_index
+
+    .return (coded_strings)
+.end
+
+.sub 'generate_code_gcc'
+    .param string infile
+    .local pmc ifh
+    ifh = open infile, 'r'
+    unless ifh goto err_infile
+
+    .local pmc encoding_table
+    encoding_table = 'generate_encoding_table'()
+
+    .local string codestring
+    .local int size
+    codestring = "const char * program_code =\n"
+    codestring .= '"'
+    size = 0
+
+  read_loop:
+    .local string pbcstring
+    .local int pbclength
+
+    pbcstring = read ifh, 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 = encoding_table[$I0]
+    codestring .= $S0
+    inc pos
+    inc size
+    $I0 = size % 32
+    unless $I0 == 0 goto code_loop
+    codestring .= '"'
+    codestring .= "\n"
+    codestring .= '"'
+    goto code_loop
+  code_done:
+    goto read_loop
+
+  read_done:
+    close ifh
+
+    codestring .= '"'
+    codestring .= "\n;\n\n"
+    codestring .= "const int bytecode_size = "
+    $S0 = size
+    codestring .= $S0
+    codestring .= ";\n"
+    .return (codestring)
+
+  err_infile:
+    die "cannot open infile"
+.end
+
 # util functions
 .sub 'compile_file'
     .param string cfile


More information about the parrot-commits mailing list