Debian Codegraph @

external/boringssl/crypto/perlasm/arm-xlate.pl

raw
1 #!/usr/bin/env perl
2
3 # ARM assembler distiller by <appro>.
4
5 my $flavour = shift;
6 my $output = shift;
7 open STDOUT,">$output" || die "can't open $output: $!";
8
9 $flavour = "linux32" if (!$flavour or $flavour eq "void");
10
11 my %GLOBALS;
12 my $dotinlocallabels=($flavour=~/linux/)?1:0;
13
14 ################################################################
15 # directives which need special treatment on different platforms
16 ################################################################
17 my $arch = sub {
18 if ($flavour =~ /linux/) { ".arch\t".join(',',@_); }
19 else { ""; }
20 };
21 my $fpu = sub {
22 if ($flavour =~ /linux/) { ".fpu\t".join(',',@_); }
23 else { ""; }
24 };
25 my $hidden = sub {
26 if ($flavour =~ /ios/) { ".private_extern\t".join(',',@_); }
27 else { ".hidden\t".join(',',@_); }
28 };
29 my $comm = sub {
30 my @args = split(/,\s*/,shift);
31 my $name = @args[0];
32 my $global = \$GLOBALS{$name};
33 my $ret;
34
35 if ($flavour =~ /ios32/) {
36 $ret = ".comm\t_$name,@args[1]\n";
37 $ret .= ".non_lazy_symbol_pointer\n";
38 $ret .= "$name:\n";
39 $ret .= ".indirect_symbol\t_$name\n";
40 $ret .= ".long\t0";
41 $name = "_$name";
42 } else { $ret = ".comm\t".join(',',@args); }
43
44 $$global = $name;
45 $ret;
46 };
47 my $globl = sub {
48 my $name = shift;
49 my $global = \$GLOBALS{$name};
50 my $ret;
51
52 SWITCH: for ($flavour) {
53 /ios/ && do { $name = "_$name";
54 last;
55 };
56 }
57
58 $ret = ".globl $name\n";
59 # All symbols in assembly files are hidden.
60 $ret .= &$hidden($name);
61 $$global = $name;
62 $ret;
63 };
64 my $global = $globl;
65 my $extern = sub {
66 &$globl(@_);
67 return; # return nothing
68 };
69 my $type = sub {
70 if ($flavour =~ /linux/) { ".type\t".join(',',@_); }
71 else { ""; }
72 };
73 my $size = sub {
74 if ($flavour =~ /linux/) { ".size\t".join(',',@_); }
75 else { ""; }
76 };
77 my $inst = sub {
78 if ($flavour =~ /linux/) { ".inst\t".join(',',@_); }
79 else { ".long\t".join(',',@_); }
80 };
81 my $asciz = sub {
82 my $line = join(",",@_);
83 if ($line =~ /^"(.*)"$/)
84 { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
85 else
86 { ""; }
87 };
88
89 sub range {
90 my ($r,$sfx,$start,$end) = @_;
91
92 join(",",map("$r$_$sfx",($start..$end)));
93 }
94
95 sub expand_line {
96 my $line = shift;
97 my @ret = ();
98
99 pos($line)=0;
100
101 while ($line =~ m/\G[^@\/\{\"]*/g) {
102 if ($line =~ m/\G(@|\/\/|$)/gc) {
103 last;
104 }
105 elsif ($line =~ m/\G\{/gc) {
106 my $saved_pos = pos($line);
107 $line =~ s/\G([rdqv])([0-9]+)([^\-]*)\-\1([0-9]+)\3/range($1,$3,$2,$4)/e;
108 pos($line) = $saved_pos;
109 $line =~ m/\G[^\}]*\}/g;
110 }
111 elsif ($line =~ m/\G\"/gc) {
112 $line =~ m/\G[^\"]*\"/g;
113 }
114 }
115
116 $line =~ s/\b(\w+)/$GLOBALS{$1} or $1/ge;
117
118 return $line;
119 }
120
121 print "#if defined(__arm__)\n" if ($flavour eq "linux32");
122 print "#if defined(__aarch64__)\n" if ($flavour eq "linux64");
123
124 while($line=<>) {
125
126 if ($line =~ m/^\s*(#|@|\/\/)/) { print $line; next; }
127
128 $line =~ s|/\*.*\*/||; # get rid of C-style comments...
129 $line =~ s|^\s+||; # ... and skip white spaces in beginning...
130 $line =~ s|\s+$||; # ... and at the end
131
132 {
133 $line =~ s|[\b\.]L(\w{2,})|L$1|g; # common denominator for Locallabel
134 $line =~ s|\bL(\w{2,})|\.L$1|g if ($dotinlocallabels);
135 }
136
137 {
138 $line =~ s|(^[\.\w]+)\:\s*||;
139 my $label = $1;
140 if ($label) {
141 printf "%s:",($GLOBALS{$label} or $label);
142 }
143 }
144
145 if ($line !~ m/^[#@]/) {
146 $line =~ s|^\s*(\.?)(\S+)\s*||;
147 my $c = $1; $c = "\t" if ($c eq "");
148 my $mnemonic = $2;
149 my $opcode;
150 if ($mnemonic =~ m/([^\.]+)\.([^\.]+)/) {
151 $opcode = eval("\$$1_$2");
152 } else {
153 $opcode = eval("\$$mnemonic");
154 }
155
156 my $arg=expand_line($line);
157
158 if (ref($opcode) eq 'CODE') {
159 $line = &$opcode($arg);
160 } elsif ($mnemonic) {
161 $line = $c.$mnemonic;
162 $line.= "\t$arg" if ($arg ne "");
163 }
164 }
165
166 print $line if ($line);
167 print "\n";
168 }
169
170 print "#endif\n" if ($flavour eq "linux32" || $flavour eq "linux64");
171
172 close STDOUT;