6 ms·
> Passing arrays, structures and other arguments which can’t simply be put into a single register requires a different method, in which a pointer to the data is
by saagarjha 5y ago
> Passing arrays, structures and other arguments which can’t simply be put into a single register requires a different method, in which a pointer to the data is passed: ‘call by reference‘.
Note that small structures will be passed in registers. Here's an example: https://godbolt.org/z/rsGEbefcv https://godbolt.org/z/rsGEbefcv. a and b are 32 bits and passed packed in x0, and c is a long passed in x1.
- pdpi 5y agoIt's also wrong to call this "call (or pass) by reference." Pass by reference and pass pointer by value are different things (here's an example — https://godbolt.org/z/Kv7jPPvnz https://godbolt.org/z/Kv7jPPvnz)
- secondcoming 5y agoA reference is just a pointer but with restrictions enforced by the compiler. ASM-wise, they're totally the same thing. 'Reference' doesn't mean C++'s reference, it means that you pass where the data is, not the data itself. Even Visual Basic had this concept.
- deleted 5y ago[deleted]
- MaxBarraclough 5y agoArgument-passing semantics (evaluation strategies) are something defined by the source language, not by the calling convention of the target architecture/platform. pdpi is right to point out that what the article is describing is not pass-by-reference semantics. Unlike C++, C does not support pass-by-reference (ignoring preprocessor silliness). Passing a pointer by value is not the same thing. edit To be clear, C++ supports both pass-by-value and pass-by-reference. This topic cropped up a year ago: https://news.ycombinator.com/item?id=23553574 https://news.ycombinator.com/item?id=23553574
- pdpi 5y agoYes exactly. I believe Fortran and Perl only do pass by reference, for example. It's not a C++ concept, I only used C++ in that example simply because godbolt.org makes it really easy to show how the language treats the two concepts differently even though they compile to the same thing.
- TheRealKing 5y agoThe Fortran standard is deliberately silent about the passing method. However, virtually all Fortran compilers pass by reference.
- jhgb 5y ago> are something defined by the source language, not by the calling convention of the target architecture/platform In that case perhaps the response should have been "pass-by-reference doesn't exist on machine level", not "pass-by-reference is something different", as the latter would seem to be a category error.
- Koshkin 5y ago> the response should have been "pass-by-reference doesn't exist on machine level" Which would barely make any sense.
- jhgb 5y agoHow come? It would illustrate the notion that to a machine, everything is a value, and that (abstract) references get translated into manipulations of values (and not even always in the same way).
- Koshkin 5y agoBecause "on a machine level" and "to a machine" are two different things (the former referring to human understanding).
- jhgb 5y agoYour example seems too C++-specific. Not all languages are defined in the same way.
- Erlangen 5y agoI am familiar with a small set of programming languages, but they define reference and value passing similarly. * Java: Java spec says "pass by value" * C: pass by value * C++: pass by value and reference(with ref qualifier) * C#: pass by value and reference(with ref keyword) Do you know a language that defines "pass by reference" in a very different way?
- Koshkin 5y agoIn assemblers and C passing by reference and passing by pointer value means the same thing.
- jhgb 5y agoNot sure if all language specs explicitly use the "pass-by-X" terminology but for example ANSI Smalltalk standard says things like "Each argument [of a message send] is a reference to an object." > but they define reference and value passing similarly Well, If all of them have been derived from C++ or designed by people trying to improve on C++, they would be very likely to have the notion defined similarly regardless of how anyone else would use the term, so there's not much surprise there.
- MaxBarraclough 5y agoThey're accepted terms in computer science. They're not specific to any programming language. https://en.wikipedia.org/wiki/Evaluation_strategy https://en.wikipedia.org/wiki/Evaluation_strategy
- jhgb 5y agoI was simply saying that C++ using a certain terminology was a sufficient condition for C++-derived languages to use the same terminology, you wouldn't need a CS-wide definition for this and your small set of language wouldn't allow you to make the inference that such a common definition existed. BTW the page you've linked seems to contradict some of your statements: > so they are typically described as call by value even though implementations frequently use call by reference internally for the efficiency benefits As you seem to be arguing that this is not call by reference you should probably correct the page. EDIT: It also seems that there IS some amount of language specificity anyway, since in another place of the page it says: > "In particular it is not call by value because mutations of arguments performed by the called routine will be visible to the caller. And it is not call by reference because access is not given to the variables of the caller, but merely to certain objects" I'm reasonably sure that the first part would be called "call by value" in C++ even if in context of CLU it wasn't if the usual way of doing the same in C++, namely passing a pointer to an object, were to be used.
- MarkSweep 5y agoYou can also pass structs of floating point or SIMD types by register. They have to be 2-4 elements and all of the same type. These are called HFA (Homogeneous Floating-point Aggregate) and HVA (Homogeneous Short-Vector Aggregate). Source: the Procedure Call Standard for the Arm 64-bit Architecture https://github.com/ARM-software/abi-aa/blob/2021Q1/aapcs64/aapcs64.rst https://github.com/ARM-software/abi-aa/blob/2021Q1/aapcs64/a...
- kps 5y agoMore importantly, it's just wrong. Large objects are not converted to references, they're passed on the stack. ¹ §5.4 https://developer.arm.com/documentation/ihi0055/b/ https://developer.arm.com/documentation/ihi0055/b/