summaryrefslogtreecommitdiff
path: root/mi/dfcheck.c
blob: fc9664c0a1d581b9efca42b0ce8f10e534aa5ead (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
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "util.h"
#include "parse.h"
#include "mi.h"

static void
checkundef(Node *n, Reaching *r, Bitset *reach, Bitset *kill)
{
	size_t i, j, did;
	Node *def;
	Type *t;

	if (n->type != Nexpr)
		return;
	if (exprop(n) == Ovar) {
		did = n->expr.did;
		if (decls[did]->decl.isconst)
			return;
		for (j = 0; j < r->ndefs[did]; j++) {
			t = tybase(exprtype(n));
			if (t->type == Tystruct || t->type == Tyunion || t->type == Tyarray ||
				t->type == Tytuple || t->type == Tyvoid)
				continue;
			if (bshas(kill, r->defs[did][j]))
				continue;
			if (!bshas(reach, r->defs[did][j]))
				continue;
			def = nodes[r->defs[did][j]];
			if (exprop(def) == Oundef)
				fatal(n, "%s used before definition", namestr(n->expr.args[0]));
		}
	} else {
		switch (exprop(n)) {
		case Oset:
		case Oasn:
		case Oblit:
			checkundef(n->expr.args[1], r, reach, kill);
			break;
		case Oaddr:
		case Oslice:
			/* these don't actually look at the value of args[0], so they're ok. */
			for (i = 1; i < n->expr.nargs; i++)
				checkundef(n->expr.args[i], r, reach, kill);
			break;
		case Ocall:
			for (i = 1; i < n->expr.nargs; i++)
				if (exprop(n->expr.args[i]) != Oaddr)
					checkundef(n->expr.args[i], r, reach, kill);
			break;
		default:
			for (i = 0; i < n->expr.nargs; i++)
				checkundef(n->expr.args[i], r, reach, kill);
			break;
		}
	}
}

static void
checkreach(Cfg *cfg)
{
	Bitset *reach, *kill;
	size_t i, j, k;
	Reaching *r;
	Node *n, *m;
	Bb *bb;

	r = reaching(cfg);
	for (i = 0; i < cfg->nbb; i++) {
		bb = cfg->bb[i];
		if (!bb)
			continue;
		reach = bsdup(r->in[i]);
		kill = mkbs();
		for (j = 0; j < bb->nnl; j++) {
			n = bb->nl[j];
			if (exprop(n) == Oundef) {
				bsput(reach, n->nid);
			} else {
				m = assignee(n);
				if (m)
					for (k = 0; k < r->ndefs[m->expr.did]; k++)
						bsput(kill, r->defs[m->expr.did][k]);
				checkundef(n, r, reach, kill);
			}
		}
		bsfree(reach);
		bsfree(kill);
	}
	reachingfree(r);
}

static void
checkpredret(Cfg *cfg, Bb *bb)
{
	Bb *pred;
	Op op;
	size_t i;

	for (i = 0; bsiter(bb->pred, &i); i++) {
		pred = cfg->bb[i];
		if (pred->nnl == 0) {
			checkpredret(cfg, pred);
		} else {
			op = exprop(pred->nl[pred->nnl - 1]);
			if (op != Oret && op != Odead) {
				fatal(pred->nl[pred->nnl-1], "Reaches end of function without return\n");
			}
		}
	}
}

static void
checkret(Cfg *cfg)
{
	Type *ft;

	ft = tybase(decltype(cfg->fn));
	assert(ft->type == Tyfunc || ft->type == Tycode);
	if (ft->sub[0]->type == Tyvoid)
		return;

	checkpredret(cfg, cfg->end);
}

void
check(Cfg *cfg)
{
	checkret(cfg);
	checkreach(cfg);
}