From ac3c464a51a08c8400530dfa68a5e8f829a60112 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:59:22 +0100 Subject: [PATCH] X64: jmp to a register --- diff --git a/src/X64.raz b/src/X64.raz index 201c6e8..4119a49 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -1401,6 +1401,13 @@ return nil; end; +(* Whether the identifier at the scan is a register name, not a label *) +proc atRegister(): BOOL; +var r, size: INT; +begin + return isReg(asmWord(), ADR(r), ADR(size)); +end; + (* One instruction or label, as assemble reads it *) proc statement(constExpr: ConstFn): ERROR; var @@ -1441,7 +1448,7 @@ if Scan.tok # Scan.sComma then break; end; Scan.next; until false; - elsif (aw[0] = 'j') and ((aw = "jmp") or (cond(part(aw, 1, 3)) >= 0)) and (Scan.tok = Scan.sIdent) then + elsif (aw[0] = 'j') and ((aw = "jmp") or (cond(part(aw, 1, 3)) >= 0)) and (Scan.tok = Scan.sIdent) and not atRegister() then (* a jump to a label *) k := try lab(Scan.id); Scan.next; diff --git a/test/err/AsmJccRegister.err b/test/err/AsmJccRegister.err new file mode 100644 index 0000000..da1b9a2 --- /dev/null +++ b/test/err/AsmJccRegister.err @@ -0,0 +1,4 @@ +test/err/AsmJccRegister.raz:4:5: error: unknown instruction jz + 4 | jz rax; + | ^^ +raz: 1 error diff --git a/test/err/AsmJccRegister.raz b/test/err/AsmJccRegister.raz new file mode 100644 index 0000000..4368b37 --- /dev/null +++ b/test/err/AsmJccRegister.raz @@ -0,0 +1,6 @@ +module AsmJccRegister; +proc f; +code + jz rax; +end f; +end AsmJccRegister. diff --git a/test/run/Asm.expect b/test/run/Asm.expect index 8db1732..e1b5235 100644 --- a/test/run/Asm.expect +++ b/test/run/Asm.expect @@ -7,5 +7,6 @@ 13 40 42 42 42 +42 cdef 0 100 -1 diff --git a/test/run/Asm.raz b/test/run/Asm.raz index e41a658..5ee265f 100644 --- a/test/run/Asm.raz +++ b/test/run/Asm.raz @@ -120,6 +120,12 @@ db 0C3H; end answer; +(* the procedure f, jumped to through a register: its result comes back as ours *) +proc through(f: proc: I64): I64; +code + jmp rdi; +end through; + (* s without its first k bytes: s in rdi and rsi, k in rdx, the slice back in rax and rdx *) proc tail(s: []U8; k: INT): []U8; code @@ -162,6 +168,7 @@ c := 40; Out.int(fetchAdd(ADR(c), 2)); Out.str(" "); Out.int(c); Out.ln; Out.int(I64(two() * 21.0)); Out.str(" "); Out.int(answer()); Out.ln; + Out.int(through(answer)); Out.ln; t := "abcdef"; Out.str(tail(t[0..6], 2)); Out.str(" "); Out.int(LEN(tail(t[0..6], 6))); Out.ln; Out.int(pick(3, t[0..6])); Out.str(" "); Out.int(pick(9, t[0..6])); Out.ln;