From 41fa4bdce0a4968c06dd32c986d9bd632e46d070 Mon Sep 17 00:00:00 2001 From: mlokr Date: Tue, 3 Sep 2024 22:34:17 +0200 Subject: [PATCH] adding disasembler --- .gitignore | 1 + hblang/build.rs | 161 +++++++++++----- hblang/src/codegen.rs | 54 +++++- hblang/src/lib.rs | 122 +++++++++++- hblang/src/parser.rs | 1 - hblang/tests/codegen_tests_arithmetic.txt | 22 +++ hblang/tests/codegen_tests_arrays.txt | 46 +++++ hblang/tests/codegen_tests_c_strings.txt | 41 ++++ hblang/tests/codegen_tests_comments.txt | 14 ++ ...ts_comptime_function_from_another_file.txt | 8 + .../codegen_tests_comptime_min_reg_leak.txt | 8 + .../tests/codegen_tests_different_types.txt | 61 ++++++ hblang/tests/codegen_tests_directives.txt | 23 +++ hblang/tests/codegen_tests_fb_driver.txt | 69 +++++++ hblang/tests/codegen_tests_functions.txt | 32 ++++ .../tests/codegen_tests_generic_functions.txt | 37 ++++ hblang/tests/codegen_tests_generic_types.txt | 164 ++++++++++++++++ .../tests/codegen_tests_global_variables.txt | 8 + ...odegen_tests_hex_octal_binary_literals.txt | 27 +++ hblang/tests/codegen_tests_if_statements.txt | 33 ++++ hblang/tests/codegen_tests_inline.txt | 12 ++ hblang/tests/codegen_tests_inline_test.txt | 181 ++++++++++++++++++ hblang/tests/codegen_tests_loops.txt | 35 ++++ hblang/tests/codegen_tests_pointers.txt | 33 ++++ .../codegen_tests_sort_something_viredly.txt | 8 + .../tests/codegen_tests_struct_operators.txt | 73 +++++++ .../tests/codegen_tests_struct_patterns.txt | 74 +++++++ ...sts_struct_return_from_module_function.txt | 35 ++++ hblang/tests/codegen_tests_structs.txt | 51 +++++ hblang/tests/codegen_tests_variables.txt | 13 ++ 30 files changed, 1386 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index d0a6340..499fa03 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ /hbbytecode/src/opcode.rs /hbbytecode/src/ops.rs /hblang/src/instrs.rs +/hblang/src/disasm.rs /.rgignore rust-ice-* diff --git a/hblang/build.rs b/hblang/build.rs index 5cf605c..468e3d9 100644 --- a/hblang/build.rs +++ b/hblang/build.rs @@ -1,76 +1,135 @@ #![feature(iter_next_chunk)] -use std::fmt::Write; +use std::{collections::HashSet, fmt::Write}; fn main() -> Result<(), Box> { println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=../hbbytecode/instructions.in"); + gen_instrs()?; + + Ok(()) +} + +fn gen_instrs() -> Result<(), Box> { let mut generated = String::new(); writeln!(generated, "#![allow(dead_code)] #![allow(clippy::upper_case_acronyms)]")?; - gen_max_size(&mut generated)?; - gen_encodes(&mut generated)?; - gen_structs(&mut generated)?; - gen_name_list(&mut generated)?; - std::fs::write("src/instrs.rs", generated)?; + '_max_size: { + let max = instructions() + .map( + |[_, _, ty, _]| { + if ty == "N" { + 1 + } else { + iter_args(ty).map(arg_to_width).sum::() + 1 + } + }, + ) + .max() + .unwrap(); - Ok(()) -} - -fn gen_name_list(generated: &mut String) -> Result<(), Box> { - writeln!(generated, "pub const NAMES: [&str; {}] = [", instructions().count())?; - for [_, name, _, _] in instructions() { - writeln!(generated, " \"{}\",", name.to_lowercase())?; + writeln!(generated, "pub const MAX_SIZE: usize = {max};")?; } - writeln!(generated, "];")?; - Ok(()) -} + '_encoders: { + for [op, name, ty, doc] in instructions() { + writeln!(generated, "/// {}", doc.trim_matches('"'))?; + let name = name.to_lowercase(); + let args = comma_sep( + iter_args(ty) + .enumerate() + .map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))), + ); + writeln!(generated, "pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{")?; + let arg_names = + comma_sep(iter_args(ty).enumerate().map(|(i, c)| format!("{}{i}", arg_to_name(c)))); + writeln!(generated, " unsafe {{ crate::encode({ty}({op}, {arg_names})) }}")?; + writeln!(generated, "}}")?; + } + } -fn gen_max_size(generated: &mut String) -> Result<(), Box> { - let max = instructions() - .map(|[_, _, ty, _]| { - if ty == "N" { - 1 - } else { - iter_args(ty).map(|(_, c)| arg_to_width(c)).sum::() + 1 + '_structs: { + let mut seen = std::collections::HashSet::new(); + for [_, _, ty, _] in instructions() { + if !seen.insert(ty) { + continue; } - }) - .max() - .unwrap(); + let types = comma_sep(iter_args(ty).map(arg_to_type).map(|s| s.to_string())); + writeln!(generated, "#[repr(packed)] pub struct {ty}(u8, {types});")?; + } + } - writeln!(generated, "pub const MAX_SIZE: usize = {};", max)?; + '_name_list: { + writeln!(generated, "pub const NAMES: [&str; {}] = [", instructions().count())?; + for [_, name, _, _] in instructions() { + writeln!(generated, " \"{}\",", name.to_lowercase())?; + } + writeln!(generated, "];")?; + } - Ok(()) -} + let instr = "Instr"; + let oper = "Oper"; -fn gen_encodes(generated: &mut String) -> Result<(), Box> { - for [op, name, ty, doc] in instructions() { - writeln!(generated, "/// {}", doc.trim_matches('"'))?; - let name = name.to_lowercase(); - let args = comma_sep( - iter_args(ty).map(|(i, c)| format!("{}{i}: {}", arg_to_name(c), arg_to_type(c))), - ); - writeln!(generated, "pub fn {name}({args}) -> (usize, [u8; MAX_SIZE]) {{")?; - let arg_names = comma_sep(iter_args(ty).map(|(i, c)| format!("{}{i}", arg_to_name(c)))); - writeln!(generated, " unsafe {{ crate::encode({ty}({op}, {arg_names})) }}")?; + '_instr_enum: { + writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u8)]")?; + writeln!(generated, "pub enum {instr} {{")?; + for [id, name, ..] in instructions() { + writeln!(generated, " {name} = {id},")?; + } writeln!(generated, "}}")?; } - Ok(()) -} - -fn gen_structs(generated: &mut String) -> Result<(), Box> { - let mut seen = std::collections::HashSet::new(); - for [_, _, ty, _] in instructions() { - if !seen.insert(ty) { - continue; + '_arg_kind: { + writeln!(generated, "#[derive(Debug, Clone, Copy, PartialEq, Eq)]")?; + writeln!(generated, "pub enum {oper} {{")?; + let mut seen = HashSet::new(); + for ty in instructions().flat_map(|[.., ty, _]| iter_args(ty)) { + if !seen.insert(ty) { + continue; + } + writeln!(generated, " {ty}({}),", arg_to_type(ty))?; } - let types = comma_sep(iter_args(ty).map(|(_, c)| arg_to_type(c).to_string())); - writeln!(generated, "#[repr(packed)] pub struct {ty}(u8, {types});")?; + writeln!(generated, "}}")?; } + '_parse_opers: { + writeln!( + generated, + "/// This assumes the instruction byte is still at the beginning of the buffer" + )?; + writeln!(generated, "pub fn parse_args(bytes: &mut &[u8], kind: {instr}, buf: &mut Vec<{oper}>) -> Option<()> {{")?; + writeln!(generated, " match kind {{")?; + let mut instrs = instructions().collect::>(); + instrs.sort_unstable_by_key(|&[.., ty, _]| ty); + for group in instrs.chunk_by(|[.., a, _], [.., b, _]| a == b) { + let ty = group[0][2]; + for &[_, name, ..] in group { + writeln!(generated, " | {instr}::{name}")?; + } + generated.pop(); + writeln!(generated, " => {{")?; + if iter_args(ty).count() != 0 { + writeln!(generated, " let data = unsafe {{ std::ptr::read(bytes.take(..std::mem::size_of::<{ty}>())?.as_ptr() as *const {ty}) }};")?; + writeln!( + generated, + " buf.extend([{}]);", + comma_sep( + iter_args(ty).zip(1u32..).map(|(t, i)| format!("{oper}::{t}(data.{i})")) + ) + )?; + } else { + writeln!(generated, " bytes.take(..std::mem::size_of::<{ty}>())?;")?; + } + + writeln!(generated, " }}")?; + } + writeln!(generated, " }}")?; + writeln!(generated, " Some(())")?; + writeln!(generated, "}}")?; + } + + std::fs::write("src/instrs.rs", generated)?; Ok(()) } @@ -119,6 +178,6 @@ fn arg_to_name(arg: char) -> &'static str { } } -fn iter_args(ty: &'static str) -> impl Iterator { - ty.chars().enumerate().filter(|(_, c)| *c != 'N') +fn iter_args(ty: &'static str) -> impl Iterator { + ty.chars().filter(|c| *c != 'N') } diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index c566f80..c9bc125 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -863,7 +863,9 @@ struct Func { file: FileId, expr: ExprRef, sig: Option, + runtime: bool, offset: Offset, + size: Size, } struct Global { @@ -1281,6 +1283,7 @@ const VM_STACK_SIZE: usize = 1024 * 1024 * 2; struct Comptime { vm: hbvm::Vm, + depth: usize, _stack: Box<[u8; VM_STACK_SIZE]>, } @@ -1291,7 +1294,21 @@ impl Default for Comptime { let ptr = unsafe { stack.as_mut_ptr().cast::().add(VM_STACK_SIZE) as u64 }; log::dbg!("stack_ptr: {:x}", ptr); vm.write_reg(STACK_PTR, ptr); - Self { vm, _stack: unsafe { stack.assume_init() } } + Self { vm, depth: 0, _stack: unsafe { stack.assume_init() } } + } +} + +impl Comptime { + fn active(&self) -> bool { + self.depth != 0 + } + + fn enter(&mut self) { + self.depth += 1; + } + + fn exit(&mut self) { + self.depth -= 1; } } @@ -1380,7 +1397,7 @@ impl Codegen { self.link(); } - pub fn dump(mut self, out: &mut impl std::io::Write) -> std::io::Result<()> { + pub fn dump(&mut self, out: &mut impl std::io::Write) -> std::io::Result<()> { let reloc = Reloc::shifted(0, 3, 4); self.output.funcs.push((0, reloc)); self.link(); @@ -2272,6 +2289,8 @@ impl Codegen { self.tys.funcs.push(Func { file: fuc.file, offset: u32::MAX, + size: 0, + runtime: false, sig: Some(Sig { args, ret }), expr: fuc.expr, }); @@ -2698,6 +2717,7 @@ impl Codegen { self.output.emit(jala(ZERO, RET_ADDR, 0)); self.ci.regs.free(std::mem::take(&mut self.ci.ret_reg)); self.tys.funcs[id as usize].offset = self.ci.snap.code as Offset; + self.tys.funcs[id as usize].size = self.local_offset(); self.pool.cis.push(std::mem::replace(&mut self.ci, prev_ci)); self.ct.vm.write_reg(reg::STACK_PTR, ct_stack_base); } @@ -3089,7 +3109,9 @@ impl Codegen { debug_assert!(refr.get(&f).is_some()); refr }, + runtime: false, offset: u32::MAX, + size: 0, }; let id = self.tys.funcs.len() as _; @@ -3128,6 +3150,7 @@ impl Codegen { fn make_func_reachable(&mut self, func: ty::Func) { let fuc = &mut self.tys.funcs[func as usize]; + fuc.runtime |= !self.ct.active(); if fuc.offset == u32::MAX { fuc.offset = task::id(self.tasks.len() as _); self.tasks.push(Some(FTask { file: fuc.file, id: func })); @@ -3191,6 +3214,7 @@ impl Codegen { compile: impl FnOnce(&mut Self, &mut ItemCtx) -> Result, ) -> Result { log::dbg!("eval"); + self.ct.enter(); let stash = self.pop_stash(); let mut prev_ci = std::mem::replace(&mut self.ci, ci); @@ -3231,6 +3255,7 @@ impl Codegen { self.ci.snap = self.output.snap(); self.push_stash(stash); + self.ct.exit(); log::dbg!("eval-end"); ret @@ -3336,7 +3361,11 @@ impl Codegen { mod tests { use { super::parser, - crate::{codegen::LoggedMem, log, parser::FileId}, + crate::{ + codegen::LoggedMem, + log, + parser::{Expr, FileId}, + }, std::io, }; @@ -3400,7 +3429,24 @@ mod tests { let mut out = Vec::new(); codegen.dump(&mut out).unwrap(); - log::dbg!("code: {}", String::from_utf8_lossy(&out)); + let mut sluce = out.as_slice(); + let functions = codegen + .tys + .funcs + .iter() + .filter(|f| f.offset != u32::MAX && f.runtime) + .map(|f| { + let file = &codegen.files[f.file as usize]; + let Expr::BinOp { left: &Expr::Ident { name, .. }, .. } = f.expr.get(file).unwrap() + else { + unreachable!() + }; + (f.offset, (name, f.size)) + }) + .collect::>(); + if crate::disasm(&mut sluce, &functions, output).is_err() { + panic!("{} {:?}", output, sluce); + } use std::fmt::Write; diff --git a/hblang/src/lib.rs b/hblang/src/lib.rs index fdbf4b8..06f742e 100644 --- a/hblang/src/lib.rs +++ b/hblang/src/lib.rs @@ -11,15 +11,16 @@ portable_simd, iter_collect_into, ptr_metadata, - slice_ptr_get + slice_ptr_get, + slice_take, + map_try_insert )] -#![feature(map_try_insert)] #![allow(internal_features, clippy::format_collect)] use { parser::Ast, std::{ - collections::VecDeque, + collections::{hash_map, VecDeque}, io::{self, Read}, path::{Path, PathBuf}, sync::Mutex, @@ -128,6 +129,117 @@ unsafe fn encode(instr: T) -> (usize, [u8; instrs::MAX_SIZE]) { std::ptr::write(buf.as_mut_ptr() as *mut T, instr); (std::mem::size_of::(), buf) } +#[cfg(test)] +fn disasm( + binary: &mut &[u8], + functions: &HashMap, + out: &mut String, +) -> std::fmt::Result { + use self::instrs::Instr; + + fn instr_from_byte(b: u8) -> Result { + if b as usize >= instrs::NAMES.len() { + return Err(std::fmt::Error); + } + unsafe { std::mem::transmute(b) } + } + + let mut labels = HashMap::::default(); + let mut buf = Vec::::new(); + + '_offset_pass: for (&off, &(_name, len)) in functions.iter() { + let prev = *binary; + + binary.take(..off as usize).unwrap(); + let mut label_count = 0; + while let Some(&byte) = binary.first() { + let inst = instr_from_byte(byte)?; + let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); + if offset as u32 == off + len { + break; + } + instrs::parse_args(binary, inst, &mut buf).ok_or(std::fmt::Error)?; + + for op in buf.drain(..) { + let rel = match op { + instrs::Oper::O(rel) => rel, + instrs::Oper::P(rel) => rel.into(), + _ => continue, + }; + + let global_offset: u32 = (offset + rel).try_into().unwrap(); + if functions.get(&global_offset).is_some() { + continue; + } + label_count += labels.try_insert(global_offset, label_count).is_ok() as u32; + } + } + + *binary = prev; + } + + '_dump: for (&off, &(name, len)) in functions.iter() { + let prev = *binary; + + use std::fmt::Write; + + writeln!(out, "{name}:")?; + + binary.take(..off as usize).unwrap(); + while let Some(&byte) = binary.first() { + let inst = instr_from_byte(byte)?; + let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); + if offset as u32 == off + len { + break; + } + instrs::parse_args(binary, inst, &mut buf).ok_or(std::fmt::Error)?; + + if let Some(label) = labels.get(&offset.try_into().unwrap()) { + write!(out, "{:>2}: ", label)?; + } else { + write!(out, " ")?; + } + + write!(out, "{inst:<8?} ")?; + + 'a: for (i, op) in buf.drain(..).enumerate() { + if i != 0 { + write!(out, ", ")?; + } + + let rel = 'b: { + match op { + instrs::Oper::O(rel) => break 'b rel, + instrs::Oper::P(rel) => break 'b rel.into(), + instrs::Oper::R(r) => write!(out, "r{r}")?, + instrs::Oper::B(b) => write!(out, "{b}b")?, + instrs::Oper::H(h) => write!(out, "{h}h")?, + instrs::Oper::W(w) => write!(out, "{w}w")?, + instrs::Oper::D(d) if (d as i64) < 0 => write!(out, "{}d", d as i64)?, + instrs::Oper::D(d) => write!(out, "{d}d")?, + instrs::Oper::A(a) => write!(out, "{a}a")?, + } + + continue 'a; + }; + + let global_offset: u32 = (offset + rel).try_into().unwrap(); + if let Some(&(name, _)) = functions.get(&global_offset) { + write!(out, ":{name}")?; + } else { + let label = labels.get(&global_offset).unwrap(); + write!(out, ":{label}")?; + } + } + + writeln!(out)?; + } + + *binary = prev; + } + + Ok(()) +} struct TaskQueue { inner: Mutex>, @@ -367,10 +479,10 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result> { let mut seen = seen.lock().unwrap(); let len = seen.len(); match seen.entry(physiscal_path.clone()) { - std::collections::hash_map::Entry::Occupied(entry) => { + hash_map::Entry::Occupied(entry) => { return Ok(*entry.get()); } - std::collections::hash_map::Entry::Vacant(entry) => { + hash_map::Entry::Vacant(entry) => { entry.insert(len as _); len as u32 } diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index 2931108..04995f4 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -3,7 +3,6 @@ use { codegen, ident::{self, Ident}, lexer::{self, Lexer, Token, TokenKind}, - log, }, std::{ cell::{Cell, UnsafeCell}, diff --git a/hblang/tests/codegen_tests_arithmetic.txt b/hblang/tests/codegen_tests_arithmetic.txt index 572eddf..4429161 100644 --- a/hblang/tests/codegen_tests_arithmetic.txt +++ b/hblang/tests/codegen_tests_arithmetic.txt @@ -1,3 +1,25 @@ +main: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + LI64 r32, 10d + LI64 r33, 20d + LI64 r34, 2d + DIRS64 r33, r0, r33, r34 + SUB64 r32, r32, r33 + LI64 r33, 4d + LI64 r34, 2d + ADDI64 r34, r34, 2d + MUL64 r33, r33, r34 + ADD64 r32, r32, r33 + LI64 r33, 4d + MULI64 r33, r33, 4d + SUB64 r32, r32, r33 + ADDI64 r32, r32, 1d + SLUI64 r32, r32, 0b + CP r1, r32 + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a code size: 188 ret: 1 status: Ok(()) diff --git a/hblang/tests/codegen_tests_arrays.txt b/hblang/tests/codegen_tests_arrays.txt index 3246da5..dc42dca 100644 --- a/hblang/tests/codegen_tests_arrays.txt +++ b/hblang/tests/codegen_tests_arrays.txt @@ -1,3 +1,49 @@ +main: + ADDI64 r254, r254, -72d + ST r31, r254, 48a, 24h + LI64 r32, 1d + ST r32, r254, 0a, 24h + LI64 r32, 2d + ST r32, r254, 8a, 24h + LI64 r32, 4d + ST r32, r254, 16a, 24h + ADDI64 r32, r254, 0d + ADDI64 r33, r254, 24d + BMC r32, r33, 24h + ADDI64 r33, r254, 24d + CP r2, r33 + JAL r31, r0, :pass + LD r31, r254, 48a, 24h + ADDI64 r254, r254, 72d + JALA r0, r31, 0a +pass: + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h + CP r32, r2 + CP r33, r32 + LI64 r34, 0d + MULI64 r34, r34, 8d + ADD64 r33, r33, r34 + LD r34, r33, 0a, 8h + CP r33, r32 + LI64 r35, 1d + MULI64 r35, r35, 8d + ADD64 r33, r33, r35 + LD r35, r33, 0a, 8h + ADD64 r34, r34, r35 + CP r35, r32 + LI64 r33, 1d + MULI64 r33, r33, 8d + ADD64 r32, r32, r33 + LD r33, r32, 0a, 8h + MULI64 r33, r33, 8d + ADD64 r35, r35, r33 + LD r33, r35, 0a, 8h + ADD64 r34, r34, r33 + CP r1, r34 + LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d + JALA r0, r31, 0a code size: 408 ret: 7 status: Ok(()) diff --git a/hblang/tests/codegen_tests_c_strings.txt b/hblang/tests/codegen_tests_c_strings.txt index f66f396..051858c 100644 --- a/hblang/tests/codegen_tests_c_strings.txt +++ b/hblang/tests/codegen_tests_c_strings.txt @@ -1,3 +1,44 @@ +main: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + LRA r32, r0, :0 + CP r2, r32 + JAL r31, r0, :str_len + CP r32, r1 + LRA r33, r0, :1 + CP r2, r33 + JAL r31, r0, :str_len + CP r33, r1 + ADD64 r33, r33, r32 + CP r1, r33 + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a +str_len: + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h + CP r32, r2 + LI64 r33, 0d + 2: CP r34, r32 + CP r35, r0 + LD r35, r34, 0a, 1h + LI64 r34, 0d + CMPU r35, r35, r34 + CMPUI r35, r35, 0d + NOT r35, r35 + JEQ r35, r0, :0 + JMP :1 + 0: CP r35, r33 + ADDI64 r35, r35, 1d + CP r33, r35 + CP r35, r32 + ADDI64 r35, r35, 1d + CP r32, r35 + JMP :2 + 1: CP r1, r33 + LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d + JALA r0, r31, 0a code size: 303 ret: 16 status: Ok(()) diff --git a/hblang/tests/codegen_tests_comments.txt b/hblang/tests/codegen_tests_comments.txt index 08da43b..74809d0 100644 --- a/hblang/tests/codegen_tests_comments.txt +++ b/hblang/tests/codegen_tests_comments.txt @@ -1,3 +1,17 @@ +main: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + JAL r31, r0, :foo + LI64 r1, 0d + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a +foo: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a code size: 143 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_comptime_function_from_another_file.txt b/hblang/tests/codegen_tests_comptime_function_from_another_file.txt index 59c666a..4cfadc1 100644 --- a/hblang/tests/codegen_tests_comptime_function_from_another_file.txt +++ b/hblang/tests/codegen_tests_comptime_function_from_another_file.txt @@ -1,3 +1,11 @@ +main: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + LRA r32, r0, :0 + LD r1, r32, 0a, 8h + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a code size: 245 ret: 50 status: Ok(()) diff --git a/hblang/tests/codegen_tests_comptime_min_reg_leak.txt b/hblang/tests/codegen_tests_comptime_min_reg_leak.txt index c3bac2f..3c3ef0b 100644 --- a/hblang/tests/codegen_tests_comptime_min_reg_leak.txt +++ b/hblang/tests/codegen_tests_comptime_min_reg_leak.txt @@ -1,3 +1,11 @@ +main: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + LRA r32, r0, :0 + LD r1, r32, 0a, 8h + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a code size: 221 ret: 50 status: Ok(()) diff --git a/hblang/tests/codegen_tests_different_types.txt b/hblang/tests/codegen_tests_different_types.txt index a28d285..05bd4ea 100644 --- a/hblang/tests/codegen_tests_different_types.txt +++ b/hblang/tests/codegen_tests_different_types.txt @@ -1,3 +1,64 @@ +main: + ADDI64 r254, r254, -48d + ST r31, r254, 24a, 24h + LI64 r32, 255d + ST r32, r254, 0a, 1h + LI64 r32, 0d + ST r32, r254, 1a, 1h + LI64 r32, 0d + ST r32, r254, 2a, 1h + LI64 r32, 255d + ST r32, r254, 3a, 1h + LI64 r32, 0d + ST r32, r254, 4a, 4h + LI64 r32, 2d + ST r32, r254, 8a, 4h + ADDI64 r32, r254, 0d + ADDI64 r33, r254, 12d + BMC r32, r33, 12h + LI64 r33, 1d + ADDI64 r32, r254, 16d + MULI64 r33, r33, 4d + ADD64 r32, r32, r33 + CP r33, r0 + LD r33, r32, 0a, 4h + LI64 r32, 2d + CMPU r33, r33, r32 + CMPUI r33, r33, 0d + JEQ r33, r0, :0 + LI64 r1, 0d + JMP :1 + 0: ADDI64 r33, r254, 20d + ADDI64 r33, r33, -4d + CP r32, r0 + LD r32, r33, 0a, 4h + LI64 r33, 0d + CMPU r32, r32, r33 + CMPUI r32, r32, 0d + JEQ r32, r0, :2 + LI64 r1, 64d + JMP :1 + 2: CP r32, r0 + LD r32, r254, 16a, 4h + CP r33, r0 + LD r33, r254, 20a, 4h + ADD32 r32, r32, r33 + CP r33, r0 + LD r33, r254, 12a, 1h + ADD32 r32, r32, r33 + CP r33, r0 + LD r33, r254, 13a, 1h + ADD32 r32, r32, r33 + CP r33, r0 + LD r33, r254, 14a, 1h + ADD32 r32, r32, r33 + CP r33, r0 + LD r33, r254, 15a, 1h + ADD32 r32, r32, r33 + CP r1, r32 + 1: LD r31, r254, 24a, 24h + ADDI64 r254, r254, 48d + JALA r0, r31, 0a code size: 531 ret: 512 status: Ok(()) diff --git a/hblang/tests/codegen_tests_directives.txt b/hblang/tests/codegen_tests_directives.txt index 02f8530..f3d453c 100644 --- a/hblang/tests/codegen_tests_directives.txt +++ b/hblang/tests/codegen_tests_directives.txt @@ -1,3 +1,26 @@ +main: + ADDI64 r254, r254, -80d + ST r31, r254, 16a, 64h + LI64 r32, 10d + LI64 r33, 30d + LI64 r34, 40d + LI64 r35, 16d + LI64 r36, 8d + LI64 r37, 10d + LI64 r2, 1d + LI64 r38, 10d + ST r38, r254, 0a, 8h + LI64 r38, 20d + ST r38, r254, 8a, 8h + LD r3, r254, 0a, 16h + LI64 r5, 5d + LI64 r6, 6d + ECA + CP r38, r1 + LI64 r1, 0d + LD r31, r254, 16a, 64h + ADDI64 r254, r254, 80d + JALA r0, r31, 0a ev: Ecall code size: 234 ret: 0 diff --git a/hblang/tests/codegen_tests_fb_driver.txt b/hblang/tests/codegen_tests_fb_driver.txt index b20e820..904d5ec 100644 --- a/hblang/tests/codegen_tests_fb_driver.txt +++ b/hblang/tests/codegen_tests_fb_driver.txt @@ -1,3 +1,72 @@ +check_platform: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + JAL r31, r0, :x86_fb_ptr + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -64d + ST r31, r254, 0a, 64h + JAL r31, r0, :check_platform + CP r32, r1 + LI64 r33, 100d + LI64 r34, 30d + LI64 r35, 0d + LI64 r36, 0d + 4: CP r37, r35 + CP r38, r34 + ADDI64 r38, r38, 1d + CMPS r37, r37, r38 + CMPUI r37, r37, 1d + JEQ r37, r0, :0 + CP r2, r35 + CP r3, r36 + CP r4, r33 + JAL r31, r0, :set_pixel + CP r37, r35 + ADDI64 r37, r37, 1d + CP r35, r37 + JMP :1 + 0: CP r2, r35 + CP r3, r36 + CP r4, r33 + JAL r31, r0, :set_pixel + LI64 r35, 0d + CP r37, r36 + ADDI64 r37, r37, 1d + CP r36, r37 + 1: CP r37, r36 + CP r38, r33 + CMPS r37, r37, r38 + CMPUI r37, r37, 0d + NOT r37, r37 + JEQ r37, r0, :2 + JMP :3 + 2: JMP :4 + 3: LI64 r1, 0d + LD r31, r254, 0a, 64h + ADDI64 r254, r254, 64d + JALA r0, r31, 0a +set_pixel: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + CP r32, r2 + CP r33, r3 + CP r34, r4 + MUL64 r33, r33, r34 + ADD64 r33, r33, r32 + LI64 r1, 0d + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a +x86_fb_ptr: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + LI64 r1, 100d + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a code size: 505 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_functions.txt b/hblang/tests/codegen_tests_functions.txt index db32607..85150f8 100644 --- a/hblang/tests/codegen_tests_functions.txt +++ b/hblang/tests/codegen_tests_functions.txt @@ -1,3 +1,35 @@ +add_two: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + CP r32, r2 + ADDI64 r32, r32, 2d + CP r1, r32 + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + LI64 r2, 10d + JAL r31, r0, :add_one + CP r32, r1 + LI64 r2, 20d + JAL r31, r0, :add_two + CP r33, r1 + ADD64 r32, r32, r33 + CP r1, r32 + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a +add_one: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + CP r32, r2 + ADDI64 r32, r32, 1d + CP r1, r32 + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a code size: 266 ret: 33 status: Ok(()) diff --git a/hblang/tests/codegen_tests_generic_functions.txt b/hblang/tests/codegen_tests_generic_functions.txt index 4186302..6e8b20a 100644 --- a/hblang/tests/codegen_tests_generic_functions.txt +++ b/hblang/tests/codegen_tests_generic_functions.txt @@ -1,3 +1,40 @@ +add: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + CP r32, r2 + CP r33, r3 + ADD32 r32, r32, r33 + CP r1, r32 + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + LI64 r2, 2d + LI64 r3, 2d + JAL r31, r0, :add + CP r32, r1 + LI64 r2, 1d + LI64 r3, 3d + JAL r31, r0, :add + CP r33, r1 + SXT32 r32, r32 + SUB64 r32, r32, r33 + CP r1, r32 + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a +add: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + CP r32, r2 + CP r33, r3 + ADD64 r32, r32, r33 + CP r1, r32 + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a code size: 281 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_generic_types.txt b/hblang/tests/codegen_tests_generic_types.txt index 3ac54b1..d257147 100644 --- a/hblang/tests/codegen_tests_generic_types.txt +++ b/hblang/tests/codegen_tests_generic_types.txt @@ -1,3 +1,167 @@ +new: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + CP r32, r1 + LI64 r33, 0d + ST r33, r32, 0a, 8h + LI64 r33, 0d + ST r33, r32, 8a, 8h + LI64 r33, 0d + ST r33, r32, 16a, 8h + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a +push: + ADDI64 r254, r254, -88d + ST r31, r254, 0a, 88h + CP r32, r2 + CP r33, r3 + LD r34, r32, 8a, 8h + LD r35, r32, 16a, 8h + CMPU r34, r34, r35 + CMPUI r34, r34, 0d + NOT r34, r34 + JEQ r34, r0, :0 + LD r34, r32, 16a, 8h + LI64 r35, 0d + CMPU r34, r34, r35 + CMPUI r34, r34, 0d + NOT r34, r34 + JEQ r34, r0, :1 + LI64 r34, 1d + ST r34, r32, 16a, 8h + JMP :2 + 1: LD r34, r32, 16a, 8h + MULI64 r34, r34, 2d + ST r34, r32, 16a, 8h + 2: LD r34, r32, 16a, 8h + MULI64 r34, r34, 8d + CP r2, r34 + LI64 r3, 8d + JAL r31, r0, :malloc + CP r34, r1 + CP r35, r34 + LI64 r36, 0d + CMPU r35, r35, r36 + CMPUI r35, r35, 0d + NOT r35, r35 + JEQ r35, r0, :3 + LI64 r1, 0d + JMP :4 + 3: LD r35, r32, 0a, 8h + CP r36, r34 + LD r37, r32, 0a, 8h + LD r38, r32, 8a, 8h + MULI64 r38, r38, 8d + ADD64 r37, r37, r38 + 7: CP r38, r35 + CP r39, r37 + CMPU r38, r38, r39 + CMPUI r38, r38, 0d + NOT r38, r38 + JEQ r38, r0, :5 + JMP :6 + 5: CP r38, r36 + CP r39, r35 + CP r40, r39 + CP r41, r38 + BMC r40, r41, 8h + CP r38, r35 + ADDI64 r38, r38, 8d + CP r35, r38 + CP r38, r36 + ADDI64 r38, r38, 8d + CP r36, r38 + JMP :7 + 6: LD r38, r32, 8a, 8h + LI64 r39, 0d + CMPU r38, r38, r39 + CMPUI r38, r38, 0d + JEQ r38, r0, :8 + LD r2, r32, 0a, 8h + LD r38, r32, 8a, 8h + MULI64 r38, r38, 8d + CP r3, r38 + LI64 r4, 8d + JAL r31, r0, :free + 8: ST r34, r32, 0a, 8h + 0: LD r34, r32, 0a, 8h + LD r38, r32, 8a, 8h + MULI64 r38, r38, 8d + ADD64 r34, r34, r38 + CP r38, r34 + ST r33, r38, 0a, 8h + LD r38, r32, 8a, 8h + ADDI64 r38, r38, 1d + ST r38, r32, 8a, 8h + CP r1, r34 + 4: LD r31, r254, 0a, 88h + ADDI64 r254, r254, 88d + JALA r0, r31, 0a +deinit: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + CP r32, r2 + LD r2, r32, 0a, 8h + LD r33, r32, 16a, 8h + MULI64 r33, r33, 8d + CP r3, r33 + LI64 r4, 8d + JAL r31, r0, :free + CP r1, r32 + JAL r31, r0, :new + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a +malloc: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + CP r32, r2 + CP r33, r3 + LRA r34, r0, :0 + LD r2, r34, 0a, 8h + CP r3, r32 + CP r4, r33 + ECA + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a +free: + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h + CP r32, r2 + CP r33, r3 + CP r34, r4 + LRA r35, r0, :0 + LD r2, r35, 0a, 8h + CP r3, r32 + CP r4, r33 + CP r5, r34 + ECA + LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -72d + ST r31, r254, 48a, 24h + ADDI64 r1, r254, 0d + JAL r31, r0, :new + ADDI64 r32, r254, 0d + ADDI64 r33, r254, 24d + BMC r32, r33, 24h + ADDI64 r33, r254, 24d + CP r2, r33 + LI64 r3, 69d + JAL r31, r0, :push + LD r33, r254, 24a, 8h + LD r32, r33, 0a, 8h + ADDI64 r33, r254, 24d + CP r2, r33 + JAL r31, r0, :deinit + CP r1, r32 + LD r31, r254, 48a, 24h + ADDI64 r254, r254, 72d + JALA r0, r31, 0a code size: 1469 ret: 69 status: Ok(()) diff --git a/hblang/tests/codegen_tests_global_variables.txt b/hblang/tests/codegen_tests_global_variables.txt index 9d6a68f..6046aee 100644 --- a/hblang/tests/codegen_tests_global_variables.txt +++ b/hblang/tests/codegen_tests_global_variables.txt @@ -1,3 +1,11 @@ +main: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + LRA r32, r0, :0 + LD r1, r32, 0a, 8h + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a code size: 267 ret: 50 status: Ok(()) diff --git a/hblang/tests/codegen_tests_hex_octal_binary_literals.txt b/hblang/tests/codegen_tests_hex_octal_binary_literals.txt index 9e3f9ab..e228db7 100644 --- a/hblang/tests/codegen_tests_hex_octal_binary_literals.txt +++ b/hblang/tests/codegen_tests_hex_octal_binary_literals.txt @@ -1,3 +1,30 @@ +main: + ADDI64 r254, r254, -48d + ST r31, r254, 0a, 48h + LI64 r32, 255d + LI64 r33, 255d + LI64 r34, 255d + LI64 r35, 255d + CP r36, r33 + CMPS r32, r32, r36 + CMPUI r32, r32, 0d + NOT r32, r32 + CP r36, r33 + CMPS r34, r34, r36 + CMPUI r34, r34, 0d + NOT r34, r34 + AND r32, r32, r34 + CMPS r35, r35, r33 + CMPUI r35, r35, 0d + NOT r35, r35 + AND r32, r32, r35 + JEQ r32, r0, :0 + LI64 r1, 0d + JMP :1 + 0: LI64 r1, 1d + 1: LD r31, r254, 0a, 48h + ADDI64 r254, r254, 48d + JALA r0, r31, 0a code size: 205 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_if_statements.txt b/hblang/tests/codegen_tests_if_statements.txt index 6d9419a..1b9a128 100644 --- a/hblang/tests/codegen_tests_if_statements.txt +++ b/hblang/tests/codegen_tests_if_statements.txt @@ -1,3 +1,36 @@ +main: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + LI64 r2, 10d + JAL r31, r0, :fib + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a +fib: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + CP r32, r2 + CP r33, r32 + LI64 r34, 2d + CMPS r33, r33, r34 + CMPUI r33, r33, 1d + JEQ r33, r0, :0 + LI64 r1, 1d + JMP :1 + 0: CP r33, r32 + ADDI64 r33, r33, -1d + CP r2, r33 + JAL r31, r0, :fib + CP r33, r1 + ADDI64 r32, r32, -2d + CP r2, r32 + JAL r31, r0, :fib + CP r32, r1 + ADD64 r33, r33, r32 + CP r1, r33 + 1: LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a code size: 252 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_inline.txt b/hblang/tests/codegen_tests_inline.txt index deb087b..36dc922 100644 --- a/hblang/tests/codegen_tests_inline.txt +++ b/hblang/tests/codegen_tests_inline.txt @@ -1,3 +1,15 @@ +main: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + LI64 r33, 1d + ADDI64 r33, r33, 2d + ADDI64 r33, r33, 3d + CP r32, r33 + ADDI64 r32, r32, -6d + CP r1, r32 + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a code size: 116 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_inline_test.txt b/hblang/tests/codegen_tests_inline_test.txt index c780e3c..ac6a0d7 100644 --- a/hblang/tests/codegen_tests_inline_test.txt +++ b/hblang/tests/codegen_tests_inline_test.txt @@ -1,3 +1,184 @@ +integer: + ADDI64 r254, r254, -56d + ST r31, r254, 0a, 56h + CP r32, r2 + CP r33, r3 + LI64 r2, 3d + LI64 r3, 4d + ECA + CP r34, r1 + CP r35, r32 + LI64 r36, 0d + CMPS r35, r35, r36 + CMPUI r35, r35, 0d + CP r36, r33 + LI64 r37, 0d + CMPS r36, r36, r37 + CMPUI r36, r36, 0d + OR r35, r35, r36 + JEQ r35, r0, :0 + CP r35, r34 + CP r36, r32 + SUB64 r33, r33, r36 + ADDI64 r33, r33, 1d + DIRS64 r0, r35, r35, r33 + ADD64 r35, r35, r32 + CP r1, r35 + JMP :1 + 0: CP r1, r34 + 1: LD r31, r254, 0a, 56h + ADDI64 r254, r254, 56d + JALA r0, r31, 0a +line: + ADDI64 r254, r254, -80d + ST r31, r254, 48a, 32h + ST r2, r254, 0a, 16h + ST r4, r254, 16a, 16h + ST r6, r254, 32a, 16h + CP r32, r8 + LI64 r33, 1d + JEQ r33, r0, :0 + LD r33, r254, 0a, 8h + LD r34, r254, 16a, 8h + CMPS r33, r33, r34 + CMPUI r33, r33, 1d + NOT r33, r33 + JEQ r33, r0, :1 + JMP :1 + 1: JMP :2 + 0: LD r33, r254, 8a, 8h + LD r34, r254, 24a, 8h + CMPS r33, r33, r34 + CMPUI r33, r33, 1d + NOT r33, r33 + JEQ r33, r0, :2 + JMP :2 + 2: LD r31, r254, 48a, 32h + ADDI64 r254, r254, 80d + JALA r0, r31, 0a +example: + ADDI64 r254, r254, -48d + ST r31, r254, 0a, 48h + LI64 r2, 3d + LI64 r3, 4d + ECA + CP r33, r1 + LI64 r34, 0d + LI64 r35, 0d + CMPS r34, r34, r35 + CMPUI r34, r34, 0d + LI64 r35, 1024d + LI64 r36, 0d + CMPS r35, r35, r36 + CMPUI r35, r35, 0d + OR r34, r34, r35 + JEQ r34, r0, :0 + CP r34, r33 + LI64 r35, 1024d + ADDI64 r35, r35, 0d + ADDI64 r35, r35, 1d + DIRS64 r0, r34, r34, r35 + ADDI64 r34, r34, 0d + CP r32, r34 + JMP :1 + 0: CP r32, r33 + 1: LI64 r2, 0d + LI64 r3, 768d + JAL r31, r0, :integer + CP r33, r1 + CP r34, r32 + JMP :2 + 2: LD r31, r254, 0a, 48h + ADDI64 r254, r254, 48d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -64d + ST r31, r254, 48a, 16h + LI64 r32, 0d + ST r32, r254, 0a, 8h + LI64 r32, 0d + ST r32, r254, 8a, 8h + LD r2, r254, 0a, 16h + LI64 r32, 0d + ST r32, r254, 16a, 8h + LI64 r32, 0d + ST r32, r254, 24a, 8h + LD r4, r254, 16a, 16h + LI64 r32, 0d + ST r32, r254, 32a, 8h + LI64 r32, 0d + ST r32, r254, 40a, 8h + LD r6, r254, 32a, 16h + LI64 r8, 10d + JAL r31, r0, :line + LI64 r32, 0d + ST r32, r254, 0a, 8h + LI64 r32, 0d + ST r32, r254, 8a, 8h + LD r2, r254, 0a, 16h + LI64 r32, 0d + ST r32, r254, 16a, 8h + LI64 r32, 0d + ST r32, r254, 24a, 8h + LD r4, r254, 16a, 16h + LI64 r32, 0d + ST r32, r254, 32a, 8h + LI64 r32, 0d + ST r32, r254, 40a, 8h + LD r6, r254, 32a, 16h + LI64 r8, 10d + JAL r31, r0, :rect_line + JAL r31, r0, :example + LI64 r1, 0d + LD r31, r254, 48a, 16h + ADDI64 r254, r254, 64d + JALA r0, r31, 0a +rect_line: + ADDI64 r254, r254, -112d + ST r31, r254, 48a, 64h + ST r2, r254, 0a, 16h + ST r4, r254, 16a, 16h + ST r6, r254, 32a, 16h + CP r32, r8 + LI64 r33, 0d + LI64 r34, 0d + LI64 r35, 0d + 5: CP r36, r33 + CP r37, r32 + CMPS r36, r36, r37 + CMPUI r36, r36, 0d + NOT r36, r36 + JEQ r36, r0, :0 + JMP :1 + 0: LD r34, r254, 8a, 8h + LD r35, r254, 0a, 8h + 4: CP r36, r34 + LD r37, r254, 8a, 8h + LD r38, r254, 16a, 8h + ADD64 r37, r37, r38 + CMPS r36, r36, r37 + CMPUI r36, r36, 0d + NOT r36, r36 + JEQ r36, r0, :2 + JMP :3 + 2: LI64 r36, 1d + LI64 r37, 10d + ADD64 r36, r36, r37 + LI64 r37, 1d + LI64 r38, 2d + ADD64 r37, r37, r38 + CP r36, r37 + CP r36, r34 + ADDI64 r36, r36, 1d + CP r34, r36 + JMP :4 + 3: CP r36, r33 + ADDI64 r36, r36, 1d + CP r33, r36 + JMP :5 + 1: LD r31, r254, 48a, 64h + ADDI64 r254, r254, 112d + JALA r0, r31, 0a code size: 1521 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_loops.txt b/hblang/tests/codegen_tests_loops.txt index 9ce8c1b..ac8523d 100644 --- a/hblang/tests/codegen_tests_loops.txt +++ b/hblang/tests/codegen_tests_loops.txt @@ -1,3 +1,38 @@ +main: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + LI64 r2, 10d + JAL r31, r0, :fib + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a +fib: + ADDI64 r254, r254, -48d + ST r31, r254, 0a, 48h + CP r32, r2 + LI64 r33, 0d + LI64 r34, 1d + 2: CP r35, r32 + LI64 r36, 0d + CMPS r35, r35, r36 + CMPUI r35, r35, 0d + NOT r35, r35 + JEQ r35, r0, :0 + JMP :1 + 0: CP r35, r33 + CP r36, r34 + ADD64 r35, r35, r36 + CP r33, r34 + CP r34, r35 + CP r35, r32 + ADDI64 r35, r35, -1d + CP r32, r35 + LI64 r35, 0d + JMP :2 + 1: CP r1, r33 + LD r31, r254, 0a, 48h + ADDI64 r254, r254, 48d + JALA r0, r31, 0a code size: 258 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_pointers.txt b/hblang/tests/codegen_tests_pointers.txt index def5877..6631352 100644 --- a/hblang/tests/codegen_tests_pointers.txt +++ b/hblang/tests/codegen_tests_pointers.txt @@ -1,3 +1,36 @@ +main: + ADDI64 r254, r254, -40d + ST r31, r254, 8a, 32h + LI64 r32, 1d + ST r32, r254, 0a, 8h + ADDI64 r32, r254, 0d + CP r2, r32 + JAL r31, r0, :modify + LD r2, r254, 0a, 8h + JAL r31, r0, :drop + LI64 r33, 0d + LD r34, r32, 0a, 8h + ADDI64 r34, r34, -2d + CP r1, r34 + LD r31, r254, 8a, 32h + ADDI64 r254, r254, 40d + JALA r0, r31, 0a +drop: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + CP r32, r2 + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a +modify: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + CP r32, r2 + LI64 r33, 2d + ST r33, r32, 0a, 8h + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d + JALA r0, r31, 0a code size: 315 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_sort_something_viredly.txt b/hblang/tests/codegen_tests_sort_something_viredly.txt index 7c615dd..9a14185 100644 --- a/hblang/tests/codegen_tests_sort_something_viredly.txt +++ b/hblang/tests/codegen_tests_sort_something_viredly.txt @@ -1,3 +1,11 @@ +main: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + LI64 r32, 1610612737d + LI64 r1, 0d + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d + JALA r0, r31, 0a code size: 87 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_struct_operators.txt b/hblang/tests/codegen_tests_struct_operators.txt index 7f383d1..54cbc18 100644 --- a/hblang/tests/codegen_tests_struct_operators.txt +++ b/hblang/tests/codegen_tests_struct_operators.txt @@ -1,3 +1,76 @@ +main: + ADDI64 r254, r254, -136d + ST r31, r254, 112a, 24h + LI64 r32, 1d + ST r32, r254, 32a, 8h + LI64 r32, 2d + ST r32, r254, 40a, 8h + LI64 r32, 3d + ST r32, r254, 16a, 8h + LI64 r32, 4d + ST r32, r254, 24a, 8h + LD r32, r254, 32a, 8h + LD r33, r254, 16a, 8h + ADD64 r32, r32, r33 + ST r32, r254, 48a, 8h + LD r32, r254, 40a, 8h + LD r33, r254, 24a, 8h + ADD64 r32, r32, r33 + ST r32, r254, 56a, 8h + LD r32, r254, 16a, 8h + LD r33, r254, 32a, 8h + SUB64 r32, r32, r33 + ST r32, r254, 64a, 8h + LD r32, r254, 24a, 8h + LD r33, r254, 40a, 8h + SUB64 r32, r32, r33 + ST r32, r254, 72a, 8h + LI64 r32, 0d + ST r32, r254, 0a, 8h + LI64 r32, 0d + ST r32, r254, 8a, 8h + LD r32, r254, 0a, 8h + LD r33, r254, 16a, 8h + SUB64 r32, r32, r33 + ST r32, r254, 80a, 8h + LD r32, r254, 8a, 8h + LD r33, r254, 24a, 8h + SUB64 r32, r32, r33 + ST r32, r254, 88a, 8h + ADDI64 r32, r254, 32d + ADDI64 r33, r254, 96d + BMC r32, r33, 16h + LD r33, r254, 80a, 8h + LD r32, r254, 48a, 8h + ADD64 r33, r33, r32 + ST r33, r254, 80a, 8h + LD r33, r254, 88a, 8h + LD r32, r254, 56a, 8h + ADD64 r33, r33, r32 + ST r33, r254, 88a, 8h + LD r33, r254, 96a, 8h + LD r32, r254, 64a, 8h + ADD64 r33, r33, r32 + ST r33, r254, 96a, 8h + LD r33, r254, 104a, 8h + LD r32, r254, 72a, 8h + ADD64 r33, r33, r32 + ST r33, r254, 104a, 8h + LD r33, r254, 80a, 8h + LD r32, r254, 96a, 8h + ADD64 r33, r33, r32 + ST r33, r254, 96a, 8h + LD r33, r254, 88a, 8h + LD r32, r254, 104a, 8h + ADD64 r33, r33, r32 + ST r33, r254, 104a, 8h + LD r33, r254, 96a, 8h + LD r32, r254, 104a, 8h + ADD64 r33, r33, r32 + CP r1, r33 + LD r31, r254, 112a, 24h + ADDI64 r254, r254, 136d + JALA r0, r31, 0a code size: 781 ret: 10 status: Ok(()) diff --git a/hblang/tests/codegen_tests_struct_patterns.txt b/hblang/tests/codegen_tests_struct_patterns.txt index 6f3db46..324dea1 100644 --- a/hblang/tests/codegen_tests_struct_patterns.txt +++ b/hblang/tests/codegen_tests_struct_patterns.txt @@ -1,3 +1,77 @@ +fib: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + CP r32, r2 + CP r33, r32 + LI64 r34, 2d + CMPS r33, r33, r34 + CMPUI r33, r33, -1d + NOT r33, r33 + JEQ r33, r0, :0 + CP r1, r32 + JMP :1 + 0: CP r33, r32 + ADDI64 r33, r33, -1d + CP r2, r33 + JAL r31, r0, :fib + CP r33, r1 + ADDI64 r32, r32, -2d + CP r2, r32 + JAL r31, r0, :fib + CP r32, r1 + ADD64 r33, r33, r32 + CP r1, r33 + 1: LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -26d + ST r31, r254, 2a, 24h + LI64 r32, 10d + ST r32, r254, 0a, 1h + LI64 r32, 10d + ST r32, r254, 1a, 1h + CP r32, r0 + LD r32, r254, 0a, 1h + CP r33, r0 + LD r33, r254, 1a, 1h + CP r2, r32 + JAL r31, r0, :fib + CP r32, r1 + CP r2, r33 + JAL r31, r0, :fib_iter + CP r33, r1 + SUB64 r32, r32, r33 + CP r1, r32 + LD r31, r254, 2a, 24h + ADDI64 r254, r254, 26d + JALA r0, r31, 0a +fib_iter: + ADDI64 r254, r254, -48d + ST r31, r254, 0a, 48h + CP r32, r2 + LI64 r33, 0d + LI64 r34, 1d + 2: CP r35, r32 + LI64 r36, 0d + CMPS r35, r35, r36 + CMPUI r35, r35, 0d + NOT r35, r35 + JEQ r35, r0, :0 + JMP :1 + 0: CP r35, r33 + CP r36, r34 + ADD64 r35, r35, r36 + CP r33, r34 + CP r34, r35 + CP r35, r32 + ADDI64 r35, r35, -1d + CP r32, r35 + JMP :2 + 1: CP r1, r33 + LD r31, r254, 0a, 48h + ADDI64 r254, r254, 48d + JALA r0, r31, 0a code size: 518 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_struct_return_from_module_function.txt b/hblang/tests/codegen_tests_struct_return_from_module_function.txt index c08808c..7f6c404 100644 --- a/hblang/tests/codegen_tests_struct_return_from_module_function.txt +++ b/hblang/tests/codegen_tests_struct_return_from_module_function.txt @@ -1,3 +1,38 @@ +main: + ADDI64 r254, r254, -40d + ST r31, r254, 16a, 24h + LI64 r32, 7d + JAL r31, r0, :foo + ST r1, r254, 0a, 16h + LD r33, r254, 0a, 8h + SUB64 r32, r32, r33 + JAL r31, r0, :foo + ST r1, r254, 0a, 16h + CP r33, r0 + LD r33, r254, 8a, 4h + SXT32 r33, r33 + SUB64 r32, r32, r33 + JAL r31, r0, :foo + ST r1, r254, 0a, 16h + CP r33, r0 + LD r33, r254, 12a, 4h + SXT32 r33, r33 + SUB64 r32, r32, r33 + CP r1, r32 + LD r31, r254, 16a, 24h + ADDI64 r254, r254, 40d + JALA r0, r31, 0a +foo: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + LI64 r1, 3d + ANDI r2, r2, -4294967296d + ORI r2, r2, 2d + ANDI r2, r2, 4294967295d + ORI r2, r2, 8589934592d + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a code size: 320 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_structs.txt b/hblang/tests/codegen_tests_structs.txt index c179894..cfc7100 100644 --- a/hblang/tests/codegen_tests_structs.txt +++ b/hblang/tests/codegen_tests_structs.txt @@ -1,3 +1,54 @@ +pass: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + CP r32, r2 + LD r33, r32, 0a, 8h + LD r34, r32, 8a, 8h + SUB64 r33, r33, r34 + CP r1, r33 + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -72d + ST r31, r254, 48a, 24h + LI64 r32, 4d + ST r32, r254, 24a, 8h + LI64 r32, 1d + ST r32, r254, 32a, 8h + LI64 r32, 3d + ST r32, r254, 40a, 8h + ADDI64 r2, r254, 24d + ADDI64 r1, r254, 0d + JAL r31, r0, :odher_pass + ADDI64 r32, r254, 0d + ADDI64 r33, r254, 24d + BMC r32, r33, 24h + LD r33, r254, 40a, 8h + LI64 r32, 3d + CMPS r33, r33, r32 + CMPUI r33, r33, 0d + NOT r33, r33 + JEQ r33, r0, :0 + ADDI64 r33, r254, 24d + CP r2, r33 + JAL r31, r0, :pass + JMP :1 + 0: LI64 r1, 0d + 1: LD r31, r254, 48a, 24h + ADDI64 r254, r254, 72d + JALA r0, r31, 0a +odher_pass: + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h + CP r32, r2 + CP r33, r1 + CP r34, r32 + CP r35, r33 + BMC r34, r35, 24h + LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d + JALA r0, r31, 0a code size: 445 ret: 3 status: Ok(()) diff --git a/hblang/tests/codegen_tests_variables.txt b/hblang/tests/codegen_tests_variables.txt index 27ddd88..27495d2 100644 --- a/hblang/tests/codegen_tests_variables.txt +++ b/hblang/tests/codegen_tests_variables.txt @@ -1,3 +1,16 @@ +main: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + LI64 r32, 1d + LI64 r33, 2d + CP r34, r32 + ADDI64 r34, r34, 1d + CP r32, r34 + SUB64 r32, r32, r33 + CP r1, r32 + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a code size: 111 ret: 0 status: Ok(())