Elements is one of the most impressive things I've ever seen! Some technical curiosities

I found Elements by accident today during a google search today.

Thought I must be misinterpreting the homepage claims at first – my jaw was on the floor after reading the documentation briefly.

(Cross-language interop is really fascinating to me. In my spare time I have been studying + working on some LLVM-based tools that do codegen for bindings/transpilation from C++ headers to a few other languages, and experimenting with JIT-interpreted interop. In this same vein, GraalVM/Sulong is a marvel to me.)

I am curious, if it’s not too much of a secret – how is it that Elements is possible? Is it that you’ve written LLVM frontends for all of these languages, and when you compile an Elements application, it’s turned into homogenous LLVM bitcode so that it’s all transparent interop and there’s no marshalling/serialization cost?

Whatever it is, it’s absolutely brilliant and without a doubt one of the coolest things I’ve ever seen! :sweat_smile:

The extensions you have added to the languages are also brilliant. I especially love Contracts – D and Ada have them and in other languages they are sorely missed. The integration of LINQ-like queryable types is fantastic as well.

One the one hand, it’s a bit of a shame that the platform isn’t OSS so that there is widespread adoption of these enhanced language features (your Pascal implementation is something to give most modern languages a run for their money IMO) but in the same sense, I get that the compiler toolchain and the platform are worth every penny.

I had a few final short questions if I could ask them:

  1. I noticed from your Github that you seem to use Pascal for the majority of your codebase. Curious whether this is because of some specific reasons, or more generally just because it reads like English.

  2. With the free community Swift compiler, is it possible to build shared/static libs that export a C API for FFI with other languages, and to control the layout of things like structs? IE if someone wanted to model the vtbl of a C++ inherited object, and manually fill out the function pointers in Swift so that it could be used in Swift ↔ C++ interop?

  3. How would someone who was interested in learning how to build (or just a solid understanding of) something like Elements go about it? Are there any particular recommendations you have, or topics?

Thank you ! =)

Gavin Ray

3 Likes

Hi Gavin,

Essentially, yes. At a higher level than LLVM though, as the LLVM back=end s only used for the native platforms, not for .NET and Java. This blog post goes into a bit more detail: Mixing Languages.

i’m happy to hear that, much appreciated!

:pray:t3:. I like to think so, yeah :wink:

It’s mostly because most of us come from a Pascal background (Oxygene was the first language n Elements, all the way back in 2004), and we really like the language. The two main big bodies of code we have that aren’t Pascal are Fire/Water (I used C# when I started it, to dog-food our then-new C# front-end on Cocoa), and CodeGen4 (use Swift, for the same reason).

Yes. I’ll have to let my colleague go into the details about struct layout, but in general, you can enable the “Generate .h File” option on any static library project to get a C header file consumable form C, Objective-C and C++. Yo can also do the reverse — import a C/Objective-C library you have a .h file for (or really any native library, no m atter what language it was implemented it, as long as the header is C/Objective-C. (check Import Projects for more on this)

Notye thnayt we cannot import C++ objects at its stage, as those are not compatible with Elements, and C++'s object model and ABI is inherently too complex (and to different, between compilers) to make that feasible.

You mean built something with Elements, or something like Elements? :wink:

Thanx for the kind words, much appreciated. Please help spread the word!

—marc

3 Likes

At a higher level than LLVM though, as the LLVM back=end s only used for the native platforms, not for .NET and Java. This blog post goes into a bit more detail: Mixing Languages.

Oh thank you! Wow, this post and the entire blog are full of gems it seems. I’ll have reading materials for some while, fantastic stuff!

Note that we cannot import C++ objects at its stage, as those are not compatible with Elements, and C++'s object model and ABI is inherently too complex (and to different, between compilers) to make that feasible.

Yeah that’s reasonable – the only languages that I know that can do this are D and Ada, and it’s only because they have specific extern annotations that tell the compiler to match the layout and calling conventions of C++.

I’m sure you know, but Swift has an -enable-experimental-cxx-interop flag that makes it the only language I’ve ever heard of that is able to import C++ objects directly, without writing bindings in the host language. It just requires a .modulemap of the C++ project. I think it works by doing a mixed compilation of the C++ and Swift into single LLVM objects?

I think this is really exciting :eyes:

The only other way I’ve seen C++ interop done is by doing a C ABI from C++ with manual vtbl’s like below. I have been trying to study this area lately to get a better understanding :sweat_smile:

class FUnknown {
    tresult queryInterface(void*, const TUID, void**);
    uint32* addRef(void *);
    uint32* release(void *);
};
class IComponentHandler : FUnknown {
    tresult beginEdit(void *, ParamID);
    tresult performEdit(void *, ParamID, ParamValue);
    tresult endEdit(void *, ParamID);
    tresult restartComponent(void *, int32);
};

#ifdef __cplusplus
extern "C" {
#endif
typedef struct FUnknownVTable {
    tresult (*queryInterface)(void *, const TUID, void **);
    uint32 (*addRef)(void *);
    uint32 (*release)(void *);
} FUnknownVTable;

typedef struct SFUnknown {
    FUnknownVTable *vtable;
} SFUnknown;

typedef struct IComponentHandlerVTable {
    FUnknownVTable FUnknown;
    tresult (*beginEdit)(void *, ParamID);
    tresult (*performEdit)(void *, ParamID, ParamValue);
    tresult (*endEdit)(void *, ParamID);
    tresult (*restartComponent)(void *, int32);
} IComponentHandlerVTable;

typedef struct SIComponentHandler {
    IComponentHandlerVTable *vtable;
} SIComponentHandler;
#ifdef __cplusplus
}
#endif

You mean built something with Elements, or something like Elements? :wink:

Like Elements! :smiley:

I am interested in compiler architecture, specifically fascinated by interopability between languages or mixed-language code.

I have been going through the LLVM Kaleidoscope tutorial, and reading materials to better understand things like vtables and the common ABI’s.

Thanx for the kind words, much appreciated. Please help spread the word!

Absolutely =)