bluespec.com Forum Index bluespec.com
Bluespec Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Static elaboration: Vector with size as Integer definition?

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ...
View previous topic :: View next topic  
Author Message
chris



Joined: 19 Sep 2011
Posts: 10

PostPosted: Wed Sep 21, 2011 4:16 pm    Post subject: Static elaboration: Vector with size as Integer definition? Reply with quote

Why is it not possible to define a vector with a specific length determined by an Integer?

Code:
Integer data_len = 25;

Vector#(data_len , UInt#(16)) data= genWith(gen_data);

rule fill_uut(step < fromInteger(data_len));
    uut.put(data[step]);
endrule


I get the following error:

Code:
Error: line "uut.put(data[step]);" : (T0035)
  Bit vector of unknown size introduced near this location.
  Please remove unnecessary extensions, truncations and concatenations and/or
  provide more type information to resolve this ambiguity.


Everything works, when I change the Vector definition to:


Code:
Vector#(25, UInt#(16)) data= genWith(gen_data);



What's the problem with my first approach, shouldn't vector_size be known in static elaboration?


Last edited by chris on Wed Sep 21, 2011 6:46 pm; edited 2 times in total
Back to top
View user's profile Send private message
patil.nikhil



Joined: 17 Aug 2007
Posts: 69
Location: University of Texas at Austin

PostPosted: Wed Sep 21, 2011 5:36 pm    Post subject: Reply with quote

Chris, I don't really know why, but "Integer"s and "numeric type"s are treated as separate entities by the bluespec compiler. Integers cannot be converted to numeric types, but numeric types can be converted to Integers (using valueof). My strategy therefore is to define "constants" like data_len as numeric types rather than Integers. So, here's a workaround for what you are trying to do:

Code:

typedef 25 Data_len;   // note the capital 'D', since it is a type now.

Vector#(Data_len, UInt#(16)) data = genWith(gen_data);

rule fill_uut (step < fromInteger(valueof(Data_len))); // note the "valueof"
    uut.put(data[step]);
endrule
Back to top
View user's profile Send private message Visit poster's website
chris



Joined: 19 Sep 2011
Posts: 10

PostPosted: Wed Sep 21, 2011 6:31 pm    Post subject: Reply with quote

This a very useful, thanks for the hint.
Back to top
View user's profile Send private message
SteveA



Joined: 03 May 2007
Posts: 32

PostPosted: Thu Sep 22, 2011 11:12 am    Post subject: Reply with quote

Patil is correct...

As an added tip, don't forget you can get the Integer version of this also if thats useful (particularly because "fromInteger()" can be used to generate almost every other type, except Num):

typedef 25 Data_len; // note the capital 'D', since it is a type now.
Integer data_len = valueof(Data_len);

That should cover all the various versions/datatypes of data_len that you need (though I'm not a big fan of making case sensitivity the only difference between variable names - there it is anyway)...

In any case, I often do this when defining vectors...

_________________
Steve Allen
Senior Consulting Engineer
Bluespec, Inc
Back to top
View user's profile Send private message
stoy



Joined: 22 Aug 2007
Posts: 17
Location: Bluespec

PostPosted: Mon Sep 26, 2011 10:26 am    Post subject: Reply with quote

May I add my two cents?

As Nikhil says, you can easily find the Integer value corresponding to a numeric type (using valueof), but not the other way round. The reason for this is that the type-checking/type-inference phase of the compiler is over and done with before the evaluator for the static elaboration phase begins its work. This is partly so that the evaluator need not worry about the possibility of type errors. So the types are all worked out before the compiler even starts to look at any of the actual values, and thus type inference can't take any of the Integer values into account.

The best you can do (and it is admittedly inelegant) is shown in the attached example. Each arm of the big conditional at the end instantiates _i with an interface of exactly the same type; so it all typechecks, even though each arm uses a differently sized register. Then later, during static elaboration, one arm is selected and all the others discarded.

joe stoy
Bluespec Inc.



VSizeCounter.bsv
 Description:

Download
 Filename:  VSizeCounter.bsv
 Filesize:  2.71 KB
 Downloaded:  969 Time(s)

Back to top
View user's profile Send private message AIM Address
chris



Joined: 19 Sep 2011
Posts: 10

PostPosted: Tue Sep 27, 2011 11:05 am    Post subject: Reply with quote

Thanks stoy, I have already seen this pattern in the BSVSource. There should really be a more concise form to auto-generate something like that as it is needed surprisingly often.
Back to top
View user's profile Send private message
gururaj



Joined: 31 Oct 2012
Posts: 19

PostPosted: Sat Nov 10, 2012 11:15 am    Post subject: Reply with quote

Suppose I have something like
typedef 5 ThreadIDWidth

and I want to do something like
Vector#(numThreads,someDataType) somethingforEveryThread;

where numThreads = 2^ThreadIDWidth (in this case 32)

I could just go ahead and write typedef 32 numThreads, but each time I change my ThreadIDWidth I have to change my numThreads as well, which totally defeats the purpose of a typedef!

Any simple way so that my numThreads gets calculated automatically when I change ThreadIDWidth?

I may be missing something basic since I am new to Bluespec. Please bear with a newbie!
Back to top
View user's profile Send private message
stoy



Joined: 22 Aug 2007
Posts: 17
Location: Bluespec

PostPosted: Sat Nov 10, 2012 12:29 pm    Post subject: Reply with quote

Yes. You need
typedef TExp#(ThreadIDWidth) NumThreads;
Note the capital N for NumThreads.

See Section B.3.2 of the Reference Guide.

joe stoy
Back to top
View user's profile Send private message AIM Address
gururaj



Joined: 31 Oct 2012
Posts: 19

PostPosted: Sat Nov 10, 2012 2:48 pm    Post subject: Reply with quote

That just made my life a little bit easier.
Thanks Joe!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ... All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
bluespec.com topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP