Page 1 of 1

[untagged] magic

Posted: Fri Jul 10, 2020 9:55 am
by adimetrius
Colleagues,

why does the following work - I mean, I cannot find a specification for this behavior, other than the text of the compiler itself; but then the compiler is an implementation that should comply with a certain definition, in our case - the Language Report and, possibly, the Platform-Specific issues. I searched as best I could, but couldn't find a specification in either.
VAR s: POINTER TO ARRAY [untagged] OF CHAR;
t: POINTER TO ARRAY OF CHAR;
s := "Why does this work?!"';
s := t^; (* just as bad *)

Re: [untagged] magic

Posted: Fri Jul 10, 2020 3:37 pm
by luowy
[untagged] is P-S-I, implemented without careful document,

if you understand this assignment is legal:
s:=a;
(*
s: POINTER TO ARRAY [untagged] OF CHAR;
a:ARRAY 32 OF CHAR;
*)
untagged pointer of array can be assigned by a tagged array, the value of the pointer will be the address of the first element of the array;

P.S. "POINTER TO ARRAY [untagged] OF CHAR" same as "POINTER[untagged] TO ARRAY [untagged] OF CHAR"

then your example will be easy to understand;
s := "Why does this work?!"';
s := t^; (* just as bad *)

Re: [untagged] magic

Posted: Fri Jul 10, 2020 3:46 pm
by Zinn
You should use the [untagged] flag only for interfacing data and DLLs outside BlackBox. For modules written inside Blackbox you should never use the [untagged] flag.

The documentation advice:

Always use untagged records as replacements for C structures, in order to avoid the allocation of a type tag for the garbage collector. The system flag [untagged] marks a type as untagged (no type information is available at run-time) with standard alignment rules for record fields (2-byte fields are aligned to 2-byte boundaries, 4-byte or larger fields to 4-byte boundaries). The system flags [noalign], [align2], and [align8] also identify untagged types but with different alignments for record fields.
Like all system flags, "untagged" can only be used if module SYSTEM is imported.

You find the following rules for untagged variables, records and pointers in the documentation:

- No typetag and no type descriptor is allocated.
- The garbage collector ignores untagged variables.
- NEW is not allowed on pointers to untagged variables.
- Pointers to this array type inherit the attribute of being untagged.
- Only one-dimensional untagged open arrays are allowed.
- For open untagged arrays, index bounds are not checked.

I hope that helps.
- Helmut