File: fragment.c

package info (click to toggle)
lsfcc 0.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 324 kB
  • ctags: 217
  • sloc: ansic: 1,551; yacc: 408; sh: 337; lex: 85; makefile: 55
file content (133 lines) | stat: -rw-r--r-- 3,270 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdlib.h>
#include <string.h>

#include "fragment.h"

struct fragment *frag_new() {
	struct fragment *f;

	f = malloc(sizeof(*f));
	memset(f, 0, sizeof(*f));
	return f;
}

void frag_free(struct fragment *f) {
	if(f->true)
		free(f->true);
	if(f->false)
		free(f->false);
	if(f->insns)
		free(f->insns);
	if(f->ftrue)
		free(f->ftrue);
	if(f->tfalse)
		free(f->tfalse);
	free(f);
}

void frag_fixupt(struct fragment *f, int dest) {
	int i, insn;

	if(f->numtrue) {
		for(i = 0; i < f->numtrue; i++) {
			insn = f->true[i];
			f->insns[insn].jt = f->numinsns + dest - insn - 1;
		}
		free(f->true);
		f->true = NULL;
		f->numtrue = 0;
	}
	if(f->numftrue) {
		for(i = 0; i < f->numftrue; i++) {
			insn = f->ftrue[i];
			f->insns[insn].jf = f->numinsns + dest - insn - 1;
		}
		free(f->ftrue);
		f->ftrue = NULL;
		f->numftrue = 0;
	}
}

void frag_fixupf(struct fragment *f, int dest) {
	int i, insn;

	if(f->numfalse) {
		for(i = 0; i < f->numfalse; i++) {
			insn = f->false[i];
			f->insns[insn].jf = f->numinsns + dest - insn - 1;
		}
		free(f->false);
		f->false = NULL;
		f->numfalse = 0;
	}
	if(f->numtfalse) {
		for(i = 0; i < f->numtfalse; i++) {
			insn = f->tfalse[i];
			f->insns[insn].jt = f->numinsns + dest - insn - 1;
		}
		free(f->tfalse);
		f->tfalse = NULL;
		f->numtfalse = 0;
	}
}

void frag_append(struct fragment *f, struct fragment *g) {
	int newlen, newt, newf, newft, newtf, i;

	if(!f || !g)
		return;
	newlen = f->numinsns + g->numinsns;
	newt = f->numtrue + g->numtrue;
	newf = f->numfalse + g->numfalse;
	newft = f->numftrue + g->numftrue;
	newtf = f->numtfalse + g->numtfalse;
	f->insns = realloc(f->insns, newlen * sizeof(*f->insns));
	for(i = 0; i < g->numinsns; i++)
		f->insns[i + f->numinsns] = g->insns[i];
	if(newt)
		f->true = realloc(f->true, newt * sizeof(*f->true));
	for(i = 0; i < g->numtrue; i++)
		f->true[i + f->numtrue] = g->true[i] + f->numinsns;
	if(newft)
		f->ftrue = realloc(f->ftrue, newft * sizeof(*f->ftrue));
	for(i = 0; i < g->numftrue; i++)
		f->ftrue[i + f->numftrue] = g->ftrue[i] + f->numinsns;
	if(newf)
		f->false = realloc(f->false, newf * sizeof(*f->false));
	for(i = 0; i < g->numfalse; i++)
		f->false[i + f->numfalse] = g->false[i] + f->numinsns;
	if(newtf)
		f->tfalse = realloc(f->tfalse, newtf * sizeof(*f->tfalse));
	for(i = 0; i < g->numtfalse; i++)
		f->tfalse[i + f->numtfalse] = g->tfalse[i] + f->numinsns;
	f->numinsns = newlen;
	f->numtrue = newt;
	f->numfalse = newf;
	f->numftrue = newft;
	f->numtfalse = newtf;
}

void frag_add(struct fragment *f, struct bpf_insn *i) {
	f->insns = realloc(f->insns, ++f->numinsns * sizeof(*f->insns));
	f->insns[f->numinsns - 1] = *i;
}

void frag_sett(struct fragment *f) {
	f->true = realloc(f->true, ++f->numtrue * sizeof(*f->true));
	f->true[f->numtrue - 1] = f->numinsns - 1;
}

void frag_setf(struct fragment *f) {
	f->false = realloc(f->false, ++f->numfalse * sizeof(*f->false));
	f->false[f->numfalse - 1] = f->numinsns - 1;
}

void frag_setft(struct fragment *f) {
	f->ftrue = realloc(f->ftrue, ++f->numftrue * sizeof(*f->ftrue));
	f->ftrue[f->numftrue - 1] = f->numinsns - 1;
}

void frag_settf(struct fragment *f) {
	f->tfalse = realloc(f->tfalse, ++f->numtfalse * sizeof(*f->tfalse));
	f->tfalse[f->numtfalse - 1] = f->numinsns - 1;
}