0.5.0 -- tuples and more optimizations #12

Merged
dbidwell merged 34 commits from 43-tuple-return into master 2025-12-31 17:03:51 -07:00
14 changed files with 173 additions and 14 deletions
Showing only changes of commit 964ad92077 - Show all commits

View File

@@ -172,4 +172,37 @@ mod tests {
let output = compile_with_and_without_optimization(source);
insta::assert_snapshot!(output);
}
#[test]
fn test_tuples() {
let source = indoc! {r#"
device self = "db";
device day = "d0";
fn getSomethingElse(input) {
return input + 1;
}
fn getSensorData() {
return (
day.Vertical,
day.Horizontal,
getSomethingElse(3)
);
}
loop {
yield();
let (vertical, horizontal, _) = getSensorData();
(horizontal, _, _) = getSensorData();
self.Setting = horizontal;
}
"#};
let output = compile_with_and_without_optimization(source);
insta::assert_snapshot!(output);
}
}

View File

@@ -12,7 +12,6 @@ move r9 r1
## Optimized Output
j 1
move r8 5
move r1 5
move r9 5

View File

@@ -43,7 +43,6 @@ move r13 r3
add r4 r11 r12
add r5 r4 r13
move r15 r5
j 16
pop ra
pop sp
j ra

View File

@@ -10,5 +10,4 @@ move r8 15
## Optimized Output
j 1
move r8 15

View File

@@ -26,7 +26,6 @@ push sp
push ra
add r1 r8 1
move r15 r1
j 7
pop ra
pop sp
j ra

View File

@@ -38,7 +38,6 @@ push sp
push ra
add r1 r9 r8
move r15 r1
j 8
pop ra
pop sp
j ra

View File

@@ -19,4 +19,5 @@ j ra
## Optimized Output
j main
pop r8
j ra

View File

@@ -68,16 +68,15 @@ push sp
push ra
add r1 r9 r8
move r15 r1
j 8
pop ra
pop sp
j ra
pop r8
pop r9
push sp
push ra
add r1 r9 r9
move r15 r1
j 17
pop ra
pop sp
j ra
@@ -99,9 +98,11 @@ push r10
push r10
push 2
jal 11
pop r10
pop r9
pop r8
move r11 r15
move r15 r11
j 41
pop ra
pop sp
j ra

View File

@@ -31,7 +31,6 @@ push sp
push ra
select r9 r8 10 20
move r15 r9
j 7
pop ra
pop sp
j ra

View File

@@ -25,7 +25,6 @@ push sp
push ra
add r1 r8 r8
move r15 r1
j 7
pop ra
pop sp
j ra

View File

@@ -0,0 +1,94 @@
---
source: libs/integration_tests/src/lib.rs
expression: output
---
## Unoptimized Output
j main
getSomethingElse:
pop r8
push sp
push ra
add r1 r8 1
move r15 r1
j __internal_L1
__internal_L1:
pop ra
pop sp
j ra
getSensorData:
push sp
push ra
l r1 d0 Vertical
push r1
l r2 d0 Horizontal
push r2
push 3
jal getSomethingElse
move r3 r15
push r3
sub r0 sp 5
get r0 db r0
move r15 r0
j __internal_L2
__internal_L2:
sub r0 sp 4
get ra db r0
j ra
main:
__internal_L3:
yield
jal getSensorData
pop r0
pop r9
pop r8
move sp r15
jal getSensorData
pop r0
pop r0
pop r9
move sp r15
s db Setting r9
j __internal_L3
__internal_L4:
## Optimized Output
j 27
pop r8
push sp
push ra
add r1 r8 1
move r15 r1
pop ra
pop sp
j ra
push sp
push ra
l r1 d0 Vertical
push r1
l r2 d0 Horizontal
push r2
push 3
jal 1
move r3 r15
push r3
sub r0 sp 5
get r0 db r0
move r15 r0
sub r0 sp 4
get ra db r0
j ra
yield
jal 10
pop r0
pop r9
pop r8
move sp r15
jal 10
pop r0
pop r0
pop r9
move sp r15
s db Setting r9
j 27

View File

@@ -1,4 +1,4 @@
use il::{Instruction, InstructionNode};
use il::{Instruction, InstructionNode, Operand};
/// Pass: Redundant Move Elimination
/// Removes moves where source and destination are the same: `move rx rx`
@@ -43,6 +43,31 @@ pub fn remove_unreachable_code<'a>(
(output, changed)
}
/// Pass: Remove Redundant Jumps
/// Removes jumps to the next instruction (after label resolution).
/// Must run AFTER label resolution since it needs line numbers.
pub fn remove_redundant_jumps<'a>(
input: Vec<InstructionNode<'a>>,
) -> (Vec<InstructionNode<'a>>, bool) {
let mut output = Vec::with_capacity(input.len());
let mut changed = false;
for (i, node) in input.iter().enumerate() {
// Check if this is a jump to the next line number
if let Instruction::Jump(Operand::Number(target)) = &node.instruction {
// Current line number is i, next line number is i+1
// If jump target equals the next line, it's redundant
if target.to_string().parse::<usize>().ok() == Some(i + 1) {
changed = true;
continue; // Skip this redundant jump
}
}
output.push(node.clone());
}
(output, changed)
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -13,13 +13,20 @@ pub fn dead_store_elimination<'a>(
// Scan for dead writes
for (i, node) in input.iter().enumerate() {
// Never remove Pop instructions - they have critical side effects on the stack pointer
if matches!(node.instruction, Instruction::Pop(_)) {
continue;
}
if let Some(dest_reg) = get_destination_reg(&node.instruction) {
// If this register was written before and hasn't been read, previous write is dead
if let Some(&prev_idx) = last_write.get(&dest_reg) {
// Check if the value was ever used between prev_idx and current
let was_used = input[prev_idx + 1..i]
.iter()
.any(|n| reg_is_read_or_affects_control(&n.instruction, dest_reg));
.any(|n| reg_is_read_or_affects_control(&n.instruction, dest_reg))
// Also check if current instruction reads the register before overwriting it
|| reg_is_read_or_affects_control(&node.instruction, dest_reg);
if !was_used {
// Previous write was dead

View File

@@ -17,7 +17,7 @@ mod strength_reduction;
use algebraic_simplification::algebraic_simplification;
use constant_propagation::constant_propagation;
use dead_code::{remove_redundant_moves, remove_unreachable_code};
use dead_code::{remove_redundant_jumps, remove_redundant_moves, remove_unreachable_code};
use dead_store_elimination::dead_store_elimination;
use function_call_optimization::optimize_function_calls;
use label_resolution::resolve_labels;
@@ -91,5 +91,10 @@ pub fn optimize<'a>(instructions: Instructions<'a>) -> Instructions<'a> {
}
// Final Pass: Resolve Labels to Line Numbers
Instructions::new(resolve_labels(instructions))
let mut instructions = resolve_labels(instructions);
// Post-resolution Pass: Remove redundant jumps (must run after label resolution)
let (instructions, _) = remove_redundant_jumps(instructions);
Instructions::new(instructions)
}