feat 新的运行时环境

This commit is contained in:
zzy
2025-11-20 11:22:37 +08:00
parent e22811f2f5
commit 5c24f35c87
23 changed files with 1755 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
[package]
name = "libcore"
default_features = [
"std_impl",
]
features = [
"std_impl",
]
dependencies = [
# TODO define some to disable stdio for self-contained build
{ name = "log", path = "../log" }
]

View 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__

View 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__

View 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__ */

View 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__ */

View 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

View 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__

View File

@@ -0,0 +1,95 @@
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <core_impl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/* ====== 内存管理核心接口实现 ====== */
void* smcc_malloc(usize size) {
return malloc(size);
}
void* smcc_calloc(usize count, usize size) {
return calloc(count, size);
}
void* smcc_realloc(void *ptr, usize new_size) {
return realloc(ptr, new_size);
}
void smcc_free(void *ptr) {
free(ptr);
}
/* ====== 文件系统核心接口实现 ====== */
static const char* get_file_mode_string(smcc_file_mode_t mode) {
switch (mode) {
case SMCC_FILE_READ: return "rb";
case SMCC_FILE_WRITE: return "wb";
case SMCC_FILE_APPEND: return "ab";
default: return "rb";
}
}
smcc_file_t smcc_fopen(const char *path, smcc_file_mode_t mode) {
const char* mode_str = get_file_mode_string(mode);
return (smcc_file_t)fopen(path, mode_str);
}
void smcc_fclose(smcc_file_t file) {
if (file) {
fclose((FILE*)file);
}
}
usize smcc_fread(smcc_file_t file, void *buffer, usize size) {
if (!file || !buffer) return 0;
return fread(buffer, 1, size, (FILE*)file);
}
usize smcc_fwrite(smcc_file_t file, const void *buffer, usize size) {
if (!file || !buffer) return 0;
return fwrite(buffer, 1, size, (FILE*)file);
}
cbool smcc_fexists(const char *path) {
smcc_file_t fp = smcc_fopen(path, SMCC_FILE_READ);
if (!fp) return false;
smcc_fclose(fp);
return true;
}
/* ====== 输入输出核心接口实现 ====== */
void smcc_snprintf(char *buf, usize size, const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(buf, size, format, args); // NOLINT
va_end(args);
}
void smcc_printf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
void smcc_eprintf(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
/* ====== 系统核心接口实现 ====== */
void smcc_exit(int code) {
exit(code);
}

View File

@@ -0,0 +1,224 @@
#include <core_mem.h>
#include <stdint.h>
// 判断是否支持非对齐访问x86/x64 支持)
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
#define UNALIGNED_ACCESS_ALLOWED 1
#else
#define UNALIGNED_ACCESS_ALLOWED 0
#endif
void* smcc_memcpy(void * dest, const void * restrict src, usize n) {
char* d = (char*)dest;
const char* s = (const char*)src;
// 快速路径:小内存拷贝
if (n <= 16) {
switch(n) {
case 16: d[15] = s[15]; /* fall through */
case 15: d[14] = s[14]; /* fall through */
case 14: d[13] = s[13]; /* fall through */
case 13: d[12] = s[12]; /* fall through */
case 12: d[11] = s[11]; /* fall through */
case 11: d[10] = s[10]; /* fall through */
case 10: d[9] = s[9]; /* fall through */
case 9: d[8] = s[8]; /* fall through */
case 8: d[7] = s[7]; /* fall through */
case 7: d[6] = s[6]; /* fall through */
case 6: d[5] = s[5]; /* fall through */
case 5: d[4] = s[4]; /* fall through */
case 4: d[3] = s[3]; /* fall through */
case 3: d[2] = s[2]; /* fall through */
case 2: d[1] = s[1]; /* fall through */
case 1: d[0] = s[0]; /* fall through */
default: break;
}
return dest;
}
#if UNALIGNED_ACCESS_ALLOWED
// 按8字节批量复制适用于支持非对齐访问的平台
uint64_t* d64 = (uint64_t*)d;
const uint64_t* s64 = (const uint64_t*)s;
while (n >= 8) {
*d64++ = *s64++;
n -= 8;
}
d = (char*)d64;
s = (const char*)s64;
#endif
// 处理剩余字节
while (n--) {
*d++ = *s++;
}
return dest;
}
void *smcc_memmove(void *dest, const void *src, usize n)
{
char* d = (char*)dest;
const char* s = (const char*)src;
// 地址相同直接返回
if (d == s) {
return dest;
}
// 内存区域无重叠或前向拷贝
if (d < s || d >= s + n) {
return smcc_memcpy(d, s, n);
} else {
// 后向拷贝处理重叠情况
d += n;
s += n;
while (n--) {
*(--d) = *(--s);
}
}
return dest;
}
void* smcc_memset(void *s, int c, usize n) {
unsigned char* p = (unsigned char*)s;
unsigned char byte_val = (unsigned char)c;
// 快速设置小块内存
if (n <= 16) {
switch(n) {
case 16: p[15] = byte_val; /* fall through */
case 15: p[14] = byte_val; /* fall through */
case 14: p[13] = byte_val; /* fall through */
case 13: p[12] = byte_val; /* fall through */
case 12: p[11] = byte_val; /* fall through */
case 11: p[10] = byte_val; /* fall through */
case 10: p[9] = byte_val; /* fall through */
case 9: p[8] = byte_val; /* fall through */
case 8: p[7] = byte_val; /* fall through */
case 7: p[6] = byte_val; /* fall through */
case 6: p[5] = byte_val; /* fall through */
case 5: p[4] = byte_val; /* fall through */
case 4: p[3] = byte_val; /* fall through */
case 3: p[2] = byte_val; /* fall through */
case 2: p[1] = byte_val; /* fall through */
case 1: p[0] = byte_val; /* fall through */
default: break;
}
return s;
}
#if UNALIGNED_ACCESS_ALLOWED
// 构造一个8字节值用于批量填充
uint64_t fill_val = ((uint64_t)byte_val << 56) |
((uint64_t)byte_val << 48) |
((uint64_t)byte_val << 40) |
((uint64_t)byte_val << 32) |
((uint64_t)byte_val << 24) |
((uint64_t)byte_val << 16) |
((uint64_t)byte_val << 8) |
(uint64_t)byte_val;
uint64_t* p64 = (uint64_t*)p;
while (n >= 8) {
*p64++ = fill_val;
n -= 8;
}
p = (unsigned char*)p64;
#endif
// 设置剩余字节
while (n--) {
*p++ = byte_val;
}
return s;
}
int smcc_memcmp(const void *s1, const void *s2, usize n) {
const unsigned char* p1 = (const unsigned char*)s1;
const unsigned char* p2 = (const unsigned char*)s2;
// 快速比较小块内存
if (n <= 16) {
unsigned char diff = 0;
switch(n) {
case 16: diff |= (p1[15] ^ p2[15]); /* fall through */
case 15: diff |= (p1[14] ^ p2[14]); /* fall through */
case 14: diff |= (p1[13] ^ p2[13]); /* fall through */
case 13: diff |= (p1[12] ^ p2[12]); /* fall through */
case 12: diff |= (p1[11] ^ p2[11]); /* fall through */
case 11: diff |= (p1[10] ^ p2[10]); /* fall through */
case 10: diff |= (p1[9] ^ p2[9]); /* fall through */
case 9: diff |= (p1[8] ^ p2[8]); /* fall through */
case 8: diff |= (p1[7] ^ p2[7]); /* fall through */
case 7: diff |= (p1[6] ^ p2[6]); /* fall through */
case 6: diff |= (p1[5] ^ p2[5]); /* fall through */
case 5: diff |= (p1[4] ^ p2[4]); /* fall through */
case 4: diff |= (p1[3] ^ p2[3]); /* fall through */
case 3: diff |= (p1[2] ^ p2[2]); /* fall through */
case 2: diff |= (p1[1] ^ p2[1]); /* fall through */
case 1: diff |= (p1[0] ^ p2[0]); /* fall through */
default: break;
}
// 只有当所有字节都相等时diff才为0
if (!diff) return 0;
// 找到第一个不同的字节并返回差值
size_t i = 0;
switch(n) {
case 16: if(p1[15] != p2[15]) {i=15;break;}
case 15: if(p1[14] != p2[14]) {i=14;break;}
case 14: if(p1[13] != p2[13]) {i=13;break;}
case 13: if(p1[12] != p2[12]) {i=12;break;}
case 12: if(p1[11] != p2[11]) {i=11;break;}
case 11: if(p1[10] != p2[10]) {i=10;break;}
case 10: if(p1[9] != p2[9]) {i=9;break;}
case 9: if(p1[8] != p2[8]) {i=8;break;}
case 8: if(p1[7] != p2[7]) {i=7;break;}
case 7: if(p1[6] != p2[6]) {i=6;break;}
case 6: if(p1[5] != p2[5]) {i=5;break;}
case 5: if(p1[4] != p2[4]) {i=4;break;}
case 4: if(p1[3] != p2[3]) {i=3;break;}
case 3: if(p1[2] != p2[2]) {i=2;break;}
case 2: if(p1[1] != p2[1]) {i=1;break;}
case 1: if(p1[0] != p2[0]) {i=0;break;}
default: break;
}
return p1[i] - p2[i];
}
#if UNALIGNED_ACCESS_ALLOWED
// 按8字节批量比较
const uint64_t* p1_64 = (const uint64_t*)p1;
const uint64_t* p2_64 = (const uint64_t*)p2;
while (n >= 8) {
if (*p1_64 != *p2_64) {
// 发现不同在8字节内定位具体位置
const unsigned char* b1 = (const unsigned char*)p1_64;
const unsigned char* b2 = (const unsigned char*)p2_64;
for (int j = 0; j < 8; j++) {
if (b1[j] != b2[j])
return b1[j] - b2[j];
}
}
p1_64++;
p2_64++;
n -= 8;
}
p1 = (const unsigned char*)p1_64;
p2 = (const unsigned char*)p2_64;
#endif
// 比较剩余字节
while (n--) {
if (*p1 != *p2) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}

View File

@@ -0,0 +1,13 @@
#include <libcore.h>
#include <stdio.h>
int main(void) {
printf("test log...\n");
Assert(1 == 1);
LOG_TRACE("log trace");
LOG_NOTSET("log notset");
LOG_DEBUG("log debug");
LOG_INFO("log info");
LOG_WARN("log warn");
LOG_ERROR("log error");
}