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 | 349 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/readme | 124 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86asm.pl | 135 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86ms.pl | 380 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86nasm.pl | 362 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86unix.pl | 628 |
7 files changed, 2412 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..22149c680e --- /dev/null +++ b/src/lib/libcrypto/perlasm/cbc.pl | |||
| @@ -0,0 +1,349 @@ | |||
| 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 | &set_label("cbc_enc_jmp_table",1); | ||
| 326 | &data_word("0"); | ||
| 327 | &data_word(&label("ej1")."-".&label("PIC_point")); | ||
| 328 | &data_word(&label("ej2")."-".&label("PIC_point")); | ||
| 329 | &data_word(&label("ej3")."-".&label("PIC_point")); | ||
| 330 | &data_word(&label("ej4")."-".&label("PIC_point")); | ||
| 331 | &data_word(&label("ej5")."-".&label("PIC_point")); | ||
| 332 | &data_word(&label("ej6")."-".&label("PIC_point")); | ||
| 333 | &data_word(&label("ej7")."-".&label("PIC_point")); | ||
| 334 | # not used | ||
| 335 | #&set_label("cbc_dec_jmp_table",1); | ||
| 336 | #&data_word("0"); | ||
| 337 | #&data_word(&label("dj1")."-".&label("PIC_point")); | ||
| 338 | #&data_word(&label("dj2")."-".&label("PIC_point")); | ||
| 339 | #&data_word(&label("dj3")."-".&label("PIC_point")); | ||
| 340 | #&data_word(&label("dj4")."-".&label("PIC_point")); | ||
| 341 | #&data_word(&label("dj5")."-".&label("PIC_point")); | ||
| 342 | #&data_word(&label("dj6")."-".&label("PIC_point")); | ||
| 343 | #&data_word(&label("dj7")."-".&label("PIC_point")); | ||
| 344 | |||
| 345 | &function_end_B($name); | ||
| 346 | |||
| 347 | } | ||
| 348 | |||
| 349 | 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/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl new file mode 100644 index 0000000000..60233f80e8 --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86asm.pl | |||
| @@ -0,0 +1,135 @@ | |||
| 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=$sol=$aout=$win32=$gaswin=$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 "gaswin")) | ||
| 31 | { $gaswin=1; $aout=1; require "x86unix.pl"; } | ||
| 32 | elsif ( ($type eq "sol")) | ||
| 33 | { $sol=1; require "x86unix.pl"; } | ||
| 34 | elsif ( ($type eq "cpp")) | ||
| 35 | { $cpp=1; require "x86unix.pl"; } | ||
| 36 | elsif ( ($type eq "win32")) | ||
| 37 | { $win32=1; require "x86ms.pl"; } | ||
| 38 | elsif ( ($type eq "win32n")) | ||
| 39 | { $win32=1; require "x86nasm.pl"; } | ||
| 40 | else | ||
| 41 | { | ||
| 42 | print STDERR <<"EOF"; | ||
| 43 | Pick one target type from | ||
| 44 | elf - linux, FreeBSD etc | ||
| 45 | a.out - old linux | ||
| 46 | sol - x86 solaris | ||
| 47 | cpp - format so x86unix.cpp can be used | ||
| 48 | win32 - Windows 95/Windows NT | ||
| 49 | win32n - Windows 95/Windows NT NASM format | ||
| 50 | openbsd-elf - OpenBSD elf | ||
| 51 | openbsd-a.out - OpenBSD a.out | ||
| 52 | EOF | ||
| 53 | exit(1); | ||
| 54 | } | ||
| 55 | |||
| 56 | $pic=0; | ||
| 57 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } | ||
| 58 | |||
| 59 | &asm_init_output(); | ||
| 60 | |||
| 61 | &comment("Don't even think of reading this code"); | ||
| 62 | &comment("It was automatically generated by $filename"); | ||
| 63 | &comment("Which is a perl program used to generate the x86 assember for"); | ||
| 64 | &comment("any of elf, a.out, BSDI, Win32, gaswin (for GNU as on Win32) or Solaris"); | ||
| 65 | &comment("eric <eay\@cryptsoft.com>"); | ||
| 66 | &comment(""); | ||
| 67 | |||
| 68 | $filename =~ s/\.pl$//; | ||
| 69 | &file($filename); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub asm_finish_cpp | ||
| 73 | { | ||
| 74 | return unless $cpp; | ||
| 75 | |||
| 76 | local($tmp,$i); | ||
| 77 | foreach $i (&get_labels()) | ||
| 78 | { | ||
| 79 | $tmp.="#define $i _$i\n"; | ||
| 80 | } | ||
| 81 | print <<"EOF"; | ||
| 82 | /* Run the C pre-processor over this file with one of the following defined | ||
| 83 | * ELF - elf object files, | ||
| 84 | * OUT - a.out object files, | ||
| 85 | * BSDI - BSDI style a.out object files | ||
| 86 | * SOL - Solaris style elf | ||
| 87 | */ | ||
| 88 | |||
| 89 | #define TYPE(a,b) .type a,b | ||
| 90 | #define SIZE(a,b) .size a,b | ||
| 91 | |||
| 92 | #if defined(OUT) || (defined(BSDI) && !defined(ELF)) | ||
| 93 | $tmp | ||
| 94 | #endif | ||
| 95 | |||
| 96 | #ifdef OUT | ||
| 97 | #define OK 1 | ||
| 98 | #define ALIGN 4 | ||
| 99 | #if defined(__CYGWIN__) || defined(__DJGPP__) | ||
| 100 | #undef SIZE | ||
| 101 | #undef TYPE | ||
| 102 | #define SIZE(a,b) | ||
| 103 | #define TYPE(a,b) .def a; .scl 2; .type 32; .endef | ||
| 104 | #endif /* __CYGWIN || __DJGPP */ | ||
| 105 | #endif | ||
| 106 | |||
| 107 | #if defined(BSDI) && !defined(ELF) | ||
| 108 | #define OK 1 | ||
| 109 | #define ALIGN 4 | ||
| 110 | #undef SIZE | ||
| 111 | #undef TYPE | ||
| 112 | #define SIZE(a,b) | ||
| 113 | #define TYPE(a,b) | ||
| 114 | #endif | ||
| 115 | |||
| 116 | #if defined(ELF) || defined(SOL) | ||
| 117 | #define OK 1 | ||
| 118 | #define ALIGN 16 | ||
| 119 | #endif | ||
| 120 | |||
| 121 | #ifndef OK | ||
| 122 | You need to define one of | ||
| 123 | ELF - elf systems - linux-elf, NetBSD and DG-UX | ||
| 124 | OUT - a.out systems - linux-a.out and FreeBSD | ||
| 125 | SOL - solaris systems, which are elf with strange comment lines | ||
| 126 | BSDI - a.out with a very primative version of as. | ||
| 127 | #endif | ||
| 128 | |||
| 129 | /* Let the Assembler begin :-) */ | ||
| 130 | EOF | ||
| 131 | } | ||
| 132 | |||
| 133 | sub main'align() {} # swallow align statements in 0.9.7 context | ||
| 134 | |||
| 135 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/x86ms.pl b/src/lib/libcrypto/perlasm/x86ms.pl new file mode 100644 index 0000000000..b6bd744057 --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86ms.pl | |||
| @@ -0,0 +1,380 @@ | |||
| 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 { push(@labels,@_); } | ||
| 31 | |||
| 32 | sub main'LB | ||
| 33 | { | ||
| 34 | (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n"; | ||
| 35 | return($lb{$_[0]}); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub main'HB | ||
| 39 | { | ||
| 40 | (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n"; | ||
| 41 | return($hb{$_[0]}); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub main'BP | ||
| 45 | { | ||
| 46 | &get_mem("BYTE",@_); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub main'DWP | ||
| 50 | { | ||
| 51 | &get_mem("DWORD",@_); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub main'BC | ||
| 55 | { | ||
| 56 | return @_; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub main'DWC | ||
| 60 | { | ||
| 61 | return @_; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub main'stack_push | ||
| 65 | { | ||
| 66 | local($num)=@_; | ||
| 67 | $stack+=$num*4; | ||
| 68 | &main'sub("esp",$num*4); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub main'stack_pop | ||
| 72 | { | ||
| 73 | local($num)=@_; | ||
| 74 | $stack-=$num*4; | ||
| 75 | &main'add("esp",$num*4); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub get_mem | ||
| 79 | { | ||
| 80 | local($size,$addr,$reg1,$reg2,$idx)=@_; | ||
| 81 | local($t,$post); | ||
| 82 | local($ret)="$size PTR "; | ||
| 83 | |||
| 84 | $addr =~ s/^\s+//; | ||
| 85 | if ($addr =~ /^(.+)\+(.+)$/) | ||
| 86 | { | ||
| 87 | $reg2=&conv($1); | ||
| 88 | $addr="_$2"; | ||
| 89 | } | ||
| 90 | elsif ($addr =~ /^[_a-zA-Z]/) | ||
| 91 | { | ||
| 92 | $addr="_$addr"; | ||
| 93 | } | ||
| 94 | |||
| 95 | if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; } | ||
| 96 | |||
| 97 | $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 98 | $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 99 | if (($addr ne "") && ($addr ne 0)) | ||
| 100 | { | ||
| 101 | if ($addr !~ /^-/) | ||
| 102 | { $ret.=$addr; } | ||
| 103 | else { $post=$addr; } | ||
| 104 | } | ||
| 105 | if ($reg2 ne "") | ||
| 106 | { | ||
| 107 | $t=""; | ||
| 108 | $t="*$idx" if ($idx != 0); | ||
| 109 | $reg1="+".$reg1 if ("$reg1$post" ne ""); | ||
| 110 | $ret.="[$reg2$t$reg1$post]"; | ||
| 111 | } | ||
| 112 | else | ||
| 113 | { | ||
| 114 | $ret.="[$reg1$post]" | ||
| 115 | } | ||
| 116 | $ret =~ s/\[\]//; # in case $addr was the only argument | ||
| 117 | return($ret); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub main'mov { &out2("mov",@_); } | ||
| 121 | sub main'movb { &out2("mov",@_); } | ||
| 122 | sub main'and { &out2("and",@_); } | ||
| 123 | sub main'or { &out2("or",@_); } | ||
| 124 | sub main'shl { &out2("shl",@_); } | ||
| 125 | sub main'shr { &out2("shr",@_); } | ||
| 126 | sub main'xor { &out2("xor",@_); } | ||
| 127 | sub main'xorb { &out2("xor",@_); } | ||
| 128 | sub main'add { &out2("add",@_); } | ||
| 129 | sub main'adc { &out2("adc",@_); } | ||
| 130 | sub main'sub { &out2("sub",@_); } | ||
| 131 | sub main'rotl { &out2("rol",@_); } | ||
| 132 | sub main'rotr { &out2("ror",@_); } | ||
| 133 | sub main'exch { &out2("xchg",@_); } | ||
| 134 | sub main'cmp { &out2("cmp",@_); } | ||
| 135 | sub main'lea { &out2("lea",@_); } | ||
| 136 | sub main'mul { &out1("mul",@_); } | ||
| 137 | sub main'div { &out1("div",@_); } | ||
| 138 | sub main'dec { &out1("dec",@_); } | ||
| 139 | sub main'inc { &out1("inc",@_); } | ||
| 140 | sub main'jmp { &out1("jmp",@_); } | ||
| 141 | sub main'jmp_ptr { &out1p("jmp",@_); } | ||
| 142 | sub main'je { &out1("je",@_); } | ||
| 143 | sub main'jle { &out1("jle",@_); } | ||
| 144 | sub main'jz { &out1("jz",@_); } | ||
| 145 | sub main'jge { &out1("jge",@_); } | ||
| 146 | sub main'jl { &out1("jl",@_); } | ||
| 147 | sub main'ja { &out1("ja",@_); } | ||
| 148 | sub main'jae { &out1("jae",@_); } | ||
| 149 | sub main'jb { &out1("jb",@_); } | ||
| 150 | sub main'jbe { &out1("jbe",@_); } | ||
| 151 | sub main'jc { &out1("jc",@_); } | ||
| 152 | sub main'jnc { &out1("jnc",@_); } | ||
| 153 | sub main'jnz { &out1("jnz",@_); } | ||
| 154 | sub main'jne { &out1("jne",@_); } | ||
| 155 | sub main'jno { &out1("jno",@_); } | ||
| 156 | sub main'push { &out1("push",@_); $stack+=4; } | ||
| 157 | sub main'pop { &out1("pop",@_); $stack-=4; } | ||
| 158 | sub main'bswap { &out1("bswap",@_); &using486(); } | ||
| 159 | sub main'not { &out1("not",@_); } | ||
| 160 | sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); } | ||
| 161 | sub main'ret { &out0("ret"); } | ||
| 162 | sub main'nop { &out0("nop"); } | ||
| 163 | sub main'movz { &out2("movzx",@_); } | ||
| 164 | |||
| 165 | sub out2 | ||
| 166 | { | ||
| 167 | local($name,$p1,$p2)=@_; | ||
| 168 | local($l,$t); | ||
| 169 | |||
| 170 | push(@out,"\t$name\t"); | ||
| 171 | $t=&conv($p1).","; | ||
| 172 | $l=length($t); | ||
| 173 | push(@out,$t); | ||
| 174 | $l=4-($l+9)/8; | ||
| 175 | push(@out,"\t" x $l); | ||
| 176 | push(@out,&conv($p2)); | ||
| 177 | push(@out,"\n"); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub out0 | ||
| 181 | { | ||
| 182 | local($name)=@_; | ||
| 183 | |||
| 184 | push(@out,"\t$name\n"); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub out1 | ||
| 188 | { | ||
| 189 | local($name,$p1)=@_; | ||
| 190 | local($l,$t); | ||
| 191 | |||
| 192 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub conv | ||
| 196 | { | ||
| 197 | local($p)=@_; | ||
| 198 | |||
| 199 | $p =~ s/0x([0-9A-Fa-f]+)/0$1h/; | ||
| 200 | return $p; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub using486 | ||
| 204 | { | ||
| 205 | return if $using486; | ||
| 206 | $using486++; | ||
| 207 | grep(s/\.386/\.486/,@out); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub main'file | ||
| 211 | { | ||
| 212 | local($file)=@_; | ||
| 213 | |||
| 214 | local($tmp)=<<"EOF"; | ||
| 215 | TITLE $file.asm | ||
| 216 | .386 | ||
| 217 | .model FLAT | ||
| 218 | EOF | ||
| 219 | push(@out,$tmp); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub main'function_begin | ||
| 223 | { | ||
| 224 | local($func,$extra)=@_; | ||
| 225 | |||
| 226 | push(@labels,$func); | ||
| 227 | |||
| 228 | local($tmp)=<<"EOF"; | ||
| 229 | _TEXT SEGMENT | ||
| 230 | PUBLIC _$func | ||
| 231 | $extra | ||
| 232 | _$func PROC NEAR | ||
| 233 | push ebp | ||
| 234 | push ebx | ||
| 235 | push esi | ||
| 236 | push edi | ||
| 237 | EOF | ||
| 238 | push(@out,$tmp); | ||
| 239 | $stack=20; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub main'function_begin_B | ||
| 243 | { | ||
| 244 | local($func,$extra)=@_; | ||
| 245 | |||
| 246 | local($tmp)=<<"EOF"; | ||
| 247 | _TEXT SEGMENT | ||
| 248 | PUBLIC _$func | ||
| 249 | $extra | ||
| 250 | _$func PROC NEAR | ||
| 251 | EOF | ||
| 252 | push(@out,$tmp); | ||
| 253 | $stack=4; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub main'function_end | ||
| 257 | { | ||
| 258 | local($func)=@_; | ||
| 259 | |||
| 260 | local($tmp)=<<"EOF"; | ||
| 261 | pop edi | ||
| 262 | pop esi | ||
| 263 | pop ebx | ||
| 264 | pop ebp | ||
| 265 | ret | ||
| 266 | _$func ENDP | ||
| 267 | _TEXT ENDS | ||
| 268 | EOF | ||
| 269 | push(@out,$tmp); | ||
| 270 | $stack=0; | ||
| 271 | %label=(); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub main'function_end_B | ||
| 275 | { | ||
| 276 | local($func)=@_; | ||
| 277 | |||
| 278 | local($tmp)=<<"EOF"; | ||
| 279 | _$func ENDP | ||
| 280 | _TEXT ENDS | ||
| 281 | EOF | ||
| 282 | push(@out,$tmp); | ||
| 283 | $stack=0; | ||
| 284 | %label=(); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub main'function_end_A | ||
| 288 | { | ||
| 289 | local($func)=@_; | ||
| 290 | |||
| 291 | local($tmp)=<<"EOF"; | ||
| 292 | pop edi | ||
| 293 | pop esi | ||
| 294 | pop ebx | ||
| 295 | pop ebp | ||
| 296 | ret | ||
| 297 | EOF | ||
| 298 | push(@out,$tmp); | ||
| 299 | } | ||
| 300 | |||
| 301 | sub main'file_end | ||
| 302 | { | ||
| 303 | push(@out,"END\n"); | ||
| 304 | } | ||
| 305 | |||
| 306 | sub main'wparam | ||
| 307 | { | ||
| 308 | local($num)=@_; | ||
| 309 | |||
| 310 | return(&main'DWP($stack+$num*4,"esp","",0)); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub main'swtmp | ||
| 314 | { | ||
| 315 | return(&main'DWP($_[0]*4,"esp","",0)); | ||
| 316 | } | ||
| 317 | |||
| 318 | # Should use swtmp, which is above esp. Linix can trash the stack above esp | ||
| 319 | #sub main'wtmp | ||
| 320 | # { | ||
| 321 | # local($num)=@_; | ||
| 322 | # | ||
| 323 | # return(&main'DWP(-(($num+1)*4),"esp","",0)); | ||
| 324 | # } | ||
| 325 | |||
| 326 | sub main'comment | ||
| 327 | { | ||
| 328 | foreach (@_) | ||
| 329 | { | ||
| 330 | push(@out,"\t; $_\n"); | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | sub main'label | ||
| 335 | { | ||
| 336 | if (!defined($label{$_[0]})) | ||
| 337 | { | ||
| 338 | $label{$_[0]}="\$${label}${_[0]}"; | ||
| 339 | $label++; | ||
| 340 | } | ||
| 341 | return($label{$_[0]}); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub main'set_label | ||
| 345 | { | ||
| 346 | if (!defined($label{$_[0]})) | ||
| 347 | { | ||
| 348 | $label{$_[0]}="\$${label}${_[0]}"; | ||
| 349 | $label++; | ||
| 350 | } | ||
| 351 | if((defined $_[2]) && ($_[2] == 1)) | ||
| 352 | { | ||
| 353 | push(@out,"$label{$_[0]}::\n"); | ||
| 354 | } | ||
| 355 | else | ||
| 356 | { | ||
| 357 | push(@out,"$label{$_[0]}:\n"); | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | sub main'data_word | ||
| 362 | { | ||
| 363 | push(@out,"\tDD\t$_[0]\n"); | ||
| 364 | } | ||
| 365 | |||
| 366 | sub out1p | ||
| 367 | { | ||
| 368 | local($name,$p1)=@_; | ||
| 369 | local($l,$t); | ||
| 370 | |||
| 371 | push(@out,"\t$name\t ".&conv($p1)."\n"); | ||
| 372 | } | ||
| 373 | |||
| 374 | sub main'picmeup | ||
| 375 | { | ||
| 376 | local($dst,$sym)=@_; | ||
| 377 | &main'lea($dst,&main'DWP($sym)); | ||
| 378 | } | ||
| 379 | |||
| 380 | sub main'blindpop { &out1("pop",@_); } | ||
diff --git a/src/lib/libcrypto/perlasm/x86nasm.pl b/src/lib/libcrypto/perlasm/x86nasm.pl new file mode 100644 index 0000000000..5009acb4b3 --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86nasm.pl | |||
| @@ -0,0 +1,362 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | package x86nasm; | ||
| 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 | |||
| 31 | sub main'external_label | ||
| 32 | { | ||
| 33 | push(@labels,@_); | ||
| 34 | foreach (@_) { | ||
| 35 | push(@out, "extern\t_$_\n"); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | sub main'LB | ||
| 40 | { | ||
| 41 | (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n"; | ||
| 42 | return($lb{$_[0]}); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub main'HB | ||
| 46 | { | ||
| 47 | (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n"; | ||
| 48 | return($hb{$_[0]}); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub main'BP | ||
| 52 | { | ||
| 53 | &get_mem("BYTE",@_); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub main'DWP | ||
| 57 | { | ||
| 58 | &get_mem("DWORD",@_); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub main'BC | ||
| 62 | { | ||
| 63 | return "BYTE @_"; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub main'DWC | ||
| 67 | { | ||
| 68 | return "DWORD @_"; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub main'stack_push | ||
| 72 | { | ||
| 73 | my($num)=@_; | ||
| 74 | $stack+=$num*4; | ||
| 75 | &main'sub("esp",$num*4); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub main'stack_pop | ||
| 79 | { | ||
| 80 | my($num)=@_; | ||
| 81 | $stack-=$num*4; | ||
| 82 | &main'add("esp",$num*4); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub get_mem | ||
| 86 | { | ||
| 87 | my($size,$addr,$reg1,$reg2,$idx)=@_; | ||
| 88 | my($t,$post); | ||
| 89 | my($ret)="$size ["; | ||
| 90 | $addr =~ s/^\s+//; | ||
| 91 | if ($addr =~ /^(.+)\+(.+)$/) | ||
| 92 | { | ||
| 93 | $reg2=&conv($1); | ||
| 94 | $addr="_$2"; | ||
| 95 | } | ||
| 96 | elsif ($addr =~ /^[_a-zA-Z]/) | ||
| 97 | { | ||
| 98 | $addr="_$addr"; | ||
| 99 | } | ||
| 100 | |||
| 101 | if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; } | ||
| 102 | |||
| 103 | $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 104 | $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 105 | if (($addr ne "") && ($addr ne 0)) | ||
| 106 | { | ||
| 107 | if ($addr !~ /^-/) | ||
| 108 | { $ret.="${addr}+"; } | ||
| 109 | else { $post=$addr; } | ||
| 110 | } | ||
| 111 | if ($reg2 ne "") | ||
| 112 | { | ||
| 113 | $t=""; | ||
| 114 | $t="*$idx" if ($idx != 0); | ||
| 115 | $reg1="+".$reg1 if ("$reg1$post" ne ""); | ||
| 116 | $ret.="$reg2$t$reg1$post]"; | ||
| 117 | } | ||
| 118 | else | ||
| 119 | { | ||
| 120 | $ret.="$reg1$post]" | ||
| 121 | } | ||
| 122 | $ret =~ s/\+\]/]/; # in case $addr was the only argument | ||
| 123 | return($ret); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub main'mov { &out2("mov",@_); } | ||
| 127 | sub main'movb { &out2("mov",@_); } | ||
| 128 | sub main'and { &out2("and",@_); } | ||
| 129 | sub main'or { &out2("or",@_); } | ||
| 130 | sub main'shl { &out2("shl",@_); } | ||
| 131 | sub main'shr { &out2("shr",@_); } | ||
| 132 | sub main'xor { &out2("xor",@_); } | ||
| 133 | sub main'xorb { &out2("xor",@_); } | ||
| 134 | sub main'add { &out2("add",@_); } | ||
| 135 | sub main'adc { &out2("adc",@_); } | ||
| 136 | sub main'sub { &out2("sub",@_); } | ||
| 137 | sub main'rotl { &out2("rol",@_); } | ||
| 138 | sub main'rotr { &out2("ror",@_); } | ||
| 139 | sub main'exch { &out2("xchg",@_); } | ||
| 140 | sub main'cmp { &out2("cmp",@_); } | ||
| 141 | sub main'lea { &out2("lea",@_); } | ||
| 142 | sub main'mul { &out1("mul",@_); } | ||
| 143 | sub main'div { &out1("div",@_); } | ||
| 144 | sub main'dec { &out1("dec",@_); } | ||
| 145 | sub main'inc { &out1("inc",@_); } | ||
| 146 | sub main'jmp { &out1("jmp",@_); } | ||
| 147 | sub main'jmp_ptr { &out1p("jmp",@_); } | ||
| 148 | |||
| 149 | # This is a bit of a kludge: declare all branches as NEAR. | ||
| 150 | sub main'je { &out1("je NEAR",@_); } | ||
| 151 | sub main'jle { &out1("jle NEAR",@_); } | ||
| 152 | sub main'jz { &out1("jz NEAR",@_); } | ||
| 153 | sub main'jge { &out1("jge NEAR",@_); } | ||
| 154 | sub main'jl { &out1("jl NEAR",@_); } | ||
| 155 | sub main'ja { &out1("ja NEAR",@_); } | ||
| 156 | sub main'jae { &out1("jae NEAR",@_); } | ||
| 157 | sub main'jb { &out1("jb NEAR",@_); } | ||
| 158 | sub main'jbe { &out1("jbe NEAR",@_); } | ||
| 159 | sub main'jc { &out1("jc NEAR",@_); } | ||
| 160 | sub main'jnc { &out1("jnc NEAR",@_); } | ||
| 161 | sub main'jnz { &out1("jnz NEAR",@_); } | ||
| 162 | sub main'jne { &out1("jne NEAR",@_); } | ||
| 163 | sub main'jno { &out1("jno NEAR",@_); } | ||
| 164 | |||
| 165 | sub main'push { &out1("push",@_); $stack+=4; } | ||
| 166 | sub main'pop { &out1("pop",@_); $stack-=4; } | ||
| 167 | sub main'bswap { &out1("bswap",@_); &using486(); } | ||
| 168 | sub main'not { &out1("not",@_); } | ||
| 169 | sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); } | ||
| 170 | sub main'ret { &out0("ret"); } | ||
| 171 | sub main'nop { &out0("nop"); } | ||
| 172 | sub main'movz { &out2("movzx",@_); } | ||
| 173 | |||
| 174 | sub out2 | ||
| 175 | { | ||
| 176 | my($name,$p1,$p2)=@_; | ||
| 177 | my($l,$t); | ||
| 178 | |||
| 179 | push(@out,"\t$name\t"); | ||
| 180 | if ($name eq "lea") | ||
| 181 | { | ||
| 182 | $p1 =~ s/^[^\[]*\[/\[/; | ||
| 183 | $p2 =~ s/^[^\[]*\[/\[/; | ||
| 184 | } | ||
| 185 | $t=&conv($p1).","; | ||
| 186 | $l=length($t); | ||
| 187 | push(@out,$t); | ||
| 188 | $l=4-($l+9)/8; | ||
| 189 | push(@out,"\t" x $l); | ||
| 190 | push(@out,&conv($p2)); | ||
| 191 | push(@out,"\n"); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub out0 | ||
| 195 | { | ||
| 196 | my($name)=@_; | ||
| 197 | |||
| 198 | push(@out,"\t$name\n"); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub out1 | ||
| 202 | { | ||
| 203 | my($name,$p1)=@_; | ||
| 204 | my($l,$t); | ||
| 205 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub conv | ||
| 209 | { | ||
| 210 | my($p)=@_; | ||
| 211 | $p =~ s/0x([0-9A-Fa-f]+)/0$1h/; | ||
| 212 | return $p; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub using486 | ||
| 216 | { | ||
| 217 | return if $using486; | ||
| 218 | $using486++; | ||
| 219 | grep(s/\.386/\.486/,@out); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub main'file | ||
| 223 | { | ||
| 224 | push(@out, "segment .text use32\n"); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub main'function_begin | ||
| 228 | { | ||
| 229 | my($func,$extra)=@_; | ||
| 230 | |||
| 231 | push(@labels,$func); | ||
| 232 | my($tmp)=<<"EOF"; | ||
| 233 | global _$func | ||
| 234 | _$func: | ||
| 235 | push ebp | ||
| 236 | push ebx | ||
| 237 | push esi | ||
| 238 | push edi | ||
| 239 | EOF | ||
| 240 | push(@out,$tmp); | ||
| 241 | $stack=20; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub main'function_begin_B | ||
| 245 | { | ||
| 246 | my($func,$extra)=@_; | ||
| 247 | my($tmp)=<<"EOF"; | ||
| 248 | global _$func | ||
| 249 | _$func: | ||
| 250 | EOF | ||
| 251 | push(@out,$tmp); | ||
| 252 | $stack=4; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub main'function_end | ||
| 256 | { | ||
| 257 | my($func)=@_; | ||
| 258 | |||
| 259 | my($tmp)=<<"EOF"; | ||
| 260 | pop edi | ||
| 261 | pop esi | ||
| 262 | pop ebx | ||
| 263 | pop ebp | ||
| 264 | ret | ||
| 265 | EOF | ||
| 266 | push(@out,$tmp); | ||
| 267 | $stack=0; | ||
| 268 | %label=(); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub main'function_end_B | ||
| 272 | { | ||
| 273 | $stack=0; | ||
| 274 | %label=(); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub main'function_end_A | ||
| 278 | { | ||
| 279 | my($func)=@_; | ||
| 280 | |||
| 281 | my($tmp)=<<"EOF"; | ||
| 282 | pop edi | ||
| 283 | pop esi | ||
| 284 | pop ebx | ||
| 285 | pop ebp | ||
| 286 | ret | ||
| 287 | EOF | ||
| 288 | push(@out,$tmp); | ||
| 289 | } | ||
| 290 | |||
| 291 | sub main'file_end | ||
| 292 | { | ||
| 293 | } | ||
| 294 | |||
| 295 | sub main'wparam | ||
| 296 | { | ||
| 297 | my($num)=@_; | ||
| 298 | |||
| 299 | return(&main'DWP($stack+$num*4,"esp","",0)); | ||
| 300 | } | ||
| 301 | |||
| 302 | sub main'swtmp | ||
| 303 | { | ||
| 304 | return(&main'DWP($_[0]*4,"esp","",0)); | ||
| 305 | } | ||
| 306 | |||
| 307 | # Should use swtmp, which is above esp. Linix can trash the stack above esp | ||
| 308 | #sub main'wtmp | ||
| 309 | # { | ||
| 310 | # my($num)=@_; | ||
| 311 | # | ||
| 312 | # return(&main'DWP(-(($num+1)*4),"esp","",0)); | ||
| 313 | # } | ||
| 314 | |||
| 315 | sub main'comment | ||
| 316 | { | ||
| 317 | foreach (@_) | ||
| 318 | { | ||
| 319 | push(@out,"\t; $_\n"); | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | sub main'label | ||
| 324 | { | ||
| 325 | if (!defined($label{$_[0]})) | ||
| 326 | { | ||
| 327 | $label{$_[0]}="\$${label}${_[0]}"; | ||
| 328 | $label++; | ||
| 329 | } | ||
| 330 | return($label{$_[0]}); | ||
| 331 | } | ||
| 332 | |||
| 333 | sub main'set_label | ||
| 334 | { | ||
| 335 | if (!defined($label{$_[0]})) | ||
| 336 | { | ||
| 337 | $label{$_[0]}="\$${label}${_[0]}"; | ||
| 338 | $label++; | ||
| 339 | } | ||
| 340 | push(@out,"$label{$_[0]}:\n"); | ||
| 341 | } | ||
| 342 | |||
| 343 | sub main'data_word | ||
| 344 | { | ||
| 345 | push(@out,"\tDD\t$_[0]\n"); | ||
| 346 | } | ||
| 347 | |||
| 348 | sub out1p | ||
| 349 | { | ||
| 350 | my($name,$p1)=@_; | ||
| 351 | my($l,$t); | ||
| 352 | |||
| 353 | push(@out,"\t$name\t ".&conv($p1)."\n"); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub main'picmeup | ||
| 357 | { | ||
| 358 | local($dst,$sym)=@_; | ||
| 359 | &main'lea($dst,&main'DWP($sym)); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub main'blindpop { &out1("pop",@_); } | ||
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl new file mode 100644 index 0000000000..b61425e951 --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86unix.pl | |||
| @@ -0,0 +1,628 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | package x86unix; | ||
| 4 | |||
| 5 | $label="L000"; | ||
| 6 | $const=""; | ||
| 7 | $constl=0; | ||
| 8 | |||
| 9 | $align=($main'aout)?"4":"16"; | ||
| 10 | $under=($main'aout)?"_":""; | ||
| 11 | $com_start=($main'sol)?"/":"#"; | ||
| 12 | |||
| 13 | sub main'asm_init_output { @out=(); } | ||
| 14 | sub main'asm_get_output { return(@out); } | ||
| 15 | sub main'get_labels { return(@labels); } | ||
| 16 | sub main'external_label { push(@labels,@_); } | ||
| 17 | |||
| 18 | if ($main'openbsd) | ||
| 19 | { | ||
| 20 | $com_start='/*'; | ||
| 21 | $com_end='*/'; | ||
| 22 | } | ||
| 23 | |||
| 24 | if ($main'cpp) | ||
| 25 | { | ||
| 26 | $align="ALIGN"; | ||
| 27 | $under=""; | ||
| 28 | $com_start='/*'; | ||
| 29 | $com_end='*/'; | ||
| 30 | } | ||
| 31 | |||
| 32 | %lb=( 'eax', '%al', | ||
| 33 | 'ebx', '%bl', | ||
| 34 | 'ecx', '%cl', | ||
| 35 | 'edx', '%dl', | ||
| 36 | 'ax', '%al', | ||
| 37 | 'bx', '%bl', | ||
| 38 | 'cx', '%cl', | ||
| 39 | 'dx', '%dl', | ||
| 40 | ); | ||
| 41 | |||
| 42 | %hb=( 'eax', '%ah', | ||
| 43 | 'ebx', '%bh', | ||
| 44 | 'ecx', '%ch', | ||
| 45 | 'edx', '%dh', | ||
| 46 | 'ax', '%ah', | ||
| 47 | 'bx', '%bh', | ||
| 48 | 'cx', '%ch', | ||
| 49 | 'dx', '%dh', | ||
| 50 | ); | ||
| 51 | |||
| 52 | %regs=( 'eax', '%eax', | ||
| 53 | 'ebx', '%ebx', | ||
| 54 | 'ecx', '%ecx', | ||
| 55 | 'edx', '%edx', | ||
| 56 | 'esi', '%esi', | ||
| 57 | 'edi', '%edi', | ||
| 58 | 'ebp', '%ebp', | ||
| 59 | 'esp', '%esp', | ||
| 60 | ); | ||
| 61 | |||
| 62 | %reg_val=( | ||
| 63 | 'eax', 0x00, | ||
| 64 | 'ebx', 0x03, | ||
| 65 | 'ecx', 0x01, | ||
| 66 | 'edx', 0x02, | ||
| 67 | 'esi', 0x06, | ||
| 68 | 'edi', 0x07, | ||
| 69 | 'ebp', 0x05, | ||
| 70 | 'esp', 0x04, | ||
| 71 | ); | ||
| 72 | |||
| 73 | sub main'LB | ||
| 74 | { | ||
| 75 | (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n"; | ||
| 76 | return($lb{$_[0]}); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub main'HB | ||
| 80 | { | ||
| 81 | (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n"; | ||
| 82 | return($hb{$_[0]}); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub main'DWP | ||
| 86 | { | ||
| 87 | local($addr,$reg1,$reg2,$idx)=@_; | ||
| 88 | |||
| 89 | $ret=""; | ||
| 90 | $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/; | ||
| 91 | $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 92 | $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 93 | $ret.=$addr if ($addr ne "") && ($addr ne 0); | ||
| 94 | if ($reg2 ne "") | ||
| 95 | { | ||
| 96 | if($idx ne "" && $idx != 0) | ||
| 97 | { $ret.="($reg1,$reg2,$idx)"; } | ||
| 98 | else | ||
| 99 | { $ret.="($reg1,$reg2)"; } | ||
| 100 | } | ||
| 101 | elsif ($reg1 ne "") | ||
| 102 | { $ret.="($reg1)" } | ||
| 103 | return($ret); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub main'BP | ||
| 107 | { | ||
| 108 | return(&main'DWP(@_)); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub main'BC | ||
| 112 | { | ||
| 113 | return @_; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub main'DWC | ||
| 117 | { | ||
| 118 | return @_; | ||
| 119 | } | ||
| 120 | |||
| 121 | #sub main'BP | ||
| 122 | # { | ||
| 123 | # local($addr,$reg1,$reg2,$idx)=@_; | ||
| 124 | # | ||
| 125 | # $ret=""; | ||
| 126 | # | ||
| 127 | # $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/; | ||
| 128 | # $reg1="$regs{$reg1}" if defined($regs{$reg1}); | ||
| 129 | # $reg2="$regs{$reg2}" if defined($regs{$reg2}); | ||
| 130 | # $ret.=$addr if ($addr ne "") && ($addr ne 0); | ||
| 131 | # if ($reg2 ne "") | ||
| 132 | # { $ret.="($reg1,$reg2,$idx)"; } | ||
| 133 | # else | ||
| 134 | # { $ret.="($reg1)" } | ||
| 135 | # return($ret); | ||
| 136 | # } | ||
| 137 | |||
| 138 | sub main'mov { &out2("movl",@_); } | ||
| 139 | sub main'movb { &out2("movb",@_); } | ||
| 140 | sub main'and { &out2("andl",@_); } | ||
| 141 | sub main'or { &out2("orl",@_); } | ||
| 142 | sub main'shl { &out2("sall",@_); } | ||
| 143 | sub main'shr { &out2("shrl",@_); } | ||
| 144 | sub main'xor { &out2("xorl",@_); } | ||
| 145 | sub main'xorb { &out2("xorb",@_); } | ||
| 146 | sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); } | ||
| 147 | sub main'adc { &out2("adcl",@_); } | ||
| 148 | sub main'sub { &out2("subl",@_); } | ||
| 149 | sub main'rotl { &out2("roll",@_); } | ||
| 150 | sub main'rotr { &out2("rorl",@_); } | ||
| 151 | sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); } | ||
| 152 | sub main'cmp { &out2("cmpl",@_); } | ||
| 153 | sub main'lea { &out2("leal",@_); } | ||
| 154 | sub main'mul { &out1("mull",@_); } | ||
| 155 | sub main'div { &out1("divl",@_); } | ||
| 156 | sub main'jmp { &out1("jmp",@_); } | ||
| 157 | sub main'jmp_ptr { &out1p("jmp",@_); } | ||
| 158 | sub main'je { &out1("je",@_); } | ||
| 159 | sub main'jle { &out1("jle",@_); } | ||
| 160 | sub main'jne { &out1("jne",@_); } | ||
| 161 | sub main'jnz { &out1("jnz",@_); } | ||
| 162 | sub main'jz { &out1("jz",@_); } | ||
| 163 | sub main'jge { &out1("jge",@_); } | ||
| 164 | sub main'jl { &out1("jl",@_); } | ||
| 165 | sub main'ja { &out1("ja",@_); } | ||
| 166 | sub main'jae { &out1("jae",@_); } | ||
| 167 | sub main'jb { &out1("jb",@_); } | ||
| 168 | sub main'jbe { &out1("jbe",@_); } | ||
| 169 | sub main'jc { &out1("jc",@_); } | ||
| 170 | sub main'jnc { &out1("jnc",@_); } | ||
| 171 | sub main'jno { &out1("jno",@_); } | ||
| 172 | sub main'dec { &out1("decl",@_); } | ||
| 173 | sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); } | ||
| 174 | sub main'push { &out1("pushl",@_); $stack+=4; } | ||
| 175 | sub main'pop { &out1("popl",@_); $stack-=4; } | ||
| 176 | sub main'pushf { &out0("pushf"); $stack+=4; } | ||
| 177 | sub main'popf { &out0("popf"); $stack-=4; } | ||
| 178 | sub main'not { &out1("notl",@_); } | ||
| 179 | sub main'call { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); } | ||
| 180 | sub main'ret { &out0("ret"); } | ||
| 181 | sub main'nop { &out0("nop"); } | ||
| 182 | sub main'test { &out2("testl",@_); } | ||
| 183 | sub main'movz { &out2("movzbl",@_); } | ||
| 184 | sub main'neg { &out1("negl",@_); } | ||
| 185 | |||
| 186 | # The bswapl instruction is new for the 486. Emulate if i386. | ||
| 187 | sub main'bswap | ||
| 188 | { | ||
| 189 | if ($main'i386) | ||
| 190 | { | ||
| 191 | &main'comment("bswapl @_"); | ||
| 192 | &main'exch(main'HB(@_),main'LB(@_)); | ||
| 193 | &main'rotr(@_,16); | ||
| 194 | &main'exch(main'HB(@_),main'LB(@_)); | ||
| 195 | } | ||
| 196 | else | ||
| 197 | { | ||
| 198 | &out1("bswapl",@_); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | sub out2 | ||
| 203 | { | ||
| 204 | local($name,$p1,$p2)=@_; | ||
| 205 | local($l,$ll,$t); | ||
| 206 | local(%special)=( "roll",0xD1C0,"rorl",0xD1C8, | ||
| 207 | "rcll",0xD1D0,"rcrl",0xD1D8, | ||
| 208 | "shll",0xD1E0,"shrl",0xD1E8, | ||
| 209 | "sarl",0xD1F8); | ||
| 210 | |||
| 211 | if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1)) | ||
| 212 | { | ||
| 213 | $op=$special{$name}|$reg_val{$p1}; | ||
| 214 | $tmp1=sprintf(".byte %d\n",($op>>8)&0xff); | ||
| 215 | $tmp2=sprintf(".byte %d\t",$op &0xff); | ||
| 216 | push(@out,$tmp1); | ||
| 217 | push(@out,$tmp2); | ||
| 218 | |||
| 219 | $p2=&conv($p2); | ||
| 220 | $p1=&conv($p1); | ||
| 221 | &main'comment("$name $p2 $p1"); | ||
| 222 | return; | ||
| 223 | } | ||
| 224 | |||
| 225 | push(@out,"\t$name\t"); | ||
| 226 | $t=&conv($p2).","; | ||
| 227 | $l=length($t); | ||
| 228 | push(@out,$t); | ||
| 229 | $ll=4-($l+9)/8; | ||
| 230 | $tmp1=sprintf("\t" x $ll); | ||
| 231 | push(@out,$tmp1); | ||
| 232 | push(@out,&conv($p1)."\n"); | ||
| 233 | } | ||
| 234 | |||
| 235 | sub out1 | ||
| 236 | { | ||
| 237 | local($name,$p1)=@_; | ||
| 238 | local($l,$t); | ||
| 239 | local(%special)=("bswapl",0x0FC8); | ||
| 240 | |||
| 241 | if ((defined($special{$name})) && defined($regs{$p1})) | ||
| 242 | { | ||
| 243 | $op=$special{$name}|$reg_val{$p1}; | ||
| 244 | $tmp1=sprintf(".byte %d\n",($op>>8)&0xff); | ||
| 245 | $tmp2=sprintf(".byte %d\t",$op &0xff); | ||
| 246 | push(@out,$tmp1); | ||
| 247 | push(@out,$tmp2); | ||
| 248 | |||
| 249 | $p2=&conv($p2); | ||
| 250 | $p1=&conv($p1); | ||
| 251 | &main'comment("$name $p2 $p1"); | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | push(@out,"\t$name\t".&conv($p1)."\n"); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub out1p | ||
| 259 | { | ||
| 260 | local($name,$p1)=@_; | ||
| 261 | local($l,$t); | ||
| 262 | |||
| 263 | push(@out,"\t$name\t*".&conv($p1)."\n"); | ||
| 264 | } | ||
| 265 | |||
| 266 | sub out0 | ||
| 267 | { | ||
| 268 | push(@out,"\t$_[0]\n"); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub conv | ||
| 272 | { | ||
| 273 | local($p)=@_; | ||
| 274 | |||
| 275 | # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/; | ||
| 276 | |||
| 277 | $p=$regs{$p} if (defined($regs{$p})); | ||
| 278 | |||
| 279 | $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/; | ||
| 280 | $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/; | ||
| 281 | return $p; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub main'file | ||
| 285 | { | ||
| 286 | local($file)=@_; | ||
| 287 | |||
| 288 | if ($main'openbsd) | ||
| 289 | { push(@out,"#include <machine/asm.h>\n"); return; } | ||
| 290 | |||
| 291 | local($tmp)=<<"EOF"; | ||
| 292 | .file "$file.s" | ||
| 293 | .version "01.01" | ||
| 294 | gcc2_compiled.: | ||
| 295 | EOF | ||
| 296 | push(@out,$tmp); | ||
| 297 | } | ||
| 298 | |||
| 299 | sub main'function_begin | ||
| 300 | { | ||
| 301 | local($func)=@_; | ||
| 302 | |||
| 303 | &main'external_label($func); | ||
| 304 | $func=$under.$func; | ||
| 305 | |||
| 306 | if ($main'openbsd) | ||
| 307 | { push (@out, "\nENTRY($func)\n"); goto skip; } | ||
| 308 | |||
| 309 | local($tmp)=<<"EOF"; | ||
| 310 | .text | ||
| 311 | .align $align | ||
| 312 | .globl $func | ||
| 313 | EOF | ||
| 314 | push(@out,$tmp); | ||
| 315 | if ($main'cpp) | ||
| 316 | { $tmp=push(@out,"\tTYPE($func,\@function)\n"); } | ||
| 317 | elsif ($main'gaswin) | ||
| 318 | { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | ||
| 319 | else { $tmp=push(@out,"\t.type\t$func,\@function\n"); } | ||
| 320 | push(@out,"$func:\n"); | ||
| 321 | skip: | ||
| 322 | $tmp=<<"EOF"; | ||
| 323 | pushl %ebp | ||
| 324 | pushl %ebx | ||
| 325 | pushl %esi | ||
| 326 | pushl %edi | ||
| 327 | |||
| 328 | EOF | ||
| 329 | push(@out,$tmp); | ||
| 330 | $stack=20; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub main'function_begin_B | ||
| 334 | { | ||
| 335 | local($func,$extra)=@_; | ||
| 336 | |||
| 337 | &main'external_label($func); | ||
| 338 | $func=$under.$func; | ||
| 339 | |||
| 340 | if ($main'openbsd) | ||
| 341 | { push(@out, "\nENTRY($func)\n"); goto skip; } | ||
| 342 | |||
| 343 | local($tmp)=<<"EOF"; | ||
| 344 | .text | ||
| 345 | .align $align | ||
| 346 | .globl $func | ||
| 347 | EOF | ||
| 348 | push(@out,$tmp); | ||
| 349 | if ($main'cpp) | ||
| 350 | { push(@out,"\tTYPE($func,\@function)\n"); } | ||
| 351 | elsif ($main'gaswin) | ||
| 352 | { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | ||
| 353 | else { push(@out,"\t.type $func,\@function\n"); } | ||
| 354 | push(@out,"$func:\n"); | ||
| 355 | skip: | ||
| 356 | $stack=4; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub main'function_end | ||
| 360 | { | ||
| 361 | local($func)=@_; | ||
| 362 | |||
| 363 | $func=$under.$func; | ||
| 364 | |||
| 365 | local($tmp)=<<"EOF"; | ||
| 366 | popl %edi | ||
| 367 | popl %esi | ||
| 368 | popl %ebx | ||
| 369 | popl %ebp | ||
| 370 | ret | ||
| 371 | .L_${func}_end: | ||
| 372 | EOF | ||
| 373 | push(@out,$tmp); | ||
| 374 | |||
| 375 | if ($main'cpp) | ||
| 376 | { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); } | ||
| 377 | elsif ($main'gaswin) | ||
| 378 | { $tmp=push(@out,"\t.align 4\n"); } | ||
| 379 | else { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); } | ||
| 380 | push(@out,".ident \"$func\"\n"); | ||
| 381 | $stack=0; | ||
| 382 | %label=(); | ||
| 383 | } | ||
| 384 | |||
| 385 | sub main'function_end_A | ||
| 386 | { | ||
| 387 | local($func)=@_; | ||
| 388 | |||
| 389 | local($tmp)=<<"EOF"; | ||
| 390 | popl %edi | ||
| 391 | popl %esi | ||
| 392 | popl %ebx | ||
| 393 | popl %ebp | ||
| 394 | ret | ||
| 395 | EOF | ||
| 396 | push(@out,$tmp); | ||
| 397 | } | ||
| 398 | |||
| 399 | sub main'function_end_B | ||
| 400 | { | ||
| 401 | local($func)=@_; | ||
| 402 | |||
| 403 | $func=$under.$func; | ||
| 404 | |||
| 405 | push(@out,".L_${func}_end:\n"); | ||
| 406 | if ($main'cpp) | ||
| 407 | { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); } | ||
| 408 | elsif ($main'gaswin) | ||
| 409 | { push(@out,"\t.align 4\n"); } | ||
| 410 | else { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); } | ||
| 411 | push(@out,".ident \"desasm.pl\"\n"); | ||
| 412 | $stack=0; | ||
| 413 | %label=(); | ||
| 414 | } | ||
| 415 | |||
| 416 | sub main'wparam | ||
| 417 | { | ||
| 418 | local($num)=@_; | ||
| 419 | |||
| 420 | return(&main'DWP($stack+$num*4,"esp","",0)); | ||
| 421 | } | ||
| 422 | |||
| 423 | sub main'stack_push | ||
| 424 | { | ||
| 425 | local($num)=@_; | ||
| 426 | $stack+=$num*4; | ||
| 427 | &main'sub("esp",$num*4); | ||
| 428 | } | ||
| 429 | |||
| 430 | sub main'stack_pop | ||
| 431 | { | ||
| 432 | local($num)=@_; | ||
| 433 | $stack-=$num*4; | ||
| 434 | &main'add("esp",$num*4); | ||
| 435 | } | ||
| 436 | |||
| 437 | sub main'swtmp | ||
| 438 | { | ||
| 439 | return(&main'DWP($_[0]*4,"esp","",0)); | ||
| 440 | } | ||
| 441 | |||
| 442 | # Should use swtmp, which is above esp. Linix can trash the stack above esp | ||
| 443 | #sub main'wtmp | ||
| 444 | # { | ||
| 445 | # local($num)=@_; | ||
| 446 | # | ||
| 447 | # return(&main'DWP(-($num+1)*4,"esp","",0)); | ||
| 448 | # } | ||
| 449 | |||
| 450 | sub main'comment | ||
| 451 | { | ||
| 452 | if (!$main'openbsd && $main'elf) | ||
| 453 | # GNU and SVR4 as'es use different comment delimiters, | ||
| 454 | { # so we just skip comments... | ||
| 455 | push(@out,"\n"); | ||
| 456 | return; | ||
| 457 | } | ||
| 458 | foreach (@_) | ||
| 459 | { | ||
| 460 | if (/^\s*$/) | ||
| 461 | { push(@out,"\n"); } | ||
| 462 | else | ||
| 463 | { push(@out,"\t$com_start $_ $com_end\n"); } | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | sub main'public_label | ||
| 468 | { | ||
| 469 | $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]})); | ||
| 470 | push(@out,".globl\t$label{$_[0]}\n"); | ||
| 471 | } | ||
| 472 | |||
| 473 | sub main'label | ||
| 474 | { | ||
| 475 | if (!defined($label{$_[0]})) | ||
| 476 | { | ||
| 477 | $label{$_[0]}=".${label}${_[0]}"; | ||
| 478 | $label++; | ||
| 479 | } | ||
| 480 | return($label{$_[0]}); | ||
| 481 | } | ||
| 482 | |||
| 483 | sub main'set_label | ||
| 484 | { | ||
| 485 | if (!defined($label{$_[0]})) | ||
| 486 | { | ||
| 487 | $label{$_[0]}=".${label}${_[0]}"; | ||
| 488 | $label++; | ||
| 489 | } | ||
| 490 | if ($main'openbsd) | ||
| 491 | { push(@out,"_ALIGN_TEXT\n") if ($_[1] != 0); } | ||
| 492 | else | ||
| 493 | { push(@out,".align $align\n") if ($_[1] != 0); } | ||
| 494 | push(@out,"$label{$_[0]}:\n"); | ||
| 495 | } | ||
| 496 | |||
| 497 | sub main'file_end | ||
| 498 | { | ||
| 499 | if ($const ne "") | ||
| 500 | { | ||
| 501 | push(@out,".section .rodata\n"); | ||
| 502 | push(@out,$const); | ||
| 503 | $const=""; | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | sub main'data_word | ||
| 508 | { | ||
| 509 | push(@out,"\t.long\t".join(',',@_)."\n"); | ||
| 510 | } | ||
| 511 | |||
| 512 | # debug output functions: puts, putx, printf | ||
| 513 | |||
| 514 | sub main'puts | ||
| 515 | { | ||
| 516 | &pushvars(); | ||
| 517 | &main'push('$Lstring' . ++$constl); | ||
| 518 | &main'call('puts'); | ||
| 519 | $stack-=4; | ||
| 520 | &main'add("esp",4); | ||
| 521 | &popvars(); | ||
| 522 | |||
| 523 | $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n"; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub main'putx | ||
| 527 | { | ||
| 528 | &pushvars(); | ||
| 529 | &main'push($_[0]); | ||
| 530 | &main'push('$Lstring' . ++$constl); | ||
| 531 | &main'call('printf'); | ||
| 532 | &main'add("esp",8); | ||
| 533 | $stack-=8; | ||
| 534 | &popvars(); | ||
| 535 | |||
| 536 | $const .= "Lstring$constl:\n\t.string \"\%X\"\n"; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub main'printf | ||
| 540 | { | ||
| 541 | $ostack = $stack; | ||
| 542 | &pushvars(); | ||
| 543 | for ($i = @_ - 1; $i >= 0; $i--) | ||
| 544 | { | ||
| 545 | if ($i == 0) # change this to support %s format strings | ||
| 546 | { | ||
| 547 | &main'push('$Lstring' . ++$constl); | ||
| 548 | $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n"; | ||
| 549 | } | ||
| 550 | else | ||
| 551 | { | ||
| 552 | if ($_[$i] =~ /([0-9]*)\(%esp\)/) | ||
| 553 | { | ||
| 554 | &main'push(($1 + $stack - $ostack) . '(%esp)'); | ||
| 555 | } | ||
| 556 | else | ||
| 557 | { | ||
| 558 | &main'push($_[$i]); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | } | ||
| 562 | &main'call('printf'); | ||
| 563 | $stack-=4*@_; | ||
| 564 | &main'add("esp",4*@_); | ||
| 565 | &popvars(); | ||
| 566 | } | ||
| 567 | |||
| 568 | sub pushvars | ||
| 569 | { | ||
| 570 | &main'pushf(); | ||
| 571 | &main'push("edx"); | ||
| 572 | &main'push("ecx"); | ||
| 573 | &main'push("eax"); | ||
| 574 | } | ||
| 575 | |||
| 576 | sub popvars | ||
| 577 | { | ||
| 578 | &main'pop("eax"); | ||
| 579 | &main'pop("ecx"); | ||
| 580 | &main'pop("edx"); | ||
| 581 | &main'popf(); | ||
| 582 | } | ||
| 583 | |||
| 584 | sub main'picmeup | ||
| 585 | { | ||
| 586 | local($dst,$sym)=@_; | ||
| 587 | if ($main'cpp) | ||
| 588 | { | ||
| 589 | local($tmp)=<<___; | ||
| 590 | #if (defined(ELF) || defined(SOL)) && defined(PIC) | ||
| 591 | .align 8 | ||
| 592 | call 1f | ||
| 593 | 1: popl $regs{$dst} | ||
| 594 | addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst} | ||
| 595 | movl $sym\@GOT($regs{$dst}),$regs{$dst} | ||
| 596 | #else | ||
| 597 | leal $sym,$regs{$dst} | ||
| 598 | #endif | ||
| 599 | ___ | ||
| 600 | push(@out,$tmp); | ||
| 601 | } | ||
| 602 | elsif ($main'openbsd) | ||
| 603 | { | ||
| 604 | push(@out, "#ifdef PIC\n"); | ||
| 605 | push(@out, "\tPIC_PROLOGUE\n"); | ||
| 606 | &main'mov($dst,"PIC_GOT($sym)"); | ||
| 607 | push(@out, "\tPIC_EPILOGUE\n"); | ||
| 608 | push(@out, "#else\n"); | ||
| 609 | &main'lea($dst,&main'DWP($sym)); | ||
| 610 | push(@out, "#endif\n"); | ||
| 611 | } | ||
| 612 | elsif ($main'pic && ($main'elf || $main'aout)) | ||
| 613 | { | ||
| 614 | push(@out,"\t.align\t8\n"); | ||
| 615 | &main'call(&main'label("PIC_me_up")); | ||
| 616 | &main'set_label("PIC_me_up"); | ||
| 617 | &main'blindpop($dst); | ||
| 618 | &main'add($dst,"\$$under"."_GLOBAL_OFFSET_TABLE_+[.-". | ||
| 619 | &main'label("PIC_me_up") . "]"); | ||
| 620 | &main'mov($dst,&main'DWP($sym."\@GOT",$dst)); | ||
| 621 | } | ||
| 622 | else | ||
| 623 | { | ||
| 624 | &main'lea($dst,&main'DWP($sym)); | ||
| 625 | } | ||
| 626 | } | ||
| 627 | |||
| 628 | sub main'blindpop { &out1("popl",@_); } | ||
