Emit IL alongside raw IC10 for use in future optimization passes

This commit is contained in:
2025-12-12 15:51:36 -07:00
parent 1230f83951
commit 3fb04aef3b
23 changed files with 990 additions and 523 deletions

View File

@@ -8,6 +8,7 @@ thiserror = { workspace = true }
parser = { path = "../parser" }
tokenizer = { path = "../tokenizer" }
helpers = { path = "../helpers" }
il = { path = "../il" }
lsp-types = { workspace = true }
rust_decimal = { workspace = true }

View File

@@ -18,7 +18,7 @@ fn simple_binary_expression() -> Result<()> {
"
j main
main:
move r8 3 #i
move r8 3
"
}
);
@@ -45,9 +45,9 @@ fn nested_binary_expressions() -> Result<()> {
"
j main
calculateArgs:
pop r8 #arg3
pop r9 #arg2
pop r10 #arg1
pop r8
pop r9
pop r10
push ra
add r1 r10 r9
mul r2 r1 r8
@@ -63,9 +63,9 @@ fn nested_binary_expressions() -> Result<()> {
push 20
push 30
jal calculateArgs
move r1 r15 #__binary_temp_3
move r1 r15
add r2 r1 100
move r8 r2 #returned
move r8 r2
"
}
);
@@ -88,7 +88,7 @@ fn stress_test_constant_folding() -> Result<()> {
"
j main
main:
move r8 -123 #negationHell
move r8 -123
"
}
);
@@ -116,7 +116,7 @@ fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
mul r2 373.2 r1
sub r3 1 r2
add r4 r3 518.15
move r8 r4 #i
move r8 r4
"
}
);
@@ -141,7 +141,7 @@ fn test_ternary_expression() -> Result<()> {
main:
sgt r1 1 2
select r2 r1 15 20
move r8 r2 #i
move r8 r2
"
}
);
@@ -165,10 +165,10 @@ fn test_ternary_expression_assignment() -> Result<()> {
"
j main
main:
move r8 0 #i
move r8 0
sgt r1 1 2
select r2 r1 15 20
move r8 r2 #i
move r8 r2
"
}
);

View File

@@ -20,10 +20,10 @@ fn test_if_statement() -> anyhow::Result<()> {
"
j main
main:
move r8 10 #a
move r8 10
sgt r1 r8 5
beq r1 0 L1
move r8 20 #a
beqz r1 L1
move r8 20
L1:
"
}
@@ -52,13 +52,13 @@ fn test_if_else_statement() -> anyhow::Result<()> {
"
j main
main:
move r8 0 #a
move r8 0
sgt r1 10 5
beq r1 0 L2
move r8 1 #a
beqz r1 L2
move r8 1
j L1
L2:
move r8 2 #a
move r8 2
L1:
"
}
@@ -89,18 +89,18 @@ fn test_if_else_if_statement() -> anyhow::Result<()> {
"
j main
main:
move r8 0 #a
move r8 0
seq r1 r8 1
beq r1 0 L2
move r8 10 #a
beqz r1 L2
move r8 10
j L1
L2:
seq r2 r8 2
beq r2 0 L4
move r8 20 #a
beqz r2 L4
move r8 20
j L3
L4:
move r8 30 #a
move r8 30
L3:
L1:
"
@@ -136,18 +136,18 @@ fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> {
"
j main
main:
move r8 1 #a
move r9 2 #b
move r10 3 #c
move r11 4 #d
move r12 5 #e
move r13 6 #f
move r14 7 #g
push 8 #h
move r8 1
move r9 2
move r10 3
move r11 4
move r12 5
move r13 6
move r14 7
push 8
seq r1 r8 1
beq r1 0 L1
beqz r1 L1
sub r0 sp 1
put db r0 99 #h
put db r0 99
L1:
sub sp sp 1
"

View File

@@ -24,7 +24,7 @@ fn no_arguments() -> anyhow::Result<()> {
j ra
main:
jal doSomething
move r8 r15 #i
move r8 r15
"
};
@@ -55,7 +55,7 @@ fn let_var_args() -> anyhow::Result<()> {
"
j main
mul2:
pop r8 #arg1
pop r8
push ra
mul r1 r8 2
move r15 r1
@@ -67,16 +67,16 @@ fn let_var_args() -> anyhow::Result<()> {
j ra
main:
L2:
move r8 123 #arg1
move r8 123
push r8
push r8
jal mul2
sub r0 sp 1
get r8 db r0
sub sp sp 1
move r9 r15 #i
move r9 r15
pow r1 r9 2
move r9 r1 #i
move r9 r1
j L2
L3:
"
@@ -123,10 +123,10 @@ fn inline_literal_args() -> anyhow::Result<()> {
"
j main
doSomething:
pop r8 #arg2
pop r9 #arg1
pop r8
pop r9
push ra
move r15 5 #returnValue
move r15 5
j L1
L1:
sub r0 sp 1
@@ -134,7 +134,7 @@ fn inline_literal_args() -> anyhow::Result<()> {
sub sp sp 1
j ra
main:
move r8 123 #thisVariableShouldStayInPlace
move r8 123
push r8
push 12
push 34
@@ -142,7 +142,7 @@ fn inline_literal_args() -> anyhow::Result<()> {
sub r0 sp 1
get r8 db r0
sub sp sp 1
move r9 r15 #returnedValue
move r9 r15
"
}
);
@@ -167,8 +167,8 @@ fn mixed_args() -> anyhow::Result<()> {
"
j main
doSomething:
pop r8 #arg2
pop r9 #arg1
pop r8
pop r9
push ra
L1:
sub r0 sp 1
@@ -176,7 +176,7 @@ fn mixed_args() -> anyhow::Result<()> {
sub sp sp 1
j ra
main:
move r8 123 #arg1
move r8 123
push r8
push r8
push 456
@@ -184,7 +184,7 @@ fn mixed_args() -> anyhow::Result<()> {
sub r0 sp 1
get r8 db r0
sub sp sp 1
move r9 r15 #returnValue
move r9 r15
"
}
);
@@ -211,9 +211,9 @@ fn with_return_statement() -> anyhow::Result<()> {
"
j main
doSomething:
pop r8 #arg1
pop r8
push ra
move r15 456 #returnValue
move r15 456
j L1
L1:
sub r0 sp 1
@@ -223,7 +223,7 @@ fn with_return_statement() -> anyhow::Result<()> {
main:
push 123
jal doSomething
move r8 r15 #returned
move r8 r15
"
}
);
@@ -250,7 +250,7 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
j main
doSomething:
push ra
move r15 -1 #returnValue
move r15 -1
L1:
sub r0 sp 1
get ra db r0
@@ -258,7 +258,7 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
j ra
main:
jal doSomething
move r8 r15 #i
move r8 r15
"
}
);

View File

@@ -15,7 +15,7 @@ fn variable_declaration_numeric_literal() -> anyhow::Result<()> {
"
j main
main:
move r8 293.15 #i
move r8 293.15
"
}
);
@@ -46,16 +46,16 @@ fn variable_declaration_numeric_literal_stack_spillover() -> anyhow::Result<()>
"
j main
main:
move r8 0 #a
move r9 1 #b
move r10 2 #c
move r11 3 #d
move r12 4 #e
move r13 5 #f
move r14 6 #g
push 7 #h
push 8 #i
push 9 #j
move r8 0
move r9 1
move r10 2
move r11 3
move r12 4
move r13 5
move r14 6
push 7
push 8
push 9
sub sp sp 3
"
}
@@ -79,7 +79,7 @@ fn variable_declaration_negative() -> anyhow::Result<()> {
"
j main
main:
move r8 -1 #i
move r8 -1
"
}
);
@@ -103,8 +103,8 @@ fn test_boolean_declaration() -> anyhow::Result<()> {
"
j main
main:
move r8 1 #t
move r9 0 #f
move r8 1
move r9 0
"
}
);
@@ -132,7 +132,7 @@ fn test_boolean_return() -> anyhow::Result<()> {
j main
getTrue:
push ra
move r15 1 #returnValue
move r15 1
j L1
L1:
sub r0 sp 1
@@ -141,7 +141,7 @@ fn test_boolean_return() -> anyhow::Result<()> {
j ra
main:
jal getTrue
move r8 r15 #val
move r8 r15
"
}
);

View File

@@ -13,13 +13,13 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
indoc! {"
j main
doSomething:
pop r8 #arg9
pop r9 #arg8
pop r10 #arg7
pop r11 #arg6
pop r12 #arg5
pop r13 #arg4
pop r14 #arg3
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
push ra
L1:
sub r0 sp 1
@@ -54,10 +54,10 @@ fn test_early_return() -> anyhow::Result<()> {
doSomething:
push ra
seq r1 1 1
beq r1 0 L2
beqz r1 L2
j L1
L2:
move r8 3 #i
move r8 3
j L1
L1:
sub r0 sp 1
@@ -66,7 +66,7 @@ fn test_early_return() -> anyhow::Result<()> {
j ra
main:
jal doSomething
move r1 r15 #__binary_temp_2
move r1 r15
"
}
);
@@ -87,8 +87,8 @@ fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
indoc! {"
j main
doSomething:
pop r8 #arg2
pop r9 #arg1
pop r8
pop r9
push ra
L1:
sub r0 sp 1

View File

@@ -23,17 +23,17 @@ fn test_comparison_expressions() -> anyhow::Result<()> {
j main
main:
sgt r1 10 5
move r8 r1 #isGreater
move r8 r1
slt r2 5 10
move r9 r2 #isLess
move r9 r2
seq r3 5 5
move r10 r3 #isEqual
move r10 r3
sne r4 5 10
move r11 r4 #isNotEqual
move r11 r4
sge r5 10 10
move r12 r5 #isGreaterOrEqual
move r12 r5
sle r6 5 5
move r13 r6 #isLessOrEqual
move r13 r6
"
}
);
@@ -59,11 +59,11 @@ fn test_logical_and_or_not() -> anyhow::Result<()> {
j main
main:
and r1 1 1
move r8 r1 #logic1
move r8 r1
or r2 1 0
move r9 r2 #logic2
move r9 r2
seq r3 1 0
move r10 r3 #logic3
move r10 r3
"
}
);
@@ -89,7 +89,7 @@ fn test_complex_logic() -> anyhow::Result<()> {
sgt r1 10 5
slt r2 5 10
and r3 r1 r2
move r8 r3 #logic
move r8 r3
"
}
);
@@ -113,7 +113,7 @@ fn test_math_with_logic() -> anyhow::Result<()> {
j main
main:
sgt r1 3 1
move r8 r1 #logic
move r8 r1
"
}
);
@@ -137,7 +137,7 @@ fn test_boolean_in_logic() -> anyhow::Result<()> {
j main
main:
and r1 1 0
move r8 r1 #res
move r8 r1
"
}
);
@@ -163,11 +163,11 @@ fn test_invert_a_boolean() -> anyhow::Result<()> {
"
j main
main:
move r8 1 #i
move r8 1
seq r1 r8 0
move r9 r1 #y
move r9 r1
seq r2 r9 0
move r10 r2 #result
move r10 r2
"
}
);

View File

@@ -21,10 +21,10 @@ fn test_infinite_loop() -> anyhow::Result<()> {
"
j main
main:
move r8 0 #a
move r8 0
L1:
add r1 r8 1
move r8 r1 #a
move r8 r1
j L1
L2:
"
@@ -56,12 +56,12 @@ fn test_loop_break() -> anyhow::Result<()> {
"
j main
main:
move r8 0 #a
move r8 0
L1:
add r1 r8 1
move r8 r1 #a
move r8 r1
sgt r2 r8 10
beq r2 0 L3
beqz r2 L3
j L2
L3:
j L1
@@ -92,12 +92,12 @@ fn test_while_loop() -> anyhow::Result<()> {
"
j main
main:
move r8 0 #a
move r8 0
L1:
slt r1 r8 10
beq r1 0 L2
beqz r1 L2
add r2 r8 1
move r8 r2 #a
move r8 r2
j L1
L2:
"
@@ -130,12 +130,12 @@ fn test_loop_continue() -> anyhow::Result<()> {
"
j main
main:
move r8 0 #a
move r8 0
L1:
add r1 r8 1
move r8 r1 #a
move r8 r1
slt r2 r8 5
beq r2 0 L3
beqz r2 L3
j L1
L3:
j L2

View File

@@ -19,7 +19,7 @@ fn test_acos() -> Result<()> {
j main
main:
acos r15 123
move r8 r15 #i
move r8 r15
"
}
);
@@ -43,7 +43,7 @@ fn test_asin() -> Result<()> {
j main
main:
asin r15 123
move r8 r15 #i
move r8 r15
"
}
);
@@ -67,7 +67,7 @@ fn test_atan() -> Result<()> {
j main
main:
atan r15 123
move r8 r15 #i
move r8 r15
"
}
);
@@ -91,7 +91,7 @@ fn test_atan2() -> Result<()> {
j main
main:
atan2 r15 123 456
move r8 r15 #i
move r8 r15
"
}
);
@@ -115,7 +115,7 @@ fn test_abs() -> Result<()> {
j main
main:
abs r15 -123
move r8 r15 #i
move r8 r15
"
}
);
@@ -139,7 +139,7 @@ fn test_ceil() -> Result<()> {
j main
main:
ceil r15 123.90
move r8 r15 #i
move r8 r15
"
}
);
@@ -163,7 +163,7 @@ fn test_cos() -> Result<()> {
j main
main:
cos r15 123
move r8 r15 #i
move r8 r15
"
}
);
@@ -187,7 +187,7 @@ fn test_floor() -> Result<()> {
j main
main:
floor r15 123
move r8 r15 #i
move r8 r15
"
}
);
@@ -211,7 +211,7 @@ fn test_log() -> Result<()> {
j main
main:
log r15 123
move r8 r15 #i
move r8 r15
"
}
);
@@ -235,7 +235,7 @@ fn test_max() -> Result<()> {
j main
main:
max r15 123 456
move r8 r15 #i
move r8 r15
"
}
);
@@ -259,9 +259,9 @@ fn test_max_from_game() -> Result<()> {
"
j main
main:
move r8 0 #item
move r8 0
max r15 3 2
move r8 r15 #item
move r8 r15
"
}
);
@@ -285,7 +285,7 @@ fn test_min() -> Result<()> {
j main
main:
min r15 123 456
move r8 r15 #i
move r8 r15
"
}
);
@@ -309,7 +309,7 @@ fn test_rand() -> Result<()> {
j main
main:
rand r15
move r8 r15 #i
move r8 r15
"
}
);
@@ -333,7 +333,7 @@ fn test_sin() -> Result<()> {
j main
main:
sin r15 3
move r8 r15 #i
move r8 r15
"
}
);
@@ -357,7 +357,7 @@ fn test_sqrt() -> Result<()> {
j main
main:
sqrt r15 3
move r8 r15 #i
move r8 r15
"
}
);
@@ -381,7 +381,7 @@ fn test_tan() -> Result<()> {
j main
main:
tan r15 3
move r8 r15 #i
move r8 r15
"
}
);
@@ -405,7 +405,7 @@ fn test_trunc() -> Result<()> {
j main
main:
trunc r15 3.234
move r8 r15 #i
move r8 r15
"
}
);

View File

@@ -44,7 +44,7 @@ fn test_sleep() -> anyhow::Result<()> {
j main
main:
sleep 3
move r8 15 #sleepAmount
move r8 15
sleep r8
mul r1 r8 2
sleep r1
@@ -73,7 +73,7 @@ fn test_set_on_device() -> anyhow::Result<()> {
"
j main
main:
move r8 293.15 #internalTemp
move r8 293.15
sgt r1 r8 298.15
s d0 On r1
"
@@ -150,7 +150,7 @@ fn test_load_from_device() -> anyhow::Result<()> {
j main
main:
l r15 d0 On
move r8 r15 #setting
move r8 r15
"
}
);
@@ -176,7 +176,7 @@ fn test_load_from_slot() -> anyhow::Result<()> {
j main
main:
ls r15 d0 0 Occupied
move r8 r15 #setting
move r8 r15
"
}
);

File diff suppressed because it is too large Load Diff

View File

@@ -3,8 +3,9 @@
// r1 - r7 : Temporary Variables
// r8 - r14 : Persistant Variables
use helpers::Span;
use lsp_types::{Diagnostic, DiagnosticSeverity};
use parser::tree_node::{Literal, Span};
use parser::tree_node::Literal;
use std::{
borrow::Cow,
collections::{HashMap, VecDeque},

View File

@@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
crc32fast = { workspace = true }
lsp-types = { workspace = true }

View File

@@ -2,6 +2,44 @@ mod helper_funcs;
mod macros;
mod syscall;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub start_line: usize,
pub end_line: usize,
pub start_col: usize,
pub end_col: usize,
}
impl From<Span> for lsp_types::Range {
fn from(value: Span) -> Self {
Self {
start: lsp_types::Position {
line: value.start_line as u32,
character: value.start_col as u32,
},
end: lsp_types::Position {
line: value.end_line as u32,
character: value.end_col as u32,
},
}
}
}
impl From<&Span> for lsp_types::Range {
fn from(value: &Span) -> Self {
Self {
start: lsp_types::Position {
line: value.start_line as u32,
character: value.start_col as u32,
},
end: lsp_types::Position {
line: value.end_line as u32,
character: value.end_col as u32,
},
}
}
}
/// This trait will allow the LSP to emit documentation for various tokens and expressions.
/// You can easily create documentation for large enums with the `documented!` macro.
pub trait Documentation {

View File

@@ -0,0 +1,8 @@
[package]
name = "il"
version = "0.1.0"
edition = "2024"
[dependencies]
helpers = { path = "../helpers" }
rust_decimal = { workspace = true }

View File

@@ -0,0 +1,286 @@
use helpers::Span;
use rust_decimal::Decimal;
use std::borrow::Cow;
use std::fmt;
pub struct InstructionNode<'a> {
pub instruction: Instruction<'a>,
pub span: Option<Span>,
}
impl<'a> InstructionNode<'a> {
pub fn new(instr: Instruction<'a>, span: Option<Span>) -> Self {
Self {
span,
instruction: instr,
}
}
}
/// Represents the different types of operands available in IC10.
#[derive(Debug, Clone, PartialEq)]
pub enum Operand<'a> {
/// A hardware register (r0-r15)
Register(u8),
/// A device alias or direct connection (d0-d5, db)
Device(Cow<'a, str>),
/// A numeric literal (integer or float)
Number(Decimal),
/// A label used for jumping
Label(Cow<'a, str>),
/// A logic type string (e.g., "Temperature", "Open")
LogicType(Cow<'a, str>),
/// Special register: Stack Pointer
StackPointer,
/// Special register: Return Address
ReturnAddress,
}
impl<'a> fmt::Display for Operand<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Operand::Register(r) => write!(f, "r{}", r),
Operand::Device(d) => write!(f, "{}", d),
Operand::Number(n) => write!(f, "{}", n),
Operand::Label(l) => write!(f, "{}", l),
Operand::LogicType(t) => write!(f, "{}", t),
Operand::StackPointer => write!(f, "sp"),
Operand::ReturnAddress => write!(f, "ra"),
}
}
}
/// Represents a single IC10 MIPS instruction.
#[derive(Debug, Clone, PartialEq)]
pub enum Instruction<'a> {
/// `move dst val` - Copy value to register
Move(Operand<'a>, Operand<'a>),
/// `add dst a b` - Addition
Add(Operand<'a>, Operand<'a>, Operand<'a>),
/// `sub dst a b` - Subtraction
Sub(Operand<'a>, Operand<'a>, Operand<'a>),
/// `mul dst a b` - Multiplication
Mul(Operand<'a>, Operand<'a>, Operand<'a>),
/// `div dst a b` - Division
Div(Operand<'a>, Operand<'a>, Operand<'a>),
/// `mod dst a b` - Modulo
Mod(Operand<'a>, Operand<'a>, Operand<'a>),
/// `pow dst a b` - Power
Pow(Operand<'a>, Operand<'a>, Operand<'a>),
/// `acos dst a`
Acos(Operand<'a>, Operand<'a>),
/// `asin dst a`
Asin(Operand<'a>, Operand<'a>),
/// `atan dst a`
Atan(Operand<'a>, Operand<'a>),
/// `atan2 dst a b`
Atan2(Operand<'a>, Operand<'a>, Operand<'a>),
/// `abs dst a`
Abs(Operand<'a>, Operand<'a>),
/// `ceil dst a`
Ceil(Operand<'a>, Operand<'a>),
/// `cos dst a`
Cos(Operand<'a>, Operand<'a>),
/// `floor dst a`
Floor(Operand<'a>, Operand<'a>),
/// `log dst a`
Log(Operand<'a>, Operand<'a>),
/// `max dst a b`
Max(Operand<'a>, Operand<'a>, Operand<'a>),
/// `min dst a b`
Min(Operand<'a>, Operand<'a>, Operand<'a>),
/// `rand dst`
Rand(Operand<'a>),
/// `sin dst a`
Sin(Operand<'a>, Operand<'a>),
/// `sqrt dst a`
Sqrt(Operand<'a>, Operand<'a>),
/// `tan dst a`
Tan(Operand<'a>, Operand<'a>),
/// `trunc dst a`
Trunc(Operand<'a>, Operand<'a>),
/// `l register device type` - Load from device
Load(Operand<'a>, Operand<'a>, Operand<'a>),
/// `s device type value` - Set on device
Store(Operand<'a>, Operand<'a>, Operand<'a>),
/// `ls register device slot type` - Load Slot
LoadSlot(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
/// `ss device slot type value` - Set Slot
StoreSlot(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
/// `lb register deviceHash type batchMode` - Load Batch
LoadBatch(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
/// `sb deviceHash type value` - Set Batch
StoreBatch(Operand<'a>, Operand<'a>, Operand<'a>),
/// `lbn register deviceHash nameHash type batchMode` - Load Batch Named
LoadBatchNamed(
Operand<'a>,
Operand<'a>,
Operand<'a>,
Operand<'a>,
Operand<'a>,
),
/// `sbn deviceHash nameHash type value` - Set Batch Named
StoreBatchNamed(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
/// `j label` - Unconditional Jump
Jump(Operand<'a>),
/// `jal label` - Jump and Link (Function Call)
JumpAndLink(Operand<'a>),
/// `jr offset` - Jump Relative
JumpRelative(Operand<'a>),
/// `beq a b label` - Branch if Equal
BranchEq(Operand<'a>, Operand<'a>, Operand<'a>),
/// `bne a b label` - Branch if Not Equal
BranchNe(Operand<'a>, Operand<'a>, Operand<'a>),
/// `bgt a b label` - Branch if Greater Than
BranchGt(Operand<'a>, Operand<'a>, Operand<'a>),
/// `blt a b label` - Branch if Less Than
BranchLt(Operand<'a>, Operand<'a>, Operand<'a>),
/// `bge a b label` - Branch if Greater or Equal
BranchGe(Operand<'a>, Operand<'a>, Operand<'a>),
/// `ble a b label` - Branch if Less or Equal
BranchLe(Operand<'a>, Operand<'a>, Operand<'a>),
/// `beqz a label` - Branch if Equal Zero
BranchEqZero(Operand<'a>, Operand<'a>),
/// `bnez a label` - Branch if Not Equal Zero
BranchNeZero(Operand<'a>, Operand<'a>),
/// `seq dst a b` - Set if Equal
SetEq(Operand<'a>, Operand<'a>, Operand<'a>),
/// `sne dst a b` - Set if Not Equal
SetNe(Operand<'a>, Operand<'a>, Operand<'a>),
/// `sgt dst a b` - Set if Greater Than
SetGt(Operand<'a>, Operand<'a>, Operand<'a>),
/// `slt dst a b` - Set if Less Than
SetLt(Operand<'a>, Operand<'a>, Operand<'a>),
/// `sge dst a b` - Set if Greater or Equal
SetGe(Operand<'a>, Operand<'a>, Operand<'a>),
/// `sle dst a b` - Set if Less or Equal
SetLe(Operand<'a>, Operand<'a>, Operand<'a>),
/// `and dst a b` - Logical AND
And(Operand<'a>, Operand<'a>, Operand<'a>),
/// `or dst a b` - Logical OR
Or(Operand<'a>, Operand<'a>, Operand<'a>),
/// `xor dst a b` - Logical XOR
Xor(Operand<'a>, Operand<'a>, Operand<'a>),
/// `push val` - Push to Stack
Push(Operand<'a>),
/// `pop dst` - Pop from Stack
Pop(Operand<'a>),
/// `peek dst` - Peek from Stack (Usually sp - 1)
Peek(Operand<'a>),
/// `get dst dev num`
Get(Operand<'a>, Operand<'a>, Operand<'a>),
/// put dev addr val
Put(Operand<'a>, Operand<'a>, Operand<'a>),
/// `select dst cond a b` - Ternary Select
Select(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
/// `yield` - Pause execution
Yield,
/// `sleep val` - Sleep for seconds
Sleep(Operand<'a>),
/// `alias name target` - Define Alias (Usually handled by compiler, but good for IR)
Alias(Cow<'a, str>, Operand<'a>),
/// `define name val` - Define Constant (Usually handled by compiler)
Define(Cow<'a, str>, f64),
/// A label definition `Label:`
LabelDef(Cow<'a, str>),
/// A comment `# text`
Comment(Cow<'a, str>),
}
impl<'a> fmt::Display for Instruction<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Instruction::Move(dst, val) => write!(f, "move {} {}", dst, val),
Instruction::Add(dst, a, b) => write!(f, "add {} {} {}", dst, a, b),
Instruction::Sub(dst, a, b) => write!(f, "sub {} {} {}", dst, a, b),
Instruction::Mul(dst, a, b) => write!(f, "mul {} {} {}", dst, a, b),
Instruction::Div(dst, a, b) => write!(f, "div {} {} {}", dst, a, b),
Instruction::Mod(dst, a, b) => write!(f, "mod {} {} {}", dst, a, b),
Instruction::Pow(dst, a, b) => write!(f, "pow {} {} {}", dst, a, b),
Instruction::Acos(dst, a) => write!(f, "acos {} {}", dst, a),
Instruction::Asin(dst, a) => write!(f, "asin {} {}", dst, a),
Instruction::Atan(dst, a) => write!(f, "atan {} {}", dst, a),
Instruction::Atan2(dst, a, b) => write!(f, "atan2 {} {} {}", dst, a, b),
Instruction::Abs(dst, a) => write!(f, "abs {} {}", dst, a),
Instruction::Ceil(dst, a) => write!(f, "ceil {} {}", dst, a),
Instruction::Cos(dst, a) => write!(f, "cos {} {}", dst, a),
Instruction::Floor(dst, a) => write!(f, "floor {} {}", dst, a),
Instruction::Log(dst, a) => write!(f, "log {} {}", dst, a),
Instruction::Max(dst, a, b) => write!(f, "max {} {} {}", dst, a, b),
Instruction::Min(dst, a, b) => write!(f, "min {} {} {}", dst, a, b),
Instruction::Rand(dst) => write!(f, "rand {}", dst),
Instruction::Sin(dst, a) => write!(f, "sin {} {}", dst, a),
Instruction::Sqrt(dst, a) => write!(f, "sqrt {} {}", dst, a),
Instruction::Tan(dst, a) => write!(f, "tan {} {}", dst, a),
Instruction::Trunc(dst, a) => write!(f, "trunc {} {}", dst, a),
Instruction::Load(reg, dev, typ) => write!(f, "l {} {} {}", reg, dev, typ),
Instruction::Store(dev, typ, val) => write!(f, "s {} {} {}", dev, typ, val),
Instruction::LoadSlot(reg, dev, slot, typ) => {
write!(f, "ls {} {} {} {}", reg, dev, slot, typ)
}
Instruction::StoreSlot(dev, slot, typ, val) => {
write!(f, "ss {} {} {} {}", dev, slot, typ, val)
}
Instruction::LoadBatch(reg, hash, typ, mode) => {
write!(f, "lb {} {} {} {}", reg, hash, typ, mode)
}
Instruction::StoreBatch(hash, typ, val) => write!(f, "sb {} {} {}", hash, typ, val),
Instruction::LoadBatchNamed(reg, d_hash, n_hash, typ, mode) => {
write!(f, "lbn {} {} {} {} {}", reg, d_hash, n_hash, typ, mode)
}
Instruction::StoreBatchNamed(d_hash, n_hash, typ, val) => {
write!(f, "sbn {} {} {} {}", d_hash, n_hash, typ, val)
}
Instruction::Jump(lbl) => write!(f, "j {}", lbl),
Instruction::JumpAndLink(lbl) => write!(f, "jal {}", lbl),
Instruction::JumpRelative(off) => write!(f, "jr {}", off),
Instruction::BranchEq(a, b, lbl) => write!(f, "beq {} {} {}", a, b, lbl),
Instruction::BranchNe(a, b, lbl) => write!(f, "bne {} {} {}", a, b, lbl),
Instruction::BranchGt(a, b, lbl) => write!(f, "bgt {} {} {}", a, b, lbl),
Instruction::BranchLt(a, b, lbl) => write!(f, "blt {} {} {}", a, b, lbl),
Instruction::BranchGe(a, b, lbl) => write!(f, "bge {} {} {}", a, b, lbl),
Instruction::BranchLe(a, b, lbl) => write!(f, "ble {} {} {}", a, b, lbl),
Instruction::BranchEqZero(a, lbl) => write!(f, "beqz {} {}", a, lbl),
Instruction::BranchNeZero(a, lbl) => write!(f, "bnez {} {}", a, lbl),
Instruction::SetEq(dst, a, b) => write!(f, "seq {} {} {}", dst, a, b),
Instruction::SetNe(dst, a, b) => write!(f, "sne {} {} {}", dst, a, b),
Instruction::SetGt(dst, a, b) => write!(f, "sgt {} {} {}", dst, a, b),
Instruction::SetLt(dst, a, b) => write!(f, "slt {} {} {}", dst, a, b),
Instruction::SetGe(dst, a, b) => write!(f, "sge {} {} {}", dst, a, b),
Instruction::SetLe(dst, a, b) => write!(f, "sle {} {} {}", dst, a, b),
Instruction::And(dst, a, b) => write!(f, "and {} {} {}", dst, a, b),
Instruction::Or(dst, a, b) => write!(f, "or {} {} {}", dst, a, b),
Instruction::Xor(dst, a, b) => write!(f, "xor {} {} {}", dst, a, b),
Instruction::Push(val) => write!(f, "push {}", val),
Instruction::Pop(dst) => write!(f, "pop {}", dst),
Instruction::Peek(dst) => write!(f, "peek {}", dst),
Instruction::Get(dst, dev, val) => write!(f, "get {} {} {}", dst, dev, val),
Instruction::Put(dev, addr, val) => write!(f, "put {} {} {}", dev, addr, val),
Instruction::Select(dst, cond, a, b) => {
write!(f, "select {} {} {} {}", dst, cond, a, b)
}
Instruction::Yield => write!(f, "yield"),
Instruction::Sleep(val) => write!(f, "sleep {}", val),
Instruction::Alias(name, target) => write!(f, "alias {} {}", name, target),
Instruction::Define(name, val) => write!(f, "define {} {}", name, val),
Instruction::LabelDef(lbl) => write!(f, "{}:", lbl),
Instruction::Comment(c) => write!(f, "# {}", c),
}
}
}

View File

@@ -0,0 +1,6 @@
[package]
name = "optimizer"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View File

@@ -4,6 +4,7 @@ mod test;
pub mod tree_node;
use crate::sys_call::{Math, System};
use helpers::Span;
use std::{borrow::Cow, io::SeekFrom};
use sys_call::SysCall;
use thiserror::Error;

View File

@@ -1,5 +1,6 @@
use super::sys_call::SysCall;
use crate::sys_call;
use helpers::Span;
use safer_ffi::prelude::*;
use std::{borrow::Cow, ops::Deref};
use tokenizer::token::Number;
@@ -301,44 +302,6 @@ impl<'a> std::fmt::Display for WhileExpression<'a> {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub start_line: usize,
pub end_line: usize,
pub start_col: usize,
pub end_col: usize,
}
impl From<Span> for lsp_types::Range {
fn from(value: Span) -> Self {
Self {
start: lsp_types::Position {
line: value.start_line as u32,
character: value.start_col as u32,
},
end: lsp_types::Position {
line: value.end_line as u32,
character: value.end_col as u32,
},
}
}
}
impl From<&Span> for lsp_types::Range {
fn from(value: &Span) -> Self {
Self {
start: lsp_types::Position {
line: value.start_line as u32,
character: value.start_col as u32,
},
end: lsp_types::Position {
line: value.end_line as u32,
character: value.end_col as u32,
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Spanned<T> {
pub span: Span,

View File

@@ -403,6 +403,12 @@ pub enum Number {
Decimal(Decimal),
}
impl From<bool> for Number {
fn from(value: bool) -> Self {
Self::Integer(if value { 1 } else { 0 })
}
}
impl From<Number> for Decimal {
fn from(value: Number) -> Self {
match value {