refactor: sema.c + codegen.c 拆分,全部源文件 <800 行
sema.c 1129行 → sema.c 499行 + typeck.c 629行 + sema_internal.h 51行 - typeck.c: 表达式类型检查 (10个analyze_*函数) + 泛型单态化 + 类型关系 - sema.c: analyze_node + sema_analyze codegen.c 947行 → codegen.c 453行 + cg_expr.c 440行 + codegen_internal.h 83行 - cg_expr.c: LLVM表达式生成 + 类型映射 (to_llvm_type/coerce_int/type_info_to_llvm) - codegen.c: 语句生成 + 模块入口 + 符号表 + 内存清理 全部核心源文件 <800 行限制: parser(662+498), sema(499+629), codegen(453+440) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#ifndef CODEGEN_INTERNAL_H
|
||||
#define CODEGEN_INTERNAL_H
|
||||
|
||||
#include "codegen.h"
|
||||
#include "ast.h"
|
||||
#include "arena.h"
|
||||
#include "l_lang.h"
|
||||
#include <llvm-c/Analysis.h>
|
||||
#include <llvm-c/Types.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 递归深度限制
|
||||
extern int codegen_depth;
|
||||
#define MAX_CODEGEN_DEPTH 1000
|
||||
|
||||
// === 内部状态 ===
|
||||
typedef struct VarEntry {
|
||||
const char* name;
|
||||
LLVMValueRef alloca;
|
||||
LLVMTypeRef alloca_type;
|
||||
struct VarEntry* next;
|
||||
} VarEntry;
|
||||
|
||||
typedef struct FnEntry {
|
||||
const char* name;
|
||||
LLVMValueRef fn;
|
||||
TypeKind ret;
|
||||
TypeKind* params;
|
||||
size_t pc;
|
||||
struct FnEntry* next;
|
||||
} FnEntry;
|
||||
|
||||
typedef struct StructTypeEntry {
|
||||
const char* name;
|
||||
LLVMTypeRef llvm_type;
|
||||
size_t field_count;
|
||||
struct StructTypeEntry* next;
|
||||
} StructTypeEntry;
|
||||
|
||||
typedef struct {
|
||||
Arena* arena;
|
||||
LLVMContextRef context;
|
||||
LLVMModuleRef module;
|
||||
LLVMBuilderRef builder;
|
||||
VarEntry* var_table;
|
||||
const char* error;
|
||||
FnEntry* fn_table;
|
||||
StructTypeEntry* struct_table;
|
||||
LLVMValueRef printf_fn;
|
||||
LLVMTypeRef printf_ty;
|
||||
LLVMValueRef malloc_fn;
|
||||
LLVMValueRef free_fn;
|
||||
LLVMValueRef strlen_fn;
|
||||
LLVMValueRef memcpy_fn;
|
||||
LLVMValueRef* cleanup_list;
|
||||
size_t cleanup_count;
|
||||
size_t cleanup_cap;
|
||||
} CgCtx;
|
||||
|
||||
// === 类型映射 ===
|
||||
LLVMTypeRef to_llvm_type(CgCtx* ctx, TypeKind kind);
|
||||
LLVMValueRef to_llvm_const(LLVMTypeRef ty, AstNode* lit);
|
||||
LLVMTypeRef type_info_to_llvm(CgCtx* ctx, const TypeInfo* ti);
|
||||
LLVMValueRef coerce_int(CgCtx* ctx, LLVMValueRef val, LLVMTypeRef from_ty, LLVMTypeRef to_ty);
|
||||
|
||||
// === 表操作 ===
|
||||
LLVMValueRef find_var(CgCtx* ctx, const char* name);
|
||||
void add_var(CgCtx* ctx, const char* name, LLVMValueRef alloca, LLVMTypeRef alloca_type);
|
||||
LLVMValueRef find_fn(CgCtx* ctx, const char* name);
|
||||
void add_fn(CgCtx* ctx, const char* name, LLVMValueRef fn);
|
||||
void add_struct_type(CgCtx* ctx, const char* name, LLVMTypeRef ty, size_t fc);
|
||||
LLVMTypeRef find_struct_type(CgCtx* ctx, const char* name);
|
||||
|
||||
// === 内存清理 ===
|
||||
void cleanup_add(CgCtx* ctx, LLVMValueRef alloca);
|
||||
void cleanup_emit(CgCtx* ctx, size_t from_mark);
|
||||
|
||||
// === 代码生成函数 ===
|
||||
LLVMValueRef codegen_expr(CgCtx* ctx, AstNode* node);
|
||||
void codegen_stmt(CgCtx* ctx, AstNode* node);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user