forked from cdkersey/chdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcassign.h
87 lines (70 loc) · 1.98 KB
/
cassign.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef CHDL_CASSIGN_H
#define CHDL_CASSIGN_H
#include <sstream>
#include <stack>
// #define GCC_CASSIGN_INSTRUMENTATION
namespace chdl {
template <typename T> struct cassign {
cassign(const T &v): v(v) {}
cassign<T> IF(node x, const T &a
#ifdef GCC_CASSIGN_INSTRUMENTATION
,const char* func = __builtin_FUNCTION(),
unsigned line = __builtin_LINE()
#endif
) {
#ifdef GCC_CASSIGN_INSTRUMENTATION
std::ostringstream hstr;
hstr << "IF," << func << ':' << line;
hierarchy_enter(hstr.str());
#else
HIERARCHY_ENTER();
#endif
T val;
v = Mux(x, val, a);
hierarchy_exit();
return cassign(val);
}
cassign<T> IF(node x
#ifdef GCC_CASSIGN_INSTRUMENTATION
,const char* func = __builtin_FUNCTION(),
unsigned line = __builtin_LINE()
#endif
) {
#ifdef GCC_CASSIGN_INSTRUMENTATION
std::ostringstream hstr;
hstr << "IF," << func << ':' << line;
hierarchy_enter(hstr.str());
#else
HIERARCHY_ENTER();
#endif
T nested_val;
cassign<T> nest(nested_val);
cstack.push(IF(x, nested_val));
hierarchy_exit();
return nest;
}
cassign<T> ELSE() {
T nested_val;
cassign<T> nest(nested_val);
cstack.push(ELSE(nested_val));
return nest;
}
cassign<T> END() {
if (cstack.empty()) abort(); // assert(!cstack.empty());
cassign<T> rval(cstack.top());
cstack.pop();
return rval;
}
cassign<T> ELSE(const T &a) { v = a; return *this; }
cassign<T> THEN() { return *this; }
cassign<T> THEN(const T &a) { return ELSE(a).END(); }
T v;
static std::stack<cassign> cstack;
};
template <typename T> std::stack<cassign<T>> cassign<T>::cstack;
template <typename T> cassign<T> Cassign(const T &v);
};
template <typename T> chdl::cassign<T> chdl::Cassign(const T &v) {
return chdl::cassign<T>(v);
}
#endif