summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjkim <jkim@FreeBSD.org>2008-06-24 20:12:44 +0000
committerjkim <jkim@FreeBSD.org>2008-06-24 20:12:44 +0000
commit47cf9c39e7e5ac127429cc6f1747d0337f9ad65a (patch)
tree14bbb5f2aeab0acc386470ce067aabc807d526a9
parent4cc7195a06d01e25b8176bc18a46c83f41833260 (diff)
downloadFreeBSD-src-47cf9c39e7e5ac127429cc6f1747d0337f9ad65a.zip
FreeBSD-src-47cf9c39e7e5ac127429cc6f1747d0337f9ad65a.tar.gz
Emit opcodes closer to GNU as(1) generated codes and micro-optimize.
-rw-r--r--sys/i386/i386/bpf_jit_machdep.c22
-rw-r--r--sys/i386/i386/bpf_jit_machdep.h83
2 files changed, 46 insertions, 59 deletions
diff --git a/sys/i386/i386/bpf_jit_machdep.c b/sys/i386/i386/bpf_jit_machdep.c
index ed9a4ba..8d3142d 100644
--- a/sys/i386/i386/bpf_jit_machdep.c
+++ b/sys/i386/i386/bpf_jit_machdep.c
@@ -166,7 +166,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
ADDib(sizeof(int), ECX);
CMPodd(0x10, EBP, ECX);
JLEb(7);
- ZERO_EAX();
+ ZEROrd(EAX);
POP(EBX);
POP(ESI);
POP(EDI);
@@ -176,7 +176,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
break;
case BPF_LD|BPF_H|BPF_ABS:
- ZERO_EAX();
+ ZEROrd(EAX);
MOVid(ins->k, ECX);
MOVrd(ECX, ESI);
ADDib(sizeof(short), ECX);
@@ -191,7 +191,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
break;
case BPF_LD|BPF_B|BPF_ABS:
- ZERO_EAX();
+ ZEROrd(EAX);
MOVid(ins->k, ECX);
CMPodd(0x10, EBP, ECX);
JLEb(5);
@@ -217,7 +217,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
ADDib(sizeof(int), ECX);
CMPodd(0x10, EBP, ECX);
JLEb(7);
- ZERO_EAX();
+ ZEROrd(EAX);
POP(EBX);
POP(ESI);
POP(EDI);
@@ -227,7 +227,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
break;
case BPF_LD|BPF_H|BPF_IND:
- ZERO_EAX();
+ ZEROrd(EAX);
MOVid(ins->k, ECX);
ADDrd(EDX, ECX);
MOVrd(ECX, ESI);
@@ -243,7 +243,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
break;
case BPF_LD|BPF_B|BPF_IND:
- ZERO_EAX();
+ ZEROrd(EAX);
MOVid(ins->k, ECX);
ADDrd(EDX, ECX);
CMPodd(0x10, EBP, ECX);
@@ -259,12 +259,12 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
MOVid(ins->k, ECX);
CMPodd(0x10, EBP, ECX);
JLEb(7);
- ZERO_EAX();
+ ZEROrd(EAX);
POP(EBX);
POP(ESI);
POP(EDI);
LEAVE_RET();
- ZERO_EDX();
+ ZEROrd(EDX);
MOVobb(EBX, ECX, DL);
ANDib(0xf, DL);
SHLib(2, EDX);
@@ -396,13 +396,13 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
case BPF_ALU|BPF_DIV|BPF_X:
CMPid(0, EDX);
JNEb(7);
- ZERO_EAX();
+ ZEROrd(EAX);
POP(EBX);
POP(ESI);
POP(EDI);
LEAVE_RET();
MOVrd(EDX, ECX);
- ZERO_EDX();
+ ZEROrd(EDX);
DIVrd(ECX);
MOVrd(ECX, EDX);
break;
@@ -442,7 +442,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
case BPF_ALU|BPF_DIV|BPF_K:
MOVrd(EDX, ECX);
- ZERO_EDX();
+ ZEROrd(EDX);
MOVid(ins->k, ESI);
DIVrd(ESI);
MOVrd(ECX, EDX);
diff --git a/sys/i386/i386/bpf_jit_machdep.h b/sys/i386/i386/bpf_jit_machdep.h
index 1a93ee2..4d6cb40 100644
--- a/sys/i386/i386/bpf_jit_machdep.h
+++ b/sys/i386/i386/bpf_jit_machdep.h
@@ -103,14 +103,14 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* movl sr32,dr32 */
#define MOVrd(sr32, dr32) do { \
- emitm(&stream, (8 << 4) | 3 | (1 << 3), 1); \
+ emitm(&stream, 0x89, 1); \
emitm(&stream, \
- (3 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
+ (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
} while (0)
/* movl off(sr32),dr32 */
#define MOVodd(off, sr32, dr32) do { \
- emitm(&stream, (8 << 4) | 3 | (1 << 3), 1); \
+ emitm(&stream, 0x8b, 1); \
emitm(&stream, \
(1 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
emitm(&stream, off, 1); \
@@ -118,15 +118,14 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* movl (sr32,or32,1),dr32 */
#define MOVobd(sr32, or32, dr32) do { \
- emitm(&stream, (8 << 4) | 3 | (1 << 3), 1); \
+ emitm(&stream, 0x8b, 1); \
emitm(&stream, ((dr32 & 0x7) << 3) | 4, 1); \
emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
} while (0)
/* movw (sr32,or32,1),dr16 */
#define MOVobw(sr32, or32, dr16) do { \
- emitm(&stream, 0x66, 1); \
- emitm(&stream, (8 << 4) | 3 | (1 << 3), 1); \
+ emitm(&stream, 0x8b66, 2); \
emitm(&stream, ((dr16 & 0x7) << 3) | 4, 1); \
emitm(&stream, ((or32 & 0x7) << 3) | (sr32 & 0x7), 1); \
} while (0)
@@ -153,8 +152,7 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* xchgb %al,%ah */
#define SWAP_AX() do { \
- emitm(&stream, 0x86, 1); \
- emitm(&stream, 0xc4, 1); \
+ emitm(&stream, 0xc486, 2); \
} while (0)
/* pushl r32 */
@@ -169,15 +167,14 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* leave/ret */
#define LEAVE_RET() do { \
- emitm(&stream, 0xc9, 1); \
- emitm(&stream, 0xc3, 1); \
+ emitm(&stream, 0xc3c9, 2); \
} while (0)
/* addl sr32,dr32 */
#define ADDrd(sr32, dr32) do { \
- emitm(&stream, 0x03, 1); \
+ emitm(&stream, 0x01, 1); \
emitm(&stream, \
- (3 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
+ (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
} while (0)
/* addl i32,%eax */
@@ -202,9 +199,9 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* subl sr32,dr32 */
#define SUBrd(sr32, dr32) do { \
- emitm(&stream, 0x2b, 1); \
+ emitm(&stream, 0x29, 1); \
emitm(&stream, \
- (3 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
+ (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
} while (0)
/* subl i32,%eax */
@@ -227,8 +224,12 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* andb i8,r8 */
#define ANDib(i8, r8) do { \
- emitm(&stream, 0x80, 1); \
- emitm(&stream, (7 << 5) | r8, 1); \
+ if (r8 == AL) { \
+ emitm(&stream, 0x24, 1); \
+ } else { \
+ emitm(&stream, 0x80, 1); \
+ emitm(&stream, (7 << 5) | r8, 1); \
+ } \
emitm(&stream, i8, 1); \
} while (0)
@@ -236,38 +237,36 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
#define ANDid(i32, r32) do { \
if (r32 == EAX) { \
emitm(&stream, 0x25, 1); \
- emitm(&stream, i32, 4); \
} else { \
emitm(&stream, 0x81, 1); \
emitm(&stream, (7 << 5) | r32, 1); \
- emitm(&stream, i32, 4); \
} \
+ emitm(&stream, i32, 4); \
} while (0)
/* andl sr32,dr32 */
#define ANDrd(sr32, dr32) do { \
- emitm(&stream, 0x23, 1); \
+ emitm(&stream, 0x21, 1); \
emitm(&stream, \
- (3 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
+ (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
} while (0)
/* orl sr32,dr32 */
#define ORrd(sr32, dr32) do { \
- emitm(&stream, 0x0b, 1); \
+ emitm(&stream, 0x09, 1); \
emitm(&stream, \
- (3 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
+ (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
} while (0)
/* orl i32,r32 */
#define ORid(i32, r32) do { \
if (r32 == EAX) { \
emitm(&stream, 0x0d, 1); \
- emitm(&stream, i32, 4); \
} else { \
emitm(&stream, 0x81, 1); \
emitm(&stream, (25 << 3) | r32, 1); \
- emitm(&stream, i32, 4); \
} \
+ emitm(&stream, i32, 4); \
} while (0)
/* shll i8,r32 */
@@ -304,7 +303,7 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* cmpl off(sr32),dr32 */
#define CMPodd(off, sr32, dr32) do { \
- emitm(&stream, (3 << 4) | 3 | (1 << 3), 1); \
+ emitm(&stream, 0x3b, 1); \
emitm(&stream, \
(1 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
emitm(&stream, off, 1); \
@@ -312,9 +311,9 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* cmpl sr32,dr32 */
#define CMPrd(sr32, dr32) do { \
- emitm(&stream, 0x3b, 1); \
+ emitm(&stream, 0x39, 1); \
emitm(&stream, \
- (3 << 6) | ((dr32 & 0x7) << 3) | (sr32 & 0x7), 1); \
+ (3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
} while (0)
/* cmpl i32,dr32 */
@@ -337,15 +336,13 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* je off32 */
#define JE(off32) do { \
- emitm(&stream, 0x0f, 1); \
- emitm(&stream, 0x84, 1); \
+ emitm(&stream, 0x840f, 2); \
emitm(&stream, off32, 4); \
} while (0)
/* jle off32 */
#define JLE(off32) do { \
- emitm(&stream, 0x0f, 1); \
- emitm(&stream, 0x8e, 1); \
+ emitm(&stream, 0x8e0f, 2); \
emitm(&stream, off32, 4); \
} while (0)
@@ -357,29 +354,25 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
/* ja off32 */
#define JA(off32) do { \
- emitm(&stream, 0x0f, 1); \
- emitm(&stream, 0x87, 1); \
+ emitm(&stream, 0x870f, 2); \
emitm(&stream, off32, 4); \
} while (0)
/* jae off32 */
#define JAE(off32) do { \
- emitm(&stream, 0x0f, 1); \
- emitm(&stream, 0x83, 1); \
+ emitm(&stream, 0x830f, 2); \
emitm(&stream, off32, 4); \
} while (0)
/* jg off32 */
#define JG(off32) do { \
- emitm(&stream, 0x0f, 1); \
- emitm(&stream, 0x8f, 1); \
+ emitm(&stream, 0x8f0f, 2); \
emitm(&stream, off32, 4); \
} while (0)
/* jge off32 */
#define JGE(off32) do { \
- emitm(&stream, 0x0f, 1); \
- emitm(&stream, 0x8d, 1); \
+ emitm(&stream, 0x8d0f, 2); \
emitm(&stream, off32, 4); \
} while (0)
@@ -389,16 +382,10 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
emitm(&stream, off32, 4); \
} while (0)
-/* xorl %eax,%eax */
-#define ZERO_EAX() do { \
- emitm(&stream, 0x31, 1); \
- emitm(&stream, 0xc0, 1); \
-} while (0)
-
-/* xorl %edx,%edx */
-#define ZERO_EDX() do { \
+/* xorl r32,r32 */
+#define ZEROrd(r32) do { \
emitm(&stream, 0x31, 1); \
- emitm(&stream, 0xd2, 1); \
+ emitm(&stream, (3 << 6) | ((r32 & 0x7) << 3) | (r32 & 0x7), 1); \
} while (0)
#endif /* _BPF_JIT_MACHDEP_H_ */
OpenPOWER on IntegriCloud