File: ControlFlow.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (246 lines) | stat: -rw-r--r-- 6,866 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "stdafx.h"
#include "ControlFlow.h"
#include "Code/Listing.h"
#include "Code/Arena.h"
#include "Gc/CodeTable.h"
#include "Gc/DwarfTable.h"
#include "Type.h"
#include "BuiltInSource.h"

namespace storm {

	static void flattenHelper(const ControlFlowItem &current, Array<ControlFlowItem> *out) {
		if (current.isCall()) {
			out->push(current);
		} else if (current.isLoop()) {
			Array<ControlFlowItem> *body = current.loop();

			out->push(ControlFlowItem(current.offset(), current.endOffset(), body));

			for (Nat i = 0; i < body->count(); i++) {
				flattenHelper(body->at(i), out);
			}
		}
	}

	Array<ControlFlowItem> *ControlFlowItem::flatten() const {
		Array<ControlFlowItem> *out = new (data) Array<ControlFlowItem>();
		flattenHelper(*this, out);
		return out;
	}

	Array<ControlFlowItem> *flatten(Array<ControlFlowItem> *items) {
		Array<ControlFlowItem> *out = new (items) Array<ControlFlowItem>();
		for (Nat i = 0; i < items->count(); i++)
			flattenHelper(items->at(i), out);
		return out;
	}

	void ControlFlowItem::toS(StrBuf *to) const {
		*to << S("@") << offset() << S(" ");

		if (hasFunction())
			*to << function()->identifier();
		else if (hasBuiltIn())
			*to << builtIn()->title();
		else if (isLoop())
			*to << S("-> ") << endOffset() << S(" ") << loop();

		statusSuffix(to, status());
	}

	void ControlFlowItem::statusSuffix(StrBuf *to, Status status) {
		switch (status) {
		case none:
			break;
		case removed:
			*to << S(" (removed)");
			break;
		}
	}

	void ControlFlowItem::replaceOffsets(Map<Nat, Nat> *replace) {
		if (replace->has(offset())) {
			offset(replace->get(offset()));
		}

		if (isLoop()) {
			Array<ControlFlowItem> *item = loop();
			for (Nat i = 0; i < item->count(); i++)
				item->at(i).replaceOffsets(replace);
		}
	}

	void replaceOffsets(Array<ControlFlowItem> *items, Map<Nat, Nat> *replace) {
		for (Nat i = 0; i < items->count(); i++)
			items->at(i).replaceOffsets(replace);
	}

	static ControlFlowItem filterFunctions(Nat offset, Function *f) {
		if (!f)
			return ControlFlowItem();

		// Don't include destructors - they are almost always not present explcitly in the source listings.
		if (*f->name == Type::DTOR)
			return ControlFlowItem();
		return ControlFlowItem(offset, f);
	}

	// Find a function object from a RefSource:
	static ControlFlowItem findFunction(Nat offset, MAYBE(code::RefSource *) source) {
		if (NamedSource *n = as<NamedSource>(source)) {
			return filterFunctions(offset, as<Function>(n->named()));
		} else if (BuiltInSource *b = as<BuiltInSource>(source)) {
			return ControlFlowItem(offset, b);
		} else {
			return ControlFlowItem();
		}
	}

	// Find a function object from a pointer to the code.
	static ControlFlowItem findFunctionFromCode(Nat offset, void *code, Nat refId) {
		code::Binary *binary = code::codeBinary(code);
		if (code::Reference *ref = binary->findReferenceBySlot(refId)) {
			return findFunction(offset, ref->source());
		} else {
			return ControlFlowItem();
		}
	}

	// Find a function object from a Ref. Should correspond roughly to 'findFunctionFromCode'.
	static ControlFlowItem findFunctionFromRef(Nat offset, code::Ref ref) {
		return findFunction(offset, ref.source());
	}


	class CodeRefSort {
	public:
		GcCodeRef *src;

		CodeRefSort(GcCodeRef *src) : src(src) {}

		bool operator() (size_t a, size_t b) const {
			return src[a].offset < src[b].offset;
		}
	};

	Array<ControlFlowItem> *controlFlowList(code::Binary *code) {
		return controlFlowListRaw((void *)code->address());
	}

	// Generate a control flow list from a pre-compiled function:
	Array<ControlFlowItem> *controlFlowListRaw(void *code) {
		code::Binary *b = code::codeBinary(code);
		Engine &e = b->engine();

		GcCode *refs = runtime::codeRefs(code);

		// First, sort the refs. They are not necessarily in increasing order.
		vector<size_t> order(refs->refCount, 0);
		for (size_t i = 0; i < order.size(); i++)
			order[i] = i;

		std::sort(order.begin(), order.end(), CodeRefSort(refs->refs));

		// Then, traverse them and create our result:
		Array<ControlFlowItem> *result = new (e) Array<ControlFlowItem>();

		for (size_t i = 0; i < refs->refCount; i++) {
			GcCodeRef &ref = refs->refs[i];

			if (ref.kind == GcCodeRef::jump) {
				// Create a function item if we can find the underlying function.
				ControlFlowItem call = findFunctionFromCode(Nat(ref.offset), code, Nat(i));
				if (call.isCall())
					result->push(call);
			} else if (ref.kind == GcCodeRef::backEdge) {
				// Look back in 'result' to find things to include in the loop.
				size_t target = size_t(ref.pointer);

				Nat includeUntil = result->count();
				while (includeUntil > 0) {
					if (result->at(includeUntil - 1).offset() <= target)
						break;

					includeUntil--;
				}

				Array<ControlFlowItem> *sub = new (e) Array<ControlFlowItem>();
				sub->reserve(result->count() - includeUntil);
				while (result->count() > includeUntil) {
					sub->push(result->last());
					result->pop();
				}
				sub->reverse();

				result->push(ControlFlowItem(Nat(target), Nat(ref.offset), sub));
			}
		}

		return result;
	}

	// Generate a control flow list from a Listing:
	Array<ControlFlowItem> *controlFlowList(code::Listing *code) {
		using namespace code;

		// Just go through the listing, keep track of labels we have found in order to find back-edges.
		Array<Nat> *labelTarget = new (code) Array<Nat>(code->labelCount(), code->count());

		Array<ControlFlowItem> *result = new (code) Array<ControlFlowItem>();

		for (Nat i = 0; i < code->count(); i++) {
			Instr *instr = code->at(i);

			// Mark labels:
			if (Array<Label> *labels = code->labels(i)) {
				for (Nat j = 0; j < labels->count(); j++) {
					labelTarget->at(labels->at(j).key()) = i;
				}
			}

			// Look at the instruction. We are only interested in 'fnCall' and 'jmp':
			switch (instr->op()) {
			case op::fnCall:
			case op::fnCallRef:
				if (instr->src().type() == opReference) {
					ControlFlowItem call = findFunctionFromRef(i, instr->src().ref());
					if (call.isCall())
						result->push(call);
				}
				break;
			case op::jmp:
			case op::jmpBlock:
				if (instr->dest().type() == opLabel) {
					Nat target = labelTarget->at(instr->dest().label().key());
					// Not a back-edge?
					if (target >= i)
						break;

					// Copy elements, and replace:
					Nat includeUntil = result->count();
					while (includeUntil > 0) {
						if (result->at(includeUntil - 1).offset() <= target)
							break;

						includeUntil--;
					}

					Array<ControlFlowItem> *sub = new (code) Array<ControlFlowItem>();
					sub->reserve(result->count() - includeUntil);
					while (result->count() > includeUntil) {
						sub->push(result->last());
						result->pop();
					}
					sub->reverse();

					result->push(ControlFlowItem(target, i, sub));
				}
				break;
			}
		}

		return result;
	}

}