123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2010 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* 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!StdlibopenCodeopenFlowletrecfunction_cardinalityinfoxacc=get_approxinfo(funx->matchinfo.info_defs.(Var.idxx)with|Expr(Closure(l,_))->Some(List.lengthl)|Expr(Prim(Extern"%closure",[Pc(NativeStringprim)]))->(trySome(Primitive.arityprim)withNot_found->None)|Expr(Apply{f;args;_})->(ifList.memf~set:accthenNoneelsematchfunction_cardinalityinfof(f::acc)with|Somen->letdiff=n-List.lengthargsinifdiff>0thenSomediffelseNone|None->None)|_->None)None(funuv->matchu,vwith|Somen,Somemwhenn=m->u|_->None)xletspecialize_instrinfo(acc,free_pc,extra)i=matchiwith|Let(x,Apply{f;args;_})whenConfig.Flag.optcall()->(letn'=List.lengthargsinmatchfunction_cardinalityinfof[]with|None->i::acc,free_pc,extra|Somenwhenn=n'->Let(x,Apply{f;args;exact=true})::acc,free_pc,extra|Somenwhenn<n'->letv=Code.Var.fresh()inletargs,rest=List.takenargsin(Let(v,Apply{f;args;exact=true})::Let(x,Apply{f=v;args=rest;exact=false})::acc,free_pc,extra)|Somenwhenn>n'->letmissing=Array.init(n-n')~f:(fun_->Code.Var.fresh())inletmissing=Array.to_listmissinginletblock=letparams'=Array.init(n-n')~f:(fun_->Code.Var.fresh())inletparams'=Array.to_listparams'inletreturn'=Code.Var.fresh()in{params=params';body=[Let(return',Apply{f;args=args@params';exact=true})];branch=Returnreturn'}in(Let(x,Closure(missing,(free_pc,missing)))::acc,free_pc+1,(free_pc,block)::extra)|_->i::acc,free_pc,extra)|_->i::acc,free_pc,extraletspecialize_instrsinfop=letblocks,free_pc=Addr.Map.fold(funpcblock(blocks,free_pc)->letbody,free_pc,extra=List.fold_rightblock.body~init:([],free_pc,[])~f:(funiacc->specialize_instrinfoacci)inletblocks=List.fold_leftextra~init:blocks~f:(funblocks(pc,b)->Addr.Map.addpcbblocks)inAddr.Map.addpc{blockwithCode.body}blocks,free_pc)p.blocks(Addr.Map.empty,p.free_pc)in{pwithblocks;free_pc}letfinfop=specialize_instrsinfop