When compiling Arm Thumb code, GCC and LLVM ([including zig](https://godbolt.org/z/sfxd37P3x)) use R7 as the frame pointer (except when `-fomit-frame-pointer` is specified, of course) rather than R11 which is used in 32-bit mode. This is mainly due to Thumb1's `PUSH`/`POP` instructions only supporting R0-R7 and R14/R15. Armv7-M does have Thumb2 `PUSH.W`/`POP.W` which can do it, but LLVM still uses R7, even if you explicitly use e.g. `-mcpu cortex_m3`. Note that AAPCS32 doesn't mention using R7 as FP in Thumb mode at all, but it also only barely mentions using R11 as FP: "In some variants r11 (FP) may be used as a frame pointer in order to chain frame activation records into a linked list."
`std.debug.cpu_context.Arm` currently always assumes R11 is the frame pointer for both `arm` and `thumb` targets, which means FP unwinding simply doesn't work on thumb targets, including most arm microcontrollers. It would be nice to have stack tracing available through the FP on these constrained platforms since you often don't have enough ROM space to waste on the `.debug_frame` section, and [`.ARM.exidx`/`.ARM.extab` unwinding](https://codeberg.org/ziglang/zig/issues/30931) isn't implemented yet.
On some platforms, it's possible to have both thumb and arm functions interleaved within a single call stack. In these cases, [compilers use R11 for the FP in arm functions, and R7 in thumb functions](https://godbolt.org/z/EsaYW8rhh), so neither blindly using R11 nor R7 will guarantee a correct stack unwinding in the general case. The most reliable way to determine which register is the FP for the currently executing stack frame would be by reading `CPSR` using the `MRS` instruction and checking the `T` bit, but since zig currently doesn't have an equivalent to `__attribute__((target("thumb")))` that can be applied on a per-function basis, the thumb-ness of all non-extern functions will always match `@import("builtin").target.arch.isThumb()`.
For deeper stack frames, it doesn't matter what register the FP was stored in, since when we unwind we're getting it from the stack instead. As long as all functions were compiled with `-fno-omit-frame-pointer`, the FP will always have been pushed immediately after the LR.
So the first change is to have `std.debug.cpu_context.Arm.getFp()` use R7 when `native_arch` is thumb.
The current inline assembly for grabbing the CPU context mentions only using `STM` for R0-R12 for thumb compatibility, but it still uses several 32-bit Thumb2 instructions that don't exist on architectures like Cortex M0/M0+. This PR adds a slightly more verbose alternative version that only uses 16-bit instructions if the target doesn't support Thumb2. I thought about skipping R8-R12 entirely on targets that only support Thumb1, since it's highly unlikely that they'll actually be used for anything, but for now they are still being captured.
With these two changes, and appropriate `debug` declarations in the root module to enable symbol lookup using a custom minified string table tacked onto the end of the firmware blob, I can get stack traces on an RP2040 microcontroller (Cortex M0+):
```
src/main.zig:135:37: 0x10000b5a in dumpCurrentStack (firmware)
src/main.zig:131:5: 0x10000b50 in iAmAFunctionYesIAm (firmware)
src/main.zig💯21: 0x10000a7c in main (firmware)
$rpi/src/rp2040/boot.zig:112:16: 0x10000310 in _start (firmware)
$rpi/src/rp2040/boot.zig:31:11: 0x100002b6 in _boot3 (firmware)
```
But there's still one more problem: all of the reported addresses point to the instruction immediately following the `BL`/`BLX` instruction that we would expect. For example, here's the disassembly for the top two functions:
```
main.iAmAFunctionYesIAm:
10000b48 80b5 push {r7, lr}
10000b4a 00af add r7, sp, #0
10000b4c 00f001f8 bl #main.dumpCurrentStack
10000b50 80bd pop {r7, pc}
main.dumpCurrentStack:
10000b52 80b5 push {r7, lr}
10000b54 00af add r7, sp, #0
10000b56 00f001f8 bl #debug.writeCurrentStackTrace
10000b5a 80bd pop {r7, pc}
```
In this case, none of the resolved symbols or source locations are affected by the incorrect instruction addresses, but if the next instruction after the subroutine happened to be an inlined function, they could be erroneous. We need to make sure that the thumb bit is cleared so that when `std.debug.StackIterator` subtracts 1 from the return address, it ends up in the previous call instruction. I put this in `std.debug.stripInstructionPtrAuthCode` and renamed it to `std.debug.stripInstructionPtrMarkerBits` since I believe everywhere that's currently used should have the thumb bit stripped as well (or at least it shouldn't harm anything to do so).
With all of the changes, my RP2040 test now looks correct:
```
src/main.zig:135:37: 0x10000b59 in dumpCurrentStack (firmware)
src/main.zig:131:5: 0x10000b4f in iAmAFunctionYesIAm (firmware)
src/main.zig💯21: 0x10000a7b in main (firmware)
$rpi/src/rp2040/boot.zig:112:16: 0x1000030f in _start (firmware)
$rpi/src/rp2040/boot.zig:31:11: 0x100002b5 in _boot3 (firmware)
```
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37482
Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>
* Add `.llvmbc`/`.llvmir` to `ObjectFormat` for emitting unoptimized IR
* Replace `--verbose-air`, `--verbose-intern-pool`,
`--verbose-generic-instances`, `--verbose-llvm-ir`, and
`--verbose-llvm-bc` with various `--debug-log` scopes
* `--debug-log illegal-air`: pre-legalized Air
* `--debug-log legal-air`: post-legalized Air
* `--debug-log air`: final Air with liveness
* `--debug-log ip-stats`: InternPool statistics
* `--debug-log ip-values`: InternPool values
* `--debug-log ip-generic-instances`: InternPool generic instances
* `--debug-log llvm-ir`: pre-optimized LLVM IR
* `--debug-log opt-llvm-ir`: post-optimized LLVM IR
* Delete `--verbose-cimport` which does nothing
* Delete `--verbose-llvm-cpu-features` which is equivalent to looking at
the outputs of `--show-builtin` and `--debug-log llvm-ir`
This was included as part of 5d58306162
but there was no mention of it in the commit message and I didn't notice
it in the review.
This is a partial (one-line) revert. Let's discuss this change carefully
and separately.
The existing `lock` and `unlock` methods continue to act exclusively.
They should be used when data may be mutated. New methods, `lockShared`
and `unlockShared`, may be used for shared locking in situations where
multiple independent users are reading but not mutating data.
For generic code, it is far more useful to consider this equivalent to
`unreachable`. Meanwhile, it is difficult to accidentally write concrete
code which performs this operation, so very little is actually lost from
not having a compile error.
If we accept that this operation does not trigger a compile error, then
it already has the potential to invoke Illegal Behavior today, because
uninstantiable types have an unspecified size in memory, so the
dereference always potentially exceeds the pointer's provenance. (That
said, this only means it is *possible* for the operation to invoke IB,
so the langspec should nonetheless explicitly specify that dereferencing
a pointer to an uninstantiable type is itself Illegal Behavior.)
Storing uninstantiable types into memory does not require any specific
language rules, because that operation can never be reached anyway due
to it requiring an operand (the RHS of `a = b`) whose type is the store
type, which is (by definition) impossible for uninstantiable types.
Resolves: https://codeberg.org/ziglang/zig/issues/36247
and remove "release" from the enum tag names.
no functional change, however, despite the addition of
backwards-compatibile declarations in this patch, it is breaking because
expressions that use `==` or `!=` operators will not able to use the
deprecated names.
I predict these names to survive until the 1.0 tag without any more
breakage.
Adds target info for the PSX. Builds fine with the following:
```zig
const target = b.resolveTargetQuery(.{
.os_tag = .psx,
.cpu_arch = .mipsel,
});
```
the only "problem" being that it spits out an error from LLVM even
though generating an object file succeeds:
```sh
❯ zig build
install
└─ install generated to main.o
└─ compile obj obj Debug mipsel-psx failure
error: warning: MIPS-I support is experimental
```
If one overrides `SelfInfo` in their `root.debug`, it is no longer possible
to refer to the default `SelfInfo` that the std.debug uses.
This allows the developer to still refer to the std's default without
copying the SelfInfo zig files to their project.
The intention behind the iterator was to avoid needing to allocate the
symbols, but in practice we need to allocate them anyway since we need
to reverse their order and don't have random access. The alternative
would be an N^2 algorithm.
In practice this isn't that bad, because even if the allocation fails,
we'll still end up printing the address, so the user still ends up with
the necessary information to reconstruct the crash. I don't think it's
worth it to try to set up some kind of ring buffer or return partial
results on failure, but may revisit this.