Move instruction interferes with the if condition check #23

Closed
opened 2025-08-26 16:17:49 +00:00 by Furkan · 2 comments

I tried to simplify the code but I failed to reproduce the bug in a simpler case so here is the full code:

import io

main() {
	raylib.InitWindow(1280, 720, "raylib example\0".ptr)
	raylib.SetTargetFPS(165)

	x  := 640
	y  := 360

	loop {
		if raylib.WindowShouldClose() {
			return
		}

		raylib.BeginDrawing()
		raylib.ClearBackground(BLACK)

		midx := x
		midy := y

		raylib.DrawRectangle(x, y, 32, 32, GREEN)

		if false {
			y -= 1       // This line shouldn't execute but it does
			io.write(x)  // This line shouldn't execute but it does
		}

		if false {
			y += 1
			raylib.DrawLine(midx, midy, midx, midy + 50, RED)
		}

		raylib.EndDrawing()
	}
}

extern {
	raylib {
		InitWindow(width int, height int, title *byte)
		WindowShouldClose() -> bool
		SetTargetFPS(fps int)
		GetFPS() -> int

		// Shapes
		BeginDrawing()
		EndDrawing()
		ClearBackground(color int)
		DrawLine(startPosX int, startPosY int, endPosX int, endPosY int, color int)
		DrawRectangle(posX int, posY int, width int, height int, color int)
	}
}

const {
	RED        = 0xFF3729E6
	BLACK      = 0xFF000000
}

Expected behaviour: green box stays in the same place
Current result: It moves and prints numbers

I tried to simplify the code but I failed to reproduce the bug in a simpler case so here is the full code: ``` import io main() { raylib.InitWindow(1280, 720, "raylib example\0".ptr) raylib.SetTargetFPS(165) x := 640 y := 360 loop { if raylib.WindowShouldClose() { return } raylib.BeginDrawing() raylib.ClearBackground(BLACK) midx := x midy := y raylib.DrawRectangle(x, y, 32, 32, GREEN) if false { y -= 1 // This line shouldn't execute but it does io.write(x) // This line shouldn't execute but it does } if false { y += 1 raylib.DrawLine(midx, midy, midx, midy + 50, RED) } raylib.EndDrawing() } } extern { raylib { InitWindow(width int, height int, title *byte) WindowShouldClose() -> bool SetTargetFPS(fps int) GetFPS() -> int // Shapes BeginDrawing() EndDrawing() ClearBackground(color int) DrawLine(startPosX int, startPosY int, endPosX int, endPosY int, color int) DrawRectangle(posX int, posY int, width int, height int, color int) } } const { RED = 0xFF3729E6 BLACK = 0xFF000000 } ``` Expected behaviour: green box stays in the same place Current result: It moves and prints numbers
ed self-assigned this 2025-08-26 16:29:53 +00:00
Owner

Inspecting with:

q build examples/raylib --os windows --dry --asm --func main

When the second if statement is commented out, the code of the first if is correct, comparing 0 to 0:

  move r1, 0
  compare r1, 0
  jump if == :if.exit.2

With both if statements in place we get a move that doesn't belong there:

  move r8, 0
  move r8, r3
  compare r8, 0
  jump if == :if.exit.2

I'll check where the move is coming from.

Edit: It's coming from a Phi within SSA form. The position of the move is incorrect.

Inspecting with: ``` q build examples/raylib --os windows --dry --asm --func main ``` When the second if statement is commented out, the code of the first `if` is correct, comparing `0` to `0`: ``` move r1, 0 compare r1, 0 jump if == :if.exit.2 ``` With both if statements in place we get a `move` that doesn't belong there: ``` move r8, 0 move r8, r3 compare r8, 0 jump if == :if.exit.2 ``` I'll check where the move is coming from. *Edit:* It's coming from a Phi within SSA form. The position of the move is incorrect.
Owner

Fixed in cli/q@9ae5194041.

Fixed in https://git.urbach.dev/cli/q/commit/9ae519404119454f3c1081e5cc9cc06c07f758be.
ed changed title from Very strange bug to Move instruction interferes with the if condition check 2025-08-26 18:01:46 +00:00
ed added spent time 2025-08-26 18:02:10 +00:00
1 hour
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Total time spent: 1 hour
ed
1 hour
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cli/q#23
No description provided.