Cr0a3 / ygen

Ygen - Yet another code generation libary
https://ygen.vercel.app/
Apache License 2.0
85 stars 3 forks source link
codegen compiler llvm

Ygen - Yet another Code Generator

GitHub branch check runs Crates.io Version GitHub Repo stars

Welcome to Ygen! This repository contains the source code of the ygen project.

Ygen is a toolkit for building modern compilers, using a llvm like api.

Why ygen?

You are probably wondering: Why would I choose ygen and not llvm? Here are a few reasons:

[!WARNING] This project is still early in its developement. Bugs and miscompilations are expected.
ONLY USE YGEN FOR TOY COMPILERS

Contributions

Contribution activity

Simple example

Here is a simple example on how to use Ygen to build an add function:

use std::error::Error;
use Ygen::prelude::*;

pub fn main() -> Result<(), Box<dyn Error>> {
    let mut module = Module();

    let ty = FnTy(vec![TypeMetadata::i32, TypeMetadata::i32], TypeMetadata::i32);

    let func = module.add(
        "add", &ty
    );

    func.extrn();

    func.addBlock("entry");

    let val = func.BuildAdd(ty.arg(0), ty.arg(1));

    func.BuildRet( val );

    module.verify()?;

    // prints out the ir of the module
    println!("{}", module.dump());

    let triple = Triple::host();

    // compiles the module in the host assembly and saves it in the specified path
    module.emitToAsmFile(
        triple,
        &mut initializeAllTargets(triple)?,
        Path::new("out.asm")
    )?;

    // compiles the module to a host object file
    module
        .emitMachineCode(
            triple, 
            &mut initializeAllTargets(triple)?,
            false // is debugging metadata enabled
        )?.0.emit(
            OpenOptions::new().write(true).truncate(true).create(true).open("out.o")?, 
            None // if debugging metadata is enabled here is the outputed metadata
    )?;

    Ok(())
}

When executed this simple program builds an add function and dumps it's ir:

define i32 @add( i32 %0,  i32 %1 ) {
 entry:
    %2 = add i32 %0, %1
    ret i32 %2
}

Support

Ygen currently supports following architectures:

Copyright

This project is owned by Cr0a3 and licensed under the Apache2 License