id,summary,reporter,owner,description,type,status,priority,milestone,component,version,severity,resolution,keywords,cc,lang,patch,platform
1431,Register assigned to .local string variable is mis-remembered,Austin_Hastings,,"Summary: A .local variable stored in S1 is referenced in a key constant as S0.

The following test code, when fed to parrot-nqp, produces an output file like the one attached (nqp-trace.out):
{{{
pir::trace(1);
P6metaclass.register('Exception');
}}}
Running this command will display the error:
{{{
parrot-nqp test.nqp 2>&1 | grep -A 4 'callmethodcc P., ""methods""'
}}}
producing two blocks, the first of which is:
{{{
   311 callmethodcc P2, ""methods""                                        P2=Class=PMC(0xb73f1840) 
   314 iter P9, P7                                        P9=PMCNULL P7=Hash=PMC(0xb6f788a0)
   317 unless P9, -18                                        P9=HashIterator=PMC(0xb6f788dc) 
   320 shift S1, P9                                        S1=""(null)"" P9=HashIterator=PMC(0xb6f788dc)
   323 set P3, P7[S0=""PMCProxy""]                                        P3=PMCNULL P7=Hash=PMC(0xb6f788a0) 
   327 isa I0, P3, ""NCI""                                        I0=0 P3=Sub=PMC(0xb73f03f0 pc:89) 
--
}}}
The problem is that offsets 320 and 323 refer to S1 and S0, when they should refer to the same register. The code in question is a part of the ""add_parent"" multisub in P6object.pir (~ line 263):
{{{
  method_loop:
    unless methoditer goto mro_loop
    .local string methodname
    .local pmc methodpmc
    methodname = shift methoditer
    methodpmc = methods[methodname]
    # don't add NCI methods (they don't work)
    $I0 = isa methodpmc, 'NCI'
}}}
In this case, the problem occurs with the local string variable `methodname`. Notice that methodname is shifted off `methoditer` and then used to index the `methods` hash.

Compiling to a .pbc and dumping the binary yields these lines (note that offset hex 0140 = decimal 320):
{{{
 0140:  00000347 00000001 00000009                            shift_s_p
 0143:  000003d1 00000003 00000007 00000049                   set_p_p_kc
}}}
I interpret that (possibly wrongly - someone please double-check me) as 
{{{
shift S1, P9
set P3, P3[constant key x49]
}}}
The constants table contains (constant index 0x49 = decimal 73)
{{{
    # 73:
    [ 'PFC_KEY' (1 items)
       {
        FLAGS => 0x4141214 (private2,private4,is_PMC,external,on_free_list,custom_GC,needs_early_gc)
        TYPE        => S REGISTER
        DATA        => 0
       },
    ],
}}}
(which I am interpreting as [S0], per the trace output).
",bug,closed,normal,,none,2.0.0,medium,fixed,,,,,
