#1138: Create a 'vivify' opcode
-----------------------------+----------------------------------------------
 Reporter:  Austin_Hastings  |        Type:  todo  
   Status:  new              |    Priority:  normal
Milestone:                   |   Component:  core  
  Version:  1.7.0            |    Severity:  medium
 Keywords:                   |        Lang:        
    Patch:                   |    Platform:        
-----------------------------+----------------------------------------------

Comment(by pmichaud):

 Having just written further details on TT #787, I thought I'd add a note
 here:  In general I'm in favor of coming up with opcode(s) that avoid or
 simplify the "create if null" sequences that NQP/PCT currently end up
 generating.  But I'm opposed to the "vivify" opcode as proposed above for
 two reasons:

 1.  It's misnamed -- it's really just defaulting a null PMC to a value, it
 doesn't "vivify" a lexical symbol or aggregate element in the sense of
 "create and bind" that HLLs normally have.

 2.  I think we can come up with a far more powerful set of opcodes than
 this.

 Just brainstorming a bit:  I'm thinking that what might be very powerful
 would be separate "fetch" and "vivify" opcodes that handle nearly all of
 the rvalue and lvalue scenarios we currently have.  The generic form of
 each would be:
 {{{
     $P0 = fetch $P1[key], $P2     # rvalue fetch
     $P0 = vivify $P1[key], $P2    # lvalue fetch
 }}}

 The first form does a lookup of 'key' in an aggregate $P1, if found it
 returns it, otherwise it returns $P2.  The second form is similar, but if
 'key' isn't found then we clone $P2 and bind the aggregate to the clone
 before returning it.

 Since lexpads and namespaces effectively act as hashes, these two opcodes
 should be sufficient for nearly all lexical, package, and keyed variable
 accesses.  For example:
 {{{
     # at beginning of sub
     .local pmc lexpad, undef
     $P0 = getinterp
     lexpad = $P0['lexpad', 0]
     undef = new ['Undef']

     # say($foo)
     $P99 = fetch lexpad['$foo'], undef
     say $P99

     # $foo = 4
     $P99 = vivify lexpad['$foo'], undef
     assign $P99, 4
 }}}

 Clearly these would also work well for namespace and aggregates:
 {{{
     # $ABC::Test::xyz = 'hello';
     $P0 = get_namespace ['ABC';'Test']
     $P99 = vivify $P0['$xyz'], undef
     assign $P99, 'hello'

     # %hash['key'] = $a
     $P100 = vivify lexpad['%hash'], hash
     $P101 = vivify $P0['key'], undef
     $P102 = fetch  lexpad['$a'], undef
     assign $P101, $P102
 }}}

 Here I've proposed that the last argument to 'fetch' and 'vivify' be used
 as values to be cloned instead of types to be created -- I think that
 cloning a default value gives us more flexibility in what that default can
 be (e.g., initial values or properties already in place).

 Thoughts welcomed on the above.  If fetch and vivify were made available
 along the lines I've given here it would greatly simplify variable access
 in PCT and be far more efficient than what we've been generating.

 Pm

-- 
Ticket URL: <https://trac.parrot.org/parrot/ticket/1138#comment:2>
Parrot <https://trac.parrot.org/parrot/>
Parrot Development
_______________________________________________
parrot-tickets mailing list
[email protected]
http://lists.parrot.org/mailman/listinfo/parrot-tickets

Reply via email to