Debian Codegraph @

kernel/byterun/rocq_interp.c

raw symbols SCIP
1 /***********************************************************************/
2 /* */
3 /* Rocq Compiler */
4 /* */
5 /* Benjamin Gregoire, projets Logical and Cristal */
6 /* INRIA Rocquencourt */
7 /* */
8 /* */
9 /***********************************************************************/
10
11 /* The bytecode interpreter */
12
13 /* Spiwack: expanded the virtual machine with operators used
14 for fast computation of bounded (31bits) integers */
15
16 #include <stdio.h>
17 #include <signal.h>
18 #include <stdint.h>
19 #include <math.h>
20
21 #define CAML_INTERNALS
22 #include <caml/memory.h>
23 #include <caml/signals.h>
24 #include <caml/version.h>
25 #include <caml/callback.h>
26
27 #include "rocq_instruct.h"
28 #include "rocq_fix_code.h"
29 #include "rocq_memory.h"
30 #include "rocq_values.h"
31
32 #if OCAML_VERSION < 41000
33 extern void caml_minor_collection(void);
34
35 #undef Alloc_small
36 #define Alloc_small(result, wosize, tag) do{ \
37 caml_young_ptr -= Bhsize_wosize(wosize); \
38 if (caml_young_ptr < caml_young_limit) { \
39 caml_young_ptr += Bhsize_wosize(wosize); \
40 Setup_for_gc; \
41 caml_minor_collection(); \
42 Restore_after_gc; \
43 caml_young_ptr -= Bhsize_wosize(wosize); \
44 } \
45 Hd_hp(caml_young_ptr) = Make_header((wosize), (tag), Caml_black); \
46 (result) = Val_hp(caml_young_ptr); \
47 }while(0)
48 #endif
49
50 #ifdef ARCH_SIXTYFOUR
51 #include "rocq_uint63_native.h"
52 #else
53 #include "rocq_uint63_emul.h"
54 #endif
55
56
57
58 /* Registers for the abstract machine:
59 pc the code pointer
60 sp the stack pointer (grows downward)
61 accu the accumulator
62 env heap-allocated environment
63 trapsp pointer to the current trap frame
64 extra_args number of extra arguments provided by the caller
65
66 sp is a local copy of the global variable extern_sp. */
67
68
69
70 /* Instruction decoding */
71
72
73 #ifdef THREADED_CODE
74 # define Instruct(name) rocq_lbl_##name:
75 # if defined(ARCH_SIXTYFOUR) && !defined(ARCH_CODE32)
76 # define rocq_Jumptbl_base &&rocq_lbl_ACC0
77 # else
78 # define rocq_Jumptbl_base 0
79 # define rocq_jumptbl_base ((char *) 0)
80 # endif
81 # ifdef DEBUG
82 # define Next goto next_instr
83 # else
84 # define Next goto *(void *)(rocq_jumptbl_base + *pc++)
85 # endif
86 #else
87 # define Instruct(name) case name:
88 # define Next break
89 #endif
90
91 /* #define _ROCQ_DEBUG_ */
92
93 #ifdef _ROCQ_DEBUG_
94 # define print_instr(s) printf("%s\n",s)
95 # define print_int(i) printf("%d\n",i)
96 # define print_lint(i) printf("%ld\n",i)
97 # else
98 # define print_instr(s)
99 # define print_int(i)
100 # define print_lint(i)
101 #endif
102
103 #define CHECK_STACK(num_args) { \
104 if (sp - num_args < rocq_stack_threshold) { \
105 rocq_sp = sp; \
106 realloc_rocq_stack(num_args + Rocq_stack_threshold / sizeof(value)); \
107 sp = rocq_sp; \
108 } \
109 }
110
111 /* GC interface */
112 #define Setup_for_gc { sp -= 2; sp[0] = accu; sp[1] = rocq_env; rocq_sp = sp; }
113 #define Restore_after_gc { accu = sp[0]; rocq_env = sp[1]; sp += 2; }
114 #define Setup_for_caml_call { *--sp = rocq_env; rocq_sp = sp; }
115 #define Restore_after_caml_call rocq_env = *sp++;
116
117 #if OCAML_VERSION >= 50000
118 #define Enter_gc(dom_st, wosize) do { \
119 Setup_for_gc; \
120 Alloc_small_enter_GC(dom_st, wosize); \
121 Restore_after_gc; \
122 } while (0)
123 #define Rocq_alloc_small(result, wosize, tag) Alloc_small(result, wosize, tag, Enter_gc)
124 #else
125 #define Rocq_alloc_small(result, wosize, tag) Alloc_small(result, wosize, tag)
126 #endif
127
128 /* Beware: Alloc_small can only invoked for allocations smaller than
129 Max_young_wosize, which is usually 256. For larger allocations,
130 caml_alloc_shr needs to be used instead, and the block needs to be filled
131 using caml_initialize. Note that these two functions never trigger a GC,
132 so calling Setup_for_gc is not needed.
133 */
134
135 /* If there is an asynchronous exception, we reset the vm */
136 #define Handle_potential_exception(res) \
137 if (Is_exception_result(res)) { \
138 rocq_sp = rocq_stack_high; \
139 caml_raise(Extract_exception(res)); \
140 }
141
142 /* Register optimization.
143 Some compilers underestimate the use of the local variables representing
144 the abstract machine registers, and don't put them in hardware registers,
145 which slows down the interpreter considerably.
146 For GCC, Xavier Leroy have hand-assigned hardware registers for
147 several architectures.
148 */
149
150 #if defined(__GNUC__) && !defined(DEBUG) && !defined(__INTEL_COMPILER) \
151 && !defined(__llvm__)
152 #ifdef __mips__
153 #define PC_REG asm("$16")
154 #define SP_REG asm("$17")
155 #define ACCU_REG asm("$18")
156 #endif
157 #ifdef __sparc__
158 #define PC_REG asm("%l0")
159 #define SP_REG asm("%l1")
160 #define ACCU_REG asm("%l2")
161 #endif
162 #ifdef __alpha__
163 #ifdef __CRAY__
164 #define PC_REG asm("r9")
165 #define SP_REG asm("r10")
166 #define ACCU_REG asm("r11")
167 #define JUMPTBL_BASE_REG asm("r12")
168 #else
169 #define PC_REG asm("$9")
170 #define SP_REG asm("$10")
171 #define ACCU_REG asm("$11")
172 #define JUMPTBL_BASE_REG asm("$12")
173 #endif
174 #endif
175 #ifdef __i386__
176 #define PC_REG asm("%esi")
177 #define SP_REG asm("%edi")
178 #define ACCU_REG
179 #endif
180 #if defined(__ppc__) || defined(__ppc64__)
181 #define PC_REG asm("26")
182 #define SP_REG asm("27")
183 #define ACCU_REG asm("28")
184 #endif
185 #ifdef __hppa__
186 #define PC_REG asm("%r18")
187 #define SP_REG asm("%r17")
188 #define ACCU_REG asm("%r16")
189 #endif
190 #ifdef __mc68000__
191 #define PC_REG asm("a5")
192 #define SP_REG asm("a4")
193 #define ACCU_REG asm("d7")
194 #endif
195 /* OCaml PR#4953: these specific registers not available in Thumb mode */
196 #if defined(__arm__) && !defined(__thumb__)
197 #define PC_REG asm("r6")
198 #define SP_REG asm("r8")
199 #define ACCU_REG asm("r7")
200 #endif
201 #ifdef __ia64__
202 #define PC_REG asm("36")
203 #define SP_REG asm("37")
204 #define ACCU_REG asm("38")
205 #define JUMPTBL_BASE_REG asm("39")
206 #endif
207 #ifdef __x86_64__
208 #define PC_REG asm("%r15")
209 #define SP_REG asm("%r14")
210 #define ACCU_REG asm("%r13")
211 #endif
212 #ifdef __aarch64__
213 #define PC_REG asm("%x19")
214 #define SP_REG asm("%x20")
215 #define ACCU_REG asm("%x21")
216 #define JUMPTBL_BASE_REG asm("%x22")
217 #endif
218 #endif
219
220 /* We should also check "Code_val(v) == accumulate" to be sure,
221 but Is_accu is only used in places where closures cannot occur. */
222 #define Is_accu(v) (Is_block(v) && Tag_val(v) == Closure_tag)
223
224 #define CheckPrimArgs(cond, apply_lbl) do{ \
225 if (cond) pc++; \
226 else{ \
227 *--sp=accu; \
228 accu = Field(rocq_global_data, *pc++); \
229 goto apply_lbl; \
230 } \
231 }while(0)
232
233 #define CheckInt1() CheckPrimArgs(Is_uint63(accu), apply1)
234 #define CheckInt2() CheckPrimArgs(Is_uint63(accu) && Is_uint63(sp[0]), apply2)
235 #define CheckInt3() CheckPrimArgs(Is_uint63(accu) && Is_uint63(sp[0]) \
236 && Is_uint63(sp[1]), apply3)
237 #define CheckFloat1() CheckPrimArgs(Is_double(accu), apply1)
238 #define CheckFloat2() CheckPrimArgs(Is_double(accu) && Is_double(sp[0]), apply2)
239
240 #define AllocCarry(cond) Rocq_alloc_small(accu, 1, (cond)? rocq_tag_C1 : rocq_tag_C0)
241 #define AllocPair() Rocq_alloc_small(accu, 2, rocq_tag_pair)
242
243 /* Beware: we cannot use caml_copy_double here as it doesn't use
244 Alloc_small, hence doesn't protect the stack via
245 Setup_for_gc/Restore_after_gc. */
246 #define Rocq_copy_double(val) do{ \
247 double Rocq_copy_double_f__ = (val); \
248 Rocq_alloc_small(accu, Double_wosize, Double_tag);\
249 Store_double_val(accu, Rocq_copy_double_f__); \
250 }while(0);
251
252 #define Swap_accu_sp do{ \
253 value swap_accu_sp_tmp__ = accu; \
254 accu = *sp; \
255 *sp = swap_accu_sp_tmp__; \
256 }while(0)
257
258 /* Turn a code pointer into a stack value usable as a return address, and conversely.
259 The least significant bit is set to 1 so that the GC does not mistake return
260 addresses for heap pointers. */
261 #define StoreRA(p) ((value)(p) + 1)
262 #define LoadRA(p) ((code_t)((value)(p) - 1))
263
264 #if OCAML_VERSION < 41000
265 /* For signal handling, we hijack some code from the caml runtime */
266
267 extern intnat volatile caml_signals_are_pending;
268 extern intnat volatile caml_pending_signals[];
269 extern void caml_process_pending_signals(void);
270 #endif
271
272 extern double rocq_next_up(double);
273 extern double rocq_next_down(double);
274
275 value rocq_subst_instance(value arg, value tosubst)
276 {
277 static const value * closure_f = NULL;
278 if (closure_f == NULL) {
279 /* First time around, look up by name */
280 closure_f = caml_named_value("rocq_subst_instance");
281 }
282 return caml_callback2_exn(*closure_f, arg, tosubst);
283 }
284
285 /* The interpreter itself */
286
287 value rocq_interprete
288 (code_t rocq_pc, value rocq_accu, value rocq_atom_tbl, value rocq_global_data, value rocq_env, long rocq_extra_args)
289 {
290 /* rocq_accu is not allocated on the OCaml heap */
291 CAMLparam2(rocq_atom_tbl, rocq_global_data);
292
293 /*Declaration des variables */
294 #ifdef PC_REG
295 register code_t pc PC_REG;
296 register value * sp SP_REG;
297 register value accu ACCU_REG;
298 #else
299 register code_t pc;
300 register value * sp;
301 register value accu;
302 #endif
303 #if defined(THREADED_CODE) && defined(ARCH_SIXTYFOUR) && !defined(ARCH_CODE32)
304 #ifdef JUMPTBL_BASE_REG
305 register char * rocq_jumptbl_base JUMPTBL_BASE_REG;
306 #else
307 register char * rocq_jumptbl_base;
308 #endif
309 #endif
310 #ifdef THREADED_CODE
311 static void * rocq_jumptable[] = {
312 # include "rocq_jumptbl.h"
313 };
314 #else
315 opcode_t curr_instr;
316 #endif
317 print_instr("Enter Interpreter");
318 if (rocq_pc == NULL) { /* Interpreter is initializing */
319 print_instr("Interpreter is initializing");
320 #ifdef THREADED_CODE
321 rocq_init_thread_code(rocq_jumptable, rocq_Jumptbl_base);
322 #endif
323 CAMLreturn(Val_unit);
324 }
325 #if defined(THREADED_CODE) && defined(ARCH_SIXTYFOUR) && !defined(ARCH_CODE32)
326 rocq_jumptbl_base = rocq_Jumptbl_base;
327 #endif
328
329 /* Initialisation */
330 sp = rocq_sp;
331 pc = rocq_pc;
332 accu = rocq_accu;
333
334 CHECK_STACK(0);
335
336 #ifdef THREADED_CODE
337 goto *(void *)(rocq_jumptbl_base + *pc++); /* Jump to the first instruction */
338 #else
339 while(1) {
340 curr_instr = *pc++;
341 switch(curr_instr) {
342 #endif
343 /* Basic stack operations */
344
345 Instruct(ACC0){
346 print_instr("ACC0");
347 accu = sp[0]; Next;
348 }
349 Instruct(ACC1){
350 print_instr("ACC1");
351 accu = sp[1]; Next;
352 }
353 Instruct(ACC2){
354 print_instr("ACC2");
355 accu = sp[2]; Next;
356 }
357 Instruct(ACC3){
358 print_instr("ACC3");
359 accu = sp[3]; Next;
360 }
361 Instruct(ACC4){
362 print_instr("ACC4");
363 accu = sp[4]; Next;
364 }
365 Instruct(ACC5){
366 print_instr("ACC5");
367 accu = sp[5]; Next;
368 }
369 Instruct(ACC6){
370 print_instr("ACC6");
371 accu = sp[6]; Next;
372 }
373 Instruct(ACC7){
374 print_instr("ACC7");
375 accu = sp[7]; Next;
376 }
377 Instruct(PUSH){
378 print_instr("PUSH");
379 *--sp = accu; Next;
380 }
381 Instruct(PUSHACC1){
382 print_instr("PUSHACC1");
383 *--sp = accu; accu = sp[1]; Next;
384 }
385 Instruct(PUSHACC2){
386 print_instr("PUSHACC2");
387 *--sp = accu; accu = sp[2]; Next;
388 }
389 Instruct(PUSHACC3){
390 print_instr("PUSHACC3");
391 *--sp = accu; accu = sp[3]; Next;
392 }
393 Instruct(PUSHACC4){
394 print_instr("PUSHACC4");
395 *--sp = accu; accu = sp[4]; Next;
396 }
397 Instruct(PUSHACC5){
398 print_instr("PUSHACC5");
399 *--sp = accu; accu = sp[5]; Next;
400 }
401 Instruct(PUSHACC6){
402 print_instr("PUSHACC5");
403 *--sp = accu; accu = sp[6]; Next;
404 }
405 Instruct(PUSHACC7){
406 print_instr("PUSHACC7");
407 *--sp = accu; accu = sp[7]; Next;
408 }
409 Instruct(PUSHACC){
410 print_instr("PUSHACC");
411 *--sp = accu;
412 }
413 /* Fallthrough */
414
415 Instruct(ACC){
416 print_instr("ACC");
417 accu = sp[*pc++];
418 Next;
419 }
420
421 Instruct(PUSHACCMANY) {
422 int first = *pc++;
423 int size = *pc++;
424 int i;
425 print_instr("PUSHACCMANY");
426 sp -= size;
427 for (i = 1; i < size; ++i) sp[i - 1] = sp[first + i];
428 sp[size - 1] = accu;
429 accu = sp[first];
430 Next;
431 }
432
433 Instruct(POP){
434 print_instr("POP");
435 sp += *pc++;
436 Next;
437 }
438 /* Access in heap-allocated environment */
439
440 Instruct(ENVACC0){
441 print_instr("ENVACC0");
442 accu = Field(rocq_env, 2);
443 Next;
444 }
445 Instruct(ENVACC1){
446 print_instr("ENVACC1");
447 accu = Field(rocq_env, 3);
448 Next;
449 }
450 Instruct(ENVACC2){
451 print_instr("ENVACC2");
452 accu = Field(rocq_env, 4);
453 Next;
454 }
455 Instruct(ENVACC3){
456 print_instr("ENVACC3");
457 accu = Field(rocq_env, 5);
458 Next;
459 }
460 Instruct(PUSHENVACC0){
461 print_instr("PUSHENVACC0");
462 *--sp = accu;
463 accu = Field(rocq_env, 2);
464 Next;
465 }
466 Instruct(PUSHENVACC1){
467 print_instr("PUSHENVACC1");
468 *--sp = accu;
469 accu = Field(rocq_env, 3);
470 Next;
471 }
472 Instruct(PUSHENVACC2){
473 print_instr("PUSHENVACC2");
474 *--sp = accu;
475 accu = Field(rocq_env, 4);
476 Next;
477 }
478 Instruct(PUSHENVACC3){
479 print_instr("PUSHENVACC3");
480 *--sp = accu;
481 accu = Field(rocq_env, 5);
482 Next;
483 }
484 Instruct(PUSHENVACC){
485 print_instr("PUSHENVACC");
486 *--sp = accu;
487 }
488 /* Fallthrough */
489 Instruct(ENVACC){
490 print_instr("ENVACC");
491 print_int(*pc);
492 accu = Field(rocq_env, 2 + *pc++);
493 Next;
494 }
495
496 Instruct(PUSHENVACCMANY) {
497 int first = *pc++;
498 int size = *pc++;
499 int i;
500 print_instr("PUSHENVACCMANY");
501 first += 2;
502 sp -= size;
503 for (i = 1; i < size; ++i) sp[i - 1] = Field(rocq_env, first + i);
504 sp[size - 1] = accu;
505 accu = Field(rocq_env, first);
506 Next;
507 }
508
509 /* Function application */
510
511 Instruct(PUSH_RETADDR) {
512 print_instr("PUSH_RETADDR");
513 sp -= 3;
514 sp[0] = StoreRA(pc + *pc);
515 sp[1] = rocq_env;
516 sp[2] = Val_long(rocq_extra_args);
517 rocq_extra_args = 0;
518 pc++;
519 Next;
520 }
521 Instruct(APPLY) {
522 print_instr("APPLY");
523 rocq_extra_args = *pc - 1;
524 pc = Code_val(accu);
525 rocq_env = accu;
526 goto check_stack;
527 }
528 Instruct(APPLY1) {
529 value arg1;
530 apply1:
531 print_instr("APPLY1");
532 arg1 = sp[0];
533 sp -= 3;
534 sp[0] = arg1;
535 sp[1] = StoreRA(pc);
536 sp[2] = rocq_env;
537 sp[3] = Val_long(rocq_extra_args);
538 print_instr("call stack=");
539 print_lint(sp[1]);
540 print_lint(sp[2]);
541 print_lint(sp[3]);
542 pc = Code_val(accu);
543 rocq_env = accu;
544 rocq_extra_args = 0;
545 goto check_stack;
546 }
547
548 Instruct(APPLY2) {
549 value arg1;
550 value arg2;
551 apply2:
552 print_instr("APPLY2");
553 arg1 = sp[0];
554 arg2 = sp[1];
555 sp -= 3;
556 sp[0] = arg1;
557 sp[1] = arg2;
558 sp[2] = StoreRA(pc);
559 sp[3] = rocq_env;
560 sp[4] = Val_long(rocq_extra_args);
561 pc = Code_val(accu);
562 rocq_env = accu;
563 rocq_extra_args = 1;
564 goto check_stack;
565 }
566
567 Instruct(APPLY3) {
568 value arg1;
569 value arg2;
570 value arg3;
571 apply3:
572 print_instr("APPLY3");
573 arg1 = sp[0];
574 arg2 = sp[1];
575 arg3 = sp[2];
576 sp -= 3;
577 sp[0] = arg1;
578 sp[1] = arg2;
579 sp[2] = arg3;
580 sp[3] = StoreRA(pc);
581 sp[4] = rocq_env;
582 sp[5] = Val_long(rocq_extra_args);
583 pc = Code_val(accu);
584 rocq_env = accu;
585 rocq_extra_args = 2;
586 goto check_stack;
587 }
588
589 Instruct(APPLY4) {
590 value arg1 = sp[0];
591 value arg2 = sp[1];
592 value arg3 = sp[2];
593 value arg4 = sp[3];
594 print_instr("APPLY4");
595 sp -= 3;
596 sp[0] = arg1;
597 sp[1] = arg2;
598 sp[2] = arg3;
599 sp[3] = arg4;
600 sp[4] = StoreRA(pc);
601 sp[5] = rocq_env;
602 sp[6] = Val_long(rocq_extra_args);
603 pc = Code_val(accu);
604 rocq_env = accu;
605 rocq_extra_args = 3;
606 goto check_stack;
607 }
608
609 /* Stack checks */
610
611 check_stack:
612 print_instr("check_stack");
613 CHECK_STACK(0);
614 /* We also check for signals */
615 #if OCAML_VERSION >= 50000
616 if (Caml_check_gc_interrupt(Caml_state)) {
617 Setup_for_gc;
618 value res = caml_process_pending_actions_exn();
619 Handle_potential_exception(res);
620 Restore_after_gc;
621 }
622 #elif OCAML_VERSION >= 41000
623 if (caml_something_to_do) {
624 Setup_for_gc;
625 value res = caml_process_pending_actions_exn();
626 Handle_potential_exception(res);
627 Restore_after_gc;
628 }
629 #else
630 if (caml_signals_are_pending) {
631 /* If there's a Ctrl-C, we reset the vm */
632 intnat sigint = caml_pending_signals[SIGINT];
633 if (sigint) { rocq_sp = rocq_stack_high; }
634 Setup_for_gc;
635 caml_process_pending_signals();
636 Restore_after_gc;
637 if (sigint) {
638 caml_failwith("Rocq VM: Fatal error: SIGINT signal detected "
639 "but no exception was raised");
640 }
641 }
642 #endif
643 Next;
644
645 Instruct(ENSURESTACKCAPACITY) {
646 print_instr("ENSURESTACKCAPACITY");
647 int size = *pc++;
648 /* CHECK_STACK may trigger here a useless allocation because of the
649 threshold, but check_stack: often does it anyway, so we prefer to
650 factorize the code. */
651 CHECK_STACK(size);
652 Next;
653 }
654
655 Instruct(APPTERM) {
656 int nargs = *pc++;
657 int slotsize = *pc;
658 value * newsp;
659 int i;
660 print_instr("APPTERM");
661 /* Slide the nargs bottom words of the current frame to the top
662 of the frame, and discard the remainder of the frame */
663 newsp = sp + slotsize - nargs;
664 for (i = nargs - 1; i >= 0; i--) newsp[i] = sp[i];
665 sp = newsp;
666 pc = Code_val(accu);
667 rocq_env = accu;
668 rocq_extra_args += nargs - 1;
669 goto check_stack;
670 }
671 Instruct(APPTERM1) {
672 value arg1 = sp[0];
673 print_instr("APPTERM1");
674 sp = sp + *pc - 1;
675 sp[0] = arg1;
676 pc = Code_val(accu);
677 rocq_env = accu;
678 goto check_stack;
679 }
680 Instruct(APPTERM2) {
681 value arg1 = sp[0];
682 value arg2 = sp[1];
683 print_instr("APPTERM2");
684 sp = sp + *pc - 2;
685 sp[0] = arg1;
686 sp[1] = arg2;
687 pc = Code_val(accu);
688 rocq_env = accu;
689 rocq_extra_args += 1;
690 goto check_stack;
691 }
692 Instruct(APPTERM3) {
693 value arg1 = sp[0];
694 value arg2 = sp[1];
695 value arg3 = sp[2];
696 print_instr("APPTERM3");
697 sp = sp + *pc - 3;
698 sp[0] = arg1;
699 sp[1] = arg2;
700 sp[2] = arg3;
701 pc = Code_val(accu);
702 rocq_env = accu;
703 rocq_extra_args += 2;
704 goto check_stack;
705 }
706
707 Instruct(RETURN) {
708 print_instr("RETURN");
709 print_int(*pc);
710 sp += *pc++;
711 print_instr("stack=");
712 print_lint(sp[0]);
713 print_lint(sp[1]);
714 print_lint(sp[2]);
715 if (rocq_extra_args > 0) {
716 print_instr("extra args > 0");
717 print_lint(rocq_extra_args);
718 rocq_extra_args--;
719 pc = Code_val(accu);
720 rocq_env = accu;
721 } else {
722 print_instr("extra args = 0");
723 pc = LoadRA(sp[0]);
724 rocq_env = sp[1];
725 rocq_extra_args = Long_val(sp[2]);
726 sp += 3;
727 }
728 Next;
729 }
730
731 Instruct(RESTART) {
732 int num_args = Wosize_val(rocq_env) - 3;
733 int i;
734 print_instr("RESTART");
735 CHECK_STACK(num_args);
736 sp -= num_args;
737 for (i = 0; i < num_args; i++) sp[i] = Field(rocq_env, i + 3);
738 rocq_env = Field(rocq_env, 2);
739 rocq_extra_args += num_args;
740 Next;
741 }
742
743 Instruct(GRAB) {
744 int required = *pc++;
745 print_instr("GRAB");
746 /* printf("GRAB %d\n",required); */
747 if (rocq_extra_args >= required) {
748 rocq_extra_args -= required;
749 } else {
750 mlsize_t num_args, i;
751 num_args = 1 + rocq_extra_args; /* arg1 + extra args */
752 Rocq_alloc_small(accu, num_args + 3, Closure_tag);
753 Field(accu, 1) = Val_int(2);
754 Field(accu, 2) = rocq_env;
755 for (i = 0; i < num_args; i++) Field(accu, i + 3) = sp[i];
756 Code_val(accu) = pc - 3; /* Point to the preceding RESTART instr. */
757 sp += num_args;
758 pc = LoadRA(sp[0]);
759 rocq_env = sp[1];
760 rocq_extra_args = Long_val(sp[2]);
761 sp += 3;
762 }
763 Next;
764 }
765
766 Instruct(GRABREC) {
767 int rec_pos = *pc++; /* commence a zero */
768 print_instr("GRABREC");
769 if (rec_pos <= rocq_extra_args && !Is_accu(sp[rec_pos])) {
770 pc++; /* Skip the next RESTART, then point rocq_env at the free variables. */
771 rocq_env = rocq_env + (Int_val(Field(rocq_env, 1)) - 2) * sizeof(value);
772 } else {
773 if (rocq_extra_args < rec_pos) {
774 /* Partial application */
775 mlsize_t num_args, i;
776 num_args = 1 + rocq_extra_args; /* arg1 + extra args */
777 Rocq_alloc_small(accu, num_args + 3, Closure_tag);
778 Code_val(accu) = pc - 3; /* Point to the preceding RESTART instr. */
779 Field(accu, 1) = Val_int(2);
780 Field(accu, 2) = rocq_env;
781 for (i = 0; i < num_args; i++) Field(accu, i + 3) = sp[i];
782 sp += num_args;
783 pc = LoadRA(sp[0]);
784 rocq_env = sp[1];
785 rocq_extra_args = Long_val(sp[2]);
786 sp += 3;
787 } else {
788 /* The recursive argument is an accumulator */
789 mlsize_t num_args, sz, i;
790 value block;
791 /* Construction of fixpoint applied to its [rec_pos-1] first arguments */
792 Rocq_alloc_small(accu, rec_pos + 3, Closure_tag);
793 Code_val(accu) = pc; /* Point to the next RESTART instr. */
794 Field(accu, 1) = Val_int(2);
795 Field(accu, 2) = rocq_env; // We store the fixpoint in the first field
796 for (i = 0; i < rec_pos; i++) Field(accu, i + 3) = *sp++; // Storing args
797 /* Construction of the atom */
798 Rocq_alloc_small(block, 2, ATOM_FIX_TAG);
799 Field(block, 0) = *sp++;
800 Field(block, 1) = accu;
801 accu = block;
802 /* Construction of the accumulator */
803 num_args = rocq_extra_args - rec_pos;
804 sz = 3 + num_args;
805 if (sz <= Max_young_wosize) {
806 Rocq_alloc_small(block, sz, Closure_tag);
807 Field(block, 2) = accu;
808 for (i = 3; i < sz; ++i)
809 Field(block, i) = *sp++;
810 } else {
811 block = caml_alloc_shr(sz, Closure_tag);
812 caml_initialize(&Field(block, 2), accu);
813 for (i = 3; i < sz; ++i)
814 caml_initialize(&Field(block, i), *sp++);
815 }
816 Code_val(block) = accumulate;
817 Field(block, 1) = Val_int(2);
818 accu = block;
819 pc = LoadRA(sp[0]);
820 rocq_env = sp[1];
821 rocq_extra_args = Long_val(sp[2]);
822 sp += 3;
823 }
824 }
825 Next;
826 }
827
828 Instruct(CLOSURE) {
829 int nvars = *pc++;
830 int i;
831 print_instr("CLOSURE");
832 print_int(nvars);
833 if (nvars > 0) *--sp = accu;
834 Rocq_alloc_small(accu, 2 + nvars, Closure_tag);
835 Field(accu, 1) = Val_int(2);
836 Code_val(accu) = pc + *pc;
837 pc++;
838 for (i = 0; i < nvars; i++) {
839 print_lint(sp[i]);
840 Field(accu, i + 2) = sp[i];
841 }
842 sp += nvars;
843 Next;
844 }
845
846 Instruct(CLOSUREREC) {
847 int nfuncs = *pc++;
848 int nvars = *pc++;
849 int start = *pc++;
850 int i;
851 value * p;
852 print_instr("CLOSUREREC");
853 if (nvars > 0) *--sp = accu;
854 /* construction du vecteur de type */
855 Rocq_alloc_small(accu, nfuncs, Abstract_tag);
856 for(i = 0; i < nfuncs; i++) {
857 Field(accu,i) = (value)(pc+pc[i]);
858 }
859 pc += nfuncs;
860 *--sp=accu;
861 Rocq_alloc_small(accu, nfuncs * 3 + nvars, Closure_tag);
862 Field(accu, nfuncs * 3 + nvars - 1) = *sp++;
863 p = (value *) &Field(accu, 0);
864 *p++ = (value) (pc + pc[0]);
865 *p++ = Val_int(nfuncs * 3 - 1);
866 for (i = 1; i < nfuncs; i++) {
867 *p++ = Make_header(i * 3, Infix_tag, 0); /* color irrelevant. */
868 *p++ = (value) (pc + pc[i]);
869 *p++ = Val_int((nfuncs - i) * 3 - 1);
870 }
871 for (i = 0; i < nvars; i++) *p++ = *sp++;
872 pc += nfuncs;
873 accu = accu + start * 3 * sizeof(value);
874 Next;
875 }
876
877 Instruct(CLOSURECOFIX){
878 int nfunc = *pc++;
879 int nvars = *pc++;
880 int start = *pc++;
881 int i, j , size;
882 value * p;
883 print_instr("CLOSURECOFIX");
884 if (nvars > 0) *--sp = accu;
885 /* construction du vecteur de type */
886 Rocq_alloc_small(accu, nfunc, Abstract_tag);
887 for(i = 0; i < nfunc; i++) {
888 Field(accu,i) = (value)(pc+pc[i]);
889 }
890 pc += nfunc;
891 *--sp=accu;
892
893 /* Creation des blocks accumulate */
894 for(i=0; i < nfunc; i++) {
895 Rocq_alloc_small(accu, 3, Closure_tag);
896 Code_val(accu) = accumulate;
897 Field(accu, 1) = Val_int(2);
898 Field(accu, 2) = Val_int(1);
899 *--sp=accu;
900 }
901 /* creation des fonction cofix */
902
903 p = sp;
904 size = nfunc + nvars + 3;
905 for (i=0; i < nfunc; i++) {
906 value block;
907 Rocq_alloc_small(accu, size, Closure_tag);
908 Code_val(accu) = pc+pc[i];
909 Field(accu, 1) = Val_int(2);
910 for (j = 0; j < nfunc; ++j) Field(accu, j + 2) = p[j];
911 Field(accu, size - 1) = p[nfunc];
912 for (j = nfunc + 1; j <= nfunc + nvars; ++j) Field(accu, j + 1) = p[j];
913 Rocq_alloc_small(block, 1, ATOM_COFIX_TAG);
914 Field(block, 0) = accu;
915 /* update the accumulate block */
916 caml_modify(&Field(p[i], 2), block);
917 }
918 pc += nfunc;
919 accu = p[start];
920 sp = p + nfunc + 1 + nvars;
921 print_instr("ici4");
922 Next;
923 }
924
925
926 Instruct(PUSHOFFSETCLOSURE) {
927 print_instr("PUSHOFFSETCLOSURE");
928 *--sp = accu;
929 } /* fallthrough */
930 Instruct(OFFSETCLOSURE) {
931 print_instr("OFFSETCLOSURE");
932 accu = rocq_env - *pc++ * 3 * sizeof(value);
933 Next;
934 }
935 Instruct(PUSHOFFSETCLOSURE0) {
936 print_instr("PUSHOFFSETCLOSURE0");
937 *--sp = accu;
938 }/* fallthrough */
939 Instruct(OFFSETCLOSURE0) {
940 print_instr("OFFSETCLOSURE0");
941 accu = rocq_env;
942 Next;
943 }
944 Instruct(PUSHOFFSETCLOSURE1){
945 print_instr("PUSHOFFSETCLOSURE1");
946 *--sp = accu; /* fallthrough */
947 }
948 Instruct(OFFSETCLOSURE1) {
949 print_instr("OFFSETCLOSURE1");
950 accu = rocq_env - 3 * sizeof(value);
951 Next;
952 }
953
954 /* Access to global variables */
955
956 Instruct(PUSHGETGLOBAL) {
957 print_instr("PUSH");
958 *--sp = accu;
959 }
960 /* Fallthrough */
961 Instruct(GETGLOBAL){
962 print_instr("GETGLOBAL");
963 print_int(*pc);
964 accu = Field(rocq_global_data, *pc);
965 pc++;
966 Next;
967 }
968
969 /* Allocation of blocks */
970
971 Instruct(MAKEBLOCK) {
972 mlsize_t wosize = *pc++;
973 tag_t tag = *pc++;
974 mlsize_t i;
975 value block;
976 print_instr("MAKEBLOCK, tag=");
977 if (wosize == 0) {
978 accu = Atom(tag);
979 } else if (wosize <= Max_young_wosize) {
980 Rocq_alloc_small(block, wosize, tag);
981 Field(block, 0) = accu;
982 for (i = 1; i < wosize; i++) Field(block, i) = *sp++;
983 accu = block;
984 } else {
985 block = caml_alloc_shr(wosize, tag);
986 caml_initialize(&Field(block, 0), accu);
987 for (i = 1; i < wosize; i++) caml_initialize(&Field(block, i), *sp++);
988 accu = block;
989 }
990 Next;
991 }
992
993 Instruct(MAKEBLOCK1) {
994 tag_t tag = *pc++;
995 value block;
996 print_instr("MAKEBLOCK1, tag=");
997 print_int(tag);
998 Rocq_alloc_small(block, 1, tag);
999 Field(block, 0) = accu;
1000 accu = block;
1001 Next;
1002 }
1003 Instruct(MAKEBLOCK2) {
1004
1005 tag_t tag = *pc++;
1006 value block;
1007 print_instr("MAKEBLOCK2, tag=");
1008 print_int(tag);
1009 Rocq_alloc_small(block, 2, tag);
1010 Field(block, 0) = accu;
1011 Field(block, 1) = sp[0];
1012 sp += 1;
1013 accu = block;
1014 Next;
1015 }
1016 Instruct(MAKEBLOCK3) {
1017 tag_t tag = *pc++;
1018 value block;
1019 print_instr("MAKEBLOCK3, tag=");
1020 print_int(tag);
1021 Rocq_alloc_small(block, 3, tag);
1022 Field(block, 0) = accu;
1023 Field(block, 1) = sp[0];
1024 Field(block, 2) = sp[1];
1025 sp += 2;
1026 accu = block;
1027 Next;
1028 }
1029 Instruct(MAKEBLOCK4) {
1030 tag_t tag = *pc++;
1031 value block;
1032 print_instr("MAKEBLOCK4, tag=");
1033 print_int(tag);
1034 Rocq_alloc_small(block, 4, tag);
1035 Field(block, 0) = accu;
1036 Field(block, 1) = sp[0];
1037 Field(block, 2) = sp[1];
1038 Field(block, 3) = sp[2];
1039 sp += 3;
1040 accu = block;
1041 Next;
1042 }
1043
1044
1045 /* Access to components of blocks */
1046
1047 Instruct(SWITCH) {
1048 uint32_t sizes = *pc++;
1049 print_instr("SWITCH");
1050 if (Is_block(accu)) {
1051 long index = Tag_val(accu);
1052 if (index == Closure_tag) index = 0;
1053 print_instr("block");
1054 print_lint(index);
1055 pc += pc[(sizes >> 8) + index];
1056 } else {
1057 long index = Long_val(accu);
1058 print_instr("constant");
1059 print_lint(index);
1060 pc += pc[index];
1061 }
1062 Next;
1063 }
1064
1065 Instruct(PUSHFIELDS){
1066 int i;
1067 int size = *pc++;
1068 print_instr("PUSHFIELDS");
1069 sp -= size;
1070 for(i=0;i<size;i++)sp[i] = Field(accu,i);
1071 Next;
1072 }
1073
1074 Instruct(GETFIELD0){
1075 print_instr("GETFIELD0");
1076 accu = Field(accu, 0);
1077 Next;
1078 }
1079
1080 Instruct(GETFIELD1){
1081 print_instr("GETFIELD1");
1082 accu = Field(accu, 1);
1083 Next;
1084 }
1085
1086 Instruct(GETFIELD){
1087 print_instr("GETFIELD");
1088 accu = Field(accu, *pc);
1089 pc++;
1090 Next;
1091 }
1092
1093 Instruct(SETFIELD){
1094 print_instr("SETFIELD");
1095 caml_modify(&Field(accu, *pc),*sp);
1096 sp++; pc++;
1097 Next;
1098 }
1099
1100
1101 Instruct(PROJ){
1102 do_proj:
1103 print_instr("PROJ");
1104 if (Is_accu (accu)) {
1105 *--sp = accu; // Save matched block on stack
1106 accu = Field(accu, 2); // Save atom to accu register
1107 switch (Tag_val(accu)) {
1108 case ATOM_COFIX_TAG: // We are forcing a cofix
1109 {
1110 mlsize_t i, nargs;
1111 sp -= 2;
1112 // Push the current instruction as the return address
1113 sp[0] = StoreRA(pc - 1);
1114 sp[1] = rocq_env;
1115 rocq_env = Field(accu, 0); // Pointer to suspension
1116 accu = sp[2]; // Save accumulator to accu register
1117 sp[2] = Val_long(rocq_extra_args); // Push number of args for return
1118 nargs = Wosize_val(accu) - 3; // Number of args = size of accumulator - 2 (accumulator closure) - 1 (atom)
1119 // Push arguments to stack
1120 CHECK_STACK(nargs + 1);
1121 sp -= nargs;
1122 for (i = 0; i < nargs; ++i) sp[i] = Field(accu, i + 3);
1123 *--sp = accu; // Last argument is the pointer to the suspension
1124 rocq_extra_args = nargs;
1125 pc = Code_val(rocq_env); // Trigger evaluation
1126 goto check_stack;
1127 }
1128 case ATOM_COFIXEVALUATED_TAG:
1129 {
1130 accu = Field(accu, 1);
1131 ++sp;
1132 goto do_proj;
1133 }
1134 default:
1135 {
1136 value block;
1137 int index = *pc++;
1138 /* Create atom */
1139 Rocq_alloc_small(accu, 2, ATOM_PROJ_TAG);
1140 Field(accu, 0) = Val_int(index);
1141 Field(accu, 1) = *sp++;
1142 /* Create accumulator */
1143 Rocq_alloc_small(block, 3, Closure_tag);
1144 Code_val(block) = accumulate;
1145 Field(block, 1) = Val_int(2);
1146 Field(block, 2) = accu;
1147 accu = block;
1148 }
1149 }
1150 } else {
1151 accu = Field(accu, *pc);
1152 pc++;
1153 }
1154 Next;
1155 }
1156
1157 /* Integer constants */
1158
1159 Instruct(CONST0){
1160 print_instr("CONST0");
1161 accu = Val_int(0); Next;}
1162 Instruct(CONST1){
1163 print_instr("CONST1");
1164 accu = Val_int(1); Next;}
1165 Instruct(CONST2){
1166 print_instr("CONST2");
1167 accu = Val_int(2); Next;}
1168 Instruct(CONST3){
1169 print_instr("CONST3");
1170 accu = Val_int(3); Next;}
1171
1172 Instruct(PUSHCONST0){
1173 print_instr("PUSHCONST0");
1174 *--sp = accu; accu = Val_int(0); Next;
1175 }
1176 Instruct(PUSHCONST1){
1177 print_instr("PUSHCONST1");
1178 *--sp = accu; accu = Val_int(1); Next;
1179 }
1180 Instruct(PUSHCONST2){
1181 print_instr("PUSHCONST2");
1182 *--sp = accu; accu = Val_int(2); Next;
1183 }
1184 Instruct(PUSHCONST3){
1185 print_instr("PUSHCONST3");
1186 *--sp = accu; accu = Val_int(3); Next;
1187 }
1188
1189 Instruct(PUSHCONSTINT){
1190 print_instr("PUSHCONSTINT");
1191 *--sp = accu;
1192 }
1193 /* Fallthrough */
1194 Instruct(CONSTINT) {
1195 print_instr("CONSTINT");
1196 print_int(*pc);
1197 accu = Val_int(*pc);
1198 pc++;
1199 Next;
1200 }
1201
1202 /* Special operations for reduction of open term */
1203 Instruct(ACCUMULATE) {
1204 mlsize_t i, size, sz;
1205 print_instr("ACCUMULATE");
1206 size = Wosize_val(rocq_env);
1207 sz = size + rocq_extra_args + 1;
1208 if (sz <= Max_young_wosize) {
1209 Rocq_alloc_small(accu, sz, Closure_tag);
1210 for (i = 0; i < size; ++i)
1211 Field(accu, i) = Field(rocq_env, i);
1212 for (i = size; i < sz; ++i)
1213 Field(accu, i) = *sp++;
1214 } else {
1215 accu = caml_alloc_shr(sz, Closure_tag);
1216 for (i = 0; i < size; ++i)
1217 caml_initialize(&Field(accu, i), Field(rocq_env, i));
1218 for (i = size; i < sz; ++i)
1219 caml_initialize(&Field(accu, i), *sp++);
1220 }
1221 pc = LoadRA(sp[0]);
1222 rocq_env = sp[1];
1223 rocq_extra_args = Long_val(sp[2]);
1224 sp += 3;
1225 Next;
1226 }
1227 Instruct(MAKESWITCHBLOCK) {
1228 print_instr("MAKESWITCHBLOCK");
1229 *--sp = accu; // Save matched block on stack
1230 accu = Field(accu, 2); // Save atom to accu register
1231 switch (Tag_val(accu)) {
1232 case ATOM_COFIX_TAG: // We are forcing a cofix
1233 {
1234 mlsize_t i, nargs;
1235 print_instr("COFIX_TAG");
1236 sp-=2;
1237 pc++;
1238 // Push the return address
1239 sp[0] = StoreRA(pc + *pc);
1240 sp[1] = rocq_env;
1241 rocq_env = Field(accu,0); // Pointer to suspension
1242 accu = sp[2]; // Save accumulator to accu register
1243 sp[2] = Val_long(rocq_extra_args); // Push number of args for return
1244 nargs = Wosize_val(accu) - 3; // Number of args = size of accumulator - 2 (accumulator closure) - 1 (atom)
1245 // Push arguments to stack
1246 CHECK_STACK(nargs+1);
1247 sp -= nargs;
1248 for (i = 0; i < nargs; i++) sp[i] = Field(accu, i + 3);
1249 *--sp = accu; // Leftmost argument is the pointer to the suspension
1250 print_lint(nargs);
1251 rocq_extra_args = nargs;
1252 pc = Code_val(rocq_env); // Trigger evaluation
1253 goto check_stack;
1254 }
1255 case ATOM_COFIXEVALUATED_TAG:
1256 {
1257 print_instr("COFIX_EVAL_TAG");
1258 accu = Field(accu,1);
1259 pc++;
1260 pc = pc + *pc;
1261 sp++;
1262 Next;
1263 }
1264 default:
1265 {
1266 mlsize_t sz;
1267 int i, annot;
1268 code_t typlbl,swlbl;
1269 value block;
1270 print_instr("MAKESWITCHBLOCK");
1271
1272 typlbl = (code_t)pc + *pc;
1273 pc++;
1274 swlbl = (code_t)pc + *pc;
1275 pc++;
1276 annot = *pc++;
1277 sz = *pc++;
1278 *--sp=Field(rocq_global_data, annot);
1279 /* We save the stack */
1280 if (sz == 0) accu = Atom(0);
1281 else if (sz <= Max_young_wosize) {
1282 Rocq_alloc_small(accu, sz, Default_tag);
1283 if (Is_tailrec_switch(*sp)) {
1284 for (i = 0; i < sz; i++) Field(accu, i) = sp[i+2];
1285 }else{
1286 for (i = 0; i < sz; i++) Field(accu, i) = sp[i+5];
1287 }
1288 } else {
1289 accu = caml_alloc_shr(sz, Default_tag);
1290 if (Is_tailrec_switch(*sp)) {
1291 for (i = 0; i < sz; i++) caml_initialize(&Field(accu, i), sp[i+2]);
1292 }else{
1293 for (i = 0; i < sz; i++) caml_initialize(&Field(accu, i), sp[i+5]);
1294 }
1295 }
1296 *--sp = accu;
1297 /* Create bytecode wrappers */
1298 Rocq_alloc_small(accu, 1, Abstract_tag);
1299 Code_val(accu) = typlbl;
1300 *--sp = accu;
1301 Rocq_alloc_small(accu, 1, Abstract_tag);
1302 Code_val(accu) = swlbl;
1303 /* We create the switch zipper */
1304 Rocq_alloc_small(block, 5, Default_tag);
1305 Field(block, 0) = sp[0];
1306 Field(block, 1) = accu;
1307 Field(block, 2) = sp[2];
1308 Field(block, 3) = sp[1];
1309 Field(block, 4) = rocq_env;
1310 sp += 3;
1311 accu = block;
1312 /* We create the atom */
1313 Rocq_alloc_small(block, 2, ATOM_SWITCH_TAG);
1314 Field(block, 0) = *sp++;
1315 Field(block, 1) = accu;
1316 accu = block;
1317 /* We create the accumulator */
1318 Rocq_alloc_small(block, 3, Closure_tag);
1319 Code_val(block) = accumulate;
1320 Field(block, 1) = Val_int(2);
1321 Field(block, 2) = accu;
1322 accu = block;
1323 }
1324 }
1325 Next;
1326 }
1327
1328
1329
1330 Instruct(MAKEACCU) {
1331 mlsize_t i, sz;
1332 print_instr("MAKEACCU");
1333 sz = rocq_extra_args + 4;
1334 if (sz <= Max_young_wosize) {
1335 Rocq_alloc_small(accu, sz, Closure_tag);
1336 Field(accu, 2) = Field(rocq_atom_tbl, *pc);
1337 for (i = 3; i < sz; ++i)
1338 Field(accu, i) = *sp++;
1339 } else {
1340 accu = caml_alloc_shr(sz, Closure_tag);
1341 caml_initialize(&Field(accu, 2), Field(rocq_atom_tbl, *pc));
1342 for (i = 3; i < sz; ++i)
1343 caml_initialize(&Field(accu, i), *sp++);
1344 }
1345 Code_val(accu) = accumulate;
1346 Field(accu, 1) = Val_int(2);
1347 pc = LoadRA(sp[0]);
1348 rocq_env = sp[1];
1349 rocq_extra_args = Long_val(sp[2]);
1350 sp += 3;
1351 Next;
1352 }
1353
1354 Instruct(SUBSTINSTANCE) {
1355 print_instr("SUBSTINSTANCE");
1356 print_int(*pc);
1357
1358 Setup_for_caml_call;
1359 accu = rocq_subst_instance(accu, Field(rocq_global_data, *pc++));
1360 Restore_after_caml_call;
1361 Handle_potential_exception(accu);
1362 Next;
1363 }
1364
1365 Instruct(BRANCH) {
1366 /* unconditional branching */
1367 print_instr("BRANCH");
1368 pc += *pc;
1369 /* pc = (code_t)(pc+*pc); */
1370 Next;
1371 }
1372
1373 Instruct(CHECKADDINT63){
1374 print_instr("CHECKADDINT63");
1375 CheckInt2();
1376 /* Adds the integer in the accumulator with
1377 the one ontop of the stack (which is poped)*/
1378 Uint63_add(accu, *sp++);
1379 Next;
1380 }
1381
1382 Instruct (CHECKADDCINT63) {
1383 print_instr("CHECKADDCINT63");
1384 CheckInt2();
1385 /* returns the sum with a carry */
1386 int c;
1387 Uint63_add(accu, *sp);
1388 Uint63_lt(c, accu, *sp);
1389 Swap_accu_sp;
1390 AllocCarry(c);
1391 Field(accu, 0) = *sp++;
1392 Next;
1393 }
1394
1395 Instruct (CHECKADDCARRYCINT63) {
1396 print_instr("ADDCARRYCINT63");
1397 CheckInt2();
1398 /* returns the sum plus one with a carry */
1399 int c;
1400 Uint63_addcarry(accu, *sp);
1401 Uint63_leq(c, accu, *sp);
1402 Swap_accu_sp;
1403 AllocCarry(c);
1404 Field(accu, 0) = *sp++;
1405 Next;
1406 }
1407
1408 Instruct (CHECKSUBINT63) {
1409 print_instr("CHECKSUBINT63");
1410 CheckInt2();
1411 /* returns the subtraction */
1412 Uint63_sub(accu, *sp++);
1413 Next;
1414 }
1415
1416 Instruct (CHECKSUBCINT63) {
1417 print_instr("SUBCINT63");
1418 CheckInt2();
1419 /* returns the subtraction with a carry */
1420 int c;
1421 Uint63_lt(c, accu, *sp);
1422 Uint63_sub(accu, *sp);
1423 Swap_accu_sp;
1424 AllocCarry(c);
1425 Field(accu, 0) = *sp++;
1426 Next;
1427 }
1428
1429 Instruct (CHECKSUBCARRYCINT63) {
1430 print_instr("SUBCARRYCINT63");
1431 CheckInt2();
1432 /* returns the subtraction minus one with a carry */
1433 int c;
1434 Uint63_leq(c,accu,*sp);
1435 Uint63_subcarry(accu,*sp);
1436 Swap_accu_sp;
1437 AllocCarry(c);
1438 Field(accu, 0) = *sp++;
1439 Next;
1440 }
1441
1442 Instruct (CHECKMULINT63) {
1443 print_instr("MULINT63");
1444 CheckInt2();
1445 /* returns the multiplication */
1446 Uint63_mul(accu,*sp++);
1447 Next;
1448 }
1449
1450 Instruct (CHECKMULCINT63) {
1451 /*returns the multiplication on a pair */
1452 print_instr("MULCINT63");
1453 CheckInt2();
1454 Uint63_mulc(accu, *sp, sp);
1455 *--sp = accu;
1456 AllocPair();
1457 Field(accu, 1) = *sp++;
1458 Field(accu, 0) = *sp++;
1459 Next;
1460 }
1461
1462 Instruct(CHECKDIVINT63) {
1463 print_instr("CHEKDIVINT63");
1464 CheckInt2();
1465 /* spiwack: a priori no need of the NON_STANDARD_DIV_MOD flag
1466 since it probably only concerns negative number.
1467 needs to be checked at this point */
1468 int b;
1469 Uint63_eq0(b, *sp);
1470 if (b) {
1471 accu = *sp++;
1472 }
1473 else {
1474 Uint63_div(accu, *sp++);
1475 }
1476 Next;
1477 }
1478
1479 Instruct(CHECKMODINT63) {
1480 print_instr("CHEKMODINT63");
1481 CheckInt2();
1482 int b;
1483 Uint63_eq0(b, *sp);
1484 if (b) {
1485 sp++;
1486 }
1487 else {
1488 Uint63_mod(accu,*sp++);
1489 }
1490 Next;
1491 }
1492
1493 Instruct (CHECKDIVEUCLINT63) {
1494 print_instr("DIVEUCLINT63");
1495 CheckInt2();
1496 /* spiwack: a priori no need of the NON_STANDARD_DIV_MOD flag
1497 since it probably only concerns negative number.
1498 needs to be checked at this point */
1499 int b;
1500 Uint63_eq0(b, *sp);
1501 if (b) {
1502 value block;
1503 Rocq_alloc_small(block, 2, rocq_tag_pair);
1504 Field(block, 0) = *sp++;
1505 Field(block, 1) = accu;
1506 accu = block;
1507 }
1508 else {
1509 *--sp = accu;
1510 Uint63_div(sp[0],sp[1]);
1511 Swap_accu_sp;
1512 Uint63_mod(accu,sp[1]);
1513 sp[1] = sp[0];
1514 Swap_accu_sp;
1515 AllocPair();
1516 Field(accu, 0) = sp[1];
1517 Field(accu, 1) = sp[0];
1518 sp += 2;
1519 }
1520 Next;
1521 }
1522
1523 Instruct(CHECKDIVSINT63) {
1524 print_instr("CHEKDIVSINT63");
1525 CheckInt2();
1526 int b;
1527 Uint63_eq0(b, *sp);
1528 if (b) {
1529 accu = *sp++;
1530 }
1531 else {
1532 Uint63_eqm1(b, *sp);
1533 if (b) {
1534 Uint63_neg(accu);
1535 sp++;
1536 }
1537 else {
1538 Uint63_divs(accu, *sp++);
1539 }
1540 }
1541 Next;
1542 }
1543
1544 Instruct(CHECKMODSINT63) {
1545 print_instr("CHEKMODSINT63");
1546 CheckInt2();
1547 int b;
1548 Uint63_eq0(b, *sp);
1549 if (b) {
1550 sp++;
1551 }
1552 else {
1553 Uint63_mods(accu,*sp++);
1554 }
1555 Next;
1556 }
1557
1558 Instruct (CHECKDIV21INT63) {
1559 print_instr("DIV21INT63");
1560 CheckInt3();
1561 Uint63_div21(accu, sp[0], sp[1], &(sp[1]));
1562 Swap_accu_sp;
1563 AllocPair();
1564 Field(accu, 0) = sp[1];
1565 Field(accu, 1) = sp[0];
1566 sp += 2;
1567 Next;
1568 }
1569
1570 Instruct(CHECKLXORINT63) {
1571 print_instr("CHECKLXORINT63");
1572 CheckInt2();
1573 Uint63_lxor(accu,*sp++);
1574 Next;
1575 }
1576
1577 Instruct(CHECKLORINT63) {
1578 print_instr("CHECKLORINT63");
1579 CheckInt2();
1580 Uint63_lor(accu,*sp++);
1581 Next;
1582 }
1583
1584 Instruct(CHECKLANDINT63) {
1585 print_instr("CHECKLANDINT63");
1586 CheckInt2();
1587 Uint63_land(accu,*sp++);
1588 Next;
1589 }
1590
1591 Instruct(CHECKLSLINT63) {
1592 print_instr("CHECKLSLINT63");
1593 CheckInt2();
1594 Uint63_lsl(accu,*sp++);
1595 Next;
1596 }
1597
1598 Instruct(CHECKLSRINT63) {
1599 print_instr("CHECKLSRINT63");
1600 CheckInt2();
1601 Uint63_lsr(accu,*sp++);
1602 Next;
1603 }
1604
1605 Instruct(CHECKASRINT63) {
1606 print_instr("CHECKASRINT63");
1607 CheckInt2();
1608 Uint63_asr(accu,*sp++);
1609 Next;
1610 }
1611
1612 Instruct (CHECKADDMULDIVINT63) {
1613 print_instr("CHECKADDMULDIVINT63");
1614 CheckInt3();
1615 Uint63_addmuldiv(accu,sp[0],sp[1]);
1616 sp += 2;
1617 Next;
1618 }
1619
1620 Instruct (CHECKEQINT63) {
1621 print_instr("CHECKEQINT63");
1622 CheckInt2();
1623 int b;
1624 Uint63_eq(b, accu, *sp++);
1625 accu = b ? rocq_true : rocq_false;
1626 Next;
1627 }
1628
1629 Instruct (CHECKLTINT63) {
1630 print_instr("CHECKLTINT63");
1631 CheckInt2();
1632 int b;
1633 Uint63_lt(b,accu,*sp++);
1634 accu = b ? rocq_true : rocq_false;
1635 Next;
1636 }
1637
1638 Instruct (CHECKLEINT63) {
1639 print_instr("CHECKLEINT63");
1640 CheckInt2();
1641 int b;
1642 Uint63_leq(b,accu,*sp++);
1643 accu = b ? rocq_true : rocq_false;
1644 Next;
1645 }
1646
1647 Instruct (CHECKLTSINT63) {
1648 print_instr("CHECKLTSINT63");
1649 CheckInt2();
1650 int b;
1651 Uint63_lts(b,accu,*sp++);
1652 accu = b ? rocq_true : rocq_false;
1653 Next;
1654 }
1655
1656 Instruct (CHECKLESINT63) {
1657 print_instr("CHECKLESINT63");
1658 CheckInt2();
1659 int b;
1660 Uint63_les(b,accu,*sp++);
1661 accu = b ? rocq_true : rocq_false;
1662 Next;
1663 }
1664
1665 Instruct (CHECKCOMPAREINT63) {
1666 /* returns Eq if equal, Lt if accu is less than *sp, Gt otherwise */
1667 /* assumes Inductive _ : _ := Eq | Lt | Gt */
1668 print_instr("CHECKCOMPAREINT63");
1669 CheckInt2();
1670 int b;
1671 Uint63_eq(b, accu, *sp);
1672 if (b) {
1673 accu = rocq_Eq;
1674 sp++;
1675 }
1676 else {
1677 Uint63_lt(b, accu, *sp++);
1678 accu = b ? rocq_Lt : rocq_Gt;
1679 }
1680 Next;
1681 }
1682
1683 Instruct (CHECKCOMPARESINT63) {
1684 /* returns Eq if equal, Lt if accu is less than *sp, Gt otherwise */
1685 /* assumes Inductive _ : _ := Eq | Lt | Gt */
1686 print_instr("CHECKCOMPARESINT63");
1687 CheckInt2();
1688 int b;
1689 Uint63_eq(b, accu, *sp);
1690 if (b) {
1691 accu = rocq_Eq;
1692 sp++;
1693 }
1694 else {
1695 Uint63_lts(b, accu, *sp++);
1696 accu = b ? rocq_Lt : rocq_Gt;
1697 }
1698 Next;
1699 }
1700
1701 Instruct (CHECKHEAD0INT63) {
1702 print_instr("CHECKHEAD0INT63");
1703 CheckInt1();
1704 Uint63_head0(accu);
1705 Next;
1706 }
1707
1708 Instruct (CHECKTAIL0INT63) {
1709 print_instr("CHECKTAIL0INT63");
1710 CheckInt1();
1711 Uint63_tail0(accu);
1712 Next;
1713 }
1714
1715 Instruct (CHECKOPPFLOAT) {
1716 print_instr("CHECKOPPFLOAT");
1717 CheckFloat1();
1718 Rocq_copy_double(-Double_val(accu));
1719 Next;
1720 }
1721
1722 Instruct (CHECKABSFLOAT) {
1723 print_instr("CHECKABSFLOAT");
1724 CheckFloat1();
1725 Rocq_copy_double(fabs(Double_val(accu)));
1726 Next;
1727 }
1728
1729 Instruct (CHECKEQFLOAT) {
1730 print_instr("CHECKEQFLOAT");
1731 CheckFloat2();
1732 accu = Double_val(accu) == Double_val(*sp++) ? rocq_true : rocq_false;
1733 Next;
1734 }
1735
1736 Instruct (CHECKLTFLOAT) {
1737 print_instr("CHECKLTFLOAT");
1738 CheckFloat2();
1739 accu = Double_val(accu) < Double_val(*sp++) ? rocq_true : rocq_false;
1740 Next;
1741 }
1742
1743 Instruct (CHECKLEFLOAT) {
1744 print_instr("CHECKLEFLOAT");
1745 CheckFloat2();
1746 accu = Double_val(accu) <= Double_val(*sp++) ? rocq_true : rocq_false;
1747 Next;
1748 }
1749
1750 Instruct (CHECKCOMPAREFLOAT) {
1751 double x, y;
1752 print_instr("CHECKCOMPAREFLOAT");
1753 CheckFloat2();
1754 x = Double_val(accu);
1755 y = Double_val(*sp++);
1756 if(x < y) {
1757 accu = rocq_FLt;
1758 }
1759 else if(x > y) {
1760 accu = rocq_FGt;
1761 }
1762 else if(x == y) {
1763 accu = rocq_FEq;
1764 }
1765 else { // nan value
1766 accu = rocq_FNotComparable;
1767 }
1768 Next;
1769 }
1770
1771 Instruct (CHECKEQUALFLOAT) {
1772 double x, y;
1773 print_instr("CHECKEQUALFLOAT");
1774 CheckFloat2();
1775 x = Double_val(accu);
1776 y = Double_val(*sp++);
1777 if (x == y) {
1778 accu = (x != 0 || !signbit(x) == !signbit(y)) ? rocq_true : rocq_false;
1779 /* note that the spec of signbit only promises a "nonzero value"
1780 so we need to normalize booleans with ! before comparing them */
1781 } else {
1782 accu = (x != x && y != y) ? rocq_true : rocq_false;
1783 /* is_nan(x) && is_nan(y) */
1784 }
1785 Next;
1786 }
1787
1788 Instruct (CHECKCLASSIFYFLOAT) {
1789 double x;
1790 print_instr("CHECKCLASSIFYFLOAT");
1791 CheckFloat1();
1792 x = Double_val(accu);
1793 switch (fpclassify(x)) {
1794 case FP_NORMAL:
1795 accu = signbit(x) ? rocq_NNormal : rocq_PNormal;
1796 break;
1797 case FP_SUBNORMAL:
1798 accu = signbit(x) ? rocq_NSubn : rocq_PSubn;
1799 break;
1800 case FP_ZERO:
1801 accu = signbit(x) ? rocq_NZero : rocq_PZero;
1802 break;
1803 case FP_INFINITE:
1804 accu = signbit(x) ? rocq_NInf : rocq_PInf;
1805 break;
1806 default: /* FP_NAN */
1807 accu = rocq_NaN;
1808 break;
1809 }
1810 Next;
1811 }
1812
1813 Instruct (CHECKADDFLOAT) {
1814 print_instr("CHECKADDFLOAT");
1815 CheckFloat2();
1816 Rocq_copy_double(Double_val(accu) + Double_val(*sp++));
1817 Next;
1818 }
1819
1820 Instruct (CHECKSUBFLOAT) {
1821 print_instr("CHECKSUBFLOAT");
1822 CheckFloat2();
1823 Rocq_copy_double(Double_val(accu) - Double_val(*sp++));
1824 Next;
1825 }
1826
1827 Instruct (CHECKMULFLOAT) {
1828 print_instr("CHECKMULFLOAT");
1829 CheckFloat2();
1830 Rocq_copy_double(Double_val(accu) * Double_val(*sp++));
1831 Next;
1832 }
1833
1834 Instruct (CHECKDIVFLOAT) {
1835 print_instr("CHECKDIVFLOAT");
1836 CheckFloat2();
1837 Rocq_copy_double(Double_val(accu) / Double_val(*sp++));
1838 Next;
1839 }
1840
1841 Instruct (CHECKSQRTFLOAT) {
1842 print_instr("CHECKSQRTFLOAT");
1843 CheckFloat1();
1844 Rocq_copy_double(sqrt(Double_val(accu)));
1845 Next;
1846 }
1847
1848 Instruct (CHECKFLOATOFINT63) {
1849 print_instr("CHECKFLOATOFINT63");
1850 CheckInt1();
1851 Uint63_to_double(accu);
1852 Next;
1853 }
1854
1855 Instruct (CHECKFLOATNORMFRMANTISSA) {
1856 double f;
1857 print_instr("CHECKFLOATNORMFRMANTISSA");
1858 CheckFloat1();
1859 f = fabs(Double_val(accu));
1860 if (f >= 0.5 && f < 1) {
1861 Uint63_of_double(ldexp(f, DBL_MANT_DIG));
1862 }
1863 else {
1864 Uint63_of_int(Val_int(0));
1865 }
1866 Next;
1867 }
1868
1869 Instruct (CHECKFRSHIFTEXP) {
1870 int exp;
1871 double f;
1872 print_instr("CHECKFRSHIFTEXP");
1873 CheckFloat1();
1874 /* frexp(infinity) incorrectly returns nan on mingw */
1875 #if defined(__MINGW32__) || defined(__MINGW64__)
1876 if (fpclassify(Double_val(accu)) == FP_INFINITE) {
1877 f = Double_val(accu);
1878 } else
1879 #endif
1880 f = frexp(Double_val(accu), &exp);
1881 if (fpclassify(f) == FP_NORMAL) {
1882 exp += FLOAT_EXP_SHIFT;
1883 }
1884 else {
1885 exp = 0;
1886 }
1887 Rocq_copy_double(f);
1888 *--sp = accu;
1889 #ifdef ARCH_SIXTYFOUR
1890 Rocq_alloc_small(accu, 2, rocq_tag_pair);
1891 Field(accu, 1) = Val_int(exp);
1892 #else
1893 Uint63_of_int(Val_int(exp));
1894 *--sp = accu;
1895 Rocq_alloc_small(accu, 2, rocq_tag_pair);
1896 Field(accu, 1) = *sp++;
1897 #endif
1898 Field(accu, 0) = *sp++;
1899 Next;
1900 }
1901
1902 Instruct (CHECKLDSHIFTEXP) {
1903 print_instr("CHECKLDSHIFTEXP");
1904 CheckPrimArgs(Is_double(accu) && Is_uint63(sp[0]), apply2);
1905 Swap_accu_sp;
1906 Uint63_to_int_min(accu, Val_int(2 * FLOAT_EXP_SHIFT));
1907 accu = Int_val(accu);
1908 Rocq_copy_double(ldexp(Double_val(*sp++), accu - FLOAT_EXP_SHIFT));
1909 Next;
1910 }
1911
1912 Instruct (CHECKNEXTUPFLOAT) {
1913 print_instr("CHECKNEXTUPFLOAT");
1914 CheckFloat1();
1915 Rocq_copy_double(rocq_next_up(Double_val(accu)));
1916 Next;
1917 }
1918
1919 Instruct (CHECKNEXTDOWNFLOAT) {
1920 print_instr("CHECKNEXTDOWNFLOAT");
1921 CheckFloat1();
1922 Rocq_copy_double(rocq_next_down(Double_val(accu)));
1923 Next;
1924 }
1925
1926 Instruct (CHECKNEXTUPFLOATINPLACE) {
1927 print_instr("CHECKNEXTUPFLOATINPLACE");
1928 CheckFloat1();
1929 Store_double_val(accu, rocq_next_up(Double_val(accu)));
1930 Next;
1931 }
1932
1933 Instruct (CHECKNEXTDOWNFLOATINPLACE) {
1934 print_instr("CHECKNEXTDOWNFLOATINPLACE");
1935 CheckFloat1();
1936 Store_double_val(accu, rocq_next_down(Double_val(accu)));
1937 Next;
1938 }
1939
1940 Instruct(CHECKCAMLCALL2_1) {
1941 // arity-2 callback, the last argument can be an accumulator
1942 value arg;
1943 print_instr("CHECKCAMLCALL2_1");
1944 if (!Is_accu(accu)) {
1945 pc++;
1946 print_int(*pc);
1947 arg = sp[0];
1948 Setup_for_caml_call;
1949 accu = caml_callback2_exn(Field(rocq_global_data, *pc), accu, arg);
1950 Restore_after_caml_call;
1951 Handle_potential_exception(accu);
1952 sp += 1;
1953 pc++;
1954 } else pc += *pc;
1955 Next;
1956 }
1957
1958 Instruct(CHECKCAMLCALL1) {
1959 // arity-1 callback, no argument can be an accumulator
1960 print_instr("CHECKCAMLCALL1");
1961 if (!Is_accu(accu)) {
1962 pc++;
1963 Setup_for_caml_call;
1964 print_int(*pc);
1965 accu = caml_callback_exn(Field(rocq_global_data, *pc), accu);
1966 Restore_after_caml_call;
1967 Handle_potential_exception(accu);
1968 pc++;
1969 }
1970 else pc += *pc;
1971 Next;
1972 }
1973
1974 Instruct(CHECKCAMLCALL2) {
1975 // arity-2 callback, no argument can be an accumulator
1976 value arg;
1977 print_instr("CHECKCAMLCALL2");
1978 if (!Is_accu(accu) && !Is_accu(sp[0])) {
1979 pc++;
1980 arg = sp[0];
1981 Setup_for_caml_call;
1982 print_int(*pc);
1983 accu = caml_callback2_exn(Field(rocq_global_data, *pc), accu, arg);
1984 Restore_after_caml_call;
1985 Handle_potential_exception(accu);
1986 sp += 1;
1987 pc++;
1988 } else pc += *pc;
1989 Next;
1990 }
1991
1992 Instruct(CHECKCAMLCALL3) {
1993 // arity-3 callback, no argument can be an accumulator
1994 value arg1;
1995 value arg2;
1996 print_instr("CHECKCAMLCALL3");
1997 if (!Is_accu(accu) && !Is_accu(sp[0]) && !Is_accu(sp[1])) {
1998 pc++;
1999 arg1 = sp[0];
2000 arg2 = sp[1];
2001 Setup_for_caml_call;
2002 print_int(*pc);
2003 accu = caml_callback3_exn(Field(rocq_global_data, *pc), accu, arg1, arg2);
2004 Restore_after_caml_call;
2005 Handle_potential_exception(accu);
2006 sp += 2;
2007 pc++;
2008 } else pc += *pc;
2009 Next;
2010 }
2011
2012 Instruct(CHECKCAMLCALL3_1) {
2013 // arity-3 callback, the last argument can be an accumulator
2014 value arg1;
2015 value arg2;
2016 print_instr("CHECKCAMLCALL3_1");
2017 if (!Is_accu(accu) && !Is_accu(sp[0])) {
2018 pc++;
2019 arg1 = sp[0];
2020 arg2 = sp[1];
2021 Setup_for_caml_call;
2022 print_int(*pc);
2023 accu = caml_callback3_exn(Field(rocq_global_data, *pc), accu, arg1, arg2);
2024 Restore_after_caml_call;
2025 Handle_potential_exception(accu);
2026 sp += 2;
2027 pc++;
2028 } else pc += *pc;
2029 Next;
2030 }
2031
2032 /* Debugging and machine control */
2033
2034 Instruct(STOP){
2035 print_instr("STOP");
2036 rocq_sp = sp;
2037 CAMLreturn(accu);
2038 }
2039
2040
2041 #ifndef THREADED_CODE
2042 default:
2043 /*fprintf(stderr, "%d\n", *pc);*/
2044 caml_failwith("Rocq VM: Fatal error: bad opcode");
2045 }
2046 }
2047 #endif
2048 }
2049
2050 value rocq_push_ra(value code) {
2051 code_t tcode = Code_val(code);
2052 print_instr("push_ra");
2053 rocq_sp -= 3;
2054 rocq_sp[0] = StoreRA(tcode);
2055 rocq_sp[1] = Val_unit;
2056 rocq_sp[2] = Val_long(0);
2057 return Val_unit;
2058 }
2059
2060 value rocq_push_val(value v) {
2061 print_instr("push_val");
2062 *--rocq_sp = v;
2063 return Val_unit;
2064 }
2065
2066 value rocq_push_arguments(value args) {
2067 int nargs,i;
2068 value * sp = rocq_sp;
2069 nargs = Wosize_val(args) - 3;
2070 CHECK_STACK(nargs);
2071 rocq_sp -= nargs;
2072 print_instr("push_args");print_int(nargs);
2073 for (i = 0; i < nargs; i++) rocq_sp[i] = Field(args, i + 3);
2074 return Val_unit;
2075 }
2076
2077 value rocq_push_vstack(value stk, value max_stack_size) {
2078 int len,i;
2079 value * sp = rocq_sp;
2080 len = Wosize_val(stk);
2081 CHECK_STACK(len);
2082 rocq_sp -= len;
2083 print_instr("push_vstack");print_int(len);
2084 for(i = 0; i < len; i++) rocq_sp[i] = Field(stk,i);
2085 sp = rocq_sp;
2086 CHECK_STACK(uint_of_value(max_stack_size));
2087 return Val_unit;
2088 }
2089
2090 value rocq_interprete_ml(value tcode, value a, value t, value g, value e, value ea) {
2091 // Registering the other arguments w.r.t. the OCaml GC is done by rocq_interprete
2092 CAMLparam1(tcode);
2093 print_instr("rocq_interprete");
2094 value res = rocq_interprete(Code_val(tcode), a, t, g, e, Long_val(ea));
2095 print_instr("end rocq_interprete");
2096 CAMLreturn(res);
2097 }
2098
2099 value rocq_interprete_byte(value* argv, int argn){
2100 return rocq_interprete_ml(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
2101 }