jasmin icon indicating copy to clipboard operation
jasmin copied to clipboard

Initialisation at declaration site

Open eponier opened this issue 1 year ago • 5 comments
trafficstars

This is a feature request to have the option to initialize a variable at declaration time, e.g. reg u64 res = 1;.

eponier avatar Feb 29 '24 15:02 eponier

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.

eponier avatar Mar 20 '24 13:03 eponier

(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.)

eponier avatar Mar 20 '24 13:03 eponier

const means that r has partial ownership (aka read-only) on the array.

vbgl avatar Mar 27 '24 12:03 vbgl

For now, we reject both r = s and r[i] = .... Maybe we should just reject the latter.

eponier avatar Mar 27 '24 14:03 eponier

Agreed (this is unrelated to the topic of this issue, though).

vbgl avatar Mar 28 '24 05:03 vbgl

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

MrDaiki avatar Sep 03 '24 12:09 MrDaiki

Done in #908.

vbgl avatar Sep 27 '24 05:09 vbgl