gencode.h revision 56891
117683Spst/*
217683Spst * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that: (1) source code distributions
717683Spst * retain the above copyright notice and this paragraph in its entirety, (2)
817683Spst * distributions including binary code include the above copyright notice and
917683Spst * this paragraph in its entirety in the documentation or other materials
1017683Spst * provided with the distribution, and (3) all advertising materials mentioning
1117683Spst * features or use of this software display the following acknowledgement:
1217683Spst * ``This product includes software developed by the University of California,
1317683Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1417683Spst * the University nor the names of its contributors may be used to endorse
1517683Spst * or promote products derived from this software without specific prior
1617683Spst * written permission.
1717683Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1817683Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1917683Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2017683Spst *
2156891Sfenner * $FreeBSD: head/contrib/libpcap/gencode.h 56891 2000-01-30 00:43:38Z fenner $
2256891Sfenner * @(#) $Header: /tcpdump/master/libpcap/gencode.h,v 1.37 1999/10/19 15:18:29 itojun Exp $ (LBL)
2317683Spst */
2417683Spst
2517683Spst/*XXX*/
2617683Spst#include "gnuc.h"
2717683Spst
2817683Spst/* Address qualifiers. */
2917683Spst
3017683Spst#define Q_HOST		1
3117683Spst#define Q_NET		2
3217683Spst#define Q_PORT		3
3317683Spst#define Q_GATEWAY	4
3417683Spst#define Q_PROTO		5
3556891Sfenner#define Q_PROTOCHAIN	6
3617683Spst
3717683Spst/* Protocol qualifiers. */
3817683Spst
3917683Spst#define Q_LINK		1
4017683Spst#define Q_IP		2
4117683Spst#define Q_ARP		3
4217683Spst#define Q_RARP		4
4317683Spst#define Q_TCP		5
4417683Spst#define Q_UDP		6
4517683Spst#define Q_ICMP		7
4617683Spst#define Q_IGMP		8
4717683Spst#define Q_IGRP		9
4817683Spst
4917683Spst
5017683Spst#define	Q_ATALK		10
5117683Spst#define	Q_DECNET	11
5217683Spst#define	Q_LAT		12
5317683Spst#define Q_SCA		13
5417683Spst#define	Q_MOPRC		14
5517683Spst#define	Q_MOPDL		15
5617683Spst
5756891Sfenner#define Q_IPV6		16
5856891Sfenner#define Q_ICMPV6	17
5956891Sfenner#define Q_AH		18
6056891Sfenner#define Q_ESP		19
6156891Sfenner
6256891Sfenner#define Q_PIM		20
6356891Sfenner
6456891Sfenner#define Q_ISO		21
6556891Sfenner#define Q_ESIS		22
6656891Sfenner#define Q_ISIS		23
6756891Sfenner
6817683Spst/* Directional qualifiers. */
6917683Spst
7017683Spst#define Q_SRC		1
7117683Spst#define Q_DST		2
7217683Spst#define Q_OR		3
7317683Spst#define Q_AND		4
7417683Spst
7517683Spst#define Q_DEFAULT	0
7617683Spst#define Q_UNDEF		255
7717683Spst
7856891Sfennerstruct slist;
7956891Sfenner
8017683Spststruct stmt {
8117683Spst	int code;
8256891Sfenner	struct slist *jt;	/*only for relative jump in block*/
8356891Sfenner	struct slist *jf;	/*only for relative jump in block*/
8417683Spst	bpf_int32 k;
8517683Spst};
8617683Spst
8717683Spststruct slist {
8817683Spst	struct stmt s;
8917683Spst	struct slist *next;
9017683Spst};
9117683Spst
9217683Spst/*
9317683Spst * A bit vector to represent definition sets.  We assume TOT_REGISTERS
9417683Spst * is smaller than 8*sizeof(atomset).
9517683Spst */
9617683Spsttypedef bpf_u_int32 atomset;
9717683Spst#define ATOMMASK(n) (1 << (n))
9817683Spst#define ATOMELEM(d, n) (d & ATOMMASK(n))
9917683Spst
10017683Spst/*
10117683Spst * An unbounded set.
10217683Spst */
10317683Spsttypedef bpf_u_int32 *uset;
10417683Spst
10517683Spst/*
10617683Spst * Total number of atomic entities, including accumulator (A) and index (X).
10717683Spst * We treat all these guys similarly during flow analysis.
10817683Spst */
10917683Spst#define N_ATOMS (BPF_MEMWORDS+2)
11017683Spst
11117683Spststruct edge {
11217683Spst	int id;
11317683Spst	int code;
11417683Spst	uset edom;
11517683Spst	struct block *succ;
11617683Spst	struct block *pred;
11717683Spst	struct edge *next;	/* link list of incoming edges for a node */
11817683Spst};
11917683Spst
12017683Spststruct block {
12117683Spst	int id;
12217683Spst	struct slist *stmts;	/* side effect stmts */
12317683Spst	struct stmt s;		/* branch stmt */
12417683Spst	int mark;
12517683Spst	int longjt;		/* jt branch requires long jump */
12617683Spst	int longjf;		/* jf branch requires long jump */
12717683Spst	int level;
12817683Spst	int offset;
12917683Spst	int sense;
13017683Spst	struct edge et;
13117683Spst	struct edge ef;
13217683Spst	struct block *head;
13317683Spst	struct block *link;	/* link field used by optimizer */
13417683Spst	uset dom;
13517683Spst	uset closure;
13617683Spst	struct edge *in_edges;
13717683Spst	atomset def, kill;
13817683Spst	atomset in_use;
13917683Spst	atomset out_use;
14017683Spst	int oval;
14117683Spst	int val[N_ATOMS];
14217683Spst};
14317683Spst
14417683Spststruct arth {
14517683Spst	struct block *b;	/* protocol checks */
14617683Spst	struct slist *s;	/* stmt list */
14717683Spst	int regno;		/* virtual register number of result */
14817683Spst};
14917683Spst
15017683Spststruct qual {
15117683Spst	unsigned char addr;
15217683Spst	unsigned char proto;
15317683Spst	unsigned char dir;
15417683Spst	unsigned char pad;
15517683Spst};
15617683Spst
15717683Spststruct arth *gen_loadi(int);
15817683Spststruct arth *gen_load(int, struct arth *, int);
15917683Spststruct arth *gen_loadlen(void);
16017683Spststruct arth *gen_neg(struct arth *);
16117683Spststruct arth *gen_arth(int, struct arth *, struct arth *);
16217683Spst
16317683Spstvoid gen_and(struct block *, struct block *);
16417683Spstvoid gen_or(struct block *, struct block *);
16517683Spstvoid gen_not(struct block *);
16617683Spst
16717683Spststruct block *gen_scode(const char *, struct qual);
16817683Spststruct block *gen_ecode(const u_char *, struct qual);
16917683Spststruct block *gen_mcode(const char *, const char *, int, struct qual);
17056891Sfenner#ifdef INET6
17156891Sfennerstruct block *gen_mcode6(const char *, const char *, int, struct qual);
17256891Sfenner#endif
17317683Spststruct block *gen_ncode(const char *, bpf_u_int32, struct qual);
17417683Spststruct block *gen_proto_abbrev(int);
17517683Spststruct block *gen_relation(int, struct arth *, struct arth *, int);
17617683Spststruct block *gen_less(int);
17717683Spststruct block *gen_greater(int);
17817683Spststruct block *gen_byteop(int, int, int);
17917683Spststruct block *gen_broadcast(int);
18017683Spststruct block *gen_multicast(int);
18117683Spststruct block *gen_inbound(int);
18217683Spst
18317683Spstvoid bpf_optimize(struct block **);
18417683Spst#if __STDC__
18517683Spst__dead void bpf_error(const char *, ...)
18617683Spst    __attribute__((volatile, format (printf, 1, 2)));
18717683Spst#endif
18817683Spst
18917683Spstvoid finish_parse(struct block *);
19017683Spstchar *sdup(const char *);
19117683Spst
19217683Spststruct bpf_insn *icode_to_fcode(struct block *, int *);
19317683Spstint pcap_parse(void);
19417683Spstvoid lex_init(char *);
19517683Spstvoid sappend(struct slist *, struct slist *);
19617683Spst
19717683Spst/* XXX */
19817683Spst#define JT(b)  ((b)->et.succ)
19917683Spst#define JF(b)  ((b)->ef.succ)
20056891Sfenner
20156891Sfennerextern int no_optimize;
202