On Thu, 2026-09-03 at 16:51 +0200, grischka via Tinycc-devel wrote:
> On 02.09.2026 02:35, Eric Raible wrote:
> > Here is a simple self-contained example.  It seems to show that
> > tcc_add_symbol doesn't work with
> > _Thread_local, at least on my setup.
> 
> Hi, two things...
> 
> 1) "tcc -run" / JIT libtcc does not support thread local storage
>      (error message still missing...)

Those interested in contributing to tinycc that additional feature
could:

0. add the error message.

1. consider using GNU lightning library.
https://www.gnu.org/software/lightning/

(they technically could perhaps even use GNU lightning in their code
generating software).

GNU lightning is a library providing an abstract machine (close to RISC
machines) and in some cases could be easier to use than the API
provided by tinycc (essentially compilation of strings containing C
code; you have to generate correct C code yourself, see
https://arxiv.org/abs/1109.0779 for inspiration, and
https://github.com/bstarynk/misc-basile/blob/master/manydl.c for some
example).

See also
https://lists.gnu.org/archive/html/lightning/2016-09/msg00004.html


> 
> 2) __thread variables may look like normal variables but they are
> not.


Actually, thread variables are limited in their total size. On Linux
implementations have to use a memory segment allocated with the thread
stack by pthread_create (see pthread_attr_setstacksize)
> 
> As such, with
> 
> >     int __thread x2 = 42; 
> >     tcc_add_symbol(s, "x2", &x2);
> 
> this 'x2' does not have an address really.  All you can do from C is
> to take the address of the current threads instance of 'x2'. But then
> again that instance is not a '__thread' object.  It is just a  normal
> 'int' with 4 bytes somewhere in the memory address space.


Another approach for runtime code generation could be to use recent GCC
(gcc.gnu.org) compilers (e.g. GCC 15, GCC 16) with their libgccjit
enabled. Those compilers have an API to interface the compiler middle
end. https://gcc.gnu.org/onlinedocs/jit/

So libgccjit and GNU lightning (or generating C strings for tinycc
consumed by tcc_compile_string which I believe is what tcc_add_files
does) are complementary approaches: libgccjit would generate slowly
some efficient machine code (internally sometimes using dlopen) but GNU
lightning (and asmjit.com) are able to generate (like tinycc does)
quickly some "slow" machine code.

On Linux and many POSIX computers generating C (or even C++ or Rust or
Go) code in a temporary file and compiling that small temporary file
into a plugin (*.so, later dlopen-ed) is often fast enough for
interactive use (assuming you generate less than a thousand lines, and
don't have too much #include-d material).

Of course Common Lisp implementations (e.g. sbcl.org) are generating
machine code at runtime to.

Thanks for reading

NB the refpersys.org free software project also generates code and is
an inference engine (GPL licensed) project. Your help is welcome.

-- 

Basile STARYNKEVITCH                           
<[email protected]>
8 rue de la Faïencerie                      
http://starynkevitch.net/Basile/  
92340 Bourg-la-Reine                        
https://github.com/bstarynk
France                               
https://github.com/RefPerSys/RefPerSys
                  https://orcid.org/0000-0003-0908-5250

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to