Debian Codegraph @

src/vminsn.scm

raw
1 ;;;
2 ;;; vminsn.scm - Virtual machine instruction definition
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;;; This file is processed by geninsn to produce a couple of C files:
35 ;;; gauche/vminsn.h and vminsn.c, which are then included in vm.c.
36 ;;; This file is also used by the compiler.
37 ;;;
38 ;;;
39 ;;; (define-insn <name> <num-params> <operand-type>
40 ;;; :optional (<combination> '())
41 ;;; (<body> #f)
42 ;;; <flags> ...)
43 ;;;
44 ;;; <name> - instruction name. In C, an enum SCM_VM_<name> is defined.
45 ;;;
46 ;;; <num-params> - # of parameters the instruction takes.
47 ;;; Can be (N M ...) - in this case, N is the proper
48 ;;; <num-params> of this insn, but vm-build-insn tolerates
49 ;;; the passed insn to have M parameters as well. If N < M,
50 ;;; extra parameters are ignored. If N > M, 0 is assumed
51 ;;; for missing parameters. This is mainly to compile
52 ;;; Gauche itself with the previous version of Gauche.
53 ;;;
54 ;;; <operand-type> - none : the insn doesn't take an operand.
55 ;;; obj : an ScmObj operand.
56 ;;; label : a label address the next pc points.
57 ;;; code : an ScmCompiledCode operand.
58 ;;; codes: a list of ScmCompiledCodes.
59 ;;; obj+label : an ScmObj, followed by a label address
60 ;;;
61 ;;; <combination> - If this is a comibined insn, list the ingredients
62 ;;; here. It is used in multiple purposes:
63 ;;; * The instruction body is generated automatically,
64 ;;; fusing the ingredients' body.
65 ;;; * A DFA is set up in the instruction emitter
66 ;;; that replaces this specific sequence of
67 ;;; insns to the combined insn. Because of this,
68 ;;; pass5 (instruction generation) doesn't need to
69 ;;; handle instruction combination explicitly.
70 ;;;
71 ;;; <body> - a CiSE expression that handles the instruction.
72 ;;; can be #f for combined insn.
73 ;;;
74 ;;; <flags>
75 ;;; :obsoleted - mark obsoleted insn. handled but won't be
76 ;;; generated.
77 ;;; :fold-lref - the insn must be a combination of LREF + something,
78 ;;; with depth and offset parameters.
79 ;;; this indicates that the combined insn should be
80 ;;; emitted even if preceding insn is 'shortcut'
81 ;;; LREFs such as LREF0 or LREF21; that is, instead
82 ;;; of having LREF0-SOMETHING or LREF21-SOMETHING
83 ;;; separately, we'll have LREF-SOMETHING(0,0) and
84 ;;; LREF-SOMETHING(2,1), respectively.
85 ;;; :multi-value - Can result multiple values.
86
87 ;;;==============================================================
88 ;;; Common Cise macros
89 ;;;
90
91 ;;
92 ;; ($result <expr>)
93 ;; Emits code to place the value of <expr> as a result.
94 ;; Depending on the parameter 'result-type', the result will be
95 ;; either put in VAL0, pushed directly into the stack, or returned.
96 ;; The parameter result-type is defined in geninsn.
97
98 (define-cise-stmt $result
99 [(_ expr) `(begin ,@(case (result-type)
100 [(reg) `((set! VAL0 ,expr)
101 (set! (-> vm numVals) 1)
102 NEXT_PUSHCHECK)]
103 [(push) `((PUSH-ARG ,expr) NEXT)]
104 [(call) `((set! VAL0 ,expr))]
105 [(ret) `((set! VAL0 ,expr)
106 (set! (-> vm numVals) 1)
107 (RETURN-OP)
108 NEXT)]
109 ))])
110
111 ;; variations of $result with type coercion
112 (define-cise-stmt $result:b
113 [(_ expr) `($result (SCM_MAKE_BOOL ,expr))])
114 (define-cise-stmt $result:i
115 [(_ expr) (let1 r (gensym "cise__")
116 `(let* ([,r :: long ,expr])
117 ($result (SCM_MAKE_INT ,r))))])
118 (define-cise-stmt $result:n
119 [(_ expr) (let ([r (gensym "cise__")]
120 [v (gensym "cise__")])
121 `(let* ([,r :: long ,expr] [,v])
122 (if (SCM_SMALL_INT_FITS ,r)
123 (set! ,v (SCM_MAKE_INT ,r))
124 (set! ,v (Scm_MakeInteger ,r)))
125 ($result ,v)))])
126 (define-cise-stmt $result:u
127 [(_ expr) (let ([r (gensym "cise__")]
128 [v (gensym "cise__")])
129 `(let* ([,r :: u_long ,expr] [,v])
130 (if (SCM_SMALL_INT_FITS ,r)
131 (set! ,v (SCM_MAKE_INT ,r))
132 (set! ,v (Scm_MakeIntegerU ,r)))
133 ($result ,v)))])
134 (define-cise-stmt $result:f
135 [(_ expr) (let1 r (gensym "cise__")
136 `(let* ([,r :: double ,expr])
137 ($result (Scm_VMReturnFlonum ,r))))])
138
139 ;; Extract local value.
140 ;; If local var nees unboxing, necessary UNBOX insn will be generated
141 ;; by the compiler, so we don't need to worry about it.
142 (define-cise-stmt $lref!
143 [(_ var env off)
144 `(set! ,var (ENV-DATA ,env ,off))])
145
146 ;;
147 ;; ($w/argr <val> <expr> ...)
148 ;; ($w/argp <val> <expr> ...)
149 ;; Get one arg from various sources, depending on the parameter
150 ;; 'arg-source'. If 'arg-source' is #f, argr takes the value from
151 ;; VAL0 while argp pops the value from the stack.
152 ;; The parameter 'arg-source' is defined in geninsn.
153 ;;
154
155 (define-cise-stmt $w/arg_ ; internal
156 [(_ val default body)
157 (match (or (arg-source) default)
158 ['pop `(let* ((,val)) (POP-ARG ,val) ,@body)]
159 ['reg `(let* ((,val VAL0)) ,@body)]
160 ['lref (let ((dep (gensym)) (off (gensym)) (e (gensym)))
161 `(let* ((,dep :: int (SCM_VM_INSN_ARG0 code))
162 (,off :: int (SCM_VM_INSN_ARG1 code))
163 (,e :: ScmEnvFrame* ENV)
164 (,val))
165 (case/fallthrough ,dep
166 [(4) (set! ,e (-> ,e up))]
167 [(3) (set! ,e (-> ,e up))]
168 [(2) (set! ,e (-> ,e up))]
169 [(1) (set! ,e (-> ,e up))]
170 [(0) ($lref! ,val ,e ,off) (break)]
171 [else (while (> (post-- ,dep) 0) (set! ,e (-> ,e up)))
172 ($lref! ,val ,e ,off) (break)])
173 ,@body))]
174 [('lref d o) `(let* ((,val (ENV-DATA ,(let loop ((d d))
175 (if (zero? d)
176 'ENV
177 `(-> ,(loop (- d 1)) up)))
178 ,o)))
179 ,@body)])])
180
181 (define-cise-stmt $w/argr ; use VAL0 default
182 [(_ var . body) `($w/arg_ ,var reg ,body)])
183
184 (define-cise-stmt $w/argp ; pop default
185 [(_ var . body) `($w/arg_ ,var pop ,body)])
186
187 ;;
188 ;; ($arg-source src body ...)
189 ;; Switch arg-source to SRC in the BODY
190 ;;
191 (define-cise-stmt $arg-source ; override arg-source
192 env
193 [(_ src . body)
194 (parameterize ((arg-source src))
195 (cise-render-rec `(begin ,@body) 'stmt env))])
196
197 ;;
198 ;; ($insn-body insn)
199 ;; Returns the body of INSN.
200 ;;
201 (define-cise-stmt $insn-body
202 [(_ insn-name)
203 (or (and-let* ([insn (assq-ref (insn-alist) insn-name)])
204 (ref insn'body))
205 (error "cannot take the body of the instruction:" insn-name))])
206
207 ;;
208 ;; ($vm-err fmt args ...)
209 ;; Report error. This used to be a call to VM_ERR macro, but some
210 ;; compilers choke when #line directive is inserted between the macro
211 ;; arguments.
212 ;;
213 (define-cise-stmt $vm-err
214 [(_ . args) `(Scm_Error ,@args)])
215
216 ;;
217 ;; ($obsoleted name)
218 ;; Mark obsoleted instruction.
219 ;;
220 (define-cise-stmt $obsoleted
221 [(_ name)
222 `($vm-err "%s instruction is obsoleted. Using wrong compiler version?"
223 ,(cgen-safe-name-friendly (x->string name)))])
224
225 ;;
226 ;; ($type-check VAR PRED WHAT)
227 ;; Common type checking. Unless (PRED VAR), bail out.
228 ;;
229 (define-cise-stmt $type-check
230 [(_ var pred what)
231 `(unless (,pred ,var)
232 ($vm-err "%s required, but got %S" ,what ,var))])
233
234 ;;
235 ;; ($goto-insn INSN)
236 ;; Jump to the entry of the instruction INSN
237 ;;
238 (define-cise-stmt $goto-insn
239 [(_ insn)
240 `(,(format "goto label_~a;" (cgen-safe-name-friendly (x->string insn))))])
241
242 ;;
243 ;; ($branch EXPR)
244 ;; ($branch* EXPR)
245 ;; Branch. $branch* leaves the result in VAL0.
246 ;;
247 (define-cise-stmt $branch
248 [(_ expr) `(begin (if ,expr (FETCH-LOCATION PC) INCR-PC) CHECK-INTR NEXT)])
249 (define-cise-stmt $branch*
250 [(_ expr)
251 `(begin
252 (if ,expr
253 (begin (set! VAL0 SCM_FALSE) (FETCH-LOCATION PC))
254 (begin (set! VAL0 SCM_TRUE) INCR-PC))
255 CHECK-INTR
256 NEXT)])
257
258 ;;
259 ;; ($retc EXPR)
260 ;; ($retc* EXPR)
261 ;; Conditional return. $retc* leaves #f in VAL0.
262 ;;
263 (define-cise-stmt $retc
264 [(_ expr) `(begin (when ,expr (RETURN-OP)) NEXT)])
265 (define-cise-stmt $retc*
266 [(_ expr)
267 `(begin (when ,expr
268 (set! VAL0 SCM_FALSE)
269 (set! (-> vm numVals) 1)
270 (RETURN-OP))
271 NEXT)])
272
273 ;;
274 ;; ($w/numcmp r op . body)
275 ;; Compare arg (default stack top) and VAL0 with OP, and places the result
276 ;; in r.
277 (define-cise-stmt $w/numcmp
278 [(_ r op . body)
279 (let ([x (gensym)] [y (gensym)]
280 [cmp (case op
281 [(=) 'Scm_NumEq]
282 [(<) 'Scm_NumLT] [(<=) 'Scm_NumLE]
283 [(>) 'Scm_NumGT] [(>=) 'Scm_NumGE]
284 [else (error "[internal] invalid op for $w/numcmp" op)])])
285 `($w/argp ,x
286 (let* ((,y VAL0) (,r :: int))
287 (cond [(and (SCM_INTP ,x) (SCM_INTP ,y))
288 (set! ,r (,op (cast (signed long) (cast intptr_t ,x))
289 (cast (signed long) (cast intptr_t ,y))))]
290 [(and (SCM_FLONUMP ,x) (SCM_FLONUMP ,y))
291 (set! ,r (,op (SCM_FLONUM_VALUE ,x) (SCM_FLONUM_VALUE ,y)))]
292 [else
293 (set! ,r (,cmp ,x ,y))])
294 ,@body)))])
295
296 ;;
297 ;; ($undef var)
298 ;; ($define var)
299 ;; ($include var)
300 ;; Preprocessor directives.
301 ;;
302 (define-cise-stmt $undef
303 [(_ var) `("\n" |#reset-line| "#undef " ,(x->string var) "\n")])
304 (define-cise-stmt $define
305 [(_ var) `("\n" |#reset-line| "#define " ,(x->string var) "\n")])
306 (define-cise-stmt $include
307 [(_ var) `("\n" |#reset-line| "#include " ,(write-to-string var) "\n")])
308
309 ;; ($lref depth offset)
310 ;; Common code in LREF and XLREF.
311 (define-cise-stmt $lref
312 [(_ depth offset)
313 (let ([dep (gensym)]
314 [off (gensym)]
315 [e (gensym)])
316 `(let* ([,dep ::int ,depth]
317 [,off ::int ,offset]
318 [,e ::ScmEnvFrame* ENV])
319 (for [() (> ,dep 0) (post-- ,dep)]
320 (set! ,e (-> ,e up)))
321 ($result (ENV-DATA ,e ,off))))])
322
323 ;; ($lset depth offset)
324 ;; Common code in LSETL and XLSET.
325 (define-cise-stmt $lset
326 [(_ depth offset)
327 (let ([dep (gensym)]
328 [off (gensym)]
329 [e (gensym)]
330 [box (gensym)])
331 `(let* ([,dep ::int ,depth]
332 [,off ::int ,offset]
333 [,e ::ScmEnvFrame* ENV])
334 (for [() (> ,dep 0) (post-- ,dep)]
335 (VM-ASSERT (!= ,e NULL))
336 (set! ,e (-> ,e up)))
337 (VM-ASSERT (!= ,e NULL))
338 (VM-ASSERT (> (-> ,e size) ,off))
339 (SCM_FLONUM_ENSURE_MEM VAL0)
340 (let* ([,box (ENV-DATA ,e ,off)])
341 (VM_ASSERT (SCM_BOXP ,box))
342 (SCM_BOX_SET ,box VAL0))
343 (set! (-> vm numVals) 1)
344 NEXT))])
345
346 ;;
347 ;; ($lrefNN depth offset)
348 ;; Generate an expr to refer to the local variable, starting from ENV.
349 ;; DEPTH *must* be a literal integer. We unroll the loop.
350 (define-cise-stmt $lrefNN
351 [(_ depth offset)
352 (let1 v (gensym)
353 `(let* ([,v :: ScmObj (ENV_DATA ,(let loop ((d depth))
354 (case d
355 [(0) 'ENV]
356 [else `(-> ,(loop (- d 1)) up)]))
357 ,offset)])
358 ($result ,v)))])
359
360 ;;
361 ;; ($values)
362 ;; Generate common code for VALUES and VALUES-RET.
363 ;;
364 (define-cise-stmt $values
365 [(_)
366 '(let* ([nargs::int (SCM_VM_INSN_ARG code)]
367 [i::int (- nargs 1)]
368 [v VAL0])
369 (when (>= nargs SCM_VM_MAX_VALUES) ($vm-err "values got too many args"))
370 (VM-ASSERT (<= (- nargs 1) (- SP (-> vm stackBase))))
371 (when (> nargs 0)
372 (for [() (> i 0) (post-- i)]
373 (set! (aref (-> vm vals) (- i 1)) v)
374 (POP-ARG v)))
375 (set! VAL0 v)
376 (set! (-> vm numVals) nargs))])
377
378 ;;;==============================================================
379 ;;; Instruction definitions
380 ;;;
381
382 ;; NOP
383 ;; Used for placeholder. Won't appear in the final compiled code.
384 ;;
385 (define-insn NOP 0 none #f
386 NEXT)
387
388 ;; CONST <obj>
389 ;; Set <obj> to val0.
390 ;;
391 (define-insn CONST 0 obj #f
392 (let* ([val])
393 (FETCH-OPERAND val)
394 INCR-PC
395 ($result val)))
396
397 ;; Some immediate constants
398 (define-insn CONSTI 1 none #f ($result:i (SCM_VM_INSN_ARG code))) ; small int
399 (define-insn CONSTN 0 none #f ($result SCM_NIL)) ; ()
400 (define-insn CONSTF 0 none #f ($result SCM_FALSE)) ; #f
401 (define-insn CONSTU 0 none #f ($result SCM_UNDEFINED)) ; #<undef>
402
403 ;; Combined insn
404 (define-insn CONST-PUSH 0 obj (CONST PUSH))
405 (define-insn CONSTI-PUSH 1 none (CONSTI PUSH))
406 (define-insn CONSTN-PUSH 0 none (CONSTN PUSH))
407 (define-insn CONSTF-PUSH 0 none (CONSTF PUSH))
408 (define-insn CONST-RET 0 obj (CONST RET))
409 (define-insn CONSTF-RET 0 none (CONSTF RET))
410 (define-insn CONSTU-RET 0 none (CONSTU RET))
411
412 ;; push
413 ;; Push value of val0 to the stack top
414 ;; *sp++ = val0
415 ;;
416 (define-insn PUSH 0 none #f
417 (begin (CHECK-STACK-PARANOIA 1) (PUSH-ARG VAL0) NEXT))
418
419 ;; PRE-CALL(nargs) <cont>
420 ;; Prepare for a normal call. Push a continuation that resumes
421 ;; execution from <cont>.
422 ;;
423 (define-insn PRE-CALL 1 label #f
424 (let* ([next::ScmWord*])
425 (CHECK-STACK-PARANOIA CONT_FRAME_SIZE)
426 (FETCH-LOCATION next)
427 (PUSH-CONT next)
428 INCR-PC
429 CHECK-INTR
430 NEXT))
431
432 ;; combined insn
433 (define-insn PUSH-PRE-CALL 1 label (PUSH PRE-CALL))
434
435 ;; CHECK-STACK(size)
436 ;; Check for stack overflow
437 ;;
438 (define-insn CHECK-STACK 1 none #f
439 (let* ([reqstack::int (SCM_VM_INSN_ARG code)])
440 (CHECK-STACK reqstack)
441 NEXT))
442
443 ;; CALL(nargs)
444 ;; Call procedure in val0. The continuation of this call is already
445 ;; pushed by PRE-CALL, so this instruction is always the end of a graph.
446 ;;
447 (define-insn CALL 1 none #f
448 (let* ([nm] [argc::int] [proctype::int])
449 (label call_entry)
450 ($undef APPLY_CALL)
451 ($include "./vmcall.c")
452 (label tail_apply_entry)
453 ($define APPLY_CALL)
454 ($include "./vmcall.c"))
455 :multi-value)
456
457 ;; TAIL-CALL(nargs)
458 ;; Call procedure in val0. Same as CALL except this discards the
459 ;; caller's argument frame and shift the callee's argument frame.
460 ;;
461 (define-insn TAIL-CALL 1 none #f
462 (begin
463 (let* ([ct::ScmCallTrace* (-> vm callTrace)])
464 (when (!= ct NULL)
465 (set! (ref (aref (-> ct entries) (-> ct top)) base) BASE)
466 (set! (ref (aref (-> ct entries) (-> ct top)) pc) PC)
467 (set! (-> ct top) (logand (+ (-> ct top) 1)
468 (- (-> ct size) 1)))))
469 (DISCARD-ENV)
470 ($goto-insn CALL))
471 :multi-value)
472
473 ;; JUMP <addr>
474 ;; Jump to <addr>.
475 ;;
476 (define-insn JUMP 0 label #f
477 (begin (FETCH-LOCATION PC) CHECK-INTR NEXT))
478
479 ;; RET
480 ;; Pop the continuation stack.
481 ;;
482 (define-insn RET 0 none #f
483 (begin (RETURN-OP) CHECK-INTR NEXT)
484 :multi-value)
485
486 ;; DEFINE(flag) <symbol>
487 ;; Defines global binding of SYMBOL in the current module.
488 ;; The value is taken from the input stack.
489 ;; This instruction only appears at the toplevel. Internal defines
490 ;; are recognized and eliminated by the compiling process.
491 ;; Flag can be 0, SCM_BINDING_CONST(2) or SCM_BINDING_INLINABLE(4).
492 ;; (flag == 1 is also supported for the backward compatibility; remove
493 ;; it after 0.9.1 release!)
494 (define-insn DEFINE 1 obj #f
495 (let* ([var] [val VAL0])
496 (FETCH-OPERAND var)
497 (VM_ASSERT (SCM_IDENTIFIERP var))
498 (SCM_FLONUM_ENSURE_MEM val)
499 INCR-PC
500 (let* ([id::ScmIdentifier* (Scm_OutermostIdentifier (SCM_IDENTIFIER var))]
501 [mod::ScmModule* (-> id module)]
502 [name::ScmSymbol* (SCM_SYMBOL (-> id name))])
503 (case (SCM_VM_INSN_ARG code) ;flag
504 [(0) (Scm_MakeBinding mod name val 0)]
505 [(1 SCM_BINDING_CONST) (Scm_MakeBinding mod name val SCM_BINDING_CONST)]
506 [(SCM_BINDING_INLINABLE)(Scm_MakeBinding mod name val SCM_BINDING_INLINABLE)])
507 ($result (SCM_OBJ name)))))
508
509 ;; CLOSURE <code>
510 ;; Create a closure capturing current environment.
511 ;; CODE is the compiled code. Leaves created closure in val0.
512 ;;
513 (define-insn CLOSURE 0 code #f
514 (let* ((body))
515 (FETCH-OPERAND body)
516 INCR-PC
517 ($result (Scm_MakeClosure body (get_env vm)))))
518
519 ;; LOCAL-ENV(nlocals)
520 ;; Create a new environment frame from the current arg frame.
521 ;; Used for let.
522 (define-insn LOCAL-ENV 1 none #f
523 (begin (CHECK-STACK-PARANOIA (ENV-SIZE 0))
524 (FINISH-ENV SCM_FALSE ENV)
525 NEXT))
526
527 ;; combined insn
528 (define-insn PUSH-LOCAL-ENV 1 none (PUSH LOCAL-ENV)
529 (begin (CHECK-STACK-PARANOIA (ENV-SIZE 1))
530 (PUSH-ARG VAL0)
531 (FINISH-ENV SCM_FALSE ENV)
532 NEXT))
533
534 ;; LOCAL-ENV-CLOSURES(nlocals) <codelist>
535 ;; A special instruction for efficient handling of letrec.
536 ;; Similar to LOCAL-ENV, but this doesn't use the current arg frame.
537 ;; The operand contains a literal list of <compiled-code>s or <closure>s.
538 ;; This instruction creates a frame of NLOCALS size and initializes each
539 ;; slot with each element of <codelist>. If the element is a
540 ;; <compiled-code>, a new closure is created with the environment
541 ;; including the frame just created. If the element is a <closure>,
542 ;; it is just used (such <closure> does not refer to the outside environment).
543 ;; CODELIST can have 'holes', i.e. if it has #<undef>,
544 ;; the corresponding frame entry is left undefined.
545 ;; This instruction also leaves the last closure in VAL0.
546 (define-insn LOCAL-ENV-CLOSURES 1 codes #f
547 (let* ([nlocals::int (SCM_VM_INSN_ARG code)]
548 [z::ScmObj*] [cp] [clo SCM_UNDEFINED] [e::ScmEnvFrame*])
549 (FETCH-OPERAND cp)
550 INCR-PC
551 (CHECK-STACK-PARANOIA (ENV-SIZE nlocals))
552 (dotimes [i nlocals] (set! (* (post++ SP)) SCM_UNDEFINED))
553 (FINISH-ENV SCM_FALSE ENV)
554 (set! e (get_env vm))
555 (set! z (- (cast ScmObj* e) nlocals))
556 (dolist [c cp]
557 (cond [(SCM_COMPILED_CODE_P c)
558 (set! (* (post++ z)) (set! clo (Scm_MakeClosure c e)))]
559 [(SCM_PROCEDUREP c) (set! (* (post++ z)) c) (set! clo c)]
560 [else (set! (* (post++ z)) c)]))
561 ($result clo)))
562
563 ;; POP-LOCAL-ENV
564 ;; Pop one environment frame created by LOCAL-ENV. In practice,
565 ;; this is only used if 'let' is in the non-tail bottom position
566 ;; (for other cases, the env frame is discarded along other frame
567 ;; operations).
568 (define-insn POP-LOCAL-ENV 0 none #f
569 (begin (set! ENV (-> ENV up)) NEXT))
570
571 ;; LOCAL-ENV-JUMP(depth) <addr>
572 ;; Combination of LOCAL-ENV-SHIFT + JUMP.
573 ;; We can use this when none of the new environment needs boxing.
574 (define-insn LOCAL-ENV-JUMP 1 label #f
575 (begin
576 (local_env_shift vm (SCM_VM_INSN_ARG code))
577 (FETCH-LOCATION PC)
578 CHECK-INTR
579 NEXT))
580
581 ;; LOCAL-ENV-CALL(depth)
582 ;; LOCAL-ENV-TAIL-CALL(depth)
583 ;; This instruction appears when local function call is optimized.
584 ;; VAL0 has a closure to call, and the stack already has the arguments.
585 ;;
586 ;; This instruction creates an env frame with the existing
587 ;; values (just like LOCAL-ENV), then jump to the entrance point of
588 ;; the closure in VAL0. The point is that we can bypass the generic
589 ;; CALL sequence, since the arguments are already adjusted and we
590 ;; know the called closure is not a generic function.
591 ;; (# of arguments can be known by SP - ARGP).
592 (define-insn LOCAL-ENV-CALL 1 none #f
593 (let* ([nargs::int (cast int (- SP ARGP))])
594 (VM-ASSERT (SCM_CLOSUREP VAL0))
595 (cond [(> nargs 0)
596 (CHECK-STACK-PARANOIA (ENV-SIZE 0))
597 (FINISH-ENV SCM_FALSE (-> (SCM_CLOSURE VAL0) env))]
598 [else
599 (set! ENV (-> (SCM_CLOSURE VAL0) env))
600 (set! ARGP SP)])
601 (set! (-> vm base) (SCM_COMPILED_CODE (-> (SCM_CLOSURE VAL0) code)))
602 (set! PC (-> vm base code))
603 (CHECK-STACK (-> vm base maxstack))
604 CHECK-INTR
605 (SCM_PROF_COUNT_CALL vm (SCM_OBJ (-> vm base)))
606 NEXT)
607 :multi-value)
608
609 (define-insn LOCAL-ENV-TAIL-CALL 1 none #f
610 (let* ([nargs::int (cast int (- SP ARGP))] [to::ScmObj*])
611 (VM-ASSERT (SCM_CLOSUREP VAL0))
612 (if (IN-STACK-P (cast ScmObj* CONT))
613 (set! to (CONT-FRAME-END CONT))
614 (set! to (-> vm stackBase)))
615 (when (and (> nargs 0) (!= to ARGP))
616 (let* ([t::ScmObj* to] [a::ScmObj* ARGP])
617 (dotimes [c nargs]
618 (set! (* (post++ t)) (* (post++ a))))))
619 (set! ARGP to)
620 (set! SP (+ to nargs))
621 ($goto-insn LOCAL-ENV-CALL))
622 :multi-value)
623
624 ;; BF <else-offset> ; branch if VAL0 is false
625 ;; BT <else-offset> ; branch if VAL0 is true
626 ;; BNNULL <else-offset> ; branch if VAL0 is not null
627 ;; BNEQ <else-offset> ; branch if VAL0 is not eq? to (POP)
628 ;; BNEQV <else-offset> ; branch if VAL0 is not eqv? to (POP)
629 ;; BNUMNE <else-offset> ; branch if (VAL0 != (POP))
630 ;; BNLT <else-offset> ; branch if !((POP) < VAL0)
631 ;; BNLE <else-offset> ; branch if !((POP) <= VAL0)
632 ;; BNGT <else-offset> ; branch if !((POP) > VAL0)
633 ;; BNGE <else-offset> ; branch if !((POP) >= VAL0)
634 ;; Conditional branches.
635 ;; The combined operations leave the boolean value of the test result
636 ;; in VAL0.
637 (define-insn BF 0 label #f ($branch (SCM_CHECKED_FALSEP VAL0)))
638 (define-insn BT 0 label #f ($branch (not (SCM_CHECKED_FALSEP VAL0))))
639 (define-insn BNEQ 0 label #f ($w/argp z ($branch* (not (SCM_EQ VAL0 z)))))
640 (define-insn BNEQV 0 label #f ($w/argp z ($branch* (not (Scm_EqvP VAL0 z)))))
641 (define-insn BNNULL 0 label #f ($branch* (not (SCM_NULLP VAL0))))
642
643 (define-insn BNUMNE 0 label #f (let* ((y VAL0))
644 ($w/argp x ($branch* (not (Scm_NumEq x y))))))
645 (define-insn BNLT 0 label #f ($w/numcmp r < ($branch* (not r))))
646 (define-insn BNLE 0 label #f ($w/numcmp r <= ($branch* (not r))))
647 (define-insn BNGT 0 label #f ($w/numcmp r > ($branch* (not r))))
648 (define-insn BNGE 0 label #f ($w/numcmp r >= ($branch* (not r))))
649
650 ;; Compare LREF(n,m) and VAL0 and branch. This is not a simple combination
651 ;; of LREF + BNLT etc. (which would compare stack top and LREF). These insns
652 ;; save one stack operation. The compiler recognizes the pattern and
653 ;; emits these. See pass5/if-numcmp and pass5/if-numeq.
654 (define-insn LREF-VAL0-BNUMNE 2 label #f ($arg-source lref ($insn-body BNUMNE)))
655 (define-insn LREF-VAL0-BNLT 2 label #f ($arg-source lref ($insn-body BNLT)))
656 (define-insn LREF-VAL0-BNLE 2 label #f ($arg-source lref ($insn-body BNLE)))
657 (define-insn LREF-VAL0-BNGT 2 label #f ($arg-source lref ($insn-body BNGT)))
658 (define-insn LREF-VAL0-BNGE 2 label #f ($arg-source lref ($insn-body BNGE)))
659
660 ;; BNUMNEI(i) <else-offset> ; combined CONSTI(i) + BNUMNE
661 ;; BNEQC <else-offset> ; branch if immediate constant is not eq? to VAL0
662 ;; BNEQVC <else-offset> ; branch if immediate constant is not eqv? to VAL0
663 ;; NB: we tried other variations of constant op + branch combination,
664 ;; notably BNEQVI, BNUMNEF, BNLTF etc, but they did't show any
665 ;; improvement.
666 (define-insn BNUMNEI 1 label #f
667 (let* ([imm::long (SCM_VM_INSN_ARG code)])
668 ($w/argr v0
669 ($type-check v0 SCM_NUMBERP "number")
670 ($branch*
671 (not (or (and (SCM_INTP v0) (== (SCM_INT_VALUE v0) imm))
672 (and (SCM_FLONUMP v0) (== (SCM_FLONUM_VALUE v0) imm))))))))
673 (define-insn BNEQC 0 obj+label #f
674 (let* ([z]) (FETCH-OPERAND z) INCR-PC ($branch* (not (SCM_EQ VAL0 z)))))
675 (define-insn BNEQVC 0 obj+label #f
676 (let* ([z]) (FETCH-OPERAND z) INCR-PC ($branch* (not (Scm_EqvP VAL0 z)))))
677
678 ;; RF
679 ;; RT
680 ;; RNNULL
681 ;; RNEQ
682 ;; RNEQV
683 ;; Conditional returns.
684 (define-insn RF 0 none #f ($retc (SCM_CHECKED_FALSEP VAL0)))
685 (define-insn RT 0 none #f ($retc (not (SCM_CHECKED_FALSEP VAL0))))
686 (define-insn RNEQ 0 none #f ($w/argp v
687 ($retc* (not (SCM_EQ VAL0 v)))))
688 (define-insn RNEQV 0 none #f ($w/argp v
689 ($retc* (not (Scm_EqvP VAL0 v)))))
690 (define-insn RNNULL 0 none #f ($retc* (not (SCM_NULLP VAL0))))
691
692 ;;
693 ;; Common stuff for RECEIVE and TAIL-RECEIVE.
694 ;;
695 (define-cise-stmt $receive
696 [(_ . stmts)
697 `(let* ([reqargs::int (SCM_VM_INSN_ARG0 code)]
698 [restarg::int (SCM_VM_INSN_ARG1 code)]
699 [size::int] [i::int 0] [argsize::int] [rest SCM_NIL] [tail SCM_NIL]
700 [nextpc::ScmWord*])
701 (when (< (-> vm numVals) reqargs)
702 ($vm-err "received fewer values than expected"))
703 (when (and (not restarg) (> (-> vm numVals) reqargs))
704 ($vm-err "received more values than expected"))
705 (set! argsize (+ reqargs (?: restarg 1 0)))
706 (cast void argsize) ; suppress unused variable warning
707 (cast void nextpc) ; suppress unused variable warning
708 (cast void size) ; suppress unused variable warning
709 ,@stmts
710 (cond [(> reqargs 0) (PUSH-ARG VAL0) (post++ i)]
711 [(and restarg (> (-> vm numVals) 0))
712 (SCM_APPEND1 rest tail VAL0)
713 (post++ i)])
714 (for [() (< i reqargs) (post++ i)]
715 (PUSH-ARG (aref (-> vm vals) (- i 1))))
716 (when restarg
717 (for [() (< i (-> vm numVals)) (post++ i)]
718 (SCM_APPEND1 rest tail (aref (-> vm vals) (- i 1))))
719 (PUSH-ARG rest))
720 (FINISH-ENV SCM_FALSE ENV)
721 (set! (-> vm numVals) 1) ; we already processed extra vals, so reset it
722 NEXT)])
723
724 ;; RECEIVE(nargs,restarg) <cont-offset>
725 ;; Primitive operation for receive and call-with-values.
726 ;; Turn the value(s) into an environment.
727 ;; Like LET, this pushes the continuation frame to resume the
728 ;; operation from CONT-OFFSET.
729 ;;
730 (define-insn RECEIVE 2 label #f
731 ($receive (set! size (+ CONT_FRAME_SIZE (ENV_SIZE (+ reqargs restarg))))
732 (CHECK-STACK-PARANOIA size)
733 (FETCH-LOCATION nextpc)
734 INCR-PC
735 (PUSH-CONT nextpc)))
736
737 ;; TAIL-RECEIVE(nargs,restarg)
738 ;; Tail position of receive. No need to push the continuation.
739 ;; Actually, TAIL-RECEIVE can be used anywhere if there's no
740 ;; argument pushed after the last continuation frame. See TAIL-LET.
741 ;;
742 (define-insn TAIL-RECEIVE 2 none #f
743 ($receive (set! size (ENV_SIZE (+ reqargs restarg)))))
744
745 ;; RECEIVE-ALL <cont>
746 ;; A special version of RECEIVE that pushes all the current values
747 ;; into the stack and makes them into an environment.
748 ;; It is used primarily for the occasions that compiler knows it needs
749 ;; to save the values temporarily (e.g. for evaluate 'after' thunk of
750 ;; dynamic-wind, the results of its body needs to be saved).
751 ;; This must be twined with VALUES-N, which reverses the effects, i.e.
752 ;; turn the values in the env frame into values.
753 (define-insn RECEIVE-ALL 0 label #f
754 (let* ([nextpc::ScmWord*])
755 (CHECK-STACK-PARANOIA CONT_FRAME_SIZE)
756 (FETCH-LOCATION nextpc)
757 INCR_PC
758 (PUSH-CONT nextpc)
759 ($goto-insn TAIL-RECEIVE-ALL)))
760
761 ;; TAIL-RECEIVE-ALL
762 ;; Tail version of RECEIVE-ALL.
763 (define-insn TAIL-RECEIVE-ALL 0 none #f
764 (begin (CHECK-STACK-PARANOIA (ENV-SIZE (+ (-> vm numVals) 1)))
765 (PUSH-ARG VAL0)
766 (dotimes [i (- (-> vm numVals) 1)]
767 (PUSH-ARG (aref (-> vm vals) i)))
768 (FINISH-ENV SCM_FALSE ENV)
769 NEXT))
770
771 ;; VALUES-N
772 ;; Inverse of RECEIVE-ALL. Transfer the current environment content
773 ;; to the values register, and pop the env.
774 ;; Differ from VALUES since this one doesn't know # of values beforehand.
775 (define-insn VALUES-N 0 none #f
776 (begin
777 (VM-ASSERT ENV)
778 (let* ([nvals::int (cast int (-> ENV size))])
779 (set! (-> vm numVals) nvals)
780 (for [() (> nvals 1) (post-- nvals)]
781 (POP-ARG (aref (-> vm vals) (- nvals 1))))
782 (POP-ARG VAL0)
783 NEXT))
784 :multi-value)
785
786 ;; LSET(depth, offset)
787 ;; Local set
788 ;;
789 (define-insn LSET 2 none #f
790 ($lset (SCM_VM_INSN_ARG0 code) ; depth
791 (SCM_VM_INSN_ARG1 code))) ; offset
792
793 ;; XLSET(offset) depth
794 ;; LSET, but used when depth and/or offset can't fit in the params field.
795 (define-insn XLSET 1 obj #f
796 (let* ([dep_s])
797 (FETCH-OPERAND dep_s)
798 INCR-PC
799 ($lset (SCM_INT_VALUE dep_s) ; depth
800 (SCM_VM_INSN_ARG code)))) ; offset
801
802 ;; ENV-SET(offset)
803 ;; Mutate the top env's specified slot with VAL0
804 ;; This is used with LOCAL-ENV-CLOSURES to initialize non-procedure
805 ;; slots of the env. We used to use LSET for this purpose, but now
806 ;; LSET counts on that all mutable lvars are boxed, so we need a separete
807 ;; insn that directly initialize the env frame.
808 (define-insn ENV-SET 1 none #f
809 (let* ([off::int (SCM_VM_INSN_ARG code)])
810 (VM-ASSERT (> (-> ENV size) off))
811 (SCM_FLONUM_ENSURE_MEM VAL0)
812 (set! (ENV-DATA ENV off) VAL0)
813 NEXT))
814
815 ;; GSET <location>
816 ;; LOCATION may be a symbol or gloc
817 ;;
818 (define-insn GSET 0 obj #f
819 (let* ((loc))
820 (FETCH-OPERAND loc)
821 (SCM_FLONUM_ENSURE_MEM VAL0)
822 (cond
823 [(SCM_GLOCP loc) (SCM_GLOC_SET (SCM_GLOC loc) VAL0)]
824 [(SCM_IDENTIFIERP loc)
825 (let* ([gloc::ScmGloc* NULL])
826 (Scm_IdentifierGlobalSet (SCM_IDENTIFIER loc) VAL0 (& gloc))
827 ;; memoize gloc.
828 (set! (* PC) (SCM_WORD gloc)))]
829 [else ($vm-err "GSET: can't be here")])
830 INCR-PC
831 (set! (-> vm numVals) 1)
832 NEXT))
833
834 ;; LREF(depth,offset)
835 ;; Retrieve local value.
836 ;;
837 (define-insn LREF 2 none #f
838 ($lref (SCM_VM_INSN_ARG0 code) ; depth
839 (SCM_VM_INSN_ARG1 code))) ; offset
840
841 ;; Shortcut for the frequent depth/offset.
842 ;; From statistics, we found that the following depth/offset combinations
843 ;; are quite frequent:
844 ;; (0,0) (0,1) (0,2) (0,3)
845 ;; (1,0) (1,1) (1,2)
846 ;; (2,0) (2,1)
847 ;; (3,0)
848 (define-insn LREF0 0 none #f ($lrefNN 0 0))
849 (define-insn LREF1 0 none #f ($lrefNN 0 1))
850 (define-insn LREF2 0 none #f ($lrefNN 0 2))
851 (define-insn LREF3 0 none #f ($lrefNN 0 3))
852 (define-insn LREF10 0 none #f ($lrefNN 1 0))
853 (define-insn LREF11 0 none #f ($lrefNN 1 1))
854 (define-insn LREF12 0 none #f ($lrefNN 1 2))
855 (define-insn LREF20 0 none #f ($lrefNN 2 0))
856 (define-insn LREF21 0 none #f ($lrefNN 2 1))
857 (define-insn LREF30 0 none #f ($lrefNN 3 0))
858
859 ;; combined instruction
860 (define-insn LREF-PUSH 2 none (LREF PUSH))
861 (define-insn LREF0-PUSH 0 none (LREF0 PUSH))
862 (define-insn LREF1-PUSH 0 none (LREF1 PUSH))
863 (define-insn LREF2-PUSH 0 none (LREF2 PUSH))
864 (define-insn LREF3-PUSH 0 none (LREF3 PUSH))
865 (define-insn LREF10-PUSH 0 none (LREF10 PUSH))
866 (define-insn LREF11-PUSH 0 none (LREF11 PUSH))
867 (define-insn LREF12-PUSH 0 none (LREF12 PUSH))
868 (define-insn LREF20-PUSH 0 none (LREF20 PUSH))
869 (define-insn LREF21-PUSH 0 none (LREF21 PUSH))
870 (define-insn LREF30-PUSH 0 none (LREF30 PUSH))
871
872 (define-insn-lref* LREF-RET 0 none (LREF RET))
873
874 ;; XLREF(offset) depth
875 ;; LREF, but used when depth and/or offset can't fit in the params field.
876 (define-insn XLREF 1 obj #f
877 (let* ([dep_s])
878 (FETCH-OPERAND dep_s)
879 INCR-PC
880 ($lref (SCM_INT_VALUE dep_s) ;depth
881 (SCM_VM_INSN_ARG code)))) ;offset
882
883 ;; GREF <location>
884 ;; LOCATION may be a symbol or GLOC object.
885 ;; Retrieve global value in the current module.
886 ;;
887 (define-insn GREF 0 obj #f
888 (let* ((v)) (GLOBAL-REF v) ($result v)))
889
890 ;; combined instructions
891 ;; NB: PUSH-GREF itself isn't a very useful form, but it works as a
892 ;; transient state to PUSH-GREF-CALL / PUSH-GREF-TAIL-CALL, which are
893 ;; very frequent operation, during instruction combining.
894
895 (define-insn GREF-PUSH 0 obj (GREF PUSH))
896 (define-insn GREF-CALL 1 obj (GREF CALL))
897 (define-insn GREF-TAIL-CALL 1 obj (GREF TAIL-CALL))
898
899 (define-insn PUSH-GREF 0 obj (PUSH GREF))
900 (define-insn PUSH-GREF-CALL 1 obj (PUSH GREF CALL))
901 (define-insn PUSH-GREF-TAIL-CALL 1 obj (PUSH GREF TAIL-CALL))
902
903 ;; PROMISE
904 ;; Delay syntax emits this instruction. Wrap a procedure into a promise
905 ;; object.
906 ;;
907 (define-insn PROMISE 0 none #f
908 ($result (Scm_MakePromise SCM_PROMISE_UNFORCED VAL0)))
909
910 ;; VALUES-APPLY(nargs) <args>
911 ;; This instruction only appears in the code generated dynamically
912 ;; by Scm_Apply(Rec). This is used to pass the application information
913 ;; across the boundary frame (see user_eval_inner() in vm.c).
914 ;; When the VM sees this instruciton, VAL0 contains the procedure to
915 ;; call, and VAL1... contains the arguments.
916 ;; If nargs >= SCM_VM_MAX_VALUES-1, args[SCM_VM_MAX_VALUES-1] through
917 ;; args[nargs-1] are made into a list and stored in VALS[SCM_VM_MAX_VALUES-1]
918 (define-insn VALUES-APPLY 0 none #f
919 (let* ([nargs::int (SCM_VM_INSN_ARG code)])
920 (CHECK-STACK (ENV-SIZE nargs))
921 (dotimes [i nargs]
922 (when (>= i (- SCM_VM_MAX_VALUES 1))
923 (for-each (lambda (vv) (PUSH-ARG vv))
924 (aref (-> vm vals) (- SCM_VM_MAX_VALUES 1)))
925 (break))
926 (PUSH-ARG (aref (-> vm vals) i)))
927 ($goto-insn TAIL-CALL))
928 :multi-value)
929
930 ;; Inlined operators
931 ;; They work the same as corresponding Scheme primitives, but they are
932 ;; directly interpreted by VM, skipping argument processing part.
933 ;; Compiler may insert these in order to fulfill the operation (e.g.
934 ;; `case' needs MEMV). If the optimization level is high, global
935 ;; reference of those primitive calls in the user code are replaced
936 ;; as well.
937 ;;
938 (define-insn CONS 0 none #f
939 (let* ([ca]) (POP-ARG ca) ($result (Scm_Cons ca VAL0))))
940 (define-insn CONS-PUSH 0 none (CONS PUSH))
941
942 (define-insn CAR 0 none #f
943 ($w/argr v ($type-check v SCM_PAIRP "pair") ($result (SCM_CAR v))))
944 (define-insn CAR-PUSH 0 none (CAR PUSH))
945 (define-insn-lref+ LREF-CAR 0 none (LREF CAR))
946
947 (define-insn CDR 0 none #f
948 ($w/argr v ($type-check v SCM_PAIRP "pair") ($result (SCM_CDR v))))
949 (define-insn CDR-PUSH 0 none (CDR PUSH))
950 (define-insn-lref+ LREF-CDR 0 none (LREF CDR))
951
952 (define-cise-stmt $cxxr
953 [(_ a b)
954 `($w/argr obj
955 ($type-check obj SCM_PAIRP "pair")
956 (let* ([obj2 (,b obj)])
957 ($type-check obj2 SCM_PAIRP "pair")
958 ($result (,a obj2))))])
959
960 (define-insn CAAR 0 none #f ($cxxr SCM_CAR SCM_CAR))
961 (define-insn CAAR-PUSH 0 none (CAAR PUSH))
962 (define-insn CADR 0 none #f ($cxxr SCM_CAR SCM_CDR))
963 (define-insn CADR-PUSH 0 none (CADR PUSH))
964 (define-insn CDAR 0 none #f ($cxxr SCM_CDR SCM_CAR))
965 (define-insn CDAR-PUSH 0 none (CDAR PUSH))
966 (define-insn CDDR 0 none #f ($cxxr SCM_CDR SCM_CDR))
967 (define-insn CDDR-PUSH 0 none (CDDR PUSH))
968
969 (define-insn LIST 1 none #f
970 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [cp SCM_NIL] [arg])
971 (when (> nargs 0)
972 (SCM_FLONUM_ENSURE_MEM VAL0)
973 (set! cp (Scm_Cons VAL0 cp))
974 (while (> (pre-- nargs) 0)
975 (POP-ARG arg)
976 (set! cp (Scm_Cons arg cp))))
977 ($result cp)))
978
979 (define-insn LIST-STAR 1 none #f ; list*
980 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [cp SCM_NIL] [arg])
981 (VM-ASSERT (>= nargs 1))
982 (SCM_FLONUM_ENSURE_MEM VAL0)
983 (set! cp VAL0)
984 (while (> (pre-- nargs) 0)
985 (POP-ARG arg)
986 (set! cp (Scm_Cons arg cp)))
987 ($result cp)))
988
989 (define-insn LENGTH 0 none #f ; length
990 (let* ([len::int (Scm_Length VAL0)])
991 (when (< len 0) ($vm-err "proper list required, but got %S" VAL0))
992 ($result:i len)))
993
994 (define-insn MEMQ 0 none #f ($w/argp v ($result (Scm_Memq v VAL0))))
995 (define-insn MEMV 0 none #f ($w/argp v ($result (Scm_Memv v VAL0))))
996 (define-insn ASSQ 0 none #f ($w/argp v ($result (Scm_Assq v VAL0))))
997 (define-insn ASSV 0 none #f ($w/argp v ($result (Scm_Assv v VAL0))))
998 (define-insn EQ 0 none #f ($w/argp v ($result:b (SCM_EQ v VAL0))))
999 (define-insn EQV 0 none #f ($w/argp v ($result:b (Scm_EqvP v VAL0))))
1000
1001 (define-insn APPEND 1 none #f
1002 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [cp SCM_NIL] [args SCM_NIL] [a])
1003 ;; We special-case up to 2 args to save allocation of extra pairs
1004 ;; for argument lists.
1005 (case nargs
1006 [(0) (break)]
1007 [(1) (set! cp VAL0) (break)]
1008 [(2) (SCM_FLONUM_ENSURE_MEM VAL0)
1009 (POP-ARG a)
1010 (set! cp (Scm_Append2 a VAL0))
1011 (break)]
1012 [else
1013 (set! args (Scm_Cons VAL0 SCM_NIL))
1014 ;; We want to pop all args before doing works, for Scm_Append may cause
1015 ;; forcing lazy-pair.
1016 (while (> (pre-- nargs) 0)
1017 (POP-ARG a)
1018 (set! args (Scm_Cons a args)))
1019 (set! cp (Scm_Append args))])
1020 ($result cp)))
1021
1022 (define-insn NOT 0 none #f ($w/argr v ($result:b (SCM_CHECKED_FALSEP v))))
1023 (define-insn REVERSE 0 none #f ($w/argr v ($result (Scm_Reverse v))))
1024
1025 (define-insn APPLY 1 none #f
1026 ;; this instruction will go away soon. for now it only appears
1027 ;; as the result of 'cond' with SRFI-61 extension.
1028 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [cp])
1029 (SCM_FLONUM_ENSURE_MEM VAL0)
1030 (while (> (pre-- nargs) 1)
1031 (POP-ARG cp)
1032 (set! VAL0 (Scm_Cons cp VAL0)))
1033 (set! cp VAL0) ; now cp has arg list
1034 (POP-ARG VAL0) ; get proc
1035 (TAIL-CALL-INSTRUCTION)
1036 (set! VAL0 (Scm_VMApply VAL0 cp))
1037 NEXT)
1038 :multi-value)
1039
1040 (define-insn TAIL-APPLY 1 none #f
1041 ;; Inlined apply. Assumes the call is at the tail position.
1042 ;; NB: As of 0.9, all 'apply' call is expanded into this instruction.
1043 ;; If the code is not at the tail position, compiler pass5 inserts
1044 ;; PRE-CALL instruction so that the call of apply becomes a tail call.
1045 ;;
1046 ;; Here, the stack should have the following layout.
1047 ;;
1048 ;; SP >| |
1049 ;; | argN |
1050 ;; | : |
1051 ;; | arg0 |
1052 ;; ARGP>| proc | VAL0=rest
1053 ;;
1054 ;; where N = SCMVM_INSN_ARG(code)-2.
1055 ;; rest contains a list of "all the rest arguments".
1056 ;; We just push VAL0 onto stack, and move ARGP
1057 ;; We "rotate" the stack and VAL0 and jump to the
1058 ;; tail_apply_entry. (The unfolding of rest
1059 ;; argument will be done in ADJUST_ARGUMENT_FRAME later,
1060 ;; if necessary.)
1061 ;;
1062 ;; SP >| |
1063 ;; | rest |< original SP position
1064 ;; | argN |
1065 ;; | : |
1066 ;; ARGP>| arg0 | VAL0=proc
1067 ;; | proc |< original ARGP postiion (proc unused)
1068 (let* ([rest VAL0]
1069 [nargc::int (- (SCM_VM_INSN_ARG code) 2)]
1070 [proc (* (- SP nargc 1))])
1071 (set! VAL0 proc)
1072 (post++ ARGP)
1073 ;; a micro-optimization: if the rest arg is (), we just omit it and
1074 ;; pretend this is a normal TAIL-CALL.
1075 (when (SCM_NULLP rest) ($goto-insn TAIL-CALL))
1076 ;; Checking if rest argument is proper is done after tail_apply_entry.
1077 ;; Here we just ensure that rest is a normal pair (If rest is a lazy
1078 ;; pair, it is forced by SCM_PAIRP).
1079 (unless (SCM_PAIRP rest)
1080 ($vm-err "Rest argument is not proper: %S" rest))
1081 (set! VAL0 proc) ;VAL0 may be broken by lazy pair evaluation
1082 ;; normal path
1083 (set! (* (post++ SP)) rest)
1084 (DISCARD-ENV)
1085 (goto tail_apply_entry))
1086 :multi-value)
1087
1088 (define-insn IS-A 0 none #f ; is-a?
1089 ($w/argp obj
1090 ($type-check VAL0 SCM_CLASSP "class")
1091 (let* ([c::ScmClass* (SCM_CLASS VAL0)])
1092 ;; be careful to handle class redifinition case
1093 (cond [(not (SCM_FALSEP (-> (Scm_ClassOf obj) redefined)))
1094 (Scm__VMProtectStack vm)
1095 ($result (Scm_VMIsA obj c))]
1096 [else ($result:b (SCM_ISA obj c))]))))
1097
1098 (define-insn NULLP 0 none #f ($w/argr v ($result:b (SCM_NULLP v))))
1099 (define-insn PAIRP 0 none #f ($w/argr v ($result:b (SCM_PAIRP v))))
1100 (define-insn CHARP 0 none #f ($w/argr v ($result:b (SCM_CHARP v))))
1101 (define-insn EOFP 0 none #f ($w/argr v ($result:b (SCM_EOFP v))))
1102 (define-insn STRINGP 0 none #f ($w/argr v ($result:b (SCM_STRINGP v))))
1103 (define-insn SYMBOLP 0 none #f ($w/argr v ($result:b (SCM_SYMBOLP v))))
1104 (define-insn VECTORP 0 none #f ($w/argr v ($result:b (SCM_VECTORP v))))
1105 (define-insn NUMBERP 0 none #f ($w/argr v ($result:b (SCM_NUMBERP v))))
1106 (define-insn REALP 0 none #f ($w/argr v ($result:b (SCM_REALP v))))
1107 (define-insn IDENTIFIERP 0 none #f ($w/argr v ($result:b (or (SCM_SYMBOLP v) (SCM_IDENTIFIERP v)))))
1108
1109 (define-insn SETTER 0 none #f ($w/argr v ($result (Scm_Setter v))))
1110
1111 (define-insn VALUES 1 none #f (begin ($values) NEXT) :multi-value)
1112 (define-insn VALUES-RET 1 none (VALUES RET) (begin ($values) (RETURN-OP) NEXT) :multi-value)
1113
1114 (define-insn VEC 1 none #f ; vector
1115 (let* ([nargs::int (SCM_VM_INSN_ARG code)]
1116 [i::int (- nargs 1)]
1117 [vec (Scm_MakeVector nargs SCM_UNDEFINED)])
1118 (when (> nargs 0)
1119 (let* ([arg VAL0])
1120 (for [() (> i 0) (post-- i)]
1121 (SCM_FLONUM_ENSURE_MEM arg)
1122 (set! (SCM_VECTOR_ELEMENT vec i) arg)
1123 (POP-ARG arg))
1124 (SCM_FLONUM_ENSURE_MEM arg)
1125 (set! (SCM_VECTOR_ELEMENT vec 0) arg)))
1126 ($result vec)))
1127
1128 (define-insn LIST2VEC 0 none #f ; list->vector
1129 ($w/argr v ($result (Scm_ListToVector v 0 -1))))
1130
1131 (define-insn APP-VEC 1 none #f ; (compose list->vector append)
1132 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [cp SCM_NIL] [args SCM_NIL] [a])
1133 (when (> nargs 0)
1134 (SCM_FLONUM_ENSURE_MEM VAL0)
1135 (set! cp VAL0)
1136 (while (> (pre-- nargs) 0)
1137 (POP-ARG a)
1138 (set! args (Scm_Cons a args)))
1139 (dolist [a (Scm_ReverseX args)]
1140 (when (< (Scm_Length a) 0) ($vm-err "list required, but got %S" a))
1141 (set! cp (Scm_Append2 a cp))))
1142 ($result (Scm_ListToVector cp 0 -1))))
1143
1144 (define-insn VEC-LEN 0 none #f ; vector-length
1145 ($w/argr v
1146 ($type-check v SCM_VECTORP "vector")
1147 ($result:i (SCM_VECTOR_SIZE v))))
1148
1149 (define-insn VEC-REF 0 none #f ; vector-ref
1150 (let* ([k VAL0])
1151 ($w/argp vec
1152 ($type-check vec SCM_VECTORP "vector")
1153 ($type-check k SCM_INTP "fixnum")
1154 (when (or (< (SCM_INT_VALUE k) 0)
1155 (>= (SCM_INT_VALUE k) (SCM_VECTOR_SIZE vec)))
1156 ($vm-err "vector-ref index out of range: %S" k))
1157 ($result (SCM_VECTOR_ELEMENT vec (SCM_INT_VALUE k))))))
1158
1159 (define-insn VEC-SET 0 none #f ; vector-set
1160 (let* ([vec] [ind])
1161 (POP-ARG ind)
1162 (POP-ARG vec)
1163 ($type-check vec SCM_VECTORP "vector")
1164 ($type-check ind SCM_INTP "fixnum")
1165 (when (SCM_VECTOR_IMMUTABLE_P vec)
1166 ($vm-err "vector is immutable: %S" vec))
1167 (let* ([k::int (SCM_INT_VALUE ind)] [v VAL0])
1168 (when (or (< k 0) (>= k (SCM_VECTOR_SIZE vec)))
1169 ($vm-err "vector-set! index out of range: %d" k))
1170 (SCM_FLONUM_ENSURE_MEM v)
1171 (set! (SCM_VECTOR_ELEMENT vec k) v)
1172 ($result SCM_UNDEFINED))))
1173
1174 ;; VEC-REF and VEC-SET with immediate index. VAL0 must be a vector.
1175 (define-insn VEC-REFI 1 none #f
1176 ($w/argr vec
1177 ($type-check vec SCM_VECTORP "vector")
1178 (let* ([k::int (SCM_VM_INSN_ARG code)])
1179 (when (or (< k 0) (>= k (SCM_VECTOR_SIZE vec)))
1180 ($vm-err "vector-ref index out of range: %d" k))
1181 ($result (SCM_VECTOR_ELEMENT vec k)))))
1182
1183 (define-insn VEC-SETI 1 none #f
1184 ($w/argp vec
1185 ($type-check vec SCM_VECTORP "vector")
1186 (when (SCM_VECTOR_IMMUTABLE_P vec)
1187 ($vm-err "vector is immutable: %S" vec))
1188 (let* ([k::int (SCM_VM_INSN_ARG code)] [v VAL0])
1189 (when (or (< k 0) (>= k (SCM_VECTOR_SIZE vec)))
1190 ($vm-err "vector-set! index out of range: %d" k))
1191 (SCM_FLONUM_ENSURE_MEM v)
1192 (set! (SCM_VECTOR_ELEMENT vec k) v)
1193 ($result SCM_UNDEFINED))))
1194
1195 (define-insn UVEC-REF 1 none #f ; uvector-ref
1196 (let* ([k VAL0]
1197 [utype::int (SCM_VM_INSN_ARG code)])
1198 ;; Validation of utype and vec, and the range of k, is done in
1199 ;; Scm_VMUVectorRef.
1200 ($w/argp vec
1201 ($type-check k SCM_INTP "fixnum")
1202 ($result (Scm_VMUVectorRef (SCM_UVECTOR vec) utype (SCM_INT_VALUE k)
1203 SCM_UNBOUND)))))
1204
1205 ;; not enough evidence yet to support this is worth
1206 ;; (define-insn UVEC-REFI 1 none #f ; uvector-ref, index in arg.
1207 ;; (let* ([arg::int (SCM_VM_INSN_ARG code)]
1208 ;; [utype::uint (logand arg #x0f)]
1209 ;; [k::uint (>> arg 4)])
1210 ;; ($w/argr vec
1211 ;; (unless (SCM_UVECTOR_SUBTYPE_P vec utype)
1212 ;; ($vm-err "%s required, but got %S" (Scm_UVectorTypeName utype) vec))
1213 ;; (when (>= k (SCM_UVECTOR_SIZE vec))
1214 ;; ($vm-err "uvector-ref index out of range: %d" k))
1215 ;; ($result (Scm_VMUVectorRef (SCM_UVECTOR vec) utype k SCM_UNBOUND)))))
1216
1217 (define-insn NUMEQ2 0 none #f ; =
1218 ($w/argp arg
1219 (cond
1220 [(and (SCM_INTP VAL0) (SCM_INTP arg)) ($result:b (== VAL0 arg))]
1221 [(and (SCM_FLONUMP VAL0) (SCM_FLONUMP arg))
1222 ($result:b (== (SCM_FLONUM_VALUE VAL0) (SCM_FLONUM_VALUE arg)))]
1223 [else ($result:b (Scm_NumEq arg VAL0))])))
1224
1225 (define-insn NUMLT2 0 none #f ($w/numcmp r < ($result:b r)))
1226 (define-insn NUMLE2 0 none #f ($w/numcmp r <= ($result:b r)))
1227 (define-insn NUMGT2 0 none #f ($w/numcmp r > ($result:b r)))
1228 (define-insn NUMGE2 0 none #f ($w/numcmp r >= ($result:b r)))
1229
1230 (define-insn NUMADD2 0 none #f ; +
1231 ($w/argp arg
1232 (cond
1233 [(and (SCM_INTP arg) (SCM_INTP VAL0))
1234 ($result:n (+ (SCM_INT_VALUE arg) (SCM_INT_VALUE VAL0)))]
1235 [(and (SCM_FLONUMP arg) (SCM_FLONUMP VAL0))
1236 ($result:f (+ (SCM_FLONUM_VALUE arg) (SCM_FLONUM_VALUE VAL0)))]
1237 [else ($result (Scm_Add arg VAL0))])))
1238
1239 (define-insn NUMSUB2 0 none #f ; - (binary)
1240 ($w/argp arg
1241 (cond
1242 [(and (SCM_INTP arg) (SCM_INTP VAL0))
1243 ($result:n (- (SCM_INT_VALUE arg) (SCM_INT_VALUE VAL0)))]
1244 [(and (SCM_FLONUMP arg) (SCM_FLONUMP VAL0))
1245 ($result:f (- (SCM_FLONUM_VALUE arg) (SCM_FLONUM_VALUE VAL0)))]
1246 [else ($result (Scm_Sub arg VAL0))])))
1247
1248 (define-insn NUMMUL2 0 none #f ; *
1249 ($w/argp arg
1250 ;; we take a shortcut if either one is flonum and the
1251 ;; other is real. (if both are integers, the overflow check
1252 ;; would be cumbersome so we just call Scm_Mul).
1253 ;; NB: If one arg is inexact real and another arg is exact zero,
1254 ;; the result should be an exact zero.
1255 (if (SCM_FLONUMP arg)
1256 (cond [(and (== (SCM_MAKE_INT 0) VAL0)
1257 (not (SCM_IS_INF (SCM_FLONUM_VALUE arg)))
1258 (not (SCM_IS_NAN (SCM_FLONUM_VALUE arg))))
1259 ($result (SCM_MAKE_INT 0))]
1260 [(SCM_REALP VAL0)
1261 ($result:f (* (Scm_GetDouble arg) (Scm_GetDouble VAL0)))]
1262 [else ($result (Scm_Mul arg VAL0))])
1263 (if (SCM_FLONUMP VAL0)
1264 (cond [(and (== (SCM_MAKE_INT 0) arg)
1265 (not (SCM_IS_INF (SCM_FLONUM_VALUE VAL0)))
1266 (not (SCM_IS_NAN (SCM_FLONUM_VALUE VAL0))))
1267 ($result (SCM_MAKE_INT 0))]
1268 [(SCM_REALP arg)
1269 ($result:f (* (Scm_GetDouble arg) (Scm_GetDouble VAL0)))]
1270 [else ($result (Scm_Mul arg VAL0))])
1271 ($result (Scm_Mul arg VAL0))))))
1272
1273 (define-insn NUMDIV2 0 none #f ; / (binary)
1274 ($w/argp arg
1275 (if (or (and (SCM_FLONUMP arg) (SCM_REALP VAL0))
1276 (and (SCM_FLONUMP VAL0) (SCM_REALP arg)))
1277 ($result:f (/ (Scm_GetDouble arg) (Scm_GetDouble VAL0)))
1278 ($result (Scm_Div arg VAL0)))))
1279
1280 (define-insn LREF-VAL0-NUMADD2 2 none #f ($arg-source lref ($insn-body NUMADD2)))
1281
1282 (define-insn NEGATE 0 none #f ; - (unary)
1283 ($w/argr v
1284 (cond
1285 [(SCM_INTP v) ($result:n (- (SCM_INT_VALUE v)))]
1286 [(SCM_FLONUMP v) ($result:f (- (Scm_GetDouble v)))]
1287 [else ($result (Scm_Negate v))])))
1288
1289 (define-insn NUMIADD2 0 none #f ; +.
1290 ($w/argp arg
1291 (if (and (SCM_REALP arg) (SCM_REALP VAL0))
1292 ($result:f (+ (Scm_GetDouble arg) (Scm_GetDouble VAL0)))
1293 ($result (Scm_Add (Scm_Inexact arg)
1294 (Scm_Inexact VAL0))))))
1295
1296 (define-insn NUMISUB2 0 none #f ; -. (binary)
1297 ($w/argp arg
1298 (if (and (SCM_REALP arg) (SCM_REALP VAL0))
1299 ($result:f (- (Scm_GetDouble arg) (Scm_GetDouble VAL0)))
1300 ($result (Scm_Sub (Scm_Inexact arg) (Scm_Inexact VAL0))))))
1301
1302 (define-insn NUMIMUL2 0 none #f ; *.
1303 ($w/argp arg
1304 (if (and (SCM_REALP arg) (SCM_REALP VAL0))
1305 ($result:f (* (Scm_GetDouble arg) (Scm_GetDouble VAL0)))
1306 ($result (Scm_Mul (Scm_Inexact arg) (Scm_Inexact VAL0))))))
1307
1308 (define-insn NUMIDIV2 0 none #f ; /. (binary)
1309 ($w/argp arg
1310 (if (and (SCM_FLONUMP arg) (SCM_FLONUMP VAL0))
1311 ($result:f (/ (Scm_GetDouble arg) (Scm_GetDouble VAL0)))
1312 ($result (Scm_VMDivInexact arg VAL0)))))
1313
1314 (define-insn NUMADDI 1 none #f ; +, if one of op is small int
1315 (let* ([imm::long (SCM_VM_INSN_ARG code)])
1316 ($w/argr arg
1317 (cond [(SCM_INTP arg) ($result:n (+ imm (SCM_INT_VALUE arg)))]
1318 [(SCM_FLONUMP arg)
1319 ($result:f (+ (SCM_FLONUM_VALUE arg) (cast double imm)))]
1320 [else ($result (Scm_Add (SCM_MAKE_INT imm) arg))]))))
1321
1322 (define-insn-lref+ LREF-NUMADDI 1 none (LREF NUMADDI))
1323 (define-insn-lref+ LREF-NUMADDI-PUSH 1 none (LREF NUMADDI PUSH))
1324
1325 (define-insn NUMSUBI 1 none #f ; -, if one of op is small int
1326 (let* ([imm::long (SCM_VM_INSN_ARG code)])
1327 ($w/argr arg
1328 (cond [(SCM_INTP arg) ($result:n (- imm (SCM_INT_VALUE arg)))]
1329 [(SCM_FLONUMP arg)
1330 ($result:f (- (cast double imm) (SCM_FLONUM_VALUE arg)))]
1331 [else ($result (Scm_Sub (SCM_MAKE_INT imm) arg))]))))
1332
1333
1334 (define-insn NUMMODI 1 none #f
1335 (let* ([divisor::ScmSmallInt (SCM_VM_INSN_ARG code)])
1336 ($w/argr arg ($result (Scm_Modulo arg (SCM_MAKE_INT divisor) FALSE)))))
1337 (define-insn NUMREMI 1 none #f
1338 (let* ([divisor::ScmSmallInt (SCM_VM_INSN_ARG code)])
1339 ($w/argr arg ($result (Scm_Modulo arg (SCM_MAKE_INT divisor) TRUE)))))
1340
1341 (define-insn ASHI 1 none #f
1342 (let* ([cnt::ScmSmallInt (SCM_VM_INSN_ARG code)])
1343 ($w/argr arg ($result (Scm_Ash arg cnt)))))
1344 (define-insn LOGAND 0 none #f
1345 ($w/argp x ($result (Scm_LogAnd x VAL0))))
1346 (define-insn LOGIOR 0 none #f
1347 ($w/argp x ($result (Scm_LogIor x VAL0))))
1348 (define-insn LOGXOR 0 none #f
1349 ($w/argp x ($result (Scm_LogXor x VAL0))))
1350 (define-insn LOGANDC 0 obj #f
1351 (let* ([obj])
1352 ($w/argr x (FETCH-OPERAND obj) INCR-PC ($result (Scm_LogAnd x obj)))))
1353 (define-insn LOGIORC 0 obj #f
1354 (let* ([obj])
1355 ($w/argr x (FETCH-OPERAND obj) INCR-PC ($result (Scm_LogIor x obj)))))
1356 (define-insn LOGXORC 0 obj #f
1357 (let* ([obj])
1358 ($w/argr x (FETCH-OPERAND obj) INCR-PC ($result (Scm_LogXor x obj)))))
1359
1360 (define-insn READ-CHAR 1 none #f ; read-char
1361 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [ch::int 0] [port::ScmPort*])
1362 (cond [(== nargs 1)
1363 ($type-check VAL0 SCM_IPORTP "input port")
1364 (set! port (SCM_PORT VAL0))]
1365 [else
1366 (set! port SCM_CURIN)])
1367 (set! ch (Scm_Getc port))
1368 ($result (?: (< ch 0) SCM_EOF (SCM_MAKE_CHAR ch)))))
1369
1370 (define-insn PEEK-CHAR 1 none #f ; peek-char
1371 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [ch::int 0] [port::ScmPort*])
1372 (cond [(== nargs 1)
1373 ($type-check VAL0 SCM_IPORTP "input port")
1374 (set! port (SCM_PORT VAL0))]
1375 [else
1376 (set! port SCM_CURIN)])
1377 (set! ch (Scm_Peekc port))
1378 ($result (?: (< ch 0) SCM_EOF (SCM_MAKE_CHAR ch)))))
1379
1380 (define-insn WRITE-CHAR 1 none #f ; write-char
1381 (let* ([nargs::int (SCM_VM_INSN_ARG code)] [ch] [port::ScmPort*])
1382 (cond [(== nargs 2)
1383 ($type-check VAL0 SCM_OPORTP "output port")
1384 (set! port (SCM_PORT VAL0))
1385 (POP-ARG ch)]
1386 [else (set! port SCM_CUROUT
1387 ch VAL0)])
1388 ($type-check ch SCM_CHARP "character")
1389 (SCM_PUTC (SCM_CHAR_VALUE ch) port)
1390 ($result SCM_UNDEFINED)))
1391
1392 (define-insn CURIN 0 none #f ; current-input-port
1393 ($result (SCM_OBJ SCM_CURIN)))
1394 (define-insn CUROUT 0 none #f ; current-output-port
1395 ($result (SCM_OBJ SCM_CUROUT)))
1396 (define-insn CURERR 0 none #f ; current-error-port
1397 ($result (SCM_OBJ SCM_CURERR)))
1398
1399 (define-insn SLOT-REF 0 none #f ; slot-ref
1400 ($w/argp obj
1401 (TAIL-CALL-INSTRUCTION)
1402 (SCM_FLONUM_ENSURE_MEM VAL0)
1403 ($result (Scm_VMSlotRef obj VAL0 FALSE))))
1404
1405 (define-insn SLOT-SET 0 none #f ; slot-set!
1406 (let* ((slot))
1407 (POP-ARG slot)
1408 ($w/argp obj
1409 (TAIL-CALL-INSTRUCTION)
1410 (SCM_FLONUM_ENSURE_MEM slot)
1411 (SCM_FLONUM_ENSURE_MEM VAL0)
1412 ($result (Scm_VMSlotSet obj slot VAL0)))))
1413
1414 (define-insn SLOT-REFC 0 obj #f ; slot-ref with constant slot name
1415 (let* ((slot))
1416 (FETCH-OPERAND slot)
1417 INCR-PC
1418 (TAIL-CALL-INSTRUCTION)
1419 (SCM_FLONUM_ENSURE_MEM VAL0)
1420 ($result (Scm_VMSlotRef VAL0 slot FALSE))))
1421
1422 (define-insn SLOT-SETC 0 obj #f ; slot-set! with constant slot name
1423 (let* ((slot))
1424 (FETCH-OPERAND slot)
1425 INCR-PC
1426 ($w/argp obj
1427 (TAIL-CALL-INSTRUCTION)
1428 (SCM_FLONUM_ENSURE_MEM VAL0)
1429 ($result (Scm_VMSlotSet obj slot VAL0)))))
1430
1431 ;;
1432 ;; Dynamic handlers
1433 ;;
1434
1435 ;; PUSH-HANDLERS(n)
1436 ;; POP-HANDLERS
1437 ;; Used for dynamic-wind and alike.
1438 ;; Push-handlers takes two procedures (before proc in SP top, and
1439 ;; after proc in VAL0), and zero or more args. The number of args
1440 ;; is in parameter.
1441
1442 (define-insn PUSH-HANDLERS 1 none #f ; push dynamic handlers
1443 (let* ([nargs::long (SCM_VM_INSN_ARG code)]
1444 [before]
1445 [after VAL0]
1446 [args SCM_NIL])
1447 (VM-ASSERT (>= (- SP (-> vm stackBase)) (+ 1 nargs)))
1448 (when (> nargs 0)
1449 (SCM_FLONUM_ENSURE_MEM VAL0)
1450 (set! args (Scm_Cons VAL0 SCM_NIL))
1451 (while (> (pre-- nargs) 0)
1452 (let* ([v])
1453 (POP-ARG v)
1454 (SCM_FLONUM_ENSURE_MEM v)
1455 (set! args (Scm_Cons v args))))
1456 (POP-ARG after))
1457 (POP-ARG before)
1458 (SCM_FLONUM_ENSURE_MEM before)
1459 (SCM_FLONUM_ENSURE_MEM after)
1460 (push_dynamic_handlers vm before after args)
1461 NEXT))
1462
1463 (define-insn POP-HANDLERS 0 none #f ; pop dynamic handlers
1464 (begin
1465 (pop_dynamic_handlers vm)
1466 NEXT))
1467
1468 ;;
1469 ;; Environment
1470 ;;
1471
1472 ;; if param == 0, VAL0 <- box(VAL0)
1473 ;; else if param > 0, ENV[param-1] <- box(ENV[param-1])
1474 ;; The second case is for arguments that are mutated.
1475 (define-insn BOX 1 none #f
1476 (let* ([param::int (SCM_VM_INSN_ARG code)])
1477 (cond [(== param 0)
1478 (SCM_FLONUM_ENSURE_MEM VAL0)
1479 (let* ([b::ScmBox* (Scm_MakeBox VAL0)])
1480 (set! VAL0 (SCM_OBJ b)))]
1481 [(> param 0)
1482 (let* ([off::int (- param 1)])
1483 (VM-ASSERT (> (-> ENV size) off))
1484 (let* ([v (ENV-DATA ENV off)])
1485 (SCM_FLONUM_ENSURE_MEM v)
1486 (let* ([b::ScmBox* (Scm_MakeBox v)])
1487 (set! (ENV-DATA ENV off) (SCM_OBJ b)))))])
1488 NEXT))
1489
1490 ;; UNBOX
1491 ;; VAL0 <- unbox(VAL0)
1492 (define-insn UNBOX 0 none #f
1493 ($w/argr v
1494 (set! VAL0 (SCM_BOX_VALUE v))
1495 NEXT))
1496
1497 (define-insn LREF-UNBOX 2 none (LREF UNBOX) #f :fold-lref)
1498
1499 ;; LOCAL-ENV-SHIFT(depth)
1500 ;; This instruction appears when local function call is optimized.
1501 ;; The stack already has NLOCALS values. We discard DEPTH env frames,
1502 ;; and creates a new local env from the stack value.
1503 (define-insn LOCAL-ENV-SHIFT 1 none #f
1504 (begin
1505 (local_env_shift vm (SCM_VM_INSN_ARG code))
1506 NEXT))
1507
1508 ;; TAIL-EXTEND-DENV(op)
1509 ;; Push new key-value pair on the current denv.
1510 ;; VAL0 holds the value, and stack top holds the key.
1511 ;; EXTEND-DENV is for non-tail calls, and a continuation is pushed
1512 ;; before adding key-value pair.
1513 ;; If OP == 0, the value shadows the previous value.
1514 ;; If OP == 1, the value is consed to the previous value.
1515 ;; TRANSIENT: We allow num-params = 0 only to compile this with 0.9.15.
1516 ;; Drop it when a new release is made.
1517
1518 ;; TRANSIENT: EXTEND-DENV is no longer emitted. We keep it to run
1519 ;; code precompiled with 0.9.15. Remove it after the next release.
1520 (define-insn EXTEND-DENV (1 0) label #f
1521 (let* ([op::int (SCM_VM_INSN_ARG code)]
1522 [key] [next::ScmWord*])
1523 (SCM_FLONUM_ENSURE_MEM VAL0)
1524 (POP-ARG key)
1525 (FETCH-LOCATION next)
1526 INCR_PC
1527 (PUSH-CONT next)
1528 (if (== op 0)
1529 (Scm_VMPushDynamicEnv key VAL0)
1530 (Scm_VMPushDynamicEnv key (Scm_Cons VAL0
1531 (Scm_VMFindDynamicEnv key SCM_NIL))))
1532 NEXT))
1533
1534 (define-insn TAIL-EXTEND-DENV (1 0) none #f
1535 (let* ([op::int (SCM_VM_INSN_ARG code)] [key])
1536 (SCM_FLONUM_ENSURE_MEM VAL0)
1537 (POP-ARG key)
1538 (if (== op 0)
1539 (Scm_VMPushDynamicEnv key VAL0)
1540 (Scm_VMPushDynamicEnv key (Scm_Cons VAL0
1541 (Scm_VMFindDynamicEnv key SCM_NIL))))
1542 NEXT))
1543
1544 ;; EXPERIMENTAL
1545 ;; XINSN info code-addr
1546 ;; 'Extended instruction' - JIT compiled instruction handler.
1547 ;; The operand is an address of native code vector.
1548 (define-insn XINSN 0 obj+native #f
1549 (.if (and SCM_TARGET_X86_64 (not GAUCHE_WINDOWS))
1550 (let* ([jitcode::void*])
1551 INCR_PC
1552 (FETCH_LOCATION jitcode)
1553 INCR_PC
1554 (asm :volatile
1555 "mov %[vm], %%r15; \
1556 call *%[jitcode]"
1557 ()
1558 ((vm "r" vm)
1559 (jitcode "r" jitcode))
1560 ("r12" "r15"))
1561 NEXT)
1562 (Scm_Panic "XINSN instruction should never be seen on this platform.")))