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

Last change on this file since 5652 was 5446, checked in by nmedfort, 2 years ago

Refactoring work + correction for getRawItemPointer

File size: 12.6 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 "codegenstate.h"
8#include <pablo/printer_pablos.h>
9#include <pablo/boolean.h>
10#include <pablo/arithmetic.h>
11#include <pablo/branch.h>
12#include <pablo/pe_advance.h>
13#include <pablo/pe_count.h>
14#include <pablo/pe_infile.h>
15#include <pablo/pe_integer.h>
16#include <pablo/pe_lookahead.h>
17#include <pablo/pe_matchstar.h>
18#include <pablo/pe_ones.h>
19#include <pablo/pe_phi.h>
20#include <pablo/pe_scanthru.h>
21#include <pablo/pe_string.h>
22#include <pablo/pe_var.h>
23#include <pablo/pe_zeroes.h>
24#include <pablo/ps_assign.h>
25#include <pablo/pablo_kernel.h>
26#include <IR_Gen/idisa_builder.h>
27#include <llvm/IR/Module.h>
28#include <llvm/Support/raw_os_ostream.h>
29
30#define CHECK_SAME_TYPE(A, B) \
31    assert ("DIFFERING CONTEXTS" && (&((A)->getType()->getContext()) == &((B)->getType()->getContext()))); \
32    assert ("DIFFERING TYPES" && ((A)->getType() == (B)->getType()))
33
34using namespace llvm;
35
36namespace pablo {
37
38
39Phi * PabloBlock::createPhi(Type * type) {
40    return new (mAllocator) Phi(type, mAllocator);
41}
42
43/// UNARY CREATE FUNCTIONS
44///
45
46Count * PabloBlock::createCount(PabloAST * expr) {
47    IntegerType * const type = getParent()->getSizeTy();
48    return insertAtInsertionPoint(new (mAllocator) Count(expr, makeName("count"), type, mAllocator));
49}
50
51Count * PabloBlock::createCount(PabloAST * const expr, const llvm::StringRef & prefix)  {
52    IntegerType * const type = getParent()->getSizeTy();
53    return insertAtInsertionPoint(new (mAllocator) Count(expr, makeName(prefix), type, mAllocator));
54}
55
56Not * PabloBlock::createNot(PabloAST * expr, String * name) {
57    assert (expr);
58    if (name == nullptr) {
59        name = makeName("not");
60    }
61    return insertAtInsertionPoint(new (mAllocator) Not(expr, name, mAllocator));
62}
63
64Var * PabloBlock::createVar(PabloAST * name, Type * type) {
65    if (type == nullptr) {
66        type = getParent()->getStreamTy();
67    }
68    if (LLVM_UNLIKELY(name == nullptr || !isa<String>(name))) {
69        throw std::runtime_error("Var objects must have a String name");
70    }
71    return mParent->makeVariable(cast<String>(name), type);
72}
73
74InFile * PabloBlock::createInFile(PabloAST * expr, String * name) {
75    assert (expr);
76    if (name == nullptr) {
77        name = makeName("inFile");
78    }
79    return insertAtInsertionPoint(new (mAllocator) InFile(expr, name, mAllocator));
80}
81
82AtEOF * PabloBlock::createAtEOF(PabloAST * expr, String * name) {
83    assert (expr);
84    if (name == nullptr) {
85        name = makeName("atEOF");
86    }
87    return insertAtInsertionPoint(new (mAllocator) AtEOF(expr, name, mAllocator));
88}
89   
90   
91/// BINARY CREATE FUNCTIONS
92
93Advance * PabloBlock::createAdvance(PabloAST * expr, PabloAST * shiftAmount, String * name) {
94    if (name == nullptr) {
95        name = makeName("advance");
96    }
97    return insertAtInsertionPoint(new (mAllocator) Advance(expr, shiftAmount, name, mAllocator));
98}
99
100Lookahead * PabloBlock::createLookahead(PabloAST * expr, PabloAST * shiftAmount, String * name) {
101    if (name == nullptr) {
102        name = makeName("lookahead");
103    }
104    return insertAtInsertionPoint(new (mAllocator) Lookahead(expr, shiftAmount, name, mAllocator));
105}
106
107Extract * PabloBlock::createExtract(PabloAST * array, PabloAST * index, String * name) {
108    assert (array && index);
109    if (name == nullptr) {
110        std::string tmp;
111        raw_string_ostream out(tmp);
112        array->print(out);
113        out << '[';
114        index->print(out);
115        out << ']';
116        name = makeName(out.str());
117    }
118    Type * type = array->getType();
119    if (LLVM_LIKELY(isa<ArrayType>(type))) {
120        type = cast<ArrayType>(type)->getArrayElementType();
121    } else {
122        std::string tmp;
123        raw_string_ostream out(tmp);
124        out << "cannot extract element from ";
125        array->print(out);
126        out << " : not a StreamType or ArrayType";
127        throw std::runtime_error(out.str());
128    }
129    return insertAtInsertionPoint(new (mAllocator) Extract(array, index, name, type, mAllocator));
130}
131
132And * PabloBlock::createAnd(PabloAST * expr1, PabloAST * expr2, String * name) {
133    CHECK_SAME_TYPE(expr1, expr2);
134    if (name == nullptr) {
135        name = makeName("and");
136    }
137    return insertAtInsertionPoint(new (mAllocator) And(expr1->getType(), expr1, expr2, name, mAllocator));
138}
139
140And * PabloBlock::createAnd(Type * const type, const unsigned reserved, String * name) {
141    if (name == nullptr) {
142        name = makeName("and");
143    }
144    return insertAtInsertionPoint(new (mAllocator) And(type, reserved, name, mAllocator));
145}
146
147Or * PabloBlock::createOr(PabloAST * expr1, PabloAST * expr2, String * name) {
148    CHECK_SAME_TYPE(expr1, expr2);
149    if (name == nullptr) {
150        name = makeName("or");
151    }
152    return insertAtInsertionPoint(new (mAllocator) Or(expr1->getType(), expr1, expr2, name, mAllocator));
153}
154
155Or * PabloBlock::createOr(Type * const type, const unsigned reserved, String * name) {
156    if (name == nullptr) {
157        name = makeName("or");
158    }
159    return insertAtInsertionPoint(new (mAllocator) Or(type, reserved, name, mAllocator));
160}
161
162Xor * PabloBlock::createXor(PabloAST * expr1, PabloAST * expr2, String * name) {
163    CHECK_SAME_TYPE(expr1, expr2);
164    if (name == nullptr) {
165        name = makeName("xor");
166    }
167    return insertAtInsertionPoint(new (mAllocator) Xor(expr1->getType(), expr1, expr2, name, mAllocator));
168}
169
170Xor * PabloBlock::createXor(Type * const type, const unsigned reserved, String * name) {
171    if (name == nullptr) {
172        name = makeName("xor");
173    }
174    return insertAtInsertionPoint(new (mAllocator) Xor(type, reserved, name, mAllocator));
175}
176
177Add * PabloBlock::createAdd(PabloAST * expr1, PabloAST * expr2) {
178    CHECK_SAME_TYPE(expr1, expr2);
179    return new (mAllocator) Add(expr1->getType(), expr1, expr2, mAllocator);
180}
181
182Subtract * PabloBlock::createSubtract(PabloAST * expr1, PabloAST * expr2) {
183    CHECK_SAME_TYPE(expr1, expr2);
184    return new (mAllocator) Subtract(expr1->getType(), expr1, expr2, mAllocator);
185}
186
187LessThan * PabloBlock::createLessThan(PabloAST * expr1, PabloAST * expr2) {
188    CHECK_SAME_TYPE(expr1, expr2);
189    IntegerType * const int1Ty = getParent()->getInt1Ty();
190    return new (mAllocator) LessThan(int1Ty, expr1, expr2, mAllocator);
191}
192
193enum class AssignErrorType {
194    TypeMismatch
195    , ReadOnlyVar
196    , NotAVariable
197};
198
199static void reportAssignError(PabloAST * const var, PabloAST * const value, const AssignErrorType type) {
200    std::string tmp;
201    raw_string_ostream out(tmp);
202    out << "Cannot assign ";
203    value->print(out);
204    out << " to ";
205    var->print(out);
206    out << ": ";
207    switch (type) {
208        case AssignErrorType::TypeMismatch:
209            out << "type mismatch ";
210            value->getType()->print(out);
211            out << " vs. ";
212            var->getType()->print(out);
213            break;
214        case AssignErrorType::ReadOnlyVar:
215            var->print(out);
216            out << " is read only";
217            break;
218        case AssignErrorType::NotAVariable:
219            var->print(out);
220            out << " is not a variable";
221            break;
222    }
223    llvm::report_fatal_error(out.str());
224}
225
226Assign * PabloBlock::createAssign(PabloAST * const var, PabloAST * const value) {
227    CHECK_SAME_TYPE(var, value);
228
229    if (LLVM_UNLIKELY(var->getType() != value->getType())) {
230        reportAssignError(var, value, AssignErrorType::TypeMismatch);
231    }
232
233    PabloAST * test = var;
234    for (;;) {
235        if (LLVM_LIKELY(isa<Var>(test))) {
236            if (LLVM_UNLIKELY(cast<Var>(test)->isReadOnly())) {
237                reportAssignError(var, value, AssignErrorType::ReadOnlyVar);
238            }
239            break;
240        } else if (isa<Extract>(test)) {
241            test = cast<Extract>(test)->getArray();
242        } else {
243            reportAssignError(var, value, AssignErrorType::NotAVariable);
244        }
245    }
246
247    return insertAtInsertionPoint(new (mAllocator) Assign(var, value, mAllocator));
248}
249
250MatchStar * PabloBlock::createMatchStar(PabloAST * marker, PabloAST * charclass, String * name) {
251    CHECK_SAME_TYPE(marker, charclass);
252    if (name == nullptr) {
253        name = makeName("matchstar");
254    }
255    return insertAtInsertionPoint(new (mAllocator) MatchStar(marker, charclass, name, mAllocator));
256}
257
258ScanThru * PabloBlock::createScanThru(PabloAST * from, PabloAST * thru, String * name) {
259    CHECK_SAME_TYPE(from, thru);
260    if (name == nullptr) {
261        name = makeName("scanthru");
262    }
263    return insertAtInsertionPoint(new (mAllocator) ScanThru(from, thru, name, mAllocator));
264}
265
266ScanTo * PabloBlock::createScanTo(PabloAST * from, PabloAST * to, String * name) {
267    CHECK_SAME_TYPE(from, to);
268    if (name == nullptr) {
269        name = makeName("scanto");
270    }
271    return insertAtInsertionPoint(new (mAllocator) ScanTo(from, to, name, mAllocator));
272}
273
274AdvanceThenScanThru * PabloBlock::createAdvanceThenScanThru(PabloAST * from, PabloAST * thru, String * name) {
275    CHECK_SAME_TYPE(from, thru);
276    if (name == nullptr) {
277        name = makeName("advscanthru");
278    }
279    return insertAtInsertionPoint(new (mAllocator) AdvanceThenScanThru(from, thru, name, mAllocator));
280}
281
282AdvanceThenScanTo * PabloBlock::createAdvanceThenScanTo(PabloAST * from, PabloAST * to, String * name) {
283    CHECK_SAME_TYPE(from, to);
284    if (name == nullptr) {
285        name = makeName("advscanto");
286    }
287    return insertAtInsertionPoint(new (mAllocator) AdvanceThenScanTo(from, to, name, mAllocator));
288}
289
290If * PabloBlock::createIf(PabloAST * condition, PabloBlock * body) {
291    assert (condition);
292    If * const node = insertAtInsertionPoint(new (mAllocator) If(condition, body, mAllocator));
293    body->setBranch(node);
294    return node;
295}
296
297While * PabloBlock::createWhile(PabloAST * condition, PabloBlock * body) {
298    assert (condition);
299    While * const node = insertAtInsertionPoint(new (mAllocator) While(condition, body, mAllocator));
300    body->setBranch(node);
301    return node;
302}
303
304/// TERNARY CREATE FUNCTION
305
306Sel * PabloBlock::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr, String * name) {
307    CHECK_SAME_TYPE(trueExpr, falseExpr);
308    if (name == nullptr) {
309        name = makeName("sel");
310    }
311    return insertAtInsertionPoint(new (mAllocator) Sel(condition, trueExpr, falseExpr, name, mAllocator));
312}
313
314/** ------------------------------------------------------------------------------------------------------------- *
315 * @brief Create
316 ** ------------------------------------------------------------------------------------------------------------- */
317PabloBlock * PabloBlock::Create(PabloKernel * const parent) noexcept {
318    Allocator & allocator = parent->mAllocator;
319    return new (allocator) PabloBlock(parent, allocator);
320}
321
322/** ------------------------------------------------------------------------------------------------------------- *
323 * @brief insert
324 ** ------------------------------------------------------------------------------------------------------------- */
325void PabloBlock::insert(Statement * const statement) {
326    assert (statement);
327    if (LLVM_UNLIKELY(mInsertionPoint == nullptr)) {
328        if (mFirst) {
329            statement->insertBefore(mFirst);
330        } else {
331            statement->removeFromParent();
332            statement->mParent = this;
333            mFirst = mLast = statement;
334        }
335    } else if (LLVM_LIKELY(statement != mInsertionPoint)) {
336        statement->insertAfter(mInsertionPoint);
337        mLast = (mLast == mInsertionPoint) ? statement : mLast;
338        assert (statement->mPrev == mInsertionPoint);
339    }
340    mInsertionPoint = statement;
341}
342
343/** ------------------------------------------------------------------------------------------------------------- *
344 * @brief eraseFromParent
345 ** ------------------------------------------------------------------------------------------------------------- */
346void PabloBlock::eraseFromParent(const bool recursively) {
347    Statement * stmt = front();
348    while (stmt) {
349        stmt = stmt->eraseFromParent(recursively);
350    }
351}
352
353/** ------------------------------------------------------------------------------------------------------------- *
354 * @brief getPredecessor
355 ** ------------------------------------------------------------------------------------------------------------- */
356PabloBlock * PabloBlock::getPredecessor() const {
357    return getBranch() ? getBranch()->getParent() : nullptr;
358}
359
360/** ------------------------------------------------------------------------------------------------------------- *
361 * @brief print
362 ** ------------------------------------------------------------------------------------------------------------- */
363void PabloBlock::print(raw_ostream & O, const bool expandNested) const {
364    PabloPrinter::print(this, O, expandNested);
365}
366
367}
Note: See TracBrowser for help on using the repository browser.