# VSL AMM spec
Rough outline for how VSL will implement and optimize AMM (automatic memory management).
## States
An parameter (i.e. function arg) can be:
- **Contained**: does not escape at all
- **Uncontained**: does escape to _global_ scope
- **Binding**: does return itself to the _callee_ scope
## Glossary
- **Managed value**: one that is reference counted
- **Unmanaged value**: one that is not directly counted.
- **Control**: For something (a scope or a class) to have 'control' means it will be reponsible for free()ing the object.
- If a class has control of a 'value' that means it will free() that value when the class itself is being free()d
- **Escape**: For a 'value' to escape means for that value's control to go to a higher scope. (e.g. local to global).
## Basic Rules
- Every value as has dependency state (contained, uncontained, or binding).
- Interpreting a _managed value_ as an _unmanaged value_ means to unwrap the managed value
- Intepreting an _unmanaged value_ as a _managed value_ is semantically invalid and means the _unmanaged value_ should in fact be managed.
## Identification Rules
- An **identifier** denotes an intially 'unmanaged value'
- A **literal** denotes an 'unmanaged value'
- **Static property access** (a readonly op) will maintain the state (_unmanaged op_)
- **Dynamic access** will maintain same rules as a method call
- **Method call** will follow the _Method Rules_
- **Global call** will follow the _Function Rules_
- **Operators** will maintain the same state (_unmanaged op_)
- **Assignment**:
- If target is a parent context
- Target is **managed** along with _value_ (_managed op_)
- If target is not
- Target remains **unmanaged** (_unmanaged op_)
## Method Call
- If a method is an _unmanaged op_ it will take the respective value
- A parameter is bound to it's _identification rules_
- A return value has three possibilities:
- Parent bound: references a parent context
- Identification
- Has an uncontained dependency
- Behavior
- Is **managed value** along with parent.
- Parameter bound: references a parameter
- Identification
- Only parameter dependencies
- Behavior
- Will pass **control** to parent callee
- Callee bound: references a locally created value
- Identification
- No non-binding dependents
- Behavior
- Will pass **control** to parent callee
## Unmanaged value cleanup
- Unmanaged would be cleaned up at the end of their referencing scope. (TODO: reconsider if this should be changed, see: problems)
## Problems
- Rapidly reassigning locals will mean they won't be cleaned until the scope exits?
- Possible Solution: Cleanup controled, unmanaged locals at their last reference