adjust the vector offsets to match stdc changes
[AROS.git] / compiler / purify / purify.gawk
blobd8da5aec641ac335e80b004b5ae70039bc14d0d4
1 BEGIN {
2     if (cpu=="i586") cpu="i386";
4     if (cpu!="i386")
5     {
6         print "Unsupported cpu: "cpu;
7         exit (10);
8     }
9     print "# Purify V0.2" > out;
10     mainfunc=0;
11     section="";
13 /^[ \t]*.text/ {
14     section=".text";
16 /^[ \t]*.data/ {
17     section=".data";
19 /^[ \t]*.section/ {
20     section="$2";
22 /^[ \t]call ([mc]alloc|free|realloc|f?read|mem(move|mem|set)|str(n?cpy|dup|cat)|fgets|getcwd|[lf]?stat|readdir)$/ {
23     #print >> out; next;
24     print "\t\tcall Purify_"$2 >> out; next;
26 /^[ \t]*pushl/ {
27     #print >> out; next;
28     print "\t\tpushl $4" >> out
29     print "\t\tpushl %esp" >> out;
30     print "\t\tcall _Purify_Push" >> out;
31     print "\t\taddl $8,%esp" >> out;
32     print >> out ; next
34 /^[ \t]*popl/ {
35     #print >> out; next;
36     print "\t\tpushl $4" >> out
37     print "\t\tpushl %esp" >> out;
38     print "\t\tcall _Purify_Pop" >> out;
39     print "\t\taddl $8,%esp" >> out;
40     print >> out ; next
42 /^[ \t]*subl.*,%esp$/ {
43     #print >> out; next;
44     na=split($2,a,",");
45     print "\t\tpushl "a[1] >> out
46     print "\t\tpushl %esp" >> out;
47     print "\t\tcall _Purify_Alloca" >> out;
48     print "\t\taddl $8,%esp" >> out;
49     print >> out ; next
51 /^[ \t]*movl.*,%esp$/ {
52     #print >> out; next;
53     na=split($2,a,",");
54     print "\t\tpushl "a[1] >> out
55     print "\t\tpushl %esp" >> out;
56     print "\t\tcall _Purify_MoveSP" >> out;
57     print "\t\taddl $8,%esp" >> out;
58     print >> out; next;
60 /^[ \t]*((lea|jmp)|.*%st[(][0-9]+[)])/ { print >> out; next }
61 /^[ \t]*[a-z]+ .*([^(]+)?[(][^)]+[)]/ {
62     #print >> out; next;
63     #print $0 " NR="NR" line="line
65     if (match($2,/^([^(,]+)?[(][^)]+[)]/))
66     {
67         arg1=substr($2,RSTART,RLENGTH);
68         check1=1;
69     }
70     else
71     {
72         match($2,/[^,]+/);
73         arg1=substr($2,RSTART,RLENGTH);
74         check1=0;
75     }
77     arg2=substr($2,RSTART+RLENGTH+1);
78     check2=match(arg2,/[(]/);
80     #print "arg1="arg1" c1="check1" arg2="arg2" c2="check2
82     if (check1 && check2)
83     {
84         print "Warning: unsupported double indirect access "NR
85         print $0
86     }
87     else
88     {
89         cmd=substr($1,1,3);
91         if (match($1,/^movs/))
92             size=substr($1,5,1);
93         else if (match($1,/^or/))
94             size=substr($1,3,1);
95         else
96             size=substr($1,4,1);
98         if (match($1,/^cmp/))
99         {
100             check1=check2;
101             arg1=arg2;
102             check2=0;
103         }
104         else if (match ($1,/^(inc|dec)/))
105         {
106             check2=1;
107             arg2=arg1;
108         }
110         if (check1)
111             arg=arg1;
113         if (check2)
114             arg=arg2;
116         if (check1 || check2)
117         {
118             print "\t\tpushl %eax" >> out
119             if (size == "l")
120                 print "\t\tpushl $4" >> out
121             else if (size == "w")
122                 print "\t\tpushl $2" >> out
123             else
124                 print "\t\tpushl $1" >> out
125             print "\t\tleal "arg",%eax" >> out
126             print "\t\tpushl %eax" >> out
127             if (check1)
128                 print "\t\tcall _Purify_CheckRead" >> out;
129             if (check2)
130                 print "\t\tcall _Purify_CheckWrite" >> out;
131             print "\t\taddl $8,%esp" >> out;
132             print "\t\tpopl %eax" >> out
133         }
134     }
136     print >> out;
137     next;
139 /^[a-zA-Z0-9_]+:$/ {
140     print >> out;
141     if (newfunc)
142     {
143         if (funcname == "main")
144         {
145             print "\t\tcall _Purify_Init" >> out;
146             print "\t\tpushl $10240" >> out;
147             print "\t\tpushl %esp" >> out;
148             print "\t\tcall _Purify_InitStack" >> out;
149             print "\t\taddl $8,%esp" >> out;
150             mainfunc=1;
151         }
153     }
154     next;
156 /^main:$/ {
157     next;
159 /^[ \t]ret$/ {
160     #print >> out; next;
161     print "\t\tcall _Purify_LeaveFunction" >> out
162     if (mainfunc)
163     {
164         print "\t\tcall _Purify_Exit" >> out;
165         mainfunc = 0;
166     }
168 /^[ \t]*.size/ {
169     print >> out;
170     if (section==".text")
171     {
172         print ".section\t.rodata" >> out;
173         print ".L"funcname":" >> out;
174         print "\t\t.string \""funcname"\"" >> out;
175         restore_section(".rodata");
176     }
177     next;
179 /^[ \t]*.type/ {
180     match($2,/^[^,]+/);
181     funcname=substr($2,RSTART,RLENGTH);
182     newfunc=1;
184 /^.stabn 68/ {
185     na=split($2,a,",");
186     line=a[3];
187     if (newfunc)
188     {
189         print "\t\tpushl %ebp" >> out;
190         print "\t\tpushl $"line >> out;
191         print "\t\tpushl $.L"funcname >> out;
192         print "\t\tpushl $.Lfilename" >> out;
193         print "\t\tcall _Purify_EnterFunction" >> out;
194         print "\t\taddl $16,%esp" >> out;
195         newfunc=0;
196     }
197     else
198     {
199         print "\t\tmovl $"line",Purify_Lineno" >> out;
200     }
202 /^.stabs "\/[^"]+",100/ {
203     match($2,/"[^"]+"/);
204     path=substr($2,RSTART+1,RLENGTH-2);
206 /^.stabs "[^\/][^"]+",100/ {
207     match($2,/"[^"]+"/);
208     filename=substr($2,RSTART+1,RLENGTH-2);
209     print >> out;
210     print ".section\t.rodata" >> out;
211     print ".Lfilename:" >> out;
212     print "\t\t.string \""path filename"\"" >> out;
213     restore_section(".rodata");
214     next
216 /^[ \t]*\./ { print >> out; next }
217 /^[ \t]*.*[(][^)]+[)]/ {
218     print "Warning: Unsupported memory access found in line "line" ("NR")"
219     print $0
220     print $0 >> out;
222  { print >> out; }
224 function restore_section(sect) {
225     if (sect != section)
226     {
227         if (section==".text" || section==".data")
228             print section >> out;
229         else if (section!="")
230             print ".section "section >> out;
231     }