Compare commits
2 Commits
aa8a1ff8ce
...
e5cb70732e
| Author | SHA1 | Date | |
|---|---|---|---|
| e5cb70732e | |||
| 096177e7e8 |
@@ -609,8 +609,9 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
else
|
||||
return scc_hir_builder_load(&ctx->builder, field_ptr);
|
||||
}
|
||||
case SCC_AST_EXPR_CAST:
|
||||
break;
|
||||
case SCC_AST_EXPR_CAST: {
|
||||
TODO();
|
||||
} break;
|
||||
case SCC_AST_EXPR_SIZE_OF: {
|
||||
TODO();
|
||||
// return scc_hir_builder_integer(
|
||||
@@ -622,10 +623,14 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
// &ctx->builder, scc_hir_builder_type_u64(&ctx->builder), val);
|
||||
}
|
||||
case SCC_AST_EXPR_COMPOUND: {
|
||||
TODO();
|
||||
} break;
|
||||
case SCC_AST_EXPR_LVALUE: {
|
||||
TODO();
|
||||
} break;
|
||||
case SCC_AST_EXPR_BUILTIN: {
|
||||
TODO();
|
||||
} break;
|
||||
case SCC_AST_EXPR_LVALUE:
|
||||
break;
|
||||
// SCC_AST_EXPR_BUILTIN,// 内置表达式 ... directive map to ir builtin
|
||||
case SCC_AST_EXPR_INT_LITERAL: {
|
||||
// FIXME maybe using some array to int;
|
||||
scc_hir_type_ref_t type_ref = scc_hir_builder_type_i32(&ctx->builder);
|
||||
@@ -884,9 +889,17 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt) {
|
||||
scc_hir_builder_set_current_bblock(&ctx->builder, exit_block);
|
||||
break;
|
||||
}
|
||||
// SCC_AST_STMT_SWITCH, // switch 语句
|
||||
// SCC_AST_STMT_CASE, // case 语句
|
||||
// SCC_AST_STMT_DEFAULT, // default 语句
|
||||
case SCC_AST_STMT_SWITCH: {
|
||||
scc_hir_value_ref_t exit_block =
|
||||
scc_hir_builder_bblock(&ctx->builder, "switch_exit");
|
||||
scc_hashtable_set(&ctx->break_cache, stmt, (void *)exit_block);
|
||||
|
||||
// TODO:
|
||||
} break;
|
||||
case SCC_AST_STMT_CASE:
|
||||
case SCC_AST_STMT_DEFAULT:
|
||||
UNREACHABLE();
|
||||
break;
|
||||
case SCC_AST_STMT_BREAK: {
|
||||
scc_hir_bblock_ref_t target =
|
||||
(usize)scc_hashtable_get(&ctx->break_cache, stmt->jump._target);
|
||||
@@ -1098,7 +1111,11 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl,
|
||||
&ctx->builder, scc_hir_builder_type_i32(&ctx->builder), &val);
|
||||
scc_hashtable_set(&ctx->ast2ir_cache, item,
|
||||
(void *)(usize)item_val_ref);
|
||||
scc_ap_set_int(&val, ++idx);
|
||||
// FIXME hack ap
|
||||
idx = val.data.digit;
|
||||
idx += 1;
|
||||
// scc_ap_add 1 ();
|
||||
scc_ap_set_int(&val, idx);
|
||||
}
|
||||
} break;
|
||||
case SCC_AST_DECL_TYPEDEF:
|
||||
|
||||
@@ -455,12 +455,12 @@ static void format_ref_or_value(scc_hir_dump_t *ctx,
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%%%u", value_ref);
|
||||
return;
|
||||
}
|
||||
// FIXME
|
||||
// if (value->tag == SCC_HIR_VALUE_TAG_INTEGER) {
|
||||
// scc_tree_dump_append_fmt(ctx->dump_ctx, "%d",
|
||||
// value->data.const_int.int32);
|
||||
// return;
|
||||
// }
|
||||
if (value->tag == SCC_HIR_VALUE_TAG_INTEGER) {
|
||||
// FIXME hack ap
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%d",
|
||||
value->data.integer.data.digit);
|
||||
return;
|
||||
}
|
||||
if (value->name && value->name[0] != '\0') {
|
||||
scc_tree_dump_node(ctx->dump_ctx, "%%%u[%s]", value_ref, value->name);
|
||||
} else {
|
||||
|
||||
@@ -32,6 +32,7 @@ typedef enum scc_lir_attr {
|
||||
|
||||
typedef enum {
|
||||
SCC_LIR_INSTR_KIND_NONE, // 无操作数
|
||||
SCC_LIR_INSTR_KIND_ARG, // 参数
|
||||
SCC_LIR_INSTR_KIND_VREG, // 虚拟寄存器
|
||||
// SCC_LIR_INSTR_KIND_PREG, // 物理寄存器 (后端定义编号)
|
||||
SCC_LIR_INSTR_KIND_IMM, // 整数立即数
|
||||
@@ -54,7 +55,8 @@ typedef struct scc_lir_addr {
|
||||
typedef struct scc_lir_instr {
|
||||
scc_lir_instr_kind_t kind;
|
||||
union {
|
||||
unsigned int reg; // VREG 或 PREG 索引
|
||||
int arg;
|
||||
int reg; // VREG 或 PREG 索引
|
||||
scc_ap_t imm; // 整型立即数
|
||||
f64 fimm; // 浮点立即数
|
||||
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)})
|
||||
#define SCC_LIR_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_16 2
|
||||
|
||||
@@ -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: {
|
||||
// 函数参数:预先已分配 vreg
|
||||
unsigned int vreg = get_vreg_for_value(ctx, val_ref);
|
||||
return SCC_LIR_VREG(vreg);
|
||||
return SCC_LIR_ARG(val->data.arg_ref.idx);
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_NULLPTR: {
|
||||
scc_ap_t ap;
|
||||
|
||||
@@ -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:
|
||||
scc_tree_dump_append(td, "_");
|
||||
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:
|
||||
scc_tree_dump_append_fmt(td, "%%%u", op->data.reg);
|
||||
break;
|
||||
|
||||
@@ -71,7 +71,7 @@ scc_lir_symbol_id_t scc_lir_module_add_data(scc_lir_module_t *lir_module,
|
||||
scc_vec_push(lir_module->symbol_metas, meta);
|
||||
scc_cfg_symbol_id_t id =
|
||||
scc_cfg_module_add_symbol(&lir_module->cfg_module, &sym);
|
||||
if (id = SCC_CFG_ID_nullptr) {
|
||||
if (id == SCC_CFG_ID_nullptr) {
|
||||
/* 冲突时释放已分配的数据 */
|
||||
scc_free(meta->data.init_data);
|
||||
return SCC_CFG_ID_nullptr;
|
||||
|
||||
63
libs/ir/mir/include/arch/x86_64_isel.h
Normal file
63
libs/ir/mir/include/arch/x86_64_isel.h
Normal 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__ */
|
||||
22
libs/ir/mir/include/core_pass/scc_abi_lowering.h
Normal file
22
libs/ir/mir/include/core_pass/scc_abi_lowering.h
Normal 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__ */
|
||||
@@ -3,8 +3,7 @@
|
||||
#ifndef __SCC_MIR_H__
|
||||
#define __SCC_MIR_H__
|
||||
|
||||
#include <scc_lir.h> // 复用 VREG 概念和一些基础类型
|
||||
// #include "scc_target_desc.h" // 目标架构描述(寄存器文件、指令编码)
|
||||
#include <scc_lir.h>
|
||||
|
||||
typedef enum {
|
||||
SCC_MIR_OP_NONE,
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
#include <scc_lir_module.h>
|
||||
#include <scc_mir_module.h>
|
||||
#include <scc_tree_dump.h>
|
||||
#include <arch/x86_64_isel.h>
|
||||
|
||||
#include <x86/scc_x86_iform.c>
|
||||
#include <x86/scc_x86_reg.c>
|
||||
@@ -44,34 +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;
|
||||
} 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 操作数
|
||||
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};
|
||||
switch (val->kind) {
|
||||
case SCC_LIR_INSTR_KIND_NONE:
|
||||
@@ -95,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.symbol = val->data.symbol;
|
||||
break;
|
||||
case SCC_LIR_INSTR_KIND_ARG:
|
||||
Assert(isel->abi_lowering.lower_param);
|
||||
op = isel->abi_lowering.lower_param(isel, val);
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
@@ -102,16 +79,15 @@ 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) {
|
||||
// FIXME
|
||||
static int next_temp = 10000; // 避免与常规 vreg 冲突
|
||||
return (scc_mir_operand_t){.kind = SCC_MIR_OP_VREG, .vreg = next_temp++};
|
||||
static scc_mir_operand_t new_vreg_temp(scc_x86_64_isel_t *isel) {
|
||||
return (scc_mir_operand_t){.kind = SCC_MIR_OP_VREG,
|
||||
.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) {
|
||||
if (dst.kind == SCC_MIR_OP_VREG || dst.kind == SCC_MIR_OP_PREG) {
|
||||
if (src.kind == SCC_MIR_OP_VREG) {
|
||||
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);
|
||||
} else if (src.kind == SCC_MIR_OP_IMM) {
|
||||
add_instr_2(isel,
|
||||
@@ -126,7 +102,7 @@ static void emit_move(x86_isel_t *isel, scc_mir_operand_t dst,
|
||||
UNREACHABLE();
|
||||
}
|
||||
} else if (dst.kind == SCC_MIR_OP_MEM) {
|
||||
if (src.kind == SCC_MIR_OP_VREG) {
|
||||
if (src.kind == SCC_MIR_OP_VREG || src.kind == SCC_MIR_OP_PREG) {
|
||||
add_instr_2(isel, SCC_X86_IFORM_MOV_MEMV_GPRV, dst, src);
|
||||
} else if (src.kind == SCC_MIR_OP_IMM) {
|
||||
add_instr_2(isel, SCC_X86_IFORM_MOV_MEMV_IMMZ, 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);
|
||||
} else if (src.kind == SCC_MIR_OP_MEM) {
|
||||
scc_mir_operand_t temp = new_vreg_temp(isel);
|
||||
emit_move(isel, temp, src, size);
|
||||
emit_move(isel, dst, temp, size);
|
||||
scc_x86_emit_move(isel, temp, src, size);
|
||||
scc_x86_emit_move(isel, dst, temp, size);
|
||||
} else {
|
||||
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) {
|
||||
// cmp op0, op1 (注意 x86 是 cmp a, b 即 a - b)
|
||||
if (op0.kind == SCC_MIR_OP_VREG && op1.kind == SCC_MIR_OP_IMM) {
|
||||
@@ -158,66 +134,47 @@ static void emit_compare(x86_isel_t *isel, scc_mir_operand_t op0,
|
||||
}
|
||||
}
|
||||
|
||||
static scc_x86_iform_t cond_to_jcc(scc_lir_cond_t cond) {
|
||||
/* 条件码到 setcc 指令的映射 */
|
||||
static scc_x86_iform_t cond_to_setcc(scc_lir_cond_t cond) {
|
||||
switch (cond) {
|
||||
case SCC_LIR_COND_EQ:
|
||||
return SCC_X86_IFORM_JZ_RELBRZ;
|
||||
return SCC_X86_IFORM_SETZ_GPR8;
|
||||
case SCC_LIR_COND_NE:
|
||||
return SCC_X86_IFORM_JNZ_RELBRZ;
|
||||
return SCC_X86_IFORM_SETNZ_GPR8;
|
||||
case SCC_LIR_COND_SLT:
|
||||
return SCC_X86_IFORM_JL_RELBRZ;
|
||||
return SCC_X86_IFORM_SETL_GPR8;
|
||||
case SCC_LIR_COND_SLE:
|
||||
return SCC_X86_IFORM_JLE_RELBRZ;
|
||||
return SCC_X86_IFORM_SETLE_GPR8;
|
||||
case SCC_LIR_COND_SGT:
|
||||
return SCC_X86_IFORM_JNLE_RELBRZ; // JNLE = JG
|
||||
return SCC_X86_IFORM_SETNLE_GPR8; // SETG
|
||||
case SCC_LIR_COND_SGE:
|
||||
return SCC_X86_IFORM_JNL_RELBRZ; // JNL = JGE
|
||||
return SCC_X86_IFORM_SETNL_GPR8; // SETGE
|
||||
case SCC_LIR_COND_ULT:
|
||||
return SCC_X86_IFORM_JB_RELBRZ;
|
||||
return SCC_X86_IFORM_SETB_GPR8;
|
||||
case SCC_LIR_COND_ULE:
|
||||
return SCC_X86_IFORM_JBE_RELBRZ;
|
||||
return SCC_X86_IFORM_SETBE_GPR8;
|
||||
case SCC_LIR_COND_UGT:
|
||||
return SCC_X86_IFORM_JNBE_RELBRZ; // JNBE = JA
|
||||
return SCC_X86_IFORM_SETNBE_GPR8; // SETA
|
||||
case SCC_LIR_COND_UGE:
|
||||
return SCC_X86_IFORM_JNB_RELBRZ; // JNB = JAE
|
||||
// 浮点比较暂不处理(需要 fcomi + jcc)
|
||||
return SCC_X86_IFORM_SETNB_GPR8; // SETAE
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_compare_and_branch(x86_isel_t *isel, scc_lir_cond_t cond,
|
||||
scc_mir_operand_t lhs,
|
||||
scc_mir_operand_t rhs,
|
||||
scc_mir_operand_t true_bb,
|
||||
scc_mir_operand_t false_bb, u8 size) {
|
||||
if (lhs.kind == SCC_MIR_OP_VREG && rhs.kind == SCC_MIR_OP_IMM)
|
||||
add_instr_2(isel, SCC_X86_IFORM_CMP_GPRV_IMMZ, lhs, rhs);
|
||||
else if (lhs.kind == SCC_MIR_OP_VREG && rhs.kind == SCC_MIR_OP_VREG)
|
||||
add_instr_2(isel, SCC_X86_IFORM_CMP_GPRV_GPRV_39, lhs, rhs);
|
||||
else
|
||||
UNREACHABLE();
|
||||
|
||||
scc_x86_iform_t jcc = cond_to_jcc(cond);
|
||||
add_instr_1(isel, jcc, true_bb);
|
||||
add_instr_1(isel, SCC_X86_IFORM_JMP_RELBRZ, false_bb);
|
||||
}
|
||||
|
||||
static void emit_ret(x86_isel_t *isel, scc_lir_val_t ret_val) {
|
||||
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);
|
||||
static void emit_copy_if_needed(scc_x86_64_isel_t *isel, scc_mir_operand_t dst,
|
||||
scc_mir_operand_t src0, u8 size) {
|
||||
if (dst.kind == SCC_MIR_OP_VREG && src0.kind == SCC_MIR_OP_VREG &&
|
||||
dst.vreg == src0.vreg) {
|
||||
return;
|
||||
}
|
||||
add_instr_0(isel, SCC_X86_IFORM_RET_NEAR);
|
||||
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 src1, u8 size) {
|
||||
if (dst.kind == SCC_MIR_OP_VREG && src0.kind == SCC_MIR_OP_VREG &&
|
||||
dst.vreg != src0.vreg)
|
||||
emit_move(isel, dst, src0, size);
|
||||
emit_copy_if_needed(isel, dst, src0, size);
|
||||
|
||||
bool is_imm = (src1.kind == SCC_MIR_OP_IMM);
|
||||
scc_x86_iform_t iform;
|
||||
@@ -252,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};
|
||||
}
|
||||
|
||||
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};
|
||||
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};
|
||||
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,
|
||||
scc_mir_operand_t ret_reg) {
|
||||
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) {
|
||||
static void emit_alloca(scc_x86_64_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 rsp = {.kind = SCC_MIR_OP_PREG, .preg = SCC_X86_REG_RSP};
|
||||
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) {
|
||||
scc_mir_operand_t dst = lir_val_to_mir_op(&instr->to);
|
||||
scc_mir_operand_t src0 = lir_val_to_mir_op(&instr->arg0);
|
||||
scc_mir_operand_t src1 = lir_val_to_mir_op(&instr->arg1);
|
||||
static void sel_mir(scc_x86_64_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
scc_mir_operand_t dst = scc_x86_lir_val_to_mir_op(isel, &instr->to);
|
||||
scc_mir_operand_t src0 = scc_x86_lir_val_to_mir_op(isel, &instr->arg0);
|
||||
scc_mir_operand_t src1 = scc_x86_lir_val_to_mir_op(isel, &instr->arg1);
|
||||
u8 size = instr->size;
|
||||
|
||||
switch (instr->op) {
|
||||
/* ---- 数据移动 ---- */
|
||||
case SCC_LIR_MOV:
|
||||
emit_move(isel, dst, src0, size);
|
||||
scc_x86_emit_move(isel, dst, src0, size);
|
||||
break;
|
||||
|
||||
case SCC_LIR_LOAD:
|
||||
@@ -297,6 +244,9 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
add_instr_2(isel, SCC_X86_IFORM_MOV_GPRV_MEMV, dst, src0);
|
||||
break;
|
||||
|
||||
case SCC_LIR_STORE_ADDR:
|
||||
TODO();
|
||||
break;
|
||||
case SCC_LIR_STORE:
|
||||
// 将 src0 存入 [src1]
|
||||
add_instr_2(isel, SCC_X86_IFORM_MOV_MEMV_GPRV, src1, src0);
|
||||
@@ -327,19 +277,15 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
|
||||
case SCC_LIR_MUL:
|
||||
// imul dst, src0, src1 → 需要 mov + imul
|
||||
if (src0.kind == SCC_MIR_OP_VREG && dst.kind == SCC_MIR_OP_VREG &&
|
||||
src0.vreg != dst.vreg)
|
||||
emit_move(isel, dst, src0, size);
|
||||
emit_copy_if_needed(isel, dst, src0, size);
|
||||
add_instr_2(isel, SCC_X86_IFORM_IMUL_GPRV_GPRV, dst, src1);
|
||||
break;
|
||||
|
||||
case SCC_LIR_SHL:
|
||||
case SCC_LIR_SHR:
|
||||
case SCC_LIR_SAR:
|
||||
case SCC_LIR_SAR: {
|
||||
// 双地址:dst = dst op count
|
||||
if (src0.kind == SCC_MIR_OP_VREG && dst.kind == SCC_MIR_OP_VREG &&
|
||||
src0.vreg != dst.vreg)
|
||||
emit_move(isel, dst, src0, size);
|
||||
emit_copy_if_needed(isel, dst, src0, size);
|
||||
|
||||
if (src1.kind == SCC_MIR_OP_IMM) {
|
||||
scc_x86_iform_t iform;
|
||||
@@ -361,7 +307,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
// 移位量在 CL(需要先 mov cl, src1)
|
||||
scc_mir_operand_t cl = {.kind = SCC_MIR_OP_PREG,
|
||||
.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;
|
||||
switch (instr->op) {
|
||||
case SCC_LIR_SHL:
|
||||
@@ -378,7 +324,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
}
|
||||
add_instr_2(isel, iform, dst, cl);
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
/* ---- 除法与取模 ---- */
|
||||
case SCC_LIR_DIV_S:
|
||||
@@ -390,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,
|
||||
.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) {
|
||||
// 有符号扩展:cqo / cdq(根据 size 选择,这里简化为 64 位 cqo)
|
||||
@@ -398,7 +344,7 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
} else {
|
||||
// 无符号:xor edx, edx
|
||||
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 =
|
||||
@@ -409,55 +355,64 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
|
||||
// 结果:商在 RAX,余数在 RDX
|
||||
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
|
||||
emit_move(isel, dst, rax, size);
|
||||
break;
|
||||
scc_x86_emit_move(isel, dst, rax, size);
|
||||
} break;
|
||||
|
||||
/* ---- 比较指令 ---- */
|
||||
case SCC_LIR_CMP: {
|
||||
// 1. 比较并设置标志位
|
||||
if (src0.kind == SCC_MIR_OP_VREG && src1.kind == SCC_MIR_OP_IMM)
|
||||
add_instr_2(isel, SCC_X86_IFORM_CMP_GPRV_IMMZ, src0, src1);
|
||||
else if (src0.kind == SCC_MIR_OP_VREG && src1.kind == SCC_MIR_OP_VREG)
|
||||
add_instr_2(isel, SCC_X86_IFORM_CMP_GPRV_GPRV_39, src0, src1);
|
||||
else
|
||||
UNREACHABLE();
|
||||
|
||||
// 2. 标志位 -> 布尔值 (写入 dst)
|
||||
scc_x86_iform_t setcc = cond_to_setcc(instr->metadata.cond);
|
||||
add_instr_1(isel, setcc, dst); // 注意 setcc 只写低 8 位
|
||||
// 若需 32/64 位布尔值,可再 movzx dst, dst
|
||||
if (size > 1) {
|
||||
// movzx dst, dst (假设 MOVZX_GPRV_GPR8 存在;这里临时用 and 模拟)
|
||||
// 简单处理:用 and dst, 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);
|
||||
}
|
||||
} break;
|
||||
|
||||
/* ---- 比较与分支 ---- */
|
||||
case SCC_LIR_CMP:
|
||||
// 比较并设置标志位,结果通过后续 BR 使用。
|
||||
// 当前 LIR 中 CMP 不直接生成 setcc,需要配合分支。
|
||||
emit_compare(isel, src0, src1, size);
|
||||
// 如果有需要将比较结果写入 to(即 bool 值),可后续添加 SETcc
|
||||
break;
|
||||
|
||||
/* ---- 条件分支 ---- */
|
||||
case SCC_LIR_BR: {
|
||||
// 条件分支:依赖前一条 CMP 设置的标志位
|
||||
// 问题:LIR 的 BR 未携带条件码,实际需要依据前一条 CMP 的条件。
|
||||
// 这里暂时无法精确生成 jcc,故保留原始构造假的直接跳转,待上层 IR 合并
|
||||
// CMP+BR 后再完善。 以下代码仅为占位,实际不可用。 scc_mir_operand_t
|
||||
// true_bb = { .kind = SCC_MIR_OP_BLOCK, .block_id =
|
||||
// instr->metadata.br.true_target }; scc_mir_operand_t false_bb = {
|
||||
// .kind = SCC_MIR_OP_BLOCK, .block_id = instr->metadata.br.false_target
|
||||
// }; add_instr_1(isel, SCC_X86_IFORM_JMP_RELBRZ, true_bb); //
|
||||
// 不合理占位
|
||||
UNREACHABLE(); // 当前不可达,要求上层保证 CMP+BR 合并
|
||||
break;
|
||||
}
|
||||
// arg0 是 CMP 产生的布尔值 (0 或 1)
|
||||
// test src0, src0 ; jnz true_bb ; jmp false_bb
|
||||
scc_mir_operand_t true_bb = {.kind = SCC_MIR_OP_BLOCK,
|
||||
.block_id =
|
||||
instr->metadata.br.true_target};
|
||||
scc_mir_operand_t false_bb = {.kind = SCC_MIR_OP_BLOCK,
|
||||
.block_id =
|
||||
instr->metadata.br.false_target};
|
||||
|
||||
case SCC_LIR_JMP:
|
||||
// test src0, src0
|
||||
add_instr_2(isel, SCC_X86_IFORM_TEST_GPRV_GPRV, src0, src0);
|
||||
// jnz true
|
||||
add_instr_1(isel, SCC_X86_IFORM_JNZ_RELBRZ, true_bb);
|
||||
// jmp false
|
||||
add_instr_1(isel, SCC_X86_IFORM_JMP_RELBRZ, false_bb);
|
||||
} break;
|
||||
|
||||
case SCC_LIR_JMP: {
|
||||
add_instr_1(
|
||||
isel, SCC_X86_IFORM_JMP_RELBRZ,
|
||||
(scc_mir_operand_t){.kind = SCC_MIR_OP_BLOCK,
|
||||
.block_id = instr->metadata.jmp_target});
|
||||
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;
|
||||
} break;
|
||||
|
||||
/* ---- 栈分配 ---- */
|
||||
case SCC_LIR_ALLOCA:
|
||||
// emit_alloca(isel, dst, instr->metadata.alloca.size_bytes);
|
||||
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){
|
||||
.kind = SCC_MIR_OP_IMM,
|
||||
.imm = instr->size,
|
||||
@@ -468,16 +423,31 @@ static void sel_mir(x86_isel_t *isel, const scc_lir_instr_t *instr) {
|
||||
case SCC_LIR_NOP:
|
||||
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();
|
||||
break;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
static void sel_func(const scc_lir_module_t *lir_module,
|
||||
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);
|
||||
scc_vec_foreach(func->bblocks, i) {
|
||||
scc_vec_init(isel.instrs);
|
||||
|
||||
@@ -507,7 +477,7 @@ void scc_isel_x86_64(scc_mir_module_t *mir_module,
|
||||
Assert(func_meta != nullptr);
|
||||
scc_mir_func_meta_init(func_meta);
|
||||
scc_vec_push(mir_module->func_metas, func_meta);
|
||||
func->meta = func_meta;
|
||||
sel_func(lir_module, func);
|
||||
func->meta = func_meta;
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,6 @@ typedef struct scc_parser {
|
||||
int errcode;
|
||||
} scc_parser_t;
|
||||
|
||||
// static inline scc_ast_qual_type_t *scc_parser_
|
||||
|
||||
/**
|
||||
* @brief 初始化解析器
|
||||
* @param parser 解析器实例
|
||||
@@ -120,8 +118,8 @@ static inline void scc_parse_type_sema(scc_parser_t *parser,
|
||||
scc_sema_type(parser, type ? type->base.type : SCC_AST_UNKNOWN, type);
|
||||
}
|
||||
|
||||
static inline const scc_ast_qual_type_t *
|
||||
scc_parse_got_type(scc_parser_t *parser, const char *name) {
|
||||
static inline scc_ast_qual_type_t *scc_parse_got_type(scc_parser_t *parser,
|
||||
const char *name) {
|
||||
return parser->sema_ctx->got_type(parser->sema_ctx, name);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ typedef struct scc_sema_ctx scc_sema_ctx_t;
|
||||
typedef void (*scc_sema_callback_t)(scc_sema_ctx_t *context,
|
||||
scc_ast_node_kind_t node_type, void *node);
|
||||
|
||||
typedef const scc_ast_qual_type_t *(*scc_sema_got_type_t)(
|
||||
scc_sema_ctx_t *context, const char *name);
|
||||
typedef scc_ast_qual_type_t *(*scc_sema_got_type_t)(scc_sema_ctx_t *context,
|
||||
const char *name);
|
||||
|
||||
/**
|
||||
* @brief 语义分析回调集合
|
||||
|
||||
@@ -573,7 +573,7 @@ static scc_ast_qual_type_t *parse_record_type(scc_parser_t *parser,
|
||||
UNREACHABLE();
|
||||
break;
|
||||
}
|
||||
const scc_ast_qual_type_t *type = nullptr;
|
||||
scc_ast_qual_type_t *type = nullptr;
|
||||
if (name == nullptr) {
|
||||
name = "<anonymous>";
|
||||
}
|
||||
@@ -581,7 +581,6 @@ static scc_ast_qual_type_t *parse_record_type(scc_parser_t *parser,
|
||||
|
||||
type = scc_parse_got_type(parser, scc_str_as_cstr(&symbol_name));
|
||||
if (type == nullptr) {
|
||||
new_type:
|
||||
scc_ast_canon_type_t *canon = scc_ast_ctx_alloc_type(parser->ast_ctx);
|
||||
scc_ast_decl_t *decl = SCC_AST_ALLOC_DECL(parser->ast_ctx);
|
||||
|
||||
@@ -602,7 +601,12 @@ static scc_ast_qual_type_t *parse_record_type(scc_parser_t *parser,
|
||||
}
|
||||
_scc_ast_decl_record_init(decl, decl_kind, name, nullptr, *pos);
|
||||
|
||||
const scc_ast_qual_type_t *qual_type =
|
||||
// FIXME 必须立即挂载到AST但是需要hack
|
||||
{
|
||||
scc_vec_push(parser->translation_unit->declarations, decl);
|
||||
}
|
||||
|
||||
scc_ast_qual_type_t *qual_type =
|
||||
SCC_AST_ALLOC_QUAL_TYPE(parser->ast_ctx);
|
||||
_scc_ast_type_record_init(qual_type, canon, type_kind, name, decl,
|
||||
*pos);
|
||||
|
||||
@@ -9,8 +9,8 @@ static void dummy_sema_callback(scc_sema_ctx_t *context,
|
||||
return;
|
||||
}
|
||||
|
||||
static const scc_ast_qual_type_t *
|
||||
dummy_got_type_callback(scc_sema_ctx_t *context, const char *name) {
|
||||
static scc_ast_qual_type_t *dummy_got_type_callback(scc_sema_ctx_t *context,
|
||||
const char *name) {
|
||||
(void)context;
|
||||
(void)name;
|
||||
return nullptr;
|
||||
@@ -61,6 +61,7 @@ scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {
|
||||
return nullptr;
|
||||
unit->base.type = SCC_AST_TRANSLATION_UNIT;
|
||||
scc_vec_init(unit->declarations);
|
||||
parser->translation_unit = unit;
|
||||
|
||||
/**
|
||||
* Program := (Declaration | Definition)*
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <scc_sema.h>
|
||||
#include <sema_symtab.h>
|
||||
|
||||
static const scc_ast_qual_type_t *got_type_callback(scc_sema_ctx_t *sema_ctx,
|
||||
static scc_ast_qual_type_t *got_type_callback(scc_sema_ctx_t *sema_ctx,
|
||||
const char *name);
|
||||
|
||||
static void gen_symbol_name(const scc_ast_decl_t *decl, scc_str_t *name) {
|
||||
@@ -219,7 +219,7 @@ static void decl_callback(scc_sema_ctx_t *sema_ctx,
|
||||
return;
|
||||
}
|
||||
|
||||
static const scc_ast_qual_type_t *got_type_callback(scc_sema_ctx_t *sema_ctx,
|
||||
static scc_ast_qual_type_t *got_type_callback(scc_sema_ctx_t *sema_ctx,
|
||||
const char *name) {
|
||||
scc_sema_symtab_t *sema_symtab = sema_ctx->context;
|
||||
scc_ast_node_t *node = scc_sema_symtab_lookup_symbol(sema_symtab, name);
|
||||
|
||||
@@ -227,8 +227,7 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
scc_argparse_opt_t opt_emit_target;
|
||||
scc_argparse_opt_init(&opt_emit_target, 0, "emit-target",
|
||||
scc_hints[SCC_HINT_EMIT_TARGET]);
|
||||
scc_argparse_spec_setup_string(&opt_emit_target.spec,
|
||||
&(config->emit_target));
|
||||
scc_argparse_spec_setup_bool(&opt_emit_target.spec, &(config->emit_target));
|
||||
scc_argparse_cmd_add_opt(root, &opt_emit_target);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# windows powershell: echo $LASTEXITCODE
|
||||
# nushell: echo $env.LAST_EXIT_CODE
|
||||
# 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/03_decl_init.c" = 11
|
||||
"./return_val_cases/04_if.c" = 1
|
||||
@@ -14,12 +14,12 @@
|
||||
"./return_val_cases/10_main.c" = 3
|
||||
"./return_val_cases/11_recursive.c" = 120
|
||||
"./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/15_array_subscript.c" = 1198
|
||||
"./return_val_cases/15_array_subscript.c" = 209
|
||||
"./return_val_cases/16_enum.c" = 5
|
||||
"./return_val_cases/17_more_arg.c" = 45
|
||||
"./return_val_cases/18_break_continue.c" = 676
|
||||
"./return_val_cases/19_goto.c" = 676
|
||||
"./return_val_cases/18_break_continue.c" = 219
|
||||
"./return_val_cases/19_goto.c" = 219
|
||||
[stdout_val_cases]
|
||||
"./stdout_val_cases/01_include.c" = "Hello World!\n"
|
||||
|
||||
@@ -1,3 +1 @@
|
||||
int main() {
|
||||
return 65536;
|
||||
}
|
||||
int main() { return 255; }
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
int main(void) {
|
||||
char buff[] = "hello buffer";
|
||||
char buff[] = "hi";
|
||||
int res = 0;
|
||||
for (char *ptr = buff; *ptr != 0; ptr += 1) {
|
||||
res += *ptr;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
int main(void) {
|
||||
char buff[] = "hello buffer";
|
||||
char buff[] = "hi";
|
||||
int res = 0;
|
||||
for (int i = 0; buff[i] != 0; i += 1) {
|
||||
res += buff[i];
|
||||
|
||||
@@ -3,7 +3,7 @@ int main(void) {
|
||||
char buff[] = "hello buffer";
|
||||
int res = 0;
|
||||
for (int i = 0; buff[i] != 0; i += 1) {
|
||||
if (i < 2)
|
||||
if (i <= 6)
|
||||
continue;
|
||||
if (i > 8)
|
||||
break;
|
||||
|
||||
@@ -2,7 +2,7 @@ int main(void) {
|
||||
char buff[] = "hello buffer";
|
||||
int res = 0;
|
||||
for (int i = 0; buff[i] != 0; i += 1) {
|
||||
if (i < 2)
|
||||
if (i <= 6)
|
||||
goto the_continue;
|
||||
if (i > 8)
|
||||
goto the_break;
|
||||
|
||||
@@ -14,6 +14,7 @@ import sys
|
||||
import tempfile
|
||||
import tomllib
|
||||
import uuid
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Sequence
|
||||
@@ -24,7 +25,7 @@ from typing import Sequence
|
||||
WORKSPACE = Path(__file__).resolve().parent
|
||||
CC_PATH = WORKSPACE / "../../build/dev/scc"
|
||||
CONFIG_PATH = WORKSPACE / "expect.toml"
|
||||
DEFAULT_TIMEOUT = 10 # seconds
|
||||
DEFAULT_TIMEOUT = 1 # seconds
|
||||
|
||||
logger = logging.getLogger("scc-test")
|
||||
|
||||
@@ -173,14 +174,30 @@ class Runner:
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def run_all(self, tests: Sequence[TestCase]) -> tuple[int, int]:
|
||||
def run_all(self, tests: Sequence[TestCase], global_timeout: float | None = None) -> tuple[int, int]:
|
||||
"""Run a sequence of tests. Returns (passed, total)."""
|
||||
passed = 0
|
||||
tested = 0
|
||||
start_time = time.monotonic()
|
||||
|
||||
for idx, test in enumerate(tests, start=1):
|
||||
if global_timeout is not None and (time.monotonic() - start_time) >= global_timeout:
|
||||
logger.warning(
|
||||
"Global timeout (%.1fs) reached - skipping remaining %d tests.",
|
||||
global_timeout, len(tests) - tested
|
||||
)
|
||||
break
|
||||
|
||||
logger.info("(%d/%d) %s", idx, len(tests), test.description)
|
||||
if self.run_one(test):
|
||||
passed += 1
|
||||
return passed, len(tests)
|
||||
tested += 1
|
||||
|
||||
total_skipped = len(tests) - tested
|
||||
if total_skipped > 0:
|
||||
logger.warning("%d tests skipped due to global timeout.", total_skipped)
|
||||
|
||||
return passed, tested
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -302,8 +319,8 @@ def main() -> None:
|
||||
return
|
||||
|
||||
# Filter tests
|
||||
tests = select_tests(all_tests, args.tests)
|
||||
if not tests:
|
||||
selected = select_tests(all_tests, args.tests)
|
||||
if not selected:
|
||||
logger.warning("No tests to run.")
|
||||
return
|
||||
|
||||
@@ -314,11 +331,14 @@ def main() -> None:
|
||||
keep_temps=args.keep_temps,
|
||||
)
|
||||
|
||||
passed, total = runner.run_all(tests)
|
||||
global_timeout = DEFAULT_TIMEOUT * 3
|
||||
passed, total = runner.run_all(selected, global_timeout)
|
||||
logger.info("=" * 40)
|
||||
if global_timeout and total < len(selected):
|
||||
logger.info("Summary: %d/%d passed (%d skipped due to timeout)",
|
||||
passed, total, len(selected) - total)
|
||||
else:
|
||||
logger.info("Summary: %d/%d passed", passed, total)
|
||||
if passed != total:
|
||||
sys.exit(1)
|
||||
|
||||
runner.cleanup()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user