Skip to content

Commit

Permalink
structure array fixes, plus nested structure fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanohar committed May 10, 2023
1 parent d33ddf6 commit cee95f6
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 175 deletions.
275 changes: 100 additions & 175 deletions chpsim.cc

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions chpsim.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct chpsimderef {
Data *d; // used for structures

int offset; // offset / offseti for struct
int stride; // stride for struct arrays
unsigned int isbool:1; // is this a bool type?
unsigned int isenum:1; // is this an enumeration?
int enum_sz; // used for enumerations
Expand Down
196 changes: 196 additions & 0 deletions state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,199 @@ void *ActSimState::allocState (int sz)
list_append (extra_state, s);
return s->space;
}



int expr_multires::_count (Data *d)
{
int n = 0;
Assert (d, "What?");
for (int i=0; i < d->getNumPorts(); i++) {
int sz = 1;
InstType *it = d->getPortType (i);
if (it->arrayInfo()) {
sz = it->arrayInfo()->size();
}
if (TypeFactory::isStructure (it)) {
n += _count (dynamic_cast<Data *>(d->getPortType(i)->BaseType()))*sz;
}
else {
n += sz;
}
}
return n;
}

void expr_multires::_init_helper (Data *d, int *pos)
{
Assert (d, "What?");
for (int i=0; i < d->getNumPorts(); i++) {
int sz = 1;
InstType *it = d->getPortType (i);
if (it->arrayInfo()) {
sz = it->arrayInfo()->size();
}
if (TypeFactory::isStructure (it)) {
while (sz > 0) {
_init_helper (dynamic_cast<Data *>(it->BaseType()), pos);
sz--;
}
}
else if (TypeFactory::isBoolType (it)) {
while (sz > 0) {
v[*pos].setWidth (1);
v[*pos].setVal (0, 0);
*pos = (*pos) + 1;
sz--;
}
}
else if (TypeFactory::isIntType (it)) {
while (sz > 0) {
v[*pos].setWidth (1);
v[*pos].setVal (0, 0);
v[*pos].setWidth (TypeFactory::bitWidth (it));
*pos = *pos + 1;
sz--;
}
}
}
}

void expr_multires::_init (Data *d)
{
if (!d) return;
_d = d;
nvals = _count (d);
MALLOC (v, BigInt, nvals);
for (int i=0; i < nvals; i++) {
new (&v[i]) BigInt;
}
int pos = 0;
_init_helper (d, &pos);
Assert (pos == nvals, "What?");
}

void expr_multires::_fill_helper (Data *d, ActSimCore *sc, int *pos, int *oi, int *ob)
{
Assert (d, "Hmm");
for (int i=0; i < d->getNumPorts(); i++) {
int sz = 1;
InstType *it = d->getPortType (i);
if (it->arrayInfo()) {
sz = it->arrayInfo()->size();
}
if (TypeFactory::isStructure (it)) {
while (sz > 0) {
_fill_helper (dynamic_cast<Data *>(it->BaseType()),
sc, pos, oi, ob);
sz--;
}
}
else if (TypeFactory::isBoolType (it)) {
while (sz > 0) {
if (sc->getBool (*ob)) {
v[*pos].setVal (0, 1);
}
else {
v[*pos].setVal (0, 0);
}
*ob = *ob + 1;
*pos = (*pos) + 1;
sz--;
}
}
else if (TypeFactory::isIntType (it)) {
while (sz > 0) {
v[*pos] = *(sc->getInt (*oi));
*oi = *oi + 1;
*pos = *pos + 1;
sz--;
}
}
}
}

void expr_multires::fillValue (Data *d, ActSimCore *sc, int off_i, int off_b)
{
int pos = 0;
_fill_helper (d, sc, &pos, &off_i, &off_b);
}


BigInt *expr_multires::getField (ActId *x)
{
Assert (x, "setField with scalar called with NULL ID value");
int off = _d->getStructOffset (x, NULL);
Assert (0 <= off && off < nvals, "Hmm");
return &v[off];
}

void expr_multires::setField (ActId *x, BigInt *val)
{
Assert (x, "setField with scalar called with NULL ID value");
int off = _d->getStructOffset (x, NULL);
Assert (0 <= off && off < nvals, "Hmm");

int w = v[off].getWidth();

v[off] = *val;
v[off].setWidth (w);
}

void expr_multires::setField (ActId *x, expr_multires *m)
{
if (!x) {
//m->Print (stdout);
//printf ("\n");
*this = *m;
}
else {
int off = _d->getStructOffset (x, NULL);
Assert (0 <= off && off < nvals, "Hmm");
Assert (off + m->nvals <= nvals, "What?");
for (int i=0; i < m->nvals; i++) {
int w = v[off + i].getWidth ();
v[off + i] = m->v[i];
v[off + i].setWidth (w);
}
}
}


void expr_multires::Print (FILE *fp)
{
fprintf (fp, "v:%d;", nvals);
for (int i=0; i < nvals; i++) {
fprintf (fp, " %d(w=%d):%lu", i, v[i].getWidth(), v[i].getVal (0));
}
}


expr_multires expr_multires::getStruct (ActId *x)
{
InstType *it;
int off, sz;

off = _d->getStructOffset (x, &sz, &it);

if (off == -1) {
warning ("expr_multires::getStruct(): failed for field starting with %s", x->getName());
return *this;
}
if (!TypeFactory::isStructure (it)) {
warning ("expr_multires::getStruct(): not a structure! Failed for field starting with %s", x->getName());
return *this;
}
Data *d = dynamic_cast<Data *> (it->BaseType());
Assert (d, "Hmm");

expr_multires m(d);

Assert (m.nvals == sz, "What are we doing?");

for (int i=off; i < off + sz; i++) {
m.v[i-off] = v[i];
}

return m;
}
1 change: 1 addition & 0 deletions state.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class expr_multires {
void setField (ActId *field, BigInt *v);
void setField (ActId *field, expr_multires *v);
BigInt *getField (ActId *x);
expr_multires getStruct (ActId *x);

BigInt *v;
int nvals;
Expand Down
22 changes: 22 additions & 0 deletions test/96.act
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
deftype struct (bool x, y; int w) { }

defproc test()
{
struct s[100];
int i;
bool qrst;

chp {
i := 0;
s[0].w := 1;
s[0].x := false;
s[1].x := false;
log ("got ", s[0].x, " ", s[0].w);
log ("got ", s[1].x);
s[i].x := true;
s[0].w := s[0].w + 2;
s[i+1].x := true; qrst := true;
log ("got ", s[0].x, " ", s[0].w);
log ("got ", s[1].x)
}
}
46 changes: 46 additions & 0 deletions test/97.act
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
deftype bar(int<3> v_0, v_1, v_2, v_3, v_4, v_5, v_6, v_7,
v_8, v_9, v_10, v_11, v_12, v_13, v_14, v_15,
v_16, v_17, v_18, v_19, v_20, v_21, v_22){}

deftype foo(bar a){}

function abc(bar b) : bar
{
chp {
self.v_0 := b.v_0 ; self.v_1 := b.v_1 ; self.v_2 := b.v_2 ; self.v_3 := b.v_3 ;
self.v_4 := b.v_4 ; self.v_5 := b.v_5 ; self.v_6 := b.v_6 ; self.v_7 := b.v_7 ;
self.v_8 := b.v_8 ; self.v_9 := b.v_9 ; self.v_10 := b.v_10; self.v_11 := b.v_11;
self.v_12 := b.v_12; self.v_13 := b.v_13; self.v_14 := b.v_14; self.v_15 := b.v_15;
self.v_16 := b.v_16; self.v_17 := b.v_17; self.v_18 := b.v_18; self.v_19 := b.v_19;
self.v_20 := b.v_20; self.v_21 := b.v_21; self.v_22 := b.v_22
}
}

function baz(foo f) : foo
{
chp {
self.a := abc(f.a);
[ f.a.v_0 = 0 -> self.a.v_0 := 2
[]else -> self.a.v_0 := 0
]
}
}

defproc test ()
{

foo x;
foo f;
int i;

chp {
i := 0;
*[ i < 10 ->
x := baz(f);
log ("got ", x.a.v_0);
f := x;
i := i + 1
]
}
}
test t;
1 change: 1 addition & 0 deletions test/runs/96.act.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WARNING: test<>: substituting chp model (requested prs, not found)
4 changes: 4 additions & 0 deletions test/runs/96.act.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[ 40] <> got 0 1
[ 40] <> got 0
[ 80] <> got 1 3
[ 80] <> got 1
1 change: 1 addition & 0 deletions test/runs/97.act.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WARNING: test<>: substituting chp model (requested prs, not found)
10 changes: 10 additions & 0 deletions test/runs/97.act.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[ 20] <> got 2
[ 50] <> got 0
[ 80] <> got 2
[ 110] <> got 0
[ 140] <> got 2
[ 170] <> got 0
[ 200] <> got 2
[ 230] <> got 0
[ 260] <> got 2
[ 290] <> got 0

0 comments on commit cee95f6

Please sign in to comment.