/
build.pir
79 lines (69 loc) · 1.7 KB
/
build.pir
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
# Copyright (C) 2011-2012, Parrot Foundation.
.sub 'main' :main
.param pmc args
.local pmc outfh, ifh
.local string infile, outfile, program_name
.local int size
program_name = shift args
infile = shift args
outfile = infile . ".c"
say outfile
ifh = new ['FileHandle']
ifh.'open'(infile, 'rb')
outfh = new ['FileHandle']
outfh.'open'(outfile, 'wb')
print outfh, "#include <stdlib.h>\n"
print outfh, "#include \"parrot/api.h\"\n"
print outfh, "static const unsigned char program_code[] = {"
size = 0
read_loop:
.local string pbcstring
.local int pbclength
pbcstring = ifh.'read'(16384)
pbclength = length pbcstring
unless pbclength > 0 goto read_done
.local int pos
pos = 0
code_loop:
unless pos < pbclength goto code_done
$I0 = ord pbcstring, pos
$S0 = $I0
print outfh, $S0
print outfh, ','
inc pos
inc size
$I0 = size % 32
unless $I0 == 0 goto code_loop
print outfh, "\n"
goto code_loop
code_done:
goto read_loop
read_done:
say "Done reading input"
ifh.'close'()
say "writing coda"
print outfh, "\n};\n\nstatic const size_t bytecode_size = "
$S0 = size
print outfh, $S0
print outfh, ";\n"
print outfh, <<'END_OF_FUNCTION'
const unsigned char * get_program_code(void)
{
return program_code;
}
size_t get_program_code_size(void)
{
return bytecode_size;
}
END_OF_FUNCTION
outfh.'flush'()
outfh.'close'()
.return ()
err_infile:
die "cannot open infile"
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: