comparison lisp/bytecomp.el @ 446:1ccc32a20af4 r21-2-38

Import from CVS: tag r21-2-38
author cvs
date Mon, 13 Aug 2007 11:37:21 +0200
parents 576fb035e263
children 7039e6323819
comparison
equal deleted inserted replaced
445:34f3776fcf0e 446:1ccc32a20af4
441 (defvar byte-compile-free-references) 441 (defvar byte-compile-free-references)
442 (defvar byte-compile-free-assignments) 442 (defvar byte-compile-free-assignments)
443 443
444 (defvar byte-compiler-error-flag) 444 (defvar byte-compiler-error-flag)
445 445
446 ;;; A form of eval that includes the currently defined macro definitions.
447 ;;; This helps implement the promise made in the Lispref:
448 ;;;
449 ;;; "If a file being compiled contains a `defmacro' form, the macro is
450 ;;; defined temporarily for the rest of the compilation of that file."
451 (defun byte-compile-eval (form)
452 (let ((save-macro-environment nil))
453 (unwind-protect
454 (loop for (sym . def) in byte-compile-macro-environment do
455 (push
456 (if (fboundp sym) (cons sym (symbol-function sym)) sym)
457 save-macro-environment)
458 (fset sym (cons 'macro def))
459 finally return (eval form))
460 (dolist (elt save-macro-environment)
461 (if (symbolp elt)
462 (fmakunbound elt)
463 (fset (car elt) (cdr elt)))))))
464
446 (defconst byte-compile-initial-macro-environment 465 (defconst byte-compile-initial-macro-environment
447 '((byte-compiler-options . (lambda (&rest forms) 466 '((byte-compiler-options . (lambda (&rest forms)
448 (apply 'byte-compiler-options-handler forms))) 467 (apply 'byte-compiler-options-handler forms)))
449 (eval-when-compile . (lambda (&rest body) 468 (eval-when-compile . (lambda (&rest body)
450 (list 'quote (eval (cons 'progn body))))) 469 (list 'quote (byte-compile-eval (cons 'progn body)))))
451 (eval-and-compile . (lambda (&rest body) 470 (eval-and-compile . (lambda (&rest body)
452 (eval (cons 'progn body)) 471 (byte-compile-eval (cons 'progn body))
453 (cons 'progn body)))) 472 (cons 'progn body))))
454 "The default macro-environment passed to macroexpand by the compiler. 473 "The default macro-environment passed to macroexpand by the compiler.
455 Placing a macro here will cause a macro to have different semantics when 474 Placing a macro here will cause a macro to have different semantics when
456 expanded by the compiler as when expanded by the interpreter.") 475 expanded by the compiler as when expanded by the interpreter.")
457 476
2723 ;; Compile those primitive ordinary functions 2742 ;; Compile those primitive ordinary functions
2724 ;; which have special byte codes just for speed. 2743 ;; which have special byte codes just for speed.
2725 2744
2726 (defmacro byte-defop-compiler (function &optional compile-handler) 2745 (defmacro byte-defop-compiler (function &optional compile-handler)
2727 ;; add a compiler-form for FUNCTION. 2746 ;; add a compiler-form for FUNCTION.
2728 ;; If function is a symbol, then the variable "byte-SYMBOL" must name 2747 ;; If FUNCTION is a symbol, then the variable "byte-SYMBOL" must name
2729 ;; the opcode to be used. If function is a list, the first element 2748 ;; the opcode to be used. If is a list, the first element
2730 ;; is the function and the second element is the bytecode-symbol. 2749 ;; is the function and the second element is the bytecode-symbol.
2731 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or 2750 ;; COMPILE-HANDLER is the function to use to compile this byte-op, or
2732 ;; may be the abbreviations 0, 1, 2, 3, 0-1, 1-2, 2-3, 0+1, 1+1, 2+1, 2751 ;; may be the abbreviations 0, 1, 2, 3, 0-1, 1-2, 2-3, 0+1, 1+1, 2+1,
2733 ;; 0-1+1, 1-2+1, 2-3+1, 0+2, or 1+2. If it is nil, then the handler is 2752 ;; 0-1+1, 1-2+1, 2-3+1, 0+2, or 1+2. If it is nil, then the handler is
2734 ;; "byte-compile-SYMBOL." 2753 ;; "byte-compile-SYMBOL."
2914 (byte-defop-compiler aset 3) 2933 (byte-defop-compiler aset 3)
2915 2934
2916 (byte-defop-compiler-rmsfun member 2) 2935 (byte-defop-compiler-rmsfun member 2)
2917 (byte-defop-compiler-rmsfun assq 2) 2936 (byte-defop-compiler-rmsfun assq 2)
2918 2937
2919 (byte-defop-compiler max byte-compile-associative)
2920 (byte-defop-compiler min byte-compile-associative)
2921 (byte-defop-compiler (+ byte-plus) byte-compile-associative)
2922 (byte-defop-compiler (* byte-mult) byte-compile-associative)
2923
2924 ;;####(byte-defop-compiler move-to-column 1) 2938 ;;####(byte-defop-compiler move-to-column 1)
2925 (byte-defop-compiler-1 interactive byte-compile-noop) 2939 (byte-defop-compiler-1 interactive byte-compile-noop)
2926 (byte-defop-compiler-1 domain byte-compile-domain) 2940 (byte-defop-compiler-1 domain byte-compile-domain)
2927 2941
2928 ;; As of GNU Emacs 19.18 and Lucid Emacs 19.8, mod and % are different: `%' 2942 ;; As of GNU Emacs 19.18 and Lucid Emacs 19.8, mod and % are different: `%'
2997 ;; `byte-compile-subr-wrong-args' also converts the call to non-inlined. 3011 ;; `byte-compile-subr-wrong-args' also converts the call to non-inlined.
2998 3012
2999 (defun byte-compile-no-args-with-one-extra (form) 3013 (defun byte-compile-no-args-with-one-extra (form)
3000 (case (length (cdr form)) 3014 (case (length (cdr form))
3001 (0 (byte-compile-no-args form)) 3015 (0 (byte-compile-no-args form))
3002 (1 (byte-compile-normal-call form)) 3016 (1 (if (eq nil (nth 1 form))
3017 (byte-compile-no-args (butlast form))
3018 (byte-compile-normal-call form)))
3003 (t (byte-compile-subr-wrong-args form "0-1")))) 3019 (t (byte-compile-subr-wrong-args form "0-1"))))
3004 3020
3005 (defun byte-compile-one-arg-with-one-extra (form) 3021 (defun byte-compile-one-arg-with-one-extra (form)
3006 (case (length (cdr form)) 3022 (case (length (cdr form))
3007 (1 (byte-compile-one-arg form)) 3023 (1 (byte-compile-one-arg form))
3008 (2 (byte-compile-normal-call form)) 3024 (2 (if (eq nil (nth 2 form))
3025 (byte-compile-one-arg (butlast form))
3026 (byte-compile-normal-call form)))
3009 (t (byte-compile-subr-wrong-args form "1-2")))) 3027 (t (byte-compile-subr-wrong-args form "1-2"))))
3010 3028
3011 (defun byte-compile-two-args-with-one-extra (form) 3029 (defun byte-compile-two-args-with-one-extra (form)
3012 (case (length (cdr form)) 3030 (case (length (cdr form))
3013 (2 (byte-compile-two-args form)) 3031 (2 (byte-compile-two-args form))
3014 (3 (byte-compile-normal-call form)) 3032 (3 (if (eq nil (nth 3 form))
3033 (byte-compile-two-args (butlast form))
3034 (byte-compile-normal-call form)))
3015 (t (byte-compile-subr-wrong-args form "2-3")))) 3035 (t (byte-compile-subr-wrong-args form "2-3"))))
3016 3036
3017 (defun byte-compile-zero-or-one-arg-with-one-extra (form) 3037 (defun byte-compile-zero-or-one-arg-with-one-extra (form)
3018 (case (length (cdr form)) 3038 (case (length (cdr form))
3019 (0 (byte-compile-one-arg (append form '(nil)))) 3039 (0 (byte-compile-one-arg (append form '(nil))))
3020 (1 (byte-compile-one-arg form)) 3040 (1 (byte-compile-one-arg form))
3021 (2 (byte-compile-normal-call form)) 3041 (2 (if (eq nil (nth 2 form))
3042 (byte-compile-one-arg (butlast form))
3043 (byte-compile-normal-call form)))
3022 (t (byte-compile-subr-wrong-args form "0-2")))) 3044 (t (byte-compile-subr-wrong-args form "0-2"))))
3023 3045
3024 (defun byte-compile-one-or-two-args-with-one-extra (form) 3046 (defun byte-compile-one-or-two-args-with-one-extra (form)
3025 (case (length (cdr form)) 3047 (case (length (cdr form))
3026 (1 (byte-compile-two-args (append form '(nil)))) 3048 (1 (byte-compile-two-args (append form '(nil))))
3027 (2 (byte-compile-two-args form)) 3049 (2 (byte-compile-two-args form))
3028 (3 (byte-compile-normal-call form)) 3050 (3 (if (eq nil (nth 3 form))
3051 (byte-compile-two-args (butlast form))
3052 (byte-compile-normal-call form)))
3029 (t (byte-compile-subr-wrong-args form "1-3")))) 3053 (t (byte-compile-subr-wrong-args form "1-3"))))
3030 3054
3031 (defun byte-compile-two-or-three-args-with-one-extra (form) 3055 (defun byte-compile-two-or-three-args-with-one-extra (form)
3032 (case (length (cdr form)) 3056 (case (length (cdr form))
3033 (2 (byte-compile-three-args (append form '(nil)))) 3057 (2 (byte-compile-three-args (append form '(nil))))
3034 (3 (byte-compile-three-args form)) 3058 (3 (byte-compile-three-args form))
3035 (4 (byte-compile-normal-call form)) 3059 (4 (if (eq nil (nth 4 form))
3060 (byte-compile-three-args (butlast form))
3061 (byte-compile-normal-call form)))
3036 (t (byte-compile-subr-wrong-args form "2-4")))) 3062 (t (byte-compile-subr-wrong-args form "2-4"))))
3037 3063
3038 (defun byte-compile-no-args-with-two-extra (form) 3064 (defun byte-compile-no-args-with-two-extra (form)
3039 (case (length (cdr form)) 3065 (case (length (cdr form))
3040 (0 (byte-compile-no-args form)) 3066 (0 (byte-compile-no-args form))
3062 (byte-compile-constant nil)) 3088 (byte-compile-constant nil))
3063 3089
3064 (defun byte-compile-discard () 3090 (defun byte-compile-discard ()
3065 (byte-compile-out 'byte-discard 0)) 3091 (byte-compile-out 'byte-discard 0))
3066 3092
3067 ;; Compile a function that accepts one or more args and is right-associative. 3093 (defun byte-compile-max (form)
3068 ;; We do it by left-associativity so that the operations 3094 (let ((args (cdr form)))
3069 ;; are done in the same order as in interpreted code.
3070 ;(defun byte-compile-associative (form)
3071 ; (if (cdr form)
3072 ; (let ((opcode (get (car form) 'byte-opcode))
3073 ; (args (copy-sequence (cdr form))))
3074 ; (byte-compile-form (car args))
3075 ; (setq args (cdr args))
3076 ; (while args
3077 ; (byte-compile-form (car args))
3078 ; (byte-compile-out opcode 0)
3079 ; (setq args (cdr args))))
3080 ; (byte-compile-constant (eval form))))
3081
3082 ;; Compile a function that accepts one or more args and is right-associative.
3083 ;; We do it by left-associativity so that the operations
3084 ;; are done in the same order as in interpreted code.
3085 (defun byte-compile-associative (form)
3086 (let ((args (cdr form))
3087 (opcode (get (car form) 'byte-opcode)))
3088 (case (length args) 3095 (case (length args)
3089 (0 (byte-compile-constant (eval form))) 3096 (0 (byte-compile-subr-wrong-args form "1 or more"))
3097 (1 (byte-compile-form (car args))
3098 (when (not byte-compile-delete-errors)
3099 (byte-compile-out 'byte-dup 0)
3100 (byte-compile-out 'byte-max 0)))
3090 (t (byte-compile-form (car args)) 3101 (t (byte-compile-form (car args))
3091 (dolist (arg (cdr args)) 3102 (dolist (elt (cdr args))
3092 (byte-compile-form arg) 3103 (byte-compile-form elt)
3093 (byte-compile-out opcode 0)))))) 3104 (byte-compile-out 'byte-max 0))))))
3105
3106 (defun byte-compile-min (form)
3107 (let ((args (cdr form)))
3108 (case (length args)
3109 (0 (byte-compile-subr-wrong-args form "1 or more"))
3110 (1 (byte-compile-form (car args))
3111 (when (not byte-compile-delete-errors)
3112 (byte-compile-out 'byte-dup 0)
3113 (byte-compile-out 'byte-min 0)))
3114 (t (byte-compile-form (car args))
3115 (dolist (elt (cdr args))
3116 (byte-compile-form elt)
3117 (byte-compile-out 'byte-min 0))))))
3094 3118
3095 3119
3096 ;; more complicated compiler macros 3120 ;; more complicated compiler macros
3097 3121
3098 (byte-defop-compiler list) 3122 (byte-defop-compiler list)
3099 (byte-defop-compiler concat) 3123 (byte-defop-compiler concat)
3100 (byte-defop-compiler fset) 3124 (byte-defop-compiler fset)
3101 (byte-defop-compiler insert) 3125 (byte-defop-compiler insert)
3102 (byte-defop-compiler-1 function byte-compile-function-form) 3126 (byte-defop-compiler-1 function byte-compile-function-form)
3103 (byte-defop-compiler-1 - byte-compile-minus) 3127 (byte-defop-compiler max)
3104 (byte-defop-compiler (/ byte-quo) byte-compile-quo) 3128 (byte-defop-compiler min)
3129 (byte-defop-compiler (+ byte-plus) byte-compile-plus)
3130 (byte-defop-compiler-1 - byte-compile-minus)
3131 (byte-defop-compiler (* byte-mult) byte-compile-mult)
3132 (byte-defop-compiler (/ byte-quo) byte-compile-quo)
3105 (byte-defop-compiler nconc) 3133 (byte-defop-compiler nconc)
3106 (byte-defop-compiler-1 beginning-of-line) 3134 (byte-defop-compiler-1 beginning-of-line)
3107 3135
3108 (byte-defop-compiler (= byte-eqlsign) byte-compile-arithcompare) 3136 (byte-defop-compiler (= byte-eqlsign) byte-compile-arithcompare)
3109 (byte-defop-compiler (< byte-lss) byte-compile-arithcompare) 3137 (byte-defop-compiler (< byte-lss) byte-compile-arithcompare)
3174 ((< nargs 256) 3202 ((< nargs 256)
3175 (mapcar 'byte-compile-form args) 3203 (mapcar 'byte-compile-form args)
3176 (byte-compile-out 'byte-concatN nargs)) 3204 (byte-compile-out 'byte-concatN nargs))
3177 ((byte-compile-normal-call form))))) 3205 ((byte-compile-normal-call form)))))
3178 3206
3207 (defun byte-compile-plus (form)
3208 (let ((args (cdr form)))
3209 (case (length args)
3210 (0 (byte-compile-constant 0))
3211 (1 (byte-compile-plus (append form '(0))))
3212 (t (byte-compile-form (car args))
3213 (dolist (elt (cdr args))
3214 (case elt
3215 (0 (when (not byte-compile-delete-errors)
3216 (byte-compile-constant 0)
3217 (byte-compile-out 'byte-plus 0)))
3218 (+1 (byte-compile-out 'byte-add1 0))
3219 (-1 (byte-compile-out 'byte-sub1 0))
3220 (t
3221 (byte-compile-form elt)
3222 (byte-compile-out 'byte-plus 0))))))))
3223
3179 (defun byte-compile-minus (form) 3224 (defun byte-compile-minus (form)
3180 (let ((args (cdr form))) 3225 (let ((args (cdr form)))
3181 (case (length args) 3226 (case (length args)
3182 (0 (byte-compile-subr-wrong-args form "1 or more")) 3227 (0 (byte-compile-subr-wrong-args form "1 or more"))
3183 (1 (byte-compile-form (car args)) 3228 (1 (byte-compile-form (car args))
3184 (byte-compile-out 'byte-negate 0)) 3229 (byte-compile-out 'byte-negate 0))
3185 (t (byte-compile-form (car args)) 3230 (t (byte-compile-form (car args))
3186 (dolist (elt (cdr args)) 3231 (dolist (elt (cdr args))
3187 (byte-compile-form elt) 3232 (case elt
3188 (byte-compile-out 'byte-diff 0)))))) 3233 (0 (when (not byte-compile-delete-errors)
3234 (byte-compile-constant 0)
3235 (byte-compile-out 'byte-diff 0)))
3236 (+1 (byte-compile-out 'byte-sub1 0))
3237 (-1 (byte-compile-out 'byte-add1 0))
3238 (t
3239 (byte-compile-form elt)
3240 (byte-compile-out 'byte-diff 0))))))))
3241
3242 (defun byte-compile-mult (form)
3243 (let ((args (cdr form)))
3244 (case (length args)
3245 (0 (byte-compile-constant 1))
3246 (1 (byte-compile-mult (append form '(1))))
3247 (t (byte-compile-form (car args))
3248 (dolist (elt (cdr args))
3249 (case elt
3250 (1 (when (not byte-compile-delete-errors)
3251 (byte-compile-constant 1)
3252 (byte-compile-out 'byte-mult 0)))
3253 (-1 (byte-compile-out 'byte-negate 0))
3254 (2 (byte-compile-out 'byte-dup 0)
3255 (byte-compile-out 'byte-plus 0))
3256 (t
3257 (byte-compile-form elt)
3258 (byte-compile-out 'byte-mult 0))))))))
3189 3259
3190 (defun byte-compile-quo (form) 3260 (defun byte-compile-quo (form)
3191 (let ((args (cdr form))) 3261 (let ((args (cdr form)))
3192 (case (length args) 3262 (case (length args)
3193 (0 (byte-compile-subr-wrong-args form "1 or more")) 3263 (0 (byte-compile-subr-wrong-args form "1 or more"))
3194 (1 (byte-compile-constant 1) 3264 (1 (byte-compile-constant 1)
3195 (byte-compile-form (car args)) 3265 (byte-compile-form (car args))
3196 (byte-compile-out 'byte-quo 0)) 3266 (byte-compile-out 'byte-quo 0))
3197 (t (byte-compile-form (car args)) 3267 (t (byte-compile-form (car args))
3198 (dolist (elt (cdr args)) 3268 (dolist (elt (cdr args))
3199 (byte-compile-form elt) 3269 (case elt
3200 (byte-compile-out 'byte-quo 0)))))) 3270 (+1 (when (not byte-compile-delete-errors)
3271 (byte-compile-constant 1)
3272 (byte-compile-out 'byte-quo 0)))
3273 (-1 (byte-compile-out 'byte-negate 0))
3274 (t
3275 (when (and (numberp elt) (= elt 0))
3276 (byte-compile-warn "Attempt to divide by zero: %s" form))
3277 (byte-compile-form elt)
3278 (byte-compile-out 'byte-quo 0))))))))
3201 3279
3202 (defun byte-compile-nconc (form) 3280 (defun byte-compile-nconc (form)
3203 (let ((args (cdr form))) 3281 (let ((args (cdr form)))
3204 (case (length args) 3282 (case (length args)
3205 (0 (byte-compile-constant nil)) 3283 (0 (byte-compile-constant nil))