feat: complete Lab3 instruction selection and assembly generation

This commit is contained in:
2026-04-25 14:30:22 +08:00
parent 979d271ebe
commit 0b0bc04be3
13 changed files with 1078 additions and 160 deletions

View File

@@ -19,7 +19,14 @@ class MIRContext {
MIRContext& DefaultContext();
enum class PhysReg { W0, W8, W9, X29, X30, SP };
enum class PhysReg {
W0, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11, W12, W13, W14, W15,
W19, W20, W21, W22, W23, W24, W25, W26, W27, W28,
X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15,
X19, X20, X21, X22, X23, X24, X25, X26, X27, X28,
S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15,
X29, X30, SP
};
const char* PhysRegName(PhysReg reg);
@@ -30,28 +37,57 @@ enum class Opcode {
LoadStack,
StoreStack,
AddRR,
SubRR,
MulRR,
SDivRR,
MSubRRRR,
FAddRRR,
FSubRRR,
FMulRRR,
FDivRRR,
CmpRR,
FCmpRR,
Cset,
B,
BCond,
Call,
Ret,
MovReg,
Adrp,
AddRegImm,
LdrRegReg,
StrRegReg,
SIToFP,
FPToSI,
ZExt
};
class Operand {
public:
enum class Kind { Reg, Imm, FrameIndex };
enum class Kind { Reg, Imm, FrameIndex, Global, Label, Cond };
static Operand Reg(PhysReg reg);
static Operand Imm(int value);
static Operand FrameIndex(int index);
static Operand Global(std::string name);
static Operand Label(std::string name);
static Operand Cond(std::string cond);
Kind GetKind() const { return kind_; }
PhysReg GetReg() const { return reg_; }
int GetImm() const { return imm_; }
int GetFrameIndex() const { return imm_; }
const std::string& GetGlobalName() const { return str_; }
const std::string& GetLabelName() const { return str_; }
const std::string& GetCondCode() const { return str_; }
private:
Operand(Kind kind, PhysReg reg, int imm);
Operand(Kind kind, PhysReg reg, int imm, std::string str = "");
Kind kind_;
PhysReg reg_;
int imm_;
std::string str_;
};
class MachineInstr {
@@ -93,9 +129,12 @@ class MachineFunction {
explicit MachineFunction(std::string name);
const std::string& GetName() const { return name_; }
MachineBasicBlock& GetEntry() { return entry_; }
const MachineBasicBlock& GetEntry() const { return entry_; }
MachineBasicBlock& CreateBlock(std::string name);
std::vector<MachineBasicBlock>& GetBlocks() { return blocks_; }
const std::vector<MachineBasicBlock>& GetBlocks() const { return blocks_; }
// Stack/Frame management
int CreateFrameIndex(int size = 4);
FrameSlot& GetFrameSlot(int index);
const FrameSlot& GetFrameSlot(int index) const;
@@ -106,14 +145,15 @@ class MachineFunction {
private:
std::string name_;
MachineBasicBlock entry_;
std::vector<MachineBasicBlock> blocks_;
std::vector<FrameSlot> frame_slots_;
int frame_size_ = 0;
};
std::unique_ptr<MachineFunction> LowerToMIR(const ir::Module& module);
std::vector<std::unique_ptr<MachineFunction>> LowerToMIR(const ir::Module& module);
void RunRegAlloc(MachineFunction& function);
void RunFrameLowering(MachineFunction& function);
void PrintAsm(const MachineFunction& function, std::ostream& os);
void PrintGlobals(const ir::Module& module, std::ostream& os);
} // namespace mir