1
0
forked from alex-eg/sex

make each compiler module one file, built once

A module was a body plus a (module ...) wrapper naming its exports, and
the wrapper came in up to three copies: the root's, a test one that
exported everything, and sextest's, each recompiling the same body.

csc -module makes a file a module by itself, with export forms in its
body, so the wrappers are gone. tests/ and tools/sextest/ link what the
root built, finding it on the repository path; the tests see what a
module exports, and the names only they need are exported as such.
This commit is contained in:
2026-10-14 20:51:05 +03:00
parent 540f5c75cd
commit dcb65c5504
32 changed files with 203 additions and 370 deletions

View File

@@ -65,42 +65,29 @@ sexc: $(OBJ) main.scm
#------------------------------------------------------------------ #------------------------------------------------------------------
utils.o: utils.module.scm utils.scm # A module is one file that declares its own exports, made a module by
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils # -module
types.o: types.module.scm types.scm %.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) types.module.scm -o types.o -unit types $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -module $* -o $@ -unit $*
infer.o: infer.module.scm infer.scm types.o utils.o infer.o: types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) infer.module.scm -o infer.o -unit infer -link types,utils reader.o: utils.o
sex-modules.o: reader.o types.o utils.o
sex-macros.o: sex-macros.module.scm sex-macros.scm semen.o: infer.o sex-macros.o sex-modules.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros fmt-c-writer.o: sex-fmt-c.o types.o utils.o
sexc.o: fmt-c-writer.o infer.o reader.o semen.o sex-macros.o sex-modules.o types.o utils.o
reader.o: reader.module.scm reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
sex-modules.o: sex-modules.module.scm sex-modules.scm reader.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,types,utils
semen.o: semen.module.scm semen.scm infer.o sex-macros.o sex-modules.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,infer,types,utils
# Vendored, and a (module ...) form of its own
sex-fmt-c.o: sex-fmt-c.scm sex-fmt-c.o: sex-fmt-c.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< -o $@ -unit sex-fmt-c
fmt-c-writer.o: fmt-c-writer.module.scm fmt-c-writer.scm sex-fmt-c.o types.o utils.o # Unit testing. Both link the modules built here
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,types,utils sex-tests: $(OBJ)
sexc.o: sexc.module.scm infer.o types.o sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,infer,types,utils
# Unit testing
sex-tests:
$(MAKE) -C ./tests sex-tests $(MAKE) -C ./tests sex-tests
cp ./tests/sex-tests ./ cp ./tests/sex-tests ./
sextest: sextest: reader.o utils.o
$(MAKE) -C ./tools/sextest sextest $(MAKE) -C ./tools/sextest sextest
cp ./tools/sextest/sextest . cp ./tools/sextest/sextest .

View File

@@ -1,3 +0,0 @@
(module fmt-c-writer (emit-c
sex-line-directives)
"fmt-c-writer.scm")

View File

@@ -4,6 +4,7 @@
scheme scheme
(scheme base) ; make-parameter (scheme base) ; make-parameter
(chicken base) (chicken base)
(chicken module)
(chicken string) (chicken string)
(chicken syntax) (chicken syntax)
brev-separate ; fn, flatten brev-separate ; fn, flatten
@@ -16,6 +17,14 @@
types ; array-bound?, named-arg? types ; array-bound?, named-arg?
utils) utils)
(export
emit-c
sex-line-directives
;; for the tests
atom-to-fmt-c unkebabify type-convert-to-c
process-toplevel-form walk-expr walk-type walk-var walk-fn-def walk-struct)
;;; egg `tree' not ported to CHICKEN 6 yet ;;; egg `tree' not ported to CHICKEN 6 yet
(define (tree-map f tree) (define (tree-map f tree)
(cond ((null? tree) (list)) (cond ((null? tree) (list))

View File

@@ -1,45 +0,0 @@
(module infer
(;; The IR
tvar?
tvar-id
tvar-classes
tvar-rigid?
fresh-tvar
fresh-rigid-tvar
prim-type? prim-name prim-quals make-prim
ptr-type? ptr-target ptr-quals make-ptr
array-type? array-elt array-size make-array-type
fn-type? fn-ret fn-args fn-variadic? make-fn-type
agg-type? agg-kind agg-name agg-spelling agg-quals make-agg
alias-type? alias-name alias-expansion alias-quals make-alias
unknown-type? the-unknown-type
resolve
underlying
c-primitive?
type-quals
free-tvars
decay
;; The boundary
parse-type
unparse-type
;; Constraints
register-class!
add-instance!
entails?
default-tvar!
default-type-variables!
;; Unification
unify
;; Type schemes
scheme? scheme-vars scheme-constraints scheme-type
make-scheme
generalize
instantiate
substitute)
"infer.scm")

View File

@@ -25,12 +25,58 @@
scheme scheme
(scheme base) (scheme base)
(chicken base) (chicken base)
(chicken module)
matchable matchable
srfi-1 srfi-1
srfi-69 srfi-69
types types
utils) utils)
(export
;; The IR
tvar?
tvar-id
tvar-classes
tvar-rigid?
fresh-tvar
fresh-rigid-tvar
prim-type? prim-name prim-quals make-prim
ptr-type? ptr-target ptr-quals make-ptr
array-type? array-elt array-size make-array-type
fn-type? fn-ret fn-args fn-variadic? make-fn-type
agg-type? agg-kind agg-name agg-spelling agg-quals make-agg
alias-type? alias-name alias-expansion alias-quals make-alias
unknown-type? the-unknown-type
resolve
underlying
c-primitive?
type-quals
free-tvars
decay
;; The boundary
parse-type
unparse-type
;; Constraints
register-class!
add-instance!
entails?
default-tvar!
default-type-variables!
;; Unification
unify
;; Type schemes
scheme? scheme-vars scheme-constraints scheme-type
make-scheme
generalize
instantiate
substitute)
;;; --------------------------------------------------------------- ;;; ---------------------------------------------------------------
;;; The IR ;;; The IR
;;; --------------------------------------------------------------- ;;; ---------------------------------------------------------------

View File

@@ -1,6 +0,0 @@
(module reader (read-from-file
read-raw-forms
current-features
platform-features)
"reader.scm")

View File

@@ -16,11 +16,22 @@
scheme scheme
(scheme base) ; make-parameter (scheme base) ; make-parameter
(chicken base) (chicken base)
(chicken module)
(chicken pathname) (chicken pathname)
(chicken platform) ; software-version, machine-type (chicken platform) ; software-version, machine-type
(only srfi-1 every any) ; srfi-1 also has an append-reverse (only srfi-1 every any) ; srfi-1 also has an append-reverse
utils) utils)
(export
read-from-file
read-raw-forms
current-features
platform-features
;; for the tests
parse-all)
;;; Sentinels for structural tokens ;;; Sentinels for structural tokens
(define close-paren (list '%close-paren)) (define close-paren (list '%close-paren))
(define close-bracket (list '%close-bracket)) (define close-bracket (list '%close-bracket))

View File

@@ -1,2 +0,0 @@
(module semen ()
"semen.scm")

View File

@@ -19,7 +19,11 @@
) )
(export/rename (process semen-process)) (export/rename (process semen-process))
(export closure-env-declaration) (export closure-env-declaration
;; for the tests
sex-fn? sex-fn-public? sex-fn-name sex-fn-arglist
sex-fn-return-type sex-fn-prototype sex-fn-body
walk-form macro-expand)
;;; for lambda extraction, docstring processing, macro expansion, ;;; for lambda extraction, docstring processing, macro expansion,
;;; injection of module headers, i.e. all things that rearrange code ;;; injection of module headers, i.e. all things that rearrange code

View File

@@ -1,9 +0,0 @@
(module sex-macros
(register-macro
cat
comment
get-macro
macro?
apply-macro
defmacro)
"sex-macros.scm")

View File

@@ -2,9 +2,19 @@
scheme scheme
(only fmt fmt) (only fmt fmt)
(chicken base) (chicken base)
(chicken module)
(chicken plist) (chicken plist)
(chicken string)) (chicken string))
(export
register-macro
cat
comment
get-macro
macro?
apply-macro
defmacro)
(define (cat-syms s-1 s-2) (define (cat-syms s-1 s-2)
(fmt #f s-1 s-2)) (fmt #f s-1 s-2))

View File

@@ -1,7 +0,0 @@
(module sex-modules
(get-modules-public-forms
load-persistent-module-paths
check-public-interface
module-dependencies
read-public-interface)
"sex-modules.scm")

View File

@@ -4,6 +4,7 @@
(chicken base) (chicken base)
(chicken file) (chicken file)
(chicken load) (chicken load)
(chicken module)
(chicken pathname) (chicken pathname)
(chicken process-context) (chicken process-context)
(chicken string) (chicken string)
@@ -14,6 +15,16 @@
types types
utils) utils)
(export
get-modules-public-forms
load-persistent-module-paths
check-public-interface
module-dependencies
read-public-interface
;; for the tests
get-module-paths)
(define +persistent-module-paths+ (list)) (define +persistent-module-paths+ (list))
;;; for guarding against multiple imports (sort of mandatory #pragma ;;; for guarding against multiple imports (sort of mandatory #pragma

View File

@@ -1 +0,0 @@
(module sexc (main) "sexc.scm")

View File

@@ -4,6 +4,7 @@
(chicken base) (chicken base)
(chicken condition) ; handle-exceptions (chicken condition) ; handle-exceptions
(chicken file) (chicken file)
(chicken module)
(chicken plist) (chicken plist)
(chicken pretty-print) (chicken pretty-print)
(chicken process) (chicken process)
@@ -20,6 +21,13 @@
srfi-1 ; list routines srfi-1 ; list routines
utils) utils)
(export
main
;; for the tests
args-before-separator args-after-separator
dependency-rule spell-stdout-output)
;;; Main function facilities ;;; Main function facilities
(define opts-grammar (define opts-grammar

View File

@@ -1,53 +1,21 @@
CHICKEN_C = csc CHICKEN_C = csc
CSC_FLAGS += -K prefix -static CSC_FLAGS += -K prefix -static
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
# The compiler's modules are built once, in the root; their import
# libraries and objects are found there through the repository path
ROOT = ..
export CHICKEN_REPOSITORY_PATH := $(abspath $(ROOT)):$(or $(CHICKEN_REPOSITORY_PATH),$(shell chicken-install -repository))
MODULES = utils types infer sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc MODULES = utils types infer sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
SEX_OBJ = $(MODULES:%=%.o)
TESTS = basic semen reader fmt-c-writer utils line-directives codegen encapsulation args module-path types infer TESTS = basic semen reader fmt-c-writer utils line-directives codegen encapsulation args module-path types infer
TEST_SRCS = $(TESTS:%=%.scm) TEST_SRCS = $(TESTS:%=%.scm)
sex-tests: run.scm $(TEST_SRCS) $(SEX_OBJ) sex-tests: run.scm $(TEST_SRCS) $(MODULES:%=$(ROOT)/%.o)
$(CHICKEN_C) $(CSC_FLAGS) run.scm -o sex-tests -link sexc $(CHICKEN_C) $(CSC_FLAGS) run.scm -o sex-tests -link sexc
#------------------------------------------------------------------
utils.o: utils.module.scm ../utils.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
types.o: types.module.scm ../types.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) types.module.scm -o types.o -unit types
infer.o: infer.module.scm ../infer.scm types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) infer.module.scm -o infer.o -unit infer -link types,utils
sex-macros.o: sex-macros.module.scm ../sex-macros.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros
reader.o: reader.module.scm ../reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
sex-modules.o: sex-modules.module.scm ../sex-modules.scm reader.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,types,utils
semen.o: semen.module.scm ../semen.scm infer.o sex-macros.o sex-modules.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,infer,types,utils
sex-fmt-c.o: ../sex-fmt-c.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) ../sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c
fmt-c-writer.o: fmt-c-writer.module.scm ../fmt-c-writer.scm sex-fmt-c.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,types,utils
sexc.o: sexc.module.scm infer.o types.o ../sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,infer,types,utils
clean: clean:
rm -f $(SEX_OBJ)
rm -f *.import.scm
rm -f *.link
rm -f sex-tests rm -f sex-tests
.PHONY: clean .PHONY: clean

View File

@@ -1,3 +0,0 @@
(module fmt-c-writer
*
"../fmt-c-writer.scm")

View File

@@ -1,45 +0,0 @@
(module infer
(;; The IR
tvar?
tvar-id
tvar-classes
tvar-rigid?
fresh-tvar
fresh-rigid-tvar
prim-type? prim-name prim-quals make-prim
ptr-type? ptr-target ptr-quals make-ptr
array-type? array-elt array-size make-array-type
fn-type? fn-ret fn-args fn-variadic? make-fn-type
agg-type? agg-kind agg-name agg-spelling agg-quals make-agg
alias-type? alias-name alias-expansion alias-quals make-alias
unknown-type? the-unknown-type
resolve
underlying
c-primitive?
type-quals
free-tvars
decay
;; The boundary
parse-type
unparse-type
;; Constraints
register-class!
add-instance!
entails?
default-tvar!
default-type-variables!
;; Unification
unify
;; Type schemes
scheme? scheme-vars scheme-constraints scheme-type
make-scheme
generalize
instantiate
substitute)
"../infer.scm")

View File

@@ -1,3 +0,0 @@
(module reader
*
"../reader.scm")

View File

@@ -1,2 +0,0 @@
(module semen *
"../semen.scm")

View File

@@ -1,3 +0,0 @@
(module sex-macros
*
"../sex-macros.scm")

View File

@@ -1,3 +0,0 @@
(module sex-modules
*
"../sex-modules.scm")

View File

@@ -1,3 +0,0 @@
(module sexc
*
"../sexc.scm")

View File

@@ -1,50 +0,0 @@
(module types
(add-struct
add-union
add-enum
add-typedef
add-define
type-match
type-pattern-matches?
map-fields
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!
add-spec!
get-spec
spec-parameter
spec-methods
add-realization!
realizes?
normalize-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of
current-type-of
get-return-type
get-type-info
get-tag-info
get-fields
get-underlying-type
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
"../types.scm")

View File

@@ -1,3 +0,0 @@
(module utils
*
"../utils.scm")

View File

@@ -1,23 +1,16 @@
CHICKEN_C = csc CHICKEN_C = csc
CSC_FLAGS += -K prefix -static CSC_FLAGS += -K prefix -static
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
ROOT = ../..
# sextest reuses sexc's reader (and its utils dependency) instead of # sextest reuses sexc's reader (and its utils dependency) instead of
# duplicating the S-expression reader. The module wrappers here include # duplicating the S-expression reader: both are linked from the root,
# the shared sources from the project root. # found there through the repository path
ROOT = ../..
export CHICKEN_REPOSITORY_PATH := $(abspath $(ROOT)):$(or $(CHICKEN_REPOSITORY_PATH),$(shell chicken-install -repository))
sextest: sextest.scm reader.o utils.o sextest: sextest.scm $(ROOT)/reader.o $(ROOT)/utils.o
$(CHICKEN_C) $(CSC_FLAGS) sextest.scm -o sextest -link reader,utils $(CHICKEN_C) $(CSC_FLAGS) sextest.scm -o sextest -link reader,utils
utils.o: utils.module.scm $(ROOT)/utils.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
reader.o: reader.module.scm $(ROOT)/reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
clean: clean:
rm -f *.o *.import.scm *.link sextest rm -f sextest
.PHONY: clean .PHONY: clean

View File

@@ -1,6 +0,0 @@
(module reader (read-from-file
read-raw-forms
current-features
platform-features)
"../../reader.scm")

View File

@@ -1,21 +0,0 @@
(module utils
(get-env-var
set-working-directory
to-absolute-pathname
comment-form?
strip-header-comments
list-split
list-join
recons
current-source-file
set-form-source!
form-source
form-file
form-line
copy-form-source!
stamp-form-source!
form-location
sex-error
with-directory
)
"../../utils.scm")

View File

@@ -1,50 +0,0 @@
(module types
(add-struct
add-union
add-enum
add-typedef
add-define
type-match
type-pattern-matches?
map-fields
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!
add-spec!
get-spec
spec-parameter
spec-methods
add-realization!
realizes?
normalize-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of
current-type-of
get-return-type
get-type-info
get-tag-info
get-fields
get-underlying-type
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
"types.scm")

View File

@@ -12,9 +12,60 @@
scheme scheme
(scheme base) (scheme base)
(chicken base) (chicken base)
(chicken module)
srfi-1 srfi-1
srfi-69) srfi-69)
(export
add-struct
add-union
add-enum
add-typedef
add-define
type-match
type-pattern-matches?
map-fields
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!
add-spec!
get-spec
spec-parameter
spec-methods
add-realization!
realizes?
normalize-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of
current-type-of
get-return-type
get-type-info
get-tag-info
get-fields
get-underlying-type
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
;;; Two namespaces: `struct point' and a `point' typedef are separate ;;; Two namespaces: `struct point' and a `point' typedef are separate
;;; declarations. Tags -- struct, union and enum alike -- share the ;;; declarations. Tags -- struct, union and enum alike -- share the
;;; second table between them ;;; second table between them

View File

@@ -1,25 +0,0 @@
(module utils
(get-env-var
set-working-directory
to-absolute-pathname
comment-form?
strip-header-comments
list-split
list-join
recons
current-source-file
set-form-source!
form-source
form-file
form-line
form-end-line
copy-form-source!
stamp-form-source!
form-location
set-form-type!
form-type
sex-error
sex-warning
with-directory
)
"utils.scm")

View File

@@ -2,11 +2,36 @@
scheme scheme
(scheme base) ; make-parameter (scheme base) ; make-parameter
(chicken base) (chicken base)
(chicken module)
(chicken pathname) (chicken pathname)
(chicken process-context) (chicken process-context)
srfi-1 srfi-1
srfi-69) srfi-69)
(export
get-env-var
set-working-directory
to-absolute-pathname
comment-form?
strip-header-comments
list-split
list-join
recons
current-source-file
set-form-source!
form-source
form-file
form-line
form-end-line
copy-form-source!
stamp-form-source!
form-location
set-form-type!
form-type
sex-error
sex-warning
with-directory)
(define-syntax prog1 (define-syntax prog1
(syntax-rules () (syntax-rules ()
((prog1 form . forms) ((prog1 form . forms)