From 8e2f80544edddae654b550a8647b8f1021808f7a Mon Sep 17 00:00:00 2001 From: Cr0a3 Date: Sat, 19 Oct 2024 19:06:33 +0200 Subject: [PATCH] [WASM] starting function structure --- src/CodeGen/compilation/cast.rs | 4 +- src/CodeGen/instr.rs | 8 +-- src/Target/wasm/lower.rs | 107 ++++++++++++++++++-------------- src/Target/wasm/lower/br.rs | 10 +++ src/Target/wasm/lower/call.rs | 6 ++ src/Target/wasm/lower/cast.rs | 7 +++ src/Target/wasm/lower/cmove.rs | 10 +++ src/Target/wasm/lower/cmp.rs | 6 ++ src/Target/wasm/lower/math.rs | 22 +++++++ src/Target/wasm/lower/mov.rs | 6 ++ src/Target/wasm/lower/ret.rs | 6 ++ src/Target/wasm/lower/stack.rs | 18 ++++++ src/Target/wasm/lower/switch.rs | 7 +++ src/Target/x64/call.rs | 85 ------------------------- src/Target/x64/lower.rs | 4 +- src/Target/x64/lower/call.rs | 2 +- src/Target/x64/mod.rs | 1 - 17 files changed, 166 insertions(+), 143 deletions(-) create mode 100644 src/Target/wasm/lower/br.rs create mode 100644 src/Target/wasm/lower/call.rs create mode 100644 src/Target/wasm/lower/cast.rs create mode 100644 src/Target/wasm/lower/cmove.rs create mode 100644 src/Target/wasm/lower/cmp.rs create mode 100644 src/Target/wasm/lower/math.rs create mode 100644 src/Target/wasm/lower/mov.rs create mode 100644 src/Target/wasm/lower/ret.rs create mode 100644 src/Target/wasm/lower/stack.rs create mode 100644 src/Target/wasm/lower/switch.rs delete mode 100644 src/Target/x64/call.rs diff --git a/src/CodeGen/compilation/cast.rs b/src/CodeGen/compilation/cast.rs index 56235107..79507a75 100644 --- a/src/CodeGen/compilation/cast.rs +++ b/src/CodeGen/compilation/cast.rs @@ -12,7 +12,7 @@ impl CompilationHelper { let op = { if node.inner1.ty.float() || node.inner2.float() { - MachineMnemonic::FCast(node.inner1.ty) + MachineMnemonic::FCast } else if node.inner1.ty.bitSize() > node.inner2.bitSize() { MachineMnemonic::Zext } else if node.inner1.ty.bitSize() < node.inner2.bitSize(){ @@ -20,7 +20,7 @@ impl CompilationHelper { } else { return; } - }; + }(node.inner1.ty); let mut instr = MachineInstr::new(op); diff --git a/src/CodeGen/instr.rs b/src/CodeGen/instr.rs index 4dab5ebd..7e26c69d 100644 --- a/src/CodeGen/instr.rs +++ b/src/CodeGen/instr.rs @@ -239,8 +239,8 @@ pub enum MachineMnemonic { BrCond(/*if yes*/String, /*if no*/String), Compare(CmpMode), - Zext, - Downcast, + Zext(/*from type*/TypeMetadata), + Downcast(/*from type*/TypeMetadata), Call(String), Br(String), @@ -292,8 +292,8 @@ impl MachineMnemonic { MachineMnemonic::Sub => "sub", MachineMnemonic::Xor => "xor", MachineMnemonic::Rem => "rem", - MachineMnemonic::Zext => "zext", - MachineMnemonic::Downcast => "dwcast", + MachineMnemonic::Zext(_) => "zext", + MachineMnemonic::Downcast(_) => "dwcast", MachineMnemonic::Call(_) => "call", MachineMnemonic::Return => "return", MachineMnemonic::AdressLoad(_) => "adrload", diff --git a/src/Target/wasm/lower.rs b/src/Target/wasm/lower.rs index b002bad5..5476fefd 100644 --- a/src/Target/wasm/lower.rs +++ b/src/Target/wasm/lower.rs @@ -1,54 +1,65 @@ +mod mov; +mod math; +mod cmove; +mod stack; +mod ret; +mod br; +mod cmp; +mod cast; +mod call; +mod switch; + use crate::{CodeGen::{MCInstr, MachineInstr}, Optimizations::Optimize, Target::CallConv}; pub(crate) fn wasm_lower_instr(sink: &mut Vec, instr: MachineInstr) { - match instr.mnemonic { - crate::CodeGen::MachineMnemonic::Move => todo!(), - crate::CodeGen::MachineMnemonic::Add => todo!(), - crate::CodeGen::MachineMnemonic::And => todo!(), - crate::CodeGen::MachineMnemonic::Div => todo!(), - crate::CodeGen::MachineMnemonic::Mul => todo!(), - crate::CodeGen::MachineMnemonic::Or => todo!(), - crate::CodeGen::MachineMnemonic::Sub => todo!(), - crate::CodeGen::MachineMnemonic::Xor => todo!(), - crate::CodeGen::MachineMnemonic::Rem => todo!(), - crate::CodeGen::MachineMnemonic::Neg => todo!(), - crate::CodeGen::MachineMnemonic::Shl => todo!(), - crate::CodeGen::MachineMnemonic::Shr => todo!(), - crate::CodeGen::MachineMnemonic::FMove => todo!(), - crate::CodeGen::MachineMnemonic::FAdd => todo!(), - crate::CodeGen::MachineMnemonic::FAnd => todo!(), - crate::CodeGen::MachineMnemonic::FDiv => todo!(), - crate::CodeGen::MachineMnemonic::FMul => todo!(), - crate::CodeGen::MachineMnemonic::FOr => todo!(), - crate::CodeGen::MachineMnemonic::FSub => todo!(), - crate::CodeGen::MachineMnemonic::FXor => todo!(), - crate::CodeGen::MachineMnemonic::FRem => todo!(), - crate::CodeGen::MachineMnemonic::FNeg => todo!(), - crate::CodeGen::MachineMnemonic::FShl => todo!(), - crate::CodeGen::MachineMnemonic::FShr => todo!(), - crate::CodeGen::MachineMnemonic::FCompare(cmp_mode) => todo!(), - crate::CodeGen::MachineMnemonic::FCast(type_metadata) => todo!(), - crate::CodeGen::MachineMnemonic::BrCond(iftrue, iffalse) => todo!(), - crate::CodeGen::MachineMnemonic::Compare(cmp_mode) => todo!(), - crate::CodeGen::MachineMnemonic::Zext => todo!(), - crate::CodeGen::MachineMnemonic::Downcast => todo!(), - crate::CodeGen::MachineMnemonic::Call(func) => todo!(), - crate::CodeGen::MachineMnemonic::Br(block_name) => todo!(), - crate::CodeGen::MachineMnemonic::Return => todo!(), - crate::CodeGen::MachineMnemonic::AdressLoad(const_name) => todo!(), - crate::CodeGen::MachineMnemonic::StackAlloc => todo!(), - crate::CodeGen::MachineMnemonic::Store => todo!(), - crate::CodeGen::MachineMnemonic::Load => todo!(), - crate::CodeGen::MachineMnemonic::Prolog => todo!(), - crate::CodeGen::MachineMnemonic::Epilog => todo!(), - crate::CodeGen::MachineMnemonic::Push => todo!(), - crate::CodeGen::MachineMnemonic::PushCleanup => todo!(), - crate::CodeGen::MachineMnemonic::CallStackPrepare => todo!(), - crate::CodeGen::MachineMnemonic::CallStackRedo => todo!(), - crate::CodeGen::MachineMnemonic::AdrMove => todo!(), - crate::CodeGen::MachineMnemonic::Switch(cases) => todo!(), - crate::CodeGen::MachineMnemonic::MovIfZero => todo!(), - crate::CodeGen::MachineMnemonic::MovIfNotZero => todo!(), + match instr.mnemonic.to_owned() { + crate::CodeGen::MachineMnemonic::Move => mov::wasm_lower_mov(sink, &instr), + crate::CodeGen::MachineMnemonic::Add => math::wasm_lower_add(sink, &instr), + crate::CodeGen::MachineMnemonic::And => math::wasm_lower_and(sink, &instr), + crate::CodeGen::MachineMnemonic::Div => math::wasm_lower_div(sink, &instr), + crate::CodeGen::MachineMnemonic::Mul => math::wasm_lower_mul(sink, &instr), + crate::CodeGen::MachineMnemonic::Or => math::wasm_lower_or(sink, &instr), + crate::CodeGen::MachineMnemonic::Sub => math::wasm_lower_sub(sink, &instr), + crate::CodeGen::MachineMnemonic::Xor => math::wasm_lower_xor(sink, &instr), + crate::CodeGen::MachineMnemonic::Rem => math::wasm_lower_rem(sink, &instr), + crate::CodeGen::MachineMnemonic::Neg => math::wasm_lower_neg(sink, &instr), + crate::CodeGen::MachineMnemonic::Shl => math::wasm_lower_shl(sink, &instr), + crate::CodeGen::MachineMnemonic::Shr => math::wasm_lower_shr(sink, &instr), + crate::CodeGen::MachineMnemonic::FMove => mov::wasm_lower_mov(sink, &instr), + crate::CodeGen::MachineMnemonic::FAdd => math::wasm_lower_add(sink, &instr), + crate::CodeGen::MachineMnemonic::FAnd => math::wasm_lower_and(sink, &instr), + crate::CodeGen::MachineMnemonic::FDiv => math::wasm_lower_div(sink, &instr), + crate::CodeGen::MachineMnemonic::FMul => math::wasm_lower_mul(sink, &instr), + crate::CodeGen::MachineMnemonic::FOr => math::wasm_lower_or(sink, &instr), + crate::CodeGen::MachineMnemonic::FSub => math::wasm_lower_sub(sink, &instr), + crate::CodeGen::MachineMnemonic::FXor => math::wasm_lower_xor(sink, &instr), + crate::CodeGen::MachineMnemonic::FRem => math::wasm_lower_rem(sink, &instr), + crate::CodeGen::MachineMnemonic::FNeg => math::wasm_lower_neg(sink, &instr), + crate::CodeGen::MachineMnemonic::FShl => math::wasm_lower_shl(sink, &instr), + crate::CodeGen::MachineMnemonic::FShr => math::wasm_lower_shr(sink, &instr), + crate::CodeGen::MachineMnemonic::FCompare(cmp_mode) => cmp::wasm_lower_cmp(sink, &instr, cmp_mode), + crate::CodeGen::MachineMnemonic::FCast(start_ty) => cast::wasm_lower_cast(sink, &instr, start_ty), + crate::CodeGen::MachineMnemonic::BrCond(iftrue, iffalse) => br::wasm_lower_brcond(sink, &instr, iftrue, iffalse), + crate::CodeGen::MachineMnemonic::Compare(cmp_mode) => cmp::wasm_lower_cmp(sink, &instr, cmp_mode), + crate::CodeGen::MachineMnemonic::Zext(start_ty) => cast::wasm_lower_cast(sink, &instr, start_ty), + crate::CodeGen::MachineMnemonic::Downcast(start_ty) => cast::wasm_lower_cast(sink, &instr, start_ty), + crate::CodeGen::MachineMnemonic::Call(func) => call::wasm_lower_call(sink, &instr, func), + crate::CodeGen::MachineMnemonic::Br(block) => br::wasm_lower_br(sink, &instr, block), + crate::CodeGen::MachineMnemonic::Return => ret::wasm_lower_return(sink, &instr), + crate::CodeGen::MachineMnemonic::AdressLoad(constant) => stack::wasm_lower_adress_load(sink, &instr, constant), + crate::CodeGen::MachineMnemonic::StackAlloc => stack::wasm_lower_alloc(sink, &instr), + crate::CodeGen::MachineMnemonic::Store => stack::wasm_lower_store(sink, &instr), + crate::CodeGen::MachineMnemonic::Load => stack::wasm_lower_load(sink, &instr), + crate::CodeGen::MachineMnemonic::Prolog => {}, + crate::CodeGen::MachineMnemonic::Epilog => {}, + crate::CodeGen::MachineMnemonic::Push => panic!("illegal instruction for "), + crate::CodeGen::MachineMnemonic::PushCleanup => {}, + crate::CodeGen::MachineMnemonic::CallStackPrepare => {}, + crate::CodeGen::MachineMnemonic::CallStackRedo => {}, + crate::CodeGen::MachineMnemonic::AdrMove => mov::wasm_lower_mov(sink, &instr), + crate::CodeGen::MachineMnemonic::Switch(cases) => switch::wasm_lower_switch(sink, &instr, cases), + crate::CodeGen::MachineMnemonic::MovIfZero => cmove::wasm_lower_cmove(sink, &instr), + crate::CodeGen::MachineMnemonic::MovIfNotZero => cmove::wasm_lower_cmovne(sink, &instr), } } diff --git a/src/Target/wasm/lower/br.rs b/src/Target/wasm/lower/br.rs new file mode 100644 index 00000000..ab4e75b0 --- /dev/null +++ b/src/Target/wasm/lower/br.rs @@ -0,0 +1,10 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; + +pub(crate) fn wasm_lower_br(sink: &mut Vec, instr: &MachineInstr, block: String) { + todo!() +} + +pub(crate) fn wasm_lower_brcond(sink: &mut Vec, instr: &MachineInstr, iftrue: String, iffalse: String) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/call.rs b/src/Target/wasm/lower/call.rs new file mode 100644 index 00000000..fe86efb9 --- /dev/null +++ b/src/Target/wasm/lower/call.rs @@ -0,0 +1,6 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; + +pub(crate) fn wasm_lower_call(sink: &mut Vec, instr: &MachineInstr, func: String) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/cast.rs b/src/Target/wasm/lower/cast.rs new file mode 100644 index 00000000..80dd7401 --- /dev/null +++ b/src/Target/wasm/lower/cast.rs @@ -0,0 +1,7 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; +use crate::IR::TypeMetadata; + +pub(crate) fn wasm_lower_cast(sink: &mut Vec, instr: &MachineInstr, start_ty: TypeMetadata) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/cmove.rs b/src/Target/wasm/lower/cmove.rs new file mode 100644 index 00000000..21cb5769 --- /dev/null +++ b/src/Target/wasm/lower/cmove.rs @@ -0,0 +1,10 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; + +pub(crate) fn wasm_lower_cmove(sink: &mut Vec, instr: &MachineInstr) { + todo!() +} + +pub(crate) fn wasm_lower_cmovne(sink: &mut Vec, instr: &MachineInstr) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/cmp.rs b/src/Target/wasm/lower/cmp.rs new file mode 100644 index 00000000..5cae79c1 --- /dev/null +++ b/src/Target/wasm/lower/cmp.rs @@ -0,0 +1,6 @@ +use super::super::asm::*; +use crate::{prelude::CmpMode, CodeGen::MachineInstr}; + +pub(crate) fn wasm_lower_cmp(sink: &mut Vec, instr: &MachineInstr, mode: CmpMode) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/math.rs b/src/Target/wasm/lower/math.rs new file mode 100644 index 00000000..ba28b9e7 --- /dev/null +++ b/src/Target/wasm/lower/math.rs @@ -0,0 +1,22 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; + +macro_rules! WasmMathLower { + ($func:ident) => { + pub(crate) fn $func(sink: &mut Vec, instr: &MachineInstr) { + todo!() + } + }; +} + +WasmMathLower!(wasm_lower_add); +WasmMathLower!(wasm_lower_and); +WasmMathLower!(wasm_lower_div); +WasmMathLower!(wasm_lower_mul); +WasmMathLower!(wasm_lower_or); +WasmMathLower!(wasm_lower_sub); +WasmMathLower!(wasm_lower_xor); +WasmMathLower!(wasm_lower_rem); +WasmMathLower!(wasm_lower_neg); +WasmMathLower!(wasm_lower_shl); +WasmMathLower!(wasm_lower_shr); \ No newline at end of file diff --git a/src/Target/wasm/lower/mov.rs b/src/Target/wasm/lower/mov.rs new file mode 100644 index 00000000..5892a80f --- /dev/null +++ b/src/Target/wasm/lower/mov.rs @@ -0,0 +1,6 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; + +pub(crate) fn wasm_lower_mov(sink: &mut Vec, instr: &MachineInstr) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/ret.rs b/src/Target/wasm/lower/ret.rs new file mode 100644 index 00000000..4f59797e --- /dev/null +++ b/src/Target/wasm/lower/ret.rs @@ -0,0 +1,6 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; + +pub(crate) fn wasm_lower_return(sink: &mut Vec, instr: &MachineInstr) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/stack.rs b/src/Target/wasm/lower/stack.rs new file mode 100644 index 00000000..5300df5d --- /dev/null +++ b/src/Target/wasm/lower/stack.rs @@ -0,0 +1,18 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; + +pub(crate) fn wasm_lower_alloc(sink: &mut Vec, instr: &MachineInstr) { + todo!() +} + +pub(crate) fn wasm_lower_store(sink: &mut Vec, instr: &MachineInstr) { + todo!() +} + +pub(crate) fn wasm_lower_load(sink: &mut Vec, instr: &MachineInstr) { + todo!() +} + +pub(crate) fn wasm_lower_adress_load(sink: &mut Vec, instr: &MachineInstr, constant: String) { + todo!() +} \ No newline at end of file diff --git a/src/Target/wasm/lower/switch.rs b/src/Target/wasm/lower/switch.rs new file mode 100644 index 00000000..8d9e7da4 --- /dev/null +++ b/src/Target/wasm/lower/switch.rs @@ -0,0 +1,7 @@ +use super::super::asm::*; +use crate::CodeGen::MachineInstr; +use crate::IR::{Type, BlockId}; + +pub(crate) fn wasm_lower_switch(sink: &mut Vec, instr: &MachineInstr, cases: Vec<(Type, BlockId)>) { + todo!() +} \ No newline at end of file diff --git a/src/Target/x64/call.rs b/src/Target/x64/call.rs deleted file mode 100644 index 000cb725..00000000 --- a/src/Target/x64/call.rs +++ /dev/null @@ -1,85 +0,0 @@ -use crate::Target::CallConv; - -use super::x64Reg; - -impl CallConv { - /// Returns the number of register arguments in the calling convention - pub fn regArgs(&self) -> usize { - match self { - CallConv::SystemV => 6, - CallConv::WindowsFastCall => 4, - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } - - /// used for system v call conv - pub fn reset_eax(&self) -> bool { - match self { - CallConv::SystemV => true, - CallConv::WindowsFastCall => false, - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } - - /// Returns the 16Bit intenger argument registers as a vec - pub fn args16(&self) -> Vec { - match self { - CallConv::SystemV => vec![x64Reg::Di, x64Reg::Si, x64Reg::Dx, x64Reg::Cx, x64Reg::R8w, x64Reg::R9w], - CallConv::WindowsFastCall => vec![x64Reg::Cx, x64Reg::Dx, x64Reg::R8w, x64Reg::R9w], - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } - - /// Returns the 32Bit intenger argument registers as a vec - pub fn args32(&self) -> Vec { - match self { - CallConv::SystemV => vec![x64Reg::Edi, x64Reg::Esi, x64Reg::Edx, x64Reg::Ecx, x64Reg::R8d, x64Reg::R9d], - CallConv::WindowsFastCall => vec![x64Reg::Ecx, x64Reg::Edx, x64Reg::R8d, x64Reg::R9d], - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } - - /// Returns the 16Bit intenger argument registers as a vec - pub fn args64(&self) -> Vec { - match self { - CallConv::SystemV => vec![x64Reg::Rdi, x64Reg::Rsi, x64Reg::Rdx, x64Reg::Rcx, x64Reg::R8, x64Reg::R9], - CallConv::WindowsFastCall => vec![x64Reg::Rcx, x64Reg::Rdx, x64Reg::R8, x64Reg::R9], - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } - - /// Returns the return register - pub fn ret16(&self) -> x64Reg { - match self { - CallConv::WindowsFastCall => x64Reg::Ax, - CallConv::SystemV => x64Reg::Ax, - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } - - /// Returns the return register - pub fn ret32(&self) -> x64Reg { - match self { - CallConv::WindowsFastCall =>x64Reg::Eax, - CallConv::SystemV => x64Reg::Eax, - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } - - /// Returns the return register - pub fn ret64(&self) -> x64Reg { - match self { - CallConv::WindowsFastCall => x64Reg::Rax, - CallConv::SystemV => x64Reg::Rax, - CallConv::AppleAarch64 => todo!(), - CallConv::WasmBasicCAbi => todo!(), - } - } -} \ No newline at end of file diff --git a/src/Target/x64/lower.rs b/src/Target/x64/lower.rs index d64bc8c8..63861086 100644 --- a/src/Target/x64/lower.rs +++ b/src/Target/x64/lower.rs @@ -60,8 +60,8 @@ pub(crate) fn x64_lower_instr(conv: CallConv, sink: &mut Vec, instr: MachineMnemonic::Xor => math::x64_lower_xor(sink, &instr), MachineMnemonic::Shl => math::x64_lower_shl(sink, &instr), MachineMnemonic::Shr => math::x64_lower_shr(sink, &instr), - MachineMnemonic::Zext => zext::x64_lower_zext(sink, &instr), - MachineMnemonic::Downcast => downcast::x64_lower_downcast(sink, &instr), + MachineMnemonic::Zext(_) => zext::x64_lower_zext(sink, &instr), + MachineMnemonic::Downcast(_) => downcast::x64_lower_downcast(sink, &instr), MachineMnemonic::Call(to) => call::x64_lower_call(conv, sink, &instr, to), MachineMnemonic::Return => ret::x64_lower_return(sink, &instr), MachineMnemonic::AdressLoad(to) => adr::x64_lower_adr_load(sink, &instr, to), diff --git a/src/Target/x64/lower/call.rs b/src/Target/x64/lower/call.rs index 92b541c8..d3809c05 100644 --- a/src/Target/x64/lower/call.rs +++ b/src/Target/x64/lower/call.rs @@ -5,7 +5,7 @@ use crate::Target::x64::asm::instr::*; pub(crate) fn x64_lower_call(conv: CallConv, sink: &mut Vec, _: &MachineInstr, target: &String) { let func = target; - if conv.reset_eax() { + if conv == CallConv::SystemV{ sink.push( X64MCInstr::with2(Mnemonic::Xor, Operand::Reg(x64Reg::Eax), Operand::Reg(x64Reg::Eax)) ); } diff --git a/src/Target/x64/mod.rs b/src/Target/x64/mod.rs index ef98ab35..62b29691 100644 --- a/src/Target/x64/mod.rs +++ b/src/Target/x64/mod.rs @@ -10,7 +10,6 @@ mod reg; use compilation::construct_compilation_helper; pub use reg::*; -pub(crate) mod call; mod asm; mod lower;