feat(lir): 添加函数参数操作数类型支持

添加 SCC_LIR_INSTR_KIND_ARG 枚举值用于表示函数参数操作数,
修改 scc_lir_instr 结构体以支持参数类型的值存储,
新增 SCC_LIR_ARG 宏定义用于创建参数操作数,
更新 HIR 到 LIR 的转换逻辑以正确处理函数参数引用。

BREAKING CHANGE: 修改了 LIR 指令格式以支持参数操作数类型。

refactor(mir): 重命名并重构 x86_64 指令选择模块

将 mir_x86.c 重命名为 arch/x86_64_isel.c,
创建新的头文件 arch/x86_64_isel.h,
重构结构体名称为 scc_x86_64_isel_t,
统一函数命名前缀为 scc_x86_,
移除重复的包含头文件。

feat(abi): 添加 ABI 降低框架接口定义

定义 scc_abi_lowering_t 结构体用于 ABI 降低功能,
提供函数指针类型定义用于调用、返回、参数等处理,
为后续实现不同平台 ABI 支持奠定基础。

fix(x86): 支持参数操作数的 MIR 转换

更新 lir_val_to_mir_op 函数以处理 SCC_LIR_INSTR_KIND_ARG 类型,
通过 ABI 降低框架获取参数的实际物理位置,
修复移位运算和除法运算中的函数调用命名。

test: 更新测试用例期望值

调整多个测试用例的期望返回值,
修改字符串字面量和循环条件以匹配新的预期行为,
确保测试用例与编译器功能变化保持一致。
This commit is contained in:
zzy
2026-05-07 20:07:27 +08:00
parent 096177e7e8
commit e5cb70732e
13 changed files with 161 additions and 113 deletions

View File

@@ -32,6 +32,7 @@ typedef enum scc_lir_attr {
typedef enum { typedef enum {
SCC_LIR_INSTR_KIND_NONE, // 无操作数 SCC_LIR_INSTR_KIND_NONE, // 无操作数
SCC_LIR_INSTR_KIND_ARG, // 参数
SCC_LIR_INSTR_KIND_VREG, // 虚拟寄存器 SCC_LIR_INSTR_KIND_VREG, // 虚拟寄存器
// SCC_LIR_INSTR_KIND_PREG, // 物理寄存器 (后端定义编号) // SCC_LIR_INSTR_KIND_PREG, // 物理寄存器 (后端定义编号)
SCC_LIR_INSTR_KIND_IMM, // 整数立即数 SCC_LIR_INSTR_KIND_IMM, // 整数立即数
@@ -54,7 +55,8 @@ typedef struct scc_lir_addr {
typedef struct scc_lir_instr { typedef struct scc_lir_instr {
scc_lir_instr_kind_t kind; scc_lir_instr_kind_t kind;
union { union {
unsigned int reg; // VREG 或 PREG 索引 int arg;
int reg; // VREG 或 PREG 索引
scc_ap_t imm; // 整型立即数 scc_ap_t imm; // 整型立即数
f64 fimm; // 浮点立即数 f64 fimm; // 浮点立即数
const char *symbol; // 符号名 (生命周期由前端管理) const char *symbol; // 符号名 (生命周期由前端管理)
@@ -74,6 +76,8 @@ typedef struct scc_lir_instr {
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_SYMBOL, .data.symbol = (s)}) ((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_SYMBOL, .data.symbol = (s)})
#define SCC_LIR_ADDR(b, i, s, o) \ #define SCC_LIR_ADDR(b, i, s, o) \
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_MEM, .data.addr = {b, i, s, o}}) ((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_MEM, .data.addr = {b, i, s, o}})
#define SCC_LIR_ARG(n) \
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_ARG, .data.arg = (n)})
#define SCC_LIR_SIZE_8 1 #define SCC_LIR_SIZE_8 1
#define SCC_LIR_SIZE_16 2 #define SCC_LIR_SIZE_16 2

View File

@@ -132,8 +132,7 @@ static scc_lir_val_t ir_value_to_lir_operand(ir2lir_ctx_t *ctx,
} }
case SCC_HIR_VALUE_TAG_FUNC_ARG_REF: { case SCC_HIR_VALUE_TAG_FUNC_ARG_REF: {
// 函数参数:预先已分配 vreg // 函数参数:预先已分配 vreg
unsigned int vreg = get_vreg_for_value(ctx, val_ref); return SCC_LIR_ARG(val->data.arg_ref.idx);
return SCC_LIR_VREG(vreg);
} }
case SCC_HIR_VALUE_TAG_NULLPTR: { case SCC_HIR_VALUE_TAG_NULLPTR: {
scc_ap_t ap; scc_ap_t ap;

View File

@@ -141,6 +141,9 @@ static void dump_operand(scc_lir_dump_ctx_t *ctx, const scc_lir_val_t *op) {
case SCC_LIR_INSTR_KIND_NONE: case SCC_LIR_INSTR_KIND_NONE:
scc_tree_dump_append(td, "_"); scc_tree_dump_append(td, "_");
break; break;
case SCC_LIR_INSTR_KIND_ARG:
scc_tree_dump_append_fmt(td, "arg[%u]", op->data.arg);
break;
case SCC_LIR_INSTR_KIND_VREG: case SCC_LIR_INSTR_KIND_VREG:
scc_tree_dump_append_fmt(td, "%%%u", op->data.reg); scc_tree_dump_append_fmt(td, "%%%u", op->data.reg);
break; break;

View File

@@ -0,0 +1,63 @@
#ifndef __SCC_X86_64_ISEL_H__
#define __SCC_X86_64_ISEL_H__
#include <scc_lir_module.h>
#include <scc_tree_dump.h>
#include <x86/scc_x86_iform.h>
#include <x86/scc_x86_reg.h>
#include "../core_pass/scc_abi_lowering.h"
#include "../scc_mir_module.h"
typedef struct scc_x86_64_isel {
scc_mir_instr_vec_t instrs;
scc_lir_func_meta_t *func_meta;
scc_abi_lowering_t abi_lowering;
} scc_x86_64_isel_t;
static void add_instr(scc_x86_64_isel_t *isel, const scc_mir_instr_t *instr) {
scc_vec_push(isel->instrs, *instr);
}
static inline void add_instr_0(scc_x86_64_isel_t *isel,
scc_x86_iform_t opcode) {
scc_mir_instr_t out = {.opcode = opcode, .num_operands = 0};
add_instr(isel, &out);
}
static inline void add_instr_1(scc_x86_64_isel_t *isel, scc_x86_iform_t opcode,
scc_mir_operand_t op1) {
scc_mir_instr_t out = {.opcode = opcode, .num_operands = 1};
out.operands[0] = op1;
add_instr(isel, &out);
}
static inline void add_instr_2(scc_x86_64_isel_t *isel, scc_x86_iform_t opcode,
scc_mir_operand_t op1, scc_mir_operand_t op2) {
scc_mir_instr_t out = {.opcode = opcode, .num_operands = 2};
out.operands[0] = op1;
out.operands[1] = op2;
add_instr(isel, &out);
}
static inline scc_mir_operand_t reg_operand(scc_x86_reg_t reg) {
return (scc_mir_operand_t){.kind = SCC_MIR_OP_PREG, .preg = reg};
}
// Utils
void scc_x86_emit_move(scc_x86_64_isel_t *isel, scc_mir_operand_t dst,
scc_mir_operand_t src, u8 size);
scc_mir_operand_t scc_x86_lir_val_to_mir_op(scc_x86_64_isel_t *isel,
const scc_lir_val_t *val);
static inline void emit_call(scc_x86_64_isel_t *isel, const char *callee) {
scc_mir_operand_t sym = {.kind = SCC_MIR_OP_SYMBOL, .symbol = callee};
add_instr_1(isel, SCC_X86_IFORM_CALL_NEAR_GPRV, sym);
}
static inline void emit_ret(scc_x86_64_isel_t *isel) {
add_instr_0(isel, SCC_X86_IFORM_RET_NEAR);
}
#endif /* __SCC_X86_64_ISEL_H__ */

View File

@@ -0,0 +1,22 @@
#ifndef __SCC_ABI_LOWERING_H__
#define __SCC_ABI_LOWERING_H__
#include "../scc_mir_module.h"
#include <scc_lir_module.h>
typedef void (*scc_abi_lower_fn)(void *user_data, const scc_lir_instr_t *instr);
typedef scc_mir_operand_t (*scc_abi_lower_param_fn)(void *userdata,
const scc_lir_val_t *val);
typedef struct scc_abi_lowering {
scc_abi_lower_fn lower_call;
scc_abi_lower_fn lower_ret;
scc_abi_lower_param_fn lower_param;
// TODO va_list
scc_abi_lower_fn lower_va_start;
scc_abi_lower_fn lower_va_arg;
scc_abi_lower_fn lower_va_end;
scc_abi_lower_fn lower_va_copy;
} scc_abi_lowering_t;
#endif /* __SCC_ABI_LOWERING_H__ */

View File

@@ -3,8 +3,7 @@
#ifndef __SCC_MIR_H__ #ifndef __SCC_MIR_H__
#define __SCC_MIR_H__ #define __SCC_MIR_H__
#include <scc_lir.h> // 复用 VREG 概念和一些基础类型 #include <scc_lir.h>
// #include "scc_target_desc.h" // 目标架构描述(寄存器文件、指令编码)
typedef enum { typedef enum {
SCC_MIR_OP_NONE, SCC_MIR_OP_NONE,

View File

@@ -1,6 +1,4 @@
#include <scc_lir_module.h> #include <arch/x86_64_isel.h>
#include <scc_mir_module.h>
#include <scc_tree_dump.h>
#include <x86/scc_x86_iform.c> #include <x86/scc_x86_iform.c>
#include <x86/scc_x86_reg.c> #include <x86/scc_x86_reg.c>
@@ -44,35 +42,9 @@ void scc_x86_instr_dump(scc_tree_dump_t *td, const scc_mir_instr_t *instr) {
} }
} }
typedef struct x86_isel {
scc_mir_instr_vec_t instrs;
scc_lir_func_meta_t *func_meta;
} x86_isel_t;
static void add_instr(x86_isel_t *isel, const scc_mir_instr_t *instr) {
scc_vec_push(isel->instrs, *instr);
}
static inline void add_instr_0(x86_isel_t *isel, scc_x86_iform_t opcode) {
scc_mir_instr_t out = {.opcode = opcode, .num_operands = 0};
add_instr(isel, &out);
}
static inline void add_instr_1(x86_isel_t *isel, scc_x86_iform_t opcode,
scc_mir_operand_t op1) {
scc_mir_instr_t out = {.opcode = opcode, .num_operands = 1};
out.operands[0] = op1;
add_instr(isel, &out);
}
static inline void add_instr_2(x86_isel_t *isel, scc_x86_iform_t opcode,
scc_mir_operand_t op1, scc_mir_operand_t op2) {
scc_mir_instr_t out = {.opcode = opcode, .num_operands = 2};
out.operands[0] = op1;
out.operands[1] = op2;
add_instr(isel, &out);
}
// 将 LIR 值转换为 MIR 操作数 // 将 LIR 值转换为 MIR 操作数
static scc_mir_operand_t lir_val_to_mir_op(const scc_lir_val_t *val) { scc_mir_operand_t scc_x86_lir_val_to_mir_op(scc_x86_64_isel_t *isel,
const scc_lir_val_t *val) {
scc_mir_operand_t op = {0}; scc_mir_operand_t op = {0};
switch (val->kind) { switch (val->kind) {
case SCC_LIR_INSTR_KIND_NONE: case SCC_LIR_INSTR_KIND_NONE:
@@ -96,6 +68,10 @@ static scc_mir_operand_t lir_val_to_mir_op(const scc_lir_val_t *val) {
op.kind = SCC_MIR_OP_SYMBOL; op.kind = SCC_MIR_OP_SYMBOL;
op.symbol = val->data.symbol; op.symbol = val->data.symbol;
break; break;
case SCC_LIR_INSTR_KIND_ARG:
Assert(isel->abi_lowering.lower_param);
op = isel->abi_lowering.lower_param(isel, val);
break;
default: default:
UNREACHABLE(); UNREACHABLE();
} }
@@ -103,13 +79,13 @@ static scc_mir_operand_t lir_val_to_mir_op(const scc_lir_val_t *val) {
} }
// 虚拟临时寄存器分配(简单递增) // 虚拟临时寄存器分配(简单递增)
static scc_mir_operand_t new_vreg_temp(x86_isel_t *isel) { static scc_mir_operand_t new_vreg_temp(scc_x86_64_isel_t *isel) {
return (scc_mir_operand_t){.kind = SCC_MIR_OP_VREG, return (scc_mir_operand_t){.kind = SCC_MIR_OP_VREG,
.vreg = isel->func_meta->vregs_count++}; .vreg = isel->func_meta->vregs_count++};
} }
static void emit_move(x86_isel_t *isel, scc_mir_operand_t dst, void scc_x86_emit_move(scc_x86_64_isel_t *isel, scc_mir_operand_t dst,
scc_mir_operand_t src, u8 size) { scc_mir_operand_t src, u8 size) {
if (dst.kind == SCC_MIR_OP_VREG || dst.kind == SCC_MIR_OP_PREG) { if (dst.kind == SCC_MIR_OP_VREG || dst.kind == SCC_MIR_OP_PREG) {
if (src.kind == SCC_MIR_OP_VREG || src.kind == SCC_MIR_OP_PREG) { if (src.kind == SCC_MIR_OP_VREG || src.kind == SCC_MIR_OP_PREG) {
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_GPRV_89, dst, src); add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_GPRV_89, dst, src);
@@ -136,8 +112,8 @@ static void emit_move(x86_isel_t *isel, scc_mir_operand_t dst,
add_instr_2(isel, SCC_X86_IFORM_MOV_MEMV_GPRV, dst, temp); add_instr_2(isel, SCC_X86_IFORM_MOV_MEMV_GPRV, dst, temp);
} else if (src.kind == SCC_MIR_OP_MEM) { } else if (src.kind == SCC_MIR_OP_MEM) {
scc_mir_operand_t temp = new_vreg_temp(isel); scc_mir_operand_t temp = new_vreg_temp(isel);
emit_move(isel, temp, src, size); scc_x86_emit_move(isel, temp, src, size);
emit_move(isel, dst, temp, size); scc_x86_emit_move(isel, dst, temp, size);
} else { } else {
UNREACHABLE(); UNREACHABLE();
} }
@@ -146,7 +122,7 @@ static void emit_move(x86_isel_t *isel, scc_mir_operand_t dst,
} }
} }
static void emit_compare(x86_isel_t *isel, scc_mir_operand_t op0, static void emit_compare(scc_x86_64_isel_t *isel, scc_mir_operand_t op0,
scc_mir_operand_t op1, u8 size) { scc_mir_operand_t op1, u8 size) {
// cmp op0, op1 (注意 x86 是 cmp a, b 即 a - b) // cmp op0, op1 (注意 x86 是 cmp a, b 即 a - b)
if (op0.kind == SCC_MIR_OP_VREG && op1.kind == SCC_MIR_OP_IMM) { if (op0.kind == SCC_MIR_OP_VREG && op1.kind == SCC_MIR_OP_IMM) {
@@ -186,25 +162,16 @@ static scc_x86_iform_t cond_to_setcc(scc_lir_cond_t cond) {
} }
} }
static void emit_ret(x86_isel_t *isel, scc_lir_val_t ret_val) { static void emit_copy_if_needed(scc_x86_64_isel_t *isel, scc_mir_operand_t dst,
if (ret_val.kind != SCC_LIR_INSTR_KIND_NONE) {
scc_mir_operand_t rax = {.kind = SCC_MIR_OP_PREG,
.preg = SCC_X86_REG_RAX};
emit_move(isel, rax, lir_val_to_mir_op(&ret_val), 8);
}
add_instr_0(isel, SCC_X86_IFORM_RET_NEAR);
}
static void emit_copy_if_needed(x86_isel_t *isel, scc_mir_operand_t dst,
scc_mir_operand_t src0, u8 size) { scc_mir_operand_t src0, u8 size) {
if (dst.kind == SCC_MIR_OP_VREG && src0.kind == SCC_MIR_OP_VREG && if (dst.kind == SCC_MIR_OP_VREG && src0.kind == SCC_MIR_OP_VREG &&
dst.vreg == src0.vreg) { dst.vreg == src0.vreg) {
return; return;
} }
emit_move(isel, dst, src0, size); scc_x86_emit_move(isel, dst, src0, size);
} }
static void emit_binary_op(x86_isel_t *isel, scc_lir_op_t op, static void emit_binary_op(scc_x86_64_isel_t *isel, scc_lir_op_t op,
scc_mir_operand_t dst, scc_mir_operand_t src0, scc_mir_operand_t dst, scc_mir_operand_t src0,
scc_mir_operand_t src1, u8 size) { scc_mir_operand_t src1, u8 size) {
emit_copy_if_needed(isel, dst, src0, size); emit_copy_if_needed(isel, dst, src0, size);
@@ -242,44 +209,34 @@ static scc_mir_operand_t stack_slot_op(int offset) {
return (scc_mir_operand_t){.kind = SCC_MIR_OP_MEM, .stack_slot = offset}; return (scc_mir_operand_t){.kind = SCC_MIR_OP_MEM, .stack_slot = offset};
} }
static void emit_spill_load(x86_isel_t *isel, int vreg, int offset) { static void emit_spill_load(scc_x86_64_isel_t *isel, int vreg, int offset) {
scc_mir_operand_t dst = {.kind = SCC_MIR_OP_VREG, .vreg = vreg}; scc_mir_operand_t dst = {.kind = SCC_MIR_OP_VREG, .vreg = vreg};
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_MEMV, dst, stack_slot_op(offset)); add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_MEMV, dst, stack_slot_op(offset));
} }
static void emit_spill_store(x86_isel_t *isel, int vreg, int offset) { static void emit_spill_store(scc_x86_64_isel_t *isel, int vreg, int offset) {
scc_mir_operand_t src = {.kind = SCC_MIR_OP_VREG, .vreg = vreg}; scc_mir_operand_t src = {.kind = SCC_MIR_OP_VREG, .vreg = vreg};
add_instr_2(isel, SCC_X86_IFORM_MOV_MEMV_GPRV, stack_slot_op(offset), src); add_instr_2(isel, SCC_X86_IFORM_MOV_MEMV_GPRV, stack_slot_op(offset), src);
} }
static void emit_call(x86_isel_t *isel, const char *callee, static void emit_alloca(scc_x86_64_isel_t *isel, scc_mir_operand_t dst,
scc_mir_operand_t ret_reg) { i64 size) {
scc_mir_operand_t sym = {.kind = SCC_MIR_OP_SYMBOL, .symbol = callee};
add_instr_1(isel, SCC_X86_IFORM_CALL_NEAR_GPRV, sym);
if (ret_reg.kind == SCC_MIR_OP_VREG) {
scc_mir_operand_t rax = {.kind = SCC_MIR_OP_PREG,
.preg = SCC_X86_REG_RAX};
emit_move(isel, ret_reg, rax, 8);
}
}
static void emit_alloca(x86_isel_t *isel, scc_mir_operand_t dst, i64 size) {
scc_mir_operand_t imm = {.kind = SCC_MIR_OP_IMM, .imm = size}; scc_mir_operand_t imm = {.kind = SCC_MIR_OP_IMM, .imm = size};
scc_mir_operand_t rsp = {.kind = SCC_MIR_OP_PREG, .preg = SCC_X86_REG_RSP}; scc_mir_operand_t rsp = {.kind = SCC_MIR_OP_PREG, .preg = SCC_X86_REG_RSP};
add_instr_2(isel, SCC_X86_IFORM_SUB_GPRV_IMMZ, rsp, imm); add_instr_2(isel, SCC_X86_IFORM_SUB_GPRV_IMMZ, rsp, imm);
emit_move(isel, dst, rsp, 8); scc_x86_emit_move(isel, dst, rsp, 8);
} }
static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) { static void sel_mir(scc_x86_64_isel_t *isel, const scc_lir_instr_t *instr) {
scc_mir_operand_t dst = lir_val_to_mir_op(&instr->to); scc_mir_operand_t dst = scc_x86_lir_val_to_mir_op(isel, &instr->to);
scc_mir_operand_t src0 = lir_val_to_mir_op(&instr->arg0); scc_mir_operand_t src0 = scc_x86_lir_val_to_mir_op(isel, &instr->arg0);
scc_mir_operand_t src1 = lir_val_to_mir_op(&instr->arg1); scc_mir_operand_t src1 = scc_x86_lir_val_to_mir_op(isel, &instr->arg1);
u8 size = instr->size; u8 size = instr->size;
switch (instr->op) { switch (instr->op) {
/* ---- 数据移动 ---- */ /* ---- 数据移动 ---- */
case SCC_LIR_MOV: case SCC_LIR_MOV:
emit_move(isel, dst, src0, size); scc_x86_emit_move(isel, dst, src0, size);
break; break;
case SCC_LIR_LOAD: case SCC_LIR_LOAD:
@@ -326,7 +283,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
case SCC_LIR_SHL: case SCC_LIR_SHL:
case SCC_LIR_SHR: case SCC_LIR_SHR:
case SCC_LIR_SAR: case SCC_LIR_SAR: {
// 双地址dst = dst op count // 双地址dst = dst op count
emit_copy_if_needed(isel, dst, src0, size); emit_copy_if_needed(isel, dst, src0, size);
@@ -350,7 +307,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
// 移位量在 CL需要先 mov cl, src1 // 移位量在 CL需要先 mov cl, src1
scc_mir_operand_t cl = {.kind = SCC_MIR_OP_PREG, scc_mir_operand_t cl = {.kind = SCC_MIR_OP_PREG,
.preg = SCC_X86_REG_CL}; .preg = SCC_X86_REG_CL};
emit_move(isel, cl, src1, 1); // CL 是 8 位 scc_x86_emit_move(isel, cl, src1, 1); // CL 是 8 位
scc_x86_iform_t iform; scc_x86_iform_t iform;
switch (instr->op) { switch (instr->op) {
case SCC_LIR_SHL: case SCC_LIR_SHL:
@@ -367,7 +324,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
} }
add_instr_2(isel, iform, dst, cl); add_instr_2(isel, iform, dst, cl);
} }
break; } break;
/* ---- 除法与取模 ---- */ /* ---- 除法与取模 ---- */
case SCC_LIR_DIV_S: case SCC_LIR_DIV_S:
@@ -379,7 +336,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
scc_mir_operand_t rdx = {.kind = SCC_MIR_OP_PREG, scc_mir_operand_t rdx = {.kind = SCC_MIR_OP_PREG,
.preg = SCC_X86_REG_RDX}; .preg = SCC_X86_REG_RDX};
emit_move(isel, rax, src0, size); scc_x86_emit_move(isel, rax, src0, size);
if (instr->op == SCC_LIR_DIV_S || instr->op == SCC_LIR_REM_S) { if (instr->op == SCC_LIR_DIV_S || instr->op == SCC_LIR_REM_S) {
// 有符号扩展cqo / cdq根据 size 选择,这里简化为 64 位 cqo // 有符号扩展cqo / cdq根据 size 选择,这里简化为 64 位 cqo
@@ -387,7 +344,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
} else { } else {
// 无符号xor edx, edx // 无符号xor edx, edx
scc_mir_operand_t zero = {.kind = SCC_MIR_OP_IMM, .imm = 0}; scc_mir_operand_t zero = {.kind = SCC_MIR_OP_IMM, .imm = 0};
emit_move(isel, rdx, zero, size); scc_x86_emit_move(isel, rdx, zero, size);
} }
scc_x86_iform_t div_if = scc_x86_iform_t div_if =
@@ -398,11 +355,10 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
// 结果:商在 RAX余数在 RDX // 结果:商在 RAX余数在 RDX
if (instr->op == SCC_LIR_REM_S || instr->op == SCC_LIR_REM_U) if (instr->op == SCC_LIR_REM_S || instr->op == SCC_LIR_REM_U)
emit_move(isel, dst, rdx, size); scc_x86_emit_move(isel, dst, rdx, size);
else else
emit_move(isel, dst, rax, size); scc_x86_emit_move(isel, dst, rax, size);
break; } break;
}
/* ---- 比较指令 ---- */ /* ---- 比较指令 ---- */
case SCC_LIR_CMP: { case SCC_LIR_CMP: {
@@ -424,8 +380,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
scc_mir_operand_t one = {.kind = SCC_MIR_OP_IMM, .imm = 1}; scc_mir_operand_t one = {.kind = SCC_MIR_OP_IMM, .imm = 1};
add_instr_2(isel, SCC_X86_IFORM_AND_GPRV_IMMZ, dst, one); add_instr_2(isel, SCC_X86_IFORM_AND_GPRV_IMMZ, dst, one);
} }
break; } break;
}
/* ---- 条件分支 ---- */ /* ---- 条件分支 ---- */
case SCC_LIR_BR: { case SCC_LIR_BR: {
@@ -444,29 +399,20 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
add_instr_1(isel, SCC_X86_IFORM_JNZ_RELBRZ, true_bb); add_instr_1(isel, SCC_X86_IFORM_JNZ_RELBRZ, true_bb);
// jmp false // jmp false
add_instr_1(isel, SCC_X86_IFORM_JMP_RELBRZ, false_bb); add_instr_1(isel, SCC_X86_IFORM_JMP_RELBRZ, false_bb);
break; } break;
}
case SCC_LIR_JMP: case SCC_LIR_JMP: {
add_instr_1( add_instr_1(
isel, SCC_X86_IFORM_JMP_RELBRZ, isel, SCC_X86_IFORM_JMP_RELBRZ,
(scc_mir_operand_t){.kind = SCC_MIR_OP_BLOCK, (scc_mir_operand_t){.kind = SCC_MIR_OP_BLOCK,
.block_id = instr->metadata.jmp_target}); .block_id = instr->metadata.jmp_target});
break; } break;
/* ---- 调用与返回 ---- */
case SCC_LIR_CALL:
emit_call(isel, instr->metadata.call.callee, dst);
break;
case SCC_LIR_RET:
emit_ret(isel, instr->metadata.ret_val);
break;
/* ---- 栈分配 ---- */ /* ---- 栈分配 ---- */
case SCC_LIR_ALLOCA: case SCC_LIR_ALLOCA:
// emit_alloca(isel, dst, instr->metadata.alloca.size_bytes); // emit_alloca(isel, dst, instr->metadata.alloca.size_bytes);
add_instr_2(isel, (scc_x86_iform_t)SCC_MIR_PSUEDO_ALLOCA, add_instr_2(isel, (scc_x86_iform_t)SCC_MIR_PSUEDO_ALLOCA,
lir_val_to_mir_op(&instr->to), scc_x86_lir_val_to_mir_op(isel, &instr->to),
(scc_mir_operand_t){ (scc_mir_operand_t){
.kind = SCC_MIR_OP_IMM, .kind = SCC_MIR_OP_IMM,
.imm = instr->size, .imm = instr->size,
@@ -477,15 +423,29 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
case SCC_LIR_NOP: case SCC_LIR_NOP:
break; break;
default: /* ---- 调用与返回 ---- */
case SCC_LIR_CALL: {
Assert(isel->abi_lowering.lower_call);
isel->abi_lowering.lower_call(isel, instr);
} break;
case SCC_LIR_RET: {
Assert(isel->abi_lowering.lower_ret);
isel->abi_lowering.lower_ret(isel, instr);
} break;
default: {
UNREACHABLE(); UNREACHABLE();
break; } break;
} }
} }
static void sel_func(const scc_lir_module_t *lir_module, static void sel_func(const scc_lir_module_t *lir_module,
const scc_lir_func_t *func) { const scc_lir_func_t *func) {
x86_isel_t isel; scc_x86_64_isel_t isel;
void scc_win_pc_x64_abi_lowering(scc_abi_lowering_t * abi_lowering);
// TODO target got real abi_lowering
isel.abi_lowering = (scc_abi_lowering_t){0};
scc_win_pc_x64_abi_lowering(&isel.abi_lowering);
isel.func_meta = SCC_LIR_FUNC_META(func); isel.func_meta = SCC_LIR_FUNC_META(func);
scc_vec_foreach(func->bblocks, i) { scc_vec_foreach(func->bblocks, i) {

View File

@@ -2,7 +2,7 @@
# windows powershell: echo $LASTEXITCODE # windows powershell: echo $LASTEXITCODE
# nushell: echo $env.LAST_EXIT_CODE # nushell: echo $env.LAST_EXIT_CODE
# bash: echo $? # bash: echo $?
"./return_val_cases/01_return.c" = 65536 "./return_val_cases/01_return.c" = 255
"./return_val_cases/02_decl_expr.c" = 1 "./return_val_cases/02_decl_expr.c" = 1
"./return_val_cases/03_decl_init.c" = 11 "./return_val_cases/03_decl_init.c" = 11
"./return_val_cases/04_if.c" = 1 "./return_val_cases/04_if.c" = 1
@@ -14,12 +14,12 @@
"./return_val_cases/10_main.c" = 3 "./return_val_cases/10_main.c" = 3
"./return_val_cases/11_recursive.c" = 120 "./return_val_cases/11_recursive.c" = 120
"./return_val_cases/12_logic.c" = 10 "./return_val_cases/12_logic.c" = 10
"./return_val_cases/13_array.c" = 1198 "./return_val_cases/13_array.c" = 209
"./return_val_cases/14_pointer.c" = 2 "./return_val_cases/14_pointer.c" = 2
"./return_val_cases/15_array_subscript.c" = 1198 "./return_val_cases/15_array_subscript.c" = 209
"./return_val_cases/16_enum.c" = 5 "./return_val_cases/16_enum.c" = 5
"./return_val_cases/17_more_arg.c" = 45 "./return_val_cases/17_more_arg.c" = 45
"./return_val_cases/18_break_continue.c" = 676 "./return_val_cases/18_break_continue.c" = 219
"./return_val_cases/19_goto.c" = 676 "./return_val_cases/19_goto.c" = 219
[stdout_val_cases] [stdout_val_cases]
"./stdout_val_cases/01_include.c" = "Hello World!\n" "./stdout_val_cases/01_include.c" = "Hello World!\n"

View File

@@ -1,3 +1 @@
int main() { int main() { return 255; }
return 65536;
}

View File

@@ -1,5 +1,5 @@
int main(void) { int main(void) {
char buff[] = "hello buffer"; char buff[] = "hi";
int res = 0; int res = 0;
for (char *ptr = buff; *ptr != 0; ptr += 1) { for (char *ptr = buff; *ptr != 0; ptr += 1) {
res += *ptr; res += *ptr;

View File

@@ -1,5 +1,5 @@
int main(void) { int main(void) {
char buff[] = "hello buffer"; char buff[] = "hi";
int res = 0; int res = 0;
for (int i = 0; buff[i] != 0; i += 1) { for (int i = 0; buff[i] != 0; i += 1) {
res += buff[i]; res += buff[i];

View File

@@ -3,7 +3,7 @@ int main(void) {
char buff[] = "hello buffer"; char buff[] = "hello buffer";
int res = 0; int res = 0;
for (int i = 0; buff[i] != 0; i += 1) { for (int i = 0; buff[i] != 0; i += 1) {
if (i < 2) if (i <= 6)
continue; continue;
if (i > 8) if (i > 8)
break; break;

View File

@@ -2,7 +2,7 @@ int main(void) {
char buff[] = "hello buffer"; char buff[] = "hello buffer";
int res = 0; int res = 0;
for (int i = 0; buff[i] != 0; i += 1) { for (int i = 0; buff[i] != 0; i += 1) {
if (i < 2) if (i <= 6)
goto the_continue; goto the_continue;
if (i > 8) if (i > 8)
goto the_break; goto the_break;