1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00
Commit graph

4577 commits

Author SHA1 Message Date
Andy Wingo
48548df91e Fix effects analysis: field writes clobber object reads
* module/language/cps/effects-analysis.scm (compute-clobber-map):
Previously a whole-object read would not be clobbered by a specific
field write.  This crops up for the &read introduced at the site of
`cons` for the synthetic car and cdr definitions.  This error was there
before but didn't cause bugs before 3.0.10 because cons got eagerly
lowered to separate allocation and initialization instructions.
2024-03-20 11:50:53 +01:00
Andy Wingo
2c645571b3 Fix inlinable-exports serialization for kwargs
* module/language/tree-il/inlinable-exports.scm (inlinable-exp): Call
assq-ref in the right order.  Embarrassing!
* module/language/tree-il/peval.scm (peval): Print the symbols so that
we can diagnose later failures.
2024-03-18 16:40:32 +01:00
Andy Wingo
e15617dc0e Expose read-c-struct, write-c-struct syntax
* module/system/foreign.scm (read-c-struct): Rename from read-fields.
Export.
(write-c-struct): Rename from write-fields.  Export.
(%write-c-struct, %read-c-struct): Add % prefix to these private
bindings.
2024-03-17 21:40:58 +01:00
Andy Wingo
d7ae468c17 Rework read-fields, write-fields to not return offset
* module/system/foreign.scm (read-fields, write-fields): Don't return
the final offset, as the offset after the final field is not necessarily
the end of the struct, because of padding.
2024-03-17 21:40:58 +01:00
Andy Wingo
7fef214f6e Remove vestigial code from srfi-9
* module/srfi/srfi-9.scm (%define-record-type): No need to define
record-layout.
2024-03-17 21:40:58 +01:00
Andy Wingo
88e0933450 Rework make-c-struct, parse-c-struct
* module/system/foreign.scm (bytevector-complex-single-native-ref)
(bytevector-complex-single-native-set!)
(bytevector-complex-double-native-ref)
(bytevector-complex-double-native-set!): Be more static in our
definitions.
(compile-time-eval):
(switch/compile-time-keys): New helpers.
(align): Make available at compile-time.
(read-field, read-fields, write-field, write-fields): New helpers.  More
efficient than the alist.
(write-c-struct, read-c-struct): Rework in terms of new helpers.
(parse-c-struct): Just use sizeof to get the size.
2024-03-17 21:40:58 +01:00
Andy Wingo
2b58dea2d2 (scheme foreign): API is less configuration-dependent
* libguile/foreign.h:
* libguile/foreign.c: Always define complex-float and complex-double.
Fall back to alignof float / 2*sizeof float if no complex numbers.  (But
with C99 surely it exists everywhere.)
* module/system/foreign.scm (*writers*, *readers*): Always include
complex-float and complex-double readers and writers.
* test-suite/tests/foreign.test: Always run the complex tests.
2024-03-17 21:40:58 +01:00
Andy Wingo
137b0e85b9 (system base target) doesn't load (system foreign)
* module/system/base/target.scm (%native-word-size): Use
most-positive-fixnum instead of importing (system foreign).
2024-03-17 21:40:58 +01:00
Andy Wingo
17e8d7530f peval: Indenting. NFC.
* module/language/tree-il/peval.scm (peval): Reindent.
2024-03-13 20:28:43 +01:00
Andy Wingo
f95bf6921e peval: Enable inlining for functions with kwargs
* module/language/tree-il/peval.scm (peval): Handle all lambda inlining
the same, and extend with support for multiple clauses and keyword
arguments.
* test-suite/tests/peval.test ("case-lambda"): Enable kwarg inlining.
2024-03-13 20:19:59 +01:00
Andy Wingo
c758c99b5e New optimization: demux-lambda
Can help reduce case-lambda* / lambda* at Tree-IL optimization-time.

* module/language/tree-il/demux-lambda.scm: New file.
* am/bootstrap.am (SOURCES): Add new file.
* module/language/tree-il/optimize.scm (make-optimizer):
* module/system/base/optimize.scm (available-optimizations): Enable
demux-lambda at level 2.
2024-03-13 13:11:14 +01:00
Andy Wingo
9e0f03c5fd Macro-introduced top-level vars scope to their module even if unbound
* module/ice-9/psyntax.scm (analyze-variable): Previously, a reference
to a top-level variable in a module other than the current module would
be silently rewritten to reference the current module, if the variable
was unbound in its original module.  This was a hack from the early days
of when we extended psyntax to know about the module system.  Fix to
properly use the scope of the introduced binding instead of the scope of
the macro use site.
* test-suite/tests/syntax.test ("macro-introduced cross-module unbound
identifiers"): Add test.
* module/ice-9/psyntax-pp.scm: Regenerate.
2024-02-20 14:15:12 +01:00
Andy Wingo
2717773bb1 Regenerate psyntax-pp.scm
* module/ice-9/psyntax-pp.scm (syntax?): Regenerate.  With the modified
pretty-printer, things are a bit different.
2024-02-20 14:15:12 +01:00
Jonas Hahnfeld
fb1f5e28b1
Match on correct argument in make-custom-port
* module/ice-9/custom-ports.scm (make-custom-port): Match on correct
argument for conversion strategy.
* NEWS: Update.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2024-01-29 14:44:58 +01:00
Jonas Hahnfeld
5223c71d0d
Fix loading of custom-ports extension
* module/ice-9/custom-ports.scm: Load extension also in expand and eval.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2024-01-29 14:42:30 +01:00
Tomas Volf
b6866ded2b
ftw: Fix getuid-or-false, getgid-or-false macros.
Both macros were missing a quote for the procedure call, causing the
actual return value to be compiled into the ftw.go, instead of the
procedure call.  Snippet from disassembly of ftw.go does confirm that:

  55    (make-immediate 2 3990)         ;; 997                at ice-9/ftw.scm:319:46
  56    (make-long-immediate 1 120002)  ;; 30000              at ice-9/ftw.scm:320:46

That effectively prevented ftw from entering directories without access
for others.  Simple reproduction:

    scheme@(guile-user)> ,use (ice-9 ftw)
    scheme@(guile-user)> (mkdir "/tmp/xxxx")
    scheme@(guile-user)> (chmod "/tmp/xxxx" #o0700)
    scheme@(guile-user)> (ftw "/tmp/xxxx" (lambda (_ __ f) (pk f) #t))

    ;;; (directory-not-readable)
    $1 = #t
    scheme@(guile-user)> (system "ls -al /tmp/xxxx")
    total 0
    drwx------ 1 wolf wolf   0 Oct 11 22:54 .
    drwxrwxrwt 1 root root 888 Oct 11 22:54 ..
    $2 = 0

The fix is to quote the procedure call, leading to the intended
behavior.

Fixes <https://bugs.gnu.org/55344>.

* module/ice-9/ftw.scm (getuid-or-false): Quote the (getuid).
(getgid-or-false): Quote the (getgid).
* NEWS: Update.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2024-01-29 14:31:01 +01:00
Andy Wingo
bb5829f5ff default-frame-width is a parameter
* module/system/repl/debug.scm (default-frame-width): A parameter
instead of a fluid.
(print-frames): Adapt use.
2024-01-29 10:59:54 +01:00
Andy Wingo
1349c41a60 Ensure macro-introduced top-level identifiers are unique
* module/ice-9/psyntax.scm (expand-top-sequence): When making a fresh
name for an introduced identifier, the hash isn't enough: it's quite
possible for normal programs to have colliding hash values, because
Guile's hash functions on pairs doesn't traverse the whole tree.
Therefore, append a uniquifying counter if the introduced name is
already defined in the current expansion unit.
* test-suite/tests/syntax.test ("duplicate top-level introduced
definitions"): Add test.
2024-01-29 10:53:13 +01:00
Olivier Dion
455ee49f55
Fix asymetric mutex locking when joining thread.
If `join-thread' timeout, the thread mutex is not unlocked, resulting in
deadlock to the next call to it or deadlock of the thread itself when it
terminates.

Thus, always unlock the mutex.

Fixes <https://bugs.gnu.org/55356>.

* module/ice-9/threads.scm (join-thread): Always unlock thread mutex.
* test-suite/tests/threads.test (join-thread): New test to ensure the
mutex is released.
* NEWS: Update.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2024-01-25 23:12:53 +01:00
Ludovic Courtès
2bbeece034
Bump user-visible copyright years to 2024.
* module/ice-9/command-line.scm (version-etc):
* module/scripts/compile.scm (show-version):
* module/system/repl/common.scm (*version*): Bump to 2024.
2024-01-24 10:49:27 +01:00
Ludovic Courtès
7f26021c24
debug: Print wider stack frames when not writing to a tty.
This satisfies a longstanding complaint that Guile backtraces were being
truncated too much by default (72 columns), often hindering debugging.

* module/system/repl/debug.scm (default-frame-width): New variable.
(print-frames): Change default #:width value depending on whether PORT
is a tty.
2023-12-28 12:24:02 +01:00
Andy Wingo
d7cf5bf373 Recognize append as a primcall and optimize it
* module/language/tree-il/primitives.scm (*primitive-constructors*):
(append): Recognize append and reduce it to only the two-operand form.
* module/language/tree-il/peval.scm (peval): Add optimizations to
append.
2023-11-27 14:30:41 +01:00
Andy Wingo
38e9bd7a2f Avoid swallowing errors for (values) operands of elided primcalls
* module/language/tree-il/peval.scm (peval): When visiting (values) in
anything other than an effect or values context,
residualize (values (values)), which will cause a run-time error.
* test-suite/tests/peval.test ("values"): Add test.
2023-11-27 14:24:50 +01:00
Andy Wingo
3b7c78cc83 Unbound vars fixup
* module/language/tree-il/compile-cps.scm (%box-ref): Fix untested bug.
The peril of a compiler spanning modules...
2023-11-23 12:37:20 +01:00
Andy Wingo
5ef0ea30fa Allow targets to preclude unbound variables
Allowing variables to hold an unbound value and requiring a check on
each load is suboptimal; the fixing letrec boolean check is better.  So
other runtimes (hoot) might preclude unbound variables by construction.
Allow them to do so.

* module/language/cps/guile-vm.scm (target-has-unbound-boxes?): New
definition.
* module/language/tree-il/compile-cps.scm (target-has-unbound-boxes?):
(%box-ref): Only residualize an unbound check if the target has unbound
boxes.
2023-11-23 12:31:38 +01:00
Andy Wingo
83449a8683 prompts avoid introducing throw; fixup peval test
* test-suite/tests/peval.test ("partial evaluation"): Fix to expect
raise-type-error from dynwind peval.  Update prompt expectation
similarly.
* module/language/tree-il/primitives.scm (call-with-prompt): Use
raise-type-error.
2023-11-23 12:30:36 +01:00
Andy Wingo
711077586b peval avoids introducing 'throw
* module/language/tree-il/peval.scm (peval): Introduce raise-type-error
for dynwind unwinder thunk check.
* module/language/tree-il/compile-cps.scm (raise-type-error):
* module/language/tree-il/compile-bytecode.scm (canonicalize): Handle
raise-type-error, as it can be in Tree-IL now.
2023-11-23 12:11:22 +01:00
Andy Wingo
4118f09030 CPS conversion does not introduce "throw"
This keeps things higher level, and is a step towards structured
exceptions in guile.

* module/language/cps/guile-vm/reify-primitives.scm (reify-primitives):
Turn raise-type-error, raise-range-error, and raise-arity-error into
variants of "throw".  Lower raise-exception to a non-tail primcall.

* module/language/tree-il/compile-cps.scm: Instead of residualizing
"throw/value+data" throws, exceptions introduced by CPS lowering are
more structured: raise-type-error, raise-range-error, and
raise-arity-error.  Also, lower raise-exception to an ordinary `$throw`
instead of eagerly producing the non-tail call to a $prim.
2023-11-23 10:25:02 +01:00
Andy Wingo
d579848cb5 Fix bug lowering logand/immediate to ulogand/immediate
* module/language/cps/specialize-numbers.scm (logand/immediate): Define
a sigbits handler.
(specialize-operations): Require logand/immediate operand to be u64 to
lower to ulogand/immediate.  Shouldn't be necessary but even if only u64
bits are used, negative fixnums will have the sign bit set, which trips
up further unboxed uses which error if the operand to `scm->u64` is
negative.
* module/language/cps/type-fold.scm (rem): Emit logand/immediate.
2023-11-20 16:45:35 +01:00
Andy Wingo
89501a83ce Fix mistype in specialize-numbers
*
module/language/cps/specialize-numbers.scm (compute-specializable-u64-vars):
We were failing to match scm->u64/truncate.
2023-11-20 15:11:29 +01:00
Andy Wingo
5959531c54 Allow target runtime to override symbol hash
Also rework so that the symbol hash uses the low bits instead of high
bits.  We can do this because, for the guile-vm target, now we compute
the full target hash.

* module/language/cps/guile-vm.scm (jenkins-lookup3-hashword2):
(target-symbol-hash, target-symbol-hash-bits): New exported functions..
* module/language/cps/switch.scm (optimize-branch-chain): Change to use
target-symbol-hash and target-symbol-hash-bits from the current
target-runtime.
2023-11-20 14:23:14 +01:00
Andy Wingo
4d834bdc12 Add logand/immediate, ulogand/immediate primcalls
* libguile/jit.c (compile_ulogand_immediate, compile_ulogand_immediate_slow)
* libguile/vm-engine.c (ulogand_immediate): New JIT and interpreter
support for ulogand/immediate.

* module/language/cps/guile-vm/lower-primcalls.scm (string-ref):
(vtable-vtable?):
(vtable-field-boxed?): Emit ulogand/immediate.
* module/language/cps/guile-vm/reify-primitives.scm (reify-primitives):
Remove logand/immediate.  Only emit ulogand/immediate if the immediate
is a u8.  Refactor mul/immediate.

* module/language/cps/specialize-numbers.scm (specialize-operations):
Produce ulogand/immediate if the result is a u64.

* module/language/cps/effects-analysis.scm:
* module/language/cps/types.scm (logand/immediate): Add effect and type
inference for logand/immediate, ulogand/immediate,

* module/language/cps/utils.scm (primcall-raw-representations):
ulogand/immediate makes a u64.

* module/language/tree-il/compile-cps.scm (convert): Generate
logand/immediate if possible.

* module/language/cps/compile-bytecode.scm (compile-function):
* module/system/vm/assembler.scm (system): Add ulogand/immediate
emitter.

* libguile/loader.h (SCM_OBJCODE_MINOR_VERSION): Bump.
2023-11-20 13:43:47 +01:00
Andy Wingo
5b0c261b04 Mark symbol-hash primcall as having unboxed result
* module/language/cps/utils.scm (primcall-raw-representations): Add
symbol-hash.
2023-11-17 15:28:59 +01:00
Andy Wingo
49aa0940bc Add CPS primcall for symbol-hash
* module/language/cps/effects-analysis.scm: symbol-hash is effect-free.
* module/language/cps/guile-vm/lower-primcalls.scm (symbol-hash): Lower
to word-ref/immediate.
* module/language/cps/switch.scm (optimize-branch-chain): Emit
symbol-hash instead of word-ref/immediate.
* module/language/cps/types.scm (symbol-hash): Infer result.
2023-11-17 08:48:33 +01:00
Andy Wingo
c2a9380a42 peval: better primcall folding in effect contexts
* module/language/tree-il/peval.scm (peval): If a primcall is
effect-free, don't require that its args are too: just revisit args as a
sequence in effect context.
* module/language/tree-il/effects.scm (effect-free-primcall?): New
exported function.
2023-11-15 19:31:24 +01:00
Andy Wingo
28a428135f peval: elide effect-free primcalls in effect context
* module/language/tree-il/peval.scm (peval): fix-letrec can residualize
useless primcalls, when a let or letrec-bound var is unused.  Fix to
elide these.
2023-11-15 15:17:29 +01:00
Andy Wingo
e529db04a4 Add extensibility to Tree-IL effects analysis
* module/language/tree-il/effects.scm (add-primcall-effect-analyzer!):
New facility.
* module/language/tree-il/effects.scm (make-effects-analyzer): If a
primcall's args cause no effects, call out to a user-provided
effect-free? primitive for a primcall.  If true, the primcall will be
marked as depending on all effects but causing none; this will allow it
to be elided by letrectify or peval.
2023-11-15 14:59:02 +01:00
Andy Wingo
d08cc4f6e2 Allow string->utf8 to constant-fold
* module/language/tree-il/primitives.scm (*interesting-primitive-names*):
(*primitive-accessors*): Add string->utf8, utf8->string, and
string-utf8-length.
(primitive-module): New public function, moved here from (language
tree-il compile-bytecode).

* module/language/tree-il/compile-bytecode.scm: Use primitive-module
from (language tree-il primitives).

* module/language/tree-il/peval.scm (peval): A bugfix: load primitives
from their proper module.  Allows bytevector primitives to fold.

* module/language/cps/guile-vm/reify-primitives.scm:
* module/language/cps/effects-analysis.scm:
* module/language/cps/types.scm
* module/language/tree-il/primitives.scm:
* module/language/tree-il/cps-primitives.scm:
* module/language/tree-il/effects.scm (make-effects-analyzer):
Add string->utf8, utf8->string, and string-utf8-length.

* module/language/tree-il/compile-cps.scm (string->utf8)
(string-utf8-length, utf8->string): New custom lowerers, including type
checks and an unboxed result for string-utf8-length.

* module/system/vm/assembler.scm:
* libguile/intrinsics.h:
* libguile/intrinsics.c: Because string-utf8-length returns an unboxed
value, we need an intrinsic for it; go ahead and add an intrinsic for
string->utf8 and utf8->string too, as we will likely be able to use
these in the future.
2023-11-15 10:44:21 +01:00
Andy Wingo
437e5ac43d Fix pretty-print with a per-line prefix
* module/ice-9/pretty-print.scm (pretty-print): Include the per-line
prefix in the indent.
* test-suite/tests/print.test ("pretty-print"): Add test.
2023-11-15 09:35:23 +01:00
Andy Wingo
75cd95060f Update set of decimal zeroes for Unicode 15.1.0
* module/scheme/char.scm (*decimal-zeroes*): Update.
2023-11-02 11:31:32 +01:00
Daniel Llorens
4daf4664cf Add test for r7rs string-for-each
* test-suite/tests/r7rs.test: As stated.
* module/scheme/base.scm: Reindent and add minimal doc.
2023-09-29 16:51:27 +02:00
Taylan Kammer
3243d96bb5 Fix (scheme base) string-for-each.
* module/scheme/base.scm (r7:string-for-each): New procedure.

Fixes <https://bugs.gnu.org/40584>.
2023-09-29 15:28:20 +02:00
Andy Wingo
8441d8ff56 Fix docstring for symbol->string, string->symbol
* libguile/symbols.c (scm_symbol_to_string, scm_string_to_symbol):
Remove some confusing documentation that assumes that Guile is
case-insensitive, and which uses a word that may be perceived as a slur.
* module/ice-9/test.scm:
* test-suite/tests/r4rs.test: Rename a test to avoid using a slur.
2023-09-18 16:03:41 +02:00
Andy Wingo
1ad31adf30 Better compilation for symbol->keyword, keyword->symbol
* module/language/tree-il/primitives.scm (*interesting-primitive-names*):
(*effect-free-primitives*): Recognize keyword->symbol, symbol->keyword.
* module/language/tree-il/cps-primitives.scm: Plumb through to CPS.
(keyword->symbol):
* module/language/cps/effects-analysis.scm: New prims have no effect.
Fix effects for string->symbol.
(annotation->memory-kind): Add keywords.
* module/language/cps/guile-vm/lower-primcalls.scm (keyword->symbol):
Lower to scm-ref/immediate.
* module/language/cps/types.scm (annotation->type): Add case for
keywords.
* module/language/tree-il/compile-cps.scm: Add converters for new prims,
with type guards.
2023-09-18 12:32:33 +02:00
Andy Wingo
55256ab33f Better compilation for rational?, exact?, and so on
These numeric predicates now have CPS branching primcalls, which allows
type inference and folding to reduce them to less-strong instructions.

* module/language/cps/effects-analysis.scm (heap-numbers-equal?): Put
all the number predicates together.  None have type checks.
* module/language/cps/guile-vm/lower-primcalls.scm
(define-branching-primcall-alias): New helper.
(complex?): Same as number?.
* module/language/cps/guile-vm/lower-primcalls.scm (real?)
(rational?, integer?, exact-integer?, exact?, inexact?): Define
lowerers.
* module/language/cps/type-fold.scm (number?, complex?, real?)
(rational?, integer?, exact-integer?, exact?, inexact?): Add folders and
reducers for all of these.
* module/language/cps/type.scm (number?, complex?, real?)
(rational?, integer?, exact-integer?, exact?, inexact?): Add type
inference for these.
* module/language/tree-il/compile-cps.scm (convert): Add number? checks
before exact? and inexact?.  Remove the eager lowering of
exact-integer?; instead rely on folders.
* module/language/tree-il/cps-primitives.scm (number?, complex?)
(real?, rational?, integer?, exact-integer?, exact?, inexact?): Add
primitive decls.  Define as "number-type-predicates?", meaning they need
a number? guard.
2023-09-15 15:21:26 +02:00
Andy Wingo
d5347b59fb Better compilation of number?
* module/language/cps/guile-vm/lower-primcalls.scm (number?):
* module/language/tree-il/cps-primitives.scm (number?): Lower as CPS
branching predicate.
2023-09-15 11:19:55 +02:00
Andy Wingo
6756aeff95 Better compilation for symbol->string
* libguile/intrinsics.c (scm_bootstrap_intrinsics):
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS): Add symbol->string
intrinsic.
* module/language/cps/guile-vm/reify-primitives.scm (compute-known-primitives):
* module/language/tree-il/compile-bytecode.scm (+):
* module/language/tree-il/compile-cps.scm (symbol->string):
* module/language/tree-il/cps-primitives.scm (symbol->string):
* module/language/cps/effects-analysis.scm (symbol->string):
* module/language/cps/types.scm (symbol->keyword):
* module/system/vm/assembler.scm (symbol->string): Add the necessary
code to compile symbol->string.
2023-09-15 10:18:50 +02:00
Andy Wingo
c0715e0903 Recognize procedure? as a CPS primitive
* module/language/cps/effects-analysis.scm: Mark more predicates as
effect-free.  Sort the list.
* module/language/cps/guile-vm/lower-primcalls.scm (procedure?): Reify a
call to a primitive.  Sadly we can't elide the $kreceive, as even though
we know that it's single-valued, $call can't continue to $kargs (has to
be $callk).  Perhaps is worth relaxing in the future.
* module/language/cps/type-fold.scm: Define a number of additional
folders for disjoint types.
(procedure?): Define a folder for &procedure.  Has to include structs,
though.
* module/language/cps/types.scm: Same as for type-fold.scm.
* module/language/tree-il/cps-primitives.scm: Lower procedure? primcalls
to CPS.
2023-09-12 14:00:57 +02:00
Andy Wingo
b5bedf74ad Fix missing port in `pretty-print'
* module/ice-9/pretty-print.scm (pretty-print): We were defaulting to
the current output port.
2023-09-06 16:28:45 +02:00
Andy Wingo
a80ccec207 Add reprs for more s64 macro-instructions
* module/language/cps/utils.scm (primcall-raw-representations): Add
sadd, ssub, etc; these are lowered to uadd, usub, etc later for the
guile-vm target, but it is still useful to record their reprs for
pre-lowering analysis.
2023-09-05 11:54:31 +02:00