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