Skip to content

Commit

Permalink
[FIX] fixed example from the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Oct 29, 2024
1 parent 3671d1b commit df3eb9d
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
Welcome to Ygen!
This repository contains the source code of the ygen project.

Ygen is a toolkit for building fast and clean compilers using a memory safe api.
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 or cranelift??
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 i designed the `IRBuilder` to be very similar to the `Builder` from LLVM
- **Traits**: Ygen uses a lot of traits to overload functions. Great examples are the `Build...` functions from the `IRBuilder` to build ir nodes
- **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. <br>
Expand All @@ -35,26 +35,44 @@ use Ygen::prelude::*;
pub fn main() -> Result<(), Box<dyn Error>> {
let mut module = Module();

let mut builder = IRBuilder();

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

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

func.extrn(); // make function externally visible
func.extrn();

let entry = func.addBlock("entry");
builder.positionAtEnd(entry);
func.addBlock("entry");

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

func.BuildRet( val );

module.verify().print();
module.verify()?;

eprintln!("{}",
module.dumpColored()
);
// 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).create(true).open("out.o")?,
None // if debugging metadata is enabled here is the outputed metadata
)?;

Ok(())
}
Expand All @@ -68,20 +86,10 @@ define i32 @add( i32 %0, i32 %1 ) {
}
```

You can add following lines (you need to include `std::fs::Path`) to compile the IR down to assembly:
```Rust
module.emitToAsmFile(
Triple::host(),
&mut initializeAllTargets(Triple::host())?,
Path::new("out.asm")
)?;
```

### Support
Ygen currently supports following architectures
|Name |Full ir |Full isa|
|--------|--------|--------|
| x64 | <b style="color:green">Yes</b> | <b style="color:red">No</b>|
Ygen currently supports following architectures:
- `x86-64`
- `wasm` [WIP]

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

0 comments on commit df3eb9d

Please sign in to comment.