Skip to content

String literals in asm files: use asm quoting conventions, not OCaml's#588

Merged
xavierleroy merged 2 commits into
masterfrom
asm-string-quoting
Jul 14, 2026
Merged

String literals in asm files: use asm quoting conventions, not OCaml's#588
xavierleroy merged 2 commits into
masterfrom
asm-string-quoting

Conversation

@xavierleroy

Copy link
Copy Markdown
Contributor

Currently, we use the %S format of Printf.printf to emit some string literals in assembly files: filenames in .file directives, and .asciz directives for DWARF debug information.

(String literals coming from C source files follow a different path that doesn't use %S. They are not affected by the problem fixed in this commit.)

The %S format produces an OCaml string literal, which can be misinterpreted by the GNU and Diab assemblers. In particular, OCaml uses decimal escapes \ddd (three decimal digits) while assemblers, like C, use octal escapes \ooo (three octal digits). Consequently, a "vertical tab" character (ASCII 0x0B) is printed as \011, which is interpreted as "horizontal tab" (ASCII 0x09) by the assembler.

This commits introduces PrintAsmaux.quote_string to produce a string literal in asm / C syntax, with octal escape sequences, and uses it in assembler printers in place of %S.

Currently, we use the `%S` format of Printf.printf to emit some string
literals in assembly files: filenames in `.file` directives,
and `.asciz` directives for DWARF debug information.

(String literals coming from C source files follow a different path
that doesn't use `%S`.  They are not affected by the problem fixed in
this commit.)

The `%S` format produces an OCaml string literal, which can be
misinterpreted by the GNU and Diab assemblers.  In particular, OCaml
uses decimal escapes `\ddd` (three decimal digits) while assemblers,
like C, use octal escapes `\ooo` (three octal digits).  Consequently,
a "vertical tab" character (ASCII 0x0B) is printed as `\011`, which is
interpreted as "horizontal tab" (ASCII 0x09) by the assembler.

This commits introduces `PrintAsmaux.quote_string` to produce a string
literal in asm / C syntax, with octal escape sequences, and uses it in
assembler printers in place of `%S`.
Comment thread backend/PrintAsmaux.ml
(fun c ->
match c with
| '"' | '\\' -> Buffer.add_char b '\\'; Buffer.add_char b c
| '\x00' .. '\x1F' -> Printf.bprintf b "\\%03o" (Char.code c)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about quoting at least newline and tab as "\n" and "\t" instead of octal for readability?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, let's do that. Commit fcda834.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. Thanks!

@xavierleroy xavierleroy merged commit eeeb0aa into master Jul 14, 2026
7 checks passed
@xavierleroy xavierleroy deleted the asm-string-quoting branch July 14, 2026 13:05
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.

2 participants