Skip to content

Commit 96556f8

Browse files
committed
Compiler: consume hints for unsafe string/bytes/ba get/set
1 parent 76f44a3 commit 96556f8

File tree

3 files changed

+165
-14
lines changed

3 files changed

+165
-14
lines changed

compiler/lib/parse_bytecode.ml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,23 @@ and compile infos pc state (instrs : instr list) =
19711971
let y = State.accu state in
19721972
let z = State.peek 0 state in
19731973
let x, state = State.fresh_var state in
1974-
1974+
let prim =
1975+
match prim with
1976+
| "caml_ba_uint8_get16"
1977+
| "caml_ba_uint8_get32"
1978+
| "caml_ba_uint8_get64"
1979+
| "caml_string_get16"
1980+
| "caml_string_get32"
1981+
| "caml_string_get64"
1982+
| "caml_bytes_get16"
1983+
| "caml_bytes_get32"
1984+
| "caml_bytes_get64" ->
1985+
let hints = Hints.find infos.hints pc in
1986+
if List.mem ~eq:Hints.equal Hints.Hint_unsafe hints
1987+
then prim ^ "u"
1988+
else prim
1989+
| _ -> prim
1990+
in
19751991
if debug_parser ()
19761992
then
19771993
Format.printf
@@ -1994,7 +2010,20 @@ and compile infos pc state (instrs : instr list) =
19942010
let z = State.peek 0 state in
19952011
let t = State.peek 1 state in
19962012
let x, state = State.fresh_var state in
1997-
2013+
let prim =
2014+
match prim with
2015+
| "caml_ba_uint8_set16"
2016+
| "caml_ba_uint8_set32"
2017+
| "caml_ba_uint8_set64"
2018+
| "caml_bytes_set16"
2019+
| "caml_bytes_set32"
2020+
| "caml_bytes_set64" ->
2021+
let hints = Hints.find infos.hints pc in
2022+
if List.mem ~eq:Hints.equal Hints.Hint_unsafe hints
2023+
then prim ^ "u"
2024+
else prim
2025+
| _ -> prim
2026+
in
19982027
if debug_parser ()
19992028
then
20002029
Format.printf

runtime/js/bigarray.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ function caml_ba_get_generic(ba, i) {
548548
return ba.get(ofs);
549549
}
550550

551+
552+
//Provides: caml_ba_uint8_get16u
553+
function caml_ba_uint8_get16u(ba, i0) {
554+
var ofs = ba.offset(i0);
555+
var b1 = ba.get(ofs);
556+
var b2 = ba.get(ofs + 1);
557+
return b1 | (b2 << 8);
558+
}
551559
//Provides: caml_ba_uint8_get16
552560
//Requires: caml_array_bound_error
553561
function caml_ba_uint8_get16(ba, i0) {
@@ -558,6 +566,16 @@ function caml_ba_uint8_get16(ba, i0) {
558566
return b1 | (b2 << 8);
559567
}
560568

569+
//Provides: caml_ba_uint8_get32u
570+
function caml_ba_uint8_get32u(ba, i0) {
571+
var ofs = ba.offset(i0);
572+
var b1 = ba.get(ofs + 0);
573+
var b2 = ba.get(ofs + 1);
574+
var b3 = ba.get(ofs + 2);
575+
var b4 = ba.get(ofs + 3);
576+
return (b1 << 0) | (b2 << 8) | (b3 << 16) | (b4 << 24);
577+
}
578+
561579
//Provides: caml_ba_uint8_get32
562580
//Requires: caml_array_bound_error
563581
function caml_ba_uint8_get32(ba, i0) {
@@ -570,6 +588,21 @@ function caml_ba_uint8_get32(ba, i0) {
570588
return (b1 << 0) | (b2 << 8) | (b3 << 16) | (b4 << 24);
571589
}
572590

591+
//Provides: caml_ba_uint8_get64u
592+
//Requires: caml_int64_of_bytes
593+
function caml_ba_uint8_get64u(ba, i0) {
594+
var ofs = ba.offset(i0);
595+
var b1 = ba.get(ofs + 0);
596+
var b2 = ba.get(ofs + 1);
597+
var b3 = ba.get(ofs + 2);
598+
var b4 = ba.get(ofs + 3);
599+
var b5 = ba.get(ofs + 4);
600+
var b6 = ba.get(ofs + 5);
601+
var b7 = ba.get(ofs + 6);
602+
var b8 = ba.get(ofs + 7);
603+
return caml_int64_of_bytes([b8, b7, b6, b5, b4, b3, b2, b1]);
604+
}
605+
573606
//Provides: caml_ba_uint8_get64
574607
//Requires: caml_array_bound_error, caml_int64_of_bytes
575608
function caml_ba_uint8_get64(ba, i0) {
@@ -608,6 +641,14 @@ function caml_ba_set_generic(ba, i, v) {
608641
return 0;
609642
}
610643

644+
//Provides: caml_ba_uint8_set16u
645+
function caml_ba_uint8_set16u(ba, i0, v) {
646+
var ofs = ba.offset(i0);
647+
ba.set(ofs + 0, v & 0xff);
648+
ba.set(ofs + 1, (v >>> 8) & 0xff);
649+
return 0;
650+
}
651+
611652
//Provides: caml_ba_uint8_set16
612653
//Requires: caml_array_bound_error
613654
function caml_ba_uint8_set16(ba, i0, v) {
@@ -618,6 +659,15 @@ function caml_ba_uint8_set16(ba, i0, v) {
618659
return 0;
619660
}
620661

662+
//Provides: caml_ba_uint8_set32u
663+
function caml_ba_uint8_set32u(ba, i0, v) {
664+
var ofs = ba.offset(i0);
665+
ba.set(ofs + 0, v & 0xff);
666+
ba.set(ofs + 1, (v >>> 8) & 0xff);
667+
ba.set(ofs + 2, (v >>> 16) & 0xff);
668+
ba.set(ofs + 3, (v >>> 24) & 0xff);
669+
return 0;
670+
}
621671
//Provides: caml_ba_uint8_set32
622672
//Requires: caml_array_bound_error
623673
function caml_ba_uint8_set32(ba, i0, v) {
@@ -630,6 +680,15 @@ function caml_ba_uint8_set32(ba, i0, v) {
630680
return 0;
631681
}
632682

683+
//Provides: caml_ba_uint8_set64u
684+
//Requires: caml_int64_to_bytes
685+
function caml_ba_uint8_set64u(ba, i0, v) {
686+
var ofs = ba.offset(i0);
687+
var v = caml_int64_to_bytes(v);
688+
for (var i = 0; i < 8; i++) ba.set(ofs + i, v[7 - i]);
689+
return 0;
690+
}
691+
633692
//Provides: caml_ba_uint8_set64
634693
//Requires: caml_array_bound_error, caml_int64_to_bytes
635694
function caml_ba_uint8_set64(ba, i0, v) {

runtime/js/mlBytes.js

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,50 @@ function caml_string_get(s, i) {
142142
}
143143

144144
//Provides: caml_string_get16
145-
//Requires: caml_string_unsafe_get, caml_string_bound_error
145+
//Requires: caml_string_bound_error
146146
//Requires: caml_ml_string_length
147+
//Requires: caml_string_get16u
147148
function caml_string_get16(s, i) {
148149
if (i >>> 0 >= caml_ml_string_length(s) - 1) caml_string_bound_error();
150+
return caml_string_get16u(s,i);
151+
}
152+
153+
//Provides: caml_string_get16u
154+
//Requires: caml_string_unsafe_get
155+
function caml_string_get16u(s, i) {
149156
var b1 = caml_string_unsafe_get(s, i),
150157
b2 = caml_string_unsafe_get(s, i + 1);
151158
return (b2 << 8) | b1;
152159
}
153160

154161
//Provides: caml_bytes_get16
155-
//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error
162+
//Requires: caml_bytes_bound_error
163+
//Requires: caml_bytes_get16u
156164
function caml_bytes_get16(s, i) {
157165
if (i >>> 0 >= s.l - 1) caml_bytes_bound_error();
166+
return caml_bytes_get16u(s,i)
167+
}
168+
169+
//Provides: caml_bytes_get16u
170+
//Requires: caml_bytes_unsafe_get
171+
function caml_bytes_get16u(s, i) {
158172
var b1 = caml_bytes_unsafe_get(s, i),
159173
b2 = caml_bytes_unsafe_get(s, i + 1);
160174
return (b2 << 8) | b1;
161175
}
162176

163177
//Provides: caml_string_get32
164-
//Requires: caml_string_unsafe_get, caml_string_bound_error
178+
//Requires: caml_string_bound_error
165179
//Requires: caml_ml_string_length
180+
//Requires: caml_string_get32u
166181
function caml_string_get32(s, i) {
167182
if (i >>> 0 >= caml_ml_string_length(s) - 3) caml_string_bound_error();
183+
return caml_string_get32u(s,i);
184+
}
185+
186+
//Provides: caml_string_get32u
187+
//Requires: caml_string_unsafe_get
188+
function caml_string_get32u(s, i) {
168189
var b1 = caml_string_unsafe_get(s, i),
169190
b2 = caml_string_unsafe_get(s, i + 1),
170191
b3 = caml_string_unsafe_get(s, i + 2),
@@ -173,9 +194,16 @@ function caml_string_get32(s, i) {
173194
}
174195

175196
//Provides: caml_bytes_get32
176-
//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error
197+
//Requires: caml_bytes_bound_error
198+
//Requires: caml_bytes_get32u
177199
function caml_bytes_get32(s, i) {
178200
if (i >>> 0 >= s.l - 3) caml_bytes_bound_error();
201+
return caml_bytes_get32u(s,i)
202+
}
203+
204+
//Provides: caml_bytes_get32u
205+
//Requires: caml_bytes_unsafe_get
206+
function caml_bytes_get32u(s, i) {
179207
var b1 = caml_bytes_unsafe_get(s, i),
180208
b2 = caml_bytes_unsafe_get(s, i + 1),
181209
b3 = caml_bytes_unsafe_get(s, i + 2),
@@ -184,11 +212,18 @@ function caml_bytes_get32(s, i) {
184212
}
185213

186214
//Provides: caml_string_get64
187-
//Requires: caml_string_unsafe_get, caml_string_bound_error
188-
//Requires: caml_int64_of_bytes
215+
//Requires: caml_string_bound_error
189216
//Requires: caml_ml_string_length
217+
//Requires: caml_string_get64u
190218
function caml_string_get64(s, i) {
191219
if (i >>> 0 >= caml_ml_string_length(s) - 7) caml_string_bound_error();
220+
return caml_string_get64u(s,i);
221+
}
222+
223+
//Provides: caml_string_get64u
224+
//Requires: caml_string_unsafe_get
225+
//Requires: caml_int64_of_bytes
226+
function caml_string_get64u(s, i) {
192227
var a = new Array(8);
193228
for (var j = 0; j < 8; j++) {
194229
a[7 - j] = caml_string_unsafe_get(s, i + j);
@@ -197,10 +232,17 @@ function caml_string_get64(s, i) {
197232
}
198233

199234
//Provides: caml_bytes_get64
200-
//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error
201-
//Requires: caml_int64_of_bytes
235+
//Requires: caml_bytes_bound_error
236+
//Requires: caml_bytes_get64u
202237
function caml_bytes_get64(s, i) {
203238
if (i >>> 0 >= s.l - 7) caml_bytes_bound_error();
239+
return caml_bytes_get64u(s,i)
240+
}
241+
242+
//Provides: caml_bytes_get64u
243+
//Requires: caml_bytes_unsafe_get
244+
//Requires: caml_int64_of_bytes
245+
function caml_bytes_get64u(s, i) {
204246
var a = new Array(8);
205247
for (var j = 0; j < 8; j++) {
206248
a[7 - j] = caml_bytes_unsafe_get(s, i + j);
@@ -231,9 +273,16 @@ function caml_string_set(s, i, c) {
231273
}
232274

233275
//Provides: caml_bytes_set16
234-
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set
276+
//Requires: caml_bytes_bound_error
277+
//Requires: caml_bytes_set16u
235278
function caml_bytes_set16(s, i, i16) {
236279
if (i >>> 0 >= s.l - 1) caml_bytes_bound_error();
280+
return caml_bytes_set16u(s,i,i16);
281+
}
282+
283+
//Provides: caml_bytes_set16u
284+
//Requires: caml_bytes_unsafe_set
285+
function caml_bytes_set16u(s, i, i16) {
237286
var b2 = 0xff & (i16 >> 8),
238287
b1 = 0xff & i16;
239288
caml_bytes_unsafe_set(s, i + 0, b1);
@@ -242,9 +291,16 @@ function caml_bytes_set16(s, i, i16) {
242291
}
243292

244293
//Provides: caml_bytes_set32
245-
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set
294+
//Requires: caml_bytes_bound_error
295+
//Requires: caml_bytes_set32u
246296
function caml_bytes_set32(s, i, i32) {
247297
if (i >>> 0 >= s.l - 3) caml_bytes_bound_error();
298+
return caml_bytes_set32u(s,i,i32);
299+
}
300+
301+
//Provides: caml_bytes_set32u
302+
//Requires: caml_bytes_unsafe_set
303+
function caml_bytes_set32u(s, i, i32) {
248304
var b4 = 0xff & (i32 >> 24),
249305
b3 = 0xff & (i32 >> 16),
250306
b2 = 0xff & (i32 >> 8),
@@ -257,10 +313,17 @@ function caml_bytes_set32(s, i, i32) {
257313
}
258314

259315
//Provides: caml_bytes_set64
260-
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set
261-
//Requires: caml_int64_to_bytes
316+
//Requires: caml_bytes_bound_error
317+
//Requires: caml_bytes_set64u
262318
function caml_bytes_set64(s, i, i64) {
263319
if (i >>> 0 >= s.l - 7) caml_bytes_bound_error();
320+
return caml_bytes_set64u(s,i,i64);
321+
}
322+
323+
//Provides: caml_bytes_set64u
324+
//Requires: caml_bytes_unsafe_set
325+
//Requires: caml_int64_to_bytes
326+
function caml_bytes_set64u(s, i, i64) {
264327
var a = caml_int64_to_bytes(i64);
265328
for (var j = 0; j < 8; j++) {
266329
caml_bytes_unsafe_set(s, i + 7 - j, a[j]);

0 commit comments

Comments
 (0)