raz / commit 2e254a62

commit
2e254a6250823b36f57dc5491e4d0f02878d2066
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e73bb952
X64: a jump to the end of a procedure keeps the return's jmp it lands after

patch · log

3 files changed, +30 -2

src/X64.raz (history) modified +5 -2

@@ -57,6 +57,7 @@
5757 listing*: BOOL; (* -S: each instruction's text is kept, for list *)
5858 said: ^Said;
5959 numSaid, saidCap: INT;
60+ labelAt: INT; (* where a jump was last fixed to the place it was at *)
6061
6162 proc setListing*(yes: BOOL);
6263 begin
@@ -881,6 +882,7 @@
881882
882883 proc fixLink*(L: INT);
883884 begin
885+ if L # NoLabel then labelAt := Chunk.pc; end;
884886 fixLinkTo(L, Chunk.pc);
885887 end;
886888
@@ -891,10 +893,11 @@
891893 4 bytes back and an E9 byte (jmp) is before it; a jcc's field follows
892894 the bytes 0F 8x, never E9. L then becomes the next jump on the chain,
893895 which the field held. The caller must place L next, before anything
894- can have noted where the jmp ended. *)
896+ can have noted where the jmp ended; and no jump may have been fixed
897+ to just after it, as an if's, which would then land beyond. *)
895898 proc dropJumpTo*(var L: INT);
896899 begin
897- if (L = NoLabel) or (L # Chunk.pc - 4) or (Chunk.text[L - 1] # 0E9H) then return; end;
900+ if (L = NoLabel) or (L # Chunk.pc - 4) or (Chunk.text[L - 1] # 0E9H) or (labelAt = Chunk.pc) then return; end;
898901 L := Chunk.get4At(L);
899902 Chunk.takeBack(5);
900903 if listing and (numSaid > 0) and (said[numSaid - 1].at = Chunk.pc) then DEC(numSaid); end;

test/run/IfReturnEnds.expect (history) added +1 -0

@@ -0,0 +1 @@
1+ok

test/run/IfReturnEnds.raz (history) added +24 -0

@@ -0,0 +1,24 @@
1+(* An if that ends a procedure, its body ending in return: the jump past
2+ the if lands at the procedure's return, not beyond it. *)
3+module IfReturnEnds;
4+import Sys;
5+
6+var keys: [1]INT;
7+
8+proc f(on: BOOL);
9+begin
10+ if on and (keys[0] > 0) then
11+ keys[0] := 2;
12+ return;
13+ end;
14+end;
15+
16+proc main*;
17+begin
18+ f(false);
19+ keys[0] := 1;
20+ f(true);
21+ if keys[0] # 2 then Sys.exit(1); end;
22+ _ := Sys.write(Sys.stdout, "ok\n");
23+end;
24+end IfReturnEnds.