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

345 lines
12 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/>.
;;; 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-07 10:30:03 +00:00
(defconst comp-passes '(comp-recuparate-lap
comp-limplify)
"Passes to be executed in order.")
2019-07-07 19:49:11 +00:00
(defconst comp-known-ret-types '((Fcons . cons)))
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)
)
2019-07-07 10:30:03 +00:00
(cl-defstruct comp-args
2019-07-08 13:29:32 +00:00
(min nil :type number
:documentation "Minimum number of arguments allowed")
(max nil
:documentation "Maximum number of arguments allowed
To be used when ncall-conv is nil.")
(ncall-conv nil :type boolean
:documentation "If t the signature is:
(ptrdiff_t nargs, Lisp_Object *args)"))
2019-07-07 10:30:03 +00:00
(cl-defstruct (comp-func (:copier nil))
"Internal rapresentation for a function."
(symbol-name nil
2019-07-08 10:11:34 +00:00
:documentation "Function symbol's name")
(c-func-name nil :type 'string
:documentation "The function name in the native world")
2019-07-07 10:30:03 +00:00
(func nil
2019-07-08 10:11:34 +00:00
:documentation "Original form")
2019-07-07 10:30:03 +00:00
(byte-func nil
2019-07-08 10:11:34 +00:00
:documentation "Byte compiled version")
2019-07-07 10:30:03 +00:00
(ir nil
:documentation "Current intermediate rappresentation")
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-08 15:04:33 +00:00
(blocks () :type list
:documentation "List of basic block")
2019-07-08 05:56:37 +00:00
(limple-cnt -1 :type 'number
: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-08 05:56:37 +00:00
:documentation "SSA number")
2019-07-07 10:30:03 +00:00
(slot nil :type fixnum
2019-07-07 19:49:11 +00:00
:documentation "Slot position")
2019-07-07 10:30:03 +00:00
(const-vld nil
2019-07-07 19:49:11 +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
propagation")
(type nil
:documentation "When non nil is used for type propagation"))
(cl-defstruct (comp-limple-frame (:copier nil))
"A LIMPLE func."
(sp 0 :type 'fixnum
:documentation "Current stack pointer")
(frame nil :type 'vector
:documentation "Meta-stack used to flat LAP"))
(defun comp-limple-frame-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-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-10 19:29:32 +00:00
;; Nassi's algorithm.
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
(rx (not (any "a-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-08 05:56:37 +00:00
(defun comp-recuparate-lap (func)
"Byte compile and recuparate LAP rapresentation for FUNC."
2019-07-07 10:30:03 +00:00
;; FIXME block timers here, otherwise we could spill the wrong LAP.
2019-07-08 05:56:37 +00:00
(setf (comp-func-byte-func func)
(byte-compile (comp-func-symbol-name func)))
2019-07-07 10:30:03 +00:00
(when comp-debug
(cl-prettyprint byte-compile-lap-output))
2019-07-09 20:28:29 +00:00
(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")))
2019-07-08 05:56:37 +00:00
(setf (comp-func-ir func) byte-compile-lap-output)
2019-07-08 13:29:32 +00:00
(setf (comp-func-frame-size func) (aref (comp-func-byte-func func) 3))
2019-07-08 05:56:37 +00:00
func)
2019-07-08 09:37:17 +00:00
(declare-function comp-init-ctxt "comp.c")
(declare-function comp-release-ctxt "comp.c")
(declare-function comp-add-func-to-ctxt "comp.c")
(declare-function comp-compile-and-load-ctxt "comp.c")
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
;; Special vars used during limplifications
(defvar comp-frame)
(defvar comp-limple)
(defvar comp-func)
2019-07-08 07:29:13 +00:00
(cl-defun make-comp-mvar (&key slot const-vld constant type)
2019-07-09 20:28:29 +00:00
(make--comp-mvar :id (cl-incf (comp-func-limple-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."
'(comp-limple-frame-sp comp-frame))
2019-07-07 10:30:03 +00:00
2019-07-07 16:42:55 +00:00
(defmacro comp-slot-n (n)
"Slot N into the meta-stack."
`(aref (comp-limple-frame-frame comp-frame) ,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-13 09:33:15 +00:00
(defun comp-emit-call (call)
"Emit CALL."
(cl-assert call)
(setf (comp-slot)
2019-07-08 07:29:13 +00:00
(make-comp-mvar :slot (comp-sp)
2019-07-13 09:33:15 +00:00
:type (alist-get (cadr call)
comp-known-ret-types)))
2019-07-13 09:33:15 +00:00
(push (list 'set (comp-slot) call) comp-limple))
(defun comp-push-call (call)
"Push call CALL into frame."
(cl-incf (comp-sp))
(comp-emit-call call))
(defun comp-push-slot-n (n)
2019-07-07 10:30:03 +00:00
"Push slot number N into frame."
(let ((src-slot (comp-slot-n n)))
2019-07-08 09:18:17 +00:00
(cl-assert src-slot)
(cl-incf (comp-sp))
(setf (comp-slot)
(copy-sequence src-slot))
(setf (comp-mvar-slot (comp-slot)) (comp-sp))
2019-07-11 20:39:42 +00:00
(push (list 'set (comp-slot) src-slot) comp-limple)))
2019-07-10 16:55:19 +00:00
(defun comp-emit-annotation (str)
"Emit annotation STR."
(push `(comment ,str) comp-limple))
(defun comp-push-const (val)
"Push VAL into frame.
VAL is known at compile time."
(cl-incf (comp-sp))
2019-07-09 20:28:29 +00:00
(setf (comp-slot) (make-comp-mvar :slot (comp-sp)
:const-vld t
2019-07-09 20:28:29 +00:00
:constant val))
2019-07-11 20:39:42 +00:00
(push (list 'setimm (comp-slot) val) comp-limple))
2019-07-08 15:04:33 +00:00
2019-07-13 09:33:15 +00:00
(defun comp-emit-block (bblock)
2019-07-08 15:04:33 +00:00
"Push basic block BBLOCK."
(push bblock (comp-func-blocks comp-func))
;; Every new block we are forced to wipe out all the frame.
;; This will be superseded by proper flow analysis.
(setf (comp-limple-frame-frame comp-frame)
(comp-limple-frame-new-frame (comp-func-frame-size comp-func)))
2019-07-08 15:04:33 +00:00
(push `(block ,bblock) comp-limple))
(defun comp-pop (n)
2019-07-07 10:30:03 +00:00
"Pop N elements from the meta-stack."
(cl-decf (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-07-13 09:33:15 +00:00
(comp-emit-call `(call Fcons ,(comp-slot)
2019-07-08 07:29:13 +00:00
,(make-comp-mvar :const-vld t
:constant nil)))
2019-07-08 07:15:09 +00:00
(dotimes (_ (1- n))
2019-07-13 09:33:15 +00:00
(comp-pop 1)
(comp-emit-call `(call Fcons
,(comp-slot)
,(comp-slot-n (1+ (comp-sp)))))))
2019-07-08 07:15:09 +00:00
(defun comp-limplify-lap-inst (inst)
2019-07-08 07:29:13 +00:00
"Limplify LAP instruction INST accumulating in `comp-limple'."
2019-07-08 07:15:09 +00:00
(let ((op (car inst)))
(pcase op
('byte-dup
(comp-push-slot-n (comp-sp)))
('byte-varref
2019-07-10 01:06:21 +00:00
(comp-push-call `(call Fsymbol_value ,(make-comp-mvar
:const-vld t
:constant (cadr inst)))))
2019-07-08 09:18:17 +00:00
;; ('byte-varset
2019-07-13 09:33:15 +00:00
;; (comp-emit-call `(call Fsymbol_value ,(cadr inst))))
2019-07-08 07:15:09 +00:00
('byte-constant
2019-07-08 09:37:17 +00:00
(comp-push-const (cadr inst)))
2019-07-08 07:15:09 +00:00
('byte-stack-ref
(comp-push-slot-n (- (comp-sp) (cdr inst))))
('byte-plus
(comp-pop 1)
2019-07-13 09:33:15 +00:00
(comp-emit-call `(callref Fplus 2 ,(comp-sp))))
('byte-cons
2019-07-08 07:15:09 +00:00
(comp-pop 1)
2019-07-13 09:33:15 +00:00
(comp-emit-call `(call Fcons ,(comp-slot) ,(comp-slot-next))))
('byte-car
(comp-emit-call `(call Fcar ,(comp-slot))))
('byte-cdr
(comp-emit-call `(call Fcdr ,(comp-slot))))
2019-07-08 07:29:13 +00:00
('byte-car-safe
2019-07-13 09:33:15 +00:00
(comp-emit-call `(call Fcar_safe ,(comp-slot))))
2019-07-08 07:29:13 +00:00
('byte-cdr-safe
2019-07-13 09:33:15 +00:00
(comp-emit-call `(call Fcdr_safe ,(comp-slot))))
2019-07-08 07:15:09 +00:00
('byte-list1
(comp-limplify-listn 1))
('byte-list2
(comp-limplify-listn 2))
('byte-list3
(comp-limplify-listn 3))
('byte-list4
(comp-limplify-listn 4))
('byte-return
2019-07-08 13:29:32 +00:00
(push (list 'return (comp-slot)) comp-limple)
2019-07-08 07:15:09 +00:00
`(return ,(comp-slot)))
(_ (error "Unexpected LAP op %s" (symbol-name op))))))
2019-07-07 10:30:03 +00:00
2019-07-08 05:56:37 +00:00
(defun comp-limplify (func)
2019-07-08 15:04:33 +00:00
"Given FUNC and return 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)
(comp-frame (make-comp-limple-frame
:sp -1
:frame (comp-limple-frame-new-frame frame-size)))
(comp-limple ()))
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-07-08 05:56:37 +00:00
(cl-loop for i below (comp-args-mandatory (comp-func-args func))
2019-07-07 16:42:55 +00:00
do (progn
(cl-incf (comp-sp))
2019-07-11 20:39:42 +00:00
(push `(setpar ,(comp-slot) ,i) comp-limple)))
2019-07-08 15:04:33 +00:00
(push '(jump body) comp-limple)
;; Body
2019-07-13 09:33:15 +00:00
(comp-emit-block 'body)
(mapc #'comp-limplify-lap-inst (comp-func-ir func))
(setf (comp-func-ir func) (reverse comp-limple))
2019-07-08 15:44:19 +00:00
;; Prologue block must be first
(setf (comp-func-blocks func) (reverse (comp-func-blocks func)))
2019-07-07 10:30:03 +00:00
(when comp-debug
2019-07-08 05:56:37 +00:00
(cl-prettyprint (comp-func-ir func)))
func))
2019-07-07 07:23:10 +00:00
(defun native-compile (fun)
2019-07-08 09:37:17 +00:00
"FUN is the function definition to be compiled into native code."
2019-07-07 07:23:10 +00:00
(if-let ((f (symbol-function fun)))
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-08 09:37:17 +00:00
(let ((func (make-comp-func :symbol-name fun
2019-07-08 10:11:34 +00:00
:func f
:c-func-name (comp-c-func-name fun))))
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