File: conditionals.bs

package info (click to toggle)
storm-lang 0.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,836 kB
  • sloc: ansic: 261,420; cpp: 138,870; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (284 lines) | stat: -rw-r--r-- 6,071 bytes parent folder | download | duplicates (3)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use core:lang;
use core:asm;
use lang:bs:macro;

// Do type checking and -casting for a conditional.
Expr castCond(Block parent, Expr cond) on Compiler {
	Value type = cond.result.type;

	// TODO: References!

	if (isCppPtr(type)) {
		return CheckPtr(cond);
	}

	if (type.isAsmType) {
		return CheckIntegral(cond);
	}

	if (type.type is named{runtime:tid_t}) {
		return CheckIntegral(cond);
	}

	// TODO: We don't need to be this restrictive.
	if (type.type !is named{Bool})
		throw SyntaxError(cond.pos, "A condition must evaluate to a boolean, not ${type}.");

	cond;
}


/**
 * Check if a pointer is null.
 */
class CheckPtr extends Expr {
	init(Expr e) {
		init(e.pos) { e = e; }
	}

	Expr e;

	ExprResult result() : override {
		Value(named{Bool});
	}

	void code(CodeGen gen, CodeResult res) : override {
		CodeResult inner(e.result.type.asRef(false), gen.block);
		e.code(gen, inner);

		Var ptr = inner.location(gen);
		gen.l << cmp(ptrRel(ptr), ptrConst(0));
		gen.l << setCond(res.location(gen), CondFlag:ifNotEqual);
	}

	void codePtr(CodeGen gen, Type type) : override {
		CodeResult inner(e.result.type.asRef(false), gen.block);
		e.code(gen, inner);

		CppVar var = allocTemporary(gen, named{Bool});
		var.adjust(gen, ptrA);
		// We only need to check the pointer-part of the pointer.
		gen.l << cmp(ptrRel(inner.location(gen)), ptrConst(0));
		gen.l << setCond(byteRel(ptrA), CondFlag:ifNotEqual);
		var.created(gen);
		gen.l << location(SrcPos());
		var.ptr(gen);
	}
}


/**
 * Check if an integer value is nonzero.
 */
class CheckIntegral extends Expr {
	init(Expr e) {
		init(e.pos) { e = e; }
	}

	Expr e;

	ExprResult result() : override {
		Value(named{Bool});
	}

	void code(CodeGen gen, CodeResult res) : override {
		CodeResult inner(e.result.type.asRef(false), gen.block);
		e.code(gen, inner);

		Var val = inner.location(gen);
		gen.l << cmp(val, xConst(val.size, 0));
		gen.l << setCond(res.location(gen), CondFlag:ifNotEqual);
	}

	void codePtr(CodeGen gen, Type type) : override {
		CodeResult inner(e.result.type.asRef(false), gen.block);
		e.code(gen, inner);
		Var val = inner.location(gen);

		CppVar var = allocTemporary(gen, named{Bool});
		var.adjust(gen, ptrA);
		gen.l << cmp(val, xConst(val.size, 0));
		gen.l << setCond(byteRel(ptrA), CondFlag:ifNotEqual);
		var.created(gen);
		gen.l << location(SrcPos());
		var.ptr(gen);
	}
}


/**
 * An if-statement.
 */
class IfStmt extends Block {
	init(SrcPos pos, Block parent) {
		init(pos, parent) {}
	}

	// Condition.
	Expr? cond;

	// True branch.
	Stmt? ifBranch;

	// False branch.
	Stmt? elseBranch;

	// Setters for the grammar.
	void cond(Expr e) { cond = castCond(this, e); }
	void ifTrue(Stmt s) { ifBranch = s; }
	void ifFalse(Stmt s) { elseBranch = s; }

	Bool blockCode(CodeGen gen) : override {
		unless (cond)
			throw SyntaxError(pos, "This if-statement has no condition set.");

		var onFalse = gen.l.label();
		var exit = gen.l.label();

		CodeResult condResult(Value(named{Bool}), gen.block);
		CodeGen sub = gen.child();
		if (step)
			gen.l << location(cond.pos);
		gen.l << begin(sub.block);
		cond.code(sub, condResult);
		gen.l << end(sub.block);

		gen.l << cmp(condResult.location(gen), byteConst(0));
		gen.l << jmp(onFalse, CondFlag:ifEqual);

		Bool returns = false;
		if (ifBranch)
			returns |= executeStmt(gen, ifBranch);
		else
			returns |= true;

		gen.l << jmp(exit);
		gen.l << onFalse;

		if (elseBranch)
			returns |= executeStmt(gen, elseBranch);
		else
			returns |= true;


		gen.l << exit;

		returns;
	}
}

/**
 * Logical and/or (short circuiting).
 */
class LogicOp extends ExprRef {
	// If "and" is true, we're an and operation, otherwise we're an or operation.
	init(SrcPos pos, Block parent, Expr lhs, Expr rhs, Bool and) {
		init(pos) {
			lhs = castCond(parent, lhs);
			rhs = castCond(parent, rhs);
			and = and;
			step = parent.step;
		}
	}

	private Expr lhs;
	private Expr rhs;
	private Bool and;
	private Bool step;

	ExprResult result() : override {
		Value(named{Bool}).asRef(true);
	}

	void code(CodeGen gen, CodeResult res) : override {
		if (res.type.ref) {
			CppVar var = allocType(gen, named{Bool});
			codeRef(gen, var);

			var.adjust(gen, res.safeLocation(gen, Value(named{Bool}).asRef()));
		} else {
			code(gen, res.safeLocation(gen, Value(named{Bool})));
		}
		res.created(gen);
	}

	void codeRef(CodeGen gen, CppVar var) : override {
		code(gen, al);
		var.adjust(gen, ptrB);
		gen.l << mov(byteRel(ptrB), al);
		var.created(gen);
	}

	private void code(CodeGen gen, Operand result) {
		Value bool(named{Bool});
		CodeResult lResult(bool, gen.block);
		if (step)
			gen.l << location(lhs.pos);
		lhs.code(gen, lResult);

		Label skip = gen.l.label();

		gen.l << mov(result, lResult.location(gen));
		gen.l << cmp(result, byteConst(0));

		if (and)
			gen.l << jmp(skip, CondFlag:ifEqual);
		else
			gen.l << jmp(skip, CondFlag:ifNotEqual);

		CodeResult rResult(bool, gen.block);
		if (step)
			gen.l << location(rhs.pos);
		rhs.code(gen, rResult);

		gen.l << mov(result, rResult.location(gen));

		gen.l << skip;
	}
}

/**
 * Negation operator (!)
 */
class NegateOp extends ExprRef {
	init(SrcPos pos, Block parent, Expr expr) {
		init(pos) {
			expr = castCond(parent, expr);
		}
	}

	private Expr expr;

	ExprResult result() : override {
		Value(named{Bool}).asRef(true);
	}

	void code(CodeGen gen, CodeResult res) : override {
		if (res.type.ref) {
			CppVar var = allocType(gen, named{Bool});
			codeRef(gen, var);

			var.adjust(gen, res.safeLocation(gen, Value(named{Bool}).asRef()));
		} else {
			code(gen, res.safeLocation(gen, Value(named{Bool})));
		}
		res.created(gen);
	}

	void codeRef(CodeGen gen, CppVar var) : override {
		code(gen, al);
		var.adjust(gen, ptrB);
		gen.l << mov(byteRel(ptrB), al);
		var.created(gen);
	}

	private void code(CodeGen gen, Operand result) {
		Value bool(named{Bool});
		CodeResult res(bool, gen.block);
		expr.code(gen, res);

		gen.l << mov(result, res.location(gen));
		gen.l << cmp(result, byteConst(0));
		gen.l << setCond(result, CondFlag:ifEqual);
	}
}