Commit Graph

3714 Commits

Author SHA1 Message Date
Meng Zhuo
3257afa160 riscv64-link: add R_RISCV_RELATIVE case in relocate()
arm64-link.c has R_AARCH64_RELATIVE handling.  Without this case,
incoming R_RISCV_RELATIVE relocations from object files fall through
to the 'FIXME: handle reloc type' default path.

P1.7 (NEED_BUILD_GOT guards) is not needed: NEED_RELOC_TYPE and
NEED_BUILD_GOT are always defined in the build context (tcc.h:1608).
2026-05-06 12:30:34 +08:00
Meng Zhuo
419b527657 riscv64-asm: implement far branches via expansion
For branch targets that are external/static symbols (where the
offset cannot be determined at assembly time), expand to:
  b<inverse> .+8       # skip if condition false
  auipc t0, %pcrel_hi  # load target upper bits
  jalr x0, %pcrel_lo(t0)  # jump

This handles the ±4 KiB B-type limitation without requiring
linker relaxation (R_RISCV_RELAX).  Local immediates within
range still use the compact B-type encoding.
2026-05-06 12:30:34 +08:00
Meng Zhuo
5c2240a896 riscv64-asm: remove long long 'not implemented' errors in inline asm
On RV64, long long (64-bit) fits in a single general-purpose register.
The existing load/store calls already handle 64-bit values correctly.
The 'not implemented' errors were vestigial from 32-bit architectures
where long long requires a register pair.

Removing these errors allows inline asm to accept 64-bit integer operands
on RV64, which is important since this is the native register width.
2026-05-06 12:30:34 +08:00
Meng Zhuo
366569eb7a riscv64-asm: fix neg/negw pseudo-instruction encoding
neg/negw were incorrectly using asm_emit_i (I-type/xori) with an
immediate of 1, producing 'xori rd, rs, 1' instead of the intended
'sub rd, x0, rs'.  Fixed to use asm_emit_r (R-type) with proper
SUB/SUBW opcode (0x33/0x3B, func3=0, func7=0x20).

The old code generated wrong machine code for these pseudo-instructions.
negw now also correctly uses OP-32 (0x3B) for 32-bit subtraction.
2026-05-06 12:30:34 +08:00
Meng Zhuo
ff917c09aa riscv64: implement gen_cvt_sxtw with addiw instruction
Replaces the empty stub that relied on 'RV64 registers are always
sign-extended' assumption.  Now emits addiw rd, rs, 0 for proper
32-to-64 bit sign extension, matching arm64's sxtw behavior.

Verified on Spacemit X100: tests2 pass (125_atomic_misc has a
pre-existing intermittent segfault, not caused by this change).
2026-05-06 12:30:34 +08:00
Meng Zhuo
9f0915a506 riscv64: implement gen_cvt_csti for narrow int promotion
Add sign/zero extension for char/short to int casts using
slli+srai (signed) or slli+srli/andi (unsigned) sequences.
Also adds RISCV64 to the gen_cvt_csti fast-path conditional
in tccgen.c:3466, matching arm64/i386/x86_64.

Verified on Spacemit X100: all tests2 pass, no regressions.
2026-05-06 12:30:34 +08:00
Meng Zhuo
69c8e92566 riscv64: add PROMOTE_RET for narrow return ABI compliance
Other mature backends (i386, x86_64, arm64) all define PROMOTE_RET,
which forces explicit sign/zero extension of narrow return types at the
caller side.  Without it, riscv64 relied on the assumption that RV64
registers are always sign-extended, which may not hold when interfacing
with non-TCC compilers.

Verified on Spacemit X100 (riscv64): self-compilation is self-consistent
across 3 layers.  test3 reference mismatch is expected — the test
intentionally invokes UB via type-punned function pointers (csf macro).
2026-05-06 12:30:34 +08:00
grischka
30afb50e64 arm64-win32 review: fix problems and pass tests
tccpe.c:
- fix arm64 unwind codes (to make native set/longjmp() work)
  sizeof(RUNTIME_FUNCTION) is 8 on arm64 in the first place
  no need to note stack slots if we don't save any registers anyway

arm64-gen.c:
- fix long double reg-move
- fix arm64_hfa() for structs with float arrays
- gfunc_prolog(): setup stackframe eariler (simplifies unwind codes)
- new function gv_addr(RC);

win32/include/setjmp.h:
- provide correct definition for setjmo() (frameoffset = 224)

tccasm.c:
- support ".quad" with symbol & relocation
- support ".size"
- fix ". - symbol" arithmetic

win32/lib/crt1.c and win32/include/stdlib.h:
- do not write to __argc/__argv which reside in msvcrt.dll
  (msvcrt.dll on arm64 does not like that, crashes on unload)

tcc.c,libtcc.c:
- new functions tcc_fopen/fclose to avoid different stdio unstances
  in tcc.exe & libtcc.dll

tests & github workflow:
- add test-win32.bat to run tests with a tcc compiled by build-tcc.bat
- add msvcrt_start.c for gcc/clang to use the same runtime as tcc

  the problem is that newer gcc as well as clang and cl are
  linking to newer runtimes (such as UCRT) that have partially
  different printf format behavior which makes tcctest fail.

  the solution here is to force these compilers to link with
  msvcrt.dll just like tcc.

  Also, there is no gcc on arm64-win32 currently at all.

  Anyway, this approach to running the github CI tests
  does not require msys2.  But It does rely on gnumake
  as well as on some 'sh' shell though which seems to be
  installed somewhere (maybe it is the one from git).
2026-05-04 12:51:10 +02:00
Benjamin Oldenburg
576cd2a923 arm64-win32 support: symbolic op-code constants
from: https://github.com/bold84/tinycc
Author: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Committer: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Branch: win_arm64_clean
Commit: 8b5ab1bb01

Also here: https://repo.or.cz/tinycc.git/shortlog/refs/mob/mob_bold84/win_arm64_clean

This and the previous commits on mob is selected parts
of that original branch. So it is not everything.

It is not, for example:
- unrelated whitespace changes in many files
- a "pin msvcrt.dll" feature in tccpe.c (why that)
- a native getenv() replacement in tcc.c (why that)
- larger changes to the win32/lib runtime and tccrun.c (not needed)
- a very gcc specific detail for struct alignent in tccgen.c
- a custom set/longjmp implementation/replacement (not needed)
- lots of rather basic test files in the win32 folder
- a 'tests/asm' folder with some files (one file renamed to 140_test...c)
- a .docs folder with one file
2026-05-04 12:50:42 +02:00
Benjamin Oldenburg
44e6853cb1 arm64-win32 support: target PE & varargs 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
5c728d6506 arm64-win32 support: improve HFA & struct 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
90d17c9748 arm64-win32 support: use arm64_sub_sp() 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
99713bcbfa arm64-win32 support: use arm64_sym() 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
303badef22 arm64-win32 support: fix scaled mask 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
41bcb4a78f arm64-win32 support : linker & -run 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
f459aff5f6 arm64-win32 support : arm64 assembler 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
ea823189d6 arm64-win32 support : tests 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
03d58b0746 arm64-win32 support : runtime 2026-05-04 12:49:53 +02:00
Benjamin Oldenburg
ff5d3b4874 arm64-win32 support : configure & Makefiles
from: https://github.com/bold84/tinycc
Author: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Committer: Benjamin Oldenburg <benjamin.oldenburg@ordis.co.th>  2026-04-04 16:29:28
Branch: win_arm64_clean
Commit: 8b5ab1bb01

Also here: https://repo.or.cz/tinycc.git/shortlog/refs/mob/mob_bold84/win_arm64_clean

This and the followup commits on mob is selected parts
of that original branch. So it is not everything.

It is not, for example:
- unrelated whitespace changes in many files
- a "pin msvcrt.dll" feature in tccpe.c (why that)
- a native getenv() replacement in tcc.c (why that)
- larger changes to the win32/lib runtime and tccrun.c (not needed)
- a very gcc specific detail for struct alignent in tccgen.c
- a custom set/longjmp implementation/replacement (not needed)
- lots of rather basic test files in the win32 folder
- a 'tests/asm' folder with some files (one file renamed to 140_test...c)
- a .docs folder with one file
2026-05-04 12:49:25 +02:00
grischka
d9a6d9aec0 reverts (11/2025 - 04/2026)
Revert "Add support to debug libtcc code"
- not fully developed experimental feature
This reverts commit 1fe3e3bff5.
This reverts commit 4768b11737.

Revert "tests: add test for x86_64 xor REX prefix bug in load()"
- AI generated nonsense test
This reverts commit d5ecb52a71.

Revert "tccpp.c: Improve integer constant overflow warning"
- Too long and confusing messages and comments for feature
  with questionable benefit.
This reverts commit 085bdf8997.

riscv64-link:
- cleanup "pair pcrel lo relocations by hi address"
  From fada98b1ce

tccgen.c:
- Simplify "Cast signed pointer offset to ptrdiff_t before performing arithmetic"
  From 5ad52cc1ed

libtcc.c:
- Revert "tcc options: document behavior and clashes (no-op)"
  a bit more information than one would like to have I think.
  (why try to understand that comments plus the extra
  script if one can as well just read the code itself ;)
  From 234e2dd2bf

tccdefs.h:
- Revert "Move lib/va_list.c into include/tccdefs.h"
  Lets not fill tccdefs.h with too much inline code
  Also, -nostdlib -run is no longer supported
  From fa6a6bfbbd

arm64-gen.c:
- cleanup "Implement TOK_NEG for floats natively"
  Also, make it "no lvalue" in tccgen.c/x86-64-gen.c
  From c39eaf10cf

lib/lib-arm64.c:
- cleanup "Remove libc dependency from lib-arm64"
  using unions is much faster than some made up memcpy()
  From 8c61b91de8
2026-05-03 11:09:45 +02:00
H-language
a66ac623b2 win32: add missing AttachConsole export to kernel32.def
AttachConsole has been exported from kernel32.dll since Windows XP
but was absent from TCC's import definitions, causing
"undefined symbol 'AttachConsole'" at link time for code that uses it.
2026-05-01 09:58:03 +12:00
Cyan Ogilvie
4768b11737 tccrun: fix OOB read in elf_output_obj after tcc_relocate with -g
When -g is set, cleanup_sections() preserves all sections after relocate,
including the SHT_RELX relocation sections. cleanup_symbols() drops all
STB_LOCAL symbols from the symbol table, which renumbers the remaining
globals. The kept relas still hold r_info indices from the pre-cleanup
numbering, but nothing rewrites them. A later elf_output_obj() call ends
up in sort_syms() / update_relocs(), which allocates old_to_new_syms[]
sized for the post-cleanup (smaller) symtab and then indexes it with the
stale (larger) sym_index values from those rela entries, reading past
the end of the array.

Fix: when do_debug is set, build an old->new index map while re-adding
globals, then walk every retained SHT_RELX section linked to the symtab
and rewrite its r_info entries. Locals (and the undef sym at 0) map to
0 by tcc_mallocz, so relocations against dropped locals now refer to
SHN_UNDEF - the best we can do without preserving the locals themselves.

Reproducible with libtcc_debug.c if the program contains enough static
helpers / string literals for the symtab to actually shrink; observed
under valgrind in jitc (https://github.com/cyanogilvie/jitc) when
compiling re2c-generated state machines with -g.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 15:37:49 -03:00
Petr Skocik
b39da9f6fa Fix false warnings with readonly atomics
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
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
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
grischka
5ec0e6f84b some reverts & fixes
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
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
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
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
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
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