Skip to content

Commit 0ad9d56

Browse files
committed
integrate
1 parent 911a4bb commit 0ad9d56

File tree

7 files changed

+33
-19
lines changed

7 files changed

+33
-19
lines changed

src/builtins.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ export namespace BuiltinSymbols {
474474
export const memory_fill = "~lib/memory/memory.fill";
475475
// std/gc.ts
476476
export const iterateRoots = "~lib/gc/iterateRoots";
477+
// internals
478+
export const rt_classid = "~lib/builtins/__rt_classid";
479+
export const rt_iterateroots = "~lib/builtins/__rt_iterateroots";
477480
}
478481

479482
/** Compiles a call to a built-in function. */
@@ -3591,9 +3594,17 @@ export function compileCall(
35913594
return module.createUnary(op, arg0);
35923595
}
35933596

3594-
// === GC integration =========================================================================
3597+
// === Internal runtime =======================================================================
35953598

3596-
case BuiltinSymbols.iterateRoots: {
3599+
case BuiltinSymbols.rt_classid: {
3600+
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
3601+
compiler.currentType = Type.u32;
3602+
if (!type) return module.createUnreachable();
3603+
let classReference = type.classReference;
3604+
if (!classReference) return module.createUnreachable();
3605+
return module.createI32(classReference.prototype.classId);
3606+
}
3607+
case BuiltinSymbols.rt_iterateroots: {
35973608
if (
35983609
checkTypeAbsent(typeArguments, reportNode, prototype) |
35993610
checkArgsRequired(operands, 1, reportNode, compiler)

src/program.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ export class Program extends DiagnosticEmitter {
344344
/** Memory allocation function. */
345345
memoryAllocateInstance: Function | null = null;
346346

347+
/** Next class id. */
348+
nextClassId: u32 = 1;
349+
347350
// gc integration
348351

349352
/** Whether a garbage collector is present or not. */
@@ -2355,7 +2358,7 @@ export class FunctionPrototype extends DeclaredElement {
23552358

23562359
/** Constructs a new function prototype. */
23572360
constructor(
2358-
/** Simple na,e */
2361+
/** Simple name */
23592362
name: string,
23602363
/** Parent element, usually a file, namespace or class (if a method). */
23612364
parent: Element,
@@ -2792,6 +2795,8 @@ export class ClassPrototype extends DeclaredElement {
27922795
overloadPrototypes: Map<OperatorKind, FunctionPrototype> = new Map();
27932796
/** Already resolved instances. */
27942797
instances: Map<string,Class> | null = null;
2798+
/** Unique class id. */
2799+
classId: u32 = 0;
27952800

27962801
constructor(
27972802
/** Simple name. */
@@ -2813,6 +2818,8 @@ export class ClassPrototype extends DeclaredElement {
28132818
declaration
28142819
);
28152820
this.decoratorFlags = decoratorFlags;
2821+
this.classId = u32(this.program.nextClassId++);
2822+
assert(this.classId); // must not wrap around to 0
28162823
}
28172824

28182825
/** Gets the associated type parameter nodes. */

std/assembly/builtins.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,3 +501,6 @@ export namespace v8x16 {
501501
}
502502

503503
@builtin export declare function start(): void;
504+
505+
@builtin export declare function __rt_classid<T>(): u32;
506+
@builtin export declare function __rt_iterateroots(fn: (ref: usize) => void): void;

std/assembly/collector/itcm.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
@inline export const HEADER_SIZE: usize = (offsetof<ManagedObject>() + AL_MASK) & ~AL_MASK;
1313

1414
import { AL_MASK, MAX_SIZE_32 } from "../internal/allocator";
15-
import { iterateRoots } from "../gc";
1615

1716
/** Collector states. */
1817
const enum State {
@@ -142,7 +141,7 @@ function step(): void {
142141
}
143142
case State.IDLE: {
144143
if (TRACE) trace("gc~step/IDLE");
145-
iterateRoots(__gc_mark);
144+
__rt_iterateroots(__gc_mark);
146145
state = State.MARK;
147146
if (TRACE) trace("gc~state = MARK");
148147
break;
@@ -163,7 +162,7 @@ function step(): void {
163162
obj.hookFn(objToRef(obj));
164163
} else {
165164
if (TRACE) trace("gc~step/MARK finish");
166-
iterateRoots(__gc_mark);
165+
__rt_iterateroots(__gc_mark);
167166
obj = iter.next;
168167
if (obj === toSpace) {
169168
let from = fromSpace;

std/assembly/gc.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* tslint:disable */
22

3-
@builtin export declare function iterateRoots(fn: (ref: usize) => void): void;
4-
53
export namespace gc {
64

75
export function collect(): void {

std/assembly/internal/runtime.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class HEADER {
2525
@inline export const HEADER_MAGIC: u32 = 0xA55E4B17;
2626

2727
/** Aligns an allocation to actual block size. */
28-
function ALIGN(payloadSize: usize): usize {
28+
export function ALIGN(payloadSize: usize): usize {
2929
// round up to power of 2, e.g. with HEADER_SIZE=8:
3030
// 0 -> 2^3 = 8
3131
// 1..8 -> 2^4 = 16
@@ -36,7 +36,7 @@ function ALIGN(payloadSize: usize): usize {
3636
}
3737

3838
/** Gets to the common runtime header of the specified reference. */
39-
function UNREF(ref: usize): HEADER {
39+
export function UNREF(ref: usize): HEADER {
4040
assert(ref >= HEAP_BASE + HEADER_SIZE); // must be a heap object
4141
var header = changetype<HEADER>(ref - HEADER_SIZE);
4242
assert(header.classId == HEADER_MAGIC); // must be unregistered
@@ -96,14 +96,10 @@ export function FREE(ref: usize): void {
9696
memory.free(changetype<usize>(header));
9797
}
9898

99-
function CLASSID<T>(): u32 {
100-
return 1;
101-
}
102-
10399
/** Registers a managed object with GC. Cannot be changed anymore afterwards. */
104100
export function REGISTER<T>(ref: usize, parentRef: usize): void {
105101
var header = UNREF(ref);
106-
header.classId = CLASSID<T>();
102+
header.classId = __rt_classid<T>();
107103
if (GC) __REGISTER_IMPL(ref, parentRef);
108104
}
109105

std/portable/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ declare namespace i8 {
142142
export function parseInt(string: string, radix?: i32): i8;
143143
}
144144
/** Converts any other numeric value to a 16-bit signed integer. */
145-
declare function i16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
145+
declare function i16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i16;
146146
declare namespace i16 {
147147
/** Smallest representable value. */
148148
export const MIN_VALUE: i16;
@@ -178,7 +178,7 @@ declare namespace isize {
178178
export function parseInt(string: string, radix?: i32): isize;
179179
}
180180
/** Converts any other numeric value to an 8-bit unsigned integer. */
181-
declare function u8(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
181+
declare function u8(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): u8;
182182
declare namespace u8 {
183183
/** Smallest representable value. */
184184
export const MIN_VALUE: u8;
@@ -190,7 +190,7 @@ declare namespace u8 {
190190
export function parseInt(string: string, radix?: i32): u8;
191191
}
192192
/** Converts any other numeric value to a 16-bit unsigned integer. */
193-
declare function u16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
193+
declare function u16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): u16;
194194
declare namespace u16 {
195195
/** Smallest representable value. */
196196
export const MIN_VALUE: u16;
@@ -202,7 +202,7 @@ declare namespace u16 {
202202
export function parseInt(string: string, radix?: i32): u16;
203203
}
204204
/** Converts any other numeric value to a 32-bit unsigned integer. */
205-
declare function u32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i32;
205+
declare function u32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): u32;
206206
declare namespace u32 {
207207
/** Smallest representable value. */
208208
export const MIN_VALUE: u32;

0 commit comments

Comments
 (0)