So, both mul and imul instructions multiply machine words and store both the result and overflow in some registers (see this for example https://c9x.me/x86/html/file_module_x86_id_210.html). I'm trying to write a vop that would use this information fully and return 2 values. Is this possible? I can't find any info on how to make vop return multiple values. If someone can just show the example of how the whole thing would look like, I would appreciate it as well.
EDIT: So I figured out something, but it's still not good enough. I'll post it here and then explain the problem.
(defpackage #:fixmul
(:use #:CL)
(:export #:fixmul))
(in-package #:fixmul)
(sb-c:defknown fixmul (fixnum fixnum) (values fixnum fixnum &optional)
(sb-c:foldable sb-c:flushable sb-c:movable)
:overwrite-fndb-silently t)
(in-package #:sb-vm)
(define-vop (fixmul:fixmul)
(:policy :fast-safe)
(:translate fixmul:fixmul)
(:args (x :scs (signed-reg) :target eax)
(y :scs (signed-reg signed-stack)))
(:arg-types fixnum fixnum)
(:args-var args)
(:temporary (:sc signed-reg :offset eax-offset :target quo
:from (:argument 0) :to (:result 0)) eax)
(:temporary (:sc signed-reg :offset edx-offset :target rem
:from (:argument 0) :to (:result 1)) edx)
(:results (quo :scs (signed-reg))
(rem :scs (signed-reg)))
(:result-types fixnum fixnum)
(:note "inline (unsigned-byte 64) arithmetic")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(move eax x)
(inst mul eax y)
(move quo eax)
(move rem edx)))
(in-package #:fixmul)
(defun fixmul (a b)
(fixmul a b))
So, this disassembles to:
> (disassemble 'fixmul)
; disassembly for FIXMUL
; Size: 35 bytes. Origin: #x52C42F4F ; FIXMUL
; 4F: 48F7E7 MUL RAX, RDI
; 52: 488BFA MOV RDI, RDX
; 55: 48D1E0 SHL RAX, 1
; 58: 48D1E7 SHL RDI, 1
; 5B: 488BD0 MOV RDX, RAX
; 5E: 488D5D10 LEA RBX, [RBP+16]
; 62: B904000000 MOV ECX, 4
; 67: BE17001050 MOV ESI, #x50100017 ; NIL
; 6C: F9 STC
; 6D: 488BE5 MOV RSP, RBP
; 70: 5D POP RBP
; 71: C3 RET
NIL
This isn't bad, but I don't understand most of what I'm doing on sbcl side, in particular -- why am I getting this LEA RBX, [RBP+16] and MOV ESI, #x50100017
instructions ?
EDIT2: It seems those instructions are mostly about returning 2 values. However, the whole thing is problematic in a sense that it uses CL native fixnums etc, instead of just raw machine words. I'm not sure how to resolve this problem, which is why I don't put a self answer.