Skip to content

AMD64_NT C++ bootstrap support. - #1336

Open
jaykrell wants to merge 1 commit into
modula3:masterfrom
jaykrell:cpp-throw
Open

jaykrell wants to merge 1 commit into
modula3:masterfrom
jaykrell:cpp-throw

Conversation

@jaykrell

@jaykrell jaykrell commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

In the boot1+boot2 workflow, old compilers use current config. This might be something to fix. Maybe they should carry config.

The bootstrap I am working from has .c files, that are not quite valid C++. The mistake is they declare alloca without extern "C" and so fail to link.

In this context, unconditional global -TP is bad. It breaks building the bootstrap.

To deal with this... two tactics here, one commented out.

  • When running boot2, define M3BOOT2, to guide config into a more compatible mode. That is, skip -TP. This is commented, but probably reasonable and works.

  • Another line of thinking.. Years ago people complained when I compiled all the .c files in the tree as C++. cpp_source was proposed. It doesn't look like I completed this. My preferences for/against C and C++ do wax and wane, I admit.

    So then, we can observe, that vast majority of the .c files in tree:

    • Have remained valid C (and C++). At least after I put extern "C" wrappers in them.
    • Have no super compelling reason to compelling reason to compile as C++. Depending on your like/dislike for C++. :)
    • Except to allow this global configuration.

This global configuration is because compiling as C++ does have significant but not universal use-case. It is needed for any Modula-3 file with TRY/EXCEPT/FINALLY when using ex_stack. It is needed in one hand written file, RTExStackC.c that contains a throw statement.

We can address these needs in multiple ways.

  • The -TP switch, as so far attempted, mostly works, except with old bootstraps. We could also patch up bootstrap alloca references. We could move forward to a newer bootstrap, though I really like seeing how far back we can go. It has mostly worked well.
  • File extension guides the compiler. All the M3C outputs were long ago changed to .cpp. So all Modula-3 code through C++ backend is compiled as C++, whether or not it has TRY/EXCEPT/FINALLY. You could actually imagine making this conditional, and measuring if C frontend is faster than C++.
  • Similarly, for this one file, RTExStackC, we can can have RTExStackCpp.cpp, that #includes RTExStackC.c and use m3build cpp_source, if it is defined. Leaving all the other hand written .c as C.

That last option is presented in this PR.
Complete AMD64_NT bootstrap ex_frame => ex_stack has not completed, but logs show this works.

Clearly, as stated, there are many ways to address the situation. It might seem like backward compatible config is a lost cause but I've worked it at a lot, successfully.

Obviously yet another approach is, not for the bootstrap per se to carry its config but go back to the old git hash, and get and use those files. Repair the bootstrap retroactively. Or again, patch the alloca with sed or such.

Older bootstraps also error some:

error C4716: 'FloatMode__GetBehavior': must return a value

i.e. in functions that always throw.
At least when compiled as C++. Maybe C was OK?
Tony pointed out that C stands the test of time as a stable redistributable format, but there are gaps/flaws.
So disable that here. Current HEAD M3C.m3 outputs a pragma to disable it.
In time we might invest in "noreturn", but it is tricky matter, as to who knows what? How does what analysis?
Well, rather than marking these functions as anything, we should make M3Throw as noreturn.
A decent C++ compiler will either backpropagate that information and not warn because is not true, or not warn because it does no analysis. Maybe we self-inflict a problem here by wrapping longjmp in m3_longjmp.
In any case, move along, not much to see...

@jaykrell
jaykrell marked this pull request as ready for review November 23, 2026 10:23

@hosking hosking left a comment

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.

Thanks, Jay. Using a .cpp wrapper plus cpp_source for the one file that has to be C++ looks right to me, better than a global -TP. All the M3C output is already .cpp, so -TP only ever affected hand-written C. A few things before it goes in:

  1. Scope -wd4716 to old compilers. It's only needed for older bootstraps, whose generated C has always-raising functions without a return; current M3C emits the pragma itself. Applied globally, it also hides genuine missing-return errors in hand-written C. M3BOOT2 seems the natural guard: if defined ("M3BOOT2") args += ["-wd4716"] end.
  2. M3BOOT2 is defined but unused. boot2.py now passes -DM3BOOT2, but the only test of it in NT.common is commented out. Either use it, as in (1), or leave it out of boot2.py for now.
  3. The M3_USE_STACK_WALKER block at the end of NT.common. Both branches set it to FALSE, and it's marked as temporary. Because it comes at the end of NT.common, and AMD64_NT includes NT.common, it would also override any setting a target config makes before the include. Could it come out of this PR and be decided in #1346, where NT moves to ex_stack? It's good to see that nothing here turns ex_stack on for Windows; master's NT targets stay on ex_frame (relevant to #1287).
  4. Hand-written C on NT is now compiled as C. Anything relying on C++-only constructs would now fail. CI's NT jobs passed, which covers what CI builds. Did you build any packages outside CI's set on NT with this?
  5. Small things.
    • The comment says RTSTackC.c; the file is RTExStackC.c.
    • "i.e. for jmpbuf, which ought to be rendered as jmpbuf" reads as if a word is missing.
    • The UNWIND lines in NT.common and the blank line in scripts/python/boot2.py are CRLF-to-LF conversions. That's a fine cleanup, but a separate commit would keep this diff to the functional change.

🤖 Generated with Claude Code

@jaykrell

Copy link
Copy Markdown
Contributor Author

-wd4716 is too broad

I hadn't thought of this. Mildly agreed.
So yeah let's guard it, in something.
I have an experiment where boot2.py is inlined into *yml.
What to call it?
Asis M3BOOT2?
M3BOOT_OLD?
M3_CONFIG_OLD_COMPILER?
M3_CONFIG_WITH_OLD_COMPILER?
M3_CONFIG_SUPPORT_OLD_COMPILER?

I have a draft where we get the old m3core/libm3 source via github.
It might make sense to get the old config that way too.
That may or may not resolve this.
Well, I mean, I don't know if this occured in C and C++ or only C++.
I'll test that again, though it is reasonable insurance either way.

small things

CRLF changes are usually an accident, my editor, and then
I can only easily change an entire file to one or the other.
This is all unfortunate

jmpbuf

Maybe I can trim comments.

M3_USE_STACK_WALKER too broad and not overridable.

Yeah it broke I386_NT. Agreed, can make it more flexible.

@jaykrell jaykrell changed the title AMD64_NT C++ bootstrap support (long story). AMD64_NT C++ bootstrap support. Nov 24, 2026
@jaykrell
jaykrell force-pushed the cpp-throw branch 2 times, most recently from d2c236d to cbf9586 Compare November 24, 2026 00:17
@jaykrell

Copy link
Copy Markdown
Contributor Author

I left it as M3BOOT2 but open to other.
I removed the M3_USE_STACK_WALKER.
We could also turn it off, if cpp_source is not defined.
Or, you know, we'll use that condition, later in another PR, once I have a workflow that publishes a distribution in the end.
One form of that can be tar cfz the install, "in the middle of" boot2. I realize that isn't source-based.
It is super easy. I also want the bootstrap to build old libm3/m3core, and maybe use old config.

@jaykrell

Copy link
Copy Markdown
Contributor Author

Rewrote jmpbuf comment shorter.

In the boot1+boot2 workflow, old compilers use current config.
This might be something to fix. Maybe they should carry config.

The bootstrap I am working from has .c files, that are not quite valid C++.
The mistake is they declare alloca without extern "C" and so fail to link.

In this context, unconditional global -TP is bad. It breaks building the bootstrap.

To deal with this... two tactics here, one commented out.

- When running boot2, define M3BOOT2, to guide config into a more compatible mode.
  That is, skip -TP.
  This is commented, but probably reasonable and works.

- Another line of thinking..
  Years ago people complained when I compiled all the .c files in the tree as C++.
  cpp_source was proposed. It doesn't look like I completed this.
  My preferences for/against C and C++ do wax and wane, I admit.

  So the, we can observe, that vast majority of the .c files in tree:
   - Have remained valid C (and C++).
   - Have no super compelling reason to compelling reason to compile as C++.
   - Except to allow this global configuration.

Compiling as C++ does have significant but not universal use-case.
It is needed for any Modula-3 file wity TRY/EXCEPT/FINALLY when using ex_stack.
It is needed in one hand written file, RTExStackC.c that contains a throw statement.

We can address these needs in multiple ways.
 - The -TP switch, as so far attempted, mostly works, except with old bootstraps.
   We could also patch up bootstrap alloca references.
   We could move forward to a newer bootstrap, though I really like seeing how far back we can go.
   It has mostly worked well.
 - File extension guides the compiler.
   All the M3C outputs were long ago changed to .cpp.
   So all Modula-3 code through C++ backend is compiled as C++, whether or not it has TRY/EXCEPT/FINALLY.
   You could actually imagine making this conditional, and measuring if C frontend is faster than C++.
 - Similarly, for this one file, RTExStackC, we can can have RTExStackCpp.cpp, that #includes RTExStackC.c
   and use m3build cpp_source, if it is defined.
   Leaving all the other hand written .c as C.

That last option is presented in this PR.
I'm not done validating it (check the commands/logs), but it is convincing.

Clearly, as stated, there are many ways to address the situation.
It might seem like backward compatible config is a lost cause but I've worked it at a lot, successfully.

Obviously yet another approach is, not for the bootstrap per se to carry its config but go back
to the old git hash, and get and use those files. Repair the bootstrap retroactively.
Or again, patch the alloca with sed or such.

This branch has not been deployed

No deployments
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