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

Last change on this file since 4603 was 4603, checked in by nmedfort, 4 years ago

Temporary check-in

File size: 3.5 KB
Line 
1#include "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
18#define MAKE_BINARY(NAME, TYPE, ARGS...) \
19struct __##NAME { \
20    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2) { \
21        return mPb.NAME(arg1, arg2); \
22    } \
23    inline __##NAME(PabloBlock & pb) : mPb(pb) {} \
24private: \
25    PabloBlock & mPb; \
26}; \
27__##NAME functor(mPb); \
28PabloAST * result = mExprTable.findBinaryOrCall(std::move(functor), TYPE, ARGS)
29
30#define MAKE_NAMED_BINARY(NAME, TYPE, ARGS...) \
31struct __##NAME { \
32    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, const std::string name) { \
33        return mPb.NAME(arg1, arg2, name); \
34    } \
35    inline __##NAME(PabloBlock & pb) : mPb(pb) {} \
36private: \
37    PabloBlock & mPb; \
38}; \
39__##NAME functor(mPb); \
40PabloAST * result = mExprTable.findBinaryOrCall(std::move(functor), TYPE, ARGS)
41
42#define MAKE_TERNARY(NAME, TYPE, ARGS...) \
43struct __##NAME { \
44    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, PabloAST * arg3) { \
45        return mPb.NAME(arg1, arg2, arg3); \
46    } \
47    inline __##NAME(PabloBlock & pb) : mPb(pb) {} \
48private: \
49    PabloBlock & mPb; \
50}; \
51__##NAME functor(mPb); \
52PabloAST * result = mExprTable.findTernaryOrCall(std::move(functor), TYPE, ARGS)
53
54
55Call * PabloBuilder::createCall(String * name) {
56    MAKE_UNARY(createCall, PabloAST::ClassTypeId::Call, name);
57    return cast<Call>(result);
58}
59
60PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount) {
61    MAKE_BINARY(createAdvance, PabloAST::ClassTypeId::Advance, expr, shiftAmount);
62    return result;
63}
64
65PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount, const std::string prefix) {
66    MAKE_NAMED_BINARY(createAdvance, PabloAST::ClassTypeId::Advance, expr, shiftAmount, prefix);
67    return result;
68}
69
70PabloAST * PabloBuilder::createNot(PabloAST * expr) {
71    MAKE_UNARY(createNot, PabloAST::ClassTypeId::Not, expr);
72    return result;
73}
74
75PabloAST * PabloBuilder::createAnd(PabloAST * expr1, PabloAST * expr2) {
76    if (expr1 < expr2) {
77        std::swap(expr1, expr2);
78    }
79    MAKE_BINARY(createAnd, PabloAST::ClassTypeId::And, expr1, expr2);
80    return result;
81}
82
83PabloAST * PabloBuilder::createOr(PabloAST * expr1, PabloAST * expr2) {
84    if (expr1 < expr2) {
85        std::swap(expr1, expr2);
86    }
87    MAKE_BINARY(createOr, PabloAST::ClassTypeId::Or, expr1, expr2);
88    return result;
89}
90
91PabloAST * PabloBuilder::createXor(PabloAST * expr1, PabloAST * expr2) {
92    if (expr1 < expr2) {
93        std::swap(expr1, expr2);
94    }
95    MAKE_BINARY(createXor, PabloAST::ClassTypeId::Xor, expr1, expr2);
96    return result;
97}
98
99PabloAST * PabloBuilder::createMatchStar(PabloAST * marker, PabloAST * charclass) {
100    MAKE_BINARY(createMatchStar, PabloAST::ClassTypeId::MatchStar, marker, charclass);
101    return result;
102}
103
104PabloAST * PabloBuilder::createScanThru(PabloAST * from, PabloAST * thru) {
105    MAKE_BINARY(createScanThru, PabloAST::ClassTypeId::ScanThru, from, thru);
106    return result;
107}
108
109PabloAST * PabloBuilder::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr) {
110    MAKE_TERNARY(createSel, PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr);
111    return result;
112}
113
114}
Note: See TracBrowser for help on using the repository browser.