Compiler Function Variable Escaping Stack to Heap
Find Innovative SolutionsGenerate Solutions
Solution Overview
Problem
Existing methods for compiling JavaScript and similar programming languages that store function variables in stack memory face errors when a second function relies on variables of a first function after the first function has returned, as the variables are no longer available in the stack.
Innovation Solution
Generating machine code that conditionally escapes function variables from the stack to heap memory if they need to be available to another function after the first function has returned, using a temporary object to store pointers to these variables during execution and copying their values to the heap upon return.
Engineering Contradictions & Design Principles
Engineering Contradiction Analysis
1Speed
If function variables are stored in stack memory, then access time is reduced and execution speed is improved, but variables become unavailable to subsequent functions after the first function returns
Solution Approach 1:
The patent applies dynamics by making the storage location of function variables changeable rather than fixed. Variables dynamically switch between stack storage (during function execution for speed) and heap storage (after function return for availability). The compiler analyzes control flow to determine when variables need to escape the stack and generates appropriate code to transfer them to heap memory, enabling the system to adapt storage location based on execution state.
Solution Approach 2:
The patent uses heap memory as an intermediary storage location between the stack and the requirements of subsequent functions. When a function variable needs to remain available after the function returns, the system transfers the variable from the stack to the heap as an intermediary step, allowing the variable to persist across function boundaries while maintaining fast access during execution.
2Reliability
If function variables are stored in heap memory, then variables remain available to subsequent functions, but access time increases and execution speed decreases
Solution Approach 1:
The system dynamically selects the optimal storage location based on execution context. During function execution, variables are stored in the stack for fast access. The compiler analyzes control flow graphs to identify variables that need to escape to subsequent functions and generates code to transfer them to the heap at appropriate times, achieving both speed and availability.
Solution Approach 2:
The patent segments the storage strategy into two distinct phases: stack storage during execution for performance, and heap storage after return for availability. The compiler divides the variable lifecycle into segments and applies different storage mechanisms to each segment, optimizing for the appropriate metric at each phase.
3Reliability
If the compiler transfers variables from stack to heap, then variable availability is maintained, but additional memory operations are required increasing complexity
Solution Approach 1:
The compiler performs preliminary analysis of the control flow graph to identify which variables need to escape to subsequent functions before generating code. This static analysis allows the compiler to pre-determine the escapement requirements and generate optimized code that transfers variables at the correct moments, avoiding unnecessary runtime operations and reducing overall complexity.
Solution Approach 2:
The patent changes the parameter of storage location from fixed (either stack or heap) to variable based on function execution state. The compiler modifies the storage parameter dynamically by transferring variables between stack and heap at appropriate points in execution, enabling the system to adapt to different control flow scenarios without requiring complex runtime decision-making.
Data Source
AI summary
Optimized storage of function variables in compiled code is disclosed. It is determined that a variable of a first function is required to be available for use by a second function subsequent to return of the first function. Machine code is generated to escape the variable from a storage location in a stack memory to a storage location in a heap memory, prior to the variable being removed from the stack memory, in connection with return of the first function.


