1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-13 09:32:47 +00:00
emacs/lisp/emacs-lisp/comp.el

722 lines
26 KiB
EmacsLisp
Raw Normal View History

2019-07-07 07:23:10 +00:00
;;; comp.el --- compilation of Lisp code into native code -*- lexical-binding: t -*-
;; Copyright (C) 2019 Free Software Foundation, Inc.
;; Keywords: lisp
;; Package: emacs
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
2019-07-13 13:48:02 +00:00
;;; Commentary:
2019-07-14 12:39:29 +00:00
;; This code is an attempt to make the pig fly.
;; Or, to put it another way to make a Carrera out of a turbocharged VW Bug.
2019-07-13 13:48:02 +00:00
2019-07-07 07:23:10 +00:00
;;; Code:
2019-07-07 10:30:03 +00:00
(require 'bytecomp)
2019-07-08 09:37:17 +00:00
(require 'cl-lib)
(require 'cl-extra)
(require 'subr-x)
2019-07-07 07:23:10 +00:00
(defgroup comp nil
"Emacs Lisp native compiler."
:group 'lisp)
2019-07-07 10:30:03 +00:00
(defconst comp-debug t)
2019-07-07 07:23:10 +00:00
2019-07-14 22:58:03 +00:00
;; FIXME these has to be removed
2019-07-13 15:24:44 +00:00
(defvar comp-speed 2)
2019-07-14 22:58:03 +00:00
(defvar byte-compile-lap-output)
2019-07-13 15:24:44 +00:00
2019-07-21 13:20:39 +00:00
(defvar comp-pass nil
"Every pass has the right to bind what it likes here.")
(defconst comp-passes '(comp-spill-lap
2019-07-07 10:30:03 +00:00
comp-limplify)
"Passes to be executed in order.")
2019-07-21 13:20:39 +00:00
(defconst comp-known-ret-types '((Fcons . cons))
"Alist used for type propagation.")
2019-07-07 19:49:11 +00:00
2019-07-08 05:17:28 +00:00
(defconst comp-mostly-pure-funcs
'(% * + - / /= 1+ 1- < <= = > >= cons list % concat logand logcount logior
lognot logxor regexp-opt regexp-quote string-to-char string-to-syntax
symbol-name)
"Functions on witch we do constant propagation."
;; Is it acceptable to move into the compile time functions that are
;; allocating memory? (these are technically not side effect free)
)
(eval-when-compile
(defconst comp-op-stack-info
(cl-loop with h = (make-hash-table)
for k across byte-code-vector
for v across byte-stack+-info
when k
do (puthash k v h)
finally return h)
"Hash table lap-op -> stack adjustment."))
2019-07-07 10:30:03 +00:00
(cl-defstruct comp-args
2019-07-08 13:29:32 +00:00
(min nil :type number
2019-07-14 18:54:53 +00:00
:documentation "Minimum number of arguments allowed.")
2019-07-08 13:29:32 +00:00
(max nil
2019-07-21 13:20:39 +00:00
:documentation "Maximum number of arguments allowed.
To be used when ncall-conv is nil.")
2019-07-08 13:29:32 +00:00
(ncall-conv nil :type boolean
:documentation "If t the signature is:
2019-07-14 18:54:53 +00:00
(ptrdiff_t nargs, Lisp_Object *args)."))
2019-07-07 10:30:03 +00:00
2019-07-14 15:21:34 +00:00
(cl-defstruct (comp-block (:copier nil))
"A basic block."
2019-07-21 13:20:39 +00:00
;; The first two slots are used during limplification.
2019-07-14 15:21:34 +00:00
(sp nil
2019-07-21 13:20:39 +00:00
:documentation "When non nil indicates the sp value while entering
2019-07-14 18:54:53 +00:00
into it.")
2019-07-14 15:21:34 +00:00
(closed nil :type 'boolean
2019-07-21 13:20:39 +00:00
:documentation "If the block was already closed.")
(insns () :type list
:documentation "List of instructions."))
2019-07-14 15:21:34 +00:00
2019-07-07 10:30:03 +00:00
(cl-defstruct (comp-func (:copier nil))
2019-07-21 13:20:39 +00:00
"LIMPLE representation of a function."
2019-07-07 10:30:03 +00:00
(symbol-name nil
2019-07-14 18:54:53 +00:00
:documentation "Function symbol's name.")
2019-07-08 10:11:34 +00:00
(c-func-name nil :type 'string
2019-07-14 18:54:53 +00:00
:documentation "The function name in the native world.")
2019-07-07 10:30:03 +00:00
(func nil
2019-07-14 18:54:53 +00:00
:documentation "Original form.")
2019-07-07 10:30:03 +00:00
(byte-func nil
2019-07-14 18:54:53 +00:00
:documentation "Byte compiled version.")
2019-07-21 13:20:39 +00:00
(lap () :type list
:documentation "Lap assembly representation.")
2019-07-08 05:56:37 +00:00
(args nil :type 'comp-args)
2019-07-08 13:29:32 +00:00
(frame-size nil :type 'number)
2019-07-14 15:21:34 +00:00
(blocks (make-hash-table) :type 'hash-table
:documentation "Key is the basic block symbol value is a comp-block
2019-07-14 18:54:53 +00:00
structure.")
2019-07-14 12:39:29 +00:00
(lap-block (make-hash-table :test #'equal) :type 'hash-table
:documentation "Key value to convert from LAP label number to
2019-07-14 18:54:53 +00:00
LIMPLE basic block.")
2019-07-21 13:20:39 +00:00
(ssa-cnt -1 :type 'number
2019-07-14 18:54:53 +00:00
:documentation "Counter to create ssa limple vars."))
2019-07-07 10:30:03 +00:00
2019-07-08 05:56:37 +00:00
(cl-defstruct (comp-mvar (:copier nil) (:constructor make--comp-mvar))
2019-07-07 19:49:11 +00:00
"A meta-variable being a slot in the meta-stack."
2019-07-09 20:28:29 +00:00
(id nil :type number
2019-07-14 18:54:53 +00:00
:documentation "SSA number.")
2019-07-07 10:30:03 +00:00
(slot nil :type fixnum
2019-07-14 18:54:53 +00:00
:documentation "Slot position.")
2019-07-07 10:30:03 +00:00
(const-vld nil
2019-07-14 18:54:53 +00:00
:documentation "Valid signal for the following slot.")
2019-07-07 10:30:03 +00:00
(constant nil
:documentation "When const-vld non nil this is used for constant
2019-07-14 18:54:53 +00:00
propagation.")
2019-07-07 10:30:03 +00:00
(type nil
2019-07-14 18:54:53 +00:00
:documentation "When non nil is used for type propagation."))
2019-07-07 10:30:03 +00:00
2019-07-14 15:33:18 +00:00
(cl-defstruct (comp-limplify (:copier nil))
2019-07-14 18:54:53 +00:00
"Support structure used during limplification."
2019-07-07 10:30:03 +00:00
(sp 0 :type 'fixnum
2019-07-14 18:54:53 +00:00
:documentation "Current stack pointer while walking LAP.")
2019-07-07 10:30:03 +00:00
(frame nil :type 'vector
2019-07-14 18:54:53 +00:00
:documentation "Meta-stack used to flat LAP.")
2019-07-14 16:36:57 +00:00
(block-name nil :type 'symbol
2019-07-14 18:54:53 +00:00
:documentation "Current basic block name."))
2019-07-07 10:30:03 +00:00
2019-07-21 13:20:39 +00:00
(defun comp-pretty-print-func (func)
"Pretty print function FUNC in the current buffer."
(insert (format "\n\n Function: %s" (comp-func-symbol-name func)))
2019-08-03 14:15:37 +00:00
(cl-loop for block-name being each hash-keys of (comp-func-blocks func)
using (hash-value bb)
2019-07-21 13:20:39 +00:00
do (progn
(insert (concat "\n<" (symbol-name block-name) ">"))
(cl-prettyprint (comp-block-insns bb)))))
;;; spill-lap pass specific code.
2019-07-08 10:11:34 +00:00
(defun comp-c-func-name (symbol-function)
"Given SYMBOL-FUNCTION return a name suitable for the native code."
;; Unfortunatelly not all symbol names are valid as C function names...
2019-07-21 13:20:39 +00:00
;; Nassi's algorithm here:
2019-07-08 10:11:34 +00:00
(let* ((orig-name (symbol-name symbol-function))
(crypted (cl-loop with str = (make-string (* 2 (length orig-name)) 0)
for j from 0 by 2
for i across orig-name
for byte = (format "%x" i)
do (aset str j (aref byte 0))
do (aset str (1+ j) (aref byte 1))
finally return str))
(human-readable (replace-regexp-in-string
2019-07-10 19:29:32 +00:00
"-" "_" orig-name))
(human-readable (replace-regexp-in-string
2019-07-14 19:10:56 +00:00
(rx (not (any "0-9a-z_"))) "" human-readable)))
2019-07-08 10:11:34 +00:00
(concat "F" crypted "_" human-readable)))
2019-07-07 10:30:03 +00:00
(defun comp-decrypt-lambda-list (x)
"Decript lambda list X."
2019-07-08 13:29:32 +00:00
(let ((rest (not (= (logand x 128) 0)))
(mandatory (logand x 127))
(nonrest (ash x -8)))
(if (and (null rest)
(< nonrest 9)) ;; SUBR_MAX_ARGS
(make-comp-args :min mandatory
:max nonrest)
(make-comp-args :min mandatory
:ncall-conv t))))
2019-07-07 10:30:03 +00:00
2019-07-21 13:20:39 +00:00
(defun comp-spill-lap (func)
"Byte compile and spill the LAP rapresentation for FUNC."
(let (byte-compile-lap-output)
(setf (comp-func-byte-func func)
(byte-compile (comp-func-symbol-name func)))
(when comp-debug
(cl-prettyprint byte-compile-lap-output))
(let ((lambda-list (aref (comp-func-byte-func func) 0)))
(if (fixnump lambda-list)
(setf (comp-func-args func)
(comp-decrypt-lambda-list (aref (comp-func-byte-func func) 0)))
(error "Can't native compile a non lexical scoped function")))
(setf (comp-func-lap func) byte-compile-lap-output)
(setf (comp-func-frame-size func) (aref (comp-func-byte-func func) 3))
func))
2019-07-08 05:56:37 +00:00
2019-07-21 13:20:39 +00:00
;;; Limplification pass specific code.
;; Special vars used during limplifications
(defvar comp-block)
(defvar comp-func)
2019-07-08 09:37:17 +00:00
2019-07-08 05:56:37 +00:00
;; (defun comp-opt-call (inst)
;; "Optimize if possible a side-effect-free call in INST."
;; (cl-destructuring-bind (_ f &rest args) inst
;; (when (and (member f comp-mostly-pure-funcs)
;; (cl-every #'identity (mapcar #'comp-mvar-const-vld args)))
;; (apply f (mapcar #'comp-mvar-constant args)))))
2019-07-07 10:30:03 +00:00
2019-07-21 13:20:39 +00:00
(defun comp-new-frame (size)
"Return a clean frame of meta variables of size SIZE."
(let ((v (make-vector size nil)))
(cl-loop for i below size
do (aset v i (make-comp-mvar :slot i)))
v))
2019-08-03 14:15:37 +00:00
(cl-defun make-comp-mvar (&key slot (constant nil const-vld) type)
2019-07-21 13:20:39 +00:00
(make--comp-mvar :id (cl-incf (comp-func-ssa-cnt comp-func))
2019-07-08 07:29:13 +00:00
:slot slot :const-vld const-vld :constant constant
:type type))
2019-07-07 10:30:03 +00:00
(defmacro comp-sp ()
"Current stack pointer."
2019-07-14 18:54:53 +00:00
'(comp-limplify-sp comp-pass))
2019-07-07 10:30:03 +00:00
(defmacro comp-with-sp (sp &rest body)
"Execute BODY setting the stack pointer to SP.
2019-07-14 12:39:29 +00:00
Restore the original value afterwards."
(declare (debug (form body))
2019-07-14 12:39:29 +00:00
(indent defun))
(let ((sym (gensym)))
`(let ((,sym (comp-sp)))
(setf (comp-sp) ,sp)
(progn ,@body)
(setf (comp-sp) ,sym))))
2019-07-07 16:42:55 +00:00
(defmacro comp-slot-n (n)
"Slot N into the meta-stack."
(declare (debug (form)))
2019-07-14 18:54:53 +00:00
`(aref (comp-limplify-frame comp-pass) ,n))
2019-07-07 16:42:55 +00:00
2019-07-07 10:30:03 +00:00
(defmacro comp-slot ()
"Current slot into the meta-stack pointed by sp."
2019-07-07 16:42:55 +00:00
'(comp-slot-n (comp-sp)))
2019-07-07 10:30:03 +00:00
2019-07-07 19:49:11 +00:00
(defmacro comp-slot-next ()
"Slot into the meta-stack pointed by sp + 1."
'(comp-slot-n (1+ (comp-sp))))
2019-07-21 13:20:39 +00:00
(defun comp-emit (insn)
"Emit INSN into current basic block."
(push insn (comp-block-insns comp-block)))
(defun comp-emit-set-call (call)
2019-07-13 15:08:15 +00:00
"Emit CALL assigning the result the the current slot frame.
If the calle function is known to have a return type propagate it."
2019-07-13 09:33:15 +00:00
(cl-assert call)
(setf (comp-slot)
2019-07-08 07:29:13 +00:00
(make-comp-mvar :slot (comp-sp)
2019-07-13 15:08:15 +00:00
:type (when (> comp-speed 0)
(alist-get (cadr call)
comp-known-ret-types))))
(comp-emit (list 'set (comp-slot) call)))
2019-07-13 09:33:15 +00:00
2019-07-20 13:49:30 +00:00
(defmacro comp-emit-set-call-subr (subr-name sp-delta &optional c-fun-name)
2019-07-14 18:25:42 +00:00
"Emit a call for SUBR-NAME using C-FUN-NAME.
2019-07-20 13:49:30 +00:00
SP-DELTA is the stack adjustment.
If C-FUN-NAME is nil it will be guessed from SUBR-NAME."
2019-07-14 21:35:04 +00:00
(let ((subr (symbol-function subr-name))
2019-07-20 13:49:30 +00:00
(subr-str (symbol-name subr-name))
(nargs (1+ (- sp-delta))))
2019-07-14 21:35:04 +00:00
(cl-assert (subrp subr) nil
"%s not a subr" subr-str)
(let* ((arity (subr-arity subr))
(minarg (car arity))
(maxarg (cdr arity)))
(unless c-fun-name
(setq c-fun-name
(intern (concat "F"
(replace-regexp-in-string
"-" "_"
subr-str)))))
2019-07-20 13:49:30 +00:00
(cl-assert (not (eq maxarg 'unevalled)) nil
"%s contains unevalled arg" subr-name)
(if (eq maxarg 'many)
;; callref case.
`(comp-emit-set-call (list 'callref ',c-fun-name ,nargs (comp-sp)))
;; Normal call.
(cl-assert (and (>= maxarg nargs) (<= minarg nargs))
(nargs maxarg minarg)
"Incoherent stack adjustment %d, maxarg %d minarg %d")
`(let* ((c-fun-name ',c-fun-name)
(slots (cl-loop for i from 0 below ,maxarg
collect (comp-slot-n (+ i (comp-sp))))))
(comp-emit-set-call `(call ,c-fun-name ,@slots)))))))
2019-07-14 18:25:42 +00:00
2019-07-20 17:26:30 +00:00
(defun comp-copy-slot (src-n &optional dst-n)
"Set slot number DST-N to slot number SRC-N as source.
If DST-N is specified use it otherwise assume it to be the current slot."
(comp-with-sp (if dst-n dst-n (comp-sp))
(let ((src-slot (comp-slot-n src-n)))
(cl-assert src-slot)
;; FIXME id should encrease here.
(setf (comp-slot)
(copy-sequence src-slot))
(setf (comp-mvar-slot (comp-slot)) (comp-sp))
(comp-emit (list 'set (comp-slot) src-slot)))))
2019-07-10 16:55:19 +00:00
(defun comp-emit-annotation (str)
"Emit annotation STR."
(comp-emit `(comment ,str)))
2019-07-10 16:55:19 +00:00
2019-07-14 18:25:42 +00:00
(defun comp-emit-set-const (val)
2019-07-13 16:28:00 +00:00
"Set constant VAL to current slot."
2019-07-09 20:28:29 +00:00
(setf (comp-slot) (make-comp-mvar :slot (comp-sp)
:constant val))
(comp-emit (list 'setimm (comp-slot) val)))
2019-07-08 15:04:33 +00:00
2019-07-14 16:36:57 +00:00
(defun comp-mark-block-closed ()
"Mark current basic block as closed."
2019-07-14 18:54:53 +00:00
(setf (comp-block-closed (gethash (comp-limplify-block-name comp-pass)
2019-07-14 16:36:57 +00:00
(comp-func-blocks comp-func)))
t))
(defun comp-emit-jump (target)
"Emit an unconditional branch to block TARGET."
(comp-emit (list 'jump target))
(comp-mark-block-closed))
2019-07-14 15:21:34 +00:00
(defun comp-emit-block (block-name)
"Emit basic block BLOCK-NAME."
2019-07-14 16:36:57 +00:00
(let ((blocks (comp-func-blocks comp-func)))
;; In case does not exist register it into comp-func-blocks.
(unless (gethash block-name blocks)
(puthash block-name
(make-comp-block :sp (comp-sp))
blocks))
;; If we are abandoning an non closed basic block close it with a fall
;; through.
(when (and (not (eq block-name 'entry))
2019-07-21 13:20:39 +00:00
(not (comp-block-closed
(gethash (comp-limplify-block-name comp-pass)
blocks))))
2019-07-14 16:36:57 +00:00
(comp-emit-jump block-name))
2019-07-21 13:20:39 +00:00
;; Set this a currently compiled block.
(setf comp-block (gethash block-name blocks))
2019-07-14 16:36:57 +00:00
;; Every new block we are forced to wipe out all the frame.
;; This will be optimized by proper flow analysis.
2019-07-14 18:54:53 +00:00
(setf (comp-limplify-frame comp-pass)
(comp-new-frame (comp-func-frame-size comp-func)))
2019-07-14 16:36:57 +00:00
;; If we are landing here form a recorded branch adjust sp accordingly.
(setf (comp-sp)
(comp-block-sp (gethash block-name blocks)))
2019-07-14 18:54:53 +00:00
(setf (comp-limplify-block-name comp-pass) block-name)))
2019-07-14 16:36:57 +00:00
2019-08-03 15:08:55 +00:00
(defun comp-emit-cond-jump (a b target-offset lap-label negated)
"Emit a conditional jump to LAP-LABEL when A and B satisfy EQ.
2019-07-21 07:48:52 +00:00
TARGET-OFFSET is the positive offset on the SP when branching to the target
block.
2019-08-03 15:08:55 +00:00
If NEGATED non nil negate the tested condition."
2019-07-21 07:48:52 +00:00
(let ((blocks (comp-func-blocks comp-func))
(bb (comp-new-block-sym))) ;; Fall through block
2019-07-14 16:36:57 +00:00
(puthash bb
2019-07-21 07:48:52 +00:00
(make-comp-block :sp (comp-sp))
2019-07-14 16:36:57 +00:00
blocks)
2019-07-21 07:48:52 +00:00
(let ((target (comp-lap-to-limple-bb lap-label)))
(comp-emit (if negated
2019-08-03 15:08:55 +00:00
(list 'cond-jump a b target bb)
(list 'cond-jump a b bb target)))
2019-07-21 07:48:52 +00:00
(puthash target
(make-comp-block :sp (+ target-offset (comp-sp)))
blocks)
(comp-mark-block-closed))
2019-07-14 16:36:57 +00:00
(comp-emit-block bb)))
2019-07-14 12:39:29 +00:00
2019-07-13 16:28:00 +00:00
(defun comp-stack-adjust (n)
"Move sp by N."
(cl-incf (comp-sp) n))
2019-07-07 10:30:03 +00:00
2019-07-08 07:15:09 +00:00
(defun comp-limplify-listn (n)
2019-07-08 07:29:13 +00:00
"Limplify list N."
2019-08-03 17:42:57 +00:00
(comp-with-sp (+ (comp-sp) n -1)
(comp-emit-set-call `(call Fcons
,(comp-slot)
2019-08-03 14:15:37 +00:00
,(make-comp-mvar :constant nil))))
(cl-loop for sp from (+ (comp-sp) n -2) downto (comp-sp)
do (comp-with-sp sp
(comp-emit-set-call `(call Fcons
,(comp-slot)
,(comp-slot-next))))))
2019-07-14 12:39:29 +00:00
(defun comp-new-block-sym ()
"Return a symbol naming the next new basic block."
2019-07-14 15:21:34 +00:00
(intern (format "bb_%s" (hash-table-count (comp-func-blocks comp-func)))))
2019-07-14 12:39:29 +00:00
(defun comp-lap-to-limple-bb (n)
"Given the LAP label N return the limple basic block."
(let ((hash (comp-func-lap-block comp-func)))
(if-let ((bb (gethash n hash)))
;; If was already created return it.
bb
(let ((name (comp-new-block-sym)))
(puthash n name hash)
name))))
2019-07-22 09:08:53 +00:00
(defun comp-emit-handler (guarded-label handler-type)
"Emit a non local exit handler for GUARDED-LABEL of type HANDLER-TYPE."
(let ((blocks (comp-func-blocks comp-func))
(guarded-bb (comp-new-block-sym)))
(puthash guarded-bb
(make-comp-block :sp (comp-sp))
blocks)
(let ((handler-bb (comp-lap-to-limple-bb guarded-label)))
(comp-emit (list 'push-handler (comp-slot-next)
handler-type
handler-bb
guarded-bb))
(puthash handler-bb
(make-comp-block :sp (1+ (comp-sp)))
blocks)
(comp-mark-block-closed)
(comp-emit-block guarded-bb))))
2019-08-03 15:08:55 +00:00
(defun comp-emit-switch (var m-hash)
"Emit a limple for a lap jump table given VAR and M-HASH."
(cl-assert (comp-mvar-const-vld m-hash))
(cl-loop for test being each hash-keys of (comp-mvar-constant m-hash)
using (hash-value target-label)
for m-test = (make-comp-mvar :constant test)
do (comp-emit-cond-jump var m-test 0 target-label nil)))
(defmacro comp-op-case (&rest cases)
2019-07-14 18:25:42 +00:00
"Expand CASES into the corresponding pcase.
2019-07-14 19:02:01 +00:00
This is responsible for generating the proper stack adjustment when known and
the annotation emission."
(declare (debug (body))
(indent defun))
2019-07-20 14:44:40 +00:00
(cl-labels ((op-to-fun (x)
;; Given the LAP op strip "byte-" to have the subr name.
(intern (replace-regexp-in-string "byte-" "" x)))
(body-eff (body op-name sp-delta)
;; Given the original body BODY compute the effective one.
2019-07-20 15:35:57 +00:00
;; When BODY is auto guess function name form the LAP bytecode
;; name. Othewise expect lname fnname.
2019-07-20 14:44:40 +00:00
(pcase (car body)
('auto
(list `(comp-emit-set-call-subr
,(op-to-fun op-name)
,sp-delta)))
((pred symbolp)
(list `(comp-emit-set-call-subr
,(car body)
,sp-delta
,(cadr body))))
(_ body))))
2019-07-14 21:35:04 +00:00
`(pcase op
,@(cl-loop for (op . body) in cases
for sp-delta = (gethash op comp-op-stack-info)
for op-name = (symbol-name op)
if body
collect `(',op
2019-07-20 15:35:57 +00:00
;; Log all LAP ops except the TAG one.
2019-07-14 21:35:04 +00:00
,(unless (eq op 'TAG)
`(comp-emit-annotation
,(concat "LAP op " op-name)))
2019-07-20 15:35:57 +00:00
;; Emit the stack adjustment if present.
2019-07-20 13:49:30 +00:00
,(when (and sp-delta (not (eq 0 sp-delta)))
2019-07-14 21:35:04 +00:00
`(comp-stack-adjust ,sp-delta))
2019-07-20 14:44:40 +00:00
,@(body-eff body op-name sp-delta))
2019-07-14 21:35:04 +00:00
else
collect `(',op (error ,(concat "Unsupported LAP op "
op-name))))
(_ (error "Unexpected LAP op %s" (symbol-name op))))))
2019-07-08 07:15:09 +00:00
2019-07-22 09:08:53 +00:00
(defun comp-limplify-lap-inst (insn)
"Limplify LAP instruction INSN pushng it in the proper basic block."
(let ((op (car insn))
(arg (if (consp (cdr insn))
(cadr insn)
(cdr insn))))
(comp-op-case
2019-07-14 12:39:29 +00:00
(TAG
(comp-emit-block (comp-lap-to-limple-bb arg)))
(byte-stack-ref
2019-07-20 17:26:30 +00:00
(comp-copy-slot (- (comp-sp) arg 1)))
(byte-varref
2019-07-13 16:28:00 +00:00
(comp-emit-set-call `(call Fsymbol_value ,(make-comp-mvar
2019-07-14 08:57:46 +00:00
:constant arg))))
(byte-varset
(comp-emit `(call set_internal
2019-08-03 14:15:37 +00:00
,(make-comp-mvar :constant arg)
,(comp-slot))))
2019-07-14 19:26:20 +00:00
(byte-varbind
(comp-emit `(call specbind
2019-08-03 14:15:37 +00:00
,(make-comp-mvar :constant arg)
2019-07-14 19:26:20 +00:00
,(comp-slot-next))))
2019-07-14 09:15:18 +00:00
(byte-call
(comp-stack-adjust (- arg))
(comp-emit-set-call `(callref Ffuncall ,(1+ arg) ,(comp-sp))))
2019-07-14 19:26:20 +00:00
(byte-unbind
2019-08-11 07:34:30 +00:00
(comp-emit `(call helper_unbind_n
,(make-comp-mvar :constant arg))))
2019-07-21 09:38:26 +00:00
(byte-pophandler
(comp-emit '(pop-handler)))
(byte-pushconditioncase
2019-07-22 09:08:53 +00:00
(comp-emit-handler (cl-third insn) 'condition-case))
(byte-pushcatch
(comp-emit-handler (cl-third insn) 'catcher))
2019-07-14 22:58:03 +00:00
(byte-nth auto)
(byte-symbolp auto)
(byte-consp auto)
2019-07-14 21:35:04 +00:00
(byte-stringp auto)
(byte-listp auto)
(byte-eq auto)
(byte-memq auto)
2019-07-20 15:35:57 +00:00
(byte-not null Fnull)
2019-07-14 21:35:04 +00:00
(byte-car auto)
(byte-cdr auto)
(byte-cons auto)
(byte-list1
(comp-limplify-listn 1))
(byte-list2
(comp-limplify-listn 2))
(byte-list3
(comp-limplify-listn 3))
(byte-list4
(comp-limplify-listn 4))
2019-07-14 21:35:04 +00:00
(byte-length auto)
(byte-aref auto)
(byte-aset auto)
(byte-symbol-value auto)
2019-07-14 22:58:03 +00:00
(byte-symbol-function auto)
2019-07-14 21:35:04 +00:00
(byte-set auto)
(byte-fset auto)
(byte-get auto)
2019-07-20 14:34:37 +00:00
(byte-substring auto)
2019-07-14 08:57:46 +00:00
(byte-concat2
(comp-emit-set-call `(callref Fconcat 2 ,(comp-sp))))
(byte-concat3
(comp-emit-set-call `(callref Fconcat 3 ,(comp-sp))))
(byte-concat4
(comp-emit-set-call `(callref Fconcat 4 ,(comp-sp))))
2019-07-20 15:35:57 +00:00
(byte-sub1 1- Fsub1)
(byte-add1 1+ Fadd1)
(byte-eqlsign = Feqlsign)
2019-07-20 15:22:13 +00:00
(byte-gtr > Fgtr)
(byte-lss < Flss)
(byte-leq <= Fleq)
(byte-geq >= Fgeq)
2019-07-20 15:35:57 +00:00
(byte-diff - Fminus)
(byte-negate - Fminus)
2019-07-20 15:22:13 +00:00
(byte-plus + Fplus)
2019-07-20 14:34:37 +00:00
(byte-max auto)
(byte-min auto)
2019-07-20 15:22:13 +00:00
(byte-mult * Ftimes)
2019-07-14 22:58:03 +00:00
(byte-point auto)
2019-07-14 21:35:04 +00:00
(byte-goto-char auto)
2019-07-20 14:34:37 +00:00
(byte-insert auto)
2019-07-14 22:58:03 +00:00
(byte-point-max auto)
(byte-point-min auto)
2019-07-20 14:34:37 +00:00
(byte-char-after auto)
2019-07-14 21:35:04 +00:00
(byte-following-char auto)
2019-07-14 22:58:03 +00:00
(byte-preceding-char auto)
(byte-current-column auto)
2019-07-20 14:34:37 +00:00
(byte-indent-to auto)
(byte-scan-buffer-OBSOLETE)
2019-07-14 22:58:03 +00:00
(byte-eolp auto)
(byte-eobp auto)
(byte-bolp auto)
(byte-bobp auto)
(byte-current-buffer auto)
(byte-set-buffer auto)
(byte-save-current-buffer
(comp-emit '(call record_unwind_current_buffer)))
(byte-set-mark-OBSOLETE)
(byte-interactive-p-OBSOLETE)
2019-07-20 14:34:37 +00:00
(byte-forward-char auto)
(byte-forward-word auto)
(byte-skip-chars-forward auto)
(byte-skip-chars-backward auto)
(byte-forward-line auto)
2019-07-14 22:58:03 +00:00
(byte-char-syntax auto)
(byte-buffer-substring auto)
(byte-delete-region auto)
(byte-narrow-to-region)
(byte-widen)
2019-07-20 14:34:37 +00:00
(byte-end-of-line auto)
(byte-constant2)
2019-07-14 12:39:29 +00:00
(byte-goto
2019-07-22 09:08:53 +00:00
(comp-emit-jump (comp-lap-to-limple-bb (cl-third insn))))
2019-07-14 12:39:29 +00:00
(byte-goto-if-nil
2019-08-03 15:08:55 +00:00
(comp-emit-cond-jump (comp-slot-next) (make-comp-mvar :constant nil) 0
(cl-third insn) nil))
2019-07-14 12:39:29 +00:00
(byte-goto-if-not-nil
2019-08-03 15:08:55 +00:00
(comp-emit-cond-jump (comp-slot-next) (make-comp-mvar :constant nil) 0
(cl-third insn) t))
2019-07-14 12:39:29 +00:00
(byte-goto-if-nil-else-pop
2019-08-03 15:08:55 +00:00
(comp-emit-cond-jump (comp-slot-next) (make-comp-mvar :constant nil) 1
(cl-third insn) nil))
2019-07-14 12:39:29 +00:00
(byte-goto-if-not-nil-else-pop
2019-08-03 15:08:55 +00:00
(comp-emit-cond-jump (comp-slot-next) (make-comp-mvar :constant nil) 1
(cl-third insn) t))
(byte-return
(comp-emit `(return ,(comp-slot-next)))
2019-07-14 16:36:57 +00:00
(comp-mark-block-closed))
2019-07-14 21:35:04 +00:00
(byte-discard 'pass)
(byte-dup
2019-07-20 17:26:30 +00:00
(comp-copy-slot (1- (comp-sp))))
(byte-save-excursion)
(byte-save-window-excursion-OBSOLETE)
(byte-save-restriction)
(byte-catch)
(byte-unwind-protect
(comp-emit `(call helper_unwind_protect ,(comp-slot-next))))
(byte-condition-case)
(byte-temp-output-buffer-setup-OBSOLETE)
(byte-temp-output-buffer-show-OBSOLETE)
(byte-unbind-all)
2019-07-20 14:34:37 +00:00
(byte-set-marker auto)
(byte-match-beginning auto)
(byte-match-end auto)
(byte-upcase auto)
(byte-downcase auto)
2019-07-20 15:35:57 +00:00
(byte-string= string-equal Fstring_equal)
(byte-string< string-lessp Fstring_lessp)
2019-07-20 14:34:37 +00:00
(byte-equal auto)
2019-07-14 22:58:03 +00:00
(byte-nthcdr auto)
(byte-elt auto)
(byte-member auto)
(byte-assq auto)
(byte-nreverse auto)
(byte-setcar auto)
(byte-setcdr auto)
2019-07-20 15:35:57 +00:00
(byte-car-safe auto)
(byte-cdr-safe auto)
2019-07-20 14:34:37 +00:00
(byte-nconc auto)
2019-07-20 15:35:57 +00:00
(byte-quo / Fquo)
(byte-rem % Frem)
2019-07-14 22:58:03 +00:00
(byte-numberp auto)
(byte-integerp auto)
2019-07-20 16:50:41 +00:00
(byte-listN
2019-07-20 17:26:30 +00:00
(comp-stack-adjust (- 1 arg))
2019-07-20 16:50:41 +00:00
(comp-emit-set-call `(callref Flist ,arg ,(comp-sp))))
2019-07-14 08:57:46 +00:00
(byte-concatN
2019-07-20 17:26:30 +00:00
(comp-stack-adjust (- 1 arg))
2019-07-14 08:57:46 +00:00
(comp-emit-set-call `(callref Fconcat ,arg ,(comp-sp))))
2019-07-20 16:50:41 +00:00
(byte-insertN
2019-07-20 17:26:30 +00:00
(comp-stack-adjust (- 1 arg))
2019-07-20 16:50:41 +00:00
(comp-emit-set-call `(callref Finsert ,arg ,(comp-sp))))
2019-07-20 17:26:30 +00:00
(byte-stack-set
(comp-with-sp (1+ (comp-sp))
(comp-copy-slot (comp-sp) (- (comp-sp) arg))))
(byte-stack-set2)
2019-07-20 17:26:30 +00:00
(byte-discardN
(comp-stack-adjust (- arg)))
2019-08-03 15:08:55 +00:00
(byte-switch
(comp-emit-switch (comp-slot-next) (comp-slot-n (+ 2 (comp-sp)))))
(byte-constant
2019-07-20 16:50:41 +00:00
(comp-emit-set-const arg))
(byte-discardN-preserve-tos
(comp-stack-adjust (- arg))
2019-07-20 17:26:30 +00:00
(comp-copy-slot (+ arg (comp-sp)))))))
2019-07-07 10:30:03 +00:00
2019-07-08 05:56:37 +00:00
(defun comp-limplify (func)
2019-07-14 18:54:53 +00:00
"Given FUNC compute its LIMPLE ir."
2019-07-08 13:29:32 +00:00
(let* ((frame-size (comp-func-frame-size func))
2019-07-08 07:29:13 +00:00
(comp-func func)
2019-07-14 18:54:53 +00:00
(comp-pass (make-comp-limplify
:sp -1
:frame (comp-new-frame frame-size)))
2019-08-04 18:14:50 +00:00
(args-min (comp-args-min (comp-func-args func)))
2019-07-21 13:20:39 +00:00
(comp-block ()))
2019-07-07 16:42:55 +00:00
;; Prologue
2019-07-13 09:33:15 +00:00
(comp-emit-block 'entry)
2019-07-10 19:29:32 +00:00
(comp-emit-annotation (concat "Lisp function: "
2019-07-10 16:55:19 +00:00
(symbol-name (comp-func-symbol-name func))))
2019-08-04 18:14:50 +00:00
(if (not (comp-args-ncall-conv (comp-func-args func)))
(cl-loop for i below (comp-args-max (comp-func-args func))
do (cl-incf (comp-sp))
do (comp-emit `(setpar ,(comp-slot) ,i)))
(comp-emit `(ncall-prolog ,args-min))
(cl-incf (comp-sp) (1+ args-min)))
2019-07-08 15:04:33 +00:00
;; Body
2019-07-21 20:02:17 +00:00
(comp-emit-block 'bb_1)
2019-07-21 13:20:39 +00:00
(mapc #'comp-limplify-lap-inst (comp-func-lap func))
;; Reverse insns into all basic blocks.
(cl-loop for bb being the hash-value in (comp-func-blocks func)
do (setf (comp-block-insns bb)
(reverse (comp-block-insns bb))))
2019-07-07 10:30:03 +00:00
(when comp-debug
2019-07-21 13:20:39 +00:00
(comp-pretty-print-func func))
2019-07-08 05:56:37 +00:00
func))
2019-07-07 07:23:10 +00:00
2019-07-21 11:57:51 +00:00
;;; Entry points.
2019-07-21 13:20:39 +00:00
(defun native-compile (func-symbol-name)
"FUNC-SYMBOL-NAME is the function name to be compiled into native code."
(if-let ((f (symbol-function func-symbol-name)))
2019-07-07 10:30:03 +00:00
(progn
(when (byte-code-function-p f)
(error "Can't native compile an already bytecompiled function"))
2019-07-21 13:20:39 +00:00
(let ((func (make-comp-func :symbol-name func-symbol-name
2019-07-08 10:11:34 +00:00
:func f
2019-07-21 13:20:39 +00:00
:c-func-name (comp-c-func-name
func-symbol-name))))
2019-07-08 09:37:17 +00:00
(mapc (lambda (pass)
(funcall pass func))
comp-passes)
;; Once we have the final LIMPLE we jump into C.
2019-07-08 13:29:32 +00:00
(comp-init-ctxt)
(comp-add-func-to-ctxt func)
(comp-compile-and-load-ctxt)
(comp-release-ctxt)))
2019-07-08 10:11:34 +00:00
(error "Trying to native compile something not a function")))
2019-07-07 07:23:10 +00:00
(provide 'comp)
;;; comp.el ends here