Skip to content

c_backend: Implement C backend - #3504

Open
ManuLinares wants to merge 8 commits into
c3lang:masterfrom
ManuLinares:c_backend_r2
Open

c_backend: Implement C backend#3504
ManuLinares wants to merge 8 commits into
c3lang:masterfrom
ManuLinares:c_backend_r2

Conversation

@ManuLinares

Copy link
Copy Markdown
Member

Add the --cflags option to forward flags to the underlying C compiler.

It's a biggie, it passed all the unit tests:

c3c compile-test -O1 --backend=c --cc clang test/unit/
c3c compile-test -O1 --backend=c --cc gcc test/unit/

haven't tested on msvc yet so it most probably break there, but I'll put it in draft.

Add the `--cflags` option to forward flags to the underlying C compiler
Cross-compiling for Windows using the C backend failed to produce valid
C code and target object files.

Win32 entry points were incorrectly named `main` with invalid parameter
counts, untyped function pointer invocations produced illegal C syntax,
and mismatched cross-module pointer types caused compiler rejections.
Additionally, Clang emitted host-format binaries because target triples
were not forwarded, and the runtime required non-freestanding headers.
Optimized builds targeting x86_64 encountered hardware alignment
faults during execution under Windows/Wine.

Local variables requiring 16-byte alignment, such as jmp_buf for
setjmp and large aggregate buffers, lacked alignment specifiers in
the generated C code. This caused unaligned stack placement and
crashed SSE instructions like movdqa. Additionally, direct
pointer dereferences of 128-bit integers led compilers to emit
aligned vector instructions on unaligned memory.

Ensure stack variables maintain required alignment and safely
perform 128-bit integer pointer accesses.

TODO: Needs cleaning
Check C3 type bit width on shifts to prevent false aborts.
Unwrap replaced decls to fix local variable lookups.
Emit weak attribute on functions to prevent link collisions.
Add missing math builtins, bitwise intrinsics, and __dso_handle for
TCC (although TCC doesn't support int128).
Consolidate repeated code patterns across C codegen to reduce size and
improve maintainability. Guard against emitting types from inactive
modules.
@ManuLinares

Copy link
Copy Markdown
Member Author

mmm, I don't know if I should "namespace" the externs, so they don't conflict with each other on same name but different args. testproject

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.

1 participant