From 7176b227c48d5dbd3947c500dc528704fa02b128 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:12:50 +0100 Subject: [PATCH] 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. --- diff --git a/doc/compiler.md b/doc/compiler.md index 2782e2b..1169d50 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -552,7 +552,9 @@ The assembler, `Assemble`, reads a code procedure's instructions with the scanner, Intel's syntax as NASM writes it, and encodes them with `OpM` and `OpR`: `Operand` reads a register, a memory operand or a -constant (by the parser's `ConstExpr`, handed in), and a few tables +constant (by the parser's `AsmConst`, handed in: a whole expression, +or for a scale a single factor, so that `[rdi + rsi * 4 + 16]` doesn't +read `4 + 16` as the scale), and a few tables give the operations of a kind their opcodes (`Alus`, `Unaries`, `Shifts`, `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside diff --git a/src/parse.pas b/src/parse.pas index 6285b19..e36f5f8 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -2123,6 +2123,18 @@ CloseScope; end; +{ An integer constant in a code procedure, for the assembler: a whole + expression, or a single factor where an expression would read on + too far } +function AsmConst(whole: boolean): int64; +var x: TItem; +begin + if whole then exit(ConstExpr); + Factor(x, nil); + if not IsInt(x.typ) then MarkAt(x.pos, 'expected an integer constant, found ' + What(x)); + AsmConst := ConstValue(x); +end; + procedure CodeBody(p: PObj; sig: PType; const at: TPos); var q: PObj; ni, nf: longint; begin @@ -2143,7 +2155,7 @@ or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then MarkAt(at, 'a code procedure returns two results only in rax and rdx'); end else if not IsScalar(sig^.base) then MarkAt(at, 'a code procedure returns a single value'); - CodeProc(p^.chunk, @ConstExpr); + CodeProc(p^.chunk, @AsmConst); Expect(sEnd); end; diff --git a/src/x64.pas b/src/x64.pas index c6026f4..7d846d7 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -111,7 +111,9 @@ function JoinLinks(L1, L2: longint): longint; type - TConstFn = function: int64; + { The parser's reader of an integer constant in a code procedure: a + whole expression, or, when whole is false, a single factor } + TConstFn = function(whole: boolean): int64; const RegNames: array[0..3, 0..15] of string[5] = ( @@ -846,10 +848,17 @@ repeat if IsReg(AsmWord, r, size) and (size = 8) then begin Next; - if tok = sTimes then begin Next; o.m.index := r; o.m.scale := constExpr() end + { the scale is a single number or constant: a whole expression + would take in the displacement after it, as 4 + 16 in + [rdi + rsi * 4 + 16] } + if tok = sTimes then begin + Next; + o.m.index := r; + o.m.scale := constExpr(false); + end else if o.m.base = NoBase then o.m.base := r else begin o.m.index := r; o.m.scale := 1 end; - end else o.m.disp := o.m.disp + constExpr(); + end else o.m.disp := o.m.disp + constExpr(true); if not (tok in [sPlus, sMinus]) then break; SaveScan(st); Next; @@ -863,7 +872,7 @@ Next; end else begin o.kind := oImm; - o.v := constExpr(); + o.v := constExpr(true); end; end; @@ -976,7 +985,7 @@ if w = 'db' then repeat start := TokPos; - v := constExpr(); + v := constExpr(true); if (v < -128) or (v > 255) then MarkAt(SpanFrom(start), 'expected a byte from 0 to 255, found ' + IntStr(v)); Put1(v); if tok <> sComma then break; diff --git a/test/run/Asm.expect b/test/run/Asm.expect index b2d7498..ff13790 100644 --- a/test/run/Asm.expect +++ b/test/run/Asm.expect @@ -1,5 +1,5 @@ 35 -4321 0 +4321 0 4001 98 azc 142 6 3 9 below diff --git a/test/run/Asm.raz b/test/run/Asm.raz index 1d421be..6942bfc 100644 --- a/test/run/Asm.raz +++ b/test/run/Asm.raz @@ -3,7 +3,9 @@ each checked by what it computes *) import Out; -const Seven = 7; +const + Seven = 7; + Eight = 8; (* a * 10 + b - 7, by lea, shifts and adds *) proc tens(a, b: I64): I64; @@ -30,6 +32,15 @@ ret; end sum; +(* p[i + 2] + p[i - 1]: a scale, by number and by constant, then a + displacement *) +proc around(p: ^I64; i: INT): I64; +code + mov rax, [rdi + rsi * 8 + 16]; + add rax, [rdi + rsi * Eight - 8]; + ret; +end around; + (* the byte at p + i, zero-extended, and the byte there set to v *) proc swapByte(p: ^U8; i: INT; v: U8): I64; code @@ -114,7 +125,8 @@ begin Out.int(tens(4, 2)); Out.ln; a := [1, 20, 300, 4000]; - Out.int(sum(ADR(a[0]), 4)); Out.str(" "); Out.int(sum(ADR(a[0]), 0)); Out.ln; + Out.int(sum(ADR(a[0]), 4)); Out.str(" "); Out.int(sum(ADR(a[0]), 0)); Out.str(" "); + Out.int(around(ADR(a[0]), 1)); Out.ln; s := "abc"; Out.int(swapByte(ADR(s[0]), 1, 'z')); Out.str(" "); Out.str(s[0..3]); Out.ln; q, r := udivmod(1000, 7);