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-13 15:24:44 +00:00
|
|
|
(defvar comp-speed 2)
|
|
|
|
|
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-14 07:53:06 +00:00
|
|
|
(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
|
|
|
|
:documentation "Maximum number of arguments allowed
|
2019-07-14 18:54:53 +00:00
|
|
|
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."
|
|
|
|
(sp nil
|
2019-07-14 16:36:57 +00:00
|
|
|
:documentation "When non nil indicates its 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-14 18:54:53 +00:00
|
|
|
:documentation "If the block was already closed."))
|
2019-07-14 15:21:34 +00:00
|
|
|
|
2019-07-07 10:30:03 +00:00
|
|
|
(cl-defstruct (comp-func (:copier nil))
|
|
|
|
"Internal rapresentation for a function."
|
|
|
|
(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-07 10:30:03 +00:00
|
|
|
(ir nil
|
2019-07-14 18:54:53 +00:00
|
|
|
: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-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-08 05:56:37 +00:00
|
|
|
(limple-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-14 18:54:53 +00:00
|
|
|
(defun comp-new-frame (size)
|
2019-07-09 16:09:47 +00:00
|
|
|
"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
|
|
|
|
2019-07-08 07:06:58 +00:00
|
|
|
;; Special vars used during limplifications
|
2019-07-14 18:54:53 +00:00
|
|
|
(defvar comp-pass)
|
2019-07-08 07:06:58 +00:00
|
|
|
(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."
|
2019-07-14 18:54:53 +00:00
|
|
|
'(comp-limplify-sp comp-pass))
|
2019-07-07 10:30:03 +00:00
|
|
|
|
2019-07-14 07:53:06 +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."
|
2019-07-14 07:53:06 +00:00
|
|
|
(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-14 07:53:06 +00:00
|
|
|
|
2019-07-07 16:42:55 +00:00
|
|
|
(defmacro comp-slot-n (n)
|
|
|
|
"Slot N into the meta-stack."
|
2019-07-14 07:53:06 +00:00
|
|
|
(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-13 14:34:59 +00:00
|
|
|
(defun comp-emit (x)
|
|
|
|
"Emit X into current LIMPLE ir.."
|
|
|
|
(push x comp-limple))
|
|
|
|
|
|
|
|
(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)
|
2019-07-08 07:06:58 +00:00
|
|
|
(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))))
|
2019-07-13 14:34:59 +00:00
|
|
|
(comp-emit (list 'set (comp-slot) call)))
|
2019-07-13 09:33:15 +00:00
|
|
|
|
2019-07-13 16:28:00 +00:00
|
|
|
(defun comp-copy-slot-n (n)
|
|
|
|
"Set current slot with slot number N as source."
|
2019-07-08 07:06:58 +00:00
|
|
|
(let ((src-slot (comp-slot-n n)))
|
2019-07-08 09:18:17 +00:00
|
|
|
(cl-assert src-slot)
|
2019-07-14 12:39:29 +00:00
|
|
|
;; FIXME should the id increase?
|
2019-07-08 07:06:58 +00:00
|
|
|
(setf (comp-slot)
|
|
|
|
(copy-sequence src-slot))
|
|
|
|
(setf (comp-mvar-slot (comp-slot)) (comp-sp))
|
2019-07-13 14:34:59 +00:00
|
|
|
(comp-emit (list 'set (comp-slot) src-slot))))
|
2019-07-08 07:06:58 +00:00
|
|
|
|
2019-07-10 16:55:19 +00:00
|
|
|
(defun comp-emit-annotation (str)
|
|
|
|
"Emit annotation STR."
|
2019-07-13 14:34:59 +00:00
|
|
|
(comp-emit `(comment ,str)))
|
2019-07-10 16:55:19 +00:00
|
|
|
|
2019-07-13 16:28:00 +00:00
|
|
|
(defun comp-set-const (val)
|
|
|
|
"Set constant VAL to current slot."
|
2019-07-09 20:28:29 +00:00
|
|
|
(setf (comp-slot) (make-comp-mvar :slot (comp-sp)
|
2019-07-08 07:06:58 +00:00
|
|
|
:const-vld t
|
2019-07-09 20:28:29 +00:00
|
|
|
:constant val))
|
2019-07-13 14:34:59 +00:00
|
|
|
(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-14 18:54:53 +00:00
|
|
|
(not (comp-block-closed (gethash (comp-limplify-block-name comp-pass)
|
2019-07-14 16:36:57 +00:00
|
|
|
blocks))))
|
|
|
|
(comp-emit-jump block-name))
|
|
|
|
;; 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)))
|
|
|
|
(comp-emit `(block ,block-name))
|
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
|
|
|
|
|
|
|
(defun comp-emit-cond-jump (discard-n lap-label negated)
|
|
|
|
"Emit a conditional jump to LAP-LABEL.
|
|
|
|
Discard DISCARD-N slots afterward.
|
|
|
|
If NEGATED non nil negate the test condition."
|
|
|
|
(let ((bb (comp-new-block-sym))
|
|
|
|
(blocks (comp-func-blocks comp-func)))
|
|
|
|
(puthash bb
|
|
|
|
(make-comp-block :sp (- (comp-sp) discard-n))
|
|
|
|
blocks)
|
|
|
|
(progn
|
|
|
|
(let ((target (comp-lap-to-limple-bb lap-label)))
|
|
|
|
(comp-emit (if negated
|
|
|
|
(list 'cond-jump (comp-slot-next) target bb)
|
|
|
|
(list 'cond-jump (comp-slot-next) bb target)))
|
|
|
|
(puthash target
|
|
|
|
(make-comp-block :sp (comp-sp))
|
|
|
|
blocks)
|
|
|
|
(comp-mark-block-closed)))
|
|
|
|
(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-07-14 07:53:06 +00:00
|
|
|
(comp-with-sp (1- n)
|
2019-07-13 14:34:59 +00:00
|
|
|
(comp-emit-set-call `(call Fcons
|
2019-07-14 07:53:06 +00:00
|
|
|
,(comp-slot)
|
|
|
|
,(make-comp-mvar :const-vld t
|
|
|
|
: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-14 07:53:06 +00:00
|
|
|
(defmacro comp-op-case (&rest cases)
|
2019-07-14 12:39:29 +00:00
|
|
|
"Expand CASES into the corresponding pcase."
|
2019-07-14 07:53:06 +00:00
|
|
|
(declare (debug (body))
|
|
|
|
(indent defun))
|
|
|
|
`(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-14 12:39:29 +00:00
|
|
|
,(unless (eq op 'TAG)
|
|
|
|
`(comp-emit-annotation
|
|
|
|
,(concat "LAP op " op-name)))
|
|
|
|
,(when sp-delta
|
|
|
|
`(comp-stack-adjust ,sp-delta))
|
2019-07-14 07:53:06 +00:00
|
|
|
(progn ,@body))
|
|
|
|
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-08 07:06:58 +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-14 08:57:46 +00:00
|
|
|
(let ((op (car inst))
|
|
|
|
(arg (if (consp (cdr inst))
|
|
|
|
(cadr inst)
|
|
|
|
(cdr inst))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(comp-op-case
|
2019-07-14 12:39:29 +00:00
|
|
|
(TAG
|
|
|
|
(comp-emit-block (comp-lap-to-limple-bb arg)))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-stack-ref
|
|
|
|
(comp-copy-slot-n (- (comp-sp) (cdr inst) 1)))
|
|
|
|
(byte-varref
|
2019-07-13 16:28:00 +00:00
|
|
|
(comp-emit-set-call `(call Fsymbol_value ,(make-comp-mvar
|
|
|
|
:const-vld t
|
2019-07-14 08:57:46 +00:00
|
|
|
:constant arg))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-varset
|
2019-07-13 14:34:59 +00:00
|
|
|
(comp-emit `(call set_internal
|
2019-07-13 15:24:44 +00:00
|
|
|
,(make-comp-mvar :const-vld t
|
2019-07-14 08:57:46 +00:00
|
|
|
:constant arg)
|
2019-07-13 14:34:59 +00:00
|
|
|
,(comp-slot))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-varbind)
|
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 07:53:06 +00:00
|
|
|
(byte-unbind)
|
|
|
|
(byte-pophandler)
|
|
|
|
(byte-pushconditioncase)
|
|
|
|
(byte-pushcatch)
|
|
|
|
(byte-nth)
|
|
|
|
(byte-symbolp)
|
|
|
|
(byte-consp)
|
|
|
|
(byte-stringp)
|
|
|
|
(byte-listp)
|
|
|
|
(byte-eq)
|
|
|
|
(byte-memq)
|
|
|
|
(byte-not)
|
|
|
|
(byte-car
|
|
|
|
(comp-emit-set-call `(call Fcar ,(comp-slot))))
|
|
|
|
(byte-cdr
|
|
|
|
(comp-emit-set-call `(call Fcdr ,(comp-slot))))
|
|
|
|
(byte-cons
|
|
|
|
(comp-emit-set-call `(call Fcons ,(comp-slot) ,(comp-slot-next))))
|
|
|
|
(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-length
|
|
|
|
(comp-emit-set-call `(call Flength ,(comp-slot))))
|
|
|
|
(byte-aref
|
2019-07-13 15:08:15 +00:00
|
|
|
(comp-emit-set-call `(call Faref
|
|
|
|
,(comp-slot)
|
|
|
|
,(comp-slot-next))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-aset
|
2019-07-13 15:08:15 +00:00
|
|
|
(comp-emit-set-call `(call Faset
|
|
|
|
,(comp-slot)
|
|
|
|
,(comp-slot-next)
|
|
|
|
,(comp-slot-n (+ 2 (comp-sp))))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-symbol-value
|
|
|
|
(comp-emit-set-call `(call Fsymbol_value ,(comp-slot))))
|
|
|
|
(byte-symbol-function)
|
|
|
|
(byte-set)
|
|
|
|
(byte-fset)
|
|
|
|
(byte-get)
|
|
|
|
(byte-substring)
|
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-14 07:53:06 +00:00
|
|
|
(byte-sub1)
|
|
|
|
(byte-add1)
|
|
|
|
(byte-eqlsign)
|
|
|
|
(byte-gtr)
|
|
|
|
(byte-lss)
|
|
|
|
(byte-leq)
|
|
|
|
(byte-geq)
|
|
|
|
(byte-diff)
|
|
|
|
(byte-negate)
|
|
|
|
(byte-plus
|
|
|
|
(comp-emit-set-call `(callref Fplus 2 ,(comp-sp))))
|
|
|
|
(byte-max)
|
|
|
|
(byte-min)
|
|
|
|
(byte-mult)
|
|
|
|
(byte-point)
|
|
|
|
(byte-goto-char)
|
|
|
|
(byte-insert)
|
|
|
|
(byte-point-max)
|
|
|
|
(byte-point-min)
|
|
|
|
(byte-char-after)
|
|
|
|
(byte-following-char)
|
|
|
|
(byte-preceding-char)
|
|
|
|
(byte-current-column)
|
|
|
|
(byte-indent-to)
|
|
|
|
(byte-scan-buffer-OBSOLETE)
|
|
|
|
(byte-eolp)
|
|
|
|
(byte-eobp)
|
|
|
|
(byte-bolp)
|
|
|
|
(byte-bobp)
|
|
|
|
(byte-current-buffer)
|
|
|
|
(byte-set-buffer)
|
|
|
|
(byte-save-current-buffer)
|
|
|
|
(byte-set-mark-OBSOLETE)
|
|
|
|
(byte-interactive-p-OBSOLETE)
|
|
|
|
(byte-forward-char)
|
|
|
|
(byte-forward-word)
|
|
|
|
(byte-skip-chars-forward)
|
|
|
|
(byte-skip-chars-backward)
|
|
|
|
(byte-forward-line)
|
|
|
|
(byte-char-syntax)
|
|
|
|
(byte-buffer-substring)
|
|
|
|
(byte-delete-region)
|
|
|
|
(byte-narrow-to-region)
|
|
|
|
(byte-widen)
|
|
|
|
(byte-end-of-line)
|
|
|
|
(byte-constant2)
|
2019-07-14 12:39:29 +00:00
|
|
|
(byte-goto
|
2019-07-14 16:36:57 +00:00
|
|
|
(comp-with-fall-through-block bb 0
|
2019-07-14 12:39:29 +00:00
|
|
|
(let ((target (comp-lap-to-limple-bb (cl-third inst))))
|
2019-07-14 16:36:57 +00:00
|
|
|
(comp-emit-jump target)
|
|
|
|
(puthash target
|
|
|
|
(make-comp-block :sp (comp-sp))
|
|
|
|
(comp-func-blocks comp-func)))))
|
2019-07-14 12:39:29 +00:00
|
|
|
(byte-goto-if-nil
|
2019-07-14 16:36:57 +00:00
|
|
|
(comp-emit-cond-jump 0 (cl-third inst) nil))
|
2019-07-14 12:39:29 +00:00
|
|
|
(byte-goto-if-not-nil
|
2019-07-14 16:36:57 +00:00
|
|
|
(comp-emit-cond-jump 0 (cl-third inst) t))
|
2019-07-14 12:39:29 +00:00
|
|
|
(byte-goto-if-nil-else-pop
|
2019-07-14 16:36:57 +00:00
|
|
|
(comp-emit-cond-jump 1 (cl-third inst) nil))
|
2019-07-14 12:39:29 +00:00
|
|
|
(byte-goto-if-not-nil-else-pop
|
2019-07-14 16:36:57 +00:00
|
|
|
(comp-emit-cond-jump 1 (cl-third inst) t))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-return
|
2019-07-14 16:36:57 +00:00
|
|
|
(comp-emit (list 'return (comp-slot-next)))
|
|
|
|
(comp-mark-block-closed))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-discard t)
|
|
|
|
(byte-dup
|
|
|
|
(comp-copy-slot-n (1- (comp-sp))))
|
|
|
|
(byte-save-excursion)
|
|
|
|
(byte-save-window-excursion-OBSOLETE)
|
|
|
|
(byte-save-restriction)
|
|
|
|
(byte-catch)
|
|
|
|
(byte-unwind-protect)
|
|
|
|
(byte-condition-case)
|
|
|
|
(byte-temp-output-buffer-setup-OBSOLETE)
|
|
|
|
(byte-temp-output-buffer-show-OBSOLETE)
|
|
|
|
(byte-unbind-all)
|
|
|
|
(byte-set-marker)
|
|
|
|
(byte-match-beginning)
|
|
|
|
(byte-match-end)
|
|
|
|
(byte-upcase)
|
|
|
|
(byte-downcase)
|
|
|
|
(byte-string=)
|
|
|
|
(byte-string<)
|
|
|
|
(byte-equal)
|
|
|
|
(byte-nthcdr)
|
|
|
|
(byte-elt)
|
|
|
|
(byte-member)
|
|
|
|
(byte-assq)
|
|
|
|
(byte-nreverse)
|
|
|
|
(byte-setcar)
|
|
|
|
(byte-setcdr)
|
|
|
|
(byte-car-safe
|
2019-07-13 14:34:59 +00:00
|
|
|
(comp-emit-set-call `(call Fcar_safe ,(comp-slot))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-cdr-safe
|
2019-07-13 14:34:59 +00:00
|
|
|
(comp-emit-set-call `(call Fcdr_safe ,(comp-slot))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-nconc)
|
|
|
|
(byte-quo)
|
|
|
|
(byte-rem)
|
|
|
|
(byte-numberp)
|
|
|
|
(byte-integerp)
|
|
|
|
(byte-listN)
|
2019-07-14 08:57:46 +00:00
|
|
|
(byte-concatN
|
|
|
|
(comp-stack-adjust (- (1- arg)))
|
|
|
|
(comp-emit-set-call `(callref Fconcat ,arg ,(comp-sp))))
|
2019-07-14 07:53:06 +00:00
|
|
|
(byte-insertN)
|
|
|
|
(byte-stack-set)
|
|
|
|
(byte-stack-set2)
|
|
|
|
(byte-discardN)
|
|
|
|
(byte-switch)
|
|
|
|
(byte-constant
|
2019-07-14 08:57:46 +00:00
|
|
|
(comp-set-const arg)))))
|
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-07-08 07:06:58 +00:00
|
|
|
(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-14 16:36:57 +00:00
|
|
|
(comp-emit-jump 'body)
|
2019-07-08 15:04:33 +00:00
|
|
|
;; Body
|
2019-07-13 09:33:15 +00:00
|
|
|
(comp-emit-block 'body)
|
2019-07-08 07:06:58 +00:00
|
|
|
(mapc #'comp-limplify-lap-inst (comp-func-ir func))
|
|
|
|
(setf (comp-func-ir func) (reverse comp-limple))
|
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
|