summaryrefslogtreecommitdiff
path: root/opt/df.c
blob: 08b864cc42426ee999a0e494eef435b95b53d962 (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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.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 "parse.h"
#include "opt.h"


Bb *mkbb(Cfg *cfg)
{
    static int nextbbid = 0;
    Bb *bb;

    bb = zalloc(sizeof(Bb));
    bb->id = nextbbid++;
    bb->in = mkbs();
    bb->out = mkbs();
    lappend(&cfg->bb, &cfg->nbb, bb);
    return bb;
}

void label(Cfg *cfg, Node *lbl, Bb *bb)
{
    htput(cfg->lblmap, lbl->lbl.name, bb);
    lappend(&bb->lbls, &bb->nlbls, lbl->lbl.name);
}

int addnode(Cfg *cfg, Bb *bb, Node *n)
{
    switch (exprop(n)) {
        case Ojmp:
        case Ocjmp:
            lappend(&bb->nl, &bb->nnl, n);
            lappend(&cfg->fixjmp, &cfg->nfixjmp, n);
            lappend(&cfg->fixblk, &cfg->nfixblk, n);
            return 1;
            break;
        default:
            lappend(&bb->nl, &bb->nnl, n);
            break;
    }
    return 0;
}

Cfg *mkcfg(Node **nl, int nn)
{
    Cfg *cfg;
    Bb *bb, *targ;
    Node *a, *b;
    int i;
    
    cfg = zalloc(sizeof(Cfg));
    cfg->lblmap = mkht(strhash, streq);
    bb = mkbb(cfg);
    for (i = 0; i < nn; i++) {
        switch (nl[i]->type) {
            case Nlbl:
                if (bb->nnl)
                    bb = mkbb(cfg);
                label(cfg, nl[i], bb);
                break;
            case Nexpr:
                if (addnode(cfg, bb, nl[i]))
                    bb = mkbb(cfg);
                break;
            case Ndecl:
                break;
            default:
                die("Invalid node type %s in mkcfg", nodestr(nl[i]->type));
        }
    }
    for (i = 0; i < cfg->nfixjmp; i++) {
        bb = cfg->fixblk[i];
        switch (exprop(cfg->fixjmp[i])) {
            case Ojmp:
                a = cfg->fixjmp[i]->expr.args[0];
                b = NULL;
                break;
            case Ocjmp:
                a = cfg->fixjmp[i]->expr.args[0];
                b = cfg->fixjmp[i]->expr.args[1];
                break;
            default:
                die("Bad jump fix thingy");
                break;
        }
        if (a) {
            targ = htget(cfg->lblmap, a->lbl.name);
            if (!targ)
                die("No bb with label %s", a->lbl.name);
            bsput(bb->out, targ->id);
            bsput(targ->in, bb->id);
        }
        if (b) {
            targ = htget(cfg->lblmap, b->lbl.name);
            if (!targ)
                die("No bb with label %s", b->lbl.name);
            bsput(bb->out, targ->id);
            bsput(targ->in, bb->id);
        }
    }
    return cfg;
}
void dumpcfg(Cfg *cfg, FILE *fd)
{
    int i, j;
    Bb *bb;
    char *sep;

    for (j = 0; j < cfg->nbb; j++) {
        bb = cfg->bb[j];
        fprintf(fd, "Bb: %d\n", bb->id);

        /* in edges */
        fprintf(fd, "In:  ");
        sep = "";
        for (i = 0; i < bsmax(bb->in); i++) {
             if (bshas(bb->in, i))
                fprintf(fd, "%d%s", i, sep);
             sep = ",";
        }
        fprintf(fd, "\n");

        /* out edges */
        fprintf(fd, "Out: ");
        sep = "";
        for (i = 0; i < bsmax(bb->out); i++) {
             if (bshas(bb->in, i))
                fprintf(fd, "%d%s", i, sep);
             sep = ",";
        }
        fprintf(fd, "\n");

        for (i = 0; i < bb->nnl; i++)
            dump(bb->nl[i], fd);
    }
}