Ticket #688: faster_pbc_to_exe.patch

File faster_pbc_to_exe.patch, 2.1 KB (added by Util, 13 years ago)

First test patch

  • tools/dev/pbc_to_exe.pir

     
    172172    .return(infile, cfile, objfile, exefile) 
    173173.end 
    174174 
     175# The PBC will be represented as a C string, so this sub builds a table 
     176# of the C representation of each ASCII character, for lookup by ordinal value. 
     177.sub 'generate_encoding_table' 
     178    # Use '\%o' for speed, or '\x%02x' for readability 
     179    .const string encoding_format = '\%o' 
     180 
     181    # The 'sprintf' op requires the arglist to be in an array, even when 
     182    # there is only one arg. 
     183    .local pmc one_number 
     184    one_number    = new 'FixedIntegerArray' 
     185    set one_number, 1 
     186 
     187    .local pmc coded_strings 
     188    coded_strings = new 'FixedStringArray' 
     189    set coded_strings, 256 
     190 
     191    .local int index 
     192    index = 0 
     193 
     194  next_index: 
     195    one_number[0] = index 
     196    $S0 = sprintf encoding_format, one_number 
     197    coded_strings[index] = $S0 
     198    inc index 
     199    if index < 256 goto next_index 
     200 
     201    .return (coded_strings) 
     202.end 
     203 
    175204.sub 'generate_code' 
    176205    .param string infile 
    177206    .local pmc ifh 
    178207    ifh = open infile, 'r' 
    179208    unless ifh goto err_infile 
     209 
     210    .local pmc encoding_table 
     211    encoding_table = 'generate_encoding_table'() 
     212 
    180213    .local string codestring 
    181214    .local int size 
    182     codestring = "const Parrot_UInt1 program_code[] = {" 
     215    codestring = "const char * program_code =\n" 
     216    codestring .= '"' 
    183217    size = 0 
    184218 
    185219  read_loop: 
     
    195229  code_loop: 
    196230    unless pos < pbclength goto code_done 
    197231    $I0 = ord pbcstring, pos 
    198     $S0 = $I0 
     232    $S0 = encoding_table[$I0] 
    199233    codestring .= $S0 
    200     codestring .= ',' 
    201234    inc pos 
    202235    inc size 
    203236    $I0 = size % 32 
    204237    unless $I0 == 0 goto code_loop 
     238    codestring .= '"' 
    205239    codestring .= "\n" 
     240    codestring .= '"' 
    206241    goto code_loop 
    207242  code_done: 
    208243    goto read_loop 
     
    210245  read_done: 
    211246    close ifh 
    212247 
    213     codestring .= "\n};\n\n" 
     248    codestring .= '"' 
     249    codestring .= "\n;\n\n" 
    214250    codestring .= "const int bytecode_size = " 
    215251    $S0 = size 
    216252    codestring .= $S0