Files
scc/libs/ir2mcode/src/frame_manager.c
zzy 4144f7841c refactor(argparse): 将null替换为nullptr以提高C++兼容性
- 在argparse库中将所有null指针常量替换为nullptr
- 更新头文件和源文件中的指针初始化和比较操作
- 修改测试文件中的相关断言检查
- 更新AST定义文件中的注释说明
2026-04-05 20:18:09 +08:00

51 lines
1.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// frame_manager.c
#include "frame_manager.h"
#include <assert.h>
#include <stdlib.h>
struct frame_manager {
int shadow_space; // 影子空间大小(字节)
int saved_reg_size; // 已分配的保存寄存器区域大小
int local_size; // 已分配的局部变量区域大小
int align; // 栈对齐要求
};
void frame_manager_init(frame_manager_t *fm, int shadow_space, int align) {
fm->shadow_space = shadow_space;
fm->saved_reg_size = 0;
fm->local_size = 0;
fm->align = align;
}
int frame_alloc_slot(frame_manager_t *fm, int size) {
int offset = fm->local_size;
fm->local_size += size;
return offset; // 返回虚拟偏移从0开始
}
int frame_alloc_saved_reg(frame_manager_t *fm, int reg_width) {
int offset = fm->saved_reg_size;
fm->saved_reg_size += reg_width;
return offset;
}
int frame_total_size(frame_manager_t *fm) {
int total = fm->shadow_space + fm->saved_reg_size + fm->local_size;
// 对齐到 align 字节
return (total + fm->align - 1) & ~(fm->align - 1);
}
int frame_slot_offset(frame_manager_t *fm, int slot_idx) {
// 布局: RBP 指向保存的 RBP向下依次是
// [影子空间] [保存寄存器区] [局部变量区]
// 局部变量区的起始地址 = RBP - (8 + shadow_space + saved_reg_size)
// 其中 8 是 push rbp 占用的空间(返回地址在 RBP+8但 RBP 本身指向保存的
// RBP
int base = 8 + fm->shadow_space + fm->saved_reg_size;
return base + slot_idx; // 返回正数,表示从 RBP 向下的字节数
}
int frame_shadow_space(frame_manager_t *fm) { return fm->shadow_space; }
int frame_saved_reg_size(frame_manager_t *fm) { return fm->saved_reg_size; }