Re: Problem with Return Value from a Function



James J. Weinkam wrote:
(snip regarding function return values)

This is not what either the F or the Optimizing compiler did at least for external functions. It was always possible to write an assembly language routine that could be called from PL/I as a function and the location to receive the returned value was passed by reference as an n+1st argument. Function references to assembler routines were only possible if the ASSEMBLER option was *not* used, thus the compiler was unaware the the routine being called was an assembler routine and perforce must have compiled the same linkage it would compile when it was calling a routine written in PL/I. This is all clearly explained in chapter 13 of the programmers guide for the optimizing compiler. I wrote many such functions over the years. You may be thinking of Fortran. It is certainly possible that for internal procedures the optimizer may short circuit some of what it would do for external procedures. You would have to study the compiled code from a particular compiler to find out.

I wrote many PL/I (F) callable assembly routines, but probably
not any were functions. I also called Fortran subroutines,
but again likely no functions.

It is common among other languages and calling sequences to
return integer and float values in registers, and others with
a separate argument. That is, for example, what Systems/C
does. (Though it used R15 for integer and pointer functions,
instead of R0.)

In the discussion at hand, the OP has a (erroneous) function that does not return a result except when its argument is 'B', and he asserts that the the variable to which the returned value is assigned is unchanged in all cases. How can this happen unless the compiler has done what I suggested? If the compiler had created a temporary and subsequently assigned that to A, the final value of A would be whatever random content the temporary contained except when the argument was 'B'.

It is not too hard to have the value accidentally end up in
the right register, but I agree that not assigning at all would
be the easiest way. It is an interesting optimization to realize
that the function return value is assigned directly to a variable.
Consider if the function was used in a more complicated expression.

-- glen

.



Relevant Pages

  • Re: Problem with Return Value from a Function
    ... The optimizing conmpiler no doubt passed a reference to A directly to the function rather than creating a temporary to receive the result and assigning that to A after the function returns. ... It was always possible to write an assembly language routine that could be called from PL/I as a function and the location to receive the returned value was passed by reference as an n+1st argument. ... Function references to assembler routines were only possible if the ASSEMBLER option was *not* used, thus the compiler was unaware the the routine being called was an assembler routine and perforce must have compiled the same linkage it would compile when it was calling a routine written in PL/I. ...
    (comp.lang.pl1)
  • Re: Getting the lr register in a C subroutine
    ... you can't do this in ADS without assembler. ... > The compiler can place the return address whereever it wants, ... The routine I would create is a controled one and all it will do is get ... about happens here, the only problem is the warning message, that to me ...
    (comp.sys.arm)
  • Re: floating point question on MSP430
    ... Writing a routine that sets a bit and then squares ... > In a later post you state that you are using a compiler. ... meant "assembler". ... the F149 does have the hardware multiplier. ...
    (comp.arch.embedded)
  • Re: What micros do you actually hate to work with?
    ... with less than tiny asm ... hassle to work with 32 bit longs in assembler for an 8 bit uP, ... Microchip's C compiler tools and their assembly under MPLAB. ... is that other programmers for C are easier to find. ...
    (comp.arch.embedded)
  • Re: The Promise of Forth
    ... Declare variables, declare locals, let the compiler do the work ... Forth could be considered the assembler for a simplified virtual ... If you use locals then you have to initialise them each time. ... there are likely to be libraries that could be profitably used within ...
    (comp.lang.forth)

Loading