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