cornell-zhang / heterocl

HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Heterogeneous Computing
https://cornell-zhang.github.io/heterocl/
Apache License 2.0
322 stars 92 forks source link

[API] Remove confusing reversed slicing and add new `.reverse()` API #443

Open chhzh123 opened 2 years ago

chhzh123 commented 2 years ago

The bit slicing API was introduced in #291 , but it is confusing when it allows reversed slicing, since the ranges in Python are all left-closed right-open, while the reversed slicing breaks this rule. To avoid confusion, I think we should prevent using reversed slicing and force the begin of the slice smaller than the end of the slice. This is also consistent with other hardware-oriented Python DSLs like PyMTL3 (See Page 8 of the documentation).

For reversing the sliced bits, we need to introduce a new API .reverse(). The grammar and behavior is like the one described in the VHLS documentation. Therefore, we have the following example:

>>> a = 0xabcd0123
>>> a[28:32] # containing the bit of 28, 29, 30, 31
0xa
>>> a[4:24]
0xcd012
>>> a[28:32].reverse()
0x5

And we no longer support grammar like a[32:28] which leads to confusion.