wok diff linld/stuff/src/pipehole.awk @ rev 20458

Up amule (2.3.2)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Oct 10 10:43:41 2018 +0200 (2018-10-10)
parents
children c2946aeb8b57
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/linld/stuff/src/pipehole.awk	Wed Oct 10 10:43:41 2018 +0200
     1.3 @@ -0,0 +1,155 @@
     1.4 +BEGIN { hold=0 }
     1.5 +function isnum(n) { return match(n,/^[0-9+-]/) }
     1.6 +{
     1.7 +	if (hold == 0) {
     1.8 +		s=$0
     1.9 +		if (/^	mov	.x,bx$/ || /^	mov	.x,.i$/) {
    1.10 +			hold=1; split($2,regs,","); next
    1.11 +		}
    1.12 +		if (/^	inc	e?.[ix]/ || /^	dec	e?.[ix]/) {
    1.13 +			hold=2; r=$2; next
    1.14 +		}
    1.15 +		if (/^	mov	[abcds][ix],/ && ! /,.s/) {
    1.16 +			hold=3; split($2,regs,","); next
    1.17 +		}
    1.18 +		if (/^	movzx	eax,ax$/) { hold=4; next }
    1.19 +if (0) {
    1.20 +		if (/^	cmp	dx,-1$/) { hold=10; next }
    1.21 +}
    1.22 +	}
    1.23 +	else if (hold == 1) {
    1.24 +		hold=0; split($2,args,","); op=""
    1.25 +		if ($1 == "add") op="+"
    1.26 +		if ($1 == "sub") op="-"
    1.27 +		if (op != "" && regs[1] == args[1] && isnum(args[2])) {
    1.28 +			print "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
    1.29 +			next
    1.30 +		}
    1.31 +		print "\tmov\t" regs[1] "," regs[2]
    1.32 +	}
    1.33 +	else if (hold == 2) {
    1.34 +		hold=0; split($2,args,","); print s
    1.35 +		if ($1 == "or" && r == args[1] && r == args[2]) next	# don't clear C ...
    1.36 +	}
    1.37 +	else if (hold == 3) {
    1.38 +		hold=0
    1.39 +		if (/^	add	[abcds][ix],/ || /^	sub	[abcds][ix],/) {
    1.40 +			split($2,regs2,",")
    1.41 +			if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
    1.42 +				t=$0; sub(/mov/,$1,s)
    1.43 +				if ($1 == "add") sub(/add/,"mov",t); else sub(/sub/,"mov",t)
    1.44 +				print t; print s; next
    1.45 +			}
    1.46 +		}
    1.47 +		print s
    1.48 +	}
    1.49 +	else if (hold == 4) {
    1.50 +		hold=0
    1.51 +		if (/^	push	eax$/) {
    1.52 +			print "	push	0"; print "	push	ax"; next
    1.53 +		} else { print s }
    1.54 +	}
    1.55 +	else if (hold == 10) {
    1.56 +		if ($1 == "je" || $1 == "jne") { s2=$0; cmp=$1; hold++; next }
    1.57 +		hold=0; print s
    1.58 +	}
    1.59 +	else if (hold == 11) {
    1.60 +		if (/^	cmp	ax,-1$/) { s3=$0; hold++; next }
    1.61 +		hold=0; print s; print s2
    1.62 +	}
    1.63 +	else if (hold == 12) {
    1.64 +		if (($1 == "je" || $1 == "jne") && $1 != cmp) {
    1.65 +			print "	and	ax,dx"; print "	inc	ax"
    1.66 +		} else { print s; print s2; print s3 }
    1.67 +		hold=0
    1.68 +	}
    1.69 +	s=$0
    1.70 +	# These optimisation may break ZF or CF
    1.71 +	if (/^	add	sp,4/) { print "	pop	cx"; print "	pop	cx"; next }
    1.72 +	if (/^	mov	d*word ptr .*,0$/ || /^	mov	dword ptr .*,large 0$/) {
    1.73 +		sub(/mov/,"and",s); print s; next	# slower
    1.74 +	}
    1.75 +	if (/^	mov	d*word ptr .*,-1$/ || /^	mov	dword ptr .*,large -1$/) {
    1.76 +		sub(/mov/,"or",s); print s; next	# slower
    1.77 +	}
    1.78 +	if (/^	or	.*,0$/ || /^	and	.*,-1$/) next
    1.79 +	if (/^	or	[abcd]x,/) {
    1.80 +		split($2,args,",")
    1.81 +		if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
    1.82 +			print "	or	" substr(args[1],1,1) "l," args[2]; next
    1.83 +		}
    1.84 +	}
    1.85 +	if (/^	and	[abcd]x,/) {
    1.86 +		split($2,args,",")
    1.87 +		if (isnum(args[2]) && args[2] >= -255 && args[2] < 0) {
    1.88 +			print "	and	" substr(args[1],1,1) "l," args[2]; next
    1.89 +		}
    1.90 +	}
    1.91 +	if (/^	or	e[abcd]x,/) {
    1.92 +		split($2,args,",")
    1.93 +		if (args[2] == "large") { args[2] = $3 }
    1.94 +		if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
    1.95 +			print "	or	" substr(args[1],2,1) "l," args[2]; next
    1.96 +		}
    1.97 +	}
    1.98 +	if (/^	and	e[abcd]x,/) {
    1.99 +		split($2,args,",")
   1.100 +		if (args[2] == "large") { args[2] = $3 }
   1.101 +		if (isnum(args[2]) && args[2] >= -255 && args[2] < 0) {
   1.102 +			print "	and	" substr(args[1],2,1) "l," args[2]; next
   1.103 +		}
   1.104 +	}
   1.105 +	if (/^	or	e[abcds][ix],/) {
   1.106 +		split($2,args,",")
   1.107 +		if (args[2] == "large") { args[2] = $3 }
   1.108 +		if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
   1.109 +			print "	or	" substr(args[1],2) "," args[2]; next
   1.110 +		}
   1.111 +	}
   1.112 +	if (/^	and	e[abcds][ix],/) {
   1.113 +		split($2,args,",")
   1.114 +		if (args[2] == "large") { args[2] = $3 }
   1.115 +		if (isnum(args[2]) && args[2] >= -65535 && args[2] < 0) {
   1.116 +			print "	and	" substr(args[1],2) "," args[2]; next
   1.117 +		}
   1.118 +	}
   1.119 +	if (/^	add	word ptr/ || /^	sub	word ptr/) {
   1.120 +		split($0,args,",")
   1.121 +		if (isnum(args[2]) && (args[2] % 256 == 0)) {
   1.122 +			sub(/word/,"byte",s); sub(/\]/,"+1]",s)
   1.123 +			print s "/256"; next
   1.124 +		}
   1.125 +	}
   1.126 +	if (/^	add	dword ptr/ || /^	sub	dword ptr/) {
   1.127 +		split($0,args,",")
   1.128 +		if (args[2] == "large") { split(args[2],args," ") }
   1.129 +		if (isnum(args[2])) {
   1.130 +			if (args[2] % 16777216 == 0) {
   1.131 +				sub(/dword/,"byte",s); sub(/\]/,"+3]",s)
   1.132 +				print s "/16777216"; next
   1.133 +			}
   1.134 +			if (args[2] % 65536 == 0) {
   1.135 +				sub(/dword/,"word",s); sub(/\]/,"+2]",s)
   1.136 +				print s "/65536"; next
   1.137 +			}
   1.138 +		}
   1.139 +	}
   1.140 +	if (/^	mov	e.x,/) {
   1.141 +		split($2,args,",")
   1.142 +		r=args[1]
   1.143 +		if (args[2] == "large") { args[2] = $3 }
   1.144 +		if (isnum(args[2]) && args[2] % 65536 == args[2]) {
   1.145 +			if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
   1.146 +				print "	xor	" r "," r
   1.147 +				if (args[2] == 0) next 
   1.148 +				x="	mov	" substr(r,2,1)
   1.149 +				if (args[2] % 256 == 0) {
   1.150 +					print x "h," args[2] "/256"
   1.151 +				}
   1.152 +				else { print x "l," args[2] }
   1.153 +				next
   1.154 +			}
   1.155 +		}
   1.156 +	}
   1.157 +	print
   1.158 +}