移除了对scc_abi包的依赖,将相关头文件从libs/abi移动到libs/ast2ir目录下。 重构了基本类型解析功能,将parse_base_type函数提取为独立的 scc_ast2ir_parse_base_type实现,并支持有符号/无符号类型区分。 feat(ast2ir): 实现整数常量表达式求值器 新增了完整的整数常量表达式求值功能,支持C11标准中的常量表达式规则, 包括字面量、标识符、sizeof/_Alignof、一元/二元运算、条件表达式和 类型转换等操作。该功能用于数组大小和枚举值的编译期计算验证。 refactor(ast2ir): 完善类型提升和算术转换机制 改进了整数提升和寻常算术转换的实现,修复了移位操作的符号处理问题, 添加了无符号比较操作的支持,增强了类型安全检查,统一了错误处理流程。 fix(ast2ir): 修复赋值表达式返回值和数组大小计算问题 修正了赋值表达式的返回值处理,确保返回右侧值而不是存储指令引用。 使用新的常量表达式求值器替代原有的硬编码数组大小计算,提高了 数组声明的正确性。
100 lines
3.1 KiB
C
100 lines
3.1 KiB
C
#ifndef __SCC_AP_H__
|
|
#define __SCC_AP_H__
|
|
/**
|
|
* @brief Arbitrary Precision Library
|
|
*
|
|
*/
|
|
#ifndef __NO_SCC_RUNTIME__
|
|
#include <scc_core.h>
|
|
#define SCC_AP_DIGIT u64
|
|
#define SCC_AP_PANIC Panic
|
|
#define SCC_AP_ASSERT Assert
|
|
#define SCC_AP_MALLOC scc_malloc
|
|
#define SCC_AP_REALLOC scc_realloc
|
|
#define SCC_AP_FREE scc_free
|
|
#else
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#define SCC_AP_DIGIT uint64_t
|
|
#define SCC_AP_PANIC(...) \
|
|
do { \
|
|
fprintf(stderr, __VA_ARGS__); \
|
|
abort(); \
|
|
} while (0)
|
|
#define SCC_AP_ASSERT assert
|
|
#define SCC_AP_MALLOC malloc
|
|
#define SCC_AP_REALLOC realloc
|
|
#define SCC_AP_FREE free
|
|
#endif
|
|
|
|
#ifndef SCC_AP_DIGIT
|
|
#error "SCC_AP_DIGIT is not defined"
|
|
#endif
|
|
#define SCC_AP_DIGIT_BITS (sizeof(SCC_AP_DIGIT))
|
|
|
|
#ifndef nullptr
|
|
#define nullptr ((void *)0)
|
|
#endif
|
|
|
|
typedef SCC_AP_DIGIT scc_ap_digit;
|
|
typedef struct {
|
|
int capacity; // maybe power of 2 (-1 means using digit)
|
|
int len; // data length (sign with in)
|
|
union {
|
|
scc_ap_digit *array;
|
|
scc_ap_digit digit;
|
|
} data;
|
|
} scc_ap_t;
|
|
|
|
static inline void scc_ap_init(scc_ap_t *ap) {
|
|
ap->capacity = -1;
|
|
ap->len = 1;
|
|
ap->data.digit = 0;
|
|
}
|
|
|
|
static inline void scc_ap_set_int(scc_ap_t *ap, int val) {
|
|
ap->capacity = -1;
|
|
SCC_AP_ASSERT(sizeof(scc_ap_digit) >= sizeof(int));
|
|
if (val < 0) {
|
|
ap->len = -1;
|
|
ap->data.digit = (scc_ap_digit)(-(int64_t)val);
|
|
} else {
|
|
ap->len = 1;
|
|
ap->data.digit = (scc_ap_digit)val;
|
|
}
|
|
}
|
|
|
|
void scc_ap_drop(scc_ap_t *ap);
|
|
void scc_ap_with_bits(scc_ap_t *ap, int bits);
|
|
void scc_ap_from_string(scc_ap_t *ap, const char *str, int len, int base);
|
|
void scc_ap_set_digit(scc_ap_t *ap, scc_ap_digit digit);
|
|
|
|
void scc_ap_add(scc_ap_t *to, const scc_ap_t *from_a, const scc_ap_t *from_b);
|
|
void scc_ap_sub(scc_ap_t *to, const scc_ap_t *from_a, const scc_ap_t *from_b);
|
|
void scc_ap_mul(scc_ap_t *to, const scc_ap_t *from_a, const scc_ap_t *from_b);
|
|
void scc_ap_div(scc_ap_t *to, const scc_ap_t *from_a, const scc_ap_t *from_b);
|
|
void scc_ap_mod(scc_ap_t *to, const scc_ap_t *from_a, const scc_ap_t *from_b);
|
|
|
|
void scc_ap_shl(scc_ap_t *to, const scc_ap_t *from, unsigned shift);
|
|
void scc_ap_shr(scc_ap_t *to, const scc_ap_t *from, unsigned shift);
|
|
void scc_ap_lshr(scc_ap_t *to, const scc_ap_t *from, unsigned shift);
|
|
|
|
void scc_ap_and(scc_ap_t *to, const scc_ap_t *a, const scc_ap_t *b);
|
|
void scc_ap_or(scc_ap_t *to, const scc_ap_t *a, const scc_ap_t *b);
|
|
void scc_ap_xor(scc_ap_t *to, const scc_ap_t *a, const scc_ap_t *b);
|
|
void scc_ap_not(scc_ap_t *to, const scc_ap_t *from);
|
|
|
|
void scc_ap_neg(scc_ap_t *to, const scc_ap_t *from);
|
|
|
|
int scc_ap_cmp(const scc_ap_t *a, const scc_ap_t *b);
|
|
int scc_ap_is_zero(const scc_ap_t *ap);
|
|
|
|
int scc_ap_eql(const scc_ap_t *a, const scc_ap_t *b);
|
|
|
|
typedef void (*ap_dump_fn)(const char ch, void *user_data);
|
|
int scc_ap_dump(scc_ap_t *ap, ap_dump_fn dump_fn, void *user_data);
|
|
|
|
#endif /* __SCC_AP_H__ */
|