From: "Grant Olson" <olsongt@verizon.net>
To: <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] High-performance bytecode interpreter in OCaml
Date: Wed, 15 Aug 2007 19:31:51 -0400 [thread overview]
Message-ID: <007c01c7df94$756a40c0$ac01a8c0@johnyaya> (raw)
In-Reply-To: <EC4C88C0-681B-4691-B7A0-BDF52B78895D@gmail.com>
>
> 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
next prev parent reply other threads:[~2007-08-16 8:33 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-15 11:49 Joel Reymont
2007-08-15 13:01 ` [Caml-list] " Jon Harrop
2007-08-15 13:20 ` Joel Reymont
2007-08-15 15:18 ` Jon Harrop
2007-08-15 23:26 ` Joel Reymont
2007-08-16 3:58 ` Jon Harrop
2007-08-15 23:31 ` Grant Olson [this message]
2007-08-16 15:55 ` Jon Harrop
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='007c01c7df94$756a40c0$ac01a8c0@johnyaya' \
--to=olsongt@verizon.net \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).