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

87 commits

Author SHA1 Message Date
Andy Wingo
c829531a46 fix alpha-rename for kwargs
* module/language/tree-il/optimize.scm (alpha-rename): Fix
  alpha-renaming of keyword arguments.
2011-09-24 17:09:40 +02:00
Ludovic Courtès
ec6e09bee7 peval: Rectify style.
* module/language/tree-il/optimize.scm (peval): Rename `src' to
  `lv-src', and `src2' to `src'; pass `make-let-values' the right source
  locations.  Reindent `let*'.
2011-09-23 18:12:28 +02:00
Andy Wingo
40bd6a7e57 peval comment & reindentation
* module/language/tree-il/optimize.scm (peval): Add a comment regarding
  failure modes, and reindent one clause.
2011-09-21 08:58:27 +02:00
Andy Wingo
9581febbb0 fix comment regarding alpha-renaming
* module/language/tree-il/optimize.scm (peval): Fix comment regarding
  alpha-renaming: it's not simply the allocator that needs unique names;
  rather, all transformations depend on it.
2011-09-21 08:58:27 +02:00
Andy Wingo
2605b6ba27 better pure-expression?
* module/language/tree-il/optimize.scm (peval): Allow dynref, fix, and
  let-values to be pure expressions.
2011-09-21 08:58:27 +02:00
Andy Wingo
ddbee5c00f more alpha-rename robustness
* module/language/tree-il/optimize.scm (alpha-rename): Handle all kinds
  of tree-il, with the current exceptions of lexical set!, prompt, and
  abort.
2011-09-21 08:58:27 +02:00
Andy Wingo
5d5e4f399a more robust alpha-renaming
* module/language/tree-il/optimize.scm (fresh-gensyms): New helper.
  (alpha-rename): Name the new gensyms using the old names as templates,
  not the old gensyms.  This prevents accidental collisions between
  gensyms, if #{x 1}# becomes #{x 12}# instead of #{x 2}#.
2011-09-21 08:58:27 +02:00
Andy Wingo
4f33b47591 peval: inlining of let-values
* module/language/tree-il/optimize.scm (peval): Add support for
  let-values.  Try to inline the consumer into the body of the producer,
  if there is only one return point, and we can figure out how many
  values are being returned, and that number is compatible with the
  consumer.
2011-09-21 08:58:09 +02:00
Andy Wingo
65a3265525 peval support for more forms
* module/language/tree-il/optimize.scm (peval): Add support for fix,
  dynwind, dynlet, dynref, module-set, and toplevel-set.  (Mutating a
  variable directly is similar to calling a function that does so behind
  our backs, so this presents no additional problem.)
2011-09-21 08:56:09 +02:00
Andy Wingo
d111abd0f6 more optimize.scm factoring
* module/language/tree-il/optimize.scm (vlist-any): New helper.
  (peval): Use it here.
2011-09-21 05:37:07 +02:00
Andy Wingo
0c448ef47b optimize.scm refactor
* module/language/tree-il/optimize.scm (let/ec, tree-il-any): New
  helpers.
  (code-contains-calls?): Use them here.
2011-09-21 05:37:07 +02:00
Ludovic Courtès
72b2ca55f6 peval: Abort inlining when the residual code contains recursive calls.
* module/language/tree-il/optimize.scm (code-contains-calls?): New
  procedure.
  (peval): Use it and abort inlining if the residual code of a procedure
  application contains recursive calls.  Suggested by Wingo, Waddell,
  and Dybvig.  Fixes <http://debbugs.gnu.org/9542>.

* test-suite/tests/tree-il.test ("partial evaluation"): Update 2 tests
  that relied on the previous behavior.  Add 1 another test.
2011-09-18 23:01:51 +02:00
Ludovic Courtès
2b0b09fed4 peval: Typo.
* module/language/tree-il/optimize.scm (peval)[maybe-unlambda]: Fix
  typo in comment.
2011-09-17 16:53:23 +02:00
Ludovic Courtès
2ae0775e40 peval: Alpha-rename anonymous lambdas that are duplicated.
* module/language/tree-il/optimize.scm (alpha-rename): New procedure.
  (peval)[maybe-unlambda]: Use it.

* test-suite/tests/tree-il.test ("partial evaluation"): Add two test
  cases for <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html>.
2011-09-17 16:49:41 +02:00
Ludovic Courtès
3f2d6efc7b peval: Use `resolve-primitives!'.
* module/language/tree-il/optimize.scm (peval): Add `cenv' optional
  argument; caller updated.
  Use `resolve-primitives!' to resolve <primitive-ref> expressions.
2011-09-13 23:34:07 +02:00
Ludovic Courtès
af1c6e424f peval: Clarify `pure-expression?'.
* module/language/tree-il/optimize.scm (peval)[pure-expression?]:
  Clarify the comment.
2011-09-13 23:30:22 +02:00
Ludovic Courtès
735249513a peval: Inline thunks.
* module/language/tree-il/optimize.scm (peval): Inline thunks.

* test-suite/tests/tree-il.test ("partial evaluation"): Add test.
2011-09-13 18:54:01 +02:00
Ludovic Courtès
61237fa4b9 peval: Use the right scope when replacing a lambda by a lexical-ref.
* module/language/tree-il/optimize.scm (peval)[maybe-unlambda]: New
  procedures.
  Use it to de-duplicate named lambdas.  This fixes the scoping bug
  described at <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html>.

* test-suite/tests/tree-il.test ("partial evaluation"): Add tests to
  reproduce the bug.
2011-09-13 18:25:09 +02:00
Ludovic Courtès
1e8ace33d1 peval: Add tests for inlining with both static & dynamic arguments.
* module/language/tree-il/optimize.scm (peval): Improve comment on the
  inlining heuristics.

* test-suite/tests/tree-il.test ("partial evaluation"): Add two tests.
2011-09-13 16:00:24 +02:00
Ludovic Courtès
870dfc609b peval: Propagate only pure expressions to lambdas.
* module/language/tree-il/optimize.scm (peval): Propagate ARGS to BODY
  only when all of ARGS are pure.  Change APP to use `maybe-unconst' for
  its arguments.

* test-suite/tests/tree-il.test ("partial evaluation"): Add tests for
  mutability preservation and non-propagation of non-constant arguments
  to lambdas.
2011-09-11 00:43:23 +02:00
Ludovic Courtès
89436781e8 peval: Try hard to preserve mutability.
* module/language/tree-il/optimize.scm (peval)[make-values]: Distinguish
  between 1 or another number of values.
  [mutable?, make-value-construction, maybe-unconst]: New procedures.
  Use it in <let>, <letrec>, <toplevel-define>, and <lambda-case>.

* test-suite/tests/tree-il.test ("partial evaluation"): Add tests
  for mutability preservation.
2011-09-11 00:43:23 +02:00
Ludovic Courtès
11671bbacb Add a partial evaluator for use in the compiler.
Thanks to William R. Cook for his excellent tutorial,
<http://softlang.uni-koblenz.de/dsl11/>.

* module/language/tree-il/optimize.scm (optimize!): Call `peval' unless
  the #:partial-eval? option asks otherwise.
  (peval): New procedure.

* module/language/tree-il/inline.scm: Add comment.

* module/language/tree-il/primitives.scm (*primitive-constructors*): New
  variable.
  (*effect-free-primitives*): Use it.
  (constructor-primitive?): New primitive.

* test-suite/tests/tree-il.test (assert-tree-il->glil): Extend to
  support `with-partial-evaluation', `without-partial-evaluation', and
  `with-options'.
  (peval): New binding.
  (pass-if-peval): New macro.
  ("lexical refs"): Run tests without partial evaluation.
  ("letrec"): Likewise.
  ("the or hack"): Likewise.
  ("conditional"): Likewise, for some tests.
  ("sequence"): Adjust to new generated code.
  ("partial evaluation"): New test prefix.
2011-09-09 00:05:34 +02:00
Andy Wingo
f95f82f8e1 compilation enviroments are always modules; simplifications & refactorings
* module/ice-9/boot-9.scm (make-fresh-user-module): New public function,
  makes an anonymous beautified module.

* module/language/objcode/spec.scm: We used to have some things in here
  that allowed lexical variable names and values to be a part of the
  environment, but no more. Now an environment is just a module. If you
  want to "inject" free variables into code, just use lambda.

* module/language/scheme/compile-tree-il.scm (compile-tree-il): Same
  here. Also, rely on the fact that an environment *will* be a module --
  because (system base compile) guarantees that for us.

* module/language/scheme/spec.scm (scheme): In the reader, rely on the
  environment being a module. Define a #:make-default-environment
  handler, which returns a beautified module, augmented with a fresh
  definition for current-reader, so that side effects to current-reader
  are restricted to the compilation unit.

* module/language/tree-il/analyze.scm
  (report-possibly-unbound-variables):
* module/language/tree-il/compile-glil.scm (compile-glil):
* module/language/tree-il/optimize.scm (optimize!): The environment will
  be a module.

* module/system/base/language.scm (<language>): New field,
  `make-default-environment'. Defaults to `make-fresh-user-module'.
  (default-environment): New accessor, returns a default environment for
  a language.

* module/system/repl/common.scm (repl-compile): Always compile relative
  to the current module, because a module is always acceptable as an
  environment.

* module/system/base/compile.scm (compile-file, compile-and-load): Both
  of these have a new keyword argument, #:env. For `compile-file', it
  defaults to the default environment of the source language, and for
  `compile-and-load', to the current module.
  (read-and-compile): If there are no expressions read, pass the joiner
  its default environment (via `default-environment joint').
2009-10-16 15:33:32 +02:00
Andy Wingo
4dcd84998f let-values in terms of syntax-case, add make-tree-il-folder
* module/language/tree-il.scm (tree-il-fold): Fix for let-values case.
  (make-tree-il-folder): New public macro, makes a multi-valued folder
  specific to the number of seeds that the user wants.
* module/language/tree-il/optimize.scm (optimize!): Reverse the order of
  inline! and fix-letrec!, as the latter might expose opportunities for
  the former.
* module/srfi/srfi-11.scm (let-values): Reimplement in terms of
  syntax-case, so that its expressions may reference hygienically bound
  variables. See the NEWS for the rationale.
  (let*-values): An empty let*-values still introduces a local `let'
  binding contour.
* module/system/base/syntax.scm (record-case): Yukkkk. Reimplement in
  terms of syntax-case. Ug-ly, but see the NEWS again: "Lexical bindings
  introduced by hygienic macros may not be referenced by nonhygienic
  macros."
2009-08-05 21:35:30 +02:00
Andy Wingo
c21c89b138 add <fix> tree-il construct, and compile it
* libguile/vm-i-system.c (fix-closure): New instruction, for wiring
  together fixpoint procedures.

* module/Makefile.am (TREE_IL_LANG_SOURCES): Add fix-letrec.scm.

* module/language/glil/compile-assembly.scm (glil->assembly): Reindent
  the <glil-lexical> case, and handle 'fix for locally-bound vars.

* module/language/tree-il.scm (<fix>): Add the <fix> tree-il type and
  accessors, for fixed-point bindings. This IL construct is taken from
  the Waddell paper.
  (parse-tree-il, unparse-tree-il, tree-il->scheme, tree-il-fold)
  (pre-order!, post-order!): Update for <fix>.

* module/language/tree-il/analyze.scm (analyze-lexicals): Update for
  <fix>. The difference here is that the bindings may not be assigned,
  and are not marked as such. They are not boxed.
  (report-unused-variables): Update for <fix>.

* module/language/tree-il/compile-glil.scm (flatten): Compile <fix> to
  GLIL.

* module/language/tree-il/fix-letrec.scm: A stub implementation of
  fixing letrec -- will flesh out in a separate commit.

* module/language/tree-il/inline.scm: Fix license, it was mistakenly
  added with LGPL v2.1+.

* module/language/tree-il/optimize.scm (optimize!): Run the fix-letrec!
  pass.
2009-08-05 17:52:27 +02:00
Andy Wingo
dab0f9d55d add a brain-dead inliner
* module/Makefile.am (TREE_IL_LANG_SOURCES):
* module/language/tree-il/inline.scm: Add a brain-dead inliner, to
  inline ((lambda () x)) => x.

* module/language/tree-il/optimize.scm (optimize!): Invoke the inliner.
2009-08-05 16:17:20 +02:00
Neil Jerram
53befeb700 Change Guile license to LGPLv3+
(Not quite finished, the following will be done tomorrow.
   module/srfi/*.scm
   module/rnrs/*.scm
   module/scripts/*.scm
   testsuite/*.scm
   guile-readline/*
)
2009-06-17 00:22:09 +01:00
Andy Wingo
55ae815b62 move things to (language tree-il primitives)
* module/language/tree-il/optimize.scm:
* module/language/tree-il/primitives.scm: Move primitive-related things
  to primitive.scm from inline.scm and optimize.scm.

* module/Makefile.am: Update for inventory changes.
2009-05-22 16:07:41 +02:00
Andy Wingo
0f423f20aa fix apply and call/cc in drop contexts
* module/language/tree-il/compile-glil.scm (flatten): Actually apply only
  needs one arg after the proc. And shit, call/cc and apply in drop
  contexts also need to be able to return arbitrary numbers of values;
  work it by trampolining through their applicative (non-@) definitions.
  Also, simplify the single-valued drop case to avoid the
  truncate-values.

* module/language/tree-il/inline.scm (call/cc):
* module/language/tree-il/optimize.scm (*interesting-primitive-names*):
  Define call/cc as "interesting". Perhaps we should be hashing on value
  and not on variable.

* test-suite/tests/tree-il.test ("application"): Fix up test for new,
  sleeker output. (Actually the GLIL is more verbose, but the assembly is
  better.)
  ("apply", "call/cc"): Add some more tests.
2009-05-21 21:13:24 +02:00
Andy Wingo
68623e8e78 remove compile-time-environment
* module/ice-9/boot-9.scm (guile-user): Move the `compile' autoload to
  the guile-user module. Remove reference to compile-time-environment.

* module/language/scheme/compile-ghil.scm:
* module/language/tree-il/compile-glil.scm:
* module/language/tree-il/optimize.scm:
* module/system/base/compile.scm:
* test-suite/tests/compiler.test: Remove definition of and references to
  compile-time-environment. While I do think that recompilation based on
  a lexical environment can be useful, I think it needs to be implemented
  differently. So for now we've lost nothing if we take it away, as it
  doesn't work with syncase anyway.
2009-05-20 17:41:21 +02:00
Andy Wingo
ad9b8c451b fix @slot-ref / @slot-set! compilation
* module/language/tree-il/compile-glil.scm: Add primcall compilers for
  @slot-ref and @slot-set.

* module/language/tree-il/optimize.scm (add-interesting-primitive!): New
  export. Creates an association between a variable in the current module
  and a primitive name.

* module/oop/goops.scm: Rework compiler hooks to work with tree-il and
  not ghil.
2009-05-20 13:59:42 +02:00
Andy Wingo
c11f46afe1 compile list' and vector' to their associated opcodes
* module/language/glil/compile-assembly.scm (glil->assembly): Check the
  length when emitting calls to variable-argument stack instructions.
  Allow two-byte lengths -- allows e.g. calls to `list' with more than
  256 arguments.

* module/language/tree-il/compile-glil.scm: Add primcall associations for
  `list' and `vector', with any number of arguments. Necessary because
  syncase's quasiquote expansions will produce calls to `list' with many
  arguments.

* module/language/tree-il/optimize.scm (*interesting-primitive-names*):
  Add `list' and `vector' to the set of primitives to resolve.
2009-05-20 13:33:44 +02:00
Andy Wingo
a1a482e0e9 and, or, cond etc use syntax-rules, compile scheme through tree-il
* libguile/vm-i-system.c:
* libguile/vm-engine.h (ASSERT_BOUND): New assertion, that a value is
  bound. Used by local-ref and external-ref in paranoid mode.

* module/ice-9/boot-9.scm (and, or, cond, case, do): Since we are
  switching to use psyntax as the first pass of the compiler, and perhaps
  soon of the interpreter too, we need to make sure it expands out all
  forms to primitive expressions. So define expanders for these derived
  syntax forms, as in the R5RS report.

* module/ice-9/psyntax-pp.scm: Regenerate, with core forms fully
  expanded.

* module/ice-9/psyntax.scm (build-void): New constructor, for making
  undefined values.
  (build-primref): Add in a hack so that primitive refs in the boot
  module expand out to toplevel refs, not module refs.
  (chi-void): Use build-void.
  (if): Define an expander for if that calls build-conditional.

* module/language/scheme/compile-tree-il.scm (compile-tree-il): Use let*
  so as not to depend on binding order for the result of
  (current-module).

* module/language/scheme/spec.scm (scheme): Switch over to tree-il as the
  primary intermediate language. Not yet fully tested, but at least it
  can compile psyntax-pp.scm.

* module/language/tree-il/analyze.scm (analyze-lexicals): Arguments don't
  count towards a function's nlocs.

* module/language/tree-il/compile-glil.scm (*comp-module*, compile-glil):
  Define a "compilation module" fluid.
  (flatten-lambda): Fix a call to make-glil-argument. Fix bug in
  heapifying arguments.
  (flatten): Fix number of arguments passed to apply instruction. Add a
  special case for `(values ...)'. If inlining primitive-refs fails,
  try expanding into toplevel-refs if the comp-module's variable is the
  same as the root variable.

* module/language/tree-il/optimize.scm (resolve-primitives!): Add missing
  src variable for <module-ref>.

* test-suite/tests/tree-il.test ("lambda"): Fix nlocs counts. Add a
  closure test case.
2009-05-20 11:15:22 +02:00
Andy Wingo
ce09ee1989 add tree-il->glil compilation test suite
* module/language/tree-il.scm (parse-tree-il): Fix a number of bugs.
  (unparse-tree-il): Apply takes rest args now.

* module/language/tree-il/analyze.scm (analyze-lexicals)
  (analyze-lexicals): Heap vars shouldn't increment the number of locals.

* module/language/tree-il/optimize.scm (resolve-primitives!): Don't
  resolve public refs to primitives, not at the moment anyway.

* test-suite/Makefile.am (SCM_TESTS): Add tree-il test.

* test-suite/lib.scm (pass-if, expect-fail, pass-if-exception)
  (expect-fail-exception): Rewrite as syntax-rules macros. In a very
  amusing turn of events, it turns out that bindings introduced by
  hygienic macros are not visible inside expansions produced by
  defmacros. This seems to be expected, so go ahead and work around the
  problem.

* test-suite/tests/srfi-31.test ("rec special form"): Expand in eval.

* test-suite/tests/syntax.test ("begin"): Do some more expanding in eval,
  though all is not yet well.

* test-suite/tests/tree-il.test: New test suite, for tree-il->glil
  compilation.
2009-05-18 23:45:35 +02:00
Andy Wingo
073bb617eb add lexical analyzer and allocator
* module/language/tree-il/optimize.scm: Rework to just export the
  optimize! procedure.

* module/language/tree-il/compile-glil.scm (analyze-lexicals): New
  function, analyzes and allocates lexical variables. Almost ready to
  compile now.
  (codegen): Dedent.
2009-05-14 00:11:54 +02:00
Andy Wingo
cb28c08537 add primitive expander for tree-il
* module/Makefile.am: Add inline.scm.

* module/language/tree-il.scm (pre-order!, post-order!): pre-order! is
  new. post-order! existed but was not public. They do destructive tree
  traversals of tree-il, and need more documentation. Also, add
  predicates to tree-il's export list.

* module/language/tree-il/inline.scm: New file, which expands primitives
  into more primitive primitives. In the future perhaps it will not be
  necessary, as the general inlining infrastructure will handle these
  cases, but for now it's useful.

* module/language/tree-il/optimize.scm: Move post-order! out to better
  pastures.
2009-05-12 22:29:34 +02:00
Andy Wingo
9efc833d65 add tree-il optimizer
* module/language/tree-il/optimize.scm: New module, for optimizations.
  Currently all we have is resolving some toplevel refs to primitive
  refs.

* module/Makefile.am: Add new module.

* module/language/tree-il.scm: Fix exports for accessors for `src'.

* module/language/tree-il/compile-glil.scm: Tweaks, things still aren't
  working yet.
2009-05-11 23:23:34 +02:00