• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daisytuner / sdfglib / 17576881200

09 Sep 2025 08:38AM UTC coverage: 61.126% (+2.0%) from 59.145%
17576881200

Pull #219

github

web-flow
Merge 51d6d85aa into b8fdeb232
Pull Request #219: stdlib Library Nodes and ConstantNodes

424 of 1350 new or added lines in 74 files covered. (31.41%)

90 existing lines in 32 files now uncovered.

9307 of 15226 relevant lines covered (61.13%)

109.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.73
/src/codegen/code_generators/cpp_code_generator.cpp
1
#include "sdfg/codegen/code_generators/cpp_code_generator.h"
2

3
#include "sdfg/codegen/dispatchers/node_dispatcher_registry.h"
4
#include "sdfg/codegen/instrumentation/instrumentation_plan.h"
5

6
namespace sdfg {
7
namespace codegen {
8

9
std::string CPPCodeGenerator::function_definition() {
1✔
10
    /********** Arglist **********/
11
    std::vector<std::string> args;
1✔
12
    for (auto& container : sdfg_.arguments()) {
1✔
13
        args.push_back(language_extension_.declaration(container, sdfg_.type(container)));
×
14
    }
15
    std::stringstream arglist;
1✔
16
    arglist << sdfg::helpers::join(args, ", ");
1✔
17
    std::string arglist_str = arglist.str();
1✔
18
    if (arglist_str.empty()) {
1✔
19
        arglist_str = "void";
1✔
20
    }
1✔
21

22
    return "extern \"C\" " + this->language_extension_.declaration("", sdfg_.return_type()) + sdfg_.name() + "(" +
2✔
23
           arglist_str + ")";
1✔
24
};
1✔
25

26
void CPPCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
×
27
    std::string name = sdfg_.name();
×
28

29
    ofs_source << "static void* __capture_ctx;" << std::endl;
×
30
    ofs_source << "static void __attribute__((constructor(1000))) __capture_ctx_init(void) {" << std::endl;
×
31
    ofs_source << "\t__capture_ctx = __daisy_capture_init(\"" << name << "\");" << std::endl;
×
32
    ofs_source << "}" << std::endl;
×
33
    ofs_source << std::endl;
×
34
}
×
35

36
void CPPCodeGenerator::dispatch_includes() {
5✔
37
    this->includes_stream_ << "#include <cmath>" << std::endl;
5✔
38
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
5✔
39
};
5✔
40

41
void CPPCodeGenerator::dispatch_structures() {
5✔
42
    // Forward declarations
43
    for (auto& structure : sdfg_.structures()) {
9✔
44
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
4✔
45
    }
46

47
    // Generate topology-sorted structure definitions
48
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
49
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
50
    std::vector<std::string> names;
5✔
51
    for (auto& structure : sdfg_.structures()) {
9✔
52
        names.push_back(structure);
4✔
53
    }
54
    structures_graph graph(names.size());
5✔
55

56
    for (auto& structure : names) {
9✔
57
        auto& definition = sdfg_.structure(structure);
4✔
58
        for (size_t i = 0; i < definition.num_members(); i++) {
8✔
59
            auto member_type = &definition.member_type(symbolic::integer(i));
4✔
60
            while (dynamic_cast<const types::Array*>(member_type)) {
4✔
61
                auto array_type = static_cast<const types::Array*>(member_type);
×
62
                member_type = &array_type->element_type();
×
63
            }
64

65
            if (auto member_structure = dynamic_cast<const sdfg::types::Structure*>(member_type)) {
4✔
66
                boost::add_edge(
1✔
67
                    std::find(names.begin(), names.end(), member_structure->name()) - names.begin(),
1✔
68
                    std::find(names.begin(), names.end(), structure) - names.begin(),
1✔
69
                    graph
70
                );
71
            }
1✔
72
        }
4✔
73
    }
74

75
    std::list<Vertex> order;
5✔
76
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
5✔
77
    boost::topological_sort(
5✔
78
        graph, std::back_inserter(order), boost::color_map(boost::make_assoc_property_map(vertex_colors))
5✔
79
    );
80
    order.reverse();
5✔
81

82
    for (auto& structure_index : order) {
9✔
83
        std::string structure = names.at(structure_index);
4✔
84
        auto& definition = sdfg_.structure(structure);
4✔
85
        this->classes_stream_ << "struct ";
4✔
86
        if (definition.is_packed()) {
4✔
87
            this->classes_stream_ << "__attribute__((packed)) ";
1✔
88
        }
1✔
89
        this->classes_stream_ << structure << std::endl;
4✔
90
        this->classes_stream_ << "{\n";
4✔
91

92
        for (size_t i = 0; i < definition.num_members(); i++) {
8✔
93
            auto& member_type = definition.member_type(symbolic::integer(i));
4✔
94
            if (dynamic_cast<const sdfg::types::Structure*>(&member_type)) {
4✔
95
                this->classes_stream_ << "struct ";
1✔
96
            }
1✔
97
            this->classes_stream_
8✔
98
                << language_extension_.declaration("member_" + std::to_string(i), member_type, false, true);
4✔
99
            this->classes_stream_ << ";" << std::endl;
4✔
100
        }
4✔
101

102
        this->classes_stream_ << "};" << std::endl;
4✔
103
    }
4✔
104
};
5✔
105

106
void CPPCodeGenerator::dispatch_globals() {
5✔
107
    // Declare globals
108
    for (auto& container : sdfg_.externals()) {
6✔
109
        // Function declarations
110
        if (auto function_type = dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
NEW
111
            this->globals_stream_ << "extern " << language_extension_.declaration(container, *function_type) << ";"
×
NEW
112
                                  << std::endl;
×
NEW
113
            continue;
×
114
        }
115

116
        // Other types must be pointers
117
        auto& type = dynamic_cast<const types::Pointer&>(sdfg_.type(container));
1✔
118
        assert(type.has_pointee_type() && "Externals must have a pointee type");
2✔
119
        auto& base_type = type.pointee_type();
1✔
120

121
        if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
122
            this->globals_stream_ << "extern " << language_extension_.declaration(container, base_type) << ";"
2✔
123
                                  << std::endl;
1✔
124
        } else {
1✔
125
            this->globals_stream_ << "static " << language_extension_.declaration(container, base_type);
×
126
            if (!type.initializer().empty()) {
×
127
                this->globals_stream_ << " = " << type.initializer();
×
128
            }
×
129
            this->globals_stream_ << ";" << std::endl;
×
130
        }
131
    }
132
};
5✔
133

134
void CPPCodeGenerator::dispatch_schedule() {
5✔
135
    // Declare transient containers
136
    for (auto& container : sdfg_.containers()) {
6✔
137
        if (!sdfg_.is_transient(container)) {
1✔
138
            continue;
1✔
139
        }
140

141
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
142
        if (!val.empty()) {
×
143
            this->main_stream_ << val;
×
144
            this->main_stream_ << ";" << std::endl;
×
145
        }
×
146
    }
×
147

148
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), instrumentation_plan_);
5✔
149
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
5✔
150
};
5✔
151

152
} // namespace codegen
153
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc