Mailing lists / fpc-devel / 2017-12
An extension of fpc language: the BASED construct
17 messages · 7 people · –
- GGiuliano Colla 6
- SSven Barth 3
- MMark Morgan Lloyd 2
- OOzz Nixon 2
- TThorsten Engler 2
- KKaroly Balogh (Charlie/SGR 1
- +1 more
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Giuliano Colla wrote 20 lines
What does gdb (and possibly other debuggers) make of this? Is it really appropriate to declare Item as a variable, when it's really more akin to a macro?
Item: BYTE BASED ItemPtr;
Ignoring any other considerations for now, I would have at least used a logical extension derived from already supported syntax:
Item: Byte absolute ItemPtr^;What does gdb (and possibly other debuggers) make of this? Is it really appropriate to…
I like the concept, I would like to ask that we do not support INTEGER as a type, and be more specific by limiting to SHORTINT, SMALLINT, LONGINT, INT64. That way code snippets are not confusing when you implement 16bit INT logic on a 32bit INT environment. Because I do a lot of documentation for n00bs in Pascal - the interchangeable INTEGER is probably one of Borland's biggest mistakes with the grammar! I would love to see FPC make the statement that the type INTEGER is 16bit only - deal with people freaking out for a year or two, and move the language forward. Even the INTEGER example here is assuming 16bit - which would be incorrect on 32bit platforms in specific $MODE. (may have picked wrong wording, however, I think everyone see's the point).
Other than that, if FPC adds this, I will add it to Modern Pascal, so at least the two products are supporting "BASED".
On Tue, Dec 26, 2017 at 8:27 AM, Mark Morgan Lloyd <
Show quoted text 40 lines
See: http://tutorial.modernpascal.com/?DataTypes
I implemented INTEGER as 32bit, but, if FPC makes it 16bit only, I will go make the change right now. My end-users all know *DO NOT USE INTEGER* it's even in the license agreement. "It is only there for make code compatible w/ Delphi and FPC." I am still spending all my spare time to get MPC's tutorial site up to par with FPC - as I promote MPC as FPC's Script Engine.
Ozz Nixon wrote 59 lines
Ignoring any other considerations for now, I would have at least used a logical extension…
"Thorsten Engler" wroteItem: BYTE BASED ItemPtr;
Ignoring any other considerations for now, I would have at least used a logical extension derived from already supported syntax:
Item: Byte absolute ItemPtr^;I agree, as it avoids adding a new keyword (even if it is context specific).
Regards,
Sven
Am 26.12.2017 13:33 schrieb "Giuliano Colla" :
If the idea is not rejected, then a number of refinements (which I'm ready to implement) are required to make the feature generally available:
My following remarks are independent of an eventual acceptance of the feature :
- All architectures should be supported (now it's only for x86_64 - symcpu.pas).
It might be possible to implement this in a platform independent way that might simply need to expand the capabilites of "absolute".
- It should be decided if to make the feature generally available, or conditioned by an {$IFDEF FPCHASBASED}
- It should be decided if the feature should be available in all modes or only in a selected number of modes (what of TurboPascal Mode or Delphi Mode?).
Rule of thumb: new features added as a separate mode switch, no FPC_HAS_* define necessary as by definition this can be done with version checks (there is only one trunk version, namely the current one).
- It should be decided if error messages should be specific for the BASED construct or common to the ABSOLUTE one, slightly rephrasing the error message.
Might be one or the other depending on the message.
- It should be decided if internal error # which currently keep the same number of the nearby ABSOLUTE internal error should be given new values.
Internal errors shall always be unique as they are used to find the error location, even for copy pasted code.
Regards,
Sven
Ignoring any other considerations for now, I would have at least used a logical extension…
HI,
Thorsten Engler wrote 6 lines
As far as I understand (I did not try) this was once supported, but explicitly removed by FPC 2.4.0 from Absolute keyword dereferencing is no longer allowed:
http://wiki.freepascal.org/User_Changes_2.4.0#Absolute_variable_declarations
As the reasoning there shows, it's a very slippery slope this, which could open a can of worms, so careful people. :)
Charlie
In that case, if the functionality is going to be included, the best course of action might be to restore pre 2.4.0 behaviour, add the ability to use explicit dereference, and bind it to a separate mode switch.
Binding it to a mode switch solves the Delphi compatibility reason for removing it. Allowing both explicit and implicit dereference solves the consistency reason for removing it.
And as Sven said, allowing absolute with expressions would be preferable to introducing a new keyword.
Show quoted text 34 lines
What does gdb (and possibly other debuggers) make of this? Is it really appropriate to…
Il 26/12/2017 14:27, Mark Morgan Lloyd ha scritto:
What does gdb (and possibly other debuggers) make of this?
What currently gdb tells (and any other debugger would tell) is :
No symbol "Item" in current context.
Once the appropriate entries are implemented in the debugger symbol table, it will behave like it does in similar conditions, i.e. displaying a value referenced by a pointer. I didn't mention in my ToDo list because I'm a bit lazy, but this too has to be done.
Is it really appropriate to declare Item as a variable, when it's really more akin to a macro?
It's not different from the declarations:
myString: string;
myObject: TObject;where your declaration only reserves a pointer, while the actual string or object will be instantiated only at run time.
Item is a variable, whose location isn't known at compile time, but will be known only at run time.
Giuliano
As far as I understand (I did not try) this was once supported, but explicitly removed by…
Il 26/12/2017 18:43, Karoly Balogh (Charlie/SGR) ha scritto:
Show quoted text 17 lines
I'd not be in favour of using the same syntax for two quite different beasts.
Although the practice implementation is similar, the semantics is completely different:
ABSOLUTE declares a variable whose storage is known and assigned at compile time.
BASED declares a variable whose storage is unknown, and will only be instantiated at run time.
Mixing the two things may cause a lot of troubles, and most likely that's the reason why it was removed from fpc 2.4.0.
Giuliano
If the idea is not rejected, then a number of refinements (which I'm ready to implement)…
Il 26/12/2017 18:26, Sven Barth via fpc-devel ha scritto:
Show quoted text 16 lines
The feature in itself is platform independent.
The only catch is that in pdecvar.pas (where variable declaration is handled) there's a per platform hook for each symbol type, in order to allow for special handling of some features.
For consistency I did follow the same rule, which is used for all the other declarations, disregarding whether they're platform independent or not.
As a consequence, for each supported platform you must add in its specific symcpu.pas a type entry:
tcpubasedvarsym = class(tbasedvarsym)
end;
tcpubasedvarsymclass = class of tcpubasedvarsym;and a line of code:
cbasedvarsym:=tcpubasedvarsym;As this is tedious, I've only done it for the platform I'm using (x86_64/symcpu.pas). To deploy the feature, the same lines must be added in each platform symcpu.pas unit. There's no more that that.
Show quoted text 24 lines
What's the rule to avoiding clashes? I gathered sort of YYYYMMDD## is it correct?
Giuliano
As far as I understand (I did not try) this was once supported, but explicitly removed by…
Il 26/12/2017 18:43, Karoly Balogh (Charlie/SGR) ha scritto:
Show quoted text 8 lines
What was allowed before 2.4.0 was *implicit* keyword dereferencing, while explicit dereferencing has never been allowed. This was rather inconsistent, and rightfully suppressed.
Giuliano
Il 26/12/2017 14:27, Mark Morgan Lloyd ha scritto: What currently gdb tells (and any…
Giuliano Colla wrote 8 lines
I'm getting uncomfortable with the amount of "magic" required here.
Show quoted text 8 lines
Except that you've already said above that "Item" doesn't actually exist, while "myString" does even if the compiler etc. knows that it's to be implicitly dereferenced.
I'd suggest that this would be better approached either as a generalisation of managed types- strings and the rest, or as a final resolution of the "with" controversy including full consideration of the scope issues.
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
Il 26/12/2017 18:26, Sven Barth via fpc-devel ha scritto: The feature in itself is…
Am 27.12.2017 00:54 schrieb "Giuliano Colla" :
Il 26/12/2017 18:26, Sven Barth via fpc-devel ha scritto:
Am 26.12.2017 13:33 schrieb "Giuliano Colla" :
If the idea is not rejected, then a number of refinements (which I'm ready to implement) are required to make the feature generally available:
My following remarks are independent of an eventual acceptance of the feature :
- All architectures should be supported (now it's only for x86_64 - symcpu.pas).
It might be possible to implement this in a platform independent way that might simply need to expand the capabilites of "absolute".
The feature in itself is platform independent.
The only catch is that in pdecvar.pas (where variable declaration is handled) there's a per platform hook for each symbol type, in order to allow for special handling of some features.
For consistency I did follow the same rule, which is used for all the other declarations, disregarding whether they're platform independent or not. As a consequence, for each supported platform you must add in its specific symcpu.pas a type entry:
tcpubasedvarsym = class(tbasedvarsym)
end;
tcpubasedvarsymclass = class of tcpubasedvarsym;and a line of code:
cbasedvarsym:=tcpubasedvarsym;As this is tedious, I've only done it for the platform I'm using (x86_64/symcpu.pas). To deploy the feature, the same lines must be added in each platform symcpu.pas unit. There's no more that that.
No matter the syntax that might be chosen for this it will likely be sufficient to handle that feature by an absolutevarsym with a boolean flag or something like that. The difference to an ordinary absolute variable appears to be too small to warrant a new sym type.
- It should be decided if internal error # which currently keep the same number of the nearby ABSOLUTE internal error should be given new values.
Internal errors shall always be unique as they are used to find the error location, even for copy pasted code.
What's the rule to avoiding clashes? I gathered sort of YYYYMMDD## is it correct?
Correct.
Regards,
Sven
Il 27/12/2017 15:05, Sven Barth via fpc-devel ha scritto:
Show quoted text 5 lines
After struggling sometime to find out the best way to implement the based feature without adding a new keyword, I found that the syntax:
aVar: anyType ABSOLUTE somePTR^;can be implemented with minimal changes.
Attached the patch which does the trick, and which works like the old proven one.
It doesn't support *implicit* dereferencing, because I've left untouched the code which filters it out.
One could remove the filtering for consistency, but I don't like very much the idea: the aim of such a feature is to provide a more readable and understandable code, not the opposite.
Giuliano
