nlvm
nlvm copied to clipboard
could clang library be used to handle `emit` / C code embedded in nim?
for example, for Calypso (cf my other post https://github.com/nim-lang/Nim/issues/8327), IIRC, when ldc(calypso produced binary) encounters a C++ import such as modmap (C++) "OGRE/SdkCameraMan.h";(eg
https://github.com/Syniurge/Calypso/blob/bc928fbd0226cd3ab39053fc3f12d3c2178f1194/examples/ogre3d/demo.d#L14) it uses clang library (linked in the binary) to produce LLVM IR from it, I believe it's handled here: https://github.com/Syniurge/Calypso/blob/master/ddmd/cpp/calypso.h
(maybe @Syniurge can confirm)
By the work, really curious what you think of https://github.com/nim-lang/Nim/issues/8327 ; this could be a killer application for your project
well, it's not that it's impossible, but I expect it to be a quite the undertaking.. the issue with wrapping c++ is that you need to support the whole c++ abi - vtables, multiple inheritance, exceptions etc etc - using clang, you at least get away with supporting only one ABI which is an improvement, but there's still a lot to do - C++ has a lot of corner cases which is why nobody's bothered or succeeded in wrapping it successfully so far (ie see Calypso claim: "almost entire spectrum of C++ features"), and C remains the de-facto common denominator for wrappers (even llvm provides C bindings, that nlvm uses!)
If you want to experiment with this, it's fairly simple - both clang and nlvm can emit llvm bytecode (same as would happen if you were to link in clang library into nlvm), then you can use llvm linking tools to link the final executable into a binary, probably with some appropriate glue code - curious to hear how it goes!
thanks for the reply; I'll answer later but just wanted to mention another very relevant and recent project: https://github.com/atilaneves/dpp/
the skinny:
- they use clang to parse C++ code embedded in a D file (well, mostly C for now but C++ planned), and map declarations to D:
d++ goes through the input file line-by-line, and upon encountering an #include directive, parses the file to be included with libclang, loops over the definitions of data structures and functions therein and expands in-place the relevant D translations.
- see their "several "real-life" C headers" list, it's not bad
the issue with wrapping c++ is that you need to support the whole c++ abi - vtables, multiple inheritance, exceptions etc etc
yes and no: you can still have a usable tool even on a complex code base that starts with mapping the simplest declarations and ignore the rest it doesn't support yet. That's still very useful and (IIUC) could even work on complex stuff like opencv: the tool would just map what it can (and the list would grow over time). The preprocessing/parsing step would be completely handled by libclang.
- will address your other points later