Permalink
Browse files

make P5 M0 code more amenable to ops changes

  • Loading branch information...
1 parent 6ff1cbb commit 92347879c6581134cb1efe334938b54e81f1c040 @cotto cotto committed Oct 2, 2011
Showing with 123 additions and 45 deletions.
  1. +49 −45 src/m0/perl5/m0_assembler.pl
  2. +2 −0 src/m0/perl5/m0_interp.pl
  3. +72 −0 tools/dev/m0_opcheck.pl
@@ -97,10 +97,12 @@ sub m0b_header {
sub parse_op_data {
my $ops;
+ my $num = 0;
while (<DATA>) {
chomp;
- my ($num, $name) = split / /, $_;
- $ops->{$name} = $num;
+ next if (/^#/);
+ $ops->{$_} = $num;
+ $num++;
}
say "Parsed data for " . scalar(keys %$ops) . " ops";
return $ops;
@@ -492,7 +494,7 @@ sub opname_to_num {
$opname =~ s/-/_/g;
die "Invalid M0: unknown op '$opname'" if (! exists $ops->{$opname});
- return oct $ops->{$opname};
+ return $ops->{$opname};
}
sub parse_version {
@@ -623,45 +625,47 @@ sub parse_chunks {
# vim: expandtab shiftwidth=4:
__DATA__
-0x00 noop
-0x01 goto
-0x02 goto_if
-0x03 goto_chunk
-0x04 add_i
-0x05 add_n
-0x06 sub_i
-0x07 sub_n
-0x08 mult_i
-0x09 mult_n
-0x0A div_i
-0x0B div_n
-0x0C mod_i
-0x0D mod_n
-0x0E convert_i_n
-0x0F convert_n_i
-0x10 ashr
-0x11 lshr
-0x12 shl
-0x13 and
-0x14 or
-0x15 xor
-0x16 gc_alloc
-0x17 sys_alloc
-0x18 sys_free
-0x19 copy_mem
-0x1A set
-0x1B set_imm
-0x1C deref
-0x1D set_ref
-0x1E set_byte
-0x1F get_byte
-0x20 set_word
-0x21 get_word
-0x22 csym
-0x23 ccall_arg
-0x24 ccall_ret
-0x25 ccall
-0x26 print_s
-0x27 print_i
-0x28 print_n
-0x29 exit
+# gen_opnames_from(m0.ops) template("LC_OP")
+noop
+goto
+goto_if
+goto_chunk
+add_i
+add_n
+sub_i
+sub_n
+mult_i
+mult_n
+div_i
+div_n
+mod_i
+mod_n
+convert_i_n
+convert_n_i
+ashr
+lshr
+shl
+and
+or
+xor
+gc_alloc
+sys_alloc
+sys_free
+copy_mem
+set
+set_imm
+deref
+set_ref
+set_byte
+get_byte
+set_word
+get_word
+csym
+ccall_arg
+ccall_ret
+ccall
+print_s
+print_i
+print_n
+exit
+# end_gen
@@ -102,6 +102,7 @@ sub new_interp {
my $interp;
$interp->[OP_FUNCS] = [
+# gen_opnames_from(m0.ops) template(" \&m0_opfunc_LC_OP,")
\&m0_opfunc_noop,
\&m0_opfunc_goto,
\&m0_opfunc_goto_if,
@@ -144,6 +145,7 @@ sub new_interp {
\&m0_opfunc_print_i,
\&m0_opfunc_print_n,
\&m0_opfunc_exit,
+# end_gen
];
$interp->[CONFIG] = {};
$interp->[CALL_FRAMES] = [];
View
@@ -0,0 +1,72 @@
+#! perl
+# Copyright (C) 2011, Parrot Foundation.
+
+use strict;
+use warnings;
+use feature "say";
+
+use File::Slurp;
+
+
+# todo:
+# * ensure that all op functions are defined
+# * insert stubs for functions that aren't defined
+# * warn about unused functions
+
+my @m0_files = qw<
+src/m0/perl5/m0_assembler.pl
+src/m0/perl5/m0_interp.pl
+>;
+
+
+
+foreach my $file (@m0_files) {
+ # slurp the file into memory
+ my @lines = read_file($file);
+ my @fixed_lines;
+ my $op_gen = '';
+ foreach my $line (@lines) {
+ if ($line =~ /gen_opnames_from\(m0\.ops\)/) {
+ push @fixed_lines, $line;
+ my $template = $line;
+ $template =~ s/.*template\("([^"]*)"\).*\n/$1/;
+ $op_gen = 'names';
+ foreach my $op (m0_ops()) {
+ my $op_name = $template;
+ $op_name =~ s/LC_OP/$op/;
+ push @fixed_lines, "$op_name\n";
+ }
+ }
+ elsif ($op_gen) {
+ if ($line =~ /end_gen/) {
+ $op_gen = '';
+ push @fixed_lines, $line;
+ }
+ }
+ else {
+ push @fixed_lines, $line;
+ }
+ }
+ write_file($file, @fixed_lines);
+}
+
+
+sub m0_ops {
+ my @file_ops = read_file("src/m0/m0.ops");
+ # remove comments from m0.ops
+ my @real_ops;
+ foreach (@file_ops) {
+ push @real_ops, $_ unless (/^[\s]*#/);
+ }
+
+ chomp foreach @real_ops;
+ @real_ops;
+}
+
+# Local Variables:
+# mode: cperl
+# cperl-indent-level: 4
+# fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4:
+

0 comments on commit 9234787

Please sign in to comment.