Commit Graph

502 Commits

Author SHA1 Message Date
Cary R 860761f9c6 More cppcheck fixes - part 2 2025-10-20 23:54:15 -07:00
Cary R e55d9454da Calling front() on an empty() list is undefined 2025-07-13 19:38:43 -07:00
Cary R a2ffbc307a Validate the generate "loop" expressions 2025-06-21 16:58:30 -07:00
Cary R 26c01e7f0a Use preincrement instead of post in for loop incr 2025-06-20 12:48:01 -07:00
Cary R 03835c9d50 Report each line that has a var decl in an unnamed block 2024-12-28 20:51:30 -08:00
Cary R 8edf14ae68 Check for primitive port mismatches and other error cleanup 2024-12-08 22:21:51 -08:00
Martin Whitaker b2eaedfc94 Emit "sorry" message for unpacked array parameter declarations (issue #1180)
These aren't yet supported. Make it an error if not compiling fpr a
SystemVerilog generation.
2024-11-15 21:01:12 +00:00
Martin Whitaker aec18a6c19 Emit "sorry" message for packed array parameter declarations (issue #1180)
These aren't yet supported. Make it an error if not compiling fpr a
SystemVerilog generation.
2024-11-15 20:24:58 +00:00
Martin Whitaker 76a9d38d87 Add check for parameters used before they are declared. 2024-02-19 18:20:39 +00:00
Martin Whitaker 1c28948484 Pass lexical position information to PTrigger and PNBTrigger objects. 2024-02-19 18:20:14 +00:00
Martin Whitaker e22831553d Improve identifier lexical position accuracy in declarations.
Enhance the lists of identifiers and declaration assignments generated
by the parser to associate each identifier with its lexical_pos. Also do
this for single items in complex parser rules where the location passed
to the pform is not the location of the identifier.
2024-02-19 18:16:35 +00:00
Martin Whitaker bb80ee6905 Add lexical position information to PWire and PEvent objects. 2024-02-19 18:14:49 +00:00
Martin Whitaker 079108f32b Add lexical position information to PEIdent objects. 2024-02-19 18:13:29 +00:00
Martin Whitaker a8144fd249 Require -gxtypes to use the Icarus extension for wire data types.
e.g. wire bool [1:0]; wire real;

Currently xtypes is enabled by default, so most users won't see a
difference.
2024-01-28 15:48:38 +00: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 102d85c4e5 Attach line information to named items
Attach line information to named items. This allows to provide better
location information for messages involving named items. The location of
item itself can't always be used, since the item itself might be empty.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-08-19 10:12:00 -07:00
Lars-Peter Clausen e7f66fe7ac Use standard constructor to copy std::list to std::vector
There are a few places in the code where a std::list is copied to a
std::vector by iterating through the list and copying each element over to
the vector. The std::vector type has a iterator based constructor that can
do the same.

Update the code to use it instead. This removes a bit of boilerplate code
and also makes it easier to update the code.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-08-19 10:12:00 -07:00
Lars-Peter Clausen ce243268d0 Use `named_pexpr_t` type instead of open-coding it
`named_pexpr_t` is a typedef for `named<PExpr*>`. There are a few places
where `named<PExpr*>` is used directly. Replace those with `named_pexpr_t`
for consistency.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-08-19 10:12:00 -07:00
Lars-Peter Clausen 3daa2982ac Add support for `const` variables
SystemVerilog allows to declare const variables. These variables are
read-only and can not be assigned a value after their declaration. It is
only possible to assign an initial value as an initializer.

E.g.
```
const int x = 10;
x = 20; // Error
```

The LRM requires that for variable declarations with static storage the
initializer is a constant expression with the extension that other const
variables are also allowed. const variables with automatic storage can
be initialized by any expression.

Checking if an expression contains only const variables requires a bit more
work to implement. So for now be more lenient that what the standard
requires and allow arbitrary expressions to initialize const variables even
for those with static storage.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-07-23 15:08:39 -07:00
mole99 6b2990cfec Use more C++11 features 2023-07-12 15:10:23 +02:00
mole99 d46628b2f2 Improve parsing of timing checks 2023-07-10 15:59:14 +02:00
mole99 87885dbd9b Handle delayed signals in timing checks as assignments 2023-07-05 16:22:08 +02: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 c1c4c28313 Handle implicit task/function imports in the unit scope
SystemVerilog requires that functions and tasks are not implicitly imported
if a symbol with the same name appears in the scope, even if it the symbol
is declared after its usage.

To support this a list of potential imports is collected while parsing a
scope and only when the end of the scope is reached it is evaluated whether
the symbol should be imported or not based on whether it already exists in
the scope.

This currently works fine for all scopes except for the unit scope. Since
the unit scope might span multiple files it is never explicitly closed and
the potential imports are never checked.

Make sure that after parsing all files is done the potential imports for
the unit scope are checked.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-11 09:20:51 -07:00
Lars-Peter Clausen 78382e72d0 Add support for package export
By default an identifier that has been imported into a package is not
available for imports by other packages. Only imports that have been
exported can be imported again. E.g.

```
package P1;
  int x;
endpackage

package P2;
  import P1::x;
  export P1::x;
endpackage

module test;
  import P2::x; // This will only work if x has been exported.
endmodule
```

Exports follow the same syntax as imports and allow both export of specific
identifiers or wildcard export. Export supports the special `*::*` target,
which will export all imported items.

Add support for handling package exports.

There is one special cases that needs to be considered. Usually when using
wildcard imports from multiple packages it is an error if there multiple
import candidates for an identifier. With exports it is possible that there
are multiple candidates through different packets, but they all refer to
the same identifier. In this case it does not create a conflict. E.g.

```
package P1;
  int x;
endpackage

package P2;
  import P1::x;
  export P1::x;
endpackage

package P3;
   import P1::*;
   import P2::*;
   int y = x; // No import conflict
endpackage
```

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-17 06:14:07 -08:00
Cary R dc8b7d0184 Cleanup some cppcheck warnings 2022-12-28 00:00:31 -08:00
Stephen Williams 7b1fad78c3
Merge pull request #824 from larsclausen/class-lifetime
Fix variable initialization in class methods
2022-12-24 17:33:29 -08:00
Martin Whitaker 6b04d9795a Use consistent capitalisation for compiler parser warning messages. 2022-12-22 10:47:45 +00:00
Martin Whitaker 82aedbc36c Don't automatically add the "warning: " prefix in VLwarn().
One variant did, the other variant didn't. As well as being a trap for
the unwary, this gets in the way of using yywarn/VLwarn for non-fatal
"sorry" messages.
2022-12-22 10:34:56 +00:00
Martin Whitaker a56ccabf7f Use consistent format for compiler parser error messages.
Always prefix with "error: " or "sorry: ". Capitalise the first word
of the main message unless it's a Verilog keyword.
2022-12-22 10:13:26 +00:00
Lars-Peter Clausen 54a4e7ff0b Fix variable initialization in class methods
Up to 1800-2017 the grammar in the LRM allowed an optional lifetime
qualifier for class declarations. Icarus supports this and uses this as the
default lifetime for methods of the class. But the LRM never specified what
this qualifier should do actually actually. Starting with 1800-2023 the
qualifier will be removed from the grammar[1].

Furthermore the LRM states that methods of a class are supposed to have
automatic storage and static storage is forbidden.

This currently works in Icarus for the most part since the liftime attached
to class methods is ignored during elaboration in most places. Where it
does not work is for variable initializers where it results in broken code
being generated and vvp crashes at runtime. E.g.

```
class C;
  task t;
    int x = 10;
  endtask
endclass
```

Keep the optional lifetime qualifier for classes in the grammar for now, to
ensure backwards compatibility in case somebody is actually using it. But
ignore it and print a warning if it is specified.

In addition set the default lifetime for all classes to automatic. This
makes sure that variable initialization in classes works as expected.

[1] https://accellera.mantishub.io/view.php?id=3561

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-21 19:28:35 -08:00
Stephen Williams 03f912dc55 Clean up warnings
Clean up warnings that show up on newer compilers. Many of these warnings
are related to obsolete c library features or language features. This does
not clear up warnings in code generated by bison or flex.
2022-12-18 10:18:10 -05:00
Lars-Peter Clausen 79771b17b2 Consolidate functions for task port declaration
With the recent refactorings pform_make_task_ports_vec() and
do_make_task_ports() are now very similar.

Consolidate them into a single function.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 17:39:34 -08:00
Lars-Peter Clausen 4e69fe1355 Remove ivl_variable_type_t from PWire
The ivl_variable_type_t in PWire is now only used for passing the base type
for vector types to the elaboration stage. But we can query the base the
from the vector_type_t itself. If the there is no data_type_t set for the
PWire the base type will default to IVL_VT_LOGIC.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 17:08:24 -08:00
Stephen Williams 4ead552203
Merge pull request #805 from larsclausen/sv-type-params
Support type parameters
2022-12-11 16:45:39 -08:00
Lars-Peter Clausen e141aef828 Allow to omit dimensions in foreach loop
SystemVerilog allows to skip dimensions in a foreach loop by not specifying
an identifier name for the dimensions. E.g. the following will iterate over
the first and last dimensions, but skip the middle dimension.

```
int x[1][2][3];
foreach(x[a,,b]) ...
```

Add support for this to the parser as well as elaboration.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 13:56:48 -08:00
Lars-Peter Clausen 5ef847ea87 Support type parameters
SystemVerilog supports type parameters. These are similar to value
parameters, but they allow to pass a type to a module or similar when
instantiating it.

E.g.

```
module A #(parameter type T = int);
endmodule

module B;
  A #(.T(real)) i_a;
endmodule
```

Add support for handling type parameters.

For the vlog95 and vhdl backends type parameters, similar to typedefs, get
replaced with their actual value. For modules with non-local type
parameters for each module instance a unique module or architecture is
generated with the actual type.

Querying type parameters through VPI is not yet supported.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 13:50:14 -08:00
Lars-Peter Clausen cdc9629ce7 Add support for forward type declarations
SystemVerilog supports forward type declarations. This allows to declare a
type identifier and use it, e.g. in a signal declaration, before declaring
what the actual type is. The type still needs to be fully defined
eventually in the same scope as its forward type declaration. E.g.

```
typedef T;
T x;
typedef int T;
```

The forward type definition can also contain the kind of the type it is
going to be. E.g struct, union, class, etc. The LRM calls this the basic
type. If the actual type is not of the basic type specified in the forward
type declaration this is an error. E.g.

```
typedef struct T;
typedef int T; // Error, int is not a struct
```

It is legal to have more than one forward type declaration for the same
type name, as long as the basic type is the compatible. It is even legal to
have a forward type declaration after the actual type has already been
declared. E.g.

```
typedef T;
typedef int T;
typedef T;
```

Implement support for forward type definitions as part of the new
typedef_t. The basic type will be attached to the typedef_t.

The compatibility of the basic type for multiple forward type declarations
will be checked in the parser. The compatibility of the basic type to the
actual type will be checked during elaboration, once the actual type is
known.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 10:19:59 -08:00
Lars-Peter Clausen 2e0d6d5af1 Allow to attach additional information to typedefs
Currently typedefs are just a pointer to a data_type_t.

Currently typedefs are implemented by setting the name field of a
data_type_t when a typedef of the type is declared. This works mostly, but
there are some corner cases that can't be supported.

E.g. a typedef of a typedef does not work as it overwrites the name field
of the same data_type_t multiple times.

Forward typedefs can also not be supported since forward typedefs allow to
reference a type before it has been declared.

There are also some problems with type identifier references from a
higher-level scope if there is a type identifier in the current scope with
the same name, but it is declared after the type identifier has been
referenced. E.g. in the following x should be a vector fo width 8, but it
will be a vector of width 4, because while the right type is used it is
elaborated in the wrong scope.

```
localparam A = 8;
typedef logic [A-1:0] T;
module M;
  localparam A = 4;
  T x;
  typedef int T;
endmodule
```

Furthermore typedefs used for the type of ports are elaborated in the wrong
scope.

To handle these corner case issues introduce a data_type_t for typedefs.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 10:18:22 -08:00
Lars-Peter Clausen 410c8207ef Support compressed assignment statements for genvar loops
SystemVerilog supports using compressed assignment operators for the genvar
for loop variable update.

Add support for this in a similar way as increment/decrement operators by
transforming the statement to its uncompressed equivalent. E.g. `x += y`
gets transformed to `x = x + y`.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-04 14:37:21 -08:00
Lloyd Parkes 6c975eb89c Add #include <cstdarg> to all source files that use va_list. 2022-10-12 15:31:35 +13:00
Lars-Peter Clausen 2385b32cb3 Correctly handle unpacked array typedefs for ports
If the type of a port is an array type it currently always gets evaluated
in the scope where the port is declared.

But if the type is a typedef it might be declared in a different scope and
must be evaluated in that scope. E.g. the following will declare an array
port with 10 entries and an element type of a 5 bit vector, while it should
declare one with 4 entries and an element type of a 2 bit vector.

```
localparam A = 2;
localparam B = 4;
typedef [A-1:0] T[B];

module test (
  T x
);

localparam A = 5;
localparam B = 10;

endmodule
```

This is in part due to array types being given special handling. This was
necessary before because each base type required slightly different
handling and so the base type had to be extracted from the array type.

This has now been consolidated and all data types are treated the same.
The only exception is the vector type which still needs special handling to
support separate definition of port direction and type.

As a result it is possible to remove the special handling of the array
type. This solves the problem of evaluating the type in the wrong scope.

Some special handling needs to be retained though to be able to
differentiate between array dimensions that are part of a type and array
dimensions that are part of port declaration. This is again necessary to
correctly support separate definition of port direction and type. E.g. in
the example below port `x` and `y` get treated slightly differently, even
though the resulting signals will be identical.

```
typedef logic [7:0] T[1:0];
...
input T x;
input [7:0] y[1:0];
```

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-20 12:25:14 +02:00
Lars-Peter Clausen 8f78590bd2 Remove most references to ivl_variable_type_t from the parser
The parser used to have behavior that was dependent on the
`ivl_variable_type_t` of a signal. It also used the `ivl_variable_type_t`
of a signal to decide whether a signal can be re-declared as part of a
non-ANSI port declaration.

Neither of these is done anymore and most of the reference to
`ivl_variable_type_t` can be removed from the parser. The only thing it is
still needed for is to decide whether a vector type is 4-state or 2-state.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-15 09:36:19 +02:00
Lars-Peter Clausen b307da0831 pform_make_task_ports(): Reuse `pform_set_net_range()`
`pform_make_task_ports()` has code very similar to `pform_set_net_range()`.
Use that helper function instead of duplicating the code.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-15 09:36:19 +02:00
Lars-Peter Clausen c5ebe35953 pform_module_define_port(): Reuse `pform_set_net_range()`
pform_module_define_port() has code very similar to `pform_set_net_range()`.
Use that helper function instead of duplicating the code.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-15 09:36:19 +02:00
Lars-Peter Clausen d3be19081f pform_set_range(): Pass full vector_type_t as argument
Now that pform_set_range() is only used for vector types pass the
vector_type_t as an argument rather than deconstructing the type into range
and signedness.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-15 09:36:19 +02:00
Lars-Peter Clausen 6204b78610 Prevent invalid port redeclaration
(System)Verilog allows to declare the port direction separate from the
signal declaration. E.g.

```
output x;
integer x;
```

But this is only allowed if the port declaration
 * does not have an explicit net type
 * does not have an explicit data type
 * is a non-ANSI style declaration

For all other cases of port declarations the signal is considered fully
defined and it is not allowed to have a separate signal declaration.

In addition the declared packed dimensions need to match between the port
and signal declaration.

In the current implementation there are a few cases where this is not
handled correctly.

1) It is possible to declare non-ANSI task ports with the same name over
and over again, if it was declared as a signal before the port.

```
task t;
  string x;
  input logic x;
  output real x;
endtask
```

2) It is possible to re-declare non-ANSI input ports of a module that have
a data type, but no explicit net type.

```
module M;
  input integer x;
  wire integer x;
endmodule
```

3) It is possible to re-declare a ANSI port if it has an implicit data type.

```
module M(output [1:0] x);
  reg [1:0] x;
endmodule
```

4) It is possible to declare a vector signal for a scalar non-ANSI task
port.

```
task t;
input x;
reg [7:0] x;
```

To handle all of these correctly refactor signal declaration and lookup a
bit.

The PWire class that represents a signal already has two flags `port_set_`
and `net_set_`. These flags indicate whether a signal has already been used
in a port or signal declaration. A port declaration that includes an
explicit data type is considered both a port and signal declaration.

Use these flags to decide whether it is possible to extend an existing
declaration. E.g. when creating a port without an explicit data type and a
PWire by that name already exists and the `port_set_` flag is not set
extend the existing PWire. On the other hand if the `port_set_` flag is
already set report an error.

Similar for signals but with the `net_set_` flag.

For port declarations with an explicit data type or ANSI style port
declarations it is always an error if a PWire by that name already exists.

This is for both module and task/function ports.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-09-14 18:55:19 +02:00
Lars-Peter Clausen e15b125da8 Replace svector with std::vector
The custom `svector` class is essentially a subset of `std::vector`. There
is no inherent advantage to using `svector`. Both have the same memory
footprint.

`svector` was designed to be of static size, but there are a few places in
the parser where it has to grow at runtime. Handling this becomes a bit
easier by switching to `std::vector` since it is possible to use its
methods which take care of resizing the vector.

This also allows to remove the unused parameter of the `lgate` struct
constructor, which was only needed for compatibility with `svector`.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-05-15 21:56:48 +02:00
Lars-Peter Clausen dddc41891e lgate: Inherit from LineInfo
The lgate struct has its own fields for tracking file and line number,
while everything else that has this information attached inherits from the
LineInfo class.

Make lgate also inherit from LineInfo for consistency.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-24 17:00:46 +02:00
Lars-Peter Clausen 854422a3eb Allow `shortreal` module ports
Currently there is a restriction in the parser that rejects `shortreal`
typed module ports. And while at the moment `shortreal` signals are
implemented as `real` typed signals, which is not standard compliant, there
is nothing special about module ports in this regard.

Note that support for `shortreal` (and `real`) nets is an Icarus extension,
but ports can also be variables, in which case a shortreal port is allowed
by the LRM.

`shortreal` variables and nets are allowed everywhere else. There is no
good reason to not allow them for module ports, so remove the restriction.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-21 10:17:50 +02:00