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

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

Mod64Advance, Mod64MatchStar, Mod64ScanThru ops; -mod64-approximate command-line option

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