diff options
Diffstat (limited to 'src/lib/libcrypto/perlasm')
| -rw-r--r-- | src/lib/libcrypto/perlasm/alpha.pl | 434 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/cbc.pl | 351 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/readme | 124 | ||||
| -rwxr-xr-x | src/lib/libcrypto/perlasm/x86_64-xlate.pl | 557 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86asm.pl | 136 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86ms.pl | 472 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86nasm.pl | 455 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86unix.pl | 808 |
8 files changed, 3337 insertions, 0 deletions
diff --git a/src/lib/libcrypto/perlasm/alpha.pl b/src/lib/libcrypto/perlasm/alpha.pl new file mode 100644 index 0000000000..3dac571743 --- /dev/null +++ b/src/lib/libcrypto/perlasm/alpha.pl | |||
| @@ -0,0 +1,434 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | package alpha; | ||
| 4 | use Carp qw(croak cluck); | ||
| 5 | |||
| 6 | $label="100"; | ||
| 7 | |||
| 8 | $n_debug=0; | ||
| 9 | $smear_regs=1; | ||
| 10 | $reg_alloc=1; | ||
| 11 | |||
| 12 | $align="3"; | ||
| 13 | $com_start="#"; | ||
| 14 | |||
| 15 | sub main'asm_init_output { @out=(); } | ||
| 16 | sub main'asm_get_output { return(@out); } | ||
| 17 | sub main'get_labels { return(@labels); } | ||
| 18 | sub main'external_label { push(@labels,@_); } | ||
| 19 | |||
| 20 | # General registers | ||
| 21 | |||
| 22 | %regs=( 'r0', '$0', | ||
| 23 | 'r1', '$1', | ||
| 24 | 'r2', '$2', | ||
| 25 | 'r3', '$3', | ||
| 26 | 'r4', '$4', | ||
| 27 | 'r5', '$5', | ||
| 28 | 'r6', '$6', | ||
| 29 | 'r7', '$7', | ||
| 30 | 'r8', '$8', | ||
| 31 | 'r9', '$22', | ||
| 32 | 'r10', '$23', | ||
| 33 | 'r11', '$24', | ||
| 34 | 'r12', '$25', | ||
| 35 | 'r13', '$27', | ||
| 36 | 'r14', '$28', | ||
| 37 | 'r15', '$21', # argc == 5 | ||
| 38 | 'r16', '$20', # argc == 4 | ||
| 39 | 'r17', '$19', # argc == 3 | ||
| 40 | 'r18', '$18', # argc == 2 | ||
| 41 | 'r19', '$17', # argc == 1 | ||
| 42 | 'r20', '$16', # argc == 0 | ||
| 43 | 'r21', '$9', # save 0 | ||
| 44 | 'r22', '$10', # save 1 | ||
| 45 | 'r23', '$11', # save 2 | ||
| 46 | 'r24', '$12', # save 3 | ||
| 47 | 'r25', '$13', # save 4 | ||
| 48 | 'r26', '$14', # save 5 | ||
| 49 | |||
| 50 | 'a0', '$16', | ||
| 51 | 'a1', '$17', | ||
| 52 | 'a2', '$18', | ||
| 53 | 'a3', '$19', | ||
| 54 | 'a4', '$20', | ||
| 55 | 'a5', '$21', | ||
| 56 | |||
| 57 | 's0', '$9', | ||
| 58 | 's1', '$10', | ||
| 59 | 's2', '$11', | ||
| 60 | 's3', '$12', | ||
| 61 | 's4', '$13', | ||
| 62 | 's5', '$14', | ||
| 63 | 'zero', '$31', | ||
| 64 | 'sp', '$30', | ||
| 65 | ); | ||
| 66 | |||
| 67 | $main'reg_s0="r21"; | ||
| 68 | $main'reg_s1="r22"; | ||
| 69 | $main'reg_s2="r23"; | ||
| 70 | $main'reg_s3="r24"; | ||
| 71 | $main'reg_s4="r25"; | ||
| 72 | $main'reg_s5="r26"; | ||
| 73 | |||
| 74 | @reg=( '$0', '$1' ,'$2' ,'$3' ,'$4' ,'$5' ,'$6' ,'$7' ,'$8', | ||
| 75 | '$22','$23','$24','$25','$20','$21','$27','$28'); | ||
| 76 | |||
| 77 | |||
| 78 | sub main'sub { &out3("subq",@_); } | ||
| 79 | sub main'add { &out3("addq",@_); } | ||
| 80 | sub main'mov { &out3("bis",$_[0],$_[0],$_[1]); } | ||
| 81 | sub main'or { &out3("bis",@_); } | ||
| 82 | sub main'bis { &out3("bis",@_); } | ||
| 83 | sub main'br { &out1("br",@_); } | ||
| 84 | sub main'ld { &out2("ldq",@_); } | ||
| 85 | sub main'st { &out2("stq",@_); } | ||
| 86 | sub main'cmpult { &out3("cmpult",@_); } | ||
| 87 | sub main'cmplt { &out3("cmplt",@_); } | ||
| 88 | sub main'bgt { &out2("bgt",@_); } | ||
| 89 | sub main'ble { &out2("ble",@_); } | ||
| 90 | sub main'blt { &out2("blt",@_); } | ||
| 91 | sub main'mul { &out3("mulq",@_); } | ||
| 92 | sub main'muh { &out3("umulh",@_); } | ||
| 93 | |||
| 94 | $main'QWS=8; | ||
| 95 | |||
| 96 | sub main'asm_add | ||
| 97 | { | ||
| 98 | push(@out,@_); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub main'asm_finish | ||
| 102 | { | ||
| 103 | &main'file_end(); | ||
| 104 | print &main'asm_get_output(); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub main'asm_init | ||
| 108 | { | ||
| 109 | ($type,$fn)=@_; | ||
| 110 | $filename=$fn; | ||
| 111 | |||
| 112 | &main'asm_init_output(); | ||
| 113 | &main'comment("Don't even think of reading this code"); | ||
| 114 | &main'comment("It was automatically generated by $filename"); | ||
| 115 | &main'comment("Which is a perl program used to generate the alpha assember."); | ||
| 116 | &main'comment("eric <eay\@cryptsoft.com>"); | ||
| 117 | &main'comment(""); | ||
| 118 | |||
| 119 | $filename =~ s/\.pl$//; | ||
| 120 | &main'file($filename); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub conv | ||
| 124 | { | ||
| 125 | local($r)=@_; | ||
| 126 | local($v); | ||
| 127 | |||
| 128 | return($regs{$r}) if defined($regs{$r}); | ||
| 129 | return($r); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub main'QWPw | ||
| 133 | { | ||
| 134 | local($off,$reg)=@_; | ||
| 135 | |||
| 136 | return(&main'QWP($off*8,$reg)); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub main'QWP | ||
| 140 | { | ||
| 141 | local($off,$reg)=@_; | ||
| 142 | |||
| 143 | $ret="$off(".&conv($reg).")"; | ||
| 144 | return($ret); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub out3 | ||
| 148 | { | ||
| 149 | local($name,$p1,$p2,$p3)=@_; | ||
| 150 | |||
| 151 | $p1=&conv($p1); | ||
| 152 | $p2=&conv($p2); | ||
| 153 | $p3=&conv($p3); | ||
| 154 | push(@out,"\t$name\t"); | ||
| 155 | $l=length($p1)+1; | ||
| 156 | push(@out,$p1.","); | ||
| 157 | $ll=3-($l+9)/8; | ||
| 158 | $tmp1=sprintf("\t" x $ll); | ||
| 159 | push(@out,$tmp1); | ||
| 160 | |||
| 161 | $l=length($p2)+1; | ||
| 162 | push(@out,$p2.","); | ||
| 163 | $ll=3-($l+9)/8; | ||
| 164 | $tmp1=sprintf("\t" x $ll); | ||
| 165 | push(@out,$tmp1); | ||
| 166 | |||
| 167 | push(@out,&conv($p3)."\n"); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub out2 | ||
| 171 | { | ||
| 172 | local($name,$p1,$p2,$p3)=@_; | ||
| 173 | |||
| 174 | $p1=&conv($p1); | ||
| 175 | $p2=&conv($p2); | ||
| 176 | push(@out,"\t$name\t"); | ||
| 177 | $l=length($p1)+1; | ||
| 178 | push(@out,$p1.","); | ||
| 179 | $ll=3-($l+9)/8; | ||
| 180 | $tmp1=sprintf("\t" x $ll); | ||
| 181 | push(@out,$tmp1); | ||
| 182 | |||
| 183 | push(@out,&conv($p2)."\n"); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub out1 | ||
| 187 | { | ||
| 188 | local($name,$p1)=@_; | ||
| 189 | |||
| 190 | $p1=&conv($p1); | ||
| 191 | push(@out,"\t$name\t".$p1."\n"); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub out0 | ||
| 195 | { | ||
| 196 | push(@out,"\t$_[0]\n"); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub main'file | ||
| 200 | { | ||
| 201 | local($file)=@_; | ||
| 202 | |||
| 203 | local($tmp)=<<"EOF"; | ||
| 204 | # DEC Alpha assember | ||
| 205 | # Generated from perl scripts contains in SSLeay | ||
| 206 | .file 1 "$file.s" | ||
| 207 | .set noat | ||
| 208 | EOF | ||
| 209 | push(@out,$tmp); | ||
| 210 | } | ||
| 211 | |||
| 212 | sub main'function_begin | ||
| 213 | { | ||
| 214 | local($func)=@_; | ||
| 215 | |||
| 216 | print STDERR "$func\n"; | ||
| 217 | local($tmp)=<<"EOF"; | ||
| 218 | .text | ||
| 219 | .align $align | ||
| 220 | .globl $func | ||
| 221 | .ent $func | ||
| 222 | ${func}: | ||
| 223 | ${func}..ng: | ||
| 224 | .frame \$30,0,\$26,0 | ||
| 225 | .prologue 0 | ||
| 226 | EOF | ||
| 227 | push(@out,$tmp); | ||
| 228 | $stack=0; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub main'function_end | ||
| 232 | { | ||
| 233 | local($func)=@_; | ||
| 234 | |||
| 235 | local($tmp)=<<"EOF"; | ||
| 236 | ret \$31,(\$26),1 | ||
| 237 | .end $func | ||
| 238 | EOF | ||
| 239 | push(@out,$tmp); | ||
| 240 | $stack=0; | ||
| 241 | %label=(); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub main'function_end_A | ||
| 245 | { | ||
| 246 | local($func)=@_; | ||
| 247 | |||
| 248 | local($tmp)=<<"EOF"; | ||
| 249 | ret \$31,(\$26),1 | ||
| 250 | EOF | ||
| 251 | push(@out,$tmp); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub main'function_end_B | ||
| 255 | { | ||
| 256 | local($func)=@_; | ||
| 257 | |||
| 258 | $func=$under.$func; | ||
| 259 | |||
| 260 | push(@out,"\t.end $func\n"); | ||
| 261 | $stack=0; | ||
| 262 | %label=(); | ||
| 263 | } | ||
| 264 | |||
| 265 | sub main'wparam | ||
| 266 | { | ||
| 267 | local($num)=@_; | ||
| 268 | |||
| 269 | if ($num < 6) | ||
| 270 | { | ||
| 271 | $num=20-$num; | ||
| 272 | return("r$num"); | ||
| 273 | } | ||
| 274 | else | ||
| 275 | { return(&main'QWP($stack+$num*8,"sp")); } | ||
| 276 | } | ||
| 277 | |||
| 278 | sub main'stack_push | ||
| 279 | { | ||
| 280 | local($num)=@_; | ||
| 281 | $stack+=$num*8; | ||
| 282 | &main'sub("sp",$num*8,"sp"); | ||
| 283 | } | ||
| 284 | |||
| 285 | sub main'stack_pop | ||
| 286 | { | ||
| 287 | local($num)=@_; | ||
| 288 | $stack-=$num*8; | ||
| 289 | &main'add("sp",$num*8,"sp"); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub main'swtmp | ||
| 293 | { | ||
| 294 | return(&main'QWP(($_[0])*8,"sp")); | ||
| 295 | } | ||
| 296 | |||
| 297 | # Should use swtmp, which is above sp. Linix can trash the stack above esp | ||
| 298 | #sub main'wtmp | ||
| 299 | # { | ||
| 300 | # local($num)=@_; | ||
| 301 | # | ||
| 302 | # return(&main'QWP(-($num+1)*4,"esp","",0)); | ||
| 303 | # } | ||
| 304 | |||
| 305 | sub main'comment | ||
| 306 | { | ||
| 307 | foreach (@_) | ||
| 308 | { | ||
| 309 | if (/^\s*$/) | ||
| 310 | { push(@out,"\n"); } | ||
| 311 | else | ||
| 312 | { push(@out,"\t$com_start $_ $com_end\n"); } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | sub main'label | ||
| 317 | { | ||
| 318 | if (!defined($label{$_[0]})) | ||
| 319 | { | ||
| 320 | $label{$_[0]}=$label; | ||
| 321 | $label++; | ||
| 322 | } | ||
| 323 | return('$'.$label{$_[0]}); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub main'set_label | ||
| 327 | { | ||
| 328 | if (!defined($label{$_[0]})) | ||
| 329 | { | ||
| 330 | $label{$_[0]}=$label; | ||
| 331 | $label++; | ||
| 332 | } | ||
| 333 | # push(@out,".align $align\n") if ($_[1] != 0); | ||
| 334 | push(@out,'$'."$label{$_[0]}:\n"); | ||
| 335 | } | ||
| 336 | |||
| 337 | sub main'file_end | ||
| 338 | { | ||
| 339 | } | ||
| 340 | |||
| 341 | sub main'data_word | ||
| 342 | { | ||
| 343 | push(@out,"\t.long $_[0]\n"); | ||
| 344 | } | ||
| 345 | |||
| 346 | @pool_free=(); | ||
| 347 | @pool_taken=(); | ||
| 348 | $curr_num=0; | ||
| 349 | $max=0; | ||
| 350 | |||
| 351 | sub main'init_pool | ||
| 352 | { | ||
| 353 | local($args)=@_; | ||
| 354 | local($i); | ||
| 355 | |||
| 356 | @pool_free=(); | ||
| 357 | for ($i=(14+(6-$args)); $i >= 0; $i--) | ||
| 358 | { | ||
| 359 | push(@pool_free,"r$i"); | ||
| 360 | } | ||
| 361 | print STDERR "START :register pool:@pool_free\n"; | ||
| 362 | $curr_num=$max=0; | ||
| 363 | } | ||
| 364 | |||
| 365 | sub main'fin_pool | ||
| 366 | { | ||
| 367 | printf STDERR "END %2d:register pool:@pool_free\n",$max; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub main'GR | ||
| 371 | { | ||
| 372 | local($r)=@_; | ||
| 373 | local($i,@n,$_); | ||
| 374 | |||
| 375 | foreach (@pool_free) | ||
| 376 | { | ||
| 377 | if ($r ne $_) | ||
| 378 | { push(@n,$_); } | ||
| 379 | else | ||
| 380 | { | ||
| 381 | $curr_num++; | ||
| 382 | $max=$curr_num if ($curr_num > $max); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | @pool_free=@n; | ||
| 386 | print STDERR "GR:@pool_free\n" if $reg_alloc; | ||
| 387 | return(@_); | ||
| 388 | } | ||
| 389 | |||
| 390 | sub main'NR | ||
| 391 | { | ||
| 392 | local($num)=@_; | ||
| 393 | local(@ret); | ||
| 394 | |||
| 395 | $num=1 if $num == 0; | ||
| 396 | ($#pool_free >= ($num-1)) || croak "out of registers: want $num, have @pool_free"; | ||
| 397 | while ($num > 0) | ||
| 398 | { | ||
| 399 | push(@ret,pop @pool_free); | ||
| 400 | $curr_num++; | ||
| 401 | $max=$curr_num if ($curr_num > $max); | ||
| 402 | $num-- | ||
| 403 | } | ||
| 404 | print STDERR "nr @ret\n" if $n_debug; | ||
| 405 | print STDERR "NR:@pool_free\n" if $reg_alloc; | ||
| 406 | return(@ret); | ||
| 407 | |||
| 408 | } | ||
| 409 | |||
| 410 | sub main'FR | ||
| 411 | { | ||
| 412 | local(@r)=@_; | ||
| 413 | local(@a,$v,$w); | ||
| 414 | |||
| 415 | print STDERR "fr @r\n" if $n_debug; | ||
| 416 | # cluck "fr @r"; | ||
| 417 | for $w (@pool_free) | ||
| 418 | { | ||
| 419 | foreach $v (@r) | ||
| 420 | { | ||
| 421 | croak "double register free of $v (@pool_free)" if $w eq $v; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | foreach $v (@r) | ||
| 425 | { | ||
| 426 | croak "bad argument to FR" if ($v !~ /^r\d+$/); | ||
| 427 | if ($smear_regs) | ||
| 428 | { unshift(@pool_free,$v); } | ||
| 429 | else { push(@pool_free,$v); } | ||
| 430 | $curr_num--; | ||
| 431 | } | ||
| 432 | print STDERR "FR:@pool_free\n" if $reg_alloc; | ||
| 433 | } | ||
| 434 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl new file mode 100644 index 0000000000..e43dc9ae15 --- /dev/null +++ b/src/lib/libcrypto/perlasm/cbc.pl | |||
| @@ -0,0 +1,351 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # void des_ncbc_encrypt(input, output, length, schedule, ivec, enc) | ||
| 4 | # des_cblock (*input); | ||
| 5 | # des_cblock (*output); | ||
| 6 | # long length; | ||
| 7 | # des_key_schedule schedule; | ||
| 8 | # des_cblock (*ivec); | ||
| 9 | # int enc; | ||
| 10 | # | ||
| 11 | # calls | ||
| 12 | # des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT); | ||
| 13 | # | ||
| 14 | |||
| 15 | #&cbc("des_ncbc_encrypt","des_encrypt",0); | ||
| 16 | #&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt", | ||
| 17 | # 1,4,5,3,5,-1); | ||
| 18 | #&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt", | ||
| 19 | # 0,4,5,3,5,-1); | ||
| 20 | #&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3", | ||
| 21 | # 0,6,7,3,4,5); | ||
| 22 | # | ||
| 23 | # When doing a cipher that needs bigendian order, | ||
| 24 | # for encrypt, the iv is kept in bigendian form, | ||
| 25 | # while for decrypt, it is kept in little endian. | ||
| 26 | sub cbc | ||
| 27 | { | ||
| 28 | local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_; | ||
| 29 | # name is the function name | ||
| 30 | # enc_func and dec_func and the functions to call for encrypt/decrypt | ||
| 31 | # swap is true if byte order needs to be reversed | ||
| 32 | # iv_off is parameter number for the iv | ||
| 33 | # enc_off is parameter number for the encrypt/decrypt flag | ||
| 34 | # p1,p2,p3 are the offsets for parameters to be passed to the | ||
| 35 | # underlying calls. | ||
| 36 | |||
| 37 | &function_begin_B($name,""); | ||
| 38 | &comment(""); | ||
| 39 | |||
| 40 | $in="esi"; | ||
| 41 | $out="edi"; | ||
| 42 | $count="ebp"; | ||
| 43 | |||
| 44 | &push("ebp"); | ||
| 45 | &push("ebx"); | ||
| 46 | &push("esi"); | ||
| 47 | &push("edi"); | ||
| 48 | |||
| 49 | $data_off=4; | ||
| 50 | $data_off+=4 if ($p1 > 0); | ||
| 51 | $data_off+=4 if ($p2 > 0); | ||
| 52 | $data_off+=4 if ($p3 > 0); | ||
| 53 | |||
| 54 | &mov($count, &wparam(2)); # length | ||
| 55 | |||
| 56 | &comment("getting iv ptr from parameter $iv_off"); | ||
| 57 | &mov("ebx", &wparam($iv_off)); # Get iv ptr | ||
| 58 | |||
| 59 | &mov($in, &DWP(0,"ebx","",0));# iv[0] | ||
| 60 | &mov($out, &DWP(4,"ebx","",0));# iv[1] | ||
| 61 | |||
| 62 | &push($out); | ||
| 63 | &push($in); | ||
| 64 | &push($out); # used in decrypt for iv[1] | ||
| 65 | &push($in); # used in decrypt for iv[0] | ||
| 66 | |||
| 67 | &mov("ebx", "esp"); # This is the address of tin[2] | ||
| 68 | |||
| 69 | &mov($in, &wparam(0)); # in | ||
| 70 | &mov($out, &wparam(1)); # out | ||
| 71 | |||
| 72 | # We have loaded them all, how lets push things | ||
| 73 | &comment("getting encrypt flag from parameter $enc_off"); | ||
| 74 | &mov("ecx", &wparam($enc_off)); # Get enc flag | ||
| 75 | if ($p3 > 0) | ||
| 76 | { | ||
| 77 | &comment("get and push parameter $p3"); | ||
| 78 | if ($enc_off != $p3) | ||
| 79 | { &mov("eax", &wparam($p3)); &push("eax"); } | ||
| 80 | else { &push("ecx"); } | ||
| 81 | } | ||
| 82 | if ($p2 > 0) | ||
| 83 | { | ||
| 84 | &comment("get and push parameter $p2"); | ||
| 85 | if ($enc_off != $p2) | ||
| 86 | { &mov("eax", &wparam($p2)); &push("eax"); } | ||
| 87 | else { &push("ecx"); } | ||
| 88 | } | ||
| 89 | if ($p1 > 0) | ||
| 90 | { | ||
| 91 | &comment("get and push parameter $p1"); | ||
| 92 | if ($enc_off != $p1) | ||
| 93 | { &mov("eax", &wparam($p1)); &push("eax"); } | ||
| 94 | else { &push("ecx"); } | ||
| 95 | } | ||
| 96 | &push("ebx"); # push data/iv | ||
| 97 | |||
| 98 | &cmp("ecx",0); | ||
| 99 | &jz(&label("decrypt")); | ||
| 100 | |||
| 101 | &and($count,0xfffffff8); | ||
| 102 | &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0] | ||
| 103 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1] | ||
| 104 | |||
| 105 | &jz(&label("encrypt_finish")); | ||
| 106 | |||
| 107 | ############################################################# | ||
| 108 | |||
| 109 | &set_label("encrypt_loop"); | ||
| 110 | # encrypt start | ||
| 111 | # "eax" and "ebx" hold iv (or the last cipher text) | ||
| 112 | |||
| 113 | &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes | ||
| 114 | &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes | ||
| 115 | |||
| 116 | &xor("eax", "ecx"); | ||
| 117 | &xor("ebx", "edx"); | ||
| 118 | |||
| 119 | &bswap("eax") if $swap; | ||
| 120 | &bswap("ebx") if $swap; | ||
| 121 | |||
| 122 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
| 123 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 124 | |||
| 125 | &call($enc_func); | ||
| 126 | |||
| 127 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
| 128 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
| 129 | |||
| 130 | &bswap("eax") if $swap; | ||
| 131 | &bswap("ebx") if $swap; | ||
| 132 | |||
| 133 | &mov(&DWP(0,$out,"",0),"eax"); | ||
| 134 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
| 135 | |||
| 136 | # eax and ebx are the next iv. | ||
| 137 | |||
| 138 | &add($in, 8); | ||
| 139 | &add($out, 8); | ||
| 140 | |||
| 141 | &sub($count, 8); | ||
| 142 | &jnz(&label("encrypt_loop")); | ||
| 143 | |||
| 144 | ###################################################################3 | ||
| 145 | &set_label("encrypt_finish"); | ||
| 146 | &mov($count, &wparam(2)); # length | ||
| 147 | &and($count, 7); | ||
| 148 | &jz(&label("finish")); | ||
| 149 | &call(&label("PIC_point")); | ||
| 150 | &set_label("PIC_point"); | ||
| 151 | &blindpop("edx"); | ||
| 152 | &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx")); | ||
| 153 | &mov($count,&DWP(0,"ecx",$count,4)) | ||
| 154 | &add($count,"edx"); | ||
| 155 | &xor("ecx","ecx"); | ||
| 156 | &xor("edx","edx"); | ||
| 157 | #&mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4)); | ||
| 158 | &jmp_ptr($count); | ||
| 159 | |||
| 160 | &set_label("ej7"); | ||
| 161 | &xor("edx", "edx") if $ppro; # ppro friendly | ||
| 162 | &movb(&HB("edx"), &BP(6,$in,"",0)); | ||
| 163 | &shl("edx",8); | ||
| 164 | &set_label("ej6"); | ||
| 165 | &movb(&HB("edx"), &BP(5,$in,"",0)); | ||
| 166 | &set_label("ej5"); | ||
| 167 | &movb(&LB("edx"), &BP(4,$in,"",0)); | ||
| 168 | &set_label("ej4"); | ||
| 169 | &mov("ecx", &DWP(0,$in,"",0)); | ||
| 170 | &jmp(&label("ejend")); | ||
| 171 | &set_label("ej3"); | ||
| 172 | &movb(&HB("ecx"), &BP(2,$in,"",0)); | ||
| 173 | &xor("ecx", "ecx") if $ppro; # ppro friendly | ||
| 174 | &shl("ecx",8); | ||
| 175 | &set_label("ej2"); | ||
| 176 | &movb(&HB("ecx"), &BP(1,$in,"",0)); | ||
| 177 | &set_label("ej1"); | ||
| 178 | &movb(&LB("ecx"), &BP(0,$in,"",0)); | ||
| 179 | &set_label("ejend"); | ||
| 180 | |||
| 181 | &xor("eax", "ecx"); | ||
| 182 | &xor("ebx", "edx"); | ||
| 183 | |||
| 184 | &bswap("eax") if $swap; | ||
| 185 | &bswap("ebx") if $swap; | ||
| 186 | |||
| 187 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
| 188 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 189 | |||
| 190 | &call($enc_func); | ||
| 191 | |||
| 192 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
| 193 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
| 194 | |||
| 195 | &bswap("eax") if $swap; | ||
| 196 | &bswap("ebx") if $swap; | ||
| 197 | |||
| 198 | &mov(&DWP(0,$out,"",0),"eax"); | ||
| 199 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
| 200 | |||
| 201 | &jmp(&label("finish")); | ||
| 202 | |||
| 203 | ############################################################# | ||
| 204 | ############################################################# | ||
| 205 | &set_label("decrypt",1); | ||
| 206 | # decrypt start | ||
| 207 | &and($count,0xfffffff8); | ||
| 208 | # The next 2 instructions are only for if the jz is taken | ||
| 209 | &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
| 210 | &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
| 211 | &jz(&label("decrypt_finish")); | ||
| 212 | |||
| 213 | &set_label("decrypt_loop"); | ||
| 214 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
| 215 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
| 216 | |||
| 217 | &bswap("eax") if $swap; | ||
| 218 | &bswap("ebx") if $swap; | ||
| 219 | |||
| 220 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
| 221 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 222 | |||
| 223 | &call($dec_func); | ||
| 224 | |||
| 225 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
| 226 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
| 227 | |||
| 228 | &bswap("eax") if $swap; | ||
| 229 | &bswap("ebx") if $swap; | ||
| 230 | |||
| 231 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
| 232 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
| 233 | |||
| 234 | &xor("ecx", "eax"); | ||
| 235 | &xor("edx", "ebx"); | ||
| 236 | |||
| 237 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
| 238 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
| 239 | |||
| 240 | &mov(&DWP(0,$out,"",0),"ecx"); | ||
| 241 | &mov(&DWP(4,$out,"",0),"edx"); | ||
| 242 | |||
| 243 | &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv | ||
| 244 | &mov(&DWP($data_off+12,"esp","",0), "ebx"); # | ||
| 245 | |||
| 246 | &add($in, 8); | ||
| 247 | &add($out, 8); | ||
| 248 | |||
| 249 | &sub($count, 8); | ||
| 250 | &jnz(&label("decrypt_loop")); | ||
| 251 | ############################ ENDIT #######################3 | ||
| 252 | &set_label("decrypt_finish"); | ||
| 253 | &mov($count, &wparam(2)); # length | ||
| 254 | &and($count, 7); | ||
| 255 | &jz(&label("finish")); | ||
| 256 | |||
| 257 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
| 258 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
| 259 | |||
| 260 | &bswap("eax") if $swap; | ||
| 261 | &bswap("ebx") if $swap; | ||
| 262 | |||
| 263 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
| 264 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 265 | |||
| 266 | &call($dec_func); | ||
| 267 | |||
| 268 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
| 269 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
| 270 | |||
| 271 | &bswap("eax") if $swap; | ||
| 272 | &bswap("ebx") if $swap; | ||
| 273 | |||
| 274 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
| 275 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
| 276 | |||
| 277 | &xor("ecx", "eax"); | ||
| 278 | &xor("edx", "ebx"); | ||
| 279 | |||
| 280 | # this is for when we exit | ||
| 281 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
| 282 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
| 283 | |||
| 284 | &set_label("dj7"); | ||
| 285 | &rotr("edx", 16); | ||
| 286 | &movb(&BP(6,$out,"",0), &LB("edx")); | ||
| 287 | &shr("edx",16); | ||
| 288 | &set_label("dj6"); | ||
| 289 | &movb(&BP(5,$out,"",0), &HB("edx")); | ||
| 290 | &set_label("dj5"); | ||
| 291 | &movb(&BP(4,$out,"",0), &LB("edx")); | ||
| 292 | &set_label("dj4"); | ||
| 293 | &mov(&DWP(0,$out,"",0), "ecx"); | ||
| 294 | &jmp(&label("djend")); | ||
| 295 | &set_label("dj3"); | ||
| 296 | &rotr("ecx", 16); | ||
| 297 | &movb(&BP(2,$out,"",0), &LB("ecx")); | ||
| 298 | &shl("ecx",16); | ||
| 299 | &set_label("dj2"); | ||
| 300 | &movb(&BP(1,$in,"",0), &HB("ecx")); | ||
| 301 | &set_label("dj1"); | ||
| 302 | &movb(&BP(0,$in,"",0), &LB("ecx")); | ||
| 303 | &set_label("djend"); | ||
| 304 | |||
| 305 | # final iv is still in eax:ebx | ||
| 306 | &jmp(&label("finish")); | ||
| 307 | |||
| 308 | |||
| 309 | ############################ FINISH #######################3 | ||
| 310 | &set_label("finish",1); | ||
| 311 | &mov("ecx", &wparam($iv_off)); # Get iv ptr | ||
| 312 | |||
| 313 | ################################################# | ||
| 314 | $total=16+4; | ||
| 315 | $total+=4 if ($p1 > 0); | ||
| 316 | $total+=4 if ($p2 > 0); | ||
| 317 | $total+=4 if ($p3 > 0); | ||
| 318 | &add("esp",$total); | ||
| 319 | |||
| 320 | &mov(&DWP(0,"ecx","",0), "eax"); # save iv | ||
| 321 | &mov(&DWP(4,"ecx","",0), "ebx"); # save iv | ||
| 322 | |||
| 323 | &function_end_A($name); | ||
| 324 | |||
| 325 | &align(64); | ||
| 326 | &set_label("cbc_enc_jmp_table"); | ||
| 327 | &data_word("0"); | ||
| 328 | &data_word(&label("ej1")."-".&label("PIC_point")); | ||
| 329 | &data_word(&label("ej2")."-".&label("PIC_point")); | ||
| 330 | &data_word(&label("ej3")."-".&label("PIC_point")); | ||
| 331 | &data_word(&label("ej4")."-".&label("PIC_point")); | ||
| 332 | &data_word(&label("ej5")."-".&label("PIC_point")); | ||
| 333 | &data_word(&label("ej6")."-".&label("PIC_point")); | ||
| 334 | &data_word(&label("ej7")."-".&label("PIC_point")); | ||
| 335 | # not used | ||
| 336 | #&set_label("cbc_dec_jmp_table",1); | ||
| 337 | #&data_word("0"); | ||
| 338 | #&data_word(&label("dj1")."-".&label("PIC_point")); | ||
| 339 | #&data_word(&label("dj2")."-".&label("PIC_point")); | ||
| 340 | #&data_word(&label("dj3")."-".&label("PIC_point")); | ||
| 341 | #&data_word(&label("dj4")."-".&label("PIC_point")); | ||
| 342 | #&data_word(&label("dj5")."-".&label("PIC_point")); | ||
| 343 | #&data_word(&label("dj6")."-".&label("PIC_point")); | ||
| 344 | #&data_word(&label("dj7")."-".&label("PIC_point")); | ||
| 345 | &align(64); | ||
| 346 | |||
| 347 | &function_end_B($name); | ||
| 348 | |||
| 349 | } | ||
| 350 | |||
| 351 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/readme b/src/lib/libcrypto/perlasm/readme new file mode 100644 index 0000000000..f02bbee75a --- /dev/null +++ b/src/lib/libcrypto/perlasm/readme | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | The perl scripts in this directory are my 'hack' to generate | ||
| 2 | multiple different assembler formats via the one origional script. | ||
| 3 | |||
| 4 | The way to use this library is to start with adding the path to this directory | ||
| 5 | and then include it. | ||
| 6 | |||
| 7 | push(@INC,"perlasm","../../perlasm"); | ||
| 8 | require "x86asm.pl"; | ||
| 9 | |||
| 10 | The first thing we do is setup the file and type of assember | ||
| 11 | |||
| 12 | &asm_init($ARGV[0],$0); | ||
| 13 | |||
| 14 | The first argument is the 'type'. Currently | ||
| 15 | 'cpp', 'sol', 'a.out', 'elf' or 'win32'. | ||
| 16 | Argument 2 is the file name. | ||
| 17 | |||
| 18 | The reciprocal function is | ||
| 19 | &asm_finish() which should be called at the end. | ||
| 20 | |||
| 21 | There are 2 main 'packages'. x86ms.pl, which is the microsoft assembler, | ||
| 22 | and x86unix.pl which is the unix (gas) version. | ||
| 23 | |||
| 24 | Functions of interest are: | ||
| 25 | &external_label("des_SPtrans"); declare and external variable | ||
| 26 | &LB(reg); Low byte for a register | ||
| 27 | &HB(reg); High byte for a register | ||
| 28 | &BP(off,base,index,scale) Byte pointer addressing | ||
| 29 | &DWP(off,base,index,scale) Word pointer addressing | ||
| 30 | &stack_push(num) Basically a 'sub esp, num*4' with extra | ||
| 31 | &stack_pop(num) inverse of stack_push | ||
| 32 | &function_begin(name,extra) Start a function with pushing of | ||
| 33 | edi, esi, ebx and ebp. extra is extra win32 | ||
| 34 | external info that may be required. | ||
| 35 | &function_begin_B(name,extra) Same as norma function_begin but no pushing. | ||
| 36 | &function_end(name) Call at end of function. | ||
| 37 | &function_end_A(name) Standard pop and ret, for use inside functions | ||
| 38 | &function_end_B(name) Call at end but with poping or 'ret'. | ||
| 39 | &swtmp(num) Address on stack temp word. | ||
| 40 | &wparam(num) Parameter number num, that was push | ||
| 41 | in C convention. This all works over pushes | ||
| 42 | and pops. | ||
| 43 | &comment("hello there") Put in a comment. | ||
| 44 | &label("loop") Refer to a label, normally a jmp target. | ||
| 45 | &set_label("loop") Set a label at this point. | ||
| 46 | &data_word(word) Put in a word of data. | ||
| 47 | |||
| 48 | So how does this all hold together? Given | ||
| 49 | |||
| 50 | int calc(int len, int *data) | ||
| 51 | { | ||
| 52 | int i,j=0; | ||
| 53 | |||
| 54 | for (i=0; i<len; i++) | ||
| 55 | { | ||
| 56 | j+=other(data[i]); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | So a very simple version of this function could be coded as | ||
| 61 | |||
| 62 | push(@INC,"perlasm","../../perlasm"); | ||
| 63 | require "x86asm.pl"; | ||
| 64 | |||
| 65 | &asm_init($ARGV[0],"cacl.pl"); | ||
| 66 | |||
| 67 | &external_label("other"); | ||
| 68 | |||
| 69 | $tmp1= "eax"; | ||
| 70 | $j= "edi"; | ||
| 71 | $data= "esi"; | ||
| 72 | $i= "ebp"; | ||
| 73 | |||
| 74 | &comment("a simple function"); | ||
| 75 | &function_begin("calc"); | ||
| 76 | &mov( $data, &wparam(1)); # data | ||
| 77 | &xor( $j, $j); | ||
| 78 | &xor( $i, $i); | ||
| 79 | |||
| 80 | &set_label("loop"); | ||
| 81 | &cmp( $i, &wparam(0)); | ||
| 82 | &jge( &label("end")); | ||
| 83 | |||
| 84 | &mov( $tmp1, &DWP(0,$data,$i,4)); | ||
| 85 | &push( $tmp1); | ||
| 86 | &call( "other"); | ||
| 87 | &add( $j, "eax"); | ||
| 88 | &pop( $tmp1); | ||
| 89 | &inc( $i); | ||
| 90 | &jmp( &label("loop")); | ||
| 91 | |||
| 92 | &set_label("end"); | ||
| 93 | &mov( "eax", $j); | ||
| 94 | |||
| 95 | &function_end("calc"); | ||
| 96 | |||
| 97 | &asm_finish(); | ||
| 98 | |||
| 99 | The above example is very very unoptimised but gives an idea of how | ||
| 100 | things work. | ||
| 101 | |||
| 102 | There is also a cbc mode function generator in cbc.pl | ||
| 103 | |||
| 104 | &cbc( $name, | ||
| 105 | $encrypt_function_name, | ||
| 106 | $decrypt_function_name, | ||
| 107 | $true_if_byte_swap_needed, | ||
| 108 | $parameter_number_for_iv, | ||
| 109 | $parameter_number_for_encrypt_flag, | ||
| 110 | $first_parameter_to_pass, | ||
| 111 | $second_parameter_to_pass, | ||
| 112 | $third_parameter_to_pass); | ||
| 113 | |||
| 114 | So for example, given | ||
| 115 | void BF_encrypt(BF_LONG *data,BF_KEY *key); | ||
| 116 | void BF_decrypt(BF_LONG *data,BF_KEY *key); | ||
| 117 | void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length, | ||
| 118 | BF_KEY *ks, unsigned char *iv, int enc); | ||
| 119 | |||
| 120 | &cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1); | ||
| 121 | |||
| 122 | &cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1); | ||
| 123 | &cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5); | ||
| 124 | |||
diff --git a/src/lib/libcrypto/perlasm/x86_64-xlate.pl b/src/lib/libcrypto/perlasm/x86_64-xlate.pl new file mode 100755 index 0000000000..74153b017d --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86_64-xlate.pl | |||
| @@ -0,0 +1,557 @@ | |||
| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # Ascetic x86_64 AT&T to MASM assembler translator by <appro>. | ||
| 4 | # | ||
| 5 | # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T | ||
| 6 | # format is way easier to parse. Because it's simpler to "gear" from | ||
| 7 | # Unix ABI to Windows one [see cross-reference "card" at the end of | ||
| 8 | # file]. Because Linux targets were available first... | ||
| 9 | # | ||
| 10 | # In addition the script also "distills" code suitable for GNU | ||
| 11 | # assembler, so that it can be compiled with more rigid assemblers, | ||
| 12 | # such as Solaris /usr/ccs/bin/as. | ||
| 13 | # | ||
| 14 | # This translator is not designed to convert *arbitrary* assembler | ||
| 15 | # code from AT&T format to MASM one. It's designed to convert just | ||
| 16 | # enough to provide for dual-ABI OpenSSL modules development... | ||
| 17 | # There *are* limitations and you might have to modify your assembler | ||
| 18 | # code or this script to achieve the desired result... | ||
| 19 | # | ||
| 20 | # Currently recognized limitations: | ||
| 21 | # | ||
| 22 | # - can't use multiple ops per line; | ||
| 23 | # - indirect calls and jumps are not supported; | ||
| 24 | # | ||
| 25 | # Dual-ABI styling rules. | ||
| 26 | # | ||
| 27 | # 1. Adhere to Unix register and stack layout [see the end for | ||
| 28 | # explanation]. | ||
| 29 | # 2. Forget about "red zone," stick to more traditional blended | ||
| 30 | # stack frame allocation. If volatile storage is actually required | ||
| 31 | # that is. If not, just leave the stack as is. | ||
| 32 | # 3. Functions tagged with ".type name,@function" get crafted with | ||
| 33 | # unified Win64 prologue and epilogue automatically. If you want | ||
| 34 | # to take care of ABI differences yourself, tag functions as | ||
| 35 | # ".type name,@abi-omnipotent" instead. | ||
| 36 | # 4. To optimize the Win64 prologue you can specify number of input | ||
| 37 | # arguments as ".type name,@function,N." Keep in mind that if N is | ||
| 38 | # larger than 6, then you *have to* write "abi-omnipotent" code, | ||
| 39 | # because >6 cases can't be addressed with unified prologue. | ||
| 40 | # 5. Name local labels as .L*, do *not* use dynamic labels such as 1: | ||
| 41 | # (sorry about latter). | ||
| 42 | # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is | ||
| 43 | # required to identify the spots, where to inject Win64 epilogue! | ||
| 44 | # But on the pros, it's then prefixed with rep automatically:-) | ||
| 45 | # 7. Due to MASM limitations [and certain general counter-intuitivity | ||
| 46 | # of ip-relative addressing] generation of position-independent | ||
| 47 | # code is assisted by synthetic directive, .picmeup, which puts | ||
| 48 | # address of the *next* instruction into target register. | ||
| 49 | # | ||
| 50 | # Example 1: | ||
| 51 | # .picmeup %rax | ||
| 52 | # lea .Label-.(%rax),%rax | ||
| 53 | # Example 2: | ||
| 54 | # .picmeup %rcx | ||
| 55 | # .Lpic_point: | ||
| 56 | # ... | ||
| 57 | # lea .Label-.Lpic_point(%rcx),%rbp | ||
| 58 | |||
| 59 | my $output = shift; | ||
| 60 | |||
| 61 | { my ($stddev,$stdino,@junk)=stat(STDOUT); | ||
| 62 | my ($outdev,$outino,@junk)=stat($output); | ||
| 63 | |||
| 64 | open STDOUT,">$output" || die "can't open $output: $!" | ||
| 65 | if ($stddev!=$outdev || $stdino!=$outino); | ||
| 66 | } | ||
| 67 | |||
| 68 | my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005 | ||
| 69 | my $masm=$masmref if ($output =~ /\.asm/); | ||
| 70 | if ($masm && `ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/) | ||
| 71 | { $masm=$1 + $2*2**-16 + $4*2**-32; } | ||
| 72 | |||
| 73 | my $current_segment; | ||
| 74 | my $current_function; | ||
| 75 | |||
| 76 | { package opcode; # pick up opcodes | ||
| 77 | sub re { | ||
| 78 | my $self = shift; # single instance in enough... | ||
| 79 | local *line = shift; | ||
| 80 | undef $ret; | ||
| 81 | |||
| 82 | if ($line =~ /^([a-z][a-z0-9]*)/i) { | ||
| 83 | $self->{op} = $1; | ||
| 84 | $ret = $self; | ||
| 85 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 86 | |||
| 87 | undef $self->{sz}; | ||
| 88 | if ($self->{op} =~ /^(movz)b.*/) { # movz is pain... | ||
| 89 | $self->{op} = $1; | ||
| 90 | $self->{sz} = "b"; | ||
| 91 | } elsif ($self->{op} =~ /call/) { | ||
| 92 | $self->{sz} = "" | ||
| 93 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { | ||
| 94 | $self->{op} = $1; | ||
| 95 | $self->{sz} = $2; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | $ret; | ||
| 99 | } | ||
| 100 | sub size { | ||
| 101 | my $self = shift; | ||
| 102 | my $sz = shift; | ||
| 103 | $self->{sz} = $sz if (defined($sz) && !defined($self->{sz})); | ||
| 104 | $self->{sz}; | ||
| 105 | } | ||
| 106 | sub out { | ||
| 107 | my $self = shift; | ||
| 108 | if (!$masm) { | ||
| 109 | if ($self->{op} eq "movz") { # movz is pain... | ||
| 110 | sprintf "%s%s%s",$self->{op},$self->{sz},shift; | ||
| 111 | } elsif ($self->{op} =~ /^set/) { | ||
| 112 | "$self->{op}"; | ||
| 113 | } elsif ($self->{op} eq "ret") { | ||
| 114 | ".byte 0xf3,0xc3"; | ||
| 115 | } else { | ||
| 116 | "$self->{op}$self->{sz}"; | ||
| 117 | } | ||
| 118 | } else { | ||
| 119 | $self->{op} =~ s/^movz/movzx/; | ||
| 120 | if ($self->{op} eq "ret") { | ||
| 121 | $self->{op} = ""; | ||
| 122 | if ($current_function->{abi} eq "svr4") { | ||
| 123 | $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t". | ||
| 124 | "mov rsi,QWORD PTR 16[rsp]\n\t"; | ||
| 125 | } | ||
| 126 | $self->{op} .= "DB\t0F3h,0C3h\t\t;repret"; | ||
| 127 | } | ||
| 128 | $self->{op}; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | { package const; # pick up constants, which start with $ | ||
| 133 | sub re { | ||
| 134 | my $self = shift; # single instance in enough... | ||
| 135 | local *line = shift; | ||
| 136 | undef $ret; | ||
| 137 | |||
| 138 | if ($line =~ /^\$([^,]+)/) { | ||
| 139 | $self->{value} = $1; | ||
| 140 | $ret = $self; | ||
| 141 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 142 | } | ||
| 143 | $ret; | ||
| 144 | } | ||
| 145 | sub out { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | if (!$masm) { | ||
| 149 | # Solaris /usr/ccs/bin/as can't handle multiplications | ||
| 150 | # in $self->{value} | ||
| 151 | $self->{value} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi; | ||
| 152 | $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; | ||
| 153 | sprintf "\$%s",$self->{value}; | ||
| 154 | } else { | ||
| 155 | $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig; | ||
| 156 | sprintf "%s",$self->{value}; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | { package ea; # pick up effective addresses: expr(%reg,%reg,scale) | ||
| 161 | sub re { | ||
| 162 | my $self = shift; # single instance in enough... | ||
| 163 | local *line = shift; | ||
| 164 | undef $ret; | ||
| 165 | |||
| 166 | if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/ && | ||
| 167 | !($line =~ /^PIC_(GOT|PLT)/)) { | ||
| 168 | $self->{label} = $1; | ||
| 169 | ($self->{base},$self->{index},$self->{scale})=split(/,/,$2); | ||
| 170 | $self->{scale} = 1 if (!defined($self->{scale})); | ||
| 171 | $ret = $self; | ||
| 172 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 173 | |||
| 174 | $self->{base} =~ s/^%//; | ||
| 175 | $self->{index} =~ s/^%// if (defined($self->{index})); | ||
| 176 | } | ||
| 177 | $ret; | ||
| 178 | } | ||
| 179 | sub size {} | ||
| 180 | sub out { | ||
| 181 | my $self = shift; | ||
| 182 | my $sz = shift; | ||
| 183 | |||
| 184 | # Silently convert all EAs to 64-bit. This is required for | ||
| 185 | # elder GNU assembler and results in more compact code, | ||
| 186 | # *but* most importantly AES module depends on this feature! | ||
| 187 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | ||
| 188 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | ||
| 189 | |||
| 190 | if (!$masm) { | ||
| 191 | # Solaris /usr/ccs/bin/as can't handle multiplications | ||
| 192 | # in $self->{label} | ||
| 193 | $self->{label} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi; | ||
| 194 | $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; | ||
| 195 | |||
| 196 | if (defined($self->{index})) { | ||
| 197 | sprintf "%s(%%%s,%%%s,%d)", | ||
| 198 | $self->{label},$self->{base}, | ||
| 199 | $self->{index},$self->{scale}; | ||
| 200 | } else { | ||
| 201 | sprintf "%s(%%%s)", $self->{label},$self->{base}; | ||
| 202 | } | ||
| 203 | } else { | ||
| 204 | %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" ); | ||
| 205 | |||
| 206 | $self->{label} =~ s/\./\$/g; | ||
| 207 | $self->{label} =~ s/0x([0-9a-f]+)/0$1h/ig; | ||
| 208 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); | ||
| 209 | |||
| 210 | if (defined($self->{index})) { | ||
| 211 | sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz}, | ||
| 212 | $self->{label}, | ||
| 213 | $self->{index},$self->{scale}, | ||
| 214 | $self->{base}; | ||
| 215 | } elsif ($self->{base} eq "rip") { | ||
| 216 | sprintf "%s PTR %s",$szmap{$sz},$self->{label}; | ||
| 217 | } else { | ||
| 218 | sprintf "%s PTR %s[%s]",$szmap{$sz}, | ||
| 219 | $self->{label},$self->{base}; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | { package register; # pick up registers, which start with %. | ||
| 225 | sub re { | ||
| 226 | my $class = shift; # muliple instances... | ||
| 227 | my $self = {}; | ||
| 228 | local *line = shift; | ||
| 229 | undef $ret; | ||
| 230 | |||
| 231 | if ($line =~ /^%(\w+)/) { | ||
| 232 | bless $self,$class; | ||
| 233 | $self->{value} = $1; | ||
| 234 | $ret = $self; | ||
| 235 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 236 | } | ||
| 237 | $ret; | ||
| 238 | } | ||
| 239 | sub size { | ||
| 240 | my $self = shift; | ||
| 241 | undef $ret; | ||
| 242 | |||
| 243 | if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; } | ||
| 244 | elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; } | ||
| 245 | elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; } | ||
| 246 | elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; } | ||
| 247 | elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; } | ||
| 248 | elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; } | ||
| 249 | elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; } | ||
| 250 | elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; } | ||
| 251 | |||
| 252 | $ret; | ||
| 253 | } | ||
| 254 | sub out { | ||
| 255 | my $self = shift; | ||
| 256 | sprintf $masm?"%s":"%%%s",$self->{value}; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | { package label; # pick up labels, which end with : | ||
| 260 | sub re { | ||
| 261 | my $self = shift; # single instance is enough... | ||
| 262 | local *line = shift; | ||
| 263 | undef $ret; | ||
| 264 | |||
| 265 | if ($line =~ /(^[\.\w]+\:)/) { | ||
| 266 | $self->{value} = $1; | ||
| 267 | $ret = $self; | ||
| 268 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 269 | |||
| 270 | $self->{value} =~ s/\.L/\$L/ if ($masm); | ||
| 271 | } | ||
| 272 | $ret; | ||
| 273 | } | ||
| 274 | sub out { | ||
| 275 | my $self = shift; | ||
| 276 | |||
| 277 | if (!$masm) { | ||
| 278 | $self->{value}; | ||
| 279 | } elsif ($self->{value} ne "$current_function->{name}:") { | ||
| 280 | $self->{value}; | ||
| 281 | } elsif ($current_function->{abi} eq "svr4") { | ||
| 282 | my $func = "$current_function->{name} PROC\n". | ||
| 283 | " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n". | ||
| 284 | " mov QWORD PTR 16[rsp],rsi\n"; | ||
| 285 | my $narg = $current_function->{narg}; | ||
| 286 | $narg=6 if (!defined($narg)); | ||
| 287 | $func .= " mov rdi,rcx\n" if ($narg>0); | ||
| 288 | $func .= " mov rsi,rdx\n" if ($narg>1); | ||
| 289 | $func .= " mov rdx,r8\n" if ($narg>2); | ||
| 290 | $func .= " mov rcx,r9\n" if ($narg>3); | ||
| 291 | $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4); | ||
| 292 | $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5); | ||
| 293 | $func .= "\n"; | ||
| 294 | } else { | ||
| 295 | "$current_function->{name} PROC"; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | { package expr; # pick up expressioins | ||
| 300 | sub re { | ||
| 301 | my $self = shift; # single instance is enough... | ||
| 302 | local *line = shift; | ||
| 303 | undef $ret; | ||
| 304 | |||
| 305 | if ($line =~ /(^[^,]+)/) { | ||
| 306 | $self->{value} = $1; | ||
| 307 | $ret = $self; | ||
| 308 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 309 | |||
| 310 | $self->{value} =~ s/\.L/\$L/g if ($masm); | ||
| 311 | } | ||
| 312 | $ret; | ||
| 313 | } | ||
| 314 | sub out { | ||
| 315 | my $self = shift; | ||
| 316 | $self->{value}; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | { package directive; # pick up directives, which start with . | ||
| 320 | sub re { | ||
| 321 | my $self = shift; # single instance is enough... | ||
| 322 | local *line = shift; | ||
| 323 | undef $ret; | ||
| 324 | my $dir; | ||
| 325 | my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2: | ||
| 326 | ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48, | ||
| 327 | "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48, | ||
| 328 | "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48, | ||
| 329 | "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48, | ||
| 330 | "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c, | ||
| 331 | "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c, | ||
| 332 | "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c, | ||
| 333 | "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c ); | ||
| 334 | |||
| 335 | if ($line =~ /^\s*(\.\w+)/) { | ||
| 336 | if (!$masm) { | ||
| 337 | $self->{value} = $1; | ||
| 338 | $line =~ s/\@abi\-omnipotent/\@function/; | ||
| 339 | $line =~ s/\@function.*/\@function/; | ||
| 340 | if ($line =~ /\.picmeup\s+(%r[\w]+)/i) { | ||
| 341 | $self->{value} = sprintf "\t.long\t0x%x,0x90000000",$opcode{$1}; | ||
| 342 | } elsif ($line =~ /\.asciz\s+"(.*)"$/) { | ||
| 343 | $self->{value} = ".byte\t".join(",",unpack("C*",$1),0); | ||
| 344 | } elsif ($line =~ /\.extern/) { | ||
| 345 | $self->{value} = ""; # swallow extern | ||
| 346 | } else { | ||
| 347 | $self->{value} = $line; | ||
| 348 | } | ||
| 349 | $line = ""; | ||
| 350 | return $self; | ||
| 351 | } | ||
| 352 | |||
| 353 | $dir = $1; | ||
| 354 | $ret = $self; | ||
| 355 | undef $self->{value}; | ||
| 356 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 357 | SWITCH: for ($dir) { | ||
| 358 | /\.(text)/ | ||
| 359 | && do { my $v=undef; | ||
| 360 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
| 361 | $current_segment = "_$1\$"; | ||
| 362 | $current_segment =~ tr/[a-z]/[A-Z]/; | ||
| 363 | $v.="$current_segment\tSEGMENT "; | ||
| 364 | $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE"; | ||
| 365 | $v.=" 'CODE'"; | ||
| 366 | $self->{value} = $v; | ||
| 367 | last; | ||
| 368 | }; | ||
| 369 | /\.extern/ && do { $self->{value} = "EXTRN\t".$line.":BYTE"; last; }; | ||
| 370 | /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; }; | ||
| 371 | /\.type/ && do { ($sym,$type,$narg) = split(',',$line); | ||
| 372 | if ($type eq "\@function") { | ||
| 373 | undef $current_function; | ||
| 374 | $current_function->{name} = $sym; | ||
| 375 | $current_function->{abi} = "svr4"; | ||
| 376 | $current_function->{narg} = $narg; | ||
| 377 | } elsif ($type eq "\@abi-omnipotent") { | ||
| 378 | undef $current_function; | ||
| 379 | $current_function->{name} = $sym; | ||
| 380 | } | ||
| 381 | last; | ||
| 382 | }; | ||
| 383 | /\.size/ && do { if (defined($current_function)) { | ||
| 384 | $self->{value}="$current_function->{name}\tENDP"; | ||
| 385 | undef $current_function; | ||
| 386 | } | ||
| 387 | last; | ||
| 388 | }; | ||
| 389 | /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; }; | ||
| 390 | /\.(byte|value|long|quad)/ | ||
| 391 | && do { my @arr = split(',',$line); | ||
| 392 | my $sz = substr($1,0,1); | ||
| 393 | my $last = pop(@arr); | ||
| 394 | my $conv = sub { my $var=shift; | ||
| 395 | if ($var=~s/0x([0-9a-f]+)/0$1h/i) { $var; } | ||
| 396 | else { sprintf"0%Xh",$var; } | ||
| 397 | }; | ||
| 398 | |||
| 399 | $sz =~ tr/bvlq/BWDQ/; | ||
| 400 | $self->{value} = "\tD$sz\t"; | ||
| 401 | for (@arr) { $self->{value} .= &$conv($_).","; } | ||
| 402 | $self->{value} .= &$conv($last); | ||
| 403 | last; | ||
| 404 | }; | ||
| 405 | /\.picmeup/ && do { $self->{value} = sprintf"\tDD\t 0%Xh,090000000h",$opcode{$line}; | ||
| 406 | last; | ||
| 407 | }; | ||
| 408 | /\.asciz/ && do { if ($line =~ /^"(.*)"$/) { | ||
| 409 | my @str=unpack("C*",$1); | ||
| 410 | push @str,0; | ||
| 411 | while ($#str>15) { | ||
| 412 | $self->{value}.="DB\t" | ||
| 413 | .join(",",@str[0..15])."\n"; | ||
| 414 | foreach (0..15) { shift @str; } | ||
| 415 | } | ||
| 416 | $self->{value}.="DB\t" | ||
| 417 | .join(",",@str) if (@str); | ||
| 418 | } | ||
| 419 | last; | ||
| 420 | }; | ||
| 421 | } | ||
| 422 | $line = ""; | ||
| 423 | } | ||
| 424 | |||
| 425 | $ret; | ||
| 426 | } | ||
| 427 | sub out { | ||
| 428 | my $self = shift; | ||
| 429 | $self->{value}; | ||
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | print "#include <machine/asm.h>\n"; | ||
| 434 | |||
| 435 | while($line=<>) { | ||
| 436 | |||
| 437 | chomp($line); | ||
| 438 | |||
| 439 | $line =~ s|[#!].*$||; # get rid of asm-style comments... | ||
| 440 | $line =~ s|/\*.*\*/||; # ... and C-style comments... | ||
| 441 | $line =~ s|^\s+||; # ... and skip white spaces in beginning | ||
| 442 | |||
| 443 | undef $label; | ||
| 444 | undef $opcode; | ||
| 445 | undef $dst; | ||
| 446 | undef $src; | ||
| 447 | undef $sz; | ||
| 448 | |||
| 449 | if ($label=label->re(\$line)) { print $label->out(); } | ||
| 450 | |||
| 451 | if (directive->re(\$line)) { | ||
| 452 | printf "%s",directive->out(); | ||
| 453 | } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: { | ||
| 454 | |||
| 455 | if ($src=register->re(\$line)) { opcode->size($src->size()); } | ||
| 456 | elsif ($src=const->re(\$line)) { } | ||
| 457 | elsif ($src=ea->re(\$line)) { } | ||
| 458 | elsif ($src=expr->re(\$line)) { } | ||
| 459 | |||
| 460 | last ARGUMENT if ($line !~ /^,/); | ||
| 461 | |||
| 462 | $line = substr($line,1); $line =~ s/^\s+//; | ||
| 463 | |||
| 464 | if ($dst=register->re(\$line)) { opcode->size($dst->size()); } | ||
| 465 | elsif ($dst=const->re(\$line)) { } | ||
| 466 | elsif ($dst=ea->re(\$line)) { } | ||
| 467 | |||
| 468 | } # ARGUMENT: | ||
| 469 | |||
| 470 | $sz=opcode->size(); | ||
| 471 | |||
| 472 | if (defined($dst)) { | ||
| 473 | if (!$masm) { | ||
| 474 | printf "\t%s\t%s,%s", $opcode->out($dst->size()), | ||
| 475 | $src->out($sz),$dst->out($sz); | ||
| 476 | } else { | ||
| 477 | printf "\t%s\t%s,%s", $opcode->out(), | ||
| 478 | $dst->out($sz),$src->out($sz); | ||
| 479 | } | ||
| 480 | } elsif (defined($src)) { | ||
| 481 | printf "\t%s\t%s",$opcode->out(),$src->out($sz); | ||
| 482 | } else { | ||
| 483 | printf "\t%s",$opcode->out(); | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | print $line,"\n"; | ||
| 488 | } | ||
| 489 | |||
| 490 | print "\n$current_segment\tENDS\nEND\n" if ($masm); | ||
| 491 | |||
| 492 | close STDOUT; | ||
| 493 | |||
| 494 | ################################################# | ||
| 495 | # Cross-reference x86_64 ABI "card" | ||
| 496 | # | ||
| 497 | # Unix Win64 | ||
| 498 | # %rax * * | ||
| 499 | # %rbx - - | ||
| 500 | # %rcx #4 #1 | ||
| 501 | # %rdx #3 #2 | ||
| 502 | # %rsi #2 - | ||
| 503 | # %rdi #1 - | ||
| 504 | # %rbp - - | ||
| 505 | # %rsp - - | ||
| 506 | # %r8 #5 #3 | ||
| 507 | # %r9 #6 #4 | ||
| 508 | # %r10 * * | ||
| 509 | # %r11 * * | ||
| 510 | # %r12 - - | ||
| 511 | # %r13 - - | ||
| 512 | # %r14 - - | ||
| 513 | # %r15 - - | ||
| 514 | # | ||
| 515 | # (*) volatile register | ||
| 516 | # (-) preserved by callee | ||
| 517 | # (#) Nth argument, volatile | ||
| 518 | # | ||
| 519 | # In Unix terms top of stack is argument transfer area for arguments | ||
| 520 | # which could not be accomodated in registers. Or in other words 7th | ||
| 521 | # [integer] argument resides at 8(%rsp) upon function entry point. | ||
| 522 | # 128 bytes above %rsp constitute a "red zone" which is not touched | ||
| 523 | # by signal handlers and can be used as temporal storage without | ||
| 524 | # allocating a frame. | ||
| 525 | # | ||
| 526 | # In Win64 terms N*8 bytes on top of stack is argument transfer area, | ||
| 527 | # which belongs to/can be overwritten by callee. N is the number of | ||
| 528 | # arguments passed to callee, *but* not less than 4! This means that | ||
| 529 | # upon function entry point 5th argument resides at 40(%rsp), as well | ||
| 530 | # as that 32 bytes from 8(%rsp) can always be used as temporal | ||
| 531 | # storage [without allocating a frame]. One can actually argue that | ||
| 532 | # one can assume a "red zone" above stack pointer under Win64 as well. | ||
| 533 | # Point is that at apparently no occasion Windows kernel would alter | ||
| 534 | # the area above user stack pointer in true asynchronous manner... | ||
| 535 | # | ||
| 536 | # All the above means that if assembler programmer adheres to Unix | ||
| 537 | # register and stack layout, but disregards the "red zone" existense, | ||
| 538 | # it's possible to use following prologue and epilogue to "gear" from | ||
| 539 | # Unix to Win64 ABI in leaf functions with not more than 6 arguments. | ||
| 540 | # | ||
| 541 | # omnipotent_function: | ||
| 542 | # ifdef WIN64 | ||
| 543 | # movq %rdi,8(%rsp) | ||
| 544 | # movq %rsi,16(%rsp) | ||
| 545 | # movq %rcx,%rdi ; if 1st argument is actually present | ||
| 546 | # movq %rdx,%rsi ; if 2nd argument is actually ... | ||
| 547 | # movq %r8,%rdx ; if 3rd argument is ... | ||
| 548 | # movq %r9,%rcx ; if 4th argument ... | ||
| 549 | # movq 40(%rsp),%r8 ; if 5th ... | ||
| 550 | # movq 48(%rsp),%r9 ; if 6th ... | ||
| 551 | # endif | ||
| 552 | # ... | ||
| 553 | # ifdef WIN64 | ||
| 554 | # movq 8(%rsp),%rdi | ||
| 555 | # movq 16(%rsp),%rsi | ||
| 556 | # endif | ||
| 557 | # ret | ||
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl new file mode 100644 index 0000000000..f535c9c7fa --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86asm.pl | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # require 'x86asm.pl'; | ||
| 4 | # &asm_init("cpp","des-586.pl"); | ||
| 5 | # XXX | ||
| 6 | # XXX | ||
| 7 | # main'asm_finish | ||
| 8 | |||
| 9 | sub main'asm_finish | ||
| 10 | { | ||
| 11 | &file_end(); | ||
| 12 | &asm_finish_cpp() if $cpp; | ||
| 13 | print &asm_get_output(); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub main'asm_init | ||
| 17 | { | ||
| 18 | ($type,$fn,$i386)=@_; | ||
| 19 | $filename=$fn; | ||
| 20 | |||
| 21 | $elf=$cpp=$coff=$aout=$win32=$netware=$mwerks=$openbsd=0; | ||
| 22 | if ( ($type eq "elf")) | ||
| 23 | { $elf=1; require "x86unix.pl"; } | ||
| 24 | elsif ( ($type eq "openbsd-elf")) | ||
| 25 | { $openbsd=$elf=1; require "x86unix.pl"; } | ||
| 26 | elsif ( ($type eq "openbsd-a.out")) | ||
| 27 | { $openbsd=1; require "x86unix.pl"; } | ||
| 28 | elsif ( ($type eq "a.out")) | ||
| 29 | { $aout=1; require "x86unix.pl"; } | ||
| 30 | elsif ( ($type eq "coff" or $type eq "gaswin")) | ||
| 31 | { $coff=1; require "x86unix.pl"; } | ||
| 32 | elsif ( ($type eq "cpp")) | ||
| 33 | { $cpp=1; require "x86unix.pl"; } | ||
| 34 | elsif ( ($type eq "win32")) | ||
| 35 | { $win32=1; require "x86ms.pl"; } | ||
| 36 | elsif ( ($type eq "win32n")) | ||
| 37 | { $win32=1; require "x86nasm.pl"; } | ||
| 38 | elsif ( ($type eq "nw-nasm")) | ||
| 39 | { $netware=1; require "x86nasm.pl"; } | ||
| 40 | elsif ( ($type eq "nw-mwasm")) | ||
| 41 | { $netware=1; $mwerks=1; require "x86nasm.pl"; } | ||
| 42 | else | ||
| 43 | { | ||
| 44 | print STDERR <<"EOF"; | ||
| 45 | Pick one target type from | ||
| 46 | elf - Linux, FreeBSD, Solaris x86, etc. | ||
| 47 | a.out - OpenBSD, DJGPP, etc. | ||
| 48 | coff - GAS/COFF such as Win32 targets | ||
| 49 | win32 - Windows 95/Windows NT | ||
| 50 | win32n - Windows 95/Windows NT NASM format | ||
| 51 | openbsd-elf - OpenBSD elf | ||
| 52 | openbsd-a.out - OpenBSD a.out | ||
| 53 | nw-nasm - NetWare NASM format | ||
| 54 | nw-mwasm- NetWare Metrowerks Assembler | ||
| 55 | EOF | ||
| 56 | exit(1); | ||
| 57 | } | ||
| 58 | |||
| 59 | $pic=0; | ||
| 60 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } | ||
| 61 | |||
| 62 | &asm_init_output(); | ||
| 63 | |||
| 64 | &comment("Don't even think of reading this code"); | ||
| 65 | &comment("It was automatically generated by $filename"); | ||
| 66 | &comment("Which is a perl program used to generate the x86 assember for"); | ||
| 67 | &comment("any of ELF, a.out, COFF, Win32, ..."); | ||
| 68 | &comment("eric <eay\@cryptsoft.com>"); | ||
| 69 | &comment(""); | ||
| 70 | |||
| 71 | $filename =~ s/\.pl$//; | ||
| 72 | &file($filename); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub asm_finish_cpp | ||
| 76 | { | ||
| 77 | return unless $cpp; | ||
| 78 | |||
| 79 | local($tmp,$i); | ||
| 80 | foreach $i (&get_labels()) | ||
| 81 | { | ||
| 82 | $tmp.="#define $i _$i\n"; | ||
| 83 | } | ||
| 84 | print <<"EOF"; | ||
| 85 | /* Run the C pre-processor over this file with one of the following defined | ||
| 86 | * ELF - elf object files, | ||
| 87 | * OUT - a.out object files, | ||
| 88 | * BSDI - BSDI style a.out object files | ||
| 89 | * SOL - Solaris style elf | ||
| 90 | */ | ||
| 91 | |||
| 92 | #define TYPE(a,b) .type a,b | ||
| 93 | #define SIZE(a,b) .size a,b | ||
| 94 | |||
| 95 | #if defined(OUT) || (defined(BSDI) && !defined(ELF)) | ||
| 96 | $tmp | ||
| 97 | #endif | ||
| 98 | |||
| 99 | #ifdef OUT | ||
| 100 | #define OK 1 | ||
| 101 | #define ALIGN 4 | ||
| 102 | #if defined(__CYGWIN__) || defined(__DJGPP__) || (__MINGW32__) | ||
| 103 | #undef SIZE | ||
| 104 | #undef TYPE | ||
| 105 | #define SIZE(a,b) | ||
| 106 | #define TYPE(a,b) .def a; .scl 2; .type 32; .endef | ||
| 107 | #endif /* __CYGWIN || __DJGPP */ | ||
| 108 | #endif | ||
| 109 | |||
| 110 | #if defined(BSDI) && !defined(ELF) | ||
| 111 | #define OK 1 | ||
| 112 | #define ALIGN 4 | ||
| 113 | #undef SIZE | ||
| 114 | #undef TYPE | ||
| 115 | #define SIZE(a,b) | ||
| 116 | #define TYPE(a,b) | ||
| 117 | #endif | ||
| 118 | |||
| 119 | #if defined(ELF) || defined(SOL) | ||
| 120 | #define OK 1 | ||
| 121 | #define ALIGN 16 | ||
| 122 | #endif | ||
| 123 | |||
| 124 | #ifndef OK | ||
| 125 | You need to define one of | ||
| 126 | ELF - elf systems - linux-elf, NetBSD and DG-UX | ||
| 127 | OUT - a.out systems - linux-a.out and FreeBSD | ||
| 128 | SOL - solaris systems, which are elf with strange comment lines | ||
| 129 | BSDI - a.out with a very primative version of as. | ||
| 130 | #endif | ||
| 131 | |||
| 132 | /* Let the Assembler begin :-) */ | ||
| 133 | EOF | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/x86ms.pl b/src/lib/libcrypto/perlasm/x86ms.pl new file mode 100644 index 0000000000..a0be2934c2 --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86ms.pl | |||
| @@ -0,0 +1,472 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | package x86ms; | ||
| 4 | |||
| 5 | $label="L000"; | ||
| 6 | |||
| 7 | %lb=( 'eax', 'al', | ||
| 8 | 'ebx', 'bl', | ||
| 9 | 'ecx', 'cl', | ||
| 10 | 'edx', 'dl', | ||
| 11 | 'ax', 'al', | ||
| 12 | 'bx', 'bl', | ||
| 13 | 'cx', 'cl', | ||
| 14 | 'dx', 'dl', | ||
| 15 | ); | ||
| 16 | |||
| 17 | %hb=( 'eax', 'ah', | ||
| 18 | 'ebx', 'bh', | ||
| 19 | 'ecx', 'ch', | ||
| 20 | 'edx', 'dh', | ||
| 21 | 'ax', 'ah', | ||
| 22 | 'bx', 'bh', | ||
| 23 | 'cx', 'ch', | ||
| 24 | 'dx', 'dh', | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub main'asm_init_output { @out=(); } | ||
| 28 | sub main'asm_get_output { return(@out); } | ||
| 29 | sub main'get_labels { return(@labels); } | ||
| 30 | sub main'external_label | ||
| 31 | { | ||
| 32 | push(@labels,@_); | ||
| 33 | foreach (@_) { | ||
| 34 | push(@out, "EXTRN\t_$_:DWORD\n"); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub main'LB | ||
| 39 | { | ||
| 40 | (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n"; | ||
| 41 | return($lb{$_[0]}); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub main'HB | ||
| 45 | { | ||
| 46 | (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n"; | ||
| 47 | return($hb{$_[0]}); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub main'BP | ||
| 51 | { | ||
| 52 | &get_mem("BYTE",@_); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub main'DWP | ||
| 56 | { | ||
| 57 | &get_mem("DWORD",@_); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub main'QWP | ||
| 61 | { | ||
| 62 | &get_mem("QWORD",@_); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub main'BC | ||
| 66 | { | ||
| 67 | return @_; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub main'DWC | ||
| 71 | { | ||
| 72 | return @_; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub main'stack_push | ||
| 76 | { | ||
| 77 | local($num)=@_; | ||
| 78 | $stack+=$num*4; | ||
| 79 | &main'sub("esp",$num*4); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub main'stack_pop | ||
| 83 | { | ||
| 84 | local($num)=@_; | ||
| 85 | $stack-=$num*4; | ||
| 86 | &main'add("esp",$num*4); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub get_mem | ||
| 90 | { | ||
| 91 | local($size,$addr,$reg1,$reg2,$idx)=@_; | ||
| 92 | local($t,$post); | ||
| 93 | local($ret)="$size PTR "; | ||
| 94 | |||
| 95 | $addr =~ s/^\s+//; | ||
| 96 | if ($addr =~ /^(.+)\+(.+)$/) | ||
| 97 | { | ||
| 98 | $reg2=&conv($1); | ||
| 99 | $addr="_$2"; | ||
| 100 | } | ||
| 101 | elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i) | ||
| 102 | { | ||
| 103 | $addr="_$addr"; | ||
| 104 | } | ||
| 105 | |||
| 106 | if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; } | ||
| 107 | |||
| 108 | $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 109 | $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 110 | if (($addr ne "") && ($addr ne 0)) | ||
| 111 | { | ||
| 112 | if ($addr !~ /^-/) | ||
| 113 | { $ret.=$addr; } | ||
| 114 | else { $post=$addr; } | ||
| 115 | } | ||
| 116 | if ($reg2 ne "") | ||
| 117 | { | ||
| 118 | $t=""; | ||
| 119 | $t="*$idx" if ($idx != 0); | ||
| 120 | $reg1="+".$reg1 if ("$reg1$post" ne ""); | ||
| 121 | $ret.="[$reg2$t$reg1$post]"; | ||
| 122 | } | ||
| 123 | else | ||
| 124 | { | ||
| 125 | $ret.="[$reg1$post]" | ||
| 126 | } | ||
| 127 | $ret =~ s/\[\]//; # in case $addr was the only argument | ||
| 128 | return($ret); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub main'mov { &out2("mov",@_); } | ||
| 132 | sub main'movb { &out2("mov",@_); } | ||
| 133 | sub main'and { &out2("and",@_); } | ||
| 134 | sub main'or { &out2("or",@_); } | ||
| 135 | sub main'shl { &out2("shl",@_); } | ||
| 136 | sub main'shr { &out2("shr",@_); } | ||
| 137 | sub main'xor { &out2("xor",@_); } | ||
| 138 | sub main'xorb { &out2("xor",@_); } | ||
| 139 | sub main'add { &out2("add",@_); } | ||
| 140 | sub main'adc { &out2("adc",@_); } | ||
| 141 | sub main'sub { &out2("sub",@_); } | ||
| 142 | sub main'sbb { &out2("sbb",@_); } | ||
| 143 | sub main'rotl { &out2("rol",@_); } | ||
| 144 | sub main'rotr { &out2("ror",@_); } | ||
| 145 | sub main'exch { &out2("xchg",@_); } | ||
| 146 | sub main'cmp { &out2("cmp",@_); } | ||
| 147 | sub main'lea { &out2("lea",@_); } | ||
| 148 | sub main'mul { &out1("mul",@_); } | ||
| 149 | sub main'imul { &out2("imul",@_); } | ||
| 150 | sub main'div { &out1("div",@_); } | ||
| 151 | sub main'dec { &out1("dec",@_); } | ||
| 152 | sub main'inc { &out1("inc",@_); } | ||
| 153 | sub main'jmp { &out1("jmp",@_); } | ||
| 154 | sub main'jmp_ptr { &out1p("jmp",@_); } | ||
| 155 | sub main'je { &out1("je",@_); } | ||
| 156 | sub main'jle { &out1("jle",@_); } | ||
| 157 | sub main'jz { &out1("jz",@_); } | ||
| 158 | sub main'jge { &out1("jge",@_); } | ||
| 159 | sub main'jl { &out1("jl",@_); } | ||
| 160 | sub main'ja { &out1("ja",@_); } | ||
| 161 | sub main'jae { &out1("jae",@_); } | ||
| 162 | sub main'jb { &out1("jb",@_); } | ||
| 163 | sub main'jbe { &out1("jbe",@_); } | ||
| 164 | sub main'jc { &out1("jc",@_); } | ||
| 165 | sub main'jnc { &out1("jnc",@_); } | ||
| 166 | sub main'jnz { &out1("jnz",@_); } | ||
| 167 | sub main'jne { &out1("jne",@_); } | ||
| 168 | sub main'jno { &out1("jno",@_); } | ||
| 169 | sub main'push { &out1("push",@_); $stack+=4; } | ||
| 170 | sub main'pop { &out1("pop",@_); $stack-=4; } | ||
| 171 | sub main'pushf { &out0("pushfd"); $stack+=4; } | ||
| 172 | sub main'popf { &out0("popfd"); $stack-=4; } | ||
| 173 | sub main'bswap { &out1("bswap",@_); &using486(); } | ||
| 174 | sub main'not { &out1("not",@_); } | ||
| 175 | sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); } | ||
| 176 | sub main'call_ptr { &out1p("call",@_); } | ||
| 177 | sub main'ret { &out0("ret"); } | ||
| 178 | sub main'nop { &out0("nop"); } | ||
| 179 | sub main'test { &out2("test",@_); } | ||
| 180 | sub main'bt { &out2("bt",@_); } | ||
| 181 | sub main'leave { &out0("leave"); } | ||
| 182 | sub main'cpuid { &out0("DW\t0A20Fh"); } | ||
| 183 | sub main'rdtsc { &out0("DW\t0310Fh"); } | ||
| 184 | sub main'halt { &out0("hlt"); } | ||
| 185 | sub main'movz { &out2("movzx",@_); } | ||
| 186 | sub main'neg { &out1("neg",@_); } | ||
| 187 | sub main'cld { &out0("cld"); } | ||
| 188 | |||
| 189 | # SSE2 | ||
| 190 | sub main'emms { &out0("emms"); } | ||
| 191 | sub main'movd { &out2("movd",@_); } | ||
| 192 | sub main'movq { &out2("movq",@_); } | ||
| 193 | sub main'movdqu { &out2("movdqu",@_); } | ||
| 194 | sub main'movdqa { &out2("movdqa",@_); } | ||
| 195 | sub main'movdq2q{ &out2("movdq2q",@_); } | ||
| 196 | sub main'movq2dq{ &out2("movq2dq",@_); } | ||
| 197 | sub main'paddq { &out2("paddq",@_); } | ||
| 198 | sub main'pmuludq{ &out2("pmuludq",@_); } | ||
| 199 | sub main'psrlq { &out2("psrlq",@_); } | ||
| 200 | sub main'psllq { &out2("psllq",@_); } | ||
| 201 | sub main'pxor { &out2("pxor",@_); } | ||
| 202 | sub main'por { &out2("por",@_); } | ||
| 203 | sub main'pand { &out2("pand",@_); } | ||
| 204 | |||
| 205 | sub out2 | ||
| 206 | { | ||
| 207 | local($name,$p1,$p2)=@_; | ||
| 208 | local($l,$t,$line); | ||
| 209 | |||
| 210 | $line="\t$name\t"; | ||
| 211 | $t=&conv($p1).","; | ||
| 212 | $l=length($t); | ||
| 213 | $line.="$t"; | ||
| 214 | $l=4-($l+9)/8; | ||
| 215 | $line.="\t" x $l; | ||
| 216 | $line.=&conv($p2); | ||
| 217 | if ($line=~/\bxmm[0-7]\b/i) { $line=~s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i; } | ||
| 218 | push(@out,$line."\n"); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub out0 | ||
| 222 | { | ||
| 223 | local($name)=@_; | ||
| 224 | |||
| 225 | push(@out,"\t$name\n"); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub out1 | ||
| 229 | { | ||
| 230 | local($name,$p1)=@_; | ||
| 231 | local($l,$t); | ||
| 232 | |||
| 233 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub conv | ||
| 237 | { | ||
| 238 | local($p)=@_; | ||
| 239 | |||
| 240 | $p =~ s/0x([0-9A-Fa-f]+)/0$1h/; | ||
| 241 | return $p; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub using486 | ||
| 245 | { | ||
| 246 | return if $using486; | ||
| 247 | $using486++; | ||
| 248 | grep(s/\.386/\.486/,@out); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub main'file | ||
| 252 | { | ||
| 253 | local($file)=@_; | ||
| 254 | |||
| 255 | local($tmp)=<<"EOF"; | ||
| 256 | TITLE $file.asm | ||
| 257 | .386 | ||
| 258 | .model FLAT | ||
| 259 | _TEXT\$ SEGMENT PAGE 'CODE' | ||
| 260 | |||
| 261 | EOF | ||
| 262 | push(@out,$tmp); | ||
| 263 | } | ||
| 264 | |||
| 265 | sub main'function_begin | ||
| 266 | { | ||
| 267 | local($func,$extra)=@_; | ||
| 268 | |||
| 269 | push(@labels,$func); | ||
| 270 | |||
| 271 | local($tmp)=<<"EOF"; | ||
| 272 | PUBLIC _$func | ||
| 273 | $extra | ||
| 274 | _$func PROC NEAR | ||
| 275 | push ebp | ||
| 276 | push ebx | ||
| 277 | push esi | ||
| 278 | push edi | ||
| 279 | EOF | ||
| 280 | push(@out,$tmp); | ||
| 281 | $stack=20; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub main'function_begin_B | ||
| 285 | { | ||
| 286 | local($func,$extra)=@_; | ||
| 287 | |||
| 288 | local($tmp)=<<"EOF"; | ||
| 289 | PUBLIC _$func | ||
| 290 | $extra | ||
| 291 | _$func PROC NEAR | ||
| 292 | EOF | ||
| 293 | push(@out,$tmp); | ||
| 294 | $stack=4; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub main'function_end | ||
| 298 | { | ||
| 299 | local($func)=@_; | ||
| 300 | |||
| 301 | local($tmp)=<<"EOF"; | ||
| 302 | pop edi | ||
| 303 | pop esi | ||
| 304 | pop ebx | ||
| 305 | pop ebp | ||
| 306 | ret | ||
| 307 | _$func ENDP | ||
| 308 | EOF | ||
| 309 | push(@out,$tmp); | ||
| 310 | $stack=0; | ||
| 311 | %label=(); | ||
| 312 | } | ||
| 313 | |||
| 314 | sub main'function_end_B | ||
| 315 | { | ||
| 316 | local($func)=@_; | ||
| 317 | |||
| 318 | local($tmp)=<<"EOF"; | ||
| 319 | _$func ENDP | ||
| 320 | EOF | ||
| 321 | push(@out,$tmp); | ||
| 322 | $stack=0; | ||
| 323 | %label=(); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub main'function_end_A | ||
| 327 | { | ||
| 328 | local($func)=@_; | ||
| 329 | |||
| 330 | local($tmp)=<<"EOF"; | ||
| 331 | pop edi | ||
| 332 | pop esi | ||
| 333 | pop ebx | ||
| 334 | pop ebp | ||
| 335 | ret | ||
| 336 | EOF | ||
| 337 | push(@out,$tmp); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub main'file_end | ||
| 341 | { | ||
| 342 | # try to detect if SSE2 or MMX extensions were used... | ||
| 343 | my $xmmheader=<<___; | ||
| 344 | .686 | ||
| 345 | .XMM | ||
| 346 | IF \@Version LT 800 | ||
| 347 | XMMWORD STRUCT 16 | ||
| 348 | DQ 2 dup (?) | ||
| 349 | XMMWORD ENDS | ||
| 350 | ENDIF | ||
| 351 | ___ | ||
| 352 | if (grep {/\b[x]?mm[0-7]\b/i} @out) { | ||
| 353 | grep {s/\.[3-7]86/$xmmheader/} @out; | ||
| 354 | } | ||
| 355 | push(@out,"_TEXT\$ ENDS\n"); | ||
| 356 | push(@out,"END\n"); | ||
| 357 | } | ||
| 358 | |||
| 359 | sub main'wparam | ||
| 360 | { | ||
| 361 | local($num)=@_; | ||
| 362 | |||
| 363 | return(&main'DWP($stack+$num*4,"esp","",0)); | ||
| 364 | } | ||
| 365 | |||
| 366 | sub main'swtmp | ||
| 367 | { | ||
| 368 | return(&main'DWP($_[0]*4,"esp","",0)); | ||
| 369 | } | ||
| 370 | |||
| 371 | # Should use swtmp, which is above esp. Linix can trash the stack above esp | ||
| 372 | #sub main'wtmp | ||
| 373 | # { | ||
| 374 | # local($num)=@_; | ||
| 375 | # | ||
| 376 | # return(&main'DWP(-(($num+1)*4),"esp","",0)); | ||
| 377 | # } | ||
| 378 | |||
| 379 | sub main'comment | ||
| 380 | { | ||
| 381 | foreach (@_) | ||
| 382 | { | ||
| 383 | push(@out,"\t; $_\n"); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | sub main'public_label | ||
| 388 | { | ||
| 389 | $label{$_[0]}="_$_[0]" if (!defined($label{$_[0]})); | ||
| 390 | push(@out,"PUBLIC\t$label{$_[0]}\n"); | ||
| 391 | } | ||
| 392 | |||
| 393 | sub main'label | ||
| 394 | { | ||
| 395 | if (!defined($label{$_[0]})) | ||
| 396 | { | ||
| 397 | $label{$_[0]}="\$${label}${_[0]}"; | ||
| 398 | $label++; | ||
| 399 | } | ||
| 400 | return($label{$_[0]}); | ||
| 401 | } | ||
| 402 | |||
| 403 | sub main'set_label | ||
| 404 | { | ||
| 405 | if (!defined($label{$_[0]})) | ||
| 406 | { | ||
| 407 | $label{$_[0]}="\$${label}${_[0]}"; | ||
| 408 | $label++; | ||
| 409 | } | ||
| 410 | if ($_[1]!=0 && $_[1]>1) | ||
| 411 | { | ||
| 412 | main'align($_[1]); | ||
| 413 | } | ||
| 414 | if((defined $_[2]) && ($_[2] == 1)) | ||
| 415 | { | ||
| 416 | push(@out,"$label{$_[0]}::\n"); | ||
| 417 | } | ||
| 418 | elsif ($label{$_[0]} !~ /^\$/) | ||
| 419 | { | ||
| 420 | push(@out,"$label{$_[0]}\tLABEL PTR\n"); | ||
| 421 | } | ||
| 422 | else | ||
| 423 | { | ||
| 424 | push(@out,"$label{$_[0]}:\n"); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | sub main'data_byte | ||
| 429 | { | ||
| 430 | push(@out,"\tDB\t".join(',',@_)."\n"); | ||
| 431 | } | ||
| 432 | |||
| 433 | sub main'data_word | ||
| 434 | { | ||
| 435 | push(@out,"\tDD\t".join(',',@_)."\n"); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub main'align | ||
| 439 | { | ||
| 440 | push(@out,"\tALIGN\t$_[0]\n"); | ||
| 441 | } | ||
| 442 | |||
| 443 | sub out1p | ||
| 444 | { | ||
| 445 | local($name,$p1)=@_; | ||
| 446 | local($l,$t); | ||
| 447 | |||
| 448 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 449 | } | ||
| 450 | |||
| 451 | sub main'picmeup | ||
| 452 | { | ||
| 453 | local($dst,$sym)=@_; | ||
| 454 | &main'lea($dst,&main'DWP($sym)); | ||
| 455 | } | ||
| 456 | |||
| 457 | sub main'blindpop { &out1("pop",@_); } | ||
| 458 | |||
| 459 | sub main'initseg | ||
| 460 | { | ||
| 461 | local($f)=@_; | ||
| 462 | local($tmp)=<<___; | ||
| 463 | OPTION DOTNAME | ||
| 464 | .CRT\$XCU SEGMENT DWORD PUBLIC 'DATA' | ||
| 465 | EXTRN _$f:NEAR | ||
| 466 | DD _$f | ||
| 467 | .CRT\$XCU ENDS | ||
| 468 | ___ | ||
| 469 | push(@out,$tmp); | ||
| 470 | } | ||
| 471 | |||
| 472 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/x86nasm.pl b/src/lib/libcrypto/perlasm/x86nasm.pl new file mode 100644 index 0000000000..fa38f89c09 --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86nasm.pl | |||
| @@ -0,0 +1,455 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | package x86nasm; | ||
| 4 | |||
| 5 | $label="L000"; | ||
| 6 | $under=($main'netware)?'':'_'; | ||
| 7 | |||
| 8 | %lb=( 'eax', 'al', | ||
| 9 | 'ebx', 'bl', | ||
| 10 | 'ecx', 'cl', | ||
| 11 | 'edx', 'dl', | ||
| 12 | 'ax', 'al', | ||
| 13 | 'bx', 'bl', | ||
| 14 | 'cx', 'cl', | ||
| 15 | 'dx', 'dl', | ||
| 16 | ); | ||
| 17 | |||
| 18 | %hb=( 'eax', 'ah', | ||
| 19 | 'ebx', 'bh', | ||
| 20 | 'ecx', 'ch', | ||
| 21 | 'edx', 'dh', | ||
| 22 | 'ax', 'ah', | ||
| 23 | 'bx', 'bh', | ||
| 24 | 'cx', 'ch', | ||
| 25 | 'dx', 'dh', | ||
| 26 | ); | ||
| 27 | |||
| 28 | sub main'asm_init_output { @out=(); } | ||
| 29 | sub main'asm_get_output { return(@out); } | ||
| 30 | sub main'get_labels { return(@labels); } | ||
| 31 | |||
| 32 | sub main'external_label | ||
| 33 | { | ||
| 34 | push(@labels,@_); | ||
| 35 | foreach (@_) { | ||
| 36 | push(@out,".") if ($main'mwerks); | ||
| 37 | push(@out, "extern\t${under}$_\n"); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | sub main'LB | ||
| 42 | { | ||
| 43 | (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n"; | ||
| 44 | return($lb{$_[0]}); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub main'HB | ||
| 48 | { | ||
| 49 | (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n"; | ||
| 50 | return($hb{$_[0]}); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub main'BP | ||
| 54 | { | ||
| 55 | &get_mem("BYTE",@_); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub main'DWP | ||
| 59 | { | ||
| 60 | &get_mem("DWORD",@_); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub main'QWP | ||
| 64 | { | ||
| 65 | &get_mem("",@_); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub main'BC | ||
| 69 | { | ||
| 70 | return (($main'mwerks)?"":"BYTE ")."@_"; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub main'DWC | ||
| 74 | { | ||
| 75 | return (($main'mwerks)?"":"DWORD ")."@_"; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub main'stack_push | ||
| 79 | { | ||
| 80 | my($num)=@_; | ||
| 81 | $stack+=$num*4; | ||
| 82 | &main'sub("esp",$num*4); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub main'stack_pop | ||
| 86 | { | ||
| 87 | my($num)=@_; | ||
| 88 | $stack-=$num*4; | ||
| 89 | &main'add("esp",$num*4); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub get_mem | ||
| 93 | { | ||
| 94 | my($size,$addr,$reg1,$reg2,$idx)=@_; | ||
| 95 | my($t,$post); | ||
| 96 | my($ret)=$size; | ||
| 97 | if ($ret ne "") | ||
| 98 | { | ||
| 99 | $ret .= " PTR" if ($main'mwerks); | ||
| 100 | $ret .= " "; | ||
| 101 | } | ||
| 102 | $ret .= "["; | ||
| 103 | $addr =~ s/^\s+//; | ||
| 104 | if ($addr =~ /^(.+)\+(.+)$/) | ||
| 105 | { | ||
| 106 | $reg2=&conv($1); | ||
| 107 | $addr="$under$2"; | ||
| 108 | } | ||
| 109 | elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i) | ||
| 110 | { | ||
| 111 | $addr="$under$addr"; | ||
| 112 | } | ||
| 113 | |||
| 114 | if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; } | ||
| 115 | |||
| 116 | $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 117 | $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 118 | if (($addr ne "") && ($addr ne 0)) | ||
| 119 | { | ||
| 120 | if ($addr !~ /^-/) | ||
| 121 | { $ret.="${addr}+"; } | ||
| 122 | else { $post=$addr; } | ||
| 123 | } | ||
| 124 | if ($reg2 ne "") | ||
| 125 | { | ||
| 126 | $t=""; | ||
| 127 | $t="*$idx" if ($idx != 0); | ||
| 128 | $reg1="+".$reg1 if ("$reg1$post" ne ""); | ||
| 129 | $ret.="$reg2$t$reg1$post]"; | ||
| 130 | } | ||
| 131 | else | ||
| 132 | { | ||
| 133 | $ret.="$reg1$post]" | ||
| 134 | } | ||
| 135 | $ret =~ s/\+\]/]/; # in case $addr was the only argument | ||
| 136 | return($ret); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub main'mov { &out2("mov",@_); } | ||
| 140 | sub main'movb { &out2("mov",@_); } | ||
| 141 | sub main'and { &out2("and",@_); } | ||
| 142 | sub main'or { &out2("or",@_); } | ||
| 143 | sub main'shl { &out2("shl",@_); } | ||
| 144 | sub main'shr { &out2("shr",@_); } | ||
| 145 | sub main'xor { &out2("xor",@_); } | ||
| 146 | sub main'xorb { &out2("xor",@_); } | ||
| 147 | sub main'add { &out2("add",@_); } | ||
| 148 | sub main'adc { &out2("adc",@_); } | ||
| 149 | sub main'sub { &out2("sub",@_); } | ||
| 150 | sub main'sbb { &out2("sbb",@_); } | ||
| 151 | sub main'rotl { &out2("rol",@_); } | ||
| 152 | sub main'rotr { &out2("ror",@_); } | ||
| 153 | sub main'exch { &out2("xchg",@_); } | ||
| 154 | sub main'cmp { &out2("cmp",@_); } | ||
| 155 | sub main'lea { &out2("lea",@_); } | ||
| 156 | sub main'mul { &out1("mul",@_); } | ||
| 157 | sub main'imul { &out2("imul",@_); } | ||
| 158 | sub main'div { &out1("div",@_); } | ||
| 159 | sub main'dec { &out1("dec",@_); } | ||
| 160 | sub main'inc { &out1("inc",@_); } | ||
| 161 | sub main'jmp { &out1("jmp",@_); } | ||
| 162 | sub main'jmp_ptr { &out1p("jmp",@_); } | ||
| 163 | |||
| 164 | # This is a bit of a kludge: declare all branches as NEAR. | ||
| 165 | $near=($main'mwerks)?'':'NEAR'; | ||
| 166 | sub main'je { &out1("je $near",@_); } | ||
| 167 | sub main'jle { &out1("jle $near",@_); } | ||
| 168 | sub main'jz { &out1("jz $near",@_); } | ||
| 169 | sub main'jge { &out1("jge $near",@_); } | ||
| 170 | sub main'jl { &out1("jl $near",@_); } | ||
| 171 | sub main'ja { &out1("ja $near",@_); } | ||
| 172 | sub main'jae { &out1("jae $near",@_); } | ||
| 173 | sub main'jb { &out1("jb $near",@_); } | ||
| 174 | sub main'jbe { &out1("jbe $near",@_); } | ||
| 175 | sub main'jc { &out1("jc $near",@_); } | ||
| 176 | sub main'jnc { &out1("jnc $near",@_); } | ||
| 177 | sub main'jnz { &out1("jnz $near",@_); } | ||
| 178 | sub main'jne { &out1("jne $near",@_); } | ||
| 179 | sub main'jno { &out1("jno $near",@_); } | ||
| 180 | |||
| 181 | sub main'push { &out1("push",@_); $stack+=4; } | ||
| 182 | sub main'pop { &out1("pop",@_); $stack-=4; } | ||
| 183 | sub main'pushf { &out0("pushfd"); $stack+=4; } | ||
| 184 | sub main'popf { &out0("popfd"); $stack-=4; } | ||
| 185 | sub main'bswap { &out1("bswap",@_); &using486(); } | ||
| 186 | sub main'not { &out1("not",@_); } | ||
| 187 | sub main'call { &out1("call",($_[0]=~/^\@L/?'':$under).$_[0]); } | ||
| 188 | sub main'call_ptr { &out1p("call",@_); } | ||
| 189 | sub main'ret { &out0("ret"); } | ||
| 190 | sub main'nop { &out0("nop"); } | ||
| 191 | sub main'test { &out2("test",@_); } | ||
| 192 | sub main'bt { &out2("bt",@_); } | ||
| 193 | sub main'leave { &out0("leave"); } | ||
| 194 | sub main'cpuid { &out0("cpuid"); } | ||
| 195 | sub main'rdtsc { &out0("rdtsc"); } | ||
| 196 | sub main'halt { &out0("hlt"); } | ||
| 197 | sub main'movz { &out2("movzx",@_); } | ||
| 198 | sub main'neg { &out1("neg",@_); } | ||
| 199 | sub main'cld { &out0("cld"); } | ||
| 200 | |||
| 201 | # SSE2 | ||
| 202 | sub main'emms { &out0("emms"); } | ||
| 203 | sub main'movd { &out2("movd",@_); } | ||
| 204 | sub main'movq { &out2("movq",@_); } | ||
| 205 | sub main'movdqu { &out2("movdqu",@_); } | ||
| 206 | sub main'movdqa { &out2("movdqa",@_); } | ||
| 207 | sub main'movdq2q{ &out2("movdq2q",@_); } | ||
| 208 | sub main'movq2dq{ &out2("movq2dq",@_); } | ||
| 209 | sub main'paddq { &out2("paddq",@_); } | ||
| 210 | sub main'pmuludq{ &out2("pmuludq",@_); } | ||
| 211 | sub main'psrlq { &out2("psrlq",@_); } | ||
| 212 | sub main'psllq { &out2("psllq",@_); } | ||
| 213 | sub main'pxor { &out2("pxor",@_); } | ||
| 214 | sub main'por { &out2("por",@_); } | ||
| 215 | sub main'pand { &out2("pand",@_); } | ||
| 216 | |||
| 217 | sub out2 | ||
| 218 | { | ||
| 219 | my($name,$p1,$p2)=@_; | ||
| 220 | my($l,$t); | ||
| 221 | |||
| 222 | push(@out,"\t$name\t"); | ||
| 223 | if (!$main'mwerks and $name eq "lea") | ||
| 224 | { | ||
| 225 | $p1 =~ s/^[^\[]*\[/\[/; | ||
| 226 | $p2 =~ s/^[^\[]*\[/\[/; | ||
| 227 | } | ||
| 228 | $t=&conv($p1).","; | ||
| 229 | $l=length($t); | ||
| 230 | push(@out,$t); | ||
| 231 | $l=4-($l+9)/8; | ||
| 232 | push(@out,"\t" x $l); | ||
| 233 | push(@out,&conv($p2)); | ||
| 234 | push(@out,"\n"); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub out0 | ||
| 238 | { | ||
| 239 | my($name)=@_; | ||
| 240 | |||
| 241 | push(@out,"\t$name\n"); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub out1 | ||
| 245 | { | ||
| 246 | my($name,$p1)=@_; | ||
| 247 | my($l,$t); | ||
| 248 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub conv | ||
| 252 | { | ||
| 253 | my($p)=@_; | ||
| 254 | $p =~ s/0x([0-9A-Fa-f]+)/0$1h/; | ||
| 255 | return $p; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub using486 | ||
| 259 | { | ||
| 260 | return if $using486; | ||
| 261 | $using486++; | ||
| 262 | grep(s/\.386/\.486/,@out); | ||
| 263 | } | ||
| 264 | |||
| 265 | sub main'file | ||
| 266 | { | ||
| 267 | if ($main'mwerks) { push(@out,".section\t.text\n"); } | ||
| 268 | else { | ||
| 269 | local $tmp=<<___; | ||
| 270 | %ifdef __omf__ | ||
| 271 | section code use32 class=code | ||
| 272 | %else | ||
| 273 | section .text | ||
| 274 | %endif | ||
| 275 | ___ | ||
| 276 | push(@out,$tmp); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | sub main'function_begin | ||
| 281 | { | ||
| 282 | my($func,$extra)=@_; | ||
| 283 | |||
| 284 | push(@labels,$func); | ||
| 285 | push(@out,".") if ($main'mwerks); | ||
| 286 | my($tmp)=<<"EOF"; | ||
| 287 | global $under$func | ||
| 288 | $under$func: | ||
| 289 | push ebp | ||
| 290 | push ebx | ||
| 291 | push esi | ||
| 292 | push edi | ||
| 293 | EOF | ||
| 294 | push(@out,$tmp); | ||
| 295 | $stack=20; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub main'function_begin_B | ||
| 299 | { | ||
| 300 | my($func,$extra)=@_; | ||
| 301 | push(@out,".") if ($main'mwerks); | ||
| 302 | my($tmp)=<<"EOF"; | ||
| 303 | global $under$func | ||
| 304 | $under$func: | ||
| 305 | EOF | ||
| 306 | push(@out,$tmp); | ||
| 307 | $stack=4; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub main'function_end | ||
| 311 | { | ||
| 312 | my($func)=@_; | ||
| 313 | |||
| 314 | my($tmp)=<<"EOF"; | ||
| 315 | pop edi | ||
| 316 | pop esi | ||
| 317 | pop ebx | ||
| 318 | pop ebp | ||
| 319 | ret | ||
| 320 | EOF | ||
| 321 | push(@out,$tmp); | ||
| 322 | $stack=0; | ||
| 323 | %label=(); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub main'function_end_B | ||
| 327 | { | ||
| 328 | $stack=0; | ||
| 329 | %label=(); | ||
| 330 | } | ||
| 331 | |||
| 332 | sub main'function_end_A | ||
| 333 | { | ||
| 334 | my($func)=@_; | ||
| 335 | |||
| 336 | my($tmp)=<<"EOF"; | ||
| 337 | pop edi | ||
| 338 | pop esi | ||
| 339 | pop ebx | ||
| 340 | pop ebp | ||
| 341 | ret | ||
| 342 | EOF | ||
| 343 | push(@out,$tmp); | ||
| 344 | } | ||
| 345 | |||
| 346 | sub main'file_end | ||
| 347 | { | ||
| 348 | } | ||
| 349 | |||
| 350 | sub main'wparam | ||
| 351 | { | ||
| 352 | my($num)=@_; | ||
| 353 | |||
| 354 | return(&main'DWP($stack+$num*4,"esp","",0)); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub main'swtmp | ||
| 358 | { | ||
| 359 | return(&main'DWP($_[0]*4,"esp","",0)); | ||
| 360 | } | ||
| 361 | |||
| 362 | # Should use swtmp, which is above esp. Linix can trash the stack above esp | ||
| 363 | #sub main'wtmp | ||
| 364 | # { | ||
| 365 | # my($num)=@_; | ||
| 366 | # | ||
| 367 | # return(&main'DWP(-(($num+1)*4),"esp","",0)); | ||
| 368 | # } | ||
| 369 | |||
| 370 | sub main'comment | ||
| 371 | { | ||
| 372 | foreach (@_) | ||
| 373 | { | ||
| 374 | push(@out,"\t; $_\n"); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | sub main'public_label | ||
| 379 | { | ||
| 380 | $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]})); | ||
| 381 | push(@out,".") if ($main'mwerks); | ||
| 382 | push(@out,"global\t$label{$_[0]}\n"); | ||
| 383 | } | ||
| 384 | |||
| 385 | sub main'label | ||
| 386 | { | ||
| 387 | if (!defined($label{$_[0]})) | ||
| 388 | { | ||
| 389 | $label{$_[0]}="\@${label}${_[0]}"; | ||
| 390 | $label++; | ||
| 391 | } | ||
| 392 | return($label{$_[0]}); | ||
| 393 | } | ||
| 394 | |||
| 395 | sub main'set_label | ||
| 396 | { | ||
| 397 | if (!defined($label{$_[0]})) | ||
| 398 | { | ||
| 399 | $label{$_[0]}="\@${label}${_[0]}"; | ||
| 400 | $label++; | ||
| 401 | } | ||
| 402 | if ($_[1]!=0 && $_[1]>1) | ||
| 403 | { | ||
| 404 | main'align($_[1]); | ||
| 405 | } | ||
| 406 | push(@out,"$label{$_[0]}:\n"); | ||
| 407 | } | ||
| 408 | |||
| 409 | sub main'data_byte | ||
| 410 | { | ||
| 411 | push(@out,(($main'mwerks)?".byte\t":"DB\t").join(',',@_)."\n"); | ||
| 412 | } | ||
| 413 | |||
| 414 | sub main'data_word | ||
| 415 | { | ||
| 416 | push(@out,(($main'mwerks)?".long\t":"DD\t").join(',',@_)."\n"); | ||
| 417 | } | ||
| 418 | |||
| 419 | sub main'align | ||
| 420 | { | ||
| 421 | push(@out,".") if ($main'mwerks); | ||
| 422 | push(@out,"align\t$_[0]\n"); | ||
| 423 | } | ||
| 424 | |||
| 425 | sub out1p | ||
| 426 | { | ||
| 427 | my($name,$p1)=@_; | ||
| 428 | my($l,$t); | ||
| 429 | |||
| 430 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 431 | } | ||
| 432 | |||
| 433 | sub main'picmeup | ||
| 434 | { | ||
| 435 | local($dst,$sym)=@_; | ||
| 436 | &main'lea($dst,&main'DWP($sym)); | ||
| 437 | } | ||
| 438 | |||
| 439 | sub main'blindpop { &out1("pop",@_); } | ||
| 440 | |||
| 441 | sub main'initseg | ||
| 442 | { | ||
| 443 | local($f)=@_; | ||
| 444 | if ($main'win32) | ||
| 445 | { | ||
| 446 | local($tmp)=<<___; | ||
| 447 | segment .CRT\$XCU data | ||
| 448 | extern $under$f | ||
| 449 | DD $under$f | ||
| 450 | ___ | ||
| 451 | push(@out,$tmp); | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | 1; | ||
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; | ||
