<commit_before>(cl:in-package #:sicl-extrinsic-environment)

;;; Introduce immediates where appropriate.  The definition of the
;;; generic function says that we return an integer if we want to
;;; introduce an immediate value, and NIL if we do not want to
;;; introduce an immediate value.
(defmethod cleavir-generate-ast:convert-constant-to-immediate
    (constant (environment environment) system)
  (declare (ignore system))
  (cond ((and (typep constant 'integer)
	      (<= #.(- (expt 2 30)) constant #.(1- (expt 2 30))))
	 (* 2 constant))
	((typep constant 'character)
	 ;; FIXME: Currently, we depend on the host having the same
	 ;; character encoding as the target.
	 (+ #b11 (ash (char-code constant) 3)))
	(t
	 nil)))

;;; When we are asked to compile the name of a global function, by
;;; default Cleavir generates an FDEFINITION-AST taking the function
;;; name as an input.  For SICL, we do not want that.  Instead we want
;;; it to generate an access to the CAR of the global function cell
;;; that contains the function.  And we want the function cell to be
;;; accessed at load time.
(defmethod cleavir-generate-ast:convert-global-function
    ((info cleavir-env:global-function-info) (env environment) system)
  (declare (ignore system))
  (cleavir-ast:make-car-ast
   (cleavir-ast:make-load-time-value-ast
    `(sicl-global-environment:function-cell
      ',(cleavir-env:name info)
      sicl-global-environment:*global-environment*)
    ;; The cell is not read-only.
    nil)))

;;; When we are asked to compile the name of a special variable, by
;;; default Cleavir generates a SYMBOL-VALUE-AST taking the variable
;;; name as an input.  For SICL, we do not want that.  Instead we want
;;; it to generate a call to the function
;;; SICL-EXTRINSIC-ENVIRONMENT:SYMBOL-VALUE, passing it the symbol
;;; naming the variable and the GLOBAL ENVIRONMENT.
;;;
;;; The global environment is used by the function
;;; SICL-EXTRINSIC-ENVIRONMENT:SYMBOL-VALUE to retrieve the value
;;; representing UNBOUND in that environment, and to retrieve the
;;; global value cell in case the variable is not bound in the dynamic
;;; run-time environment.
(defmethod cleavir-generate-ast:convert-special-variable
    ((info cleavir-env:special-variable-info) (env environment) system)
  (declare (ignore system))
  (cleavir-ast:make-call-ast
   (cleavir-ast:make-car-ast
    (cleavir-ast:make-load-time-value-ast
     '(sicl-global-environment:function-cell
       'sicl-extrinsic-environment:symbol-value
       sicl-global-environment:*global-environment*)))
   (list (cleavir-ast:make-load-time-value-ast
	  `',(cleavir-env:name info)
	  t)
	 (cleavir-ast:make-load-time-value-ast
	  'sicl-global-environment:*global-environment*<commit_msg>Access enviornment at load time by calling new function.<commit_after>(cl:in-package #:sicl-extrinsic-environment)

;;; Introduce immediates where appropriate.  The definition of the
;;; generic function says that we return an integer if we want to
;;; introduce an immediate value, and NIL if we do not want to
;;; introduce an immediate value.
(defmethod cleavir-generate-ast:convert-constant-to-immediate
    (constant (environment environment) system)
  (declare (ignore system))
  (cond ((and (typep constant 'integer)
	      (<= #.(- (expt 2 30)) constant #.(1- (expt 2 30))))
	 (* 2 constant))
	((typep constant 'character)
	 ;; FIXME: Currently, we depend on the host having the same
	 ;; character encoding as the target.
	 (+ #b11 (ash (char-code constant) 3)))
	(t
	 nil)))

;;; When we are asked to compile the name of a global function, by
;;; default Cleavir generates an FDEFINITION-AST taking the function
;;; name as an input.  For SICL, we do not want that.  Instead we want
;;; it to generate an access to the CAR of the global function cell
;;; that contains the function.  And we want the function cell to be
;;; accessed at load time.
(defmethod cleavir-generate-ast:convert-global-function
    ((info cleavir-env:global-function-info) (env environment) system)
  (declare (ignore system))
  (cleavir-ast:make-car-ast
   (cleavir-ast:make-load-time-value-ast
    `(sicl-global-environment:function-cell
      ',(cleavir-env:name info)
      (sicl-global-environment:global-environment))
    ;; The cell is not read-only.
    nil)))

;;; When we are asked to compile the name of a special variable, by
;;; default Cleavir generates a SYMBOL-VALUE-AST taking the variable
;;; name as an input.  For SICL, we do not want that.  Instead we want
;;; it to generate a call to the function
;;; SICL-EXTRINSIC-ENVIRONMENT:SYMBOL-VALUE, passing it the symbol
;;; naming the variable and the GLOBAL ENVIRONMENT.
;;;
;;; The global environment is used by the function
;;; SICL-EXTRINSIC-ENVIRONMENT:SYMBOL-VALUE to retrieve the value
;;; representing UNBOUND in that environment, and to retrieve the
;;; global value cell in case the variable is not bound in the dynamic
;;; run-time environment.
(defmethod cleavir-generate-ast:convert-special-variable
    ((info cleavir-env:special-variable-info) (env environment) system)
  (declare (ignore system))
  (cleavir-ast:make-call-ast
   (cleavir-ast:make-car-ast
    (cleavir-ast:make-load-time-value-ast
     '(sicl-global-environment:function-cell
       'sicl-extrinsic-environment:symbol-value
       sicl-global-environment:*global-environment*)))
   (list (cleavir-ast:make-load-time-value-ast
	  `',(cleavir-env:name info)
	  t)
	 (cleavir-ast:make-load-time-value-ast
	  'sicl-global-environment:*global-environment*
{'fraction_non_alphanumeric': 0.11601809954751131, 'fraction_numerical': 0.003981900452488688, 'mean_word_length': 4.126159554730983, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5391501', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1664, 'n_words': 583}