lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Lightning] Register arguments


From: Paulo César Pereira de Andrade
Subject: Re: [Lightning] Register arguments
Date: Mon, 4 Sep 2017 15:10:38 -0400

2017-09-04 3:46 GMT-04:00 Marc Nieper-Wißkirchen <address@hidden>:
> The lightning documentation states: "Note that arguments in registers are
> very cheap, but will be overwritten at any moment, including on some
> operations, for example division, that on several ports is implemented as a
> function call."

  Hi Marc,

> For writing portable code, it is important to know, which instructions
> actually overwrite (register) arguments, or rather, which instructions
> preserve them. Obviously, function calls do not preserve the arguments, and
> the above quote says that using division also does not guarantee that
> arguments are preserved.

  Way too many calls use temporaries. The ones that use more are
qmul and qdiv. Most operations with an immediate argument might need
a temporary.

  If the code does jit_getarg* after a temporary was used, and the argument
was in a register, it can be clobbered in a temporary.

  It is a good practice to read arguments at the function prolog.

> On the other hand, this example from the documentation
>
> fact_entry:                  ! This is the tail call entry point
> ac = arg                     ! The accumulator is the first argument
> in = arg                     ! The factorial argument
>      getarg R0, ac           ! Move the accumulator to R0
>      getarg R1, in           ! Move the argument to R1
>      blei fact_out, R1, 1    ! Done if argument is one or less
>      mulr R0, R0, R1         ! accumulator *= argument
>      putargr R0, ac          ! Update the accumulator
>      subi R1, R1, 1          ! argument -= 1
>      putargr R1, in          ! Update the argument
>      jmpi fact_entry         ! Tail Call Optimize it!
> fact_out:
>      retr R0                 ! Return the accumulator
>
> makes it clear, that simple operations like subtraction actually do preserve
> arguments.
>
> Could the set of safe instructions (that is those instructions not modifying
> arguments) be documented? Is it safe to assume that all ALU operations
> (except for division and multiplication (?)), the compare and transfer
> operations, the network, load and store operations, and the branch
> instructions do not modify (register) arguments?

  jit_getarg* should be called at the function prolog, otherwise the argument
will be considered dead from the prolog to the jit_getarg* call, what may be
too late if it was used as a temporary. But as the example you quote tells,
one can restore (or change) an argument, with jit_putarg*.

> Marc

Thanks,
Paulo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]