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

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

Bug fixes for last check in.

File size: 5.9 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
[4443]84PabloAST * PabloBuilder::createNot(PabloAST * expr) {
[4602]85    MAKE_UNARY(createNot, PabloAST::ClassTypeId::Not, expr);
86    return result;
[4443]87}
88
[4622]89PabloAST * PabloBuilder::createNot(PabloAST * expr, const std::string prefix) {
[4692]90    MAKE_UNARY(createNot, PabloAST::ClassTypeId::Not, expr, prefix);
[4622]91    return result;
92}
93
[4443]94PabloAST * PabloBuilder::createAnd(PabloAST * expr1, PabloAST * expr2) {
[4695]95    if (isa<Not>(expr1) || expr1 > expr2) {
[4443]96        std::swap(expr1, expr2);
97    }
[4602]98    MAKE_BINARY(createAnd, PabloAST::ClassTypeId::And, expr1, expr2);
99    return result;
[4443]100}
101
[4622]102PabloAST * PabloBuilder::createAnd(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
[4695]103    if (isa<Not>(expr1) || expr1 > expr2) {
[4622]104        std::swap(expr1, expr2);
105    }
[4692]106    MAKE_BINARY(createAnd, PabloAST::ClassTypeId::And, expr1, expr2, prefix);
[4622]107    return result;
108}
109
[4443]110PabloAST * PabloBuilder::createOr(PabloAST * expr1, PabloAST * expr2) {
[4695]111    if (expr1 > expr2) {
[4443]112        std::swap(expr1, expr2);
113    }
[4602]114    MAKE_BINARY(createOr, PabloAST::ClassTypeId::Or, expr1, expr2);
115    return result;
[4443]116}
117
[4622]118PabloAST * PabloBuilder::createOr(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
[4695]119    if (expr1 > expr2) {
[4622]120        std::swap(expr1, expr2);
121    }
[4692]122    MAKE_BINARY(createOr, PabloAST::ClassTypeId::Or, expr1, expr2, prefix);
[4622]123    return result;
124}
125
[4443]126PabloAST * PabloBuilder::createXor(PabloAST * expr1, PabloAST * expr2) {
[4695]127    if (expr1 > expr2) {
[4443]128        std::swap(expr1, expr2);
129    }
[4602]130    MAKE_BINARY(createXor, PabloAST::ClassTypeId::Xor, expr1, expr2);
131    return result;
[4443]132}
133
[4622]134PabloAST * PabloBuilder::createXor(PabloAST * expr1, PabloAST * expr2, const std::string prefix) {
[4695]135    if (expr1 > expr2) {
[4622]136        std::swap(expr1, expr2);
137    }
[4692]138    MAKE_BINARY(createXor, PabloAST::ClassTypeId::Xor, expr1, expr2, prefix);
[4622]139    return result;
140}
141
[4443]142PabloAST * PabloBuilder::createMatchStar(PabloAST * marker, PabloAST * charclass) {
[4602]143    MAKE_BINARY(createMatchStar, PabloAST::ClassTypeId::MatchStar, marker, charclass);
144    return result;
[4443]145}
146
[4622]147PabloAST * PabloBuilder::createMatchStar(PabloAST * marker, PabloAST * charclass, const std::string prefix) {
[4692]148    MAKE_BINARY(createMatchStar, PabloAST::ClassTypeId::MatchStar, marker, charclass, prefix);
[4622]149    return result;
150}
151
[4443]152PabloAST * PabloBuilder::createScanThru(PabloAST * from, PabloAST * thru) {
[4602]153    MAKE_BINARY(createScanThru, PabloAST::ClassTypeId::ScanThru, from, thru);
154    return result;
[4443]155}
156
[4622]157PabloAST * PabloBuilder::createScanThru(PabloAST * from, PabloAST * thru, const std::string prefix) {
[4692]158    MAKE_BINARY(createScanThru, PabloAST::ClassTypeId::ScanThru, from, thru, prefix);
[4622]159    return result;
160}
161
162
[4602]163PabloAST * PabloBuilder::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr) {
164    MAKE_TERNARY(createSel, PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr);
165    return result;
[4443]166}
167
[4622]168PabloAST * PabloBuilder::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr, const std::string prefix) {
[4692]169    MAKE_TERNARY(createSel, PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr, prefix);
[4622]170    return result;
[4443]171}
[4622]172
173
174}
Note: See TracBrowser for help on using the repository browser.