diff options
Diffstat (limited to 'src/lib/libcrypto/perlasm/x86unix.pl')
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86unix.pl | 808 |
1 files changed, 808 insertions, 0 deletions
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl new file mode 100644 index 0000000000..ae8f0964dc --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86unix.pl | |||
| @@ -0,0 +1,808 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | package x86unix; # GAS actually... | ||
| 4 | |||
| 5 | $label="L000"; | ||
| 6 | $const=""; | ||
| 7 | $constl=0; | ||
| 8 | |||
| 9 | $align=($main'aout)?"4":"16"; | ||
| 10 | $under=($main'aout or $main'coff)?"_":""; | ||
| 11 | $dot=($main'aout)?"":"."; | ||
| 12 | $com_start="#" if ($main'aout or $main'coff); | ||
| 13 | |||
| 14 | sub main'asm_init_output { @out=(); } | ||
| 15 | sub main'asm_get_output { return(@out); } | ||
| 16 | sub main'get_labels { return(@labels); } | ||
| 17 | sub main'external_label { push(@labels,@_); } | ||
| 18 | |||
| 19 | if ($main'openbsd) | ||
| 20 | { | ||
| 21 | $com_start='/*'; | ||
| 22 | $com_end='*/'; | ||
| 23 | } | ||
| 24 | |||
| 25 | if ($main'cpp) | ||
| 26 | { | ||
| 27 | $align="ALIGN"; | ||
| 28 | $under=""; | ||
| 29 | $com_start='/*'; | ||
| 30 | $com_end='*/'; | ||
| 31 | } | ||
| 32 | |||
| 33 | %lb=( 'eax', '%al', | ||
| 34 | 'ebx', '%bl', | ||
| 35 | 'ecx', '%cl', | ||
| 36 | 'edx', '%dl', | ||
| 37 | 'ax', '%al', | ||
| 38 | 'bx', '%bl', | ||
| 39 | 'cx', '%cl', | ||
| 40 | 'dx', '%dl', | ||
| 41 | ); | ||
| 42 | |||
| 43 | %hb=( 'eax', '%ah', | ||
| 44 | 'ebx', '%bh', | ||
| 45 | 'ecx', '%ch', | ||
| 46 | 'edx', '%dh', | ||
| 47 | 'ax', '%ah', | ||
| 48 | 'bx', '%bh', | ||
| 49 | 'cx', '%ch', | ||
| 50 | 'dx', '%dh', | ||
| 51 | ); | ||
| 52 | |||
| 53 | %regs=( 'eax', '%eax', | ||
| 54 | 'ebx', '%ebx', | ||
| 55 | 'ecx', '%ecx', | ||
| 56 | 'edx', '%edx', | ||
| 57 | 'esi', '%esi', | ||
| 58 | 'edi', '%edi', | ||
| 59 | 'ebp', '%ebp', | ||
| 60 | 'esp', '%esp', | ||
| 61 | |||
| 62 | 'mm0', '%mm0', | ||
| 63 | 'mm1', '%mm1', | ||
| 64 | 'mm2', '%mm2', | ||
| 65 | 'mm3', '%mm3', | ||
| 66 | 'mm4', '%mm4', | ||
| 67 | 'mm5', '%mm5', | ||
| 68 | 'mm6', '%mm6', | ||
| 69 | 'mm7', '%mm7', | ||
| 70 | |||
| 71 | 'xmm0', '%xmm0', | ||
| 72 | 'xmm1', '%xmm1', | ||
| 73 | 'xmm2', '%xmm2', | ||
| 74 | 'xmm3', '%xmm3', | ||
| 75 | 'xmm4', '%xmm4', | ||
| 76 | 'xmm5', '%xmm5', | ||
| 77 | 'xmm6', '%xmm6', | ||
| 78 | 'xmm7', '%xmm7', | ||
| 79 | ); | ||
| 80 | |||
| 81 | %reg_val=( | ||
| 82 | 'eax', 0x00, | ||
| 83 | 'ebx', 0x03, | ||
| 84 | 'ecx', 0x01, | ||
| 85 | 'edx', 0x02, | ||
| 86 | 'esi', 0x06, | ||
| 87 | 'edi', 0x07, | ||
| 88 | 'ebp', 0x05, | ||
| 89 | 'esp', 0x04, | ||
| 90 | ); | ||
| 91 | |||
| 92 | sub main'LB | ||
| 93 | { | ||
| 94 | (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n"; | ||
| 95 | return($lb{$_[0]}); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub main'HB | ||
| 99 | { | ||
| 100 | (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n"; | ||
| 101 | return($hb{$_[0]}); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub main'DWP | ||
| 105 | { | ||
| 106 | local($addr,$reg1,$reg2,$idx)=@_; | ||
| 107 | |||
| 108 | $ret=""; | ||
| 109 | $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/; | ||
| 110 | $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 111 | $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 112 | $ret.=$addr if ($addr ne "") && ($addr ne 0); | ||
| 113 | if ($reg2 ne "") | ||
| 114 | { | ||
| 115 | if($idx ne "" && $idx != 0) | ||
| 116 | { $ret.="($reg1,$reg2,$idx)"; } | ||
| 117 | else | ||
| 118 | { $ret.="($reg1,$reg2)"; } | ||
| 119 | } | ||
| 120 | elsif ($reg1 ne "") | ||
| 121 | { $ret.="($reg1)" } | ||
| 122 | return($ret); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub main'QWP | ||
| 126 | { | ||
| 127 | return(&main'DWP(@_)); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub main'BP | ||
| 131 | { | ||
| 132 | return(&main'DWP(@_)); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub main'BC | ||
| 136 | { | ||
| 137 | return @_; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub main'DWC | ||
| 141 | { | ||
| 142 | return @_; | ||
| 143 | } | ||
| 144 | |||
| 145 | #sub main'BP | ||
| 146 | # { | ||
| 147 | # local($addr,$reg1,$reg2,$idx)=@_; | ||
| 148 | # | ||
| 149 | # $ret=""; | ||
| 150 | # | ||
| 151 | # $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/; | ||
| 152 | # $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 153 | # $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 154 | # $ret.=$addr if ($addr ne "") && ($addr ne 0); | ||
| 155 | # if ($reg2 ne "") | ||
| 156 | # { $ret.="($reg1,$reg2,$idx)"; } | ||
| 157 | # else | ||
| 158 | # { $ret.="($reg1)" } | ||
| 159 | # return($ret); | ||
| 160 | # } | ||
| 161 | |||
| 162 | sub main'mov { &out2("movl",@_); } | ||
| 163 | sub main'movb { &out2("movb",@_); } | ||
| 164 | sub main'and { &out2("andl",@_); } | ||
| 165 | sub main'or { &out2("orl",@_); } | ||
| 166 | sub main'shl { &out2("sall",@_); } | ||
| 167 | sub main'shr { &out2("shrl",@_); } | ||
| 168 | sub main'xor { &out2("xorl",@_); } | ||
| 169 | sub main'xorb { &out2("xorb",@_); } | ||
| 170 | sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); } | ||
| 171 | sub main'adc { &out2("adcl",@_); } | ||
| 172 | sub main'sub { &out2("subl",@_); } | ||
| 173 | sub main'sbb { &out2("sbbl",@_); } | ||
| 174 | sub main'rotl { &out2("roll",@_); } | ||
| 175 | sub main'rotr { &out2("rorl",@_); } | ||
| 176 | sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); } | ||
| 177 | sub main'cmp { &out2("cmpl",@_); } | ||
| 178 | sub main'lea { &out2("leal",@_); } | ||
| 179 | sub main'mul { &out1("mull",@_); } | ||
| 180 | sub main'imul { &out2("imull",@_); } | ||
| 181 | sub main'div { &out1("divl",@_); } | ||
| 182 | sub main'jmp { &out1("jmp",@_); } | ||
| 183 | sub main'jmp_ptr { &out1p("jmp",@_); } | ||
| 184 | sub main'je { &out1("je",@_); } | ||
| 185 | sub main'jle { &out1("jle",@_); } | ||
| 186 | sub main'jne { &out1("jne",@_); } | ||
| 187 | sub main'jnz { &out1("jnz",@_); } | ||
| 188 | sub main'jz { &out1("jz",@_); } | ||
| 189 | sub main'jge { &out1("jge",@_); } | ||
| 190 | sub main'jl { &out1("jl",@_); } | ||
| 191 | sub main'ja { &out1("ja",@_); } | ||
| 192 | sub main'jae { &out1("jae",@_); } | ||
| 193 | sub main'jb { &out1("jb",@_); } | ||
| 194 | sub main'jbe { &out1("jbe",@_); } | ||
| 195 | sub main'jc { &out1("jc",@_); } | ||
| 196 | sub main'jnc { &out1("jnc",@_); } | ||
| 197 | sub main'jno { &out1("jno",@_); } | ||
| 198 | sub main'dec { &out1("decl",@_); } | ||
| 199 | sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); } | ||
| 200 | sub main'push { &out1("pushl",@_); $stack+=4; } | ||
| 201 | sub main'pop { &out1("popl",@_); $stack-=4; } | ||
| 202 | sub main'pushf { &out0("pushfl"); $stack+=4; } | ||
| 203 | sub main'popf { &out0("popfl"); $stack-=4; } | ||
| 204 | sub main'not { &out1("notl",@_); } | ||
| 205 | sub main'call { my $pre=$under; | ||
| 206 | foreach $i (%label) | ||
| 207 | { if ($label{$i} eq $_[0]) { $pre=''; last; } } | ||
| 208 | &out1("call",$pre.$_[0]); | ||
| 209 | } | ||
| 210 | sub main'call_ptr { &out1p("call",@_); } | ||
| 211 | sub main'ret { &out0("ret"); } | ||
| 212 | sub main'nop { &out0("nop"); } | ||
| 213 | sub main'test { &out2("testl",@_); } | ||
| 214 | sub main'bt { &out2("btl",@_); } | ||
| 215 | sub main'leave { &out0("leave"); } | ||
| 216 | sub main'cpuid { &out0(".byte\t0x0f,0xa2"); } | ||
| 217 | sub main'rdtsc { &out0(".byte\t0x0f,0x31"); } | ||
| 218 | sub main'halt { &out0("hlt"); } | ||
| 219 | sub main'movz { &out2("movzbl",@_); } | ||
| 220 | sub main'neg { &out1("negl",@_); } | ||
| 221 | sub main'cld { &out0("cld"); } | ||
| 222 | |||
| 223 | # SSE2 | ||
| 224 | sub main'emms { &out0("emms"); } | ||
| 225 | sub main'movd { &out2("movd",@_); } | ||
| 226 | sub main'movdqu { &out2("movdqu",@_); } | ||
| 227 | sub main'movdqa { &out2("movdqa",@_); } | ||
| 228 | sub main'movdq2q{ &out2("movdq2q",@_); } | ||
| 229 | sub main'movq2dq{ &out2("movq2dq",@_); } | ||
| 230 | sub main'paddq { &out2("paddq",@_); } | ||
| 231 | sub main'pmuludq{ &out2("pmuludq",@_); } | ||
| 232 | sub main'psrlq { &out2("psrlq",@_); } | ||
| 233 | sub main'psllq { &out2("psllq",@_); } | ||
| 234 | sub main'pxor { &out2("pxor",@_); } | ||
| 235 | sub main'por { &out2("por",@_); } | ||
| 236 | sub main'pand { &out2("pand",@_); } | ||
| 237 | sub main'movq { | ||
| 238 | local($p1,$p2,$optimize)=@_; | ||
| 239 | if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/) | ||
| 240 | # movq between mmx registers can sink Intel CPUs | ||
| 241 | { push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n"); } | ||
| 242 | else { &out2("movq",@_); } | ||
| 243 | } | ||
| 244 | |||
| 245 | # The bswapl instruction is new for the 486. Emulate if i386. | ||
| 246 | sub main'bswap | ||
| 247 | { | ||
| 248 | if ($main'i386) | ||
| 249 | { | ||
| 250 | &main'comment("bswapl @_"); | ||
| 251 | &main'exch(main'HB(@_),main'LB(@_)); | ||
| 252 | &main'rotr(@_,16); | ||
| 253 | &main'exch(main'HB(@_),main'LB(@_)); | ||
| 254 | } | ||
| 255 | else | ||
| 256 | { | ||
| 257 | &out1("bswapl",@_); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | sub out2 | ||
| 262 | { | ||
| 263 | local($name,$p1,$p2)=@_; | ||
| 264 | local($l,$ll,$t); | ||
| 265 | local(%special)=( "roll",0xD1C0,"rorl",0xD1C8, | ||
| 266 | "rcll",0xD1D0,"rcrl",0xD1D8, | ||
| 267 | "shll",0xD1E0,"shrl",0xD1E8, | ||
| 268 | "sarl",0xD1F8); | ||
| 269 | |||
| 270 | if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1)) | ||
| 271 | { | ||
| 272 | $op=$special{$name}|$reg_val{$p1}; | ||
| 273 | $tmp1=sprintf(".byte %d\n",($op>>8)&0xff); | ||
| 274 | $tmp2=sprintf(".byte %d\t",$op &0xff); | ||
| 275 | push(@out,$tmp1); | ||
| 276 | push(@out,$tmp2); | ||
| 277 | |||
| 278 | $p2=&conv($p2); | ||
| 279 | $p1=&conv($p1); | ||
| 280 | &main'comment("$name $p2 $p1"); | ||
| 281 | return; | ||
| 282 | } | ||
| 283 | |||
| 284 | push(@out,"\t$name\t"); | ||
| 285 | $t=&conv($p2).","; | ||
| 286 | $l=length($t); | ||
| 287 | push(@out,$t); | ||
| 288 | $ll=4-($l+9)/8; | ||
| 289 | $tmp1=sprintf("\t" x $ll); | ||
| 290 | push(@out,$tmp1); | ||
| 291 | push(@out,&conv($p1)."\n"); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub out1 | ||
| 295 | { | ||
| 296 | local($name,$p1)=@_; | ||
| 297 | local($l,$t); | ||
| 298 | local(%special)=("bswapl",0x0FC8); | ||
| 299 | |||
| 300 | if ((defined($special{$name})) && defined($regs{$p1})) | ||
| 301 | { | ||
| 302 | $op=$special{$name}|$reg_val{$p1}; | ||
| 303 | $tmp1=sprintf(".byte %d\n",($op>>8)&0xff); | ||
| 304 | $tmp2=sprintf(".byte %d\t",$op &0xff); | ||
| 305 | push(@out,$tmp1); | ||
| 306 | push(@out,$tmp2); | ||
| 307 | |||
| 308 | $p2=&conv($p2); | ||
| 309 | $p1=&conv($p1); | ||
| 310 | &main'comment("$name $p2 $p1"); | ||
| 311 | return; | ||
| 312 | } | ||
| 313 | |||
| 314 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub out1p | ||
| 318 | { | ||
| 319 | local($name,$p1)=@_; | ||
| 320 | local($l,$t); | ||
| 321 | |||
| 322 | push(@out,"\t$name\t*".&conv($p1)."\n"); | ||
| 323 | } | ||
| 324 | |||
| 325 | sub out0 | ||
| 326 | { | ||
| 327 | push(@out,"\t$_[0]\n"); | ||
| 328 | } | ||
| 329 | |||
| 330 | sub conv | ||
| 331 | { | ||
| 332 | local($p)=@_; | ||
| 333 | |||
| 334 | # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/; | ||
| 335 | |||
| 336 | $p=$regs{$p} if (defined($regs{$p})); | ||
| 337 | |||
| 338 | $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/; | ||
| 339 | $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/; | ||
| 340 | return $p; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub main'file | ||
| 344 | { | ||
| 345 | local($file)=@_; | ||
| 346 | |||
| 347 | if ($main'openbsd) | ||
| 348 | { push(@out,"#include <machine/asm.h>\n"); } | ||
| 349 | |||
| 350 | local($tmp)=<<"EOF"; | ||
| 351 | .file "$file.s" | ||
| 352 | EOF | ||
| 353 | push(@out,$tmp); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub main'function_begin | ||
| 357 | { | ||
| 358 | local($func,$junk,$llabel)=@_; | ||
| 359 | |||
| 360 | &main'external_label($func); | ||
| 361 | $func=$under.$func; | ||
| 362 | |||
| 363 | if ($main'openbsd) | ||
| 364 | { | ||
| 365 | push (@out, "\nENTRY($func)\n"); | ||
| 366 | push (@out, "$llabel:\n") if $llabel; | ||
| 367 | goto skip; | ||
| 368 | } | ||
| 369 | |||
| 370 | local($tmp)=<<"EOF"; | ||
| 371 | .text | ||
| 372 | .globl $func | ||
| 373 | EOF | ||
| 374 | push(@out,$tmp); | ||
| 375 | if ($main'cpp) | ||
| 376 | { $tmp=push(@out,"TYPE($func,\@function)\n"); } | ||
| 377 | elsif ($main'coff) | ||
| 378 | { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | ||
| 379 | elsif ($main'aout and !$main'pic) | ||
| 380 | { } | ||
| 381 | else { $tmp=push(@out,".type\t$func,\@function\n"); } | ||
| 382 | push(@out,".align\t$align\n"); | ||
| 383 | push(@out,"$func:\n"); | ||
| 384 | skip: | ||
| 385 | $tmp=<<"EOF"; | ||
| 386 | pushl %ebp | ||
| 387 | pushl %ebx | ||
| 388 | pushl %esi | ||
| 389 | pushl %edi | ||
| 390 | |||
| 391 | EOF | ||
| 392 | push(@out,$tmp); | ||
| 393 | $stack=20; | ||
| 394 | } | ||
| 395 | |||
| 396 | sub main'function_begin_B | ||
| 397 | { | ||
| 398 | local($func,$extra)=@_; | ||
| 399 | |||
| 400 | &main'external_label($func); | ||
| 401 | $func=$under.$func; | ||
| 402 | |||
| 403 | if ($main'openbsd) | ||
| 404 | { push(@out, "\nENTRY($func)\n"); goto skip; } | ||
| 405 | |||
| 406 | local($tmp)=<<"EOF"; | ||
| 407 | .text | ||
| 408 | .globl $func | ||
| 409 | EOF | ||
| 410 | push(@out,$tmp); | ||
| 411 | if ($main'cpp) | ||
| 412 | { push(@out,"TYPE($func,\@function)\n"); } | ||
| 413 | elsif ($main'coff) | ||
| 414 | { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | ||
| 415 | elsif ($main'aout and !$main'pic) | ||
| 416 | { } | ||
| 417 | else { push(@out,".type $func,\@function\n"); } | ||
| 418 | push(@out,".align\t$align\n"); | ||
| 419 | push(@out,"$func:\n"); | ||
| 420 | skip: | ||
| 421 | $stack=4; | ||
| 422 | } | ||
| 423 | |||
| 424 | # Like function_begin_B but with static linkage | ||
| 425 | sub main'function_begin_C | ||
| 426 | { | ||
| 427 | local($func,$extra)=@_; | ||
| 428 | |||
| 429 | &main'external_label($func); | ||
| 430 | $func=$under.$func; | ||
| 431 | |||
| 432 | if ($main'openbsd) | ||
| 433 | { | ||
| 434 | local($tmp)=<<"EOF"; | ||
| 435 | .text | ||
| 436 | _ALIGN_TEXT | ||
| 437 | .type $func,\@function | ||
| 438 | $func: | ||
| 439 | EOF | ||
| 440 | push(@out, $tmp); | ||
| 441 | goto skip; | ||
| 442 | } | ||
| 443 | |||
| 444 | local($tmp)=<<"EOF"; | ||
| 445 | .text | ||
| 446 | .globl $func | ||
| 447 | EOF | ||
| 448 | push(@out,$tmp); | ||
| 449 | if ($main'cpp) | ||
| 450 | { push(@out,"TYPE($func,\@function)\n"); } | ||
| 451 | elsif ($main'coff) | ||
| 452 | { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | ||
| 453 | elsif ($main'aout and !$main'pic) | ||
| 454 | { } | ||
| 455 | else { push(@out,".type $func,\@function\n"); } | ||
| 456 | push(@out,".align\t$align\n"); | ||
| 457 | push(@out,"$func:\n"); | ||
| 458 | skip: | ||
| 459 | $stack=4; | ||
| 460 | } | ||
| 461 | |||
| 462 | sub main'function_end | ||
| 463 | { | ||
| 464 | local($func)=@_; | ||
| 465 | |||
| 466 | $func=$under.$func; | ||
| 467 | |||
| 468 | local($tmp)=<<"EOF"; | ||
| 469 | popl %edi | ||
| 470 | popl %esi | ||
| 471 | popl %ebx | ||
| 472 | popl %ebp | ||
| 473 | ret | ||
| 474 | ${dot}L_${func}_end: | ||
| 475 | EOF | ||
| 476 | push(@out,$tmp); | ||
| 477 | |||
| 478 | if ($main'cpp) | ||
| 479 | { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); } | ||
| 480 | elsif ($main'coff or $main'aout) | ||
| 481 | { } | ||
| 482 | else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); } | ||
| 483 | push(@out,".ident \"$func\"\n"); | ||
| 484 | $stack=0; | ||
| 485 | %label=(); | ||
| 486 | } | ||
| 487 | |||
| 488 | sub main'function_end_A | ||
| 489 | { | ||
| 490 | local($func)=@_; | ||
| 491 | |||
| 492 | local($tmp)=<<"EOF"; | ||
| 493 | popl %edi | ||
| 494 | popl %esi | ||
| 495 | popl %ebx | ||
| 496 | popl %ebp | ||
| 497 | ret | ||
| 498 | EOF | ||
| 499 | push(@out,$tmp); | ||
| 500 | } | ||
| 501 | |||
| 502 | sub main'function_end_B | ||
| 503 | { | ||
| 504 | local($func)=@_; | ||
| 505 | |||
| 506 | $func=$under.$func; | ||
| 507 | |||
| 508 | push(@out,"${dot}L_${func}_end:\n"); | ||
| 509 | if ($main'cpp) | ||
| 510 | { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); } | ||
| 511 | elsif ($main'coff or $main'aout) | ||
| 512 | { } | ||
| 513 | else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); } | ||
| 514 | push(@out,".ident \"$func\"\n"); | ||
| 515 | $stack=0; | ||
| 516 | %label=(); | ||
| 517 | } | ||
| 518 | |||
| 519 | sub main'function_end_C { function_end_B(@_); } | ||
| 520 | |||
| 521 | sub main'wparam | ||
| 522 | { | ||
| 523 | local($num)=@_; | ||
| 524 | |||
| 525 | return(&main'DWP($stack+$num*4,"esp","",0)); | ||
| 526 | } | ||
| 527 | |||
| 528 | sub main'stack_push | ||
| 529 | { | ||
| 530 | local($num)=@_; | ||
| 531 | $stack+=$num*4; | ||
| 532 | &main'sub("esp",$num*4); | ||
| 533 | } | ||
| 534 | |||
| 535 | sub main'stack_pop | ||
| 536 | { | ||
| 537 | local($num)=@_; | ||
| 538 | $stack-=$num*4; | ||
| 539 | &main'add("esp",$num*4); | ||
| 540 | } | ||
| 541 | |||
| 542 | sub main'swtmp | ||
| 543 | { | ||
| 544 | return(&main'DWP($_[0]*4,"esp","",0)); | ||
| 545 | } | ||
| 546 | |||
| 547 | # Should use swtmp, which is above esp. Linix can trash the stack above esp | ||
| 548 | #sub main'wtmp | ||
| 549 | # { | ||
| 550 | # local($num)=@_; | ||
| 551 | # | ||
| 552 | # return(&main'DWP(-($num+1)*4,"esp","",0)); | ||
| 553 | # } | ||
| 554 | |||
| 555 | sub main'comment | ||
| 556 | { | ||
| 557 | if (!defined($com_start) or (!$main'openbsd && $main'elf)) | ||
| 558 | { # Regarding $main'elf above... | ||
| 559 | # GNU and SVR4 as'es use different comment delimiters, | ||
| 560 | push(@out,"\n"); # so we just skip ELF comments... | ||
| 561 | return; | ||
| 562 | } | ||
| 563 | foreach (@_) | ||
| 564 | { | ||
| 565 | if (/^\s*$/) | ||
| 566 | { push(@out,"\n"); } | ||
| 567 | else | ||
| 568 | { push(@out,"\t$com_start $_ $com_end\n"); } | ||
| 569 | } | ||
| 570 | } | ||
| 571 | |||
| 572 | sub main'public_label | ||
| 573 | { | ||
| 574 | $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]})); | ||
| 575 | push(@out,".globl\t$label{$_[0]}\n"); | ||
| 576 | } | ||
| 577 | |||
| 578 | sub main'label | ||
| 579 | { | ||
| 580 | if (!defined($label{$_[0]})) | ||
| 581 | { | ||
| 582 | $label{$_[0]}="${dot}${label}${_[0]}"; | ||
| 583 | $label++; | ||
| 584 | } | ||
| 585 | return($label{$_[0]}); | ||
| 586 | } | ||
| 587 | |||
| 588 | sub main'set_label | ||
| 589 | { | ||
| 590 | if (!defined($label{$_[0]})) | ||
| 591 | { | ||
| 592 | $label{$_[0]}="${dot}${label}${_[0]}"; | ||
| 593 | $label++; | ||
| 594 | } | ||
| 595 | if ($_[1]!=0) | ||
| 596 | { | ||
| 597 | if ($_[1]>1) { main'align($_[1]); } | ||
| 598 | else | ||
| 599 | { | ||
| 600 | if ($main'openbsd) | ||
| 601 | { push(@out,"_ALIGN_TEXT\n"); } | ||
| 602 | else | ||
| 603 | { push(@out,".align $align\n"); } | ||
| 604 | } | ||
| 605 | } | ||
| 606 | push(@out,"$label{$_[0]}:\n"); | ||
| 607 | } | ||
| 608 | |||
| 609 | sub main'file_end | ||
| 610 | { | ||
| 611 | # try to detect if SSE2 or MMX extensions were used on ELF platform... | ||
| 612 | if ($main'elf && grep {/\b%[x]*mm[0-7]\b|OPENSSL_ia32cap_P\b/i} @out) { | ||
| 613 | local($tmp); | ||
| 614 | |||
| 615 | push (@out,"\n.section\t.bss\n"); | ||
| 616 | push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n"); | ||
| 617 | |||
| 618 | return; | ||
| 619 | } | ||
| 620 | |||
| 621 | if ($const ne "") | ||
| 622 | { | ||
| 623 | push(@out,".section .rodata\n"); | ||
| 624 | push(@out,$const); | ||
| 625 | $const=""; | ||
| 626 | } | ||
| 627 | } | ||
| 628 | |||
| 629 | sub main'data_byte | ||
| 630 | { | ||
| 631 | push(@out,"\t.byte\t".join(',',@_)."\n"); | ||
| 632 | } | ||
| 633 | |||
| 634 | sub main'data_word | ||
| 635 | { | ||
| 636 | push(@out,"\t.long\t".join(',',@_)."\n"); | ||
| 637 | } | ||
| 638 | |||
| 639 | sub main'align | ||
| 640 | { | ||
| 641 | my $val=$_[0],$p2,$i; | ||
| 642 | if ($main'aout) { | ||
| 643 | for ($p2=0;$val!=0;$val>>=1) { $p2++; } | ||
| 644 | $val=$p2-1; | ||
| 645 | $val.=",0x90"; | ||
| 646 | } | ||
| 647 | push(@out,".align\t$val\n"); | ||
| 648 | if ($main'openbsd) | ||
| 649 | { push(@out,"_ALIGN_TEXT\n"); } | ||
| 650 | else | ||
| 651 | { push(@out,".align $tval\n"); } | ||
| 652 | } | ||
| 653 | |||
| 654 | # debug output functions: puts, putx, printf | ||
| 655 | |||
| 656 | sub main'puts | ||
| 657 | { | ||
| 658 | &pushvars(); | ||
| 659 | &main'push('$Lstring' . ++$constl); | ||
| 660 | &main'call('puts'); | ||
| 661 | $stack-=4; | ||
| 662 | &main'add("esp",4); | ||
| 663 | &popvars(); | ||
| 664 | |||
| 665 | $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n"; | ||
| 666 | } | ||
| 667 | |||
| 668 | sub main'putx | ||
| 669 | { | ||
| 670 | &pushvars(); | ||
| 671 | &main'push($_[0]); | ||
| 672 | &main'push('$Lstring' . ++$constl); | ||
| 673 | &main'call('printf'); | ||
| 674 | &main'add("esp",8); | ||
| 675 | $stack-=8; | ||
| 676 | &popvars(); | ||
| 677 | |||
| 678 | $const .= "Lstring$constl:\n\t.string \"\%X\"\n"; | ||
| 679 | } | ||
| 680 | |||
| 681 | sub main'printf | ||
| 682 | { | ||
| 683 | $ostack = $stack; | ||
| 684 | &pushvars(); | ||
| 685 | for ($i = @_ - 1; $i >= 0; $i--) | ||
| 686 | { | ||
| 687 | if ($i == 0) # change this to support %s format strings | ||
| 688 | { | ||
| 689 | &main'push('$Lstring' . ++$constl); | ||
| 690 | $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n"; | ||
| 691 | } | ||
| 692 | else | ||
| 693 | { | ||
| 694 | if ($_[$i] =~ /([0-9]*)\(%esp\)/) | ||
| 695 | { | ||
| 696 | &main'push(($1 + $stack - $ostack) . '(%esp)'); | ||
| 697 | } | ||
| 698 | else | ||
| 699 | { | ||
| 700 | &main'push($_[$i]); | ||
| 701 | } | ||
| 702 | } | ||
| 703 | } | ||
| 704 | &main'call('printf'); | ||
| 705 | $stack-=4*@_; | ||
| 706 | &main'add("esp",4*@_); | ||
| 707 | &popvars(); | ||
| 708 | } | ||
| 709 | |||
| 710 | sub pushvars | ||
| 711 | { | ||
| 712 | &main'pushf(); | ||
| 713 | &main'push("edx"); | ||
| 714 | &main'push("ecx"); | ||
| 715 | &main'push("eax"); | ||
| 716 | } | ||
| 717 | |||
| 718 | sub popvars | ||
| 719 | { | ||
| 720 | &main'pop("eax"); | ||
| 721 | &main'pop("ecx"); | ||
| 722 | &main'pop("edx"); | ||
| 723 | &main'popf(); | ||
| 724 | } | ||
| 725 | |||
| 726 | sub main'picmeup | ||
| 727 | { | ||
| 728 | local($dst,$sym)=@_; | ||
| 729 | if ($main'cpp) | ||
| 730 | { | ||
| 731 | local($tmp)=<<___; | ||
| 732 | #if (defined(ELF) || defined(SOL)) && defined(PIC) | ||
| 733 | call 1f | ||
| 734 | 1: popl $regs{$dst} | ||
| 735 | addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst} | ||
| 736 | movl $sym\@GOT($regs{$dst}),$regs{$dst} | ||
| 737 | #else | ||
| 738 | leal $sym,$regs{$dst} | ||
| 739 | #endif | ||
| 740 | ___ | ||
| 741 | push(@out,$tmp); | ||
| 742 | } | ||
| 743 | elsif ($main'openbsd) | ||
| 744 | { | ||
| 745 | push(@out, "#ifdef PIC\n"); | ||
| 746 | push(@out, "\tPIC_PROLOGUE\n"); | ||
| 747 | &main'mov($dst,"PIC_GOT($sym)"); | ||
| 748 | push(@out, "\tPIC_EPILOGUE\n"); | ||
| 749 | push(@out, "#else\n"); | ||
| 750 | &main'lea($dst,&main'DWP($sym)); | ||
| 751 | push(@out, "#endif\n"); | ||
| 752 | } | ||
| 753 | elsif ($main'pic && ($main'elf || $main'aout)) | ||
| 754 | { | ||
| 755 | &main'call(&main'label("PIC_me_up")); | ||
| 756 | &main'set_label("PIC_me_up"); | ||
| 757 | &main'blindpop($dst); | ||
| 758 | &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-". | ||
| 759 | &main'label("PIC_me_up") . "]"); | ||
| 760 | &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst)); | ||
| 761 | } | ||
| 762 | else | ||
| 763 | { | ||
| 764 | &main'lea($dst,&main'DWP($sym)); | ||
| 765 | } | ||
| 766 | } | ||
| 767 | |||
| 768 | sub main'blindpop { &out1("popl",@_); } | ||
| 769 | |||
| 770 | sub main'initseg | ||
| 771 | { | ||
| 772 | local($f)=@_; | ||
| 773 | local($tmp); | ||
| 774 | if ($main'elf) | ||
| 775 | { | ||
| 776 | $tmp=<<___; | ||
| 777 | .section .init | ||
| 778 | PIC_PROLOGUE | ||
| 779 | call PIC_PLT($under$f) | ||
| 780 | PIC_EPILOGUE | ||
| 781 | jmp .Linitalign | ||
| 782 | .align $align | ||
| 783 | .Linitalign: | ||
| 784 | ___ | ||
| 785 | } | ||
| 786 | elsif ($main'coff) | ||
| 787 | { | ||
| 788 | $tmp=<<___; # applies to both Cygwin and Mingw | ||
| 789 | .section .ctors | ||
| 790 | .long $under$f | ||
| 791 | ___ | ||
| 792 | } | ||
| 793 | elsif ($main'aout) | ||
| 794 | { | ||
| 795 | local($ctor)="${under}_GLOBAL_\$I\$$f"; | ||
| 796 | $tmp=".text\n"; | ||
| 797 | $tmp.=".type $ctor,\@function\n" if ($main'pic); | ||
| 798 | $tmp.=<<___; # OpenBSD way... | ||
| 799 | .globl $ctor | ||
| 800 | .align 2 | ||
| 801 | $ctor: | ||
| 802 | jmp $under$f | ||
| 803 | ___ | ||
| 804 | } | ||
| 805 | push(@out,$tmp) if ($tmp); | ||
| 806 | } | ||
| 807 | |||
| 808 | 1; | ||
