Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create ++ operator in VHDL

I would like to have a new C++ style operator for the STD_LOGIC_VECTOR type. So far I managed to create and use the following function:

FUNCTION PLUS_ONE ( a : STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR is 
BEGIN
    RETURN std_logic_vector( unsigned( a ) + 1);
END FUNCTION;

now if i create this:

FUNCTION "++" ( a : STD_LOGIC_VECTOR) RETURN      STD_LOGIC_VECTOR is 
BEGIN
    RETURN std_logic_vector( unsigned( a ) + 1);
END FUNCTION;

ISE throws the following error:

"++" is not a predefined operator.

Now the question is, is it possible to create new operators in VHDL an I am missing something

like image 241
klassenpeter Avatar asked Mar 22 '23 09:03

klassenpeter


1 Answers

You can only overload operators in VHDL, you cannot create new operator symbols. Quoting the LRM (section 4.5.2):

The declaration of a function whose designator is an operator symbol is used to overload an operator. The sequence of characters of the operator symbol shall be one of the operators in the operator classes defined in 9.2.

And the corresponding section of the manual says:

condition_operator ::= ??
logical_operator ::= and | or | nand | nor | xor | xnor
relational_operator ::= = | /= | < | <= | > | >= | ?= | ?/= | ?< | ?<= | ?> | ?>=
shift_operator ::= sll | srl | sla | sra | rol | ror
adding_operator ::= + | – | &
sign ::= + | –
multiplying_operator ::= * | / | mod | rem
miscellaneous_operator ::= ** | abs | not

As much as I like brevity, I must admit that choosing shorthand operators over standard ways of writing expressions is "syntactic sugar", and has a potential to obfuscate the code. It is interesting to note that "trendier" languages like Python and Ruby don't have a ++ operator as well.

Could VHDL support the ++ operator? I'm currently working on a VHDL parser, and I risk saying that adding a postfix ++ operator would break quite a few rules of the language grammar, especially because unary operators expect to take an operand to the right of the symbol. Owing to this and to the fact that aren't many strong arguments in favor of such a change, I don't expect to see it anytime soon. All thigs considered, my personal choice has been to stick with value := value + 1 for standard data types.

like image 196
rick Avatar answered Mar 27 '23 21:03

rick