| | @@ -16,12 +16,11 @@ |
| 16 | 16 | needs it in a register, so that it can be an instruction's operand. |
| 17 | 17 | |
| 18 | 18 | Calls use the System V convention. Arguments are computed left to |
| 19 | | - right into frame slots, then loaded into registers just before the |
| 20 | | - call, so that computing one argument can't disturb another; a |
| 21 | | - constant or an address, which nothing can disturb, goes straight to |
| 22 | | - its register. A record, |
| 23 | | - array, slice or variant passed by value goes as the address of a copy |
| 24 | | - the caller makes; one returned goes through a hidden pointer, as C's |
| 19 | + right onto the stack, and moved into their registers just before the |
| 20 | + call: a call in a later argument spills the earlier ones, as any call |
| 21 | + spills the values under it, so that it can't disturb them. A slice goes as its two words, as System V passes a |
| 22 | + 16-byte struct of two integers; a record, array or variant passed by |
| 23 | + value goes as the address of a copy the caller makes; one returned goes through a hidden pointer, as C's |
| 25 | 24 | structs do, and so do several results. |
| 26 | 25 | |
| 27 | 26 | A procedure's frame holds its parameters, its variables, and the |
| | @@ -62,10 +61,6 @@ |
| 62 | 61 | chunk: longint; { bGlobal: the chunk; bSlot: the frame slot; bReg: the register } |
| 63 | 62 | off: int64; { added to the address } |
| 64 | 63 | end; |
| 65 | | - |
| 66 | | - { How an argument waits for its call: in a frame slot, or noted, as a |
| 67 | | - constant or as the address of the frame or a chunk plus an offset } |
| 68 | | - TArgHow = (argSlot, argConst, argAdr, argHeld); |
| 69 | 64 | |
| 70 | 65 | { The code made so far, to go back to for a second try at a body } |
| 71 | 66 | TCodeMark = record pc, fixups, lines, said: longint; end; |
| | @@ -75,10 +70,8 @@ |
| 75 | 70 | param: PObj; { the parameter the next argument is for } |
| 76 | 71 | obj: PObj; { the procedure called, if it has a name } |
| 77 | 72 | n: longint; { the arguments } |
| 78 | | - slots: array[0..63] of int64; { each one's frame slot, constant or offset } |
| 79 | | - how: array[0..63] of TArgHow; |
| 80 | | - adrChunk: array[0..63] of longint; { argAdr: the chunk, or -1 for the frame } |
| 81 | 73 | isF: array[0..63] of boolean; { ... goes in an xmm register } |
| 74 | + pair: array[0..63] of boolean; { ... is a slice's first word, the next its second } |
| 82 | 75 | target: int64; { the frame slot of a procedure value, if chunk < 0 } |
| 83 | 76 | chunk: longint; { else the procedure's chunk } |
| 84 | 77 | code: boolean; { a code procedure: two results come in rax and rdx } |
| | @@ -152,7 +145,6 @@ |
| 152 | 145 | procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); |
| 153 | 146 | procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); |
| 154 | 147 | |
| 155 | | -procedure PopArg(var c: TCall); |
| 156 | 148 | procedure Call(var c: TCall); |
| 157 | 149 | |
| 158 | 150 | function AllocTemp(size, align: int64): int64; |
| | @@ -1172,46 +1164,27 @@ |
| 1172 | 1164 | { The call c, its arguments already in their frame slots: they go to |
| 1173 | 1165 | registers or the stack, the procedure is called, and its result, if |
| 1174 | 1166 | any, is pushed on the value stack. } |
| 1175 | | -{ The value on top is argument c.n of c, which waits for the call: a |
| 1176 | | - constant or an address is noted, a real or anything else goes to a |
| 1177 | | - frame slot. } |
| 1178 | | -procedure PopArg(var c: TCall); |
| 1179 | | -var i: longint; |
| 1167 | +{ Argument value i, as it is on the stack, into register r } |
| 1168 | +procedure ArgTo(i, r: longint); |
| 1180 | 1169 | begin |
| 1181 | | - i := depth - 1; |
| 1182 | | - c.how[c.n] := argSlot; |
| 1183 | | - if not c.isF[c.n] and (stack[i].loc = lConst) then c.how[c.n] := argConst |
| 1184 | | - else if stack[i].loc = lAdr then c.how[c.n] := argAdr |
| 1185 | | - else if stack[i].loc = lHeld then c.how[c.n] := argHeld; |
| 1186 | | - if c.how[c.n] = argSlot then begin |
| 1187 | | - c.slots[c.n] := AllocTemp(8, 8); |
| 1188 | | - Store(Mem(RBP, c.slots[c.n]), Top, 8); |
| 1189 | | - end else begin |
| 1190 | | - c.slots[c.n] := stack[i].val; |
| 1191 | | - c.adrChunk[c.n] := stack[i].chunk; |
| 1170 | + case stack[i].loc of |
| 1171 | + lConst: MovRI(r, stack[i].val); |
| 1172 | + lHeld: MovRR(r, stack[i].val); |
| 1173 | + lAdr: Lea(r, AdrMem(i, 0)); |
| 1174 | + lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); |
| 1175 | + else Load(r, Mem(RBP, stack[i].val), 8, false); |
| 1192 | 1176 | end; |
| 1193 | | - Drop; |
| 1194 | 1177 | end; |
| 1195 | 1178 | |
| 1196 | | -{ Argument i of c into register r } |
| 1197 | | -procedure LoadArg(const c: TCall; i, r: longint); |
| 1198 | | -begin |
| 1199 | | - case c.how[i] of |
| 1200 | | - argConst: MovRI(r, c.slots[i]); |
| 1201 | | - argHeld: MovRR(r, c.slots[i]); |
| 1202 | | - argAdr: |
| 1203 | | - if c.adrChunk[i] < 0 then Lea(r, Mem(RBP, c.slots[i])) |
| 1204 | | - else Lea(r, MemRip(c.adrChunk[i], c.slots[i])); |
| 1205 | | - else Load(r, Mem(RBP, c.slots[i]), 8, false); |
| 1206 | | - end; |
| 1207 | | -end; |
| 1208 | | - |
| 1209 | 1179 | procedure Call(var c: TCall); |
| 1210 | 1180 | var |
| 1211 | | - res: PType; i, ni, nf, k, pad, r: longint; |
| 1212 | | - onStack: array[0..63] of boolean; |
| 1181 | + res: PType; i, ni, nf, k, pad, r, first: longint; |
| 1182 | + onStack: array[0..63] of boolean; late: array[4..5] of longint; |
| 1213 | 1183 | begin |
| 1214 | | - SpillBelow(depth); { the registers in use, saved around the call } |
| 1184 | + { the arguments are the c.n values on top, where computing them left |
| 1185 | + them; the registers in use under them are saved around the call } |
| 1186 | + first := depth - c.n; |
| 1187 | + SpillBelow(first); |
| 1215 | 1188 | res := c.sig^.base; |
| 1216 | 1189 | c.ret := 0; |
| 1217 | 1190 | ni := 0; nf := 0; |
| | @@ -1224,7 +1197,13 @@ |
| 1224 | 1197 | k := 0; |
| 1225 | 1198 | for i := 0 to c.n - 1 do begin |
| 1226 | 1199 | if c.isF[i] then begin onStack[i] := nf >= 8; if nf < 8 then inc(nf) end |
| 1227 | | - else begin onStack[i] := ni >= 6; if ni < 6 then inc(ni) end; |
| 1200 | + else begin |
| 1201 | + { a slice's words both go in registers, or both on the stack } |
| 1202 | + if (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1] |
| 1203 | + else if c.pair[i] then onStack[i] := ni > 4 |
| 1204 | + else onStack[i] := ni >= 6; |
| 1205 | + if not onStack[i] then inc(ni); |
| 1206 | + end; |
| 1228 | 1207 | if onStack[i] then inc(k); |
| 1229 | 1208 | end; |
| 1230 | 1209 | { System V wants rsp a multiple of 16 at the call: a quadword of |
| | @@ -1233,15 +1212,28 @@ |
| 1233 | 1212 | if pad <> 0 then AluI(aluSub, RSP, 8); |
| 1234 | 1213 | for i := c.n - 1 downto 0 do |
| 1235 | 1214 | if onStack[i] then begin |
| 1236 | | - LoadArg(c, i, RAX); |
| 1215 | + ArgTo(first + i, RAX); |
| 1237 | 1216 | Push(RAX); |
| 1238 | 1217 | end; |
| 1239 | 1218 | ni := 0; nf := 0; |
| 1219 | + late[4] := -1; late[5] := -1; |
| 1240 | 1220 | if (c.ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; |
| 1221 | + { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as |
| 1222 | + they may hold arguments still } |
| 1241 | 1223 | for i := 0 to c.n - 1 do |
| 1242 | 1224 | if not onStack[i] then |
| 1243 | | - if c.isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end |
| 1244 | | - else begin LoadArg(c, i, IntRegs[ni]); inc(ni) end; |
| 1225 | + if c.isF[i] then begin |
| 1226 | + if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val)) |
| 1227 | + else begin ArgTo(first + i, RAX); MovqXR(nf, RAX) end; |
| 1228 | + inc(nf); |
| 1229 | + end else begin |
| 1230 | + if ni < 4 then ArgTo(first + i, IntRegs[ni]) else late[ni] := first + i; |
| 1231 | + inc(ni); |
| 1232 | + end; |
| 1233 | + if late[4] >= 0 then ArgTo(late[4], RAX); |
| 1234 | + if late[5] >= 0 then ArgTo(late[5], R9); |
| 1235 | + if late[4] >= 0 then MovRR(R8, RAX); |
| 1236 | + for i := 1 to c.n do Drop; |
| 1245 | 1237 | { a procedure taking ... is told in al how many xmm registers it got } |
| 1246 | 1238 | if c.sig^.variadic then MovRI(RAX, nf); |
| 1247 | 1239 | if c.ext then CallM(MemRip(c.chunk, 0)) |
| | @@ -1309,7 +1301,7 @@ |
| 1309 | 1301 | pushed it. A var parameter, and a record or array passed by value, |
| 1310 | 1302 | holds an address (ind). } |
| 1311 | 1303 | procedure BeginProc(sig: PType; params: PObj); |
| 1312 | | -var p: PObj; ni, nf: longint; stackOff: int64; f: boolean; |
| 1304 | +var p: PObj; ni, nf, k: longint; stackOff: int64; f: boolean; |
| 1313 | 1305 | begin |
| 1314 | 1306 | frameTop := 0; frameMax := 0; permTop := 0; |
| 1315 | 1307 | ResetStack(0, 0); |
| | @@ -1326,9 +1318,22 @@ |
| 1326 | 1318 | end; |
| 1327 | 1319 | p := params; |
| 1328 | 1320 | while p <> nil do begin |
| 1329 | | - p^.ind := p^.isVar or IsAggregate(p^.typ); |
| 1321 | + p^.ind := p^.isVar or IsAggregate(p^.typ) and (p^.typ^.form <> fSlice); |
| 1330 | 1322 | f := (p^.typ^.form = fReal) and not p^.isVar; |
| 1331 | | - if (f and (nf < 8)) or (not f and (ni < 6)) then begin |
| 1323 | + if (p^.typ^.form = fSlice) and not p^.isVar then begin |
| 1324 | + { its two words, in two registers or both on the stack } |
| 1325 | + if ni <= 4 then begin |
| 1326 | + p^.val := AllocPerm(16, 8); |
| 1327 | + for k := 0 to 1 do begin |
| 1328 | + inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + 8 * k; |
| 1329 | + inSize[numIn] := 8; inObj[numIn] := nil; |
| 1330 | + inc(numIn); inc(ni); |
| 1331 | + end; |
| 1332 | + end else begin |
| 1333 | + p^.val := stackOff; |
| 1334 | + inc(stackOff, 16); |
| 1335 | + end; |
| 1336 | + end else if (f and (nf < 8)) or (not f and (ni < 6)) then begin |
| 1332 | 1337 | p^.val := AllocPerm(8, 8); |
| 1333 | 1338 | if f then begin inReg[numIn] := -1 - nf; inc(nf) end |
| 1334 | 1339 | else begin inReg[numIn] := IntRegs[ni]; inc(ni) end; |