source: icGREP/icgrep-devel/icgrep/pablo/carry_data.cpp @ 4682

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

Carry Manager progress and bug fix

File size: 5.3 KB
Line 
1/*
2 *  Copyright (c) 2015 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
8#include <include/simd-lib/bitblock.hpp>
9#include <pablo/pablo_compiler.h>
10#include <pablo/codegenstate.h>
11#include <pablo/carry_data.h>
12#include <iostream>
13
14namespace pablo {
15
16   
17unsigned alignCeiling(unsigned toAlign, unsigned alignment) {
18    return ((toAlign - 1) | (alignment - 1)) + 1;
19}
20
21void EnsurePackHasSpace(unsigned & packedTotalBits, unsigned addedBits) {
22    unsigned bitsInCurrentPack = packedTotalBits % PACK_SIZE;
23    if ((bitsInCurrentPack > 0) && (bitsInCurrentPack + addedBits > PACK_SIZE)) {
24      packedTotalBits = alignCeiling(packedTotalBits, PACK_SIZE);
25    }
26}
27
28unsigned PabloBlockCarryData::enumerate(PabloBlock & blk) {
29    for (Statement * stmt : blk) {
30        if (Advance * adv = dyn_cast<Advance>(stmt)) {
31            unsigned shift_amount = adv->getAdvanceAmount();
32            if (shift_amount == 1) {
33                adv->setLocalAdvanceIndex(advance1.entries);
34                advance1.entries++;               
35            }
36            else if (shift_amount < LongAdvanceBase) {
37#ifdef PACKING
38                EnsurePackHasSpace(shortAdvance.allocatedBits, shift_amount);
39                adv->setLocalAdvanceIndex(shortAdvance.allocatedBits);
40#else
41                adv->setLocalAdvanceIndex(shortAdvance.entries);
42#endif
43                shortAdvance.entries++;
44                shortAdvance.allocatedBits += shift_amount;
45            }
46            else {
47                adv->setLocalAdvanceIndex(longAdvance.allocatedBitBlocks);
48                longAdvance.entries++;
49                longAdvance.allocatedBitBlocks += longAdvanceBufferSize(shift_amount);
50            }
51        }
52        else if (MatchStar * m = dyn_cast<MatchStar>(stmt)) {
53            m->setLocalCarryIndex(addWithCarry.entries);
54            ++addWithCarry.entries;
55        }
56        else if (ScanThru * s = dyn_cast<ScanThru>(stmt)) {
57            s->setLocalCarryIndex(addWithCarry.entries);
58            ++addWithCarry.entries;
59        }
60    }
61    longAdvance.frameOffsetinBits = 0;
62#ifdef PACKING
63    shortAdvance.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
64    addWithCarry.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.allocatedBits;
65    EnsurePackHasSpace(addWithCarry.frameOffsetinBits, addWithCarry.entries);
66    advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries;
67    EnsurePackHasSpace(advance1.frameOffsetinBits, advance1.entries);
68    nested.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries;
69#else
70    shortAdvance.frameOffsetinBits = longAdvance.frameOffsetinBits + longAdvance.allocatedBitBlocks * BLOCK_SIZE;
71    addWithCarry.frameOffsetinBits = shortAdvance.frameOffsetinBits + shortAdvance.entries * BLOCK_SIZE;
72    advance1.frameOffsetinBits = addWithCarry.frameOffsetinBits + addWithCarry.entries * BLOCK_SIZE;
73    nested.frameOffsetinBits = advance1.frameOffsetinBits + advance1.entries * BLOCK_SIZE;
74#endif
75    unsigned nestedframePosition = nested.frameOffsetinBits;
76   
77    for (Statement * stmt : blk) {
78        if (If * ifStatement = dyn_cast<If>(stmt)) {
79            PabloBlockCarryData & nestedBlockData = ifStatement->getBody().carryData;
80            nestedBlockData.ifDepth = ifDepth + 1;
81            nestedBlockData.whileDepth = whileDepth;
82            const unsigned ifCarryDataBits = nestedBlockData.enumerate(ifStatement->getBody());
83#ifdef PACKING
84            EnsurePackHasSpace(nestedframePosition, ifCarryDataBits);
85#endif
86            nestedBlockData.framePosition = nestedframePosition;
87            nestedframePosition += ifCarryDataBits;
88            if (maxNestingDepth <= nestedBlockData.maxNestingDepth) maxNestingDepth = nestedBlockData.maxNestingDepth + 1;
89            nested.entries++;
90        }
91        else if (While * whileStatement = dyn_cast<While>(stmt)) {
92            PabloBlockCarryData & nestedBlockData = whileStatement->getBody().carryData;
93            nestedBlockData.ifDepth = ifDepth;
94            nestedBlockData.whileDepth = whileDepth + 1;
95            unsigned whileCarryDataBits = nestedBlockData.enumerate(whileStatement->getBody());
96            //if (whileStatement->isMultiCarry()) whileCarryDataBits *= whileStatement->getMaxIterations();
97#ifdef PACKING
98            EnsurePackHasSpace(nestedframePosition, whileCarryDataBits);
99#endif
100            nestedBlockData.framePosition = nestedframePosition;
101            nestedframePosition += whileCarryDataBits;
102            if (maxNestingDepth <= nestedBlockData.maxNestingDepth) maxNestingDepth = nestedBlockData.maxNestingDepth + 1;
103            nested.entries++;
104        }
105    }
106   
107   
108    scopeCarryDataBits = nestedframePosition;
109   
110    if (explicitSummaryRequired()) {
111        // Need extra space for the summary variable, always the last
112        // entry within an if block.
113        scopeCarryDataBits = alignCeiling(scopeCarryDataBits, PACK_SIZE);
114        summary.frameOffsetinBits = scopeCarryDataBits;
115        summary.allocatedBits = PACK_SIZE;
116        scopeCarryDataBits += PACK_SIZE;
117    }
118    else {
119        summary.frameOffsetinBits = 0;
120        summary.allocatedBits = scopeCarryDataBits;
121    }
122    return scopeCarryDataBits;
123}
124
125}
Note: See TracBrowser for help on using the repository browser.