I was working with a polyhedral compiler that produce quite unreadable code, here is a small sample:
for (t2=2*t1;t2<=min(floord(2*T+N-3,32),floord(64*t1+N+61,32));t2++) {
for (t3=max(ceild(32*t2-N-27,32),2*t1);t3<=min(min(floord(2*T+N-3,32),floord(64*t1+N+61,32)),floord(32*t2+N+27,32));t3++) {
if ((t1 <= floord(32*t3-N+1,64)) && (t2 <= t3-1)) {
if ((N+1)%2 == 0) {
for (t5=max(32*t2,32*t3-N+4);t5<=32*t2+31;t5++) {
a[-32*t3+t5+N-2][N-2]=b[-32*t3+t5+N-2][N-2];;
}
}
}
if ((t1 <= floord(32*t2-N+1,64)) && (t2 >= t3)) {
if ((N+1)%2 == 0) {
for (t6=max(32*t3,32*t2-N+4);t6<=min(32*t2,32*t3+31);t6++) {
a[N-2][-32*t2+t6+N-2]=b[N-2][-32*t2+t6+N-2];;
}
}
}
I was trying to debug a part of the compiler by translating arithmetic expression in the produced code with a printf of the array indices, e.g., this expression:
a[-32*t3+t5+N-2][N-2]=b[-32*t3+t5+N-2][N-2];;
should become this printf:
printf("a[%d][%d] = b[%d][%d]\n",-32*t3+t5+N-2,N-2,-32*t3+t5+N-2,N-2);
I started trying with awk and produced this simple program that identifies strings to be modified and let the rest of the program unchanged:
awk '{if ($0 ~ "^[ ]*[a,b]") print "printf("; else print $0;}'
However, I do not know how to parse the arithmetic expression in order to leave its structure while removing the indexes of the array accesses.
I tried with a while loop but I am stuck at the moment.
awk should be ok to do such substitions but any suggestions in other languages are welcome!
update The arithmetic expression can be any arithmetic expression, such as:
b[t3][t4]=0.2*(a[t3][t4]+a[t3][t4-1]+a[t3][1+t4]+a[1+t3][t4]+a[t3-1][t4]);;
Code:
Explanations:
awk '/\[/ { ........; next; }1'will execute........on any line where it finds a[character, and will print the line untouched otherwise.sub(/^ */, "");trims leading space characters off the current linesub(/;*$/, "");trims tailing semi-colon characters off the current lineprintstatement concatenating 5 bitsgensub("\\[[^]]*\\]", "[%d]", "g")returns a copy of the current line, where any square-bracketed statements are replaced with with[%d]. Note that nested square-brackets would break this. Also note, that as opposed tosub, thegensubcommand does not actually modify the current line.gensub("[^\\[]*\\[([^]]*)\\][^\\[]*", ",\\1", "g")also takes a copy of the current line, and for each square-bracketed expression it finds, it does 3 things:[^\\[]*removes leading non-[characters\\[([^]]*)\\]matches the square-bracketed expression, and",\\1"replaces it with a comma character followed by what was inside the square-brackets[^\\[]*removes trailing non-[characters (until the next square-bracketed expression or the end of the line)[characters is redundant between 2 square-bracketed expression, but is useful at the very beginning and the very end of the line.Input:
Output: