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

daisytuner / sdfglib / 17656823807

11 Sep 2025 08:42PM UTC coverage: 60.447% (+1.1%) from 59.335%
17656823807

Pull #219

github

web-flow
Merge d5416236f into 6c1992b40
Pull Request #219: stdlib Library Nodes and ConstantNodes

460 of 1635 new or added lines in 81 files covered. (28.13%)

93 existing lines in 35 files now uncovered.

9385 of 15526 relevant lines covered (60.45%)

107.21 hits per line

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

77.78
/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() {
4✔
37
    this->includes_stream_ << "#include <alloca.h>" << std::endl;
4✔
38
    this->includes_stream_ << "#include <cmath>" << std::endl;
4✔
39
    this->includes_stream_ << "#include <cstdio>" << std::endl;
4✔
40
    this->includes_stream_ << "#include <cstdlib>" << std::endl;
4✔
41
    this->includes_stream_ << "#include <cstring>" << std::endl;
4✔
42
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
4✔
43
};
4✔
44

45
void CPPCodeGenerator::dispatch_structures() {
4✔
46
    // Forward declarations
47
    for (auto& structure : sdfg_.structures()) {
8✔
48
        this->classes_stream_ << "struct " << structure << ";" << std::endl;
4✔
49
    }
50

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

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

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

79
    std::list<Vertex> order;
4✔
80
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
4✔
81
    boost::topological_sort(
4✔
82
        graph, std::back_inserter(order), boost::color_map(boost::make_assoc_property_map(vertex_colors))
4✔
83
    );
84
    order.reverse();
4✔
85

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

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

106
        this->classes_stream_ << "};" << std::endl;
4✔
107
    }
4✔
108
};
4✔
109

110
void CPPCodeGenerator::dispatch_globals() {
4✔
111
    // Declare globals
112
    const std::unordered_set<std::string> reserved_symbols = {"stderr", "stdin", "stdout"};
4✔
113
    for (auto& container : sdfg_.externals()) {
5✔
114
        // Function declarations
115
        if (dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
NEW
116
            continue;
×
117
        }
118
        // Reserved symbols
119
        if (reserved_symbols.find(container) != reserved_symbols.end()) {
1✔
NEW
120
            continue;
×
121
        }
122

123
        // Other types must be pointers
124
        auto& type = dynamic_cast<const types::Pointer&>(sdfg_.type(container));
1✔
125
        assert(type.has_pointee_type() && "Externals must have a pointee type");
2✔
126
        auto& base_type = type.pointee_type();
1✔
127

128
        if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
129
            this->globals_stream_ << "extern " << language_extension_.declaration(container, base_type) << ";"
2✔
130
                                  << std::endl;
1✔
131
        } else {
1✔
132
            this->globals_stream_ << "static " << language_extension_.declaration(container, base_type);
×
133
            if (!type.initializer().empty()) {
×
134
                this->globals_stream_ << " = " << type.initializer();
×
135
            }
×
136
            this->globals_stream_ << ";" << std::endl;
×
137
        }
138
    }
139
};
4✔
140

141
void CPPCodeGenerator::dispatch_schedule() {
4✔
142
    // Declare transient containers
143
    for (auto& container : sdfg_.containers()) {
5✔
144
        if (!sdfg_.is_transient(container)) {
1✔
145
            continue;
1✔
146
        }
147

148
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
149
        if (!val.empty()) {
×
150
            this->main_stream_ << val;
×
151
            this->main_stream_ << ";" << std::endl;
×
152
        }
×
153
    }
×
154

155
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), instrumentation_plan_);
4✔
156
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
4✔
157
};
4✔
158

159
} // namespace codegen
160
} // 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