关于system-verilog中的struct?

问题描述 投票:0回答:2

在struct的声明中添加函数时,我得到了vcs编译错误。 IEEE doc没有提到是否允许struct中的函数。

尝试为字段分配默认值时,我也遇到了vcs编译错误。但它允许在IEEE-1800-2012 7.2.2。

typedef struct {
        int a = 1; //compile error here
        int b;
        function void func();
                 b = a;
        endfunction
 } a_struct;

所以我按照建议添加命令行和错误信息:

vcs -sverilog a.sv

Error-[V2KIIAD] Invalid initialization at declaration
....
Struct or union member field 'a' cannot be initialized at declaration.

Error-[SE] Syntax error
  Following verilog source has syntax error :
  "a.sv", 4: token is 'function'
     function void func();
             ^

我的vcs版本是2013.06-SP1-10

system-verilog
2个回答
1
投票

IEEE Std 1800-2012开始,不支持在结构体内声明的函数。查看结构声明的语法,struct_union_member是data_type_or_void而函数不是data_type。

§7.2结构

data_type ::= // from A.2.2.1
...
| struct_union [ packed [ signing ] ] {  struct_union_member { struct_union_member } }
{ packed_dimension }
struct_union_member ::=
{ attribute_instance } [random_qualifier] data_type_or_void list_of_variable_decl_assignments ;
data_type_or_void ::= data_type | void
struct_union ::= struct | union [ tagged ]

从§A.2.2.1Net和变量类型扩展data_type

data_type ::=
integer_vector_type [ signing ] { packed_dimension }
| integer_atom_type [ signing ]
| non_integer_type
| struct_union [ packed [ signing ] ] {  struct_union_member { struct_union_member } }
{ packed_dimension }
| enum [ enum_base_type ] {  enum_name_declaration { ,  enum_name_declaration } }
{ packed_dimension }
| string
| chandle
| virtual [ interface ] interface_identifier [ parameter_value_assignment ] [ .  modport_identifier ]
| [ class_scope | package_scope ] type_identifier { packed_dimension }
| class_type
| event
| ps_covergroup_identifier
| type_reference

0
投票

我们知道结构是静态的,类是动态数据类型。所以在SV中,当我们使用类时,我们可以在初始或其他条件下分配值。但是对于结构,我们不能初始化声明中的任何变量。

© www.soinside.com 2019 - 2024. All rights reserved.