optimize.c (26175) | optimize.c (56889) |
---|---|
1/* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 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 --- 8 unchanged lines hidden (view full) --- 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. 20 * 21 * Optimization module for tcpdump intermediate representation. 22 */ 23#ifndef lint 24static const char rcsid[] = | 1/* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 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 --- 8 unchanged lines hidden (view full) --- 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. 20 * 21 * Optimization module for tcpdump intermediate representation. 22 */ 23#ifndef lint 24static const char rcsid[] = |
25 "@(#) $Header: optimize.c,v 1.60 96/09/26 23:28:14 leres Exp $ (LBL)"; | 25 "@(#) $Header: /tcpdump/master/libpcap/optimize.c,v 1.61 1999/10/19 15:18:30 itojun Exp $ (LBL)"; |
26#endif 27 28#include <sys/types.h> 29#include <sys/time.h> 30 31#include <stdio.h> 32#include <stdlib.h> 33#include <memory.h> --- 1065 unchanged lines hidden (view full) --- 1099 struct block *b; 1100 int do_stmts; 1101{ 1102 struct slist *s; 1103 struct edge *p; 1104 int i; 1105 bpf_int32 aval; 1106 | 26#endif 27 28#include <sys/types.h> 29#include <sys/time.h> 30 31#include <stdio.h> 32#include <stdlib.h> 33#include <memory.h> --- 1065 unchanged lines hidden (view full) --- 1099 struct block *b; 1100 int do_stmts; 1101{ 1102 struct slist *s; 1103 struct edge *p; 1104 int i; 1105 bpf_int32 aval; 1106 |
1107#if 0 1108 for (s = b->stmts; s && s->next; s = s->next) 1109 if (BPF_CLASS(s->s.code) == BPF_JMP) { 1110 do_stmts = 0; 1111 break; 1112 } 1113#endif 1114 |
|
1107 /* 1108 * Initialize the atom values. 1109 * If we have no predecessors, everything is undefined. 1110 * Otherwise, we inherent our values from our predecessors. 1111 * If any register has an ambiguous value (i.e. control paths are 1112 * merging) give it the undefined value of 0. 1113 */ 1114 p = b->in_edges; --- 766 unchanged lines hidden (view full) --- 1881convert_code_r(p) 1882 struct block *p; 1883{ 1884 struct bpf_insn *dst; 1885 struct slist *src; 1886 int slen; 1887 u_int off; 1888 int extrajmps; /* number of extra jumps inserted */ | 1115 /* 1116 * Initialize the atom values. 1117 * If we have no predecessors, everything is undefined. 1118 * Otherwise, we inherent our values from our predecessors. 1119 * If any register has an ambiguous value (i.e. control paths are 1120 * merging) give it the undefined value of 0. 1121 */ 1122 p = b->in_edges; --- 766 unchanged lines hidden (view full) --- 1889convert_code_r(p) 1890 struct block *p; 1891{ 1892 struct bpf_insn *dst; 1893 struct slist *src; 1894 int slen; 1895 u_int off; 1896 int extrajmps; /* number of extra jumps inserted */ |
1897 struct slist **offset = NULL; |
|
1889 1890 if (p == 0 || isMarked(p)) 1891 return (1); 1892 Mark(p); 1893 1894 if (convert_code_r(JF(p)) == 0) 1895 return (0); 1896 if (convert_code_r(JT(p)) == 0) 1897 return (0); 1898 1899 slen = slength(p->stmts); 1900 dst = ftail -= (slen + 1 + p->longjt + p->longjf); 1901 /* inflate length by any extra jumps */ 1902 1903 p->offset = dst - fstart; 1904 | 1898 1899 if (p == 0 || isMarked(p)) 1900 return (1); 1901 Mark(p); 1902 1903 if (convert_code_r(JF(p)) == 0) 1904 return (0); 1905 if (convert_code_r(JT(p)) == 0) 1906 return (0); 1907 1908 slen = slength(p->stmts); 1909 dst = ftail -= (slen + 1 + p->longjt + p->longjf); 1910 /* inflate length by any extra jumps */ 1911 1912 p->offset = dst - fstart; 1913 |
1914 /* generate offset[] for convenience */ 1915 if (slen) { 1916 offset = (struct slist **)calloc(sizeof(struct slist *), slen); 1917 if (!offset) { 1918 bpf_error("not enough core"); 1919 /*NOTREACHED*/ 1920 } 1921 } 1922 src = p->stmts; 1923 for (off = 0; off < slen && src; off++) { 1924#if 0 1925 printf("off=%d src=%x\n", off, src); 1926#endif 1927 offset[off] = src; 1928 src = src->next; 1929 } 1930 1931 off = 0; |
|
1905 for (src = p->stmts; src; src = src->next) { 1906 if (src->s.code == NOP) 1907 continue; 1908 dst->code = (u_short)src->s.code; 1909 dst->k = src->s.k; | 1932 for (src = p->stmts; src; src = src->next) { 1933 if (src->s.code == NOP) 1934 continue; 1935 dst->code = (u_short)src->s.code; 1936 dst->k = src->s.k; |
1937 1938 /* fill block-local relative jump */ 1939 if (BPF_CLASS(src->s.code) != BPF_JMP || src->s.code == BPF_JMP|BPF_JA) { 1940#if 0 1941 if (src->s.jt || src->s.jf) { 1942 bpf_error("illegal jmp destination"); 1943 /*NOTREACHED*/ 1944 } 1945#endif 1946 goto filled; 1947 } 1948 if (off == slen - 2) /*???*/ 1949 goto filled; 1950 1951 { 1952 int i; 1953 int jt, jf; 1954 char *ljerr = "%s for block-local relative jump: off=%d"; 1955 1956#if 0 1957 printf("code=%x off=%d %x %x\n", src->s.code, 1958 off, src->s.jt, src->s.jf); 1959#endif 1960 1961 if (!src->s.jt || !src->s.jf) { 1962 bpf_error(ljerr, "no jmp destination", off); 1963 /*NOTREACHED*/ 1964 } 1965 1966 jt = jf = 0; 1967 for (i = 0; i < slen; i++) { 1968 if (offset[i] == src->s.jt) { 1969 if (jt) { 1970 bpf_error(ljerr, "multiple matches", off); 1971 /*NOTREACHED*/ 1972 } 1973 1974 dst->jt = i - off - 1; 1975 jt++; 1976 } 1977 if (offset[i] == src->s.jf) { 1978 if (jf) { 1979 bpf_error(ljerr, "multiple matches", off); 1980 /*NOTREACHED*/ 1981 } 1982 dst->jf = i - off - 1; 1983 jf++; 1984 } 1985 } 1986 if (!jt || !jf) { 1987 bpf_error(ljerr, "no destination found", off); 1988 /*NOTREACHED*/ 1989 } 1990 } 1991filled: |
|
1910 ++dst; | 1992 ++dst; |
1993 ++off; |
|
1911 } | 1994 } |
1995 if (offset) 1996 free(offset); 1997 |
|
1912#ifdef BDEBUG 1913 bids[dst - fstart] = p->id + 1; 1914#endif 1915 dst->code = (u_short)p->s.code; 1916 dst->k = p->s.k; 1917 if (JT(p)) { 1918 extrajmps = 0; 1919 off = JT(p)->offset - (p->offset + slen) - 1; --- 85 unchanged lines hidden --- | 1998#ifdef BDEBUG 1999 bids[dst - fstart] = p->id + 1; 2000#endif 2001 dst->code = (u_short)p->s.code; 2002 dst->k = p->s.k; 2003 if (JT(p)) { 2004 extrajmps = 0; 2005 off = JT(p)->offset - (p->offset + slen) - 1; --- 85 unchanged lines hidden --- |