Permalink
Browse files

Teach the M0 assembler about the special register symbols like INTERP…

…, PC and friends
  • Loading branch information...
1 parent 20a1d88 commit f8ae291be56228c7934745e3833d3de6409cec8f @leto leto committed May 15, 2011
Showing with 14 additions and 3 deletions.
  1. +14 −3 src/m0/m0_assembler.pl
@@ -32,6 +32,8 @@ =head1 DESCRIPTION
my $M0_META_SEG = 0x03;
my $M0_BC_SEG = 0x04;
+use constant M0_REG_RX => qr/^(([INSP]\d+)|INTERP|PC|EH|PCX|VAR|MDS|BCS)/;
+
assemble($file);
sub assemble {
@@ -104,6 +106,16 @@ sub parse_op_data {
sub register_name_to_num {
my ($register) = @_;
+ return 0 if ($register eq 'INTERP');
+
+ my $symbols = { PC => 1, EH => 2, PCX => 3, VAR => 4, MDS => 5, BCS => 6};
+
+ if($register !~ /\d+/){
+ my $number = $symbols->{$register};
+ die "Invalid register name: $register" unless $number;
+ return $number;
+ }
+
if( length $register > 3 ){
die "Invalid register name: $register";
}
@@ -115,8 +127,7 @@ sub register_name_to_num {
die "Invalid register number: $register";
}
- my $reg_rx = qr/^[INSP]/;
- unless ( $type =~ $reg_rx ) {
+ unless ( $type =~ m/^[INSP]$/ ) {
die "Invalid register type $type in register $register";
}
@@ -139,7 +150,7 @@ sub to_bytecode {
# as described in "Register Types and Context Structure" in the M0 spec
map {
- $bytecode .= pack('C', $_ =~ /^[INSP]/ ? register_name_to_num($a1) : $_ );
+ $bytecode .= pack('C', $_ =~ M0_REG_RX ? register_name_to_num($a1) : $_ );
} ($a1, $a2, $a3);
return $bytecode;

0 comments on commit f8ae291

Please sign in to comment.