cjdrake / pyeda

Python EDA
BSD 2-Clause "Simplified" License
304 stars 55 forks source link

Overhaul operators #53

Closed cjdrake closed 10 years ago

cjdrake commented 10 years ago

Currently, pyeda uses the following operators for the following purposes:

Variables and Functions:

BitVectors:

Problems:

  1. No symbolic XOR operator for Boolean variables
  2. Cannot use -+* for arithmetic on Boolean vectors
  3. Cannot use >> and << for shift on Boolean vectors

pyeda so far has an emphasis on Boolean algebra, and not higher-level arithmetic. If you decide to use the + operator for addition between two vectors, there isn't enough information to decide what type of adder (ie RCA, LCA, etc) you want to use. I can envision creating some kind of "black box" object that behaves like a Bitvector, but requires you to define the behavior abstractly. That might be useful for RTL simulation purposes, because you really only care about the correctness of the arithmetic, and not whether it's implemented using Brent-Kung. Doing this will make it obvious that the -+*% symbols can only be used for abstract arithmetic.

Python has only >> and <<, because integers do not have a fixed size, and all shifts are arithmetic. For Bitvectors, if you want to use symbols at all, you really need to differentiate between right shift (rsh) and arithmetic right shift (arsh). So until Python grows an overloadable "right arrow" operator, using them for implication seems reasonable.

The biggest issue by far is the lack of a symbol for binary XOR on functions. Even though you only need NOT/OR/AND for a complete Boolean algebra, XOR is extremely useful. Writing (~a&b|a&~b) is very verbose, and other data structures like DDs should have more convenient ways for XOR. So until Python grows another operator that is sufficient, I am afraid the only reasonable solution is to use ~|*^ for both Boolean Function and VectorFunction types. The only thing you lose is the ability to type a - b as a short-hand for a + -b, since a ~ b is not valid Python.

cjdrake commented 10 years ago

Another possible use for + and * operators is concatenation and repetition of BitVector types, respectively. This might seem counter-intuitive, but is probably more useful than arithmetic.