Skip to content

Commit

Permalink
streamlined global reg allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Mar 16, 2024
1 parent b8b5c9b commit 302ca44
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions source/compiler/codegen/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ function CompileAssign(ctx: Context, syntax: Syntax.Term_Assign) {

if (target.type instanceof IntrinsicValue) {
switch (target.base.locality) {
case BasePointerType.global: ctx.block.push(Instruction.global.get(target.base)); break;
case BasePointerType.local: ctx.block.push(Instruction.local.get(target.base)); break;
case BasePointerType.global: ctx.block.push(Instruction.global.get(target.base.ref)); break;
case BasePointerType.local: ctx.block.push(Instruction.local.get(target.base.ref)); break;
default: AssertUnreachable(target.base.locality);
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function StructBuilder(ctx: Context, syntax: Syntax.Term_Container, expec
});
seen.push(name);

ctx.block.push(Instruction.global.get(ctx.file.owner.project.stackBase));
ctx.block.push(Instruction.global.get(ctx.file.owner.project.stackBase.ref));
const expr = CompileExpr(ctx, elm.value[1], attr.type);
if (expr instanceof IntrinsicValue) {
Store(ctx, expr.type, new LatentOffset(alloc.getOffset(), attr.offset) );
Expand Down
8 changes: 4 additions & 4 deletions source/compiler/codegen/expression/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function Load(ctx: Context, type: SolidType, base: BasePointer, offset: n
if (!(type instanceof IntrinsicType)) Panic("Unimplemented");

switch (base.locality) {
case BasePointerType.global: ctx.block.push(Instruction.global.get(base)); break;
case BasePointerType.local: ctx.block.push(Instruction.local.get(base)); break;
case BasePointerType.global: ctx.block.push(Instruction.global.get(base.ref)); break;
case BasePointerType.local: ctx.block.push(Instruction.local.get(base.ref)); break;
default: AssertUnreachable(base.locality);
}

Expand Down Expand Up @@ -64,8 +64,8 @@ export function ResolveLinearType(ctx: Context, type: LinearType, ref: Reference
Load(ctx, base.type, ctx.file.owner.project.stackBase, type.offset);
} else {
switch (type.base.locality) {
case BasePointerType.global: ctx.block.push(Instruction.global.get(type.base)); break;
case BasePointerType.local: ctx.block.push(Instruction.local.get(type.base)); break;
case BasePointerType.global: ctx.block.push(Instruction.global.get(type.base.ref)); break;
case BasePointerType.local: ctx.block.push(Instruction.local.get(type.base.ref)); break;
default: AssertUnreachable(type.base.locality);
}

Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/postfix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function CompileCall(ctx: Context, syntax: Syntax.Term_Expr_call, operand: Opera
const alloc = ctx.scope.stack.allocate(primary.size, primary.align);
returnType = LinearType.make(primary, alloc, ctx.file.owner.project.stackBase);

ctx.block.push(Instruction.global.get(ctx.file.owner.project.stackReg.idx))
ctx.block.push(Instruction.global.get(ctx.file.owner.project.stackReg.ref))
ctx.block.push(Instruction.const.i32(alloc.getOffset()));
}
} else Panic(
Expand Down
12 changes: 8 additions & 4 deletions source/compiler/codegen/expression/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { StackAllocation } from "~/compiler/codegen/allocation/stack.ts";
import { ReferenceRange } from "~/parser.ts";
import { IsNamespace } from "~/compiler/file.ts";
import { Namespace } from "~/compiler/file.ts";
import { Intrinsic } from "~/wasm/type.ts";
import { LocalRef } from "~/wasm/funcRef.ts";


Expand Down Expand Up @@ -41,12 +40,17 @@ export function IsContainerType(a: any): boolean {


export enum BasePointerType { global, local };
export class BasePointer extends LocalRef {
export class BasePointer {
locality: BasePointerType;
ref: LocalRef;

constructor(type: Intrinsic, locality: BasePointerType) {
super(type);
constructor(locality: BasePointerType, ref: LocalRef) {
this.locality = locality;
this.ref = ref;
}

get () {
return this.ref.get();
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/compiler/codegen/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RegisterAllocator } from "~/compiler/codegen/allocation/registers.ts";
import { StackAllocator } from "~/compiler/codegen/allocation/stack.ts";
import { ReferenceRange } from "~/parser.ts";
import { Function } from "~/wasm/function.ts";
import { BasePointerType } from "~/compiler/codegen/expression/type.ts";

export class Scope {
_parent: Scope | null;
Expand Down Expand Up @@ -44,8 +45,7 @@ export class Scope {
} else if (type instanceof Structure) {
const reg = this.register.allocate(type.getBitcode(), true);

// TODO(@ajanibilby): fix immediately, don't bypass the reg allocator like this
const linear = LinearType.make(type, null, new BasePointer(type.getBitcode(), this._localRegs));
const linear = LinearType.make(type, null, new BasePointer(BasePointerType.local, reg.ref));
this.vars[name] = new StructVariable(name, linear);
this.vars[name].markDefined();
} else AssertUnreachable(type);
Expand Down
3 changes: 1 addition & 2 deletions source/compiler/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export default class Project {
true,
Instruction.const.i32(0)
);
this.stackBase = new BasePointer(Intrinsic.i32, BasePointerType.global);
this.stackBase.resolve(this.stackReg.idx);
this.stackBase = new BasePointer(BasePointerType.global, this.stackReg.ref);

this.module.addMemory(0, 1);
}
Expand Down
1 change: 0 additions & 1 deletion source/wasm/funcRef.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LatentValue, type Byte } from "~/helper.ts";

import { EncodeU32, Intrinsic } from "~/wasm/type.ts";

export class FuncRef extends LatentValue<number> {
Expand Down
13 changes: 6 additions & 7 deletions source/wasm/section/global.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { Intrinsic } from "~/wasm/type.ts";
import { EncodeU32 } from "~/wasm/type.ts";
import { Constant } from "~/wasm/instruction/constant.ts";
import { LocalRef } from "~/wasm/funcRef.ts";
import { Byte } from "~/helper.ts";


export class GlobalRegister {
idx: number;
type: Intrinsic;
mutable: boolean;
expr: Constant;
ref: LocalRef;

constructor(type: Intrinsic, mutable: boolean, expr: Constant, index: number) {
this.type = type;
this.idx = index;
this.ref = new LocalRef(type);
this.mutable = mutable;
this.expr = expr;
}

toBinary(): Byte[] {
return [
this.type,
this.ref.type,
this.mutable ? 0x01 : 0x00,

...this.expr.toBinary(),
Expand All @@ -41,8 +39,9 @@ export default class GlobalSection {
const idx = this.bindings.length;

const n = new GlobalRegister(type, mut, expr, idx);
this.bindings.push(n);
n.ref.resolve(idx);

this.bindings.push(n);
return n;
}

Expand Down

0 comments on commit 302ca44

Please sign in to comment.