>From f44999c9ead1f6807943371dfdb1e22106bf2b67 Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Sun, 10 Aug 2014 17:31:54 +0200 Subject: [PATCH 07/19] compiler-modules: Convert c-backend to a module. In chicken-ffi-syntax, foreign-type-size now expands directly to c-backend#foreign-type-declaration, which is needed in order to remove foreign-type-declaration from compiler-namespace. Added simple test to ensure that this macro still functions correctly. Added the variables target-include-file words-per-flonum, parameter-limit and small-parameter-limit to c-platform's export list. These are consumed only by c-backend, so can be removed from compiler-namespace. Added a lot of accessors defined by "compiler" to the compiler-namespace, which were not renamed correctly before (and, thus, inadvertantly accessible to user macros when running inside the compiler). Removed the unused variable unique-id. --- batch-driver.scm | 7 +++--- c-backend.scm | 31 +++++++++++++++---------- c-platform.scm | 10 ++++---- chicken-ffi-syntax.scm | 9 +++++--- chicken.scm | 2 +- compiler-namespace.scm | 57 +++++++++++++++++++++++++++++----------------- rules.make | 6 +++-- tests/compiler-tests.scm | 3 +++ 8 files changed, 78 insertions(+), 47 deletions(-) diff --git a/batch-driver.scm b/batch-driver.scm index d00a2bb..1dcd97e 100644 --- a/batch-driver.scm +++ b/batch-driver.scm @@ -30,7 +30,8 @@ (declare (unit batch-driver) (uses extras data-structures files srfi-1 - lfa2 compiler-syntax optimizer c-platform) ) + ;; TODO: Backend should be configurable + lfa2 compiler-syntax optimizer c-platform c-backend) ) ;; TODO: Remove these once everything's converted to modules (include "private-namespace") @@ -41,10 +42,10 @@ user-options-pass user-read-pass user-preprocessor-pass user-pass user-post-analysis-pass) - + (import (except chicken put! get quit syntax-error) scheme extras data-structures files srfi-1 - lfa2 compiler-syntax optimizer c-platform) + lfa2 compiler-syntax optimizer c-platform c-backend) (include "tweaks") diff --git a/c-backend.scm b/c-backend.scm index eade651..bc8972b 100644 --- a/c-backend.scm +++ b/c-backend.scm @@ -25,12 +25,26 @@ ; POSSIBILITY OF SUCH DAMAGE. -(declare (unit backend)) - - +;; TODO: Rename c-backend back to "backend" and turn it into a +;; functor? This may require the creation of an additional file. +;; Same goes for "platform" and "driver". +(declare + (unit c-backend) + (uses srfi-1 data-structures + c-platform compiler)) + +;; TODO: Remove these once everything's converted to modules +(include "private-namespace") (include "compiler-namespace") -(include "tweaks") +(module c-backend + (generate-code + ;; For "foreign" (aka chicken-ffi-syntax): + foreign-type-declaration) + +(import (except chicken put! get quit syntax-error) scheme foreign + srfi-1 data-structures + c-platform) ;;; Write atoms to output-port: @@ -49,14 +63,6 @@ (lambda (x) (display x output)) (intersperse lst #\space) ) ) - -;;; Unique id/prefix: - -(define unique-id - (string->c-identifier - (sprintf "C_~X_~A_" (random #x1000000) (current-seconds)) ) ) - - ;;; Generate target code: (define (generate-code literals lliterals lambda-table out source-file dynamic db) @@ -1428,3 +1434,4 @@ return((C_header_bits(lit) >> 24) & 0xff); (encode-size len) (list-tabulate len (lambda (i) (encode-literal (##sys#slot lit i))))) ""))))) ) +) \ No newline at end of file diff --git a/c-platform.scm b/c-platform.scm index 588435e..4353d4a 100644 --- a/c-platform.scm +++ b/c-platform.scm @@ -40,7 +40,11 @@ (module c-platform (default-declarations default-profiling-declarations units-used-by-default - valid-compiler-options valid-compiler-options-with-argument) + valid-compiler-options valid-compiler-options-with-argument + + ;; For consumption by c-backend *only* + target-include-file words-per-flonum + parameter-limit small-parameter-limit) (import (except chicken put! get quit syntax-error) scheme srfi-1 data-structures @@ -75,11 +79,8 @@ ##sys#profile-entry ##sys#profile-exit) ) ) ) (define units-used-by-default '(library eval chicken-syntax)) -;; TODO: export this and remove it from compiler-namespace (define words-per-flonum 4) -;; TODO: export this and remove it from compiler-namespace (define parameter-limit 1024) -;; TODO: export this and remove it from compiler-namespace (define small-parameter-limit 128) ;; TODO: export this and remove it from compiler-namespace (define unlikely-variables '(unquote unquote-splicing)) @@ -89,7 +90,6 @@ '(("C_i_memq" . "C_eqp") ("C_u_i_memq" . "C_eqp") ("C_i_member" . "C_i_equalp") ("C_i_memv" . "C_i_eqvp") ) ) (membership-unfold-limit 20) -;; TODO: export this and remove it from compiler-namespace (define target-include-file "chicken.h") (define valid-compiler-options diff --git a/chicken-ffi-syntax.scm b/chicken-ffi-syntax.scm index d07764f..57acabb 100644 --- a/chicken-ffi-syntax.scm +++ b/chicken-ffi-syntax.scm @@ -28,7 +28,10 @@ (declare (unit chicken-ffi-syntax) (disable-interrupts) - (fixnum) ) + (fixnum)) + +;; IMPORTANT: These macros expand directly into fully qualified names +;; from the c-backend module. #+(not debugbuild) (declare @@ -38,7 +41,6 @@ (##sys#provide 'chicken-ffi-syntax) - (define ##sys#chicken-ffi-macro-environment (let ((me0 (##sys#macro-environment))) @@ -287,7 +289,8 @@ (decl (if (string? t) t - (##compiler#foreign-type-declaration t "")))) + ;; TODO: Backend should be configurable + (c-backend#foreign-type-declaration t "")))) `(##core#begin (##core#define-foreign-variable ,tmp size_t ,(string-append "sizeof(" decl ")")) (##core#the fixnum #f ,tmp)))))) diff --git a/chicken.scm b/chicken.scm index 86c2328..0ef388b 100644 --- a/chicken.scm +++ b/chicken.scm @@ -30,7 +30,7 @@ srfi-1 srfi-4 utils files extras data-structures support compiler optimizer lfa2 compiler-syntax scrutinizer ;; TODO: These three need to be made configurable somehow - batch-driver c-platform backend + batch-driver c-platform c-backend srfi-69)) diff --git a/compiler-namespace.scm b/compiler-namespace.scm index 0b08e65..0a4f0c9 100644 --- a/compiler-namespace.scm +++ b/compiler-namespace.scm @@ -51,7 +51,6 @@ check-signature chop-extension chop-separator - cleanup close-checked-input-file collapsable-literal? collect! @@ -107,7 +106,6 @@ emit-type-file enable-inline-files enable-specialization - encode-literal estimate-foreign-result-location-size estimate-foreign-result-size expand-debug-assignment @@ -139,25 +137,33 @@ foldable? foldable-bindings follow-without-loop - foreign-argument-conversion + foreign-callback-stubs + foreign-callback-stub-argument-types + foreign-callback-stub-body + foreign-callback-stub-callback + foreign-callback-stub-cps + foreign-callback-stub-id + foreign-callback-stub-name + foreign-callback-stub-qualifiers + foreign-callback-stub-return-type foreign-declarations foreign-lambda-stubs - foreign-result-conversion foreign-string-result-reserve + foreign-stub-argument-types + foreign-stub-argument-names + foreign-stub-body + foreign-stub-callback + foreign-stub-cps + foreign-stub-id + foreign-stub-name + foreign-stub-qualifiers + foreign-stub-return-type foreign-type->scrutiny-type foreign-type-check foreign-type-convert-argument foreign-type-convert-result - foreign-type-declaration foreign-type-table foreign-variables - gen - gen-list - generate-code - generate-external-variables - generate-foreign-callback-header - generate-foreign-callback-stub-prototypes - generate-foreign-stubs get get-all get-line @@ -184,6 +190,23 @@ intrinsic? line-number-database-2 line-number-database-size + lambda-literal-id + lambda-literal-external + lambda-literal-arguments + lambda-literal-argument-count + lambda-literal-rest-argument + lambda-literal-rest-argument-mode + lambda-literal-temporaries + lambda-literal-unboxed-temporaries + lambda-literal-callee-signatures + lambda-literal-allocated + lambda-literal-directly-called + lambda-literal-closure-size + lambda-literal-looping + lambda-literal-customizable + rest-argument-mode + lambda-literal-body + lambda-literal-direct llist-length llist-match? load-identifier-database @@ -191,10 +214,8 @@ load-type-database local-definitions location-pointer-map - make-argument-list make-block-variable-literal make-random-name - make-variable-list mark-variable match-node no-argc-checks @@ -214,8 +235,6 @@ optimization-iterations optimize-leaf-routines original-program-size - output - parameter-limit parenthesis-synonyms pending-canonicalizations perform-closure-conversion @@ -258,7 +277,6 @@ simplify-type slashify sort-symbols - small-parameter-limit source-filename source-info->string source-info->line @@ -271,14 +289,12 @@ stringify symbolify target-heap-size - target-include-file target-stack-size toplevel-lambda-id toplevel-scope tree-copy uncommentify undefine-shadowed-macros - unique-id unit-name unlikely-variables unsafe @@ -293,5 +309,4 @@ verbose-mode with-debugging-output words - words->bytes - words-per-flonum) + words->bytes) diff --git a/rules.make b/rules.make index 2f2c854..01aa441 100644 --- a/rules.make +++ b/rules.make @@ -492,7 +492,7 @@ endef $(foreach lib, $(SETUP_API_OBJECTS_1),\ $(eval $(call declare-emitted-import-lib-dependency,$(lib)))) -$(foreach lib, batch-driver lfa2 compiler-syntax optimizer c-platform,\ +$(foreach lib, batch-driver lfa2 compiler-syntax optimizer c-platform c-backend,\ $(eval $(call declare-emitted-import-lib-dependency,$(lib)))) chicken.c: chicken.scm batch-driver.import.scm batch-driver.scm \ @@ -500,8 +500,10 @@ chicken.c: chicken.scm batch-driver.import.scm batch-driver.scm \ batch-driver.c: batch-driver.scm lfa2.import.scm lfa2.scm \ compiler-syntax.scm compiler-syntax.import.scm \ optimizer.scm optimizer.import.scm \ - c-platform.scm c-platform.import.scm + c-platform.scm c-platform.import.scm \ + c-backend.scm c-backend.import.scm c-platform.c: c-platform.scm optimizer.scm optimizer.import.scm +c-backend.c: c-backend.scm c-platform.scm c-platform.import.scm compiler.scm define profile-flags $(if $(filter $(basename $(1)),$(PROFILE_OBJECTS)),-profile) diff --git a/tests/compiler-tests.scm b/tests/compiler-tests.scm index 078cb0d..87b472f 100644 --- a/tests/compiler-tests.scm +++ b/tests/compiler-tests.scm @@ -13,6 +13,9 @@ (assert (eq? 'ok (foo))) +(assert (= 1 (foreign-type-size "char"))) +(let ((bytes-in-a-word (##sys#fudge 7))) + (assert (= bytes-in-a-word (foreign-type-size "C_word")))) ;; test hiding of unexported toplevel variables -- 1.7.10.4