https://github.com/cr0a3/ygen
Ygen - Yet another code generation libary (abandoned; gained two internships from it)
https://github.com/cr0a3/ygen
codegen compiler llvm
Last synced: 12 months ago
JSON representation
Ygen - Yet another code generation libary (abandoned; gained two internships from it)
- Host: GitHub
- URL: https://github.com/cr0a3/ygen
- Owner: Cr0a3
- License: apache-2.0
- Created: 2024-07-08T14:29:45.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-02T13:48:33.000Z (12 months ago)
- Last Synced: 2025-04-06T07:14:52.657Z (12 months ago)
- Topics: codegen, compiler, llvm
- Language: Rust
- Homepage: https://ygen.vercel.app/
- Size: 7.11 MB
- Stars: 107
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Ygen - Yet another Code Generator



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:
- **Simplicity**: One of ygens main focus is simplicity which means to us that as much code as possible is readable and shared
- **Similare API**: Ygens API is very similar to LLVMs API. For example all function names for building ir are nearly the safe as llvms.
- **Simple start**: You can easily start with ygen. You do not need to install any dlls, or build it. Ygen also has many simple examples.
> [!WARNING]
> This project is still early in its developement. Bugs and miscompilations are expected.
> ONLY USE YGEN FOR TOY COMPILERS
### Contributions

### Simple example
Here is a simple example on how to use Ygen to build an add function:
```rust
use std::error::Error;
use Ygen::prelude::*;
pub fn main() -> Result<(), Box> {
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:
```LLVM
define i32 @add( i32 %0, i32 %1 ) {
entry:
%2 = add i32 %0, %1
ret i32 %2
}
```
### Copyright
This project is owned by Cr0a3 and licensed under the Apache2 License