Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion layout/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// 链接 kvspace dispatch 前端(已安装 /usr/lib)。布局侧只通过 extern "C" ABI 调用,
// 链接 kvspace dispatch 前端(与后端同装 /usr/lib/kvspace)。布局侧只通过 extern "C" ABI 调用,
// 运行期由前端按 DSN 选后端(shm://→kvspace-c,其余→kvspace-durable)。
fn main() {
println!("cargo:rustc-link-search=native=/usr/lib/kvspace");
println!("cargo:rustc-link-lib=dylib=kvspace");
println!("cargo:rustc-link-arg=-Wl,--disable-new-dtags"); // rpath 转 DT_RPATH,传递解析子依赖
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/kvspace");
}
20 changes: 0 additions & 20 deletions layout/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,26 +316,6 @@ pub fn float_lit(v: &str) -> Expr {
}
}

pub fn bool_lit(v: &str) -> Expr {
Expr {
op: String::new(),
args: Vec::new(),
val: v.to_string(),
quote: 0,
lit: LitKind::LitBool,
}
}

pub fn none_lit() -> Expr {
Expr {
op: String::new(),
args: Vec::new(),
val: "None".to_string(),
quote: 0,
lit: LitKind::LitNil,
}
}

pub fn call(op: &str, args: Vec<Expr>) -> Expr {
Expr {
op: op.to_string(),
Expand Down
12 changes: 10 additions & 2 deletions layout/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,19 @@ pub fn write_func(kv: &mut Kv, pkg: &str, fn_: &mut Func) {
// 不带 `*` 的按**值传递**(槽存值本体,体内裸坐标 `[0,-k]` 直读)。见 spec [[函数]]。
let mut param_coord: HashMap<String, String> = HashMap::new();
for (i, p) in fn_.sig.params.iter().enumerate() {
let d = if langtype::is_addr_param(&p.ty) { "*" } else { "" };
let d = if langtype::is_addr_param(&p.ty) {
"*"
} else {
""
};
param_coord.insert(p.name.clone(), format!("{d}[0,-{}]", i + 1));
}
for (i, r) in fn_.sig.returns.iter().enumerate() {
let d = if langtype::is_addr_param(&r.ty) { "*" } else { "" };
let d = if langtype::is_addr_param(&r.ty) {
"*"
} else {
""
};
param_coord.insert(r.name.clone(), format!("{d}[0,{}]", i + 1));
}

Expand Down
65 changes: 0 additions & 65 deletions layout/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub type Handle = *mut c_void;

// ── extern "C" 声明 ─────────────────────────────────────────────────────

#[allow(dead_code)]
extern "C" {
fn kvspaceConnect(dsn: *const c_char) -> Handle;
fn kvspaceClose(h: Handle);
Expand All @@ -28,16 +27,6 @@ extern "C" {
out: *mut *mut u8,
out_len: *mut u32,
) -> c_int;
/// 就地写:key 已存在、body_len==原 body_len → 返回原 box body 偏移指针;否则非 0。
fn kvspaceWriteInPlace(
h: Handle,
key: *const c_char,
resolve: c_int,
body_len: u32,
body: *mut *mut u8,
err: *mut c_char,
err_cap: u32,
) -> c_int;
/// 新位置写:按 (ref, storetype, ro, vid, langtype, body_len) 分配新 box、写 head,返回 body 偏移指针。
fn kvspaceWriteNewPlace(
h: Handle,
Expand Down Expand Up @@ -70,13 +59,6 @@ extern "C" {
buf_cap: u32,
out_len: *mut u32,
) -> c_int;
fn kvspaceDel(
h: Handle,
keys: *const *const c_char,
nkeys: u32,
err: *mut c_char,
err_cap: u32,
) -> c_int;
fn kvspaceDelTree(h: Handle, prefix: *const c_char, err: *mut c_char, err_cap: u32) -> c_int;
fn kvspaceMkindex(
h: Handle,
Expand All @@ -85,17 +67,6 @@ extern "C" {
err: *mut c_char,
err_cap: u32,
) -> c_int;
fn kvspaceMkindexExt(
h: Handle,
path: *const c_char,
ext_path: *const c_char,
err: *mut c_char,
err_cap: u32,
) -> c_int;
fn kvspaceRmindexExt(h: Handle, path: *const c_char, err: *mut c_char, err_cap: u32) -> c_int;
fn kvspaceClear(h: Handle, err: *mut c_char, err_cap: u32) -> c_int;
fn kvspaceDisconnect(h: Handle, err: *mut c_char, err_cap: u32) -> c_int;

fn kvspaceTlvEncode(
kind: *const c_char,
raw: *const u8,
Expand All @@ -107,12 +78,6 @@ extern "C" {
) -> c_int;
fn kvspaceDecodeHead(data: *const u8, data_len: u32, out: *mut kvspaceHead_t) -> c_int;

fn kvspaceNewPtr(
target_langtype: *const c_char,
target: *const c_char,
out: *mut *mut u8,
out_len: *mut u32,
) -> c_int;
fn kvspaceNewChar(bytes: *const u8, len: u32, out: *mut *mut u8, out_len: *mut u32) -> c_int;
fn kvspaceNewBool(v: u8, out: *mut *mut u8, out_len: *mut u32) -> c_int;
fn kvspaceNewInt64(v: i64, out: *mut *mut u8, out_len: *mut u32) -> c_int;
Expand Down Expand Up @@ -294,30 +259,6 @@ impl Kv {
unsafe { kvspaceMkindex(self.h, c.as_ptr(), 0, err.as_mut_ptr(), err.len() as u32) };
err_ret(&mut err, ret)
}

pub fn ext_index(&mut self, path: &str, ext_path: &str) -> Result<(), String> {
let cp = CString::new(path).expect("no NUL");
let ce = CString::new(ext_path).expect("no NUL");
let mut err: [c_char; 256] = [0; 256];
let ret = unsafe {
kvspaceMkindexExt(
self.h,
cp.as_ptr(),
ce.as_ptr(),
err.as_mut_ptr(),
err.len() as u32,
)
};
err_ret(&mut err, ret)
}

pub fn del_ext_index(&mut self, path: &str) -> Result<(), String> {
let c = CString::new(path).expect("no NUL");
let mut err: [c_char; 256] = [0; 256];
let ret =
unsafe { kvspaceRmindexExt(self.h, c.as_ptr(), err.as_mut_ptr(), err.len() as u32) };
err_ret(&mut err, ret)
}
}

impl Drop for Kv {
Expand Down Expand Up @@ -379,12 +320,6 @@ pub fn decode_head(data: &[u8]) -> kvspaceHead_t {

// ── 标准标量构造器 ───────────────────────────────────────────────────

pub fn new_ptr(target_langtype: &str, target: &str) -> Vec<u8> {
let ck = CString::new(target_langtype).expect("no NUL");
let ct = CString::new(target).expect("no NUL");
call_codec(|out, out_len| unsafe { kvspaceNewPtr(ck.as_ptr(), ct.as_ptr(), out, out_len) })
}

pub fn new_char(kind: &str, s: &str) -> Vec<u8> {
let bytes = s.as_bytes();
if kind == "char/utf8" {
Expand Down
101 changes: 1 addition & 100 deletions layout/src/keytree.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
//! KV 路径与成员分隔符统一管理(对齐 keytree/const.go、entry.go、frame.go、
//! member.go、vthread.go、sys.go)。所有构造 KV 路径的地方均须使用本模块常量。
//! KV 路径与成员分隔符统一管理。所有构造 KV 路径的地方均须使用本模块常量。

// ── 常量 ─────────────────────────────────────────────────────────────

pub const MEMBER_SEP: &str = "·"; // 成员访问分隔符(U+00B7 中点号):释放 '.' 供小数 key 使用
pub const RUNTIME_MEMBER_SEP: &str = "\u{2025}"; // U+2025,运行时保留字段前缀
pub const PATH_SEG_SEP: &str = "/"; // 路径分隔符
pub const PATH_SEG_LIB: &str = "lib"; // /lib
pub const PATH_SEG_VTHREAD: &str = "vthread"; // /vthread

pub const SEG_RO: &str = "ro";
pub const SEG_RPARAM: &str = "rparam";
pub const SEG_WPARAM: &str = "wparam";
pub const SEG_PC: &str = "pc";
pub const SEG_CALLPC: &str = "callpc";
pub const SEG_RETURNPC: &str = "returnpc";
pub const SEG_STATUS: &str = "status";
pub const SEG_CTIME: &str = "ctime";
pub const SEG_DEBUGGER: &str = "debugger";

pub const SEG_LIB: &str = "\u{2025}lib"; // 帧 extindex 标记
pub const SEG_LABELS: &str = "labels"; // /lib/<func>/‥labels/<name> → irseq
pub const SRC_EXT: &str = ".src"; // 函数源码文件后缀

pub const LIB_ROOT: &str = "/lib";
pub const RWIR_ROOT: &str = "/lib";
pub const VTHREAD_ROOT: &str = "/vthread";

// ── /lib ─────────────────────────────────────────────────────────────

Expand Down Expand Up @@ -59,90 +42,8 @@ pub fn rwir(opcode: &str) -> String {
format!("{RWIR_ROOT}/{opcode}")
}

// ── 帧路径 ───────────────────────────────────────────────────────────

fn frame_member(root: &str, seg: &str) -> String {
format!("{}{}{}", stack(root), RUNTIME_MEMBER_SEP, seg)
}

pub fn stack(root: &str) -> String {
format!("{}/", root.trim_end_matches(PATH_SEG_SEP))
}

pub fn frame_ro(root: &str) -> String {
frame_member(root, SEG_RO)
}

pub fn call_pc(root: &str) -> String {
frame_member(root, SEG_CALLPC)
}

pub fn return_pc(root: &str) -> String {
frame_member(root, SEG_RETURNPC)
}

/// Frame root of a PC (`/vthread/42/[3]/[5,0]` → `/vthread/42/[3]`).
pub fn frame_root(pc: &str) -> &str {
if let Some(idx) = pc.rfind("/[") {
&pc[..idx]
} else {
panic!("frame_root: pc has no /[coord] segment: {pc:?}")
}
}

pub fn entry_pc(root: &str) -> String {
format!("{}/[1,0]", root.trim_end_matches(PATH_SEG_SEP))
}

pub fn is_entry_pc(pc: &str) -> bool {
pc.ends_with("/[1,0]")
}

// ── 成员 ─────────────────────────────────────────────────────────────

pub fn member(base: &str, name: &str) -> String {
format!("{base}{MEMBER_SEP}{name}")
}

// ── /vthread ─────────────────────────────────────────────────────────

pub fn v_thread(vtid: &str) -> String {
format!("{VTHREAD_ROOT}/{vtid}")
}

fn vt_member(vtid: &str, seg: &str) -> String {
format!("{}/{}{}", v_thread(vtid), RUNTIME_MEMBER_SEP, seg)
}

pub fn v_thread_pc(vtid: &str) -> String {
vt_member(vtid, SEG_PC)
}

pub fn v_thread_status(vtid: &str) -> String {
vt_member(vtid, SEG_STATUS)
}

pub fn v_thread_ctime(vtid: &str) -> String {
vt_member(vtid, SEG_CTIME)
}

pub fn v_thread_debugger(vtid: &str) -> String {
vt_member(vtid, SEG_DEBUGGER)
}

pub fn v_thread_at(vtid: &str, key: &str) -> String {
format!("{}/{key}", v_thread(vtid))
}

/// 从 pc(/vthread/<vtid>/...)提取 vtid。
pub fn vtid_from_pc(pc: &str) -> String {
let prefix = format!("{VTHREAD_ROOT}/");
if !pc.starts_with(&prefix) {
return String::new();
}
let rest = &pc[prefix.len()..];
match rest.find(PATH_SEG_SEP) {
Some(i) => rest[..i].to_string(),
None => rest.to_string(),
}
}
8 changes: 0 additions & 8 deletions layout/src/kvkind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ use super::ffi;
// ── kind 常量 ─────────────────────────────────────────────────────────

pub const KIND_CHAR: &str = "char/utf32";
pub const KIND_CHAR_UTF8: &str = "char/utf8";
pub const KIND_CHAR_ASCII: &str = "char/ascii";
pub const KIND_BOOL: &str = "bool";
pub const KIND_INT64: &str = "int64";
pub const KIND_FLOAT64: &str = "float64";
pub const KIND_MAP: &str = "stringkeymap";
pub const KIND_INDEX: &str = "index";
pub const KIND_EXT_INDEX: &str = "extindex";
pub const KIND_STRUCT: &str = "struct";

// kvlang 自有 kind
Expand All @@ -25,7 +18,6 @@ pub const KIND_RWFUNC: &str = "rwfunc";
pub const KIND_DEF_RWIR: &str = "def rwir";
pub const KIND_DEF_LANGTYPE: &str = "def langtype";
pub const KIND_RWIR_OR_RWFUNC: &str = "rwir|rwfunc";
pub const KIND_SCOPE: &str = "scope";

// ── 通用 XValue 字节访问器 ───────────────────────────────────────────

Expand Down
13 changes: 8 additions & 5 deletions layout/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,12 @@ impl Parser {
ret.name,
ret.ty
.split('|')
.map(|a| if !a.starts_with('*') && (a.starts_with('/') || a.contains('·')) {
format!("*{a}")
} else {
a.to_string()
.map(|a| {
if !a.starts_with('*') && (a.starts_with('/') || a.contains('·')) {
format!("*{a}")
} else {
a.to_string()
}
})
.collect::<Vec<_>>()
.join("|")
Expand Down Expand Up @@ -737,7 +739,8 @@ impl Parser {
// 于是经 q 写成员就是在写读参的对象。只读性沿别名传播,否则 `q = p; q·x = v`
// 一句话就把「签名诚实原则」洗白了。
// 别名 → 源头读参(`q = p` 记 q→p;再 `r = q` 追到 p),供诊断点名真凶。
let mut tainted: std::collections::HashMap<String, String> = std::collections::HashMap::new();
let mut tainted: std::collections::HashMap<String, String> =
std::collections::HashMap::new();
let mut all: Vec<&Instruction> = Vec::new();
collect_body_insts(&func.body, &mut all);
loop {
Expand Down
15 changes: 10 additions & 5 deletions layout/tests/pipeline_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ fn compile_simple_func() {
let b = body(&sig_val);
assert_eq!(kvkind::rwfunc_num_reads(b), 2);
assert_eq!(kvkind::rwfunc_num_writes(b), 1);
// langtype 列表:读参在前(nr=2)、写参在后(nw=1)
assert_eq!(
kvkind::rwfunc_param_types(b),
vec!["int64", "int64", "int64"]
);
// langtype 列表:读参在前(nr=2)、写参在后(nw=1),落参数定义键 .[0,±k]
let types: Vec<String> = ["[0,-1]", "[0,-2]", "[0,1]"]
.iter()
.map(|k| {
kvkind::def_param_parts(&kv.get_one(&format!("/lib/sum.{k}")))
.unwrap()
.1
})
.collect();
assert_eq!(types, vec!["int64", "int64", "int64"]);

// 参数 Ptr
let a = kv.get_one("/lib/sum/A");
Expand Down
2 changes: 2 additions & 0 deletions runtime-rs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn main() {
let bin = format!("{manifest}/../bin"); // kvlang/bin(新构建的 libkvlang_runtime.so)

println!("cargo:rustc-link-search=native={bin}");
println!("cargo:rustc-link-search=native=/usr/lib/kvspace");
println!("cargo:rustc-link-search=native=/usr/lib");
println!("cargo:rustc-link-lib=dylib=kvlang_runtime");
println!("cargo:rustc-link-lib=dylib=kvspace");
Expand All @@ -32,6 +33,7 @@ fn main() {
println!("cargo:rustc-link-arg=-Wl,--disable-new-dtags"); // rpath 转 DT_RPATH,传递解析子依赖
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../lib");
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/kvspace");
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib");

// 内嵌顶层 stdlib/**/*.kv → EMBEDDED_KV。
Expand Down
Loading
Loading