2019-07-07 07:23:10 +00:00
|
|
|
|
;;; comp.el --- compilation of Lisp code into native code -*- lexical-binding: t -*-
|
|
|
|
|
|
2019-09-01 09:58:20 +00:00
|
|
|
|
;; Author: Andrea Corallo <akrl@sdf.com>
|
|
|
|
|
|
2019-07-07 07:23:10 +00:00
|
|
|
|
;; 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.
|
2019-09-23 10:45:06 +00:00
|
|
|
|
;; Or, to put it another way to make a 911 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-10-05 15:08:48 +00:00
|
|
|
|
(require 'gv)
|
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-11-10 13:43:47 +00:00
|
|
|
|
(defcustom comp-speed 2
|
|
|
|
|
"Compiler optimization level. From 0 to 3."
|
|
|
|
|
:type 'number
|
|
|
|
|
:group 'comp)
|
2019-11-10 13:51:38 +00:00
|
|
|
|
|
|
|
|
|
(defcustom comp-debug 0
|
|
|
|
|
"Compiler debug level. From 0 to 3.
|
|
|
|
|
- 0 no debug facility
|
|
|
|
|
- 1 emit debug symbols and dump pseudo C code
|
|
|
|
|
- 2 dump gcc passes and libgccjit log file
|
|
|
|
|
- 3 dump libgccjit reproducer"
|
|
|
|
|
:type 'number
|
|
|
|
|
:group 'comp)
|
|
|
|
|
|
2019-09-29 16:41:31 +00:00
|
|
|
|
(defcustom comp-verbose 0
|
2019-11-09 16:12:56 +00:00
|
|
|
|
"Compiler verbosity. From 0 to 3.
|
2019-09-29 16:41:31 +00:00
|
|
|
|
- 0 no logging
|
|
|
|
|
- 1 final limple is logged
|
2019-11-09 15:56:55 +00:00
|
|
|
|
- 2 LAP and final limple and some pass info are logged
|
|
|
|
|
- 3 max verbosity"
|
2019-09-29 15:54:10 +00:00
|
|
|
|
:type 'number
|
2019-08-11 09:59:31 +00:00
|
|
|
|
:group 'comp)
|
|
|
|
|
|
|
|
|
|
(defconst native-compile-log-buffer "*Native-compile-Log*"
|
2019-11-09 16:12:56 +00:00
|
|
|
|
"Name of the native-compiler log buffer.")
|
2019-07-07 07:23:10 +00:00
|
|
|
|
|
2019-09-23 09:48:19 +00:00
|
|
|
|
(defvar comp-native-compiling nil
|
|
|
|
|
"This gets bound to t while native compilation.
|
|
|
|
|
Can be used by code that wants to expand differently in this case.")
|
|
|
|
|
|
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-09-07 06:57:07 +00:00
|
|
|
|
comp-limplify
|
2019-09-11 21:13:13 +00:00
|
|
|
|
comp-ssa
|
2019-09-15 12:43:30 +00:00
|
|
|
|
comp-propagate
|
2019-09-18 09:30:23 +00:00
|
|
|
|
comp-call-optim
|
2019-09-22 15:11:53 +00:00
|
|
|
|
comp-propagate
|
2019-09-22 16:49:11 +00:00
|
|
|
|
comp-dead-code
|
2019-09-07 06:57:07 +00:00
|
|
|
|
comp-final)
|
2019-07-07 10:30:03 +00:00
|
|
|
|
"Passes to be executed in order.")
|
|
|
|
|
|
2019-09-15 12:43:30 +00:00
|
|
|
|
;; TODO hash here.
|
|
|
|
|
(defconst comp-known-ret-types '((cons . cons)
|
|
|
|
|
(1+ . number)
|
|
|
|
|
(1- . number)
|
|
|
|
|
(+ . number)
|
|
|
|
|
(- . number)
|
|
|
|
|
(* . number)
|
|
|
|
|
(/ . number)
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(% . number)
|
|
|
|
|
;; Type hint
|
|
|
|
|
(comp-hint-fixnum . fixnum)
|
|
|
|
|
(comp-hint-cons . cons))
|
2019-07-21 13:20:39 +00:00
|
|
|
|
"Alist used for type propagation.")
|
2019-07-07 19:49:11 +00:00
|
|
|
|
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(defconst comp-type-hints '(comp-hint-fixnum
|
|
|
|
|
comp-hint-cons)
|
|
|
|
|
"List of fake functions used to give compiler hints.")
|
|
|
|
|
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(defconst comp-limple-sets '(set
|
|
|
|
|
setimm
|
|
|
|
|
set-par-to-local
|
|
|
|
|
set-args-to-local
|
|
|
|
|
set-rest-args-to-local)
|
|
|
|
|
"Limple set operators.")
|
|
|
|
|
|
2019-10-20 19:00:17 +00:00
|
|
|
|
(defconst comp-limple-assignments `(fetch-handler
|
2019-09-22 16:49:11 +00:00
|
|
|
|
,@comp-limple-sets)
|
2019-09-17 11:18:40 +00:00
|
|
|
|
"Limple operators that clobbers the first mvar argument.")
|
2019-09-14 10:39:53 +00:00
|
|
|
|
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(defconst comp-limple-calls '(call
|
|
|
|
|
callref
|
|
|
|
|
direct-call
|
|
|
|
|
direct-callref)
|
|
|
|
|
"Limple operators use to call subrs.")
|
|
|
|
|
|
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-08-18 13:36:36 +00:00
|
|
|
|
(cl-defstruct comp-ctxt
|
2019-09-07 06:57:07 +00:00
|
|
|
|
"Lisp side of the compiler context."
|
2019-10-13 15:41:26 +00:00
|
|
|
|
(output nil :type string
|
2019-09-07 06:57:07 +00:00
|
|
|
|
:documentation "Target output filename for the compilation.")
|
2019-11-02 16:32:42 +00:00
|
|
|
|
(top-level-forms () :type list
|
|
|
|
|
:documentation "List of spilled top level forms.")
|
2019-08-18 15:17:56 +00:00
|
|
|
|
(funcs-h (make-hash-table) :type hash-table
|
2019-08-18 21:09:20 +00:00
|
|
|
|
:documentation "lisp-func-name -> comp-func.
|
2019-08-18 15:17:56 +00:00
|
|
|
|
This is to build the prev field.")
|
2019-08-18 13:36:36 +00:00
|
|
|
|
(data-relocs-l () :type list
|
|
|
|
|
:documentation "Constant objects used by functions.")
|
|
|
|
|
(data-relocs-idx (make-hash-table :test #'equal) :type hash-table
|
|
|
|
|
:documentation "Obj -> position into data-relocs.")
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(func-relocs-l () :type list
|
2019-08-18 13:36:36 +00:00
|
|
|
|
:documentation "Native functions imported.")
|
|
|
|
|
(func-relocs-idx (make-hash-table :test #'equal) :type hash-table
|
|
|
|
|
:documentation "Obj -> position into func-relocs."))
|
|
|
|
|
|
2019-08-16 13:48:38 +00:00
|
|
|
|
(cl-defstruct comp-args-base
|
2019-07-08 13:29:32 +00:00
|
|
|
|
(min nil :type number
|
2019-08-11 12:54:13 +00:00
|
|
|
|
:documentation "Minimum number of arguments allowed."))
|
|
|
|
|
|
2019-08-16 13:48:38 +00:00
|
|
|
|
(cl-defstruct (comp-args (:include comp-args-base))
|
2019-08-11 12:54:13 +00:00
|
|
|
|
(max nil :type number
|
2019-07-21 13:20:39 +00:00
|
|
|
|
:documentation "Maximum number of arguments allowed.
|
2019-08-11 12:54:13 +00:00
|
|
|
|
To be used when ncall-conv is nil."))
|
|
|
|
|
|
2019-08-16 13:48:38 +00:00
|
|
|
|
(cl-defstruct (comp-nargs (:include comp-args-base))
|
2019-08-11 12:54:13 +00:00
|
|
|
|
"Describe args when the functin signature is of kind:
|
|
|
|
|
(ptrdiff_t nargs, Lisp_Object *args)."
|
|
|
|
|
(nonrest nil :type number
|
2019-11-02 11:17:26 +00:00
|
|
|
|
:documentation "Number of non rest arguments.")
|
|
|
|
|
(rest nil :type boolean
|
|
|
|
|
:documentation "t if rest argument is present."))
|
2019-07-07 10:30:03 +00:00
|
|
|
|
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(cl-defstruct (comp-block (:copier nil)
|
|
|
|
|
(:constructor make--comp-block
|
|
|
|
|
(addr sp name))) ; Positional
|
2019-07-14 15:21:34 +00:00
|
|
|
|
"A basic block."
|
2019-09-11 19:51:37 +00:00
|
|
|
|
(name nil :type symbol)
|
|
|
|
|
;; These two slots are used during limplification.
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(sp nil :type number
|
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-10-13 08:36:22 +00:00
|
|
|
|
(addr nil :type number
|
|
|
|
|
:documentation "Start block LAP address.")
|
2019-07-21 13:20:39 +00:00
|
|
|
|
(insns () :type list
|
2019-09-13 18:56:24 +00:00
|
|
|
|
:documentation "List of instructions.")
|
2019-10-20 14:04:29 +00:00
|
|
|
|
(closed nil :type boolean
|
|
|
|
|
:documentation "t if closed.")
|
2019-09-14 08:13:38 +00:00
|
|
|
|
;; All the followings are for SSA and CGF analysis.
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(in-edges () :type list
|
|
|
|
|
:documentation "List of incoming edges.")
|
|
|
|
|
(out-edges () :type list
|
|
|
|
|
:documentation "List of outcoming edges.")
|
|
|
|
|
(dom nil :type comp-block
|
|
|
|
|
:documentation "Immediate dominator.")
|
2019-09-14 08:13:38 +00:00
|
|
|
|
(df (make-hash-table) :type hash-table
|
2019-09-14 10:39:53 +00:00
|
|
|
|
:documentation "Dominance frontier set. Block-name -> block")
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(post-num nil :type number
|
2019-09-16 23:01:34 +00:00
|
|
|
|
:documentation "Post order number.")
|
|
|
|
|
(final-frame nil :type vector
|
|
|
|
|
:documentation "This is a copy of the frame when leaving the block.
|
|
|
|
|
Is in use to help the SSA rename pass."))
|
2019-07-14 15:21:34 +00:00
|
|
|
|
|
2019-09-13 15:55:16 +00:00
|
|
|
|
(cl-defstruct (comp-edge (:copier nil) (:constructor make--comp-edge))
|
|
|
|
|
"An edge connecting two basic blocks."
|
|
|
|
|
(src nil :type comp-block)
|
|
|
|
|
(dst nil :type comp-block)
|
|
|
|
|
(number nil :type number
|
|
|
|
|
:documentation "The index number corresponding to this edge in the
|
|
|
|
|
edge vector."))
|
|
|
|
|
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(defun comp-block-preds (basic-block)
|
|
|
|
|
"Given BASIC-BLOCK return the list of its predecessors."
|
|
|
|
|
(mapcar #'comp-edge-src (comp-block-in-edges basic-block)))
|
|
|
|
|
|
2019-09-13 15:55:16 +00:00
|
|
|
|
(defun comp-gen-counter ()
|
|
|
|
|
"Return a sequential number generator."
|
|
|
|
|
(let ((n -1))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(cl-incf n))))
|
|
|
|
|
|
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-08-18 08:34:18 +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
|
|
|
|
(byte-func nil
|
2019-07-14 18:54:53 +00:00
|
|
|
|
:documentation "Byte compiled version.")
|
2019-11-02 16:33:55 +00:00
|
|
|
|
(doc nil :type string
|
|
|
|
|
:documentation "Doc string.")
|
2019-07-21 13:20:39 +00:00
|
|
|
|
(lap () :type list
|
2019-09-22 07:58:20 +00:00
|
|
|
|
:documentation "LAP assembly representation.")
|
2019-08-18 08:34:18 +00:00
|
|
|
|
(args nil :type comp-args-base)
|
|
|
|
|
(frame-size nil :type number)
|
|
|
|
|
(blocks (make-hash-table) :type hash-table
|
2019-07-14 15:21:34 +00:00
|
|
|
|
:documentation "Key is the basic block symbol value is a comp-block
|
2019-07-14 18:54:53 +00:00
|
|
|
|
structure.")
|
2019-08-18 08:34:18 +00:00
|
|
|
|
(lap-block (make-hash-table :test #'equal) :type hash-table
|
2019-09-22 07:58:20 +00:00
|
|
|
|
:documentation "LAP lable -> LIMPLE basic block name.")
|
2019-09-11 21:13:13 +00:00
|
|
|
|
(edges () :type list
|
|
|
|
|
:documentation "List of edges connecting basic blocks.")
|
2019-09-22 07:58:20 +00:00
|
|
|
|
(block-cnt-gen (funcall #'comp-gen-counter) :type function
|
|
|
|
|
:documentation "Generates block numbers.")
|
2019-09-14 10:39:53 +00:00
|
|
|
|
(edge-cnt-gen (funcall #'comp-gen-counter) :type function
|
2019-09-22 07:58:20 +00:00
|
|
|
|
:documentation "Generates edges numbers.")
|
2019-09-14 10:39:53 +00:00
|
|
|
|
(ssa-cnt-gen (funcall #'comp-gen-counter) :type function
|
2019-11-10 09:17:24 +00:00
|
|
|
|
:documentation "Counter to create ssa limple vars."))
|
2019-07-07 10:30:03 +00:00
|
|
|
|
|
2019-09-14 10:39:53 +00:00
|
|
|
|
(defun comp-func-reset-generators (func)
|
|
|
|
|
"Reset unique id generators for FUNC."
|
|
|
|
|
(setf (comp-func-edge-cnt-gen func) (comp-gen-counter))
|
|
|
|
|
(setf (comp-func-ssa-cnt-gen func) (comp-gen-counter)))
|
|
|
|
|
|
2019-11-09 15:22:07 +00:00
|
|
|
|
(cl-defstruct (comp-mvar (:constructor make--comp-mvar))
|
2019-07-07 19:49:11 +00:00
|
|
|
|
"A meta-variable being a slot in the meta-stack."
|
2019-07-07 10:30:03 +00:00
|
|
|
|
(slot nil :type fixnum
|
2019-09-14 16:12:16 +00:00
|
|
|
|
:documentation "Slot number.")
|
|
|
|
|
(id nil :type (or null number)
|
2019-09-14 15:00:16 +00:00
|
|
|
|
:documentation "SSA number.")
|
2019-09-15 12:43:30 +00:00
|
|
|
|
(const-vld nil :type boolean
|
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-09-15 12:43:30 +00:00
|
|
|
|
:documentation "When non nil is used for type propagation.")
|
|
|
|
|
(ref nil :type boolean
|
|
|
|
|
:documentation "When t this is used by reference."))
|
2019-07-07 10:30:03 +00:00
|
|
|
|
|
2019-09-14 13:25:11 +00:00
|
|
|
|
;; Special vars used by some passes
|
|
|
|
|
(defvar comp-func)
|
|
|
|
|
|
2019-08-18 13:36:36 +00:00
|
|
|
|
|
2019-09-15 12:43:30 +00:00
|
|
|
|
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(defun comp-set-op-p (op)
|
|
|
|
|
"Assignment predicate for OP."
|
|
|
|
|
(cl-find op comp-limple-sets))
|
|
|
|
|
|
2019-09-14 15:00:16 +00:00
|
|
|
|
(defun comp-assign-op-p (op)
|
|
|
|
|
"Assignment predicate for OP."
|
|
|
|
|
(cl-find op comp-limple-assignments))
|
|
|
|
|
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(defun comp-limple-insn-call-p (insn)
|
|
|
|
|
"Limple INSN call predicate."
|
|
|
|
|
(when (member (car-safe insn) comp-limple-calls)
|
|
|
|
|
t))
|
|
|
|
|
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(defun comp-type-hint-p (func)
|
|
|
|
|
"Type hint predicate for function name FUNC."
|
|
|
|
|
(member func comp-type-hints))
|
|
|
|
|
|
2019-08-18 13:36:36 +00:00
|
|
|
|
(defun comp-add-const-to-relocs (obj)
|
2019-08-19 15:59:20 +00:00
|
|
|
|
"Keep track of OBJ into the ctxt relocations.
|
|
|
|
|
The corresponding index is returned."
|
2019-08-18 13:36:36 +00:00
|
|
|
|
(let ((data-relocs-idx (comp-ctxt-data-relocs-idx comp-ctxt)))
|
2019-09-01 15:02:35 +00:00
|
|
|
|
(if-let ((idx (gethash obj data-relocs-idx)))
|
|
|
|
|
idx
|
2019-08-18 13:36:36 +00:00
|
|
|
|
(push obj (comp-ctxt-data-relocs-l comp-ctxt))
|
|
|
|
|
(puthash obj (hash-table-count data-relocs-idx) data-relocs-idx))))
|
|
|
|
|
|
2019-08-22 14:00:43 +00:00
|
|
|
|
(defun comp-add-subr-to-relocs (subr-name)
|
|
|
|
|
"Keep track of SUBR-NAME into the ctxt relocations.
|
|
|
|
|
The corresponding index is returned."
|
|
|
|
|
(let ((func-relocs-idx (comp-ctxt-func-relocs-idx comp-ctxt)))
|
2019-09-01 15:02:35 +00:00
|
|
|
|
(if-let ((idx (gethash subr-name func-relocs-idx)))
|
|
|
|
|
idx
|
2019-08-22 14:00:43 +00:00
|
|
|
|
(push subr-name (comp-ctxt-func-relocs-l comp-ctxt))
|
|
|
|
|
(puthash subr-name (hash-table-count func-relocs-idx) func-relocs-idx))))
|
|
|
|
|
|
2019-08-11 09:59:31 +00:00
|
|
|
|
(defmacro comp-within-log-buff (&rest body)
|
|
|
|
|
"Execute BODY while at the end the log-buffer.
|
2019-09-29 15:54:10 +00:00
|
|
|
|
BODY is evaluate only if `comp-verbose' is > 0."
|
2019-08-11 09:59:31 +00:00
|
|
|
|
(declare (debug (form body))
|
|
|
|
|
(indent defun))
|
2019-09-29 15:54:10 +00:00
|
|
|
|
`(when (> comp-verbose 0)
|
2019-08-11 09:59:31 +00:00
|
|
|
|
(with-current-buffer (get-buffer-create native-compile-log-buffer)
|
|
|
|
|
(setq buffer-read-only t)
|
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
,@body))))
|
|
|
|
|
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(defun comp-log (data verbosity)
|
2019-11-09 16:12:56 +00:00
|
|
|
|
"Log DATA given VERBOSITY."
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(when (>= comp-verbose verbosity)
|
|
|
|
|
(if noninteractive
|
|
|
|
|
(if (atom data)
|
|
|
|
|
(message "%s" data)
|
|
|
|
|
(mapc (lambda (x)
|
|
|
|
|
(message "%s"(prin1-to-string x)))
|
|
|
|
|
data))
|
|
|
|
|
(comp-within-log-buff
|
|
|
|
|
(if (and data (atom data))
|
|
|
|
|
(insert data)
|
|
|
|
|
(mapc (lambda (x)
|
|
|
|
|
(insert (prin1-to-string x) "\n"))
|
|
|
|
|
data)
|
|
|
|
|
(insert "\n"))))))
|
|
|
|
|
|
|
|
|
|
(defun comp-log-func (func verbosity)
|
2019-09-08 18:54:41 +00:00
|
|
|
|
"Log function FUNC."
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(when (>= comp-verbose verbosity)
|
|
|
|
|
(comp-log (format "\nFunction: %s\n" (comp-func-symbol-name func)) verbosity)
|
|
|
|
|
(cl-loop for block-name being each hash-keys of (comp-func-blocks func)
|
|
|
|
|
using (hash-value bb)
|
|
|
|
|
do (comp-log (concat "<" (symbol-name block-name) ">\n") verbosity)
|
|
|
|
|
(comp-log (comp-block-insns bb) verbosity))))
|
2019-07-21 13:20:39 +00:00
|
|
|
|
|
2019-09-11 21:13:13 +00:00
|
|
|
|
(defun comp-log-edges (func)
|
|
|
|
|
"Log edges in FUNC."
|
|
|
|
|
(let ((edges (comp-func-edges func)))
|
2019-09-29 16:41:31 +00:00
|
|
|
|
(when (> comp-verbose 2)
|
|
|
|
|
(comp-log (format "\nEdges in function: %s\n"
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-func-symbol-name func))
|
|
|
|
|
0))
|
2019-09-11 21:13:13 +00:00
|
|
|
|
(mapc (lambda (e)
|
2019-09-29 16:41:31 +00:00
|
|
|
|
(when (> comp-verbose 2)
|
|
|
|
|
(comp-log (format "n: %d src: %s dst: %s\n"
|
|
|
|
|
(comp-edge-number e)
|
|
|
|
|
(comp-block-name (comp-edge-src e))
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-block-name (comp-edge-dst e)))
|
|
|
|
|
0)))
|
2019-09-11 21:13:13 +00:00
|
|
|
|
edges)))
|
|
|
|
|
|
2019-07-21 13:20:39 +00:00
|
|
|
|
|
|
|
|
|
;;; spill-lap pass specific code.
|
2019-07-09 16:09:47 +00:00
|
|
|
|
|
2019-08-21 19:20:27 +00:00
|
|
|
|
(defun comp-c-func-name (symbol prefix)
|
|
|
|
|
"Given SYMBOL return a name suitable for the native code.
|
2019-08-21 10:17:56 +00:00
|
|
|
|
Put PREFIX in front of it."
|
2019-07-08 10:11:34 +00:00
|
|
|
|
;; Unfortunatelly not all symbol names are valid as C function names...
|
2019-07-21 13:20:39 +00:00
|
|
|
|
;; Nassi's algorithm here:
|
2019-08-21 19:20:27 +00:00
|
|
|
|
(let* ((orig-name (symbol-name symbol))
|
2019-07-08 10:11:34 +00:00
|
|
|
|
(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))
|
2019-10-14 20:08:24 +00:00
|
|
|
|
(aset str (1+ j) (aref byte 1))
|
2019-07-08 10:11:34 +00:00
|
|
|
|
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-08-21 10:17:56 +00:00
|
|
|
|
(concat prefix crypted "_" human-readable)))
|
2019-07-08 10:11:34 +00:00
|
|
|
|
|
2019-07-07 10:30:03 +00:00
|
|
|
|
(defun comp-decrypt-lambda-list (x)
|
|
|
|
|
"Decript lambda list X."
|
2019-09-07 09:17:02 +00:00
|
|
|
|
(unless (fixnump x)
|
|
|
|
|
(error "Can't native compile a non lexical scoped function"))
|
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)
|
2019-08-11 12:54:13 +00:00
|
|
|
|
(make-comp-nargs :min mandatory
|
2019-11-02 11:17:26 +00:00
|
|
|
|
:nonrest nonrest
|
|
|
|
|
:rest rest))))
|
2019-07-07 10:30:03 +00:00
|
|
|
|
|
2019-09-29 19:43:57 +00:00
|
|
|
|
(defun comp-byte-frame-size (byte-compiled-func)
|
|
|
|
|
"Given BYTE-COMPILED-FUNC return the frame size to be allocated."
|
|
|
|
|
;; Is this really correct?
|
|
|
|
|
;; For the 1+ see bytecode.c:365 (finger crossed).
|
|
|
|
|
(1+ (aref byte-compiled-func 3)))
|
|
|
|
|
|
2019-11-02 16:34:32 +00:00
|
|
|
|
(defun comp-spill-lap-function (_function-name)
|
2019-09-07 09:17:02 +00:00
|
|
|
|
"Byte compile FUNCTION-NAME spilling data from the byte compiler."
|
2019-11-02 16:34:32 +00:00
|
|
|
|
(error "To be reimplemented")
|
|
|
|
|
;; (let* ((f (symbol-function function-name))
|
|
|
|
|
;; (func (make-comp-func :symbol-name function-name
|
|
|
|
|
;; :c-func-name (comp-c-func-name
|
|
|
|
|
;; function-name
|
|
|
|
|
;; "F"))))
|
|
|
|
|
;; (when (byte-code-function-p f)
|
|
|
|
|
;; (error "Can't native compile an already bytecompiled function"))
|
|
|
|
|
;; (setf (comp-func-byte-func func)
|
|
|
|
|
;; (byte-compile (comp-func-symbol-name func)))
|
|
|
|
|
;; (let ((lap (alist-get function-name (reverse byte-to-native-bytecode))))
|
|
|
|
|
;; (cl-assert lap)
|
|
|
|
|
;; (comp-log lap)
|
|
|
|
|
;; (let ((lambda-list (aref (comp-func-byte-func func) 0)))
|
|
|
|
|
;; (setf (comp-func-args func)
|
|
|
|
|
;; (comp-decrypt-lambda-list lambda-list)))
|
|
|
|
|
;; (setf (comp-func-lap func) lap)
|
|
|
|
|
;; (setf (comp-func-frame-size func)
|
|
|
|
|
;; (comp-byte-frame-size (comp-func-byte-func func)))
|
|
|
|
|
;; func))
|
|
|
|
|
)
|
2019-09-07 06:18:08 +00:00
|
|
|
|
|
2019-09-07 09:17:02 +00:00
|
|
|
|
(defun comp-spill-lap-functions-file (filename)
|
|
|
|
|
"Byte compile FILENAME spilling data from the byte compiler."
|
|
|
|
|
(byte-compile-file filename)
|
2019-11-02 16:34:32 +00:00
|
|
|
|
(setf (comp-ctxt-top-level-forms comp-ctxt) (reverse byte-to-native-top-level-forms))
|
|
|
|
|
(cl-loop
|
|
|
|
|
for f in (cl-loop for x in byte-to-native-top-level-forms ; All non anonymous.
|
|
|
|
|
when (and (byte-to-native-function-p x)
|
|
|
|
|
(byte-to-native-function-name x))
|
|
|
|
|
collect x)
|
|
|
|
|
for name = (byte-to-native-function-name f)
|
|
|
|
|
for data = (byte-to-native-function-data f)
|
|
|
|
|
for doc = (when (>= (length data) 5) (aref data 4))
|
|
|
|
|
for lap = (alist-get name byte-to-native-lap)
|
|
|
|
|
for lambda-list = (aref data 0)
|
|
|
|
|
for func = (make-comp-func :symbol-name name
|
|
|
|
|
:byte-func data
|
|
|
|
|
:doc doc
|
|
|
|
|
:c-func-name (comp-c-func-name
|
|
|
|
|
name
|
|
|
|
|
"F")
|
|
|
|
|
:args (comp-decrypt-lambda-list lambda-list)
|
|
|
|
|
:lap lap
|
|
|
|
|
:frame-size (comp-byte-frame-size data))
|
2019-11-09 15:56:55 +00:00
|
|
|
|
do (comp-log (format "Function %s:\n" name) 1)
|
|
|
|
|
(comp-log lap 1)
|
2019-11-02 16:34:32 +00:00
|
|
|
|
collect func))
|
2019-09-07 09:17:02 +00:00
|
|
|
|
|
2019-09-07 06:18:08 +00:00
|
|
|
|
(defun comp-spill-lap (input)
|
|
|
|
|
"Byte compile and spill the LAP rapresentation for INPUT.
|
|
|
|
|
If INPUT is a symbol this is the function-name to be compiled.
|
|
|
|
|
If INPUT is a string this is the file path to be compiled."
|
2019-09-06 17:33:16 +00:00
|
|
|
|
(let ((byte-native-compiling t)
|
2019-09-21 15:18:57 +00:00
|
|
|
|
(byte-to-native-lap ())
|
|
|
|
|
(byte-to-native-bytecode ())
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(byte-to-native-top-level-forms ()))
|
2019-09-07 06:18:08 +00:00
|
|
|
|
(cl-typecase input
|
|
|
|
|
(symbol (list (comp-spill-lap-function input)))
|
2019-09-07 09:17:02 +00:00
|
|
|
|
(string (comp-spill-lap-functions-file input)))))
|
2019-07-08 05:56:37 +00:00
|
|
|
|
|
2019-07-21 13:20:39 +00:00
|
|
|
|
|
|
|
|
|
;;; Limplification pass specific code.
|
|
|
|
|
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(cl-defstruct (comp-limplify (:copier nil))
|
|
|
|
|
"Support structure used during function limplification."
|
|
|
|
|
(frame nil :type vector
|
|
|
|
|
:documentation "Meta-stack used to flat LAP.")
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(curr-block nil :type comp-block
|
|
|
|
|
:documentation "Current block baing limplified.")
|
2019-10-20 07:32:57 +00:00
|
|
|
|
(sp -1 :type number
|
|
|
|
|
:documentation "Current stack pointer while walking LAP.
|
|
|
|
|
Points to the next slot to be filled.")
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(pc 0 :type number
|
|
|
|
|
:documentation "Current program counter while walking LAP.")
|
2019-10-13 15:41:26 +00:00
|
|
|
|
(label-to-addr nil :type hash-table
|
|
|
|
|
:documentation "LAP hash table -> address.")
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(pending-blocks () :type list
|
|
|
|
|
:documentation "List of blocks waiting for limplification."))
|
|
|
|
|
|
|
|
|
|
(defconst comp-lap-eob-ops
|
|
|
|
|
'(byte-goto byte-goto-if-nil byte-goto-if-not-nil byte-goto-if-nil-else-pop
|
2019-10-19 14:31:02 +00:00
|
|
|
|
byte-goto-if-not-nil-else-pop byte-return byte-pushcatch
|
2019-10-20 08:39:59 +00:00
|
|
|
|
byte-switch byte-pushconditioncase)
|
2019-10-13 08:36:22 +00:00
|
|
|
|
"LAP end of basic blocks op codes.")
|
|
|
|
|
|
|
|
|
|
(defsubst comp-lap-eob-p (inst)
|
|
|
|
|
"Return t if INST closes the current basic blocks, nil otherwise."
|
|
|
|
|
(when (member (car inst) comp-lap-eob-ops)
|
|
|
|
|
t))
|
2019-10-05 15:08:48 +00:00
|
|
|
|
|
2019-10-19 09:20:15 +00:00
|
|
|
|
(defsubst comp-lap-fall-through-p (inst)
|
2019-10-19 14:31:02 +00:00
|
|
|
|
"Return t if INST fall through, nil otherwise."
|
2019-10-19 09:20:15 +00:00
|
|
|
|
(when (not (member (car inst) '(byte-goto byte-return)))
|
|
|
|
|
t))
|
|
|
|
|
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(defsubst comp-sp ()
|
2019-09-18 22:07:10 +00:00
|
|
|
|
"Current stack pointer."
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(comp-limplify-sp comp-pass))
|
|
|
|
|
(gv-define-setter comp-sp (value)
|
|
|
|
|
`(setf (comp-limplify-sp comp-pass) ,value))
|
2019-09-18 22:07:10 +00:00
|
|
|
|
|
|
|
|
|
(defmacro comp-with-sp (sp &rest body)
|
|
|
|
|
"Execute BODY setting the stack pointer to SP.
|
|
|
|
|
Restore the original value afterwards."
|
|
|
|
|
(declare (debug (form body))
|
|
|
|
|
(indent defun))
|
|
|
|
|
(let ((sym (gensym)))
|
|
|
|
|
`(let ((,sym (comp-sp)))
|
|
|
|
|
(setf (comp-sp) ,sp)
|
|
|
|
|
(progn ,@body)
|
|
|
|
|
(setf (comp-sp) ,sym))))
|
|
|
|
|
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(defsubst comp-slot-n (n)
|
2019-09-18 22:07:10 +00:00
|
|
|
|
"Slot N into the meta-stack."
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(aref (comp-limplify-frame comp-pass) n))
|
2019-09-18 22:07:10 +00:00
|
|
|
|
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(defsubst comp-slot ()
|
2019-09-18 22:07:10 +00:00
|
|
|
|
"Current slot into the meta-stack pointed by sp."
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(comp-slot-n (comp-sp)))
|
2019-09-18 22:07:10 +00:00
|
|
|
|
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(defsubst comp-slot+1 ()
|
2019-09-18 22:07:10 +00:00
|
|
|
|
"Slot into the meta-stack pointed by sp + 1."
|
2019-10-05 15:08:48 +00:00
|
|
|
|
(comp-slot-n (1+ (comp-sp))))
|
2019-07-08 09:37:17 +00:00
|
|
|
|
|
2019-10-13 16:58:46 +00:00
|
|
|
|
(defsubst comp-label-to-addr (label)
|
|
|
|
|
"Find the address of LABEL."
|
|
|
|
|
(or (gethash label (comp-limplify-label-to-addr comp-pass))
|
|
|
|
|
(error "Can't find label %d" label)))
|
|
|
|
|
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(defun comp-bb-maybe-add (lap-addr &optional sp)
|
|
|
|
|
"If necessary create a pending basic block for LAP-ADDR with stack depth SP.
|
|
|
|
|
The basic block is returned regardless it was already declared or not."
|
|
|
|
|
(let ((bb (or (cl-loop ; See if the block was already liplified.
|
|
|
|
|
for bb being the hash-value in (comp-func-blocks comp-func)
|
|
|
|
|
when (equal (comp-block-addr bb) lap-addr)
|
|
|
|
|
return bb)
|
|
|
|
|
(cl-find-if (lambda (bb) ; Look within the pendings blocks.
|
|
|
|
|
(= (comp-block-addr bb) lap-addr))
|
|
|
|
|
(comp-limplify-pending-blocks comp-pass)))))
|
|
|
|
|
(if bb
|
|
|
|
|
(progn
|
|
|
|
|
(cl-assert (or (null sp) (= sp (comp-block-sp bb)))
|
|
|
|
|
(sp (comp-block-sp bb)) "sp %d %d differs")
|
|
|
|
|
bb)
|
|
|
|
|
(car (push (make--comp-block lap-addr sp (comp-new-block-sym))
|
2019-10-20 19:00:17 +00:00
|
|
|
|
(comp-limplify-pending-blocks comp-pass))))))
|
2019-10-13 08:36:22 +00:00
|
|
|
|
|
2019-08-21 10:17:56 +00:00
|
|
|
|
(defun comp-call (func &rest args)
|
|
|
|
|
"Emit a call for function FUNC with ARGS."
|
2019-08-22 14:00:43 +00:00
|
|
|
|
(comp-add-subr-to-relocs func)
|
2019-08-21 19:20:27 +00:00
|
|
|
|
`(call ,func ,@args))
|
2019-08-19 15:59:20 +00:00
|
|
|
|
|
2019-08-21 10:17:56 +00:00
|
|
|
|
(defun comp-callref (func &rest args)
|
|
|
|
|
"Emit a call usign narg abi for FUNC with ARGS."
|
2019-08-22 14:00:43 +00:00
|
|
|
|
(comp-add-subr-to-relocs func)
|
2019-09-15 13:41:42 +00:00
|
|
|
|
`(callref ,func ,@(cl-loop with (nargs off) = args
|
|
|
|
|
repeat nargs
|
|
|
|
|
for sp from off
|
|
|
|
|
collect (comp-slot-n sp))))
|
2019-08-19 15:59:20 +00:00
|
|
|
|
|
2019-09-14 16:12:16 +00:00
|
|
|
|
(cl-defun make-comp-mvar (&key slot (constant nil const-vld) type)
|
|
|
|
|
(when const-vld
|
|
|
|
|
(comp-add-const-to-relocs constant))
|
|
|
|
|
(make--comp-mvar :slot slot :const-vld const-vld :constant constant
|
|
|
|
|
:type type))
|
|
|
|
|
|
2019-09-15 10:08:22 +00:00
|
|
|
|
(defun comp-new-frame (size &optional ssa)
|
2019-07-21 13:20:39 +00:00
|
|
|
|
"Return a clean frame of meta variables of size SIZE."
|
2019-09-11 19:56:26 +00:00
|
|
|
|
(cl-loop with v = (make-vector size nil)
|
|
|
|
|
for i below size
|
2019-10-24 20:20:38 +00:00
|
|
|
|
for mvar = (if ssa
|
|
|
|
|
(make-comp-ssa-mvar :slot i)
|
|
|
|
|
(make-comp-mvar :slot i))
|
2019-09-15 10:08:22 +00:00
|
|
|
|
do (aset v i mvar)
|
2019-11-02 13:34:31 +00:00
|
|
|
|
finally return v))
|
2019-07-08 07:06:58 +00:00
|
|
|
|
|
2019-10-21 09:30:39 +00:00
|
|
|
|
(defsubst comp-emit (insn)
|
|
|
|
|
"Emit INSN into basic block BB."
|
|
|
|
|
(let ((bb (comp-limplify-curr-block comp-pass)))
|
2019-10-20 19:00:17 +00:00
|
|
|
|
(cl-assert (not (comp-block-closed bb)))
|
|
|
|
|
(push insn (comp-block-insns bb))))
|
2019-07-13 14:34:59 +00:00
|
|
|
|
|
2019-10-21 09:30:39 +00:00
|
|
|
|
(defsubst comp-emit-as-head (insn bb)
|
|
|
|
|
"Emit INSN at the head of basic block BB.
|
|
|
|
|
NOTE: this is used for late fixup therefore ignore if the basic block is closed."
|
|
|
|
|
(setf (comp-block-insns bb) (nconc (comp-block-insns bb) (list insn))))
|
|
|
|
|
|
2019-10-24 20:20:38 +00:00
|
|
|
|
(defsubst comp-emit-set-call (call)
|
2019-07-13 15:08:15 +00:00
|
|
|
|
"Emit CALL assigning the result the the current slot frame.
|
2019-08-11 17:03:06 +00:00
|
|
|
|
If the callee function is known to have a return type propagate it."
|
2019-07-13 09:33:15 +00:00
|
|
|
|
(cl-assert call)
|
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-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)
|
2019-09-14 10:39:53 +00:00
|
|
|
|
(comp-emit `(set ,(comp-slot) ,src-slot)))))
|
2019-07-08 07:06:58 +00:00
|
|
|
|
|
2019-10-24 20:20:38 +00:00
|
|
|
|
(defsubst comp-emit-annotation (str)
|
2019-07-10 16:55:19 +00:00
|
|
|
|
"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-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-08-18 19:39:09 +00:00
|
|
|
|
(let ((rel-idx (comp-add-const-to-relocs val)))
|
2019-09-01 15:02:35 +00:00
|
|
|
|
(cl-assert (numberp rel-idx))
|
2019-08-18 21:09:20 +00:00
|
|
|
|
(comp-emit `(setimm ,(comp-slot) ,rel-idx ,val))))
|
2019-07-08 15:04:33 +00:00
|
|
|
|
|
2019-10-19 14:31:02 +00:00
|
|
|
|
(defun comp-make-curr-block (block-name entry-sp &optional addr)
|
2019-10-13 08:36:22 +00:00
|
|
|
|
"Create a basic block with BLOCK-NAME and set it as current block.
|
|
|
|
|
ENTRY-SP is the sp value when entering.
|
|
|
|
|
The block is added to the current function.
|
|
|
|
|
The block is returned."
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(let ((bb (make--comp-block addr entry-sp block-name)))
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(setf (comp-limplify-curr-block comp-pass) bb)
|
2019-10-19 14:31:02 +00:00
|
|
|
|
(setf (comp-limplify-pc comp-pass) addr)
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(setf (comp-limplify-sp comp-pass) (comp-block-sp bb))
|
|
|
|
|
(puthash (comp-block-name bb) bb (comp-func-blocks comp-func))
|
|
|
|
|
bb))
|
|
|
|
|
|
|
|
|
|
(defun comp-emit-uncond-jump (lap-label)
|
|
|
|
|
"Emit an unconditional branch to LAP-LABEL."
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(cl-destructuring-bind (label-num . stack-depth) lap-label
|
2019-10-24 20:13:29 +00:00
|
|
|
|
(when stack-depth
|
|
|
|
|
(cl-assert (= (1- stack-depth) (comp-sp))))
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(let ((target (comp-bb-maybe-add (comp-label-to-addr label-num)
|
|
|
|
|
(comp-sp))))
|
|
|
|
|
(comp-emit `(jump ,(comp-block-name target)))
|
2019-10-20 14:04:29 +00:00
|
|
|
|
(setf (comp-block-closed (comp-limplify-curr-block comp-pass)) t))))
|
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-10-19 14:31:02 +00:00
|
|
|
|
If NEGATED non null negate the tested condition.
|
|
|
|
|
Return value is the fall through block name."
|
2019-10-20 08:39:59 +00:00
|
|
|
|
(cl-destructuring-bind (label-num . label-sp) lap-label
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(let* ((bb (comp-block-name (comp-bb-maybe-add (1+ (comp-limplify-pc comp-pass))
|
|
|
|
|
(comp-sp)))) ; Fall through block.
|
|
|
|
|
(target-sp (+ target-offset (comp-sp)))
|
|
|
|
|
(target (comp-block-name (comp-bb-maybe-add (comp-label-to-addr label-num)
|
|
|
|
|
target-sp))))
|
2019-10-20 12:42:06 +00:00
|
|
|
|
(when label-sp
|
|
|
|
|
(cl-assert (= (1- label-sp) (+ target-offset (comp-sp)))))
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(comp-emit (if negated
|
|
|
|
|
(list 'cond-jump a b target bb)
|
2019-10-19 14:31:02 +00:00
|
|
|
|
(list 'cond-jump a b bb target)))
|
2019-10-20 14:04:29 +00:00
|
|
|
|
(setf (comp-block-closed (comp-limplify-curr-block comp-pass)) t)
|
2019-10-19 14:31:02 +00:00
|
|
|
|
bb)))
|
2019-10-13 18:45:14 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-emit-handler (lap-label handler-type)
|
|
|
|
|
"Emit a non local exit handler to LAP-LABEL of type HANDLER-TYPE."
|
2019-10-20 08:39:59 +00:00
|
|
|
|
(cl-destructuring-bind (label-num . label-sp) lap-label
|
2019-10-20 19:00:17 +00:00
|
|
|
|
(cl-assert (= (- label-sp 2) (comp-sp)))
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(let* ((guarded-bb (comp-bb-maybe-add (1+ (comp-limplify-pc comp-pass))
|
|
|
|
|
(comp-sp)))
|
|
|
|
|
(handler-bb (comp-bb-maybe-add (comp-label-to-addr label-num)
|
2019-11-10 09:17:24 +00:00
|
|
|
|
(1+ (comp-sp)))))
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(comp-emit (list 'push-handler
|
|
|
|
|
handler-type
|
2019-10-20 19:00:17 +00:00
|
|
|
|
(comp-slot+1)
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(comp-block-name handler-bb)
|
|
|
|
|
(comp-block-name guarded-bb)))
|
2019-10-20 19:00:17 +00:00
|
|
|
|
(setf (comp-block-closed (comp-limplify-curr-block comp-pass)) t)
|
2019-11-10 09:17:24 +00:00
|
|
|
|
(comp-emit-as-head `(fetch-handler ,(comp-slot+1)) handler-bb))))
|
2019-07-14 12:39:29 +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)
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-call 'cons
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-slot)
|
|
|
|
|
(make-comp-mvar :constant nil))))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(cl-loop for sp from (+ (comp-sp) n -2) downto (comp-sp)
|
|
|
|
|
do (comp-with-sp sp
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-call 'cons
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-slot)
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-slot+1))))))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
|
2019-07-14 12:39:29 +00:00
|
|
|
|
(defun comp-new-block-sym ()
|
2019-09-22 07:59:14 +00:00
|
|
|
|
"Return a unique symbol naming the next new basic block."
|
|
|
|
|
(intern (format "bb_%s" (funcall (comp-func-block-cnt-gen comp-func)))))
|
2019-07-14 12:39:29 +00:00
|
|
|
|
|
2019-10-13 15:41:26 +00:00
|
|
|
|
(defun comp-fill-label-h ()
|
|
|
|
|
"Fill label-to-addr hash table for the current function."
|
|
|
|
|
(setf (comp-limplify-label-to-addr comp-pass) (make-hash-table :test 'eql))
|
|
|
|
|
(cl-loop for insn in (comp-func-lap comp-func)
|
|
|
|
|
for addr from 0
|
|
|
|
|
do (pcase insn
|
2019-10-13 16:58:46 +00:00
|
|
|
|
(`(TAG ,label . ,_)
|
2019-10-13 15:41:26 +00:00
|
|
|
|
(puthash label addr (comp-limplify-label-to-addr comp-pass))))))
|
|
|
|
|
|
2019-09-16 17:47:49 +00:00
|
|
|
|
(defun comp-emit-switch (var last-insn)
|
|
|
|
|
"Emit a limple for a lap jump table given VAR and LAST-INSN."
|
|
|
|
|
(pcase last-insn
|
|
|
|
|
(`(setimm ,_ ,_ ,const)
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(cl-loop
|
|
|
|
|
for test being each hash-keys of const
|
|
|
|
|
using (hash-value target-label)
|
|
|
|
|
with len = (hash-table-count const)
|
|
|
|
|
for n from 1
|
|
|
|
|
for last = (= n len)
|
|
|
|
|
for m-test = (make-comp-mvar :constant test)
|
|
|
|
|
for target-name = (comp-block-name (comp-bb-maybe-add (comp-label-to-addr target-label)
|
|
|
|
|
(comp-sp)))
|
|
|
|
|
for ff-bb = (if last
|
|
|
|
|
(comp-bb-maybe-add (1+ (comp-limplify-pc comp-pass))
|
|
|
|
|
(comp-sp))
|
|
|
|
|
(make--comp-block nil
|
2019-10-19 14:31:02 +00:00
|
|
|
|
(comp-sp)
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(comp-new-block-sym)))
|
|
|
|
|
for ff-bb-name = (comp-block-name ff-bb)
|
|
|
|
|
do
|
|
|
|
|
(comp-emit (list 'cond-jump var m-test ff-bb-name target-name))
|
|
|
|
|
(unless last
|
|
|
|
|
;; All fall through are artificially created here except the last one.
|
|
|
|
|
(puthash ff-bb-name ff-bb (comp-func-blocks comp-func))
|
|
|
|
|
(setf (comp-limplify-curr-block comp-pass) ff-bb))))
|
2019-09-16 17:47:49 +00:00
|
|
|
|
(_ (error "Missing previous setimm while creating a switch"))))
|
2019-08-03 15:08:55 +00:00
|
|
|
|
|
2019-10-05 14:20:57 +00:00
|
|
|
|
(defun comp-emit-set-call-subr (subr-name sp-delta)
|
|
|
|
|
"Emit a call for SUBR-NAME.
|
|
|
|
|
SP-DELTA is the stack adjustment."
|
|
|
|
|
(let ((subr (symbol-function subr-name))
|
|
|
|
|
(subr-str (symbol-name subr-name))
|
|
|
|
|
(nargs (1+ (- sp-delta))))
|
|
|
|
|
(cl-assert (subrp subr) nil
|
|
|
|
|
"%s not a subr" subr-str)
|
|
|
|
|
(let* ((arity (subr-arity subr))
|
|
|
|
|
(minarg (car arity))
|
|
|
|
|
(maxarg (cdr arity)))
|
|
|
|
|
(cl-assert (not (eq maxarg 'unevalled)) nil
|
|
|
|
|
"%s contains unevalled arg" subr-name)
|
|
|
|
|
(if (eq maxarg 'many)
|
|
|
|
|
;; callref case.
|
|
|
|
|
(comp-emit-set-call (comp-callref subr-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* ((subr-name subr-name)
|
|
|
|
|
(slots (cl-loop for i from 0 below maxarg
|
|
|
|
|
collect (comp-slot-n (+ i (comp-sp))))))
|
|
|
|
|
(comp-emit-set-call (apply #'comp-call (cons subr-name slots))))))))
|
|
|
|
|
|
|
|
|
|
(eval-when-compile
|
|
|
|
|
(defun comp-op-to-fun (x)
|
|
|
|
|
"Given the LAP op strip \"byte-\" to have the subr name."
|
|
|
|
|
(intern (replace-regexp-in-string "byte-" "" x)))
|
|
|
|
|
|
|
|
|
|
(defun comp-body-eff (body op-name sp-delta)
|
|
|
|
|
"Given the original body BODY compute the effective one.
|
|
|
|
|
When BODY is auto guess function name form the LAP bytecode
|
|
|
|
|
name. Othewise expect lname fnname."
|
|
|
|
|
(pcase (car body)
|
|
|
|
|
('auto
|
|
|
|
|
(list `(comp-emit-set-call-subr
|
|
|
|
|
',(comp-op-to-fun op-name)
|
|
|
|
|
,sp-delta)))
|
|
|
|
|
((pred symbolp)
|
|
|
|
|
(list `(comp-emit-set-call-subr
|
|
|
|
|
',(car body)
|
|
|
|
|
,sp-delta)))
|
|
|
|
|
(_ body))))
|
|
|
|
|
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(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."
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(declare (debug (body))
|
|
|
|
|
(indent defun))
|
2019-10-05 14:20:57 +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
|
|
|
|
|
;; Log all LAP ops except the TAG one.
|
|
|
|
|
,(unless (eq op 'TAG)
|
|
|
|
|
`(comp-emit-annotation
|
|
|
|
|
,(concat "LAP op " op-name)))
|
|
|
|
|
;; Emit the stack adjustment if present.
|
|
|
|
|
,(when (and sp-delta (not (eq 0 sp-delta)))
|
2019-10-19 16:15:00 +00:00
|
|
|
|
`(cl-incf (comp-sp) ,sp-delta))
|
2019-10-05 14:20:57 +00:00
|
|
|
|
,@(comp-body-eff body op-name sp-delta))
|
|
|
|
|
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))))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(comp-op-case
|
2019-07-14 12:39:29 +00:00
|
|
|
|
(TAG
|
2019-10-24 20:19:14 +00:00
|
|
|
|
(cl-destructuring-bind (_TAG label-num . label-sp) insn
|
2019-10-20 12:42:06 +00:00
|
|
|
|
;; Paranoid?
|
|
|
|
|
(when label-sp
|
2019-10-24 20:19:14 +00:00
|
|
|
|
(cl-assert (= (1- label-sp) (comp-limplify-sp comp-pass))))
|
|
|
|
|
(comp-emit-annotation (format "LAP TAG %d" label-num))))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-stack-ref
|
2019-07-20 17:26:30 +00:00
|
|
|
|
(comp-copy-slot (- (comp-sp) arg 1)))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-varref
|
2019-09-02 16:08:59 +00:00
|
|
|
|
(comp-emit-set-call (comp-call 'symbol-value (make-comp-mvar
|
2019-08-19 16:22:26 +00:00
|
|
|
|
:constant arg))))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-varset
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-emit (comp-call 'set_internal
|
|
|
|
|
(make-comp-mvar :constant arg)
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-slot+1))))
|
2019-08-11 12:41:35 +00:00
|
|
|
|
(byte-varbind ;; Verify
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-emit (comp-call 'specbind
|
|
|
|
|
(make-comp-mvar :constant arg)
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-slot+1))))
|
2019-07-14 09:15:18 +00:00
|
|
|
|
(byte-call
|
2019-10-19 16:15:00 +00:00
|
|
|
|
(cl-incf (comp-sp) (- arg))
|
2019-09-18 09:55:25 +00:00
|
|
|
|
(comp-emit-set-call (comp-callref 'funcall (1+ arg) (comp-sp))))
|
2019-07-14 19:26:20 +00:00
|
|
|
|
(byte-unbind
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-emit (comp-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-10-13 18:45:14 +00:00
|
|
|
|
(comp-emit-handler (cddr insn) 'condition-case))
|
2019-07-22 09:08:53 +00:00
|
|
|
|
(byte-pushcatch
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(comp-emit-handler (cddr 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-08-19 16:22:26 +00:00
|
|
|
|
(byte-not null)
|
2019-07-14 21:35:04 +00:00
|
|
|
|
(byte-car auto)
|
|
|
|
|
(byte-cdr auto)
|
|
|
|
|
(byte-cons auto)
|
2019-07-14 07:53:06 +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))
|
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
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-callref 'concat 2 (comp-sp))))
|
2019-07-14 08:57:46 +00:00
|
|
|
|
(byte-concat3
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-callref 'concat 3 (comp-sp))))
|
2019-07-14 08:57:46 +00:00
|
|
|
|
(byte-concat4
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-callref 'concat 4 (comp-sp))))
|
|
|
|
|
(byte-sub1 1-)
|
|
|
|
|
(byte-add1 1+)
|
|
|
|
|
(byte-eqlsign =)
|
|
|
|
|
(byte-gtr >)
|
|
|
|
|
(byte-lss <)
|
|
|
|
|
(byte-leq <=)
|
|
|
|
|
(byte-geq >=)
|
|
|
|
|
(byte-diff -)
|
2019-08-15 19:06:07 +00:00
|
|
|
|
(byte-negate
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-emit-set-call (comp-call 'negate (comp-slot))))
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(byte-plus +)
|
2019-07-20 14:34:37 +00:00
|
|
|
|
(byte-max auto)
|
|
|
|
|
(byte-min auto)
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(byte-mult *)
|
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-08-19 16:22:26 +00:00
|
|
|
|
(byte-preceding-char preceding-char)
|
2019-07-14 22:58:03 +00:00
|
|
|
|
(byte-current-column auto)
|
2019-08-15 20:23:32 +00:00
|
|
|
|
(byte-indent-to
|
2019-10-27 14:55:08 +00:00
|
|
|
|
(comp-emit-set-call (comp-call 'indent-to
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-slot)
|
|
|
|
|
(make-comp-mvar :constant nil))))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(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)
|
2019-08-08 15:35:25 +00:00
|
|
|
|
(byte-save-current-buffer
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-emit (comp-call 'record_unwind_current_buffer)))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(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)
|
2019-08-11 10:37:21 +00:00
|
|
|
|
(byte-narrow-to-region
|
2019-10-27 14:55:08 +00:00
|
|
|
|
(comp-emit-set-call (comp-call 'narrow-to-region
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-slot)
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-slot+1))))
|
2019-08-11 10:37:21 +00:00
|
|
|
|
(byte-widen
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-call 'widen)))
|
2019-07-20 14:34:37 +00:00
|
|
|
|
(byte-end-of-line auto)
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(byte-constant2) ; TODO
|
|
|
|
|
;; Branches.
|
2019-07-14 12:39:29 +00:00
|
|
|
|
(byte-goto
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(comp-emit-uncond-jump (cddr insn)))
|
2019-07-14 12:39:29 +00:00
|
|
|
|
(byte-goto-if-nil
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-emit-cond-jump (comp-slot+1) (make-comp-mvar :constant nil) 0
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(cddr insn) nil))
|
2019-07-14 12:39:29 +00:00
|
|
|
|
(byte-goto-if-not-nil
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-emit-cond-jump (comp-slot+1) (make-comp-mvar :constant nil) 0
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(cddr insn) t))
|
2019-07-14 12:39:29 +00:00
|
|
|
|
(byte-goto-if-nil-else-pop
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-emit-cond-jump (comp-slot+1) (make-comp-mvar :constant nil) 1
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(cddr insn) nil))
|
2019-07-14 12:39:29 +00:00
|
|
|
|
(byte-goto-if-not-nil-else-pop
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-emit-cond-jump (comp-slot+1) (make-comp-mvar :constant nil) 1
|
2019-10-13 18:45:14 +00:00
|
|
|
|
(cddr insn) t))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-return
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(comp-emit `(return ,(comp-slot+1))))
|
2019-07-14 21:35:04 +00:00
|
|
|
|
(byte-discard 'pass)
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-dup
|
2019-07-20 17:26:30 +00:00
|
|
|
|
(comp-copy-slot (1- (comp-sp))))
|
2019-08-11 10:24:15 +00:00
|
|
|
|
(byte-save-excursion
|
2019-08-19 15:59:20 +00:00
|
|
|
|
(comp-emit (comp-call 'record_unwind_protect_excursion)))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-save-window-excursion-OBSOLETE)
|
2019-08-11 12:10:57 +00:00
|
|
|
|
(byte-save-restriction
|
2019-11-09 10:43:16 +00:00
|
|
|
|
(comp-emit (comp-call 'helper_save_restriction)))
|
2019-08-11 12:10:57 +00:00
|
|
|
|
(byte-catch) ;; Obsolete
|
2019-08-08 15:35:25 +00:00
|
|
|
|
(byte-unwind-protect
|
2019-09-08 14:57:40 +00:00
|
|
|
|
(comp-emit (comp-call 'helper_unwind_protect (comp-slot+1))))
|
2019-08-11 12:10:57 +00:00
|
|
|
|
(byte-condition-case) ;; Obsolete
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-temp-output-buffer-setup-OBSOLETE)
|
|
|
|
|
(byte-temp-output-buffer-show-OBSOLETE)
|
2019-08-11 12:10:57 +00:00
|
|
|
|
(byte-unbind-all) ;; Obsolete
|
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-08-19 16:22:26 +00:00
|
|
|
|
(byte-string= string-equal)
|
|
|
|
|
(byte-string< string-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-08-19 16:22:26 +00:00
|
|
|
|
(byte-quo /)
|
|
|
|
|
(byte-rem %)
|
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-10-19 16:15:00 +00:00
|
|
|
|
(cl-incf (comp-sp) (- 1 arg))
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-callref 'list arg (comp-sp))))
|
2019-07-14 08:57:46 +00:00
|
|
|
|
(byte-concatN
|
2019-10-19 16:15:00 +00:00
|
|
|
|
(cl-incf (comp-sp) (- 1 arg))
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-callref 'concat arg (comp-sp))))
|
2019-07-20 16:50:41 +00:00
|
|
|
|
(byte-insertN
|
2019-10-19 16:15:00 +00:00
|
|
|
|
(cl-incf (comp-sp) (- 1 arg))
|
2019-08-19 16:22:26 +00:00
|
|
|
|
(comp-emit-set-call (comp-callref 'insert arg (comp-sp))))
|
2019-07-20 17:26:30 +00:00
|
|
|
|
(byte-stack-set
|
2019-09-16 17:48:13 +00:00
|
|
|
|
(comp-with-sp (1+ (comp-sp)) ;; FIXME!!
|
2019-07-20 17:26:30 +00:00
|
|
|
|
(comp-copy-slot (comp-sp) (- (comp-sp) arg))))
|
2019-09-08 16:48:29 +00:00
|
|
|
|
(byte-stack-set2 (cl-assert nil)) ;; TODO
|
2019-07-20 17:26:30 +00:00
|
|
|
|
(byte-discardN
|
2019-10-19 16:15:00 +00:00
|
|
|
|
(cl-incf (comp-sp) (- arg)))
|
2019-08-03 15:08:55 +00:00
|
|
|
|
(byte-switch
|
2019-09-16 17:47:49 +00:00
|
|
|
|
;; Assume to follow the emission of a setimm.
|
|
|
|
|
;; This is checked into comp-emit-switch.
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(comp-emit-switch (comp-slot+1)
|
|
|
|
|
(cl-second (comp-block-insns
|
|
|
|
|
(comp-limplify-curr-block comp-pass)))))
|
2019-07-14 07:53:06 +00:00
|
|
|
|
(byte-constant
|
2019-07-20 16:50:41 +00:00
|
|
|
|
(comp-emit-set-const arg))
|
|
|
|
|
(byte-discardN-preserve-tos
|
2019-10-19 16:15:00 +00:00
|
|
|
|
(cl-incf (comp-sp) (- arg))
|
2019-07-20 17:26:30 +00:00
|
|
|
|
(comp-copy-slot (+ arg (comp-sp)))))))
|
2019-07-07 10:30:03 +00:00
|
|
|
|
|
2019-11-02 11:17:26 +00:00
|
|
|
|
(defun comp-emit-narg-prologue (minarg nonrest rest)
|
2019-08-11 14:44:12 +00:00
|
|
|
|
"Emit the prologue for a narg function."
|
2019-08-11 17:03:06 +00:00
|
|
|
|
(cl-loop for i below minarg
|
2019-10-14 20:08:24 +00:00
|
|
|
|
do (comp-emit `(set-args-to-local ,(comp-slot-n i)))
|
|
|
|
|
(comp-emit '(inc-args)))
|
2019-08-11 17:03:06 +00:00
|
|
|
|
(cl-loop for i from minarg below nonrest
|
2019-08-11 14:44:12 +00:00
|
|
|
|
for bb = (intern (format "entry_%s" i))
|
|
|
|
|
for fallback = (intern (format "entry_fallback_%s" i))
|
2019-10-14 20:08:24 +00:00
|
|
|
|
do (comp-emit `(cond-jump-narg-leq ,i ,bb ,fallback))
|
|
|
|
|
(comp-make-curr-block bb (comp-sp))
|
|
|
|
|
(comp-emit `(set-args-to-local ,(comp-slot-n i)))
|
|
|
|
|
(comp-emit '(inc-args))
|
2019-10-27 16:13:03 +00:00
|
|
|
|
finally (comp-emit '(jump entry_rest_args)))
|
2019-10-20 09:10:22 +00:00
|
|
|
|
(when (not (= minarg nonrest))
|
|
|
|
|
(cl-loop for i from minarg below nonrest
|
2019-10-27 16:13:03 +00:00
|
|
|
|
for bb = (intern (format "entry_fallback_%s" i))
|
|
|
|
|
for next-bb = (if (= (1+ i) nonrest)
|
|
|
|
|
'entry_rest_args
|
|
|
|
|
(intern (format "entry_fallback_%s" (1+ i))))
|
2019-10-20 09:10:22 +00:00
|
|
|
|
do (comp-with-sp i
|
2019-10-27 16:13:03 +00:00
|
|
|
|
(comp-make-curr-block bb (comp-sp))
|
|
|
|
|
(comp-emit-set-const nil)
|
|
|
|
|
(comp-emit `(jump ,next-bb)))))
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(comp-make-curr-block 'entry_rest_args (comp-sp))
|
2019-11-02 11:17:26 +00:00
|
|
|
|
(comp-emit `(set-rest-args-to-local ,(comp-slot-n nonrest)))
|
|
|
|
|
(setf (comp-sp) nonrest)
|
|
|
|
|
(when (and (> nonrest 8) (null rest))
|
|
|
|
|
(cl-decf (comp-sp))))
|
2019-08-11 14:44:12 +00:00
|
|
|
|
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(defun comp-limplify-finalize-function (func)
|
|
|
|
|
"Reverse insns into all basic blocks of FUNC."
|
|
|
|
|
(cl-loop for bb being the hash-value in (comp-func-blocks func)
|
|
|
|
|
do (setf (comp-block-insns bb)
|
|
|
|
|
(nreverse (comp-block-insns bb))))
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log-func func 2)
|
2019-09-07 14:35:07 +00:00
|
|
|
|
func)
|
|
|
|
|
|
2019-11-03 14:27:57 +00:00
|
|
|
|
(cl-defgeneric comp-emit-for-top-level (form)
|
|
|
|
|
"Emit the limple code for top level FORM.")
|
|
|
|
|
|
|
|
|
|
(cl-defmethod comp-emit-for-top-level ((form byte-to-native-function))
|
|
|
|
|
(let* ((name (byte-to-native-function-name form))
|
|
|
|
|
(f (gethash name (comp-ctxt-funcs-h comp-ctxt)))
|
|
|
|
|
(args (comp-func-args f))
|
|
|
|
|
(c-name (comp-func-c-func-name f))
|
|
|
|
|
(doc (comp-func-doc f)))
|
|
|
|
|
(cl-assert (and name f))
|
|
|
|
|
(comp-emit (comp-call 'comp--register-subr
|
|
|
|
|
(make-comp-mvar :constant name)
|
|
|
|
|
(make-comp-mvar :constant (comp-args-base-min args))
|
|
|
|
|
(make-comp-mvar :constant (if (comp-args-p args)
|
|
|
|
|
(comp-args-max args)
|
|
|
|
|
'many))
|
|
|
|
|
(make-comp-mvar :constant c-name)
|
|
|
|
|
(make-comp-mvar :constant doc)))))
|
|
|
|
|
|
|
|
|
|
(cl-defmethod comp-emit-for-top-level ((form byte-to-native-top-level))
|
2019-11-04 21:13:20 +00:00
|
|
|
|
(let ((form (byte-to-native-top-level-form form)))
|
|
|
|
|
(comp-emit (comp-call 'eval
|
|
|
|
|
(make-comp-mvar :constant form)
|
|
|
|
|
(make-comp-mvar :constant t)))))
|
2019-11-03 14:27:57 +00:00
|
|
|
|
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(defun comp-limplify-top-level ()
|
|
|
|
|
"Create a limple function doing the business for top level forms.
|
2019-10-05 15:51:49 +00:00
|
|
|
|
This will be called at load-time."
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(let* ((func (make-comp-func :symbol-name 'top-level-run
|
|
|
|
|
:c-func-name "top_level_run"
|
|
|
|
|
:args (make-comp-args :min 0 :max 0)
|
|
|
|
|
:frame-size 0))
|
|
|
|
|
(comp-func func)
|
|
|
|
|
(comp-pass (make-comp-limplify
|
2019-11-01 14:28:17 +00:00
|
|
|
|
:curr-block (make--comp-block -1 0 'top-level)
|
2019-10-13 08:36:22 +00:00
|
|
|
|
:frame (comp-new-frame 0))))
|
|
|
|
|
(comp-make-curr-block 'entry (comp-sp))
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(comp-emit-annotation "Top level")
|
2019-11-03 14:27:57 +00:00
|
|
|
|
(mapc #'comp-emit-for-top-level (comp-ctxt-top-level-forms comp-ctxt))
|
|
|
|
|
(comp-emit `(return ,(make-comp-mvar :constant t)))
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(comp-limplify-finalize-function func)))
|
|
|
|
|
|
2019-10-13 16:58:46 +00:00
|
|
|
|
(defun comp-addr-to-bb-name (addr)
|
|
|
|
|
"Search for a block starting at ADDR into pending or limplified blocks."
|
|
|
|
|
;; FIXME: Actually we could have another hash for this.
|
|
|
|
|
(cl-flet ((pred (bb)
|
|
|
|
|
(equal (comp-block-addr bb) addr)))
|
|
|
|
|
(if-let ((pending (cl-find-if #'pred
|
|
|
|
|
(comp-limplify-pending-blocks comp-pass))))
|
|
|
|
|
(comp-block-name pending)
|
|
|
|
|
(cl-loop for bb being the hash-value in (comp-func-blocks comp-func)
|
|
|
|
|
when (pred bb)
|
2019-11-02 13:34:31 +00:00
|
|
|
|
return (comp-block-name bb)))))
|
2019-10-13 16:58:46 +00:00
|
|
|
|
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(defun comp-limplify-block (bb)
|
|
|
|
|
"Limplify basic-block BB and add it to the current function."
|
2019-10-19 14:31:02 +00:00
|
|
|
|
(setf (comp-limplify-curr-block comp-pass) bb)
|
|
|
|
|
(setf (comp-limplify-sp comp-pass) (comp-block-sp bb))
|
|
|
|
|
(setf (comp-limplify-pc comp-pass) (comp-block-addr bb))
|
|
|
|
|
(puthash (comp-block-name bb) bb (comp-func-blocks comp-func))
|
|
|
|
|
(cl-loop
|
2019-10-27 15:57:29 +00:00
|
|
|
|
for inst-cell on (nthcdr (comp-limplify-pc comp-pass)
|
2019-10-19 14:31:02 +00:00
|
|
|
|
(comp-func-lap comp-func))
|
2019-10-27 15:57:29 +00:00
|
|
|
|
for inst = (car inst-cell)
|
|
|
|
|
for next-inst = (car-safe (cdr inst-cell))
|
2019-10-19 14:31:02 +00:00
|
|
|
|
do (comp-limplify-lap-inst inst)
|
|
|
|
|
(cl-incf (comp-limplify-pc comp-pass))
|
2019-10-27 15:57:29 +00:00
|
|
|
|
when (comp-lap-fall-through-p inst)
|
|
|
|
|
do (pcase next-inst
|
|
|
|
|
(`(TAG ,_label . ,label-sp)
|
|
|
|
|
(when label-sp
|
|
|
|
|
(cl-assert (= (1- label-sp) (comp-sp))))
|
|
|
|
|
(let* ((stack-depth (if label-sp
|
|
|
|
|
(1- label-sp)
|
|
|
|
|
(comp-sp)))
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(next-bb (comp-block-name (comp-bb-maybe-add (comp-limplify-pc comp-pass) stack-depth))))
|
2019-10-27 15:57:29 +00:00
|
|
|
|
(unless (comp-block-closed bb)
|
|
|
|
|
(comp-emit `(jump ,next-bb))))
|
|
|
|
|
(cl-return)))
|
2019-10-19 14:31:02 +00:00
|
|
|
|
until (comp-lap-eob-p inst)))
|
2019-10-13 08:36:22 +00:00
|
|
|
|
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(defun comp-limplify-function (func)
|
|
|
|
|
"Limplify a single function FUNC."
|
|
|
|
|
(let* ((frame-size (comp-func-frame-size func))
|
|
|
|
|
(comp-func func)
|
|
|
|
|
(comp-pass (make-comp-limplify
|
|
|
|
|
:frame (comp-new-frame frame-size)))
|
2019-11-02 11:17:26 +00:00
|
|
|
|
(args (comp-func-args func)))
|
2019-10-13 15:41:26 +00:00
|
|
|
|
(comp-fill-label-h)
|
2019-09-07 14:35:07 +00:00
|
|
|
|
;; Prologue
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(comp-make-curr-block 'entry (comp-sp))
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(comp-emit-annotation (concat "Lisp function: "
|
|
|
|
|
(symbol-name (comp-func-symbol-name func))))
|
|
|
|
|
(if (comp-args-p args)
|
|
|
|
|
(cl-loop for i below (comp-args-max args)
|
|
|
|
|
do (cl-incf (comp-sp))
|
2019-10-14 20:08:24 +00:00
|
|
|
|
(comp-emit `(set-par-to-local ,(comp-slot) ,i)))
|
2019-11-02 11:17:26 +00:00
|
|
|
|
(comp-emit-narg-prologue (comp-args-base-min args)
|
|
|
|
|
(comp-nargs-nonrest args)
|
|
|
|
|
(comp-nargs-rest args)))
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(comp-emit '(jump bb_0))
|
2019-09-07 14:35:07 +00:00
|
|
|
|
;; Body
|
2019-11-01 14:28:17 +00:00
|
|
|
|
(comp-bb-maybe-add 0 (comp-sp))
|
2019-10-13 08:36:22 +00:00
|
|
|
|
(cl-loop for next-bb = (pop (comp-limplify-pending-blocks comp-pass))
|
|
|
|
|
while next-bb
|
|
|
|
|
do (comp-limplify-block next-bb))
|
2019-10-27 17:14:33 +00:00
|
|
|
|
;; Sanity check against block duplication.
|
|
|
|
|
(cl-loop with addr-h = (make-hash-table)
|
|
|
|
|
for bb being the hash-value in (comp-func-blocks func)
|
|
|
|
|
for addr = (comp-block-addr bb)
|
2019-11-01 14:28:17 +00:00
|
|
|
|
when addr
|
|
|
|
|
do (cl-assert (null (gethash addr addr-h)))
|
|
|
|
|
(puthash addr t addr-h))
|
2019-09-07 14:35:07 +00:00
|
|
|
|
(comp-limplify-finalize-function func)))
|
|
|
|
|
|
2019-09-18 21:25:37 +00:00
|
|
|
|
(defun comp-add-func-to-ctxt (func)
|
|
|
|
|
"Add FUNC to the current compiler contex."
|
|
|
|
|
(puthash (comp-func-symbol-name func)
|
|
|
|
|
func
|
|
|
|
|
(comp-ctxt-funcs-h comp-ctxt)))
|
|
|
|
|
|
2019-09-22 13:42:49 +00:00
|
|
|
|
(defun comp-limplify (lap-funcs)
|
|
|
|
|
"Compute the LIMPLE ir for LAP-FUNCS.
|
2019-09-07 14:35:07 +00:00
|
|
|
|
Top level forms for the current context are rendered too."
|
2019-11-02 16:32:20 +00:00
|
|
|
|
(mapc #'comp-add-func-to-ctxt (mapcar #'comp-limplify-function lap-funcs))
|
|
|
|
|
(comp-add-func-to-ctxt (comp-limplify-top-level)))
|
2019-07-07 07:23:10 +00:00
|
|
|
|
|
2019-09-11 21:13:13 +00:00
|
|
|
|
|
|
|
|
|
;;; SSA pass specific code.
|
2019-09-14 08:52:57 +00:00
|
|
|
|
;; After limplification no edges are present between basic blocks and an
|
|
|
|
|
;; implicit phi is present for every slot at the beginning of every basic block.
|
|
|
|
|
;; This pass is responsible for building all the edges and replace all m-vars
|
|
|
|
|
;; plus placing the needed phis.
|
2019-09-14 16:12:16 +00:00
|
|
|
|
;; Because the number of phis placed is (supposed) to be the minimum necessary
|
2019-09-14 08:52:57 +00:00
|
|
|
|
;; this form is called 'minimal SSA form'.
|
|
|
|
|
;; This pass should be run every time basic blocks or mvar are shuffled.
|
2019-09-13 18:56:24 +00:00
|
|
|
|
|
2019-09-14 16:12:16 +00:00
|
|
|
|
(cl-defun make-comp-ssa-mvar (&key slot (constant nil const-vld) type)
|
|
|
|
|
(make--comp-mvar :id (funcall (comp-func-ssa-cnt-gen comp-func))
|
|
|
|
|
:slot slot :const-vld const-vld :constant constant
|
|
|
|
|
:type type))
|
|
|
|
|
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(defun comp-compute-edges ()
|
|
|
|
|
"Compute the basic block edges for the current function."
|
2019-09-14 08:52:57 +00:00
|
|
|
|
(cl-flet ((edge-add (&rest args)
|
|
|
|
|
(push
|
|
|
|
|
(apply #'make--comp-edge
|
|
|
|
|
:number (funcall (comp-func-edge-cnt-gen comp-func))
|
|
|
|
|
args)
|
|
|
|
|
(comp-func-edges comp-func))))
|
|
|
|
|
|
|
|
|
|
(cl-loop with blocks = (comp-func-blocks comp-func)
|
|
|
|
|
for bb being each hash-value of blocks
|
|
|
|
|
for last-insn = (car (last (comp-block-insns bb)))
|
2019-11-10 09:17:24 +00:00
|
|
|
|
for (op first second third forth) = last-insn
|
2019-09-21 17:13:11 +00:00
|
|
|
|
do (cl-case op
|
2019-09-14 08:52:57 +00:00
|
|
|
|
(jump
|
2019-09-16 18:11:05 +00:00
|
|
|
|
(edge-add :src bb :dst (gethash first blocks)))
|
2019-09-14 08:52:57 +00:00
|
|
|
|
(cond-jump
|
2019-09-16 18:11:05 +00:00
|
|
|
|
(edge-add :src bb :dst (gethash third blocks))
|
|
|
|
|
(edge-add :src bb :dst (gethash forth blocks)))
|
|
|
|
|
(cond-jump-narg-leq
|
|
|
|
|
(edge-add :src bb :dst (gethash second blocks))
|
|
|
|
|
(edge-add :src bb :dst (gethash third blocks)))
|
|
|
|
|
(push-handler
|
2019-11-10 09:17:24 +00:00
|
|
|
|
(edge-add :src bb :dst (gethash third blocks))
|
|
|
|
|
(edge-add :src bb :dst (gethash forth blocks)))
|
2019-09-21 17:13:11 +00:00
|
|
|
|
(return)
|
|
|
|
|
(otherwise
|
|
|
|
|
(error "Block %s does not end with a branch in func %s"
|
|
|
|
|
bb (comp-func-symbol-name comp-func))))
|
2019-09-14 08:52:57 +00:00
|
|
|
|
finally (progn
|
|
|
|
|
(setf (comp-func-edges comp-func)
|
|
|
|
|
(nreverse (comp-func-edges comp-func)))
|
|
|
|
|
;; Update edge refs into blocks.
|
|
|
|
|
(cl-loop for edge in (comp-func-edges comp-func)
|
|
|
|
|
do (push edge
|
|
|
|
|
(comp-block-out-edges (comp-edge-src edge)))
|
2019-10-14 20:08:24 +00:00
|
|
|
|
(push edge
|
2019-09-14 08:52:57 +00:00
|
|
|
|
(comp-block-in-edges (comp-edge-dst edge))))
|
|
|
|
|
(comp-log-edges comp-func)))))
|
2019-09-13 18:56:24 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-collect-rev-post-order (basic-block)
|
|
|
|
|
"Walk BASIC-BLOCK childs and return their name in reversed post-oder."
|
|
|
|
|
(let ((visited (make-hash-table))
|
|
|
|
|
(acc ()))
|
|
|
|
|
(cl-labels ((collect-rec (bb)
|
2019-09-14 08:13:38 +00:00
|
|
|
|
(let ((name (comp-block-name bb)))
|
|
|
|
|
(unless (gethash name visited)
|
|
|
|
|
(puthash name t visited)
|
|
|
|
|
(cl-loop for e in (comp-block-out-edges bb)
|
|
|
|
|
for dst-block = (comp-edge-dst e)
|
|
|
|
|
do (collect-rec dst-block))
|
|
|
|
|
(push name acc)))))
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(collect-rec basic-block)
|
|
|
|
|
acc)))
|
|
|
|
|
|
|
|
|
|
(defun comp-compute-dominator-tree ()
|
|
|
|
|
"Compute immediate dominators for each basic block in current function."
|
2019-09-14 12:51:43 +00:00
|
|
|
|
;; Originally based on: "A Simple, Fast Dominance Algorithm"
|
2019-09-13 18:56:24 +00:00
|
|
|
|
;; Cooper, Keith D.; Harvey, Timothy J.; Kennedy, Ken (2001).
|
|
|
|
|
(cl-flet ((intersect (b1 b2)
|
|
|
|
|
(let ((finger1 (comp-block-post-num b1))
|
|
|
|
|
(finger2 (comp-block-post-num b2)))
|
|
|
|
|
(while (not (= finger1 finger2))
|
|
|
|
|
(while (< finger1 finger2)
|
|
|
|
|
(setf b1 (comp-block-dom b1))
|
|
|
|
|
(setf finger1 (comp-block-post-num b1)))
|
|
|
|
|
(while (< finger2 finger1)
|
|
|
|
|
(setf b2 (comp-block-dom b2))
|
|
|
|
|
(setf finger2 (comp-block-post-num b2))))
|
|
|
|
|
b1))
|
|
|
|
|
(first-processed (l)
|
|
|
|
|
(if-let ((p (cl-find-if (lambda (p) (comp-block-dom p)) l)))
|
|
|
|
|
p
|
|
|
|
|
(error "Cant't find first preprocessed"))))
|
2019-09-14 10:39:53 +00:00
|
|
|
|
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(when-let ((blocks (comp-func-blocks comp-func))
|
|
|
|
|
(entry (gethash 'entry blocks))
|
2019-09-14 08:52:57 +00:00
|
|
|
|
;; No point to go on if the only bb is 'entry'.
|
2019-09-16 20:18:19 +00:00
|
|
|
|
(bb1 (gethash 'bb_1 blocks)))
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(cl-loop with rev-bb-list = (comp-collect-rev-post-order entry)
|
|
|
|
|
with changed = t
|
|
|
|
|
while changed
|
|
|
|
|
initially (progn
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log "Computing dominator tree...\n" 2)
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(setf (comp-block-dom entry) entry)
|
|
|
|
|
;; Set the post order number.
|
|
|
|
|
(cl-loop for name in (reverse rev-bb-list)
|
|
|
|
|
for b = (gethash name blocks)
|
|
|
|
|
for i from 0
|
|
|
|
|
do (setf (comp-block-post-num b) i)))
|
|
|
|
|
do (cl-loop
|
|
|
|
|
for name in (cdr rev-bb-list)
|
|
|
|
|
for b = (gethash name blocks)
|
|
|
|
|
for preds = (comp-block-preds b)
|
2019-09-14 08:13:38 +00:00
|
|
|
|
for new-idom = (first-processed preds)
|
2019-09-13 18:56:24 +00:00
|
|
|
|
initially (setf changed nil)
|
2019-09-14 08:13:38 +00:00
|
|
|
|
do (cl-loop for p in (delq new-idom preds)
|
2019-09-13 18:56:24 +00:00
|
|
|
|
when (comp-block-dom p)
|
2019-09-14 08:13:38 +00:00
|
|
|
|
do (setf new-idom (intersect p new-idom)))
|
|
|
|
|
unless (eq (comp-block-dom b) new-idom)
|
2019-10-14 20:08:24 +00:00
|
|
|
|
do (setf (comp-block-dom b) new-idom)
|
|
|
|
|
(setf changed t))))))
|
2019-09-14 08:13:38 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-compute-dominator-frontiers ()
|
2019-09-14 12:51:43 +00:00
|
|
|
|
;; Originally based on: "A Simple, Fast Dominance Algorithm"
|
2019-09-14 08:13:38 +00:00
|
|
|
|
;; Cooper, Keith D.; Harvey, Timothy J.; Kennedy, Ken (2001).
|
|
|
|
|
(cl-loop with blocks = (comp-func-blocks comp-func)
|
|
|
|
|
for b-name being each hash-keys of blocks
|
|
|
|
|
using (hash-value b)
|
|
|
|
|
for preds = (comp-block-preds b)
|
|
|
|
|
when (>= (length preds) 2) ; All joins
|
|
|
|
|
do (cl-loop for p in preds
|
|
|
|
|
for runner = p
|
|
|
|
|
do (while (not (eq runner (comp-block-dom b)))
|
|
|
|
|
(puthash b-name b (comp-block-df runner))
|
|
|
|
|
(setf runner (comp-block-dom runner))))))
|
|
|
|
|
|
|
|
|
|
(defun comp-log-block-info ()
|
|
|
|
|
"Log basic blocks info for the current function."
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(maphash (lambda (name bb)
|
2019-09-14 08:52:57 +00:00
|
|
|
|
(let ((dom (comp-block-dom bb))
|
|
|
|
|
(df (comp-block-df bb)))
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log (format "block: %s idom: %s DF %s\n"
|
|
|
|
|
name
|
|
|
|
|
(when dom (comp-block-name dom))
|
|
|
|
|
(cl-loop for b being each hash-keys of df
|
|
|
|
|
collect b))
|
|
|
|
|
3)))
|
2019-09-13 18:56:24 +00:00
|
|
|
|
(comp-func-blocks comp-func)))
|
2019-09-11 21:13:13 +00:00
|
|
|
|
|
2019-09-14 10:39:53 +00:00
|
|
|
|
(defun comp-place-phis ()
|
|
|
|
|
"Place phi insns into the current function."
|
2019-09-14 12:51:43 +00:00
|
|
|
|
;; Originally based on: Static Single Assignment Book
|
2019-09-14 10:39:53 +00:00
|
|
|
|
;; Algorithm 3.1: Standard algorithm for inserting phi-functions
|
|
|
|
|
(cl-flet ((add-phi (slot-n bb)
|
|
|
|
|
;; Add a phi func for slot SLOT-N at the top of BB.
|
|
|
|
|
(push `(phi ,slot-n) (comp-block-insns bb)))
|
|
|
|
|
(slot-assigned-p (slot-n bb)
|
|
|
|
|
;; Return t if a SLOT-N was assigned within BB.
|
|
|
|
|
(cl-loop for insn in (comp-block-insns bb)
|
2019-09-14 15:00:16 +00:00
|
|
|
|
when (and (comp-assign-op-p (car insn))
|
2019-09-14 10:39:53 +00:00
|
|
|
|
(= slot-n (comp-mvar-slot (cadr insn))))
|
2019-11-02 13:34:31 +00:00
|
|
|
|
return t)))
|
2019-09-14 10:39:53 +00:00
|
|
|
|
|
|
|
|
|
(cl-loop for i from 0 below (comp-func-frame-size comp-func)
|
|
|
|
|
;; List of blocks with a definition of mvar i
|
2019-09-16 23:01:34 +00:00
|
|
|
|
for defs-v = (cl-loop with blocks = (comp-func-blocks comp-func)
|
2019-09-14 10:39:53 +00:00
|
|
|
|
for b being each hash-value of blocks
|
|
|
|
|
when (slot-assigned-p i b)
|
|
|
|
|
collect b)
|
|
|
|
|
;; Set of basic blocks where phi is added.
|
2019-09-16 23:01:34 +00:00
|
|
|
|
for f = ()
|
2019-09-14 10:39:53 +00:00
|
|
|
|
;; Worklist, set of basic blocks that contain definitions of v.
|
2019-09-16 23:01:34 +00:00
|
|
|
|
for w = defs-v
|
2019-09-14 10:39:53 +00:00
|
|
|
|
do
|
|
|
|
|
(while w
|
|
|
|
|
(let ((x (pop w)))
|
|
|
|
|
(cl-loop for y being each hash-value of (comp-block-df x)
|
|
|
|
|
unless (cl-find y f)
|
2019-10-14 20:08:24 +00:00
|
|
|
|
do (add-phi i y)
|
|
|
|
|
(push y f)
|
|
|
|
|
;; Adding a phi implies mentioning the
|
|
|
|
|
;; corresponding slot so in case adjust w.
|
|
|
|
|
(unless (cl-find y defs-v)
|
|
|
|
|
(push y w))))))))
|
2019-09-14 10:39:53 +00:00
|
|
|
|
|
2019-09-14 15:55:03 +00:00
|
|
|
|
(defun comp-dom-tree-walker (bb pre-lambda post-lambda)
|
2019-09-14 12:51:43 +00:00
|
|
|
|
"Dominator tree walker function starting from basic block BB.
|
|
|
|
|
PRE-LAMBDA and POST-LAMBDA are called in pre or post-order if non nil."
|
|
|
|
|
(when pre-lambda
|
|
|
|
|
(funcall pre-lambda bb))
|
|
|
|
|
(when-let ((out-edges (comp-block-out-edges bb)))
|
|
|
|
|
(cl-loop for ed in out-edges
|
|
|
|
|
for child = (comp-edge-dst ed)
|
|
|
|
|
when (eq bb (comp-block-dom child))
|
2019-09-14 15:00:16 +00:00
|
|
|
|
;; Current block is the immediate dominator then recur.
|
2019-09-14 15:55:03 +00:00
|
|
|
|
do (comp-dom-tree-walker child pre-lambda post-lambda)))
|
2019-09-14 12:51:43 +00:00
|
|
|
|
(when post-lambda
|
|
|
|
|
(funcall post-lambda bb)))
|
|
|
|
|
|
2019-09-14 15:00:16 +00:00
|
|
|
|
(cl-defstruct (comp-ssa (:copier nil))
|
|
|
|
|
"Support structure used while SSA renaming."
|
2019-09-15 10:07:04 +00:00
|
|
|
|
(frame (comp-new-frame (comp-func-frame-size comp-func) t) :type vector
|
2019-09-14 15:00:16 +00:00
|
|
|
|
:documentation "Vector of mvars."))
|
|
|
|
|
|
2019-09-16 20:18:58 +00:00
|
|
|
|
(defun comp-ssa-rename-insn (insn frame)
|
|
|
|
|
(dotimes (slot-n (comp-func-frame-size comp-func))
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(cl-flet ((targetp (x)
|
2019-09-16 20:18:58 +00:00
|
|
|
|
;; Ret t if x is an mvar and target the correct slot number.
|
|
|
|
|
(and (comp-mvar-p x)
|
|
|
|
|
(eql slot-n (comp-mvar-slot x))))
|
|
|
|
|
(new-lvalue ()
|
|
|
|
|
;; If is an assignment make a new mvar and put it as l-value.
|
|
|
|
|
(let ((mvar (make-comp-ssa-mvar :slot slot-n)))
|
|
|
|
|
(setf (aref frame slot-n) mvar)
|
|
|
|
|
(setf (cadr insn) mvar))))
|
|
|
|
|
(pcase insn
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(`(,(pred comp-assign-op-p) ,(pred targetp) . ,_)
|
2019-09-16 20:18:58 +00:00
|
|
|
|
(let ((mvar (aref frame slot-n)))
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(setcdr insn (cl-nsubst-if mvar #'targetp (cdr insn))))
|
2019-09-16 20:18:58 +00:00
|
|
|
|
(new-lvalue))
|
|
|
|
|
(`(phi ,n)
|
|
|
|
|
(when (equal n slot-n)
|
|
|
|
|
(new-lvalue)))
|
|
|
|
|
(_
|
|
|
|
|
(let ((mvar (aref frame slot-n)))
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(setcdr insn (cl-nsubst-if mvar #'targetp (cdr insn)))))))))
|
2019-09-16 20:18:58 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-ssa-rename ()
|
|
|
|
|
"Entry point to rename SSA within the current function."
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log "Renaming\n" 2)
|
2019-09-16 20:18:58 +00:00
|
|
|
|
(let ((frame-size (comp-func-frame-size comp-func))
|
|
|
|
|
(visited (make-hash-table)))
|
|
|
|
|
(cl-labels ((ssa-rename-rec (bb in-frame)
|
|
|
|
|
(unless (gethash bb visited)
|
|
|
|
|
(puthash bb t visited)
|
|
|
|
|
(cl-loop for insn in (comp-block-insns bb)
|
|
|
|
|
do (comp-ssa-rename-insn insn in-frame))
|
2019-09-16 23:01:34 +00:00
|
|
|
|
(setf (comp-block-final-frame bb)
|
|
|
|
|
(copy-sequence in-frame))
|
2019-09-16 20:18:58 +00:00
|
|
|
|
(when-let ((out-edges (comp-block-out-edges bb)))
|
|
|
|
|
(cl-loop for ed in out-edges
|
|
|
|
|
for child = (comp-edge-dst ed)
|
|
|
|
|
;; Provide a copy of the same frame to all childs.
|
|
|
|
|
do (ssa-rename-rec child (copy-sequence in-frame)))))))
|
|
|
|
|
|
|
|
|
|
(ssa-rename-rec (gethash 'entry (comp-func-blocks comp-func))
|
|
|
|
|
(comp-new-frame frame-size t)))))
|
2019-09-14 15:55:03 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-finalize-phis ()
|
|
|
|
|
"Fixup r-values into phis in all basic blocks."
|
|
|
|
|
(cl-flet ((finalize-phi (args b)
|
|
|
|
|
;; Concatenate into args all incoming mvars for this phi.
|
|
|
|
|
(setcdr args
|
|
|
|
|
(cl-loop with slot-n = (comp-mvar-slot (car args))
|
|
|
|
|
for e in (comp-block-in-edges b)
|
|
|
|
|
for b = (comp-edge-src e)
|
|
|
|
|
for in-frame = (comp-block-final-frame b)
|
|
|
|
|
collect (aref in-frame slot-n))) ))
|
|
|
|
|
|
|
|
|
|
(cl-loop for b being each hash-value of (comp-func-blocks comp-func)
|
|
|
|
|
do (cl-loop for (op . args) in (comp-block-insns b)
|
|
|
|
|
when (eq op 'phi)
|
|
|
|
|
do (finalize-phi args b)))))
|
2019-09-14 15:00:16 +00:00
|
|
|
|
|
2019-09-22 13:42:49 +00:00
|
|
|
|
(defun comp-ssa (_)
|
2019-09-14 08:52:57 +00:00
|
|
|
|
"Port FUNCS into mininal SSA form."
|
2019-09-22 13:42:49 +00:00
|
|
|
|
(maphash (lambda (_ f)
|
|
|
|
|
(let ((comp-func f))
|
|
|
|
|
;; TODO: if this is run more than once we should clean all CFG
|
|
|
|
|
;; data including phis here.
|
|
|
|
|
(comp-func-reset-generators comp-func)
|
|
|
|
|
(comp-compute-edges)
|
|
|
|
|
(comp-compute-dominator-tree)
|
|
|
|
|
(comp-compute-dominator-frontiers)
|
|
|
|
|
(comp-log-block-info)
|
|
|
|
|
(comp-place-phis)
|
|
|
|
|
(comp-ssa-rename)
|
|
|
|
|
(comp-finalize-phis)
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log-func comp-func 3)))
|
2019-09-22 13:42:49 +00:00
|
|
|
|
(comp-ctxt-funcs-h comp-ctxt)))
|
2019-09-15 10:31:44 +00:00
|
|
|
|
|
2019-09-15 12:43:30 +00:00
|
|
|
|
|
|
|
|
|
;;; propagate pass specific code.
|
|
|
|
|
;; A very basic propagation pass follows.
|
|
|
|
|
|
2019-09-30 15:13:07 +00:00
|
|
|
|
(defsubst comp-strict-type-of (obj)
|
|
|
|
|
"Given OBJ return its type understanding fixnums."
|
|
|
|
|
;; Should be certainly smarter but now we take advantages just from fixnums.
|
|
|
|
|
(if (fixnump obj)
|
|
|
|
|
'fixnum
|
|
|
|
|
(type-of obj)))
|
|
|
|
|
|
2019-11-09 15:22:07 +00:00
|
|
|
|
(defun comp-copy-insn (insn)
|
|
|
|
|
"Deep copy INSN."
|
2019-11-10 13:25:17 +00:00
|
|
|
|
;; Adapted from `copy-tree'.
|
|
|
|
|
(if (consp insn)
|
|
|
|
|
(let (result)
|
|
|
|
|
(while (consp insn)
|
|
|
|
|
(let ((newcar (car insn)))
|
|
|
|
|
(if (or (consp (car insn)) (comp-mvar-p (car insn)))
|
|
|
|
|
(setq newcar (comp-copy-insn (car insn))))
|
|
|
|
|
(push newcar result))
|
|
|
|
|
(setq insn (cdr insn)))
|
|
|
|
|
(nconc (nreverse result)
|
|
|
|
|
(if (comp-mvar-p insn) (comp-copy-insn insn) insn)))
|
|
|
|
|
(if (comp-mvar-p insn)
|
|
|
|
|
(copy-comp-mvar insn)
|
|
|
|
|
insn)))
|
2019-11-09 15:22:07 +00:00
|
|
|
|
|
2019-09-15 12:43:30 +00:00
|
|
|
|
(defun comp-basic-const-propagate ()
|
|
|
|
|
"Propagate simple constants for setimm operands.
|
|
|
|
|
This can run just once."
|
2019-09-30 15:13:07 +00:00
|
|
|
|
(cl-loop
|
|
|
|
|
for b being each hash-value of (comp-func-blocks comp-func)
|
|
|
|
|
do (cl-loop
|
|
|
|
|
for insn in (comp-block-insns b)
|
|
|
|
|
do (pcase insn
|
|
|
|
|
(`(setimm ,lval ,_ ,v)
|
|
|
|
|
(setf (comp-mvar-const-vld lval) t)
|
|
|
|
|
(setf (comp-mvar-constant lval) v)
|
|
|
|
|
(setf (comp-mvar-type lval) (comp-strict-type-of v)))))))
|
2019-09-15 12:43:30 +00:00
|
|
|
|
|
2019-09-16 20:18:58 +00:00
|
|
|
|
(defsubst comp-mvar-propagate (lval rval)
|
|
|
|
|
"Propagate into LVAL properties of RVAL."
|
|
|
|
|
(setf (comp-mvar-const-vld lval) (comp-mvar-const-vld rval))
|
|
|
|
|
(setf (comp-mvar-constant lval) (comp-mvar-constant rval))
|
|
|
|
|
(setf (comp-mvar-type lval) (comp-mvar-type rval)))
|
|
|
|
|
|
2019-09-15 12:43:30 +00:00
|
|
|
|
(defun comp-propagate-insn (insn)
|
2019-11-09 15:22:07 +00:00
|
|
|
|
"Propagate within INSN."
|
2019-09-15 12:43:30 +00:00
|
|
|
|
(pcase insn
|
|
|
|
|
(`(set ,lval ,rval)
|
|
|
|
|
(pcase rval
|
2019-09-22 15:11:53 +00:00
|
|
|
|
(`(,(or 'call 'direct-call) ,f . ,_)
|
2019-09-15 12:43:30 +00:00
|
|
|
|
(setf (comp-mvar-type lval)
|
2019-10-03 20:15:43 +00:00
|
|
|
|
(alist-get f comp-known-ret-types)))
|
2019-09-22 15:11:53 +00:00
|
|
|
|
(`(,(or 'callref 'direct-callref) ,f . ,args)
|
2019-09-15 12:43:30 +00:00
|
|
|
|
(cl-loop for v in args
|
|
|
|
|
do (setf (comp-mvar-ref v) t))
|
|
|
|
|
(setf (comp-mvar-type lval)
|
2019-10-03 20:15:43 +00:00
|
|
|
|
(alist-get f comp-known-ret-types)))
|
2019-09-15 12:43:30 +00:00
|
|
|
|
(_
|
|
|
|
|
(comp-mvar-propagate lval rval))))
|
|
|
|
|
(`(phi ,lval . ,rest)
|
|
|
|
|
;; Const prop here.
|
|
|
|
|
(when (and (cl-every #'comp-mvar-const-vld rest)
|
|
|
|
|
(cl-reduce #'equal (mapcar #'comp-mvar-constant rest)))
|
|
|
|
|
(setf (comp-mvar-constant lval) (comp-mvar-constant (car rest))))
|
|
|
|
|
;; Type propagation.
|
|
|
|
|
;; FIXME: checking for type equality is not sufficient cause does not
|
|
|
|
|
;; account type hierarchy!!
|
|
|
|
|
(when (cl-reduce #'eq (mapcar #'comp-mvar-type rest))
|
|
|
|
|
(setf (comp-mvar-type lval) (comp-mvar-type (car rest))))
|
|
|
|
|
;; Reference propagation.
|
2019-11-07 20:40:51 +00:00
|
|
|
|
(let ((operands (cons lval rest)))
|
|
|
|
|
(when (cl-some #'comp-mvar-ref operands)
|
2019-11-09 14:46:44 +00:00
|
|
|
|
(mapc (lambda (x) (setf (comp-mvar-ref x) t)) operands))))))
|
2019-09-15 12:43:30 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-propagate* ()
|
2019-11-09 15:22:07 +00:00
|
|
|
|
"Propagate for set* and phi operands.
|
|
|
|
|
Return t if something was changed."
|
|
|
|
|
(cl-loop with modified = nil
|
|
|
|
|
for b being each hash-value of (comp-func-blocks comp-func)
|
2019-09-15 12:43:30 +00:00
|
|
|
|
do (cl-loop for insn in (comp-block-insns b)
|
2019-11-09 15:22:07 +00:00
|
|
|
|
for orig-insn = (unless modified ; Save consing after 1th change.
|
|
|
|
|
(comp-copy-insn insn))
|
|
|
|
|
do (comp-propagate-insn insn)
|
|
|
|
|
when (and (null modified) (not (equal insn orig-insn)))
|
|
|
|
|
do (setf modified t))
|
|
|
|
|
finally (cl-return modified)))
|
2019-09-15 12:43:30 +00:00
|
|
|
|
|
2019-09-22 13:42:49 +00:00
|
|
|
|
(defun comp-propagate (_)
|
|
|
|
|
(maphash (lambda (_ f)
|
|
|
|
|
(let ((comp-func f))
|
|
|
|
|
(comp-basic-const-propagate)
|
2019-11-09 15:22:07 +00:00
|
|
|
|
(cl-loop
|
|
|
|
|
for i from 1
|
|
|
|
|
while (comp-propagate*)
|
2019-11-09 15:56:55 +00:00
|
|
|
|
finally (comp-log (format "Propagation run %d times\n" i) 2))
|
|
|
|
|
(comp-log-func comp-func 3)))
|
2019-09-22 13:42:49 +00:00
|
|
|
|
(comp-ctxt-funcs-h comp-ctxt)))
|
2019-09-11 21:13:13 +00:00
|
|
|
|
|
2019-09-18 09:30:23 +00:00
|
|
|
|
|
|
|
|
|
;;; Call optimizer pass specific code.
|
2019-09-23 07:13:46 +00:00
|
|
|
|
;; This pass is responsible for the following optimizations:
|
|
|
|
|
;; - Call to subrs that are in defined in the C source and are passing through
|
|
|
|
|
;; funcall trampoline gets optimized into normal indirect calls.
|
|
|
|
|
;; This makes effectively this calls equivalent to all the subrs that got
|
|
|
|
|
;; dedicated byte-code ops.
|
|
|
|
|
;; Triggered at comp-speed >= 2.
|
|
|
|
|
;; - Recursive calls gets optimized into direct calls.
|
|
|
|
|
;; Triggered at comp-speed >= 2.
|
|
|
|
|
;; - Intra compilation unit procedure calls gets optimized into direct calls.
|
|
|
|
|
;; This can be a big win and even allow gcc to inline but does not make
|
|
|
|
|
;; function in the compilation unit re-definable safely without recompiling
|
|
|
|
|
;; the full compilation unit.
|
|
|
|
|
;; For this reason this is triggered only at comp-speed == 3.
|
2019-09-18 09:30:23 +00:00
|
|
|
|
|
2019-09-18 23:25:46 +00:00
|
|
|
|
(defun comp-call-optim-form-call (callee args self)
|
2019-09-18 21:25:37 +00:00
|
|
|
|
""
|
2019-09-21 09:38:40 +00:00
|
|
|
|
(cl-flet ((fill-args (args total)
|
|
|
|
|
;; Fill missing args to reach TOTAL
|
|
|
|
|
(append args (cl-loop repeat (- total (length args))
|
2019-09-22 17:00:28 +00:00
|
|
|
|
collect (make-comp-mvar :constant nil))))
|
|
|
|
|
(clean-args-ref (args)
|
|
|
|
|
;; Clean-up the ref slot in all args
|
|
|
|
|
(mapc (lambda (arg)
|
|
|
|
|
(setf (comp-mvar-ref arg) nil))
|
|
|
|
|
args)
|
|
|
|
|
args))
|
2019-09-21 09:38:40 +00:00
|
|
|
|
(when (symbolp callee) ; Do nothing if callee is a byte compiled func.
|
|
|
|
|
(let* ((f (symbol-function callee))
|
|
|
|
|
(subrp (subrp f))
|
|
|
|
|
(callee-in-unit (gethash callee
|
|
|
|
|
(comp-ctxt-funcs-h comp-ctxt))))
|
2019-09-23 08:51:40 +00:00
|
|
|
|
(cond
|
2019-09-26 10:11:13 +00:00
|
|
|
|
((and subrp (not (subr-native-elisp-p f)))
|
2019-09-23 08:51:40 +00:00
|
|
|
|
;; Trampoline removal.
|
|
|
|
|
(let* ((maxarg (cdr (subr-arity f)))
|
|
|
|
|
(call-type (if (if subrp
|
|
|
|
|
(not (numberp maxarg))
|
|
|
|
|
(comp-nargs-p callee-in-unit))
|
|
|
|
|
'callref
|
|
|
|
|
'call))
|
|
|
|
|
(args (if (eq call-type 'callref)
|
|
|
|
|
args
|
|
|
|
|
(fill-args args maxarg))))
|
|
|
|
|
(comp-add-subr-to-relocs callee)
|
|
|
|
|
`(,call-type ,callee ,@(clean-args-ref args))))
|
|
|
|
|
;; Intra compilation unit procedure call optimization.
|
|
|
|
|
;; Attention speed 3 triggers that for non self calls too!!
|
|
|
|
|
((or (eq callee self)
|
|
|
|
|
(and (>= comp-speed 3)
|
|
|
|
|
callee-in-unit))
|
|
|
|
|
(let* ((func-args (comp-func-args callee-in-unit))
|
|
|
|
|
(nargs (comp-nargs-p func-args))
|
|
|
|
|
(call-type (if nargs 'direct-callref 'direct-call))
|
|
|
|
|
(args (if (eq call-type 'direct-callref)
|
|
|
|
|
args
|
|
|
|
|
(fill-args args (comp-args-max func-args)))))
|
2019-09-23 09:41:36 +00:00
|
|
|
|
`(,call-type ,callee ,@(clean-args-ref args))))
|
|
|
|
|
((comp-type-hint-p callee)
|
|
|
|
|
`(call ,callee ,@args)))))))
|
2019-09-18 21:25:37 +00:00
|
|
|
|
|
2019-09-22 13:42:49 +00:00
|
|
|
|
(defun comp-call-optim-func ()
|
|
|
|
|
"Perform trampoline call optimization for the current function."
|
2019-09-18 09:30:23 +00:00
|
|
|
|
(cl-loop
|
2019-09-22 13:42:49 +00:00
|
|
|
|
with self = (comp-func-symbol-name comp-func)
|
|
|
|
|
for b being each hash-value of (comp-func-blocks comp-func)
|
2019-09-18 09:30:23 +00:00
|
|
|
|
do (cl-loop
|
2019-09-22 13:42:49 +00:00
|
|
|
|
for insn-cell on (comp-block-insns b)
|
|
|
|
|
for insn = (car insn-cell)
|
|
|
|
|
do (pcase insn
|
|
|
|
|
(`(set ,lval (callref funcall ,f . ,rest))
|
|
|
|
|
(when-let ((new-form (comp-call-optim-form-call
|
|
|
|
|
(comp-mvar-constant f) rest self)))
|
|
|
|
|
(setcar insn-cell `(set ,lval ,new-form))))
|
|
|
|
|
(`(callref funcall ,f . ,rest)
|
|
|
|
|
(when-let ((new-form (comp-call-optim-form-call
|
|
|
|
|
(comp-mvar-constant f) rest self)))
|
2019-10-05 14:26:52 +00:00
|
|
|
|
(setcar insn-cell new-form)))))))
|
2019-09-22 13:42:49 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-call-optim (_)
|
|
|
|
|
"Given FUNCS try to avoid funcall trampoline usage when possible."
|
|
|
|
|
(when (>= comp-speed 2)
|
|
|
|
|
(maphash (lambda (_ f)
|
|
|
|
|
(let ((comp-func f))
|
|
|
|
|
(comp-call-optim-func)))
|
|
|
|
|
(comp-ctxt-funcs-h comp-ctxt))))
|
2019-09-18 09:30:23 +00:00
|
|
|
|
|
2019-09-22 16:49:11 +00:00
|
|
|
|
|
|
|
|
|
;;; Dead code elimination pass specific code.
|
|
|
|
|
;; This simple pass try to eliminate insns became useful after propagation.
|
|
|
|
|
;; Even if gcc would take care of this is good to perform this here
|
|
|
|
|
;; in the hope of removing memory references (remember that most lisp
|
|
|
|
|
;; objects are loaded from the reloc array).
|
2019-09-23 09:41:36 +00:00
|
|
|
|
;;
|
2019-09-22 16:49:11 +00:00
|
|
|
|
;; This pass can be run as last optim.
|
|
|
|
|
|
|
|
|
|
(defun comp-collect-mvar-ids (insn)
|
|
|
|
|
"Collect the mvar unique identifiers into INSN."
|
|
|
|
|
(cl-loop for x in insn
|
|
|
|
|
if (consp x)
|
|
|
|
|
append (comp-collect-mvar-ids x)
|
|
|
|
|
else
|
|
|
|
|
when (comp-mvar-p x)
|
|
|
|
|
collect (comp-mvar-id x)))
|
|
|
|
|
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(defun comp-dead-assignments-func ()
|
|
|
|
|
"Clean-up dead assignments into current function."
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(let ((l-vals ())
|
|
|
|
|
(r-vals ()))
|
|
|
|
|
;; Collect used r and l values.
|
|
|
|
|
(cl-loop
|
|
|
|
|
for b being each hash-value of (comp-func-blocks comp-func)
|
|
|
|
|
do (cl-loop
|
|
|
|
|
for insn in (comp-block-insns b)
|
|
|
|
|
for (op arg0 . rest) = insn
|
|
|
|
|
if (comp-set-op-p op)
|
|
|
|
|
do (push (comp-mvar-id arg0) l-vals)
|
2019-10-14 20:08:24 +00:00
|
|
|
|
(setf r-vals (nconc (comp-collect-mvar-ids rest) r-vals))
|
2019-09-22 16:49:11 +00:00
|
|
|
|
else
|
|
|
|
|
do (setf r-vals (nconc (comp-collect-mvar-ids insn) r-vals))))
|
|
|
|
|
;; Every l-value appearing that does not appear as r-value has no right to
|
|
|
|
|
;; exist and gets nuked.
|
|
|
|
|
(let ((nuke-list (cl-set-difference l-vals r-vals)))
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log (format "Function %s\nl-vals %s\nr-vals %s\nNuking ids: %s\n"
|
|
|
|
|
(comp-func-symbol-name comp-func)
|
|
|
|
|
l-vals
|
|
|
|
|
r-vals
|
|
|
|
|
nuke-list)
|
|
|
|
|
3)
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(cl-loop
|
|
|
|
|
for b being each hash-value of (comp-func-blocks comp-func)
|
|
|
|
|
do (cl-loop
|
|
|
|
|
for insn-cell on (comp-block-insns b)
|
|
|
|
|
for insn = (car insn-cell)
|
|
|
|
|
for (op arg0 rest) = insn
|
|
|
|
|
when (and (comp-set-op-p op)
|
|
|
|
|
(member (comp-mvar-id arg0) nuke-list))
|
2019-10-14 20:08:24 +00:00
|
|
|
|
do (setcar insn-cell
|
|
|
|
|
(if (comp-limple-insn-call-p rest)
|
|
|
|
|
rest
|
|
|
|
|
`(comment ,(format "optimized out: %s"
|
|
|
|
|
insn)))))))))
|
2019-09-22 16:49:11 +00:00
|
|
|
|
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(defun comp-remove-type-hints-func ()
|
|
|
|
|
"Remove type hints from the current function.
|
|
|
|
|
These are substituted with normals 'set'."
|
|
|
|
|
(cl-loop
|
|
|
|
|
for b being each hash-value of (comp-func-blocks comp-func)
|
|
|
|
|
do (cl-loop
|
|
|
|
|
for insn-cell on (comp-block-insns b)
|
|
|
|
|
for insn = (car insn-cell)
|
|
|
|
|
do (pcase insn
|
|
|
|
|
(`(set ,l-val (call ,(pred comp-type-hint-p) ,r-val))
|
|
|
|
|
(setcar insn-cell `(set ,l-val ,r-val)))))))
|
|
|
|
|
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(defun comp-dead-code (_)
|
|
|
|
|
"Dead code elimination."
|
|
|
|
|
(when (>= comp-speed 2)
|
|
|
|
|
(maphash (lambda (_ f)
|
|
|
|
|
(let ((comp-func f))
|
2019-09-23 09:41:36 +00:00
|
|
|
|
(comp-dead-assignments-func)
|
|
|
|
|
(comp-remove-type-hints-func)
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log-func comp-func 3)))
|
2019-09-22 16:49:11 +00:00
|
|
|
|
(comp-ctxt-funcs-h comp-ctxt))))
|
|
|
|
|
|
2019-08-18 15:17:56 +00:00
|
|
|
|
|
2019-09-07 06:57:07 +00:00
|
|
|
|
;;; Final pass specific code.
|
2019-08-18 15:17:56 +00:00
|
|
|
|
|
|
|
|
|
(defun comp-compile-ctxt-to-file (name)
|
2019-09-07 06:57:07 +00:00
|
|
|
|
"Compile as native code the current context naming it NAME.
|
2019-09-22 16:49:11 +00:00
|
|
|
|
Prepare every function for final compilation and drive the C back-end."
|
2019-08-18 15:17:56 +00:00
|
|
|
|
(cl-assert (= (length (comp-ctxt-data-relocs-l comp-ctxt))
|
|
|
|
|
(hash-table-count (comp-ctxt-data-relocs-idx comp-ctxt))))
|
|
|
|
|
(comp--compile-ctxt-to-file name))
|
|
|
|
|
|
2019-09-18 21:25:37 +00:00
|
|
|
|
(defun comp-final (_)
|
|
|
|
|
"Final pass driving DATA into the C back-end for code emission."
|
2019-09-07 06:57:07 +00:00
|
|
|
|
(let (compile-result)
|
|
|
|
|
(comp--init-ctxt)
|
|
|
|
|
(unwind-protect
|
2019-09-18 21:25:37 +00:00
|
|
|
|
(setq compile-result
|
|
|
|
|
(comp-compile-ctxt-to-file (comp-ctxt-output comp-ctxt)))
|
2019-09-07 06:57:07 +00:00
|
|
|
|
(and (comp--release-ctxt)
|
|
|
|
|
compile-result))))
|
2019-08-18 15:17:56 +00:00
|
|
|
|
|
2019-07-21 11:57:51 +00:00
|
|
|
|
|
2019-09-23 09:41:36 +00:00
|
|
|
|
;;; Compiler type hints.
|
|
|
|
|
;; These are public entry points be used in user code to give comp suggestion
|
|
|
|
|
;; about types.
|
2019-09-23 10:45:06 +00:00
|
|
|
|
;; These can be used to implement CL style 'the', 'declare' or something like.
|
|
|
|
|
;; Note: types will propagates.
|
2019-09-23 09:41:36 +00:00
|
|
|
|
;; WARNING: At speed >= 2 type checking is not performed anymore and suggestions
|
|
|
|
|
;; are assumed just to be true. Use with extreme caution...
|
|
|
|
|
|
|
|
|
|
(defun comp-hint-fixnum (x)
|
|
|
|
|
(cl-assert (fixnump x)))
|
|
|
|
|
|
|
|
|
|
(defun comp-hint-cons (x)
|
|
|
|
|
(cl-assert (consp x)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Compiler entry points.
|
2019-07-21 11:57:51 +00:00
|
|
|
|
|
2019-10-21 07:53:00 +00:00
|
|
|
|
;;;###autoload
|
2019-09-07 06:18:08 +00:00
|
|
|
|
(defun native-compile (input)
|
|
|
|
|
"Compile INPUT into native code.
|
|
|
|
|
This is the entrypoint for the Emacs Lisp native compiler.
|
2019-09-07 09:17:02 +00:00
|
|
|
|
If INPUT is a symbol, native-compile its function definition.
|
2019-11-02 11:34:09 +00:00
|
|
|
|
If INPUT is a string, use it as the file path to be native compiled.
|
|
|
|
|
Return the compilation unit filename."
|
2019-09-07 06:57:07 +00:00
|
|
|
|
(unless (or (symbolp input)
|
|
|
|
|
(stringp input))
|
2019-09-07 09:17:02 +00:00
|
|
|
|
(error "Trying to native compile something not a symbol function or file"))
|
2019-09-07 06:18:08 +00:00
|
|
|
|
(let ((data input)
|
2019-09-23 09:48:19 +00:00
|
|
|
|
(comp-native-compiling t)
|
2019-09-29 09:05:54 +00:00
|
|
|
|
(comp-ctxt (make-comp-ctxt
|
|
|
|
|
:output (if (symbolp input)
|
|
|
|
|
(symbol-name input)
|
|
|
|
|
(file-name-sans-extension (expand-file-name input))))))
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log "\n\n" 1)
|
2019-09-07 06:18:08 +00:00
|
|
|
|
(mapc (lambda (pass)
|
2019-11-09 15:56:55 +00:00
|
|
|
|
(comp-log (format "Running pass %s:\n" pass) 2)
|
2019-09-07 06:18:08 +00:00
|
|
|
|
(setq data (funcall pass data)))
|
2019-11-02 11:34:09 +00:00
|
|
|
|
comp-passes)
|
|
|
|
|
data))
|
2019-07-07 07:23:10 +00:00
|
|
|
|
|
|
|
|
|
(provide 'comp)
|
|
|
|
|
|
|
|
|
|
;;; comp.el ends here
|