Handle invalid function calls
This commit is contained in:
@@ -16,11 +16,13 @@ func TestCal(t *testing.T) {
|
|||||||
functions:
|
functions:
|
||||||
- - [cal, +1]
|
- - [cal, +1]
|
||||||
- [add, g0, 1]
|
- [add, g0, 1]
|
||||||
|
- [cal, +2] # Function doesn't exist, immediate implicit return
|
||||||
|
- [add, g0, 2]
|
||||||
|
|
||||||
- - [mov, g0, 5]
|
- - [mov, g0, 5]
|
||||||
`)
|
`)
|
||||||
|
|
||||||
expectGlobalMemory(t, state, 0, 6)
|
expectGlobalMemory(t, state, 0, 8)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRet(t *testing.T) {
|
func TestRet(t *testing.T) {
|
||||||
|
|||||||
@@ -71,6 +71,10 @@ func (state *State) processInstruction() {
|
|||||||
state.instructionIndex += 1
|
state.instructionIndex += 1
|
||||||
instr.opHandler(state, instr)
|
instr.opHandler(state, instr)
|
||||||
|
|
||||||
|
if state.functionIndex >= int64(len(state.functions)) {
|
||||||
|
state.ret()
|
||||||
|
}
|
||||||
|
|
||||||
if state.instructionIndex >= int64(len(fnc)) {
|
if state.instructionIndex >= int64(len(fnc)) {
|
||||||
state.ret()
|
state.ret()
|
||||||
}
|
}
|
||||||
@@ -132,11 +136,6 @@ func (state *State) writeSigned(op *Operand, value int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) call(functionOffset int64) {
|
func (state *State) call(functionOffset int64) {
|
||||||
if state.functionIndex+functionOffset >= int64(len(state.functions)) {
|
|
||||||
state.setError(fmt.Errorf("Invalid function call index: %d + %d = %d", state.functionIndex, functionOffset, state.functionIndex+functionOffset))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
stackFrame := newStackFrame(state)
|
stackFrame := newStackFrame(state)
|
||||||
state.stack = append(state.stack, stackFrame)
|
state.stack = append(state.stack, stackFrame)
|
||||||
state.functionIndex += functionOffset
|
state.functionIndex += functionOffset
|
||||||
|
|||||||
Reference in New Issue
Block a user