-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use ygen::prelude::*; | ||
|
||
#[test] | ||
pub fn ir_optimization() { | ||
let mut module = Module(); | ||
|
||
let mut builder = IRBuilder(); | ||
|
||
let other = module.add("cfunc", &FnTy(vec![TypeMetadata::i32, TypeMetadata::i32], TypeMetadata::i32)); | ||
other.import(); | ||
let other = other.clone(); | ||
|
||
let ty = FnTy(vec![TypeMetadata::i32, TypeMetadata::i32], TypeMetadata::i32); | ||
|
||
let func = module.add( | ||
"add", &ty | ||
); | ||
|
||
func.extrn(); | ||
|
||
let entry = func.addBlock("entry"); | ||
builder.positionAtEnd(entry); | ||
|
||
let val = builder.BuildCall( &other, vec![ty.arg(0), ty.arg(1)] ); | ||
let val = builder.BuildAdd(val, ty.arg(0)); | ||
|
||
builder.BuildRet( val ); | ||
|
||
//assert_eq!(module.dump(), "define i32 @add(i32 %0, i32 %1) {\n entry:\n\t%2 = call i32 cfunc i32 %0 i32 %1 \n\tadd = %3 i32 %2, %0\n\tret i32 %3\n\n}\ndeclare i32 @cfunc(i32 %0, i32 %1)\n\n".to_string()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use ygen::{Optimizations::auto_max_optimize, Target::{instr::*, x64Reg, Reg}}; | ||
|
||
#[test] | ||
pub fn test_mov() { | ||
let instr = Instr::with2( | ||
Mnemonic::Mov, | ||
Operand::Reg(x64Reg::Rcx.boxed()), | ||
Operand::Mem(MemOp { base: Some(x64Reg::R15.boxed()), index: None, scale: 1, displ: 5, rip: false }) | ||
); | ||
|
||
assert_eq!(instr.encode(), Ok((vec![0x49, 0x8B, 0x4F, 0x05], None))); | ||
|
||
let instr = Instr::with2( | ||
Mnemonic::Mov, | ||
Operand::Reg(x64Reg::R12b.boxed()), | ||
Operand::Imm(12) | ||
); | ||
|
||
assert_eq!(instr.encode(), Ok((vec![0x41, 0xC6, 0xC4, 0x0C], None))); | ||
} | ||
|
||
#[test] | ||
pub fn test_ret() { | ||
let instr = Instr::with0(Mnemonic::Ret); | ||
|
||
assert_eq!(instr.encode(), Ok((vec![0xC3], None))); | ||
} | ||
|
||
#[test] | ||
pub fn test_optimization() { | ||
let mut instrs = vec![ | ||
Instr::with2(Mnemonic::Mov, Operand::Reg(x64Reg::Rax.boxed()), Operand::Reg(x64Reg::Rcx.boxed())), | ||
Instr::with2(Mnemonic::Add, Operand::Reg(x64Reg::Rax.boxed()), Operand::Reg(x64Reg::Rdx.boxed())), | ||
Instr::with2(Mnemonic::Mov, Operand::Reg(x64Reg::Rcx.boxed()), Operand::Reg(x64Reg::Rax.boxed())), | ||
]; | ||
|
||
let expected_optimized = vec![ | ||
Instr::with2(Mnemonic::Lea, Operand::Reg(x64Reg::Rax.boxed()), Operand::Mem(x64Reg::Rcx + x64Reg::Rdx)), | ||
Instr::with2(Mnemonic::Mov, Operand::Reg(x64Reg::Rcx.boxed()), Operand::Reg(x64Reg::Rax.boxed())), | ||
]; | ||
|
||
auto_max_optimize(&mut instrs); | ||
|
||
assert_eq!(instrs, expected_optimized); | ||
} |