summaryrefslogtreecommitdiff
path: root/src/trans/codegen_c.hpp
blob: 77ac5d5c7dcf6329b035431608047ccd1de89d2b (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
/*
 * MRustC - Mutabah's Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * trans/codegen_c.hpp
 * - C codegen backend (internal header)
 */
#pragma once
#include <vector>
#include <memory>

class Node;

struct NodeRef
{
    ::std::unique_ptr<Node>    node;
    size_t  bb_idx;

    NodeRef(size_t idx);
    NodeRef(Node node);

    bool has_target() const;
    size_t target() const;

    bool operator==(size_t idx) const {
        return !node && bb_idx == idx;
    }
};

// A node corresponds to a C statement/block
TAGGED_UNION(Node, Block,
(Block, struct {
    size_t  next_bb;
    ::std::vector<NodeRef>  nodes;
    }),
(If, struct {
    size_t  next_bb;
    const ::MIR::LValue* val;
    NodeRef arm_true;
    NodeRef arm_false;
    }),
(Switch, struct {
    size_t  next_bb;
    const ::MIR::LValue* val;
    ::std::vector<NodeRef>  arms;
    }),
(SwitchValue, struct {
    size_t  next_bb;
    const ::MIR::LValue* val;
    NodeRef def_arm;
    ::std::vector<NodeRef>  arms;
    const ::MIR::SwitchValues*  vals;
    }),
(Loop, struct {
    size_t  next_bb;
    NodeRef code;
    })
);

extern ::std::vector<Node> MIR_To_Structured(const ::MIR::Function& fcn);