| | @@ -75,7 +75,7 @@ |
| 75 | 75 | target: int64; { the frame slot of a procedure value, if chunk < 0 } |
| 76 | 76 | chunk: longint; { else the procedure's chunk } |
| 77 | 77 | ext: boolean; { C's: called through the chunk holding its address } |
| 78 | | - ret: int64; { set by Call: the temporary a result of more than one word is in } |
| 78 | + ret: int64; { set by Call: the temporary a slice or a record result is in } |
| 79 | 79 | end; |
| 80 | 80 | |
| 81 | 81 | const |
| | @@ -335,8 +335,30 @@ |
| 335 | 335 | begin |
| 336 | 336 | dec(depth); |
| 337 | 337 | if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1; |
| 338 | +end; |
| 339 | + |
| 340 | +{ The value i into register r, from wherever it is: a constant or an |
| 341 | + address is made there, a variable's register or the value's own |
| 342 | + register copied, a spilled value loaded. Unlike Loaded, it leaves the |
| 343 | + value where it was, so r is its only new copy. } |
| 344 | +procedure ValueTo(i, r: longint); |
| 345 | +begin |
| 346 | + case stack[i].loc of |
| 347 | + lConst: MovRI(r, stack[i].val); |
| 348 | + lHeld: MovRR(r, stack[i].val); |
| 349 | + lAdr: Lea(r, AdrMem(i, 0)); |
| 350 | + lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); |
| 351 | + else Load(r, Mem(RBP, stack[i].val), 8, false); |
| 352 | + end; |
| 338 | 353 | end; |
| 339 | 354 | |
| 355 | +{ The value on top, popped into register r } |
| 356 | +procedure PopTo(r: longint); |
| 357 | +begin |
| 358 | + ValueTo(depth - 1, r); |
| 359 | + Drop; |
| 360 | +end; |
| 361 | + |
| 340 | 362 | { Whether the value on top is a constant, and if so, which } |
| 341 | 363 | function TopConst(var v: int64): boolean; |
| 342 | 364 | begin |
| | @@ -493,10 +515,8 @@ |
| 493 | 515 | size bytes to off bytes past the destination. } |
| 494 | 516 | procedure CopyBlock(size, off: int64); |
| 495 | 517 | begin |
| 496 | | - MovRR(RSI, Top); |
| 497 | | - Drop; |
| 498 | | - MovRR(RDI, Top); |
| 499 | | - Drop; |
| 518 | + PopTo(RSI); |
| 519 | + PopTo(RDI); |
| 500 | 520 | if off <> 0 then Lea(RDI, Mem(RDI, off)); |
| 501 | 521 | CopyBytes(size); |
| 502 | 522 | end; |
| | @@ -504,8 +524,7 @@ |
| 504 | 524 | { Pops a source's address, and copies size bytes to a. } |
| 505 | 525 | procedure CopyTo(const a: TAdr; size: int64); |
| 506 | 526 | begin |
| 507 | | - MovRR(RSI, Top); |
| 508 | | - Drop; |
| 527 | + PopTo(RSI); |
| 509 | 528 | Lea(RDI, Place(a, RDI)); |
| 510 | 529 | CopyBytes(size); |
| 511 | 530 | end; |
| | @@ -796,8 +815,7 @@ |
| 796 | 815 | procedure ErrorText(tmp: int64); |
| 797 | 816 | var L: longint; |
| 798 | 817 | begin |
| 799 | | - MovRR(RAX, Top); |
| 800 | | - Drop; |
| 818 | + PopTo(RAX); |
| 801 | 819 | MovRI(RCX, 0); |
| 802 | 820 | L := NoLabel; |
| 803 | 821 | TestRR(RAX, RAX); |
| | @@ -861,8 +879,7 @@ |
| 861 | 879 | var r, n: longint; |
| 862 | 880 | begin |
| 863 | 881 | if which = bCas then begin n := Top; Drop end; |
| 864 | | - MovRR(RAX, Top); |
| 865 | | - Drop; |
| 882 | + PopTo(RAX); |
| 866 | 883 | r := Top; |
| 867 | 884 | case which of |
| 868 | 885 | bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end; |
| | @@ -1059,21 +1076,28 @@ |
| 1059 | 1076 | end; |
| 1060 | 1077 | |
| 1061 | 1078 | { Pops an index, and the address of the slice at a if it is on the |
| 1062 | | - stack, and pushes the address of its element of size bytes. } |
| 1079 | + stack, and pushes the address of its element of size bytes: the |
| 1080 | + index, checked against the length, times size, plus the pointer. |
| 1081 | + It is worked out in the index's own register, which then holds the |
| 1082 | + address; but if the slice's address is on the stack, under the |
| 1083 | + index, the element's register is the one that holds that, still |
| 1084 | + needed, so the work is done in rax. } |
| 1063 | 1085 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 1064 | | -var m, ml: TMem; |
| 1086 | +var m, ml: TMem; r: longint; inRax: boolean; |
| 1065 | 1087 | begin |
| 1066 | | - MovRR(RAX, Top); |
| 1067 | | - Drop; |
| 1088 | + inRax := a.base = bStack; |
| 1089 | + if inRax then begin r := RAX; PopTo(RAX) end |
| 1090 | + else begin r := Top; Drop end; |
| 1068 | 1091 | m := Place(a, RCX); |
| 1069 | 1092 | ml := m; ml.disp := ml.disp + 8; |
| 1070 | 1093 | if checked then begin |
| 1071 | | - AluM(aluCmp, RAX, ml); |
| 1072 | | - Check(ccB, 0, p, RAX, ml, 0, -1); |
| 1094 | + AluM(aluCmp, r, ml); |
| 1095 | + Check(ccB, 0, p, r, ml, 0, -1); |
| 1073 | 1096 | end; |
| 1074 | | - if size <> 1 then ImulI(RAX, size); |
| 1075 | | - AluM(aluAdd, RAX, m); |
| 1076 | | - MovRR(Target, RAX); |
| 1097 | + if size <> 1 then ImulI(r, size); |
| 1098 | + AluM(aluAdd, r, m); |
| 1099 | + if inRax then MovRR(Target, RAX) |
| 1100 | + else Claim(depth); { the index's register again, now for the address } |
| 1077 | 1101 | Computed(kWord); |
| 1078 | 1102 | end; |
| 1079 | 1103 | |
| | @@ -1083,10 +1107,8 @@ |
| 1083 | 1107 | procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); |
| 1084 | 1108 | var m, ml: TMem; |
| 1085 | 1109 | begin |
| 1086 | | - MovRR(RDX, Top); |
| 1087 | | - Drop; |
| 1088 | | - MovRR(RCX, Top); |
| 1089 | | - Drop; |
| 1110 | + PopTo(RDX); |
| 1111 | + PopTo(RCX); |
| 1090 | 1112 | m := Place(a, RDI); |
| 1091 | 1113 | ml := m; ml.disp := ml.disp + 8; |
| 1092 | 1114 | if checked then begin |
| | @@ -1142,12 +1164,9 @@ |
| 1142 | 1164 | bounds can't be known, but it can't run backwards. } |
| 1143 | 1165 | procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); |
| 1144 | 1166 | begin |
| 1145 | | - MovRR(RDX, Top); |
| 1146 | | - Drop; |
| 1147 | | - MovRR(RCX, Top); |
| 1148 | | - Drop; |
| 1149 | | - MovRR(RDI, Top); |
| 1150 | | - Drop; |
| 1167 | + PopTo(RDX); |
| 1168 | + PopTo(RCX); |
| 1169 | + PopTo(RDI); |
| 1151 | 1170 | if checked then begin |
| 1152 | 1171 | Alu(aluCmp, RCX, RDX); |
| 1153 | 1172 | Check(ccLE, 3, p, RCX, NoMem, 0, -1); |
| | @@ -1165,17 +1184,6 @@ |
| 1165 | 1184 | { The call c, its arguments already in their frame slots: they go to |
| 1166 | 1185 | registers or the stack, the procedure is called, and its result, if |
| 1167 | 1186 | any, is pushed on the value stack. } |
| 1168 | | -{ Argument value i, as it is on the stack, into register r } |
| 1169 | | -procedure ArgTo(i, r: longint); |
| 1170 | | -begin |
| 1171 | | - case stack[i].loc of |
| 1172 | | - lConst: MovRI(r, stack[i].val); |
| 1173 | | - lHeld: MovRR(r, stack[i].val); |
| 1174 | | - lAdr: Lea(r, AdrMem(i, 0)); |
| 1175 | | - lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); |
| 1176 | | - else Load(r, Mem(RBP, stack[i].val), 8, false); |
| 1177 | | - end; |
| 1178 | | -end; |
| 1179 | 1187 | |
| 1180 | 1188 | procedure Call(var c: TCall); |
| 1181 | 1189 | var |
| | @@ -1189,7 +1197,7 @@ |
| 1189 | 1197 | res := c.sig^.base; |
| 1190 | 1198 | c.ret := 0; |
| 1191 | 1199 | ni := 0; nf := 0; |
| 1192 | | - if (res <> nil) and IsAggregate(res) then begin |
| 1200 | + if (res <> nil) and (BehindPointer(res) or (res^.form = fSlice)) then begin |
| 1193 | 1201 | c.ret := AllocTemp(res^.size, res^.align); |
| 1194 | 1202 | if BehindPointer(res) then ni := 1; |
| 1195 | 1203 | end; |
| | @@ -1213,7 +1221,7 @@ |
| 1213 | 1221 | if pad <> 0 then AluI(aluSub, RSP, 8); |
| 1214 | 1222 | for i := c.n - 1 downto 0 do |
| 1215 | 1223 | if onStack[i] then begin |
| 1216 | | - ArgTo(first + i, RAX); |
| 1224 | + ValueTo(first + i, RAX); |
| 1217 | 1225 | Push(RAX); |
| 1218 | 1226 | end; |
| 1219 | 1227 | ni := 0; nf := 0; |
| | @@ -1225,14 +1233,14 @@ |
| 1225 | 1233 | if not onStack[i] then |
| 1226 | 1234 | if c.isF[i] then begin |
| 1227 | 1235 | if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val)) |
| 1228 | | - else begin ArgTo(first + i, RAX); MovqXR(nf, RAX) end; |
| 1236 | + else begin ValueTo(first + i, RAX); MovqXR(nf, RAX) end; |
| 1229 | 1237 | inc(nf); |
| 1230 | 1238 | end else begin |
| 1231 | | - if ni < 4 then ArgTo(first + i, IntRegs[ni]) else late[ni] := first + i; |
| 1239 | + if ni < 4 then ValueTo(first + i, IntRegs[ni]) else late[ni] := first + i; |
| 1232 | 1240 | inc(ni); |
| 1233 | 1241 | end; |
| 1234 | | - if late[4] >= 0 then ArgTo(late[4], RAX); |
| 1235 | | - if late[5] >= 0 then ArgTo(late[5], R9); |
| 1242 | + if late[4] >= 0 then ValueTo(late[4], RAX); |
| 1243 | + if late[5] >= 0 then ValueTo(late[5], R9); |
| 1236 | 1244 | if late[4] >= 0 then MovRR(R8, RAX); |
| 1237 | 1245 | for i := 1 to c.n do Drop; |
| 1238 | 1246 | { a procedure taking ... is told in al how many xmm registers it got } |
| | @@ -1246,15 +1254,17 @@ |
| 1246 | 1254 | CallR(R11); |
| 1247 | 1255 | end; |
| 1248 | 1256 | if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); |
| 1249 | | - if (c.ret <> 0) and InTwoRegs(res) then |
| 1250 | | - if res^.form = fSlice then begin |
| 1251 | | - Store(Mem(RBP, c.ret), RAX, 8); |
| 1252 | | - Store(Mem(RBP, c.ret + 8), RDX, 8); |
| 1253 | | - end else begin |
| 1254 | | - Store(Mem(RBP, c.ret + res^.fields^.val), RAX, res^.fields^.typ^.size); |
| 1255 | | - Store(Mem(RBP, c.ret + res^.fields^.next^.val), RDX, res^.fields^.next^.typ^.size); |
| 1256 | | - end; |
| 1257 | | - if (res <> nil) and (c.ret = 0) then begin |
| 1257 | + { the result: a slice in rax and rdx goes to the caller's temporary, |
| 1258 | + two results on the stack, the first under the second, and a single |
| 1259 | + value on top } |
| 1260 | + if res = nil then |
| 1261 | + else if res^.form = fSlice then begin |
| 1262 | + Store(Mem(RBP, c.ret), RAX, 8); |
| 1263 | + Store(Mem(RBP, c.ret + 8), RDX, 8); |
| 1264 | + end else if InTwoRegs(res) then begin |
| 1265 | + MovRR(Target, RAX); Computed(kWord); |
| 1266 | + MovRR(Target, RDX); Computed(kWord); |
| 1267 | + end else if c.ret = 0 then begin |
| 1258 | 1268 | r := Target; |
| 1259 | 1269 | if res^.form = fReal then MovqRX(r, 0) |
| 1260 | 1270 | else begin |
| | @@ -1465,8 +1475,7 @@ |
| 1465 | 1475 | bytes into it. } |
| 1466 | 1476 | procedure CopyResult(off, size: int64); |
| 1467 | 1477 | begin |
| 1468 | | - MovRR(RSI, Top); |
| 1469 | | - Drop; |
| 1478 | + PopTo(RSI); |
| 1470 | 1479 | Load(RDI, Mem(RBP, retSlot), 8, false); |
| 1471 | 1480 | if off <> 0 then Lea(RDI, Mem(RDI, off)); |
| 1472 | 1481 | CopyBytes(size); |
| | @@ -1480,11 +1489,11 @@ |
| 1480 | 1489 | if resType <> nil then |
| 1481 | 1490 | if BehindPointer(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) |
| 1482 | 1491 | else if InTwoRegs(resType) then begin |
| 1483 | | - MovRR(RDX, Top); Drop; |
| 1484 | | - MovRR(RAX, Top); Drop; |
| 1492 | + PopTo(RDX); |
| 1493 | + PopTo(RAX); |
| 1485 | 1494 | end else begin |
| 1486 | | - if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top); |
| 1487 | | - Drop; |
| 1495 | + if resType^.form = fReal then begin MovqXR(0, Top); Drop end |
| 1496 | + else PopTo(RAX); |
| 1488 | 1497 | end; |
| 1489 | 1498 | Jmp(exitL); |
| 1490 | 1499 | end; |