[fpc-devel] An extension of fpc language: the BASED construct
jn at sirrida.de
jn at sirrida.de
Wed Dec 27 11:34:05 CET 2017
Hello folks,
As described on my site
programming.sirrida.de/pascal_proposals.html#offset I would propose a
slightly more general approach to the theme. In contrast to the
presented solution my offsets are separated from the base pointer.
Calculations with offsets, e.g.:
TYPE
tr_rec = RECORD
a,b: integer;
END;
VAR
v: tr_rec;
o: OFFSET tr_rec OF integer;
BEGIN
o := OFFSET(tr_rec,a);
v.[o] := v.b;
...
END.
An probably better alternative syntax for "OFFSET(tr_rec,a)" could be
"tr_rec @ a" thereby overloading the address operator without using a
new keyword.
The symbol pair ".[" denotes access "at offset" and should be applicable
on records, objects, classes and arrays. This allows fields to be
addressed similar to array elements. It might help to speed up multiple
array accesses because the multiplication can happen earlier. In the
implementation in assembler the offset is simply added to the base
address. In contrast to general offsets, these specialized offsets can
not be added, subtracted or even scaled.
The WITH statement could be enhanced to handle offsets as well.
You may emulate the functionality as follows:
TYPE
tp_int = ^integer;
ta_8u = ARRAY [0..maxint] OF byte;
tpa_8u = ^ta_8u;
tpr_rec = ^tr_rec;
VAR
v: tr_rec;
o: integer;
BEGIN
o := integer(@tpr_rec(nil)^.a); // o := OFFSET(tr_rec,a);
tp_int(@tpa_8u(@v)^[o])^ := v.b; // v.[o] := v.b;
…
END.
As you see, there are some nasty things necessary for the emulation: We
assume that nil is zero and we use a cast from pointer to integer, and
there are some more castings. An alternative approach using "extended
syntax" (pointer arithmetic) is a little better. My proposal however is
clean and type-safe and does not need any castings.
The ARRAY [0..maxint] is an example of a substitute of a half open array
definition.
Best regards
Jasper Neumann
More information about the fpc-devel
mailing list