Skip to content

packed struct produces byte stores instead of word stores #4056

@markfirmware

Description

@markfirmware

See https://github.com/markfirmware/zig-bare-metal-microbit/blob/7d7b6d23f49c454a36cf23ff7fa8806fd113ac1b/lib00_basics.zig#L384

Good - not a packed struct, does a 32-bit word store

0000000000009388 mission03_model_railroad_button_controlled_pwm.Throttle.prepare:
; fn prepare(self: *Throttle) void {
    9388:	81 b0 	sub	sp, #4
    938a:	00 90 	str	r0, [sp]
    938c:	02 48 	ldr	r0, [pc, #8]
    938e:	02 21 	movs	r1, #2
; short_cuts.shorts = 0x002;
    9390:	01 60 	str	r1, [r0]
; fn prepare(self: *Throttle) void {
    9392:	01 b0 	add	sp, #4
    9394:	70 47 	bx	lr
    9396:	c0 46 	mov	r8, r8

0000000000009398 $d.425:
    9398:	00 92 00 40 	.word	0x40009200

Bad - changing that struct to packed struct, it does a sequence of (4) byte stores. Does not work because mmio needs a word write.

0000000000009388 mission03_model_railroad_button_controlled_pwm.Throttle.prepare:
; fn prepare(self: *Throttle) void {
    9388:	81 b0 	sub	sp, #4
    938a:	00 90 	str	r0, [sp]
    938c:	05 48 	ldr	r0, [pc, #20]
    938e:	00 21 	movs	r1, #0
; short_cuts.shorts = 0x002;
    9390:	01 70 	strb	r1, [r0]
    9392:	05 48 	ldr	r0, [pc, #20]
    9394:	01 70 	strb	r1, [r0]
    9396:	05 48 	ldr	r0, [pc, #20]
    9398:	01 70 	strb	r1, [r0]
    939a:	05 48 	ldr	r0, [pc, #20]
    939c:	02 21 	movs	r1, #2
    939e:	01 70 	strb	r1, [r0]
; fn prepare(self: *Throttle) void {
    93a0:	01 b0 	add	sp, #4
    93a2:	70 47 	bx	lr

00000000000093a4 $d.425:
    93a4:	03 92 00 40 	.word	0x40009203
    93a8:	02 92 00 40 	.word	0x40009202
    93ac:	01 92 00 40 	.word	0x40009201
    93b0:	00 92 00 40 	.word	0x40009200

Metadata

Metadata

Assignees

No one assigned

    Labels

    use caseDescribes a real use case that is difficult or impossible, but does not propose a solution.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions