raz / commit 7176b227

commit
7176b227c48d5dbd3947c500dc528704fa02b128
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b1472f5d
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.

patch · log

5 files changed, +45 -10

doc/compiler.md (history) modified +3 -1

@@ -552,7 +552,9 @@
552552 The assembler, `Assemble`, reads a code procedure's instructions with
553553 the scanner, Intel's syntax as NASM writes it, and encodes them with
554554 `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
556558 give the operations of a kind their opcodes (`Alus`, `Unaries`,
557559 `Shifts`, `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations
558560 on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside

src/parse.pas modified +13 -1

@@ -2123,6 +2123,18 @@
21232123 CloseScope;
21242124 end;
21252125
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+
21262138 procedure CodeBody(p: PObj; sig: PType; const at: TPos);
21272139 var q: PObj; ni, nf: longint;
21282140 begin
@@ -2143,7 +2155,7 @@
21432155 or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then
21442156 MarkAt(at, 'a code procedure returns two results only in rax and rdx');
21452157 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);
21472159 Expect(sEnd);
21482160 end;
21492161

src/x64.pas modified +14 -5

@@ -111,7 +111,9 @@
111111 function JoinLinks(L1, L2: longint): longint;
112112
113113 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;
115117
116118 const
117119 RegNames: array[0..3, 0..15] of string[5] = (
@@ -846,10 +848,17 @@
846848 repeat
847849 if IsReg(AsmWord, r, size) and (size = 8) then begin
848850 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
850859 else if o.m.base = NoBase then o.m.base := r
851860 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);
853862 if not (tok in [sPlus, sMinus]) then break;
854863 SaveScan(st);
855864 Next;
@@ -863,7 +872,7 @@
863872 Next;
864873 end else begin
865874 o.kind := oImm;
866- o.v := constExpr();
875+ o.v := constExpr(true);
867876 end;
868877 end;
869878
@@ -976,7 +985,7 @@
976985 if w = 'db' then
977986 repeat
978987 start := TokPos;
979- v := constExpr();
988+ v := constExpr(true);
980989 if (v < -128) or (v > 255) then MarkAt(SpanFrom(start), 'expected a byte from 0 to 255, found ' + IntStr(v));
981990 Put1(v);
982991 if tok <> sComma then break;

test/run/Asm.expect (history) modified +1 -1

@@ -1,5 +1,5 @@
11 35
2-4321 0
2+4321 0 4001
33 98 azc
44 142 6
55 3 9 below

test/run/Asm.raz (history) modified +14 -2

@@ -3,7 +3,9 @@
33 each checked by what it computes *)
44 import Out;
55
6-const Seven = 7;
6+const
7+ Seven = 7;
8+ Eight = 8;
79
810 (* a * 10 + b - 7, by lea, shifts and adds *)
911 proc tens(a, b: I64): I64;
@@ -30,6 +32,15 @@
3032 ret;
3133 end sum;
3234
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+
3344 (* the byte at p + i, zero-extended, and the byte there set to v *)
3445 proc swapByte(p: ^U8; i: INT; v: U8): I64;
3546 code
@@ -114,7 +125,8 @@
114125 begin
115126 Out.int(tens(4, 2)); Out.ln;
116127 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;
118130 s := "abc";
119131 Out.int(swapByte(ADR(s[0]), 1, 'z')); Out.str(" "); Out.str(s[0..3]); Out.ln;
120132 q, r := udivmod(1000, 7);