LLVM C++ To UDON Compiler

Hello,
I am implementing a compiler that generates UDON Assembly from C++ (LLVM).
I can just run this program, but a few lines of code generate a huge uasm.
The reason why the generated code becomes huge is that I represent memory as a ByteArray on UDON and call System.Convert and bit operations many times to read and write it.
If UDON has additional instructions to read and write ByteArray directly, the generated code can be shortened.
And this can only handle unsigned values now, but if UDON can read and write signed values and floating point numbers from a ByteArray, this can also support them.
code
result

Thank you.

6 Likes

If UDON were added instructions to read and write primitive types directly from ByteArray, the generated code could be reduced from 277KB to 29KB.

3 Likes

NICE!

I was wondering if this could work and went to the forums just to look for this.
Now if only this was available in C# lol

1 Like

This supported dynamic memory allocation.
However, signed values ​​or floating point values are not yet available.
And the resulting assembly is too large!

Do you have a basic tutorial on how to make your own compiler? The one provided here doesn’t leave much room to go off of.

sorry, I don’t have any tutorials or documents.
However, it is possible to implement compiler with current UDON.
I will answer if you tell me what part you think is difficult to implement.

I think how to start would be nice. I don’t know how where to start or have a good example. I’m just digging through the scripts to see how things are converted.

1 Like

I’m investigating this stuff myself, for compiler you’ll need lexer (source file => tokens), parser (tokens => syntax/parse tree) and translator (tree => udon assembly code). What for translator, i’m sure it’s not super hard thing to figure out by your own. For lexer and parser i’ve seen alot of information just by googling it, but for now i’m digging into antlr4 - it’s parser generator written in java, but can generate parser code that will run by c#. Language can be any - you describe it by special grammar rules file. There are many already written grammars for different languages on it (including c# 6). Another option is to use roslyn (it generates syntax tree from source code) and use it to translate c# to udon. Bad thing is that you need to fully understand syntax and rules which is used by language grammar to be able to make translator from it (as i see)

1 Like