refactor(backend): Remove literal pool
This commit is contained in:
+53
-17
@@ -3,8 +3,9 @@ use std::{fmt::Display, ops::Add};
|
||||
use crate::{backend::register_allocator::{REG_FP, REG_LR, REG_PC, REG_R0, REG_R1, REG_R2, REG_R3, REG_R12, REG_SP, Register}, ir::types::CmpOp as IRCmpOp};
|
||||
pub enum ARMInstr{
|
||||
Move(MoveInstr),
|
||||
Movw(MovwInstr),
|
||||
Movt(MovtInstr),
|
||||
Load(LoadInstr),
|
||||
LoadPseudo(LoadPseudoInstr),
|
||||
Store(StoreInstr),
|
||||
Mul(MulInstr),
|
||||
SDiv(SDivInstr),
|
||||
@@ -24,8 +25,9 @@ impl Display for ARMInstr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ARMInstr::Move(instr) => write!(f, "{}", instr),
|
||||
ARMInstr::Movw(instr) => write!(f, "{}", instr),
|
||||
ARMInstr::Movt(instr) => write!(f, "{}", instr),
|
||||
ARMInstr::Load(instr) => write!(f, "{}", instr),
|
||||
ARMInstr::LoadPseudo(instr) => write!(f, "{}", instr),
|
||||
ARMInstr::Store(instr) => write!(f, "{}", instr),
|
||||
ARMInstr::Mul(instr) => write!(f, "{}", instr),
|
||||
ARMInstr::SDiv(instr) => write!(f, "{}", instr),
|
||||
@@ -45,7 +47,7 @@ impl Display for ARMInstr {
|
||||
pub enum RegisterOrImm {
|
||||
Reg(Register),
|
||||
Imm(i32),
|
||||
}
|
||||
}
|
||||
impl Display for RegisterOrImm {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
@@ -73,7 +75,7 @@ impl From<IRCmpOp> for ConditionCode {
|
||||
IRCmpOp::Ge => ConditionCode::Ge,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Display for ConditionCode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let code_str = match self {
|
||||
@@ -127,6 +129,52 @@ impl Display for MoveInstr {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum MoveWideOperand {
|
||||
Imm(u16),
|
||||
Symbol(String),
|
||||
}
|
||||
|
||||
pub struct MovwInstr(Register, MoveWideOperand);
|
||||
impl MovwInstr {
|
||||
pub fn new_imm(dest: Register, value: u16) -> ARMInstr {
|
||||
ARMInstr::Movw(MovwInstr(dest, MoveWideOperand::Imm(value)))
|
||||
}
|
||||
|
||||
pub fn new_symbol(dest: Register, name: String) -> ARMInstr {
|
||||
ARMInstr::Movw(MovwInstr(dest, MoveWideOperand::Symbol(name)))
|
||||
}
|
||||
}
|
||||
impl Display for MovwInstr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let MovwInstr(dest, operand) = self;
|
||||
match operand {
|
||||
MoveWideOperand::Imm(value) => write!(f, "movw {}, #{}", dest, value),
|
||||
MoveWideOperand::Symbol(name) => write!(f, "movw {}, #:lower16:{}", dest, name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MovtInstr(Register, MoveWideOperand);
|
||||
impl MovtInstr {
|
||||
pub fn new_imm(dest: Register, value: u16) -> ARMInstr {
|
||||
ARMInstr::Movt(MovtInstr(dest, MoveWideOperand::Imm(value)))
|
||||
}
|
||||
|
||||
pub fn new_symbol(dest: Register, name: String) -> ARMInstr {
|
||||
ARMInstr::Movt(MovtInstr(dest, MoveWideOperand::Symbol(name)))
|
||||
}
|
||||
}
|
||||
impl Display for MovtInstr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let MovtInstr(dest, operand) = self;
|
||||
match operand {
|
||||
MoveWideOperand::Imm(value) => write!(f, "movt {}, #{}", dest, value),
|
||||
MoveWideOperand::Symbol(name) => write!(f, "movt {}, #:upper16:{}", dest, name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoadInstr(Register, Register, Option<RegisterOrImm>);
|
||||
impl LoadInstr {
|
||||
pub fn new(dest: Register, base: Register, offset: Option<RegisterOrImm>) -> ARMInstr {
|
||||
@@ -146,18 +194,6 @@ impl Display for LoadInstr {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct LoadPseudoInstr(Register, String);
|
||||
impl LoadPseudoInstr {
|
||||
pub fn new(dest: Register, name: String) -> ARMInstr {
|
||||
ARMInstr::LoadPseudo(LoadPseudoInstr(dest, name))
|
||||
}
|
||||
}
|
||||
impl Display for LoadPseudoInstr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let LoadPseudoInstr(dest, name) = self;
|
||||
write!(f, "ldr {}, ={}", dest, name)
|
||||
}
|
||||
}
|
||||
pub struct StoreInstr(Register, Register, Option<RegisterOrImm>);
|
||||
impl StoreInstr {
|
||||
pub fn new(src: Register, dest: Register, offset: Option<RegisterOrImm>) -> ARMInstr {
|
||||
@@ -347,4 +383,4 @@ impl BInstr {
|
||||
pub fn new_cond(condition: ConditionCode, label_name: String) -> ARMInstr {
|
||||
ARMInstr::B(BInstr(Some(condition), label_name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user