Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global FTYPE declarations vs local TYPE declarations #459

Open
galdor opened this issue Sep 24, 2023 · 0 comments
Open

Global FTYPE declarations vs local TYPE declarations #459

galdor opened this issue Sep 24, 2023 · 0 comments

Comments

@galdor
Copy link

galdor commented Sep 24, 2023

Given the following code:

(defpackage :example
  (:use :cl))

(in-package :example)

(declaim (ftype (function (float float) float) foo1))
(defun foo1 (a b)
  (+ a b))

(defun foo2 (a b)
  (declare (type float a b))
  (the float (+ a b)))

CCL compiles FOO1 to:

    (recover-fn-from-rip)                   ;     [0]
    (cmpl ($ 16) (% nargs))                 ;     [7]
    (jne L65)                               ;    [10]
    (pushq (% rbp))                         ;    [12]
    (movq (% rsp) (% rbp))                  ;    [13]
    (pushq (% arg_y))                       ;    [16]
    (pushq (% arg_z))                       ;    [17]

;;; (+ a b)
    (movq (@ -8 (% rbp)) (% arg_y))         ;    [18]
    (movq (@ -16 (% rbp)) (% arg_z))        ;    [22]
    (movq (% rbp) (% rsp))                  ;    [26]
    (popq (% rbp))                          ;    [29]
    (movl (% arg_y.l) (% imm0.l))           ;    [30]
    (orl (% arg_z.l) (% imm0.l))            ;    [32]
    (testb ($ 7) (% imm0.b))                ;    [34]
    (jne L53)                               ;    [37]
    (addq (% arg_y) (% arg_z))              ;    [39]
    (jo L46)                                ;    [42]
    (repz)
    (retq)                                  ;    [44]
L46                                         ;   [@61]
    (jmp (@ .SPFIX-OVERFLOW))               ;    [46]

    (:align 2)
L53                                         ;   [@68]
    (jmp (@ .SPBUILTIN-PLUS))               ;    [53]

;;; #<no source text>

    (:align 4)
L65                                         ;   [@80]
    (uuo-error-wrong-number-of-args)        ;    [65]

And FOO2 to:

    (recover-fn-from-rip)                   ;     [0]
    (cmpl ($ 16) (% nargs))                 ;     [7]
    (jne L41)                               ;    [10]
    (pushq (% rbp))                         ;    [12]
    (movq (% rsp) (% rbp))                  ;    [13]
    (pushq (% arg_y))                       ;    [16]
    (pushq (% arg_z))                       ;    [17]

;;; (+ a b)
    (movq (@ -8 (% rbp)) (% arg_y))         ;    [18]
    (movq (@ -16 (% rbp)) (% arg_z))        ;    [22]
    (movq (% rbp) (% rsp))                  ;    [26]
    (popq (% rbp))                          ;    [29]
    (jmp (@ .SPBUILTIN-PLUS))               ;    [30]

;;; #<no source text>

    (:align 3)
L41                                         ;   [@56]
    (uuo-error-wrong-number-of-args)        ;    [41]

It feels like CCL has the same type information for FOO1 (using the global FTYPE declaration) and for FOO2 (using both the local TYPE declarations and the THE form for the return value). Yet the generated code is quite different.

What is CCL missing in FOO1 that leads to less compact generated code compared to FOO2? It seems to add an overflow check which is not present in FOO2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant