c++parameterscallstack

Portable way to push arguments onto the call stack (C++)


I'm writing a small scripting language.

I am looking for a portable C++ way to push parameters onto the call stack so that my function will read them in normally.

Is this possible? It has to compile in Visual Studio, XCode, and Android Studio-- preferably using asm embedded in the C++ (having an external assembler file is not an option)


Solution

  • AFAIK, there’s no portable ways.

    Also, asm embedded in the C++ is not portable, MS Visual studio doesn’t support it when compiling for Win64.

    One approach around this is invent some ABI on top of C or C++, suitable for your particular use case and script language. This can be done in portable way and without assembly.

    See how MS did it with their VBScript, IDispatch, and VARIANT data type.

    I’m not saying you must stick with OLE automation ABI, that was just an example. E.g. if you only passing several float values to your functions, you can do following: int Invoke(IDFunc func, int argc, const float* argv), make your script runtime resolve function names into IDFunc identifiers, fill arguments array, and call the Invoke function. Then on the C++ side of the interop, call the correct function. You no longer need to manually push arguments, you only need to dispatch the function call, and there’re many approaches for that, macros, templates, code gen, or some combination of them.

    P.S. The IDFunc data type is int16 for MS OLE, but you can make it to be a pointer to something instead, might help with implementation.