Skip to content

Commit 42080ca

Browse files
Merge pull request #112 from AjayBrahmakshatriya/dev-remove-builder
Removed the internal builder::builder type
2 parents 22bcf94 + 6012034 commit 42080ca

198 files changed

Lines changed: 6078 additions & 6610 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci-all-samples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ jobs:
3333
uses: actions/checkout@v2
3434
- run: make -C ${{ github.workspace }} -j$(sysctl -n hw.physicalcpu) run
3535
- run: echo "Tests completed"
36+

include/blocks/c_code_generator.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
#include "blocks/block_visitor.h"
1010
#include "blocks/stmt.h"
11-
#include "builder/dyn_var.h"
1211
#include "util/printer.h"
1312
#include <unordered_map>
1413
#include <unordered_set>
14+
#include "builder/forward_declarations.h"
15+
#include "builder/providers_dyn_var.h"
1516

1617
namespace block {
1718
class c_code_generator : public block_visitor {
@@ -116,11 +117,11 @@ class c_code_generator : public block_visitor {
116117
}
117118
template <typename T>
118119
static void generate_struct_decl(std::ostream &oss, int indent = 0) {
119-
static_assert(std::is_base_of<builder::var, T>::value, "Template argument should be a dyn_var");
120-
auto save = builder::options::track_members;
121-
builder::options::track_members = true;
120+
static_assert(builder::is_dyn_var_type<T>::value, "Template argument should be a dyn_var");
121+
auto save = builder::user_defined_provider_track_members;
122+
builder::user_defined_provider_track_members = true;
122123
T v = builder::with_name("_");
123-
builder::options::track_members = save;
124+
builder::user_defined_provider_track_members = save;
124125

125126
// Construct a struct decl
126127
auto sd = std::make_shared<struct_decl>();
@@ -130,9 +131,9 @@ class c_code_generator : public block_visitor {
130131
"Cannot yet, generate decls for types with template args");
131132

132133
sd->struct_name = to<named_type>(var_type)->type_name;
133-
for (auto member: v.members) {
134+
for (auto member: v.user_defined_members) {
134135
auto decl = std::make_shared<decl_stmt>();
135-
decl->decl_var = member->block_var;
136+
decl->decl_var = member;
136137
decl->init_expr = nullptr;
137138
sd->members.push_back(decl);
138139
}

include/builder/arena_dyn_var.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#ifndef ARENA_DYN_VAR_H
2+
#define ARENA_DYN_VAR_H
3+
#include <cstddef>
4+
5+
namespace builder {
6+
7+
using byte_t = unsigned char;
8+
struct arena_list {
9+
std::vector<byte_t*> chunks;
10+
size_t used_objects = 0;
11+
};
12+
13+
constexpr const int arena_objects_per_chunk = 64;
14+
15+
// Registry type for types that are allocatable
16+
// Each global construct call assigns a unique id to each type
17+
struct allocatable_type_registry {
18+
// Declaration for unique counter, definition is in cpp
19+
static int type_counter;
20+
21+
// A function that can destroy a list of objects of a type
22+
using deleter_t = void(*)(arena_list*);
23+
24+
// This has to be a pointer because we cannot guarantee order of invocation of constructors
25+
static std::vector<deleter_t> *type_deleters;
26+
27+
int type_id;
28+
29+
// Grab an ID and increment
30+
allocatable_type_registry(deleter_t deleter) {
31+
if (type_deleters == nullptr) {
32+
type_deleters = new std::vector<deleter_t>();
33+
}
34+
type_deleters->push_back(deleter);
35+
type_id = type_counter;
36+
type_counter++;
37+
}
38+
39+
// Post global constructor, this returns the maximum
40+
// type id. Make sure this is ONLY called from main
41+
static int get_max_type_id(void) {
42+
return type_counter;
43+
}
44+
};
45+
46+
// This type is instantiated for each
47+
// type that is allocated in the whole binary
48+
template <typename T>
49+
struct allocatable_type_manager {
50+
static allocatable_type_registry register_type;
51+
static void delete_objects(arena_list *list) {
52+
for (size_t i = 0; i < list->used_objects; i++) {
53+
int chunk_id = i / arena_objects_per_chunk;
54+
int chunk_offset = i % arena_objects_per_chunk;
55+
56+
auto ptr_b = list->chunks[chunk_id] + sizeof(T) * chunk_offset;
57+
auto ptr = (T*)ptr_b;
58+
ptr->~T();
59+
}
60+
list->used_objects = 0;
61+
}
62+
};
63+
64+
template <typename T>
65+
allocatable_type_registry allocatable_type_manager<T>::register_type(allocatable_type_manager<T>::delete_objects);
66+
67+
class dyn_var_arena {
68+
// Arena contains a separate list for each type
69+
// indexed by a generated type id
70+
std::vector<arena_list> arena_lists;
71+
72+
template <typename T>
73+
byte_t* grab_buffer() {
74+
// Get arena list index for this type
75+
int index = allocatable_type_manager<T>::register_type.type_id;
76+
arena_list& list = arena_lists[index];
77+
// If list is full add another chunk
78+
if (list.used_objects == list.chunks.size() * arena_objects_per_chunk) {
79+
// Alignment and pointer and the end
80+
static_assert(alignof(T) <= alignof(std::max_align_t),
81+
"Allocated type has higher alignment requirement that std::max_align_t"
82+
"needs manual alignment");
83+
byte_t* new_chunk = new byte_t[sizeof(T) * arena_objects_per_chunk];
84+
list.chunks.push_back(new_chunk);
85+
}
86+
int chunk_id = list.used_objects / arena_objects_per_chunk;
87+
int chunk_offset = list.used_objects % arena_objects_per_chunk;
88+
89+
auto ptr = list.chunks[chunk_id] + sizeof(T) * chunk_offset;
90+
list.used_objects++;
91+
return ptr;
92+
}
93+
94+
public:
95+
96+
dyn_var_arena() {
97+
arena_lists.resize(allocatable_type_registry::get_max_type_id());
98+
}
99+
100+
template <typename T, typename...Args>
101+
T* allocate(Args&&...args) {
102+
auto ptr_b = grab_buffer<T>();
103+
auto ptr = new (ptr_b) T(std::forward<Args>(args)...);
104+
return ptr;
105+
}
106+
107+
108+
void reset_arena(void) {
109+
// For each type call the respective deleters on the type
110+
for (unsigned int i = 0; i < arena_lists.size(); i++) {
111+
if (arena_lists[i].used_objects > 0) {
112+
(*allocatable_type_registry::type_deleters)[i](&(arena_lists[i]));
113+
}
114+
}
115+
}
116+
117+
~dyn_var_arena() {
118+
reset_arena();
119+
for (auto& a: arena_lists) {
120+
for (auto c: a.chunks) {
121+
delete[] c;
122+
}
123+
}
124+
}
125+
};
126+
127+
}
128+
129+
#endif

include/builder/array.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@
55

66
namespace builder {
77

8-
9-
// If the array is of dyn_vars, we initialize with a
10-
// builder, anything else and we directly initialize with T
11-
// This avoids unnecessary copies unless entirely necessary
8+
// This helper avoids unnecessary copies for
9+
// dyn_var objects
1210
template <typename T>
1311
struct initializer_selector {
1412
typedef const T type;
1513
};
1614

1715
template <typename T>
1816
struct initializer_selector<dyn_var<T>> {
19-
typedef builder type;
17+
typedef expr_wrapper type;
2018
};
2119

22-
23-
2420
template <typename T, size_t size = 0>
2521
class array {
2622
private:

include/builder/block_type_extractor.h

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#ifndef TYPE_EXTRACTOR_H
22
#define TYPE_EXTRACTOR_H
33

4-
54
#include "builder/forward_declarations.h"
65
#include "util/mtp_utils.h"
7-
#include "builder/generics.h"
86
#include <algorithm>
97

108
namespace builder {
119

12-
struct custom_type_base;
1310

1411
template <typename T>
1512
struct external_type_namer;
@@ -83,10 +80,8 @@ template <typename T>
8380
class type_extractor {
8481
public:
8582
// This implementation is currenty only used
86-
// by custom types which are derived from custom_type_base
83+
// by custom types which are derived from custom_type
8784
static block::type::Ptr extract_type(void) {
88-
//static_assert(std::is_base_of<custom_type_base, T>::value,
89-
//"Custom types should inherit from builder::custom_type_base");
9085
block::named_type::Ptr type = std::make_shared<block::named_type>();
9186
type->type_name = type_namer<T>::get_type_name();
9287
type->template_args = type_template<T>::get_templates();
@@ -345,67 +340,27 @@ class type_extractor<generic> {
345340
};
346341

347342

348-
349-
// Extracting function types
350-
template <typename... args>
351-
std::vector<block::type::Ptr> extract_type_vector_dyn(void);
352-
353-
template <typename T, typename... args>
354-
std::vector<block::type::Ptr> extract_type_vector_helper_dyn(void) {
355-
std::vector<block::type::Ptr> rest = extract_type_vector_dyn<args...>();
356-
rest.push_back(type_extractor<T>::extract_type());
357-
return rest;
358-
}
359-
360-
template <typename... args>
361-
std::vector<block::type::Ptr> extract_type_vector_dyn(void) {
362-
return extract_type_vector_helper_dyn<args...>();
363-
}
364-
365-
template <>
366-
std::vector<block::type::Ptr> extract_type_vector_dyn<>(void);
367-
368343
template <typename r_type, typename... a_types>
369344
class type_extractor<r_type(a_types...)> {
370345
public:
371346
static block::type::Ptr extract_type(void) {
372347
block::function_type::Ptr type = std::make_shared<block::function_type>();
373348
type->return_type = type_extractor<r_type>::extract_type();
374-
type->arg_types = extract_type_vector_dyn<a_types...>();
375-
std::reverse(type->arg_types.begin(), type->arg_types.end());
349+
type->arg_types = std::vector<block::type::Ptr>({type_extractor<a_types>::extract_type()...});
376350
return type;
377351
}
378352
};
379353

380354
template <const char *N, typename... Args>
381355
struct name {};
382356

383-
template <typename... Args>
384-
struct extract_type_from_args;
385-
386-
template <typename T1, typename... Args>
387-
struct extract_type_from_args<T1, Args...> {
388-
static std::vector<block::type::Ptr> get_types() {
389-
auto a = extract_type_from_args<Args...>::get_types();
390-
a.insert(a.begin(), type_extractor<T1>::extract_type());
391-
return a;
392-
}
393-
};
394-
395-
template <>
396-
struct extract_type_from_args<> {
397-
static std::vector<block::type::Ptr> get_types() {
398-
return std::vector<block::type::Ptr>();
399-
}
400-
};
401-
402357
template <const char *N, typename... Args>
403358
class type_extractor<name<N, Args...>> {
404359
public:
405360
static block::type::Ptr extract_type(void) {
406361
block::named_type::Ptr type = std::make_shared<block::named_type>();
407362
type->type_name = N;
408-
type->template_args = extract_type_from_args<Args...>::get_types();
363+
type->template_args = {type_extractor<Args>::extract_type()...};
409364
return type;
410365
}
411366
};

include/builder/builder.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)