source: icGREP/icgrep-devel/icgrep/pablo/codegenstate.cpp @ 4415

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

More work on usedef info.

File size: 6.5 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <pablo/codegenstate.h>
8
9namespace pablo {
10
11/// UNARY CREATE FUNCTIONS
12
13PabloAST * PabloBlock::createAdvance(PabloAST * expr, const int shiftAmount) {
14    if (isa<Zeroes>(expr) || shiftAmount == 0) {
15        return expr;
16    }
17    return insertIfNew(mUnaryWithInt.findOrMake<Advance>(PabloAST::ClassTypeId::Advance, expr, shiftAmount, mSymbolGenerator, this));
18}
19
20Assign * PabloBlock::createImmutableAssign(const std::string prefix, PabloAST * expr, const int outputIndex) {
21    return insertIfNew(mUnary.findOrMake<Assign>(PabloAST::ClassTypeId::Assign, expr, outputIndex, mSymbolGenerator->make(prefix), this));
22}
23
24Call * PabloBlock::createCall(const std::string name) {
25    return createCall(mSymbolGenerator->get(name));
26}
27
28Call * PabloBlock::createCall(String * name) {
29    return insertIfNew(mUnary.findOrMake<Call>(PabloAST::ClassTypeId::Call, name, this));
30}
31
32PabloAST * PabloBlock::createNot(PabloAST * expr) {
33    return mUnary.findOrCall<OptimizeNot>(PabloAST::ClassTypeId::Not, expr, this);
34}
35
36Not * PabloBlock::createNotImm(PabloAST * expr) {
37    return insertIfNew(mUnary.findOrMake<Not>(PabloAST::ClassTypeId::Not, expr, this));
38}
39
40Var * PabloBlock::createVar(const std::string name) {
41    return createVar(mSymbolGenerator->get(name));
42}
43
44Var * PabloBlock::createVar(String * name) {
45    return mUnary.findOrMake<Var>(PabloAST::ClassTypeId::Var, name, this).first;
46}
47
48/// BINARY CREATE FUNCTIONS
49
50Next * PabloBlock::createNext(Assign * assign, PabloAST * expr) {
51    return insertIfNew(mBinary.findOrMake<Next>(PabloAST::ClassTypeId::Next, assign, expr, this));
52}
53
54PabloAST * PabloBlock::createMatchStar(PabloAST * marker, PabloAST * charclass) {
55    if (isa<Zeroes>(marker) || isa<Zeroes>(charclass)) {
56        return marker;
57    }
58    return insertIfNew(mBinary.findOrMake<MatchStar>(PabloAST::ClassTypeId::MatchStar, marker, charclass, mSymbolGenerator, this));
59}
60
61PabloAST * PabloBlock::createScanThru(PabloAST * from, PabloAST * thru) {
62    if (isa<Zeroes>(from) || isa<Zeroes>(thru)) {
63        return from;
64    }
65    return insertIfNew(mBinary.findOrMake<ScanThru>(PabloAST::ClassTypeId::ScanThru, from, thru, mSymbolGenerator, this));
66}
67
68PabloAST * PabloBlock::createAnd(PabloAST * expr1, PabloAST * expr2) {
69    if (expr1 < expr2) {
70        std::swap(expr1, expr2);
71    }
72    return mBinary.findOrCall<OptimizeAnd>(PabloAST::ClassTypeId::And, expr1, expr2, this);
73}
74
75And * PabloBlock::createAndImm(PabloAST * expr1, PabloAST * expr2) {
76    return insertIfNew(mBinary.findOrMake<And>(PabloAST::ClassTypeId::And, expr1, expr2, this));
77}
78
79PabloAST * PabloBlock::createOr(PabloAST * expr1, PabloAST * expr2) {
80    if (expr1 < expr2) {
81        std::swap(expr1, expr2);
82    }
83    return mBinary.findOrCall<OptimizeOr>(PabloAST::ClassTypeId::Or, expr1, expr2, this);
84}
85
86Or * PabloBlock::createOrImm(PabloAST * expr1, PabloAST * expr2) {
87    return insertIfNew(mBinary.findOrMake<Or>(PabloAST::ClassTypeId::Or, expr1, expr2, this));
88}
89
90PabloAST * PabloBlock::createXor(PabloAST * expr1, PabloAST * expr2) {
91    if (expr1 < expr2) {
92        std::swap(expr1, expr2);
93    }
94    return mBinary.findOrCall<OptimizeXor>(PabloAST::ClassTypeId::Xor, expr1, expr2, this);
95}
96
97Xor * PabloBlock::createXorImm(PabloAST * expr1, PabloAST * expr2) {
98    return insertIfNew(mBinary.findOrMake<Xor>(PabloAST::ClassTypeId::Xor, expr1, expr2,  this));
99}
100
101/// TERNARY CREATE FUNCTION
102
103PabloAST * PabloBlock::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr) {
104    return mTernary.findOrCall<OptimizeSel>(PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr, this);
105}
106
107Sel * PabloBlock::createSelImm(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr) {
108    return insertIfNew(mTernary.findOrMake<Sel>(PabloAST::ClassTypeId::Sel, condition, trueExpr, falseExpr, this));
109}
110
111PabloAST * PabloBlock::setOperandOf(Statement * inst, const unsigned index, PabloAST * value) {
112
113    assert (index < inst->getNumOperands() && inst->getOperand(index) != value);
114
115    // get the current insertion point so we can restore it later
116    Statement * const ip = getInsertPoint();
117    const bool replacingCurrentIP = (ip == inst);
118
119    // set the current ip to the position of the inst we're "updating"
120    setInsertPoint(inst);
121    // this will move the ip back to inst's prior node
122    inst->removeFromParent();
123
124    PabloAST * retVal = inst;
125
126    switch (inst->getClassTypeId()) {
127        case PabloAST::ClassTypeId::Advance:
128            retVal = createAdvance(value, cast<Advance>(inst)->getAdvanceAmount());
129            break;
130        case PabloAST::ClassTypeId::Not:
131            retVal = createNot(value);
132            break;
133//        case PabloAST::ClassTypeId::Assign:
134//            retVal = createAssign(value, cast<Assign>(inst)->getOutputIndex());
135//            break;
136        case PabloAST::ClassTypeId::Var:
137            retVal = createVar(cast<String>(value));
138            break;
139        case PabloAST::ClassTypeId::Call:
140            retVal = createCall(cast<String>(value));
141            break;
142        case PabloAST::ClassTypeId::And:
143            retVal = createAnd(index == 0 ? value : inst->getOperand(0), index == 1 ? value : inst->getOperand(1));
144            break;
145        case PabloAST::ClassTypeId::Or:
146            retVal = createOr(index == 0 ? value : inst->getOperand(0), index == 1 ? value : inst->getOperand(1));
147            break;
148        case PabloAST::ClassTypeId::Xor:
149            retVal = createXor(index == 0 ? value : inst->getOperand(0), index == 1 ? value : inst->getOperand(1));
150            break;
151        case PabloAST::ClassTypeId::Next:
152            retVal = createNext(cast<Assign>(index == 0 ? value : inst->getOperand(0)), index == 1 ? value : inst->getOperand(1));
153            break;
154        case PabloAST::ClassTypeId::ScanThru:
155            retVal = createScanThru(index == 0 ? value : inst->getOperand(0), index == 1 ? value : inst->getOperand(1));
156            break;
157        case PabloAST::ClassTypeId::MatchStar:
158            retVal = createMatchStar(index == 0 ? value : inst->getOperand(0), index == 1 ? value : inst->getOperand(1));
159            break;
160        case PabloAST::ClassTypeId::Sel:
161            retVal = createSel(index == 0 ? value : inst->getOperand(0), index == 1 ? value : inst->getOperand(1), index == 2 ? value : inst->getOperand(2));
162            break;
163        default:
164            break;
165    }
166
167    // restore our insertion point
168    setInsertPoint(replacingCurrentIP ? inst : ip);
169
170    return retVal;
171}
172
173}
Note: See TracBrowser for help on using the repository browser.