]>
The Tcpdump Group git mirrors - libpcap/blob - optimize.c
2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Optimization module for tcpdump intermediate representation.
24 static const char rcsid
[] _U_
=
25 "@(#) $Header: /tcpdump/master/libpcap/optimize.c,v 1.88 2007-07-15 19:53:54 guy Exp $ (LBL)";
43 #ifdef HAVE_OS_PROTO_H
51 #if defined(MSDOS) && !defined(__DJGPP__)
52 extern int _w32_ffs (int mask
);
57 * Represents a deleted instruction.
62 * Register numbers for use-def values.
63 * 0 through BPF_MEMWORDS-1 represent the corresponding scratch memory
64 * location. A_ATOM is the accumulator and X_ATOM is the index
67 #define A_ATOM BPF_MEMWORDS
68 #define X_ATOM (BPF_MEMWORDS+1)
71 * This define is used to represent *both* the accumulator and
72 * x register in use-def computations.
73 * Currently, the use-def code assumes only one definition per instruction.
75 #define AX_ATOM N_ATOMS
78 * A flag to indicate that further optimization is needed.
79 * Iterative passes are continued until a given pass yields no
85 * A block is marked if only if its mark equals the current mark.
86 * Rather than traverse the code array, marking each item, 'cur_mark' is
87 * incremented. This automatically makes each element unmarked.
90 #define isMarked(p) ((p)->mark == cur_mark)
91 #define unMarkAll() cur_mark += 1
92 #define Mark(p) ((p)->mark = cur_mark)
94 static void opt_init(struct block
*);
95 static void opt_cleanup(void);
97 static void make_marks(struct block
*);
98 static void mark_code(struct block
*);
100 static void intern_blocks(struct block
*);
102 static int eq_slist(struct slist
*, struct slist
*);
104 static void find_levels_r(struct block
*);
106 static void find_levels(struct block
*);
107 static void find_dom(struct block
*);
108 static void propedom(struct edge
*);
109 static void find_edom(struct block
*);
110 static void find_closure(struct block
*);
111 static int atomuse(struct stmt
*);
112 static int atomdef(struct stmt
*);
113 static void compute_local_ud(struct block
*);
114 static void find_ud(struct block
*);
115 static void init_val(void);
116 static int F(int, int, int);
117 static inline void vstore(struct stmt
*, int *, int, int);
118 static void opt_blk(struct block
*, int);
119 static int use_conflict(struct block
*, struct block
*);
120 static void opt_j(struct edge
*);
121 static void or_pullup(struct block
*);
122 static void and_pullup(struct block
*);
123 static void opt_blks(struct block
*, int);
124 static inline void link_inedge(struct edge
*, struct block
*);
125 static void find_inedges(struct block
*);
126 static void opt_root(struct block
**);
127 static void opt_loop(struct block
*, int);
128 static void fold_op(struct stmt
*, int, int);
129 static inline struct slist
*this_op(struct slist
*);
130 static void opt_not(struct block
*);
131 static void opt_peep(struct block
*);
132 static void opt_stmt(struct stmt
*, int[], int);
133 static void deadstmt(struct stmt
*, struct stmt
*[]);
134 static void opt_deadstores(struct block
*);
135 static struct block
*fold_edge(struct block
*, struct edge
*);
136 static inline int eq_blk(struct block
*, struct block
*);
137 static int slength(struct slist
*);
138 static int count_blocks(struct block
*);
139 static void number_blks_r(struct block
*);
140 static int count_stmts(struct block
*);
141 static int convert_code_r(struct block
*);
143 static void opt_dump(struct block
*);
147 struct block
**blocks
;
152 * A bit vector set representation of the dominators.
153 * We round up the set size to the next power of two.
155 static int nodewords
;
156 static int edgewords
;
157 struct block
**levels
;
159 #define BITS_PER_WORD (8*sizeof(bpf_u_int32))
161 * True if a is in uset {p}
163 #define SET_MEMBER(p, a) \
164 ((p)[(unsigned)(a) / BITS_PER_WORD] & (1 << ((unsigned)(a) % BITS_PER_WORD)))
169 #define SET_INSERT(p, a) \
170 (p)[(unsigned)(a) / BITS_PER_WORD] |= (1 << ((unsigned)(a) % BITS_PER_WORD))
173 * Delete 'a' from uset p.
175 #define SET_DELETE(p, a) \
176 (p)[(unsigned)(a) / BITS_PER_WORD] &= ~(1 << ((unsigned)(a) % BITS_PER_WORD))
181 #define SET_INTERSECT(a, b, n)\
183 register bpf_u_int32 *_x = a, *_y = b;\
184 register int _n = n;\
185 while (--_n >= 0) *_x++ &= *_y++;\
191 #define SET_SUBTRACT(a, b, n)\
193 register bpf_u_int32 *_x = a, *_y = b;\
194 register int _n = n;\
195 while (--_n >= 0) *_x++ &=~ *_y++;\
201 #define SET_UNION(a, b, n)\
203 register bpf_u_int32 *_x = a, *_y = b;\
204 register int _n = n;\
205 while (--_n >= 0) *_x++ |= *_y++;\
208 static uset all_dom_sets
;
209 static uset all_closure_sets
;
210 static uset all_edge_sets
;
213 #define MAX(a,b) ((a)>(b)?(a):(b))
229 find_levels_r(JT(b
));
230 find_levels_r(JF(b
));
231 level
= MAX(JT(b
)->level
, JF(b
)->level
) + 1;
235 b
->link
= levels
[level
];
240 * Level graph. The levels go from 0 at the leaves to
241 * N_LEVELS at the root. The levels[] array points to the
242 * first node of the level list, whose elements are linked
243 * with the 'link' field of the struct block.
249 memset((char *)levels
, 0, n_blocks
* sizeof(*levels
));
255 * Find dominator relationships.
256 * Assumes graph has been leveled.
267 * Initialize sets to contain all nodes.
270 i
= n_blocks
* nodewords
;
273 /* Root starts off empty. */
274 for (i
= nodewords
; --i
>= 0;)
277 /* root->level is the highest level no found. */
278 for (i
= root
->level
; i
>= 0; --i
) {
279 for (b
= levels
[i
]; b
; b
= b
->link
) {
280 SET_INSERT(b
->dom
, b
->id
);
283 SET_INTERSECT(JT(b
)->dom
, b
->dom
, nodewords
);
284 SET_INTERSECT(JF(b
)->dom
, b
->dom
, nodewords
);
293 SET_INSERT(ep
->edom
, ep
->id
);
295 SET_INTERSECT(ep
->succ
->et
.edom
, ep
->edom
, edgewords
);
296 SET_INTERSECT(ep
->succ
->ef
.edom
, ep
->edom
, edgewords
);
301 * Compute edge dominators.
302 * Assumes graph has been leveled and predecessors established.
313 for (i
= n_edges
* edgewords
; --i
>= 0; )
316 /* root->level is the highest level no found. */
317 memset(root
->et
.edom
, 0, edgewords
* sizeof(*(uset
)0));
318 memset(root
->ef
.edom
, 0, edgewords
* sizeof(*(uset
)0));
319 for (i
= root
->level
; i
>= 0; --i
) {
320 for (b
= levels
[i
]; b
!= 0; b
= b
->link
) {
328 * Find the backwards transitive closure of the flow graph. These sets
329 * are backwards in the sense that we find the set of nodes that reach
330 * a given node, not the set of nodes that can be reached by a node.
332 * Assumes graph has been leveled.
342 * Initialize sets to contain no nodes.
344 memset((char *)all_closure_sets
, 0,
345 n_blocks
* nodewords
* sizeof(*all_closure_sets
));
347 /* root->level is the highest level no found. */
348 for (i
= root
->level
; i
>= 0; --i
) {
349 for (b
= levels
[i
]; b
; b
= b
->link
) {
350 SET_INSERT(b
->closure
, b
->id
);
353 SET_UNION(JT(b
)->closure
, b
->closure
, nodewords
);
354 SET_UNION(JF(b
)->closure
, b
->closure
, nodewords
);
360 * Return the register number that is used by s. If A and X are both
361 * used, return AX_ATOM. If no register is used, return -1.
363 * The implementation should probably change to an array access.
369 register int c
= s
->code
;
374 switch (BPF_CLASS(c
)) {
377 return (BPF_RVAL(c
) == BPF_A
) ? A_ATOM
:
378 (BPF_RVAL(c
) == BPF_X
) ? X_ATOM
: -1;
382 return (BPF_MODE(c
) == BPF_IND
) ? X_ATOM
:
383 (BPF_MODE(c
) == BPF_MEM
) ? s
->k
: -1;
393 if (BPF_SRC(c
) == BPF_X
)
398 return BPF_MISCOP(c
) == BPF_TXA
? X_ATOM
: A_ATOM
;
405 * Return the register number that is defined by 's'. We assume that
406 * a single stmt cannot define more than one register. If no register
407 * is defined, return -1.
409 * The implementation should probably change to an array access.
418 switch (BPF_CLASS(s
->code
)) {
432 return BPF_MISCOP(s
->code
) == BPF_TAX
? X_ATOM
: A_ATOM
;
438 * Compute the sets of registers used, defined, and killed by 'b'.
440 * "Used" means that a statement in 'b' uses the register before any
441 * statement in 'b' defines it, i.e. it uses the value left in
442 * that register by a predecessor block of this block.
443 * "Defined" means that a statement in 'b' defines it.
444 * "Killed" means that a statement in 'b' defines it before any
445 * statement in 'b' uses it, i.e. it kills the value left in that
446 * register by a predecessor block of this block.
453 atomset def
= 0, use
= 0, kill
= 0;
456 for (s
= b
->stmts
; s
; s
= s
->next
) {
457 if (s
->s
.code
== NOP
)
459 atom
= atomuse(&s
->s
);
461 if (atom
== AX_ATOM
) {
462 if (!ATOMELEM(def
, X_ATOM
))
463 use
|= ATOMMASK(X_ATOM
);
464 if (!ATOMELEM(def
, A_ATOM
))
465 use
|= ATOMMASK(A_ATOM
);
467 else if (atom
< N_ATOMS
) {
468 if (!ATOMELEM(def
, atom
))
469 use
|= ATOMMASK(atom
);
474 atom
= atomdef(&s
->s
);
476 if (!ATOMELEM(use
, atom
))
477 kill
|= ATOMMASK(atom
);
478 def
|= ATOMMASK(atom
);
481 if (BPF_CLASS(b
->s
.code
) == BPF_JMP
) {
483 * XXX - what about RET?
485 atom
= atomuse(&b
->s
);
487 if (atom
== AX_ATOM
) {
488 if (!ATOMELEM(def
, X_ATOM
))
489 use
|= ATOMMASK(X_ATOM
);
490 if (!ATOMELEM(def
, A_ATOM
))
491 use
|= ATOMMASK(A_ATOM
);
493 else if (atom
< N_ATOMS
) {
494 if (!ATOMELEM(def
, atom
))
495 use
|= ATOMMASK(atom
);
508 * Assume graph is already leveled.
518 * root->level is the highest level no found;
519 * count down from there.
521 maxlevel
= root
->level
;
522 for (i
= maxlevel
; i
>= 0; --i
)
523 for (p
= levels
[i
]; p
; p
= p
->link
) {
528 for (i
= 1; i
<= maxlevel
; ++i
) {
529 for (p
= levels
[i
]; p
; p
= p
->link
) {
530 p
->out_use
|= JT(p
)->in_use
| JF(p
)->in_use
;
531 p
->in_use
|= p
->out_use
&~ p
->kill
;
537 * These data structures are used in a Cocke and Shwarz style
538 * value numbering scheme. Since the flowgraph is acyclic,
539 * exit values can be propagated from a node's predecessors
540 * provided it is uniquely defined.
546 struct valnode
*next
;
550 static struct valnode
*hashtbl
[MODULUS
];
554 /* Integer constants mapped with the load immediate opcode. */
555 #define K(i) F(BPF_LD|BPF_IMM|BPF_W, i, 0L)
562 struct vmapinfo
*vmap
;
563 struct valnode
*vnode_base
;
564 struct valnode
*next_vnode
;
570 next_vnode
= vnode_base
;
571 memset((char *)vmap
, 0, maxval
* sizeof(*vmap
));
572 memset((char *)hashtbl
, 0, sizeof hashtbl
);
575 /* Because we really don't have an IR, this stuff is a little messy. */
585 hash
= (u_int
)code
^ (v0
<< 4) ^ (v1
<< 8);
588 for (p
= hashtbl
[hash
]; p
; p
= p
->next
)
589 if (p
->code
== code
&& p
->v0
== v0
&& p
->v1
== v1
)
593 if (BPF_MODE(code
) == BPF_IMM
&&
594 (BPF_CLASS(code
) == BPF_LD
|| BPF_CLASS(code
) == BPF_LDX
)) {
595 vmap
[val
].const_val
= v0
;
596 vmap
[val
].is_const
= 1;
603 p
->next
= hashtbl
[hash
];
610 vstore(s
, valp
, newval
, alter
)
616 if (alter
&& *valp
== newval
)
629 a
= vmap
[v0
].const_val
;
630 b
= vmap
[v1
].const_val
;
632 switch (BPF_OP(s
->code
)) {
647 bpf_error("division by zero");
675 s
->code
= BPF_LD
|BPF_IMM
;
679 static inline struct slist
*
683 while (s
!= 0 && s
->s
.code
== NOP
)
692 struct block
*tmp
= JT(b
);
703 struct slist
*next
, *last
;
711 for (/*empty*/; /*empty*/; s
= next
) {
717 break; /* nothing left in the block */
720 * Find the next real instruction after that one
723 next
= this_op(s
->next
);
725 break; /* no next instruction */
729 * st M[k] --> st M[k]
732 if (s
->s
.code
== BPF_ST
&&
733 next
->s
.code
== (BPF_LDX
|BPF_MEM
) &&
734 s
->s
.k
== next
->s
.k
) {
736 next
->s
.code
= BPF_MISC
|BPF_TAX
;
742 if (s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
743 next
->s
.code
== (BPF_MISC
|BPF_TAX
)) {
744 s
->s
.code
= BPF_LDX
|BPF_IMM
;
745 next
->s
.code
= BPF_MISC
|BPF_TXA
;
749 * This is an ugly special case, but it happens
750 * when you say tcp[k] or udp[k] where k is a constant.
752 if (s
->s
.code
== (BPF_LD
|BPF_IMM
)) {
753 struct slist
*add
, *tax
, *ild
;
756 * Check that X isn't used on exit from this
757 * block (which the optimizer might cause).
758 * We know the code generator won't generate
759 * any local dependencies.
761 if (ATOMELEM(b
->out_use
, X_ATOM
))
765 * Check that the instruction following the ldi
766 * is an addx, or it's an ldxms with an addx
767 * following it (with 0 or more nops between the
770 if (next
->s
.code
!= (BPF_LDX
|BPF_MSH
|BPF_B
))
773 add
= this_op(next
->next
);
774 if (add
== 0 || add
->s
.code
!= (BPF_ALU
|BPF_ADD
|BPF_X
))
778 * Check that a tax follows that (with 0 or more
779 * nops between them).
781 tax
= this_op(add
->next
);
782 if (tax
== 0 || tax
->s
.code
!= (BPF_MISC
|BPF_TAX
))
786 * Check that an ild follows that (with 0 or more
787 * nops between them).
789 ild
= this_op(tax
->next
);
790 if (ild
== 0 || BPF_CLASS(ild
->s
.code
) != BPF_LD
||
791 BPF_MODE(ild
->s
.code
) != BPF_IND
)
794 * We want to turn this sequence:
797 * (005) ldxms [14] {next} -- optional
800 * (008) ild [x+0] {ild}
802 * into this sequence:
810 * XXX We need to check that X is not
811 * subsequently used, because we want to change
812 * what'll be in it after this sequence.
814 * We know we can eliminate the accumulator
815 * modifications earlier in the sequence since
816 * it is defined by the last stmt of this sequence
817 * (i.e., the last statement of the sequence loads
818 * a value into the accumulator, so we can eliminate
819 * earlier operations on the accumulator).
829 * If the comparison at the end of a block is an equality
830 * comparison against a constant, and nobody uses the value
831 * we leave in the A register at the end of a block, and
832 * the operation preceding the comparison is an arithmetic
833 * operation, we can sometime optimize it away.
835 if (b
->s
.code
== (BPF_JMP
|BPF_JEQ
|BPF_K
) &&
836 !ATOMELEM(b
->out_use
, A_ATOM
)) {
838 * We can optimize away certain subtractions of the
841 if (last
->s
.code
== (BPF_ALU
|BPF_SUB
|BPF_X
)) {
842 val
= b
->val
[X_ATOM
];
843 if (vmap
[val
].is_const
) {
845 * If we have a subtract to do a comparison,
846 * and the X register is a known constant,
847 * we can merge this value into the
853 b
->s
.k
+= vmap
[val
].const_val
;
856 } else if (b
->s
.k
== 0) {
858 * If the X register isn't a constant,
859 * and the comparison in the test is
860 * against 0, we can compare with the
861 * X register, instead:
867 b
->s
.code
= BPF_JMP
|BPF_JEQ
|BPF_X
;
872 * Likewise, a constant subtract can be simplified:
875 * jeq #y -> jeq #(x+y)
877 else if (last
->s
.code
== (BPF_ALU
|BPF_SUB
|BPF_K
)) {
883 * And, similarly, a constant AND can be simplified
884 * if we're testing against 0, i.e.:
889 else if (last
->s
.code
== (BPF_ALU
|BPF_AND
|BPF_K
) &&
892 b
->s
.code
= BPF_JMP
|BPF_K
|BPF_JSET
;
900 * jset #ffffffff -> always
902 if (b
->s
.code
== (BPF_JMP
|BPF_K
|BPF_JSET
)) {
905 if (b
->s
.k
== 0xffffffff)
909 * If we're comparing against the index register, and the index
910 * register is a known constant, we can just compare against that
913 val
= b
->val
[X_ATOM
];
914 if (vmap
[val
].is_const
&& BPF_SRC(b
->s
.code
) == BPF_X
) {
915 bpf_int32 v
= vmap
[val
].const_val
;
920 * If the accumulator is a known constant, we can compute the
923 val
= b
->val
[A_ATOM
];
924 if (vmap
[val
].is_const
&& BPF_SRC(b
->s
.code
) == BPF_K
) {
925 bpf_int32 v
= vmap
[val
].const_val
;
926 switch (BPF_OP(b
->s
.code
)) {
933 v
= (unsigned)v
> b
->s
.k
;
937 v
= (unsigned)v
>= b
->s
.k
;
957 * Compute the symbolic value of expression of 's', and update
958 * anything it defines in the value table 'val'. If 'alter' is true,
959 * do various optimizations. This code would be cleaner if symbolic
960 * evaluation and code transformations weren't folded together.
963 opt_stmt(s
, val
, alter
)
973 case BPF_LD
|BPF_ABS
|BPF_W
:
974 case BPF_LD
|BPF_ABS
|BPF_H
:
975 case BPF_LD
|BPF_ABS
|BPF_B
:
976 v
= F(s
->code
, s
->k
, 0L);
977 vstore(s
, &val
[A_ATOM
], v
, alter
);
980 case BPF_LD
|BPF_IND
|BPF_W
:
981 case BPF_LD
|BPF_IND
|BPF_H
:
982 case BPF_LD
|BPF_IND
|BPF_B
:
984 if (alter
&& vmap
[v
].is_const
) {
985 s
->code
= BPF_LD
|BPF_ABS
|BPF_SIZE(s
->code
);
986 s
->k
+= vmap
[v
].const_val
;
987 v
= F(s
->code
, s
->k
, 0L);
991 v
= F(s
->code
, s
->k
, v
);
992 vstore(s
, &val
[A_ATOM
], v
, alter
);
996 v
= F(s
->code
, 0L, 0L);
997 vstore(s
, &val
[A_ATOM
], v
, alter
);
1000 case BPF_LD
|BPF_IMM
:
1002 vstore(s
, &val
[A_ATOM
], v
, alter
);
1005 case BPF_LDX
|BPF_IMM
:
1007 vstore(s
, &val
[X_ATOM
], v
, alter
);
1010 case BPF_LDX
|BPF_MSH
|BPF_B
:
1011 v
= F(s
->code
, s
->k
, 0L);
1012 vstore(s
, &val
[X_ATOM
], v
, alter
);
1015 case BPF_ALU
|BPF_NEG
:
1016 if (alter
&& vmap
[val
[A_ATOM
]].is_const
) {
1017 s
->code
= BPF_LD
|BPF_IMM
;
1018 s
->k
= -vmap
[val
[A_ATOM
]].const_val
;
1019 val
[A_ATOM
] = K(s
->k
);
1022 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], 0L);
1025 case BPF_ALU
|BPF_ADD
|BPF_K
:
1026 case BPF_ALU
|BPF_SUB
|BPF_K
:
1027 case BPF_ALU
|BPF_MUL
|BPF_K
:
1028 case BPF_ALU
|BPF_DIV
|BPF_K
:
1029 case BPF_ALU
|BPF_AND
|BPF_K
:
1030 case BPF_ALU
|BPF_OR
|BPF_K
:
1031 case BPF_ALU
|BPF_LSH
|BPF_K
:
1032 case BPF_ALU
|BPF_RSH
|BPF_K
:
1033 op
= BPF_OP(s
->code
);
1036 /* don't optimize away "sub #0"
1037 * as it may be needed later to
1038 * fixup the generated math code */
1039 if (op
== BPF_ADD
||
1040 op
== BPF_LSH
|| op
== BPF_RSH
||
1045 if (op
== BPF_MUL
|| op
== BPF_AND
) {
1046 s
->code
= BPF_LD
|BPF_IMM
;
1047 val
[A_ATOM
] = K(s
->k
);
1051 if (vmap
[val
[A_ATOM
]].is_const
) {
1052 fold_op(s
, val
[A_ATOM
], K(s
->k
));
1053 val
[A_ATOM
] = K(s
->k
);
1057 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], K(s
->k
));
1060 case BPF_ALU
|BPF_ADD
|BPF_X
:
1061 case BPF_ALU
|BPF_SUB
|BPF_X
:
1062 case BPF_ALU
|BPF_MUL
|BPF_X
:
1063 case BPF_ALU
|BPF_DIV
|BPF_X
:
1064 case BPF_ALU
|BPF_AND
|BPF_X
:
1065 case BPF_ALU
|BPF_OR
|BPF_X
:
1066 case BPF_ALU
|BPF_LSH
|BPF_X
:
1067 case BPF_ALU
|BPF_RSH
|BPF_X
:
1068 op
= BPF_OP(s
->code
);
1069 if (alter
&& vmap
[val
[X_ATOM
]].is_const
) {
1070 if (vmap
[val
[A_ATOM
]].is_const
) {
1071 fold_op(s
, val
[A_ATOM
], val
[X_ATOM
]);
1072 val
[A_ATOM
] = K(s
->k
);
1075 s
->code
= BPF_ALU
|BPF_K
|op
;
1076 s
->k
= vmap
[val
[X_ATOM
]].const_val
;
1079 F(s
->code
, val
[A_ATOM
], K(s
->k
));
1084 * Check if we're doing something to an accumulator
1085 * that is 0, and simplify. This may not seem like
1086 * much of a simplification but it could open up further
1088 * XXX We could also check for mul by 1, etc.
1090 if (alter
&& vmap
[val
[A_ATOM
]].is_const
1091 && vmap
[val
[A_ATOM
]].const_val
== 0) {
1092 if (op
== BPF_ADD
|| op
== BPF_OR
) {
1093 s
->code
= BPF_MISC
|BPF_TXA
;
1094 vstore(s
, &val
[A_ATOM
], val
[X_ATOM
], alter
);
1097 else if (op
== BPF_MUL
|| op
== BPF_DIV
||
1098 op
== BPF_AND
|| op
== BPF_LSH
|| op
== BPF_RSH
) {
1099 s
->code
= BPF_LD
|BPF_IMM
;
1101 vstore(s
, &val
[A_ATOM
], K(s
->k
), alter
);
1104 else if (op
== BPF_NEG
) {
1109 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], val
[X_ATOM
]);
1112 case BPF_MISC
|BPF_TXA
:
1113 vstore(s
, &val
[A_ATOM
], val
[X_ATOM
], alter
);
1116 case BPF_LD
|BPF_MEM
:
1118 if (alter
&& vmap
[v
].is_const
) {
1119 s
->code
= BPF_LD
|BPF_IMM
;
1120 s
->k
= vmap
[v
].const_val
;
1123 vstore(s
, &val
[A_ATOM
], v
, alter
);
1126 case BPF_MISC
|BPF_TAX
:
1127 vstore(s
, &val
[X_ATOM
], val
[A_ATOM
], alter
);
1130 case BPF_LDX
|BPF_MEM
:
1132 if (alter
&& vmap
[v
].is_const
) {
1133 s
->code
= BPF_LDX
|BPF_IMM
;
1134 s
->k
= vmap
[v
].const_val
;
1137 vstore(s
, &val
[X_ATOM
], v
, alter
);
1141 vstore(s
, &val
[s
->k
], val
[A_ATOM
], alter
);
1145 vstore(s
, &val
[s
->k
], val
[X_ATOM
], alter
);
1152 register struct stmt
*s
;
1153 register struct stmt
*last
[];
1159 if (atom
== AX_ATOM
) {
1170 last
[atom
]->code
= NOP
;
1178 register struct block
*b
;
1180 register struct slist
*s
;
1182 struct stmt
*last
[N_ATOMS
];
1184 memset((char *)last
, 0, sizeof last
);
1186 for (s
= b
->stmts
; s
!= 0; s
= s
->next
)
1187 deadstmt(&s
->s
, last
);
1188 deadstmt(&b
->s
, last
);
1190 for (atom
= 0; atom
< N_ATOMS
; ++atom
)
1191 if (last
[atom
] && !ATOMELEM(b
->out_use
, atom
)) {
1192 last
[atom
]->code
= NOP
;
1198 opt_blk(b
, do_stmts
)
1205 bpf_int32 aval
, xval
;
1208 for (s
= b
->stmts
; s
&& s
->next
; s
= s
->next
)
1209 if (BPF_CLASS(s
->s
.code
) == BPF_JMP
) {
1216 * Initialize the atom values.
1221 * We have no predecessors, so everything is undefined
1222 * upon entry to this block.
1224 memset((char *)b
->val
, 0, sizeof(b
->val
));
1227 * Inherit values from our predecessors.
1229 * First, get the values from the predecessor along the
1230 * first edge leading to this node.
1232 memcpy((char *)b
->val
, (char *)p
->pred
->val
, sizeof(b
->val
));
1234 * Now look at all the other nodes leading to this node.
1235 * If, for the predecessor along that edge, a register
1236 * has a different value from the one we have (i.e.,
1237 * control paths are merging, and the merging paths
1238 * assign different values to that register), give the
1239 * register the undefined value of 0.
1241 while ((p
= p
->next
) != NULL
) {
1242 for (i
= 0; i
< N_ATOMS
; ++i
)
1243 if (b
->val
[i
] != p
->pred
->val
[i
])
1247 aval
= b
->val
[A_ATOM
];
1248 xval
= b
->val
[X_ATOM
];
1249 for (s
= b
->stmts
; s
; s
= s
->next
)
1250 opt_stmt(&s
->s
, b
->val
, do_stmts
);
1253 * This is a special case: if we don't use anything from this
1254 * block, and we load the accumulator or index register with a
1255 * value that is already there, or if this block is a return,
1256 * eliminate all the statements.
1258 * XXX - what if it does a store?
1260 * XXX - why does it matter whether we use anything from this
1261 * block? If the accumulator or index register doesn't change
1262 * its value, isn't that OK even if we use that value?
1264 * XXX - if we load the accumulator with a different value,
1265 * and the block ends with a conditional branch, we obviously
1266 * can't eliminate it, as the branch depends on that value.
1267 * For the index register, the conditional branch only depends
1268 * on the index register value if the test is against the index
1269 * register value rather than a constant; if nothing uses the
1270 * value we put into the index register, and we're not testing
1271 * against the index register's value, and there aren't any
1272 * other problems that would keep us from eliminating this
1273 * block, can we eliminate it?
1276 ((b
->out_use
== 0 && aval
!= 0 && b
->val
[A_ATOM
] == aval
&&
1277 xval
!= 0 && b
->val
[X_ATOM
] == xval
) ||
1278 BPF_CLASS(b
->s
.code
) == BPF_RET
)) {
1279 if (b
->stmts
!= 0) {
1288 * Set up values for branch optimizer.
1290 if (BPF_SRC(b
->s
.code
) == BPF_K
)
1291 b
->oval
= K(b
->s
.k
);
1293 b
->oval
= b
->val
[X_ATOM
];
1294 b
->et
.code
= b
->s
.code
;
1295 b
->ef
.code
= -b
->s
.code
;
1299 * Return true if any register that is used on exit from 'succ', has
1300 * an exit value that is different from the corresponding exit value
1304 use_conflict(b
, succ
)
1305 struct block
*b
, *succ
;
1308 atomset use
= succ
->out_use
;
1313 for (atom
= 0; atom
< N_ATOMS
; ++atom
)
1314 if (ATOMELEM(use
, atom
))
1315 if (b
->val
[atom
] != succ
->val
[atom
])
1320 static struct block
*
1321 fold_edge(child
, ep
)
1322 struct block
*child
;
1326 int aval0
, aval1
, oval0
, oval1
;
1327 int code
= ep
->code
;
1335 if (child
->s
.code
!= code
)
1338 aval0
= child
->val
[A_ATOM
];
1339 oval0
= child
->oval
;
1340 aval1
= ep
->pred
->val
[A_ATOM
];
1341 oval1
= ep
->pred
->oval
;
1348 * The operands of the branch instructions are
1349 * identical, so the result is true if a true
1350 * branch was taken to get here, otherwise false.
1352 return sense
? JT(child
) : JF(child
);
1354 if (sense
&& code
== (BPF_JMP
|BPF_JEQ
|BPF_K
))
1356 * At this point, we only know the comparison if we
1357 * came down the true branch, and it was an equality
1358 * comparison with a constant.
1360 * I.e., if we came down the true branch, and the branch
1361 * was an equality comparison with a constant, we know the
1362 * accumulator contains that constant. If we came down
1363 * the false branch, or the comparison wasn't with a
1364 * constant, we don't know what was in the accumulator.
1366 * We rely on the fact that distinct constants have distinct
1379 register struct block
*target
;
1381 if (JT(ep
->succ
) == 0)
1384 if (JT(ep
->succ
) == JF(ep
->succ
)) {
1386 * Common branch targets can be eliminated, provided
1387 * there is no data dependency.
1389 if (!use_conflict(ep
->pred
, ep
->succ
->et
.succ
)) {
1391 ep
->succ
= JT(ep
->succ
);
1395 * For each edge dominator that matches the successor of this
1396 * edge, promote the edge successor to the its grandchild.
1398 * XXX We violate the set abstraction here in favor a reasonably
1402 for (i
= 0; i
< edgewords
; ++i
) {
1403 register bpf_u_int32 x
= ep
->edom
[i
];
1408 k
+= i
* BITS_PER_WORD
;
1410 target
= fold_edge(ep
->succ
, edges
[k
]);
1412 * Check that there is no data dependency between
1413 * nodes that will be violated if we move the edge.
1415 if (target
!= 0 && !use_conflict(ep
->pred
, target
)) {
1418 if (JT(target
) != 0)
1420 * Start over unless we hit a leaf.
1436 struct block
**diffp
, **samep
;
1444 * Make sure each predecessor loads the same value.
1447 val
= ep
->pred
->val
[A_ATOM
];
1448 for (ep
= ep
->next
; ep
!= 0; ep
= ep
->next
)
1449 if (val
!= ep
->pred
->val
[A_ATOM
])
1452 if (JT(b
->in_edges
->pred
) == b
)
1453 diffp
= &JT(b
->in_edges
->pred
);
1455 diffp
= &JF(b
->in_edges
->pred
);
1462 if (JT(*diffp
) != JT(b
))
1465 if (!SET_MEMBER((*diffp
)->dom
, b
->id
))
1468 if ((*diffp
)->val
[A_ATOM
] != val
)
1471 diffp
= &JF(*diffp
);
1474 samep
= &JF(*diffp
);
1479 if (JT(*samep
) != JT(b
))
1482 if (!SET_MEMBER((*samep
)->dom
, b
->id
))
1485 if ((*samep
)->val
[A_ATOM
] == val
)
1488 /* XXX Need to check that there are no data dependencies
1489 between dp0 and dp1. Currently, the code generator
1490 will not produce such dependencies. */
1491 samep
= &JF(*samep
);
1494 /* XXX This doesn't cover everything. */
1495 for (i
= 0; i
< N_ATOMS
; ++i
)
1496 if ((*samep
)->val
[i
] != pred
->val
[i
])
1499 /* Pull up the node. */
1505 * At the top of the chain, each predecessor needs to point at the
1506 * pulled up node. Inside the chain, there is only one predecessor
1510 for (ep
= b
->in_edges
; ep
!= 0; ep
= ep
->next
) {
1511 if (JT(ep
->pred
) == b
)
1512 JT(ep
->pred
) = pull
;
1514 JF(ep
->pred
) = pull
;
1529 struct block
**diffp
, **samep
;
1537 * Make sure each predecessor loads the same value.
1539 val
= ep
->pred
->val
[A_ATOM
];
1540 for (ep
= ep
->next
; ep
!= 0; ep
= ep
->next
)
1541 if (val
!= ep
->pred
->val
[A_ATOM
])
1544 if (JT(b
->in_edges
->pred
) == b
)
1545 diffp
= &JT(b
->in_edges
->pred
);
1547 diffp
= &JF(b
->in_edges
->pred
);
1554 if (JF(*diffp
) != JF(b
))
1557 if (!SET_MEMBER((*diffp
)->dom
, b
->id
))
1560 if ((*diffp
)->val
[A_ATOM
] != val
)
1563 diffp
= &JT(*diffp
);
1566 samep
= &JT(*diffp
);
1571 if (JF(*samep
) != JF(b
))
1574 if (!SET_MEMBER((*samep
)->dom
, b
->id
))
1577 if ((*samep
)->val
[A_ATOM
] == val
)
1580 /* XXX Need to check that there are no data dependencies
1581 between diffp and samep. Currently, the code generator
1582 will not produce such dependencies. */
1583 samep
= &JT(*samep
);
1586 /* XXX This doesn't cover everything. */
1587 for (i
= 0; i
< N_ATOMS
; ++i
)
1588 if ((*samep
)->val
[i
] != pred
->val
[i
])
1591 /* Pull up the node. */
1597 * At the top of the chain, each predecessor needs to point at the
1598 * pulled up node. Inside the chain, there is only one predecessor
1602 for (ep
= b
->in_edges
; ep
!= 0; ep
= ep
->next
) {
1603 if (JT(ep
->pred
) == b
)
1604 JT(ep
->pred
) = pull
;
1606 JF(ep
->pred
) = pull
;
1616 opt_blks(root
, do_stmts
)
1624 maxlevel
= root
->level
;
1627 for (i
= maxlevel
; i
>= 0; --i
)
1628 for (p
= levels
[i
]; p
; p
= p
->link
)
1629 opt_blk(p
, do_stmts
);
1633 * No point trying to move branches; it can't possibly
1634 * make a difference at this point.
1638 for (i
= 1; i
<= maxlevel
; ++i
) {
1639 for (p
= levels
[i
]; p
; p
= p
->link
) {
1646 for (i
= 1; i
<= maxlevel
; ++i
) {
1647 for (p
= levels
[i
]; p
; p
= p
->link
) {
1655 link_inedge(parent
, child
)
1656 struct edge
*parent
;
1657 struct block
*child
;
1659 parent
->next
= child
->in_edges
;
1660 child
->in_edges
= parent
;
1670 for (i
= 0; i
< n_blocks
; ++i
)
1671 blocks
[i
]->in_edges
= 0;
1674 * Traverse the graph, adding each edge to the predecessor
1675 * list of its successors. Skip the leaves (i.e. level 0).
1677 for (i
= root
->level
; i
> 0; --i
) {
1678 for (b
= levels
[i
]; b
!= 0; b
= b
->link
) {
1679 link_inedge(&b
->et
, JT(b
));
1680 link_inedge(&b
->ef
, JF(b
));
1689 struct slist
*tmp
, *s
;
1693 while (BPF_CLASS((*b
)->s
.code
) == BPF_JMP
&& JT(*b
) == JF(*b
))
1702 * If the root node is a return, then there is no
1703 * point executing any statements (since the bpf machine
1704 * has no side effects).
1706 if (BPF_CLASS((*b
)->s
.code
) == BPF_RET
)
1711 opt_loop(root
, do_stmts
)
1718 printf("opt_loop(root, %d) begin\n", do_stmts
);
1729 opt_blks(root
, do_stmts
);
1732 printf("opt_loop(root, %d) bottom, done=%d\n", do_stmts
, done
);
1740 * Optimize the filter code in its dag representation.
1744 struct block
**rootp
;
1753 intern_blocks(root
);
1756 printf("after intern_blocks()\n");
1763 printf("after opt_root()\n");
1776 if (BPF_CLASS(p
->s
.code
) != BPF_RET
) {
1784 * Mark code array such that isMarked(i) is true
1785 * only for nodes that are alive.
1796 * True iff the two stmt lists load the same value from the packet into
1801 struct slist
*x
, *y
;
1804 while (x
&& x
->s
.code
== NOP
)
1806 while (y
&& y
->s
.code
== NOP
)
1812 if (x
->s
.code
!= y
->s
.code
|| x
->s
.k
!= y
->s
.k
)
1821 struct block
*b0
, *b1
;
1823 if (b0
->s
.code
== b1
->s
.code
&&
1824 b0
->s
.k
== b1
->s
.k
&&
1825 b0
->et
.succ
== b1
->et
.succ
&&
1826 b0
->ef
.succ
== b1
->ef
.succ
)
1827 return eq_slist(b0
->stmts
, b1
->stmts
);
1837 int done1
; /* don't shadow global */
1840 for (i
= 0; i
< n_blocks
; ++i
)
1841 blocks
[i
]->link
= 0;
1845 for (i
= n_blocks
- 1; --i
>= 0; ) {
1846 if (!isMarked(blocks
[i
]))
1848 for (j
= i
+ 1; j
< n_blocks
; ++j
) {
1849 if (!isMarked(blocks
[j
]))
1851 if (eq_blk(blocks
[i
], blocks
[j
])) {
1852 blocks
[i
]->link
= blocks
[j
]->link
?
1853 blocks
[j
]->link
: blocks
[j
];
1858 for (i
= 0; i
< n_blocks
; ++i
) {
1864 JT(p
) = JT(p
)->link
;
1868 JF(p
) = JF(p
)->link
;
1878 free((void *)vnode_base
);
1880 free((void *)edges
);
1881 free((void *)space
);
1882 free((void *)levels
);
1883 free((void *)blocks
);
1887 * Return the number of stmts in 's'.
1895 for (; s
; s
= s
->next
)
1896 if (s
->s
.code
!= NOP
)
1902 * Return the number of nodes reachable by 'p'.
1903 * All nodes should be initially unmarked.
1909 if (p
== 0 || isMarked(p
))
1912 return count_blocks(JT(p
)) + count_blocks(JF(p
)) + 1;
1916 * Do a depth first search on the flow graph, numbering the
1917 * the basic blocks, and entering them into the 'blocks' array.`
1925 if (p
== 0 || isMarked(p
))
1933 number_blks_r(JT(p
));
1934 number_blks_r(JF(p
));
1938 * Return the number of stmts in the flowgraph reachable by 'p'.
1939 * The nodes should be unmarked before calling.
1941 * Note that "stmts" means "instructions", and that this includes
1943 * side-effect statements in 'p' (slength(p->stmts));
1945 * statements in the true branch from 'p' (count_stmts(JT(p)));
1947 * statements in the false branch from 'p' (count_stmts(JF(p)));
1949 * the conditional jump itself (1);
1951 * an extra long jump if the true branch requires it (p->longjt);
1953 * an extra long jump if the false branch requires it (p->longjf).
1961 if (p
== 0 || isMarked(p
))
1964 n
= count_stmts(JT(p
)) + count_stmts(JF(p
));
1965 return slength(p
->stmts
) + n
+ 1 + p
->longjt
+ p
->longjf
;
1969 * Allocate memory. All allocation is done before optimization
1970 * is begun. A linear bound on the size of all data structures is computed
1971 * from the total number of blocks and/or statements.
1978 int i
, n
, max_stmts
;
1981 * First, count the blocks, so we can malloc an array to map
1982 * block number to block. Then, put the blocks into the array.
1985 n
= count_blocks(root
);
1986 blocks
= (struct block
**)malloc(n
* sizeof(*blocks
));
1988 bpf_error("malloc");
1991 number_blks_r(root
);
1993 n_edges
= 2 * n_blocks
;
1994 edges
= (struct edge
**)malloc(n_edges
* sizeof(*edges
));
1996 bpf_error("malloc");
1999 * The number of levels is bounded by the number of nodes.
2001 levels
= (struct block
**)malloc(n_blocks
* sizeof(*levels
));
2003 bpf_error("malloc");
2005 edgewords
= n_edges
/ (8 * sizeof(bpf_u_int32
)) + 1;
2006 nodewords
= n_blocks
/ (8 * sizeof(bpf_u_int32
)) + 1;
2009 space
= (bpf_u_int32
*)malloc(2 * n_blocks
* nodewords
* sizeof(*space
)
2010 + n_edges
* edgewords
* sizeof(*space
));
2012 bpf_error("malloc");
2015 for (i
= 0; i
< n
; ++i
) {
2019 all_closure_sets
= p
;
2020 for (i
= 0; i
< n
; ++i
) {
2021 blocks
[i
]->closure
= p
;
2025 for (i
= 0; i
< n
; ++i
) {
2026 register struct block
*b
= blocks
[i
];
2034 b
->ef
.id
= n_blocks
+ i
;
2035 edges
[n_blocks
+ i
] = &b
->ef
;
2040 for (i
= 0; i
< n
; ++i
)
2041 max_stmts
+= slength(blocks
[i
]->stmts
) + 1;
2043 * We allocate at most 3 value numbers per statement,
2044 * so this is an upper bound on the number of valnodes
2047 maxval
= 3 * max_stmts
;
2048 vmap
= (struct vmapinfo
*)malloc(maxval
* sizeof(*vmap
));
2049 vnode_base
= (struct valnode
*)malloc(maxval
* sizeof(*vnode_base
));
2050 if (vmap
== NULL
|| vnode_base
== NULL
)
2051 bpf_error("malloc");
2055 * Some pointers used to convert the basic block form of the code,
2056 * into the array form that BPF requires. 'fstart' will point to
2057 * the malloc'd array while 'ftail' is used during the recursive traversal.
2059 static struct bpf_insn
*fstart
;
2060 static struct bpf_insn
*ftail
;
2067 * Returns true if successful. Returns false if a branch has
2068 * an offset that is too large. If so, we have marked that
2069 * branch so that on a subsequent iteration, it will be treated
2076 struct bpf_insn
*dst
;
2080 int extrajmps
; /* number of extra jumps inserted */
2081 struct slist
**offset
= NULL
;
2083 if (p
== 0 || isMarked(p
))
2087 if (convert_code_r(JF(p
)) == 0)
2089 if (convert_code_r(JT(p
)) == 0)
2092 slen
= slength(p
->stmts
);
2093 dst
= ftail
-= (slen
+ 1 + p
->longjt
+ p
->longjf
);
2094 /* inflate length by any extra jumps */
2096 p
->offset
= dst
- fstart
;
2098 /* generate offset[] for convenience */
2100 offset
= (struct slist
**)calloc(slen
, sizeof(struct slist
*));
2102 bpf_error("not enough core");
2107 for (off
= 0; off
< slen
&& src
; off
++) {
2109 printf("off=%d src=%x\n", off
, src
);
2116 for (src
= p
->stmts
; src
; src
= src
->next
) {
2117 if (src
->s
.code
== NOP
)
2119 dst
->code
= (u_short
)src
->s
.code
;
2122 /* fill block-local relative jump */
2123 if (BPF_CLASS(src
->s
.code
) != BPF_JMP
|| src
->s
.code
== (BPF_JMP
|BPF_JA
)) {
2125 if (src
->s
.jt
|| src
->s
.jf
) {
2126 bpf_error("illegal jmp destination");
2132 if (off
== slen
- 2) /*???*/
2138 const char *ljerr
= "%s for block-local relative jump: off=%d";
2141 printf("code=%x off=%d %x %x\n", src
->s
.code
,
2142 off
, src
->s
.jt
, src
->s
.jf
);
2145 if (!src
->s
.jt
|| !src
->s
.jf
) {
2146 bpf_error(ljerr
, "no jmp destination", off
);
2151 for (i
= 0; i
< slen
; i
++) {
2152 if (offset
[i
] == src
->s
.jt
) {
2154 bpf_error(ljerr
, "multiple matches", off
);
2158 dst
->jt
= i
- off
- 1;
2161 if (offset
[i
] == src
->s
.jf
) {
2163 bpf_error(ljerr
, "multiple matches", off
);
2166 dst
->jf
= i
- off
- 1;
2171 bpf_error(ljerr
, "no destination found", off
);
2183 bids
[dst
- fstart
] = p
->id
+ 1;
2185 dst
->code
= (u_short
)p
->s
.code
;
2189 off
= JT(p
)->offset
- (p
->offset
+ slen
) - 1;
2191 /* offset too large for branch, must add a jump */
2192 if (p
->longjt
== 0) {
2193 /* mark this instruction and retry */
2197 /* branch if T to following jump */
2198 dst
->jt
= extrajmps
;
2200 dst
[extrajmps
].code
= BPF_JMP
|BPF_JA
;
2201 dst
[extrajmps
].k
= off
- extrajmps
;
2205 off
= JF(p
)->offset
- (p
->offset
+ slen
) - 1;
2207 /* offset too large for branch, must add a jump */
2208 if (p
->longjf
== 0) {
2209 /* mark this instruction and retry */
2213 /* branch if F to following jump */
2214 /* if two jumps are inserted, F goes to second one */
2215 dst
->jf
= extrajmps
;
2217 dst
[extrajmps
].code
= BPF_JMP
|BPF_JA
;
2218 dst
[extrajmps
].k
= off
- extrajmps
;
2228 * Convert flowgraph intermediate representation to the
2229 * BPF array representation. Set *lenp to the number of instructions.
2231 * This routine does *NOT* leak the memory pointed to by fp. It *must
2232 * not* do free(fp) before returning fp; doing so would make no sense,
2233 * as the BPF array pointed to by the return value of icode_to_fcode()
2234 * must be valid - it's being returned for use in a bpf_program structure.
2236 * If it appears that icode_to_fcode() is leaking, the problem is that
2237 * the program using pcap_compile() is failing to free the memory in
2238 * the BPF program when it's done - the leak is in the program, not in
2239 * the routine that happens to be allocating the memory. (By analogy, if
2240 * a program calls fopen() without ever calling fclose() on the FILE *,
2241 * it will leak the FILE structure; the leak is not in fopen(), it's in
2242 * the program.) Change the program to use pcap_freecode() when it's
2243 * done with the filter program. See the pcap man page.
2246 icode_to_fcode(root
, lenp
)
2251 struct bpf_insn
*fp
;
2254 * Loop doing convert_code_r() until no branches remain
2255 * with too-large offsets.
2259 n
= *lenp
= count_stmts(root
);
2261 fp
= (struct bpf_insn
*)malloc(sizeof(*fp
) * n
);
2263 bpf_error("malloc");
2264 memset((char *)fp
, 0, sizeof(*fp
) * n
);
2269 if (convert_code_r(root
))
2278 * Make a copy of a BPF program and put it in the "fcode" member of
2281 * If we fail to allocate memory for the copy, fill in the "errbuf"
2282 * member of the "pcap_t" with an error message, and return -1;
2283 * otherwise, return 0.
2286 install_bpf_program(pcap_t
*p
, struct bpf_program
*fp
)
2291 * Free up any already installed program.
2293 pcap_freecode(&p
->fcode
);
2295 prog_size
= sizeof(*fp
->bf_insns
) * fp
->bf_len
;
2296 p
->fcode
.bf_len
= fp
->bf_len
;
2297 p
->fcode
.bf_insns
= (struct bpf_insn
*)malloc(prog_size
);
2298 if (p
->fcode
.bf_insns
== NULL
) {
2299 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2300 "malloc: %s", pcap_strerror(errno
));
2303 memcpy(p
->fcode
.bf_insns
, fp
->bf_insns
, prog_size
);
2312 struct bpf_program f
;
2314 memset(bids
, 0, sizeof bids
);
2315 f
.bf_insns
= icode_to_fcode(root
, &f
.bf_len
);
2318 free((char *)f
.bf_insns
);