Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / perlasm / ppc-xlate.pl
1 #!/usr/bin/env perl
2
3 # PowerPC assembler distiller by <appro>.
4
5 my $flavour = shift;
6 my $output = shift;
7 open STDOUT,">$output" || die "can't open $output: $!";
8
9 my %GLOBALS;
10 my $dotinlocallabels=($flavour=~/linux/)?1:0;
11
12 ################################################################
13 # directives which need special treatment on different platforms
14 ################################################################
15 my $globl = sub {
16     my $junk = shift;
17     my $name = shift;
18     my $global = \$GLOBALS{$name};
19     my $ret;
20
21     $name =~ s|^[\.\_]||;
22  
23     SWITCH: for ($flavour) {
24         /aix/           && do { $name = ".$name";
25                                 last;
26                               };
27         /osx/           && do { $name = "_$name";
28                                 last;
29                               };
30         /linux.*(32|64le)/
31                         && do { $ret .= ".globl $name\n";
32                                 $ret .= ".type  $name,\@function";
33                                 last;
34                               };
35         /linux.*64/     && do { $ret .= ".globl $name\n";
36                                 $ret .= ".type  $name,\@function\n";
37                                 $ret .= ".section       \".opd\",\"aw\"\n";
38                                 $ret .= ".align 3\n";
39                                 $ret .= "$name:\n";
40                                 $ret .= ".quad  .$name,.TOC.\@tocbase,0\n";
41                                 $ret .= ".previous\n";
42
43                                 $name = ".$name";
44                                 last;
45                               };
46     }
47
48     $ret = ".globl      $name" if (!$ret);
49     $$global = $name;
50     $ret;
51 };
52 my $text = sub {
53     my $ret = ($flavour =~ /aix/) ? ".csect" : ".text";
54     $ret = ".abiversion 2\n".$ret       if ($flavour =~ /linux.*64le/);
55     $ret;
56 };
57 my $machine = sub {
58     my $junk = shift;
59     my $arch = shift;
60     if ($flavour =~ /osx/)
61     {   $arch =~ s/\"//g;
62         $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
63     }
64     ".machine   $arch";
65 };
66 my $size = sub {
67     if ($flavour =~ /linux/)
68     {   shift;
69         my $name = shift; $name =~ s|^[\.\_]||;
70         my $ret  = ".size       $name,.-".($flavour=~/64$/?".":"").$name;
71         $ret .= "\n.size        .$name,.-.$name" if ($flavour=~/64$/);
72         $ret;
73     }
74     else
75     {   "";     }
76 };
77 my $asciz = sub {
78     shift;
79     my $line = join(",",@_);
80     if ($line =~ /^"(.*)"$/)
81     {   ".byte  " . join(",",unpack("C*",$1),0) . "\n.align     2";     }
82     else
83     {   "";     }
84 };
85
86 ################################################################
87 # simplified mnemonics not handled by at least one assembler
88 ################################################################
89 my $cmplw = sub {
90     my $f = shift;
91     my $cr = 0; $cr = shift if ($#_>1);
92     # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
93     ($flavour =~ /linux.*32/) ?
94         "       .long   ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
95         "       cmplw   ".join(',',$cr,@_);
96 };
97 my $bdnz = sub {
98     my $f = shift;
99     my $bo = $f=~/[\+\-]/ ? 16+9 : 16;  # optional "to be taken" hint
100     "   bc      $bo,0,".shift;
101 } if ($flavour!~/linux/);
102 my $bltlr = sub {
103     my $f = shift;
104     my $bo = $f=~/\-/ ? 12+2 : 12;      # optional "not to be taken" hint
105     ($flavour =~ /linux/) ?             # GNU as doesn't allow most recent hints
106         "       .long   ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
107         "       bclr    $bo,0";
108 };
109 my $bnelr = sub {
110     my $f = shift;
111     my $bo = $f=~/\-/ ? 4+2 : 4;        # optional "not to be taken" hint
112     ($flavour =~ /linux/) ?             # GNU as doesn't allow most recent hints
113         "       .long   ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
114         "       bclr    $bo,2";
115 };
116 my $beqlr = sub {
117     my $f = shift;
118     my $bo = $f=~/-/ ? 12+2 : 12;       # optional "not to be taken" hint
119     ($flavour =~ /linux/) ?             # GNU as doesn't allow most recent hints
120         "       .long   ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
121         "       bclr    $bo,2";
122 };
123 # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
124 # arguments is 64, with "operand out of range" error.
125 my $extrdi = sub {
126     my ($f,$ra,$rs,$n,$b) = @_;
127     $b = ($b+$n)&63; $n = 64-$n;
128     "   rldicl  $ra,$rs,$b,$n";
129 };
130
131 while($line=<>) {
132
133     $line =~ s|[#!;].*$||;      # get rid of asm-style comments...
134     $line =~ s|/\*.*\*/||;      # ... and C-style comments...
135     $line =~ s|^\s+||;          # ... and skip white spaces in beginning...
136     $line =~ s|\s+$||;          # ... and at the end
137
138     {
139         $line =~ s|\b\.L(\w+)|L$1|g;    # common denominator for Locallabel
140         $line =~ s|\bL(\w+)|\.L$1|g     if ($dotinlocallabels);
141     }
142
143     {
144         $line =~ s|(^[\.\w]+)\:\s*||;
145         my $label = $1;
146         if ($label) {
147             printf "%s:",($GLOBALS{$label} or $label);
148             printf "\n.localentry\t$GLOBALS{$label},0"  if ($GLOBALS{$label} && $flavour =~ /linux.*64le/);
149         }
150     }
151
152     {
153         $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
154         my $c = $1; $c = "\t" if ($c eq "");
155         my $mnemonic = $2;
156         my $f = $3;
157         my $opcode = eval("\$$mnemonic");
158         $line =~ s|\bc?[rf]([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/);
159         if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
160         elsif ($mnemonic)           { $line = $c.$mnemonic.$f."\t".$line; }
161     }
162
163     print $line if ($line);
164     print "\n";
165 }
166
167 close STDOUT;