[LLVMdev] making a copy of a byval aggregate on the callee's frame

Robert Lytton robert at xmos.com
Thu Jul 4 13:43:10 PDT 2013

Hi Tim,

Thank you for the input.
I think I follow you.

I believe the LowerCall is doing what it needs to do - passing pointer either on the stack or in register as per ABI.

The LowerFormalArguments() is where I am stuck.
LowerFormalArguments () calls CCInfo.AnalyzeFormalArguments(Ins, CC_XCore), which calls the CC_XCore().
This is where I placed the CCIfByVal<CCPassByVal<0,4>> which only pushed the pointer to the stack.
However, I don't want to push the pointer to the stack but COPY the pointee.
Indeed, I want to keep the pointer where it is BUT re-point it to a new object copied onto the callee's stack.
What I really want it something like:

static bool CC_XCore(...) {
  if (ArgFlags.isByVal()) {  // a 'CCCustom' function
    Size = ValNo.pointee.size;                     // where do I get the pointee's info from?
    NewVal = State.AllocateStack( Size, 4);  // how do I create space on the callee stack?
    memcpy(NewVal, ValNo.pointee, Size);  // how do I copy from caller's stack to callee's stack?
    ValNo.pointee=NewVal;                        // how re-point the pointer at the callee's stack (rather than caller's data)?

If this is the correct way to do it, I will continue to plough ahead.
As you can see, I have no idea what api functions I should be using :-)

Thank you

From: Tim Northover [t.p.northover at gmail.com]
Sent: 04 July 2013 20:24
To: Robert Lytton
Cc: <llvmdev at cs.uiuc.edu>
Subject: Re: [LLVMdev] making a copy of a byval aggregate on the callee's frame

Hi Robert,

> I tried adding to the XCoreCallingConv.td:
>     CCIfByVal<CCPassByVal<0,4>>        // pushes pointer to the stack

This looks sensible to me. After that it comes down to cooperation
between XCoreISelLowering's LowerFormalArguments and LowerCall
functions. LowerFormalArguments is at the beginning of a function and
is responsible for taking arguments out of registers and putting them
into sensible places for the rest of the function to use. LowerCall is
responsible for putting call arguments where callees will expect them
and making the call.

On most targets, for byval, LowerCall would store the argument by
value on the stack (likely with a memcpy equivalent from the actual
pointer that's being passed); and LowerFormalArguments would create a
fixed FrameIndex pointing there and record that as the address for use
by everything else.

You'll want to do basically the reverse: LowerCall will just put the
pointer it's given on the stack; LowerFormalArguments will do the
memcpy like operation into a local variable created for the purpose
(also a FrameIndex, but of a different kind), then it'll record that
frame-index as the address for everything else to use.

Hope this helps; come back if there's still stuff you don't understand.



More information about the llvm-dev mailing list