Commit Graph

811 Commits

Author SHA1 Message Date
Martin Whitaker a204af04a5 Support for loops with no loop condition.
SystemVerilog makes all of the initialisation, condition, and step
components of a for loop optional. We already support this for the
initialisation and step components.
2024-07-09 21:58:15 +01:00
Martin Whitaker f08ff895af Add informational messages that point to declaration after use. 2024-02-25 16:12:31 +00:00
Martin Whitaker 1c28948484 Pass lexical position information to PTrigger and PNBTrigger objects. 2024-02-19 18:20:14 +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
Martin Whitaker 079108f32b Add lexical position information to PEIdent objects. 2024-02-19 18:13:29 +00:00
Martin Whitaker ccf925a4f7 Remove the legacy version of symbol_search(). 2024-02-18 09:36:23 +00:00
Martin Whitaker c9d87abc10 Convert unpacked array variable to net when connected to a module output port.
SystemVerilog allows variables to be driven by continuous assignments,
including port connections. Internally we handle this by converting
the NetNet from a REG to an UNRESOLVED_WIRE. Here we handle the case
of an unpacked array variable connected to a module output port.

This fixes issue #1001.
2024-01-30 23:18:03 +00:00
Cary R 1471e7d53d MModule elab should check elab tasks first 2023-12-28 15:26:59 -08:00
Cary R cc829031ff Elaborate system elab tasks in gen blocks 2023-12-28 15:07:37 -08: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
mole99 11c944f5e9 Cleanup 2023-08-29 09:54:39 +02:00
mole99 6d3e2bf344 Improve SDF interconnect support, add -ginterconnect flag 2023-08-23 12:56:11 +02: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
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
Cary R ceb07dc9db
Merge pull request #966 from mole99/delayed-signals
Handle delayed signals in timing checks as assignments
2023-07-14 06:33:37 -07:00
mole99 6b2990cfec Use more C++11 features 2023-07-12 15:10:23 +02:00
Cary R 095e6daa0a Cannot use posedge, negedge or edge with a real expression 2023-07-09 05:02:01 -07:00
mole99 e54ff22fce Fix wrong output 2023-07-05 16:50:10 +02:00
mole99 87885dbd9b Handle delayed signals in timing checks as assignments 2023-07-05 16:22:08 +02: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 b8eb21b3ac Allow to omit trailing module ports in ordered list connection
The current implementation expects that for a module instantiation with a
ordered list connection all ports are supplied.

But there doesn't seem to be such a requirement in the LRMs. The Verilog
LRM doesn't mention anything in this regard and the SystemVerilog LRM
mentions in section 23.3.2.1 that a blank or omitted port connection is
either left unconnected or uses the default value of the port.

Update the implementation so that it allows to omit trailing ports and only
generates an error message if too many ports are specified in the ordered
port list.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-06-11 08:45:34 -07:00
Cary R 9cb3d53633
Merge pull request #918 from larsclausen/fix-single-element-array-ports
Fix connecting single element array ports
2023-06-04 21:09:04 -07:00
Cary R f22c1a519b
Merge pull request #900 from larsclausen/task-return
Support return in tasks
2023-06-04 21:07:30 -07:00
Cary R ef931e2e0d Spelling and space cleanup 2023-05-31 22:44:13 -07:00
Lars-Peter Clausen 829af9f438 Fix connecting single element array ports
The current check to decide whether a port is an array or a scalar signal
uses the number of pins on the NetNet. If it is larger than one the code
assumes that it is an array.

But for arrays with on a single element the number of pins will be 1 and
the port is incorrectly treated as a scalar signal which results in an
error.

Instead of using the number of pins check for the number of unpacked
dimensions to decide whether the port is an array.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-05-06 09:28:40 -07:00
Lars-Peter Clausen 730c9c28b2 Support return in tasks
SystemVerilog allows to use the `return` statement in a task to exit the
task before it reaches the end of its execution. This is defined in section
13.3 ("Tasks") of the LRM (1800-2017).

This is similar to using `disable` to stop a task from within itself with
the difference that `disable` will affect all concurrently running
executions of a task, while `return` will only affect the task from which
it has been called.

The `%disable/flow` vvp instruction allows to implement the required
behavior for task return.

There is one complication in that it is not allowed to call return from
inside a parallel block (fork-join). If a parallel block is unnamed and has
no variable declarations there won't be a NetScope for it. So it is not
possible to detect whether the return is inside a parallel block by
walking up the scope chain.

To solve this add a design global counter that gets incremented when
entering a fork block and decremented when exiting a parallel block. The
return implementation then checks if the counter is non 0 to determine
whether it is in a parallel block.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-04-16 19:22:29 -07:00
Lars-Peter Clausen e19d077d44 Iterate static arrays $left to $right in foreach loops
foreach loops are supposed to iterate arrays $left to $right. E.g. `reg
x[3:0]` should be iterated from 3 to 0 and `y[1:4]` from 1 to 4.

The current implementation iterates them from $low to $high. Modify this to
either count up or down depending on whether $left or $right is the larger
of the two values.

Note that the same applies for dynamic arrays. But since for dynamic arrays
$left is always 0 and $right is always $high they always count up and we do
not need to differentiate between two different cases.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-03-12 18:04:31 -07: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
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 dc10710198 Remove cls_val parameter from old symbol_search
There are no users of the old symbol_search that need the cls_val result.
Remove it as a output parameter of the function.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2023-01-15 22:55:57 -08:00
Stephen Williams 6c9c876c83 Add compiler support for break and continue
This includes support at the parser (pform) through enaboration
and the netlist format for the break and continue statements.

Elaboration actually already worked for for-loops, but since the code
generators need more information, this is a rewire of that support to
be explicit about for-loops. This means they are not rewritten as fancy
while loops. The code generators will have to handle that.

Given the elaboration of for-loops now work, write the vvp code generator
support needed to implement it.

Now that for-loops are presented as for-loops to the code generator, the
vlog95 code generator doesn't need to infer them anymore. Generate the code
more directly.

Also update the tests list so that the vlog95_reg tests all pass.
2023-01-15 21:46:20 -08:00
Cary R dc8b7d0184 Cleanup some cppcheck warnings 2022-12-28 00:00:31 -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
Stephen Williams 580d79eae3
Merge pull request #820 from larsclausen/array-compatibility
Add error checking for continuous unpacked array assignments
2022-12-21 11:38:15 -08:00
Lars-Peter Clausen 5ec72f4cc8 Add error checking for continuous unpacked array assignments
Currently there is no error checking for continuous assignments to unpacked
arrays. If the lvalue and rvalue net are not compatible undefined behavior
occurs. For some types of incompatibility it will crash during elaboration,
for others it will crash during simulation, and for some it will just work,
even though the assignment is not allowed by the standard.

Implement checking to ensure the two nets are compatible as required by the
standard and report an error otherwise.

Two arrays are considered to be compatible if their element types are
equivalent, they have the same number of ranges and each range has the same
number of elements.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-17 15:29:38 -08:00
Lars-Peter Clausen 0e62ff153d Add support for void cast function call
SystemVerilog has explicit support for calling a function
as a statement. This is allowed when the function call is encapsulated in
`void'(...)`. E.g. `void'(f(1, 2, 3));`

We already support calling function calls as statements without the void
cast and emit a warning when doing so.

Adding support for void casts only requires to update the parser to handle
the void cast and then do not emit the warning if a function is called as
a statement as part of a void cast.

Void casting a task or void function call is not allowed and will generate
an elaboration error.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-17 11:19:30 -08:00
Lars-Peter Clausen 54956f0f29 Be consistent on allowing calling non-void function as task
When calling non-void functions or non-void methods of built-in types  as a
task a warning is issued. But when calling a non-void method of a user
defined class as a task an error is generated.

Be consistent here and generate a warning in both cases.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-17 11:19:30 -08:00
Stephen Williams 5b9ceee062 Make the for_step of for loops optional
In IEEE Std 1800-2017 A.6.8: the for_step part of the for loop
is optional. If missing, it is assumed that the programmer known
what they are doing.
2022-12-11 16:39:53 -08:00
Stephen Williams bb779112c7
Merge pull request #807 from steveicarus/steveicarus/issue801-empty-for-init
Handle empty for-loop init statement
2022-12-11 16:07:24 -08:00
Stephen Williams 78f37f7156 Handle for loops with empty initialization statement
For loops may have empty initialization statements. In that case some things
can't be done, such as loop unrolling or synthesis, but otherwise it is a
valid thing to do. So generate the correct code in this case.
2022-12-11 15:46:16 -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 eaea5e7939 Print error if foreach loops variables exceed number of array dimensions
Currently when the number of loop variables in a foreach loop is larger
than the number of array dimensions an assertion is triggered.

Turn this into a error message instead for graceful error reporting.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 13:56:48 -08:00
Lars-Peter Clausen bdafdb1e65 Use std::swap() to exchange elaboration_work_list
Rather than moving one item at a time form the elaboration_work_list to the
temporary queue use std::swap to exchange them all at once. This is a bit
more efficient.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 12:38:47 -08:00