source: icGREP/icgrep-devel/icgrep/pablo/builder.cpp @ 4685

Last change on this file since 4685 was 4683, checked in by cameron, 4 years ago

Use #include <pablo/...> syntax uniformally for all pablo headers

File size: 7.0 KB
Line 
1#include <pablo/builder.hpp>
2
3namespace pablo {
4
5#define MAKE_UNARY(NAME, TYPE, ARGS...) \
6struct __##NAME { \
7    inline PabloAST * operator()(PabloAST * arg) { \
8        return mPb->NAME(arg); \
9    } \
10    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
11private: \
12    PabloBlock * mPb; \
13}; \
14__##NAME functor(mPb); \
15PabloAST * result = mExprTable.findUnaryOrCall(std::move(functor), TYPE, ARGS)
16
17#define MAKE_NAMED_UNARY(NAME, TYPE, ARGS...) \
18struct __##NAME { \
19    inline PabloAST * operator()(PabloAST * arg, const std::string name) { \
20        return mPb->NAME(arg, name); \
21    } \
22    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
23private: \
24    PabloBlock * mPb; \
25}; \
26__##NAME functor(mPb); \
27PabloAST * result = mExprTable.findUnaryOrCall(std::move(functor), TYPE, ARGS)
28
29#define MAKE_UNARY_VARIABLE(NAME, TYPE, ARGS...) \
30struct __##NAME { \
31    inline PabloAST * operator()(PabloAST * arg, const std::vector<PabloAST *> & args) { \
32        return mPb->NAME(arg, args); \
33    } \
34    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
35private: \
36    PabloBlock * mPb; \
37}; \
38__##NAME functor(mPb); \
39PabloAST * result = mExprTable.findUnaryVariableOrCall(std::move(functor), TYPE, ARGS)
40
41
42#define MAKE_BINARY(NAME, TYPE, ARGS...) \
43struct __##NAME { \
44    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2) { \
45        return mPb->NAME(arg1, arg2); \
46    } \
47    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
48private: \
49    PabloBlock * mPb; \
50}; \
51__##NAME functor(mPb); \
52PabloAST * result = mExprTable.findBinaryOrCall(std::move(functor), TYPE, ARGS)
53
54#define MAKE_NAMED_BINARY(NAME, TYPE, ARGS...) \
55struct __##NAME { \
56    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, const std::string name) { \
57        return mPb->NAME(arg1, arg2, name); \
58    } \
59    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
60private: \
61    PabloBlock * mPb; \
62}; \
63__##NAME functor(mPb); \
64PabloAST * result = mExprTable.findBinaryOrCall(std::move(functor), TYPE, ARGS)
65
66#define MAKE_TERNARY(NAME, TYPE, ARGS...) \
67struct __##NAME { \
68    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, PabloAST * arg3) { \
69        return mPb->NAME(arg1, arg2, arg3); \
70    } \
71    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
72private: \
73    PabloBlock * mPb; \
74}; \
75__##NAME functor(mPb); \
76PabloAST * result = mExprTable.findTernaryOrCall(std::move(functor), TYPE, ARGS)
77
78#define MAKE_NAMED_TERNARY(NAME, TYPE, ARGS...) \
79struct __##NAME { \
80    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, PabloAST * arg3, const std::string name) { \
81        return mPb->NAME(arg1, arg2, arg3, name); \
82    } \
83    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
84private: \
85    PabloBlock * mPb; \
86}; \
87__##NAME functor(mPb); \
88PabloAST * result = mExprTable.findTernaryOrCall(std::move(functor), TYPE, ARGS)
89
90#define MAKE_VARIABLE(NAME, TYPE, ARGS) \
91struct __##NAME { \
92    inline PabloAST * operator()(PabloAST * arg) { \
93        return mPb->NAME(arg); \
94    } \
95    inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
96private: \
97    PabloBlock * mPb; \
98}; \
99__##NAME functor(mPb); \
100PabloAST * result = mExprTable.findUnaryOrCall(std::move(functor), TYPE, ARGS)
101
102
103Call * PabloBuilder::createCall(Prototype * prototype, const std::vector<Var *> & args) {
104    if (args.size() != cast<Prototype>(prototype)->getNumOfParameters()) {
105        throw std::runtime_error("Invalid number of arguments passed into Call object!");
106    }
107    MAKE_UNARY_VARIABLE(createCall, PabloAST::ClassTypeId::Call, prototype, reinterpret_cast<const std::vector<PabloAST *> &>(args));
108    return cast<Call>(result);
109}
110
111PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount) {
112    MAKE_BINARY(createAdvance, PabloAST::ClassTypeId::Advance, expr, shiftAmount);
113    return result;
114}
115
116PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount, const std::string prefix) {
117    MAKE_NAMED_BINARY(createAdvance, PabloAST::ClassTypeId::Advance, expr, shiftAmount, prefix);
118    return result;
119}
120
121PabloAST * PabloBuilder::createNot(PabloAST * expr) {
122    MAKE_UNARY(createNot, PabloAST::ClassTypeId::Not, expr);
123    return result;
124}
125
126PabloAST * PabloBuilder::createNot(PabloAST * expr, const std::string prefix) {
127    MAKE_NAMED_UNARY(createNot, PabloAST::ClassTypeId::Not, expr, prefix);
128    return result;
129}
130
131PabloAST * PabloBuilder::createAnd(PabloAST * expr1, PabloAST * expr2) {
132    if (expr1 < expr2) {
133        std::swap(expr1, expr2);
134    }
135    MAKE_BINARY(createAnd, PabloAST::ClassTypeId::And, expr1, expr2);
136    return result;
137}
138
139PabloAST * PabloBuilder::createAnd(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
140    if (expr1 < expr2) {
141        std::swap(expr1, expr2);
142    }
143    MAKE_NAMED_BINARY(createAnd, PabloAST::ClassTypeId::And, expr1, expr2, prefix);
144    return result;
145}
146
147PabloAST * PabloBuilder::createOr(PabloAST * expr1, PabloAST * expr2) {
148    if (expr1 < expr2) {
149        std::swap(expr1, expr2);
150    }
151    MAKE_BINARY(createOr, PabloAST::ClassTypeId::Or, expr1, expr2);
152    return result;
153}
154
155PabloAST * PabloBuilder::createOr(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
156    if (expr1 < expr2) {
157        std::swap(expr1, expr2);
158    }
159    MAKE_NAMED_BINARY(createOr, PabloAST::ClassTypeId::Or, expr1, expr2, prefix);
160    return result;
161}
162
163PabloAST * PabloBuilder::createXor(PabloAST * expr1, PabloAST * expr2) {
164    if (expr1 < expr2) {
165        std::swap(expr1, expr2);
166    }
167    MAKE_BINARY(createXor, PabloAST::ClassTypeId::Xor, expr1, expr2);
168    return result;
169}
170
171PabloAST * PabloBuilder::createXor(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
172    if (expr1 < expr2) {
173        std::swap(expr1, expr2);
174    }
175    MAKE_NAMED_BINARY(createXor, PabloAST::ClassTypeId::Xor, expr1, expr2, prefix);
176    return result;
177}
178
179PabloAST * PabloBuilder::createMatchStar(PabloAST * marker, PabloAST * charclass) {
180    MAKE_BINARY(createMatchStar, PabloAST::ClassTypeId::MatchStar, marker, charclass);
181    return result;
182}
183
184PabloAST * PabloBuilder::createMatchStar(PabloAST * marker, PabloAST * charclass, const std::string prefix) {
185    MAKE_NAMED_BINARY(createMatchStar, PabloAST::ClassTypeId::MatchStar, marker, charclass, prefix);
186    return result;
187}
188
189PabloAST * PabloBuilder::createScanThru(PabloAST * from, PabloAST * thru) {
190    MAKE_BINARY(createScanThru, PabloAST::ClassTypeId::ScanThru, from, thru);
191    return result;
192}
193
194PabloAST * PabloBuilder::createScanThru(PabloAST * from, PabloAST * thru, const std::string prefix) {
195    MAKE_NAMED_BINARY(createScanThru, PabloAST::ClassTypeId::ScanThru, from, thru, prefix);
196    return result;
197}
198
199
200PabloAST * PabloBuilder::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr) {
201    MAKE_TERNARY(createSel, PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr);
202    return result;
203}
204
205PabloAST * PabloBuilder::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr, const std::string prefix) {
206    MAKE_NAMED_TERNARY(createSel, PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr, prefix);
207    return result;
208}
209
210
211}
Note: See TracBrowser for help on using the repository browser.