fed56e3772a3fa0a0a2c5b62e6ef57a2ba1c0006
[openssl.git] / crypto / perlasm / ppc-xlate.pl
1 #!/usr/bin/env perl
2
3 # PowerPC assembler distiller by <appro>.
4
5 my $output = shift;
6 open STDOUT,">$output" || die "can't open $output: $!";
7
8 my $flavour = $output;
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 .= ".globl $name\n";
38                                 $ret .= ".align 3\n";
39                                 $ret .= "$name:\n";
40                                 $ret .= ".quad  .$name,.TOC.\@tocbase,0\n";
41                                 $ret .= ".size  $name,24\n";
42                                 $ret .= ".previous\n";
43
44                                 $name = ".$name";
45                                 last;
46                               };
47     }
48
49     $ret = ".globl      $name" if (!$ret);
50     $$global = $name;
51     $ret;
52 };
53 my $text = sub {
54     ($flavour =~ /aix/) ? ".csect" : ".text";
55 };
56 my $machine = sub {
57     my $junk = shift;
58     my $arch = shift;
59     if ($flavour =~ /osx/)
60     {   $arch =~ s/\"//g;
61         $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
62     }
63     ".machine   $arch";
64 };
65 my $asciz = sub {
66     shift;
67     my $line = join(",",@_);
68     if ($line =~ /^"(.*)"$/)
69     {   ".byte  " . join(",",unpack("C*",$1),0) . "\n.align     2";     }
70     else
71     {   "";     }
72 };
73
74 ################################################################
75 # simplified mnemonics not handled by at least one assembler
76 ################################################################
77 my $cmplw = sub {
78     my $f = shift;
79     my $cr = 0; $cr = shift if ($#_>1);
80     # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
81     ($flavour =~ /linux.*32/) ?
82         "       .long   ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
83         "       cmplw   ".join(',',$cr,@_);
84 };
85 my $bdnz = sub {
86     my $f = shift;
87     my $bo = $f=~/[\+\-]/ ? 17 : 16;
88     "   bc      $bo,0,".shift;
89 };
90
91 while($line=<>) {
92
93     $line =~ s|[#!;].*$||;      # get rid of asm-style comments...
94     $line =~ s|/\*.*\*/||;      # ... and C-style comments...
95     $line =~ s|^\s+||;          # ... and skip white spaces in beginning...
96     $line =~ s|\s+$||;          # ... and at the end
97
98     {
99         $line =~ s|\b\.L(\w+)|L$1|g;    # common denominator for Locallabel
100         $line =~ s|\bL(\w+)|\.L$1|g     if ($dotinlocallabels);
101     }
102
103     {
104         $line =~ s|(^[\.\w]+)\:\s*||;
105         my $label = $1;
106         printf "%s:",($GLOBALS{$label} or $label) if ($label);
107     }
108
109     {
110         $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
111         my $c = $1; $c = "\t" if ($c eq "");
112         my $mnemonic = $2;
113         my $f = $3;
114         my $opcode = eval("\$$mnemonic");
115         $line =~ s|\bc?r([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/);
116         if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
117         elsif ($mnemonic)           { $line = $c.$mnemonic.$f."\t".$line; }
118     }
119
120     print $line if ($line);
121     print "\n";
122 }
123
124 close STDOUT;