From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: ** X-Spam-Status: No, score=2.6 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 71CBDBC69 for ; Thu, 16 Aug 2007 10:33:23 +0200 (CEST) Received: from vms040pub.verizon.net (vms040pub.verizon.net [206.46.252.40]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l7G8XMgq029905 for ; Thu, 16 Aug 2007 10:33:23 +0200 Received: from johnyaya ([151.201.249.207]) by vms040.mailsrvcs.net (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) with ESMTPA id <0JMU007GK9D1IMZO@vms040.mailsrvcs.net> for caml-list@yquem.inria.fr; Wed, 15 Aug 2007 18:31:50 -0500 (CDT) Date: Wed, 15 Aug 2007 19:31:51 -0400 From: "Grant Olson" Subject: RE: [Caml-list] High-performance bytecode interpreter in OCaml In-reply-to: To: Message-id: <007c01c7df94$756a40c0$ac01a8c0@johnyaya> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.3138 X-Mailer: Microsoft Office Outlook 11 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit Thread-index: AcffP93FqZzex1BSRzu3liXfVzV6+wAU+BRA X-Miltered: at concorde with ID 46C40BD2.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; bytecode:01 ocaml:01 bytecode:01 dispatcher:01 opcode:01 recursive:01 trivial:01 opcode:01 printf:01 printf:01 verizon:98 wrote:01 rec:01 caml-list:01 tail:01 > > As for CPS, what I meant is implementing each bytecode > instruction as a function that takes a continuation (next > instruction?). > > Thanks, Joel > Once again I made the mistake of following up on google groups! Anyway, nothing good was on tv so I wrote up a better example. I think you'd make things too complicated with CPS. Why not just create a dispatcher function and opcode instructions that are all tail recursive? Without knowing the internals, I don't see how it could be that much slower than CPS and is much easier for us humans to read. Anyway, here's a trivial vm: (* Stupid vm. It is initailized with an instruction counter of zero and has one register that is initially zero. There are four instructions: 'i' - increment the register 'd' - decrement the register 'p' - print the value of the register 'r' - reset the instruction counter to zero *) type vm = {current_inst:int;register:int;bytecode:string} let create_vm b = {current_inst=0;register=0;bytecode=b} let inc_inst v = {v with current_inst=v.current_inst+1} let update_reg v i = {v with register=v.register+i} let update_reg_and_inc v i = let new_v = update_reg v i in inc_inst new_v let rec dispatch v = let opcode=v.bytecode.[v.current_inst] in begin match opcode with 'i' -> inc v | 'd' -> dec v | 'p' -> print v | 'r' -> reset v end and inc v = let new_v = update_reg_and_inc v 1 in dispatch new_v and dec v = let new_v = update_reg_and_inc v 1 in dispatch new_v and print v = Printf.printf "%i\n" v.register; dispatch (inc_inst v) and reset v = dispatch {v with current_inst=0} let vm_instance = create_vm "piiipdpdddddddpiiiprpididi" let _ = dispatch vm_instance