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

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

Continued refactoring work. PabloKernel? now abstract base type with a 'generatePabloMethod' hook to generate Pablo code.

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