feat 新的运行时环境
This commit is contained in:
47
runtime/libcore/include/core_impl.h
Normal file
47
runtime/libcore/include/core_impl.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef __SMCC_CORE_IMPL_H__
|
||||
#define __SMCC_CORE_IMPL_H__
|
||||
|
||||
#include "core_type.h"
|
||||
|
||||
/* ====== 内存管理核心接口 ====== */
|
||||
|
||||
void* smcc_malloc(usize size);
|
||||
void* smcc_calloc(usize count, usize size);
|
||||
void* smcc_realloc(void *ptr, usize new_size);
|
||||
void smcc_free(void *ptr);
|
||||
|
||||
/* ====== 文件系统核心接口 ====== */
|
||||
|
||||
/* 文件句柄 - 不透明指针 */
|
||||
typedef struct smcc_file* smcc_file_t;
|
||||
|
||||
/* 文件打开模式 - 只保留编译器真正需要的 */
|
||||
typedef enum {
|
||||
SMCC_FILE_READ, /* 读取源文件、头文件 */
|
||||
SMCC_FILE_WRITE, /* 写入目标文件、汇编文件 */
|
||||
SMCC_FILE_APPEND /* 日志、调试输出 */
|
||||
} smcc_file_mode_t;
|
||||
|
||||
/* 核心文件操作 */
|
||||
smcc_file_t smcc_fopen(const char *path, smcc_file_mode_t mode);
|
||||
void smcc_fclose(smcc_file_t file);
|
||||
usize smcc_fread(smcc_file_t file, void *buffer, usize size);
|
||||
usize smcc_fwrite(smcc_file_t file, const void *buffer, usize size);
|
||||
cbool smcc_fexists(const char *path);
|
||||
|
||||
/* ====== 输入输出核心接口 ====== */
|
||||
|
||||
void smcc_snprintf(char *buf, usize size, const char *format, ...);
|
||||
|
||||
/* 标准输出 - 用于编译进度、结果 */
|
||||
void smcc_printf(const char *format, ...);
|
||||
|
||||
/* 错误输出 - 用于错误信息、警告 */
|
||||
void smcc_eprintf(const char *format, ...);
|
||||
|
||||
/* ====== 系统核心接口 ====== */
|
||||
|
||||
/* 程序控制 */
|
||||
void smcc_exit(int code);
|
||||
|
||||
#endif // __SMCC_CORE_IMPL_H__
|
||||
9
runtime/libcore/include/core_macro.h
Normal file
9
runtime/libcore/include/core_macro.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef __SMCC_CORE_MACRO_H__
|
||||
#define __SMCC_CORE_MACRO_H__
|
||||
|
||||
#define _SMCC_STR(str) #str
|
||||
#define SMCC_STR(str) _SMCC_STR(str)
|
||||
|
||||
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||
|
||||
#endif // __SMCC_CORE_MACRO_H__
|
||||
28
runtime/libcore/include/core_mem.h
Normal file
28
runtime/libcore/include/core_mem.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef __SMCC_CORE_MEM_H__
|
||||
#define __SMCC_CORE_MEM_H__
|
||||
|
||||
#include "core_type.h"
|
||||
|
||||
void* smcc_memcpy(void *dest, const void *src, usize n);
|
||||
void* smcc_memmove(void *dest, const void *src, usize n);
|
||||
void* smcc_memset(void *s, int c, usize n);
|
||||
int smcc_memcmp(const void *s1, const void *s2, usize n);
|
||||
|
||||
static inline u32 smcc_strhash32(const char* s) {
|
||||
u32 hash = 2166136261u; // FNV-1a偏移基础值
|
||||
while (*s) {
|
||||
hash ^= *s++;
|
||||
hash *= 16777619u;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
static inline int smcc_strcmp(const char* s1, const char* s2) {
|
||||
while (*s1 && *s2 && *s1 == *s2) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
return *s1 - *s2;
|
||||
}
|
||||
|
||||
#endif /* __SMCC_CORE_MEM_H__ */
|
||||
142
runtime/libcore/include/core_str.h
Normal file
142
runtime/libcore/include/core_str.h
Normal file
@@ -0,0 +1,142 @@
|
||||
#ifndef __CORE_STR_H__
|
||||
#define __CORE_STR_H__
|
||||
|
||||
#include "core_type.h"
|
||||
#include "core_impl.h"
|
||||
#include "log.h"
|
||||
|
||||
typedef struct cstring {
|
||||
char* data;
|
||||
usize len;
|
||||
usize cap;
|
||||
} cstring_t;
|
||||
|
||||
/**
|
||||
* 创建一个新的空字符串
|
||||
*/
|
||||
static inline cstring_t cstring_new(void) {
|
||||
return (cstring_t) { .data = null, .len = 0, .cap = 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定容量创建字符串
|
||||
*/
|
||||
static inline cstring_t cstring_with_capacity(usize capacity) {
|
||||
char* data = null;
|
||||
if (capacity > 0) {
|
||||
data = (char*)smcc_malloc(capacity);
|
||||
Assert(data != null);
|
||||
}
|
||||
return (cstring_t) { .data = data, .len = 0, .cap = capacity };
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 C 字符串创建 Rust 风格字符串
|
||||
*/
|
||||
static inline cstring_t cstring_from_cstr(const char* s) {
|
||||
if (s == null) {
|
||||
return cstring_new();
|
||||
}
|
||||
|
||||
usize len = 0;
|
||||
const char* p = s;
|
||||
while (*p++) len++;
|
||||
|
||||
char* data = (char*)smcc_malloc(len + 1);
|
||||
Assert(data != null);
|
||||
smcc_memcpy(data, s, len);
|
||||
data[len] = '\0';
|
||||
|
||||
return (cstring_t) { .data = data, .len = len, .cap = len };
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放字符串资源
|
||||
*/
|
||||
static inline void cstring_free(cstring_t* str) {
|
||||
if (str && str->data) {
|
||||
smcc_free(str->data);
|
||||
str->data = null;
|
||||
str->len = 0;
|
||||
str->cap = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向字符串追加内容
|
||||
*/
|
||||
static inline void cstring_push_str(cstring_t* str, const char* data, usize len) {
|
||||
if (str == null || data == null || len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果需要扩容
|
||||
if (str->len + len + 1 > str->cap) {
|
||||
// FIXME c string 兼容性问题 bad practice a lot of `+ 1`
|
||||
usize new_cap = str->cap == 0 ? len + 1 : str->cap;
|
||||
while (new_cap < str->len + len + 1) {
|
||||
new_cap *= 2;
|
||||
if (new_cap == 0) { // 处理溢出情况
|
||||
new_cap = str->len + len + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char* new_data = str->data ?
|
||||
(char*)smcc_realloc(str->data, new_cap) :
|
||||
(char*)smcc_malloc(new_cap);
|
||||
Assert(new_data != null);
|
||||
|
||||
str->data = new_data;
|
||||
str->cap = new_cap;
|
||||
}
|
||||
|
||||
smcc_memcpy(str->data + str->len, data, len);
|
||||
str->len += len;
|
||||
str->data[str->len] = '\0'; // 保证 C 字符串兼容性
|
||||
}
|
||||
|
||||
/**
|
||||
* 向字符串追加单个字符
|
||||
*/
|
||||
static inline void cstring_push(cstring_t* str, char ch) {
|
||||
cstring_push_str(str, &ch, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字符串长度
|
||||
*/
|
||||
static inline usize cstring_len(const cstring_t* str) {
|
||||
return str ? str->len : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符串是否为空
|
||||
*/
|
||||
static inline cbool cstring_is_empty(const cstring_t* str) {
|
||||
return str == null || str->len == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空字符串内容但保留分配的内存
|
||||
*/
|
||||
static inline void cstring_clear(cstring_t* str) {
|
||||
if (str) {
|
||||
str->len = 0;
|
||||
if (str->data) {
|
||||
str->data[0] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 C 风格字符串
|
||||
*/
|
||||
static inline const char* cstring_as_cstr(const cstring_t* str) {
|
||||
if (str == null || str->data == null) {
|
||||
return "";
|
||||
}
|
||||
return str->data;
|
||||
}
|
||||
|
||||
#endif /* __CORE_STR_H__ */
|
||||
71
runtime/libcore/include/core_type.h
Normal file
71
runtime/libcore/include/core_type.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef __SMCC_CORE_TYPE_H__
|
||||
#define __SMCC_CORE_TYPE_H__
|
||||
|
||||
#ifndef __SMCC_BUILTIN_TYPE__
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef int16_t i16;
|
||||
typedef int32_t i32;
|
||||
typedef int64_t i64;
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef intptr_t isize;
|
||||
typedef uintptr_t usize;
|
||||
typedef ptrdiff_t pdiff;
|
||||
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
|
||||
typedef bool cbool;
|
||||
/// void / null
|
||||
#define null NULL
|
||||
|
||||
static_assert(sizeof(cbool) == 1, "cbool size must 1");
|
||||
|
||||
#else
|
||||
#define __smcc_i8
|
||||
#define __smcc_i16
|
||||
#define __smcc_i32
|
||||
#define __smcc_i64
|
||||
#define __smcc_u8
|
||||
#define __smcc_u16
|
||||
#define __smcc_u32
|
||||
#define __smcc_u64
|
||||
#define __smcc_f32
|
||||
#define __smcc_f64
|
||||
#define __smcc_bool
|
||||
#define __smcc_char
|
||||
#define __smcc_void
|
||||
#define __smcc_null
|
||||
#define __smcc_isize
|
||||
#define __smcc_usize
|
||||
#endif
|
||||
|
||||
typedef union core_cvalue {
|
||||
long double ld;
|
||||
double d;
|
||||
float f;
|
||||
unsigned long long ull;
|
||||
|
||||
char ch;
|
||||
/* number value */
|
||||
uint64_t n;
|
||||
|
||||
/* string value */
|
||||
struct {
|
||||
char* data;
|
||||
uintptr_t len;
|
||||
} cstr;
|
||||
|
||||
/* 16 byte == 128 bit */
|
||||
char val[16];
|
||||
} core_cvalue_t;
|
||||
|
||||
#endif
|
||||
20
runtime/libcore/include/libcore.h
Normal file
20
runtime/libcore/include/libcore.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef __SMCC_CORE_H__
|
||||
#define __SMCC_CORE_H__
|
||||
|
||||
#include <core_mem.h>
|
||||
#include <core_impl.h>
|
||||
#include <core_macro.h>
|
||||
|
||||
#define __SMCC_LOG_NO_STD_IMPL__
|
||||
#define log_snprintf smcc_snprintf
|
||||
#define log_printf smcc_eprintf
|
||||
#define log_exit smcc_exit
|
||||
#include <log.h>
|
||||
|
||||
#define _SMCC_STR(str) #str
|
||||
#define SMCC_STR(str) _SMCC_STR(str)
|
||||
|
||||
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||
#include <core_str.h>
|
||||
|
||||
#endif // __SMCC_CORE_H__
|
||||
Reference in New Issue
Block a user