gencode.h revision 17749
1300906Sasomers/*
2300906Sasomers * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3300906Sasomers *	The Regents of the University of California.  All rights reserved.
4300906Sasomers *
5300906Sasomers * Redistribution and use in source and binary forms, with or without
6300906Sasomers * modification, are permitted provided that: (1) source code distributions
7300906Sasomers * retain the above copyright notice and this paragraph in its entirety, (2)
8300906Sasomers * distributions including binary code include the above copyright notice and
9300906Sasomers * this paragraph in its entirety in the documentation or other materials
10300906Sasomers * provided with the distribution, and (3) all advertising materials mentioning
11300906Sasomers * features or use of this software display the following acknowledgement:
12300906Sasomers * ``This product includes software developed by the University of California,
13300906Sasomers * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14300906Sasomers * the University nor the names of its contributors may be used to endorse
15300906Sasomers * or promote products derived from this software without specific prior
16300906Sasomers * written permission.
17300906Sasomers * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18300906Sasomers * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19300906Sasomers * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20300906Sasomers *
21300906Sasomers * @(#) $Header: gencode.h,v 1.36 96/07/17 00:11:34 leres Exp $ (LBL)
22300906Sasomers */
23300906Sasomers
24300906Sasomers/*XXX*/
25300906Sasomers#include "gnuc.h"
26300906Sasomers
27300906Sasomers/* Address qualifiers. */
28300906Sasomers
29300906Sasomers#define Q_HOST		1
30300906Sasomers#define Q_NET		2
31300906Sasomers#define Q_PORT		3
32300906Sasomers#define Q_GATEWAY	4
33300906Sasomers#define Q_PROTO		5
34300906Sasomers
35300906Sasomers/* Protocol qualifiers. */
36300906Sasomers
37300906Sasomers#define Q_LINK		1
38300906Sasomers#define Q_IP		2
39300906Sasomers#define Q_ARP		3
40300906Sasomers#define Q_RARP		4
41300906Sasomers#define Q_TCP		5
42300906Sasomers#define Q_UDP		6
43300906Sasomers#define Q_ICMP		7
44300906Sasomers#define Q_IGMP		8
45300906Sasomers#define Q_IGRP		9
46300906Sasomers
47300906Sasomers
48300906Sasomers#define	Q_ATALK		10
49300906Sasomers#define	Q_DECNET	11
50300906Sasomers#define	Q_LAT		12
51300906Sasomers#define Q_SCA		13
52300906Sasomers#define	Q_MOPRC		14
53300906Sasomers#define	Q_MOPDL		15
54300906Sasomers#define Q_ISO		16
55300906Sasomers#define Q_ESIS		17
56300906Sasomers#define Q_ISIS		18
57300906Sasomers
58300906Sasomers/* Directional qualifiers. */
59300906Sasomers
60300906Sasomers#define Q_SRC		1
61300906Sasomers#define Q_DST		2
62300906Sasomers#define Q_OR		3
63300906Sasomers#define Q_AND		4
64300906Sasomers
65300906Sasomers#define Q_DEFAULT	0
66300906Sasomers#define Q_UNDEF		255
67300906Sasomers
68300906Sasomersstruct stmt {
69300906Sasomers	int code;
70300906Sasomers	bpf_int32 k;
71300906Sasomers};
72300906Sasomers
73300906Sasomersstruct slist {
74300906Sasomers	struct stmt s;
75300906Sasomers	struct slist *next;
76300906Sasomers};
77300906Sasomers
78300906Sasomers/*
79300906Sasomers * A bit vector to represent definition sets.  We assume TOT_REGISTERS
80300906Sasomers * is smaller than 8*sizeof(atomset).
81300906Sasomers */
82300906Sasomerstypedef bpf_u_int32 atomset;
83300906Sasomers#define ATOMMASK(n) (1 << (n))
84300906Sasomers#define ATOMELEM(d, n) (d & ATOMMASK(n))
85300906Sasomers
86300906Sasomers/*
87300906Sasomers * An unbounded set.
88300906Sasomers */
89300906Sasomerstypedef bpf_u_int32 *uset;
90300906Sasomers
91300906Sasomers/*
92300906Sasomers * Total number of atomic entities, including accumulator (A) and index (X).
93300906Sasomers * We treat all these guys similarly during flow analysis.
94300906Sasomers */
95300906Sasomers#define N_ATOMS (BPF_MEMWORDS+2)
96300906Sasomers
97300906Sasomersstruct edge {
98300906Sasomers	int id;
99300906Sasomers	int code;
100300906Sasomers	uset edom;
101300906Sasomers	struct block *succ;
102300906Sasomers	struct block *pred;
103300906Sasomers	struct edge *next;	/* link list of incoming edges for a node */
104300906Sasomers};
105300906Sasomers
106300906Sasomersstruct block {
107300906Sasomers	int id;
108300906Sasomers	struct slist *stmts;	/* side effect stmts */
109300906Sasomers	struct stmt s;		/* branch stmt */
110300906Sasomers	int mark;
111300906Sasomers	int longjt;		/* jt branch requires long jump */
112300906Sasomers	int longjf;		/* jf branch requires long jump */
113300906Sasomers	int level;
114300906Sasomers	int offset;
115300906Sasomers	int sense;
116300906Sasomers	struct edge et;
117300906Sasomers	struct edge ef;
118300906Sasomers	struct block *head;
119300906Sasomers	struct block *link;	/* link field used by optimizer */
120300906Sasomers	uset dom;
121300906Sasomers	uset closure;
122300906Sasomers	struct edge *in_edges;
123300906Sasomers	atomset def, kill;
124300906Sasomers	atomset in_use;
125300906Sasomers	atomset out_use;
126300906Sasomers	int oval;
127300906Sasomers	int val[N_ATOMS];
128300906Sasomers};
129300906Sasomers
130300906Sasomersstruct arth {
131300906Sasomers	struct block *b;	/* protocol checks */
132300906Sasomers	struct slist *s;	/* stmt list */
133300906Sasomers	int regno;		/* virtual register number of result */
134300906Sasomers};
135300906Sasomers
136300906Sasomersstruct qual {
137300906Sasomers	unsigned char addr;
138300906Sasomers	unsigned char proto;
139300906Sasomers	unsigned char dir;
140300906Sasomers	unsigned char pad;
141300906Sasomers};
142300906Sasomers
143300906Sasomersstruct arth *gen_loadi(int);
144300906Sasomersstruct arth *gen_load(int, struct arth *, int);
145300906Sasomersstruct arth *gen_loadlen(void);
146300906Sasomersstruct arth *gen_neg(struct arth *);
147300906Sasomersstruct arth *gen_arth(int, struct arth *, struct arth *);
148300906Sasomers
149300906Sasomersvoid gen_and(struct block *, struct block *);
150300906Sasomersvoid gen_or(struct block *, struct block *);
151300906Sasomersvoid gen_not(struct block *);
152300906Sasomers
153300906Sasomersstruct block *gen_scode(const char *, struct qual);
154300906Sasomersstruct block *gen_ecode(const u_char *, struct qual);
155300906Sasomersstruct block *gen_mcode(const char *, const char *, int, struct qual);
156300906Sasomersstruct block *gen_ncode(const char *, bpf_u_int32, struct qual);
157300906Sasomersstruct block *gen_proto_abbrev(int);
158300906Sasomersstruct block *gen_relation(int, struct arth *, struct arth *, int);
159300906Sasomersstruct block *gen_less(int);
160300906Sasomersstruct block *gen_greater(int);
161300906Sasomersstruct block *gen_byteop(int, int, int);
162300906Sasomersstruct block *gen_broadcast(int);
163300906Sasomersstruct block *gen_multicast(int);
164300906Sasomersstruct block *gen_inbound(int);
165300906Sasomers
166300906Sasomersvoid bpf_optimize(struct block **);
167300906Sasomers#if __STDC__
168300906Sasomers__dead void bpf_error(const char *, ...)
169300906Sasomers    __attribute__((volatile, format (printf, 1, 2)));
170300906Sasomers#endif
171300906Sasomers
172300906Sasomersvoid finish_parse(struct block *);
173300906Sasomerschar *sdup(const char *);
174300906Sasomers
175300906Sasomersstruct bpf_insn *icode_to_fcode(struct block *, int *);
176300906Sasomersint pcap_parse(void);
177300906Sasomersvoid lex_init(char *);
178300906Sasomersvoid sappend(struct slist *, struct slist *);
179300906Sasomers
180300906Sasomers/* XXX */
181300906Sasomers#define JT(b)  ((b)->et.succ)
182300906Sasomers#define JF(b)  ((b)->ef.succ)
183300906Sasomers