1
0
Fork 0
Commit Graph

3468 Commits

Author SHA1 Message Date
Ekaitz Zarraga 0aca861194 fixup! riscv: Implement large addend for global address
Use `t1` instead of `t0` for the cases when `rr` is not set so `t0` is
used by default and this happens:

    lui t0, XXX
    add t0, t0, t0

Instead, now we do:

    lui t1, XXX
    add t0, t0, t1
2024-04-28 00:15:23 +02:00
Ekaitz Zarraga 8baadb3b55 riscv: asm: implement `j offset` 2024-04-25 15:13:21 +02:00
Ekaitz Zarraga 159776304f riscv: asm: Add branch to label 2024-04-24 00:54:51 +02:00
Ekaitz Zarraga 671d03f944 riscv: Add full `fence` instruction support
This commit adds support for `fence`'s predecessor and successor
arguments.
2024-04-23 15:10:08 +02:00
Ekaitz Zarraga c994068175 riscv: asm: Add load-reserved and store-conditional
Add Atomic instructions `ld` and `sc` in their 32 bit and 64 bit
versions.
2024-04-23 12:05:05 +02:00
Ekaitz Zarraga 0703df1a6a Fix Extended Asm ignored constraints
This commit fixes the case where the register of for the Extended Asm
input or output is known. Before this commit, the following case:

  register long __a0 asm ("a0") = one;
  asm volatile (
       "ecall\n\t"
       : "+r" (__a0) // NOTE the +r here
  );

Didn't treat `a0` as an input+output register (+ contraint) as the code
skipped the constraint processing when the register was already chosen
(instead of allocated later).

This issue comes from f081acbfba, that was
taken as a reference in every other Extended Assembler implementation.
2024-04-16 02:47:56 +02:00
herman ten brugge 4944f509c3 riscv: Avoid some compiler warnings 2024-04-13 16:26:12 +02:00
Ekaitz Zarraga 6b3cfdd025 riscv: Add extended assembly support
NOTE: In order to be able to deal with general-purpose vs floating-point
registers, this commit adds a flag in the 6th bit of the register. If
set, it means the register is a floating-point one. This affects all the
assembler.
2024-04-09 00:19:41 +02:00
Ekaitz Zarraga e02eec6bde riscv: fix jal: fix reloc and parsing 2024-03-27 11:50:02 +01:00
Ekaitz Zarraga 0239133488 fixup! riscv: Add .option assembly directive (unimp) 2024-03-23 12:32:32 +01:00
Ekaitz Zarraga cbe70fa629 riscv: Add .option assembly directive (unimp) 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga 618c173421 riscv: libtcc1.c support some builtins for __riscv 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga 3782da8d0c riscv: Support $ in identifiers in extended asm.
Needed for using `__global_pointer$`.
$ don't have special meaning in RISC-V assembly.
2024-03-21 13:33:27 +01:00
Ekaitz Zarraga e2d8eb3d1c riscv: jal: Add pseudo instruction support 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga 409007c9d5 riscv: jalr: implement pseudo and parse like GAS 2024-03-21 13:33:27 +01:00
Ekaitz Zarraga 8bfef6ab18 riscv: Add pseudoinstructions
call, tail, jump, jr, mv, not, neg, negw, seqz, snez, sltz, sgtz, bnez,
beqz, blez, bgez, bltz, bgtz, li
2024-03-21 13:33:25 +01:00
Ekaitz Zarraga 8cbbd2b88a riscv: Use GAS syntax for loads/stores:
Before:
    ld rd, rs, imm
    sd rs1, rs2, imm

Now:
    ld rd, imm(rs)
    sd rs2, imm(rs1)

NOTES: Just as in GAS:
    - In stores the register order is swapped
    - imm is optional
    - when imm is not included parenthesis can be removed
2024-03-19 12:38:21 +01:00
Ekaitz Zarraga 019d10fc12 riscv: Move operand parsing to a separate function 2024-03-19 12:38:21 +01:00
grischka 2b0a663df9 libtcc usability improvements
- tccgen.c: cleanup switch data etc. after errors (*)
- tccpe.c: faster get_dllexports (*)
- tccpe.c: support -Wl,-e[ntry]=... (*)
- libtcc.c: win32: use ANSI functions (GetModuleFileNameA etc.)
- tccrun.c: be nice to tcc-0.9.26 ("struct/enum already defined")
- tccpp.c: be nice to tcc-0.9.27's va_start/end macros

(*) suggested by Robert Schlicht
https://lists.gnu.org/archive/html/tinycc-devel/2024-03/msg00012.html
2024-03-13 21:06:01 +01:00
grischka 9d2068c630 tccrun: add option CONFIG_RUNMEM_RO=2
/* 0 = .text rwx  other rw (memory: min 2 pages) */
/* 1 = .text rx   other rw (memory: min 3 pages) */
/* 2 = .text rx  .rdata ro  .data/.bss rw (memory: min 4 pages) */

tcc -vv -run ... shows some info.
Also when compiled with -DMEM_DEBUG:
tcc -bench -run ... shows some memory usage
2024-03-03 21:39:53 +01:00
grischka ca061f3a96 i386-asm: fix pc-relative label ariths
See test. We need to use 'ind' from later when the address
field of the instruction is put.

Also: fix crash when the substracted symbol is undefined
Also: assume asm-symbols to be lvalues (except func/array)
2024-03-03 19:56:18 +01:00
herman ten brugge 9675c1e245 Use CONFIG_RUNMEM_RO=1 on apple 2024-03-03 06:41:27 +01:00
grischka 42395a1912 tccrun: PAGEALIGN'ed mprotect
the un-mprotect() after run was severly off the limits.

Also in tcc.c:main: do not confuse errors with non-zero
results from tcc_run()
2024-02-29 22:52:02 +01:00
Ekaitz Zarraga 7bc0cb5ba0 riscv: Implement large addend for global address 2024-02-29 19:42:02 +01:00
herman ten brugge 2656f8edcc Fix win64 noreturn problem
On win64 the code would hang in longjump with previous change
2024-02-28 07:37:09 +01:00
herman ten brugge f9cb198aa8 tccrun: update for apple and openbsd
Apple needs CONFIG_RUNMEM_RO=1
I now only set CONFIG_RUNMEM_RO=0 on _WIN32

Openbsd does not have malloc.h so remove some code

Also fix some warnings when compiling lib with gcc
2024-02-26 07:18:07 +01:00
grischka 4e91d38ddc tccrun: resign from "advanced" system calls (memaligh/gettid)
... let's stay compatible
2024-02-25 19:55:06 +01:00
grischka d2f8ceac7a tccrun: review last changes
- LIBTCCAPI int tcc_set_backtrace_func(void *ud, ...)
  accept opaque user data pointer,
- tcc -vv -run... : show section info
- use memalign() to allocate runtime memory
- printline_/dwarf : pass output to parent function
- tccpe.c : fix -nostdlib -run
- --config-backtrace=no : make it work again
2024-02-19 17:45:44 +01:00
grischka c88b19966c tccrun: exit() via rt_longjmp()
- new LIBTCC API tcc_setjmp() to allow longjmps & signals
  from compiled code back to libtcc per TCCState
- new LIBTCC API tcc_set_backtrace_func() to handle backtrace output
- move c/dtor/atexit stuff to runtime (lib/runmain.c)
- move bt-log.o into libtcc1.a
- add timeouts to github action (beware, it did happen to hang
  infinitely in the signal handler at some point)
2024-02-15 18:45:49 +01:00
herman ten brugge 8d8d75ca75 Allow tcc to run with bounds checking enabled
tcc failed to run with bounds checking enabled because the functions
rt_wait_sem, rt_post_sem and _rt_error where defined twice.
This is solved by making them weak in tccrun.c

Also a nested lock was present when setting TCC_BOUNDS_PRINT_CALLS=1
This is solved in lib/bt-exe.c by moving lock/unlock code.

Also added a testcase in tests/Makefile to test tcc with bounds
checking enabled.
2024-02-15 07:17:15 +01:00
Detlef Riekenberg f2e8b2ad79 bcheck: fix argument order for memalign
Spotted by George Sedov on the mailing list.

The bug was in the tcc source since >20 years:
https://repo.or.cz/tinycc.git?a=commit;h=ad28d4c5b03da27dc0381afb58f255d49962cca0

manpage for memalign:
https://linux.die.net/man/3/memalign

--
Regards ... Detlef
2024-02-15 00:56:54 +01:00
grischka a7cd016d71 tccrun: 'tcc_relocate()' twice no longer supported
- abort with notice when tcc_relocate() is called with the
  former two-step method
- support backtrace & bcheck not only with tcc_run() but also
  for directly called functions from tcc_get_symbol(); enable
  witn 'tcc_set_options("-bt/-b");'
- move struct rt_context and debug sections into compiled code
  for TCC_OUTPUT_MEMORY also
- protect access (g_rc) with semaphore
Also:
- add armv7/aarch4/riscv64 github tests (qemu emulated)
- win32/build-tcc.bat: build cross compiler only with -x
2024-02-14 00:56:36 +01:00
grischka 7b9f19eaab tccpp: macro cleanup
- remove TOK_NOSUBST, mark the token itself instead
- get_tok_str(); mask out SYM_FIELD & update uses
- next(): optimize (~5% faster with tcc -E)
- tok_flags: remove some redundancy
- parse_define(): do not remove spaces around '##' and after '#'
    and mark macros with '##' as MACRO_JOIN to avoid unnecessary
    call to macro_twosharps(mstr):
- next_nomacro(): removed, next_nomacro1(): renamed to next_nomacro()
- next_argstream(): cleanup & new function peek_file()
- macro_subst_tok(): handle special macros (__DATE__ etc.)
  like normal macros if they are #defined
- -DPP_DEBUG : more structured output
- pp_error(): better preprocessor expression error message
- tcctok.h: sort basic keywords (somehow)
- testspp/Makefile: generate .expect with 'make testspp.##+'
- tcc.c: tcc -E -o file : put unixy LFs also on windows
2024-02-09 18:38:14 +01:00
grischka b671fc0594 LIBTCCAPI tcc_relocate(s) : REMOVED 2nd argument
removed second argument for tcc_relocate(s). previous
'TCC_RELOCATE_AUTO' is now default and only behavior.

Rationale:
  In the past, the option to compile into memory provided by the
  user was introduced because only one TCCState could exist at a time.

  This is no longer a limitation.  As such it is also possible now to
  keep any number of compiled code snippets around together with their
  state in order to run them as needed.

- Also
  - LIBTCCAPI tcc_get_error_func/opaque() removed
  - tccrun/SELINUX: switch rx/rw mappings such that rx comes first
    (risc64-link.c:relocate_plt() does not like got < plt)
  - tcc_relocate_ex(): free local symbols and obsolete sections
    to reduce memory after tcc_relocate()
2024-02-09 13:38:27 +01:00
herman ten brugge a0ab99169e Pointer diff should use signed size 2024-02-09 08:35:32 +01:00
kbkpbot 76d605192d add MemoryBarrier marco define; tested gcc msvc 2024-02-08 11:13:51 +08:00
herman ten brugge cd6ad857cf Fix default_reallocator declaration 2024-02-07 07:01:36 +01:00
Eric Raible be6584e2b9 Allow use of a custom allocator in libtcc
When using libtcc it's reasonable to be able to use the application's
memory allocator for all allocations, including tcc_new(), and including
#define MEM_DEBUG

Ideally the allocator would be stored in the TCCState, like TCCErrorFunc.
That would imply a new API tcc_new_with_allocator(), but more importantly
would require all uses of tcc_malloc(x) to be changed to s->tcc_malloc(x).
That's a non-starter in my book.

Instead I refactored the memory management code so that all allocations
flow through tcc_realloc().  Which simply calls a function pointer, the
default value of which is the previous tcc_realloc().

It then becomess trivial to install a new allocator with the new function:
LIBTCCAPI void tcc_set_realloc(TCCReallocFunc realloc);

The resulting code adds the trivial cost of an additional function call
per allocation/free.  It also doesn't distinguish between malloc failure
and realloc failure, but since both just fprintf then exit() that seems
unimportant to me.

On the plus side the preprocessor magic is much more clear.  The diffs
don't hightlight that, but take a look at the result to see if you agree.

All tests passed on my x86 linux box.
2024-02-06 20:01:18 -08:00
kbkpbot 105d70f7b4 atomic_load/atomic_store : to ensure return the latest value, should we add a memory barrier here?
If have no these memory barriers, sometime it will cause bug in multiple threads program.
2024-02-05 08:37:41 +08:00
grischka da0d43903b review recent commits
tccpp.c:
 - revert "Preprocessor fix + new testcase"
   Fix was not a fix and nobody could understand the test.
   This reverts 6379f2ee76
 - better fix and add new test (pp/18.c)

tccgen.c:
 - remove global variables 'in_sizeof', 'constant_p'
 - rework comma expression (gexpr())
 - merge func/data 'alias_target' codes
   (See 08c777053c)
 - move call to do_Static_assert()
 - better error: "expression expected before '%s'"
 - fix "statement after label"
    - remove unnecessary second parameter to block()
    - remove unnecessary call to decl()
    - revert changes to old C89 test file
    See 7f0a28f6ca

tccelf.c:
 - rework "...make undefined global symbol STT_NOTYPE"
   (See f44060f8fc)
 - move tccelf_add_crtbegin() from libtcc.c

tcctest:
 - unfix K&R fix (keep old look of K&R functions)

tccrun.c:
 - exit(0) returns 0

libtcc.c:
 - move #defines for -dumpmachine
 - more explicit error "file not found"
   (as opposed to error while loading file)

tccpe.c, x86_64-gen.c, i386-asm.c, tccasm.c:
 - use R_X86_64_PLT32 for functions on x86_64-win32

tccdefs.h
 - empty #defines for _Nonnull, __has_builtin(), etc.

configure:
 - Simpler "macOS .dylib ... VERSION letters."
   (See 6b967b1285)

Makefile:
 - macOS version also
 - add cross searchpaths for packages

build.yml:
 - disable codesign on macos-11 (doesn't seem to work)
2024-02-04 18:18:40 +01:00
Ekaitz Zarraga 6426cc3384 Revert "riscv64-gen: Fix `load` and `store` type_size usage"
It was already contemplated by c81116e29a.

This reverts commit c7263571d2.
2024-01-30 17:41:26 +01:00
Ekaitz Zarraga c7263571d2 riscv64-gen: Fix `load` and `store` type_size usage
In `load` and `store` RISC-V gen used `type_size` to retrieve the size
of the types being moved around, the problem with that function is it
tries to obtain internal information of the type. When using `type_size`
with a `char *` it returns 1, and emits a `lb` instruction when using a
conditional expression like so (but probably also in other cases):

    char *a, *b;
    b = "hello";
    a = x ? b : "" ;                // this emits an `lb`

That `lb` clobbers the pointer, only loading the latest byte of it:

    // if `b` was, say: 0x1f822e
    a = b;
    // now `a` is 0x00002e

NOTE: We spotted this when building make-3.82, in `init_switches`
      function in the `main.c` file. This error made `make` unable to
      run long options like `make --help` (segfault when doing the
      `strlen` later)

This happens because a `char *` is internally stored as a `char[1]` or
something similar, it's result is the size of a `char` (1) times the
size of the array `1`. This is not what we want, we are copying the
pointer, not the array itself. Using `type_size` for this is not
appropriate even if it works for some cases.

If the conditional expression is rewritten to imperative style using an
`if` it works properly, so it might be related with the fact the pointer
is `load`ed to a register.

`load` and `store` should only work with integral types, so reading the
size of the array is not useful. This commit creates a simpler version
of this function that only reads the integral type of what's working
with: char * is considered just a pointer. So it makes an `ld` instead,
and the code above works.
2024-01-29 11:09:59 +01:00
herman ten brugge 7f0a28f6ca C23: Implement declaration after label
Only allow declaration after label if it is inside '{...}'
2024-01-28 07:18:12 +01:00
herman ten brugge 7d1bbc80d4 Update for clang
Clang also removed K&R support so define IMPLICIT_INT.
Fix clang warning in lib/bt-log.c
2024-01-17 07:07:48 +01:00
herman ten brugge 0059d89c0f Prepare for gcc 14
Gcc 14 reports -Wimplicit-int errors because old K&R is not supported
any more.
2024-01-16 22:21:19 +01:00
herman ten brugge bbe2e5a421 Allow declarations in case statement
This now works:

 case 1:
    int z = 123;
    break;
2024-01-16 07:51:56 +01:00
Andrius Štikonas 04365dd4c9 riscv64-asm.c: fix assembly instruction with negative immediate offsets.
This fixes expressions like ld a0, s0, -24 that regressed in
d87801bd50
2024-01-12 21:09:21 +00:00
herman ten brugge 6120656cbf Rewrite gexpr a bit 2024-01-08 11:34:47 +01:00
herman ten brugge 2701dcfb06 Add some relocations to riscv64-link.c
dlltest failed on a riscv machine.
2024-01-07 07:45:31 +01:00
herman ten brugge c13bbb5cb5 Add type promotion in comma expression and update testcase 94 2024-01-06 07:54:34 +01:00