Skip to content

Commit

Permalink
[Update] Delete ALLOC opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Siubaak committed Nov 17, 2019
1 parent 4b919fd commit 57e8d12
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/compile/declaration.ts
Expand Up @@ -16,7 +16,7 @@ export function VariableDeclaration(node: estree.VariableDeclaration, state: Sta
state.opCodes.push({ op: OP.LOADK })
}
if (declr.id.type === 'Identifier') {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(declr.id.name).pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(declr.id.name).pointer , alloc: true })
} else { // declr.id.type === 'Pattern'
compilePattern(declr.id, state)
}
Expand All @@ -26,13 +26,13 @@ export function VariableDeclaration(node: estree.VariableDeclaration, state: Sta

export function FunctionDeclaration(node: estree.FunctionDeclaration, state: State) {
compileFunc(node, state)
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(node.id.name, 'var').pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(node.id.name, 'var').pointer , alloc: true })
}

export function ClassDeclaration(node: estree.ClassDeclaration, state: State) {
const pointer = state.symbols.set(node.id.name, 'var').pointer
state.opCodes.push({ op: OP.LOADK })
state.opCodes.push({ op: OP.ALLOC, val: pointer })
state.opCodes.push({ op: OP.STORE, val: pointer , alloc: true })
compileClass(node, state)
state.opCodes.push({ op: OP.STORE, val: pointer })
}
4 changes: 2 additions & 2 deletions src/compile/expression.ts
Expand Up @@ -53,7 +53,7 @@ export function FunctionExpression(node: estree.FunctionExpression, state: State
// allocate a placeholder for a tmp function to use inside the function scope
const pointer = state.symbols.set(node.id.name, 'const').pointer
state.opCodes.push({ op: OP.LOADK })
state.opCodes.push({ op: OP.ALLOC, val: pointer })
state.opCodes.push({ op: OP.STORE, val: pointer , alloc: true })
// compile the function
compileFunc(node, state)
// store the function into the placeholder above
Expand Down Expand Up @@ -334,7 +334,7 @@ export function ClassExpression(node: estree.ClassExpression, state: State) {
// allocate a placeholder for a tmp class to use inside the constructor function scope
const pointer = state.symbols.set(node.id.name, 'const').pointer
state.opCodes.push({ op: OP.LOADK })
state.opCodes.push({ op: OP.ALLOC, val: pointer })
state.opCodes.push({ op: OP.STORE, val: pointer , alloc: true })
// compile the class
compileClass(node, state)
// store the class into the placeholder above
Expand Down
16 changes: 8 additions & 8 deletions src/compile/helper.ts
Expand Up @@ -24,18 +24,18 @@ export function compileFunc(node: FunctionDefinition, state: State) {
// isolate outer try catch block, call expression will give true runtime catch statement pc
state.catchPcStack.push(null)
if (!arrow) {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set('this', 'const').pointer })
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set('arguments', 'var').pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set('this', 'const').pointer , alloc: true })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set('arguments', 'var').pointer , alloc: true })
}
for (let i = 0; i < node.params.length; i++) {
const param = node.params[i]
if (param.type === 'Identifier') {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(param.name, 'var').pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(param.name, 'var').pointer , alloc: true })
} else if (param.type === 'RestElement') {
state.opCodes.push({ op: OP.REST, val: 'func' })
const value = param.argument
if (value.type === 'Identifier') {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(value.name, 'var').pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(value.name, 'var').pointer , alloc: true })
} else if (value.type === 'MemberExpression') {
compile(value.object, state)
if (value.object.type === 'Super') {
Expand Down Expand Up @@ -144,11 +144,11 @@ export function compileForXStatement(node: estree.ForInStatement | estree.ForOfS
const opCodes = state.opCodes
const symbols = state.symbols

opCodes.push({ op: OP.ALLOC, val: symbols.set(len, 'const').pointer }) // const len = { stack.pop() }
opCodes.push({ op: OP.ALLOC, val: symbols.set(kovs, 'const').pointer }) // const kovs = { stack.pop() }
opCodes.push({ op: OP.STORE, val: symbols.set(len, 'const').pointer , alloc: true }) // const len = { stack.pop() }
opCodes.push({ op: OP.STORE, val: symbols.set(kovs, 'const').pointer , alloc: true }) // const kovs = { stack.pop() }
// let idx = 0
opCodes.push({ op: OP.LOADK, val: 0 })
opCodes.push({ op: OP.ALLOC, val: symbols.set(idx, 'let').pointer })
opCodes.push({ op: OP.STORE, val: symbols.set(idx, 'let').pointer , alloc: true })
// while (idx < len) {
const testPc = opCodes.length
opCodes.push({ op: OP.LOADV, val: symbols.get(idx).pointer })
Expand Down Expand Up @@ -177,7 +177,7 @@ export function compileForXStatement(node: estree.ForInStatement | estree.ForOfS
for (let i = 0; i < left.declarations.length; i++) {
const declr = left.declarations[i]
if (declr.id.type === 'Identifier') {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(declr.id.name).pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(declr.id.name).pointer , alloc: true })
} else { // declr.id.type === 'Pattern'
compilePattern(declr.id, state)
}
Expand Down
6 changes: 3 additions & 3 deletions src/compile/pattern.ts
Expand Up @@ -31,7 +31,7 @@ export function ObjectPattern(node: estree.ObjectPattern, state: State) {
}
if (value.type === 'Identifier') {
if (state.symbols.type) {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(value.name).pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(value.name).pointer , alloc: true })
} else {
state.opCodes.push({ op: OP.STORE, val: state.symbols.get(value.name).pointer })
}
Expand Down Expand Up @@ -69,7 +69,7 @@ export function ArrayPattern(node: estree.ArrayPattern, state: State) {
}
if (value.type === 'Identifier') {
if (state.symbols.type) {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(value.name).pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(value.name).pointer , alloc: true })
} else {
state.opCodes.push({ op: OP.STORE, val: state.symbols.get(value.name).pointer })
}
Expand Down Expand Up @@ -104,7 +104,7 @@ export function AssignmentPattern(node: estree.AssignmentPattern, state: State)
ifnotCode.val = state.opCodes.length
if (node.left.type === 'Identifier') {
if (state.symbols.type) {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(node.left.name).pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(node.left.name).pointer , alloc: true })
} else {
state.opCodes.push({ op: OP.STORE, val: state.symbols.get(node.left.name).pointer })
}
Expand Down
2 changes: 1 addition & 1 deletion src/compile/statement.ts
Expand Up @@ -100,7 +100,7 @@ export function TryStatement(node: estree.TryStatement, state: State) {
const param = node.handler.param
if (param) {
if (param.type === 'Identifier') {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(param.name, 'var').pointer })
state.opCodes.push({ op: OP.STORE, val: state.symbols.set(param.name, 'var').pointer , alloc: true })
}
}
compile(node.handler.body, state)
Expand Down
9 changes: 6 additions & 3 deletions src/jsvm/index.ts
Expand Up @@ -19,7 +19,8 @@ function step(state: State) {
stack[state.esp++] = value
break
}
case OP.ALLOC: {
case OP.STORE: {
if (code.alloc) {
const ebp = state.ebpList[state.ebpList.length - 1]
const storeVal = state.esp > ebp ? stack[--state.esp] : undefined
state.context[code.val] = { store: storeVal }
Expand All @@ -29,9 +30,11 @@ function step(state: State) {
const globalVarName = state.symbols.globalVarName[code.val]
define(globalVariable, globalVarName, { value: storeVal, writable: true, enumerable: true })
}
break
} else {
state.context[code.val].store = stack[--state.esp]
}
break
}
case OP.STORE: state.context[code.val].store = stack[--state.esp]; break
case OP.BIOP: {
const right = stack[--state.esp]
const left = stack[--state.esp]
Expand Down
3 changes: 1 addition & 2 deletions src/share/const.ts
@@ -1,8 +1,7 @@
export enum OP {
LOADK, // push literal into stack (val: literal cooked value)
LOADV, // push variable value into stack (val: context pointer)
ALLOC, // allocate a context item and move data from stack into it, like declaration (val: context pointer)
STORE, // move data from stack to context, like assignment (val: context pointer)
STORE, // move data from stack to context (val: context pointer, alloc: allocate or just assign)
BIOP, // binary operation (val: operator)
UNOP, // unary operation (val: operator)
JMP, // jump to specified position (val: jumped pc)
Expand Down

0 comments on commit 57e8d12

Please sign in to comment.