Commit Graph

241 Commits

Author SHA1 Message Date
Stephen Williams 15b4a8a046
Merge pull request #806 from larsclausen/class-sig-elab
Elaborate class properties during signal phase instead of scope phase
2022-12-11 16:46:54 -08:00
Lars-Peter Clausen b4b591adba Elaborate class properties during signal phase instead of scope phase
Elaborating uses a multi stage approach. Currently non-static class
properties are elaborated during the scope elaboration phase. This can
cause problems if the type of a property is declared in a different scope.
In that case it is possible that the scope in which the type is defined has
not been elaborated yet and the type is not available. E.g.

```
package P;
  typedef int T;
endpackage

class C;
  P::T x;
endclass
```

Another area where this is problematic is when a class has a property of a
another class that has a forward declaration. In this case the type of the
forward declared class, which is created when the scope is elaborated, is
not available when the scope of the class that is using it is elaborated.
E.g.

```
typedef class B;

class A;
  B b;
endclass

class B;
endclass
```

To avoid this elaborate the properties during the signal elaboration phase.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 14:07:53 -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 699ceb15a5 Elaborate enum type on demand
Enum types are currently elaborated in lexical declaration order. With forward
typedefs it is possible that a type is referenced before it is declared.

To support this elaborate the enum type on demand when it is used. This is
similar to what is being done for other types.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-12-11 10:18:23 -08:00
Lars-Peter Clausen b7fce4a66e Improve handling of type identifier references
Currently when referencing a typedef this gets replaced with the
`data_type_t` that the typedef points to. This works for most cases, but
there are some corner cases where it breaks down.

E.g. it is possible to have a scoped type identifier which references a
type defined in a package. For such type identifiers, only the data_type_t
itself is remembered, but not the package scope. This will cause the type
identifier to be elaborated in the wrong scope.

Furthermore type identifiers of vector types used for module or task port
might not be elaborated in the correct scope.

Introduce a new `typeref_t` which has `data_type_t` as a base type and can
be used as the data type for a signal. A new instance of a `typeref_t` is
created when referencing a type identifier. The `typeref_t` remembers both
the data type and the scope of the type identifier.

When elaborating the `typeref_t` the elaboration is passed through to the
referenced `data_type_t`. But special care is taken to lookup the right
scope first.

With the new approach also typedefs of typedefs are supported. This
previously did not work because chained typedefs all reference the same
`data_type_t`, but each typedef sets the `name` field of the `data_type_t`.
So the second typedef overwrites the first typedef and a lookup of the
scope of the first typedef by name will fail as it will return the scope of
the second typedef.

This refactoring also allows to define clear ownership of a data_type_t
instance. This e.g. means that an array type owns its base type and the
base type can be freed when the array type itself is freed. The same is
true for signals and class properties, they now own their data type and the
data type can be freed when the signal or property is freed.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-10-15 12:32:22 +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 27f3fcc5f1 Allow queue types outside of classes
Currently the elaboration function for unpacked array types prints an error
for queues that they are not allowed inside classes. And while this error
gets triggered when declaring a property with a queue type, it also gets
triggered for other places that uses a queue type, e.g. a function return
type. The only exception is signals which uses a different internal code
path when elaborating queue types.

Move the error message, that is class property specific, to the class
property elaboration. This also makes sure that the error messages
references the line where the property is declared and not the line where
the type is declared.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-04-17 09:14:38 +02:00
Stephen Williams 658d4f5eee
Merge pull request #658 from larsclausen/class-in-module
Handle multiple instances of modules with class definitions
2022-03-27 15:48:56 -07:00
Lars-Peter Clausen 4bf0d62cd1 Support type identifier base type for enum
The base type for an enum type can be a type identifier for a typedef as
long as it resolves to a vector or integer type with at most one packed
dimension. This is described in section 6.19 ("Enumerations") of the LRM
(1800-2017). E.g.

```
typedef bit [3:0] T;
enum T {
 A
} e;
```

Add support for this by allowing to specify a type identifier as the base
type for an enum in the parser. During elaboration it is checked whether
the type identifier resolves to a valid enum base type.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-25 21:55:34 +01:00
Lars-Peter Clausen eed38bd14a Handle multiple instances of modules with class definitions
For classes declared inside a module each module instance creates a new
unique class type. These types are not compatible to each other. This is
necessary since module parameters can change the class implementation.
This is defined in section 6.22 ("Type compatibility") of the LRM (1800-2017).

In the current implementation when a class is elaborated the elaborated
type is stored in the class_type_t so it is possible to look up the
elaborated class type. But this class_type_t is shared among elaborated
class types. As a result when creating multiple instances of a module with
a class definition an internal assert is triggered.

To support multiple module instances with class definitions instead of
storing the elaborated type in the type definition look up the type in the
scope in which the type definition is references.

This is similar to how the same problem is solved for enum types.

For packages we still need to remember the elaborated type otherwise scoped
class type references wont work. Since there is only one instance of a
package this doesn't have the same problem as classes in modules.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-22 11:53:47 +01:00
Lars-Peter Clausen fabff5ef5f Restrict enum compatibility to the same scope
An enum data type declared in a module is not compatible between different
instances of the module. The type is unique in each hierarchical instance
scope. The type can for example depend on module parameters which would
result in conflicting definitions. This is defined in section 6.22 ("Type
compatibility") of the LRM (1800-2017).

At the moment enum compatibility is checked by comparing the enum_type_t.
But the enum_type_t is shared among the netenum_t that are created for each
module instance and gives the wrong result.

Since there is exactly one netenum_t created for each enum and each
instantiated scope use this to check if the data type of two enum type
signals is compatible.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-19 17:17:21 +01:00
Lars-Peter Clausen 7f4013ac66 data_type_t::elaborate_type(): Make return type const
For some data types the value returned by the `elaborate_type()` method is
shared among different signals of that type. E.g. all string or real types
get elaborated to the same ivl_type_s. This means the returned value must
not be modified, otherwise the data type for unrelated signals might get
changed.

To enforce this and protect against accidental breakage make the return
type of the `elaborate_type()` and the related `elaborate_type_raw()`
methods const.

Note that `ivl_type_t` is used for the new return type which is a typedef
for `const ivl_type_s*`.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-03-14 22:06:12 +01:00
Stephen Williams 807fb2e5d1 always_comb and friends handle void functions similar to tasks
Void functions can be used in always_comb, but since the compiler
uses the check_synth() method to generate some warnings, make sure
that function is implemented for functions as well as tasks.
2022-02-27 08:56:30 -08:00
Lars-Peter Clausen d104e28dbf Support non-overridable parameters in classes
SystemVerilog allows `parameter` and `localparam` to declare constants
within a class scope.  E.g.

```SystemVerilog
class C;
localparam A = 10;
endclass
```

In this context both declare a local parameter that can not be overwritten.

Supporting this can be achieved for the most part by adding a parser
sub-rule in class declaration rule. In addition some extra support code is
needed to mark the parameter as non-overridable.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-02-19 13:45:22 +01:00
Stephen Williams 771d02bee1
Merge pull request #607 from larsclausen/sv-parameter-in-generate
Allow parameter in generate blocks for SystemVerilog
2022-02-10 17:16:22 -08:00
Lars-Peter Clausen 9f5ad34e35 Track whether a parameter is overridable
Parameters declared in certain scopes behave like local parameters and can
not be overridden. Rather than making those parameters a localparam track
whether a parameter can be overridden.

This allows to generate better error messages when trying to override the
parameter.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-02-10 11:37:34 +01:00
Lars-Peter Clausen 1207e908b1 PScope: Keep parameter and localparams in the same list
During parsing parameters and localparams are kept in a separate list only
to be collected into the same list during elaboration.

Store them in the same list during parsing as well, this allows to remove
some duplicated code.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-02-10 11:07:03 +01:00
Lars-Peter Clausen 6a61144937 Support scoped base class type
A base class can be referenced by scope. E.g. if the base class is in a
package.

```
package P;
  class B;
  endclass
endpackage

module test;
  class C extends P::B;
  endlcass
endmodule
```

To support this let the parser accept a scope identifier for the base
class.

A small change is also necessary to how the base class lockup is done
during elaboration. At the moment the code will search for the base class
by name in the current scope. This doesn't work with scoped identifiers.

But we already have a reference to the base class data type, so we don't
have to search for it by name.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-02-06 21:37:46 +01:00
Lars-Peter Clausen 0ab1ed916f Report error when trying to override non-existing parameter
Overriding a parameter that does not exist will only generate a warning at
the moment. This can hide programming mistakes such as an typo in a
parameter override.

There is nothing in the LRMs to support that this should only be warning,
so elevate this to an error. This is consistent with how an error is
generated when trying to reference a non-existing port or variable.

The generated error message differentiates between whether the parameter
does not exist at all, or whether it is a localparam.

There are two regression tests that rely on that only a warning is
generated, these have been updated to expect an error.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-02-01 22:50:21 +01:00
Lars-Peter Clausen 057cd700fe netenum_t: Fix line info
enum_type_t inherits from LineInfo, but also has a LineInfo field called
`li`.

When a enum_type_t is created the LineInfo of the object itself is set to
the location where the type is declared.

The `li` field gets set when a signal of the enum_type_t is created to the
location where the signal is created. The `li` field is then used when
elaborating a netenum_t to set the line information on the netenum_t.

This works fine when the enum is directly used to declare a signal, since
the location of the type and signal declaration are the same and there is
only one signal of that type.

But when using a typedef and declaring multiple signals with the same type
the `li` field will be repeatedly set and eventually point to the last
signal declaration of that type.

On the other hand when using or declaring an enum as part of an aggregate
type such as an array, struct or class the line info will never be
set.

This can cause misleading error messages. E.g.

```
typedef enum {
  A, B = A
} e_t;

struct packed {
  e_t e;
} s;
```

will generate

```
:0: error: Enumeration name B and A have the same value: 32'sd0
```

To fix this use the LineInfo that was assigned to the enum_type_t itself
when it was declared and remove the `li` field.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-23 20:07:00 +01:00
Lars-Peter Clausen e75ad281fc Elaborate enums in the order they have been declared
enums for a scope are stored in a std::set. This means when iterating over
the enums during elaboration it is possible that they are elaborated in a
different order than they have been declared in. This causes problems if
one enum references items of the other enum. E.g.

```
enum {
  A
} a;

enum {
  B = A
} b;
```

In the current implementation whether this works or not depends on the
pointer values of the enum_type_t for `a` and `b`, which can change between
environments.

To make sure that enums are elaborated in the same order use a std::vector
instead of a std::set.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-22 12:24:05 +01:00
Lars-Peter Clausen 3176611771 netenum_t: Use netrange_t to store range
Use netrange_t, which is meant for storing ranges, for storing the range of
a netenum_t.

This makes the integration with the rest of the codebase a bit more
seamlessly and also allow to reuse methods defined for netrange_t such as
the width() method rather than having to reimplement it.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-08 16:26:32 +01:00
Lars-Peter Clausen 90edf48ac6 Handle invalid enum dimensions
Specifying and enum with an invalid dimension range results in an assert or
segfault. E.g. `enum [$] E { ... }`.

Use the `evaluate_ranges()` function to elaborate the enum dimensions. This
functions has proper error checking and recovery built-in.

In addition verify that there is at most one packed dimension.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
2022-01-08 16:21:45 +01:00
Martin Whitaker 668f9850bc Warn when a negative value is assigned to a genvar (-g2001 only) (issue #567) 2021-12-13 21:30:31 +00:00
Martin Whitaker 67b9374c69 Change compiler's generate loop counter from int to long.
This matches the type of the values assigned to it, and exposes a bug that
was previously only showing up under Windows.
2021-12-13 20:28:41 +00:00
Martin Whitaker dcc9b59f6d Support SV [size] dimension for module and gate instances (issue #553).
Also output a proper error message if multiple dimensions are supplied
instead of failing an assertion.
2021-11-06 00:02:38 +00:00
Martin Whitaker ecbbb60fb6 Remove "using namespace std" from compiler header files and fix the fallout. 2021-11-04 16:55:03 +00:00
Martin Whitaker 7ee7a48310 Check that a genvar is not shadowed when used in a generate loop construct.
This also ensures the same genvar cannot be used in two nested loops
(issue #533), because the implicit localparam with the same name
shadows the genvar declaration.
2021-08-04 14:35:38 +01:00
Martin Whitaker c34167b2c0 Fix detection of directly nested generate constructs.
If a generate construct is enclosed in a begin-end pair, it can't
be directly nested (1364-2005 section 12.4.2).
2021-08-04 10:37:26 +01:00
Cary R 7e40f5ac52 Add warning when too many parameter overrides are provided for a module instantiation 2021-01-21 23:16:31 -08:00
Cary R da7484eea1 Update compiler with suggestions from cppcheck 2021-01-02 14:04:46 -08:00
Stephen Williams 16646c547c Rework parsing of parameter types
Use the common data_type_or_implicit rules to support type
definitions for parameters. This eliminates a bunch of special
rules in parse.y, and opens the door for parameters having
more complex types.
2020-12-27 21:17:57 -08:00
Martin Whitaker a019994513 Fix search for class imported from another package (issue #437). 2020-12-23 19:16:14 +00:00
Martin Whitaker c4883da334 Fix width of localparam created from genvar when using -gstrict-expr-width. 2020-10-01 11:33:24 +01:00
Martin Whitaker 33b822d997 Add support for local genvar declaration in generate loops.
As requested in GitHub issue #304.
2020-01-31 20:29:22 +00:00
Martin Whitaker 8dc395940d Fix issue #298: elaborate types in the scope where they were declared.
The compiler elaborates types on the fly as they are used. For user-
defined types (typedefs) we must do the elaboration in the scope where
the type was declared, not in the scope where it is used.
2019-12-22 17:29:23 +00:00
Martin Whitaker 465e0d2710 Allow classes to reference declarations in their enclosing scope(s).
The original implementation made each class a root scope. They should
be added to the scope hierarchy just like any other declaration.
2019-12-22 10:46:38 +00:00
Martin Whitaker b0142a6406 Add support for named events in packages. 2019-10-01 09:07:39 +01:00
Martin Whitaker f69eccf903 Merge remote-tracking branch 'origin/master' into package-imports-rework 2019-10-01 09:06:15 +01:00
Stephen Williams 50d71c8512 Support for enumerations in classes. 2019-09-29 18:27:27 -07:00
Martin Whitaker e73da43cc1 Remove obsolete checks for name collisions during elaboration.
This is now handled by the parser.
2019-09-27 22:19:30 +01:00
Martin Whitaker 628f5645bf Fix file/line reported for duplicate parameter declarations.
We need to retain the old parameter information until we have reported
the error.
2019-09-27 22:19:30 +01:00
Martin Whitaker d3bced57cc Correctly handle explicit and wildcard package imports.
Explicit imports should always conflict with local declarations using
the same name. Wildcard imports only conflict if they are referenced
before a local declaration with the same name.

This also unifies the detection of identifier conflicts.
2019-09-27 22:19:30 +01:00
Martin Whitaker 55219773fd Allow nested scopes to use their parent's imports. 2019-09-27 22:19:30 +01:00
Martin Whitaker fd807a7700 Rework handling of timescales in parser.
This implements and enforces the full set of rules for determining
timescales in SystemVerilog. The previous relaxation of the rules
that allowed timescales to be redefined within the compilation unit
scope has been removed. Time unit and precision redeclarations are
now recognised after a nested module declaration.
2017-11-05 17:50:05 +00:00
Martin Whitaker 988816c0b1 Support separate compilation units in SystemVerilog.
The compilation unit scope is now treated as a specialised form of
package (with an automatically generated name). All items declared
outside a design element are added to the current compilation unit
package. Apart from when searching for a symbol, once we get into
elaboration we can treat these just like any other package.
2017-10-31 20:38:59 +00:00
Martin Whitaker 87cddf33dc Fix for GitHub issue #98 and #167
Two fixes needed:
  - when searching for a base class, we need to look in the root scope
    if the base class isn't found in the scope hierarchy
  - the classes in the root scope need to be stored in an ordered
    list, not a map, to ensure they are elaborated in the order they
    were declared. Without this, the compiler may try elaborating an
    extended class before its base class is known about.
2017-10-08 21:15:11 +01:00
Vamsi Vytla cfd3b893be Add support to handle attributes at module instantiation sites.
(* my_fancy_attribute *)
foobar1 foobar (clk(clk), rst(rst) ...);

  - Modifies PGModule to hold the attribute map (can be verified with pform_dump)
  - pform_make_modgate(s) bind the attributes from the parser to the above map
  - The attributes from PGModule are inserted into the NetScope of that module
    PGModule::elaborate_scope_mod_instances_
  - Currently these attributes automatically make it into netlist
  - These attributes are accessible via ivl_scope_attr_cnt and ivl_scope_attr_val
    from ivl_target.h
2017-03-16 07:41:10 -07:00
Johann Klammer 8bccbe7cb5 fix for const eval 2017-02-17 09:55:39 -08:00
Martin Whitaker 7bed181f68 Support timescales in design units that aren't inside a module.
SystemVerilog allows tasks, functions, and classes to be defined at the
root level or inside packages, so we can't rely on an enclosing module
being present to provide the timescale.
2016-07-22 22:48:20 +01:00