@yeyelailou/co 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,11 +42,33 @@ function 编译器代次() {
42
42
  // 字符串字面量在只读段里直接就是一个扁平串对象,所以「取长度」「打印」
43
43
  // 对字面量和拼接产物走同一条代码路径;反过来也意味着运行时绝不能写
44
44
  // 字面量对象——摊平缓存只会落在堆上的绳索节点里。
45
- // 列表对象:[0]=长度 [8]=容量 [16]=数据指针(元素为 8 字节槽)
45
+ // 列表对象:[0]=长度 [8]=容量 [16]=数据指针
46
+ // 0.0.5 起元素槽带类型标签,宽 16 字节:[+0]=标签 [+8]=载荷。
47
+ // 标签命名空间(与装箱盒共用):
48
+ // 0=空 1=整数 2=布尔 3=字符串 4=列表 5=函数 6=字典
49
+ // 装箱盒(动态值 d[k] 的产物)同样是 [0]=标签 [8]=载荷 的 16 字节,
50
+ // 所以「槽」与「盒」同构:读槽 = 拷两字进盒;写盒 = 拆两字进槽。
51
+ // 字典对象:[0]=数量 [8]=容量 [16]=条目表 [24]=键序表 [32]=键序长度 [40]=键序容量
52
+ // 条目宽 24:[0]=键串对象指针 [8]=盒指针 [16]=占用标志
53
+ // 键序表是键指针数组(插入顺序)——与宿主 0.0.5 新语义逐字节对齐:
54
+ // 打印/键/值/相等/合并一律按插入顺序,不再依赖槽位分布。
55
+ // 函数对象:[0]=5 [8]=代码地址 [16]=环境指针 [24]=返回标签 [32]=名字串指针(匿名=0)
56
+ const 标签空 = 0, 标签整数 = 1, 标签布尔 = 2, 标签字符串 = 3, 标签列表 = 4, 标签函数 = 5, 标签字典 = 6;
57
+ const 种类标签 = { none: 标签空, int: 标签整数, bool: 标签布尔, str: 标签字符串, list: 标签列表, func: 标签函数, dict: 标签字典 };
58
+ const 标签种类 = { 0: "none", 1: "int", 2: "bool", 3: "str", 4: "list", 5: "func", 6: "dict" };
59
+ const 盒字节 = 16;
60
+ const 槽字节 = 16;
46
61
  const HEAD_NEXT = 0, HEAD_LIMIT = 8;
47
62
  const 串头字节 = 24; // 扁平串数据起点
48
63
  const 绳索字节 = 48; // 绳索节点定长:[0]=-1 [8]字符 [16]字节 [24]左 [32]右 [40]摊平缓存
49
64
  const 列表头字节 = 24;
65
+ const 字典头字节 = 48;
66
+ const 条目字节 = 24;
67
+ const 函数对象字节 = 40;
68
+ const 处理器帧字节 = 32;
69
+ // 异常处理器栈顶 / 异常载荷(堆段固定地址,零初始化=空栈无异常)
70
+ const HANDLER_TOP = HEAP_ADDR + 48;
71
+ const EXC_SLOT = HEAP_ADDR + 56;
50
72
  // 摊平用「显式栈」而不是机器栈:链式 s = s + x 攒出的绳索深度等于拼接次数
51
73
  // (基准程序是 3000 层),递归写会吃掉几百 KB 机器栈,深一点就爆栈。
52
74
  // 所以从堆段开头切出一段专用栈区,压的是子节点指针,8 字节一层。
@@ -126,6 +148,11 @@ class Emitter {
126
148
  this.bytes[f.pos + 1] = (rel >> 8) & 0xFF;
127
149
  this.bytes[f.pos + 2] = (rel >> 16) & 0xFF;
128
150
  this.bytes[f.pos + 3] = (rel >> 24) & 0xFF;
151
+ } else if (f.type === "codeAbs64") {
152
+ // 代码段内的绝对地址(函数对象里的代码指针)= 基址 + ELF 头 + 程序头 + 代码偏移
153
+ const abs = BigInt(BASE_ADDR) + BigInt(ELF_HDR_SIZE + PHDR_SIZE * PHDR_NUM) + BigInt(target);
154
+ for (let i = 0; i < 8; i++)
155
+ this.bytes[f.pos + i] = Number((abs >> BigInt(i * 8)) & 0xFFn);
129
156
  } else if (f.type === "dataAbs64") {
130
157
  // 数据段紧跟代码段:基址 + ELF 头 + 全部程序头 + 代码长度 + 段内偏移
131
158
  const abs = BigInt(BASE_ADDR) + BigInt(ELF_HDR_SIZE + PHDR_SIZE * PHDR_NUM + codeSize) + BigInt(target);
@@ -404,9 +431,35 @@ function cmpRAbsR(em, r, abs) {
404
431
  em.b(rex(1, r>=8?1:0, 0, 0));
405
432
  em.b(0x3B); em.b(modrm(0, r&7, 4)); em.b(ABS_SIB); em.i32(abs);
406
433
  }
434
+ // mov qword [base + disp], imm32(REX.W C7 /0;标签值都小于 2^31,够用)
435
+ function movDispImm(em, base, disp, imm) {
436
+ const m = fitsDisp8(disp) ? 1 : 2;
437
+ em.b(rex(1, 0, 0, base>=8?1:0));
438
+ em.b(0xC7);
439
+ emitMem(em, m, 0, base, disp);
440
+ em.i32(imm);
441
+ }
442
+ // mov qword [abs], imm32
443
+ function movAbsImm(em, abs, imm) {
444
+ em.b(rex(1, 0, 0, 0));
445
+ em.b(0xC7); em.b(modrm(0, 0, 4)); em.b(ABS_SIB); em.i32(abs);
446
+ em.i32(imm);
447
+ }
448
+ // test r64, imm32(REX.W F7 /0)
449
+ function testRImm(em, r, imm) {
450
+ em.b(rex(1, 0, 0, r>=8?1:0));
451
+ em.b(0xF7); em.b(modrm(3, 0, r&7)); em.i32(imm);
452
+ }
407
453
 
408
454
  // syscall
409
455
  function syscall(em) { em.bs([0x0F, 0x05]); }
456
+ // 0.0.5 审计修正:既有「movRI64(rax,0) 后 push fixups{pos=this.pos}」把位置记在
457
+ // 立即数【开头】——resolve 从 pos 覆写 8 字节,会盖掉下一条指令前 3 字节,
458
+ // 且立即数高 32 位留在 0(匿名函数代码地址/数据地址/异常落地全部中招,随机跑飞)。
459
+ // 正确:先写 imm64,再记 pos = 当前 - 8。所有绝对地址 fixup 一律走这个助手。
460
+ function fixupAbs64(em, name, type) {
461
+ em.fixups.push({ pos: em.pos - 8, label: name, type });
462
+ }
410
463
  // ret
411
464
  function ret(em) { em.b(0xC3); }
412
465
  // leave
@@ -422,6 +475,16 @@ const SETCC = { e:0x94, ne:0x95, l:0x9C, le:0x9E, g:0x9F, ge:0x9D,
422
475
  function jmp(em, lbl) { em.b(0xE9); em.fixupRel32(lbl); }
423
476
  function jcc(em, cc, lbl) { em.bs([0x0F, cc]); em.fixupRel32(lbl); }
424
477
  function call(em, lbl) { em.b(0xE8); em.fixupRel32(lbl); }
478
+ // call r/m64(间接调用:FF /2)。r>=8 需要 REX.B;不需要 REX.W。
479
+ function callR(em, r) {
480
+ if (r >= 8) em.b(0x41);
481
+ em.b(0xFF); em.b(modrm(3, 2, r & 7));
482
+ }
483
+ // jmp r/m64(FF /4)
484
+ function jmpR(em, r) {
485
+ if (r >= 8) em.b(0x41);
486
+ em.b(0xFF); em.b(modrm(3, 4, r & 7));
487
+ }
425
488
  function setccAl(em, cc) { em.bs([0x0F, cc, 0xC0]); } // setcc al (modrm=11,000,000=al)
426
489
  function movzbEaxAl(em) { em.bs([0x0F, 0xB6, 0xC0]); }
427
490
 
@@ -459,8 +522,9 @@ function 统计声明数(node, 已见 = new WeakSet()) {
459
522
  return n;
460
523
  }
461
524
  const t = node.类型;
462
- // 与 genFor / genVarDecl 一一对应:这两类节点各调用一次 allocVar
525
+ // 与 genFor / genVarDecl / genTry 一一对应:这些节点各调用一次 allocVar
463
526
  if (t === "变量声明" || t === "对于") n++;
527
+ if (t === "尝试" && node.捕获变量) n++;
464
528
  for (const k of Object.keys(node)) {
465
529
  if (k === "类型") continue;
466
530
  n += 统计声明数(node[k], 已见);
@@ -477,6 +541,7 @@ class FuncCtx {
477
541
  this.tempBase = 变量数;
478
542
  this.breakEnds = [];
479
543
  this.contHeads = [];
544
+ this.inTry = 0;
480
545
  this.retKind = "none";
481
546
  }
482
547
  allocVar(name, kind) {
@@ -492,7 +557,11 @@ class FuncCtx {
492
557
  }
493
558
  slotOff(name) {
494
559
  const s = this.slots.get(name);
495
- if (!s) throw new Error(`未定义变量: ${name}`);
560
+ if (!s) {
561
+ if (this.闭包)
562
+ throw new Error(`匿名函数暂不支持捕获外层变量(${name}):闭包按外层环境链取值,直弹后端还没有环境对象,改走解释器兜底`);
563
+ throw new Error(`未定义变量: ${name}`);
564
+ }
496
565
  return s;
497
566
  }
498
567
  markBool(name) { const s = this.slots.get(name); if (s) s.是布尔 = true; }
@@ -530,6 +599,8 @@ class DirectGen {
530
599
  this.funcSigs = new Map(); // 标签 -> {参, 返, 数}
531
600
  this.strLabelMap = new Map();
532
601
  this.rodata = []; // {label, data: Buffer}
602
+ this.pendingLambdas = []; // 匿名函数体:表达式里先发对象,函数体统一在 co_main 之后发射
603
+ this.lambdaSeq = 0;
533
604
  }
534
605
 
535
606
  run() {
@@ -563,6 +634,12 @@ class DirectGen {
563
634
  }
564
635
  // co_main
565
636
  this.genCoMain();
637
+ // 匿名函数体:放在 co_main 之后,表达式里只引用标签(标签在链接期统一解析)。
638
+ // 循环发射,因为函数体里可能再定义匿名函数。
639
+ while (this.pendingLambdas.length > 0) {
640
+ const 待发 = this.pendingLambdas.shift();
641
+ this.emitLambda(待发.节点, 待发.标签);
642
+ }
566
643
 
567
644
  // 数据段
568
645
  const { buf: dataBuf, labelOff: dataLabelOff } = this.buildData();
@@ -653,6 +730,7 @@ class DirectGen {
653
730
  this.genStepZero();
654
731
  this.genPrintNl();
655
732
  this.genPrintSpace();
733
+ this.genDynRuntime();
656
734
  }
657
735
 
658
736
  // 把只读段消息对象的地址装进 rdi(地址要等链接期才知道,走 dataAbs64 修复)
@@ -1044,7 +1122,7 @@ class DirectGen {
1044
1122
  ret(this.em);
1045
1123
  }
1046
1124
 
1047
- // ---- 列表对象:[0]=长度 [8]=容量 [16]=数据指针(每元素 8 字节)----
1125
+ // ---- 列表对象:[0]=长度 [8]=容量 [16]=数据指针(元素为 16 字节带标签槽)----
1048
1126
  // rt_list_new: rdi=预留容量 -> rax=表
1049
1127
  genListNew() {
1050
1128
  this.em.label("rt_list_new");
@@ -1060,7 +1138,7 @@ class DirectGen {
1060
1138
  call(this.em, "rt_alloc");
1061
1139
  movRR(this.em, R.r12, R.rax); // 表头
1062
1140
  movRR(this.em, R.rdi, R.rbx);
1063
- shlRImm(this.em, R.rdi, 3); // 容量 * 8
1141
+ shlRImm(this.em, R.rdi, 4); // 容量 * 16(带标签槽)
1064
1142
  call(this.em, "rt_alloc");
1065
1143
  movDispR(this.em, R.r12, 16, R.rax); // 数据区
1066
1144
  xorRR(this.em, R.rdx, R.rdx);
@@ -1072,99 +1150,114 @@ class DirectGen {
1072
1150
  ret(this.em);
1073
1151
  }
1074
1152
 
1075
- // rt_list_append: rdi=表, rsi=值 -> rax=表(扩容后表头地址不变,可安全写回)
1153
+ // rt_list_append: rdi=表, rsi=标签, rdx=载荷 -> rax=表
1154
+ // 扩容后表头地址不变(数据区搬迁,头不动),可安全写回。
1076
1155
  genListAppend() {
1077
1156
  this.em.label("rt_list_append");
1078
1157
  const fit = this.em.nl("la_fit");
1079
1158
  movRDisp(this.em, R.rax, R.rdi, 0);
1080
1159
  cmpRDisp(this.em, R.rax, R.rdi, 8);
1081
1160
  jcc(this.em, JCC.l, fit);
1082
- // 容量不足:翻倍并整体搬迁。值先藏进 r13,避免被 rep movsb 的寄存器打乱。
1161
+ // 容量不足:翻倍并整体搬迁(槽宽 16 字节)。标签/载荷先藏进 r13/r14,
1162
+ // 因为 rep movsb 会吃掉 rsi/rdi/rcx。
1083
1163
  pushR(this.em, R.rbx);
1084
1164
  pushR(this.em, R.r12);
1085
1165
  pushR(this.em, R.r13);
1086
1166
  pushR(this.em, R.r14);
1167
+ pushR(this.em, R.r15);
1087
1168
  movRR(this.em, R.rbx, R.rdi);
1088
- movRR(this.em, R.r13, R.rsi);
1169
+ movRR(this.em, R.r13, R.rsi); // 标签
1170
+ movRR(this.em, R.r14, R.rdx); // 载荷
1089
1171
  movRDisp(this.em, R.r12, R.rbx, 8);
1090
1172
  shlRImm(this.em, R.r12, 1); // 新容量 = 旧 * 2
1091
1173
  movRR(this.em, R.rdi, R.r12);
1092
- shlRImm(this.em, R.rdi, 3);
1174
+ shlRImm(this.em, R.rdi, 4); // 新容量 * 16
1093
1175
  call(this.em, "rt_alloc");
1094
- movRR(this.em, R.r14, R.rax); // 新数据区
1176
+ movRR(this.em, R.r15, R.rax); // 新数据区
1095
1177
  movRDisp(this.em, R.rsi, R.rbx, 16); // 旧数据区
1096
- movRR(this.em, R.rdi, R.r14);
1097
1178
  movRDisp(this.em, R.rcx, R.rbx, 0);
1098
- shlRImm(this.em, R.rcx, 3); // 长度 * 8 字节
1179
+ shlRImm(this.em, R.rcx, 4); // 长度 * 16 字节
1180
+ movRR(this.em, R.rdi, R.r15);
1099
1181
  cld(this.em);
1100
1182
  repMovsb(this.em);
1101
- movDispR(this.em, R.rbx, 16, R.r14);
1183
+ movDispR(this.em, R.rbx, 16, R.r15);
1102
1184
  movDispR(this.em, R.rbx, 8, R.r12);
1103
1185
  movRR(this.em, R.rdi, R.rbx);
1104
1186
  movRR(this.em, R.rsi, R.r13);
1187
+ movRR(this.em, R.rdx, R.r14);
1188
+ popR(this.em, R.r15);
1105
1189
  popR(this.em, R.r14);
1106
1190
  popR(this.em, R.r13);
1107
1191
  popR(this.em, R.r12);
1108
1192
  popR(this.em, R.rbx);
1109
1193
  this.em.label(fit);
1110
- // 注意:rdi 必须是表对象。扩容分支里 rdi 被 rt_alloc 改写过了,
1111
- // 所以两条路都要在这里把「表」重新装回 rdi,否则元素会写进错误的列表。
1194
+ // 写 [len] 槽。rdi 必须是表对象(扩容分支改写过 rdi,两条路都重装)。
1112
1195
  movRR(this.em, R.rcx, R.rdi); // rcx = 表
1113
1196
  movRDisp(this.em, R.rax, R.rdi, 0); // 长度
1114
- movRDisp(this.em, R.rdx, R.rdi, 16); // 数据基址
1115
- movIdxStore(this.em, R.rdx, R.rax, R.rsi); // data[len] = 值
1197
+ movRDisp(this.em, R.r8, R.rdi, 16); // 数据基址
1198
+ movRR(this.em, R.r9, R.rax);
1199
+ shlRImm(this.em, R.r9, 4);
1200
+ addRR(this.em, R.r8, R.r9); // r8 = 槽地址
1201
+ movDispR(this.em, R.r8, 0, R.rsi); // [槽+0]=标签
1202
+ movDispR(this.em, R.r8, 8, R.rdx); // [槽+8]=载荷
1116
1203
  incR(this.em, R.rax);
1117
1204
  movDispR(this.em, R.rcx, 0, R.rax);
1118
1205
  movRR(this.em, R.rax, R.rcx);
1119
1206
  ret(this.em);
1120
1207
  }
1121
1208
 
1122
- // rt_list_get: rdi=表, rsi=下标 -> rax=元素。越界直接报中文错误。
1209
+ // rt_list_get_ptr: rdi=表, rsi=下标 -> rax=槽地址(越界报中文错误)
1210
+ // 返回地址而不是值:槽是 16 字节(标签+载荷),两个返回值放不进一个寄存器,
1211
+ // 让调用方按自己的静态种类读一半或装盒,快路径(整数列表)零额外开销。
1123
1212
  genListGet() {
1124
- this.em.label("rt_list_get");
1213
+ this.em.label("rt_list_get_ptr");
1125
1214
  const ok = this.em.nl("lg_ok");
1126
1215
  const load = this.em.nl("lg_load");
1216
+ const oob = this.em.nl("lg_oob");
1127
1217
  movRR(this.em, R.rax, R.rsi);
1128
1218
  testRR(this.em, R.rax, R.rax);
1129
1219
  jcc(this.em, JCC.ns, ok);
1130
1220
  movRDisp(this.em, R.rcx, R.rdi, 0); // 负下标 = 长度 + 下标
1131
1221
  addRR(this.em, R.rax, R.rcx);
1132
1222
  this.em.label(ok);
1223
+ testRR(this.em, R.rax, R.rax); // 回绕后仍为负 → 越界
1224
+ jcc(this.em, JCC.s, oob);
1133
1225
  movRDisp(this.em, R.rcx, R.rdi, 0);
1134
1226
  cmpRR(this.em, R.rax, R.rcx);
1135
- jcc(this.em, JCC.b, load);
1136
- this.loadMsg(this.strConst("运行错误:列表下标越界\n"));
1137
- jmp(this.em, "rt_fail");
1138
- this.em.label(load);
1227
+ jcc(this.em, JCC.ge, oob); // >= 长度 → 越界
1139
1228
  movRDisp(this.em, R.rdx, R.rdi, 16);
1140
- movIdxLoad(this.em, R.rax, R.rdx, R.rax);
1229
+ shlRImm(this.em, R.rax, 4); // 下标 * 16
1230
+ addRR(this.em, R.rax, R.rdx);
1141
1231
  ret(this.em);
1232
+ this.em.label(oob);
1233
+ this.loadMsg(this.strConst("运行错误:列表下标越界\n"));
1234
+ jmp(this.em, "rt_fail");
1142
1235
  }
1143
1236
 
1144
- // rt_list_set: rdi=表, rsi=下标, rdx=值
1237
+ // rt_list_set_ptr: rdi=表, rsi=下标 -> rax=槽地址(越界报错)
1145
1238
  genListSet() {
1146
- this.em.label("rt_list_set");
1239
+ this.em.label("rt_list_set_ptr");
1147
1240
  const ok = this.em.nl("ls_ok");
1148
1241
  const store = this.em.nl("ls_store");
1242
+ const oob = this.em.nl("ls_oob");
1149
1243
  movRR(this.em, R.rax, R.rsi);
1150
1244
  testRR(this.em, R.rax, R.rax);
1151
1245
  jcc(this.em, JCC.ns, ok);
1152
1246
  movRDisp(this.em, R.rcx, R.rdi, 0);
1153
1247
  addRR(this.em, R.rax, R.rcx);
1154
1248
  this.em.label(ok);
1249
+ testRR(this.em, R.rax, R.rax);
1250
+ jcc(this.em, JCC.s, oob);
1155
1251
  movRDisp(this.em, R.rcx, R.rdi, 0);
1156
1252
  cmpRR(this.em, R.rax, R.rcx);
1157
- jcc(this.em, JCC.b, store);
1253
+ jcc(this.em, JCC.ge, oob);
1254
+ movRDisp(this.em, R.rdx, R.rdi, 16);
1255
+ shlRImm(this.em, R.rax, 4);
1256
+ addRR(this.em, R.rax, R.rdx);
1257
+ ret(this.em);
1258
+ this.em.label(oob);
1158
1259
  this.loadMsg(this.strConst("运行错误:列表下标越界\n"));
1159
1260
  jmp(this.em, "rt_fail");
1160
- this.em.label(store);
1161
- // 值在 rdx(契约:rdi=表 rsi=下标 rdx=值)。取数据基址必须用别的寄存器,
1162
- // 之前直接 mov rdx,[rdi+16] 把待写入的值覆盖成数据指针,
1163
- // 结果 data[idx] = 下标,列表内容被静默写坏。
1164
- movRR(this.em, R.rcx, R.rdx); // rcx = 值
1165
- movRDisp(this.em, R.rdx, R.rdi, 16); // 数据基址
1166
- movIdxStore(this.em, R.rdx, R.rax, R.rcx);
1167
- ret(this.em);
1168
1261
  }
1169
1262
 
1170
1263
  // rt_list_len: rdi=表 -> rax=长度
@@ -1174,146 +1267,1632 @@ class DirectGen {
1174
1267
  ret(this.em);
1175
1268
  }
1176
1269
 
1177
- // rt_print_str: rdi = 串对象(字面量或绳索都走这条路)
1178
- // 先摊平(字面量本身就是扁平串,摊平是空操作;绳索摊平一次后缓存),
1179
- // 再按 [16] 的字节数定长写出——不再 strlen 扫描,所以头部 24 字节
1180
- // 的对象前缀不会被当成数据打出去。
1181
- genPrintStr() {
1182
- this.em.label("rt_print_str");
1270
+ // ============================================================
1271
+ // 0.0.5 动态值 / 字典 / 异常运行时
1272
+ // ============================================================
1273
+ // 约定(与宿主 co.c 的新语义逐字节对齐):
1274
+ // · 盒与列表槽同构:[0]=标签 [8]=载荷;标签 0空/1整数/2布尔/3字符串/4列表/5函数/6字典
1275
+ // · 字典:开放寻址(线性探测 + 向后回填删除),容量恒为 2 的幂,
1276
+ // 负载 (count+1)*4 > cap*3 时翻倍重排;键序表按【插入顺序】迭代
1277
+ // · 哈希与宿主逐位一致:h = h*31 + (signed char)byte,32 位无符号回绕
1278
+ // · 异常:堆上处理器栈(HANDLER_TOP 链表),rt_throw 恢复 rsp/rbp 后
1279
+ // jmp 落地标签;无人捕获按宿主文案写 stderr 退 1
1280
+
1281
+ // 把只读段串对象的绝对地址装进 rax(数据段地址链接期才知道)
1282
+ loadStrRax(label) {
1283
+ movRI64(this.em, R.rax, 0n);
1284
+ this.em.fixups.push({ pos: this.em.pos - 8, label, type: "dataAbs64" });
1285
+ }
1286
+ // movzx r64, byte [base]
1287
+ loadByte0(em, r, base) {
1288
+ em.b(rex(1, r>=8?1:0, 0, base>=8?1:0));
1289
+ em.b(0x0F); em.b(0xB6); em.b(modrm(0, r&7, base&7));
1290
+ }
1291
+ // movsx r32, byte [base](宿主 dict_hash 的 signed char 语义)
1292
+ signextByte0(em, r, base) {
1293
+ em.b(rex(0, r>=8?1:0, 0, base>=8?1:0));
1294
+ em.b(0x0F); em.b(0xBE); em.b(modrm(0, r&7, base&7));
1295
+ }
1296
+
1297
+ genDynRuntime() {
1298
+ // rt_box: rdi=标签, rsi=载荷 -> rax=盒
1299
+ this.em.label("rt_box");
1300
+ pushR(this.em, R.r12);
1301
+ movRR(this.em, R.r12, R.rdi);
1302
+ movRI64(this.em, R.rdi, BigInt(盒字节));
1303
+ call(this.em, "rt_alloc"); // rt_alloc 不动 rsi
1304
+ movDispR(this.em, R.rax, 0, R.r12);
1305
+ movDispR(this.em, R.rax, 8, R.rsi);
1306
+ popR(this.em, R.r12);
1307
+ ret(this.em);
1308
+
1309
+ // rt_hash: rsi=键串对象 -> rax=哈希(32 位无符号回绕,逐位对齐宿主)
1310
+ this.em.label("rt_hash");
1183
1311
  pushR(this.em, R.rbx);
1184
- movRR(this.em, R.rbx, R.rdi);
1185
- call(this.em, "rt_flatten");
1186
- movRDisp(this.em, R.rdx, R.rax, 16); // 字节数
1187
- movRR(this.em, R.rsi, R.rax);
1188
- addRImm(this.em, R.rsi, 串头字节); // 数据起点
1189
- movRI64(this.em, R.rdi, 1n);
1190
- movRI64(this.em, R.rax, 1n);
1191
- syscall(this.em);
1312
+ movRR(this.em, R.rbx, R.rsi);
1313
+ movRR(this.em, R.rdi, R.rbx);
1314
+ call(this.em, "rt_flatten"); // rax=扁平对象(字面量原地返回)
1315
+ movRR(this.em, R.rbx, R.rax);
1316
+ xorRR(this.em, R.r9, R.r9); // h = 0(只用低 32 位)
1317
+ movRDisp(this.em, R.rcx, R.rbx, 16); // 字节数
1318
+ movRR(this.em, R.rsi, R.rbx);
1319
+ addRImm(this.em, R.rsi, 串头字节);
1320
+ const hlp = this.em.nl("hs_lp");
1321
+ const hed = this.em.nl("hs_ed");
1322
+ this.em.label(hlp);
1323
+ testRR(this.em, R.rcx, R.rcx);
1324
+ jcc(this.em, JCC.z, hed);
1325
+ this.signextByte0(this.em, R.r8, R.rsi); // (signed char)byte → 32 位符号扩展
1326
+ movRR(this.em, R.rax, R.r9);
1327
+ shlRImm(this.em, R.rax, 5); // 32 位:eax 运算自动回绕
1328
+ subRR(this.em, R.rax, R.r9); // h*31
1329
+ addRR(this.em, R.rax, R.r8); // + byte(32 位回绕)
1330
+ movRR(this.em, R.r9, R.rax);
1331
+ incR(this.em, R.rsi);
1332
+ decR(this.em, R.rcx);
1333
+ jmp(this.em, hlp);
1334
+ this.em.label(hed);
1335
+ movRR(this.em, R.rax, R.r9);
1192
1336
  popR(this.em, R.rbx);
1193
1337
  ret(this.em);
1194
- }
1195
1338
 
1196
- // rt_print_int: rdi = int64
1197
- genPrintInt() {
1198
- this.em.label("rt_print_int");
1199
- pushR(this.em, R.rbp);
1200
- movRR(this.em, R.rbp, R.rsp);
1339
+ // rt_dict_new: rdi=初始容量 -> rax=字典
1340
+ this.em.label("rt_dict_new");
1201
1341
  pushR(this.em, R.rbx);
1202
1342
  pushR(this.em, R.r12);
1203
- pushR(this.em, R.r13);
1204
-
1205
- movRR(this.em, R.rbx, R.rdi); // rbx = value
1206
- subRsp(this.em, 32); // 32 字节缓冲区
1343
+ movRR(this.em, R.rbx, R.rdi);
1344
+ movRI64(this.em, R.rdi, BigInt(字典头字节));
1345
+ call(this.em, "rt_alloc");
1346
+ movRR(this.em, R.r12, R.rax);
1347
+ movRR(this.em, R.rdi, R.rbx);
1348
+ shlRImm(this.em, R.rdi, 3); // cap*8
1349
+ movRR(this.em, R.rax, R.rdi);
1350
+ shlRImm(this.em, R.rax, 1); // cap*16
1351
+ addRR(this.em, R.rdi, R.rax); // cap*24
1352
+ call(this.em, "rt_alloc");
1353
+ movDispR(this.em, R.r12, 16, R.rax); // 条目表(新内存零初始化=全空槽)
1354
+ movRI64(this.em, R.rdi, 64n); // 键序表初始 8 槽 ×8 字节
1355
+ call(this.em, "rt_alloc");
1356
+ movDispR(this.em, R.r12, 24, R.rax);
1357
+ movDispImm(this.em, R.r12, 0, 0); // 数量
1358
+ movDispR(this.em, R.r12, 8, R.rbx); // 容量
1359
+ movDispImm(this.em, R.r12, 32, 0); // 键序长度
1360
+ movDispImm(this.em, R.r12, 40, 8); // 键序容量
1361
+ movRR(this.em, R.rax, R.r12);
1362
+ popR(this.em, R.r12);
1363
+ popR(this.em, R.rbx);
1364
+ ret(this.em);
1207
1365
 
1208
- // 处理 0
1209
- testRR(this.em, R.rbx, R.rbx);
1210
- const nonzero = this.em.nl("pi_nz");
1211
- const done0 = this.em.nl("pi_d0");
1212
- jcc(this.em, JCC.ne, nonzero);
1213
- // 输出 "0"
1214
- this.em.bs([0xC6, 0x44, 0x24, 0, 0x30]); // mov byte [rsp], '0'
1215
- movRI64(this.em, R.rdi, 1n);
1216
- movRR(this.em, R.rsi, R.rsp);
1217
- movRI64(this.em, R.rdx, 1n);
1218
- movRI64(this.em, R.rax, 1n);
1219
- syscall(this.em);
1220
- jmp(this.em, done0);
1366
+ // rt_dict_get: rdi=字典, rsi=键对象 -> rax=盒指针|0
1367
+ this.em.label("rt_dict_get");
1368
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1369
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
1370
+ movRR(this.em, R.rbx, R.rdi); // 字典
1371
+ movRR(this.em, R.r12, R.rsi); // 键
1372
+ movRR(this.em, R.rsi, R.r12);
1373
+ call(this.em, "rt_hash");
1374
+ movRR(this.em, R.r13, R.rax); // h
1375
+ movRDisp(this.em, R.r14, R.rbx, 8);
1376
+ decR(this.em, R.r14); // mask = cap-1
1377
+ xorRR(this.em, R.r15, R.r15); // i
1378
+ const gpb = this.em.nl("dg_pb");
1379
+ const gnw = this.em.nl("dg_nw");
1380
+ const gfd = this.em.nl("dg_fd");
1381
+ const gen = this.em.nl("dg_en");
1382
+ this.em.label(gpb);
1383
+ // rax = (h+i)&mask; r8 = 条目地址
1384
+ movRR(this.em, R.rax, R.r13);
1385
+ addRR(this.em, R.rax, R.r15);
1386
+ andRR(this.em, R.rax, R.r14);
1387
+ movRDisp(this.em, R.r8, R.rbx, 16);
1388
+ movRR(this.em, R.r9, R.rax); shlRImm(this.em, R.r9, 3);
1389
+ movRR(this.em, R.r10, R.rax); shlRImm(this.em, R.r10, 4);
1390
+ addRR(this.em, R.r9, R.r10);
1391
+ addRR(this.em, R.r8, R.r9); // 条目 = 表 + idx*24
1392
+ movRDisp(this.em, R.rax, R.r8, 16); // 占用标志
1393
+ testRR(this.em, R.rax, R.rax);
1394
+ jcc(this.em, JCC.z, gnw);
1395
+ cmpRDisp(this.em, R.r12, R.r8, 0); // 键指针相同(字面量驻留快路)
1396
+ jcc(this.em, JCC.e, gfd);
1397
+ movRDisp(this.em, R.rdi, R.r8, 0);
1398
+ movRR(this.em, R.rsi, R.r12);
1399
+ call(this.em, "rt_str_eq");
1400
+ testRR(this.em, R.rax, R.rax);
1401
+ jcc(this.em, JCC.nz, gfd);
1402
+ incR(this.em, R.r15);
1403
+ cmpRDisp(this.em, R.r15, R.rbx, 8);
1404
+ jcc(this.em, JCC.b, gpb);
1405
+ this.em.label(gnw);
1406
+ xorRR(this.em, R.rax, R.rax);
1407
+ jmp(this.em, gen);
1408
+ this.em.label(gfd);
1409
+ movRR(this.em, R.rax, R.r13);
1410
+ addRR(this.em, R.rax, R.r15);
1411
+ andRR(this.em, R.rax, R.r14);
1412
+ movRDisp(this.em, R.r8, R.rbx, 16);
1413
+ movRR(this.em, R.r9, R.rax); shlRImm(this.em, R.r9, 3);
1414
+ movRR(this.em, R.r10, R.rax); shlRImm(this.em, R.r10, 4);
1415
+ addRR(this.em, R.r9, R.r10);
1416
+ addRR(this.em, R.r8, R.r9);
1417
+ movRDisp(this.em, R.rax, R.r8, 8); // 盒指针
1418
+ this.em.label(gen);
1419
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1420
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1421
+ ret(this.em);
1221
1422
 
1222
- this.em.label(nonzero);
1223
- // 处理负数
1224
- testRR(this.em, R.rbx, R.rbx);
1225
- const positive = this.em.nl("pi_pos");
1226
- jcc(this.em, JCC.ns, positive);
1227
- negR(this.em, R.rbx); // 绝对值
1228
- // 输出 '-'
1229
- this.em.bs([0xC6, 0x44, 0x24, 0, 0x2D]);
1230
- movRI64(this.em, R.rdi, 1n);
1231
- movRR(this.em, R.rsi, R.rsp);
1232
- movRI64(this.em, R.rdx, 1n);
1233
- movRI64(this.em, R.rax, 1n);
1234
- syscall(this.em);
1235
- this.em.label(positive);
1423
+ // rt_dict_rehash: rdi=字典 -> 条目表翻倍重排(键指针原样搬移,键序表不动)
1424
+ this.em.label("rt_dict_rehash");
1425
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1426
+ pushR(this.em, R.r14); pushR(this.em, R.r15); pushR(this.em, R.rbp);
1427
+ movRR(this.em, R.rbx, R.rdi); // 字典
1428
+ movRDisp(this.em, R.r12, R.rbx, 8); // 旧容量
1429
+ movRR(this.em, R.r13, R.r12);
1430
+ shlRImm(this.em, R.r13, 1); // 新容量
1431
+ movRDisp(this.em, R.r14, R.rbx, 16); // 旧条目表
1432
+ movRR(this.em, R.rdi, R.r13);
1433
+ shlRImm(this.em, R.rdi, 3);
1434
+ movRR(this.em, R.rax, R.rdi); shlRImm(this.em, R.rax, 1);
1435
+ addRR(this.em, R.rdi, R.rax); // 新容量*24
1436
+ call(this.em, "rt_alloc");
1437
+ movRR(this.em, R.r15, R.rax); // 新条目表(零初始化=全空)
1438
+ xorRR(this.em, R.rbp, R.rbp); // i
1439
+ const rh_lp = this.em.nl("rh_lp");
1440
+ const rh_dn = this.em.nl("rh_dn");
1441
+ const rh_rs = this.em.nl("rh_rs");
1442
+ const rh_bs = this.em.nl("rh_bs");
1443
+ const rh_nx = this.em.nl("rh_nx");
1444
+ this.em.label(rh_lp);
1445
+ cmpRR(this.em, R.rbp, R.r12);
1446
+ jcc(this.em, JCC.ge, rh_dn);
1447
+ // 旧条目 = r14 + rbp*24
1448
+ movRR(this.em, R.rax, R.rbp); shlRImm(this.em, R.rax, 3);
1449
+ movRR(this.em, R.r9, R.rbp); shlRImm(this.em, R.r9, 4);
1450
+ addRR(this.em, R.rax, R.r9);
1451
+ addRR(this.em, R.rax, R.r14);
1452
+ movRDisp(this.em, R.r9, R.rax, 16);
1453
+ testRR(this.em, R.r9, R.r9);
1454
+ jcc(this.em, JCC.z, rh_nx);
1455
+ // 旧条目键 → rt_hash(r9/r10/r11 都会被 rt_hash 吃掉,调用后按 rbp 重算旧条目地址)
1456
+ movRDisp(this.em, R.rsi, R.rax, 0);
1457
+ call(this.em, "rt_hash");
1458
+ movRR(this.em, R.r10, R.rax); // h
1459
+ movRR(this.em, R.r11, R.r13);
1460
+ decR(this.em, R.r11); // 新 mask
1461
+ movRR(this.em, R.rcx, R.r10);
1462
+ andRR(this.em, R.rcx, R.r11); // 探测起点
1463
+ // 重算旧条目地址到 r9(rt_hash 之后 rax/rcx 已被复用)
1464
+ movRR(this.em, R.rax, R.rbp); shlRImm(this.em, R.rax, 3);
1465
+ movRR(this.em, R.r8, R.rbp); shlRImm(this.em, R.r8, 4);
1466
+ addRR(this.em, R.rax, R.r8);
1467
+ addRR(this.em, R.rax, R.r14);
1468
+ movRR(this.em, R.r9, R.rax); // r9 = 旧条目
1469
+ this.em.label(rh_rs);
1470
+ movRR(this.em, R.rax, R.rcx); shlRImm(this.em, R.rax, 3);
1471
+ movRR(this.em, R.r8, R.rcx); shlRImm(this.em, R.r8, 4);
1472
+ addRR(this.em, R.rax, R.r8);
1473
+ addRR(this.em, R.rax, R.r15); // 新槽地址
1474
+ movRDisp(this.em, R.r8, R.rax, 16);
1475
+ testRR(this.em, R.r8, R.r8);
1476
+ jcc(this.em, JCC.nz, rh_bs);
1477
+ // 放入
1478
+ movRDisp(this.em, R.r8, R.r9, 0);
1479
+ movDispR(this.em, R.rax, 0, R.r8);
1480
+ movRDisp(this.em, R.r8, R.r9, 8);
1481
+ movDispR(this.em, R.rax, 8, R.r8);
1482
+ movDispImm(this.em, R.rax, 16, 1);
1483
+ jmp(this.em, rh_nx);
1484
+ this.em.label(rh_bs);
1485
+ incR(this.em, R.rcx);
1486
+ andRR(this.em, R.rcx, R.r11);
1487
+ jmp(this.em, rh_rs);
1488
+ this.em.label(rh_nx);
1489
+ incR(this.em, R.rbp);
1490
+ jmp(this.em, rh_lp);
1491
+ this.em.label(rh_dn);
1492
+ movDispR(this.em, R.rbx, 16, R.r15);
1493
+ movDispR(this.em, R.rbx, 8, R.r13);
1494
+ popR(this.em, R.rbp);
1495
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1496
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1497
+ ret(this.em);
1236
1498
 
1237
- // 转换:从 rsp+31 往前填
1238
- xorRR(this.em, R.r13, R.r13); // r13 = 位数 = 0
1239
- const convLoop = this.em.nl("pi_cl");
1240
- const convDone = this.em.nl("pi_cd");
1241
- this.em.label(convLoop);
1242
- testRR(this.em, R.rbx, R.rbx);
1243
- jcc(this.em, JCC.z, convDone);
1499
+ // rt_dict_set: rdi=字典, rsi=键对象, rdx=盒 -> 覆盖保位、新键入键序尾部
1500
+ this.em.label("rt_dict_set");
1501
+ const st_pb = this.em.nl("st_pb");
1502
+ const st_lp = this.em.nl("st_lp");
1503
+ const st_in = this.em.nl("st_in");
1504
+ const st_ow = this.em.nl("st_ow");
1505
+ const st_rp = this.em.nl("st_rp");
1506
+ const st_ed = this.em.nl("st_ed");
1507
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1508
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
1509
+ movRR(this.em, R.rbx, R.rdi);
1510
+ movRR(this.em, R.r12, R.rsi);
1511
+ movRR(this.em, R.r13, R.rdx); // 盒
1512
+ // (count+1)*4 > cap*3 → 先扩容
1513
+ movRDisp(this.em, R.rax, R.rbx, 0);
1514
+ incR(this.em, R.rax);
1515
+ shlRImm(this.em, R.rax, 2);
1516
+ movRDisp(this.em, R.r14, R.rbx, 8);
1517
+ movRR(this.em, R.r15, R.r14);
1518
+ shlRImm(this.em, R.r15, 1);
1519
+ addRR(this.em, R.r15, R.r14);
1520
+ cmpRR(this.em, R.rax, R.r15);
1521
+ jcc(this.em, JCC.be, st_pb);
1522
+ movRR(this.em, R.rdi, R.rbx);
1523
+ call(this.em, "rt_dict_rehash");
1524
+ this.em.label(st_pb);
1525
+ movRR(this.em, R.rsi, R.r12);
1526
+ call(this.em, "rt_hash");
1527
+ movRR(this.em, R.r14, R.rax); // h
1528
+ movRDisp(this.em, R.r10, R.rbx, 8);
1529
+ decR(this.em, R.r10); // mask(r10 循环内只用不改)
1530
+ xorRR(this.em, R.r15, R.r15); // i
1531
+ this.em.label(st_lp);
1532
+ movRR(this.em, R.rax, R.r14);
1533
+ addRR(this.em, R.rax, R.r15);
1534
+ andRR(this.em, R.rax, R.r10);
1535
+ movRDisp(this.em, R.r8, R.rbx, 16);
1536
+ movRR(this.em, R.r9, R.rax); shlRImm(this.em, R.r9, 3);
1537
+ movRR(this.em, R.r11, R.rax); shlRImm(this.em, R.r11, 4);
1538
+ addRR(this.em, R.r9, R.r11);
1539
+ addRR(this.em, R.r8, R.r9); // 条目
1540
+ movRDisp(this.em, R.rax, R.r8, 16);
1541
+ testRR(this.em, R.rax, R.rax);
1542
+ jcc(this.em, JCC.z, st_in);
1543
+ cmpRDisp(this.em, R.r12, R.r8, 0);
1544
+ jcc(this.em, JCC.e, st_rp);
1545
+ movRDisp(this.em, R.rdi, R.r8, 0);
1546
+ movRR(this.em, R.rsi, R.r12);
1547
+ call(this.em, "rt_str_eq");
1548
+ testRR(this.em, R.rax, R.rax);
1549
+ jcc(this.em, JCC.nz, st_rp);
1550
+ incR(this.em, R.r15);
1551
+ cmpRDisp(this.em, R.r15, R.rbx, 8);
1552
+ jcc(this.em, JCC.b, st_lp);
1553
+ // 探测一整圈没找到空槽(负载上限保证不会发生):兜底扩容重试
1554
+ movRR(this.em, R.rdi, R.rbx);
1555
+ call(this.em, "rt_dict_rehash");
1556
+ jmp(this.em, st_pb);
1557
+ this.em.label(st_rp); // 覆盖:只换盒,键序位置不动
1558
+ movDispR(this.em, R.r8, 8, R.r13);
1559
+ jmp(this.em, st_ed);
1560
+ this.em.label(st_in);
1561
+ // 键序表扩容(norder >= ordercap)
1562
+ movRDisp(this.em, R.r10, R.rbx, 32); // norder
1563
+ movRDisp(this.em, R.r11, R.rbx, 40); // ordercap
1564
+ cmpRR(this.em, R.r10, R.r11);
1565
+ jcc(this.em, JCC.b, st_ow);
1566
+ movRR(this.em, R.rdi, R.r11);
1567
+ shlRImm(this.em, R.rdi, 4); // 新容量*8
1568
+ call(this.em, "rt_alloc");
1569
+ movRR(this.em, R.r9, R.rax); // 新键序表
1570
+ movRDisp(this.em, R.rsi, R.rbx, 24);
1571
+ movRDisp(this.em, R.rcx, R.rbx, 32);
1572
+ shlRImm(this.em, R.rcx, 3); // norder*8 字节
1573
+ movRR(this.em, R.rdi, R.r9);
1574
+ cld(this.em);
1575
+ repMovsb(this.em);
1576
+ movDispR(this.em, R.rbx, 24, R.r9);
1577
+ movRDisp(this.em, R.rax, R.rbx, 40);
1578
+ shlRImm(this.em, R.rax, 1);
1579
+ movDispR(this.em, R.rbx, 40, R.rax);
1580
+ this.em.label(st_ow);
1581
+ // 追加键指针到键序表尾
1582
+ movRDisp(this.em, R.r10, R.rbx, 24);
1583
+ movRDisp(this.em, R.rax, R.rbx, 32);
1584
+ shlRImm(this.em, R.rax, 3);
1585
+ addRR(this.em, R.r10, R.rax);
1586
+ movDispR(this.em, R.r10, 0, R.r12);
1587
+ movRDisp(this.em, R.rax, R.rbx, 32);
1588
+ incR(this.em, R.rax);
1589
+ movDispR(this.em, R.rbx, 32, R.rax);
1590
+ // 写条目
1591
+ movDispR(this.em, R.r8, 0, R.r12);
1592
+ movDispR(this.em, R.r8, 8, R.r13);
1593
+ movDispImm(this.em, R.r8, 16, 1);
1594
+ movRDisp(this.em, R.rax, R.rbx, 0);
1595
+ incR(this.em, R.rax);
1596
+ movDispR(this.em, R.rbx, 0, R.rax);
1597
+ this.em.label(st_ed);
1598
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1599
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1600
+ ret(this.em);
1244
1601
 
1245
- // 必须用无符号除法:INT64_MIN 取负还是 0x8000000000000000,
1246
- // 它的无符号位型正好就是 2^63(真正的绝对值)。用 idiv 会把
1247
- // 它当负数继续除,余数为负,加 '0' 后写出一串垃圾字符。
1248
- movRR(this.em, R.rax, R.rbx);
1249
- xorRR(this.em, R.rdx, R.rdx);
1250
- movRI64(this.em, R.r10, 10n);
1251
- divR(this.em, R.r10); // rax=quot, rdx=rem(无符号)
1252
- movRR(this.em, R.rbx, R.rax); // rbx = quot
1602
+ // rt_dict_has: rdi=字典, rsi=键 -> rax=0/1
1603
+ this.em.label("rt_dict_has");
1604
+ call(this.em, "rt_dict_get");
1605
+ testRR(this.em, R.rax, R.rax);
1606
+ setccAl(this.em, SETCC.nz);
1607
+ movzbEaxAl(this.em);
1608
+ ret(this.em);
1253
1609
 
1254
- // dl += '0'
1255
- movRI64(this.em, R.rax, 48n);
1256
- addRR(this.em, R.rdx, R.rax); // rdx = rem + '0'
1610
+ // rt_dict_get_dyn: rdi=字典, rsi=键盒(须为字符串)-> rax=盒|0
1611
+ this.em.label("rt_dict_get_dyn");
1612
+ movRDisp(this.em, R.rax, R.rsi, 0);
1613
+ cmpRImm(this.em, R.rax, 标签字符串);
1614
+ jcc(this.em, JCC.ne, "gd_e");
1615
+ movRDisp(this.em, R.rsi, R.rsi, 8);
1616
+ jmp(this.em, "rt_dict_get");
1617
+ this.em.label("gd_e");
1618
+ this.loadMsg(this.strConst("运行错误:字典键必须是字符串\n"));
1619
+ jmp(this.em, "rt_fail");
1257
1620
 
1258
- // 存到 [rsp + 31 - r13]
1259
- movRR(this.em, R.rax, R.rsp);
1260
- movRI64(this.em, R.rcx, 31n);
1261
- addRR(this.em, R.rax, R.rcx);
1262
- subRR(this.em, R.rax, R.r13);
1263
- // mov [rax], dl
1264
- this.em.bs([0x88, 0x10]);
1265
- incR(this.em, R.r13);
1266
- jmp(this.em, convLoop);
1267
- this.em.label(convDone);
1621
+ // rt_dict_len: rdi=字典 -> rax=数量
1622
+ this.em.label("rt_dict_len");
1623
+ movRDisp(this.em, R.rax, R.rdi, 0);
1624
+ ret(this.em);
1268
1625
 
1269
- // 输出: 起始 = rsp + 32 - r13, 长度 = r13
1270
- // (数字存在 [rsp+31-i], i=0..r13-1,最高位在 i=r13-1 -> [rsp+32-r13])
1271
- movRR(this.em, R.rsi, R.rsp);
1272
- movRI64(this.em, R.rcx, 32n);
1273
- addRR(this.em, R.rsi, R.rcx);
1274
- subRR(this.em, R.rsi, R.r13);
1275
- movRR(this.em, R.rdx, R.r13);
1276
- movRI64(this.em, R.rdi, 1n);
1626
+ // rt_dict_del: rdi=字典, rsi=键 -> rax=1/0(向后回填 + 键序摘除)
1627
+ this.em.label("rt_dict_del");
1628
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1629
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
1630
+ movRR(this.em, R.rbx, R.rdi);
1631
+ movRR(this.em, R.r12, R.rsi);
1632
+ movRR(this.em, R.rsi, R.r12);
1633
+ call(this.em, "rt_hash");
1634
+ movRR(this.em, R.r13, R.rax); // h
1635
+ movRDisp(this.em, R.r14, R.rbx, 8);
1636
+ decR(this.em, R.r14); // mask
1637
+ xorRR(this.em, R.r15, R.r15); // i
1638
+ const dl_lp = this.em.nl("dl_lp");
1639
+ const dl_nf = this.em.nl("dl_nf");
1640
+ const dl_fd = this.em.nl("dl_fd");
1641
+ this.em.label(dl_lp);
1642
+ movRR(this.em, R.rax, R.r13);
1643
+ addRR(this.em, R.rax, R.r15);
1644
+ andRR(this.em, R.rax, R.r14);
1645
+ movRDisp(this.em, R.r8, R.rbx, 16);
1646
+ movRR(this.em, R.r9, R.rax); shlRImm(this.em, R.r9, 3);
1647
+ movRR(this.em, R.r10, R.rax); shlRImm(this.em, R.r10, 4);
1648
+ addRR(this.em, R.r9, R.r10);
1649
+ addRR(this.em, R.r8, R.r9);
1650
+ movRDisp(this.em, R.rax, R.r8, 16);
1651
+ testRR(this.em, R.rax, R.rax);
1652
+ jcc(this.em, JCC.z, dl_nf);
1653
+ cmpRDisp(this.em, R.r12, R.r8, 0);
1654
+ jcc(this.em, JCC.e, dl_fd);
1655
+ movRDisp(this.em, R.rdi, R.r8, 0);
1656
+ movRR(this.em, R.rsi, R.r12);
1657
+ call(this.em, "rt_str_eq");
1658
+ testRR(this.em, R.rax, R.rax);
1659
+ jcc(this.em, JCC.nz, dl_fd);
1660
+ incR(this.em, R.r15);
1661
+ cmpRDisp(this.em, R.r15, R.rbx, 8);
1662
+ jcc(this.em, JCC.b, dl_lp);
1663
+ this.em.label(dl_nf);
1664
+ xorRR(this.em, R.rax, R.rax);
1665
+ jmp(this.em, "dl_end");
1666
+ this.em.label(dl_fd);
1667
+ // 键序摘除:找 order[k]==[r8],前移后段、norder--
1668
+ movRDisp(this.em, R.r13, R.r8, 0); // 键指针
1669
+ xorRR(this.em, R.rcx, R.rcx);
1670
+ const dl_ol = this.em.nl("dl_ol");
1671
+ const dl_mv = this.em.nl("dl_mv");
1672
+ const dl_om = this.em.nl("dl_om");
1673
+ const dl_od = this.em.nl("dl_od");
1674
+ this.em.label(dl_ol);
1675
+ cmpRDisp(this.em, R.rcx, R.rbx, 32);
1676
+ jcc(this.em, JCC.ge, dl_od);
1677
+ movRDisp(this.em, R.r8, R.rbx, 24);
1678
+ movRR(this.em, R.r9, R.rcx); shlRImm(this.em, R.r9, 3);
1679
+ addRR(this.em, R.r8, R.r9);
1680
+ movRDisp(this.em, R.rax, R.r8, 0);
1681
+ cmpRR(this.em, R.rax, R.r13); // order[k] == 键指针?
1682
+ jcc(this.em, JCC.e, dl_mv);
1683
+ incR(this.em, R.rcx);
1684
+ jmp(this.em, dl_ol);
1685
+ this.em.label(dl_mv);
1686
+ // 把 order[k+1..] 前移一格
1687
+ movRR(this.em, R.rdi, R.r8);
1688
+ movRR(this.em, R.rsi, R.r8);
1689
+ addRImm(this.em, R.rsi, 8);
1690
+ movRDisp(this.em, R.rax, R.rbx, 32);
1691
+ subRR(this.em, R.rax, R.rcx);
1692
+ decR(this.em, R.rax); // 要搬的元素个数
1693
+ shlRImm(this.em, R.rax, 3);
1694
+ movRR(this.em, R.rcx, R.rax);
1695
+ testRR(this.em, R.rcx, R.rcx);
1696
+ jcc(this.em, JCC.z, dl_om);
1697
+ cld(this.em);
1698
+ repMovsb(this.em);
1699
+ this.em.label(dl_om);
1700
+ movRDisp(this.em, R.rax, R.rbx, 32);
1701
+ decR(this.em, R.rax);
1702
+ movDispR(this.em, R.rbx, 32, R.rax);
1703
+ this.em.label(dl_od);
1704
+ // 表内向后回填(与宿主同算法):r15=hole 序号,r14=mask 保持不动
1705
+ const dl_bl = this.em.nl("dl_bl");
1706
+ const dl_be = this.em.nl("dl_be");
1707
+ const dl_nx2 = this.em.nl("dl_nx");
1708
+ movRR(this.em, R.r13, R.rbx); // 字典
1709
+ movRI64(this.em, R.r12, 1n); // 步长 i=1
1710
+ this.em.label(dl_bl);
1711
+ movRR(this.em, R.rax, R.r15);
1712
+ addRR(this.em, R.rax, R.r12);
1713
+ andRR(this.em, R.rax, R.r14);
1714
+ movRDisp(this.em, R.r8, R.r13, 16);
1715
+ movRR(this.em, R.r9, R.rax); shlRImm(this.em, R.r9, 3);
1716
+ movRR(this.em, R.r10, R.rax); shlRImm(this.em, R.r10, 4);
1717
+ addRR(this.em, R.r9, R.r10);
1718
+ addRR(this.em, R.r8, R.r9); // 后继条目
1719
+ movRDisp(this.em, R.rax, R.r8, 16);
1720
+ testRR(this.em, R.rax, R.rax);
1721
+ jcc(this.em, JCC.z, dl_be);
1722
+ // ideal = hash(key)&mask; dist_cur=(idx-ideal)&mask; dist_hole=(hole-ideal)&mask
1723
+ movRDisp(this.em, R.rsi, R.r8, 0);
1724
+ call(this.em, "rt_hash");
1725
+ andRR(this.em, R.rax, R.r14); // ideal
1726
+ movRR(this.em, R.r9, R.rax);
1727
+ movRR(this.em, R.rax, R.r12);
1728
+ addRR(this.em, R.rax, R.r15); // idx = hole+i
1729
+ andRR(this.em, R.rax, R.r14);
1730
+ subRR(this.em, R.rax, R.r9);
1731
+ andRR(this.em, R.rax, R.r14); // dist_cur
1732
+ movRR(this.em, R.r10, R.rax);
1733
+ movRR(this.em, R.rax, R.r15);
1734
+ subRR(this.em, R.rax, R.r9);
1735
+ andRR(this.em, R.rax, R.r14); // dist_hole
1736
+ cmpRR(this.em, R.rax, R.r10);
1737
+ jcc(this.em, JCC.a, dl_nx2); // dist_hole > dist_cur:不搬
1738
+ // 搬:hole ← idx
1739
+ movRR(this.em, R.rax, R.r12);
1740
+ addRR(this.em, R.rax, R.r15);
1741
+ andRR(this.em, R.rax, R.r14); // idx(搬完后 hole 就是它)
1742
+ movRR(this.em, R.r11, R.rax); // r11 = idx
1743
+ movRDisp(this.em, R.r8, R.r13, 16);
1744
+ movRR(this.em, R.r9, R.r15); shlRImm(this.em, R.r9, 3);
1745
+ movRR(this.em, R.r10, R.r15); shlRImm(this.em, R.r10, 4);
1746
+ addRR(this.em, R.r9, R.r10);
1747
+ addRR(this.em, R.r8, R.r9); // hole 条目
1748
+ movRDisp(this.em, R.rdx, R.r13, 16);
1749
+ movRR(this.em, R.rcx, R.r11); shlRImm(this.em, R.rcx, 3);
1750
+ movRR(this.em, R.rdi, R.r11); shlRImm(this.em, R.rdi, 4);
1751
+ addRR(this.em, R.rcx, R.rdi);
1752
+ addRR(this.em, R.rdx, R.rcx); // src 条目
1753
+ movRDisp(this.em, R.rax, R.rdx, 0);
1754
+ movDispR(this.em, R.r8, 0, R.rax);
1755
+ movRDisp(this.em, R.rax, R.rdx, 8);
1756
+ movDispR(this.em, R.r8, 8, R.rax);
1757
+ movDispImm(this.em, R.r8, 16, 1);
1758
+ movDispImm(this.em, R.rdx, 16, 0);
1759
+ movRR(this.em, R.r15, R.r11); // hole = idx
1760
+ this.em.label(dl_nx2);
1761
+ incR(this.em, R.r12);
1762
+ cmpRR(this.em, R.r12, R.r14); // i <= mask(最多走 cap-1 步)
1763
+ jcc(this.em, JCC.be, dl_bl);
1764
+ this.em.label(dl_be);
1765
+ // 清 hole
1766
+ movRDisp(this.em, R.r8, R.r13, 16);
1767
+ movRR(this.em, R.r9, R.r15); shlRImm(this.em, R.r9, 3);
1768
+ movRR(this.em, R.r10, R.r15); shlRImm(this.em, R.r10, 4);
1769
+ addRR(this.em, R.r9, R.r10);
1770
+ addRR(this.em, R.r8, R.r9);
1771
+ movDispImm(this.em, R.r8, 16, 0);
1772
+ movRDisp(this.em, R.rax, R.r13, 0);
1773
+ decR(this.em, R.rax);
1774
+ movDispR(this.em, R.r13, 0, R.rax);
1277
1775
  movRI64(this.em, R.rax, 1n);
1278
- syscall(this.em);
1776
+ this.em.label("dl_end");
1777
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1778
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1779
+ ret(this.em);
1279
1780
 
1280
- this.em.label(done0);
1281
- this.finishPrintInt();
1282
- }
1781
+ // rt_dict_keys: rdi=字典 -> rax=字符串列表(插入顺序)
1782
+ this.em.label("rt_dict_keys");
1783
+ const k8_ok = this.em.nl("dk_k8");
1784
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1785
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
1786
+ movRR(this.em, R.rbx, R.rdi);
1787
+ movRDisp(this.em, R.rdi, R.rbx, 0);
1788
+ cmpRImm(this.em, R.rdi, 8);
1789
+ jcc(this.em, JCC.ge, k8_ok);
1790
+ movRI64(this.em, R.rdi, 8n);
1791
+ this.em.label(k8_ok);
1792
+ call(this.em, "rt_list_new");
1793
+ movRR(this.em, R.r12, R.rax);
1794
+ xorRR(this.em, R.r13, R.r13);
1795
+ const kl = this.em.nl("dk_lp");
1796
+ const kd = this.em.nl("dk_dn");
1797
+ this.em.label(kl);
1798
+ cmpRDisp(this.em, R.r13, R.rbx, 32);
1799
+ jcc(this.em, JCC.ge, kd);
1800
+ movRDisp(this.em, R.r14, R.rbx, 24);
1801
+ movRR(this.em, R.r15, R.r13); shlRImm(this.em, R.r15, 3);
1802
+ addRR(this.em, R.r14, R.r15);
1803
+ movRR(this.em, R.rdi, R.r12);
1804
+ movRI64(this.em, R.rsi, BigInt(标签字符串));
1805
+ movRDisp(this.em, R.rdx, R.r14, 0);
1806
+ call(this.em, "rt_list_append");
1807
+ incR(this.em, R.r13);
1808
+ jmp(this.em, kl);
1809
+ this.em.label(kd);
1810
+ movRR(this.em, R.rax, R.r12);
1811
+ popR(this.em, R.r15); popR(this.em, R.r14);
1812
+ popR(this.em, R.r13); popR(this.em, R.r12); popR(this.em, R.rbx);
1813
+ ret(this.em);
1283
1814
 
1284
- finishPrintInt() {
1285
- addRsp(this.em, 32);
1286
- popR(this.em, R.r13);
1287
- popR(this.em, R.r12);
1288
- popR(this.em, R.rbx);
1289
- popR(this.em, R.rbp);
1815
+ // rt_dict_values: rdi=字典 -> rax=动态值列表(插入顺序)
1816
+ this.em.label("rt_dict_values");
1817
+ const v8_ok = this.em.nl("dv_v8");
1818
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1819
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
1820
+ movRR(this.em, R.rbx, R.rdi);
1821
+ movRDisp(this.em, R.rdi, R.rbx, 0);
1822
+ cmpRImm(this.em, R.rdi, 8);
1823
+ jcc(this.em, JCC.ge, v8_ok);
1824
+ movRI64(this.em, R.rdi, 8n);
1825
+ this.em.label(v8_ok);
1826
+ call(this.em, "rt_list_new");
1827
+ movRR(this.em, R.r12, R.rax);
1828
+ xorRR(this.em, R.r13, R.r13);
1829
+ const vl = this.em.nl("dv_lp");
1830
+ const vd = this.em.nl("dv_dn");
1831
+ this.em.label(vl);
1832
+ cmpRDisp(this.em, R.r13, R.rbx, 32);
1833
+ jcc(this.em, JCC.ge, vd);
1834
+ movRDisp(this.em, R.r14, R.rbx, 24);
1835
+ movRR(this.em, R.r15, R.r13); shlRImm(this.em, R.r15, 3);
1836
+ addRR(this.em, R.r14, R.r15);
1837
+ movRR(this.em, R.rdi, R.rbx);
1838
+ movRDisp(this.em, R.rsi, R.r14, 0);
1839
+ call(this.em, "rt_dict_get"); // rax=盒
1840
+ movRR(this.em, R.rdi, R.r12);
1841
+ movRDisp(this.em, R.rsi, R.rax, 0);
1842
+ movRDisp(this.em, R.rdx, R.rax, 8);
1843
+ call(this.em, "rt_list_append");
1844
+ incR(this.em, R.r13);
1845
+ jmp(this.em, vl);
1846
+ this.em.label(vd);
1847
+ movRR(this.em, R.rax, R.r12);
1848
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1849
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1290
1850
  ret(this.em);
1291
- }
1292
1851
 
1293
- genPrintNl() {
1294
- this.em.label("rt_print_nl");
1295
- pushR(this.em, R.rax);
1296
- pushR(this.em, R.rdi);
1297
- pushR(this.em, R.rsi);
1298
- pushR(this.em, R.rdx);
1299
- pushR(this.em, R.rcx);
1300
- subRsp(this.em, 16);
1301
- this.em.bs([0xC6, 0x04, 0x24, 0x0A]); // mov byte [rsp], '\n'
1302
- movRI64(this.em, R.rdi, 1n);
1303
- movRR(this.em, R.rsi, R.rsp);
1304
- movRI64(this.em, R.rdx, 1n);
1305
- movRI64(this.em, R.rax, 1n);
1306
- syscall(this.em);
1307
- addRsp(this.em, 16);
1308
- popR(this.em, R.rcx);
1309
- popR(this.em, R.rdx);
1310
- popR(this.em, R.rsi);
1311
- popR(this.em, R.rdi);
1312
- popR(this.em, R.rax);
1852
+ // rt_dict_update: rdi=目标, rsi=源 —— 源键序逐个写入目标(覆盖保位)
1853
+ this.em.label("rt_dict_update");
1854
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1855
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
1856
+ movRR(this.em, R.rbx, R.rdi);
1857
+ movRR(this.em, R.r12, R.rsi);
1858
+ xorRR(this.em, R.r13, R.r13);
1859
+ const ul = this.em.nl("du_lp");
1860
+ const ud = this.em.nl("du_dn");
1861
+ this.em.label(ul);
1862
+ cmpRDisp(this.em, R.r13, R.r12, 32);
1863
+ jcc(this.em, JCC.ge, ud);
1864
+ movRDisp(this.em, R.r14, R.r12, 24);
1865
+ movRR(this.em, R.r15, R.r13); shlRImm(this.em, R.r15, 3);
1866
+ addRR(this.em, R.r14, R.r15);
1867
+ movRR(this.em, R.rdi, R.r12);
1868
+ movRDisp(this.em, R.rsi, R.r14, 0);
1869
+ call(this.em, "rt_dict_get"); // 盒
1870
+ movRR(this.em, R.r15, R.rax);
1871
+ movRR(this.em, R.rdi, R.rbx);
1872
+ movRDisp(this.em, R.rsi, R.r14, 0);
1873
+ movRR(this.em, R.rdx, R.r15);
1874
+ call(this.em, "rt_dict_set");
1875
+ incR(this.em, R.r13);
1876
+ jmp(this.em, ul);
1877
+ this.em.label(ud);
1878
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1879
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1313
1880
  ret(this.em);
1314
- }
1315
1881
 
1316
- genPrintSpace() {
1882
+ // rt_dict_eq: rdi=a, rsi=b -> rax=0/1(键集与对应值相等,顺序无关)
1883
+ this.em.label("rt_dict_eq");
1884
+ const ql = this.em.nl("de_lp");
1885
+ const qd = this.em.nl("de_dn");
1886
+ const q0 = this.em.nl("de_q0");
1887
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1888
+ pushR(this.em, R.r14); pushR(this.em, R.r15); pushR(this.em, R.rbp);
1889
+ movRR(this.em, R.rbx, R.rdi);
1890
+ movRR(this.em, R.r12, R.rsi);
1891
+ movRDisp(this.em, R.rax, R.rbx, 0);
1892
+ cmpRDisp(this.em, R.rax, R.r12, 0);
1893
+ jcc(this.em, JCC.ne, q0);
1894
+ xorRR(this.em, R.r13, R.r13);
1895
+ this.em.label(ql);
1896
+ cmpRDisp(this.em, R.r13, R.rbx, 32);
1897
+ jcc(this.em, JCC.ge, qd);
1898
+ movRDisp(this.em, R.r14, R.rbx, 24);
1899
+ movRR(this.em, R.r15, R.r13); shlRImm(this.em, R.r15, 3);
1900
+ addRR(this.em, R.r14, R.r15); // r14 = 键序槽地址
1901
+ movRR(this.em, R.rdi, R.rbx);
1902
+ movRDisp(this.em, R.rsi, R.r14, 0);
1903
+ call(this.em, "rt_dict_get");
1904
+ movRR(this.em, R.rbp, R.rax); // a 盒(callee-saved,跨第二次查找保留)
1905
+ movRR(this.em, R.rdi, R.r12);
1906
+ movRDisp(this.em, R.rsi, R.r14, 0);
1907
+ call(this.em, "rt_dict_get");
1908
+ testRR(this.em, R.rax, R.rax);
1909
+ jcc(this.em, JCC.z, q0);
1910
+ movRR(this.em, R.rsi, R.rax); // b 盒
1911
+ movRR(this.em, R.rdi, R.rbp); // a 盒
1912
+ call(this.em, "rt_dyn_eq");
1913
+ testRR(this.em, R.rax, R.rax);
1914
+ jcc(this.em, JCC.z, q0);
1915
+ incR(this.em, R.r13);
1916
+ jmp(this.em, ql);
1917
+ this.em.label(qd);
1918
+ movRI64(this.em, R.rax, 1n);
1919
+ jmp(this.em, "de_end");
1920
+ this.em.label(q0);
1921
+ xorRR(this.em, R.rax, R.rax);
1922
+ this.em.label("de_end");
1923
+ popR(this.em, R.rbp);
1924
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1925
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1926
+ ret(this.em);
1927
+
1928
+ // rt_dict_tostr: rdi=字典 -> rax=串对象({"k": v, ...},插入顺序,宿主格式)
1929
+ this.em.label("rt_dict_tostr");
1930
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
1931
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
1932
+ movRR(this.em, R.rbx, R.rdi);
1933
+ this.loadStrRax(this.strConst("{"));
1934
+ movRR(this.em, R.r12, R.rax); // 累积串
1935
+ xorRR(this.em, R.r13, R.r13);
1936
+ const tl = this.em.nl("dt_lp");
1937
+ const td = this.em.nl("dt_dn");
1938
+ const tsep = this.em.nl("dt_sp");
1939
+ const tkey = this.em.nl("dt_tk");
1940
+ this.em.label(tl);
1941
+ cmpRDisp(this.em, R.r13, R.rbx, 32);
1942
+ jcc(this.em, JCC.ge, td);
1943
+ testRR(this.em, R.r13, R.r13);
1944
+ jcc(this.em, JCC.nz, tsep);
1945
+ jmp(this.em, tkey);
1946
+ this.em.label(tsep);
1947
+ movRR(this.em, R.rdi, R.r12);
1948
+ this.loadStrRax(this.strConst(", "));
1949
+ movRR(this.em, R.rsi, R.rax);
1950
+ call(this.em, "rt_concat");
1951
+ movRR(this.em, R.r12, R.rax);
1952
+ this.em.label(tkey);
1953
+ // 键:先取键指针
1954
+ movRDisp(this.em, R.r14, R.rbx, 24);
1955
+ movRR(this.em, R.r15, R.r13); shlRImm(this.em, R.r15, 3);
1956
+ addRR(this.em, R.r14, R.r15);
1957
+ // "\"" + key + "\": "
1958
+ movRR(this.em, R.rdi, R.r12);
1959
+ this.loadStrRax(this.strConst("\""));
1960
+ movRR(this.em, R.rsi, R.rax);
1961
+ call(this.em, "rt_concat");
1962
+ movRR(this.em, R.r12, R.rax);
1963
+ movRR(this.em, R.rdi, R.r12);
1964
+ movRDisp(this.em, R.rsi, R.r14, 0);
1965
+ call(this.em, "rt_concat");
1966
+ movRR(this.em, R.r12, R.rax);
1967
+ movRR(this.em, R.rdi, R.r12);
1968
+ this.loadStrRax(this.strConst("\": "));
1969
+ movRR(this.em, R.rsi, R.rax);
1970
+ call(this.em, "rt_concat");
1971
+ movRR(this.em, R.r12, R.rax);
1972
+ // 值
1973
+ movRR(this.em, R.rdi, R.rbx);
1974
+ movRDisp(this.em, R.rsi, R.r14, 0);
1975
+ call(this.em, "rt_dict_get");
1976
+ movRR(this.em, R.rdi, R.rax);
1977
+ call(this.em, "rt_dyn_tostr");
1978
+ movRR(this.em, R.rsi, R.rax);
1979
+ movRR(this.em, R.rdi, R.r12);
1980
+ call(this.em, "rt_concat");
1981
+ movRR(this.em, R.r12, R.rax);
1982
+ incR(this.em, R.r13);
1983
+ jmp(this.em, tl);
1984
+ this.em.label(td);
1985
+ movRR(this.em, R.rdi, R.r12);
1986
+ this.loadStrRax(this.strConst("}"));
1987
+ movRR(this.em, R.rsi, R.rax);
1988
+ call(this.em, "rt_concat");
1989
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
1990
+ popR(this.em, R.r12); popR(this.em, R.rbx);
1991
+ ret(this.em);
1992
+
1993
+ // rt_print_dict: rdi=字典
1994
+ this.em.label("rt_print_dict");
1995
+ call(this.em, "rt_dict_tostr");
1996
+ movRR(this.em, R.rdi, R.rax);
1997
+ jmp(this.em, "rt_print_str");
1998
+
1999
+ // rt_print_list: rdi=表
2000
+ this.em.label("rt_print_list");
2001
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2002
+ movRR(this.em, R.rbx, R.rdi);
2003
+ this.loadMsg(this.strConst("["));
2004
+ call(this.em, "rt_print_str");
2005
+ xorRR(this.em, R.r13, R.r13);
2006
+ const pl = this.em.nl("pl_lp");
2007
+ const pd = this.em.nl("pl_dn");
2008
+ const psep = this.em.nl("pl_sp");
2009
+ const ple = this.em.nl("pl_le");
2010
+ this.em.label(pl);
2011
+ cmpRDisp(this.em, R.r13, R.rbx, 0);
2012
+ jcc(this.em, JCC.ge, pd);
2013
+ testRR(this.em, R.r13, R.r13);
2014
+ jcc(this.em, JCC.nz, psep);
2015
+ jmp(this.em, ple);
2016
+ this.em.label(psep);
2017
+ this.loadMsg(this.strConst(", "));
2018
+ call(this.em, "rt_print_str");
2019
+ this.em.label(ple);
2020
+ movRDisp(this.em, R.r12, R.rbx, 16);
2021
+ movRR(this.em, R.rax, R.r13); shlRImm(this.em, R.rax, 4);
2022
+ addRR(this.em, R.r12, R.rax);
2023
+ movRDisp(this.em, R.rsi, R.r12, 0);
2024
+ movRDisp(this.em, R.rdx, R.r12, 8);
2025
+ call(this.em, "rt_print_tagged");
2026
+ incR(this.em, R.r13);
2027
+ jmp(this.em, pl);
2028
+ this.em.label(pd);
2029
+ this.loadMsg(this.strConst("]"));
2030
+ call(this.em, "rt_print_str");
2031
+ popR(this.em, R.r13); popR(this.em, R.r12); popR(this.em, R.rbx);
2032
+ ret(this.em);
2033
+
2034
+ // rt_list_tostr: rdi=表 -> rax=串对象
2035
+ this.em.label("rt_list_tostr");
2036
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2037
+ pushR(this.em, R.r14);
2038
+ movRR(this.em, R.rbx, R.rdi);
2039
+ this.loadStrRax(this.strConst("["));
2040
+ movRR(this.em, R.r12, R.rax);
2041
+ xorRR(this.em, R.r13, R.r13);
2042
+ const ll = this.em.nl("lt_lp");
2043
+ const ld = this.em.nl("lt_dn");
2044
+ const lsep = this.em.nl("lt_sp");
2045
+ const lle = this.em.nl("lt_le");
2046
+ this.em.label(ll);
2047
+ cmpRDisp(this.em, R.r13, R.rbx, 0);
2048
+ jcc(this.em, JCC.ge, ld);
2049
+ testRR(this.em, R.r13, R.r13);
2050
+ jcc(this.em, JCC.nz, lsep);
2051
+ jmp(this.em, lle);
2052
+ this.em.label(lsep);
2053
+ movRR(this.em, R.rdi, R.r12);
2054
+ this.loadStrRax(this.strConst(", "));
2055
+ movRR(this.em, R.rsi, R.rax);
2056
+ call(this.em, "rt_concat");
2057
+ movRR(this.em, R.r12, R.rax);
2058
+ this.em.label(lle);
2059
+ movRDisp(this.em, R.r14, R.rbx, 16);
2060
+ movRR(this.em, R.rax, R.r13); shlRImm(this.em, R.rax, 4);
2061
+ addRR(this.em, R.r14, R.rax);
2062
+ movRDisp(this.em, R.rsi, R.r14, 0);
2063
+ movRDisp(this.em, R.rdx, R.r14, 8);
2064
+ call(this.em, "rt_tostr_tagged");
2065
+ movRR(this.em, R.rsi, R.rax);
2066
+ movRR(this.em, R.rdi, R.r12);
2067
+ call(this.em, "rt_concat");
2068
+ movRR(this.em, R.r12, R.rax);
2069
+ incR(this.em, R.r13);
2070
+ jmp(this.em, ll);
2071
+ this.em.label(ld);
2072
+ movRR(this.em, R.rdi, R.r12);
2073
+ this.loadStrRax(this.strConst("]"));
2074
+ movRR(this.em, R.rsi, R.rax);
2075
+ call(this.em, "rt_concat");
2076
+ popR(this.em, R.r14); popR(this.em, R.r13); popR(this.em, R.r12); popR(this.em, R.rbx);
2077
+ ret(this.em);
2078
+
2079
+ // rt_list_eq: rdi=a, rsi=b -> rax=0/1(逐槽结构比较,宿主 val_equals 语义)
2080
+ this.em.label("rt_list_eq");
2081
+ const le0 = this.em.nl("le_0");
2082
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2083
+ pushR(this.em, R.r14); pushR(this.em, R.r15); pushR(this.em, R.rbp);
2084
+ movRR(this.em, R.rbx, R.rdi);
2085
+ movRR(this.em, R.r12, R.rsi);
2086
+ movRDisp(this.em, R.rax, R.rbx, 0);
2087
+ cmpRDisp(this.em, R.rax, R.r12, 0);
2088
+ jcc(this.em, JCC.ne, le0);
2089
+ xorRR(this.em, R.r13, R.r13);
2090
+ const lel = this.em.nl("le_lp");
2091
+ const lenx = this.em.nl("le_nx");
2092
+ const led = this.em.nl("le_dn");
2093
+ const lestr = this.em.nl("le_st");
2094
+ const lef = this.em.nl("le_ft");
2095
+ const lec = this.em.nl("le_ck");
2096
+ this.em.label(lel);
2097
+ movRDisp(this.em, R.r14, R.rbx, 0);
2098
+ cmpRR(this.em, R.r13, R.r14);
2099
+ jcc(this.em, JCC.ge, led);
2100
+ movRDisp(this.em, R.r14, R.rbx, 16);
2101
+ movRR(this.em, R.rax, R.r13); shlRImm(this.em, R.rax, 4);
2102
+ addRR(this.em, R.r14, R.rax); // 槽A
2103
+ movRDisp(this.em, R.r15, R.r12, 16);
2104
+ addRR(this.em, R.r15, R.rax); // 槽B
2105
+ movRDisp(this.em, R.rbp, R.r14, 0); // 标签A(callee-saved,跨调用保留)
2106
+ cmpRDisp(this.em, R.rbp, R.r15, 0);
2107
+ jcc(this.em, JCC.ne, le0);
2108
+ movRDisp(this.em, R.rdi, R.r14, 8);
2109
+ movRDisp(this.em, R.rsi, R.r15, 8);
2110
+ cmpRR(this.em, R.rdi, R.rsi);
2111
+ jcc(this.em, JCC.e, lenx); // 指针/数值相同(整数、布尔、共享对象)
2112
+ cmpRImm(this.em, R.rbp, 标签字符串);
2113
+ jcc(this.em, JCC.ne, lestr);
2114
+ call(this.em, "rt_str_eq");
2115
+ jmp(this.em, lec);
2116
+ this.em.label(lestr);
2117
+ cmpRImm(this.em, R.rbp, 标签列表);
2118
+ jcc(this.em, JCC.ne, lef);
2119
+ call(this.em, "rt_list_eq");
2120
+ jmp(this.em, lec);
2121
+ this.em.label(lef);
2122
+ cmpRImm(this.em, R.rbp, 标签字典);
2123
+ jcc(this.em, JCC.ne, le0);
2124
+ call(this.em, "rt_dict_eq");
2125
+ this.em.label(lec);
2126
+ testRR(this.em, R.rax, R.rax);
2127
+ jcc(this.em, JCC.z, le0);
2128
+ this.em.label(lenx);
2129
+ incR(this.em, R.r13);
2130
+ jmp(this.em, lel);
2131
+ this.em.label(led);
2132
+ movRI64(this.em, R.rax, 1n);
2133
+ jmp(this.em, "le_end");
2134
+ this.em.label(le0);
2135
+ xorRR(this.em, R.rax, R.rax);
2136
+ this.em.label("le_end");
2137
+ popR(this.em, R.rbp);
2138
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
2139
+ popR(this.em, R.r12); popR(this.em, R.rbx);
2140
+ ret(this.em);
2141
+
2142
+ // rt_tostr_tagged: rsi=标签, rdx=载荷 -> rax=串对象
2143
+ this.em.label("rt_tostr_tagged");
2144
+ const t0 = this.em.nl("ts0"), t1 = this.em.nl("ts1"), t2 = this.em.nl("ts2");
2145
+ const t3 = this.em.nl("ts3"), t4 = this.em.nl("ts4"), t5 = this.em.nl("ts5");
2146
+ cmpRImm(this.em, R.rsi, 标签空);
2147
+ jcc(this.em, JCC.e, t0);
2148
+ cmpRImm(this.em, R.rsi, 标签整数);
2149
+ jcc(this.em, JCC.e, t1);
2150
+ cmpRImm(this.em, R.rsi, 标签布尔);
2151
+ jcc(this.em, JCC.e, t2);
2152
+ cmpRImm(this.em, R.rsi, 标签字符串);
2153
+ jcc(this.em, JCC.e, t3);
2154
+ cmpRImm(this.em, R.rsi, 标签列表);
2155
+ jcc(this.em, JCC.e, t4);
2156
+ cmpRImm(this.em, R.rsi, 标签字典);
2157
+ jcc(this.em, JCC.e, "t4b");
2158
+ cmpRImm(this.em, R.rsi, 标签函数);
2159
+ jcc(this.em, JCC.e, t5);
2160
+ this.em.label(t0);
2161
+ this.loadStrRax(this.strConst("空"));
2162
+ ret(this.em);
2163
+ this.em.label(t1);
2164
+ movRR(this.em, R.rdi, R.rdx);
2165
+ jmp(this.em, "rt_int_to_str");
2166
+ this.em.label(t2);
2167
+ movRR(this.em, R.rdi, R.rdx);
2168
+ jmp(this.em, "rt_bool_to_str");
2169
+ this.em.label(t3);
2170
+ movRR(this.em, R.rax, R.rdx);
2171
+ ret(this.em);
2172
+ this.em.label(t4);
2173
+ movRR(this.em, R.rdi, R.rdx);
2174
+ jmp(this.em, "rt_list_tostr");
2175
+ this.em.label("t4b");
2176
+ movRR(this.em, R.rdi, R.rdx);
2177
+ jmp(this.em, "rt_dict_tostr");
2178
+ this.em.label(t5);
2179
+ // 函数:<函数 名字> / <函数 匿名>(名字槽 [obj+32],匿名=0)
2180
+ pushR(this.em, R.rbx); pushR(this.em, R.r12);
2181
+ movRR(this.em, R.r12, R.rdx);
2182
+ this.loadStrRax(this.strConst("<函数 "));
2183
+ movRR(this.em, R.rbx, R.rax);
2184
+ movRDisp(this.em, R.rax, R.r12, 32);
2185
+ testRR(this.em, R.rax, R.rax);
2186
+ jcc(this.em, JCC.nz, "ts5n");
2187
+ movRR(this.em, R.rsi, R.rax);
2188
+ this.loadStrRax(this.strConst("匿名>"));
2189
+ movRR(this.em, R.rdi, R.rbx);
2190
+ call(this.em, "rt_concat");
2191
+ popR(this.em, R.r12); popR(this.em, R.rbx);
2192
+ ret(this.em);
2193
+ this.em.label("ts5n");
2194
+ movRR(this.em, R.rsi, R.rax);
2195
+ this.loadStrRax(this.strConst(">"));
2196
+ movRR(this.em, R.rdi, R.rbx);
2197
+ call(this.em, "rt_concat");
2198
+ popR(this.em, R.r12); popR(this.em, R.rbx);
2199
+ ret(this.em);
2200
+
2201
+ // rt_print_tagged: rsi=标签, rdx=载荷
2202
+ this.em.label("rt_print_tagged");
2203
+ call(this.em, "rt_tostr_tagged");
2204
+ movRR(this.em, R.rdi, R.rax);
2205
+ jmp(this.em, "rt_print_str");
2206
+
2207
+ // rt_dyn_print / rt_dyn_tostr: rdi=盒
2208
+ this.em.label("rt_dyn_print");
2209
+ movRDisp(this.em, R.rsi, R.rdi, 0);
2210
+ movRDisp(this.em, R.rdx, R.rdi, 8);
2211
+ jmp(this.em, "rt_print_tagged");
2212
+ this.em.label("rt_dyn_tostr");
2213
+ movRDisp(this.em, R.rsi, R.rdi, 0);
2214
+ movRDisp(this.em, R.rdx, R.rdi, 8);
2215
+ jmp(this.em, "rt_tostr_tagged");
2216
+
2217
+ // rt_dyn_truthy: rdi=盒 -> rax=0/1(宿主真值规则)
2218
+ this.em.label("rt_dyn_truthy");
2219
+ movRDisp(this.em, R.rsi, R.rdi, 0);
2220
+ movRDisp(this.em, R.rdx, R.rdi, 8);
2221
+ const y0 = this.em.nl("ty0"), y1 = this.em.nl("ty1"), y2 = this.em.nl("ty2");
2222
+ const y3 = this.em.nl("ty3"), y4 = this.em.nl("ty4"), y5 = this.em.nl("ty5");
2223
+ cmpRImm(this.em, R.rsi, 标签空);
2224
+ jcc(this.em, JCC.e, y0);
2225
+ cmpRImm(this.em, R.rsi, 标签整数);
2226
+ jcc(this.em, JCC.e, y1);
2227
+ cmpRImm(this.em, R.rsi, 标签布尔);
2228
+ jcc(this.em, JCC.e, y1);
2229
+ cmpRImm(this.em, R.rsi, 标签字符串);
2230
+ jcc(this.em, JCC.e, y2);
2231
+ cmpRImm(this.em, R.rsi, 标签列表);
2232
+ jcc(this.em, JCC.e, y3);
2233
+ cmpRImm(this.em, R.rsi, 标签字典);
2234
+ jcc(this.em, JCC.e, y3);
2235
+ jmp(this.em, y5); // 函数恒真(宿主 default: 1)
2236
+ this.em.label(y0);
2237
+ xorRR(this.em, R.rax, R.rax);
2238
+ ret(this.em);
2239
+ this.em.label(y1);
2240
+ testRR(this.em, R.rdx, R.rdx);
2241
+ setccAl(this.em, SETCC.nz);
2242
+ movzbEaxAl(this.em);
2243
+ ret(this.em);
2244
+ this.em.label(y2);
2245
+ movRDisp(this.em, R.rax, R.rdx, 16); // 字节数 != 0
2246
+ testRR(this.em, R.rax, R.rax);
2247
+ setccAl(this.em, SETCC.nz);
2248
+ movzbEaxAl(this.em);
2249
+ ret(this.em);
2250
+ this.em.label(y3);
2251
+ movRDisp(this.em, R.rax, R.rdx, 0); // 长度/数量 != 0
2252
+ testRR(this.em, R.rax, R.rax);
2253
+ setccAl(this.em, SETCC.nz);
2254
+ movzbEaxAl(this.em);
2255
+ ret(this.em);
2256
+ this.em.label(y5);
2257
+ movRI64(this.em, R.rax, 1n);
2258
+ ret(this.em);
2259
+
2260
+ // rt_div_zero: rdi=0(整除)/1(取模) —— 构造宿主同文案的可捕获异常后 rt_throw。
2261
+ // 载荷 = {"类型": "运行时错误", "消息": "..."};行/列缺省 0,
2262
+ // 无人捕获时走「运行时错误: 消息」简式(带坐标文案是已知待办,见 README)。
2263
+ this.em.label("rt_div_zero");
2264
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2265
+ movRR(this.em, R.rbx, R.rdi);
2266
+ movRI64(this.em, R.rdi, 16n);
2267
+ call(this.em, "rt_dict_new");
2268
+ movRR(this.em, R.r12, R.rax);
2269
+ // 类型 = 运行时错误
2270
+ this.loadStrRax(this.strConst("类型"));
2271
+ movRR(this.em, R.r13, R.rax);
2272
+ this.loadStrRax(this.strConst("运行时错误"));
2273
+ movRR(this.em, R.rsi, R.rax);
2274
+ movRI64(this.em, R.rax, BigInt(标签字符串));
2275
+ movRR(this.em, R.rdi, R.rax);
2276
+ call(this.em, "rt_box");
2277
+ movRR(this.em, R.rdi, R.r12);
2278
+ movRR(this.em, R.rsi, R.r13);
2279
+ movRR(this.em, R.rdx, R.rax);
2280
+ call(this.em, "rt_dict_set");
2281
+ // 消息 = 整除的除数为零 / 取模的除数为零
2282
+ this.loadStrRax(this.strConst("消息"));
2283
+ movRR(this.em, R.r13, R.rax);
2284
+ const m2 = this.em.nl("dz_m2"), m3 = this.em.nl("dz_m3");
2285
+ testRR(this.em, R.rbx, R.rbx);
2286
+ jcc(this.em, JCC.nz, m2);
2287
+ this.loadStrRax(this.strConst("整除的除数为零"));
2288
+ jmp(this.em, m3);
2289
+ this.em.label(m2);
2290
+ this.loadStrRax(this.strConst("取模的除数为零"));
2291
+ this.em.label(m3);
2292
+ movRR(this.em, R.rsi, R.rax);
2293
+ movRI64(this.em, R.rax, BigInt(标签字符串));
2294
+ movRR(this.em, R.rdi, R.rax);
2295
+ call(this.em, "rt_box");
2296
+ movRR(this.em, R.rdi, R.r12);
2297
+ movRR(this.em, R.rsi, R.r13);
2298
+ movRR(this.em, R.rdx, R.rax);
2299
+ call(this.em, "rt_dict_set");
2300
+ // 装箱字典并抛出
2301
+ movRR(this.em, R.rax, R.r12);
2302
+ movRI64(this.em, R.rsi, BigInt(标签字典));
2303
+ movRR(this.em, R.rdi, R.rsi);
2304
+ movRR(this.em, R.rsi, R.rax);
2305
+ call(this.em, "rt_box");
2306
+ movRR(this.em, R.rdi, R.rax); // rt_throw 契约:rdi=盒
2307
+ popR(this.em, R.r13); popR(this.em, R.r12); popR(this.em, R.rbx);
2308
+ jmp(this.em, "rt_throw");
2309
+
2310
+ // rt_truthy_obj: rdi=对象(字符串/列表/字典静态种类的条件真值)
2311
+ this.em.label("rt_truthy_obj");
2312
+ movRDisp(this.em, R.rax, R.rdi, 16); // 字符串按 [16] 字节数
2313
+ testRR(this.em, R.rax, R.rax);
2314
+ setccAl(this.em, SETCC.nz);
2315
+ movzbEaxAl(this.em);
2316
+ ret(this.em);
2317
+ this.em.label("rt_truthy_coll");
2318
+ movRDisp(this.em, R.rax, R.rdi, 0);
2319
+ testRR(this.em, R.rax, R.rax);
2320
+ setccAl(this.em, SETCC.nz);
2321
+ movzbEaxAl(this.em);
2322
+ ret(this.em);
2323
+
2324
+ // rt_dyn_type: rdi=盒 -> rax=类型名串对象(宿主 类型() 文案)
2325
+ this.em.label("rt_dyn_type");
2326
+ movRDisp(this.em, R.rsi, R.rdi, 0);
2327
+ const n0 = this.em.nl("tt0"), n1 = this.em.nl("tt1"), n2 = this.em.nl("tt2");
2328
+ const n3 = this.em.nl("tt3"), n4 = this.em.nl("tt4"), n5 = this.em.nl("tt5");
2329
+ const n6 = this.em.nl("tt6");
2330
+ cmpRImm(this.em, R.rsi, 标签空); jcc(this.em, JCC.e, n0);
2331
+ cmpRImm(this.em, R.rsi, 标签整数); jcc(this.em, JCC.e, n1);
2332
+ cmpRImm(this.em, R.rsi, 标签布尔); jcc(this.em, JCC.e, n2);
2333
+ cmpRImm(this.em, R.rsi, 标签字符串); jcc(this.em, JCC.e, n3);
2334
+ cmpRImm(this.em, R.rsi, 标签列表); jcc(this.em, JCC.e, n4);
2335
+ cmpRImm(this.em, R.rsi, 标签函数); jcc(this.em, JCC.e, n5);
2336
+ jmp(this.em, n6);
2337
+ this.em.label(n0); this.loadStrRax(this.strConst("空")); ret(this.em);
2338
+ this.em.label(n1); this.loadStrRax(this.strConst("整数")); ret(this.em);
2339
+ this.em.label(n2); this.loadStrRax(this.strConst("布尔")); ret(this.em);
2340
+ this.em.label(n3); this.loadStrRax(this.strConst("字符串")); ret(this.em);
2341
+ this.em.label(n4); this.loadStrRax(this.strConst("列表")); ret(this.em);
2342
+ this.em.label(n5); this.loadStrRax(this.strConst("函数")); ret(this.em);
2343
+ this.em.label(n6); this.loadStrRax(this.strConst("字典")); ret(this.em);
2344
+
2345
+ // rt_dyn_eq: rdi=a盒, rsi=b盒 -> rax=0/1(宿主 val_equals:数值跨类型可比、容器结构比较)
2346
+ this.em.label("rt_dyn_eq");
2347
+ movRDisp(this.em, R.r8, R.rdi, 0);
2348
+ movRDisp(this.em, R.r9, R.rsi, 0);
2349
+ movRDisp(this.em, R.r10, R.rdi, 8);
2350
+ movRDisp(this.em, R.r11, R.rsi, 8);
2351
+ const e1 = this.em.nl("eq1"), e0 = this.em.nl("eq0"), e2 = this.em.nl("eq2");
2352
+ cmpRR(this.em, R.r8, R.r9);
2353
+ jcc(this.em, JCC.e, e1);
2354
+ // 异标签:整数×布尔 数值相等;其余一律假(宿主字符串×布尔恒假)
2355
+ cmpRImm(this.em, R.r8, 标签整数); jcc(this.em, JCC.e, "eqx1");
2356
+ cmpRImm(this.em, R.r8, 标签布尔); jcc(this.em, JCC.ne, e0);
2357
+ this.em.label("eqx1");
2358
+ cmpRImm(this.em, R.r9, 标签整数); jcc(this.em, JCC.e, "eqx2");
2359
+ cmpRImm(this.em, R.r9, 标签布尔); jcc(this.em, JCC.ne, e0);
2360
+ this.em.label("eqx2");
2361
+ cmpRR(this.em, R.r10, R.r11);
2362
+ jcc(this.em, JCC.e, e2);
2363
+ jmp(this.em, e0);
2364
+ this.em.label(e1);
2365
+ cmpRImm(this.em, R.r8, 标签字符串); jcc(this.em, JCC.e, "eqs");
2366
+ cmpRImm(this.em, R.r8, 标签列表); jcc(this.em, JCC.e, "eql");
2367
+ cmpRImm(this.em, R.r8, 标签字典); jcc(this.em, JCC.e, "eqd");
2368
+ cmpRR(this.em, R.r10, R.r11); // 整数/布尔/空/函数:位相同即等
2369
+ jcc(this.em, JCC.e, e2);
2370
+ jmp(this.em, e0);
2371
+ this.em.label("eqs");
2372
+ movRR(this.em, R.rdi, R.r10);
2373
+ movRR(this.em, R.rsi, R.r11);
2374
+ call(this.em, "rt_str_eq");
2375
+ ret(this.em);
2376
+ this.em.label("eql");
2377
+ movRR(this.em, R.rdi, R.r10);
2378
+ movRR(this.em, R.rsi, R.r11);
2379
+ call(this.em, "rt_list_eq");
2380
+ ret(this.em);
2381
+ this.em.label("eqd");
2382
+ movRR(this.em, R.rdi, R.r10);
2383
+ movRR(this.em, R.rsi, R.r11);
2384
+ call(this.em, "rt_dict_eq");
2385
+ ret(this.em);
2386
+ this.em.label(e2);
2387
+ movRI64(this.em, R.rax, 1n);
2388
+ ret(this.em);
2389
+ this.em.label(e0);
2390
+ xorRR(this.em, R.rax, R.rax);
2391
+ ret(this.em);
2392
+
2393
+ // rt_seq_len: rdi=首字节 -> rax=序列长度(非法字节按 1,与宿主一致)
2394
+ this.em.label("rt_seq_len");
2395
+ movRI64(this.em, R.rax, 1n);
2396
+ const s1 = this.em.nl("sl1"), s2 = this.em.nl("sl2"), s3 = this.em.nl("sl3"), s4 = this.em.nl("sl4");
2397
+ cmpRImm(this.em, R.rdi, 0x80);
2398
+ jcc(this.em, JCC.b, s4);
2399
+ movRR(this.em, R.r9, R.rdi); andRImm(this.em, R.r9, 0xE0);
2400
+ cmpRImm(this.em, R.r9, 0xC0); jcc(this.em, JCC.ne, s1);
2401
+ movRI64(this.em, R.rax, 2n); jmp(this.em, s4);
2402
+ this.em.label(s1);
2403
+ movRR(this.em, R.r9, R.rdi); andRImm(this.em, R.r9, 0xF0);
2404
+ cmpRImm(this.em, R.r9, 0xE0); jcc(this.em, JCC.ne, s2);
2405
+ movRI64(this.em, R.rax, 3n); jmp(this.em, s4);
2406
+ this.em.label(s2);
2407
+ movRR(this.em, R.r9, R.rdi); andRImm(this.em, R.r9, 0xF8);
2408
+ cmpRImm(this.em, R.r9, 0xF0); jcc(this.em, JCC.ne, s4);
2409
+ movRI64(this.em, R.rax, 4n);
2410
+ this.em.label(s3);
2411
+ this.em.label(s4);
2412
+ ret(this.em);
2413
+
2414
+ // rt_str_at: rdi=串对象, rsi=字符下标(支持负)-> rax=新串对象;越界 fatal
2415
+ this.em.label("rt_str_at");
2416
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2417
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
2418
+ movRR(this.em, R.rbx, R.rdi);
2419
+ movRR(this.em, R.r12, R.rsi);
2420
+ movRR(this.em, R.rdi, R.rbx);
2421
+ call(this.em, "rt_flatten");
2422
+ movRR(this.em, R.rbx, R.rax);
2423
+ testRR(this.em, R.r12, R.r12);
2424
+ jcc(this.em, JCC.ns, "sa_ok");
2425
+ movRDisp(this.em, R.rax, R.rbx, 8);
2426
+ addRR(this.em, R.r12, R.rax);
2427
+ this.em.label("sa_ok");
2428
+ xorRR(this.em, R.r13, R.r13); // 已数到的字符
2429
+ movRR(this.em, R.r14, R.rbx);
2430
+ addRImm(this.em, R.r14, 串头字节); // 游标
2431
+ const sal = this.em.nl("sa_lp");
2432
+ const saf = this.em.nl("sa_f");
2433
+ this.em.label(sal);
2434
+ cmpRR(this.em, R.r13, R.r12);
2435
+ jcc(this.em, JCC.ge, "sa_hit");
2436
+ this.loadByte0(this.em, R.rdi, R.r14);
2437
+ testRR(this.em, R.rdi, R.rdi);
2438
+ jcc(this.em, JCC.z, saf); // 走到 NUL 还没数够 → 越界
2439
+ call(this.em, "rt_seq_len"); // rax = n
2440
+ addRR(this.em, R.r14, R.rax);
2441
+ incR(this.em, R.r13);
2442
+ jmp(this.em, sal);
2443
+ this.em.label("sa_hit");
2444
+ this.loadByte0(this.em, R.rdi, R.r14);
2445
+ testRR(this.em, R.rdi, R.rdi);
2446
+ jcc(this.em, JCC.z, saf);
2447
+ call(this.em, "rt_seq_len");
2448
+ movRR(this.em, R.r15, R.rax); // 本字符字节数
2449
+ movRR(this.em, R.rdi, R.r15);
2450
+ addRImm(this.em, R.rdi, 串头字节 + 1);
2451
+ call(this.em, "rt_alloc");
2452
+ movRR(this.em, R.r12, R.rax);
2453
+ xorRR(this.em, R.rdx, R.rdx);
2454
+ movDispR(this.em, R.r12, 0, R.rdx); // 扁平
2455
+ movDispImm(this.em, R.r12, 8, 1); // 1 字符
2456
+ movDispR(this.em, R.r12, 16, R.r15); // 字节数
2457
+ movRR(this.em, R.rdi, R.r12);
2458
+ addRImm(this.em, R.rdi, 串头字节);
2459
+ movRR(this.em, R.rsi, R.r14);
2460
+ movRR(this.em, R.rcx, R.r15);
2461
+ cld(this.em);
2462
+ repMovsb(this.em);
2463
+ movRR(this.em, R.rax, R.r12);
2464
+ movRDisp(this.em, R.rdx, R.r12, 16);
2465
+ addRR(this.em, R.rdx, R.r12);
2466
+ addRImm(this.em, R.rdx, 串头字节);
2467
+ movByteAt(this.em, R.rdx, 0); // NUL 尾
2468
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
2469
+ popR(this.em, R.r12); popR(this.em, R.rbx);
2470
+ ret(this.em);
2471
+ this.em.label(saf);
2472
+ this.loadMsg(this.strConst("运行错误:字符串索引越界\n"));
2473
+ jmp(this.em, "rt_fail");
2474
+
2475
+ // rt_dyn_index: rdi=容器盒, rsi=键盒 -> rax=值盒(字典/列表/字符串按标签分派)
2476
+ this.em.label("rt_dyn_index");
2477
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2478
+ movRR(this.em, R.rbx, R.rdi); // 容器盒
2479
+ movRR(this.em, R.r12, R.rsi); // 键盒
2480
+ movRDisp(this.em, R.r13, R.rbx, 0); // 容器标签
2481
+ const di6 = this.em.nl("di6"), di4 = this.em.nl("di4"), di3 = this.em.nl("di3");
2482
+ cmpRImm(this.em, R.r13, 标签字典); jcc(this.em, JCC.e, di6);
2483
+ cmpRImm(this.em, R.r13, 标签列表); jcc(this.em, JCC.e, di4);
2484
+ cmpRImm(this.em, R.r13, 标签字符串); jcc(this.em, JCC.e, di3);
2485
+ this.loadMsg(this.strConst("运行错误:不支持的类型索引\n"));
2486
+ jmp(this.em, "rt_fail");
2487
+ this.em.label(di6);
2488
+ movRDisp(this.em, R.rax, R.r12, 0);
2489
+ cmpRImm(this.em, R.rax, 标签字符串);
2490
+ jcc(this.em, JCC.ne, "die_k");
2491
+ movRDisp(this.em, R.rdi, R.rbx, 8);
2492
+ movRDisp(this.em, R.rsi, R.r12, 8);
2493
+ call(this.em, "rt_dict_get");
2494
+ testRR(this.em, R.rax, R.rax);
2495
+ jcc(this.em, JCC.nz, "din_r");
2496
+ xorRR(this.em, R.rdi, R.rdi); // 缺键 → 空盒(宿主:字典缺键返回 空)
2497
+ xorRR(this.em, R.rsi, R.rsi);
2498
+ call(this.em, "rt_box");
2499
+ this.em.label("din_r");
2500
+ popR(this.em, R.r13); popR(this.em, R.r12); popR(this.em, R.rbx);
2501
+ ret(this.em);
2502
+ this.em.label("die_k");
2503
+ this.loadMsg(this.strConst("运行错误:字典键必须是字符串\n"));
2504
+ jmp(this.em, "rt_fail");
2505
+ this.em.label(di4);
2506
+ movRDisp(this.em, R.rax, R.r12, 0);
2507
+ cmpRImm(this.em, R.rax, 标签整数); jcc(this.em, JCC.e, "dio");
2508
+ cmpRImm(this.em, R.rax, 标签布尔); jcc(this.em, JCC.ne, "die_i");
2509
+ this.em.label("dio");
2510
+ movRDisp(this.em, R.rdi, R.rbx, 8);
2511
+ movRDisp(this.em, R.rsi, R.r12, 8);
2512
+ call(this.em, "rt_list_get_ptr");
2513
+ movRDisp(this.em, R.rdi, R.rax, 0);
2514
+ movRDisp(this.em, R.rsi, R.rax, 8);
2515
+ call(this.em, "rt_box");
2516
+ popR(this.em, R.r13); popR(this.em, R.r12); popR(this.em, R.rbx);
2517
+ ret(this.em);
2518
+ this.em.label("die_i");
2519
+ this.loadMsg(this.strConst("运行错误:下标必须是数字\n"));
2520
+ jmp(this.em, "rt_fail");
2521
+ this.em.label(di3);
2522
+ movRDisp(this.em, R.rax, R.r12, 0);
2523
+ cmpRImm(this.em, R.rax, 标签整数); jcc(this.em, JCC.e, "dsc");
2524
+ cmpRImm(this.em, R.rax, 标签布尔); jcc(this.em, JCC.ne, "die_i");
2525
+ this.em.label("dsc");
2526
+ movRDisp(this.em, R.rdi, R.rbx, 8);
2527
+ movRDisp(this.em, R.rsi, R.r12, 8);
2528
+ call(this.em, "rt_str_at");
2529
+ movRR(this.em, R.rsi, R.rax);
2530
+ movRI64(this.em, R.rdi, BigInt(标签字符串));
2531
+ call(this.em, "rt_box");
2532
+ popR(this.em, R.r13); popR(this.em, R.r12); popR(this.em, R.rbx);
2533
+ ret(this.em);
2534
+
2535
+ // rt_dyn_setindex: rdi=容器盒, rsi=键盒, rdx=值盒
2536
+ this.em.label("rt_dyn_setindex");
2537
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2538
+ pushR(this.em, R.r14); pushR(this.em, R.r15);
2539
+ movRR(this.em, R.rbx, R.rdi);
2540
+ movRR(this.em, R.r12, R.rsi);
2541
+ movRR(this.em, R.r13, R.rdx);
2542
+ movRDisp(this.em, R.r14, R.rbx, 0);
2543
+ cmpRImm(this.em, R.r14, 标签字典); jcc(this.em, JCC.ne, "dsi_l");
2544
+ movRDisp(this.em, R.rax, R.r12, 0);
2545
+ cmpRImm(this.em, R.rax, 标签字符串); jcc(this.em, JCC.ne, "die_k2");
2546
+ movRDisp(this.em, R.rdi, R.rbx, 8);
2547
+ movRDisp(this.em, R.rsi, R.r12, 8);
2548
+ movRR(this.em, R.rdx, R.r13);
2549
+ call(this.em, "rt_dict_set");
2550
+ jmp(this.em, "dsi_e");
2551
+ this.em.label("die_k2");
2552
+ this.loadMsg(this.strConst("运行错误:字典键必须是字符串\n"));
2553
+ jmp(this.em, "rt_fail");
2554
+ this.em.label("dsi_l");
2555
+ cmpRImm(this.em, R.r14, 标签列表); jcc(this.em, JCC.ne, "dsi_x");
2556
+ movRDisp(this.em, R.rax, R.r12, 0);
2557
+ cmpRImm(this.em, R.rax, 标签整数); jcc(this.em, JCC.e, "dsi_i");
2558
+ cmpRImm(this.em, R.rax, 标签布尔); jcc(this.em, JCC.ne, "die_i2");
2559
+ this.em.label("dsi_i");
2560
+ movRDisp(this.em, R.rdi, R.rbx, 8);
2561
+ movRDisp(this.em, R.rsi, R.r12, 8);
2562
+ call(this.em, "rt_list_set_ptr");
2563
+ movRR(this.em, R.r15, R.rax);
2564
+ movRDisp(this.em, R.rax, R.r13, 0);
2565
+ movDispR(this.em, R.r15, 0, R.rax);
2566
+ movRDisp(this.em, R.rax, R.r13, 8);
2567
+ movDispR(this.em, R.r15, 8, R.rax);
2568
+ jmp(this.em, "dsi_e");
2569
+ this.em.label("die_i2");
2570
+ this.loadMsg(this.strConst("运行错误:下标必须是数字\n"));
2571
+ jmp(this.em, "rt_fail");
2572
+ this.em.label("dsi_x");
2573
+ this.loadMsg(this.strConst("运行错误:只能对列表或字典进行索引赋值\n"));
2574
+ jmp(this.em, "rt_fail");
2575
+ this.em.label("dsi_e");
2576
+ popR(this.em, R.r15); popR(this.em, R.r14); popR(this.em, R.r13);
2577
+ popR(this.em, R.r12); popR(this.em, R.rbx);
2578
+ ret(this.em);
2579
+
2580
+ // rt_dyn_call: rdi=函数盒;实参已按 System V 放好 -> rax=返回值盒
2581
+ this.em.label("rt_dyn_call");
2582
+ movRDisp(this.em, R.rax, R.rdi, 0);
2583
+ cmpRImm(this.em, R.rax, 标签函数);
2584
+ jcc(this.em, JCC.ne, "dc_e");
2585
+ pushR(this.em, R.rbx);
2586
+ movRDisp(this.em, R.rbx, R.rdi, 8); // 函数对象
2587
+ movRDisp(this.em, R.r11, R.rbx, 8); // 代码地址
2588
+ callR(this.em, R.r11);
2589
+ movRDisp(this.em, R.rdi, R.rbx, 24); // 返回标签
2590
+ movRR(this.em, R.rsi, R.rax);
2591
+ popR(this.em, R.rbx);
2592
+ jmp(this.em, "rt_box");
2593
+ this.em.label("dc_e");
2594
+ this.loadMsg(this.strConst("运行错误:值不是一个函数\n"));
2595
+ jmp(this.em, "rt_fail");
2596
+
2597
+ // rt_unwind_frame: rdi=rbp —— 函数正常返回前弹出挂在本帧上的处理器
2598
+ this.em.label("rt_unwind_frame");
2599
+ const uw_l = this.em.nl("uw_l");
2600
+ const uw_d = this.em.nl("uw_d");
2601
+ this.em.label(uw_l);
2602
+ movRAbs(this.em, R.rax, HANDLER_TOP);
2603
+ testRR(this.em, R.rax, R.rax);
2604
+ jcc(this.em, JCC.z, uw_d);
2605
+ cmpRDisp(this.em, R.rdi, R.rax, 16);
2606
+ jcc(this.em, JCC.ne, uw_d);
2607
+ movRDisp(this.em, R.r10, R.rax, 0);
2608
+ movAbsR(this.em, HANDLER_TOP, R.r10);
2609
+ jmp(this.em, uw_l);
2610
+ this.em.label(uw_d);
2611
+ ret(this.em);
2612
+
2613
+ // rt_throw: rdi=异常盒 —— 展开到最近处理器;无人捕获按宿主文案 stderr+exit1
2614
+ this.em.label("rt_throw");
2615
+ movRAbs(this.em, R.rax, HANDLER_TOP);
2616
+ testRR(this.em, R.rax, R.rax);
2617
+ jcc(this.em, JCC.z, "th_f");
2618
+ movAbsR(this.em, EXC_SLOT, R.rdi); // 载荷入全局
2619
+ movRDisp(this.em, R.r10, R.rax, 0);
2620
+ movAbsR(this.em, HANDLER_TOP, R.r10); // 弹栈
2621
+ movRDisp(this.em, R.rsp, R.rax, 24);
2622
+ movRDisp(this.em, R.rbp, R.rax, 16);
2623
+ movRDisp(this.em, R.r11, R.rax, 8);
2624
+ jmpR(this.em, R.r11);
2625
+ this.em.label("th_f");
2626
+ // 无人捕获:kind (第L行,C列): msg\n 或 kind: msg\n,退 1(宿主 die_uncaught)
2627
+ pushR(this.em, R.rbx); pushR(this.em, R.r12); pushR(this.em, R.r13);
2628
+ pushR(this.em, R.r14); pushR(this.em, R.r15); pushR(this.em, R.rbp);
2629
+ movRR(this.em, R.rbx, R.rdi); // 异常盒
2630
+ movRDisp(this.em, R.rax, R.rbx, 0);
2631
+ cmpRImm(this.em, R.rax, 标签字典);
2632
+ jcc(this.em, JCC.ne, "th_plain");
2633
+ movRDisp(this.em, R.r13, R.rbx, 8); // 字典载荷(全程保留)
2634
+ // kind 盒(缺 → 兜底「运行时错误」)
2635
+ movRR(this.em, R.rdi, R.r13);
2636
+ this.loadStrRax(this.strConst("类型"));
2637
+ movRR(this.em, R.rsi, R.rax);
2638
+ call(this.em, "rt_dict_get");
2639
+ testRR(this.em, R.rax, R.rax);
2640
+ jcc(this.em, JCC.nz, "th_k1");
2641
+ movRR(this.em, R.rdi, R.rax);
2642
+ xorRR(this.em, R.rsi, R.rsi);
2643
+ call(this.em, "rt_box");
2644
+ this.em.label("th_k1");
2645
+ movRR(this.em, R.r12, R.rax); // kind 盒
2646
+ // msg 盒(缺 → 兜底「未知错误」)
2647
+ movRR(this.em, R.rdi, R.r13);
2648
+ this.loadStrRax(this.strConst("消息"));
2649
+ movRR(this.em, R.rsi, R.rax);
2650
+ call(this.em, "rt_dict_get");
2651
+ testRR(this.em, R.rax, R.rax);
2652
+ jcc(this.em, JCC.nz, "th_m1");
2653
+ xorRR(this.em, R.rdi, R.rdi);
2654
+ movRR(this.em, R.rsi, R.rax);
2655
+ call(this.em, "rt_box");
2656
+ this.em.label("th_m1");
2657
+ movRR(this.em, R.r14, R.rax); // msg 盒
2658
+ // 行盒 / 列盒(缺 → 空盒,payload=0 走 simple 文案)
2659
+ movRR(this.em, R.rdi, R.r13);
2660
+ this.loadStrRax(this.strConst("行"));
2661
+ movRR(this.em, R.rsi, R.rax);
2662
+ call(this.em, "rt_dict_get");
2663
+ testRR(this.em, R.rax, R.rax);
2664
+ jcc(this.em, JCC.nz, "th_l1");
2665
+ xorRR(this.em, R.rdi, R.rdi);
2666
+ xorRR(this.em, R.rsi, R.rsi);
2667
+ call(this.em, "rt_box");
2668
+ this.em.label("th_l1");
2669
+ movRDisp(this.em, R.r15, R.rax, 8); // 行数值
2670
+ movRR(this.em, R.rdi, R.r13);
2671
+ this.loadStrRax(this.strConst("列"));
2672
+ movRR(this.em, R.rsi, R.rax);
2673
+ call(this.em, "rt_dict_get");
2674
+ testRR(this.em, R.rax, R.rax);
2675
+ jcc(this.em, JCC.nz, "th_c1");
2676
+ xorRR(this.em, R.rdi, R.rdi);
2677
+ xorRR(this.em, R.rsi, R.rsi);
2678
+ call(this.em, "rt_box");
2679
+ this.em.label("th_c1");
2680
+ movRDisp(this.em, R.rbp, R.rax, 8); // 列数值
2681
+ // kind 文本 / msg 文本
2682
+ movRR(this.em, R.rdi, R.r12);
2683
+ call(this.em, "rt_dyn_tostr");
2684
+ movRR(this.em, R.r12, R.rax);
2685
+ movRR(this.em, R.rdi, R.r14);
2686
+ call(this.em, "rt_dyn_tostr");
2687
+ movRR(this.em, R.r14, R.rax);
2688
+ // 组装
2689
+ testRR(this.em, R.r15, R.r15);
2690
+ jcc(this.em, JCC.z, "th_simple");
2691
+ movRR(this.em, R.rdi, R.r12);
2692
+ this.loadStrRax(this.strConst(" (第"));
2693
+ movRR(this.em, R.rsi, R.rax);
2694
+ call(this.em, "rt_concat");
2695
+ movRR(this.em, R.rdi, R.rax);
2696
+ movRR(this.em, R.rsi, R.r15);
2697
+ call(this.em, "rt_int_to_str");
2698
+ movRR(this.em, R.rsi, R.rax);
2699
+ call(this.em, "rt_concat");
2700
+ movRR(this.em, R.rdi, R.rax);
2701
+ this.loadStrRax(this.strConst("行,"));
2702
+ movRR(this.em, R.rsi, R.rax);
2703
+ call(this.em, "rt_concat");
2704
+ movRR(this.em, R.rdi, R.rax);
2705
+ movRR(this.em, R.rsi, R.rbp);
2706
+ call(this.em, "rt_int_to_str");
2707
+ movRR(this.em, R.rsi, R.rax);
2708
+ call(this.em, "rt_concat");
2709
+ movRR(this.em, R.rdi, R.rax);
2710
+ this.loadStrRax(this.strConst("列): "));
2711
+ movRR(this.em, R.rsi, R.rax);
2712
+ call(this.em, "rt_concat");
2713
+ movRR(this.em, R.rdi, R.rax);
2714
+ movRR(this.em, R.rsi, R.r14);
2715
+ call(this.em, "rt_concat");
2716
+ jmp(this.em, "th_out");
2717
+ this.em.label("th_simple");
2718
+ movRR(this.em, R.rdi, R.r12);
2719
+ this.loadStrRax(this.strConst(": "));
2720
+ movRR(this.em, R.rsi, R.rax);
2721
+ call(this.em, "rt_concat");
2722
+ movRR(this.em, R.rdi, R.rax);
2723
+ movRR(this.em, R.rsi, R.r14);
2724
+ call(this.em, "rt_concat");
2725
+ this.em.label("th_out");
2726
+ movRR(this.em, R.r12, R.rax); // 组装好的消息
2727
+ this.loadStrRax(this.strConst("\n"));
2728
+ movRR(this.em, R.rsi, R.rax);
2729
+ movRR(this.em, R.rdi, R.r12);
2730
+ call(this.em, "rt_concat");
2731
+ movRR(this.em, R.rdi, R.rax);
2732
+ call(this.em, "rt_flatten");
2733
+ movRR(this.em, R.rsi, R.rax);
2734
+ addRImm(this.em, R.rsi, 串头字节);
2735
+ movRDisp(this.em, R.rdx, R.rax, 16);
2736
+ movRI64(this.em, R.rdi, 2n);
2737
+ movRI64(this.em, R.rax, 1n);
2738
+ syscall(this.em);
2739
+ movRI64(this.em, R.rdi, 1n); // 宿主 die_uncaught 退 1
2740
+ movRI64(this.em, R.rax, 60n);
2741
+ syscall(this.em);
2742
+ this.em.label("th_plain");
2743
+ // 载荷不是字典:直接转文本输出
2744
+ movRR(this.em, R.rdi, R.rbx);
2745
+ call(this.em, "rt_dyn_tostr");
2746
+ movRR(this.em, R.rdi, R.rax);
2747
+ call(this.em, "rt_flatten");
2748
+ movRR(this.em, R.rsi, R.rax);
2749
+ addRImm(this.em, R.rsi, 串头字节);
2750
+ movRDisp(this.em, R.rdx, R.rax, 16);
2751
+ movRI64(this.em, R.rdi, 2n);
2752
+ movRI64(this.em, R.rax, 1n);
2753
+ syscall(this.em);
2754
+ movRI64(this.em, R.rdi, 1n);
2755
+ movRI64(this.em, R.rax, 60n);
2756
+ syscall(this.em);
2757
+ }
2758
+ // 再按 [16] 的字节数定长写出——不再 strlen 扫描,所以头部 24 字节
2759
+ // 的对象前缀不会被当成数据打出去。
2760
+ genPrintStr() {
2761
+ this.em.label("rt_print_str");
2762
+ pushR(this.em, R.rbx);
2763
+ movRR(this.em, R.rbx, R.rdi);
2764
+ call(this.em, "rt_flatten");
2765
+ movRDisp(this.em, R.rdx, R.rax, 16); // 字节数
2766
+ movRR(this.em, R.rsi, R.rax);
2767
+ addRImm(this.em, R.rsi, 串头字节); // 数据起点
2768
+ movRI64(this.em, R.rdi, 1n);
2769
+ movRI64(this.em, R.rax, 1n);
2770
+ syscall(this.em);
2771
+ popR(this.em, R.rbx);
2772
+ ret(this.em);
2773
+ }
2774
+
2775
+ // rt_print_int: rdi = int64
2776
+ genPrintInt() {
2777
+ this.em.label("rt_print_int");
2778
+ pushR(this.em, R.rbp);
2779
+ movRR(this.em, R.rbp, R.rsp);
2780
+ pushR(this.em, R.rbx);
2781
+ pushR(this.em, R.r12);
2782
+ pushR(this.em, R.r13);
2783
+
2784
+ movRR(this.em, R.rbx, R.rdi); // rbx = value
2785
+ subRsp(this.em, 32); // 32 字节缓冲区
2786
+
2787
+ // 处理 0
2788
+ testRR(this.em, R.rbx, R.rbx);
2789
+ const nonzero = this.em.nl("pi_nz");
2790
+ const done0 = this.em.nl("pi_d0");
2791
+ jcc(this.em, JCC.ne, nonzero);
2792
+ // 输出 "0"
2793
+ this.em.bs([0xC6, 0x44, 0x24, 0, 0x30]); // mov byte [rsp], '0'
2794
+ movRI64(this.em, R.rdi, 1n);
2795
+ movRR(this.em, R.rsi, R.rsp);
2796
+ movRI64(this.em, R.rdx, 1n);
2797
+ movRI64(this.em, R.rax, 1n);
2798
+ syscall(this.em);
2799
+ jmp(this.em, done0);
2800
+
2801
+ this.em.label(nonzero);
2802
+ // 处理负数
2803
+ testRR(this.em, R.rbx, R.rbx);
2804
+ const positive = this.em.nl("pi_pos");
2805
+ jcc(this.em, JCC.ns, positive);
2806
+ negR(this.em, R.rbx); // 绝对值
2807
+ // 输出 '-'
2808
+ this.em.bs([0xC6, 0x44, 0x24, 0, 0x2D]);
2809
+ movRI64(this.em, R.rdi, 1n);
2810
+ movRR(this.em, R.rsi, R.rsp);
2811
+ movRI64(this.em, R.rdx, 1n);
2812
+ movRI64(this.em, R.rax, 1n);
2813
+ syscall(this.em);
2814
+ this.em.label(positive);
2815
+
2816
+ // 转换:从 rsp+31 往前填
2817
+ xorRR(this.em, R.r13, R.r13); // r13 = 位数 = 0
2818
+ const convLoop = this.em.nl("pi_cl");
2819
+ const convDone = this.em.nl("pi_cd");
2820
+ this.em.label(convLoop);
2821
+ testRR(this.em, R.rbx, R.rbx);
2822
+ jcc(this.em, JCC.z, convDone);
2823
+
2824
+ // 必须用无符号除法:INT64_MIN 取负还是 0x8000000000000000,
2825
+ // 它的无符号位型正好就是 2^63(真正的绝对值)。用 idiv 会把
2826
+ // 它当负数继续除,余数为负,加 '0' 后写出一串垃圾字符。
2827
+ movRR(this.em, R.rax, R.rbx);
2828
+ xorRR(this.em, R.rdx, R.rdx);
2829
+ movRI64(this.em, R.r10, 10n);
2830
+ divR(this.em, R.r10); // rax=quot, rdx=rem(无符号)
2831
+ movRR(this.em, R.rbx, R.rax); // rbx = quot
2832
+
2833
+ // dl += '0'
2834
+ movRI64(this.em, R.rax, 48n);
2835
+ addRR(this.em, R.rdx, R.rax); // rdx = rem + '0'
2836
+
2837
+ // 存到 [rsp + 31 - r13]
2838
+ movRR(this.em, R.rax, R.rsp);
2839
+ movRI64(this.em, R.rcx, 31n);
2840
+ addRR(this.em, R.rax, R.rcx);
2841
+ subRR(this.em, R.rax, R.r13);
2842
+ // mov [rax], dl
2843
+ this.em.bs([0x88, 0x10]);
2844
+ incR(this.em, R.r13);
2845
+ jmp(this.em, convLoop);
2846
+ this.em.label(convDone);
2847
+
2848
+ // 输出: 起始 = rsp + 32 - r13, 长度 = r13
2849
+ // (数字存在 [rsp+31-i], i=0..r13-1,最高位在 i=r13-1 -> [rsp+32-r13])
2850
+ movRR(this.em, R.rsi, R.rsp);
2851
+ movRI64(this.em, R.rcx, 32n);
2852
+ addRR(this.em, R.rsi, R.rcx);
2853
+ subRR(this.em, R.rsi, R.r13);
2854
+ movRR(this.em, R.rdx, R.r13);
2855
+ movRI64(this.em, R.rdi, 1n);
2856
+ movRI64(this.em, R.rax, 1n);
2857
+ syscall(this.em);
2858
+
2859
+ this.em.label(done0);
2860
+ this.finishPrintInt();
2861
+ }
2862
+
2863
+ finishPrintInt() {
2864
+ addRsp(this.em, 32);
2865
+ popR(this.em, R.r13);
2866
+ popR(this.em, R.r12);
2867
+ popR(this.em, R.rbx);
2868
+ popR(this.em, R.rbp);
2869
+ ret(this.em);
2870
+ }
2871
+
2872
+ genPrintNl() {
2873
+ this.em.label("rt_print_nl");
2874
+ pushR(this.em, R.rax);
2875
+ pushR(this.em, R.rdi);
2876
+ pushR(this.em, R.rsi);
2877
+ pushR(this.em, R.rdx);
2878
+ pushR(this.em, R.rcx);
2879
+ subRsp(this.em, 16);
2880
+ this.em.bs([0xC6, 0x04, 0x24, 0x0A]); // mov byte [rsp], '\n'
2881
+ movRI64(this.em, R.rdi, 1n);
2882
+ movRR(this.em, R.rsi, R.rsp);
2883
+ movRI64(this.em, R.rdx, 1n);
2884
+ movRI64(this.em, R.rax, 1n);
2885
+ syscall(this.em);
2886
+ addRsp(this.em, 16);
2887
+ popR(this.em, R.rcx);
2888
+ popR(this.em, R.rdx);
2889
+ popR(this.em, R.rsi);
2890
+ popR(this.em, R.rdi);
2891
+ popR(this.em, R.rax);
2892
+ ret(this.em);
2893
+ }
2894
+
2895
+ genPrintSpace() {
1317
2896
  this.em.label("rt_print_space");
1318
2897
  pushR(this.em, R.rax);
1319
2898
  pushR(this.em, R.rdi);
@@ -1527,9 +3106,19 @@ class DirectGen {
1527
3106
  case "如果": this.genIf(st, ctx); break;
1528
3107
  case "当": this.genWhile(st, ctx); break;
1529
3108
  case "对于": this.genFor(st, ctx); break;
1530
- case "跳出": jmp(this.em, ctx.breakEnds[ctx.breakEnds.length - 1]); break;
1531
- case "继续": jmp(this.em, ctx.contHeads[ctx.contHeads.length - 1]); break;
3109
+ case "跳出":
3110
+ if (ctx.inTry > 0)
3111
+ throw new Error("直接机器码暂不支持从 尝试 块内 跳出(宿主会执行 最终 再跳出,改走解释器兜底)");
3112
+ jmp(this.em, ctx.breakEnds[ctx.breakEnds.length - 1]);
3113
+ break;
3114
+ case "继续":
3115
+ if (ctx.inTry > 0)
3116
+ throw new Error("直接机器码暂不支持从 尝试 块内 继续(改走解释器兜底)");
3117
+ jmp(this.em, ctx.contHeads[ctx.contHeads.length - 1]);
3118
+ break;
1532
3119
  case "函数调用": this.genCall(st, ctx); break;
3120
+ case "抛出": this.genThrow(st, ctx); break;
3121
+ case "尝试": this.genTry(st, ctx); break;
1533
3122
  default:
1534
3123
  if (["二元","一元","字面量","标识符","下标","列表字面量","字典字面量"].includes(st.类型))
1535
3124
  this.genExpr(st, ctx);
@@ -1542,6 +3131,22 @@ class DirectGen {
1542
3131
  const k = this.genExpr(st.初始化, ctx);
1543
3132
  if (kind === "none") kind = k;
1544
3133
  const off = ctx.allocVar(st.名字, kind);
3134
+ const sl = ctx.slots.get(st.名字);
3135
+ if (k === "func") {
3136
+ const sl2 = ctx.slots.get(st.名字);
3137
+ if (st.初始化.类型 === "函数字面量") sl2.函数标签 = `FL${this.lambdaSeq}`;
3138
+ else if (st.初始化.类型 === "标识符" && ctx.slots.has(st.初始化.名字)) {
3139
+ const 源 = ctx.slots.get(st.初始化.名字);
3140
+ if (源.函数标签) sl2.函数标签 = 源.函数标签;
3141
+ }
3142
+ }
3143
+ if (kind === "list") {
3144
+ // 列表元素种类:字面量取本次推断结果;别名拷贝源;其余按 dyn(读元素走装箱)
3145
+ if (st.初始化.类型 === "列表字面量") sl.元素种类 = this._元素种类 || "int";
3146
+ else if (st.初始化.类型 === "标识符" && ctx.slots.has(st.初始化.名字))
3147
+ sl.元素种类 = ctx.slots.get(st.初始化.名字).元素种类 || "dyn";
3148
+ else sl.元素种类 = "dyn";
3149
+ }
1545
3150
  this.storeToSlot(off, k, kind);
1546
3151
  } else {
1547
3152
  if (kind === "none") kind = "int";
@@ -1565,7 +3170,7 @@ class DirectGen {
1565
3170
  // 布尔 x = "甲" -> 字符串指针进 bool 槽,打印恒为「真」
1566
3171
  // 整数 x = "甲" -> 同上,按整数拆位打印出堆地址
1567
3172
  // 宿主对这两种都给「类型错误(变量声明):期望 X,得到 Y」。
1568
- const 种类名 = { int: "整数", bool: "布尔", str: "字符串", list: "列表", dict: "字典" };
3173
+ const 种类名 = { int: "整数", bool: "布尔", str: "字符串", list: "列表", dict: "字典", dyn: "动态值", func: "函数" };
1569
3174
  if (fromK !== toK && 种类名[toK] && 种类名[fromK])
1570
3175
  throw new Error(`直接机器码拒绝 ${种类名[fromK]}→${种类名[toK]} 赋值(宿主报「类型错误(变量声明/赋值):期望 ${种类名[toK]},得到 ${种类名[fromK]}」,改走解释器兜底)`);
1571
3176
  if (fromK === toK) {
@@ -1590,29 +3195,108 @@ class DirectGen {
1590
3195
  if (st.目标.类型 === "标识符") {
1591
3196
  const { off, kind } = ctx.slotOff(st.目标.名字);
1592
3197
  const k = this.genExpr(st.值, ctx);
3198
+ // 别名赋值把匿名函数的返回种类一起带过去(否则 法 = 双 后退化成动态值)
3199
+ if (k === "func" && st.值.类型 === "标识符" && ctx.slots.has(st.值.名字)) {
3200
+ const 源 = ctx.slots.get(st.值.名字);
3201
+ if (源.函数标签) ctx.slots.get(st.目标.名字).函数标签 = 源.函数标签;
3202
+ }
3203
+ // 列表别名同样拷贝元素种类
3204
+ if (k === "list" && st.值.类型 === "标识符" && ctx.slots.has(st.值.名字)) {
3205
+ ctx.slots.get(st.目标.名字).元素种类 = ctx.slots.get(st.值.名字).元素种类 || "dyn";
3206
+ }
1593
3207
  this.storeToSlot(off, k, kind);
1594
3208
  return;
1595
3209
  }
1596
- // 表[下标] = 值 —— 越界由 rt_list_set 报中文错误,不在生成器里复制一份边界逻辑
3210
+ // 表[下标] = 值 / 字典[键] = 值 / 动态值[键] = 值
1597
3211
  if (st.目标.类型 === "下标") {
1598
3212
  const { 容器, 索引 } = st.目标;
1599
- if (容器.类型 !== "标识符") throw new Error("原生赋值目标暂只支持 变量[下标]");
1600
- const { off, kind } = ctx.slotOff(容器.名字);
1601
- if (kind !== "list") throw new Error("只有列表支持下标赋值");
1602
- const k = this.genExpr(索引, ctx);
1603
- if (k === "double") throw new Error("列表下标必须是整数");
3213
+ // 动态容器(链式 d["a"]["b"] = v):容器求值为 dyn 后运行时分派
3214
+ if (容器.类型 !== "标识符") {
3215
+ const ck = this.genExpr(容器, ctx);
3216
+ if (ck !== "dyn") throw new Error(`原生赋值目标暂只支持 变量[下标] 与 动态值[下标](容器种类 ${ck})`);
3217
+ const 容槽 = ctx.newTemp();
3218
+ movMemR(this.em, 容槽, R.rax);
3219
+ const ik = this.genExpr(索引, ctx);
3220
+ this.boxRax(ik);
3221
+ const 键槽 = ctx.newTemp();
3222
+ movMemR(this.em, 键槽, R.rax);
3223
+ const vk = this.genExpr(st.值, ctx);
3224
+ this.boxRax(vk);
3225
+ const 值槽 = ctx.newTemp();
3226
+ movMemR(this.em, 值槽, R.rax);
3227
+ movRMem(this.em, R.rdi, 容槽);
3228
+ movRMem(this.em, R.rsi, 键槽);
3229
+ movRMem(this.em, R.rdx, 值槽);
3230
+ call(this.em, "rt_dyn_setindex");
3231
+ ctx.releaseTemp(3);
3232
+ return;
3233
+ }
3234
+ const sl = ctx.slotOff(容器.名字);
3235
+ const { off, kind } = sl;
3236
+ if (kind === "dyn") {
3237
+ movRMem(this.em, R.rax, off);
3238
+ const 容槽 = ctx.newTemp();
3239
+ movMemR(this.em, 容槽, R.rax);
3240
+ const ik = this.genExpr(索引, ctx);
3241
+ this.boxRax(ik);
3242
+ const 键槽 = ctx.newTemp();
3243
+ movMemR(this.em, 键槽, R.rax);
3244
+ const vk = this.genExpr(st.值, ctx);
3245
+ this.boxRax(vk);
3246
+ const 值槽 = ctx.newTemp();
3247
+ movMemR(this.em, 值槽, R.rax);
3248
+ movRMem(this.em, R.rdi, 容槽);
3249
+ movRMem(this.em, R.rsi, 键槽);
3250
+ movRMem(this.em, R.rdx, 值槽);
3251
+ call(this.em, "rt_dyn_setindex");
3252
+ ctx.releaseTemp(3);
3253
+ return;
3254
+ }
3255
+ if (kind === "dict") {
3256
+ const ik = this.genExpr(索引, ctx);
3257
+ if (ik !== "str")
3258
+ throw new Error(`直接机器码字典下标赋值只支持字符串键(收到 ${ik},改走解释器兜底)`);
3259
+ const 键槽 = ctx.newTemp();
3260
+ movMemR(this.em, 键槽, R.rax);
3261
+ const vk = this.genExpr(st.值, ctx);
3262
+ this.boxRax(vk);
3263
+ const 值槽 = ctx.newTemp();
3264
+ movMemR(this.em, 值槽, R.rax);
3265
+ movRMem(this.em, R.rdi, off);
3266
+ movRMem(this.em, R.rsi, 键槽);
3267
+ movRMem(this.em, R.rdx, 值槽);
3268
+ call(this.em, "rt_dict_set");
3269
+ ctx.releaseTemp(2);
3270
+ return;
3271
+ }
3272
+ if (kind !== "list") throw new Error("只有列表/字典/动态值支持下标赋值");
3273
+ const ik = this.genExpr(索引, ctx);
3274
+ if (ik === "double") throw new Error("列表下标必须是整数");
3275
+ if (ik === "dyn") throw new Error("直接机器码列表下标暂不支持动态值下标(改走解释器兜底)");
1604
3276
  const 下标槽 = ctx.newTemp();
1605
3277
  movMemR(this.em, 下标槽, R.rax);
1606
3278
  const vk = this.genExpr(st.值, ctx);
1607
3279
  if (vk === "double") throw new Error("列表元素暂不支持浮点");
1608
- if (vk === "str" || vk === "bool" || vk === "list" || vk === "dict")
1609
- throw new Error(`直接机器码列表只存裸整数(无类型标签):${vk === "str" ? "字符串" : vk === "bool" ? "布尔" : "容器"} 写入后会被当成整数读出并打印堆地址,改走解释器兜底`);
1610
3280
  const 值槽 = ctx.newTemp();
1611
3281
  movMemR(this.em, 值槽, R.rax);
1612
3282
  movRMem(this.em, R.rdi, off);
1613
3283
  movRMem(this.em, R.rsi, 下标槽);
1614
- movRMem(this.em, R.rdx, 值槽);
1615
- call(this.em, "rt_list_set");
3284
+ call(this.em, "rt_list_set_ptr"); // rax = 槽地址
3285
+ if (vk === "dyn") {
3286
+ // 值已是盒:拆盒进槽
3287
+ movRMem(this.em, R.rdx, 值槽);
3288
+ movRDisp(this.em, R.rdi, R.rdx, 0);
3289
+ movDispR(this.em, R.rax, 0, R.rdi);
3290
+ movRDisp(this.em, R.rdi, R.rdx, 8);
3291
+ movDispR(this.em, R.rax, 8, R.rdi);
3292
+ } else {
3293
+ movDispImm(this.em, R.rax, 0, this.种类到标签(vk));
3294
+ movRMem(this.em, R.rdx, 值槽);
3295
+ movDispR(this.em, R.rax, 8, R.rdx);
3296
+ }
3297
+ // 元素种类并入变量槽记录(不同种类混入 → dyn)
3298
+ const 现 = sl.元素种类;
3299
+ sl.元素种类 = 现 === undefined ? vk : (现 === vk ? vk : "dyn");
1616
3300
  ctx.releaseTemp(2);
1617
3301
  return;
1618
3302
  }
@@ -1620,6 +3304,8 @@ class DirectGen {
1620
3304
  }
1621
3305
 
1622
3306
  genReturn(st, ctx) {
3307
+ if (ctx.inTry > 0)
3308
+ throw new Error("直接机器码暂不支持在 尝试 块内 返回(宿主会执行 最终 再返回,改走解释器兜底)");
1623
3309
  if (st.值 != null) {
1624
3310
  const k = this.genExpr(st.值, ctx);
1625
3311
  if (ctx.返回种类) ctx.返回种类.push(k);
@@ -1643,10 +3329,126 @@ class DirectGen {
1643
3329
  ret(this.em);
1644
3330
  }
1645
3331
 
3332
+ // 抛出 expr —— 宿主语义:载荷是字典则原样抛;否则包成
3333
+ // {类型:"抛出", 消息:转字符串(值), 行, 列}(键序=插入序,与宿主 make_error_value 一致)
3334
+ genThrow(st, ctx) {
3335
+ const k = this.genExpr(st.值, ctx);
3336
+ const 行 = st.行 || 0, 列 = st.列 || 0;
3337
+ if (k === "dict") {
3338
+ this.boxRax(k);
3339
+ movRR(this.em, R.rdi, R.rax); // rt_throw 契约:rdi=盒(rt_box 会改写 rdi)
3340
+ call(this.em, "rt_throw");
3341
+ return;
3342
+ }
3343
+ // 非字典载荷:现场构造错误字典
3344
+ const 值槽 = ctx.newTemp();
3345
+ movMemR(this.em, 值槽, R.rax);
3346
+ movRI64(this.em, R.rdi, 16n);
3347
+ call(this.em, "rt_dict_new");
3348
+ const 典槽 = ctx.newTemp();
3349
+ movMemR(this.em, 典槽, R.rax);
3350
+ const 插 = (键名, 摆载荷) => {
3351
+ // 键先落槽再摆载荷:载荷求值(如 抛出 列表 → rt_list_tostr)会改写 rax,
3352
+ // 直接拿 rax 当键缓存会被覆盖(实测把处理器帧地址当成消息装进载荷字典)。
3353
+ this.loadStrRax(this.strConst(键名));
3354
+ const 键槽 = ctx.newTemp();
3355
+ movMemR(this.em, 键槽, R.rax);
3356
+ 摆载荷();
3357
+ const 盒槽 = ctx.newTemp();
3358
+ movMemR(this.em, 盒槽, R.rax);
3359
+ movRMem(this.em, R.rdi, 典槽);
3360
+ movRMem(this.em, R.rsi, 键槽);
3361
+ movRMem(this.em, R.rdx, 盒槽);
3362
+ call(this.em, "rt_dict_set");
3363
+ ctx.releaseTemp(2);
3364
+ };
3365
+ 插("类型", () => {
3366
+ this.loadStrRax(this.strConst("抛出"));
3367
+ movRR(this.em, R.rsi, R.rax);
3368
+ movRI64(this.em, R.rdi, BigInt(标签字符串));
3369
+ call(this.em, "rt_box");
3370
+ });
3371
+ 插("消息", () => {
3372
+ movRMem(this.em, R.rax, 值槽);
3373
+ this.coerceToStr(k); // rax=串对象
3374
+ movRR(this.em, R.rsi, R.rax);
3375
+ movRI64(this.em, R.rdi, BigInt(标签字符串));
3376
+ call(this.em, "rt_box");
3377
+ });
3378
+ 插("行", () => {
3379
+ movRI64(this.em, R.rsi, BigInt(行));
3380
+ movRI64(this.em, R.rdi, BigInt(标签整数));
3381
+ call(this.em, "rt_box");
3382
+ });
3383
+ 插("列", () => {
3384
+ movRI64(this.em, R.rsi, BigInt(列));
3385
+ movRI64(this.em, R.rdi, BigInt(标签整数));
3386
+ call(this.em, "rt_box");
3387
+ });
3388
+ ctx.releaseTemp(1);
3389
+ movRMem(this.em, R.rax, 典槽);
3390
+ this.boxRax("dict");
3391
+ movRR(this.em, R.rdi, R.rax); // rt_throw 契约:rdi=盒
3392
+ call(this.em, "rt_throw");
3393
+ ctx.releaseTemp(1);
3394
+ }
3395
+
3396
+ // 尝试 … [捕获 变量 …] [最终 …] 结束
3397
+ // 处理器帧挂在堆上:[0]=prev [8]=落地地址 [16]=rbp [24]=rsp
3398
+ // 正常路径:弹帧 → 最终;异常路径:rt_throw 已弹帧并恢复 rsp/rbp,
3399
+ // 落地点接管 EXC 槽 → 绑定捕获变量(dyn)→ 捕获体 → 最终。
3400
+ // 尝试块内 返回/跳出/继续 编译期拒绝(宿主会先执行 最终,直弹做不到同语义)。
3401
+ genTry(st, ctx) {
3402
+ const 帧 = ctx.newTemp();
3403
+ movRI64(this.em, R.rdi, BigInt(处理器帧字节));
3404
+ call(this.em, "rt_alloc");
3405
+ movMemR(this.em, 帧, R.rax);
3406
+ movRMem(this.em, R.r10, 帧);
3407
+ movRAbs(this.em, R.rax, HANDLER_TOP);
3408
+ movDispR(this.em, R.r10, 0, R.rax);
3409
+ movRI64(this.em, R.rax, 0n);
3410
+ const 落地 = this.em.nl("tr_landing");
3411
+ this.em.fixups.push({ pos: this.em.pos - 8, label: 落地, type: "codeAbs64" });
3412
+ movDispR(this.em, R.r10, 8, R.rax);
3413
+ movDispR(this.em, R.r10, 16, R.rbp);
3414
+ movDispR(this.em, R.r10, 24, R.rsp);
3415
+ movRR(this.em, R.rax, R.r10);
3416
+ movAbsR(this.em, HANDLER_TOP, R.rax);
3417
+ ctx.inTry++;
3418
+ for (const s of st.体) this.genStmt(s, ctx);
3419
+ ctx.inTry--;
3420
+ // 正常弹帧
3421
+ movRMem(this.em, R.r10, 帧);
3422
+ movRDisp(this.em, R.rax, R.r10, 0);
3423
+ movAbsR(this.em, HANDLER_TOP, R.rax);
3424
+ for (const s of (st.最终体 || [])) this.genStmt(s, ctx);
3425
+ const end = this.em.nl("tr_end");
3426
+ jmp(this.em, end);
3427
+ this.em.label(落地);
3428
+ const 有捕获 = (st.捕获体 || []).length > 0 || st.捕获变量;
3429
+ if (st.捕获变量) {
3430
+ const off = ctx.allocVar(st.捕获变量, "dyn");
3431
+ movRAbs(this.em, R.rax, EXC_SLOT);
3432
+ movMemR(this.em, off, R.rax);
3433
+ }
3434
+ if (有捕获) {
3435
+ for (const s of (st.捕获体 || [])) this.genStmt(s, ctx);
3436
+ for (const s of (st.最终体 || [])) this.genStmt(s, ctx);
3437
+ } else {
3438
+ // 只有 最终:清理后继续外抛(宿主语义)
3439
+ for (const s of (st.最终体 || [])) this.genStmt(s, ctx);
3440
+ movRAbs(this.em, R.rdi, EXC_SLOT);
3441
+ call(this.em, "rt_throw");
3442
+ }
3443
+ this.em.label(end);
3444
+ ctx.releaseTemp(1);
3445
+ }
3446
+
1646
3447
  genIf(st, ctx) {
1647
3448
  const elseL = this.em.nl("if_else");
1648
3449
  const endL = this.em.nl("if_end");
1649
3450
  const k = this.genExpr(st.条件, ctx);
3451
+ this.emitTruthy(k);
1650
3452
  testRR(this.em, R.rax, R.rax);
1651
3453
  jcc(this.em, JCC.z, elseL);
1652
3454
  for (const s of st.则体) this.genStmt(s, ctx);
@@ -1655,7 +3457,8 @@ class DirectGen {
1655
3457
  for (const br of st.否则链) {
1656
3458
  if (br.条件 != null) {
1657
3459
  const nextL = this.em.nl("elif");
1658
- this.genExpr(br.条件, ctx);
3460
+ const bk = this.genExpr(br.条件, ctx);
3461
+ this.emitTruthy(bk);
1659
3462
  testRR(this.em, R.rax, R.rax);
1660
3463
  jcc(this.em, JCC.z, nextL);
1661
3464
  for (const s of br.体) this.genStmt(s, ctx);
@@ -1674,7 +3477,8 @@ class DirectGen {
1674
3477
  ctx.breakEnds.push(end);
1675
3478
  ctx.contHeads.push(head);
1676
3479
  this.em.label(head);
1677
- this.genExpr(st.条件, ctx);
3480
+ const k = this.genExpr(st.条件, ctx);
3481
+ this.emitTruthy(k);
1678
3482
  testRR(this.em, R.rax, R.rax);
1679
3483
  jcc(this.em, JCC.z, end);
1680
3484
  for (const s of st.体) this.genStmt(s, ctx);
@@ -1783,10 +3587,16 @@ class DirectGen {
1783
3587
  if (name === "输出") { this.genPrint(st.参数, ctx); return "none"; }
1784
3588
  if (name === "长度" && st.参数.length === 1) return this.genBuiltinLen(st.参数[0], ctx);
1785
3589
  if (name === "转字符串" && st.参数.length === 1) return this.genBuiltinStr(st.参数[0], ctx);
3590
+ if (name === "类型" && st.参数.length === 1) return this.genBuiltinType(st.参数[0], ctx);
3591
+ if (name === "包含" && st.参数.length === 2) return this.genBuiltinContains(st.参数, ctx);
3592
+ if (name === "键" && st.参数.length === 1) return this.genBuiltinDictList(st.参数, ctx, true);
3593
+ if (name === "值" && st.参数.length === 1) return this.genBuiltinDictList(st.参数, ctx, false);
3594
+ if (name === "移除键" && st.参数.length === 2) return this.genBuiltinDelKey(st.参数, ctx);
3595
+ if (name === "合并字典" && st.参数.length === 2) return this.genBuiltinDictUpdate(st.参数, ctx);
1786
3596
  if (name === "追加" && st.参数.length === 2) return this.genBuiltinAppend(st.参数, ctx);
1787
3597
 
1788
- const lbl = this.funcLabelMap.get(name);
1789
- if (!lbl) throw new Error(`未定义函数: ${name}`);
3598
+ const lbl = name ? this.funcLabelMap.get(name) : undefined;
3599
+ if (!lbl) return this.genIndirectCall(st, ctx);
1790
3600
  const 签名 = this.funcSigs.get(lbl);
1791
3601
  // 实参个数:宿主在执行期报「参数个数不符」,直弹是静态调用,
1792
3602
  // 少传的参数槽会是【未初始化内存】——比报错更糟,编译期就拒。
@@ -1822,7 +3632,8 @@ class DirectGen {
1822
3632
  落槽();
1823
3633
  参数序号++;
1824
3634
  }
1825
- // 传参到寄存器
3635
+ // 传参到寄存器:必须从槽重新装载——实参求值本身(如 d[k] 的 rt_dict_get)
3636
+ // 会改写 rax,直接搬 rax 会把中间值当实参(实测 输出(键(d)) 收到 16)。
1826
3637
  let ireg = 0;
1827
3638
  for (const a of argSlots) {
1828
3639
  movRMem(this.em, ARG_REGS[ireg], a.slot);
@@ -1840,13 +3651,205 @@ class DirectGen {
1840
3651
  return this.funcRetKinds.get(lbl) || "int";
1841
3652
  }
1842
3653
 
1843
- // 长度(串|表) -> 字符数 / 元素个数(两者都是对象 [0 或 8] 里现成的数)
1844
- // 列表取 [0]=长度,字符串取 [8]=字符数:按编译期已知的种类分派,
1845
- // 不在运行时靠 magic 数嗅探,语义写错时是编译期报错而不是跑出怪数字。
3654
+ // ---- 函数值(匿名函数 / 命名函数作值)----
3655
+ // 对象布局:[0]=5(函数标签) [8]=代码地址 [16]=环境指针(本版恒 0)
3656
+ // [24]=返回标签(种类标签,dyn 记 0 之外的未知用 -1) [32]=名字串指针(匿名=0)
3657
+ // 本版不做捕获:闭包里引用外层局部会在 slotOff 处编译期拒绝,启动器回退解释器。
3658
+ genFuncObject(标签, 返回标签, 名字标签) {
3659
+ pushR(this.em, R.rbx);
3660
+ movRI64(this.em, R.rdi, BigInt(函数对象字节));
3661
+ call(this.em, "rt_alloc");
3662
+ movRR(this.em, R.rbx, R.rax);
3663
+ movRI64(this.em, R.rdx, 5n);
3664
+ movDispR(this.em, R.rbx, 0, R.rdx); // 标签
3665
+ movRI64(this.em, R.rax, 0n); // 代码地址占位
3666
+ this.em.fixups.push({ pos: this.em.pos - 8, label: 标签, type: "codeAbs64" });
3667
+ movDispR(this.em, R.rbx, 8, R.rax);
3668
+ xorRR(this.em, R.rdx, R.rdx);
3669
+ movDispR(this.em, R.rbx, 16, R.rdx); // 环境指针
3670
+ movDispImm(this.em, R.rbx, 24, 返回标签); // 返回标签
3671
+ if (名字标签) {
3672
+ movRI64(this.em, R.rax, 0n);
3673
+ this.em.fixups.push({ pos: this.em.pos - 8, label: 名字标签, type: "dataAbs64" });
3674
+ } else {
3675
+ movRI64(this.em, R.rax, 0n);
3676
+ }
3677
+ movDispR(this.em, R.rbx, 32, R.rax); // 名字串
3678
+ movRR(this.em, R.rax, R.rbx);
3679
+ popR(this.em, R.rbx);
3680
+ return "func";
3681
+ }
3682
+
3683
+ // 种类 → 运行时标签;"dyn"/未知 → -1(消费点编译期拒绝)
3684
+ 种类到标签(k) {
3685
+ if (k === "dyn" || k === "none") return -1;
3686
+ return 种类标签[k] !== undefined ? 种类标签[k] : -1;
3687
+ }
3688
+
3689
+ genFuncLiteral(node) {
3690
+ const 标签 = `FL${++this.lambdaSeq}`;
3691
+ if (!this.lambdaRetKinds) this.lambdaRetKinds = new Map();
3692
+ this.lambdaRetKinds.set(标签, this.推断返回种类(node));
3693
+ this.pendingLambdas.push({ 标签, 节点: node });
3694
+ const 返 = this.lambdaRetKinds.get(标签);
3695
+ return this.genFuncObject(标签, this.种类到标签(返), null);
3696
+ }
3697
+
3698
+ // 静态推断匿名函数返回种类:有标注按标注;否则只看 返回 的字面量。
3699
+ // 推断不出来就返回 "dyn"(消费点会编译期拒绝 → 回退解释器),不猜整数。
3700
+ 推断返回种类(node) {
3701
+ const 标注 = node.返回类型 || "";
3702
+ if (标注) return this.kindFromAnnotation(标注);
3703
+ let 种类 = null;
3704
+ const 走 = (n) => {
3705
+ if (!n || typeof n !== "object") return;
3706
+ if (Array.isArray(n)) { for (const x of n) 走(x); return; }
3707
+ if (n.类型 === "函数字面量" || n.类型 === "函数定义") return; // 不深入嵌套函数
3708
+ if (n.类型 === "返回") {
3709
+ const v = n.值;
3710
+ let k = "dyn";
3711
+ if (v == null) k = "int";
3712
+ else if (v.类型 === "字面量") {
3713
+ k = v.字面类型 === "整数" ? "int" : v.字面类型 === "布尔" ? "bool"
3714
+ : v.字面类型 === "字符串" ? "str" : "dyn";
3715
+ } else if (v.类型 === "二元" || v.类型 === "一元") k = "int";
3716
+ if (种类 === null) 种类 = k;
3717
+ else if (种类 !== k) 种类 = "dyn";
3718
+ return;
3719
+ }
3720
+ for (const key of Object.keys(n)) { if (key !== "类型") 走(n[key]); }
3721
+ };
3722
+ 走(node.体);
3723
+ return 种类 === null ? "int" : 种类;
3724
+ }
3725
+
3726
+ // 命名函数作值:f = 加一 / 映射(加一, 表)
3727
+ genFuncRef(name) {
3728
+ const 标签 = this.funcLabelMap.get(name);
3729
+ if (!标签) throw new Error(`未定义函数: ${name}`);
3730
+ const 返 = this.funcRetKinds.get(标签) || "int";
3731
+ return this.genFuncObject(标签, this.种类到标签(返), this.strConst(name));
3732
+ }
3733
+
3734
+ // 间接调用:先算被调者到槽,再算实参。
3735
+ // 返回种类静态可知(命名函数值 / 已推断的匿名函数)→ 直调取原始返回值;
3736
+ // 否则走 rt_dyn_call:运行时按函数对象 [24] 的返回标签装箱,种类记为 dyn,
3737
+ // 消费点若按整数/字符串误用会编译期拒绝(回退解释器),绝不按整数乱打印。
3738
+ genIndirectCall(st, ctx) {
3739
+ const 被调者 = st.被调者;
3740
+ let 函数槽;
3741
+ if (被调者.类型 === "标识符" && ctx.slots.has(被调者.名字)) {
3742
+ const { off, kind } = ctx.slotOff(被调者.名字);
3743
+ if (kind !== "func" && kind !== "none") throw new Error(`不能调用 ${被调者.名字}(种类 ${kind},不是函数)`);
3744
+ movRMem(this.em, R.rax, off);
3745
+ 函数槽 = ctx.newTemp();
3746
+ movMemR(this.em, 函数槽, R.rax);
3747
+ } else if (被调者.类型 === "标识符") {
3748
+ this.genFuncRef(被调者.名字);
3749
+ 函数槽 = ctx.newTemp();
3750
+ movMemR(this.em, 函数槽, R.rax);
3751
+ } else {
3752
+ const k = this.genExpr(被调者, ctx);
3753
+ if (k !== "func" && k !== "dyn")
3754
+ throw new Error(`不能调用非函数值(种类 ${k})`);
3755
+ 函数槽 = ctx.newTemp();
3756
+ movMemR(this.em, 函数槽, R.rax);
3757
+ if (k === "dyn") {
3758
+ // 盒里的函数:取出对象再走 dyn 路径(rt_dyn_call 会校验标签)
3759
+ movRMem(this.em, R.rax, 函数槽);
3760
+ movRDisp(this.em, R.rax, R.rax, 8);
3761
+ movMemR(this.em, 函数槽, R.rax);
3762
+ }
3763
+ }
3764
+ const 参数槽 = [];
3765
+ for (const a of st.参数) {
3766
+ const k = this.genExpr(a, ctx);
3767
+ if (k === "dyn")
3768
+ throw new Error("直接机器码暂不支持把动态值(d[k])直接传给函数实参(宿主按运行期标注校验,改走解释器兜底)");
3769
+ const sl = ctx.newTemp();
3770
+ movMemR(this.em, sl, R.rax);
3771
+ 参数槽.push(sl);
3772
+ }
3773
+ let ireg = 0;
3774
+ for (const sl of 参数槽) { movRMem(this.em, ARG_REGS[ireg], sl); ireg++; }
3775
+ // 静态返回种类?
3776
+ let 静返 = null;
3777
+ if (被调者.类型 === "标识符" && ctx.slots.has(被调者.名字)) {
3778
+ const sl = ctx.slots.get(被调者.名字);
3779
+ if (sl.函数标签 && this.lambdaRetKinds) {
3780
+ const k = this.lambdaRetKinds.get(sl.函数标签);
3781
+ if (k && k !== "dyn") 静返 = k;
3782
+ }
3783
+ }
3784
+ if (静返) {
3785
+ movRMem(this.em, R.r11, 函数槽);
3786
+ movRDisp(this.em, R.r11, R.r11, 8); // 代码地址
3787
+ callR(this.em, R.r11);
3788
+ ctx.releaseTemp(参数槽.length + 1);
3789
+ return 静返;
3790
+ }
3791
+ movRMem(this.em, R.rdi, 函数槽);
3792
+ call(this.em, "rt_dyn_call");
3793
+ ctx.releaseTemp(参数槽.length + 1);
3794
+ return "dyn";
3795
+ }
3796
+
3797
+ // 匿名函数体:与 genFunc 同构(序言/栈守卫/参数入槽/隐式返回/回填帧大小)
3798
+ emitLambda(st, lbl) {
3799
+ const ctx = new FuncCtx(st.参数.length + 统计声明数(st.体));
3800
+ ctx.闭包 = true;
3801
+ ctx.retKind = this.kindFromAnnotation(st.返回类型 || "");
3802
+ this.em.label(lbl);
3803
+ pushR(this.em, R.rbp);
3804
+ movRR(this.em, R.rbp, R.rsp);
3805
+ const subRspPos = this.em.pos;
3806
+ this.em.bs([0x48, 0x81, 0xEC, 0, 0, 0, 0]);
3807
+ const 栈OK = this.em.nl("lm_ok");
3808
+ movRR(this.em, R.rax, R.rsp);
3809
+ cmpRAbs(this.em, R.rax, STACK_FLOOR);
3810
+ jcc(this.em, JCC.a, 栈OK);
3811
+ jmp(this.em, "rt_stack_fail");
3812
+ this.em.label(栈OK);
3813
+ if ((st.参数默认 || []).some((d) => d != null))
3814
+ throw new Error("直接机器码暂不支持默认参数(匿名函数),改走解释器兜底");
3815
+ const paramKinds = st.参数.map((_, i) => {
3816
+ const 宣 = (st.参数类型 && st.参数类型[i]) || "";
3817
+ if (宣 === "可变")
3818
+ throw new Error("直接机器码暂不支持可变参数(匿名函数),改走解释器兜底");
3819
+ return this.kindFromAnnotation(宣);
3820
+ });
3821
+ let ireg = 0;
3822
+ st.参数.forEach((pname, i) => {
3823
+ const k = paramKinds[i];
3824
+ const off = ctx.allocVar(pname, k);
3825
+ movMemR(this.em, off, ARG_REGS[ireg]);
3826
+ ireg++;
3827
+ });
3828
+ this.funcRetKinds.set(lbl, ctx.retKind === "none" ? "int" : ctx.retKind);
3829
+ ctx.返回种类 = [];
3830
+ for (const 语句 of st.体.语句) this.genStmt(语句, ctx);
3831
+ movRI64(this.em, R.rax, 0n);
3832
+ leave(this.em);
3833
+ ret(this.em);
3834
+ const N = ctx.stackSize();
3835
+ this.em.bytes[subRspPos + 3] = N & 0xFF;
3836
+ this.em.bytes[subRspPos + 4] = (N >> 8) & 0xFF;
3837
+ this.em.bytes[subRspPos + 5] = (N >> 16) & 0xFF;
3838
+ this.em.bytes[subRspPos + 6] = (N >> 24) & 0xFF;
3839
+ const 实测 = ctx.返回种类.length === 0 ? null : ctx.返回种类[0];
3840
+ for (const k of ctx.返回种类) {
3841
+ if (k !== 实测)
3842
+ throw new Error(`直接机器码要求匿名函数各条 返回 的种类一致(实测有 ${实测} 与 ${k}),改走解释器兜底`);
3843
+ }
3844
+ if (实测 !== null) this.funcRetKinds.set(lbl, 实测);
3845
+ }
3846
+
3847
+ // 长度(串|表|字典) -> 字符数 / 元素个数 / 键数
1846
3848
  genBuiltinLen(arg, ctx) {
1847
3849
  const k = this.genExpr(arg, ctx);
1848
3850
  if (k === "str") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_str_len"); }
1849
3851
  else if (k === "list") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_list_len"); }
3852
+ else if (k === "dict") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_dict_len"); }
1850
3853
  else throw new Error(`长度() 不支持 ${k} 类型`);
1851
3854
  return "int";
1852
3855
  }
@@ -1856,10 +3859,9 @@ class DirectGen {
1856
3859
  const k = this.genExpr(arg, ctx);
1857
3860
  if (k === "str") return "str";
1858
3861
  if (k === "double") throw new Error("转字符串() 暂不支持浮点");
1859
- // 同 输出:列表/字典 转字符串 需要递归格式化例程,没有之前一律拒绝,
1860
- // 绝不能把堆地址转成数字串冒充结果。
1861
- if (k === "list" || k === "dict")
1862
- throw new Error("直接机器码暂不支持 转字符串(列表/字典)(改走解释器兜底)");
3862
+ if (k === "dyn") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_dyn_tostr"); return "str"; }
3863
+ if (k === "list") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_list_tostr"); return "str"; }
3864
+ if (k === "dict") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_dict_tostr"); return "str"; }
1863
3865
  // 转字符串(真) 宿主给「真」,走 rt_int_to_str 会给出「1」——静默值错
1864
3866
  if (k === "bool") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_bool_to_str"); return "str"; }
1865
3867
  movRR(this.em, R.rdi, R.rax);
@@ -1867,62 +3869,274 @@ class DirectGen {
1867
3869
  return "str";
1868
3870
  }
1869
3871
 
3872
+ // 类型(d[k]) -> 宿主类型名字符串(dyn 专用;静态种类沿用编译期知识)
3873
+ genBuiltinType(arg, ctx) {
3874
+ const k = this.genExpr(arg, ctx);
3875
+ if (k === "dyn") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_dyn_type"); return "str"; }
3876
+ const 名 = { int: "整数", bool: "布尔", str: "字符串", list: "列表", dict: "字典", func: "函数", none: "空" }[k];
3877
+ if (!名) throw new Error(`类型() 不支持 ${k}(改走解释器兜底)`);
3878
+ this.loadStrRax(this.strConst(名));
3879
+ return "str";
3880
+ }
3881
+
3882
+ // 包含(容器, 项):字典按键查(容器必须先存槽,再求值键,否则 rax 被覆盖)
3883
+ genBuiltinContains(args, ctx) {
3884
+ const ck = this.genExpr(args[0], ctx);
3885
+ const 容槽 = ctx.newTemp();
3886
+ movMemR(this.em, 容槽, R.rax);
3887
+ const ik = this.genExpr(args[1], ctx);
3888
+ if (ck === "dict") {
3889
+ if (ik !== "str") throw new Error("包含(字典, 键) 的键必须是字符串");
3890
+ const 键槽 = ctx.newTemp();
3891
+ movMemR(this.em, 键槽, R.rax);
3892
+ movRMem(this.em, R.rdi, 容槽);
3893
+ movRMem(this.em, R.rsi, 键槽);
3894
+ call(this.em, "rt_dict_has");
3895
+ ctx.releaseTemp(2);
3896
+ return "bool";
3897
+ }
3898
+ if (ck === "dyn") {
3899
+ throw new Error("包含() 暂不支持动态容器(d[k] 再包含,改走解释器兜底)");
3900
+ }
3901
+ throw new Error(`包含() 暂不支持 ${ck} 容器(改走解释器兜底)`);
3902
+ }
3903
+
3904
+ // 移除键(字典, 键) -> 布尔(宿主返回 1/0 整数,但语言层是布尔?宿主 builtin_del_key
3905
+ // 返回 val_int(dict_remove) —— 保持整数,与宿主逐字节一致)
3906
+ genBuiltinDelKey(args, ctx) {
3907
+ const dk = this.genExpr(args[0], ctx);
3908
+ if (dk !== "dict") throw new Error(`移除键() 的第一个参数必须是字典(收到 ${dk})`);
3909
+ const 容槽 = ctx.newTemp();
3910
+ movMemR(this.em, 容槽, R.rax);
3911
+ const kk = this.genExpr(args[1], ctx);
3912
+ if (kk !== "str") throw new Error("移除键() 的键必须是字符串");
3913
+ const 键槽 = ctx.newTemp();
3914
+ movMemR(this.em, 键槽, R.rax);
3915
+ movRMem(this.em, R.rdi, 容槽);
3916
+ movRMem(this.em, R.rsi, 键槽);
3917
+ call(this.em, "rt_dict_del");
3918
+ ctx.releaseTemp(2);
3919
+ return "int";
3920
+ }
3921
+
3922
+ // 合并字典(目标, 源) -> 目标(就地,宿主语义)
3923
+ genBuiltinDictUpdate(args, ctx) {
3924
+ const tk = this.genExpr(args[0], ctx);
3925
+ if (tk !== "dict") throw new Error(`合并字典() 的第一个参数必须是字典(收到 ${tk})`);
3926
+ const 容槽 = ctx.newTemp();
3927
+ movMemR(this.em, 容槽, R.rax);
3928
+ const sk = this.genExpr(args[1], ctx);
3929
+ if (sk !== "dict") throw new Error(`合并字典() 的第二个参数必须是字典(收到 ${sk})`);
3930
+ const 源槽 = ctx.newTemp();
3931
+ movMemR(this.em, 源槽, R.rax);
3932
+ movRMem(this.em, R.rdi, 容槽);
3933
+ movRMem(this.em, R.rsi, 源槽);
3934
+ call(this.em, "rt_dict_update");
3935
+ movRMem(this.em, R.rax, 容槽);
3936
+ ctx.releaseTemp(2);
3937
+ return "dict";
3938
+ }
3939
+
3940
+ // 键(字典) / 值(字典) -> 列表(插入顺序)
3941
+ genBuiltinDictList(args, ctx, 是键) {
3942
+ const k = this.genExpr(args[0], ctx);
3943
+ if (k !== "dict") throw new Error(`${是键 ? "键" : "值"}() 的参数必须是字典(收到 ${k})`);
3944
+ movRR(this.em, R.rdi, R.rax);
3945
+ call(this.em, 是键 ? "rt_dict_keys" : "rt_dict_values");
3946
+ return "list";
3947
+ }
3948
+
1870
3949
  // 追加(表, 值):作为语句调用时忽略返回值,作为表达式求值时返回表对象
1871
3950
  // 表参数必须是标识符,扩容写回才有地方放(这里和解释器一致:表是引用)。
1872
3951
  genBuiltinAppend(args, ctx) {
1873
3952
  const 容器 = args[0];
1874
3953
  const k = this.genExpr(args[1], ctx);
1875
3954
  if (k === "double") throw new Error("列表元素暂不支持浮点");
1876
- if (k === "str" || k === "bool" || k === "list" || k === "dict")
1877
- throw new Error(`直接机器码列表只存裸整数(无类型标签):${k === "str" ? "字符串" : k === "bool" ? "布尔" : "容器"} 元素会被当成整数读出并打印堆地址,改走解释器兜底`);
1878
3955
  const 值槽 = ctx.newTemp();
1879
3956
  movMemR(this.em, 值槽, R.rax);
1880
3957
  if (容器.类型 !== "标识符") throw new Error("追加() 的第一个参数需要是列表变量");
1881
3958
  const { off, kind } = ctx.slotOff(容器.名字);
1882
3959
  if (kind !== "list") throw new Error(`追加() 的第一个参数 ${容器.名字} 不是列表`);
1883
3960
  movRMem(this.em, R.rdi, off);
1884
- movRMem(this.em, R.rsi, 值槽);
3961
+ if (k === "dyn") {
3962
+ // 值已是盒:拆盒传 (tag, payload)
3963
+ movRMem(this.em, R.rax, 值槽);
3964
+ movRDisp(this.em, R.rsi, R.rax, 0);
3965
+ movRDisp(this.em, R.rdx, R.rax, 8);
3966
+ } else {
3967
+ movRI64(this.em, R.rsi, BigInt(this.种类到标签(k)));
3968
+ movRMem(this.em, R.rdx, 值槽);
3969
+ }
1885
3970
  call(this.em, "rt_list_append");
1886
3971
  movMemR(this.em, off, R.rax);
3972
+ // 元素种类并入(混入不同种类 → dyn)
3973
+ const sl = ctx.slots.get(容器.名字);
3974
+ const 现 = sl.元素种类;
3975
+ sl.元素种类 = 现 === undefined ? k : (现 === k ? k : "dyn");
1887
3976
  ctx.releaseTemp(1);
1888
3977
  return "list";
1889
3978
  }
1890
3979
 
3980
+ // 把 rax 里的静态种类值装箱:rdi=标签, rsi=载荷 → call rt_box
3981
+ // 顺序关键:先把载荷从 rax 搬进 rsi,再往 rax 装标签(反过来会把标签当载荷存进盒)
3982
+ boxRax(kind) {
3983
+ if (kind === "dyn") return; // 已经是盒,原地不动
3984
+ movRR(this.em, R.rsi, R.rax);
3985
+ movRI64(this.em, R.rax, BigInt(this.种类到标签(kind)));
3986
+ movRR(this.em, R.rdi, R.rax);
3987
+ call(this.em, "rt_box");
3988
+ }
3989
+
1891
3990
  genListLiteral(node, ctx) {
1892
3991
  movRI64(this.em, R.rdi, BigInt(Math.max(node.元素.length, 8)));
1893
3992
  call(this.em, "rt_list_new");
1894
3993
  const 槽 = ctx.newTemp();
1895
3994
  movMemR(this.em, 槽, R.rax);
3995
+ let 元素种 = null;
1896
3996
  for (const e of node.元素) {
1897
3997
  const k = this.genExpr(e, ctx);
1898
- if (k === "double") throw new Error("列表元素暂不支持浮点");
1899
- if (k === "str" || k === "bool" || k === "list" || k === "dict")
1900
- throw new Error(`直接机器码列表只存裸整数(无类型标签):${k === "str" ? "字符串" : k === "bool" ? "布尔" : "容器"} 元素会被当成整数读出并打印堆地址,改走解释器兜底`);
1901
3998
  movRMem(this.em, R.rdi, 槽);
1902
- movRR(this.em, R.rsi, R.rax);
3999
+ this.boxRax(k); // rax=盒
4000
+ movRDisp(this.em, R.rsi, R.rax, 0);
4001
+ movRDisp(this.em, R.rdx, R.rax, 8);
4002
+ movRMem(this.em, R.rdi, 槽);
1903
4003
  call(this.em, "rt_list_append");
1904
4004
  movMemR(this.em, 槽, R.rax);
4005
+ 元素种 = 元素种 === null ? k : (元素种 === k ? k : "dyn");
1905
4006
  }
1906
4007
  movRMem(this.em, R.rax, 槽);
1907
4008
  ctx.releaseTemp(1);
4009
+ this._元素种类 = 元素种 || "int";
1908
4010
  return "list";
1909
4011
  }
1910
4012
 
1911
- // 表[下标] —— 读;写走 genAssign 的下标分支
4013
+ // 表[下标] / 字典[键] / 动态值[下标] —— 读;写走 genAssign 的下标分支
1912
4014
  genIndex(node, ctx) {
1913
- if (node.容器.类型 !== "标识符") throw new Error("原生下标暂只支持 变量[下标]");
1914
- const { kind } = ctx.slotOff(node.容器.名字);
1915
- if (kind !== "list") throw new Error("原生下标暂只支持列表");
4015
+ // 非标识符容器:链式下标(d["a"]["b"])—— 容器求值成 dyn 后走运行时分派
4016
+ if (node.容器.类型 !== "标识符") {
4017
+ const ck = this.genExpr(node.容器, ctx);
4018
+ if (ck !== "dyn") throw new Error(`原生下标暂不支持 ${ck} 类型的表达式容器(改走解释器兜底)`);
4019
+ const 容槽 = ctx.newTemp();
4020
+ movMemR(this.em, 容槽, R.rax);
4021
+ const kk = this.genExpr(node.索引, ctx);
4022
+ this.boxRax(kk);
4023
+ const 键槽 = ctx.newTemp();
4024
+ movMemR(this.em, 键槽, R.rax);
4025
+ movRMem(this.em, R.rdi, 容槽);
4026
+ movRMem(this.em, R.rsi, 键槽);
4027
+ call(this.em, "rt_dyn_index");
4028
+ ctx.releaseTemp(2);
4029
+ return "dyn";
4030
+ }
4031
+ const sl = ctx.slotOff(node.容器.名字);
4032
+ const { off, kind } = sl;
4033
+ if (kind === "dyn") {
4034
+ movRMem(this.em, R.rax, off);
4035
+ const 容槽 = ctx.newTemp();
4036
+ movMemR(this.em, 容槽, R.rax);
4037
+ const kk = this.genExpr(node.索引, ctx);
4038
+ this.boxRax(kk);
4039
+ const 键槽 = ctx.newTemp();
4040
+ movMemR(this.em, 键槽, R.rax);
4041
+ movRMem(this.em, R.rdi, 容槽);
4042
+ movRMem(this.em, R.rsi, 键槽);
4043
+ call(this.em, "rt_dyn_index");
4044
+ ctx.releaseTemp(2);
4045
+ return "dyn";
4046
+ }
4047
+ if (kind === "dict") {
4048
+ const kk = this.genExpr(node.索引, ctx);
4049
+ if (kk !== "str" && kk !== "dyn")
4050
+ throw new Error(`直接机器码字典下标暂只支持字符串键(收到 ${kk},改走解释器兜底)`);
4051
+ const 键槽 = ctx.newTemp();
4052
+ movMemR(this.em, 键槽, R.rax);
4053
+ movRMem(this.em, R.rdi, off);
4054
+ movRMem(this.em, R.rsi, 键槽);
4055
+ call(this.em, kk === "dyn" ? "rt_dict_get_dyn" : "rt_dict_get");
4056
+ const hit = this.em.nl("ig_hit");
4057
+ const end = this.em.nl("ig_end");
4058
+ testRR(this.em, R.rax, R.rax);
4059
+ jcc(this.em, JCC.nz, hit);
4060
+ xorRR(this.em, R.rdi, R.rdi);
4061
+ xorRR(this.em, R.rsi, R.rsi);
4062
+ call(this.em, "rt_box"); // 缺键 → 空盒
4063
+ this.em.label(hit);
4064
+ ctx.releaseTemp(1);
4065
+ return "dyn";
4066
+ }
4067
+ if (kind === "str") {
4068
+ const kk = this.genExpr(node.索引, ctx);
4069
+ if (kk !== "int" && kk !== "bool")
4070
+ throw new Error("字符串下标必须是整数");
4071
+ const 键槽 = ctx.newTemp();
4072
+ movMemR(this.em, 键槽, R.rax);
4073
+ movRMem(this.em, R.rdi, off);
4074
+ movRMem(this.em, R.rsi, 键槽);
4075
+ call(this.em, "rt_str_at");
4076
+ ctx.releaseTemp(1);
4077
+ return "str";
4078
+ }
4079
+ if (kind !== "list") throw new Error(`只有列表/字典/字符串支持下标读取(收到 ${kind})`);
1916
4080
  const kk = this.genExpr(node.索引, ctx);
1917
4081
  if (kk === "double") throw new Error("列表下标必须是整数");
4082
+ if (kk === "dyn") {
4083
+ // 动态下标:容器(列表对象)装箱后走 dyn 分派;kk 已是盒
4084
+ const 键盒 = ctx.newTemp();
4085
+ movMemR(this.em, 键盒, R.rax);
4086
+ movRMem(this.em, R.rsi, off); // 列表对象
4087
+ movRI64(this.em, R.rdi, BigInt(标签列表));
4088
+ call(this.em, "rt_box");
4089
+ const 容盒 = ctx.newTemp();
4090
+ movMemR(this.em, 容盒, R.rax);
4091
+ movRMem(this.em, R.rdi, 容盒);
4092
+ movRMem(this.em, R.rsi, 键盒);
4093
+ call(this.em, "rt_dyn_index");
4094
+ ctx.releaseTemp(2);
4095
+ return "dyn";
4096
+ }
1918
4097
  const 下标槽 = ctx.newTemp();
1919
4098
  movMemR(this.em, 下标槽, R.rax);
1920
- const { off } = ctx.slotOff(node.容器.名字);
1921
4099
  movRMem(this.em, R.rdi, off);
1922
4100
  movRMem(this.em, R.rsi, 下标槽);
1923
- call(this.em, "rt_list_get");
4101
+ call(this.em, "rt_list_get_ptr");
4102
+ const 元素种 = sl.元素种类 || "dyn";
4103
+ if (元素种 === "int" || 元素种 === "bool" || 元素种 === "str") {
4104
+ movRDisp(this.em, R.rax, R.rax, 8); // 快路:已知种类直接取载荷
4105
+ ctx.releaseTemp(1);
4106
+ return 元素种;
4107
+ }
4108
+ movRDisp(this.em, R.rdi, R.rax, 0);
4109
+ movRDisp(this.em, R.rsi, R.rax, 8);
4110
+ call(this.em, "rt_box");
1924
4111
  ctx.releaseTemp(1);
1925
- return "int";
4112
+ return "dyn";
4113
+ }
4114
+
4115
+ // 字典字面量:{"键": 值, ...} —— 键求值为字符串对象,值统一装箱
4116
+ genDictLiteral(node, ctx) {
4117
+ movRI64(this.em, R.rdi, 16n);
4118
+ call(this.em, "rt_dict_new");
4119
+ const 槽 = ctx.newTemp();
4120
+ movMemR(this.em, 槽, R.rax);
4121
+ for (let i = 0; i < node.键.length; i++) {
4122
+ const kk = this.genExpr(node.键[i], ctx);
4123
+ if (kk !== "str")
4124
+ throw new Error(`直接机器码字典字面量的键必须是字符串(第 ${i + 1} 项是 ${kk},改走解释器兜底)`);
4125
+ const 键槽 = ctx.newTemp();
4126
+ movMemR(this.em, 键槽, R.rax);
4127
+ const vk = this.genExpr(node.值[i], ctx);
4128
+ this.boxRax(vk); // rax=值盒
4129
+ const 值槽 = ctx.newTemp();
4130
+ movMemR(this.em, 值槽, R.rax);
4131
+ movRMem(this.em, R.rdi, 槽);
4132
+ movRMem(this.em, R.rsi, 键槽);
4133
+ movRMem(this.em, R.rdx, 值槽);
4134
+ call(this.em, "rt_dict_set");
4135
+ ctx.releaseTemp(2);
4136
+ }
4137
+ movRMem(this.em, R.rax, 槽);
4138
+ ctx.releaseTemp(1);
4139
+ return "dict";
1926
4140
  }
1927
4141
 
1928
4142
  genPrint(args, ctx) {
@@ -1932,12 +4146,6 @@ class DirectGen {
1932
4146
  if (!first) call(this.em, "rt_print_space");
1933
4147
  first = false;
1934
4148
  const k = this.genExpr(a, ctx);
1935
- // 列表/字典对象直接打印:以前落到 rt_print_int,把【堆地址】当数字打出来
1936
- // (实测 输出 [1,2] 打 545259584、rc=0)。0.1.15 起就有,本轮审计才抓到,
1937
- // 属于「编得过、跑得动、但结果是错的」。宿主要的是 "[1, 2]" 这种带结构的
1938
- // 文本;直接后端没有递归格式化例程,宁可编译期拒绝走解释器兜底。
1939
- if (k === "list" || k === "dict")
1940
- throw new Error("直接机器码暂不支持打印 列表/字典 对象(旧行为是把堆地址当数字输出,改走解释器兜底拿 [1, 2])");
1941
4149
  if (k === "str") {
1942
4150
  movRR(this.em, R.rdi, R.rax);
1943
4151
  call(this.em, "rt_print_str");
@@ -1945,6 +4153,19 @@ class DirectGen {
1945
4153
  // 输出 真 必须打印「真/假」——走 rt_print_int 打成 1/0 就是静默值错
1946
4154
  movRR(this.em, R.rdi, R.rax);
1947
4155
  call(this.em, "rt_print_bool");
4156
+ } else if (k === "dyn") {
4157
+ movRR(this.em, R.rdi, R.rax);
4158
+ call(this.em, "rt_dyn_print");
4159
+ } else if (k === "list") {
4160
+ movRR(this.em, R.rdi, R.rax);
4161
+ call(this.em, "rt_print_list");
4162
+ } else if (k === "dict") {
4163
+ movRR(this.em, R.rdi, R.rax);
4164
+ call(this.em, "rt_print_dict");
4165
+ } else if (k === "func") {
4166
+ // 函数值打印:装箱后走 dyn 渲染(<函数 名字>,与宿主一致)
4167
+ this.boxRax(k);
4168
+ call(this.em, "rt_dyn_print");
1948
4169
  } else {
1949
4170
  movRR(this.em, R.rdi, R.rax);
1950
4171
  call(this.em, "rt_print_int");
@@ -1958,6 +4179,8 @@ class DirectGen {
1958
4179
  switch (node.类型) {
1959
4180
  case "字面量": return this.genLiteral(node, ctx);
1960
4181
  case "标识符": {
4182
+ if (!ctx.slots.has(node.名字) && this.funcLabelMap.has(node.名字))
4183
+ return this.genFuncRef(node.名字);
1961
4184
  const { off, kind } = ctx.slotOff(node.名字);
1962
4185
  movRMem(this.em, R.rax, off);
1963
4186
  return kind;
@@ -1967,6 +4190,8 @@ class DirectGen {
1967
4190
  case "函数调用": return this.genCall(node, ctx);
1968
4191
  case "下标": return this.genIndex(node, ctx);
1969
4192
  case "列表字面量": return this.genListLiteral(node, ctx);
4193
+ case "字典字面量": return this.genDictLiteral(node, ctx);
4194
+ case "函数字面量": return this.genFuncLiteral(node);
1970
4195
  default: throw new Error(`不支持表达式: ${node.类型}`);
1971
4196
  }
1972
4197
  }
@@ -2023,6 +4248,29 @@ class DirectGen {
2023
4248
  return k;
2024
4249
  }
2025
4250
 
4251
+ // 条件真值化:把 rax 里的值按种类折成 0/1(宿主 truthy 规则)。
4252
+ // 整数/布尔的 rax 本身就是数值,分支处直接 test 即可,这里不额外折。
4253
+ emitTruthy(k) {
4254
+ if (k === "dyn") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_dyn_truthy"); }
4255
+ else if (k === "str") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_truthy_obj"); }
4256
+ else if (k === "list" || k === "dict") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_truthy_coll"); }
4257
+ else if (k === "func") { movRI64(this.em, R.rax, 1n); }
4258
+ // none/int/bool:rax 即数值,分支处直接 test(沿用旧语义;none 槽装非零整数时为真,与宿主一致)
4259
+ }
4260
+
4261
+ // 把 rax 里的值转成字符串对象(宿主拼接/转字符串的渲染规则)
4262
+ coerceToStr(k) {
4263
+ if (k === "str") return;
4264
+ if (k === "int") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_int_to_str"); }
4265
+ else if (k === "bool") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_bool_to_str"); }
4266
+ else if (k === "dyn") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_dyn_tostr"); }
4267
+ else if (k === "list") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_list_tostr"); }
4268
+ else if (k === "dict") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_dict_tostr"); }
4269
+ else if (k === "func") { this.boxRax(k); call(this.em, "rt_dyn_tostr"); }
4270
+ else if (k === "none") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_int_to_str"); }
4271
+ else throw new Error(`拼接暂不支持 ${k}(改走解释器兜底)`);
4272
+ }
4273
+
2026
4274
  genBinary(node, ctx) {
2027
4275
  const op = node.运算符;
2028
4276
  if (op === "且") return this.genLogicAnd(node, ctx);
@@ -2035,37 +4283,38 @@ class DirectGen {
2035
4283
  // 右值在 rax
2036
4284
  const Rk = this.genExpr(node.右, ctx);
2037
4285
 
2038
- // 列表/字典参与运算:直接后端的列表是无类型标签的裸 int64 槽(元素句柄),
2039
- // 字典同理。裸槽相加/比较都是指针垃圾——实测 函数 拼(a,b){返回 a+b}
2040
- // 拼([1,2],[3]) 编过、跑出确定性垃圾 1090519256,随后 x[0] 报
2041
- // 「不支持的类型索引」;宿主与自举层给 [1, 2, 3]。比较(==/< 等)同理是
2042
- // 指针比较而非结构比较。按项目红线「宁可兜底也不发会把内存地址当结果的
2043
- // 产物」——编译期拒绝,启动器自动回落解释器(结果正确只是慢)。
2044
- if (L === "list" || Rk === "list" || L === "dict" || Rk === "dict") {
2045
- const 种类名 = (k) => (k === "list" ? "列表" : k === "dict" ? "字典" : k);
2046
- throw new Error(
2047
- `直接机器码暂不支持列表/字典运算(${种类名(L)} ${op} ${种类名(Rk)},改走解释器兜底,结果正确只是慢)`
2048
- );
2049
- }
4286
+ // 动态值参与比较/算术的闸门。注意 none 不在列:无标注形参/未初始化槽
4287
+ // 沿用旧的整数槽语义(直弹既有行为),把 none 划进动态集会误伤
4288
+ // 三倍 = 函数(n) => n*3 这类合法写法(实测编译被拒 → 无谓回退)。
4289
+ const 动态种 = new Set(["dyn", "dict", "list", "func"]);
2050
4290
 
2051
- // 比较
4291
+ // 比较:先于算术处理。dyn/字典/列表/函数参与 ==/!= 走 rt_dyn_eq(结构比较);
4292
+ // <,> 系列对动态值一律编译期拒绝(宿主对不可比类型报错,兜底通道给准确文案)。
2052
4293
  if (["<", ">", "<=", ">=", "==", "!="].includes(op)) {
2053
- if (L === "str" || Rk === "str") return this.genStrCompare(op, slotL, L, Rk, ctx);
4294
+ if (L === "str" || Rk === "str") {
4295
+ if (动态种.has(L) || 动态种.has(Rk)) {
4296
+ if ((L === "str" && Rk === "dyn") || (L === "dyn" && Rk === "str")) {
4297
+ return this.genDynCompare(op, slotL, L, Rk, ctx);
4298
+ }
4299
+ throw new Error(`直接机器码暂不支持 字符串 与 ${L === "str" ? Rk : L} 比较(宿主按类型规则,改走解释器兜底)`);
4300
+ }
4301
+ return this.genStrCompare(op, slotL, L, Rk, ctx);
4302
+ }
4303
+ if (动态种.has(L) || 动态种.has(Rk)) {
4304
+ if (op !== "==" && op !== "!=")
4305
+ throw new Error(`直接机器码不支持动态值的大小比较(${op},改走解释器兜底)`);
4306
+ return this.genDynCompare(op, slotL, L, Rk, ctx);
4307
+ }
2054
4308
  return this.genCompare(op, slotL, ctx);
2055
4309
  }
2056
4310
 
2057
4311
  // 「+」只要有一边是串就走拼接(O(1) 绳索),另一边按动态语义先转字符串。
2058
- // 两边都是整数才落到下面的数值加法——这条分支顺序就是「1+2」和"1"+2 的分水岭。
2059
4312
  if (op === "+" && (L === "str" || Rk === "str")) {
2060
- // 布尔入串走 rt_bool_to_str(宿主实测 "值" + 真 == "值真",不是 "值1")
2061
- if (Rk === "bool") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_bool_to_str"); }
2062
- else if (Rk !== "str") { movRR(this.em, R.rdi, R.rax); call(this.em, "rt_int_to_str"); }
4313
+ if (Rk !== "str") this.coerceToStr(Rk);
2063
4314
  const slotR = ctx.newTemp();
2064
- movMemR(this.em, slotR, R.rax); // 右对象入槽(r10 会被 rt_int_to_str 吃掉)
2065
- movRMem(this.em, R.rdi, slotL); // 左值
2066
- if (L === "bool") call(this.em, "rt_bool_to_str");
2067
- else if (L !== "str") call(this.em, "rt_int_to_str");
2068
- else movRR(this.em, R.rax, R.rdi);
4315
+ movMemR(this.em, slotR, R.rax); // 右对象入槽
4316
+ movRMem(this.em, R.rax, slotL); // 左值装回 rax(coerceToStr 的契约入口)
4317
+ this.coerceToStr(L);
2069
4318
  movRR(this.em, R.rdi, R.rax); // rdi = 左对象
2070
4319
  movRMem(this.em, R.rsi, slotR); // rsi = 右对象
2071
4320
  call(this.em, "rt_concat");
@@ -2073,6 +4322,15 @@ class DirectGen {
2073
4322
  return "str";
2074
4323
  }
2075
4324
 
4325
+ // 容器/动态值参与算术:编译期拒绝(宿主对 字典+数字 之类报「不支持的运算」,
4326
+ // 直弹若静默按指针算就是「能跑但结果错」——红线所在)。
4327
+ if (动态种.has(L) || 动态种.has(Rk)) {
4328
+ const 种类名 = (k) => (k === "list" ? "列表" : k === "dict" ? "字典" : k === "dyn" ? "动态值" : k === "func" ? "函数" : "空");
4329
+ throw new Error(
4330
+ `直接机器码不支持 ${种类名(L)} ${op} ${种类名(Rk)}(改走解释器兜底,结果正确只是慢)`
4331
+ );
4332
+ }
4333
+
2076
4334
  if (L === "str" || Rk === "str")
2077
4335
  throw new Error(`字符串不支持运算符 ${op}`);
2078
4336
 
@@ -2085,6 +4343,14 @@ class DirectGen {
2085
4343
  else if (op === "-") subRR(this.em, R.rax, R.r10);
2086
4344
  else if (op === "*") imulRR(this.em, R.rax, R.r10);
2087
4345
  else if (op === "//" || op === "%") {
4346
+ // 除零守卫:宿主报「整除的除数为零」/「取模的除数为零」且可被 捕获;
4347
+ // 直弹以前直接 idiv → SIGFPE(rc=132,「编得过、跑不过」)。
4348
+ const dz = this.em.nl("dz");
4349
+ testRR(this.em, R.r10, R.r10);
4350
+ jcc(this.em, JCC.nz, dz);
4351
+ movRI64(this.em, R.rdi, op === "//" ? 0n : 1n);
4352
+ call(this.em, "rt_div_zero");
4353
+ this.em.label(dz);
2088
4354
  cqo(this.em);
2089
4355
  idivR(this.em, R.r10);
2090
4356
  if (op === "%") movRR(this.em, R.rax, R.rdx);
@@ -2133,6 +4399,27 @@ class DirectGen {
2133
4399
  return "int";
2134
4400
  }
2135
4401
 
4402
+ // 动态值参与的 ==/!=:两侧装箱后 rt_dyn_eq,按标签分派结构比较。
4403
+ // 顺序关键:右值此刻还在 rax,必须先装箱存槽,再回头取左值——
4404
+ // 反过来会让 movRMem(rax, slotL) 覆盖右值,把左盒当右值比(实测恒假/恒真类错)。
4405
+ genDynCompare(op, slotL, Lk, Rk, ctx) {
4406
+ this.boxRax(Rk);
4407
+ const 右盒 = ctx.newTemp();
4408
+ movMemR(this.em, 右盒, R.rax);
4409
+ movRMem(this.em, R.rax, slotL);
4410
+ this.boxRax(Lk);
4411
+ movRR(this.em, R.rdi, R.rax);
4412
+ movRMem(this.em, R.rsi, 右盒);
4413
+ call(this.em, "rt_dyn_eq");
4414
+ if (op === "!=") {
4415
+ testRR(this.em, R.rax, R.rax);
4416
+ setccAl(this.em, SETCC.z);
4417
+ movzbEaxAl(this.em);
4418
+ }
4419
+ ctx.releaseTemp(1);
4420
+ return "bool";
4421
+ }
4422
+
2136
4423
  // 串参与的比较:目前只有 == / != 有定义(按 UTF-8 字节逐位比)。
2137
4424
  // 大小于需要字典序运行时,还没实现——宁可编译期拒绝,也不要悄悄比错。
2138
4425
  genStrCompare(op, slotL, Lk, Rk, ctx) {
@@ -2184,12 +4471,14 @@ class DirectGen {
2184
4471
  }
2185
4472
 
2186
4473
  genLogicAnd(node, ctx) {
2187
- this.genExpr(node.左, ctx);
4474
+ const lk = this.genExpr(node.左, ctx);
4475
+ this.emitTruthy(lk);
2188
4476
  const falseL = this.em.nl("and_f");
2189
4477
  const endL = this.em.nl("and_e");
2190
4478
  testRR(this.em, R.rax, R.rax);
2191
4479
  jcc(this.em, JCC.z, falseL);
2192
- this.genExpr(node.右, ctx);
4480
+ const rk = this.genExpr(node.右, ctx);
4481
+ this.emitTruthy(rk);
2193
4482
  testRR(this.em, R.rax, R.rax);
2194
4483
  setccAl(this.em, SETCC.ne);
2195
4484
  movzbEaxAl(this.em);
@@ -2202,12 +4491,14 @@ class DirectGen {
2202
4491
  }
2203
4492
 
2204
4493
  genLogicOr(node, ctx) {
2205
- this.genExpr(node.左, ctx);
4494
+ const lk = this.genExpr(node.左, ctx);
4495
+ this.emitTruthy(lk);
2206
4496
  const trueL = this.em.nl("or_t");
2207
4497
  const endL = this.em.nl("or_e");
2208
4498
  testRR(this.em, R.rax, R.rax);
2209
4499
  jcc(this.em, JCC.nz, trueL);
2210
- this.genExpr(node.右, ctx);
4500
+ const rk = this.genExpr(node.右, ctx);
4501
+ this.emitTruthy(rk);
2211
4502
  testRR(this.em, R.rax, R.rax);
2212
4503
  setccAl(this.em, SETCC.ne);
2213
4504
  movzbEaxAl(this.em);
@@ -2315,8 +4606,8 @@ function 检查能力(输入路径) {
2315
4606
  报告.建议 = "原生后端目前只支持整数运算。浮点表达式可改写成整数运算,"
2316
4607
  + "或把这段逻辑放进函数里用 cocn-math 之类的包(回退解释器结果一致,只是慢)。";
2317
4608
  } else if (msg.includes("字典")) {
2318
- 报告.建议 = "原生后端暂不支持字典(字典值需要类型标签,"
2319
- + "本后端的槽位不带标签,强转会静默算错)。可用列表 + 平行下标代替。";
4609
+ 报告.建议 = "字典参与算术/大小比较暂不支持(值需运行期标签分派,宿主语义在此处由解释器给出);"
4610
+ + "字典的读写/长度/包含/键/值/打印/相等自 0.0.5 起可直弹。";
2320
4611
  } else if (msg.includes("默认参数") || msg.includes("可变参数")) {
2321
4612
  报告.建议 = "原生后端暂不支持默认参数/可变参数;可改写成显式传参。";
2322
4613
  } else if (msg.includes("元组")) {