Commit Graph

101 Commits

Author SHA1 Message Date
Lars-Peter Clausen 26d1c72e77 Avoid exponential execution time behavior in arith_expr_type()
arith_expr_type() queries the expression type of its two child nodes up to two
times. Since the child nodes might also need to query their child nodes
expression type to determine their own this can lead to an exponential runtime.

For complex expressions this can easily result in very long elaboration time.

Avoid this by querying the expression type only once for each child node.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-07-12 04:43:03 -07:00
Lars-Peter Clausen bc3cb04a41 Set correct type for indexed array properties
For indexed array properties the type of the expression is the type of the
element.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-17 11:43:05 -07:00
Cary R 44bc9cba03
Merge pull request #952 from larsclausen/2state-arith
Make result of binary operations 2-state if inputs are 2-state
2023-06-17 07:15:43 -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
Lars-Peter Clausen 3fc6ab5afc Replace assert() with ivl_assert() where line information is available
`ivl_assert()` is similar to `assert()` except that it will also include
source file and line information about the expression for which the assert
was triggered.

Use `ivl_assert()` instead of `assert()` where the line information is
available. This will generate better bug reports and make it easier to
diagnose why an assert is triggered.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-16 05:06:15 -07:00
Lars-Peter Clausen f63a162329 Provide data type for more NetExpr subclasses
There are a few NetExpr subclasses where the data type of the expression
is known, but it not attached to the NetExpr and only kept as a private
member in the subclass.

Attaching the type directly to the NetExpr allows to query it externally
and implement better type checking.

It also allows to remove a bit of duplicated code in the subclasses and
rely on the default implementation in the NetExpr base class.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 11:31:22 -08:00
Lars-Peter Clausen 05d5c9b35f Provide default implementation of NetExpr::enumeration()
The current NetExpr::enumeration() always returns a nullptr.
The NetExpr class has a ivl_type_t member that represents
the type of the expression.

Provide a default implementation of NetExpr::enumeration() that
casts this type to the netenum_t type. This will allow
to share this implementation between subclasses and remove
a bit of duplicated code.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 11:31:22 -08:00
Lars-Peter Clausen c0adbd0deb Add support for handling `super` keyword
SystemVerilog allows to use the `super` keyword to access properties and
methods of a base class. This is useful if there is for example an
identifier with the same name in the current class as in the base class and
the code wants to access the base class identifier.

To support this a bit of refactoring is required. Currently properties are
internally referenced by name, this does not work if there are multiple
properties of the same. Instead reference properties always by index.

In addition when looking up an identifier that resolves to an object return
both the type and the object itself. This is necessary since both `this`
and `super` resolve to the same object, but each with a different type.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-25 09:19:21 -08:00
Lars-Peter Clausen 55a5ba3d5a Remove unnecessary overrides of `NetExpr::has_width()`
The default implementation of the virtual method `NetExpr::has_width()`
returns true. There are a few classes that directly inherit from NetExpr
that override the method with the exact same implementation. Remove these.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-14 13:50:31 +02:00
Lars-Peter Clausen 22cba1073c Handle context signedness and width expansion for method return types
The signedness of an expression can change depending on its context. E.g.
for an arithmetic operation with one unsigned operand all operands are
treated as unsigned.

For methods, both on built-in SystemVerilog types as well as user defined
classes, this is currently not considered. This can lead to incorrect behavior
if the value is sign extended.

E.g. the following will print 4294967295 rather than 65535.

```
shortint q[$];
q.push_back(-1);
$display(q.pop_front() + 32'h0);
```

Furthermore the return value is not expanded to the width of its context.
This can cause vvp to crash with an exception when it expects a vector on
the stack to have a certain width.

E.g.
```
int d[];
longint x;
bit a = 1'b1;
x = a ? d.size() : 64'h0;
```

Solve both of this by using `pad_to_width()` on the method return value if
it is a vectorable type. Since any function call, not just methods, needs
to have this done to its return value insert this on the common path.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-14 11:55:09 +02:00
Lars-Peter Clausen 156f08a1c5 Set default signedness of return types for built-in methods of SV types
The result for the built-in methods for the SystemVerilog types is
currently always unsigned. This can lead to incorrect behavior if the value
is sign extended or passed as an argument to a system function (e.g.
$display).

For most built-in methods this does not matter, since even though they have
a signed return type, they will not return a negative value. E.g. the
string `len()` or queue `size()` functions.

It does make a difference though for the queue `pop_front()` and
`pop_back()` methods. Their return type is the element type of the queue.
If the element type is signed and the value in queue is negative is will be
handled incorrectly.

E.g. the following will print `4294967295` rather than `-1`.
```
int q[$];
q.push_back(-1);
$display(q.pop_front());
```

To correctly support this consistently assign the actual data type of the
built-in method's return value to the `NetESFunc`, rather than just the width
and base type. The width, base type and also the signedness can be derived
from the data type.

Note that this only fixes the default signedness, but not the case where
the signedness of the expression is changed by its context (e.g. in
arithmetic expression). Handling this will require some additional work.

Also note that assigning the actual data type is also required to support type
checking on the return value, e.g. as needed for enum types.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-14 10:43:59 +02:00
Lars-Peter Clausen ad23b08a10 Constify scope reference in NetEConstParam and NetECRealParam
Parameter expressions need to remember the scope they have been declared in
so that the code generator backends can insert the right parameter
reference, rather than a constant value.

Currently the scope is stored as a non-const reference. But that is not
needed. Mark the scope reference as const so NetEConstParam and
NetECRealParam can be created when only a const scope reference is
available.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-02-19 13:00:12 +01:00
Lars-Peter Clausen 9b7c99b8a8 NetEConstEnum: Remove unused scope_ field
The scope_ field of the NetEConstEnum class is initialized in the
constructor, but never used anywhere again. Remove it.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-17 20:21:28 +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
Stephen Williams c335c68973 Fix compilation of enum.name method.
The implementation was mostly there, but elaboration failed
due to some obsolete assumtions.
2020-12-30 13:41:08 -08:00
Stephen Williams a286764c1d Add support for string parameters
Parameters can have string type and do the usual string stuff,
and also implement some of the string methods on string parameters
so that they evaluate down to constants.
2020-12-27 21:17:57 -08:00
Martin Whitaker 0fada92389 Fix expression type for packed struct member access (GitHub issue #386)
A NetESelect is used for accessing packed struct members and also for
accessing dynamic array elements. In these cases the expr_type() and
enumeration() methods should reflect the member/element type.
2020-11-20 16:50:11 +00: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
Stephen Williams ea4b000be6 Get arrayed property expressions down to the ivl_target API. 2014-09-15 17:37:30 -07:00
Stephen Williams c9ff48bd4e Add support for dynamic array/queue "last" index ($)
Internally, treat the "$" as a special expression type that takes
as an argument the signal that is being indexed. In the vvp target,
use the $last system function to implement this.
2014-08-21 16:44:45 -07:00
Stephen Williams a3b29dd70b Handle enumeration literals that are in $root. 2014-01-11 19:19:14 -08:00
Stephen Williams 3945b9df45 Elaborate darray new element initializer to ivl_target API 2013-10-19 15:34:15 -07:00
Stephen Williams 2030e06988 Parse/elaborate some array-patterns down to the ivl_target API. 2013-10-19 15:34:14 -07:00
Stephen Williams 8e559e4e91 Support shallow copy as far as the ivl_target API. 2013-04-27 19:54:13 -07:00
Stephen Williams 20ee350601 Generalize user defined function return type handling.
I'm gonna need functions to return class objects, so generalize
the output types of user defined functions.
2013-04-20 16:38:35 -07:00
Stephen Williams 106850ca7d Handle real value class properties.
As a side effect, this also adds support for 64bit integers.
2013-01-27 20:10:25 -08:00
Stephen Williams 2ea5919a4a Get signed-ness of property correct in certain situations. 2013-01-27 20:10:25 -08:00
Stephen Williams 860419a346 Draft run-time support for SystemVerilog class objects.
This provides the ivl_target.h interface for class definitions
and expressions, the vvp code generator support for class objects
and properties, and the vvp run time support. Trivial class objects
now seem to work.
2012-12-10 19:20:02 -08:00
Stephen Williams 318a4033b8 Flesh out class type elaboration
Add properties to the classes, and elaborate expressions that
have class properties. Describe class object property references
all the way down to the stub target.
2012-12-10 19:20:02 -08:00
Stephen Williams b6eb86d696 Blend new_darray and new_class expression nodes. 2012-12-10 19:13:43 -08:00
Stephen Williams 7a2ad01f2e Class new expressions, down to the ivl_target.h API. 2012-12-10 19:13:43 -08:00
Stephen Williams 77d24cd095 Elaborate class_new and (null) expressions
This gets the types right for class_new and null expressions, and
elaborate them down to the ivl_target.h API.
2012-12-10 19:13:43 -08:00
Stephen Williams 0059fb1ec7 Support for dynamic arrays of strings.
Strings, when put into dynamic arrays, are treated as first class
types much line reals. Add the code generator and vvp support for
this situation. Also fix a bug distinguishing between character
selects from strings and select form arrays of strings.
2012-10-14 17:16:47 -07: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
Stephen Williams a2d980540d Get some type information to the "new" expression for darrays. 2012-10-14 17:16:47 -07:00
Stephen Williams 0bdabab4fb Rework packed dimensions handling
Make packed structs more obviously parts of a vector.
Handle arrays of structs in certain cases.
2012-09-03 16:00:10 -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 312b4da46f Expression width rework.
This patch is a major rework of expression elaboration and
evaluation in the compiler, aimed at better compliance with
the IEEE standard.
2011-03-01 18:13:26 -08:00
Cary R a6220fe562 Add support for passing variable indexed part select type information
This patch modifies the compiler and the ivl interface to pass the
type of indexed part select that is being represented (up/down) in
a procedural L-value or R-value. This is needed by any back end that
wants to correctly denormalize the (zero based) base expression.
2011-02-28 19:15:48 -08:00
Cary R 629ee5b899 Fix a number of file/line issues in the compiler.
This patch fixes a bunch of objects to have the correct file/line
information. It also adds support for getting file/line information
for events (named events have a definition line).
2011-02-10 19:10:21 -08:00
Martin Whitaker b89ab1f2b0 Cleanup after parameter expression rework.
This patch removes some code made redundant by the rework of
parameter expression evaluation. It also documents the new
-g option.
2010-12-06 14:56:59 -08:00
Stephen Williams de215f1f8d Describe enum type to code generators
This gets the enumeration type through to the ivl_target API so
that code generators can do something with it. Generate stub
output with tgt-stub, and generate the proper vvp run time to
make simple enumerations work from end to end.
2010-11-20 15:09:32 -08:00
Stephen Williams e1344745f8 Handle system functions that return enumerations.
There are a few internal system functions that return enumeration
values, and the type checker needs to properly account for that.
2010-11-07 15:02:42 -08:00
Stephen Williams 5e2c0e5d04 Better handling of width of enum literals in expressions. 2010-11-07 09:58:00 -08:00
Stephen Williams cd6c6c7a70 Get the netenum_t base type data from the pform.
The pform propagates the parsed enum base type information
to the elaborator so that the base type can be fully elaborated.
This is necessary to get the types of the enumeration literals
correct.
2010-11-03 20:11:19 -07:00
Stephen Williams 5b5a6b05b7 Test type correctness during elaboration.
Create a netenum_t class to carry the enumeration type in the
netlist.h structures, and use that type to check enumerations
with assignments.
2010-11-02 20:16:42 -07:00
Stephen Williams a14fa038b4 Enum names in r-value expressions
When enum names are used as r-values in expressions, use their
values. Treat the enum names similar to (but not exactly as)
localparams so that they fit into the rest of the elaboration
flow naturally.
2010-10-31 11:26:09 -07:00
Stephen Williams ec49f10e2d Revert bad merge from vhdl branch 2010-10-02 11:02:27 -07:00
Cary R e98000426e Fix power operator width in self-determined context.
In a self determined context the width of the power operator
is defined to be the left argument width.
2009-07-28 19:52:10 -07:00