Re: Getting the lr register in a C subroutine



Wilco Dijkstra wrote:
> <joseluismarchetti@xxxxxxxxxxxx> wrote in message
> news:1120106856.718408.36300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> > Hello,
> >
> > I am trying to create a C function that will get the lr ( return
> > address ), but I am having a undesired warning from the ads compiler
> > saying that I am corrupting r14 ( lr ).
> > I saw the assembler code that was generated and did not see any
> > corruption.
> > In short what I am trying to do is:
> >
> > int GetCallerAddress( void )
> > {
> > int caller_address;
> >
> > __asm( mov caller_address, lr );
> >
> > return(caller_address);
> > }
> >
> > Does anybody knows a good C way ( not assembler ) to do that and
> > not receiving warnings ?
>
> No, you can't do this in ADS without assembler.

That is the answer I was looking for.

>
> What exactly makes you think the return address is always stored in LR?
> The compiler can place the return address whereever it wants, including
> in a different register than LR or anywhere on the stack. It may corrupt
> LR by making a function call (to push registers, do a stackcheck or similar),
> or use it to store a argument. It may change its mind and move the return
> address from one place to another (this typically happens with shrinkwrapping).
> Since LR may be corrupted or reused on function entry, it is wrong to think
> that LR still contains the return address on the first statement of a function.
>
I will tell you what makes me think this:
The routine I would create is a controled one and all it will do is get
the lr,
so the things you are talking should not happens there, actually I put
an example
of that routine:

int GetCallerAddress( void )
{
int caller_address;

__asm( mov caller_address, lr );

return(caller_address);
}

The assembler code that was generated was perfect and none of the cases
you talk
about happens here, the only problem is the warning message, that to me
does
not make sense because lr is not corrupted in this routine as the
warning message
indicates. The compiler is not smart enough to realize that and just
send out
the warning message as soon lr is in the inline assembler. Another
thing, if I
change lr to r0, the compiler send not warning message out ...

>
> In ADS you simply can't do it - the warning is telling you that you're reading
> a register that the compiler believes isn't valid - you may be lucky sometimes,
> but given the above, do you feel lucky in all cases?

As stated before, it will be a controlled routine.

> It's best to use a small
> assembler function for this - since you don't need to do this often, the overhead
> is minimal.

I thought about that and that is probably what I will do.

> In RVCT there is an intrinsic __return_address() which will go off
> and fetch the return address for you, no matter where it lives.
>

What is RVCT ? __return_address() sounds great, I hope we could have it
here.

> Wilco

Thanks!

.



Relevant Pages

  • Re: Problem with Return Value from a Function
    ... 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. ... but I agree that not assigning at all would ...
    (comp.lang.pl1)
  • 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: ZXSpin - Symbol "MAIN" not found
    ... that I assembly a code I having a warning message: ... Sounds like the assembler wants your first executable statement in the ... program to have a label MAIN. ... start with main, and if you have a C compiler that generates assembly, the ...
    (comp.sys.sinclair)
  • Re: Method ***** not found. Warning displayed in Form1.cs [Design] pane
    ... Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not ... The file where you're trying to add the call site to your new method is a special file: in addition to being compiled by the C# compiler to create your Form sub-class, it is parsed by the Visual Studio Designer as part of showing you the graphical representation of the object and allowing you to edit it. ... If the Designer comes across something it doesn't recognize, it has no way to know what to do with it. ... It's possible that you should simply call the method from your own construction, after the call to InitializeComponent, or that you should override the OnFormLoador OnShownmethods and call the method in the override. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: while (1) vs. for ( ;; )
    ... >> I was surprised at how many people reported that a compiler ... >> better expression (lower defect rate, higher productivity, ... There is for some developers. ... effects of a warning message issued by a compiler, ...
    (comp.lang.c)