• 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

79.46
/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() {
3✔
40
    this->includes_stream_ << "#include <math.h>" << std::endl;
3✔
41
    this->includes_stream_ << "#include <alloca.h>" << std::endl;
3✔
42
    this->includes_stream_ << "#include <stdbool.h>" << std::endl;
3✔
43
    this->includes_stream_ << "#include <stdio.h>" << std::endl;
3✔
44
    this->includes_stream_ << "#include <stdlib.h>" << std::endl;
3✔
45
    this->includes_stream_ << "#include <string.h>" << std::endl;
3✔
46
    this->includes_stream_ << "#include <daisy_rtl/daisy_rtl.h>" << std::endl;
3✔
47
};
3✔
48

49
void CCodeGenerator::dispatch_structures() {
3✔
50
    // Forward declarations
51
    for (auto& structure : sdfg_.structures()) {
6✔
52
        this->classes_stream_ << "typedef struct " << structure << " " << structure << ";" << std::endl;
3✔
53
    }
54

55
    // Generate topology-sorted structure definitions
56
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> structures_graph;
57
    typedef boost::graph_traits<structures_graph>::vertex_descriptor Vertex;
58
    std::vector<std::string> names;
3✔
59
    for (auto& structure : sdfg_.structures()) {
6✔
60
        names.push_back(structure);
3✔
61
    }
62
    structures_graph graph(names.size());
3✔
63

64
    for (auto& structure : names) {
6✔
65
        auto& definition = sdfg_.structure(structure);
3✔
66
        for (size_t i = 0; i < definition.num_members(); i++) {
6✔
67
            auto member_type = &definition.member_type(symbolic::integer(i));
3✔
68
            while (dynamic_cast<const types::Array*>(member_type)) {
3✔
69
                auto array_type = static_cast<const types::Array*>(member_type);
×
70
                member_type = &array_type->element_type();
×
71
            }
72

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

83
    std::list<Vertex> order;
3✔
84
    std::unordered_map<Vertex, boost::default_color_type> vertex_colors;
3✔
85
    boost::topological_sort(
3✔
86
        graph, std::back_inserter(order), boost::color_map(boost::make_assoc_property_map(vertex_colors))
3✔
87
    );
88
    order.reverse();
3✔
89

90
    for (auto& structure_index : order) {
6✔
91
        std::string structure = names.at(structure_index);
3✔
92
        auto& definition = sdfg_.structure(structure);
3✔
93
        this->classes_stream_ << "typedef struct ";
3✔
94
        if (definition.is_packed()) {
3✔
95
            this->classes_stream_ << "__attribute__((packed)) ";
×
96
        }
×
97
        this->classes_stream_ << structure << std::endl;
3✔
98
        this->classes_stream_ << "{\n";
3✔
99

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

113
        this->classes_stream_ << "} " << structure << ";" << std::endl;
3✔
114
    }
3✔
115
};
3✔
116

117
void CCodeGenerator::dispatch_globals() {
3✔
118
    // Declare globals
119
    const std::unordered_set<std::string> reserved_symbols = {"stderr", "stdin", "stdout"};
3✔
120
    for (auto& container : sdfg_.externals()) {
4✔
121
        // Function declarations
122
        if (dynamic_cast<const types::Function*>(&sdfg_.type(container))) {
1✔
NEW
123
            continue;
×
124
        }
125
        // Reserved symbols
126
        if (reserved_symbols.find(container) != reserved_symbols.end()) {
1✔
NEW
127
            continue;
×
128
        }
129

130
        // Other types must be pointers
131
        auto& type = dynamic_cast<const types::Pointer&>(sdfg_.type(container));
1✔
132
        assert(type.has_pointee_type() && "Externals must have a pointee type");
2✔
133
        auto& base_type = type.pointee_type();
1✔
134

135
        if (sdfg_.linkage_type(container) == LinkageType_External) {
1✔
136
            this->globals_stream_ << "extern " << language_extension_.declaration(container, base_type) << ";"
2✔
137
                                  << std::endl;
1✔
138
        } else {
1✔
139
            this->globals_stream_ << "static " << language_extension_.declaration(container, base_type);
×
140
            if (!type.initializer().empty()) {
×
141
                this->globals_stream_ << " = " << type.initializer();
×
142
            }
×
143
            this->globals_stream_ << ";" << std::endl;
×
144
        }
145
    }
146
};
3✔
147

148
void CCodeGenerator::dispatch_schedule() {
3✔
149
    // Declare transient containers
150
    for (auto& container : sdfg_.containers()) {
4✔
151
        if (!sdfg_.is_transient(container)) {
1✔
152
            continue;
1✔
153
        }
154

155
        std::string val = this->language_extension_.declaration(container, sdfg_.type(container), false, true);
×
156
        if (!val.empty()) {
×
157
            this->main_stream_ << val;
×
158
            this->main_stream_ << ";" << std::endl;
×
159
        }
×
160
    }
×
161

162
    auto dispatcher = create_dispatcher(language_extension_, sdfg_, sdfg_.root(), instrumentation_plan_);
3✔
163
    dispatcher->dispatch(this->main_stream_, this->globals_stream_, this->library_snippet_factory_);
3✔
164
};
3✔
165

166
} // namespace codegen
167
} // 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