31876e56b252849f67a6564f28a2cdef2b0ed255
[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=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                                 $dotinlocallabels = 1;
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 .= ".globl $name\n";
39                                 $ret .= ".align 3\n";
40                                 $ret .= "$name:\n";
41                                 $ret .= ".quad  .$name,.TOC.\@tocbase,0\n";
42                                 $ret .= ".size  $name,24\n";
43                                 $ret .= ".previous\n";
44
45                                 $name = ".$name";
46                                 $dotinlocallabels = 1;
47                                 last;
48                               };
49     }
50
51     $ret = ".globl      $name" if (!$ret);
52     $$global = $name;
53     $ret;
54 };
55 my $machine = sub {
56     my $junk = shift;
57     my $arch = shift;
58     $arch = "ppc970" if ($arch eq "any" and $flavour =~ /osx/);
59     ".machine   $arch";
60 };
61
62 ################################################################
63 # simplified mnemonics not handled by at least one assembler
64 ################################################################
65 my $cmplw = sub {
66     my $f = shift;
67     my $cr = 0; $cr = shift if ($#_>1);
68     "   cmpl$f  ".join(',',$cr,0,@_);
69 };
70 my $cmpld = sub {
71     my $f = shift;
72     my $cr = 0; $cr = shift if ($#_>1);
73     "   cmpl$f  ".join(',',$cr,1,@_);
74 };
75 my $bdnz = sub {
76     my $f = shift;
77     my $bo = $f=~/[\+\-]/ ? 17 : 16;
78     "   bc      $bo,0,".shift;
79 };
80
81 while($line=<>) {
82
83     $line =~ s|[#!;].*$||;      # get rid of asm-style comments...
84     $line =~ s|/\*.*\*/||;      # ... and C-style comments...
85     $line =~ s|^\s+||;          # ... and skip white spaces in beginning...
86     $line =~ s|\s+$||;          # ... and at the end
87
88     {
89         $line =~ s|\b\.L(\w+)|L$1|g;    # common denominator for Locallabel
90         $line =~ s|\bL(\w+)|\.L$1|g     if ($dotinlocallabels);
91     }
92
93     {
94         $line =~ s|(^[\.\w]+)\:\s*||;
95         my $label = $1;
96         printf "%s:",($GLOBALS{$label} or $label) if ($label);
97     }
98
99     {
100         $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
101         my $c = $1; $c = "\t" if ($c eq "");
102         my $mnemonic = $2;
103         my $f = $3;
104         my $opcode = eval("\$$mnemonic");
105         if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); }
106         elsif ($mnemonic)           { $line = $c.$mnemonic.$f."\t".$line; }
107     }
108
109     print $line if ($line);
110     print "\n";
111 }
112
113 close STDOUT;