Skip to content

cgen: fix x86 segment-override memory operands in basic inline asm#27782

Open
fleximus wants to merge 1 commit into
vlang:masterfrom
fleximus:fix-27777-asm-segment-override
Open

cgen: fix x86 segment-override memory operands in basic inline asm#27782
fleximus wants to merge 1 commit into
vlang:masterfrom
fleximus:fix-27777-asm-segment-override

Conversation

@fleximus

Copy link
Copy Markdown
Member

Fixes #27777

In basic inline assembly (no operand lists), the template string is passed to the assembler verbatim, so segment overrides like gs:[16] were emitted as %%gs:16 and rejected with invalid register name. Registers already handled this via stmt.is_basic; this applies the same escaping rule to segment prefixes: single % in basic asm, %% in extended asm.

Verified with the issue's repro (v -freestanding -cc clang -keepc .) — the binary now disassembles to mov %rdi,%gs:0x10. Added a coutput testdata pair covering both the basic and extended forms.

@medvednikov

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 499d0e3d31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/gen/c/cgen.v
Comment on lines +6284 to +6287
if !stmt.is_basic {
g.write('%') // escape percent with percent in extended assembly
}
g.write('%${arg.segment}:')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Escape segment prefixes in asm goto

In asm goto blocks that only provide the label list (for example asm goto amd64 { jmp gs:[16]; ; ; ; done }), the parser still leaves stmt.is_basic true because it only counts outputs/inputs/clobbers, but asm_stmt emits __asm__ goto (... : : : : label), which is extended asm. This branch therefore writes %gs: instead of %%gs:, and GCC/Clang parse %g as an operand escape (operand number missing after %-letter). Please base this escaping on whether the emitted C asm is extended, including stmt.is_goto, rather than is_basic alone.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cgen: fix x86 segment-override memory operands in basic inline asm

2 participants