Skip to content

m3core: make ex_stack target-independent - #1271

Merged
hosking merged 1 commit into
masterfrom
ex-stack-portable
Nov 22, 2026
Merged

hosking merged 1 commit into
masterfrom
ex-stack-portable

Conversation

@hosking

@hosking hosking commented Nov 21, 2026

Copy link
Copy Markdown
Member

As discussed in #1266, this makes the ex_stack (C++ zero-cost EH) runtime independent of the per-target stack walker, so any C-backend target can adopt it from its platform config.

Under ex_stack a raise is just throw _M3Exc{act}; the libunwind code in the per-target RTStackC.c is only needed for stack dumps. This PR separates the two.

  1. Common throw entry point. RTStack__ThrowM3Exc and struct _M3Exc move out of the three per-target RTStackC.c files into a new runtime/ex_stack/RTExStackC.c. The file must be compiled as C++ and fails with #error otherwise.
  2. No walker required. RTExStack no longer asserts RTStack.Has_walker. On a target without a walker, PrintStack (@M3stackdump / @M3debugex) prints that no stack dump is available.
  3. Selection follows the config. runtime/m3makefile chooses ex_stack when M3_USE_STACK_WALKER is set and M3_BACKEND_MODE = "C", instead of using a target list.
    • HAS_STACK_WALKER now only lists the targets with a libunwind walker; those get include_dir(TARGET) and the UNWIND library.
    • Every other build gets the stub RTStackC.c, moved from ex_frame/ to no_walker/, and the common RTMachine.

No existing target changes mechanism. ARM64_DARWIN, AMD64_DARWIN and AMD64_LINUX still use ex_stack with their walkers, and everything else still uses ex_frame. A new target still needs the two-phase bootstrap described in README-ex_stack.md, which this PR updates.

Testing (ARM64_DARWIN):

  • Rebuilt m3core and libm3 and ran an exception test program covering TRY/EXCEPT, TRY/FINALLY, nested raises and exception arguments. Its output was unchanged.
  • Repeated the test with ARM64_DARWIN temporarily removed from HAS_STACK_WALKER, so it used ex_stack, the stub walker and the common RTMachine. The output was identical.

🤖 Generated with Claude Code

@hosking

hosking commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

This overlaps with #1269 and #1270 (the M3_HOST_<arch>_<os> macros, intended to merge the per-target RTStackC.c files into one).

The two fit together. This PR moves the throw entry point (RTStack__ThrowM3Exc) out of those files into ex_stack/RTExStackC.c, so they are left with only the stack-dump walker. It also stops ex_stack depending on a walker.

The overlap is in how the walker is chosen. Here it is chosen by directory: the target's own directory if it is listed in HAS_STACK_WALKER in runtime/m3makefile, otherwise the stubs in no_walker/. Once the walkers are merged into one RTStackC.c under #if M3_HOST_… with a stub fallback, that table and no_walker/ can go, and the makefile only needs to choose between ex_stack and ex_frame. The common RTMachine.i3 choice (Has_stack_walker) would need the same treatment.

I'm happy to make that change in this PR or follow up after it, depending on which lands first.

ex_stack raises with a C++ throw; the per-target libunwind code only
serves stack dumps.  Separate the two so any C-backend target can use
ex_stack by setting M3_USE_STACK_WALKER in its config:

- RTStack__ThrowM3Exc and struct _M3Exc move from the three per-target
  RTStackC.c files to ex_stack/RTExStackC.c (compiled as C++).
- RTExStack no longer asserts a walker; PrintStack reports that no
  stack dump is available when RTStack.Has_walker () is FALSE.
- runtime/m3makefile selects ex_stack from M3_USE_STACK_WALKER and
  M3_BACKEND_MODE = "C".  HAS_STACK_WALKER now only says which targets
  have a libunwind walker; others get the stubs, moved from ex_frame/
  to no_walker/, and the common RTMachine.
- RTStackCommon.c tests the M3_HOST_* macros from m3core.h: the bare
  AMD64_DARWIN etc. are not defined, so Has_walker () returned FALSE
  on every target.

No existing target changes mechanism.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jaykrell

Copy link
Copy Markdown
Contributor

@hosking I'm trying to patiently hold off. Please proceed.

@hosking

hosking commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Thanks, Jay. I'll merge #1271 once CI gets a full run through. After that, rebasing #1277 to your single common walker under #if, with the selection as in #1271, sounds right. I've also posted in #1266 on the wider direction.

@hosking
hosking merged commit 8ca6ba2 into master Nov 22, 2026
11 checks passed
hosking pushed a commit that referenced this pull request Nov 22, 2026
…, LOOPHOLE fix

Brings in #1271 (ex_stack target-independent), #1294/#1299 (unused base
pointer), #1295, #1296 (_m3_caught local), #1297, #1298, the RTStackC
include fix and #1300 (LOOPHOLE between a float and a packed integer, with
m3test p308).

Conflicts:
- RTStackC.c (ARM64_DARWIN, AMD64_DARWIN, AMD64_LINUX): take master's;
  RTStack__ThrowM3Exc now lives in ex_stack/RTExStackC.c (#1271).  msir's
  terminate backstop moves there too, as in #1302 (text identical, so the
  merge of #1302 will be clean).
- RTExStack.m3: master's (no Has_walker assertion, #1271) plus msir's
  @M3stackdump switch.
- LoopholeExpr.m3: both import lists.
- m3tests m3makefile: msir's p300 to p304 and master's p305 to p308.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hosking pushed a commit that referenced this pull request Nov 22, 2026
#1271 (merged from master) selects ex_stack only when M3_BACKEND_MODE is
"C".  MSIR's LLVM lowering (MSIRObj, MSIRAsm) also emits C++ EH
(invoke/landingpad, __gxx_personality_v0) and supports only ex_stack, but
m3core silently got ex_frame in those modes: an MSIR-built cm3 then
recursed in RTHooks.Raise -> RTExFrame's backstop -> RuntimeError.Self ->
RTHooks.Raise until the stack overflowed, as soon as anything raised
(cm3 -clean in the LLVM ladder's stage 3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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