| 1 | package App::Scheme79asm; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use 5.014000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use re '/s'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Carp qw/croak/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | ▶ | use Data::Dumper qw/Dumper/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Data::SExpression qw/consp scalarp/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use Scalar::Util qw/looks_like_number/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 1 | our $VERSION = '1.000'; | |||||||||||
| 14 | |||||||||||||
| 15 | 1 | our %TYPES = ( | |||||||||||
| 16 | LIST => 0, | ||||||||||||
| 17 | SYMBOL => 1, | ||||||||||||
| 18 | NUMBER => 1, | ||||||||||||
| 19 | VAR => 2, | ||||||||||||
| 20 | VARIABLE => 2, | ||||||||||||
| 21 | CLOSURE => 3, | ||||||||||||
| 22 | PROC => 4, | ||||||||||||
| 23 | PROCEDURE => 4, | ||||||||||||
| 24 | IF => 5, | ||||||||||||
| 25 | COND => 5, | ||||||||||||
| 26 | CONDITIONAL => 5, | ||||||||||||
| 27 | CALL => 6, | ||||||||||||
| 28 | QUOTE => 7, | ||||||||||||
| 29 | QUOTED => 7, | ||||||||||||
| 30 | |||||||||||||
| 31 | MORE => 0, | ||||||||||||
| 32 | CAR => 1, | ||||||||||||
| 33 | CDR => 2, | ||||||||||||
| 34 | CONS => 3, | ||||||||||||
| 35 | ATOM => 4, | ||||||||||||
| 36 | PROGN => 5, | ||||||||||||
| 37 | 'REVERSE-LIST' => 6, | ||||||||||||
| 38 | FUNCALL => 7, | ||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | 1 | *consp = *Data::SExpression::consp; | |||||||||||
| 42 | 1 | *scalarp = *Data::SExpression::scalarp; | |||||||||||
| 43 | |||||||||||||
| 44 | 36 | ▶ | sub process { | ||||||||||
|
Subroutine
process: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||
| 45 | 36 | my ($self, $sexp, $location) = @_; | |||||||||||
| 46 | 36 | ▶ | die 'Toplevel is not a list: ', Dumper($sexp), "\n" unless ref $sexp eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 35 | my ($type, @addrs) = @$sexp; | |||||||||||
| 48 | 35 | my $addr; | |||||||||||
| 49 | |||||||||||||
| 50 | 35 | ▶ | die 'Type of toplevel is not atom: '. Dumper($type), "\n" unless scalarp($type); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | |||||||||||||
| 52 | 34 | ▶ | if (@addrs > 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 8 | $addr = $self->{freeptr} + 1; | |||||||||||
| 54 | 8 | $self->{freeptr} += @addrs; | |||||||||||
| 55 | 8 | $self->process($addrs[$_], $addr + $_) for 0 .. $#addrs; | |||||||||||
| 56 | } else { | ||||||||||||
| 57 | 26 | $addr = $addrs[0]; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 34 | ▶ | $addr = $self->process($addr) if ref $addr eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 34 | ▶ | die 'Addr of toplevel is not atom: ', Dumper($addr), "\n" unless scalarp($addr); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 33 | my ($comment_type, $comment_addr) = ($type, $addr); | |||||||||||
| 63 | 33 | ▶ | die 'Computed addr is not a number: ', Dumper($addr), "\n" unless looks_like_number $addr; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | |||||||||||||
| 65 | 32 | ▶ | if (!looks_like_number $type) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 30 | ▶ | die "No such type: $type\n" unless exists $TYPES{$type}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 29 | $type = $TYPES{$type}; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 31 | ▶ | $addr += (1 << $self->{addr_bits}) if $addr < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 31 | ▶ | die "Type too large: $type\n" if $type >= (1 << $self->{type_bits}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 30 | ▶ | die "Addr too large: $addr\n" if $addr >= (1 << $self->{addr_bits}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 29 | my $result = ($type << $self->{addr_bits}) + $addr; | |||||||||||
| 74 | |||||||||||||
| 75 | 29 | ▶ | unless ($location) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 13 | $self->{freeptr}++; | |||||||||||
| 77 | $location = $self->{freeptr} | ||||||||||||
| 78 | 13 | } | |||||||||||
| 79 | 29 | $self->{memory}[$location] = $result; | |||||||||||
| 80 | 29 | $self->{comment}[$location] = "$comment_type $comment_addr"; | |||||||||||
| 81 | 29 | $location | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 15 | ▶ | sub parse { | ||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 85 | 15 | my ($self, $string) = @_; | |||||||||||
| 86 | 15 | my $ds = Data::SExpression->new({symbol_case => 'up', use_symbol_class => 1, fold_lists => 1}); | |||||||||||
| 87 | |||||||||||||
| 88 | 15 | my $sexp; | |||||||||||
| 89 | 15 | while () { | |||||||||||
| 90 | 24 | ▶ | last if $string =~ /^\s*$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 15 | ($sexp, $string) = $ds->read($string); | |||||||||||
| 92 | 15 | $self->process($sexp) | |||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 9 | ▶ | sub finish { | ||||||||||
|
Subroutine
finish: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 97 | 9 | my ($self) = @_; | |||||||||||
| 98 | 9 | $self->{memory}[5] = $self->{memory}[$self->{freeptr}]; | |||||||||||
| 99 | 9 | $self->{comment}[5] = $self->{comment}[$self->{freeptr}]; | |||||||||||
| 100 | 9 | $self->{memory}[4] = $self->{freeptr}; | |||||||||||
| 101 | 9 | delete $self->{memory}[$self->{freeptr}] | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 16 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 105 | 16 | my ($class, %args) = @_; | |||||||||||
| 106 | 16 | ▶ | $args{type_bits} //= 3; | ||||||||||
|
Condition100%2/2
$args{"type_bits"} //= 3
MC/DC100%1/1
$args{"type_bits"} //= 3
$args{"type_bits"}
Truth table100%2/2
A
A= $args{"type_bits"}
| |||||||||||||
| 107 | 16 | ▶ | $args{addr_bits} //= 8; | ||||||||||
|
Condition100%2/2
$args{"addr_bits"} //= 8
MC/DC100%1/1
$args{"addr_bits"} //= 8
$args{"addr_bits"}
Truth table100%2/2
A
A= $args{"addr_bits"}
| |||||||||||||
| 108 | 16 | ▶ | $args{freeptr} //= 6; | ||||||||||
|
Condition100%2/2
$args{"freeptr"} //= 6
MC/DC100%1/1
$args{"freeptr"} //= 6
$args{"freeptr"}
Truth table100%2/2
A
A= $args{"freeptr"}
| |||||||||||||
| 109 | 16 | ▶ | $args{memory} //= [0, 0, (1<<$args{addr_bits}), (1<<$args{addr_bits}), 0, 0, 0]; | ||||||||||
|
Condition100%2/2
$args{"memory"} //= [0, 0, 1 << $args{"addr_bits"}, 1 << $args{"addr_bits"}, 0, 0, 0]
MC/DC100%1/1
$args{"memory"} //= [0, 0, 1 << $args{"addr_bits"}, 1 << $args{"addr_bits"}, 0, 0, 0]
$args{"memory"}
Truth table100%2/2
A
A= $args{"memory"}
| |||||||||||||
| 110 | 16 | my @default_comments = ('(cdr part of NIL)', '(car part of NIL)', '(cdr part of T)', '(car part of T)', '(free storage pointer)', '', '(result of computation)'); | |||||||||||
| 111 | 16 | for (0 .. $#default_comments) { | |||||||||||
| 112 | 112 | $args{comment}[$_] = $default_comments[$_] | |||||||||||
| 113 | } | ||||||||||||
| 114 | 16 | bless \%args, $class | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 5 | ▶ | sub print_binary16 { | ||||||||||
|
Subroutine
print_binary16: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||
| 118 | 5 | my ($self, $fh) = @_; | |||||||||||
| 119 | 5 | ▶ | $fh //= \*STDOUT; # uncoverable condition right | ||||||||||
|
Condition100%1/2
$fh //= \*STDOUT
MC/DC100%0/1
$fh //= \*STDOUT
$fh
Truth table100%1/2
A
A= $fh
| |||||||||||||
| 120 | |||||||||||||
| 121 | 5 | ▶ | die "addr_bits + type_bits >= 16\n"if $self->{addr_bits} + $self->{type_bits} > 16; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | |||||||||||||
| 123 | 4 | my $length = @{$self->{memory}}; | |||||||||||
| 124 | 4 | ▶ | print $fh pack 'n', $length or croak "Failed to print memory size: $!"; # uncoverable branch true | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 4 | for (@{$self->{memory}}) { | |||||||||||
| 126 | 41 | ▶ | print $fh pack 'n', $_ or croak "Failed to print memory: $!" # uncoverable branch true | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | } | ||||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 4 | ▶ | sub print_verilog { | ||||||||||
|
Subroutine
print_verilog: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 131 | 4 | my ($self, $fh) = @_; | |||||||||||
| 132 | 4 | ▶ | $fh //= \*STDOUT; # uncoverable condition right | ||||||||||
|
Condition100%1/2
$fh //= \*STDOUT
MC/DC100%0/1
$fh //= \*STDOUT
$fh
Truth table100%1/2
A
A= $fh
| |||||||||||||
| 133 | |||||||||||||
| 134 | 4 | my $bits = $self->{type_bits} + $self->{addr_bits}; | |||||||||||
| 135 | 4 | my $index_length = length $#{$self->{memory}}; | |||||||||||
| 136 | 4 | my $index_format = '%' . $index_length . 'd'; | |||||||||||
| 137 | 4 | for my $index (0 .. $#{$self->{memory}}) { | |||||||||||
| 138 | 41 | my $val = $self->{memory}[$index]; | |||||||||||
| 139 | 41 | my $comment = $self->{comment}[$index]; | |||||||||||
| 140 | 41 | ▶ | if ($index == 4) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | 4 | $val = "${bits}'d$val" | |||||||||||
| 142 | } else { | ||||||||||||
| 143 | 37 | ▶ | $val = $val ? sprintf "%d'b%0${bits}b", $bits, $val : '0'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | } | ||||||||||||
| 145 | 41 | my $spaces = ' ' x ($bits + 5 - (length $val)); | |||||||||||
| 146 | 41 | $index = sprintf $index_format, $index; | |||||||||||
| 147 | |||||||||||||
| 148 | 41 | my $string = "mem[$index] <= $val;"; | |||||||||||
| 149 | 41 | ▶ | $string .= "$spaces // $comment" if defined $comment; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 41 | ▶ | say $fh $string or croak "Failed to print verilog: $!"; # uncoverable branch true | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | } | ||||||||||||
| 154 | 11 | ▶ | sub parse_and_print_binary16 { | ||||||||||
|
Subroutine
parse_and_print_binary16: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 155 | 11 | my ($self, $string, $fh) = @_; | |||||||||||
| 156 | 11 | $self->parse($string); | |||||||||||
| 157 | 5 | $self->finish; | |||||||||||
| 158 | 5 | $self->print_binary16($fh); | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 4 | ▶ | sub parse_and_print_verilog { | ||||||||||
|
Subroutine
parse_and_print_verilog: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 162 | 4 | my ($self, $string, $fh) = @_; | |||||||||||
| 163 | 4 | $self->parse($string); | |||||||||||
| 164 | 4 | $self->finish; | |||||||||||
| 165 | 4 | $self->print_verilog($fh); | |||||||||||
| 166 | } | ||||||||||||
| 167 | |||||||||||||
| 168 | 1 | 1; | |||||||||||
| 169 | __END__ | ||||||||||||