-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinst.lisp
165 lines (156 loc) · 6.91 KB
/
inst.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(sb-ext:unlock-package (find-package :sb-assem))
(sb-ext:unlock-package (find-package :sb-disassem))
(in-package :sb-assem)
(defun %def-inst-encoder (symbol thing &optional accept-prefixes)
(let ((function
(or thing ; load-time effect passes the definition
;; (where compile-time doesn't).
;; Otherwise, take what we already had so that a compile-time
;; effect doesn't clobber an already-working hash-table entry
;; if re-evaluating a define-instruction form.
(car (ensure-list (gethash symbol *inst-encoder*))))))
(setf (gethash symbol *inst-encoder*)
(if accept-prefixes (cons function t) function))))
(defmacro define-instruction (name lambda-list &rest options)
(binding* ((fun-name (intern (symbol-name name) *backend-instruction-set-package*))
(segment-name (car lambda-list))
(vop-name nil)
(emitter nil)
(decls nil)
(attributes nil)
(cost nil)
(dependencies nil)
(delay nil)
(pinned nil)
(pdefs nil))
(declare (ignorable pinned))
(dolist (option-spec options)
(multiple-value-bind (option args)
(if (consp option-spec)
(values (car option-spec) (cdr option-spec))
(values option-spec nil))
(case option
(:emitter
(when emitter
(error "You can only specify :EMITTER once per instruction."))
(setf emitter args))
(:declare
(setf decls (append decls args)))
(:attributes
(setf attributes (append attributes args)))
(:cost
(setf cost (first args)))
(:dependencies
(setf dependencies (append dependencies args)))
(:delay
(when delay
(error "You can only specify :DELAY once per instruction."))
(setf delay args))
(:pinned
(setf pinned t))
(:vop-var
(if vop-name
(error "You can only specify :VOP-VAR once per instruction.")
(setf vop-name (car args))))
(:printer
(let* ((inst-args (second args))
(names (mapcar #'car inst-args)))
(when (> (length names) (length (remove-duplicates names)))
(error "Duplicate operand names in ~S~%" args)))
(destructuring-bind (name operands . options) args
(push ``(,',name (,,@(mapcar (lambda (x) ``(,',(car x) ,,@(cdr x)))
operands))
,,@options) pdefs)))
(t
(error "unknown option: ~S" option)))))
(when emitter
(unless cost (setf cost 1))
#+sb-dyncount
(push `(when (segment-collect-dynamic-statistics ,segment-name)
(let* ((info (sb-c:ir2-component-dyncount-info
(sb-c:component-info
sb-c:*component-being-compiled*)))
(costs (sb-c:dyncount-info-costs info))
(block-number (sb-c:block-number
(sb-c:ir2-block-block
(sb-c:vop-block ,vop-name)))))
(incf (aref costs block-number) ,cost)))
emitter)
(when assem-scheduler-p
(if pinned
(setf emitter
`((when (segment-run-scheduler ,segment-name)
(schedule-pending-instructions ,segment-name))
,@emitter))
(let ((flet-name (make-symbol (concatenate 'string "ENCODE-"
(string fun-name))))
(inst-name '#:inst))
(setf emitter `((flet ((,flet-name (,segment-name)
,@emitter))
(if (segment-run-scheduler ,segment-name)
(let ((,inst-name
(make-instruction
(incf (segment-inst-number
,segment-name))
#',flet-name
(instruction-attributes
,@attributes)
(progn ,@delay))))
,@(when dependencies
`((note-dependencies
(,segment-name ,inst-name)
,@dependencies)))
(queue-inst ,segment-name ,inst-name))
(,flet-name ,segment-name)))))))))
`(progn
#-sb-xc-host ; The disassembler is not used on the host.
(setf (get ',fun-name 'sb-disassem::instruction-flavors)
(list ,@pdefs))
,@(when emitter
(let* ((operands (cdr lambda-list))
(accept-prefixes (eq (car operands) '&prefix)))
(when accept-prefixes (setf operands (cdr operands)))
`((eval-when (:compile-toplevel)
(%def-inst-encoder ',fun-name nil ',accept-prefixes))
(%def-inst-encoder
',fun-name
(named-lambda ,(string fun-name) (,segment-name ,@operands)
(declare ,@decls)
(let ,(and vop-name `((,vop-name *current-vop*)))
(block ,fun-name ,@emitter)))
',accept-prefixes))))
',fun-name)))
(in-package :sb-x86-64-asm)
(sb-assem::define-instruction blsr (segment dst src)
(:printer ext-reg-reg/mem-no-width
((op '(#xf3 1))
(opcode-prefix #x0f38)
(reg/mem nil :type 'sized-reg/mem))
'(:name :tab reg/mem ", " reg))
(:emitter
(let ((size (matching-operand-size dst src)))
(when (not (eq size :qword))
(error "BLSR instruction only supported for 64-bit operand size")))
(emit-byte segment #xC4)
(emit-byte segment #b11100010)
(emit-byte segment (logior #b10000000
(ash (lognot (reg-id-num (reg-id dst))) 3)))
;; opcode
(emit-byte segment #xf3)
;; mod-r/m byte
(emit-byte segment (logior (ash (logior #b11000 1) 3)
(reg-encoding src segment)))))
(in-package :jsoon)
(sb-c:defknown %unset-rightmost-bit ((unsigned-byte 64)) (integer 0 64)
(sb-c:foldable sb-c:flushable sb-c:movable)
:overwrite-fndb-silently t)
(in-package :sb-vm)
(define-vop (jsoon::%unset-rightmost-bit)
(:policy :fast)
(:translate jsoon::%unset-rightmost-bit)
(:args (x :scs (unsigned-reg) :target r))
(:arg-types positive-fixnum)
(:results (r :scs (unsigned-reg)))
(:result-types positive-fixnum)
(:generator 1
(inst blsr x r)))