Skip to content

Commit

Permalink
implement run_ops - It's alive!!!
Browse files Browse the repository at this point in the history
The M0 interp now has the ability to either run noop or say_i from M0
bytecode.  It's nowhere near complete, but being able to run ops is shiny.
  • Loading branch information
cotto committed May 5, 2011
1 parent 9c552f6 commit dcb5eb7
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/m0/m0_interp.pl
Expand Up @@ -308,7 +308,7 @@ sub run_m0b {
my $interp = new_interp();
load_m0b($interp, $filename);
my $ctx = new_context($interp);
#run_ops($ctx);
run_ops($ctx);
}


Expand All @@ -322,7 +322,7 @@ sub run_m0b {
sub new_interp {
my $interp;

$interp->{opfuncs} = [
$interp->{op_funcs} = [
\&m0_opfunc_noop,
\&m0_opfunc_say_i,
];
Expand Down Expand Up @@ -357,6 +357,37 @@ sub new_context {
}


=item C<run_ops>
Run ops until there aren't ops left to run.
=cut

sub run_ops {
my ($ctx) = @_;

while (1) {
my $init_pc = $ctx->[PC]{instr};
my $instr_count = scalar(@{$ctx->[BCS]});
if ($ctx->[PC]{instr} >= $instr_count){
exit;
}
my $op_num = $ctx->[BCS][$init_pc][0];
my $op_func = $ctx->[INTERP]{op_funcs}[$op_num];
my $a1 = $ctx->[BCS][$init_pc][1];
my $a2 = $ctx->[BCS][$init_pc][2];
my $a3 = $ctx->[BCS][$init_pc][3];
&$op_func($ctx, $a1, $a2, $a3);
my $final_pc = $ctx->[PC]{instr};

# allow ops to change control flow
if ($init_pc == $final_pc) {
$ctx->[PC]{instr}++;
}
}
}


=item C<load_m0b>
Load an M0 bytecode file into memory.
Expand Down

2 comments on commit dcb5eb7

@leto
Copy link
Member

@leto leto commented on dcb5eb7 May 5, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do $a1, $a2 and $a3 represent?

@cotto
Copy link
Contributor Author

@cotto cotto commented on dcb5eb7 May 5, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're the arguments to the op, analogous to $1, $2, $3, ... in our .ops files. Their interpretation is up to the function implementing whichever op is executed, but they're usually register numbers.

Please sign in to comment.