Actually make assembler work
[app-scheme79asm.git] / lib / App / Scheme79asm.pm
CommitLineData
509643aa
MG
1package App::Scheme79asm;
2
3use 5.014000;
4use strict;
5use warnings;
6
4a9c3fa9 7use Data::Dumper qw/Dumper/;
509643aa
MG
8use Data::SExpression qw/consp scalarp/;
9use Scalar::Util qw/looks_like_number/;
10
11our $VERSION = '0.001';
12
13our %TYPES = (
14 LIST => 0,
15 SYMBOL => 1,
4a9c3fa9 16 NUMBER => 1,
509643aa
MG
17 VAR => 2,
18 VARIABLE => 2,
19 CLOSURE => 3,
20 PROC => 4,
21 PROCEDURE => 4,
4a9c3fa9 22 IF => 5,
509643aa
MG
23 COND => 5,
24 CONDITIONAL => 5,
25 CALL => 6,
509643aa
MG
26 QUOTE => 7,
27 QUOTED => 7,
4a9c3fa9
MG
28
29 MORE => 0,
30 CAR => 1,
31 CDR => 2,
32 CONS => 3,
33 ATOM => 4,
34 PROGN => 5,
35 MAKELIST => 6,
36 FUNCALL => 7,
509643aa
MG
37);
38
39*consp = *Data::SExpression::consp;
40*scalarp = *Data::SExpression::scalarp;
41
42sub process {
4a9c3fa9
MG
43 my ($self, $sexp, $location) = @_;
44 die 'Toplevel is not a list: ', Dumper($sexp), "\n" unless ref $sexp eq 'ARRAY';
45 my ($type, @addrs) = @$sexp;
46 my $addr;
47
48 die 'Type of toplevel is not atom: '. Dumper($type), "\n" unless scalarp($type);
49
50 if (@addrs > 1) {
51 $addr = $self->{freeptr} + 1;
52 $self->{freeptr} += @addrs;
53 $self->process($addrs[$_], $addr + $_) for 0 .. $#addrs;
54 } else {
55 $addr = $addrs[0];
56 }
57
58 $addr = $self->process($addr) if ref $addr eq 'ARRAY';
59 die 'Addr of toplevel is not atom: ', Dumper($addr), "\n" unless scalarp($addr);
509643aa 60
4a9c3fa9
MG
61 unless (looks_like_number $addr) { # is symbol
62 unless (exists $self->{symbols}{$addr}) {
63 $self->{symbols}{$addr} = $self->{nsymbols};
64 $self->{nsymbols}++;
65 }
66 $addr = $self->{symbols}{$addr}
67 }
509643aa 68
4a9c3fa9 69 die 'Computed addr is not a number: ', Dumper($addr), "\n" unless looks_like_number $addr;
509643aa
MG
70
71 if (ref $type eq 'Data::SExpression::Symbol') {
72 die "No such type: $type\n" unless exists $TYPES{$type};
73 $type = $TYPES{$type};
74 } elsif (!looks_like_number $type) {
75 die "Type is not a number or symbol: $type\n"
76 }
77
78 die "Type too large: $type\n" unless $type < (1 << $self->{type_bits});
79 die "Addr too large: $addr\n" unless $addr < (1 << $self->{addr_bits});
80 my $result = ($type << $self->{addr_bits}) + $addr;
4a9c3fa9
MG
81 unless ($location) {
82 $self->{freeptr}++;
83 $location = $self->{freeptr}
84 }
85 $self->{memory}[$location] = $result;
86 $location
509643aa
MG
87}
88
89sub parse {
90 my ($self, $string) = @_;
4a9c3fa9 91 my $ds = Data::SExpression->new({symbol_case => 'up', use_symbol_class => 1, fold_lists => 1});
509643aa
MG
92
93 my $sexp;
94 while () {
95 last if $string =~ /^\s*$/;
96 ($sexp, $string) = $ds->read($string);
97 $self->process($sexp)
98 }
99}
100
101sub finish {
102 my ($self) = @_;
103 $self->{memory}[5] = $self->{memory}[$self->{freeptr}];
104 $self->{memory}[4] = $self->{freeptr};
105 delete $self->{memory}[$self->{freeptr}]
106}
107
108sub new {
109 my ($class, %args) = @_;
110 $args{type_bits} //= 3;
111 $args{addr_bits} //= 8;
112 $args{freeptr} //= 6;
113 $args{memory} //= [0, 0, (1<<$args{addr_bits}), (1<<$args{addr_bits}), 0, 0, 0];
4a9c3fa9
MG
114 $args{symbols}{NIL} = 0;
115 $args{symbols}{T} = 1;
116 $args{nsymbols} = 2;
509643aa
MG
117 bless \%args, $class
118}
119
120sub print {
121 my ($self, $fh) = @_;
122 $fh //= \*STDOUT;
123
124 my $bits = $self->{type_bits} + $self->{addr_bits};
125 for my $index (0 .. $#{$self->{memory}}) {
126 my $val = $self->{memory}[$index];
127 if ($index == 4) {
128 $val = "${bits}'d$val"
129 } else {
130 $val = $val ? sprintf "%d'b%0${bits}b", $bits, $val : '0';
131 }
132 say $fh "mem[$index] <= $val;"
133 }
134}
135
136sub parse_and_print {
137 my ($self, $string, $fh) = @_;
138 $self->parse($string);
139 $self->finish;
140 $self->print($fh);
141}
142
1431;
144__END__
145
146=encoding utf-8
147
148=head1 NAME
149
150App::Scheme79asm - assemble sexp to Verilog ROM for SIMPLE processor
151
152=head1 SYNOPSIS
153
154 use App::Scheme79asm;
155 my $asm = App::Scheme79asm->new(type_bits => 3, addr_bits => 5);
156 $asm->parse_and_print('(number . 70)');
157
158=head1 DESCRIPTION
159
160B<NOTE:> this module does not do much at the moment.
161
162SIMPLE is a LISP processor defined in the 1979
163B<Design of LISP-Based Processors> paper by Steele and Sussman.
164
165The SIMPLE processor expects input in a particular tagged-pointer
166format. This module takes a string containing a sequence of
167S-expressions of the form C<(tag . value)> representing a tagged
168pointer. Here the tag is either a number or one of several predefined
169values (see the source for a full list), and the value is either a
170number or another tagged pointer. These values are laid out in memory
171and a block of verilog code assigning the memory contents to an array
172named C<mem> is printed.
173
174More documentation and features to follow.
175
176=head1 SEE ALSO
177
178L<http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-514.pdf>
179
180=head1 AUTHOR
181
182Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
183
184=head1 COPYRIGHT AND LICENSE
185
186Copyright (C) 2018 by Marius Gavrilescu
187
188This library is free software; you can redistribute it and/or modify
189it under the same terms as Perl itself, either Perl version 5.24.3 or,
190at your option, any later version of Perl 5 you may have available.
191
192
193=cut
This page took 0.020403 seconds and 4 git commands to generate.