From 2e254a6250823b36f57dc5491e4d0f02878d2066 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:09:19 +0100 Subject: [PATCH] X64: a jump to the end of a procedure keeps the return's jmp it lands after --- diff --git a/src/X64.raz b/src/X64.raz index 4119a49..ac76f77 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -57,6 +57,7 @@ listing*: BOOL; (* -S: each instruction's text is kept, for list *) said: ^Said; numSaid, saidCap: INT; + labelAt: INT; (* where a jump was last fixed to the place it was at *) proc setListing*(yes: BOOL); begin @@ -881,6 +882,7 @@ proc fixLink*(L: INT); begin + if L # NoLabel then labelAt := Chunk.pc; end; fixLinkTo(L, Chunk.pc); end; @@ -891,10 +893,11 @@ 4 bytes back and an E9 byte (jmp) is before it; a jcc's field follows the bytes 0F 8x, never E9. L then becomes the next jump on the chain, which the field held. The caller must place L next, before anything - can have noted where the jmp ended. *) + can have noted where the jmp ended; and no jump may have been fixed + to just after it, as an if's, which would then land beyond. *) proc dropJumpTo*(var L: INT); begin - if (L = NoLabel) or (L # Chunk.pc - 4) or (Chunk.text[L - 1] # 0E9H) then return; end; + if (L = NoLabel) or (L # Chunk.pc - 4) or (Chunk.text[L - 1] # 0E9H) or (labelAt = Chunk.pc) then return; end; L := Chunk.get4At(L); Chunk.takeBack(5); if listing and (numSaid > 0) and (said[numSaid - 1].at = Chunk.pc) then DEC(numSaid); end; diff --git a/test/run/IfReturnEnds.expect b/test/run/IfReturnEnds.expect new file mode 100644 index 0000000..9766475 --- /dev/null +++ b/test/run/IfReturnEnds.expect @@ -0,0 +1 @@ +ok diff --git a/test/run/IfReturnEnds.raz b/test/run/IfReturnEnds.raz new file mode 100644 index 0000000..595f485 --- /dev/null +++ b/test/run/IfReturnEnds.raz @@ -0,0 +1,24 @@ +(* An if that ends a procedure, its body ending in return: the jump past + the if lands at the procedure's return, not beyond it. *) +module IfReturnEnds; +import Sys; + +var keys: [1]INT; + +proc f(on: BOOL); +begin + if on and (keys[0] > 0) then + keys[0] := 2; + return; + end; +end; + +proc main*; +begin + f(false); + keys[0] := 1; + f(true); + if keys[0] # 2 then Sys.exit(1); end; + _ := Sys.write(Sys.stdout, "ok\n"); +end; +end IfReturnEnds.