| 1 | # Copyright (c) 2024-2025 Philipp Schafft | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # ABSTRACT: module for interacting with SIRTX VM code | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package SIRTX::VM::RegisterFile; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use v5.16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use SIRTX::VM::Register; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use parent 'Data::Identifier::Interface::Userdata'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | our $VERSION = v0.12; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my @_register_templates = ( | ||||||||||||||||||||||||||||
| 23 | # user: | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | (map{{name => 'user'.$_}} | |||||||||||||||||||||||||||
| 26 | 0 .. 7), # user registers | ||||||||||||||||||||||||||||
| 27 | 24 | (map{{name => 'user'.$_}} | |||||||||||||||||||||||||||
| 28 | 8 .. 31), # extended user registers | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # system: | ||||||||||||||||||||||||||||
| 31 | {name => 'arg'}, # arg register | ||||||||||||||||||||||||||||
| 32 | {name => 'ns'}, # namespace register | ||||||||||||||||||||||||||||
| 33 | {name => 'error'}, # error register | ||||||||||||||||||||||||||||
| 34 | {name => 'context'}, # context register | ||||||||||||||||||||||||||||
| 35 | {name => 'in'}, # call input register | ||||||||||||||||||||||||||||
| 36 | {name => 'out'}, # call output | ||||||||||||||||||||||||||||
| 37 | {name => 'deep'}, # deep storage | ||||||||||||||||||||||||||||
| 38 | 1 | (map{undef} | |||||||||||||||||||||||||||
| 39 | 39 .. 60), # unassigned | ||||||||||||||||||||||||||||
| 40 | {name => 'io'}, # I/O register | ||||||||||||||||||||||||||||
| 41 | {name => 'rodata'}, # rodata | ||||||||||||||||||||||||||||
| 42 | {name => 'program_text'}, # program text | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub new { | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my ($pkg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 0 | my @registers; | |||||||||||||||||||||||||||
| 49 | my %register_names; | ||||||||||||||||||||||||||||
| 50 | my $self = bless { | ||||||||||||||||||||||||||||
| 51 | physical_registers => \@registers, | ||||||||||||||||||||||||||||
| 52 | logical_registers => [], | ||||||||||||||||||||||||||||
| 53 | register_names => \%register_names, | ||||||||||||||||||||||||||||
| 54 | 0 | logical_temperature => {map {$_ => SIRTX::VM::Register::TEMPERATURE_LUKEWARM()} 0..7}, | |||||||||||||||||||||||||||
| 55 | 0 | logical_owner => {map {$_ => SIRTX::VM::Register::OWNER_MINE()} 0..7}, | |||||||||||||||||||||||||||
| 56 | }, $pkg; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | for (my $i = 0; $i < scalar(@_register_templates); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | my $template = $_register_templates[$i]; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | next unless defined $template; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $registers[$i] = SIRTX::VM::Register->_new(%{$template}, physical => $i, type => ($i >= 32 ? SIRTX::VM::Register::TYPE_SYSTEM() : SIRTX::VM::Register::TYPE_USER())); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (defined $template->{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $register_names{$template->{name}} = $registers[$i]; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $self->map_reset; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | return $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub map_reset { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
map_reset: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | for (my $i = 0; $i < 8; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $self->map($i => $i); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub map { | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my ($self, $logical, $physical) = @_; | ||||||||||||||||||||||||||
|
Subroutine
map: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($logical < 0 || $logical >= 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$logical < 0 or $logical >= 8
MC/DC0%0/2
$logical < 0 or $logical >= 8
$logical < 0
$logical >= 8
Truth table0%0/3
A or B
A= $logical < 0B= $logical >= 8
| |||||||||||||||||||||||||||||
| 88 | 0 | croak 'Bad logical register: '.$logical; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (!ref($physical)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $physical = $self->get_physical($physical); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | $self->{logical_registers}[$logical] = $physical; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub get_physical { | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my ($self, $physical) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_physical: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return $physical if ref $physical; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return $self->{physical_registers}[$physical] // croak 'Bad physical register: '.$physical; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"physical_registers"}[$physical] // croak('Bad physical register: ' . $physical)
MC/DC0%0/2
$self->{"physical_registers"}[$physical] // croak('Bad physical register: ' . $physical)
$self->{"physical_registers"}[$physical]
croak('Bad physical register: ' . $physical)
Truth table0%0/3
A // B
A= $self->{"physical_registers"}[$physical]B= croak('Bad physical register: ' . $physical)
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub get_logical { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my ($self, $logical) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_logical: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return $self->{logical_registers}[$logical] // croak 'Bad logical register: '.$logical; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"logical_registers"}[$logical] // croak('Bad logical register: ' . $logical)
MC/DC0%0/2
$self->{"logical_registers"}[$logical] // croak('Bad logical register: ' . $logical)
$self->{"logical_registers"}[$logical]
croak('Bad logical register: ' . $logical)
Truth table0%0/3
A // B
A= $self->{"logical_registers"}[$logical]B= croak('Bad logical register: ' . $logical)
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | sub get_physical_by_name { | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_physical_by_name: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (ref $name) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 116 | 0 | return $name; | |||||||||||||||||||||||||||
| 117 | } elsif ($name =~ /^r([0-9]+)$/) { | ||||||||||||||||||||||||||||
| 118 | 0 | return $self->get_logical($1); | |||||||||||||||||||||||||||
| 119 | } elsif (defined(my $r = $self->{register_names}{$name})) { | ||||||||||||||||||||||||||||
| 120 | 0 | return $r; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | croak 'Unknown register: '.$name; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # deprecated alias: | ||||||||||||||||||||||||||||
| 127 | 1 | *get_by_name = *get_physical_by_name; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub get_logical_by_name { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_logical_by_name: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($name =~ /^r([0-9]+)$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | 0 | return int($1); | |||||||||||||||||||||||||||
| 133 | } elsif (defined(my $r = $self->{register_names}{$name})) { | ||||||||||||||||||||||||||||
| 134 | 0 | return $self->get_logical_by_physical($r); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | croak 'Unknown register: '.$name; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub get_logical_by_physical { | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my ($self, $physical) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_logical_by_physical: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | $physical = $self->get_physical($physical); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | for (my $i = 0; $i < 8; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ($self->{logical_registers}[$i] == $physical) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | return $i; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | croak 'Register is not mapped'; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub register_owner { | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my ($self, $register, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
register_owner: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (ref $register) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 160 | 0 | return $register->owner($n); | |||||||||||||||||||||||||||
| 161 | } elsif ($register =~ /^r([0-9]+)$/) { | ||||||||||||||||||||||||||||
| 162 | 0 | my $logical = int($1); | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | $self->{logical_owner}{$logical} = $n if defined $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | return $self->{logical_owner}{$logical}; | |||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 0 | return $self->get_physical_by_name($register)->owner($n); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub register_temperature { | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | my ($self, $register, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
register_temperature: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if (ref $register) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 175 | 0 | return $register->temperature($n); | |||||||||||||||||||||||||||
| 176 | } elsif ($register =~ /^r([0-9]+)$/) { | ||||||||||||||||||||||||||||
| 177 | 0 | my $logical = int($1); | |||||||||||||||||||||||||||
| 178 | 0 | ▶ | $self->{logical_temperature}{$logical} = $n if defined $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | return $self->{logical_temperature}{$logical}; | |||||||||||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||||||||||
| 181 | 0 | return $self->get_physical_by_name($register)->temperature($n); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | sub expand { | ||||||||||||||||||||||||||||
| 187 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expand: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 0 | my @res; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | foreach my $reg (@args) { | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | if ($reg eq 'r*') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 192 | 0 | push(@res, map {'r'.$_} 0..7); | |||||||||||||||||||||||||||
| 193 | } elsif ($reg eq 'user*') { | ||||||||||||||||||||||||||||
| 194 | 0 | push(@res, map {'user'.$_} 0..31); | |||||||||||||||||||||||||||
| 195 | } elsif ($reg eq 'system*') { | ||||||||||||||||||||||||||||
| 196 | 0 | push(@res, grep {defined} map {scalar(eval {$self->get_physical($_)->name})} 32..63); | |||||||||||||||||||||||||||
| 197 | } else { | ||||||||||||||||||||||||||||
| 198 | 0 | push(@res, $reg); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | return @res; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | sub clone { | ||||||||||||||||||||||||||||
| 207 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 0 | my @registers; | |||||||||||||||||||||||||||
| 209 | my %register_names; | ||||||||||||||||||||||||||||
| 210 | my $clone = bless { | ||||||||||||||||||||||||||||
| 211 | physical_registers => \@registers, | ||||||||||||||||||||||||||||
| 212 | logical_registers => [], | ||||||||||||||||||||||||||||
| 213 | register_names => \%register_names, | ||||||||||||||||||||||||||||
| 214 | 0 | logical_temperature => {%{$self->{logical_temperature}}}, | |||||||||||||||||||||||||||
| 215 | 0 | logical_owner => {%{$self->{logical_owner}}}, | |||||||||||||||||||||||||||
| 216 | }, __PACKAGE__; | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # clone registers: | ||||||||||||||||||||||||||||
| 219 | 0 | foreach my $register (@{$self->{physical_registers}}) { | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | if (defined $register) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | my $c = $register->clone; | |||||||||||||||||||||||||||
| 222 | 0 | push(@registers, $c); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | ▶ | if (defined(my $name = $c->name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 0 | $register_names{$name} = $c; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | } else { | ||||||||||||||||||||||||||||
| 228 | 0 | push(@registers, undef); | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # clone map: | ||||||||||||||||||||||||||||
| 233 | 0 | ▶ | for (my $i = 0; $i < scalar(@{$self->{logical_registers}}); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 0 | $clone->map($i => $self->get_logical($i)->physical); | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | return $clone; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # ---- Private helpers ---- | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | sub _physical_name_by_number { | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | my ($pkg, $number) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_physical_name_by_number: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 244 | 0 | my $template = $_register_templates[$number]; | |||||||||||||||||||||||||||
| 245 | 0 | ▶ | return defined($template) ? $template->{name} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 1 | 1; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | __END__ | ||||||||||||||||||||||||||||