Files
marqdo-cli/tests/conformance/semantics/55-first-fit-free-list-allocator.mq.md
T
cflmy e029853e32 feat(lang): implement RFC-LANG-0002 unified semantics (breaking)
One Markdown-to-program mapping, no version selector or legacy mapper.

Frontend/runtime:
- resolve @exec/@knowledge heading declarations with nearest-ancestor
  inheritance; only an Exec H2 is callable; add UnitKind::Knowledge and
  sem.* diagnostics (mode marker, unit name, exec-without-function,
  payload nesting/empty, syntax, control stack, params position,
  version directive, inferred-signature lint)
- extract Strong/Emph/inline Code as one-statement carriers, refuse
  nested carriers, and honour heading segment boundaries so an if/while
  cannot be closed across a heading
- lower if/else/while/end to the existing Branch/Loop/Return IR with a
  compile-time control stack; keep payload-relative source spans through
  expression parsing and rewrite them onto the file
- accept params(...) only as the first direct Exec H2 payload; keep the
  existing free-variable inference and named-only rule for inferred
  signatures; fix duplicate-mode/else/end and Knowledge-body inertness
- reject marqdo_semantics/syntax front matter directives

Data and tests:
- migrate stdlib/**/*.mq.md, regenerate stdlib.json digest
- migrate examples and the full semantics/negative/runtime corpus to
  the only current syntax; expected values/runtime codes preserved
- migrate Rust-embedded .mq.md fixtures, init template, CLI help and
  security-lane fixtures

Verified: cargo fmt --check, cargo clippy -D warnings and
cargo test --workspace --locked (green).
2026-11-21 12:26:46 +08:00

6.0 KiB

@exec 写32

params(内存: Memory, 基址: Int, 值: Int)

字节3 = 值 % 256

字节2 = 值 // 256 % 256

字节1 = 值 // 65536 % 256

字节0 = 值 // 16777216 % 256

内存[基址] = 字节0

内存[基址 + 1] = 字节1

内存[基址 + 2] = 字节2

内存[基址 + 3] = 字节3

return None

@exec 读32

params(内存: Memory, 基址: Int)

return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]

@exec 成功

params(值)

return {ok: true, value: 值}

@exec 失败

params(代码, 消息)

return {ok: false, code: 代码, message: 消息}

@exec 初始化

params(内存: Memory)

区域头:偏移 0 是空闲链表头,偏移 4 是 bump 指针;首个块从 8 开始。

写32(内存, 0, 0)

写32(内存, 4, 8)

return 成功(None)

@exec 查找

params(内存: Memory, 需求: Int)

返回 [前一个空闲块, 当前空闲块, 当前块大小];没有合适块时返回零值。

前一个 = 0

当前 = 读32(内存, 0)

while 当前 != 0

块大小 = 读32(内存, 当前)

if 块大小 >= 需求

return [前一个, 当前, 块大小]

end

前一个 = 当前

当前 = 读32(内存, 当前 + 4)

end

return [0, 0, 0]

@exec 分配

params(内存: Memory, 容量: Int, 请求大小: Int)

first-fit:先查空闲链表,必要时切分;否则从 bump 指针处取新块。 块头 8 字节:偏移 0 是总块大小,偏移 4 是空闲链表 next。返回 payload 偏移。

需求 = (请求大小 + 8 + 7) // 8 * 8

位置 = 查找(内存, 需求)

前一个 = 位置[0]

当前 = 位置[1]

块大小 = 位置[2]

if 当前 != 0

余量 = 块大小 - 需求

下一个 = 读32(内存, 当前 + 4)

if 余量 >= 16

余块 = 当前 + 需求

写32(内存, 余块, 余量)

写32(内存, 余块 + 4, 下一个)

if 前一个 == 0

写32(内存, 0, 余块)

else

写32(内存, 前一个 + 4, 余块)

end

写32(内存, 当前, 需求)

return 成功(当前 + 8)

else

if 前一个 == 0

写32(内存, 0, 下一个)

else

写32(内存, 前一个 + 4, 下一个)

end

return 成功(当前 + 8)

end

else

下一个空位 = 读32(内存, 4)

if 下一个空位 + 需求 <= 容量

写32(内存, 下一个空位, 需求)

写32(内存, 4, 下一个空位 + 需求)

return 成功(下一个空位 + 8)

else

return 失败("alloc.no_space", "没有足够的空闲空间")

end

end

@exec 插入空闲

params(内存: Memory, 块: Int)

空闲链表按地址升序;插入后先与后继合并,再与前去合并。

前一个 = 0

当前 = 读32(内存, 0)

while 当前 != 0 and 当前 < 块

前一个 = 当前

当前 = 读32(内存, 当前 + 4)

end

写32(内存, 块 + 4, 当前)

if 前一个 == 0

写32(内存, 0, 块)

else

写32(内存, 前一个 + 4, 块)

end

合并(内存, 前一个, 块, 当前)

return 成功(None)

@exec 合并

params(内存: Memory, 前一个: Int, 块: Int, 当前: Int)

相邻块只有在地址连续时才合并,避免产生虚假的大块。

块大小 = 读32(内存, 块)

if 当前 != 0 and 块 + 块大小 == 当前

合并大小 = 块大小 + 读32(内存, 当前)

写32(内存, 块, 合并大小)

写32(内存, 块 + 4, 读32(内存, 当前 + 4))

else

忽略后继合并 = 0

end

前一个大小 = 0

if 前一个 != 0

前一个大小 = 读32(内存, 前一个)

else

忽略前去读取 = 0

end

后继合并完成后再处理前驱;两个判断必须分开执行。

if 前一个 != 0 and 前一个 + 前一个大小 == 块

前一个新大小 = 前一个大小 + 读32(内存, 块)

写32(内存, 前一个, 前一个新大小)

写32(内存, 前一个 + 4, 读32(内存, 块 + 4))

else

忽略前去合并 = 0

end

return 成功(None)

@exec 已在空闲表

params(内存: Memory, 块: Int)

线性扫描空闲链表,检测重复释放。

当前 = 读32(内存, 0)

结果 = false

while 当前 != 0 and not 结果

if 当前 == 块

结果 = true

else

当前 = 读32(内存, 当前 + 4)

end

end

return 结果

@exec 释放

params(内存: Memory, 地址: Int)

把 payload 偏移换算回块头;重复释放返回可恢复错误,正常释放按地址插入并由“合并”处理相邻块。

块 = 地址 - 8

if 已在空闲表(内存, 块)

return 失败("alloc.double_free", "块已在空闲链表")

end

插入空闲(内存, 块)

return 成功(None)

@exec main

内存 = memory.new(1, 1)

初始化(内存)

甲 = 分配(内存, 65536, 8)

乙 = 分配(内存, 65536, 16)

内存[甲.value] = 11

内存[乙.value] = 22

释放(内存, 甲.value)

丙 = 分配(内存, 65536, 8)

内存[丙.value] = 33

释放(内存, 乙.value)

释放(内存, 丙.value)

丁 = 分配(内存, 65536, 4)

戊 = 分配(内存, 65536, 16)

己 = 分配(内存, 65536, 64)

释放(内存, 己.value)

庚 = 分配(内存, 65536, 8)

辛 = 分配(内存, 65536, 16)

壬 = 分配(内存, 65536, 8)

癸 = 分配(内存, 65536, 8)

内存[庚.value] = 44

内存[辛.value] = 55

内存[壬.value] = 66

内存[癸.value] = 77

失败结果 = 分配(内存, 65536, 100000)

内存2 = memory.new(1, 1)

初始化(内存2)

甲2 = 分配(内存2, 65536, 8)

乙2 = 分配(内存2, 65536, 8)

丙2 = 分配(内存2, 65536, 8)

分配(内存2, 65536, 65472)

释放(内存2, 甲2.value)

释放(内存2, 乙2.value)

释放(内存2, 丙2.value)

丁2 = 分配(内存2, 65536, 40)

戊2 = 分配(内存2, 65536, 8)

释放一次 = 释放(内存2, 丁2.value)

重复释放 = 释放(内存2, 丁2.value)

return [甲, 乙, 丙, 丁, 戊, 己, 庚, 辛, 壬, 癸, 失败结果, 甲2, 乙2, 丙2, 丁2, 戊2, 释放一次, 重复释放, 内存[甲.value], 内存[乙.value], 内存[丙.value], 内存[丁.value], 内存[戊.value], 内存[庚.value], 内存[辛.value], 内存[壬.value], 内存[癸.value]]