Commit Graph

643 Commits

Author SHA1 Message Date
Cary R. 00325f3efb
Merge pull request #1348 from jotego/interface-ports
Support SystemVerilog interface-typed module ports
2026-05-16 15:33:56 -07:00
Cary R. fb6dfebcec
Merge pull request #1349 from larsclausen/new-array-init-cleanup
elab: Use common new array initializer elaboration
2026-05-10 15:24:56 -07:00
Lars-Peter Clausen fde4ef85c1 elab: Use common new array initializer elaboration
here are two separate paths `PENewArray::elaborate_expr()`, one for
assignment patterns and one for everything else.

But since since commit 5ca058bfb ("Add support for darray initialisation
from another darray"). The two paths have been effectively the same.

Both call `elaborate_expr()` on the init values with the same parameters.
The only difference is the regular path casts the type to `netarray_t`, but
that doesn't really do anything since it gets passed to a function that
takes a `ivl_type_t`, so is immediately cast back to the base type.

The comment on the regular path is also outdated since it still refers to
the tpre 5ca058bfb code.

Remove the branching and route it through the same path.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2026-05-10 14:59:44 -07:00
Cary R. 8c7f3452c5
Merge pull request #1347 from larsclausen/enum-const-width-cleanup
elab: Remove redundant enum parameter width handling
2026-05-10 14:49:33 -07:00
Cary R 5240790480 Fix/update latest cppcheck issues 2026-05-10 14:47:40 -07:00
Jose Tejada c963809709 feat(sv): support interface-typed module ports 2026-05-10 14:45:33 +02:00
Lars-Peter Clausen 71ce460caa elab: Remove redundant enum parameter width handling
`PEIdent::test_width_parameter_()` has a special case for
`NetEConstEnum` that queries the enum base type directly. This was needed
when enum constants kept their enum type separately from the `NetExpr`
type.

Commit f63a162329 ("Provide data type for more NetExpr subclasses") made
`NetEConstEnum` attach the enum type to the `NetExpr`. The generic
parameter width path now gets the same type, width and signedness as the
special case.

Remove the redundant special case and use the common path for enum
constants as well.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2026-05-09 19:05:18 -07:00
Lars-Peter Clausen 272cf91eae Support assignment of string literals to byte arrays
SystemVerilog defines a special case that allows to assign string literals
to byte arrays. Each character of the string is copied to 1 element of the
byte array.

The size of string literal and the byte array does not have to match. If
the string literal is longer it is truncated. If it is shorter it will be
padded with null-bytes.

The assignment is done left aligned, the first character ends up in the
left most entry of the array. This means the order will differ whether the
array is declared with ascending or descending element order.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2026-05-03 19:48:38 -07:00
Cary R c172a0d3a7 More cppcheck cleanup 2026-01-05 18:59:08 -08:00
Cary R d87dbb08cf cppcheck updates 2025-11-23 01:31:14 -08:00
Cary R cc496c3cf3 More ivl cppcheck cleanup 2025-10-23 10:01:06 -07:00
Cary R 860761f9c6 More cppcheck fixes - part 2 2025-10-20 23:54:15 -07:00
Cary R c7d37bcc21 Error when trying to elaborate a field of a simple variable 2025-07-16 23:37:14 -07:00
Martin Whitaker 0ecb71625b Support assignment of parray slices (issue #1256)
The existing elaboration code only allowed assignments from/to individual
elements and either failed an assertion (when assigning the entire array)
or failed to compile (when assigning an array slice).
2025-07-05 18:02:40 +01:00
Lars-Peter Clausen 4c03ac5b36 Reject invalid casts to real
Only vector types can be cast to real. Report an error when trying to cast
a different type instead of triggering an assert later on.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2025-01-12 19:51:59 -08:00
Dag Lem ba7da9d5a5 Guard against overflow / wrap around of internal part-select bit address
Internally, the maximum address space of a vector is 31 bits + a sign bit
to signal invalid addresses (out of bounds or has one or more x or z bits).

This commit ensures that unsigned part-select bit addresses which would
otherwise overflow and wrap around within this address space are correctly
handled as out of bounds.
2024-09-16 23:50:24 +02:00
Martin Whitaker 0136db7ee7 Fix the return type of the $bits system function (issue #1163)
Early editions of the SystemVerilog standard did not specify the return
type for $bits, so we made it 32 bit unsigned 2-state. Later editions
state the return type is integer (32 bit signed 4-state), so make it so.
2024-09-07 17:52:28 +01:00
Martin Whitaker f3092bba93 Correctly identify scopes for constant function calls (issue #1141)
A constant function call may be inside a named block, so we need to
search upwards to find the enclosing module before checking that the
called function is local to that module.

SystemVerilog allows constant function calls to reference functions
in (other) packages or in the $unit scope, so extend the checks to
permit that.
2024-06-30 11:50:59 +01:00
Martin Whitaker f08ff895af Add informational messages that point to declaration after use. 2024-02-25 16:12:31 +00:00
Martin Whitaker 649fbb9a59 Modify symbol_search() to only return declared nets and named events.
This only applies to simple identifiers. Only return a match if the
lexical position of the identifier being searched is later in the
source text than the lexical position of a matching symbol.
2024-02-19 18:19:55 +00:00
Cary R 7c25e8506c
Merge pull request #1060 from larsclausen/ident-test-width
Correctly calculate width of nested path identifiers
2024-01-01 12:26:51 -08:00
Lars-Peter Clausen ca69665b88 Correctly calculate width of nested path identifiers
The current `PEIdent::test_width()` method is only able to calculate width
of a path with up to two elements.

For more complex paths it will not be able to calculate the width. E.g.
 * Nested struct member access
 * function call of a enum member in a struct

To make nested structures work properly walk the whole path tail element
by element updating the type along the way. Also take the indices into
account and update the type if an arrays dimensions have been fully
consumed.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-12-30 18:30:24 -08:00
Lars-Peter Clausen 131e64c53c Detect reversed part select on inner dimensions
The order of the indices of a part select need to match the order in which
the dimension of a packed array has been declared. E.g. if the msb is less
than the lsb in the declaration it also has to be for the part select.

If the order of the part select is the opposite of the declaration this is
an error. This works as expected for part selects on the most outer
dimensions.

But for inner dimensions the current implementation just swaps the msb and
lsb of the part select if they are in the wrong order.

Refactor this so that an error is reported for both the outer and inner
dimensions.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-12-29 16:34:46 -08:00
Lars-Peter Clausen e7e663056a Handle invalid vector slice base expressions
The elaboration of the base expression of a vector slice index can fail and
return a nullptr. Currently this results in a nullptr deref. Handle that
case by exiting the function early.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-12-28 13:21:37 -08:00
Sylvain Munaut e0b255f34f elab_expr: Support part select for enums inside of structs
Fixes #1033

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
2023-11-16 09:26:04 +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 bdbe74252c Use `netrange_width()` helper where appropriate
The `netrange_width()` helper function computes the total width of a set of
ranges. There are a few places where this is currently open-coded and
`netrange_width()` can be used. This removes a bit of duplicated code.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-09-09 05:50:25 -07:00
Lars-Peter Clausen f6a51bc9db Add support for binding function/task arguments by name
In addition to providing positional arguments for task and functions
SystemVerilog allows to bind arguments by name. This is similar to how
module ports can be bound by name.

```
task t(int a, int b); ... endtask
...
t(.b(1), .a(2));
```

Extend the parser and elaboration stage to be able to handle this. During
elaboration the named argument list is transformed into a purely positional
list so that later stages like synthesis do not have to care about the
names.

For system functions and tasks all arguments must be unnamed, otherwise an
error will be reported.

In addition to functions and tasks arguments can also be bound by name for
the various different ways of invoking a class constructor.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-08-20 08:20:54 -07:00
Lars-Peter Clausen 6ea01cbf7f Fix class constructor chaining corner cases
There are some corner cases around class constructor chaining that can
result in chained constructors not being called, or being called multiple
times.

This is primarily related to that a class can have either an explicit
constructor called `new` and an implicit constructor called `new@` and how
the lookup of them is done.

Lookup is currently done independently for the implicit and explicit
constructor using the `method_from_name()` method. `method_from_name()`
will search the whole class hierarchy for a class method. If a class
doesn't have a method by that name it will look in the parent class and so
on.

As a result the lookup for the explicit constructor can return the explicit
constructor of a parent class if the class itself only has an implicit
constructor and vice versa.

E.g. in the following example the constructor of D will not be called
because the implicit constructor for C is found when looking for a implicit
constructor in D.

```
class C;
  int x = 10;
endclass

class D extends C;
  function new;
    $display("D");
  endfunction
endclass

class E extends D;
  int y;
  function new;
    y = 20;
  endfunction
endclass

E e = new;
```

There is a similar case where the constructor of a base class can be called
multiple times if the base class has an explicit constructor and the
derived class has an implicit constructor. In that case the derived class
constructor will call the base class constructor, but the code that is
emitted for the `new` statement will call both of them.

To mitigate this introduce a new method to lookup the constructor that will
search for either the explicit or implicit constructor in the current class
and only continue to search in the base class if neither is found.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-08-06 02:10:28 -07:00
Cary R fdb9465329 Indexed part selects cannot use real values 2023-07-09 12:25:34 -07:00
Cary R 2249d224de Bit/part selects cannot have real index expressions 2023-07-09 03:47:41 -07:00
Lars-Peter Clausen 7908e15093 Support $bits() for arrays and array slices
`$bits()` for array types is supposed to return the full size of the array
in bits. This currently works for data types that are passed to `$bits()`,
but not for array typed identifiers.

E.g.
```
typedef int T[1:0];
T x;
$display($bits(T)); // -> 64
$display(x); // -> 32
```

Since the `$bits()` implementation uses the expr_width of an expression
include the size of the unpacked dimensions in that for array identifiers
and array slices. Strictly speaking an array identifier does not have an
expression width, but this would be its expression with if it were for
example bitstream cast to a vector.

Special care needs to be take to not trying to pad array identifier
expressions.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-27 05:26:58 -07:00
Lars-Peter Clausen 61381fd9cd Fix bit select on signed multi-dimensional packed array
Bit selects on packed arrays are always unsigned and have a width of 1.
Element selects on a multi-dimensional packed array are always unsigned and
have the width of the element.

At the moment a element or bit select on the last level element of a
multi-dimensional signed array will incorrectly yield a signed expression.

Commit 40b36337e2 ("Fix some bugs with packed array dimensions") added
some special checks to fix the width on multi-dimensional array element
selects. But this removed the unsigned attribute from bit selects.

Commit 81947edaa5 ("A bit select is not the same as selecting part of a
packed array") fixed this for single dimensional packed array, but left it
broken for multi-dimensional arrays.

Commit 7c024d6cab ("Fix width calculation for bit/part selects of
multi-dimensioned packed arrays.") added some additional fixes for the
width calculation, which make the special checks in the first commit
unnecessary.

We can now remove those checks which will give us the correct behavior in
terms of the signedness of bit and element selects on both single- and
multi-dimensional packed arrays.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-26 06:27:30 -07:00
Lars-Peter Clausen 9549156226 Add initial support for array assignment patterns
SystemVerilog allows to use assignment patterns to assign values to an
array. E.g. `int a[4] = '{1, 2, 3, 4}`.

Each value is evaluated in the context of the element type of the array.

Nested assignment patterns are supported. E.g. `int a[2][2] = '{'{1, 2},
'{1, 2}};`

Add initial support for array assignment patterns for both continuous as
well as procedural assignments.

For continuous assignments the assignment pattern is synthesized into an
array of nets. Each pin is connected to one of the assignment pattern
values and then the whole net array is connected to target array.

For procedural assignments it is unrolled in the vvp backend. E.g
effectively turning `a = '{1, 2};` into `a[0] = 1; a[1] = 2;`.

Not yet supported are indexed initializers or `default`.
E.g. `int a[10] = '{1:10, default: 20};`

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-17 11:50:23 -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 7f3621d47d Add initial support for struct assignment pattern
Structs can be initialized by an assignment pattern. E.g.

```
struct packed {
  int x;
  shortint y;
} S = '{ 1, 2};
```

is the same as

```
struct packed {
  int x;
  shortint y;
} S;
s.x = 1;
s.y = 2;
```

Add initial support for unnamed struct assignment patterns. Named struct
assignment patterns like

```
struct packed {
  int x;
  shortint y;
} S = '{x: 1, y: 2};
```

are still unsupported.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-02-04 14:32:51 -08:00
Lars-Peter Clausen 5547858372 Add initial support for packed arrays/vector assignment pattern
SystemVerilog allows to use assignment patterns to assign a value to a
packed array.

This is similar to using a concatenation, with the difference that for
concatenations the values are evaluated in a self-determined context and
for assignment patterns they are evaluated in a context defined by the
element type of the packed array. This means that the value is for example
automatically width expanded or truncated if it does not have the same size
as the element type. Automatic type conversion is also done when allowed. E.g.

```
bit [3:0][3:0] x = '{1'b1, 32'h2, 3.0, "TEST"};
$display("%x", x); // -> 1234
```

Nested assignment patterns are also supported. E.g.
```
bit [1:0][3:0][3:0] x = '{'{1, 2, 3, 4.0}, '{5, 6, 7, 8}};
$display("%x", x); // -> 12345678
```

Add support for using assignment patterns as the right hand side value.
Since the complete type of the target variable is required to correctly
evaluate the assignment pattern it is handled as a special case in
`elab_rval_expression()`. For other types of expressions for packed values
only the total width of the target value is provided to the rvalue
elaboration function.

SystemVerilog also supports assignment patterns for the left hand side in
assignments. This is not yet supported.

Also not yet supported is specifying array elements by index, including
`default`.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-02-04 14:32:44 -08:00
Stephen Williams 18192fdba9
Merge pull request #862 from larsclausen/enum-compat-check
Improve enum compatbility checks
2023-01-16 19:34:05 -08:00
Stephen Williams b6644186a6
Merge pull request #861 from larsclausen/scoped-symbol-search
Add common implementation for scoped symbol search
2023-01-16 19:30:25 -08:00
Stephen Williams e740e4b3f3
Merge pull request #859 from larsclausen/func-empty-arg
Improvements for calling functions with empty arguments
2023-01-16 19:26:23 -08:00
Lars-Peter Clausen d1fb3f8925 Enforce enum type compatibility in more places
In SystemVerilog it is not allowed to assign a value to a enum variable
that is not of the same time as the enum variable.

This is currently enforced for assignment and continuous assignment. But
ignored in other places such as function parameter passing.

Move the enum type check into `elab_rval_expr()` to cover more cases.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 14:45:58 -08:00
Lars-Peter Clausen e24aa18a80 Add common implementation for scoped symbol search
In SystemVerilog identifiers can usually have an additional package scope
in which they should be resolved. At the moment there are many places in
the code base that handle the resolution of the package scope.

Add a common data type for package scoped paths as well as a
symbol_search() variant that works on package scoped identifiers. This
allows to handle package scope resolution in a central place.

Having the code in a central place makes it easier to ensure consistent and
correct behavior. E.g. there are currently some corner case bugs that are
common to all implementations. With the common implementation it only has
to be fixed in one place.

It will also make it easier to eventually implement class scoped
identifiers.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 12:26:24 -08:00
Stephen Williams 0ac3997142
Merge pull request #858 from larsclausen/class-prop-shadow
Consolidate class property handling
2023-01-16 11:23:44 -08:00
Stephen Williams 46a11f4b67
Merge pull request #845 from wmlye/wmlye/assertion-issue823
Clean up assertions in #823 and #840
2023-01-16 10:35:00 -08:00
Lars-Peter Clausen 07e20376d7 Consolidate class property handling
There are currently two mechanisms for handling class properties. One that
is used when a class property is accessed through an object and other when
a class property is used freestanding in a class method.

Both are very similar, but there are some small differences. E.g. one
supports arrays, the other supports nested properties.

```
class B;
  int x;
endclass

class C;
  B b;
  B ba[2];
  task t;
    ba[0] = new; // Does work
    this.ba[0] = new; // Does not work
    b.x = 10; // Does not work
    this.b.x = 10; // Does work
  endtask
```

There is another problem where free standing properties take precedence
over local variables. E.g.

```
class C;
  int x = 1;
  task t();
    int x = 2;
    $display(x); // Should print 2, will print 1
  endtask
endclass
```

The class property elaboration also ignores the package scope of the
identifier resulting in access to a class property being elaborated if
there is a property of the same name as the scoped identifier. E.g.

```
package P;
  int x = 2;
endpackage

class C;
  int x = 1;
  task t;
    $display(P::x); // Should print 2, will print 1
  endtask
endclass
```

Consolidate the two implementation to use the same code path. This is
mainly done by letting the symbol search return a result for free standing
properties as if the property had been specified on the `this` object. I.e.
`prop` and `this.prop` will return the same result from the symbol search.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 07:13:42 -08:00
Lars-Peter Clausen 6cf19ec964 Fixup empty function/task argument lists in a consistent way
As a quirk of the (System)Verilog grammar a function or task argument list
with no arguments can not be distinguished from a argument list with a
single empty argument. The iverilog parses it as the latter. There are
currently many places in the code base where this is fixed up in slightly
different ways.

Fix this up in the parser in a central way before passing the arguments to
the elaboration stage.

The existing implementation in some cases removes all empty trailing
arguments. While this works to handle the case for zero arguments it also
hides some errors that should be detected. E.g. in the following 3
arguments are passed to a function which only takes two arguments. But no
error is reported since the explicitly specified empty arguments are
removed.

```
function f(integer a, integer b = 2); ... endfunction
f(1,,);
```

In the new implementation the empty argument will only be removed if there
is exactly one empty argument in the argument list.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 04:01:49 -08:00
Lars-Peter Clausen c90265351b Duplicate default function/task argument expressions
The default value for a function or task argument is elaborated once and
then used for each function invocation where no actual value is provided.

This means if a function or task is called multiple times the same NetExpr
is passed as a sub-expression to multiple statements or expressions such as
the function call.

This is causing problems because each expression or statement expects to
have exclusive ownership over its sub-expressions. It can for example
result in a double free or other undefined behavior.

To mitigate this duplicate the default argument expression before it is
given as a sub-expression to another expression or statement.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-16 04:00:13 -08:00
Lars-Peter Clausen c1f2e0c21f Support method calls on package scoped signals
Currently package scoped function calls are supported. Update the parser
and elaboration to also allow method calls on packaged scoped variables.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-08 07:59:20 -08:00
Bill Lye ee0c6bb5e4 Modifications to give error messages rather than assertions 2022-12-30 10:36:38 -08:00
Lars-Peter Clausen 43443dd7d1 Improve error handling for package scoped function calls
Currently a package scoped function call will result in an assert if the
function does not exist in the package scope.

For non-package scoped function calls instead a proper error is reported.

Refactor the code to share the same code paths between package scoped and
non-package scoped function calls. This makes sure that errors are reported
in both cases. It also makes the code slightly smaller.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-29 10:53:16 -08:00