The stack is created by the compiler or OS / architecture

This question is a continuation of my previous question.

stack growth direction

I would like to know if the stack was created by the compiler or OS / architecture? Also, how does the OS know about these specific compilers? For example: C ++ allows variables to create data on the stack and heap, while java only allows heaps.

Also, if the stack is created on the heap, as indicated in the message, how the system can know about it, because the system only knows about the stack pointer and the base pointer.

+4
source share
4 answers

A stack is a memory location allocated to your OS by the operating system. After it is allocated, the OS sets the register (on x86, it esp ) to where the stack is located, and then runs your program. Compilers know that if they use the value in this register as the stack pointer, everything will be fine. Then they do whatever they want to do with it. The OS simply allocates a zone. He does not care about how he was used after.

The OS does not know if your program will mainly use the stack or heap. However, since most programming languages โ€‹โ€‹use the stack in one way or another, he knows that he must allocate one. For example, Java stores its objects on the heap, but most JVM implementations will use the stack to support call frames (and primitive local variables), so it also needs a stack.

+6
source

The stack is definitely defined by the compiler, the OS allocates space for it, but this is relatively trivial. The stack is the allocated memory space used by the compiler (to the extent that the compiler determines the instructions that use it) to control the flow of the program and store local variables, etc.

Thus, the OS does not know about the specifics of the compiler. The stack is still stored in main memory, it's just not part of the memory that you (the programmer) can directly control.

+3
source

One Java virtual machine stack is created for each thread in the virtual machine. The stack stores frames, and the contents cannot be manipulated directly, except for push and pop operations from frames.

Frames are created every time a message is called and used to store data and partial results, as well as to perform dynamic linking, return values โ€‹โ€‹for methods, and sending exceptions.

The language specification describes in detail that the Java virtual machine stack is similar to the stack of a regular language such as C. Thus, it is obvious to me that the jvm stack model is encoded and used in the jvm implementation and is not provided by the host operating system.

+1
source

Traditionally, the stack is the place where the return address was placed for calls to machine code (so that it could return when it was done). Consequently, there were instructions for easy access to this location in memory.

It was quickly found that including arguments in a call along with a return address was a very simple and efficient way to execute it. He then grew up to deal with local addresses, etc.

0
source

All Articles