aboutsummaryrefslogtreecommitdiff
path: root/scaf/system/scafc.pl
blob: ee6b9696b9ef343f43bd85535c67a985ce3abc4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/perl

#    Pure Data Packet - scafc: scaf compiler.
#    Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
# 
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
# 
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
# 
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# set this if you want to enable/disable optimizing

$optimize = 1;


# this parses a single scaf line
# it is not very intelligent. only looks for 1 def on a line
# todo: change later so it can read multiple lines


sub remove_illegal_characters {
    my $line = shift;
    $$line =~ s/\+/_PLUS_/g;
    $$line =~ s/-/_MINUS_/g;
    $$line =~ s/\@/_AT_/g;
    $$line =~ s/:/_COLON_/g;
    $$line =~ s/\?/_QMARK_/g;
    $$line =~ s/<</_SHIFT_/g;
    $$line =~ s/</_ST_/g;
    $$line =~ s/>/_GT_/g;
    $$line =~ s/=/_EQ_/g;
    $$line =~ s/\(/_OPEN_/g;
    $$line =~ s/\)/_CLOSE_/g;
}

sub parse_scaf_line {
    my $word, $def, $sub;
    shift;

    # this transforms the source into a parsed assembly like form
    # a word label: "<word>:<ret>"
    # a word definition line "<tab><word><ret>"
    # last def = <ret><ret>

    # dont process if line doesn't have a def

    # first remove comments
    s/\(\s+(\S+\s+)*?\)//g;

    if (m/:\s+/){

	# separate word and definition
	m/:\s+(\S+)\s+(.*)/;
	$word = $1;
	$def = $2;

	# remove illegal characters;
	remove_illegal_characters \$word;
	remove_illegal_characters \$def;

	# format definition in asm style
	$def =~ s/(\S+)(\s*)/\t$1\n/g;

	# replace ; by r
	$def =~ s/\s+;\s*/\n\tr\n/;

	# put word: def into one string
	$sub = "$word:\n$def\n";

	# debug
	#$sub =~ s/\t/<tab>/g;
	#$sub =~ s/\n/<ret>\n/g;
	#print "$sub";

	return $sub;

    }

};



# load and parse scaf source file
sub load_source {
    my $filename = shift;
    open(SOURCE, $filename) or die "Can't locate source module $filename\n";
    my @parsedsource;
    while (<SOURCE>){
	my $sub = parse_scaf_line $_;
	if ($sub) {
	    push @parsedsource, ($sub);
	}

    }
    close(SOURCE);
    return @parsedsource;

}

# this routine parses the optimization rules
sub load_optim {
    my $filename = shift;
    open(OPTIM, $filename) or die "Can't locate optimization rule file $filename\n";
    my @parsedoptim;
    while (<OPTIM>){
	unless (m/\A\#/){

	    if (m/\"\s*(.*?)\s*\".*?\"\s*(.*?)\s*\"/)
	      {
		  my $source = $1;
		  my $dest = $2;

		  $source =~ s/\s+/\n\t/;
		  $dest =~ s/\s+/\n\t/;
		  $source = "\t$source\n";
		  $dest = "\t$dest\n";

		  remove_illegal_characters \$source;
		  remove_illegal_characters \$dest;

		  push @parsedoptim, ("$source:$dest");
	      }
	}
    }
    close(OPTIM);

    return @parsedoptim;


}



# inline one parsed source's definitions into another parsed source's
sub inline_defs {
    my $dest = shift;
    my $source = shift;

    #print @$dest;
    #print @$source;


    # loop over file with inline defs
    foreach (@$source) {
	#print "<SUB>$_</SUB>\n";
	m/(\S+):\n(.*)\tr\n/s;

	my $def = "\t$1\n";
	my $body = $2;

	#print "<DEF>$def</DEF>\n";
	#print "<BODY>$body</BODY>\n";

	foreach (@$dest) {
	    s/$def/$body/g;
	}
	
    }

}

# this changes <WORD> to c <WORD> or j <WORD> all defined words
# the undefined words are supposed to be asm macros
sub call_defs {
    my $dest = shift;

    foreach (@$dest){
	m/(\S+):\n/s;
	my $word = $1;
	foreach (@$dest){
	    s/\t$word\n\tr\n/\tj $word\n/sg;
	    s/\t$word\n/\tc $word\n/sg;
	}
    }
}

# substitue word sequences in dest using optim table
sub subst_optim {
    my $dest = shift;
    my $optim = shift;
    foreach (@$optim){
	m/(.*?):(.*)/s;
	my $key = $1;
	my $subst = $2;
	
	foreach (@$dest){
	    s/$key/$subst/sg;
	}
    }
}
	
# add directives to produce global symbols
# global symbols need to start with carule_
sub global_syms {
    my $source = shift;
    foreach (@$source){
	s/rule_(\S+):\n/.globl\trule_$1\n.type\trule_$1,\@function\nrule_$1:\n/sg;
    }
}

# create an array with names for bookkeeping
sub name_array {
    my @namearray;
    my $source = shift;
    push @namearray, (".globl rulenames\nrulenames:\n");
    foreach (@$source){
	if (m/rule_(\S+):/s){
	    push @namearray, (".asciz\t\"$1\"\n");
	}
    }
    push @namearray, (".byte\t0\n");
    return @namearray;

}

# main program body

$dir=".";

$source = "-";


# parse command line
foreach (@ARGV){
    if (m/-I(.*)/) {
	$dir = $1;
    }
    else {
	$source = $_;
    }
}

$kernel = "$dir/kernel.scaf";
$macro  = "$dir/scafmacro.s";
$rules  = "$dir/optim.rules";



# load files
@psource = load_source $source;
@pkernel = load_source $kernel;
@poptim = load_optim $rules;


# substitute kernel defs in source
if ($optimize) {subst_optim \@psource, \@poptim;}
inline_defs \@psource, \@pkernel;

if ($optimize) {subst_optim \@psource, \@poptim;}

call_defs \@psource;
global_syms \@psource;
@pnames = name_array \@psource;

# print out asm file
print ".include \"$macro\"\n\n";
print @psource;
print @pnames;