Generate C++ style code using LLVM

369 views Asked by At

I am looking into making a custom language for fun, mostly to learn how it works, but I am having a bit of trouble with concepts before I dig into code.

I have looked at Kaleidoscope example code and many other online resources, but I am confused on how to do a couple things:

My Goal Translate my code into C++ code OR directly into a machine code with C++ style AST

Reason Mostly to learn, but it would be great if I get this going well enough I may develop it further.

What is my Language? My language is going to be specific to sql and database creation with emphasis on version control and caching strategies.

My problem I am unsure of how to translate some of the information in my "language" to the C++ equivalent.

Example:

//An Integer type which is nullable and the default value of null.
int number = nullable;

Would translate to something like this...

public static sqlInt number = new sqlInt(true, null);

The problem I am having is, how would I generate the AST and LLVM code generation to recognize the "nullable" field as a new sqlInt without explicitly writing it out? And this would need to work for more complex types:

Example 2:

//Create a foreign key which is of type int, points to pkID
//forces reference and holds ZeroToMany records for a single pkID
//It is also nullable with a default value of 0.
FK<int>(tableName.pkID, true, ZTM) fk1 = nullable(0);

Would translate to something like this:

public static FK<sqlInt> fk1 = new FK<sqlInt>(tableName.pkID, true, 
                                              ZTM, true, 0);

The question remains, would I have to build the AST special? if so what would I have to do to make this possible? Or would this be specific to LLVM?

I can't seem to find an example of a llvm language similar to this style.

I don't have any actual code as of now, I am simply gathering information and I can't seem to figure this part out from the code I have looked at.

Edit I understand (mostly) how to make a parser and lexer to find the function and assign it to a variable, but I am unsure when I should derive the function to declare the sqlInt and how to find the correct params...etc. Is this during the code-generation after the LLVM IR? should I account for this before the LLVM IR?

1

There are 1 answers

3
Colin LeMahieu On

If you're using LLVM you're going to want to translate from your language to LLVM IR rather than to the c++ ast.

The process of going from the text of your source language to IR is lexing, parsing, semantic analysis, and lowering.