Skip to content

Commit

Permalink
feat: implement support for EnvOp in heap
Browse files Browse the repository at this point in the history
  • Loading branch information
shenyih0ng committed Mar 31, 2024
1 parent e0caae7 commit c483776
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
9 changes: 3 additions & 6 deletions src/go-slang/ece.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ export function evaluate(program: SourceFile, slangContext: SlangContext): Value
}
}

// TEMP: for debugging purposes
console.log('Total memory allocated: ', H._debug_num_allocations() * 8, 'bytes')

return 'Program exited'
}

Expand All @@ -128,8 +125,8 @@ const interpreter: {
E.declare(id.name, { type: CommandType.ClosureOp, funcDeclNodeUid, envId: E.id() } as ClosureOp)
},

Block: ({ statements }: Block, { C, E }) => {
C.pushR(...statements, { type: CommandType.EnvOp, envId: E.id() })
Block: ({ statements }: Block, { C, E, H }) => {
C.pushR(...statements, H.alloc({ type: CommandType.EnvOp, envId: E.id() }))
E.extend({})
},

Expand Down Expand Up @@ -240,7 +237,7 @@ const interpreter: {
return new FuncArityError(calleeId.name, values.length, params.length)
}

C.pushR(body, RetMarker, { type: CommandType.EnvOp, envId: E.id() })
C.pushR(body, RetMarker, H.alloc({ type: CommandType.EnvOp, envId: E.id() }))
// set the environment to the closure's environment
E.setId(envId).extend(Object.fromEntries(zip(paramNames, values)))
},
Expand Down
29 changes: 28 additions & 1 deletion src/go-slang/lib/heap/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { BuiltinOp, CallOp, ClosureOp, CommandType, Node, isCommand, isNode } from '../../types'
import {
BuiltinOp,
CallOp,
ClosureOp,
CommandType,
EnvOp,
Node,
isCommand,
isNode
} from '../../types'
import { AstMap } from '../astMap'
import { DEFAULT_HEAP_SIZE, WORD_SIZE } from './config'
import { PointerTag } from './tags'
Expand Down Expand Up @@ -56,6 +65,8 @@ export class Heap {
return this.allocateBuiltinOp(value)
case CommandType.ClosureOp:
return this.allocateClosureOp(value)
case CommandType.EnvOp:
return this.allocateEnvOp(value)
}
}

Expand Down Expand Up @@ -106,6 +117,11 @@ export class Heap {
funcDeclNodeUid: this.memory.getInt16(mem_addr + 1),
envId: this.memory.getInt16(mem_addr + 3)
} as ClosureOp
case PointerTag.EnvOp:
return {
type: CommandType.EnvOp,
envId: this.memory.getInt16(mem_addr + 1)
} as EnvOp
}
}

Expand Down Expand Up @@ -177,6 +193,17 @@ export class Heap {
return ptr_heap_addr
}

/* Memory Layout of an EnvOp: [0:tag, 1-2:envId, 3-4:_unused_, 5-6:size, 7:_unused_] (1 word) */
private allocateEnvOp({ envId }: EnvOp): HeapAddress {
const ptr_heap_addr = this.allocateTaggedPtr(PointerTag.EnvOp)

const ptr_mem_addr = ptr_heap_addr * WORD_SIZE
// NOTE: assume there will be no more than 2^16 envs
this.memory.setInt16(ptr_mem_addr + 1, envId)

return ptr_heap_addr
}

/**
* Allocate a tagged pointer in the heap
*
Expand Down
3 changes: 2 additions & 1 deletion src/go-slang/lib/heap/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export enum PointerTag {
AstNode,
CallOp,
BuiltInOp,
ClosureOp
ClosureOp,
EnvOp
}

0 comments on commit c483776

Please sign in to comment.