Skip to content

Commit

Permalink
[WASM] starting function structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Oct 19, 2024
1 parent a05f47f commit 8e2f805
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 143 deletions.
4 changes: 2 additions & 2 deletions src/CodeGen/compilation/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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(){
MachineMnemonic::Downcast
} else {
return;
}
};
}(node.inner1.ty);

let mut instr = MachineInstr::new(op);

Expand Down
8 changes: 4 additions & 4 deletions src/CodeGen/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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",
Expand Down
107 changes: 59 additions & 48 deletions src/Target/wasm/lower.rs
Original file line number Diff line number Diff line change
@@ -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<super::asm::WasmMCInstr>, 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),
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Target/wasm/lower/br.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::super::asm::*;
use crate::CodeGen::MachineInstr;

pub(crate) fn wasm_lower_br(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr, block: String) {
todo!()
}

pub(crate) fn wasm_lower_brcond(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr, iftrue: String, iffalse: String) {
todo!()
}
6 changes: 6 additions & 0 deletions src/Target/wasm/lower/call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::super::asm::*;
use crate::CodeGen::MachineInstr;

pub(crate) fn wasm_lower_call(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr, func: String) {
todo!()
}
7 changes: 7 additions & 0 deletions src/Target/wasm/lower/cast.rs
Original file line number Diff line number Diff line change
@@ -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<WasmMCInstr>, instr: &MachineInstr, start_ty: TypeMetadata) {
todo!()
}
10 changes: 10 additions & 0 deletions src/Target/wasm/lower/cmove.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::super::asm::*;
use crate::CodeGen::MachineInstr;

pub(crate) fn wasm_lower_cmove(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr) {
todo!()
}

pub(crate) fn wasm_lower_cmovne(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr) {
todo!()
}
6 changes: 6 additions & 0 deletions src/Target/wasm/lower/cmp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::super::asm::*;
use crate::{prelude::CmpMode, CodeGen::MachineInstr};

pub(crate) fn wasm_lower_cmp(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr, mode: CmpMode) {
todo!()
}
22 changes: 22 additions & 0 deletions src/Target/wasm/lower/math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::super::asm::*;
use crate::CodeGen::MachineInstr;

macro_rules! WasmMathLower {
($func:ident) => {
pub(crate) fn $func(sink: &mut Vec<WasmMCInstr>, 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);
6 changes: 6 additions & 0 deletions src/Target/wasm/lower/mov.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::super::asm::*;
use crate::CodeGen::MachineInstr;

pub(crate) fn wasm_lower_mov(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr) {
todo!()
}
6 changes: 6 additions & 0 deletions src/Target/wasm/lower/ret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::super::asm::*;
use crate::CodeGen::MachineInstr;

pub(crate) fn wasm_lower_return(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr) {
todo!()
}
18 changes: 18 additions & 0 deletions src/Target/wasm/lower/stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::super::asm::*;
use crate::CodeGen::MachineInstr;

pub(crate) fn wasm_lower_alloc(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr) {
todo!()
}

pub(crate) fn wasm_lower_store(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr) {
todo!()
}

pub(crate) fn wasm_lower_load(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr) {
todo!()
}

pub(crate) fn wasm_lower_adress_load(sink: &mut Vec<WasmMCInstr>, instr: &MachineInstr, constant: String) {
todo!()
}
7 changes: 7 additions & 0 deletions src/Target/wasm/lower/switch.rs
Original file line number Diff line number Diff line change
@@ -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<WasmMCInstr>, instr: &MachineInstr, cases: Vec<(Type, BlockId)>) {
todo!()
}
85 changes: 0 additions & 85 deletions src/Target/x64/call.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Target/x64/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub(crate) fn x64_lower_instr(conv: CallConv, sink: &mut Vec<X64MCInstr>, 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),
Expand Down
2 changes: 1 addition & 1 deletion src/Target/x64/lower/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::Target::x64::asm::instr::*;
pub(crate) fn x64_lower_call(conv: CallConv, sink: &mut Vec<X64MCInstr>, _: &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)) );
}

Expand Down
1 change: 0 additions & 1 deletion src/Target/x64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod reg;
use compilation::construct_compilation_helper;
pub use reg::*;

pub(crate) mod call;
mod asm;
mod lower;

Expand Down

0 comments on commit 8e2f805

Please sign in to comment.