@@@ operator
Roy Eltham
Posts: 3,000
So, the documentation on the @@@ operator in BSTC is very sparse. It's not clear to me what Symbols you can use it on and where. Can it be used in SPIN code or PASM or only one of the two? Is it only usable on VARs? Or does it also work on DATs?
I'm not sure how it can know at compile time the absolute address of any symbol anywhere in HUB, especially when some are not known until runtime. So it must be limited to global symbols. If you have two instances of an object with VAR/DAT stuff which one will the @@@ resolve to?
I still don't really understand why it's needed other than as a shortcut. Can someone give me some examples of using it that aren't just contrived? Also, can someone explain a case where this gives you something you can't get another way with @ and/or @@?
I need to know all the details so that I can properly add this operator to OpenSpin.
I'm not sure how it can know at compile time the absolute address of any symbol anywhere in HUB, especially when some are not known until runtime. So it must be limited to global symbols. If you have two instances of an object with VAR/DAT stuff which one will the @@@ resolve to?
I still don't really understand why it's needed other than as a shortcut. Can someone give me some examples of using it that aren't just contrived? Also, can someone explain a case where this gives you something you can't get another way with @ and/or @@?
I need to know all the details so that I can properly add this operator to OpenSpin.
Comments
You cannot do that nicely with @.
I have used this in Z80 emulation code some time.
Given that an objects DAT section only exists once and is shared by all instances of the object then the compiler should be able to get it's real address in HUB at build time.
BTW, @@@ seems rather ugly. It's too bad, invoking the principle of least surp[rise, that @ can't simply do what new users expect in such situations.
-Phil
In your example, I believe you could have used @ and @@. If you used: 'y_ptr LONG @y' then use @@y_ptr and you get the absolute address as a result. The Propeller Manual describes this case.
Phil,
I understand how to go about implementing @@@ for Symbols that have known addresses during compile time. My questions here are more about what are the limitations on it from BSTC. What does BSTC do when you use @@@ on a VAR and have multiple instances of the object? It's not documented well at all.
Dave Hein,
I think you need to read the section in the Prop Manual about @@ again, because I think it does what you want. On other other hand, I might be wrong about that?
Andy
Meanwhile @ gives you different results depending where you use it.
It's chaos.
Heater, I've used the @@ operator. It adds the object offset to a value, and can only be used within a Spin method. The value @@0 will give you the absolute starting address of the object that it is executed from.
-Phil
Is that documented anywhere?
It sounds insane. In FORTRAN you could get the address of literal constants because it kept them in a "literal pool". Spin does not do that and any kind of address of zero (or 1 or 2 or ... n) makes no sense.
Sorry if I came off poorly towards you there. I think I just misunderstood the situation.
All,
So from what you are all saying, @@@ can only be used for DAT symbols and it's only used in PASM code, is that correct? This is the case that makes the most sense to me. I was primarily wondering how this would work on VAR variables, and why it was needed for SPIN code.
Yeah, it cannot be used for VAR variables.
@Dave,
Exactly, you used the abomination that is "@@0", which as far as I know is undocumented and meaningless to fix up for the failure of @ or @@ to do the right thing in your DAT sections.
And by the way, what is that magic number 16 doingin there?
This shows exactly why we need @@@ (Or fix @ so that it works, which can never happen now without breaking so much existing code).
@@ is bad enough, but @@@ is a syntactic kludge and just plain ugly.
Also for addresses that are knowable at link time, @ should be allowed inside the constant pseudo-operator. 'No point computing the absolute address every time. (For that matter, constant should be abolished in favor of constant folding during compile time, but that's another issue that we don't need to discuss here.)
-Phil
EDIT: The magic number 16 is the absolute address for the top object. The table in my example was generated assuming it would be used in the top object so that no further adjustment was necessary. However, I ended up using it in a child object, so I needed to subtract 16 and add @@0 to it. If I didn't already have the +16 in the numbers I could have adjusted them by doing long[ptr] := @@long[ptr]. Now that I think of it, I could have just add @@(-16), or it might even work without the parens -- @@-16. Now that's really ugly!
Yes, my understanding is that @@@ is only refers to DAT section from PASM. There was no way to get an absolute hub address into the pasm code without resorting to either:-
1. Getting a list of addresses passed via PAR and plugging these into PASM using PASM. Extremely wasteful of PASM code space.
2. Plugging the addresses into the PASM hub code via spin before launching the cog. Very messy and frowned upon.
I disagree with Phil for breaking the existing code. However, perhaps it is time (or in the future) to fix this @, @@, @@@ mess up with a new operator that just works correctly - such as &label which means indirect in some other languages.
Normally, I would not suggest breaking existing code. However, in this case, I doubt that there will have been many uses of @ in DAT sections that weren't put there in error. So the collateral damage will be minimal, and we end up with pleasing and consistent syntax and semantics. And those who have already used @@@ can easily revert to @ if they wish to use the OpenSpin compiler.
-Phil
I'm not sure if making just @ work "as you want" in PASM is feasable. How would you know if the coder intended to get the cog address or the hub address of a given DAT symbol? If you did it based on the DAT symbol being in the same org space as the PASM code, then you couldn't get the HUB address of those symbols which I think could be useful.
1. SPIN @@ means the absolute address of the current object (is this correct Dave?)
2. PASM @@ means the absolute hub address of the label (replaces @@@ operator in bst/homespun)
In PASM @@label may be used in these cases (or similar)...
But what would happen in SPIN with these, and are they valid???
Would the hubprt long @@hublabel be set the same in both SPIN & PASM cases???
This is already part of PASM. (But you knew that! )
-Phil
This code compiles under the Prop Tool Of course, @@offset is the intended use of the @@ operator. However, @@0 is useful for getting the address of the beginning of the object when doing things like examining the method table.
-Phil
Eventually, the existing Prop Tool is going away, and OpenSpin will define the lingua franca for Spin. It's easier to take a leadership role in Spin's definition early on than to let ugly syntax and bad semantics linger until it really is too late to correct them.
I so wish the same could be done with => and =<, but that train left the station years ago.
-Phil
Using "&" would be great but in order to continue the tradition Spin being designed to trip up C programmers it should really be some other symbol, using "*" would be perfect:)
How about using "¤". That has been on my keyboards for decades and I have never ever had to use it. It's about time some programming language did.
Thanks,
-Phil
I suspect that the current behavior of @ arose because he didn't want to add another pass to the compiler, not because of any philosophical preference. But it woul;dn't hurt to ask him about it.
There are Manuals, Tutorials, Books and many Forum posts that describe the @ and @@ operators.
All the Spin code that does it right according these descriptions will be broken.
All the tricks that were used to solve the not expected behavior will be broken (@Label+16 and such).
One possible solution may be to support a new section, which changes the behavior of @ inside that section.
For example: Andy