473,796 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4700
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
7743
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 how this would work with class objects. In this case do you have to push the object on the stack at the start of every public procedure etc. in the class and pop it off at the end? I can't see how else you can know which object is active - or...
14
30102
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 part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
4
2624
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 is directly as in the book, except for where I added the comments at the lines I wanted to refer to or to skip sections of code. template <class Element_Type> class Stack {
4
3631
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
8
2098
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 ignore spaces, case sensitivity and punctuation. I need three text boxes. The first will be for input of the string. The second will display the string when the "check" button is clicked and the result of the analysis (string is a palindrome or is...
4
2150
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 stack { public:
16
4451
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 in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
24
6592
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 the stack pointer should go upwards when there are "push" operations, and stack pointer should go downards when there are "pop" operations?? If this is the case, the address should go upwards (increasing) or downards (decreasing) then? i.e....
1
4223
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 errors of all kinds. I have decided to start from scratch. Any suggestions someone can give me would be greatly appreciated!! Here is the current code: #include <iostream> using std::cout; using std::cin; #include <cstring> using std::strcpy;
11
1787
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 = def push(self, item): self.items.append(item)
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.