123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2010 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
* Copyright (C) 2013 Hugo Heuzard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)open!StdlibopenCodeopenFlowletspecialize_instr~targetinfoi=matchi,targetwith|Let(x,Prim(Extern"caml_format_int",[y;z])),`JavaScript->((* We can implement the special case where the format string is "%s" in JavaScript
in a concise and efficient way with [""+x]. It does not make as much sense in
Wasm to have a special case for this. *)matchthe_string_of~targetinfoywith|Some"%d"->(matchthe_int~targetinfozwith|Somei->Let(x,Constant(String(Targetint.to_stringi)))|None->Let(x,Prim(Extern"%caml_format_int_special",[z])))|_->i)|Let(x,Prim(Extern"%caml_format_int_special",[z])),`JavaScript->(matchthe_int~targetinfozwith|Somei->Let(x,Constant(String(Targetint.to_stringi)))|None->i)(* inline the String constant argument so that generate.ml can attempt to parse it *)|(Let(x,Prim(Extern(("caml_js_var"|"caml_js_expr"|"caml_pure_js_expr")asprim),[(Pv_asy)])),target)->(matchthe_string_of~targetinfoywith|Somes->Let(x,Prim(Externprim,[Pc(Strings)]))|_->i)|Let(x,Prim(Extern("caml_register_named_value"asprim),[y;z])),_->(matchthe_string_of~targetinfoywith|SomeswhenPrimitive.need_named_values->Let(x,Prim(Externprim,[Pc(Strings);z]))|Some_->Let(x,Constant(IntTargetint.zero))|None->i)|Let(x,Prim(Extern"caml_js_call",[f;o;a])),_->(matchthe_def_ofinfoawith|Some(Block(_,a,_,_))->leta=Array.mapa~f:(funx->Pvx)inLet(x,Prim(Extern"%caml_js_opt_call",f::o::Array.to_lista))|_->i)|Let(x,Prim(Extern"caml_js_fun_call",[f;a])),_->(matchthe_def_ofinfoawith|Some(Block(_,a,_,_))->leta=Array.mapa~f:(funx->Pvx)inLet(x,Prim(Extern"%caml_js_opt_fun_call",f::Array.to_lista))|_->i)|Let(x,Prim(Extern"caml_js_meth_call",[o;m;a])),_->(matchthe_string_of~targetinfomwith|SomemwhenJavascript.is_identm->(matchthe_def_ofinfoawith|Some(Block(_,a,_,_))->leta=Array.mapa~f:(funx->Pvx)inLet(x,Prim(Extern"%caml_js_opt_meth_call",o::Pc(NativeString(Native_string.of_stringm))::Array.to_lista))|_->i)|_->i)|Let(x,Prim(Extern"caml_js_new",[c;a])),_->(matchthe_def_ofinfoawith|Some(Block(_,a,_,_))->leta=Array.mapa~f:(funx->Pvx)inLet(x,Prim(Extern"%caml_js_opt_new",c::Array.to_lista))|_->i)|Let(x,Prim(Extern"caml_js_object",[a])),_->(tryleta=matchthe_def_ofinfoawith|Some(Block(_,a,_,_))->a|_->raiseExitinleta=Array.mapa~f:(funx->matchthe_def_ofinfo(Pvx)with|Some(Block(_,[|k;v|],_,_))->letk=matchthe_string_of~targetinfo(Pvk)with|SomeswhenString.is_valid_utf_8s->Pc(NativeString(Native_string.of_strings))|Some_|None->raiseExitin[k;Pvv]|Some(Constant(Tuple(0,[|Stringk;v|],(NotArray|Unknown))))whenString.is_valid_utf_8k->[Pc(NativeString(Native_string.of_stringk));Pcv]|Some_|None->raiseExit)inLet(x,Prim(Extern"%caml_js_opt_object",List.flatten(Array.to_lista)))withExit->i)|Let(x,Prim(Extern"caml_js_get",[o;(Pv_asf)])),_->(matchthe_native_string_of~targetinfofwith|Somes->Let(x,Prim(Extern"caml_js_get",[o;Pc(NativeStrings)]))|_->i)|Let(x,Prim(Extern"caml_js_set",[o;(Pv_asf);v])),_->(matchthe_native_string_of~targetinfofwith|Somes->Let(x,Prim(Extern"caml_js_set",[o;Pc(NativeStrings);v]))|_->i)|Let(x,Prim(Extern"caml_js_delete",[o;(Pv_asf)])),_->(matchthe_native_string_of~targetinfofwith|Somes->Let(x,Prim(Extern"caml_js_delete",[o;Pc(NativeStrings)]))|_->i)|Let(x,Prim(Extern("caml_jsstring_of_string"|"caml_js_from_string"),[y])),_->(matchthe_string_of~targetinfoywith|SomeswhenString.is_valid_utf_8s->Let(x,Constant(NativeString(Native_string.of_strings)))|Some_|None->i)|Let(x,Prim(Extern"caml_jsbytes_of_string",[y])),_->(matchthe_string_of~targetinfoywith|Somes->Let(x,Constant(NativeString(Native_string.of_bytestrings)))|None->i)|Let(x,Prim(Extern"%int_mul",[y;z])),`JavaScript->(letlimit=Targetint.of_int_exn0x200000in(* Using * to multiply integers in JavaScript yields a float; and if the
float is large enough, some bits can be lost. So, in the general case,
we have to use Math.imul. There is no such issue in Wasm. *)matchthe_int~targetinfoy,the_int~targetinfozwith|Somej,_whenTargetint.(absj<limit)->Let(x,Prim(Extern"%direct_int_mul",[y;z]))|_,SomejwhenTargetint.(absj<limit)->Let(x,Prim(Extern"%direct_int_mul",[y;z]))|_->i)|Let(x,Prim(Extern"%int_div",[y;z])),_->(matchthe_int~targetinfozwith|Somejwhennot(Targetint.is_zeroj)->Let(x,Prim(Extern"%direct_int_div",[y;z]))|_->i)|Let(x,Prim(Extern"%int_mod",[y;z])),_->(matchthe_int~targetinfozwith|Somejwhennot(Targetint.is_zeroj)->Let(x,Prim(Extern"%direct_int_mod",[y;z]))|_->i)|_,_->iletskip_eventcont(Event_::l|l)=contlletrecognize_string_lengthcont=skip_event@@funl->matchlwith|(Let(len,Prim(Extern"caml_ml_string_length",[Pvstr]))asi)::l->conti~len~strl|_->Noneletrecognize_int_add~x~ycont=skip_event@@funl->matchlwith|(Let(res,Prim(Extern"%int_add",[Pvx';Pvy']))asi)::lwhenCode.Var.equalxx'&&Code.Var.equalyy'->conti~resl|_->Noneletrecognize_create_bytes~lencont=skip_event@@funl->matchlwith|Let(bytes,Prim(Extern"caml_create_bytes",[Pvlen']))::lwhenCode.Var.equallenlen'->cont~bytesl|_->Noneletrecognize_blit_string~str~bytes~ofs~lencont=skip_event@@funl->matchlwith|Let(_,Prim(Extern"caml_blit_string",[Pvstr';Pc(Intzero);Pvbytes';ofs';Pvlen']))::lwhenCode.Var.equalstrstr'&&Targetint.is_zerozero&&Code.Var.equalbytesbytes'&&Code.Var.equallenlen'&&matchofs,ofs'with|Pc(Intofs),Pc(Intofs')->Targetint.equalofsofs'|Pvofs,Pvofs'->Code.Var.equalofsofs'|_->false->contl|_->Noneletrecognize_string_of_bytes~bytescont=skip_event@@funl->matchlwith|Let(str,Prim(Extern"caml_string_of_bytes",[Pvbytes']))::lwhenCode.Var.equalbytesbytes'->cont~strl|_->Noneletrecognize_empty_bodycont=skip_event@@funl->ifList.is_emptylthencont()elseNoneletspecialize_string_concatl=Option.value~default:l(l|>recognize_string_length@@funlen1~len:alen~str:a->recognize_string_length@@funlen2~len:blen~str:b->recognize_int_add~x:alen~y:blen@@funlen3~res:len->recognize_create_bytes~len@@fun~bytes->recognize_blit_string~str:a~bytes~ofs:(Pc(IntTargetint.zero))~len:alen@@recognize_blit_string~str:b~bytes~ofs:(Pvalen)~len:blen@@recognize_string_of_bytes~bytes@@fun~str->recognize_empty_body@@fun()->Some[len1;len2;len3;Let(str,Prim(Extern"caml_string_concat",[Pva;Pvb]));Let(bytes,Prim(Extern"caml_bytes_of_string",[Pvstr]))])letspecialize_instrs~targetinfol=letrecauxinfocheckslacc=matchlwith|[]->List.revacc|i::r->((* We make bound checking explicit. Then, we can remove duplicated
bound checks. Also, it appears to be more efficient to inline
the array access. The bound checking function returns the array,
which allows to produce more compact code. *)matchiwith|Let(x,Prim(Extern(("caml_array_get"|"caml_array_get_float"|"caml_floatarray_get"|"caml_array_get_addr")asprim),[y;z]))->letidx=matchthe_int~targetinfozwith|Someidx->`Cstidx|None->`Varzinletinstry=letprim=matchprimwith|"caml_array_get"->Extern"caml_array_unsafe_get"|"caml_array_get_float"|"caml_floatarray_get"->Extern"caml_floatarray_unsafe_get"|"caml_array_get_addr"->Array_get|_->assertfalseinLet(x,Prim(prim,[y;z]))inifList.mem(y,idx)~set:checksthenletacc=instry::accinauxinfochecksraccelseletcheck=matchprimwith|"caml_array_get"->"caml_check_bound_gen"|"caml_array_get_float"|"caml_floatarray_get"->"caml_check_bound_float"|"caml_array_get_addr"->"caml_check_bound"|_->assertfalseinlety'=Code.Var.fresh()inletacc=instr(Pvy')::Let(y',Prim(Externcheck,[y;z]))::accinauxinfo((y,idx)::checks)racc|Let(x,Prim(Extern(("caml_array_set"|"caml_array_set_float"|"caml_floatarray_set"|"caml_array_set_addr")asprim),[y;z;t]))->letidx=matchthe_int~targetinfozwith|Someidx->`Cstidx|None->`Varzinletinstry=letprim=matchprimwith|"caml_array_set"->"caml_array_unsafe_set"|"caml_array_set_float"|"caml_floatarray_set"->"caml_floatarray_unsafe_set"|"caml_array_set_addr"->"caml_array_unsafe_set_addr"|_->assertfalseinLet(x,Prim(Externprim,[y;z;t]))inifList.mem(y,idx)~set:checksthenletacc=instry::accinauxinfochecksraccelseletcheck=matchprimwith|"caml_array_set"->"caml_check_bound_gen"|"caml_array_set_float"|"caml_floatarray_set"->"caml_check_bound_float"|"caml_array_set_addr"->"caml_check_bound"|_->assertfalseinlety'=Code.Var.fresh()inletacc=instr(Pvy')::Let(y',Prim(Externcheck,[y;z]))::accinauxinfo((y,idx)::checks)racc|_->leti=specialize_instr~targetinfoiinauxinfochecksr(i::acc))inauxinfo[]l[]letspecialize_all_instrs~targetinfop=letblocks=Addr.Map.map(funblock->{blockwithCode.body=specialize_instrs~targetinfo(specialize_string_concatblock.body)})p.blocksin{pwithblocks}(****)letfinfop=specialize_all_instrs~target:(Config.target())infopletf_oncep=letrecloopaccl=matchlwith|[]->List.revacc|i::r->(matchiwith|Let(x,(Prim(Extern("caml_array_set"|"caml_array_unsafe_set"|"caml_array_set_float"|"caml_floatarray_set"|"caml_array_set_addr"|"caml_array_unsafe_set_float"|"caml_floatarray_unsafe_set"),[_;_;_])asp))->letx'=Code.Var.forkxinletacc=Let(x',p)::Let(x,Constant(IntTargetint.zero))::accinloopaccr|_->loop(i::acc)r)inletblocks=Addr.Map.map(funblock->{blockwithCode.body=loop[]block.body})p.blocksin{pwithblocks}