Recent

Author Topic: Can Absolute be used on string / packed record  (Read 6292 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can Absolute be used on string / packed record
« Reply #60 on: July 01, 2022, 11:02:11 am »
This is the same with newtonic gravety and einsteinian relativity. Sure Newton is not correct, we know that it is wrong for some cases, close to the speed of light or for massive objects, but it is still valid for anything human sized, so while not being correct, and proven to be wrong after a few hundred years, it is still correct enough to be useful.

The important thing is that we have to accept that current knowledge and practice- and this applies both to mathematical astronomy and practical programming- can still be refined and improved.

None of Newton, Einstein, Kay, Wirth or Stroustrop were divine messengers delivering unto humanity an immutable revelation and in fact generally speaking none of them claimed to have the last word which would never be improved upon.

There is one exception: Ockham, who embodied the principle "Entities are not to be multiplied without necessity".

However, I would argue there that that principle, in the context of programming languages, applies to the addition of "syntactic sugar" support to the compiler, rather than any concern for eliminating boilerplate at the application level. If boilerplate can be eliminated by a macro facility or equivalent then well and good, but that should never impact on the reliability or maintainability of the development tools.

And one of the sharpest tools in modern programming, introduced by Wirth at the behest of Hoare, is a strong type system. And while I have in the past expressed my reservations about some of Wirth's behaviour, he recognised in the earliest days of Pascal that there had to be some way to do a binary conversion from one type to another, hence the untagged variant record. And he later went on in Modula-2 to refine that, by specifying the distinction between type transfers that were a pure binary overlay, and conversions that did their best to "do the right thing" like treating an integer as a floating point number.

But still, in both languages, there remained the possibility of defining an untagged variant record as a type and then using a transfer via that type to achieve a tricky binary conversion. It's not something used often, it's definitely not something for a beginner or a junior team member, and there is absolutely no need to use a concise notation like "absolute" at the point of declaration which has the effect of obscuring the fact that something tricky is being attempted at the point of use.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can Absolute be used on string / packed record
« Reply #61 on: July 01, 2022, 12:12:49 pm »
The problem is that some people (we know who they are) paint "absolute" as if it were some kind of esoteric "dirty trick" that a programmer should stay away from and, the reality is, "absolute" is a great feature when used correctly. 

Examples that come to mind are in compare functions for routines such as qsort and bsearch or any other routine that uses generic pointers.  In those cases, the simplest, cleanest and correct solution is to "absolute" variable declarations with the proper type on top of the generic pointers.  Without "absolute" every use of the generic pointer would require a typecast.

"absolute" is by far, the cleanest way of "dis-ambiguating" accesses to structures such as the optional header or the import thunks, those two among thousands of similar situations.

It should be a clue that MS didn't create a single optional header or import thunk using variants/unions.  The result would be a cumbersome, error prone mess.

All that said, "absolute" isn't a magic bullet but, neither are variants and, there are many cases where "absolute" is a significantly superior way of solving a problem than using variants/unions.

On a side note, the scientific method is great stuff but, it rarely _proves_ something.  The best it can normally do is show that under very specific conditions, the behavior of a system (usually a physical system) can be represented by one or more mathematical equations.

Nowadays, the scientific method faces a great adversary, namely "the money method".  There are too many financial interests out there throwing money at "proving" whatever it is that will bring more money to them.  The result is, there are a lot of rather dubious claims about a great number of things out there.  It's wise to take "proofs" with a grain of salt.




(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can Absolute be used on string / packed record
« Reply #62 on: July 01, 2022, 12:29:56 pm »
Examples that come to mind are in compare functions for routines such as qsort and bsearch or any other routine that uses generic pointers.  In those cases, the simplest, cleanest and correct solution is to "absolute" variable declarations with the proper type on top of the generic pointers.  Without "absolute" every use of the generic pointer would require a typecast.

"absolute" is by far, the cleanest way of "dis-ambiguating" accesses to structures such as the optional header or the import thunks, those two among thousands of similar situations.

I'd like to see @PascalDragon comment on that. The core question is whether an untagged variant record can be used in this context, i.e. with the currently-assumed type indicated by a named field, or whether it /has/ to be done using absolute.

Having said which, the dot- originally used only to indicate a field in a record- is getting perilously overloaded. But that's an argument for some other time.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Can Absolute be used on string / packed record
« Reply #63 on: July 01, 2022, 01:27:35 pm »
On a side note, the scientific method is great stuff but, it rarely _proves_ something.  The best it can normally do is show that under very specific conditions, the
Karl Popper already said that the proof is in the pudding: one should disproof, not confirm general behavior.
"empirical sciences can never be proven, but it can be falsified, meaning that it can (and should) be scrutinised with decisive experiments."

This is also the most valuable lesson I learned in my academic career.

https://en.wikipedia.org/wiki/Karl_Popper#Falsification_and_the_problem_of_induction
Even beginners should grasp this.
In short ONE counter example disproofs many confirmations.
« Last Edit: July 01, 2022, 01:37:48 pm by Thaddy »
Specialize a type, not a var.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Can Absolute be used on string / packed record
« Reply #64 on: July 01, 2022, 02:03:25 pm »
All that said, "absolute" isn't a magic bullet but, neither are variants and, there are many cases where "absolute" is a significantly superior way of solving a problem than using variants/unions.
To get back to the core issue here, I think that everything absolute can do, can also be done using pointers, but better. To get back to my original example in #26:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. var
  4.   vCurr  : Currency;
  5.   vInt64 : PInt64 = @vCurr;
  6. begin
  7.   { Trunc currency cheaply (much simpler than FPC implementation) }
  8.   vCurr := 12345.6789;
  9.   WriteLn(vCurr);
  10.   vInt64^ := vInt64^ - (vInt64^ mod 10000); // Trunc the currency
  11.   WriteLn(vCurr);
  12.   ReadLn;
  13. end.
This does exactly the same as "vInt64 : Int64 absolute vCurr" but because it is a pointer, it signalizes to the user that it actually is not it's own variable but just a reference. So same functionality but more expressive at point of use. Also there isn't much of a penalty for doing this, in this exampole it is actually shorter than the absolute variant. If you are accessing foreign memory, your code should make that clear, and absolute is designed to hide this.
So I do think that absolute should always be avoided, because I don't see any reason why not to use a pointer which does the same but gives more clarity about what it is.

The whole topic on variant records was about your example. But for the things you brought up here (e.g. quicksort with generic pointers), there a variant record would be useless, I agree, but I still think that simply casting it to a typed pointer and using that afterwards is a much cleaner solution

Or to put it simply: if you do something special (which accessing another variables memory is) it should look special in the code

On a side note, the scientific method is great stuff but, it rarely _proves_ something.  The best it can normally do is show that under very specific conditions, the behavior of a system (usually a physical system) can be represented by one or more mathematical equations.

Nowadays, the scientific method faces a great adversary, namely "the money method".  There are too many financial interests out there throwing money at "proving" whatever it is that will bring more money to them.  The result is, there are a lot of rather dubious claims about a great number of things out there.  It's wise to take "proofs" with a grain of salt.
As someone who worked in research I can tell you, when we are talking about public research (i.e. universities), which is most of the research, while funding is always a fight for, there is not necessarily a specific interest to find something specific (it's more of an interest to find something interesting), so while there is a clear bias to finding new shiny things (and for example negative results are underrepresented in publications), it's not like most papers are just bought to find something.

And it's also not just about mathematical formulas, it's often about finding correlations that are to unlikely to be random, so when you test a new medical product, you want to know if the health improvements correlate with the use of that product, while trying eliminating all other factors. Real mathematical models are mostly in the "hard" sciences (like physics and to a certain degree chemistry), where the systems are so "simple" that you can make a formular, but for example in medicine (where I for example have a certification for doing meta analysis of medical studies, if I ever bother to grab it from the university chair where I did it), the systems are way to complex to model them with formulars, where we can only use very simplified models, and most of the time it boils down to "does the treatment work or not" (and with what probability)

Even beginners should grasp this.
In short ONE counter example disproofs many confirmations.
Yes and no. One counterexample disproves a hypothesis, but the confirmations already found must therefore belong to another hypothesis that encompasses the knowledge already gained.
Going back to newtonian gravity. For example the orbit of mercury does not follow newtons law, so one observation disproves it. That doesn't mean that because of that a stone on earth won't fall with an acceleration of 9.81 m/s². Newtons law still holds for most cases, even though we know it is wrong, it is still useful.
Same holds for other things like the atom model according to Bohr or others. Findings today are still useful, even though they might not be correct and be disproven in the future.
This is the beauty of the scientific method, it is not just a good way of finding truth, but it also gives useful results along the way, even if they might not be correct
« Last Edit: July 01, 2022, 02:16:05 pm by Warfley »

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Can Absolute be used on string / packed record
« Reply #65 on: July 01, 2022, 02:42:05 pm »
All that said, "absolute" isn't a magic bullet but, neither are variants and, there are many cases where "absolute" is a significantly superior way of solving a problem than using variants/unions.
To get back to the core issue here, I think that everything absolute can do, can also be done using pointers, but better. To get back to my original example in #26:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. var
  4.   vCurr  : Currency;
  5.   vInt64 : PInt64 = @vCurr;
  6. begin
  7.   { Trunc currency cheaply (much simpler than FPC implementation) }
  8.   vCurr := 12345.6789;
  9.   WriteLn(vCurr);
  10.   vInt64^ := vInt64^ - (vInt64^ mod 10000); // Trunc the currency
  11.   WriteLn(vCurr);
  12.   ReadLn;
  13. end.
This does exactly the same as "vInt64 : Int64 absolute vCurr" but because it is a pointer, it signalizes to the user that it actually is not it's own variable but just a reference. So same functionality but more expressive at point of use. Also there isn't much of a penalty for doing this, in this exampole it is actually shorter than the absolute variant. If you are accessing foreign memory, your code should make that clear, and absolute is designed to hide this.
So I do think that absolute should always be avoided, because I don't see any reason why not to use a pointer which does the same but gives more clarity about what it is.
*snip*

As far as I can remember, at the time of TP3.0 the pointer rules were not so relaxed, and the absolute was the very convenient way to circumvent this.
Then TC1.0 came along and then some of us, being a new C converts, found that it can be expressed as:
Code: C  [Select][+][-]
  1. unsigned char far *scr = (unsigned char far *)0xb8000000;
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can Absolute be used on string / packed record
« Reply #66 on: July 01, 2022, 03:42:34 pm »
Then TC1.0 came along and then some of us, being a new C converts, found that it can be expressed as:
Code: C  [Select][+][-]
  1. unsigned char far *scr = (unsigned char far *)0xb8000000;

However, a compiler of that era would interpret that as literally having a pointer variable in memory, which could potentially be overwritten, and even if only repeatedly dereferenced after its creation would never be optimised to a write to an immediate address.

Absolute, plus possibly an equivalent term for the I/O space, is superior for that specific usage... in fact I would go so far as to say that it's indispensable for a language which has any pretence towards system programming.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Can Absolute be used on string / packed record
« Reply #67 on: July 01, 2022, 04:53:00 pm »
Code: C  [Select][+][-]
  1. unsigned char far *scr = (unsigned char far *)0xb8000000;
Lets admit that to obfuscate things, such construct should absolute'ly be introduced in Free Pascal. That would guaranty that an idiotic fool wouldn't pointer'dly mess or try to understand such straightforward readable code...   

@y.ivanov, thanks for your code sample, that was much related to the OP initial question, that might have been a bit clearer.
[Edit]OP question might have been a bit clearer.
« Last Edit: July 01, 2022, 04:56:12 pm by BrunoK »

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Can Absolute be used on string / packed record
« Reply #68 on: July 01, 2022, 05:55:33 pm »
Lets admit that to obfuscate things, such construct should absolute'ly be introduced in Free Pascal. That would guaranty that an idiotic fool wouldn't pointer'dly mess or try to understand such straightforward readable code...   
What do you think is so bad about this? Except for the C cast syntax which is terrible, the message here is really clear, it states "take this number (0xb8000000) and treat this as if it was a pointer to this specific type (unsigned char far)".
Sure it's a bit stupid that you have the type twice there, and the cast syntax and the type naming is a bit stupid, this all is improved by C++ where this would be:
Code: C  [Select][+][-]
  1. auto src = reinterpret_cast<byte far *>(0xb8000000);
Which you can nearly read as plain english "variable src is the value 0xb8000000 (re)interpreted as a byte pointer". It leaves no doubt about what this code does, even for someone who has never seen anything like it. And what is actually great, it uses only casts and pointer, two of the most common things in programming, something any programmer is likely to have a lot of experience with it. So you directly know what happens here even if you have never seen anything like it before (unlike absolute, as you can use pascal for decades without encountering this in any project you worked on).

I dislike the C syntax for many things, especially the casting with (target)val is pretty bad (combine this with function pointers and you have a mess of a codebase), but what I actually like quite a lot is how strict the typesystem in C is. I think that Pascal is a bit to lax on that, not just with absolute but also with pointers, for example that I can do this:
Code: Pascal  [Select][+][-]
  1. var
  2.   c: Char;
  3.   i: PInteger = @c;
  4. begin
  5.   i^ := 42;
  6. end.
Completely smashing my memory without even a hint that what I am doing might be dangerous and bad. C will throw a warning and C++ outright refuses to compile anything like this without an explicit cast as a "I'm sure I want this and I know what I'm doing" message to the compiler

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can Absolute be used on string / packed record
« Reply #69 on: July 01, 2022, 06:12:38 pm »
What do you think is so bad about this? Except for the C cast syntax which is terrible, the message here is really clear, it states "take this number (0xb8000000) and treat this as if it was a pointer to this specific type (unsigned char far)".

What is wrong with that is that it doesn't give the linker and (anything that does the job of an) OS any sort of hint that you're making direct access to a particular hardware address.

The way that should be done is by using whatever facilities are available in the toolchain to declare a variable of appropriate size with appropriate attributes, so that the toolchain can do whatever is necessary to place it in an appropriate group/section of whatever binary format is in use.

In early TP, that's what the absolute keyword was for. I'm fine with that, I'm not fine with the same keyword being reused to overlay variables of potentially different types (already handled by untagged variant records), in the same way that I'm not fine with const being reused to indicate a static variable with limited visibility.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Can Absolute be used on string / packed record
« Reply #70 on: July 01, 2022, 06:27:28 pm »
What is wrong with that is that it doesn't give the linker and (anything that does the job of an) OS any sort of hint that you're making direct access to a particular hardware address.
This is coverd by the type system, if you do DMA you need to use the "volatile" modifier for your type so it is "volatile byte *" or "byte volatile *" (I like the latter one more because it is the strict rule while the first one is just syntactic sugar for the second one). I guess that this is what the "far" was for in the example above, but I don't really know. But yeah if you want to do DMA, you need to defile your variable as a pointer to a volatile memory region (and casting away the volatile will throw a bunch of warnings to let you know that you are doing something bad).
This has the advantage that this information is transitive, so if you take a pointer to that pointer, it knows the underlying data is still volatile, or if you pass that pointer to something else it will retain the volatile information

But I do agree that this is not a bad use of absolute, even though I like it more to stick to a few well developed language features rather than having a special case for everything (for example I really like smalltalk, which has only like 4 keywords in total, everything else, including loops and if-conditions are actually defined within language as classes). I think that syntactic sugar is justified for features that are really common, so to optimise the hot path, and everything less common should use simple features to keep the language slim and make learning easier. Great example for this is I think Haskell, it has a lot of syntactic sugar for things like monads and other stuff that is really common, but otherwise is a very slim language which you can pretty much learn in full in 1-2 days just reading the wiki
« Last Edit: July 01, 2022, 06:33:07 pm by Warfley »

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can Absolute be used on string / packed record
« Reply #71 on: July 01, 2022, 06:56:11 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. var
  4.   vCurr  : Currency;
  5.   vInt64 : PInt64 = @vCurr;
  6. begin
  7.   { Trunc currency cheaply (much simpler than FPC implementation) }
  8.   vCurr := 12345.6789;
  9.   WriteLn(vCurr);
  10.   vInt64^ := vInt64^ - (vInt64^ mod 10000); // Trunc the currency
  11.   WriteLn(vCurr);
  12.   ReadLn;
  13. end.
And BrunoK's reply about that was right on the money.  Let's face it, the code is simpler using "absolute".   Have programmers become allergic to simplicity ?

It's also surprising that a programmer (more than one in this thread actually) who openly embraces the hiding of pointers by the use of classes has an issue with the pointer "hiding" "absolute" does (and "absolute" doesn't hide a pointer any more than a run-of-the-mill variable name does.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can Absolute be used on string / packed record
« Reply #72 on: July 01, 2022, 07:04:52 pm »
This is coverd by the type system, if you do DMA you need to use the "volatile" modifier for your type so it is "volatile byte *" or "byte volatile *" (I like the latter one more because it is the strict rule while the first one is just syntactic sugar for the second one). I guess that this is what the "far" was for in the example above, but I don't really know. But yeah if you want to do DMA, you need to defile your variable as a pointer to a volatile memory region (and casting away the volatile will throw a bunch of warnings to let you know that you are doing something bad).
This has the advantage that this information is transitive, so if you take a pointer to that pointer, it knows the underlying data is still volatile, or if you pass that pointer to something else it will retain the volatile information

Sorry, that is /not/ covered by the type system or any modification keywords. The specific case I was thinking about was stuff I've done in the past where a binder utility read an executable and from it built an embedded-system image with x86 descriptors etc., but an equally good example was something I posted a few days ago which showed how a variable on an Arduino can be placed in a particular region of memory such that it isn't overwritten after a watchdog reset forces a hardware restart (allowing the details of the restart to be fed back to a remote host).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Can Absolute be used on string / packed record
« Reply #73 on: July 01, 2022, 08:11:37 pm »
It's also surprising that a programmer (more than one in this thread actually) who openly embraces the hiding of pointers by the use of classes has an issue with the pointer "hiding" "absolute" does (and "absolute" doesn't hide a pointer any more than a run-of-the-mill variable name does.)
I hope you aren't talking about me because if you have read any of my posts on the topic you might know that I hate classes in object pascal exactly because they hide the pointer. I would rather have all the features classes have also incorporated into objects, which function as records with respect to memory management (in fact I think advanced records are superior to both classes and objects, but implementing inheritance is a real hack there).
I think the mixing of value types and reference types is the worst thing that languages like Java, C#, Python and yes also ObjectPascal have made really common.

Code should at all times give you all the information you need to understand that code, this includes marking references as such and not hiding them. I think that this comes from a misguided understanding that some programming language designer seem to have had somewhen in the 80s that because so many people have problems understanding pointers the solution must be to hide them and pretend that they don't exist.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can Absolute be used on string / packed record
« Reply #74 on: July 01, 2022, 09:06:14 pm »
I hope you aren't talking about me because if you have read any of my posts on the topic you might know that I hate classes in object pascal exactly because they hide the pointer.
I did include you in that comment but, I admit, you're definitely far from the worst case in that area.

As far as the example you posted:
Code: Pascal  [Select][+][-]
  1. {$TYPEDADDRESS ON}
  2.  
  3. program _TypedAddress;
  4.  
  5. var
  6.   c: Char;
  7.   i: PInteger = @c;
  8. begin
  9.   i^ := 42;
  10. end.                  
You do have a point there but, that kind of mistake can be avoided by specifying {$TYPEDADDRESS ON} (the above does _not_ compile.)  Unfortunately, it isn't the default (another Borland-ism likely due to the fact that making it the default would have prevented existing code from compiling.)


Ideally, {$TYPEDADDRESS ON} should be the compiler's intrinsic behavior, IOW, not even an option and, if for some reason someone wants to retype a variable (which is what that code does) then, using "absolute" is the proper method (though obviously in this case, it would be used incorrectly.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018