Commit Graph

199 Commits

Author SHA1 Message Date
Sylvain Munaut 8ca387d6de eval_tree: Properly support struct fields in get_array_info
Fixes #1032

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
2023-11-16 09:26:01 +01:00
Sylvain Munaut 37eba70ca4 eval_tree: Indent fixes
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
2023-11-16 09:26:01 +01:00
Lars-Peter Clausen 763907b0e5 Add a typedef for `std::vector<netrange_t>`
`std::vector<netrange_t>` is used for signal array dimensions. As such it is
used in quite a few places.

Add a typedef that can be used as a shorthand to refer to it. This helps to
keep lines where this is used from growing to overly long.

The new type is called `netranges_t`.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-09-09 05:50:40 -07:00
Lars-Peter Clausen 17229f99c9 Make result of binary operations 2-state if inputs are 2-state
The are many binary operations where if the two operands are 2-state the
result is guaranteed to be 2-state.

This is true for all arithmetic operation with the exception of division
where division by 0 will always result in 'x even if the inputs are both
2-state.

The same is true for all binary bitwise operators as well as the binary
logical operators.

Having the expression type be 2-state avoids some unnecessary %cast2
instructions that would otherwise get inserted when assigning the result to
a 2-state variable.

E.g without this change the following will result in

```
  int a, b, c;
  b = a + b;
```

will result in

```
  %load/vec4 ...;
  %load/vec4 ...;
  %add;
  %cast2;
  %store/vec4 ...;
```

For binary comparison operators this is already handled.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-16 07:33:20 -07:00
Cary R dc8b7d0184 Cleanup some cppcheck warnings 2022-12-28 00:00:31 -08:00
Lars-Peter Clausen 73f4f4aef7 eval_tree: Use ivl_assert() instead of assert()
Consistently use ivl_assert() instead of assert() in the eval_tree()
implementations. ivl_assert() includes information about which expression
triggered the assert and it makes debugging easier.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-03 10:15:32 +01:00
Lars-Peter Clausen 957e3d482f Short circuit logical operator to constant if possible
If the left-hand side of a logical operator is a constant that causes the
right-hand side to be short-circuited the right-hand side can be discarded
even if it is not constant.

In this case replace the expression by a constant.

E.g.
 * `0 && expr` will be replaced by a constant 0.
 * `1 || expr` will be replaced by a constant 1.
 * `0 -> expr` will be replaced by a constant 1.

Note that it is not possible to replace the expression by a constant if
only the right-hand side is a constant, even when the value of the
expression is constant. The left side still has to be evaluated for side
effects.

E.g. it is known at elaboration that `a++ && 0` will yield 0, but the
increment on `a` has to be executed regardless.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2021-12-30 19:05:18 +01:00
Martin Whitaker ecbbb60fb6 Remove "using namespace std" from compiler header files and fix the fallout. 2021-11-04 16:55:03 +00:00
Cary R 9e3d6cc996 Elaborate alternate constant ternary expressions to check for errors 2021-01-29 18:10:03 -08:00
Cary R da7484eea1 Update compiler with suggestions from cppcheck 2021-01-02 14:04:46 -08:00
Cary R 51025149a9 Report operators that cannot be used with null/class vars 2020-12-27 19:05:49 -08:00
Cary R c003bcc59a Add support for <-> in constant and procedural contexts 2020-07-07 23:29:19 -07:00
Martin Whitaker 1069a0ef02 Don't evaluate built-in system functions if they are overridden.
We don't support evaluating user-defined system functions at compile
time. If possible, defer evaluation until run time. If used in a
constant expression, output a "sorry" message.
2019-10-19 16:12:17 +01:00
Martin Whitaker f1608e163f Fix implicit fallthrough warnings when building with recent GCC. 2018-10-06 20:15:42 +01:00
Cary R e6f957b0b6 Add support for a constant $countbits() function 2018-09-03 15:58:57 -07:00
Cary R 1da9b2cea0 Add support for constant $countones, $isunknown, $onehot, $onehot0 2018-09-03 08:32:41 -07:00
Cary R 3fc9ad2db0 Add support for the wild compare operators ==? and !=? 2017-11-17 19:32:50 -08:00
Cary R d23b046203 Update the enumeration methods to set their width test attributes correctly 2017-11-17 13:09:14 -08:00
Stephen Williams e56e52bc42 Merge branch 'master' of github.com:steveicarus/iverilog 2016-09-19 12:59:41 -07:00
Maciej Suminski 3710d35dd1 Added missing brackets 2016-09-19 12:53:06 -07:00
Cary R 5853e32f82 Fix some cppcheck warning issues 2016-09-13 23:45:25 -07:00
Maciej Suminski f0fd73e146 ivl: Array querying functions ($left, $low, etc.) for localparams. 2015-06-24 23:53:33 +02:00
Stephen Williams da63ef02d4 Implement the size method for queues.
This works by translating it to a $size() system function call.
The $size function is already implemented for dynamic queues and
it is easy enough to expand it for queues.
2014-08-21 16:44:45 -07:00
Martin Whitaker bc9382eea3 Fix for GitHub issue 20 : confused about signed/unsigned in strange ?: example.
The root cause was that NetESignal::dup_exr() was not copying the calculated
type (signed/unsigned) of the expression.

In passing, found and fixed a similar issue when calculating a blended value
for a constant ternary expression.
2014-03-06 19:35:25 +00:00
Martin Whitaker 065c48527c Fix for GitHub issue 19 : incorrect handling of large shift values.
For shift operations evaluated at compile time, the compiler was converting
the right operand to a native unsigned long value. If the operand exceeded
the size of an unsigned long, excess bits were discarded, which could lead
to an incorrect result.

The fix I've chosen is to add an as_unsigned() function to the verinum class
which returns the maximum unsigned value if the internal verinum value is
wider than the native unsigned type. This then naturally gives the correct
result for shifts, as the verinum bit width is also an unsigned value.

I've changed the as_ulong() and as_ulong64() functions to do likewise, as
this is more likely to either give the correct behaviour or to give some
indication that an overflow has occurred where these functions are used.
2014-02-28 20:39:14 +00:00
Martin Whitaker a3450bf856 Fixes for GitHub issues 13 and 15.
The verinum arithmetic operators now observe the standard Verilog
rules for calculating the result width if all operands are sized.
If any operand is unsized, the result is lossless, as before.
They also now all observe the standard rules for handling partially
undefined operands (if any operand bit is 'x', the entire result is
'x').

I've also added the unary '-' operator, and renamed v_not() to be
the unary '~' operator. This has allowed some simplification in
other parts of the compiler.
2014-02-25 20:39:21 +00:00
Martin Whitaker 5a06602af2 Fix for GitHub issue 14 : Bug in processing 1'b1 >= |1'bx.
During expression evaluation, the compiler attempts to optimise away
relational operations when one side is constant and all possible values
of the other side would result in the relation being true. This is not
a valid optimisation if the other side is a 4-state variable, as an
'x' or 'z' will result in the relation being unknown.
2014-02-18 21:22:19 +00:00
Martin Whitaker ecce1d25bc Fix for GitHub issue 12 : Ternary lval-rval width mismatch.
A simple typing error that caused the expression to be incorrectly
evaluated as a real constant.
2014-02-15 23:05:41 +00:00
Martin Whitaker ac3aee0172 Fix for GitHub issue 8 : Signedness of constant binary bitwise operations.
When doing constant evaluation of binary bitwise operations, the result
value must be signed if the expression type is signed.
2014-02-15 11:39:05 +00:00
Clifford Wolf ef06bf7589 Added missing support for binary ^~ in eval_tree.cc 2013-12-30 11:53:44 +01:00
Cary R 4def0110b5 Fix compile warning. 2013-12-19 09:22:16 -08:00
Cary R 503a1bf6d7 Add support for the array querying function in a constant context
This patchs adds support for the $dimensions() and $unpacked_dimensions()
array functions. Since the argument is only used to get the type information
these functions can always be evaluated at compile time.

For the following functions if the dimension argument is constant or omitted
and the first argument is not dynamic (a string or dynamic array) they will
return the specified information.

  $left(), $right(), $high(), $low(), $increment() and $size()

Dynamic information and a variable second argument will be implement in a
future patch.
2013-12-18 19:04:22 -08:00
Cary R a196af8e15 Rework constant system function code and add $rtoi and $itor 2013-10-11 10:52:02 -07:00
Stephen Williams bcaa6accec Fix evaluation wrong expression for const >= expressions. 2013-06-12 14:09:08 -07:00
Martin Whitaker 1bcd3a97ad Support disable statements and system task calls in constant functions.
This patch implements the evaluate_function method for the NetDisable
and NetSTask classes. It also makes the checks for a function being
constant work when the function contains nested scopes (named blocks).
2013-03-24 14:42:59 -07:00
Martin Whitaker 09f90ab4e1 Add control to enable function evaluation at compile time.
Pure functions with constant operands can be evaluated at compile
time rather than at run time. This patch provides a global control
to enable this optimisation. Until constant function support is
complete, by default it is disabled.
2013-03-24 14:42:05 -07:00
Martin Whitaker e437321ea7 Fix implicit casts in assignments (part 6).
IVL_VT_NO_TYPE is now used to signal an untyped LHS in a parameter
declaration. The parser function that handles specparam declarations
needs to do this too. Also, although it should never happen, make
sure we don't set the expression width in a NetECast object to a
negative number. Make constant evaluation of NetECast objects
observe the expression width.
2013-03-24 13:51:10 -07:00
Martin Whitaker e8d4039175 Fix implicit casts in assignments (part 4).
This patch fixes a few issues/bugs that showed up when testing the
fixes for implicit casts:

1. Make the compile-time implementation of $abs, $min, and $max match
   the run-time behaviour (system functions can't be polymorphic).

2. Correctly set the type (signed/unsigned) of the result of an
   assignment inside a constant user function (the LHS should not
   inherit the type of the RHS).

3. Fix a bug in the verinum(double) constructor (insufficient bits
   were allocated in the case where the double value rounded up to
   the next power of two).
2013-03-24 13:50:59 -07:00
Martin Whitaker 22769afd20 Fix implicit casts in assignments (part 1).
This patch adds support for implicit casts to the elaborate_rval_expr()
function. This will handle the majority of cases where an implicit cast
can occur.
2013-03-24 10:14:07 -07:00
Martin Whitaker 8b70fefa59 Refactor eval_tree debug code.
This just pulls the eval_tree debug message generation into one place.
2013-02-08 11:22:49 -08:00
Martin Whitaker af91920a17 Add support for system function calls in constant functions. 2013-02-08 11:19:26 -08:00
Martin Whitaker 89244e0f70 Add support for concatenation operations in constant functions.
Also fix a compiler crash when attempting to evaluate a function
which has no valid statement(s) (due to earlier compilation errors).
2013-02-08 11:19:17 -08:00
Cary R 5830e65829 Fix space issues. 2013-02-04 12:17:41 -08:00
Martin Whitaker f3a51fdfd1 Factor out some common eval_tree code.
The eval_tree() function is identical in most of the NetEBinary and
NetEUnary sub-classes.
2013-02-04 12:17:29 -08:00
Martin Whitaker afd23d13d5 Add support for unary operators in constant user functions.
This patch implements the evaluate_function method for the NetEUnary
and NetEUReduce classes.
2013-02-04 12:17:05 -08:00
Martin Whitaker 11f479e028 Add support for all binary operators in constant user functions.
This patch implements the evaluate_function method for the NetEBBits,
NetEBDiv, NetEBLogic, NetEBMinMax, and NetEBPow classes. It also
factors out some common code into the NetEBinary class.
2013-02-04 12:16:51 -08:00
Stephen Williams d6efece5dd Handle DARRAYs of real variables
This involves working out the code to get the base type of a select
expression of a darray. Also added the runtime support for darrays
with real value elements.
2012-10-14 17:16:47 -07:00
Arun Persaud f5aafc32f9 updated FSF-address 2012-08-29 10:12:10 -07:00
Stephen Williams f77bdf7e38 Handle concatenation of SystemVerilog strings. 2012-07-22 10:52:06 -07:00
Martin Whitaker 3354d83391 Support if statements and function calls in constant functions. 2012-06-06 19:34:08 -07:00