-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild
executable file
·179 lines (143 loc) · 4.62 KB
/
build
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
#!/bin/sh
# Generate utf8 include with xxd
if command -v xxd >/dev/null; then
xxd -i src/utf8.h | sed 's/\([0-9a-f]\)$/\0, 0x00/' > src/strutf8.xxd
xxd -i src/arena.h | sed 's/\([0-9a-f]\)$/\0, 0x00/' > src/strarena.xxd
else
echo "xxd is not installed." 1>&2
echo "This is fine, until you want to change src/utf8.h or src/arena.h." 1>&2
echo "Continuing." 1>&2
fi
if [ "$1" = "install" ]; then
# Build release
cc src/pgen.c -o pgen -O3 -march=native
# Install binary
sudo cp pgen /bin/pgen
echo "Installed pgen."
elif [ "$1" = "profile" ]; then
# Build pgen and generate tokenizer and parser.
cc src/pgen.c -o pgen
./pgen -u examples/pl0.tok examples/pl0.peg -o examples/pl0.h
cd examples/
# Build pl0 tokenizer/parser for profiling
cc pl0.c -static -O3 -march=native -ggdb3
# Profile with Callgrind
valgrind --tool=callgrind --collect-jumps=yes --dump-instr=yes ./a.out 2>/dev/null
callgrind_annotate callgrind.out* > calls
nano calls
# Profile with Cachegrind
valgrind --tool=cachegrind --branch-sim=yes ./a.out 2>/dev/null
cg_annotate cachegrind.out.* > cache
nano cache
# Dump the instructions
objdump -D a.out > dump
nano dump
# Make a flamegraph
#flamegraph ./a.out
# Clean up
rm dump calls cache cachegrind.out.* callgrind.out.* 2>/dev/null
cd ..
elif [ "$1" = "pgo" ]; then
shift
if [ "$1" = "install" ]; then
INSTALL="1"
fi
CLANG_VERSION=$(clang --version | sed -n '1s/[^0-9]*\([0-9][^.]*\.[0-9][^.]*\.[0-9][^.]*\).*/\1/p' | cut -d. -f1)
CLANG="clang-$CLANG_VERSION"
PROFMERGE="llvm-profdata-$CLANG_VERSION"
# Build instrumented binary
$CLANG src/pgen.c -o pgen -O3 -march=native -fprofile-instr-generate
# Run the instrumented binary on the examples, creating perf data.
./pgen examples/calc.peg
mv *.profraw 1.p
rm calc.h
./pgen examples/bf.peg
mv *.profraw 2.p
rm bf.h
./pgen examples/pl0.peg
mv *.profraw 3.p
rm pl0.h
# Combine and index the perf data.
$PROFMERGE merge *.p -o pgen.pgo
# Recompile using the perf data
$CLANG src/pgen.c -o pgen -O3 -march=native -fprofile-use=pgen.pgo
# Install binary
if [ "$INSTALL" = "1" ]; then
sudo cp pgen /bin/pgen
echo "Installed pgen."
fi
# Clean up
rm *.p pgen.pgo
elif [ "$1" = "macrocheck" ]; then
# Build pgen and examples
cc src/pgen.c -o pgen
./pgen examples/pl0.peg -o examples/pl0.h
# Check
echo '#include <limits.h>' > .empty.c
echo '#include <stddef.h>' >> .empty.c
echo '#include <stdint.h>' >> .empty.c
echo '#include <stdio.h>' >> .empty.c
echo '#include <stdlib.h>' >> .empty.c
echo '#include <string.h>' >> .empty.c
echo '#include <inttypes.h>' >> .empty.c
echo '#include <stdbool.h>' >> .empty.c
cc -dM -E .empty.c | sort > .predef.c
cc -dM -E examples/pl0.h | sort > .nowdef.c
MACRS="$(diff .predef.c .nowdef.c | grep '>' | sed 's/^<\ //g' | cut -d' ' -f2- \
| grep -v '#define PGEN_' \
| grep -v '#define PL0_' \
| grep -v '#define UTF8_' \
| grep -v '#define recover' \
| grep -v '#define PRI_CODEPOINT')"
rm .empty.c .predef.c .nowdef.c
if [ ! "$MACRS" = "" ]; then
echo "Forgot to undef the following macros:"
printf "%s\n" "$MACRS"
exit 1;
fi
elif [ "$1" = "python_test" ]; then
cc src/pgen.c -o pgen -fsanitize=address -g
./pgen examples/calc.peg -p examples/calc/
cd examples/calc/
python setup.py build_ext --inplace >/dev/null
python test.py
if [ $? -ne 0 ]; then
exit 1
fi
cd ../..
cc src/pgen.c -o pgen -fsanitize=address -g
./pgen examples/pl0.peg -p examples/pl0/
cd examples/pl0/
python setup.py build_ext --inplace >/dev/null
python test.py
if [ $? -ne 0 ]; then
exit 1
fi
cd ../..
else # Debug Build
# Build debug
cc src/pgen.c -o pgen --std=c99 -g -Wall -Wextra -Wpedantic -Wno-unused-parameter -Wno-unused-variable -Wconversion
if [ ! "$?" -eq 0 ]; then exit 1; fi
# Test
if [ "$1" = "test" ]; then
shift;
# Generate pl0 example
./pgen examples/pl0.peg -o examples/pl0.h $@
if [ ! "$?" -eq 0 ]; then exit 1; fi
# Build and run pl0 example
cd examples/
cc pl0.c -g -Wconversion
if [ ! "$?" -eq 0 ]; then exit 1; fi
./a.out > ../.testast.json
if [ ! "$?" -eq 0 ]; then exit 1; fi
cd ..
DIFF="$(diff .refast.json .testast.json)"
if [ ! "$DIFF" = "" ]; then
echo "The test AST was different than expected.\nDiff:"
echo "$DIFF"
fi
echo "Built and tested pgen."
else
echo "Built pgen."
fi
fi