On Wed Dec 12 06:07:47 2007, kjs wrote:
> It would be helpful if PCT knew how to handle integer registers if needed.
> 
> A typical example is for using the "isgt" or any other isXX op:
> 
> a = 1
> b = 2
> $I0 = isgt a, b   # returns 0, as 1 is not greater than 2
> 
> 
> In PCT this is helpful, if you want to translate a HLL statement like
this:
> 
> if a > b then {some thing you want to do if a > b }
> 
> This can then be compiled to:
> 
> $I0 = isgt a, b
> if $I0 goto _then123


This is now possible as of r27505.  Opcodes in %piropsig are able to
specify "I" as the return value type, which means that the opcode is to
return its value into a unique I register.

The "pirop" attribute of PAST::Op nodes is also able to override the
signature to be used for a given opcode.  For example, a string
comparison can be specified as  :pirop("isgt ISs")  which coerces 
the operands into string values before executing the opcode and
returning the result in an I register.  By contrast, :pirop("isgt INn")
will coerce the operands to num values before performing the opcode.

The other nodes all understand various register mappings -- for example,
an "if" or other conditional node will use an I, S, or N register
directly if that's ultimately shorter than converting to a PMC first.

As an example, here's the difference in PIR code generated for "if $x ==
'hello' { say(1); }"  before and after the changes in r27505:

r27504:
    ...
    find_lex $P14, "$x"
    unless_null $P14, vivify_22
    new $P14, "Undef"
  vivify_22:
    new $P15, "String"
    assign $P15, "hello"
        ##  inline infix:eq
        $S0 = $P14
        $S1 = $P15
        $I0 = iseq $S0, $S1
        $P16 = new 'Integer'
        $P16 = $I0
    if $P16, if_13
    goto if_13_end
  if_13:
    get_global $P20, "_block17"
    newclosure $P20, $P20
    $P19 = $P20()
  if_13_end:


r27505:
    ...
    find_lex $P15, "$x"
    unless_null $P15, vivify_10
    new $P15, "Undef"
  vivify_10:
    set $S16, $P15
    iseq $I17, $S16, "hello"
    unless $I17, if_14_end
    get_global $P10, "_block18"
    newclosure $P10, $P10
    $P10()
  if_14_end:

Note that as of r27505 we avoid the creation of an extra String PMC for
the 'hello' constant, the Integer PMC for the result of the 'isgt'
opcode, and the extra 'goto' in the "else" portion of the test. 

Optimizing something like 

    $I0 = iseq $S0, $S1
    if $I0 goto label1

to

    eq $S0, $S1, label1

is left as an optimization for a later stage (probably either in POST or
IMCC).

For now, if anyone wants more details about this, the best place to look
is at the changes that occurred in NQP in r27505.  I expect to be
writing more about this feature in PDD26 and on my use.perl journal in
the next few days.

Thanks!

Pm

Reply via email to