Index: tools/dev/pbc_to_exe.pir =================================================================== --- tools/dev/pbc_to_exe.pir (revision 38385) +++ tools/dev/pbc_to_exe.pir (working copy) @@ -172,14 +172,48 @@ .return(infile, cfile, objfile, exefile) .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' .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 Parrot_UInt1 program_code[] = {" + codestring = "const char * program_code =\n" + codestring .= '"' size = 0 read_loop: @@ -195,14 +229,15 @@ code_loop: unless pos < pbclength goto code_done $I0 = ord pbcstring, pos - $S0 = $I0 + $S0 = encoding_table[$I0] codestring .= $S0 - codestring .= ',' 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 @@ -210,7 +245,8 @@ read_done: close ifh - codestring .= "\n};\n\n" + codestring .= '"' + codestring .= "\n;\n\n" codestring .= "const int bytecode_size = " $S0 = size codestring .= $S0