TT #1091 and DEPRECATED.pod claim that label-based exception
handlers are eligible to be removed in 2.1.  I think label-based
exception handlers are necessary; but I'm willing to be proven wrong.

In particular, I need to know details of what will be replacing
them, as the changes are likely to affect both PCT and NQP, and
thus those subsystems will need to also have deprecation notices
related to this in place before 2.0 as well.

Here's an example of where PCT currently uses label-based
exception handlers, and why I think we need them.  Consider
the following HLL (Perl 5) code:

    for($i=0; $i<20; $i++)
    {
        next if $i % 2;
        last if $i == 10;
        say $i;
    }

The PIR that would be generated for the loop portion of the code 
would look something like the following (I've simplified it a bit 
here for clarity, but all of the essential pieces are present):

    .include 'except_severity.pasm'
    .include 'except_types.pasm'

    .sub 'main' :main
        .local pmc i
        i = box 0
        .lex '$i', i
        .local pmc handler
        handler = new ['ExceptionHandler']
        set_addr handler, loop_handler
        handler.'handle_types'(.CONTROL_LOOP_NEXT, .CONTROL_LOOP_LAST, 
.CONTROL_LOOP_REDO)
        push_eh handler
      loop_test:
        unless i < 20 goto loop_done
      loop_redo:
        .const 'Sub' $P0 = 'inner'
        capture_lex $P0
        $P0()
      loop_next:
        inc i
        goto loop_test
      loop_handler:
        .local pmc exception
        .get_results (exception)
        $I0 = exception['type']
        if $I0 == .CONTROL_LOOP_NEXT goto loop_next
        if $I0 == .CONTROL_LOOP_REDO goto loop_test
        # .CONTROL_LOOP_LAST falls through
      loop_done:
        noop
        pop_eh
    .end
    
    .sub 'inner' :outer('main')
        .local pmc i
        i = find_lex '$i'
        mod $P0, i, 2
        unless $P0 goto if1_done
        'next'()
      if1_done:
        unless i == 10 goto if2_done
        'last'()
      if2_done:
        say i
    .end

The "next" and "last" builtins simply throw exceptions of
the appropriate type:
    
    .sub 'next'
        $P0 = new ['Exception']
        $P0['severity'] = .EXCEPT_NORMAL
        $P0['type'] = .CONTROL_LOOP_NEXT
        throw $P0
    .end
    
    .sub 'last'
        $P0 = new ['Exception']
        $P0['severity'] = .EXCEPT_NORMAL
        $P0['type'] = .CONTROL_LOOP_LAST
        throw $P0
    .end

Thus far I've been unable to figure out how to achieve 
generalized next/last/redo branching without having the 
ability to catch an exception at a label (such as "loop_handler" 
above).

Can someone give me a complete example of PIR code to replace
the above when TT #1091 goes into effect -- i.e., when
ExceptionHandler PMCs can only refer to another subroutine
and not a label within the current sub?

Thanks.

Pm
_______________________________________________
http://lists.parrot.org/mailman/listinfo/parrot-dev

Reply via email to