-2

I think I asked the wrong question...

The question is really, what is the path from c++ source code to machine language code with intermediate assembly step?
So when the compiler starts to convert source code to assembly code it does something important:

int x {1};  

is converted by compiler to three things:

  • the name x,
  • the address of the x object
  • the number of bytes at this address determined by the type of x.

[Editors note: The OP has forgotten "code to initialize x with value 1"]

so at machine level there is address and count of bytes for the value at this address, and I think this is what is referred to as an lvalue.

But if compiler puts a value in a processor register and did not give it an address in memory ,then this value is represented in source code as rvalue.

I want to know if this understanding is correct and this was not answered by the duplicate suggested.

  • 1
    Methinks you need to read this: https://en.wikipedia.org/wiki/As-if_rule. Compilers these days don't do some kind of line by line conversion of your code. – Bathsheba Sep 04 '18 at 11:59
  • It'd probably be quicker to just look at any times computer example code, it's a common university exercise. There are any number of ways this could be handled; there's no single answer. – Dave Newton Sep 04 '18 at 12:10
  • Have flagged as Too Broad for the following: How does a compiler work; More than one questions per question. Also part of the question is a duplicate of: https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues – Richard Critten Sep 04 '18 at 12:10
  • i am trying to understand lvalue and rvalue expression differences ....so i read about what compiler do....no optimization done....it just convert source code to machine language....this is to trace the lvalue expression and rvalues to machine language places....memory or regsiters.....then i reached the meaning i put in question and because i have no personnel reference to tell me if this is right or wrong i put this question?????.......again no compiler optimization done – Ahmed Allam Sep 04 '18 at 12:12
  • Value categories are logical concepts that relate to the C++ language and abstract machine.. They have no direct relationship with the generated machine code. – Richard Critten Sep 04 '18 at 12:15
  • @RichardCritten so in any case synario does this understanding of lvalue in memory and r value in registers only true?? – Ahmed Allam Sep 04 '18 at 12:17
  • I have (heavily) edited your question. If you don't approve of my edits, please roll them back. My changes fall into three categories: 1. Improve the English (your English is understandable, but improving it makes it easier for *other* non-native speakers). 2. Remove the supplementary questions (we have a pretty strong rule of "one question per question here"). 3. Remove the politeness at the end (we tend to avoid "please" and "thank you"). – Martin Bonner supports Monica Sep 04 '18 at 12:19
  • ok but c++ elements represents the machine....and will be converted to equivalent code working directly on machine....during this conversion:does lvalue go to memory and have address there,while rvalue just stays in registers ??? – Ahmed Allam Sep 04 '18 at 12:19
  • @MartinBonner do any thing you like to help me reach answer – Ahmed Allam Sep 04 '18 at 12:21
  • Recommended reading: [Fundamental Concepts in Programming Languages](http://www.cs.cmu.edu/~crary/819-f09/Strachey67.pdf) (Strachey, 1967). – molbdnilo Sep 04 '18 at 12:21
  • *"ok but c++ elements represents the machine"* - No. The C++ standard discusses an *abstract machine*. What implementation do is *emulate* it. A C++ program is not a word for word list of instructions for the CPU to perform. It's a high level description of the *behavior* of a program. – StoryTeller - Unslander Monica Sep 04 '18 at 12:21
  • Note that C++ (and C) hasn't mapped directly to hardware for many, many years. – molbdnilo Sep 04 '18 at 12:24
  • @StoryTeller so why in books the say that when you declare a variable of certain type the compiler reserve part in memory for this variable with size appropriate for type and if the variable initialized then value is put in this storage......my problem is with the initializing value considering it literal>>>then it is rvalue>>>with no name or address in memory ....so how the program even know that it exists......????!!! – Ahmed Allam Sep 04 '18 at 12:27
  • Because if the books didn't try to make C++ more approachable (by mixing implementation details into the explanation) it would be impossible to learn this darn language. – StoryTeller - Unslander Monica Sep 04 '18 at 12:29
  • @AhmedAllam because the books are over-simplifying and giving broad concepts. fyi read some of this (skim read it) to get an idea of what really goes on: https://blog.regehr.org/archives/1603 – Richard Critten Sep 04 '18 at 12:30
  • whenever someone says "if you do this in code then your compiler will do that" you better dont trust them (unless they are compiler writers, but those are quite rare ;). First there is the as-if-rule and then when you write code you shouldnt care too much about what the compiler does, otherwise you wouldnt need to use a high level language – 463035818_is_not_a_number Sep 04 '18 at 12:31
  • I think you're confusing syntactical categories (literals/non-literals) with value categories (lvalues/rvalues) with the machine (registers/memory). These exist in different universes, on different abstraction levels, and there is no direct translation between them. – molbdnilo Sep 04 '18 at 12:49

2 Answers2

1

No. Your understanding is not correct. There is nothing to stop the compiler putting x into a register - unless it needs an address for x (because of other references to x in other parts of the code).

Very crudely, an lvalue is something that could be assigned to, or have its address taken (with the & operator); an rvalue is the result of an expression.

Of course, if you had declared x const, you couldn't actually assign to it - but that doesn't stop it being an lvalue.

  • "Of course, if you had declared x const, you couldn't actually assign to it - but that doesn't stop it being an lvalue.">>>>>this is the sentence which started the whole problem with me 5 days ago....i read something similar in Stroustrups book in types chapter....then asked what is the difference then ????is it just the naming and addressing? this lead me to memory and registers and compiler issues which are great but little confusing as i am still starting...so i looked for an answer which clearly state differece between lvalue and more accurately prvalue – Ahmed Allam Sep 04 '18 at 12:36
  • You can bind a reference to an lvalue even if it is const. You can take the address of a const lvalue. When selecting an overload, an lvalue argument will prefer `const T&`, but an rvalue argument will prefer `const T&&`. Thinking about registers and RAM is not actually very helpful when understanding lvalues and rvalues - they are things in C++ which the compiler then implements with registers and RAM. – Martin Bonner supports Monica Sep 04 '18 at 16:21
  • i will try to understand how compiler acts during compilation of code and i think this could lead me to understand value category and the reason they are invented then my confusion may go away.......or i could skip this part about values in the book and continue as if i did not know they existed :) – Ahmed Allam Sep 04 '18 at 20:57
0

You've got it the wrong way around. The C++ language specification specifies how C++ source code is to be understood. This includes the meaning of definitions, such as int x{1}. The standard also tells us that x is an lvalue.

The compiler knows x is an lvalue, but that's not very important when generating code. Knowing that it's an lvalue tells you what you can do with x. The compiler knows exactly what you are doing with x. If you don't use x at all, the compiler could even leave out x entirely!

MSalters
  • 159,923
  • 8
  • 140
  • 320