aboutsummaryrefslogtreecommitdiff
path: root/stmt.c
blob: 075feffabca05f9849eb22896b1a950923680a58 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "cc.h"

static bool
gotolabel(struct func *f)
{
	char *name;
	struct gotolabel *g;

	if (tok.kind != TIDENT)
		return false;
	name = tok.lit;
	if (!peek(TCOLON))
		return false;
	g = funcgoto(f, name);
	g->defined = true;
	funclabel(f, g->label);
	return true;
}

/* controlling expression of loops and if-statements */
static struct expr *
ctrlexpr(struct scope *s)
{
	struct expr *e;
	struct type *t;

	e = expr(s);
	t = e->type;
	if (!(t->prop & PROPSCALAR))
		error(&tok.loc, "controlling expression must have scalar type");
	/*
	Ideally we would just do this conversion unconditionally,
	but QBE is not currently able to optimize the conversion
	away for int.
	*/
	if (t->prop & PROPFLOAT || t->size > 4)
		e = exprconvert(e, &typebool);
	return e;
}

/* 6.8 Statements and blocks */
void
stmt(struct func *f, struct scope *s)
{
	char *name;
	struct expr *e;
	struct type *t;
	struct value *v;
	struct block *label[4];
	struct switchcases swtch = {0};
	uint64_t i;

	while (gotolabel(f))
		;
	switch (tok.kind) {
	/* 6.8.1 Labeled statements */
	case TCASE:
		next();
		if (!s->switchcases)
			error(&tok.loc, "'case' label must be in switch");
		label[0] = mkblock("switch_case");
		funclabel(f, label[0]);
		i = intconstexpr(s, true);
		switchcase(s->switchcases, i, label[0]);
		expect(TCOLON, "after case expression");
		stmt(f, s);
		break;
	case TDEFAULT:
		next();
		if (!s->switchcases)
			error(&tok.loc, "'default' label must be in switch");
		if (s->switchcases->defaultlabel)
			error(&tok.loc, "multiple 'default' labels");
		expect(TCOLON, "after 'default'");
		s->switchcases->defaultlabel = mkblock("switch_default");
		funclabel(f, s->switchcases->defaultlabel);
		stmt(f, s);
		break;

	/* 6.8.2 Compound statement */
	case TLBRACE:
		next();
		s = mkscope(s);
		while (tok.kind != TRBRACE) {
			if (gotolabel(f) || !decl(s, f))
				stmt(f, s);
		}
		s = delscope(s);
		next();
		break;

	/* 6.8.3 Expression statement */
	case TSEMICOLON:
		next();
		break;
	default:
		e = expr(s);
		v = funcexpr(f, e);
		delexpr(e);
		expect(TSEMICOLON, "after expression statement");
		break;

	/* 6.8.4 Selection statement */
	case TIF:
		next();
		s = mkscope(s);
		expect(TLPAREN, "after 'if'");
		e = ctrlexpr(s);
		v = funcexpr(f, e);
		delexpr(e);
		expect(TRPAREN, "after expression");

		label[0] = mkblock("if_true");
		label[1] = mkblock("if_false");
		funcjnz(f, v, label[0], label[1]);

		funclabel(f, label[0]);
		s = mkscope(s);
		stmt(f, s);
		s = delscope(s);

		if (consume(TELSE)) {
			label[2] = mkblock("if_join");
			funcjmp(f, label[2]);
			funclabel(f, label[1]);
			s = mkscope(s);
			stmt(f, s);
			s = delscope(s);
			funclabel(f, label[2]);
		} else {
			funclabel(f, label[1]);
		}
		s = delscope(s);
		break;
	case TSWITCH:
		next();

		s = mkscope(s);
		expect(TLPAREN, "after 'switch'");
		e = expr(s);
		expect(TRPAREN, "after expression");

		if (!(e->type->prop & PROPINT))
			error(&tok.loc, "controlling expression of switch statement must have integer type");
		e = exprpromote(e);

		label[0] = mkblock("switch_cond");
		label[1] = mkblock("switch_join");

		v = funcexpr(f, e);
		funcjmp(f, label[0]);
		s = mkscope(s);
		s->breaklabel = label[1];
		s->switchcases = &swtch;
		stmt(f, s);
		funcjmp(f, label[1]);

		funclabel(f, label[0]);
		funcswitch(f, v, &swtch, swtch.defaultlabel ? swtch.defaultlabel : label[1]);
		s = delscope(s);

		funclabel(f, label[1]);
		s = delscope(s);
		break;

	/* 6.8.5 Iteration statements */
	case TWHILE:
		next();
		s = mkscope(s);
		expect(TLPAREN, "after 'while'");
		e = ctrlexpr(s);
		expect(TRPAREN, "after expression");

		label[0] = mkblock("while_cond");
		label[1] = mkblock("while_body");
		label[2] = mkblock("while_join");

		funclabel(f, label[0]);
		v = funcexpr(f, e);
		funcjnz(f, v, label[1], label[2]);
		funclabel(f, label[1]);
		s = mkscope(s);
		s->continuelabel = label[0];
		s->breaklabel = label[2];
		stmt(f, s);
		s = delscope(s);
		funcjmp(f, label[0]);
		funclabel(f, label[2]);
		s = delscope(s);
		break;
	case TDO:
		next();

		label[0] = mkblock("do_body");
		label[1] = mkblock("do_cond");
		label[2] = mkblock("do_join");

		s = mkscope(s);
		s = mkscope(s);
		s->continuelabel = label[1];
		s->breaklabel = label[2];
		funclabel(f, label[0]);
		stmt(f, s);
		s = delscope(s);

		expect(TWHILE, "after 'do' statement");
		expect(TLPAREN, "after 'while'");
		funclabel(f, label[1]);
		e = ctrlexpr(s);
		expect(TRPAREN, "after expression");

		v = funcexpr(f, e);
		funcjnz(f, v, label[0], label[2]);  // XXX: compare to 0
		funclabel(f, label[2]);
		s = delscope(s);
		expect(TSEMICOLON, "after 'do' statement");
		break;
	case TFOR:
		next();
		expect(TLPAREN, "after while");
		s = mkscope(s);
		if (!decl(s, f)) {
			if (tok.kind != TSEMICOLON) {
				e = expr(s);
				funcexpr(f, e);
				delexpr(e);
			}
			expect(TSEMICOLON, NULL);
		}

		label[0] = mkblock("for_cond");
		label[1] = mkblock("for_body");
		label[2] = mkblock("for_cont");
		label[3] = mkblock("for_join");

		funclabel(f, label[0]);
		if (tok.kind != TSEMICOLON) {
			e = ctrlexpr(s);
			v = funcexpr(f, e);
			funcjnz(f, v, label[1], label[3]);
			delexpr(e);
		}
		expect(TSEMICOLON, NULL);
		e = tok.kind == TRPAREN ? NULL : expr(s);
		expect(TRPAREN, NULL);

		funclabel(f, label[1]);
		s = mkscope(s);
		s->breaklabel = label[3];
		s->continuelabel = label[2];
		stmt(f, s);
		s = delscope(s);

		funclabel(f, label[2]);
		if (e) {
			funcexpr(f, e);
			delexpr(e);
		}
		funcjmp(f, label[0]);
		funclabel(f, label[3]);
		s = delscope(s);
		break;

	/* 6.8.6 Jump statements */
	case TGOTO:
		next();
		name = expect(TIDENT, "after 'goto'");
		funcjmp(f, funcgoto(f, name)->label);
		expect(TSEMICOLON, "after 'goto' statement");
		break;
	case TCONTINUE:
		next();
		if (!s->continuelabel)
			error(&tok.loc, "'continue' statement must be in loop");
		funcjmp(f, s->continuelabel);
		expect(TSEMICOLON, "after 'continue' statement");
		break;
	case TBREAK:
		next();
		if (!s->breaklabel)
			error(&tok.loc, "'break' statement must be in loop or switch");
		funcjmp(f, s->breaklabel);
		expect(TSEMICOLON, "after 'break' statement");
		break;
	case TRETURN:
		next();
		t = functype(f);
		if (t->base != &typevoid) {
			e = exprconvert(expr(s), t->base);
			v = funcexpr(f, e);
			delexpr(e);
		} else {
			v = NULL;
		}
		funcret(f, v);
		expect(TSEMICOLON, "after 'return' statement");
		break;

	case T__ASM__:
		error(&tok.loc, "inline assembly is not yet supported");
	}
}