Debian Codegraph @

kernel/byterun/rocq_values.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 #include <stdlib.h>
12 #include <stdio.h>
13 #include <caml/config.h>
14 #include <caml/memory.h>
15 #include "rocq_fix_code.h"
16 #include "rocq_instruct.h"
17 #include "rocq_memory.h"
18 #include "rocq_values.h"
19 #include <memory.h>
20 /* KIND OF VALUES */
21
22 #define Setup_for_gc
23 #define Restore_after_gc
24
25 #define Is_instruction(c, i) rocq_is_instruction(*c, i)
26
27 value rocq_kind_of_closure(value v) {
28 opcode_t * c;
29 int is_app = 0;
30 c = Code_val(v);
31 if (Is_instruction(c, GRAB)) return Val_int(0);
32 if (Is_instruction(c, RESTART)) {is_app = 1; c++;}
33 if (Is_instruction(c, GRABREC)) return Val_int(1+is_app);
34 if (Is_instruction(c, MAKEACCU)) return Val_int(3);
35 return Val_int(0);
36 }
37
38 value rocq_is_accumulate_code(value code)
39 {
40 code_t q = Code_val(code);
41 int res;
42 res = Is_instruction(q,ACCUMULATE);
43 return Val_bool(res);
44 }
45
46 /* DESTRUCT ACCU */
47
48 value rocq_closure_arity(value clos) {
49 opcode_t * c = Code_val(clos);
50 if (Is_instruction(c,RESTART)) {
51 c++;
52 if (Is_instruction(c,GRAB)) return Val_int(4 + c[1] - Wosize_val(clos));
53 else {
54 if (Wosize_val(clos) != 3) caml_failwith("Rocq Values : rocq_closure_arity");
55 return Val_int(1);
56 }
57 }
58 if (Is_instruction(c,GRAB)) return Val_int(1 + c[1]);
59 return Val_int(1);
60 }
61
62 /* Fonction sur les fix */
63
64 value rocq_current_fix(value v) {
65 if (Tag_val(v) == Closure_tag) return Val_int(0);
66 else return Val_long(Wsize_bsize(Infix_offset_val(v)) / 3);
67 }
68
69 value rocq_shift_fix(value v, value offset) {
70 return v + Int_val(offset) * 3 * sizeof(value);
71 }
72
73 value rocq_last_fix(value v) {
74 return v + (Int_val(Field(v, 1)) - 2) * sizeof(value);
75 }
76
77 value rocq_set_bytecode_field(value v, value i, value code) {
78 // No write barrier because the bytecode does not live on the OCaml heap
79 Field(v, Long_val(i)) = (value) Code_val(code);
80 return Val_unit;
81 }
82
83 value rocq_offset_tcode(value code,value offset){
84 CAMLparam1(code);
85 CAMLlocal1(res);
86 res = caml_alloc_small(1, Abstract_tag);
87 Code_val(res) = Code_val(code) + Int_val(offset);
88 CAMLreturn(res);
89 }
90
91 value rocq_int_tcode(value pc, value offset) {
92 code_t code = Code_val(pc);
93 return Val_int(*((code_t) code + Int_val(offset)));
94 }
95
96 value rocq_tcode_array(value tcodes) {
97 CAMLparam1(tcodes);
98 CAMLlocal2(res, tmp);
99 int i;
100 /* Assumes that the vector of types is small. This was implicit in the
101 previous code which was building the type array using Alloc_small. */
102 res = caml_alloc_small(Wosize_val(tcodes), Default_tag);
103 for (i = 0; i < Wosize_val(tcodes); i++) {
104 tmp = caml_alloc_small(1, Abstract_tag);
105 Code_val(tmp) = (code_t) Field(tcodes, i);
106 Store_field(res, i, tmp);
107 }
108 CAMLreturn(res);
109 }
110
111 /* The rocq_curry2_1 function returns a pointer to some code that
112 immediately branches to caml_curry2_1. It can be used as field 0 of
113 an OCaml closure, as long as field 3 contains a closure whose code
114 pointer accepts exactly two arguments (the first argument is stored
115 in field 2).
116
117 Since the word before the branch indicates to the garbage collector
118 that this block should be ignored, the code pointer can be used
119 inside blocks that do not have tag 247. This 2043 value is the
120 result of Caml_out_of_heap_header(2, Abstract_tag).
121
122 Keep the compile-time checks in sync with rocq_configure.c */
123
124 #ifdef NO_NATIVE_COMPUTE
125
126 value rocq_curry2_1_addr(value v) {
127 return Val_unit;
128 }
129
130 #elif defined(NO_NAKED_POINTERS)
131
132 __attribute__((weak))
133 void caml_curry2_1() {
134 abort();
135 }
136
137 #if defined(__GNUC__) && defined(__amd64__)
138
139 asm(".align 8\n\t"
140 ".quad 2043\n"
141 "rocq_curry2_1:\n\t"
142 "jmp caml_curry2_1\n");
143
144 #elif defined(__GNUC__) && defined(__i386__)
145
146 asm(".align 4\n\t"
147 ".long 2043\n"
148 "rocq_curry2_1:\n\t"
149 "jmp caml_curry2_1\n");
150
151 #else
152 #error "Unsupported architecture for native_compute."
153 #endif
154
155 value rocq_curry2_1_addr(value v) {
156 extern void rocq_curry2_1();
157 return (value)&rocq_curry2_1;
158 }
159
160 #else // not NO_NAKED_POINTERS
161
162 value rocq_curry2_1_addr(value v) {
163 extern void caml_curry2_1() __attribute__((weak));
164 return (value)&caml_curry2_1;
165 }
166
167 #endif