Skip to content

Commit b7eb470

Browse files
committed
Compiler: consume hints for unsafe string/bytes/ba get/set
1 parent bc4e217 commit b7eb470

File tree

7 files changed

+398
-31
lines changed

7 files changed

+398
-31
lines changed

compiler/lib-wasm/generate.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,26 @@ module Generate (Target : Target_sig.S) = struct
110110
; "caml_nativeint_compare", (`Pure, [ Nativeint; Nativeint ], Value)
111111
; "caml_int64_compare", (`Pure, [ Int64; Int64 ], Value)
112112
; "caml_string_get32", (`Mutator, [ Value; Value ], Int32)
113+
; "caml_string_get32u", (`Mutator, [ Value; Value ], Int32)
113114
; "caml_string_get64", (`Mutator, [ Value; Value ], Int64)
115+
; "caml_string_get64u", (`Mutator, [ Value; Value ], Int64)
114116
; "caml_bytes_get32", (`Mutator, [ Value; Value ], Int32)
117+
; "caml_bytes_get32u", (`Mutator, [ Value; Value ], Int32)
115118
; "caml_bytes_get64", (`Mutator, [ Value; Value ], Int64)
119+
; "caml_bytes_get64u", (`Mutator, [ Value; Value ], Int64)
116120
; "caml_bytes_set32", (`Mutator, [ Value; Value; Int32 ], Value)
121+
; "caml_bytes_set32u", (`Mutator, [ Value; Value; Int32 ], Value)
117122
; "caml_bytes_set64", (`Mutator, [ Value; Value; Int64 ], Value)
123+
; "caml_bytes_set64u", (`Mutator, [ Value; Value; Int64 ], Value)
118124
; "caml_lxm_next", (`Pure, [ Value ], Int64)
119125
; "caml_ba_uint8_get32", (`Mutator, [ Value; Value ], Int32)
126+
; "caml_ba_uint8_get32u", (`Mutator, [ Value; Value ], Int32)
120127
; "caml_ba_uint8_get64", (`Mutator, [ Value; Value ], Int64)
128+
; "caml_ba_uint8_get64u", (`Mutator, [ Value; Value ], Int64)
121129
; "caml_ba_uint8_set32", (`Mutator, [ Value; Value; Int32 ], Value)
130+
; "caml_ba_uint8_set32u", (`Mutator, [ Value; Value; Int32 ], Value)
122131
; "caml_ba_uint8_set64", (`Mutator, [ Value; Value; Int64 ], Value)
132+
; "caml_ba_uint8_set64u", (`Mutator, [ Value; Value; Int64 ], Value)
123133
; "caml_nextafter_float", (`Pure, [ Float; Float ], Float)
124134
; "caml_classify_float", (`Pure, [ Float ], Value)
125135
; "caml_ldexp_float", (`Pure, [ Float; Value ], Float)

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

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4826,11 +4826,13 @@
48264826
caml_bswap16 = runtime.caml_bswap16,
48274827
caml_bytes_get = runtime.caml_bytes_get,
48284828
caml_bytes_get16 = runtime.caml_bytes_get16,
4829+
caml_bytes_get16u = runtime.caml_bytes_get16u,
48294830
caml_bytes_get32 = runtime.caml_bytes_get32,
48304831
caml_bytes_get64 = runtime.caml_bytes_get64,
48314832
caml_bytes_of_string = runtime.caml_bytes_of_string,
48324833
caml_bytes_set = runtime.caml_bytes_set,
48334834
caml_bytes_set16 = runtime.caml_bytes_set16,
4835+
caml_bytes_set16u = runtime.caml_bytes_set16u,
48344836
caml_bytes_set32 = runtime.caml_bytes_set32,
48354837
caml_bytes_set64 = runtime.caml_bytes_set64,
48364838
caml_bytes_unsafe_get = runtime.caml_bytes_unsafe_get,
@@ -5616,14 +5618,14 @@
56165618
function unsafe_get_uint16_le(b, i){
56175619
/*<<bytes.ml:470:2>>*/ return Stdlib_Sys[11]
56185620
? /*<<bytes.ml:471:40>>*/ caml_bswap16
5619-
( /*<<bytes.ml:471:14>>*/ caml_bytes_get16(b, i))
5620-
: /*<<bytes.ml:472:7>>*/ caml_bytes_get16(b, i) /*<<bytes.ml:472:31>>*/ ;
5621+
( /*<<bytes.ml:471:14>>*/ caml_bytes_get16u(b, i))
5622+
: /*<<bytes.ml:472:7>>*/ caml_bytes_get16u(b, i) /*<<bytes.ml:472:31>>*/ ;
56215623
}
56225624
function unsafe_get_uint16_be(b, i){
56235625
/*<<bytes.ml:475:2>>*/ return Stdlib_Sys[11]
5624-
? /*<<bytes.ml:476:7>>*/ caml_bytes_get16(b, i)
5626+
? /*<<bytes.ml:476:7>>*/ caml_bytes_get16u(b, i)
56255627
: /*<<bytes.ml:477:40>>*/ caml_bswap16
5626-
( /*<<bytes.ml:477:14>>*/ caml_bytes_get16(b, i)) /*<<bytes.ml:477:40>>*/ ;
5628+
( /*<<bytes.ml:477:14>>*/ caml_bytes_get16u(b, i)) /*<<bytes.ml:477:40>>*/ ;
56275629
}
56285630
function get_int8(b, i){
56295631
var
@@ -5687,17 +5689,17 @@
56875689
}
56885690
function unsafe_set_uint16_le(b, i, x){
56895691
/*<<bytes.ml:516:2>>*/ if(Stdlib_Sys[11]){
5690-
/*<<bytes.ml:517:7>>*/ caml_bytes_set16(b, i, caml_bswap16(x));
5692+
/*<<bytes.ml:517:7>>*/ caml_bytes_set16u(b, i, caml_bswap16(x));
56915693
/*<<bytes.ml:517:42>>*/ return;
56925694
}
5693-
/*<<bytes.ml:518:7>>*/ caml_bytes_set16(b, i, x);
5695+
/*<<bytes.ml:518:7>>*/ caml_bytes_set16u(b, i, x);
56945696
/*<<bytes.ml:518:33>>*/ }
56955697
function unsafe_set_uint16_be(b, i, x){
56965698
/*<<bytes.ml:521:2>>*/ if(Stdlib_Sys[11]){
5697-
/*<<bytes.ml:522:7>>*/ caml_bytes_set16(b, i, x);
5699+
/*<<bytes.ml:522:7>>*/ caml_bytes_set16u(b, i, x);
56985700
/*<<bytes.ml:522:33>>*/ return;
56995701
}
5700-
/*<<bytes.ml:523:2>>*/ caml_bytes_set16(b, i, caml_bswap16(x));
5702+
/*<<bytes.ml:523:2>>*/ caml_bytes_set16u(b, i, caml_bswap16(x));
57015703
/*<<bytes.ml:523:37>>*/ }
57025704
function set_int16_le(b, i, x){
57035705
/*<<bytes.ml:526:2>>*/ return Stdlib_Sys[11]
@@ -12483,9 +12485,6 @@
1248312485
caml_bswap16 = runtime.caml_bswap16,
1248412486
caml_bytes_get = runtime.caml_bytes_get,
1248512487
caml_bytes_set = runtime.caml_bytes_set,
12486-
caml_bytes_set16 = runtime.caml_bytes_set16,
12487-
caml_bytes_set32 = runtime.caml_bytes_set32,
12488-
caml_bytes_set64 = runtime.caml_bytes_set64,
1248912488
caml_bytes_unsafe_set = runtime.caml_bytes_unsafe_set,
1249012489
caml_create_bytes = runtime.caml_create_bytes,
1249112490
caml_int32_bswap = runtime.caml_int32_bswap,
@@ -12967,10 +12966,10 @@
1296712966
new_position = /*<<buffer.ml:353:2>>*/ position + 2 | 0;
1296812967
/*<<buffer.ml:354:2>>*/ if(length < new_position){
1296912968
/*<<buffer.ml:355:4>>*/ resize(b, 2);
12970-
/*<<buffer.ml:356:4>>*/ caml_bytes_set16(b[1][1], b[2], x);
12969+
/*<<buffer.ml:356:4>>*/ runtime.caml_bytes_set16(b[1][1], b[2], x);
1297112970
}
1297212971
else
12973-
/*<<buffer.ml:358:4>>*/ caml_bytes_set16(buffer, position, x);
12972+
/*<<buffer.ml:358:4>>*/ runtime.caml_bytes_set16u(buffer, position, x);
1297412973
/*<<buffer.ml:359:2>>*/ b[2] = new_position;
1297512974
return 0;
1297612975
/*<<buffer.ml:359:28>>*/ }
@@ -12983,10 +12982,10 @@
1298312982
new_position = /*<<buffer.ml:364:2>>*/ position + 4 | 0;
1298412983
/*<<buffer.ml:365:2>>*/ if(length < new_position){
1298512984
/*<<buffer.ml:366:4>>*/ resize(b, 4);
12986-
/*<<buffer.ml:367:4>>*/ caml_bytes_set32(b[1][1], b[2], x);
12985+
/*<<buffer.ml:367:4>>*/ runtime.caml_bytes_set32(b[1][1], b[2], x);
1298712986
}
1298812987
else
12989-
/*<<buffer.ml:369:4>>*/ caml_bytes_set32(buffer, position, x);
12988+
/*<<buffer.ml:369:4>>*/ runtime.caml_bytes_set32u(buffer, position, x);
1299012989
/*<<buffer.ml:370:2>>*/ b[2] = new_position;
1299112990
return 0;
1299212991
/*<<buffer.ml:370:28>>*/ }
@@ -12999,10 +12998,10 @@
1299912998
new_position = /*<<buffer.ml:375:2>>*/ position + 8 | 0;
1300012999
/*<<buffer.ml:376:2>>*/ if(length < new_position){
1300113000
/*<<buffer.ml:377:4>>*/ resize(b, 8);
13002-
/*<<buffer.ml:378:4>>*/ caml_bytes_set64(b[1][1], b[2], x);
13001+
/*<<buffer.ml:378:4>>*/ runtime.caml_bytes_set64(b[1][1], b[2], x);
1300313002
}
1300413003
else
13005-
/*<<buffer.ml:380:4>>*/ caml_bytes_set64(buffer, position, x);
13004+
/*<<buffer.ml:380:4>>*/ runtime.caml_bytes_set64u(buffer, position, x);
1300613005
/*<<buffer.ml:381:2>>*/ b[2] = new_position;
1300713006
return 0;
1300813007
/*<<buffer.ml:381:28>>*/ }

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) {

0 commit comments

Comments
 (0)