gencode.h revision 17683
1/*
2 * Copyright (c) 1990, 1991, 1992, 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
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
16 * written permission.
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 * @(#) $Header: gencode.h,v 1.36 96/07/17 00:11:34 leres Exp $ (LBL)
22 */
23
24/*XXX*/
25#include "gnuc.h"
26
27/* Address qualifiers. */
28
29#define Q_HOST		1
30#define Q_NET		2
31#define Q_PORT		3
32#define Q_GATEWAY	4
33#define Q_PROTO		5
34
35/* Protocol qualifiers. */
36
37#define Q_LINK		1
38#define Q_IP		2
39#define Q_ARP		3
40#define Q_RARP		4
41#define Q_TCP		5
42#define Q_UDP		6
43#define Q_ICMP		7
44#define Q_IGMP		8
45#define Q_IGRP		9
46
47
48#define	Q_ATALK		10
49#define	Q_DECNET	11
50#define	Q_LAT		12
51#define Q_SCA		13
52#define	Q_MOPRC		14
53#define	Q_MOPDL		15
54
55/* Directional qualifiers. */
56
57#define Q_SRC		1
58#define Q_DST		2
59#define Q_OR		3
60#define Q_AND		4
61
62#define Q_DEFAULT	0
63#define Q_UNDEF		255
64
65struct stmt {
66	int code;
67	bpf_int32 k;
68};
69
70struct slist {
71	struct stmt s;
72	struct slist *next;
73};
74
75/*
76 * A bit vector to represent definition sets.  We assume TOT_REGISTERS
77 * is smaller than 8*sizeof(atomset).
78 */
79typedef bpf_u_int32 atomset;
80#define ATOMMASK(n) (1 << (n))
81#define ATOMELEM(d, n) (d & ATOMMASK(n))
82
83/*
84 * An unbounded set.
85 */
86typedef bpf_u_int32 *uset;
87
88/*
89 * Total number of atomic entities, including accumulator (A) and index (X).
90 * We treat all these guys similarly during flow analysis.
91 */
92#define N_ATOMS (BPF_MEMWORDS+2)
93
94struct edge {
95	int id;
96	int code;
97	uset edom;
98	struct block *succ;
99	struct block *pred;
100	struct edge *next;	/* link list of incoming edges for a node */
101};
102
103struct block {
104	int id;
105	struct slist *stmts;	/* side effect stmts */
106	struct stmt s;		/* branch stmt */
107	int mark;
108	int longjt;		/* jt branch requires long jump */
109	int longjf;		/* jf branch requires long jump */
110	int level;
111	int offset;
112	int sense;
113	struct edge et;
114	struct edge ef;
115	struct block *head;
116	struct block *link;	/* link field used by optimizer */
117	uset dom;
118	uset closure;
119	struct edge *in_edges;
120	atomset def, kill;
121	atomset in_use;
122	atomset out_use;
123	int oval;
124	int val[N_ATOMS];
125};
126
127struct arth {
128	struct block *b;	/* protocol checks */
129	struct slist *s;	/* stmt list */
130	int regno;		/* virtual register number of result */
131};
132
133struct qual {
134	unsigned char addr;
135	unsigned char proto;
136	unsigned char dir;
137	unsigned char pad;
138};
139
140struct arth *gen_loadi(int);
141struct arth *gen_load(int, struct arth *, int);
142struct arth *gen_loadlen(void);
143struct arth *gen_neg(struct arth *);
144struct arth *gen_arth(int, struct arth *, struct arth *);
145
146void gen_and(struct block *, struct block *);
147void gen_or(struct block *, struct block *);
148void gen_not(struct block *);
149
150struct block *gen_scode(const char *, struct qual);
151struct block *gen_ecode(const u_char *, struct qual);
152struct block *gen_mcode(const char *, const char *, int, struct qual);
153struct block *gen_ncode(const char *, bpf_u_int32, struct qual);
154struct block *gen_proto_abbrev(int);
155struct block *gen_relation(int, struct arth *, struct arth *, int);
156struct block *gen_less(int);
157struct block *gen_greater(int);
158struct block *gen_byteop(int, int, int);
159struct block *gen_broadcast(int);
160struct block *gen_multicast(int);
161struct block *gen_inbound(int);
162
163void bpf_optimize(struct block **);
164#if __STDC__
165__dead void bpf_error(const char *, ...)
166    __attribute__((volatile, format (printf, 1, 2)));
167#endif
168
169void finish_parse(struct block *);
170char *sdup(const char *);
171
172struct bpf_insn *icode_to_fcode(struct block *, int *);
173int pcap_parse(void);
174void lex_init(char *);
175void sappend(struct slist *, struct slist *);
176
177/* XXX */
178#define JT(b)  ((b)->et.succ)
179#define JF(b)  ((b)->ef.succ)
180