Skip to content

Commit

Permalink
Wipes bytePointer arithmetic off soft VM
Browse files Browse the repository at this point in the history
Previous implementation of soft VM decoded instructions manually.
Parts of instructions were parsed in the actual code being executed.
This was not clean and error prone.

Current commit removes all pointer arithmetic because all is done
within decoder of TSmalltalkInstruction.

Issue: #32
  • Loading branch information
0x7CFE committed Mar 25, 2014
1 parent 8c01808 commit 557dd15
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ SmalltalkVM::TExecuteResult SmalltalkVM::execute(TProcess* p, uint32_t ticks)
}

// Decoding the instruction
const uint16_t lastBytePointer = ec.bytePointer;
ec.instruction.decodeBytecode(byteCodes, ec.bytePointer);

// And executing it
Expand Down Expand Up @@ -295,7 +296,7 @@ SmalltalkVM::TExecuteResult SmalltalkVM::execute(TProcess* p, uint32_t ticks)
} break;

default:
std::fprintf(stderr, "VM: Invalid opcode %d at offset %d in method ", ec.instruction.getOpcode(), ec.bytePointer);
std::fprintf(stderr, "VM: Invalid opcode %d at offset %d in method ", ec.instruction.getOpcode(), lastBytePointer);
std::fprintf(stderr, "'%s'\n", ec.currentContext->method->name->toString().c_str() );
std::exit(1);
}
Expand All @@ -317,11 +318,8 @@ void SmalltalkVM::doPushBlock(TVMExecutionContext& ec)
// right after the block's body. There we'll probably find the actual invoking code
// such as sendMessage to a receiver with our block as a parameter or something similar.

// Reading new byte pointer that points to the code right after the inline block
uint16_t newBytePointer = byteCodes[ec.bytePointer] | (byteCodes[ec.bytePointer+1] << 8);

// Skipping the newBytePointer's data
ec.bytePointer += 2;
// New byte pointer that points to the code right after the inline block
uint16_t newBytePointer = ec.instruction.getExtra();

// Creating block object
hptr<TBlock> newBlock = newObject<TBlock>();
Expand All @@ -333,7 +331,7 @@ void SmalltalkVM::doPushBlock(TVMExecutionContext& ec)
newBlock->argumentLocation = ec.instruction.getArgument();
newBlock->blockBytePointer = ec.bytePointer;

//We set block->bytePointer, stackTop, previousContext when block is invoked
// We set block->bytePointer, stackTop, previousContext when block is invoked

// Assigning creatingContext depending on the hierarchy
// Nested blocks inherit the outer creating context
Expand Down Expand Up @@ -598,29 +596,25 @@ SmalltalkVM::TExecuteResult SmalltalkVM::doSpecial(hptr<TProcess>& process, TVME
break;

case special::branch:
ec.bytePointer = byteCodes[ec.bytePointer] | (byteCodes[ec.bytePointer+1] << 8);
ec.bytePointer = ec.instruction.getExtra();
break;

case special::branchIfTrue: {
ec.returnedValue = ec.stackPop();

if (ec.returnedValue == globals.trueObject)
ec.bytePointer = byteCodes[ec.bytePointer] | (byteCodes[ec.bytePointer+1] << 8);
else
ec.bytePointer += 2;
ec.bytePointer = ec.instruction.getExtra();
} break;

case special::branchIfFalse: {
ec.returnedValue = ec.stackPop();

if (ec.returnedValue == globals.falseObject)
ec.bytePointer = byteCodes[ec.bytePointer] | (byteCodes[ec.bytePointer+1] << 8);
else
ec.bytePointer += 2;
ec.bytePointer = ec.instruction.getExtra();
} break;

case special::sendToSuper: {
uint32_t literalIndex = byteCodes[ec.bytePointer++];
uint32_t literalIndex = ec.instruction.getExtra();
TSymbol* messageSelector = literals[literalIndex];
TClass* receiverClass = ec.currentContext->method->klass->parentClass;
TObjectArray* messageArguments = ec.stackPop<TObjectArray>();
Expand Down Expand Up @@ -662,7 +656,7 @@ void SmalltalkVM::doPushConstant(TVMExecutionContext& ec)

SmalltalkVM::TExecuteResult SmalltalkVM::doPrimitive(hptr<TProcess>& process, TVMExecutionContext& ec)
{
uint8_t opcode = (*ec.currentContext->method->byteCodes)[ec.bytePointer++];
uint8_t opcode = ec.instruction.getExtra();

// First of all, executing the primitive
// If primitive succeeds then stop execution of the current method
Expand Down

0 comments on commit 557dd15

Please sign in to comment.