## runtime.pkg
#
#####################################
# BLACK MAGIC!
#
# This file interfaces with C and assembly
# in odd ways -- modify at your peril!
#####################################
# Compiled by:
# src/lib/core/init/init.cmi
# This file implements the bootstrap communication bridge
# between Mythryl- and C-level code. In particular, it
# implements access to the
#
# get_mythryl_callable_c_function
# in
# src/c/lib/mythryl-callable-c-libraries.c
#
# which provides Mythryl-level access to all the C libraries listed in
#
# src/c/lib/mythryl-callable-c-libraries-list.h
#
# Our code here in runtime.pkg is essentially a dummy -- the
# actual package implementation is constructed in
#
# src/c/main/construct-runtime-package.c
#
# leaning heavily on code from the platform-specific files
#
# src/c/machine-dependent/prim.intel32.asm
# src/c/machine-dependent/prim.sparc32.asm
# src/c/machine-dependent/prim.pwrpc32.asm
# src/c/machine-dependent/prim.intel32.masm
#
# Mythryl source code is compiled against these declarations.
#
# There is a kludge in mythryl-runtime-intel32 so that at linktime references
# to the below heap values are transparently redirected to the
# actual C implementations.
#
# Parts of this kludge are in
# src/c/main/load-compiledfiles.c: load_compiled_files__may_heapclean
#
src/app/makelib/mythryl-compiler-compiler/find-set-of-compiledfiles-for-executable.pkg# -- search for "RUNTIME_PACKAGE_PICKLEHASH".
#
# The required special handling for runtime.pkg
# is specified by the "runtime-system-placeholder" line in the file
# src/lib/core/init/init.cmi
# and then implemented in
#
src/app/makelib/mythryl-compiler-compiler/process-mythryl-primordial-library.pkg#
# The C global variable
#
# runtime_package__global
#
# holds the actual 'runtime' package definition constructed by
#
# construct_runtime_package__global () from src/c/main/construct-runtime-package.c
#
# and substituded by
#
# load_compiled_files__may_heapclean () from src/c/main/load-compiledfiles.c
#
# for the placeholder
#
# runtime.pkg.compiled
#
# file.
#
# This is the central sleight-of-hand which makes everything work.
### "When we remember we are all mad, the mysteries
### of life disappear and life stands explained."
###
### -- Mark Twain,
### Notebook, 1898
# This package gets 'include'-d in:
#
src/lib/core/init/core.pkgpackage runtime: Runtime_Boxed { # Runtime_Boxed is from
src/lib/core/init/runtime.api #
Chunk = Chunk; # Anything in the Mythryl heap.
# Chunk is defined ultimately in
src/lib/compiler/front/typer/basics/basetype-numbers.pkg # The "Chunk" name is assigned in
src/lib/compiler/front/typer/types/more-type-types.pkg Null_Or(X) = NULL
| THE X;
# Declarations whose right handside is a primOp do not
# generate any code. This is a hack, and should be cleaned
# in the future. (ZHONG) XXX BUGGO FIXME
my cast: X -> Y = inline::cast;
package asm {
#
# asmcode functions directly callable from Mythryl.
# Overview comments may be found in
src/lib/core/init/runtime.api
#
# These functions are actually implemented in
# one of the platform-specific assembly-files:
#
# src/c/machine-dependent/prim.intel32.asm
# src/c/machine-dependent/prim.sparc32.asm
# src/c/machine-dependent/prim.pwrpc32.asm
# src/c/machine-dependent/prim.intel32.masm
#
# They become accessible by virtue of being slotted
# into an ersatz exports list RunVec in
#
# src/c/main/construct-runtime-package.c
#
# which is then passed via
#
# runtime_package__global
#
# to src/c/main/load-compiledfiles.c
#
# which quietly substitutes it for the actual (useless)
# runtime.pkg.compiled code.
Cfunction = Cfunction;
Unt8_Rw_Vector = Unt8_Rw_Vector;
Float64_Rw_Vector = Float64_Rw_Vector;
Spin_Lock = Spin_Lock;
#
fun make_typeagnostic_rw_vector (x: Chunk): Chunk = cast x;
fun find_cfun (x: Chunk): Chunk = cast x; # Gets used in
src/lib/std/src/unsafe/mythryl-callable-c-library-interface.pkg fun call_cfun (x: Chunk): Chunk = cast x; # Gets used in
src/lib/std/src/unsafe/mythryl-callable-c-library-interface.pkg fun make_unt8_rw_vector (x: Chunk): Unt8_Rw_Vector = cast x;
fun make_float64_rw_vector (x: Chunk): Float64_Rw_Vector = cast x;
fun make_string (x: Chunk): String = cast x;
fun make_typeagnostic_ro_vector (x: Chunk): Chunk = cast x;
fun floor (x: Chunk): Chunk = cast x;
fun logb (x: Chunk): Chunk = cast x;
fun scalb (x: Chunk): Chunk = cast x;
fun try_lock (x: Spin_Lock): Chunk = cast x;
fun unlock (x: Spin_Lock): Chunk = cast x;
};
exception DIVIDE_BY_ZERO;
exception OVERFLOW;
exception RUNTIME_EXCEPTION (String, Null_Or(Int)); # C-level runtime errno.h/strerror() (or such) errors. See src/c/lib/raise-error.c
my zero_length_vector__global: Vector(X) = cast(); # ZERO_LENGTH_VECTOR__GLOBAL in src/c/main/construct-runtime-package.c
my this_fn_profiling_hook_refcell__global: Ref( Int ) = cast(); # THIS_FN_PROFILING_HOOK_REFCELL__GLOBAL in src/c/main/construct-runtime-package.c
#
my software_generated_periodic_events_switch_refcell__global: Ref( Bool ) = cast(); # SOFTWARE_GENERATED_PERIODIC_EVENTS_SWITCH_REFCELL__GLOBAL in src/c/main/construct-runtime-package.c
my software_generated_periodic_event_interval_refcell__global: Ref( Int ) = cast(); # SOFTWARE_GENERATED_PERIODIC_EVENT_INTERVAL_REFCELL__GLOBAL in src/c/main/construct-runtime-package.c
my software_generated_periodic_event_handler_refcell__global: Ref( Fate(Void) -> Fate(Void) ) = cast(); # SOFTWARE_GENERATED_PERIODIC_EVENTS_HANDLER_REFCELL__GLOBAL in src/c/main/construct-runtime-package.c
#
my microthread_switch_lock_refcell__global: Ref( Int ) = cast(); # MICROTHREAD_SWITCH_LOCK_REFCELL__GLOBAL in src/c/main/construct-runtime-package.c
my pervasive_package_pickle_list__global: Ref( Chunk ) = cast(); # PERVASIVE_PACKAGE_PICKLE_LIST_REFCELL__GLOBAL in src/c/main/construct-runtime-package.c
my posix_interprocess_signal_handler_refcell__global: Ref((Int,Int,Fate(Void)) -> Fate(Void)) = cast(); # POSIX_INTERPROCESS_SIGNAL_HANDLER_REFCELL__GLOBAL in src/c/main/construct-runtime-package.c
# We use
# this_fn_profiling_hook_refcell__global
# in:
#
src/lib/std/src/nj/runtime-profiling-control.pkg #
src/lib/compiler/toplevel/interact/read-eval-print-loop-g.pkg # We use
# software_generated_periodic_events_switch_refcell__global
# in:
#
src/lib/std/src/unsafe/software-generated-periodic-events.api #
src/lib/std/src/unsafe/software-generated-periodic-events.pkg};