A scale is one factor: [rdi + rsi * 4 + 16] assembles The scale was read as a whole constant expression, which took in the displacement after it, 4 + 16, and the operand was refused. The parser now hands the assembler AsmConst, which reads a single factor for a scale, a number or a constant.
| @@ -552,7 +552,9 @@ | ||
| 552 | 552 | The assembler, `Assemble`, reads a code procedure's instructions with |
| 553 | 553 | the scanner, Intel's syntax as NASM writes it, and encodes them with |
| 554 | 554 | `OpM` and `OpR`: `Operand` reads a register, a memory operand or a |
| 555 | -constant (by the parser's `ConstExpr`, handed in), and a few tables | |
| 555 | +constant (by the parser's `AsmConst`, handed in: a whole expression, | |
| 556 | +or for a scale a single factor, so that `[rdi + rsi * 4 + 16]` doesn't | |
| 557 | +read `4 + 16` as the scale), and a few tables | |
| 556 | 558 | give the operations of a kind their opcodes (`Alus`, `Unaries`, |
| 557 | 559 | `Shifts`, `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations |
| 558 | 560 | on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside |
| @@ -2123,6 +2123,18 @@ | ||
| 2123 | 2123 | CloseScope; |
| 2124 | 2124 | end; |
| 2125 | 2125 | |
| 2126 | +{ An integer constant in a code procedure, for the assembler: a whole | |
| 2127 | + expression, or a single factor where an expression would read on | |
| 2128 | + too far } | |
| 2129 | +function AsmConst(whole: boolean): int64; | |
| 2130 | +var x: TItem; | |
| 2131 | +begin | |
| 2132 | + if whole then exit(ConstExpr); | |
| 2133 | + Factor(x, nil); | |
| 2134 | + if not IsInt(x.typ) then MarkAt(x.pos, 'expected an integer constant, found ' + What(x)); | |
| 2135 | + AsmConst := ConstValue(x); | |
| 2136 | +end; | |
| 2137 | + | |
| 2126 | 2138 | procedure CodeBody(p: PObj; sig: PType; const at: TPos); |
| 2127 | 2139 | var q: PObj; ni, nf: longint; |
| 2128 | 2140 | begin |
| @@ -2143,7 +2155,7 @@ | ||
| 2143 | 2155 | or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then |
| 2144 | 2156 | MarkAt(at, 'a code procedure returns two results only in rax and rdx'); |
| 2145 | 2157 | end else if not IsScalar(sig^.base) then MarkAt(at, 'a code procedure returns a single value'); |
| 2146 | - CodeProc(p^.chunk, @ConstExpr); | |
| 2158 | + CodeProc(p^.chunk, @AsmConst); | |
| 2147 | 2159 | Expect(sEnd); |
| 2148 | 2160 | end; |
| 2149 | 2161 |
| @@ -111,7 +111,9 @@ | ||
| 111 | 111 | function JoinLinks(L1, L2: longint): longint; |
| 112 | 112 | |
| 113 | 113 | type |
| 114 | - TConstFn = function: int64; | |
| 114 | + { The parser's reader of an integer constant in a code procedure: a | |
| 115 | + whole expression, or, when whole is false, a single factor } | |
| 116 | + TConstFn = function(whole: boolean): int64; | |
| 115 | 117 | |
| 116 | 118 | const |
| 117 | 119 | RegNames: array[0..3, 0..15] of string[5] = ( |
| @@ -846,10 +848,17 @@ | ||
| 846 | 848 | repeat |
| 847 | 849 | if IsReg(AsmWord, r, size) and (size = 8) then begin |
| 848 | 850 | Next; |
| 849 | - if tok = sTimes then begin Next; o.m.index := r; o.m.scale := constExpr() end | |
| 851 | + { the scale is a single number or constant: a whole expression | |
| 852 | + would take in the displacement after it, as 4 + 16 in | |
| 853 | + [rdi + rsi * 4 + 16] } | |
| 854 | + if tok = sTimes then begin | |
| 855 | + Next; | |
| 856 | + o.m.index := r; | |
| 857 | + o.m.scale := constExpr(false); | |
| 858 | + end | |
| 850 | 859 | else if o.m.base = NoBase then o.m.base := r |
| 851 | 860 | else begin o.m.index := r; o.m.scale := 1 end; |
| 852 | - end else o.m.disp := o.m.disp + constExpr(); | |
| 861 | + end else o.m.disp := o.m.disp + constExpr(true); | |
| 853 | 862 | if not (tok in [sPlus, sMinus]) then break; |
| 854 | 863 | SaveScan(st); |
| 855 | 864 | Next; |
| @@ -863,7 +872,7 @@ | ||
| 863 | 872 | Next; |
| 864 | 873 | end else begin |
| 865 | 874 | o.kind := oImm; |
| 866 | - o.v := constExpr(); | |
| 875 | + o.v := constExpr(true); | |
| 867 | 876 | end; |
| 868 | 877 | end; |
| 869 | 878 | |
| @@ -976,7 +985,7 @@ | ||
| 976 | 985 | if w = 'db' then |
| 977 | 986 | repeat |
| 978 | 987 | start := TokPos; |
| 979 | - v := constExpr(); | |
| 988 | + v := constExpr(true); | |
| 980 | 989 | if (v < -128) or (v > 255) then MarkAt(SpanFrom(start), 'expected a byte from 0 to 255, found ' + IntStr(v)); |
| 981 | 990 | Put1(v); |
| 982 | 991 | if tok <> sComma then break; |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | 35 |
| 2 | -4321 0 | |
| 2 | +4321 0 4001 | |
| 3 | 3 | 98 azc |
| 4 | 4 | 142 6 |
| 5 | 5 | 3 9 below |
| @@ -3,7 +3,9 @@ | ||
| 3 | 3 | each checked by what it computes *) |
| 4 | 4 | import Out; |
| 5 | 5 | |
| 6 | -const Seven = 7; | |
| 6 | +const | |
| 7 | + Seven = 7; | |
| 8 | + Eight = 8; | |
| 7 | 9 | |
| 8 | 10 | (* a * 10 + b - 7, by lea, shifts and adds *) |
| 9 | 11 | proc tens(a, b: I64): I64; |
| @@ -30,6 +32,15 @@ | ||
| 30 | 32 | ret; |
| 31 | 33 | end sum; |
| 32 | 34 | |
| 35 | +(* p[i + 2] + p[i - 1]: a scale, by number and by constant, then a | |
| 36 | + displacement *) | |
| 37 | +proc around(p: ^I64; i: INT): I64; | |
| 38 | +code | |
| 39 | + mov rax, [rdi + rsi * 8 + 16]; | |
| 40 | + add rax, [rdi + rsi * Eight - 8]; | |
| 41 | + ret; | |
| 42 | +end around; | |
| 43 | + | |
| 33 | 44 | (* the byte at p + i, zero-extended, and the byte there set to v *) |
| 34 | 45 | proc swapByte(p: ^U8; i: INT; v: U8): I64; |
| 35 | 46 | code |
| @@ -114,7 +125,8 @@ | ||
| 114 | 125 | begin |
| 115 | 126 | Out.int(tens(4, 2)); Out.ln; |
| 116 | 127 | a := [1, 20, 300, 4000]; |
| 117 | - Out.int(sum(ADR(a[0]), 4)); Out.str(" "); Out.int(sum(ADR(a[0]), 0)); Out.ln; | |
| 128 | + Out.int(sum(ADR(a[0]), 4)); Out.str(" "); Out.int(sum(ADR(a[0]), 0)); Out.str(" "); | |
| 129 | + Out.int(around(ADR(a[0]), 1)); Out.ln; | |
| 118 | 130 | s := "abc"; |
| 119 | 131 | Out.int(swapByte(ADR(s[0]), 1, 'z')); Out.str(" "); Out.str(s[0..3]); Out.ln; |
| 120 | 132 | q, r := udivmod(1000, 7); |