LLVM custom backend iplementation

I’m currently working on my own backend and implementing passing of pointers to functions. I am currently passing the pointer value as a register argument which is then used as a base to add the offset within (say an array) to access variables with.

I’m having the issue that the array to which a pointer is being passed is allocated in sequentially decreasing stack addresses, but the offset that is passed to access within the array is positive. So within the callee, the accesses are incorrect as it accesses with base + (positive offset) when really it should be base + (negative offset).

I was wondering if anyone could help point me in the right direction as to where I should look to try and fix this offset issue.

Not sure I understood your issue.
Do you mean that you pass all function arguments as a single data structure indirectly via pointer?
A simple source ll file and the expected machine code would help.

No sorry, I’m just passing an array as a pointer and accessing the array in the callee. The pointer to the base of the array is passed from the caller to the callee in a register. The register has the correct base address but when accessing arr[2] for instance, the offset from the base is 8 bytes (array of ints). On the stack, this array gets stored were array[0] is at base, array[1] is at base-4, array[2] is at base-8 … However, when the callee accesses array[2] the output machine code uses base+8 instead of base -8.

I was wondering how I could control what that offset is.

So you pass an array argument in stack with elements reversed. Is that correct?
You say that the base pointing past the last byte of the reversed array is correct, but what’s the expected behavior of accessing base[0] in this case? Since the base points past the array in memory, wouldn’t that mean that you really read/write -1th element?
Moreover, what will happen if you pass a pointer to this array to some other function which does not know that your array is reversed?
Is passing array reversed an ABI requirement?

If this is an ABI requirement, I guess you will have to actually copy the array from the arguments area into the local stack area restoring the natural order of the elements and returning a pointer to the copy.

Thanks for this. The reversing of the array is not an ABI requirement and I was actually wondering why that was happening as a separate issue. I suspect this might be a side effect of something else I have done due to requiring negative offsets from the frame pointer in some places. I’ve tested with other architectures don’t store the array in this manner by default, i.e. the 0th array element is stored at the most negative stack address, so this must be something specific to my implementation.

Do I understand correctly that you have a target with stack pointer growing up in the direction of increasing addresses?