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

Load string indices with inline asm to save space. #879

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Dirbaio
Copy link
Contributor

@Dirbaio Dirbaio commented Oct 28, 2024

Requires #878 because asm!(... sym ..) breaks if the symbol name has quote characters.

Defmt string indices are 16 bits, which can be loaded with a single movw.
However, the compiler doesn't know that, so it generates movw+movt or ldr rX, [pc, #offs]
because it sees we're loading an address of a symbol, which could be any 32bit value.
This wastes space, so we load the value with asm manually to avoid this.

#[inline(never)]
unsafe fn testfoobar() {
    info!("foo");
}

before:
00027180 <_ZN11application10testfoobar17h819f2778e910636eE>:
   27180: b580         	push	{r7, lr}
   27182: 466f         	mov	r7, sp
   27184: f000 f954    	bl	0x27430 <_defmt_acquire> @ imm = #0x2a8
   27188: 4803         	ldr	r0, [pc, #0xc]          @ 0x27198 <_ZN11application10testfoobar17h819f2778e910636eE+0x18>
   2718a: f000 fa35    	bl	0x275f8 <_ZN5defmt6export6header17hd841c3329e459a6fE> @ imm = #0x46a
   2718e: e8bd 4080    	pop.w	{r7, lr}
   27192: f000 b95b    	b.w	0x2744c <_defmt_release> @ imm = #0x2b6
   27196: bf00         	nop
   27198: 03 00 00 00  	.word	0x00000003

after:
00027180 <_ZN11application10testfoobar17hc6c595839020df66E>:
   27180: b580         	push	{r7, lr}
   27182: 466f         	mov	r7, sp
   27184: f000 f94e    	bl	0x27424 <_defmt_acquire> @ imm = #0x29c
   27188: f240 0003    	movw	r0, #0x3
   2718c: f000 fa34    	bl	0x275f8 <_ZN5defmt6export6header17h79a831a74481cee1E> @ imm = #0x468
   27190: e8bd 4080    	pop.w	{r7, lr}
   27194: f000 b954    	b.w	0x27440 <_defmt_release> @ imm = #0x2a8

defmt currently puts json as-is in symbol names, which can contain special
characters not normally found in symbol names like quotes `"`, braces `{}`
and spaces ` `.

This can cause compatibility issues with language features or tools that don't
expect this. For example it breaks `sym` in `asm!`.

This is a *breaking change* of the wire format, so this PR increases the
format numbre. `defmt-decoder` is updated to be able to decode the new format,
while keeping ability to decode older formats.
@jonathanpallant
Copy link
Contributor

Thanks for this PR - it looks neat. Once we've resolve the question about hex-encoding symbols, I would support merging this.

@Urhengulas Urhengulas added the breaking change fix / feature / improvement involves a breaking change and needs to wait until next minor version label Nov 6, 2024
@jonathanpallant
Copy link
Contributor

(This isn't a breaking change, we just want cargo server-checks to stop complaining about a PR that was committed some time ago)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change fix / feature / improvement involves a breaking change and needs to wait until next minor version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants