Skip to content

Commit 536ff4d

Browse files
committed
Compiler: consume hints for unsafe string/bytes/ba get/set
1 parent f463910 commit 536ff4d

File tree

4 files changed

+177
-31
lines changed

4 files changed

+177
-31
lines changed

compiler/lib/parse_bytecode.ml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,21 @@ and compile infos pc state (instrs : instr list) =
18801880
let y = State.accu state in
18811881
let z = State.peek 0 state in
18821882
let x, state = State.fresh_var state in
1883-
1883+
let prim =
1884+
match prim with
1885+
| "caml_ba_uint8_get16"
1886+
| "caml_ba_uint8_get32"
1887+
| "caml_ba_uint8_get64"
1888+
| "caml_string_get16"
1889+
| "caml_string_get32"
1890+
| "caml_string_get64"
1891+
| "caml_bytes_get16"
1892+
| "caml_bytes_get32"
1893+
| "caml_bytes_get64" ->
1894+
let hints = Hints.find infos.hints pc in
1895+
if List.mem Hints.Hint_unsafe ~set:hints then prim ^ "u" else prim
1896+
| _ -> prim
1897+
in
18841898
if debug_parser ()
18851899
then
18861900
Format.printf
@@ -1903,7 +1917,18 @@ and compile infos pc state (instrs : instr list) =
19031917
let z = State.peek 0 state in
19041918
let t = State.peek 1 state in
19051919
let x, state = State.fresh_var state in
1906-
1920+
let prim =
1921+
match prim with
1922+
| "caml_ba_uint8_set16"
1923+
| "caml_ba_uint8_set32"
1924+
| "caml_ba_uint8_set64"
1925+
| "caml_bytes_set16"
1926+
| "caml_bytes_set32"
1927+
| "caml_bytes_set64" ->
1928+
let hints = Hints.find infos.hints pc in
1929+
if List.mem Hints.Hint_unsafe ~set:hints then prim ^ "u" else prim
1930+
| _ -> prim
1931+
in
19071932
if debug_parser ()
19081933
then
19091934
Format.printf

compiler/tests-full/stdlib.cma.expected.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,11 +4740,13 @@
47404740
caml_bswap16 = runtime.caml_bswap16,
47414741
caml_bytes_get = runtime.caml_bytes_get,
47424742
caml_bytes_get16 = runtime.caml_bytes_get16,
4743+
caml_bytes_get16u = runtime.caml_bytes_get16u,
47434744
caml_bytes_get32 = runtime.caml_bytes_get32,
47444745
caml_bytes_get64 = runtime.caml_bytes_get64,
47454746
caml_bytes_of_string = runtime.caml_bytes_of_string,
47464747
caml_bytes_set = runtime.caml_bytes_set,
47474748
caml_bytes_set16 = runtime.caml_bytes_set16,
4749+
caml_bytes_set16u = runtime.caml_bytes_set16u,
47484750
caml_bytes_set32 = runtime.caml_bytes_set32,
47494751
caml_bytes_set64 = runtime.caml_bytes_set64,
47504752
caml_bytes_unsafe_get = runtime.caml_bytes_unsafe_get,
@@ -5529,14 +5531,14 @@
55295531
function unsafe_get_uint16_le(b, i){
55305532
/*<<bytes.ml:470:2>>*/ return Stdlib_Sys[11]
55315533
? /*<<bytes.ml:471:40>>*/ caml_bswap16
5532-
( /*<<bytes.ml:471:14>>*/ caml_bytes_get16(b, i))
5533-
: /*<<bytes.ml:472:7>>*/ caml_bytes_get16(b, i) /*<<bytes.ml:472:31>>*/ ;
5534+
( /*<<bytes.ml:471:14>>*/ caml_bytes_get16u(b, i))
5535+
: /*<<bytes.ml:472:7>>*/ caml_bytes_get16u(b, i) /*<<bytes.ml:472:31>>*/ ;
55345536
}
55355537
function unsafe_get_uint16_be(b, i){
55365538
/*<<bytes.ml:475:2>>*/ return Stdlib_Sys[11]
5537-
? /*<<bytes.ml:476:7>>*/ caml_bytes_get16(b, i)
5539+
? /*<<bytes.ml:476:7>>*/ caml_bytes_get16u(b, i)
55385540
: /*<<bytes.ml:477:40>>*/ caml_bswap16
5539-
( /*<<bytes.ml:477:14>>*/ caml_bytes_get16(b, i)) /*<<bytes.ml:477:40>>*/ ;
5541+
( /*<<bytes.ml:477:14>>*/ caml_bytes_get16u(b, i)) /*<<bytes.ml:477:40>>*/ ;
55405542
}
55415543
function get_int8(b, i){
55425544
var
@@ -5600,17 +5602,17 @@
56005602
}
56015603
function unsafe_set_uint16_le(b, i, x){
56025604
/*<<bytes.ml:516:2>>*/ if(Stdlib_Sys[11]){
5603-
/*<<bytes.ml:517:7>>*/ caml_bytes_set16(b, i, caml_bswap16(x));
5605+
/*<<bytes.ml:517:7>>*/ caml_bytes_set16u(b, i, caml_bswap16(x));
56045606
/*<<bytes.ml:517:42>>*/ return;
56055607
}
5606-
/*<<bytes.ml:518:7>>*/ caml_bytes_set16(b, i, x);
5608+
/*<<bytes.ml:518:7>>*/ caml_bytes_set16u(b, i, x);
56075609
/*<<bytes.ml:518:33>>*/ }
56085610
function unsafe_set_uint16_be(b, i, x){
56095611
/*<<bytes.ml:521:2>>*/ if(Stdlib_Sys[11]){
5610-
/*<<bytes.ml:522:7>>*/ caml_bytes_set16(b, i, x);
5612+
/*<<bytes.ml:522:7>>*/ caml_bytes_set16u(b, i, x);
56115613
/*<<bytes.ml:522:33>>*/ return;
56125614
}
5613-
/*<<bytes.ml:523:2>>*/ caml_bytes_set16(b, i, caml_bswap16(x));
5615+
/*<<bytes.ml:523:2>>*/ caml_bytes_set16u(b, i, caml_bswap16(x));
56145616
/*<<bytes.ml:523:37>>*/ }
56155617
function set_int16_le(b, i, x){
56165618
/*<<bytes.ml:526:2>>*/ return Stdlib_Sys[11]
@@ -12354,9 +12356,6 @@
1235412356
caml_bswap16 = runtime.caml_bswap16,
1235512357
caml_bytes_get = runtime.caml_bytes_get,
1235612358
caml_bytes_set = runtime.caml_bytes_set,
12357-
caml_bytes_set16 = runtime.caml_bytes_set16,
12358-
caml_bytes_set32 = runtime.caml_bytes_set32,
12359-
caml_bytes_set64 = runtime.caml_bytes_set64,
1236012359
caml_bytes_unsafe_set = runtime.caml_bytes_unsafe_set,
1236112360
caml_create_bytes = runtime.caml_create_bytes,
1236212361
caml_int32_bswap = runtime.caml_int32_bswap,
@@ -12823,10 +12822,10 @@
1282312822
new_position = /*<<buffer.ml:351:2>>*/ position + 2 | 0;
1282412823
/*<<buffer.ml:352:2>>*/ if(length < new_position){
1282512824
/*<<buffer.ml:353:4>>*/ resize(b, 2);
12826-
/*<<buffer.ml:354:4>>*/ caml_bytes_set16(b[1][1], b[2], x);
12825+
/*<<buffer.ml:354:4>>*/ runtime.caml_bytes_set16(b[1][1], b[2], x);
1282712826
}
1282812827
else
12829-
/*<<buffer.ml:356:4>>*/ caml_bytes_set16(buffer, position, x);
12828+
/*<<buffer.ml:356:4>>*/ runtime.caml_bytes_set16u(buffer, position, x);
1283012829
/*<<buffer.ml:357:2>>*/ b[2] = new_position;
1283112830
return 0;
1283212831
/*<<buffer.ml:357:28>>*/ }
@@ -12839,10 +12838,10 @@
1283912838
new_position = /*<<buffer.ml:362:2>>*/ position + 4 | 0;
1284012839
/*<<buffer.ml:363:2>>*/ if(length < new_position){
1284112840
/*<<buffer.ml:364:4>>*/ resize(b, 4);
12842-
/*<<buffer.ml:365:4>>*/ caml_bytes_set32(b[1][1], b[2], x);
12841+
/*<<buffer.ml:365:4>>*/ runtime.caml_bytes_set32(b[1][1], b[2], x);
1284312842
}
1284412843
else
12845-
/*<<buffer.ml:367:4>>*/ caml_bytes_set32(buffer, position, x);
12844+
/*<<buffer.ml:367:4>>*/ runtime.caml_bytes_set32u(buffer, position, x);
1284612845
/*<<buffer.ml:368:2>>*/ b[2] = new_position;
1284712846
return 0;
1284812847
/*<<buffer.ml:368:28>>*/ }
@@ -12855,10 +12854,10 @@
1285512854
new_position = /*<<buffer.ml:373:2>>*/ position + 8 | 0;
1285612855
/*<<buffer.ml:374:2>>*/ if(length < new_position){
1285712856
/*<<buffer.ml:375:4>>*/ resize(b, 8);
12858-
/*<<buffer.ml:376:4>>*/ caml_bytes_set64(b[1][1], b[2], x);
12857+
/*<<buffer.ml:376:4>>*/ runtime.caml_bytes_set64(b[1][1], b[2], x);
1285912858
}
1286012859
else
12861-
/*<<buffer.ml:378:4>>*/ caml_bytes_set64(buffer, position, x);
12860+
/*<<buffer.ml:378:4>>*/ runtime.caml_bytes_set64u(buffer, position, x);
1286212861
/*<<buffer.ml:379:2>>*/ b[2] = new_position;
1286312862
return 0;
1286412863
/*<<buffer.ml:379:28>>*/ }

runtime/js/bigarray.js

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

401+
402+
//Provides: caml_ba_uint8_get16u
403+
function caml_ba_uint8_get16u(ba, i0) {
404+
var ofs = ba.offset(i0);
405+
var b1 = ba.get(ofs);
406+
var b2 = ba.get(ofs + 1);
407+
return b1 | (b2 << 8);
408+
}
401409
//Provides: caml_ba_uint8_get16
402410
//Requires: caml_array_bound_error
403411
function caml_ba_uint8_get16(ba, i0) {
@@ -408,6 +416,16 @@ function caml_ba_uint8_get16(ba, i0) {
408416
return b1 | (b2 << 8);
409417
}
410418

419+
//Provides: caml_ba_uint8_get32u
420+
function caml_ba_uint8_get32u(ba, i0) {
421+
var ofs = ba.offset(i0);
422+
var b1 = ba.get(ofs + 0);
423+
var b2 = ba.get(ofs + 1);
424+
var b3 = ba.get(ofs + 2);
425+
var b4 = ba.get(ofs + 3);
426+
return (b1 << 0) | (b2 << 8) | (b3 << 16) | (b4 << 24);
427+
}
428+
411429
//Provides: caml_ba_uint8_get32
412430
//Requires: caml_array_bound_error
413431
function caml_ba_uint8_get32(ba, i0) {
@@ -420,6 +438,21 @@ function caml_ba_uint8_get32(ba, i0) {
420438
return (b1 << 0) | (b2 << 8) | (b3 << 16) | (b4 << 24);
421439
}
422440

441+
//Provides: caml_ba_uint8_get64u
442+
//Requires: caml_int64_of_bytes
443+
function caml_ba_uint8_get64u(ba, i0) {
444+
var ofs = ba.offset(i0);
445+
var b1 = ba.get(ofs + 0);
446+
var b2 = ba.get(ofs + 1);
447+
var b3 = ba.get(ofs + 2);
448+
var b4 = ba.get(ofs + 3);
449+
var b5 = ba.get(ofs + 4);
450+
var b6 = ba.get(ofs + 5);
451+
var b7 = ba.get(ofs + 6);
452+
var b8 = ba.get(ofs + 7);
453+
return caml_int64_of_bytes([b8, b7, b6, b5, b4, b3, b2, b1]);
454+
}
455+
423456
//Provides: caml_ba_uint8_get64
424457
//Requires: caml_array_bound_error, caml_int64_of_bytes
425458
function caml_ba_uint8_get64(ba, i0) {
@@ -458,6 +491,14 @@ function caml_ba_set_generic(ba, i, v) {
458491
return 0;
459492
}
460493

494+
//Provides: caml_ba_uint8_set16u
495+
function caml_ba_uint8_set16u(ba, i0, v) {
496+
var ofs = ba.offset(i0);
497+
ba.set(ofs + 0, v & 0xff);
498+
ba.set(ofs + 1, (v >>> 8) & 0xff);
499+
return 0;
500+
}
501+
461502
//Provides: caml_ba_uint8_set16
462503
//Requires: caml_array_bound_error
463504
function caml_ba_uint8_set16(ba, i0, v) {
@@ -468,6 +509,15 @@ function caml_ba_uint8_set16(ba, i0, v) {
468509
return 0;
469510
}
470511

512+
//Provides: caml_ba_uint8_set32u
513+
function caml_ba_uint8_set32u(ba, i0, v) {
514+
var ofs = ba.offset(i0);
515+
ba.set(ofs + 0, v & 0xff);
516+
ba.set(ofs + 1, (v >>> 8) & 0xff);
517+
ba.set(ofs + 2, (v >>> 16) & 0xff);
518+
ba.set(ofs + 3, (v >>> 24) & 0xff);
519+
return 0;
520+
}
471521
//Provides: caml_ba_uint8_set32
472522
//Requires: caml_array_bound_error
473523
function caml_ba_uint8_set32(ba, i0, v) {
@@ -480,6 +530,15 @@ function caml_ba_uint8_set32(ba, i0, v) {
480530
return 0;
481531
}
482532

533+
//Provides: caml_ba_uint8_set64u
534+
//Requires: caml_int64_to_bytes
535+
function caml_ba_uint8_set64u(ba, i0, v) {
536+
var ofs = ba.offset(i0);
537+
var v = caml_int64_to_bytes(v);
538+
for (var i = 0; i < 8; i++) ba.set(ofs + i, v[7 - i]);
539+
return 0;
540+
}
541+
483542
//Provides: caml_ba_uint8_set64
484543
//Requires: caml_array_bound_error, caml_int64_to_bytes
485544
function caml_ba_uint8_set64(ba, i0, v) {

0 commit comments

Comments
 (0)