Search code examples
compiler-constructionllvm

LLVM For a Recipe-style language


I want to learn LLVM. And I want to try to use the following cookbook style language to do so:

Ingredients:
    x-unit Flour
    y-unit Yeast
    z-unit Water
    a-unit Sugar

Instructions:
    bowl = mix flour with yeast for 30 seconds
    bowl = mix bowl with sugar for 25 seconds
    bowl = mix bowl with water for 1 minute
    heat bowl for 3 hours
    dough = split bowl into 3 parts
    heat dough[n] at 450 f for 1 hour

As it's a learning experience, I'm not sure if LLVM would be capable of handling a language where time/volume are dimensions that can be computed upon. And after reading through some of the documentation, I'm not entirely sure if this is the right tool for what I want to do.

I would like to take this language, compile it, and transform it to valid C/Python code so I can use a RPi or Arduino to execute the program. Essentially I want to attempt to automate basic recipes by expressing them in this language and run a small kitchen from this.

To posit the question a different way: How does LLVM handle time-specific instructions. So how would I express in LLVM IR the instruction mix flour with yeast for 30 seconds?


Solution

  • I'm not sure if LLVM would be capable of handling a language where time/volume are dimensions that can be computed upon.

    In general you can do anything in LLVM that you can do in C. After all clang compiles C to LLVM.

    I would like to take this language, compile it, and transform it to valid C/Python code

    If you want to generate C and/or Python, just do that. You go through LLVM if the target is machine code/assembly. LLVM does not help you in generating C or Python code.

    How does LLVM handle time-specific instructions

    You just call the corresponding OS or libc functions (or the equivalent on your embedded platform) - just like you would in C. For example to wait 30 seconds on a POSIX system, you'd generate a call to sleep:

    call i32 (i32) @sleep(i32 30)
    

    You'd also have to generate a declaration for the function:

    declare i32 @sleep(i32)
    

    So basically you do the exact same thing you'd do in a hand-written C program, except with LLVM's syntax for function calls and declarations instead of C's.