473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CLR stack

I would like to learn if there is any difference between the stack that
MSIL
uses for each method for executing instructions and the stack that it
uses
for storing Value types. Are they the same?

Also the processor has its own stack pointer right,.. so are these
stacks related
to the processor's stack? or are they virtually implemented by the CLR
?

Thanks

Joel

Jan 26 '07 #1
5 4668
Joel wrote:
I would like to learn if there is any difference between the stack that
MSIL
uses for each method for executing instructions and the stack that it
uses
for storing Value types. Are they the same?
Also the processor has its own stack pointer right,.. so are these
stacks related
to the processor's stack? or are they virtually implemented by the CLR
IL is based on a conceptual model called the VES, Virtual Execution
System. IL instructions logically use the VES stack for implementing
primitives like addition and subtraction (both of these pop two values
off and push the result), and to hold intermediate values while
evaluating complex expressions (any complex expression can be converted
into stack manipulations, often called RPN, or reverse polish notation).
The VES does not model local variables as being stored on its stack.
Instead, they are directly modeled as locals, described in a table at
the start of a method (actually, a signature, which is a kind of
compressed description of a table) and are accessed with instructions
like ldloc and stloc.

When people talk about a stack in the context of differentiating value
types from reference types, they are talking about the CPU stack as in
more traditional languages. The translation from the VES model to the
physical model is at the discretion of the implementation of the CLI
(CLR for .NET).

Typically the CLR on x86 will implement the VES stack as register
operations, and locals as registers but spilling out to stack where
necessary.

Don't confuse the VES stack with the CPU stack, because one doesn't
translate into the other (it could be done that way, but it would be
inefficient). Instead, think of the IL stream with respect to
expressions as a serialization of an expression tree. In fact, with
'dup' instructions, it is actually a serialization of an expression dag,
or Directed Acyclic Graph.

For example, calculating (42 + 24) / 2 could be modeled as the following
RPN:

push 42
push 24
add
push 2
div

This could alternatively be modeled as the following Lisp tree. Notice
that a post-order traversal of the tree produces the RPN expression.

(/ (+ 42 24) 2)

These two expressions are equivalent, but RPN is more space-efficient
when opcodes are mostly byte-size. Once viewed as a tree, traditional
compiler code generation techniques can be directly applied.

-- Barry

--
http://barrkel.blogspot.com/
Jan 26 '07 #2
Thanks a lot, that was really helpful.

One question,.. as you said, value types are stored in the x86
processor stack and VES locals are actually stored there.
How are these locals accessed from the stack?
push and pop instructions store or retrieve only the top of the
stack as pointed by the CPU stack pointer, how are locals at
intermediate locations in the stack accessed then?

Joel

Jan 27 '07 #3
Joel wrote:
Thanks a lot, that was really helpful.

One question,.. as you said, value types are stored in the x86
processor stack
That is how people think of them, i.e. avoiding GC overhead, but if the
value type is small enough it may be in a register instead. (Of course,
if the value types are fields of another object, then they are stored
inline in that object's memory; if the other object is a value of a
reference type, then the memory of the field of the value type is stored
inline in the value of the reference type's memory, allocated from the
GC. "Value types on the stack" refers only to parameters and locals, not
fields.)
and VES locals are actually stored there.
When IL is compiled to machine code, VES locals may allocated to
registers if they are available and the value is small enough, and only
spill to stack when necessary, just like any optimizing native code
compiler. IL is like simplified source code for an optimizing native
code compiler.
How are these locals accessed from the stack?
With CPU instructions that the CLR generates when it compiles the IL
code.
push and pop instructions store or retrieve only the top of the
stack as pointed by the CPU stack pointer, how are locals at
intermediate locations in the stack accessed then?
The x86 CPU models the top of stack as being the thing pointed to by the
ESP register, and convention uses the EBP register as the frame
register. The x86 processor's instructions such as PUSH and POP modify
ESP as they push and pop, but that does not stop other instructions like
MOV and ADD from directly accessing memory operands via expressions that
use ESP or EBP. For example:

mov eax,[esp+4]

or

add eax,[ebp+12]

.... are instructions that are using locals and arguments. The x86 stack
grows downwards, so ESP+n is deeper into the stack, while values higher
than EBP would typically be arguments that have been passed on the
stack.

Don't confuse the VES model with the CPU model. The VES is an abstract
machine, or virtual machine, which provides a context for the exact
definition and specification of what IL means. IL is only a language for
a native code compiler. The fact that IL can only push and pop and can't
directly address its stack does not limit the CPU, which can do whatever
the IL compiler (the JIT compiler in the CLR) generates code for it to
do.

-- Barry

--
http://barrkel.blogspot.com/
Jan 27 '07 #4
Thanks a lot, that was very insightful.
>
mov eax,[esp+4]

or

add eax,[ebp+12]

... are instructions that are using locals and arguments.
In other words, are you saying IL instructions like ldc and ldarg
translate
into the above?
The x86 stack
grows downwards, so ESP+n is deeper into the stack, while values higher
than EBP would typically be arguments that have been passed on the
stack.
Can you please rephrase the above statement in simpler terms.

Regards

Joel

Jan 28 '07 #5
Joel wrote:
Thanks a lot, that was very insightful.

mov eax,[esp+4]

or

add eax,[ebp+12]

... are instructions that are using locals and arguments.

In other words, are you saying IL instructions like ldc and ldarg
translate
into the above?
Yes (specifically ldloc and ldarg), but not usually that directly, not
that simply. The IL instructions are source code for another compiler,
the CLR JIT compiler, which will reconstitute a simple expression graph
from the IL and use that with more traditional compiler algorithms to
generate optimized code.
The x86 stack
grows downwards, so ESP+n is deeper into the stack, while values higher
than EBP would typically be arguments that have been passed on the
stack.

Can you please rephrase the above statement in simpler terms.
This is to do with the x86 CPU architecture. You can get specification
documents from Intel's site which fully describe the hardware and the
instruction set. It's drifting away from .NET, but I'll write some brief
info.

ESP is a 32-bit register whose value is an address in memory. x86 stack
operations like PUSH and POP modify ESP as they store and retrieve
values from the stack. By the stack growing downwards, I mean that ESP
gets smaller every time you push, and gets larger every time you pop. In
picture terms and very simplified, imagine memory, broken into dwords
here (32-bit values), addresses on the left, values on the right (but
I've used small numbers here for simplicity):

118 50
114 42
110 10
10C 90
108 90

(Memory is usually visualized with low addresses at the bottom, high
addresses at the top.)

If ESP had the value 110, that means that 10 is the value on top of the
stack. If the CPU next executed 'POP EAX', it would be equivalent to
saying 'MOV EAX, [ESP]; ADD ESP, 4'. That is, popping from the x86 stack
has increased the value of ESP, and after popping it will have the value
114, and now 42 is on top of the stack. That is, the stack grows
downwards in the picture, and shrinks upwards.

The x86 stack is different from the VES stack though. The two are
similar but different. For example, the VES stack is per-activation
record while the x86 stack helps implement subroutine calling. When the
x86 'CALL' instruction is executed, it pushes the address of the next
instruction after the CALL instruction onto the stack, and similarly a
RET instruction pops the return value into EIP (the instruction pointer
register) and continues where it left off. (There can be more to it than
that, see the Intel docs for more info.) There is no equivalent in the
VES model.

EBP has to do with the x86 calling conventions and setting up a stack
frame. You can find more info about this by (e.g.) Googling "x86 stack
frame", without the quotes it will get lots more info.

-- Barry

--
http://barrkel.blogspot.com/
Jan 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
8
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
1
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting...
11
by: tom | last post by:
Hi! Im new to Python and doing exercise found from internet. It is supposed to evaluate expression given with postfix operator using Stack() class. class Stack: def __init__(self): self.items...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.