Commit Graph

3692 Commits

Author SHA1 Message Date
Petr Skocik
b39da9f6fa Fix false warnings with readonly atomics
Code like:

    #include <stdatomic.h>
    _Atomic const int ai; int aiGet(void){ return atomic_load(&ai); }

has been raising "warning: assignment of read-only location".

This is due to `__typeof__ (*ptr) tmp;` not rvalue-converting the target
type.
2026-04-28 08:40:53 +02:00
Petr Skocik
5675177b7f allow $ at nonstarting positions in asm symbols under -fdollars-in-identifiers
e.g., asm("foo$bar: ret;");
2026-04-28 08:36:09 +02:00
Reini Urban
543d84c668 .gitignore: Add tests/libtcc_test_xor_rex 2026-04-26 14:56:39 +02:00
Detlef Riekenberg
9b8765d8ba Revert commits from OpenCode-AI and a partial regression fix
This reverts the crap from an unknown committer "OpenCode[AI]", that added an extension to TinyCC,
which is incompatible to the C standard and not available in any other mainstream C compiler:

* Revert "No More ; needed"
This reverts commit 6547ea47d6

* Revert "Add AGENTS.md"
This reverts commit 898f496dc9.

This also reverts a regression fix by "Zuhaitz.dev", which fixed a small part of the OpenCode-AI chaos:
* Revert "tccgen: Fix optional semicolon regression"
This reverts commit 169628a6ab

--
Regards ... Detlef
2026-04-20 01:42:41 +02:00
Anon
7b92d2dcd2 Fix warning 2026-04-16 13:16:17 -07:00
Zuhaitz-dev
169628a6ab tccgen: Fix optional semicolon regression 2026-04-16 17:24:21 +01:00
Cyan Ogilvie
8ecfd0a722 Relicensing TinyCC 2026-04-16 10:45:24 -03:00
Cyan Ogilvie
690fb14015 x86_64-gen: fix missing REX prefix for xor zero into r8-r15
load() used a raw o() call to emit xor-zero which lost bit 3 of the
register number via REG_VALUE():

    o(0xc031 + REG_VALUE(r) * 0x900);

For r >= 8, this emitted the wrong instruction (e.g. xor %ebx,%ebx
for TREG_R11 instead of xor %r11d,%r11d), clobbering the wrong
register.

Use orex() to emit the REX prefix, consistent with all adjacent
branches in load().
2026-04-16 10:42:16 -03:00
Cyan Ogilvie
d5ecb52a71 tests: add test for x86_64 xor REX prefix bug in load()
When load() in x86_64-gen.c generates a zero constant for a 64-bit
register, it uses:

    o(0xc031 + REG_VALUE(r) * 0x900);

REG_VALUE(r) masks to (r & 7), losing bit 3, and no orex() call
emits the REX prefix needed for registers r8-r15.  For TREG_R11
(used by gcall_or_jmp for indirect calls), this emits
"xor %ebx,%ebx" (31 db) instead of "xor %r11d,%r11d" (45 31 db),
clobbering the wrong register.

The test compiles an indirect call through a null function pointer
((void(*)(void))0)() via libtcc, then inspects the generated machine
code for the incorrect encoding.
2026-04-16 10:42:16 -03:00
OpenCode[AI]
6547ea47d6 No more ; needed 2026-04-15 20:56:01 -07:00
OpenCode[AI]
898f496dc9 Add AGENTS.md 2026-04-15 20:28:42 -07:00
Stefan
98765e5ebc libtcc.c: Change parameter name of tcc_set_realloc
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
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
grischka
5ec0e6f84b some reverts & fixes
workflow:
- revert 'pinact for security' for readability
  from 831c3fa184
tccpp.c:
- remove code that allows tcc to parse numbers incorrectly (*)
  from 829c848520
tccgen.c:
- Revert "Relaxed the 'incompatible pointer type' warning a bit" (*)
  from d9ec17d334.
tccrun.c:
- remove support for -nostdlib -run
  for simplicity, we require "main" with tcc -run always
tccpp.c:
- Revert "Free all preprocessor memmory in case of error."
  from c96f0cad61
  Remove TinyAlloc->limit instead.  Thus it can do also bigger
  allocs.  Big TokenStrings (like 200kb+ when compiling tcc)
  may come from inline functions or from large initializers.
Makefile/configure:
- use --config-pie for configuring tcc output only
- use -fPIC with clang-x86_64 to avoid 32-bit relocs
libtcc.c:
- fix "tcc file.c -run" i.e. -run as last argument
i386-gen.c:
- PIC refactor

(*) sorry, but code in tcc should have a minimum of generic relevance
2026-01-10 13:46:23 +01:00
herman ten brugge
518279dc3e Another update for macos
Sorry about last commit.
I had no access to a macos machine for a for weeks.
2026-01-09 08:25:28 +01:00
herman ten brugge
1401967ce2 Fix macos
I fixed macos problem with test case 60.
Also tccrun mmap and debug code did not work.
2026-01-06 16:01:28 +01:00
herman ten brugge
1fe3e3bff5 Add support to debug libtcc code
I tried several gdb/lldb options to debug libtcc generated code.
But gdb/lldb complained that they could not load symbols or
that module does not exist. There was also another problem. The
code itself was in memory (string) and gdb/lldb do not have
functions to acces it.

So I came up with a new api for debugging libtcc code.
First you enable debugging with: tcc_set_options(s, "-g")
Then compile the code with: tcc_compile_string_file(s, program, "<file>.c")
Then call tcc_relocate().
And finaly write the object file to disk: elf_output_obj(s, "<file>.o")
Now you can start the debugger and put an breakpoint after the
elf_output_obj() code. Then use gdb command add-symbol-file <file>.o
and from there on you can set breakpoints in the libtcc generated code.
You can also step/print variables/...
I could not find a simular function in lldb yet.

When debugging is done you remove the tcc_set_options(s, "-g").
All other code can remain because tcc_compile_string_file and
elf_output_obj do not output any file any more is debug is not set.

See also tests/libtcc_debug.c
2026-01-06 12:11:51 +01:00
herman ten brugge
8a8388c6ff Solve some bug reports
The savannah web site had some new bug report last december.
A lot of them are assemmbly bugs.
See testcase 60 for an overview.
2026-01-06 07:49:02 +01:00
herman ten brugge
9a7edb20d3 Add pic/pie support to i386
Some targets (netbsd) require that code is
compiled with -fPIE.

i386-gen.c:
Lot of changes to load/store code

i386-link.c:
Set PCRELATIVE_DLLPLT depending on configuration

tccelf.c:
Change some relocations and add get_pc_thunk.o

Makefile lib/Makefile lib/get_pc_thunk.S:
Add new file get_pc_thunk.o
2025-12-30 20:15:35 +01:00
Dylan Fei
5ce2c4b454 arm64-gen: fix 16-byte alignment for variadic arguments on stack
According to AAPCS64, when an argument requires 16-byte alignment
and is passed on the stack, the stack address must be rounded up
to the next 16-byte boundary.

TCC previously failed to perform this alignment check in gen_va_arg,
leading to data corruption when a 16-byte aligned argument followed
an 8-byte argument on the stack.

Note: This issue is most prominent on Mach-O/Apple Silicon where
variadic arguments are passed entirely on the stack. Testing shows
this fix resolves the failure on macOS while remaining compatible
with Linux/ARM64 (e.g., Raspberry Pi).

Modified gen_va_arg to perform a round-up (addr + 15) & ~15 when
align == 16.
2025-12-29 00:57:35 +08:00
Dylan Fei
b8513fe895 arm64-gen: fix address calculation for large symbol offsets
When accessing a global symbol with an addend > 0xffffff, the AArch64
backend incorrectly encoded an 'add xr, xt, #0' (Add Immediate)
instead of 'add xr, xr, xt' (Add Register).

This resulted in the base address of the symbol being overwritten
by the offset value rather than being summed with it.

Fixes the issue where (sym + 0x1000000) would resolve to 0x1000000
instead of the correct memory address.
2025-12-28 21:32:58 +08:00
Dylan Fei
11118be717 Fix pointer difference issue in aarch64/riscv64
When calculating the difference between two pointers (TOK_PDIV), the
AArch64 and RISC-V64 backends used unsigned division
instructions (udiv/divu).

This patch moves TOK_PDIV to use signed division instructions (sdiv/div)
for both backends, ensuring correct behavior as per the C standard.
2025-12-23 16:01:10 +08:00
herman ten brugge
5a370d8a6b Relocation updates
x86_64-link.c:
Check relocations R_X86_64_32/R_X86_64_32S. These relocations
can become to large if not configured with '--disable-static'
or '--config-pie' and code is run with -run -ltcc.
This only happens on x86_64.

tccdbg.c:
1) Add some .debug (dummy) sections when dwarf is used. These
   sections are otherwise not relocated correctly for R_DATA_32DW.
2) Fix stab problem when reloc is applied twice.
2025-12-23 06:46:22 +01:00
herman ten brugge
64cf0b816b undo x86_64-link.c commmit
The relocation check does not work with stabs enabled on some targets.
It works fine on fedora with and without stabs.

So remove check.
2025-12-21 18:49:21 +01:00
herman ten brugge
96119149fe Problems solved on for libtcc, pie, riscv64, arm64
I found some problems with a testcase from mailing list.

On x86_64 an overflow on reloc R_X86_64_32 occurred that was
not reported when using -run -ltcc.
The problem could be solved by compiling tcc with -fPIE, -pie
or --disable-static.

Makefile, configure, libtcc.c, x86_64-link.c:
- add --config-pie to configure help. Ignore -pie in libtcc.c
  and check reloc overflow in x86_64-link.c

arm64-gen.c:
- Fix reading from constant like '*(int *)0x7fffb7f1280c'

elf.h, riscv64-link.c:
- fix for -run -ltcc. Ignore reloc R_RISCV_SET_ULEB128 and
  R_RISCV_SUB_ULEB128 that are used in .debug_loclists section.
2025-12-21 07:05:55 +01:00
herman ten brugge
34eed88a70 Small updates
Allow 'make speedtest' in tests directory
Fix compiler warning tccpp.c
2025-12-17 20:22:08 +01:00
herman ten brugge
3c18df610d Fix clang macos 15 bug on arm64 in tests/tcctest.c 2025-12-17 19:54:22 +01:00
herman ten brugge
f3de8b5307 Use macos 15 in build.yml 2025-12-17 18:21:01 +01:00
herman ten brugge
8569427459 Move -run -nostdlib code to lib directory
tcc.h, arm-gen.c, arm64-gen.c, i386-gen.c, riscv64-gen.c, x86_64-gen.c:
- remove old code

tccrun.c:
- update to use lib/run_nostdlib.c

Makefile, lib/Makefile, lib/run_nostdlib.c:
- new code

tests/nostdlib_test.c:
- testcode
2025-12-15 14:45:11 +01:00
Stefan
829c848520 tccpp.c: Configurable integer literal overflow handling
Add the define TCC_CUT_ON_INTEGER_LITERAL_OVERFLOW to use the most
significant digits of an integer literal before its parsing led to an
overflow.

When compiling tcc with another compiler, which is not able to handle
64 bit arithmetic, it is beneficial to use the last value before an
integer literal overflows. Parsing 0x1000000000000000 then results in
0x10000000. The mescc from GNU Mes is able to compile a first tcc on a
32 bit system without 64 bit arithmetic. This change allows this first
tcc to compile a second tcc with complete 64 bit arithmetic.
2025-12-13 18:53:15 +01:00
Aleksi Hannula
cb41cbfe71 x86-64: Fix cvts*2si
Co-authored-by: Herman ten Brugge <hermantenbrugge@home.nl>
2025-12-03 17:21:00 +02:00
Reini Urban
831c3fa184 github CI: pinact run -u
for security
2025-12-02 16:31:13 +01:00
Aleksi Hannula
0fcd46f364 rv64: Generate VT_CMP output from float comparison 2025-12-02 07:30:31 +02:00
Aleksi Hannula
b4569233cb Set TYPE_PARAM parsing mode in old-style param lists 2025-12-02 07:30:27 +02:00
herman ten brugge
f5f544f436 allow cross compiling for alloca on arm64 2025-12-01 18:09:04 +01:00
Aleksi Hannula
7479a5c21a elf: Set ph sizes only if ph was allocated 2025-11-29 22:28:36 +02:00
Aleksi Hannula
78e5e690a2 rv64: Implement some pseudo-ops from Zicsr 2025-11-29 21:11:46 +02:00
herman ten brugge
6da45946ae Add bound check support for alloca on all targets
stddef.h, tccdefs.h, tccgen.c, tests/boundtest.c, tests/tcctest.c:
- remove ifdef arround alloca

lib/Makefile:
- move alloca-bt.o to COMMON_O

lib/alloca-bt.S:
- add alloca bound check code for arm, arm64, riscv64

lib/alloca.S:
- check for leading underscore

tcc.h, libtcc.c:
- include arm64-asm.c instead of arm-asm.c for arm64

tcctok.h, tccasm.c:
- add simple reloc support (only for R_AARCH64_CALL26)
2025-11-29 07:54:28 +01:00