Commit Graph

813 Commits

Author SHA1 Message Date
Janneke Nieuwenhuizen 98f37a5bdb squash! mescc: Fix switch statements' fallthrough 2023-09-17 07:44:16 +02:00
Janneke Nieuwenhuizen 6ec2715f74 Revert "squash! mescc: Fix switch statements' fallthrough --support mes"
This reverts commit ef4f4bb25a.
2023-09-17 07:41:03 +02:00
Janneke Nieuwenhuizen c65ce2798b Revert "squash! squash! mescc: Fix switch statements' fallthrough --support mes"
This reverts commit 3266f6a834.
2023-09-17 07:41:01 +02:00
Janneke Nieuwenhuizen 3266f6a834 squash! squash! mescc: Fix switch statements' fallthrough --support mes 2023-09-16 17:38:48 +02:00
Janneke Nieuwenhuizen ef4f4bb25a squash! mescc: Fix switch statements' fallthrough --support mes 2023-09-16 08:19:02 +02:00
Ekaitz 1ec88d0109 mescc: Fix switch statements' fallthrough
Flattens case structures as nyacc is giving consecutive cases as a
nested block like:

    (case testA
      (case testB
        (case testC BODY)))

We convert to:

    ((case testA (expr-stmt))
     (case testB (expr-stmt))
     (case testC BODY))

And then treat them as independent cases. For the fallthrough we just
add a jump to each case's body right before its clause (each of the case
blocks is responsible of adding its own jump to its body):

        // This doesn't have it because it's the first
    CASE1:
        testA
    CASE1_BODY:

        goto CASE2_BODY
    CASE2:
        testB
    CASE2_BODY:

        goto CASE3_BODY
    CASE3:
        testB
    CASE3_BODY:

This enables complex fallthrough schemes comparing to what was done
before.

* module/mescc/compile.scm
  (ast->info)[switch]{flatten-cases}: New variable.
  (ast->info)[switch]{statements}: Use flatten-cases on it.
  (switch->expr): Remove unneeded matchers and add jumps to body.
* build-aux/check-mescc.sh(xfail-tests): Remove
  lib/tests/scaffold/66-local-char-array.c
2023-09-13 13:48:33 +02:00
Ekaitz 4e9f08e2af mescc: Add support for signed rotation.
* module/mescc/compile.scm (expr->register)[rshift, assn-expr]: Add
signed right-rotation support.
* lib/arm-mes/arm.M1 (asr): Add instruction.
* lib/x86-mes/x86.M1 (sar): Add instruction.
* lib/x86_64-mes/x86_64.M1 (sar): Add instruction.
* module/mescc/armv4/as.scm (armv4:r0>>r1-signed): New procedure.
(armv4:instructions): Register it.
* module/mescc/i386/as.scm (i386:r0>>r1-signed): New procedure.
(i386:instructions): Register it.
* module/mescc/riscv64/as.scm (riscv64:r0>>r1-signed): New procedure.
(riscv64:instructions): Register it.
* module/mescc/x86_64/as.scm (x86_64:r0>>r1-signed): New procedure.
(x86_64:instructions): Register it.
(
2023-09-12 11:04:11 +02:00
Ekaitz b12f56c922 mescc: Initialize missing struct fields to 0.
This is a follow-up to commits
    7a8a2fc517
    mescc: x86_64 support: Refactor to abstracted assembly, add x86_64.

and

    c9ba7a619b
    mescc: Refactor variable declaration.

There was a debug "22" leaking in compile.scm.  It should be a "0".

* module/mescc/compile.scm (init-local): Replace "22" with "0".
* lib/tests/scaffold/7v-struct-initialize-zeroes.c: New test.
* build-aux/check-mescc.sh (tcc_tests): Add it.
(xfail-tests): Remove lib/tests/scaffold/72-typedef-struct-def-local.c.
2023-09-12 11:04:11 +02:00
Andrius Štikonas e42cf58d14 riscv64: Port to word based mescc-tools.
* module/mescc/M1.scm (riscv:i-format, riscv:j-format, riscv:u-format):
New procedures for RISC-V instruction formats.
(info->M1): Use them to switch from !0xAB to M1
weird strings 'AB'.
* module/mescc/riscv64/as.scm,
lib/linux/riscv64-mes-m2/_exit.c
ib/linux/riscv64-mes-m2/_write.c,.
lib/linux/riscv64-mes-m2/crt1.M1,.
lib/linux/riscv64-mes-m2/syscall.c,.
lib/linux/riscv64-mes-mescc/_exit.c,.
lib/linux/riscv64-mes-mescc/_write.c,.
lib/linux/riscv64-mes-mescc/crt1.c,.
lib/linux/riscv64-mes-mescc/syscall-internal.c,.
lib/linux/riscv64-mes-mescc/syscall.c,.
lib/m2/riscv64/riscv64_defs.M1,.
lib/riscv64-mes-mescc/setjmp.c,.
lib/riscv64-mes/riscv64.M1: Switch to riscv64 word-based macros.
* lib/linux/open.c (open)[!SYS_open]: Add support using openat syscall.
* include/linux/riscv64/syscall.h (MAKESTRING, MAKESTRING2,
RISCV_SYSCALL): New macros.
2023-09-12 11:04:11 +02:00
Jan (janneke) Nieuwenhuizen 3c767a3bf9 mescc: Use size 8 for stack.
Reported by  W. J. van der Laan <laanwj@protonmail.com>.

* module/mescc/x86_64/as.scm (x86_64:function-locals): Oops, use size 8.
2023-09-12 11:04:11 +02:00
W. J. van der Laan dc6af19506 mescc: RISC-V64 code generation.
* mes/module/mescc/mescc.mes: Import riscv64 code generation modules.
* mes/module/mescc/riscv64/as.mes: Imports for as.mes.
* mes/module/mescc/riscv64/info.mes: Imports for info.mes.
* module/mescc/mescc.scm (replace-suffix, arch-get, arch-get-info,
arch-get-machine, arch-get-m1-macros, .E?, .s?, .o?): Handle riscv64 and
some stubs for riscv32.
(arch-get-defines): Add defines for riscv32 and riscv64.
* module/mescc/riscv64/as.scm: New file: Code generator module for RISC-V64.
* module/mescc/riscv64/info.scm: New file: Architecture info for RISC-V64.
* build-aux/build-guile.sh (SCM_FILES): Add them.
2023-09-12 11:04:11 +02:00
W. J. van der Laan 55b42018db mescc: Add r0-cmp-r1 instruction.
This instruction is used to compare two registers and set the flags
accordingly. In current architectures this is the same as r0-r1, but for
RISCV it will be different.  RISC-V does not have condition flags so
(until a better solution) we are going to emulate them there.

* module/mescc/armv4/as.scm (armv4:instructions): Add r0-cmp-r1 as alias
of r0-r1.
* module/mescc/i386/as.scm: Same.
* module/mescc/x86_64/as.scm: Same.
* module/mescc/compile.scm (expr->register): Make use of the new
r0-cmp-r1 instruction.
2023-09-12 11:04:10 +02:00
Janneke Nieuwenhuizen 6117207d72 squash! HACK mescc: Consider truncate after each shift operation.
Instead of truncating on size of first operand, truncate on largest of
default/int or size of first operand
2023-09-12 11:04:02 +02:00
Janneke Nieuwenhuizen 57b60fc794 HACK mescc: Consider truncate after each shift operation.
XXX For every binop / binop* operation?  We need tests?

* module/mescc/compile.scm (expr->register): After lshift and rshift,
use convert-r0.
2023-09-12 11:04:02 +02:00
Andrius Štikonas 08b1e9fefe mescc: Do not overflow M1 64bit immediates.
M1 immediates are limited to 31-bit before they become negative.

* module/mescc/x86_64/as.scm (x86_64:value->r, x86_64:r-cmp-value,
x86_64:r-long-mem-add, x86_64:r-and): Limit M1 immediate to #x80000000.

* lib/tests/scaffold/67-m1-overflow-check.c: New test.
* build-aux/check-mescc.sh (mes_tests): Add it.
2023-09-12 11:04:02 +02:00
Andrius Štikonas abf677a8c3 mescc: Fix hex2:immediate8 to work with mes/m2-compiled mes.
* module/mescc/M1.scm (hex2:immediate8): Remove mesc? exception.
(mesc?): Remove.
2023-09-12 11:04:02 +02:00
Janneke Nieuwenhuizen 102fe7dc58 mescc: Remove duplicate include.
* module/mescc/preprocess.scm (mescc): Remove second (nyacc lang c99
parser) include.
2023-08-27 10:05:33 +02:00
Ekaitz ff02777236 mes: Add %program to mes-compatibility for Guile.
This fixes running scaffold/boot/gc.scm with Guile.

* module/mes/guile.scm (%program): Define and export.
2023-08-24 15:28:35 +02:00
Janneke Nieuwenhuizen ad7ce36b5b build: Drop support for mescc-tools 0.5.2.
* build-aux/build-scaffold.sh (stage0_cpu_flag): Remove.  Update users.
* build-aux/cflags.sh (stage0_arch): Remove.
* build-aux/config.sh.in (numbered_arch): Remove.
* configure (main): Remove check for numbered-arch?.  Remove
substitution of @numbered-arch@.
* configure.sh (numbered_arch): Likewise.
* module/mescc.scm (%numbered-arch?): Remove.
(parse-opts): Remove support for --numbered-arch?
* module/mescc/mescc.scm (mescc:compile): Remove alignment exception for
numbered-arch?.
(infos->hex2): Likewise.
(arch-get-architecture): Remove support for numbered-arch?.
* scripts/mescc.in (numbered_arch): Remove.
* scripts/mescc.scm.in (%numbered-arch?): Remove.
("%numbered_arch"): Remove.
2023-05-03 14:52:25 +02:00
Jan (janneke) Nieuwenhuizen cfb620567a
mescc: Fix Guile warnings.
* module/mescc/mescc.scm (GUILE-with-output-to-file): Remove.n
(with-output-to-file): Rename to...
(with-output-to-file*): ...this.
(mescc:preprocess, mescc:compile, infos->hex2): Update callers.
2022-10-19 10:01:48 +02:00
Ekaitz f89fed3c3b
doc: Fix typos.
* doc/mes.texi (Invoking mescc): Fix typo.
* module/mescc.scm (parse-opts): Likewise.
2022-09-22 11:14:34 +02:00
Jan (janneke) Nieuwenhuizen 2cfce3186b
ARM: Use explicit remainder in __mesabi_uldiv call.
This inexplicably fixes a segfault in tcc-boot0.

* module/mescc/armv4/as.scm (armv4:r0/r1): Instead of using "push 0",
use explicit slot for remainder.
2022-09-13 13:52:03 +02:00
Jan (janneke) Nieuwenhuizen eee5398f3b
ARM: Fix unsigned modulo.
* lib/arm-mes/arm.M1 (mov____%esp,%r3): New macro.
* module/mescc/armv4/as.scm (armv4:r0%r1): Use it to avoid overwriting
%r0 before caling __mesabi_uldiv.
2022-09-12 09:47:28 +02:00
Jan (janneke) Nieuwenhuizen aa128eaa92
build: Depend on mescc-tools 1.4.0.
* configure (main): Check for hex2, M1 1.4.0, and blood-elf 2.0.0.
* module/mescc/mescc.scm (M1->blood-elf): Use --little-endian with blood-elf call.
* kaem.run,
scaffold/argv.kaem,
scaffold/global-array.kaem,
scaffold/hello.kaem,
scaffold/local-array.kaem,
scaffold/local-static-array.kaem,
scaffold/main.kaem,
scaffold/read.kaem,
simple.make: Likewise.
2022-05-02 07:46:01 +02:00
Jan (janneke) Nieuwenhuizen 348dcbcbd7
mescc: Only use signed division when numerator is signed.
Currently, sign-extension (CLTD vs XOR EDX) and signed division (IDIV vs
DIV) selected by setting "signed?".  See, module/mescc/x86/as.scm.
Possibly we need a signed? based on numerator and denominator, and
add a extend-sign? based on the signedness of the numerator?

* module/mescc/compile.scm (expr->register): Base "signed?" strictly on
signedness of numerator.
2021-01-01 10:31:46 +01:00
Jan (janneke) Nieuwenhuizen fac6a873f7
mescc: Use unsigned type for sizeof expression.
* module/mescc/compile.scm (ast->type): Use unsigned type for sizeof
expression.
2021-01-01 10:30:48 +01:00
Jan (janneke) Nieuwenhuizen da03a384c8
mescc: Respect integer literal type suffixes.
* module/mescc/compile.scm (ast->type): Respect ULL, UL, U, LL, L suffix
on integer literals.
2021-01-01 10:28:24 +01:00
Jan (janneke) Nieuwenhuizen a551d9dcde
mescc: Use (format (current-error-port) ...) instead of stderr.
* module/mescc/M1.scm: Use (format (current-error-port) ...) instead of
stderr.
* module/mescc/compile.scm: Likewise.
* module/mescc/mescc.scm: Likewise.
* module/mescc/preprocess.scm: Likewise.
2020-12-30 21:20:19 +01:00
Jan (janneke) Nieuwenhuizen 7932d4bad7
core: Remove core:make-cell.
* src/gc.c (alloc, make_cell, cons): Move from mes.c
* src/mes.c: (make_cell_): Remove.
* src/lib.c (char_to_integer, integer_to_char): New function.
* src/builtins.c (mes_builtins): Add them; remove make_cell_.
* mes/module/mes/type-0.mes (char->integer, integer->char): Remove.
2020-05-18 00:40:50 +02:00
Jan (janneke) Nieuwenhuizen c22c1a82d9
mescc: Update to mescc-tools-1.0.0 option names.
* module/mescc/mescc.scm (M1->hex2):  Update to use --little-endian.
(hex2->elf): Likewise, and --base-address.
* module/mescc/mescc.scm (hex2->elf): Likewise, and remove
--exec_enable.
2021-10-10 09:06:50 +02:00
Jan (janneke) Nieuwenhuizen 3539572f9c
Relicense imported LGPL v3+ files to GPL v3+.
Fixes https://savannah.nongnu.org/task/?16067.
Reported via savannah by Ineiev <ineiev@gnu.org>.

* mes/module/mes/lalr.scm,
module/mes/getopt-long.scm,
module/mes/optargs.scm: Change header to GNU Mes header with GPL v3.
2021-11-09 12:01:04 +01:00
W. J. van der Laan 191d822e95
mescc: Make -m64 work again.
* module/mescc/mescc.scm (arch-get-machine): Make that -m64 on the command
line, which sets machine to "64", returns "64" and not "32".
2021-05-07 07:55:25 +02:00
W. J. van der Laan c480c7e602
mescc: Pass --64 to bloodelf for 64 bit machines.
* module/mescc/mescc.scm (M1->blood-elf): Pass --64 argument to bloodelf
for 64-bit machines.  This makes it able to generate the correct ELF
format.
2021-05-02 16:45:24 +02:00
Jan (janneke) Nieuwenhuizen 3a5a7d1e56
mescc: Support --bootstrap build on ARM.
The ARM port added the `-marm' option to mescc, where previously only
-m32' (the default) and `-m64' were supported.

Reported by Vagrant Cascadian <vagrant@reproducible-builds.org>.

* module/mescc/mescc.scm (arch-get-machine): When machine is "arm",
return "32".
* build-aux/build-mes.sh (LIBS)[mescc]: Add -lmescc.
2021-05-01 16:10:49 +02:00
Jan (janneke) Nieuwenhuizen 5bb568e455
mescc: Change --align to --align=functions, --align=globals.
This makes function alignment optional and disables function alignment
when using MesCC-Tools 0.5.2 (numbered architecture).

* module/mescc.scm (parse-opts): Make --align take a value.
* module/mescc/mescc.scm (mescc:compile, infos->hex2): Parse it and pass
it as renamed #:align keyword argument to ...
* module/mescc/M1.scm (infos->M1): ...here.  Rename parameter align? to
align, pass it to...
(info->M1): ...here.  Likewise.  Use it to make function alignment
optional.
2021-01-23 08:43:47 +01:00
Jan (janneke) Nieuwenhuizen 1e102e1d46
mescc: Skip attributes on function definitions.
* module/mescc/preprocess.scm (ast-strip-attributes): New procedure.
(c99-input->ast): Use it.
2021-01-23 08:43:47 +01:00
Jan (janneke) Nieuwenhuizen afc922d1e2
mescc: Add __SIZEOF defines for int, long, long long.
* module/mescc/mescc.scm (arch-get-define): Rename
to...  (arch-get-defines): ...this.  Return a list of defines: Also
adding __SIZEOF_INT__, __SIZEOF_LONG__, and add __SIZEOF_LONG_LONG__ if
it's >=8.
(mescc:preprocess, c->info): Update callers.
* include/stdint.h[!__SIZEOF_LONG_LONG__]: Remove typedefs for int64_t,
uint16_t.
2021-01-23 08:43:45 +01:00
Jan (janneke) Nieuwenhuizen bfd6792d07
ARM: Revert to 4 byte types only.
* module/mescc/armv4/info.scm (armv4:type-alist): Use size 4 for long
longs too.
2021-01-23 08:43:40 +01:00
Jan (janneke) Nieuwenhuizen 3f70993dd2
mescc: Update -g help text.
* module/mescc.scm (parse-opts): Remove TODO, mention call stack.
2021-01-23 08:41:13 +01:00
Jan (janneke) Nieuwenhuizen fa4147a284
mescc: Use signed division for x86, x86_64 when appropriate.
This fixes 36-compare-arithmetic.c

* module/mescc/i386/as.scm (i386:r0/r1, i386:r0%r1): Do not reset
signed?.
* module/mescc/x86_64/as.scm (x86_64:r0/r1, x86_64:r0%r1): Likewise.
* lib/tests/scaffold/36-compare-arithmetic.c (main): Use unique exit
value per failure.
2020-10-06 19:53:02 +02:00
Jan (janneke) Nieuwenhuizen be87ada6b5
build: Resurrect running mescc from elsewhere.
After setting-up a buld environment, running something like

   ~/src/mes/wip/pre-inst-env mescc -v -v ~/src/mes/wip/scaffold/main.c

works again.

TODO: Make this less complex.

 * The mescc-lib (and gcc-lib) build directories do not help;
   these were introduced to support `wip-autotools'.
 * We used to opt for short *PATH variables: "." rather than
   /gnu/store/.... or /home/janneke/src/mes/master to avoid
   filling the MES arena. XXX Has this been solved?
 * In the Guix bootstrap, we run module/mescc.scm directly
       $MES -e '(mescc)' module/mescc.scm

   ...so that's why these overrides started to appear in multiple
   places.

* build-aux/pre-inst-env.in (MES_UNINSTALLED): New variable.
* scripts/mescc.in (includedir,libdir): Use it to override these.
* module/mescc/mescc.scm (arch-find): Use it to add <lib>-messc.
2020-10-06 10:44:50 +02:00
Danny Milosavljevic 443f662361
command line: Support "mescc --print-libgcc-file-name".
* module/mescc.scm (parse-opts): Add "--print-libgcc-file-name".
2020-10-06 10:04:41 +02:00
Danny Milosavljevic 95c71f3178
div: Move __aeabi_idiv to __GNUC__; introduce __mesabi_idiv.
* lib/mes/div.c (__aeabi_idiv): Move for conditional compilation.
(__mesabi_idiv): New procedure.
* module/mescc/armv4/as.scm (armv4:r0/r1): Use __mesabi_idiv.
2020-10-06 10:04:28 +02:00
Jan (janneke) Nieuwenhuizen eba5c15088
mescc: Add signed char.
* module/mescc/armv4/info.scm (armv4:type-alist): Add signed char.
* module/mescc/i386/info.scm (i386:type-alist): Likewise.
* module/mescc/x86_64/info.scm (x86_64:type-alist): Likewise.
2020-10-06 10:04:28 +02:00
Jan (janneke) Nieuwenhuizen 1c06c0170a
ARM: as: Fix ldrsb_ typo.
(armv4:byte-r0-mem->r1-mem): : Add missing "_" to ldrsb_.
2020-10-06 10:04:28 +02:00
Jan (janneke) Nieuwenhuizen a945bee494
ARM: as: Fix strh__ typo.
* module/mescc/armv4/as.scm (armv4:word-r0-mem->r1-mem): Add missing "_"
to strh__.
2020-10-06 10:04:28 +02:00
Jan (janneke) Nieuwenhuizen ddfdd1cb7f
mescc: Do not crash when attemting to link files without extension.
* module/mescc/mescc.scm (replace-suffix): Add ELSE clause to IF for
base; add IF for old-suffix.
2020-10-06 10:04:27 +02:00
Jan (janneke) Nieuwenhuizen f3f405a83a
ARM: as: Guile compile fix.
* module/mescc/armv4/as.scm (optimize-immediate): Do not unquote compare
functions (and numbers).
2020-08-24 11:24:02 +02:00
Danny Milosavljevic d9bda45853
ARM: Handle signed values in r-byte-mem-add, r-word-mem-add.
* lib/arm-mes/arm.M1 (ldrb___%r0,(%r1)): Delete macro.
(ldrh___%r0,(%r0)): Delete macro.
(ldrsh__%r0,(%r0)): New macro.
(ldrh___%r0,(%r1)): Delete macro.
(ldrsh__%r0,(%r1)): New macro.
(ldrh___%r1,(%r1)): Delete macro.
(ldrsh__%r1,(%r1)): New macro.
(ldrh___%r2,(%r2)): Delete macro.
(ldrsh__%r2,(%r2)): New macro.
(ldrh___%r3,(%r3)): Delete macro.
(ldrsh__%r3,(%r3)): New macro.
(ldrsb__%r0,(%r1)): New macro.
* module/mescc/armv4/as.scm (armv4:r-byte-mem-add): Use ldrsb.
(armv4:r-word-mem-add): Use ldrsh, add____$i32,(%r0).
(armv4:word-mem->r): Use ldrsh.
2020-06-19 02:45:33 +02:00
Danny Milosavljevic aaa174382e
ARM: Factor out optimize-immediate.
* module/mescc/armv4/as.scm (optimize-immediate): New macro.
(immediate->r0): Use it.
(armv4:value->r): Use it.
(armv4:local->r): Use it.
(armv4:r->local+n): Use it.
(armv4:r-byte-mem-add): Use it.
(armv4:r-word-mem-add): Use it.
(armv4:local-ptr->r): Use it.
(armv4:r+value): Use it.
(armv4:r-cmp-value): Use it.
(armv4:r0+value): Use it.
(armv4:byte-r->local+n): Use it.
(armv4:word-r->local+n): Use it.
2020-06-16 21:07:23 +02:00