Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace assembly code responsible for doing generic C calls #18

Closed
SanderMertens opened this issue Nov 27, 2014 · 2 comments
Closed

Replace assembly code responsible for doing generic C calls #18

SanderMertens opened this issue Nov 27, 2014 · 2 comments

Comments

@SanderMertens
Copy link
Member

The file db_cdecl_x86.s contains assembly code that is capable of generically invoking subroutines without knowing their signature at compile-time. This is something that is not possible in C, hence the need for assembly (you can't manipulate the stack directly in C). Note that this is a problem only in C and C++.

The assembly however makes it very hard to port the code other platforms / architectures / compilers. To encourage porting the code to other platforms the dependency on assembly code must be removed.

An alternative approach is to wrap the calls in an interface that is mandated by the language binding. Consider the following C-function:

db_uint32 multiply(db_uint32 a, db_uint32 b) {
    return a + b;
}

Suppose that now a script run by the virtual machine wants to call this function. Since the virtual machine doesn't know this function and doesn't know its signature it cannot call the function directly. However, when this function would be wrapped into a mandated interface:

db_void __multiply(void *args, void *result) {
    *(db_uint32*)result = 
        multiply(*(db_uint32*)args, *(db_uint32*)((intptr_t)args + sizeof(db_uint32)));
}

... the function could be generically called from anywhere even when the callee signature is not known by the caller. This approach is likely to be faster than the assembly approach since the assembly had to figure out based on metadata how to call the function, while this approach hard-codes the mapping to a generic interface.

The only downside of this approach is that wrapper functions have to be created and executable footprint is increased by a little bit. Wrapper functions can be generated though so this wouldn't be visible for a user.

The wrapper function pointer will be stored in the "impl" field of the function.

@SanderMertens
Copy link
Member Author

The wrapper generation will happen in the C interface generator since this generator also generates the procedure bodies. Since it is undesirable that the wrapper methods "clutter" the interface implementation files they will be stored in a separate file called *__wrapper.c.

The interface implementations are currently already "cluttered" with functions that are generated for calling virtual methods. It would make sense to also store these method implementations in *__wrapper.c.

SanderMertens added a commit that referenced this issue Nov 27, 2014
Extended the interface generator to generate wrappers for procedures.
Modified the load generator to point to the wrappers. Regenerated the
parser.

TODO: modify the core to work with the new wrapper signature.
TODO: modify generated virtual methods & delegates to properly forward
to the new signature when they detect it’s a C function.
SanderMertens added a commit that referenced this issue Nov 29, 2014
The core is now using the new call convention consistently. It doesn’t
build yet since the wrapper functions for the core-procedures are not
yet implemented.

TODO: generate procedures for core.
SanderMertens added a commit that referenced this issue Nov 30, 2014
Added support for attributes in generator API. Also modified dbgen so
that it now accepts the --attr commandline argument that sets an
attribute (replaces the -o argument).

TODO: modify c_interface generator so that it can generate wrappers for
the core (#18 and #21).

This commit fixes #22
SanderMertens added a commit that referenced this issue Dec 1, 2014
This change enforces naming conventions of the C language binding on
the core by generating the interfaces.

It also generates the wrappers for the core procedures, which is
required because of #18.

Additionally, the change also moves interface/api into the core.

TODO: xml deserializer crashes on fast.xml
TODO: warning in generated wrappers
TODO: core is missing some procedures
@SanderMertens
Copy link
Member Author

This issue is solved now that dependent issue #21 is fixed.

jleeothon added a commit that referenced this issue Mar 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant