Commit Graph

3717 Commits

Author SHA1 Message Date
Benjamin Oldenburg
1f60eb4574 arm64-asm.c: deduplicate branch condition mapping
asm_branch() had two identical 15-case switch blocks (30 lines total)
that duplicated condition code mapping. This also duplicated the logic
in the existing parse_condition() helper.

Added get_branch_condition() helper that:
1. Maps branch tokens (TOK_ASM_beq) to condition tokens (TOK_ASM_eq)
2. Calls the existing parse_condition() helper
3. Returns the condition code (0-13) or -1 for non-conditional branches

This reduces code duplication from 30 lines to a single 29-line helper
function, and ensures all condition mapping logic is in one place.
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
5497f87f59 arm64-asm.c: validate operand types before encoding
Multiple instruction handlers were extracting op->reg without checking
that the operand was actually a register. When parse_operand() failed
to recognize a token, it set op->reg = -1, which when masked with 0x1F
became 31 (xzr/sp), silently encoding wrong instructions.

Now each handler validates operand types before extraction:
- asm_shift: validates op1 and op2 are registers
- asm_data_proc: validates op1, op2, and op3 are registers
- asm_ldst: validates op1 is register, op2 is address
- asm_ldst_pair: validates op1 and op2 are registers, op3 is address

This implements fail-fast behavior to catch typos and invalid operands
immediately rather than producing silently incorrect code.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
a1bf1d187d arm64-asm.c: reject invalid operands in parse_operand()
Previously, parse_operand() would silently accept any unrecognized token
and pass it to asm_expr() as an immediate, causing typos like:
  add x0, x1, xyz    ; 'xyz' is not a valid register
to be silently assembled as a symbol reference instead of erroring.

Now, if a token is not a register, condition code, or valid immediate
prefix (#, :, @, $), an error is emitted for identifier tokens.

This implements fail-fast behavior for invalid operands, making it easier
to catch typos and mistakes in assembly code.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
95c17cae64 winnt.h: remove dead ARM64 CONTEXT fallback code
The fallback CONTEXT definition at lines 2073-2124 was unreachable dead code.
The guard '#if defined(__aarch64__) && !defined(_ARM64_CONTEXT_DECLARED)'
could never be true because:

1. Line 50-51: __aarch64__ automatically defines _ARM64_
2. Line 1426: #if defined(_ARM64_) || defined(__aarch64__) always enters
3. Line 1473: _ARM64_CONTEXT_DECLARED is always defined inside that block
4. Line 2073: The fallback guard is therefore always false

This 52-line duplicate was a maintenance hazard that could silently diverge
from the official ARM64_NT_CONTEXT definition. Remove it entirely.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
aa95cfad10 winnt.h: fix ARM64 CONTEXT Bvr/Wvr register types
The fallback CONTEXT struct incorrectly defined Bvr (Breakpoint Value
Registers) and Wvr (Watchpoint Value Registers) as DWORD (32-bit) instead
of DWORD64 (64-bit).

On ARM64:
- BCR/WCR (Control Registers) are 32-bit ✓
- BVR/WVR (Value Registers) are 64-bit ✓

This mismatch caused struct size and layout errors, potentially corrupting
debug register state when used with Windows debugging APIs.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
040583cb9b winnt.h: define missing PE DLL characteristics 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
f277a57172 tccpe.c: remove duplicate IMAGE_DLLCHARACTERISTICS_* defines
These macros were defined twice (lines ~273 and ~317) with identical
values and #ifndef guards. The duplicates appear to be a copy-paste
oversight from adding ARM64 support.

Remove the redundant second set of defines. The first set (lines 273-284)
already provides the fallback definitions needed when Windows headers
are unavailable.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
5f641e2a25 winnt.h: fix ARM64 CONTEXT struct layout mismatch
The fallback CONTEXT struct for ARM64 had multiple structural issues:
- ContextFlags was DWORD64 (8 bytes) instead of ULONG (4 bytes)
- Missing Cpsr field entirely
- Missing DECLSPEC_ALIGN(16) attribute
- X registers as simple array X[29] instead of union with named struct X[31]

These mismatches caused incorrect struct size and field offsets, leading to
register corruption when used with Windows APIs like GetThreadContext or
RtlRestoreContext.

The fallback struct now matches the official ARM64_NT_CONTEXT layout exactly,
ensuring binary compatibility with Windows ARM64 system calls.
2026-04-04 20:02:32 +07:00
OpenCode
d2c06612a5 arm64-asm: validate register width consistency in data processing instructions
The asm_data_proc function was OR-ing register widths together, which
allowed invalid ARM64 instructions like 'add x0, w1, w2' (mixed widths).
ARM64 requires all registers in data processing instructions to have
the same width (all X or all W).

Fix by validating that all three operand registers have matching widths
and emitting an error if they don't match.
2026-04-04 20:02:32 +07:00
OpenCode
a27bd8a7c3 Remove trailing whitespaces from source files 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
6c5aac0da6 tccpe.c: fix msvcrt.dll handle leak on Windows ARM64
pe_get_process_msvcrt_handle() used LoadLibraryA which increments the
module reference count, but never called FreeLibrary to release it.

Use GetModuleHandleA instead, which returns a handle to the already-
loaded msvcrt.dll module without incrementing the reference count.
This is the correct API for accessing system DLLs that are already
mapped into the process address space.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
4fb1e212c1 winnt.h: fix ARM64 CONTEXT V register type
The fallback CONTEXT struct for ARM64 (used when __aarch64__ is defined
but _ARM64_CONTEXT_DECLARED is not set) incorrectly defined V[32] as
DWORD64 (64-bit) instead of ARM64_NT_NEON128 (128-bit).

This caused register corruption when RtlRestoreContext restores NEON/VFP
registers, as the struct size was 256 bytes instead of the correct
512 bytes.

Fixes potential corruption on toolchains that define __aarch64__ but not
_ARM64_ (e.g., clang on macOS or certain cross-compilation scenarios).
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
da60605fd5 tests: probe builtin support directly in tcctest 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
d317b34c71 win32: make test matrix pass across toolchains 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
c90aae5a9e tests: fix non-fatal Windows ARM64 diagnostics 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
dab39b0fe9 Revert "tests: clean trailing whitespace in win32 reference files"
This reverts commit eebee5340406d853eecb722154fb22c32e54844c.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
3ba2a224fa Revert "tests: avoid trailing spaces in tcctest output"
This reverts commit 2141b3f68f39636c5f3abbee7f1d37e8a2a998f8.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
10816e30bb tests: avoid trailing spaces in tcctest output 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
545378ed5b arm64: clarify inline asm support boundary 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
17801cdac8 tests: clean trailing whitespace in win32 reference files 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
0decb1b86f arm64: support mnemonic win32 stack helpers 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
b1763f8629 tests: fix tcctest.c for modern clang (15+) on macOS 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
f220a8c32f win32: recognize MSVC-style ARM64 host macros 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
6cb9d7ae53 arm64: use union type tags in HFA classification 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
684acad263 bcheck: restore atomic never_fatal on Windows 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
98194f55ba Fix arm64 offset mask warnings 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
e661cb9a62 Restrict arm64 old-style vararg handling to PE 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
55acb9272e Fix backtrace formatting and arm64 old-style calls 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
c24d95db09 Remove Windows ARM64 temp-exe fallback 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
4371ebd682 Fix Windows x64 env/runtime and PE alignment tests 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
396675f74f Fix Windows ARM64 runtime regressions and coverage 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
7e7917c3c9 Restore generic backtrace runtime path 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
a1da6220e3 Fix generic backtrace regression outside Windows 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
177b76b844 Add ARM64 Windows coverage and fix native run guard 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
cf4441c415 Fix Windows ARM64 runtime regressions 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
4f4b3dda6b Add and stabilize Windows ARM64 support 2026-04-04 20:02:31 +07:00
Stefan
98765e5ebc libtcc.c: Change parameter name of tcc_set_realloc
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
In libtcc.h there is void tcc_set_realloc(TCCReallocFunc *my_realloc).
Name the parameter in the function definition in libtcc.c accordingly.
2026-03-28 15:44:20 +01:00
Stefan
085bdf8997 tccpp.c: Improve integer constant overflow warning
Improve the warning about an integer constant overflow with the used
overflown value and the potentially aimed value.

This helps spotting the 0 to much in 0x80000000000000000.
2026-03-28 15:31:21 +01:00
diannaaa
fada98b1ce fix(riscv64-link): pair pcrel lo relocations by hi address
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
Track R_RISCV_PCREL_HI20/GOT_HI20 relocations by address and resolve LO12 relocations against the referenced HI, instead of only using the last seen HI.

Also reset/free the per-link relocation map in TCCState.
2026-02-23 22:26:06 +08:00
Aleksi Hannula
4597a9621e
arm64-gen: consider VT_LONG|VT_DOUBLE for TOK_NEG 2026-02-07 12:08:17 +02:00
Aleksi Hannula
5ad52cc1ed
Cast signed pointer offset to ptrdiff_t before performing arithmetic
Affects 32-bit platforms.
2026-02-05 11:49:59 +02:00
Aleksi Hannula
c937095552
Fix numbered register substitution on x86_64 2026-02-05 11:49:59 +02:00
Aleksi Hannula
8c61b91de8
Remove libc dependency from lib-arm64
Moving the u128_t struct may invoke calls to memcpy/memmove when
compiled with tinycc, which would depend on libc.

For example, some configure scripts get confused if they fail to build
test programs involving long doubles. I'm not sure if this is the root
cause, though.
2026-02-05 11:49:59 +02:00
Aleksi Hannula
aeae4b4cee
arm64: Fix long double comparison operators 2026-02-05 11:49:59 +02:00
Aleksi Hannula
c39eaf10cf
arm64: Implement TOK_NEG for floats natively 2026-02-05 11:49:59 +02:00
Aleksi Hannula
1cb0a3d52a
Allow building an empty libtcc1 on AArch64 2026-02-05 11:49:59 +02:00
herman ten brugge
abb2f394dd i386: bound checking needs pic in shared library 2026-02-05 09:31:59 +01:00
Avi Halachmi (:avih)
4fccaf6124 Revert "build c2str.exe with the host compiler; default it to gcc if cross prefix given"
This reverts commit 41fa74fc84.

Reasons for the revert:

1. The commit message is lacking and the use case is not obvious.
   At the very least a concrete example of use case should be given
   (likely cross-building tcc on platform X for running on platform Y),
   followed by an issue description in such use case, and fix descr.
   (e.g. "On platform X, when running ./configure ... && make ..., the
   build fails because whatever. this commit does Y and fixes it").

2. assign_opt is used incorrectly: 1st arg must be --OPTNAME=VALUE
   but it's not. The only thing it does is unconditional host_cc=gcc
   if --cross-prefix=... is used - no need for "assign_opt" at all.

3. cross-building tcc currently has several triggers: build/target
   cpu differ, or build/target OS differ, or --cross-prefix=... is
   used, but that commit only covers the --cross-prefix=... case.
   It's possible that it's intentional to only cover one case, but
   if so then it's not explained why.

4. The var name host_cc is likely incorrect and probably meant to be
   "native_cc", judging by the assigned value "gcc".

5. "gcc" is used unconditionally in such case, without any way to use
   a different native compiler (assuming that's indeed the goal).
   A better approach is likely supporting --native-cc=... option which
   defaults to $cc (which already defaults to "gcc") _before_
   $cross_prefix is added in-front.
2026-01-18 09:04:54 +02:00
Urja Rannikko
41fa74fc84 build c2str.exe with the host compiler; default it to gcc if cross prefix given 2026-01-17 20:47:00 +02:00
noneofyourbusiness
b39cbc70c4
riscv64-asm.c: parse_operand: document some ABI details 2026-01-17 11:35:01 +01:00