C ++: What are the R-Value links at the technical level (ASM)?

Possible duplicate:
What is the difference between r-value links and l-value links? (CodeGen)

I was wondering, can someone explain which R-Value links are at a technical level? By this I mean: what happens at the assembler level when R-Value references are created.

For a little test, to write what is going on inside, I wrote the following code:

char c = 255; char &c2 = c; char &c3 = std::move(c); 

I know that it makes no sense to create an R-value reference for 'c', but just for the sake of testing, I did it anyway to see what it was doing. And here is the result:

 unsigned char c = 255; mov byte ptr [c],0FFh unsigned char &c2 = c; lea eax,[c] mov dword ptr [c2],eax unsigned char &&c3 = std::move(c); lea eax,[c] push eax call std::move<unsigned char &> (0ED1235h) add esp,4 mov dword ptr [c3],eax 

I am certainly an expert on asm, but it seems to me that in this case "c3" is a regular reference to "c" at the end.

If I bind the R-Value link directly to a temporary one (char & c3 = 255), the last bit of the assembler changes as such:

 unsigned char &&c3 = 255; mov byte ptr [ebp-29h],0FFh lea eax,[ebp-29h] mov dword ptr [c3],eax 

From the views of this change, I assume that c3 is still a reference to some memory location that contains a value of 255. Thus, this is a regular reference - the value is not copied / not assigned to c3. It's true?

Can anyone tell if my assumptions are correct or if I am completely not up to date? Until now, I always thought of R-Value references to match the signature of the functions / methods (maybe move-ctor) when it comes to resolving the call, so the encoder knows how to process the data provided (for moving-ctor which will move data, not copy it).

To protect this rather dumb attempt that I just presented: I'm not going to hang with my code at the asm level, I just want to figure out what technical differences have been made to R-Value compared to the others that have been around all these years.

Any ideas and explanations are more than welcome!

Thanks!

+6
source share
4 answers

What happens at the assembler level when R-Value references are created.

Everything you need to maintain high-level semantics. Which compiler exactly depends on what the compiler thought would be a good idea. The Assembly has no concept of lvalues, rvalues ​​or references, so stop looking for them. Turn on optimization, and the code you are viewing is likely to change (or may cease to exist if variables are not used).

I just want to figure out what technical differences have been made to R-Value, compared to the rest that have been around all these years.

Rvalue links allow semantics to be moved, and they, in turn, provide important optimization capabilities. The standard does not say "oh, these are rvalue refs and that you must implement them in the assembly." An implementation may not even build.

+8
source

A link to an Rvalue is no different at the asm level - it can be exactly the same as regular links (depending on how the compiler sees it). The difference exists only at the level of the C ++ language. Background information on the r-value is that the reference object is temporary, and anyone who receives it can freely modify it. Information about the location of an object can be transmitted in the same way as with regular links (the compiler may try to optimize it differently, but this is an internal matter of the compiler).

The difference between r-value refernce and non-const l-value is that each l-value will be automatically returned only to the l-value link (thus preventing accidental changes), while r-value'd expressions will convert for both (using r-value ref preffered), allowing move semantics and regular calls if move semantics are not supported. std :: move does nothing but the ability to manually cast l-values ​​in r-values ​​of links.

+6
source

Before optimization, the link exists as a pointer containing the address of the associated object.

But the compiler is trying very hard to optimize it. Insertion can especially lead to the fact that all use of the reference parameter inside a small function will be replaced by direct use of the register, which contains the value of the associated object.

+4
source

The rvalue reference can be fully described at the C ++ level, there is no need to read the assembly code for this. You just need to get a minimal C ++ class that allocates internal resources, and the β€œtheft” of rvalue reference resources by another object is obvious. As in the remote_integer class from this classic article: http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2. aspx Assembler translation for this code is pretty simple, but the difference is in C ++ code. As for simple types, such as char, they can be used to demonstrate some properties of rvalue link syntax, but it makes no sense to use rvalue links for this type - both in C ++ and at Assembly level. So, if you don't see the benefits of using char && c in C ++, there is nothing interesting in Assembly.

0
source

Source: https://habr.com/ru/post/898298/


All Articles