runtime integration and flag

This commit is contained in:
dcode
2019-03-31 21:58:42 +02:00
parent c4a57025d2
commit 27f1905510
225 changed files with 8010 additions and 9365 deletions

View File

@ -14,12 +14,24 @@
import { AL_BITS, AL_SIZE, AL_MASK } from "../util/allocator";
import { HEAP_BASE, memory } from "../memory";
// @ts-ignore: decorator
@lazy
const SL_BITS: u32 = 5;
// @ts-ignore: decorator
@lazy
const SL_SIZE: usize = 1 << <usize>SL_BITS;
// @ts-ignore: decorator
@lazy
const SB_BITS: usize = <usize>(SL_BITS + AL_BITS);
// @ts-ignore: decorator
@lazy
const SB_SIZE: usize = 1 << <usize>SB_BITS;
// @ts-ignore: decorator
@lazy
const FL_BITS: u32 = (sizeof<usize>() == sizeof<u32>()
? 30 // ^= up to 1GB per block
: 32 // ^= up to 4GB per block
@ -42,10 +54,18 @@ const FL_BITS: u32 = (sizeof<usize>() == sizeof<u32>()
// F: FREE, L: LEFT_FREE
/** Tag indicating that this block is free. */
// @ts-ignore: decorator
@lazy
const FREE: usize = 1 << 0;
/** Tag indicating that this block's left block is free. */
// @ts-ignore: decorator
@lazy
const LEFT_FREE: usize = 1 << 1;
/** Mask to obtain all tags. */
// @ts-ignore: decorator
@lazy
const TAGS: usize = FREE | LEFT_FREE;
/** Block structure. */
@ -55,6 +75,7 @@ const TAGS: usize = FREE | LEFT_FREE;
info: usize;
/** End offset of the {@link Block#info} field. User data starts here. */
@lazy
static readonly INFO: usize = (sizeof<usize>() + AL_MASK) & ~AL_MASK;
/** Previous free block, if any. Only valid if free. */
@ -63,9 +84,11 @@ const TAGS: usize = FREE | LEFT_FREE;
next: Block | null;
/** Minimum size of a block, excluding {@link Block#info}. */
@lazy
static readonly MIN_SIZE: usize = (3 * sizeof<usize>() + AL_MASK) & ~AL_MASK;// prev + next + jump
/** Maximum size of a used block, excluding {@link Block#info}. */
@lazy
static readonly MAX_SIZE: usize = 1 << (FL_BITS + SB_BITS);
/** Gets this block's left (free) block in memory. */
@ -111,7 +134,7 @@ const TAGS: usize = FREE | LEFT_FREE;
// └───────────────────────────────────────────────────────────────┘ SIZE ┘
// S: Small blocks map, P: Possibly padded if 64-bit
assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits
// assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits
/** Root structure. */
@unmanaged class Root {
@ -120,6 +143,7 @@ assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits
flMap: usize = 0;
/** Start offset of second level maps. */
@lazy
private static readonly SL_START: usize = sizeof<usize>();
// Using *one* SL map per *FL bit*
@ -137,11 +161,13 @@ assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits
}
/** End offset of second level maps. */
@lazy
private static readonly SL_END: usize = Root.SL_START + FL_BITS * 4;
// Using *number bits per SL* heads per *FL bit*
/** Start offset of FL/SL heads. */
@lazy
private static readonly HL_START: usize = (Root.SL_END + AL_MASK) & ~AL_MASK;
/** Gets the head of the specified first and second level index. */
@ -164,6 +190,7 @@ assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits
}
/** End offset of FL/SL heads. */
@lazy
private static readonly HL_END: usize = (
Root.HL_START + FL_BITS * SL_SIZE * sizeof<usize>()
);
@ -172,6 +199,7 @@ assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits
set tailRef(value: usize) { store<usize>(0, value, Root.HL_END); }
/** Total size of the {@link Root} structure. */
@lazy
static readonly SIZE: usize = Root.HL_END + sizeof<usize>();
/** Inserts a previously used block back into the free list. */
@ -424,6 +452,8 @@ function fls<T extends number>(word: T): T {
}
/** Reference to the initialized {@link Root} structure, once initialized. */
// @ts-ignore: decorator
@lazy
var ROOT: Root = changetype<Root>(0);
/** Allocates a chunk of memory. */

View File

@ -19,13 +19,23 @@ const enum State {
}
/** Current collector state. */
// @ts-ignore: decorator
@lazy
var state = State.INIT;
/** Current white color value. */
// @ts-ignore: decorator
@lazy
var white = 0;
// From and to spaces
// @ts-ignore: decorator
@lazy
var fromSpace: ManagedObjectList;
// @ts-ignore: decorator
@lazy
var toSpace: ManagedObjectList;
// @ts-ignore: decorator
@lazy
var iter: ManagedObject;
// ╒═══════════════ Managed object layout (32-bit) ════════════════╕

View File

@ -27,11 +27,15 @@ import { Array } from "./array";
}
/** Common runtime header size. */
// @ts-ignore: decorator
@lazy
export const HEADER_SIZE: usize = isDefined(__ref_collect)
? (offsetof<HEADER>( ) + AL_MASK) & ~AL_MASK // full header if GC is present
: (offsetof<HEADER>("reserved1") + AL_MASK) & ~AL_MASK; // half header if GC is absent
/** Common runtime header magic. Used to assert registered/unregistered status. */
// @ts-ignore: decorator
@lazy
export const HEADER_MAGIC: u32 = 0xA55E4B17;
/** Gets the computed unique class id of a class type. */

38
std/runtime/README.md Normal file
View File

@ -0,0 +1,38 @@
AssemblyScript runtimes
=======================
Default
-------
```
$> asc ...
```
The [default runtime](./default.ts) adds proper support for dynamic memory management and garbage collection to your program.
* [TLSF memory allocator](../assembly/allocator/tlsf.ts)
* [ITCM garbage collector](../assembly/collector/itcm.ts)
Arena
-----
```
$> asc ... --runtime arena
```
The [arena runtime](./arena.ts) is just enough to make most language features work, but doesn't have sophisticated support for freeing memory. Useful when prototyping or for simple one-shot modules in that it produces very small modules with minimal overhead.
* [Arena memory allocator](../assembly/allocator/arena.ts) with `memory.reset()`
* No garbage collector
None
-----------------
```
$> asc ... --runtime none
```
[No runtime](./none.ts) features at all. Useful for building low-level modules that do not require language features like managed classes, or if you'd like to compose your own runtime by including a custom memory allocator and garbage collector.
* No memory allocator
* No garbage collector

1
std/runtime/arena.ts Normal file
View File

@ -0,0 +1 @@
import "allocator/arena";

2
std/runtime/default.ts Normal file
View File

@ -0,0 +1,2 @@
import "allocator/tlsf";
import "collector/itcm";

0
std/runtime/none.ts Normal file
View File

View File

@ -0,0 +1,6 @@
{
"extends": "../assembly.json",
"include": [
"./**/*.ts"
]
}