jasmin
jasmin copied to clipboard
Initialisation at declaration site
This is a feature request to have the option to initialize a variable at declaration time, e.g. reg u64 res = 1;.
One advantage of allowing that could be to be more strict about const arrays. For instance, for now, we allow to write
r = g, where r is a reg mut ptr and g is a global. We cannot define r as reg constr ptr, otherwise r = g is rejected. With initialization at declaration, we could write reg const ptr u64[2] r = g and refuse r = g.
(While writing that, I realize it is not clear in my mind if const means that r is a constant pointer, a pointer to constant arrays, or both.)
const means that r has partial ownership (aka read-only) on the array.
For now, we reject both r = s and r[i] = .... Maybe we should just reject the latter.
Agreed (this is unrelated to the topic of this issue, though).
In term of proper implementation, could we consider this feature to be syntactic sugar for initialize reg + affect to the right expression value ?
For exemple :
reg u64 res = 1;
Would be replaced by :
reg u64 res;
res = 1;
and then compiled normally.
Another interrogation is the specification in the case where multiples var are declared as the same time :
for example
reg u64 res1,res2 = 1;
In my opinion, this kind of syntax can create problems if the right expression is a pointer. Maybe a preferable syntax would be :
reg u64 res1 = 1, res2 = 3 ... ;
Another solution would be to forbid multiple declarations in this case
Done in #908.