-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides reuseable decorator and function to handle magma's implicit coercion of Python values. For now, I moved the logic from wiring and mux to a shared place to ensure consistency. There's probably other places to do this, but we can do this on demand as we enounter incosistencies. One issue is that `int` coercion can't really happen up front like this since it is determined by the place it's used (i.e. when it's wired to a Bits we check it can fit in the bit width). @rsetaluri perhaps we could use SmartBits for this? (convert it to a smartbits that's automatically extended to wire to a regular bits, but I think right now our rules don't allow implicit truncation, but we could define a special SmartBits subclass that only extends. My one concern would be whether this would make the error a bit harder to traceback) Fixes #828
- Loading branch information
Showing
8 changed files
with
57 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from functools import wraps | ||
|
||
import hwtypes as ht | ||
|
||
from magma.protocol_type import MagmaProtocol | ||
from magma.debug import debug_info | ||
|
||
|
||
def python_to_magma_coerce(value): | ||
if isinstance(value, debug_info): | ||
# Short circuit tuple converion | ||
return value | ||
|
||
# Circular import | ||
from magma.conversions import tuple_, sint, uint, bits, bit | ||
if isinstance(value, tuple): | ||
return tuple_(value) | ||
if isinstance(value, ht.SIntVector): | ||
return sint(value, len(value)) | ||
if isinstance(value, ht.UIntVector): | ||
return uint(value, len(value)) | ||
if isinstance(value, ht.BitVector): | ||
return bits(value, len(value)) | ||
if isinstance(value, (bool, ht.Bit)): | ||
return bit(value) | ||
|
||
if isinstance(value, MagmaProtocol): | ||
return value._get_magma_value_() | ||
|
||
return value | ||
|
||
|
||
def python_to_magma_coerce_wrapper(fn): | ||
@wraps(fn) | ||
def wrapper(*args, **kwargs): | ||
args = (python_to_magma_coerce(a) for a in args) | ||
kwargs = {k: python_to_magma_coerce(v) for k, v in kwargs.items()} | ||
return fn(*args, **kwargs) | ||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters