Skip to content

Commit

Permalink
feat: implement GoRoutineOp support in heap
Browse files Browse the repository at this point in the history
  • Loading branch information
shenyih0ng committed Apr 1, 2024
1 parent f2916a3 commit dfe8831
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
16 changes: 11 additions & 5 deletions src/go-slang/goroutine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,17 @@ const Interpreter: {
}

const { callee, args } = call as CallExpression
return C.pushR(...H.allocM([callee, ...args]), {
type: CommandType.GoRoutineOp,
calleeNodeId: A.track(callee).uid as number,
arity: args.length
})
return C.pushR(
...H.allocM([
callee,
...args,
{
type: CommandType.GoRoutineOp,
calleeNodeId: A.track(callee).uid as number,
arity: args.length
}
])
)
},

EmptyStatement: () => void {},
Expand Down
2 changes: 1 addition & 1 deletion src/go-slang/lib/heap/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// The default size of the heap in words
// Total heap size (in bytes) = DEFAULT_HEAP_SIZE * WORD_SIZE
export const DEFAULT_HEAP_SIZE = 1024 // in words
export const DEFAULT_HEAP_SIZE = 4096 // in words

// The smallest addressable unit in the heap
// We can think of it as the heap containing N number of words, each of size WORD_SIZE
Expand Down
24 changes: 13 additions & 11 deletions src/go-slang/lib/heap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ClosureOp,
CommandType,
EnvOp,
GoRoutineOp,
Node,
PopS,
UnaryOp,
Expand Down Expand Up @@ -72,6 +73,7 @@ export class Heap {
case CommandType.BinaryOp:
return this.allocateUnaryBinaryOp(value)
case CommandType.CallOp:
case CommandType.GoRoutineOp:
return this.allocateCallOp(value)
case CommandType.BuiltinOp:
return this.allocateBuiltinOp(value)
Expand Down Expand Up @@ -103,8 +105,9 @@ export class Heap {
return heap_addr
}

const tag = this.tag(heap_addr)
const mem_addr = heap_addr * WORD_SIZE
switch (this.tag(heap_addr)) {
switch (tag) {
case PointerTag.False:
return false
case PointerTag.True:
Expand All @@ -125,21 +128,18 @@ export class Heap {
idNodeUid: this.memory.getInt16(mem_addr + 1)
} as AssignOp
case PointerTag.UnaryOp:
return {
type: CommandType.UnaryOp,
opNodeId: this.memory.getInt16(mem_addr + 1)
} as UnaryOp
case PointerTag.BinaryOp:
return {
type: CommandType.BinaryOp,
type: tag === PointerTag.UnaryOp ? CommandType.UnaryOp : CommandType.BinaryOp,
opNodeId: this.memory.getInt16(mem_addr + 1)
} as BinaryOp
} as UnaryOp | BinaryOp
case PointerTag.CallOp:
case PointerTag.GoRoutineOp:
return {
type: CommandType.CallOp,
type: tag === PointerTag.CallOp ? CommandType.CallOp : CommandType.GoRoutineOp,
calleeNodeId: this.memory.getInt16(mem_addr + 1),
arity: this.memory.getInt16(mem_addr + 3)
} as CallOp
} as CallOp | GoRoutineOp
case PointerTag.BuiltInOp:
return {
type: CommandType.BuiltinOp,
Expand Down Expand Up @@ -225,8 +225,10 @@ export class Heap {
}

/* Memory Layout of a CallOp: [0:tag, 1-2:calleeNodeId, 3-4:arity, 5-6:size, 7:_unused_] (1 word) */
private allocateCallOp({ calleeNodeId, arity }: CallOp): HeapAddress {
const ptr_heap_addr = this.allocateTaggedPtr(PointerTag.CallOp)
private allocateCallOp({ type, calleeNodeId, arity }: CallOp | GoRoutineOp): HeapAddress {
const ptr_heap_addr = this.allocateTaggedPtr(
type === CommandType.CallOp ? PointerTag.CallOp : PointerTag.GoRoutineOp
)

const ptr_mem_addr = ptr_heap_addr * WORD_SIZE
// NOTE: assume there will be no more than 2^16 AST nodes
Expand Down
1 change: 1 addition & 0 deletions src/go-slang/lib/heap/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum PointerTag {
UnaryOp,
BinaryOp,
CallOp,
GoRoutineOp,
BuiltInOp,
ClosureOp,
EnvOp,
Expand Down

0 comments on commit dfe8831

Please sign in to comment.