| | @@ -94,6 +94,9 @@ |
| 94 | 94 | |
| 95 | 95 | var |
| 96 | 96 | rtFail, argcChunk, argvChunk: longint; |
| 97 | + { the stubs that Check's code calls, one for each kind of failure, |
| 98 | + made by EntryPoint for those used } |
| 99 | + failStubs: array[0..5] of longint = (-1, -1, -1, -1, -1, -1); |
| 97 | 100 | depth: longint; { the values on the stack } |
| 98 | 101 | pushed: longint; { quadwords pushed on the machine's stack } |
| 99 | 102 | codeChunk: longint = -1; { the procedure or body being compiled, or -1 } |
| | @@ -194,6 +197,7 @@ |
| 194 | 197 | end; |
| 195 | 198 | |
| 196 | 199 | const |
| 200 | + StubNames: array[0..5] of string[6] = ('check0', 'check1', 'check2', 'check3', 'check4', 'check5'); |
| 197 | 201 | { the registers of the stack: the value at depth d is in Regs[d mod |
| 198 | 202 | NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi |
| 199 | 203 | and rdi, which copying needs, aren't among them. } |
| | @@ -220,6 +224,13 @@ |
| 220 | 224 | regMask: longint; { the registers of variables, as bits of VarRegs } |
| 221 | 225 | saveAt: int64; { where they are saved, a quadword each } |
| 222 | 226 | frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations } |
| 227 | + { the checks of the procedure, whose failures EndFails makes } |
| 228 | + fails: array[0..4095] of record |
| 229 | + L, what, ra, name, at: longint; |
| 230 | + m: TMem; |
| 231 | + n: int64; |
| 232 | + end; |
| 233 | + numFails: longint; |
| 223 | 234 | |
| 224 | 235 | function KindOf(t: PType): TKind; |
| 225 | 236 | begin |
| | @@ -980,27 +991,42 @@ |
| 980 | 991 | a..b (1), each for a length len, a position a of the enumeration |
| 981 | 992 | called name (2), or a slice of a pointer a..b that runs backwards |
| 982 | 993 | (3). a is in register ra, b in RDX; len is in memory at m, or n if |
| 983 | | - m.base is NoBase. The failing code has p for its place, and the code |
| 984 | | - after it the place before it again; it never returns, and may use |
| 985 | | - any register once it has read what it needs. } |
| 994 | + m.base is NoBase. The failing code is out of the way, at the |
| 995 | + procedure's end (EndFails), so that a check that holds costs a jump |
| 996 | + not taken; it has p for its place, never returns, and may use any |
| 997 | + register once it has read what it needs. } |
| 986 | 998 | procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); |
| 987 | | -var L, line, col: longint; before: TLine; |
| 999 | +begin |
| 1000 | + if numFails > High(fails) then Mark('more than ' + IntStr(High(fails) + 1) + ' checks in one procedure'); |
| 1001 | + fails[numFails].L := NoLabel; |
| 1002 | + Jcc(cc xor 1, fails[numFails].L); { x86's conditions come in pairs, the other one its last bit flipped } |
| 1003 | + fails[numFails].what := what; |
| 1004 | + fails[numFails].ra := ra; |
| 1005 | + fails[numFails].m := m; |
| 1006 | + fails[numFails].n := n; |
| 1007 | + fails[numFails].name := name; |
| 1008 | + fails[numFails].at := p.a; |
| 1009 | + inc(numFails); |
| 1010 | +end; |
| 1011 | + |
| 1012 | +{ The failures of the procedure's checks: each gives Rt.fail its values |
| 1013 | + and the place of the offending expression, through a stub for its |
| 1014 | + kind, which takes the place from where it was called } |
| 1015 | +procedure EndFails; |
| 1016 | +var i, line, col: longint; |
| 988 | 1017 | begin |
| 989 | | - L := NoLabel; |
| 990 | | - Jcc(cc, L); |
| 991 | | - before := lines^[numLines - 1]; |
| 992 | | - LineCol(p.a, line, col); |
| 993 | | - NoteLine(line, col); |
| 994 | | - MovRR(RSI, ra); |
| 995 | | - if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); |
| 996 | | - Lea(R8, Here); |
| 997 | | - MovRI(RDI, what); |
| 998 | | - if name >= 0 then Lea(R9, MemRip(name, 0)); |
| 999 | | - AluI(aluAnd, RSP, -16); |
| 1000 | | - CallChunk(rtFail); |
| 1001 | | - Ud2; |
| 1002 | | - FixLink(L); |
| 1003 | | - NoteLine(before.line, before.col); |
| 1018 | + for i := 0 to numFails - 1 do |
| 1019 | + with fails[i] do begin |
| 1020 | + FixLink(L); |
| 1021 | + LineCol(at, line, col); |
| 1022 | + NoteLine(line, col); |
| 1023 | + if ra <> RSI then MovRR(RSI, ra); |
| 1024 | + if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); |
| 1025 | + if name >= 0 then Lea(R9, MemRip(name, 0)); |
| 1026 | + if failStubs[what] < 0 then failStubs[what] := NewChunk(secText, Intern('Rt', 2), Intern(@StubNames[what][1], 6)); |
| 1027 | + CallChunk(failStubs[what]); |
| 1028 | + end; |
| 1029 | + numFails := 0; |
| 1004 | 1030 | end; |
| 1005 | 1031 | |
| 1006 | 1032 | { For Check: a length known here, not in memory } |
| | @@ -1340,6 +1366,7 @@ |
| 1340 | 1366 | procedure Redo(const m: TCodeMark); |
| 1341 | 1367 | begin |
| 1342 | 1368 | pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said); |
| 1369 | + numFails := 0; |
| 1343 | 1370 | frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; |
| 1344 | 1371 | exitL := NoLabel; |
| 1345 | 1372 | end; |
| | @@ -1461,6 +1488,7 @@ |
| 1461 | 1488 | end; |
| 1462 | 1489 | Leave; |
| 1463 | 1490 | Ret; |
| 1491 | + EndFails; |
| 1464 | 1492 | n := AlignUp(frameMax, 16); |
| 1465 | 1493 | if n < 16 then n := 16; |
| 1466 | 1494 | if n > high(longint) then Mark('frame too big'); |
| | @@ -1503,7 +1531,7 @@ |
| 1503 | 1531 | nil, nil, rtld_fini, stack_end), with rsp aligned; its main first |
| 1504 | 1532 | gives Rt C's exit, in cExit. } |
| 1505 | 1533 | function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint; |
| 1506 | | -var entry, start: longint; m: PModule; |
| 1534 | +var entry, start, i: longint; m: PModule; |
| 1507 | 1535 | begin |
| 1508 | 1536 | entry := NewChunk(secText, -1, Intern('_start', 6)); |
| 1509 | 1537 | if dynamic then begin |
| | @@ -1548,6 +1576,19 @@ |
| 1548 | 1576 | CallChunk(exitProc); |
| 1549 | 1577 | Ud2; |
| 1550 | 1578 | EndChunk(start); |
| 1579 | + { Rt.fail, for the failures of each kind: the place is where the |
| 1580 | + check's call is, a byte before where it would return to } |
| 1581 | + for i := 0 to High(failStubs) do |
| 1582 | + if failStubs[i] >= 0 then begin |
| 1583 | + BeginChunk(failStubs[i]); |
| 1584 | + MovRI(RDI, i); |
| 1585 | + Load(R8, Mem(RSP, 0), 8, false); |
| 1586 | + AluI(aluSub, R8, 1); |
| 1587 | + AluI(aluAnd, RSP, -16); |
| 1588 | + CallChunk(rtFail); |
| 1589 | + Ud2; |
| 1590 | + EndChunk(failStubs[i]); |
| 1591 | + end; |
| 1551 | 1592 | EntryPoint := entry; |
| 1552 | 1593 | end; |
| 1553 | 1594 | |