C Metaprogramming Tool - Introspection, Code Generation, Code Insertion

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Another question, how do the meta tool know which files to process? Do you explicitly tell it or is it an automatic search?

CombatCoder
Автор

Although this video is 8 years old and you have created Odin lang now stuff like this would be really useful for creating method bindings for C code without littering the code with ugly macro hacks that are hard or impossible to debug. I ran into problem where i had to create function pointer corresponding to each method with same signature. It's possible to write it out by hand but in future if somebody changes method signature the method pointer wouldn't be up to date, so having metaprogramming tool isn't that bad idea if you must stick to C only.

hstrikes
Автор

Very nice! Someone's been watching handmade hero ;)

vexedev
Автор

Fantastic tools. The user code and generated code are both very clean. Reminds me of a metaprogramming library I wrote a few years ago, but since mine was implemented entirely in macros, it was a bit less automatic.

The introspection tool seems particularly nice. I could see it being used to automatically generate bindings for scripts or plugins.

I think your "defer" could be improved a bit, though. You mention a C++ macro for defer being inefficient, but if you're talking about the one I think you are, I believe you'll find that it's actually more efficient than your C defer. It's hard to tell in this video, but I think your version simply inserts the deferred code right before each return statement, correct? This results in a lot of duplicate code all over the place, which is difficult for the compiler to optimize. A C++ defer based on destructors will keep the code all in one place for easy optimization.

You could improve it by stacking all the deferred code at end-of-scope or end-of-function, with the return statements, and using a sort of "goto fail" mechanism. In fact, I would bet that a goto-based solution will generate the exact same assembly code as a destructor-based solution in C++.

Of course, I haven't done any actual measurements, so I could be completely wrong, but I would be happy to run some benchmarks if you're skeptical.

instantapples
Автор

Very nice. I have been thinking about leaving C++ and use pure C my self. Any plans to share your meta tools on your Github?

CombatCoder
Автор

Also: can you upload this code to GitHub and attach the link to this video?

Thanks

blugaming
Автор

How well does your parser parse declarations like these:
void (*update)(float dt);
const void* const* data;

Fantastic system btw, I have something strikingly similar for my C code.

dragoner
Автор

Amazing work, thank you very much for sharing!!

blugaming
Автор

Why not then just implement your own language that is compiled to C? It would be easier, because you don't need to parse C code then. And also faster, because you don't need to do the preprocessing, which is, as far as I can tell, the slowest part of compilation. I was thinking to do something like this, but it is problematic to make your own language. And metaprogramming by hand doesn't solve those problems. You will still need tools and other stuff like that.

ВасилийИванов-пе