123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234(* 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_instrinfoi=matchiwith|Let(x,Prim(Extern"caml_format_int",[y;z]))->(matchthe_string_ofinfoywith|Some"%d"->(matchthe_intinfozwith|Somei->Let(x,Constant(String(Int32.to_stringi)))|None->Let(x,Prim(Extern"%caml_format_int_special",[z])))|_->i)|Let(x,Prim(Extern"%caml_format_int_special",[z]))->(matchthe_intinfozwith|Somei->Let(x,Constant(String(Int32.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)]))whenConfig.Flag.safe_string()->(matchthe_string_ofinfoywith|Somes->Let(x,Prim(Externprim,[Pc(Strings)]))|_->i)|Let(x,Prim(Extern("caml_register_named_value"asprim),[y;z]))->(matchthe_string_ofinfoywith|SomeswhenPrimitive.need_named_values->Let(x,Prim(Externprim,[Pc(Strings);z]))|Some_->Let(x,Constant(Int0l))|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_ofinfomwith|SomemwhenString.is_asciim->(matchthe_def_ofinfoawith|Some(Block(_,a,_))->leta=Array.mapa~f:(funx->Pvx)inLet(x,Prim(Extern"%caml_js_opt_meth_call",o::Pc(NativeStringm)::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_ofinfo(Pvk)with|SomeswhenString.is_asciis->Pc(NativeStrings)|_->raiseExitin[k;Pvv]|_->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_ofinfofwith|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_ofinfofwith|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_ofinfofwith|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_ofinfoywith|SomeswhenString.is_asciis->Let(x,Constant(NativeStrings))|_->i)|Let(x,Prim(Extern"%int_mul",[y;z]))->(matchthe_intinfoy,the_intinfozwith|Somej,_whenInt32.(absj<0x200000l)->Let(x,Prim(Extern"%direct_int_mul",[y;z]))|_,SomejwhenInt32.(absj<0x200000l)->Let(x,Prim(Extern"%direct_int_mul",[y;z]))|_->i)|Let(x,Prim(Extern"%int_div",[y;z]))->(matchthe_intinfozwith|SomejwhenInt32.(j<>0l)->Let(x,Prim(Extern"%direct_int_div",[y;z]))|_->i)|Let(x,Prim(Extern"%int_mod",[y;z]))->(matchthe_intinfozwith|SomejwhenInt32.(j<>0l)->Let(x,Prim(Extern"%direct_int_mod",[y;z]))|_->i)|_->iletspecialize_instrsinfol=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",[y;z]))|Let(x,Prim(Extern"caml_array_get_float",[y;z]))|Let(x,Prim(Extern"caml_array_get_addr",[y;z]))->letidx=matchthe_intinfozwith|Someidx->`Cstidx|None->`VarzinifList.mem(y,idx)~set:checksthenletacc=Let(x,Prim(Extern"caml_array_unsafe_get",[y;z]))::accinauxinfochecksraccelselety'=Code.Var.fresh()inletacc=Let(x,Prim(Extern"caml_array_unsafe_get",[Pvy';z]))::Let(y',Prim(Extern"caml_check_bound",[y;z]))::accinauxinfo((y,idx)::checks)racc|Let(x,Prim(Extern"caml_array_set",[y;z;t]))|Let(x,Prim(Extern"caml_array_set_float",[y;z;t]))|Let(x,Prim(Extern"caml_array_set_addr",[y;z;t]))->letidx=matchthe_intinfozwith|Someidx->`Cstidx|None->`VarzinifList.mem(y,idx)~set:checksthenletacc=Let(x,Prim(Extern"caml_array_unsafe_set",[y;z;t]))::accinauxinfochecksraccelselety'=Code.Var.fresh()inletacc=Let(x,Prim(Extern"caml_array_unsafe_set",[Pvy';z;t]))::Let(y',Prim(Extern"caml_check_bound",[y;z]))::accinauxinfo((y,idx)::checks)racc|_->leti=specialize_instrinfoiinauxinfochecksr(i::acc))inauxinfo[]l[]letspecialize_all_instrsinfop=letblocks=Addr.Map.map(funblock->{blockwithCode.body=specialize_instrsinfoblock.body})p.blocksin{pwithblocks}(****)letfinfop=specialize_all_instrsinfopletf_oncep=letrecloopaccl=matchlwith|[]->List.revacc|i::r->(matchiwith|Let(x,(Prim(Extern("caml_array_set"|"caml_array_unsafe_set"|"caml_array_set_float"|"caml_array_set_addr"|"caml_array_unsafe_set_float"|"caml_floatarray_unsafe_set"),[_;_;_])asp))->letx'=Code.Var.forkxinletacc=Let(x',p)::Let(x,Constant(Int0l))::accinloopaccr|_->loop(i::acc)r)inletblocks=Addr.Map.map(funblock->{blockwithCode.body=loop[]block.body})p.blocksin{pwithblocks}