Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8c5c01ca1c56a4e11a0cb20d986554526b0073ce
[simgrid.git] / tools / stack-cleaner / clean-stack-filter
1 #!/usr/bin/perl -w
2 # Transform assembly in order to clean each stack frame for X86_64.
3
4 use strict;
5 $SIG{__WARN__} = sub { die @_ };
6
7
8 # Whether we are still scanning the content of a function:
9 our $scanproc = 0;
10
11 # Save lines of the function:
12 our $lines = "";
13
14 # Size of the stack for this function:
15 our $size = 0;
16
17
18 # Counter for assigning unique ids to labels:
19 our $id=0;
20
21 sub emit_code {
22     my $qsize = $size / 8;
23     my $offset = - $size - 8;
24
25     if($size != 0) {
26       print("\tmovabsq \$$qsize, %r11\n");
27       print(".Lstack_cleaner_loop$id:\n");
28       print("\tmovq    \$0, $offset(%rsp,%r11,8)\n");
29       print("\tsubq    \$1, %r11\n");
30       print("\tjne     .Lstack_cleaner_loop$id\n");
31     }
32
33     print $lines;
34
35     $id = $id + 1;
36     $size = 0;
37     $lines = "";
38     $scanproc = 0;
39 }
40
41 while (<>) {
42   if ($scanproc) {
43       $lines = $lines . $_;
44       if (m/^[ \t]*.cfi_endproc$/) {
45           emit_code();
46       } elsif (m/^[ \t]*pushq/) {
47           $size += 8;
48       } elsif (m/^[ \t]*subq[\t *]\$([0-9]*),[ \t]*%rsp$/) {
49           my $val = $1;
50           $val = oct($val) if $val =~ /^0/;
51           $size += $val;
52           emit_code();
53       }
54   } elsif (m/^[ \t]*.cfi_startproc$/) {
55       print $_;
56
57       $scanproc = 1;
58   } else {
59       print $_;
60   }
61 }