This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bfmake.in
128 lines (109 loc) · 4.62 KB
/
bfmake.in
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
#!/usr/bin/perl
use File::Basename;
use Getopt::Std;
%options = ();
sub help {
print "bfmake - asm2bf automated build utility.\n";
print "copyright (C) by Kamila Szewczyk, 2020\n";
print "\n";
print "usage: bfmake [-h/-s/-l/-c/-o file] file.asm\n";
print "\n";
print "available flags:\n";
print " -h: display this help page.\n";
print " -s: disable stripping of the brainfuck binary.\n";
print " -p: disable bfasm step.\n";
print " -l: disable standard library.\n";
print " -b: build asm2bf bytecode. Restriction from below applies.\n";
print " -c: build C code. Incompatible with -p.\n";
print " -m: print mappings\n";
print " -w: set line width; for example `-w 80'. Incompatible with -s.\n";
print " -z: emit constants in O(n) space complexity\n";
print " instead of utilizing multiplication loops.\n";
print " Incompatible with -p.\n";
print " -e: compress code; prefix style. Incompatible with -p.\n";
print " -f: compress code; postfix style. Incompatible with -p.\n";
print " -t: output tiny code for programs not utilizing the label system.\n";
print " excellent if you aim at small and fast snippets. Exclusive with -p.\n";
print " -x: use the bitwidth extender (bconv).\n";
print " -q: do not strip unknown instructions. Incompatible with `-s'.\n";
print "bfvm flags (no effect if bfvm isn't enabled):\n";
print " -x: force 32-bit bfvm output.\n";
print " -f: build freestanding bfvm code.\n";
print " -a: set bfvm heap size in cells.\n";
print " -v: don't surpress bfvm output.\n";
print " -j: put the tape on the stack, not the heap.\n";
print "\n";
print "IMPORTANT: FLAGS GO BEFORE INPUT FILE.\n";
print "-e and -f are mutually exclusive.\n";
exit
}
sub switch_ext {
my $path = shift @_;
my $text = shift @_;
my ($name,$dir,$ext) = fileparse($path,'\..*');
return $dir . $name . $text;
}
sub build {
my $name = shift @_;
die "No such file: $name" if (! -e $name);
my $target_ext = '.b';
$target_ext = '.c' if defined $options{c};
my $lib = "cat \"@PREFIX@share/asm2bf/lib-bfm-stub.lua\"";
my $stripper = "bfstrip";
my $compiler = "bfasm";
my $bconv = "cat";
my $output = switch_ext($name, $target_ext);
my $bfvm = 'cat';
my $mflag = '';
my $wf = '';
my $b32 = '';
my $freestanding = '';
my $heapsiz = '';
my $o0 = '';
my $prefrle = '';
my $postfrle = '';
my $vm = '';
my $tiny = '';
my $bfvmredir = '2> /dev/null';
my $xf = '';
my $stkflag = '';
my $bfstrip_noign = '';
my $bfvmflag = '';
$output = $options{o} if defined $options{o};
$wf = $options{w} if defined $options{w};
$lib = 'echo ""' if defined $options{l};
$stripper = 'cat' if defined $options{s} or $options{c} or $options{b};
$compiler = 'cat' if defined $options{p};
$bfvm = "bfvm" if defined $options{c};
$mflag = ' -m ' if defined $options{m};
$b32 = ' -32 ' if defined $options{x} and (defined $options{c} or defined $options{b});
$freestanding = ' -freestanding ' if defined $options{f} and defined $options{c};
$heapsiz = (' -heap ' . $options{a} . ' ') if defined $options{a} and defined $options{c};
$o0 = ' -O0 ' if defined $options{z};
$prefrle = ' -zb ' if defined $options{e};
$postfrle = ' -ze ' if defined $options{f};
$vm = ' -vm ' if defined $options{b} or defined $options{c};
$tiny = ' -t ' if defined $options{t};
$bfvmredir = '' if defined $options{v};
$stkflag = '-stack' if defined $options{j} and (defined $options{b} or defined $options{c});
$bfstrip_noign = '--no-ignore ' if defined $options{q};
$bfvmflag = '#BFVM=1' if defined $options{c} or $options{b};
$xf = '-s' if defined $options{x};
$bconv = "bconv" if (defined $options{x} or defined $options{v}) and not (defined $options{b} or defined $options{c});
`bash -c \"{ $lib ; echo ''; echo '$bfvmflag'; cat '$name' ; } | bfpp 2>> error$$.log | bflabels $mflag 2>> error$$.log | effective 2>> error$$.log | constpp 2>> error$$.log } | $compiler $tiny $prefrle $postfrle $o0 $vm 2>> error$$.log | $stripper $bfstrip_noign $wf | $bfvm $b32 $freestanding $heapsiz $bfvmredir $stkflag | $bconv $xf > $output.tmp\"`;
if(-s "error$$.log" != 0) {
print `cat error$$.log`;
unlink "error$$.log";
unlink "$output.tmp";
exit 1;
}
unlink $output;
rename($output.".tmp", $output);
unlink "error$$.log";
exit 0;
}
getopts("hsxeftpcxvjzqbmlo:w:a:", \%options);
help() if defined $options{h};
foreach (@ARGV) {
build($_);
}