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

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

Preliminary changes to inclusion of UCD Compiler into the RE Compiler.

File size: 5.9 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#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_BINARY(NAME, TYPE, ARGS...) \
30struct __##NAME { \
31    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2) { \
32        return mPb.NAME(arg1, arg2); \
33    } \
34    inline __##NAME(PabloBlock & pb) : mPb(pb) {} \
35private: \
36    PabloBlock & mPb; \
37}; \
38__##NAME functor(mPb); \
39PabloAST * result = mExprTable.findBinaryOrCall(std::move(functor), TYPE, ARGS)
40
41#define MAKE_NAMED_BINARY(NAME, TYPE, ARGS...) \
42struct __##NAME { \
43    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, const std::string name) { \
44        return mPb.NAME(arg1, arg2, name); \
45    } \
46    inline __##NAME(PabloBlock & pb) : mPb(pb) {} \
47private: \
48    PabloBlock & mPb; \
49}; \
50__##NAME functor(mPb); \
51PabloAST * result = mExprTable.findBinaryOrCall(std::move(functor), TYPE, ARGS)
52
53#define MAKE_TERNARY(NAME, TYPE, ARGS...) \
54struct __##NAME { \
55    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, PabloAST * arg3) { \
56        return mPb.NAME(arg1, arg2, arg3); \
57    } \
58    inline __##NAME(PabloBlock & pb) : mPb(pb) {} \
59private: \
60    PabloBlock & mPb; \
61}; \
62__##NAME functor(mPb); \
63PabloAST * result = mExprTable.findTernaryOrCall(std::move(functor), TYPE, ARGS)
64
65#define MAKE_NAMED_TERNARY(NAME, TYPE, ARGS...) \
66struct __##NAME { \
67    inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, PabloAST * arg3, const std::string name) { \
68        return mPb.NAME(arg1, arg2, arg3, name); \
69    } \
70    inline __##NAME(PabloBlock & pb) : mPb(pb) {} \
71private: \
72    PabloBlock & mPb; \
73}; \
74__##NAME functor(mPb); \
75PabloAST * result = mExprTable.findTernaryOrCall(std::move(functor), TYPE, ARGS)
76
77
78Call * PabloBuilder::createCall(String * name) {
79    MAKE_UNARY(createCall, PabloAST::ClassTypeId::Call, name);
80    return cast<Call>(result);
81}
82
83PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount) {
84    MAKE_BINARY(createAdvance, PabloAST::ClassTypeId::Advance, expr, shiftAmount);
85    return result;
86}
87
88PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount, const std::string prefix) {
89    MAKE_NAMED_BINARY(createAdvance, PabloAST::ClassTypeId::Advance, expr, shiftAmount, prefix);
90    return result;
91}
92
93PabloAST * PabloBuilder::createNot(PabloAST * expr) {
94    MAKE_UNARY(createNot, PabloAST::ClassTypeId::Not, expr);
95    return result;
96}
97
98PabloAST * PabloBuilder::createNot(PabloAST * expr, const std::string prefix) {
99    MAKE_NAMED_UNARY(createNot, PabloAST::ClassTypeId::Not, expr, prefix);
100    return result;
101}
102
103PabloAST * PabloBuilder::createAnd(PabloAST * expr1, PabloAST * expr2) {
104    if (expr1 < expr2) {
105        std::swap(expr1, expr2);
106    }
107    MAKE_BINARY(createAnd, PabloAST::ClassTypeId::And, expr1, expr2);
108    return result;
109}
110
111PabloAST * PabloBuilder::createAnd(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
112    if (expr1 < expr2) {
113        std::swap(expr1, expr2);
114    }
115    MAKE_NAMED_BINARY(createAnd, PabloAST::ClassTypeId::And, expr1, expr2, prefix);
116    return result;
117}
118
119PabloAST * PabloBuilder::createOr(PabloAST * expr1, PabloAST * expr2) {
120    if (expr1 < expr2) {
121        std::swap(expr1, expr2);
122    }
123    MAKE_BINARY(createOr, PabloAST::ClassTypeId::Or, expr1, expr2);
124    return result;
125}
126
127PabloAST * PabloBuilder::createOr(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
128    if (expr1 < expr2) {
129        std::swap(expr1, expr2);
130    }
131    MAKE_NAMED_BINARY(createOr, PabloAST::ClassTypeId::Or, expr1, expr2, prefix);
132    return result;
133}
134
135PabloAST * PabloBuilder::createXor(PabloAST * expr1, PabloAST * expr2) {
136    if (expr1 < expr2) {
137        std::swap(expr1, expr2);
138    }
139    MAKE_BINARY(createXor, PabloAST::ClassTypeId::Xor, expr1, expr2);
140    return result;
141}
142
143PabloAST * PabloBuilder::createXor(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
144    if (expr1 < expr2) {
145        std::swap(expr1, expr2);
146    }
147    MAKE_NAMED_BINARY(createXor, PabloAST::ClassTypeId::Xor, expr1, expr2, prefix);
148    return result;
149}
150
151PabloAST * PabloBuilder::createMatchStar(PabloAST * marker, PabloAST * charclass) {
152    MAKE_BINARY(createMatchStar, PabloAST::ClassTypeId::MatchStar, marker, charclass);
153    return result;
154}
155
156PabloAST * PabloBuilder::createMatchStar(PabloAST * marker, PabloAST * charclass, const std::string prefix) {
157    MAKE_NAMED_BINARY(createMatchStar, PabloAST::ClassTypeId::MatchStar, marker, charclass, prefix);
158    return result;
159}
160
161PabloAST * PabloBuilder::createScanThru(PabloAST * from, PabloAST * thru) {
162    MAKE_BINARY(createScanThru, PabloAST::ClassTypeId::ScanThru, from, thru);
163    return result;
164}
165
166PabloAST * PabloBuilder::createScanThru(PabloAST * from, PabloAST * thru, const std::string prefix) {
167    MAKE_NAMED_BINARY(createScanThru, PabloAST::ClassTypeId::ScanThru, from, thru, prefix);
168    return result;
169}
170
171
172PabloAST * PabloBuilder::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr) {
173    MAKE_TERNARY(createSel, PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr);
174    return result;
175}
176
177PabloAST * PabloBuilder::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr, const std::string prefix) {
178    MAKE_NAMED_TERNARY(createSel, PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr, prefix);
179    return result;
180}
181
182
183}
Note: See TracBrowser for help on using the repository browser.