Skip to content

Commit

Permalink
Support chicken scheme bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
djwatson committed Oct 13, 2023
1 parent 9c30f06 commit c62b928
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 30 deletions.
13 changes: 0 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,6 @@ option(JIT "Enable JIT" ON)
# the profiler is optional
option(PROFILER "Enable PROFILER" OFF)

# Find a suitable scheme for bootstrapping.
# Chez and chicken have been tested, it's likely most r5rs (+ write-u8) compatible
# schemes will work with minor changes.
find_program(CHEZ chez)
if (NOT CHEZ)
find_program(CHEZ chezscheme)
if (NOT CHEZ)
message(FATAL_ERROR "Can't find chez scheme for bootstrapping")
endif()
endif()
message("Using " ${CHEZ} " for bootstrapping")


# PIE turned off for testing, since we JIT hot function detection is based on address of the function.
include(CheckPIESupported)
check_pie_supported()
Expand Down
2 changes: 2 additions & 0 deletions lib/opcode_gen.chez.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(define (open-output-file-generic f) (open-output-file f 'replace ))
(include "opcode_gen.scm")
5 changes: 5 additions & 0 deletions lib/opcode_gen.chicken.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(import (r7rs))
(import (srfi 28)) ;; basic format
(define open-output-file-generic open-output-file)

(include "opcode_gen.scm")
9 changes: 4 additions & 5 deletions lib/opcode_gen.scm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(include "util.scm")
(define (open-output-file-chez f) (open-output-file f 'replace ))

(define opcodes '())

Expand Down Expand Up @@ -59,15 +58,15 @@
(iota (length opcodes))))


(define opcode-cpp (open-output-file-chez "opcodes.c"))
(define opcode-cpp (open-output-file-generic "opcodes.c"))
(display "const char* ins_names[] = {\n" opcode-cpp)
(for-each (lambda (op)
(display (format " \"~a\",\n" (symbol->string (car op))) opcode-cpp)
) opcodes)
(display "};\n" opcode-cpp)
(close-output-port opcode-cpp)

(define opcode-h (open-output-file-chez "opcodes.h"))
(define opcode-h (open-output-file-generic "opcodes.h"))
(display "#pragma once\n\n" opcode-h)
(display "extern const char* ins_names[];\n" opcode-h)
(display "enum {\n" opcode-h)
Expand All @@ -78,7 +77,7 @@
(display "};\n" opcode-h)
(close-output-port opcode-h)

(define op-table-h (open-output-file-chez "opcodes-table.h"))
(define op-table-h (open-output-file-generic "opcodes-table.h"))
(display "#ifdef PROFILER\n" op-table-h)
(for-each (lambda (op)
(display (format "void INS_PROFILE_~a(unsigned char ra, unsigned instr, unsigned *pc, long *frame, void **op_table_arg, long argcnt) {\n" (caddr op)) op-table-h)
Expand All @@ -99,7 +98,7 @@

(close-output-port op-table-h)

(define opcode-scm (open-output-file-chez "opcodes.scm"))
(define opcode-scm (open-output-file-generic "opcodes.scm"))
(display "(define opcodes '(\n" opcode-scm)
(for-each (lambda (op)
(display (format " (~a ~a)\n" (symbol->string (car op)) (cadr op)) opcode-scm))
Expand Down
70 changes: 58 additions & 12 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,77 @@ add_custom_target(
COMMAND clang-format -i *.h *.c
)

# Find a suitable scheme for bootstrapping.
# Chez and chicken have been tested, it's likely most r5rs (+ write-u8) compatible
# schemes will work with minor changes.
find_program(CHEZ chez)
if (NOT CHEZ)
find_program(CHEZ chezscheme)
if (NOT CHEZ)
find_program(CSC csc)
if (NOT CSC)
message(FATAL_ERROR "Can't find chez scheme for bootstrapping")
endif()
endif()
endif()

# Generate the bootstrap image.
if (CHEZ)
message("Using " ${CHEZ} " for bootstrapping")
add_custom_command(
OUTPUT bootstrap.scm.bc bootstrap.c
COMMAND
mkdir -p third-party
COMMAND
rsync ${CMAKE_SOURCE_DIR}/lib/bc.chez.scm ${CMAKE_SOURCE_DIR}/lib/bc.scm ${CMAKE_SOURCE_DIR}/lib/memory_layout.scm ${CMAKE_SOURCE_DIR}/lib/passes.scm ${CMAKE_SOURCE_DIR}/lib/bootstrap.scm ${CMAKE_SOURCE_DIR}/lib/str2num.scm .
COMMAND
rsync ${CMAKE_SOURCE_DIR}/third-party/alexpander.scm third-party/.
COMMAND
${CHEZ} --script bc.chez.scm bootstrap.scm > /dev/null
COMMAND
xxd -i bootstrap.scm.bc > bootstrap.c
DEPENDS ${SCM_SOURCES} opcodes.scm
)
# Generate the opcodes
add_custom_command(
OUTPUT bootstrap.scm.bc bootstrap.c
COMMAND
mkdir -p third-party
COMMAND
rsync ${CMAKE_SOURCE_DIR}/lib/bc.chez.scm ${CMAKE_SOURCE_DIR}/lib/bc.scm ${CMAKE_SOURCE_DIR}/lib/memory_layout.scm ${CMAKE_SOURCE_DIR}/lib/passes.scm ${CMAKE_SOURCE_DIR}/lib/bootstrap.scm ${CMAKE_SOURCE_DIR}/lib/str2num.scm .
COMMAND
rsync ${CMAKE_SOURCE_DIR}/third-party/alexpander.scm third-party/.
OUTPUT opcodes.c opcodes.h opcodes-table.h opcodes.scm
COMMAND
${CHEZ} --script bc.chez.scm bootstrap.scm > /dev/null
rsync ${CMAKE_SOURCE_DIR}/lib/opcode_gen.scm ${CMAKE_SOURCE_DIR}/lib/util.scm ${CMAKE_SOURCE_DIR}/src/vm.c .
COMMAND
xxd -i bootstrap.scm.bc > bootstrap.c
DEPENDS ${SCM_SOURCES} opcodes.scm
${CHEZ} --script ${CMAKE_SOURCE_DIR}/lib/opcode_gen.chez.scm
DEPENDS ${CMAKE_SOURCE_DIR}/lib/util.scm ${CMAKE_SOURCE_DIR}/lib/opcode_gen.scm vm.c
)

else()
message("Using " ${CSC} " for bootstrapping")
add_custom_command(
OUTPUT bootstrap.scm.bc bootstrap.c
COMMAND
mkdir -p third-party
COMMAND
rsync ${CMAKE_SOURCE_DIR}/lib/bc.chicken.scm ${CMAKE_SOURCE_DIR}/lib/bc.scm ${CMAKE_SOURCE_DIR}/lib/memory_layout.scm ${CMAKE_SOURCE_DIR}/lib/passes.scm ${CMAKE_SOURCE_DIR}/lib/bootstrap.scm ${CMAKE_SOURCE_DIR}/lib/str2num.scm .
COMMAND
rsync ${CMAKE_SOURCE_DIR}/third-party/alexpander.scm third-party/.
COMMAND
${CSC} bc.chicken.scm
COMMAND
./bc.chicken bootstrap.scm > /dev/null
COMMAND
xxd -i bootstrap.scm.bc > bootstrap.c
DEPENDS ${SCM_SOURCES} opcodes.scm
)
# Generate the opcodes
add_custom_command(
OUTPUT opcodes.c opcodes.h opcodes-table.h opcodes.scm
COMMAND
rsync ${CMAKE_SOURCE_DIR}/lib/opcode_gen.scm ${CMAKE_SOURCE_DIR}/lib/util.scm ${CMAKE_SOURCE_DIR}/src/vm.c .
COMMAND
${CHEZ} --script ${CMAKE_SOURCE_DIR}/lib/opcode_gen.scm
${CSC} ${CMAKE_SOURCE_DIR}/lib/opcode_gen.chicken.scm
COMMAND
${CMAKE_SOURCE_DIR}/lib/opcode_gen.chicken
DEPENDS ${CMAKE_SOURCE_DIR}/lib/util.scm ${CMAKE_SOURCE_DIR}/lib/opcode_gen.scm vm.c
)
endif()


# Actual executable
add_executable(hawk hawk.c)
Expand Down

0 comments on commit c62b928

Please sign in to comment.