|
|
|
|
|
|
|
|
|
|
|
Assignment Rules
|
|
|
Assignment rules define what is a legal assignment and how values are assigned to entities.
|
|
|
|
|
|
There are several legal ways to assign values:
|
|
|
|
|
|
- Assignment actions
- Return actions
- Parameter passing
- Variable declaration
|
|
|
|
|
|
Assignment of Numeric Types
|
|
|
Any numeric type (for example, uint, int, or one of their subtypes) can be assigned to any other numeric type. Untyped expressions, such as HDL objects, can also appear in assignments of numeric types.
|
|
|
|
|
|
Example
|
|
|
1 <'
2 extend sys {
3 ! x1: int;
4 x2: uint (bits: 3);
5 ! x3: int [10..100];
6 run() is also {
7 print x1;
8 print x2;
9 print x3;
10 x1 = x2;
11 x3 = x1;
12 var x:
13 int (bits: 48) = x3;
14 print x1;
15 print x2;
16 print x3;
17 };
18 };
19
20 '>
You could download file assign.e
here
|
|
|
|
|
|
x1 = 0
x2 = 7
x3 = 0
x1 = 7
x2 = 7
x3 = 7
|
|
|
|
|
|
Automatic casting is performed when a numeric type is assigned to a different numeric type, and automatic extension or truncation is performed if the types have different bit size.
|
|
|
|
|
|
Assignment of Boolean Types
|
|
|
A Boolean type can only be assigned with another Boolean type.
|
|
|
|
|
|
|
|
|
|
|
|
Assignment of Enumerated Types
|
|
|
An enumerated type can be assigned with that same type, or with its scalar subtype. (The scalar subtype differs only in range or bit size from the base type.) The example below shows: An assignment of the same type:
|
|
|
|
|
|
Example
|
|
|
1 <'
2 type color :[red,blue,green,white,yellow];
3
4 extend sys {
5 local_color : color;
6 run() is also {
7 print local_color;
8 local_color = red;
9 print local_color;
10 // Below assignment is wrong
11 //local_color = 3;
12 };
13 };
14 '>
You could download file enumerated.e
here
|
|
|
|
|
|
local_color = white
local_color = red
|
|
|
|
|
|
Automatic Type Casting
|
|
|
During assignment of a type to a different but compatible type, automatic type casting is performed in the following contexts:
|
|
|
|
|
|
- Numeric expressions (unsigned and signed integers) of any size are automatically type cast upon assignment to different numeric types.
- Untyped expressions are automatically cast on assignment.
- Sized scalars are automatically type cast to differently sized scalars of the same type.
- Struct subtypes are automatically type cast to their base struct type.
- If the two types differ in bit size, then the assigned value is extended or truncated to the required bit size.
- Casting of small negative numbers (signed integers) to unsigned integers produces large positive numbers.
- There is no automatic casting to a reference parameter.
|
|
|
|
|
|
Complex Type Conversions
|
|
|
Since e language allows us to define complex data types, it is sometimes confusing to assign one type to another type. Once such example would be assigning a field of struct, which is declared in sub-type. E language provides as_a() method to do complex type conversion.
|
|
|
|
|
|
as_a()
|
|
|
Returns the expression, converted into the specified type. Although Specman Elite performs some casting automatically , explicit casting is required in some cases when making assignments between different but compatible types.
|
|
|
|
|
|
syntax:exp.as_a(type: type name): type
|
|
|
|
|
|
Type Conversion Between Scalars and Lists of Scalars
|
|
|
Numeric expressions (unsigned and signed integers) of any size are automatically type cast upon assignment to different numeric types. For other scalars and lists of scalars, there are a number of ways to perform type conversion, including the as_a() method, the pack() method, the %{} bit concatenation operator and various string routines.
|
|
|
|
|
|
int represents int/uint of any size, including bit, byte, and any user-created size. If a solution is specific to bit or byte, then bit or byte is explicitly stated.
|
|
|
|
|
|
From
|
To
|
How to Do it
|
list of int
|
string
|
list_of_int.as_a(string)
|
list of byte
|
string
|
list_of_int.as_a(string)
|
string
|
list of int
|
string.as_a(list of int)
|
string
|
list of byte
|
string.as_a(list of byte)
|
string
|
bool
|
bool = string.as_a(bool) ((Only TRUE and FALSE can be converted to Boolean; all other strings return an error.)
|
bool
|
string
|
string = bool.as_a(string)
|
string
|
enum
|
enum = string.as_a(enum)
|
enum
|
string
|
string = enum.as_a(string)
|
|
|
|
|
|
|
all_values()
|
|
|
Returns a list that contains all the legal values of the specified scalar type. When that type is an enumerated type, the order of the items is the same as the order in which they were defined. When the type is a numeric type, the order of the items is from the smallest to the largest.
|
|
|
|
|
|
Syntax : all_values(scalar-type: type name): list of scalar type
|
|
|
|
|
|
Note : When the specified type has more than 1million legal values, this routine gives a compile time error to alert you to possible memory abuse.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|