Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make it compile on 32bits, to please @degomme
[simgrid.git] / tools / stack-cleaner / clean-stack-filter
1 #!/usr/bin/env perl
2 # Transform assembly in order to clean each stack frame for X86_64.
3
4 use strict;
5 use warnings;
6
7 $SIG{__WARN__} = sub { die @_ };
8
9 # Whether we are still scanning the content of a function:
10 our $scanproc = 0;
11
12 # Save lines of the function:
13 our $lines = "";
14
15 # Size of the stack for this function:
16 our $size = 0;
17
18
19 # Counter for assigning unique ids to labels:
20 our $id=0;
21
22 sub emit_code {
23     my $qsize = $size / 8;
24     my $offset = - $size - 8;
25
26     if($size != 0) {
27       # This is a crude hack to disable the stack cleaning on the main
28       # stack.  It relies on the fact that the main stack is high in
29       # the address space and the other stacks are in the heap (lower).
30       print("\tmovq \$0x7fff00000000, %r11\n");
31       print("\tcmpq %r11, %rsp\n");
32       print("\tjae .Lstack_cleaner_done$id\n");
33
34       # Loop over the stack frame quadwords and zero it:
35       print("\tmovabsq \$$qsize, %r11\n");
36       print(".Lstack_cleaner_loop$id:\n");
37       print("\tmovq    \$0, $offset(%rsp,%r11,8)\n");
38       print("\tsubq    \$1, %r11\n");
39       print("\tjne     .Lstack_cleaner_loop$id\n");
40       print(".Lstack_cleaner_done$id:\n");
41     }
42
43     print $lines;
44
45     $id = $id + 1;
46     $size = 0;
47     $lines = "";
48     $scanproc = 0;
49 }
50
51 while (<>) {
52   if ($scanproc) {
53       $lines = $lines . $_;
54       if (m/^[ \t]*.cfi_endproc$/) {
55           emit_code();
56       } elsif (m/^[ \t]*pushq/) {
57           $size += 8;
58       } elsif (m/^[ \t]*subq[\t *]\$([0-9]*),[ \t]*%rsp$/) {
59           my $val = $1;
60           $val = oct($val) if $val =~ /^0/;
61           $size += $val;
62           emit_code();
63       }
64   } elsif (m/^[ \t]*.cfi_startproc$/) {
65       print $_;
66
67       $scanproc = 1;
68   } else {
69       print $_;
70   }
71 }