2
0

ppc-xlate.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #! /usr/bin/env perl
  2. # Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. my $flavour = shift;
  9. my $output = shift;
  10. open STDOUT,">$output" || die "can't open $output: $!";
  11. my %GLOBALS;
  12. my %TYPES;
  13. my $dotinlocallabels=($flavour=~/linux/)?1:0;
  14. ################################################################
  15. # directives which need special treatment on different platforms
  16. ################################################################
  17. my $type = sub {
  18. my ($dir,$name,$type) = @_;
  19. $TYPES{$name} = $type;
  20. if ($flavour =~ /linux/) {
  21. $name =~ s|^\.||;
  22. ".type $name,$type";
  23. } else {
  24. "";
  25. }
  26. };
  27. my $globl = sub {
  28. my $junk = shift;
  29. my $name = shift;
  30. my $global = \$GLOBALS{$name};
  31. my $type = \$TYPES{$name};
  32. my $ret;
  33. $name =~ s|^\.||;
  34. SWITCH: for ($flavour) {
  35. /aix/ && do { if (!$$type) {
  36. $$type = "\@function";
  37. }
  38. if ($$type =~ /function/) {
  39. $name = ".$name";
  40. }
  41. last;
  42. };
  43. /osx/ && do { $name = "_$name";
  44. last;
  45. };
  46. /linux.*(32|64(le|v2))/
  47. && do { $ret .= ".globl $name";
  48. if (!$$type) {
  49. $ret .= "\n.type $name,\@function";
  50. $$type = "\@function";
  51. }
  52. last;
  53. };
  54. /linux.*64/ && do { $ret .= ".globl $name";
  55. if (!$$type) {
  56. $ret .= "\n.type $name,\@function";
  57. $$type = "\@function";
  58. }
  59. if ($$type =~ /function/) {
  60. $ret .= "\n.section \".opd\",\"aw\"";
  61. $ret .= "\n.align 3";
  62. $ret .= "\n$name:";
  63. $ret .= "\n.quad .$name,.TOC.\@tocbase,0";
  64. $ret .= "\n.previous";
  65. $name = ".$name";
  66. }
  67. last;
  68. };
  69. }
  70. $ret = ".globl $name" if (!$ret);
  71. $$global = $name;
  72. $ret;
  73. };
  74. my $text = sub {
  75. my $ret = ($flavour =~ /aix/) ? ".csect\t.text[PR],7" : ".text";
  76. $ret = ".abiversion 2\n".$ret if ($flavour =~ /linux.*64(le|v2)/);
  77. $ret;
  78. };
  79. my $p2align = sub {
  80. my $ret = ($flavour =~ /aix64-as/) ? "" : ".p2align $line";
  81. $ret;
  82. };
  83. my $machine = sub {
  84. my $junk = shift;
  85. my $arch = shift;
  86. if ($flavour =~ /osx/)
  87. { $arch =~ s/\"//g;
  88. $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any");
  89. }
  90. ".machine $arch";
  91. };
  92. my $size = sub {
  93. if ($flavour =~ /linux/)
  94. { shift;
  95. my $name = shift;
  96. my $real = $GLOBALS{$name} ? \$GLOBALS{$name} : \$name;
  97. my $ret = ".size $$real,.-$$real";
  98. $name =~ s|^\.||;
  99. if ($$real ne $name) {
  100. $ret .= "\n.size $name,.-$$real";
  101. }
  102. $ret;
  103. }
  104. else
  105. { ""; }
  106. };
  107. my $asciz = sub {
  108. shift;
  109. my $line = join(",",@_);
  110. if ($line =~ /^"(.*)"$/)
  111. { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; }
  112. else
  113. { ""; }
  114. };
  115. my $quad = sub {
  116. shift;
  117. my @ret;
  118. my ($hi,$lo);
  119. for (@_) {
  120. if (/^0x([0-9a-f]*?)([0-9a-f]{1,8})$/io)
  121. { $hi=$1?"0x$1":"0"; $lo="0x$2"; }
  122. elsif (/^([0-9]+)$/o)
  123. { $hi=$1>>32; $lo=$1&0xffffffff; } # error-prone with 32-bit perl
  124. else
  125. { $hi=undef; $lo=$_; }
  126. if (defined($hi))
  127. { push(@ret,$flavour=~/le$/o?".long\t$lo,$hi":".long\t$hi,$lo"); }
  128. else
  129. { push(@ret,".quad $lo"); }
  130. }
  131. join("\n",@ret);
  132. };
  133. ################################################################
  134. # vector register number hacking
  135. ################################################################
  136. # It is convenient to be able to set a variable like:
  137. # my $foo = "v33";
  138. # and use this in different contexts where:
  139. # * a VSR (Vector-Scaler Register) number (i.e. "v33") is required
  140. # * a VR (Vector Register) number (i.e. "v1") is required
  141. # Map VSR numbering to VR number for certain vector instructions.
  142. # vs<N> -> v<N-32> if N > 32
  143. sub vsr2vr1 {
  144. my $in = shift;
  145. my ($prefix, $reg) = ($in =~ m/(\D*)(\d+)/);
  146. my $n = int($reg);
  147. if ($n >= 32) {
  148. $n -= 32;
  149. }
  150. return "${prefix}${n}";
  151. }
  152. # As above for first $num register args, returns list
  153. sub _vsr2vr {
  154. my $num = shift;
  155. my @rest = @_;
  156. my @subst = splice(@rest, 0, $num);
  157. @subst = map { vsr2vr1($_); } @subst;
  158. return (@subst, @rest);
  159. }
  160. # As above but 1st arg ($f) is extracted and reinserted after
  161. # processing so that it can be ignored by a code generation function
  162. # that consumes the result
  163. sub vsr2vr_args {
  164. my $num = shift;
  165. my $f = shift;
  166. my @out = _vsr2vr($num, @_);
  167. return ($f, @out);
  168. }
  169. # As above but 1st arg is mnemonic, return formatted instruction
  170. sub vsr2vr {
  171. my $mnemonic = shift;
  172. my $num = shift;
  173. my $f = shift;
  174. my @out = _vsr2vr($num, @_);
  175. " ${mnemonic}${f} " . join(",", @out);
  176. }
  177. # ISA 2.03
  178. my $vsel = sub { vsr2vr("vsel", 4, @_); };
  179. my $vsl = sub { vsr2vr("vsl", 3, @_); };
  180. my $vspltisb = sub { vsr2vr("vspltisb", 1, @_); };
  181. my $vspltisw = sub { vsr2vr("vspltisw", 1, @_); };
  182. my $vsr = sub { vsr2vr("vsr", 3, @_); };
  183. my $vsro = sub { vsr2vr("vsro", 3, @_); };
  184. # ISA 3.0
  185. my $lxsd = sub { vsr2vr("lxsd", 1, @_); };
  186. ################################################################
  187. # simplified mnemonics not handled by at least one assembler
  188. ################################################################
  189. my $cmplw = sub {
  190. my $f = shift;
  191. my $cr = 0; $cr = shift if ($#_>1);
  192. # Some out-of-date 32-bit GNU assembler just can't handle cmplw...
  193. ($flavour =~ /linux.*32/) ?
  194. " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 :
  195. " cmplw ".join(',',$cr,@_);
  196. };
  197. my $bdnz = sub {
  198. my $f = shift;
  199. my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint
  200. " bc $bo,0,".shift;
  201. } if ($flavour!~/linux/);
  202. my $bltlr = sub {
  203. my $f = shift;
  204. my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint
  205. ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
  206. " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 :
  207. " bclr $bo,0";
  208. };
  209. my $bnelr = sub {
  210. my $f = shift;
  211. my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint
  212. ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
  213. " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 :
  214. " bclr $bo,2";
  215. };
  216. my $beqlr = sub {
  217. my $f = shift;
  218. my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint
  219. ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints
  220. " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 :
  221. " bclr $bo,2";
  222. };
  223. # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two
  224. # arguments is 64, with "operand out of range" error.
  225. my $extrdi = sub {
  226. my ($f,$ra,$rs,$n,$b) = @_;
  227. $b = ($b+$n)&63; $n = 64-$n;
  228. " rldicl $ra,$rs,$b,$n";
  229. };
  230. my $vmr = sub {
  231. my ($f,$vx,$vy) = @_;
  232. " vor $vx,$vy,$vy";
  233. };
  234. # Some ABIs specify vrsave, special-purpose register #256, as reserved
  235. # for system use.
  236. my $no_vrsave = ($flavour =~ /aix|linux64(le|v2)/);
  237. my $mtspr = sub {
  238. my ($f,$idx,$ra) = @_;
  239. if ($idx == 256 && $no_vrsave) {
  240. " or $ra,$ra,$ra";
  241. } else {
  242. " mtspr $idx,$ra";
  243. }
  244. };
  245. my $mfspr = sub {
  246. my ($f,$rd,$idx) = @_;
  247. if ($idx == 256 && $no_vrsave) {
  248. " li $rd,-1";
  249. } else {
  250. " mfspr $rd,$idx";
  251. }
  252. };
  253. # PowerISA 2.06 stuff
  254. sub vsxmem_op {
  255. my ($f, $vrt, $ra, $rb, $op) = @_;
  256. " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|($rb<<11)|($op*2+1);
  257. }
  258. # made-up unaligned memory reference AltiVec/VMX instructions
  259. my $lvx_u = sub { vsxmem_op(@_, 844); }; # lxvd2x
  260. my $stvx_u = sub { vsxmem_op(@_, 972); }; # stxvd2x
  261. my $lvdx_u = sub { vsxmem_op(@_, 588); }; # lxsdx
  262. my $stvdx_u = sub { vsxmem_op(@_, 716); }; # stxsdx
  263. my $lvx_4w = sub { vsxmem_op(@_, 780); }; # lxvw4x
  264. my $stvx_4w = sub { vsxmem_op(@_, 908); }; # stxvw4x
  265. my $lvx_splt = sub { vsxmem_op(@_, 332); }; # lxvdsx
  266. # VSX instruction[s] masqueraded as made-up AltiVec/VMX
  267. my $vpermdi = sub { # xxpermdi
  268. my ($f, $vrt, $vra, $vrb, $dm) = @_;
  269. $dm = oct($dm) if ($dm =~ /^0/);
  270. " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($dm<<8)|(10<<3)|7;
  271. };
  272. my $vxxlor = sub { # xxlor
  273. my ($f, $vrt, $vra, $vrb) = @_;
  274. " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|(146<<3)|6;
  275. };
  276. my $vxxlorc = sub { # xxlor
  277. my ($f, $vrt, $vra, $vrb) = @_;
  278. " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|(146<<3)|1;
  279. };
  280. # PowerISA 2.07 stuff
  281. sub vcrypto_op {
  282. my ($f, $vrt, $vra, $vrb, $op) = vsr2vr_args(3, @_);
  283. " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|$op;
  284. }
  285. sub vfour {
  286. my ($f, $vrt, $vra, $vrb, $vrc, $op) = @_;
  287. " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($vrc<<6)|$op;
  288. };
  289. sub vfour_vsr {
  290. my ($f, $vrt, $vra, $vrb, $vrc, $op) = vsr2vr_args(4, @_);
  291. " .long ".sprintf "0x%X",(4<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($vrc<<6)|$op;
  292. };
  293. my $vcipher = sub { vcrypto_op(@_, 1288); };
  294. my $vcipherlast = sub { vcrypto_op(@_, 1289); };
  295. my $vncipher = sub { vcrypto_op(@_, 1352); };
  296. my $vncipherlast= sub { vcrypto_op(@_, 1353); };
  297. my $vsbox = sub { vcrypto_op(@_, 0, 1480); };
  298. my $vshasigmad = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1730); };
  299. my $vshasigmaw = sub { my ($st,$six)=splice(@_,-2); vcrypto_op(@_, $st<<4|$six, 1666); };
  300. my $vpmsumb = sub { vcrypto_op(@_, 1032); };
  301. my $vpmsumd = sub { vcrypto_op(@_, 1224); };
  302. my $vpmsubh = sub { vcrypto_op(@_, 1096); };
  303. my $vpmsumw = sub { vcrypto_op(@_, 1160); };
  304. # These are not really crypto, but vcrypto_op template works
  305. my $vaddudm = sub { vcrypto_op(@_, 192); };
  306. my $vadduqm = sub { vcrypto_op(@_, 256); };
  307. my $vmuleuw = sub { vcrypto_op(@_, 648); };
  308. my $vmulouw = sub { vcrypto_op(@_, 136); };
  309. my $vrld = sub { vcrypto_op(@_, 196); };
  310. my $vsld = sub { vcrypto_op(@_, 1476); };
  311. my $vsrd = sub { vcrypto_op(@_, 1732); };
  312. my $vsubudm = sub { vcrypto_op(@_, 1216); };
  313. my $vaddcuq = sub { vcrypto_op(@_, 320); };
  314. my $vaddeuqm = sub { vfour_vsr(@_,60); };
  315. my $vaddecuq = sub { vfour_vsr(@_,61); };
  316. my $vmrgew = sub { vfour_vsr(@_,0,1932); };
  317. my $vmrgow = sub { vfour_vsr(@_,0,1676); };
  318. my $mtsle = sub {
  319. my ($f, $arg) = @_;
  320. " .long ".sprintf "0x%X",(31<<26)|($arg<<21)|(147*2);
  321. };
  322. # VSX instructions masqueraded as AltiVec/VMX
  323. my $mtvrd = sub {
  324. my ($f, $vrt, $ra) = @_;
  325. " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(179<<1)|1;
  326. };
  327. my $mtvrwz = sub {
  328. my ($f, $vrt, $ra) = @_;
  329. " .long ".sprintf "0x%X",(31<<26)|($vrt<<21)|($ra<<16)|(243<<1)|1;
  330. };
  331. my $lvwzx_u = sub { vsxmem_op(@_, 12); }; # lxsiwzx
  332. my $stvwx_u = sub { vsxmem_op(@_, 140); }; # stxsiwx
  333. # PowerISA 3.0 stuff
  334. my $maddhdu = sub { vfour(@_,49); };
  335. my $maddld = sub { vfour(@_,51); };
  336. my $darn = sub {
  337. my ($f, $rt, $l) = @_;
  338. " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($l<<16)|(755<<1);
  339. };
  340. my $iseleq = sub {
  341. my ($f, $rt, $ra, $rb) = @_;
  342. " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|(2<<6)|30;
  343. };
  344. # VSX instruction[s] masqueraded as made-up AltiVec/VMX
  345. my $vspltib = sub { # xxspltib
  346. my ($f, $vrt, $imm8) = @_;
  347. $imm8 = oct($imm8) if ($imm8 =~ /^0/);
  348. $imm8 &= 0xff;
  349. " .long ".sprintf "0x%X",(60<<26)|($vrt<<21)|($imm8<<11)|(360<<1)|1;
  350. };
  351. # PowerISA 3.0B stuff
  352. my $addex = sub {
  353. my ($f, $rt, $ra, $rb, $cy) = @_; # only cy==0 is specified in 3.0B
  354. " .long ".sprintf "0x%X",(31<<26)|($rt<<21)|($ra<<16)|($rb<<11)|($cy<<9)|(170<<1);
  355. };
  356. my $vmsumudm = sub { vfour_vsr(@_, 35); };
  357. # PowerISA 3.1 stuff
  358. my $brd = sub {
  359. my ($f, $ra, $rs) = @_;
  360. " .long ".sprintf "0x%X",(31<<26)|($rs<<21)|($ra<<16)|(187<<1);
  361. };
  362. my $vsrq = sub { vcrypto_op(@_, 517); };
  363. while($line=<>) {
  364. $line =~ s|[#!;].*$||; # get rid of asm-style comments...
  365. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  366. $line =~ s|^\s+||; # ... and skip whitespaces in beginning...
  367. $line =~ s|\s+$||; # ... and at the end
  368. {
  369. $line =~ s|\.L(\w+)|L$1|g; # common denominator for Locallabel
  370. $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels);
  371. }
  372. {
  373. $line =~ s|(^[\.\w]+)\:\s*||;
  374. my $label = $1;
  375. if ($label) {
  376. my $xlated = ($GLOBALS{$label} or $label);
  377. print "$xlated:";
  378. if ($flavour =~ /linux.*64(le|v2)/) {
  379. if ($TYPES{$label} =~ /function/) {
  380. printf "\n.localentry %s,0\n",$xlated;
  381. }
  382. }
  383. }
  384. }
  385. {
  386. $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||;
  387. my $c = $1; $c = "\t" if ($c eq "");
  388. my $mnemonic = $2;
  389. my $f = $3;
  390. my $opcode = eval("\$$mnemonic");
  391. $line =~ s/\b(c?[rf]|v|vs)([0-9]+)\b/$2/g if ($c ne "." and $flavour !~ /osx/);
  392. if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(/,\s*/,$line)); }
  393. elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; }
  394. }
  395. print $line if ($line);
  396. print "\n";
  397. }
  398. close STDOUT or die "error closing STDOUT: $!";