Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/asm: constant MOVQ transformed to MOVL, but MOVL upper bit zeroing not guaranteed #58016

Closed
BullSprite opened this issue Jan 26, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@BullSprite
Copy link

BullSprite commented Jan 26, 2023

What version of Go are you using (go version)?

$go version
go version devel go1.20-6001c043dc Fri Aug 19 03:32:27 2022+0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOOS="linux"

What did you do?

When 64bit immediate that can be fit into 32bit immediate is used as argument of MOVQ instruction, the compiler assembler transforms it to MOVL. On X86_64 platform the test shows that upper that upper 32bits of RAX are zeroed after MOVL of 32bit constant (tested on Xeon 6230N CPU). But there is no such statement in SDM so this behavior should be classified as 'non-documented' and can be easily changed by the platform owner without any announcement.

test.sFile
TEXT main·test(SB), 4, $0
    MOVQ    $0xFFFFFFFE, AX
    MOVQ    AX, ret+8(FP)
    RET

Commands that I run:

$ go tool asm test.s
$ go tool objdimp -gnu test.o

What did you expect to see?

test.s:4 0x332 48b8feffffff00000000 MOVQ $0xfffffffe, AX // mov $0xfffffffe,%rax
test.s:7 0x33c 4889442410 MOVQ AX, 0x10(SP) // mov %rax,0x10(%rsp)
test.s:8 0x341 c3 RET // retq

What did you see instead?

test.s:4 0x332 b8feffffff MOVL $-0x2, AX // mov $-0x2,%eax
test.s:7 0x337 4889442410 MOVQ AX, 0x10(SP) // mov %rax,0x10(%rsp)
test.s:8 0x341 c3 RET // retq
@seankhliao seankhliao changed the title go tool asm mov encoding cmd/asm: mov encoding Jan 26, 2023
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 26, 2023
@mknyszek mknyszek changed the title cmd/asm: mov encoding cmd/asm: constant MOVQ transformed to MOVL, but MOVL upper bit zeroing not guaranteed Jan 30, 2023
@mknyszek mknyszek added this to the Backlog milestone Jan 30, 2023
@mknyszek
Copy link
Contributor

CC @golang/compiler

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 30, 2023
@mknyszek
Copy link
Contributor

It's weird the SDM doesn't mention this (though I haven't confirmed myself). It would be surprising to me if part of the register was ever left dirty on a constant MOV (I feel like at this point that might break quite a bit), but I don't know enough about the history of x86 to say anything more.

@randall77
Copy link
Contributor

This is specified in the SDM, vol 1 section 3.4.1.1.

32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register.

In addition, other compilers assume this, e.g. gcc.

@golang golang locked and limited conversation to collaborators Jan 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants