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

daisytuner / sdfglib / 17589393671

09 Sep 2025 04:35PM UTC coverage: 61.094% (+1.9%) from 59.145%
17589393671

Pull #219

github

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

424 of 1357 new or added lines in 74 files covered. (31.25%)

90 existing lines in 32 files now uncovered.

9307 of 15234 relevant lines covered (61.09%)

109.01 hits per line

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

77.57
/src/codegen/code_generators/c_code_generator.cpp
1
#include "sdfg/codegen/code_generators/c_code_generator.h"
2

3
#include "sdfg/analysis/analysis.h"
4
#include "sdfg/analysis/users.h"
5
#include "sdfg/codegen/dispatchers/node_dispatcher_registry.h"
6
#include "sdfg/codegen/instrumentation/capture_var_plan.h"
7
#include "sdfg/codegen/instrumentation/instrumentation_plan.h"
8

9
namespace sdfg {
10
namespace codegen {
11

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

25
    return "extern " + this->language_extension_.declaration("", sdfg_.return_type()) + sdfg_.name() + "(" +
2✔
26
           arglist_str + ")";
1✔
27
};
1✔
28

29
void CCodeGenerator::emit_capture_context_init(std::ostream& ofs_source) const {
1✔
30
    std::string name = sdfg_.name();
1✔
31

32
    ofs_source << "static void* __capture_ctx;" << std::endl;
1✔
33
    ofs_source << "static void __attribute__((constructor(1000))) __capture_ctx_init(void) {" << std::endl;
1✔
34
    ofs_source << "\t__capture_ctx = __daisy_capture_init(\"" << name << "\");" << std::endl;
1✔
35
    ofs_source << "}" << std::endl;
1✔
36
    ofs_source << std::endl;
1✔
37
}
1✔
38

39
void CCodeGenerator::dispatch_includes() {
4✔
40
    this->includes_stream_ << "#include <math.h>" << std::endl;
4✔
41
    this->includes_stream_ << "#include <stdbool.h>" << std::endl;
4✔
42
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
4✔
43
};
4✔
44

45
void CCodeGenerator::dispatch_structures() {
4✔
46
    // Forward declarations
47
    for (auto& structure : sdfg_.structures()) {
7✔
48
        this->classes_stream_ << "typedef struct " << structure << " " << structure << ";" << std::endl;
3✔
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()) {
7✔
56
        names.push_back(structure);
3✔
57
    }
58
    structures_graph graph(names.size());
4✔
59

60
    for (auto& structure : names) {
7✔
61
        auto& definition = sdfg_.structure(structure);
3✔
62
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
63
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
64
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
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)) {
3✔
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
        }
3✔
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) {
7✔
87
        std::string structure = names.at(structure_index);
3✔
88
        auto& definition = sdfg_.structure(structure);
3✔
89
        this->classes_stream_ << "typedef struct ";
3✔
90
        if (definition.is_packed()) {
3✔
91
            this->classes_stream_ << "__attribute__((packed)) ";
×
92
        }
×
93
        this->classes_stream_ << structure << std::endl;
3✔
94
        this->classes_stream_ << "{\n";
3✔
95

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

109
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
110
    }
3✔
111
};
4✔
112

113
void CCodeGenerator::dispatch_globals() {
4✔
114
    // Declare globals
115
    for (auto& container : sdfg_.externals()) {
5✔
116
        // Function declarations
117
        if (auto function_type = dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
NEW
118
            this->globals_stream_ << "extern " << language_extension_.declaration(container, *function_type) << ";"
×
NEW
119
                                  << std::endl;
×
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 CCodeGenerator::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