Skip to content

Commit a934e9a

Browse files
authored
Restore block restart (#64)
This PR restores the restart primitive on blocks so that `#whileTrue:` and `#whileFalse:` work correctly for blocks that were not inlined. Issue reported by @sillycross. Thanks. Needs SOM-st/SOM#125 to be merged to update core-lib to a merged version.
2 parents a619100 + 3ae68b5 commit a934e9a

File tree

10 files changed

+41
-10
lines changed

10 files changed

+41
-10
lines changed

core-lib

src/interpreter/Interpreter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
THE SOFTWARE.
2727
*/
2828

29+
#include <assert.h>
30+
2931
#include "../misc/defs.h"
3032
#include "../vmobjects/ObjectFormats.h"
3133
#include "../vmobjects/VMFrame.h"
@@ -62,6 +64,12 @@ class Interpreter {
6264

6365
static inline size_t GetBytecodeIndex() { return bytecodeIndexGlobal; }
6466

67+
static void ResetBytecodeIndex(VMFrame* forFrame) {
68+
assert(frame == forFrame);
69+
assert(forFrame != nullptr);
70+
bytecodeIndexGlobal = 0;
71+
}
72+
6573
private:
6674
static vm_oop_t GetSelf();
6775

src/primitives/Block.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@
2323
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
THE SOFTWARE.
2525
*/
26+
27+
#include "Block.h"
28+
29+
#include "../vmobjects/VMFrame.h"
30+
31+
static void bRestart(VMFrame* frame) {
32+
frame->ResetBytecodeIndex();
33+
frame->ResetStackPointer();
34+
}
35+
36+
_Block::_Block() {
37+
Add("restart", &bRestart, false);
38+
}

src/primitives/Block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131

3232
class _Block : public PrimitiveContainer {
3333
public:
34-
_Block() = default;
34+
_Block();
3535
};

src/primitives/Method.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static vm_oop_t mSignature(vm_oop_t rcvr) {
2020
}
2121

2222
static void mInvokeOnWith(VMFrame* frame) {
23-
// REM: this is a clone with _Primitive::InvokeOn_With_
23+
// REM: this is a clone with _Primitive pInvokeOnWith
2424
auto* args = static_cast<VMArray*>(frame->Pop());
2525
auto* rcvr = frame->Pop();
2626
auto* mthd = static_cast<VMMethod*>(frame->Pop());

src/primitives/Method.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66
class _Method : public PrimitiveContainer {
77
public:
88
_Method();
9-
10-
void InvokeOn_With_(VMFrame*);
119
};

src/primitives/Primitive.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static vm_oop_t pSignature(vm_oop_t rcvr) {
1818
}
1919

2020
static void pInvokeOnWith(VMFrame* frame) {
21-
// REM: this is a clone with _Primitive::InvokeOn_With_
21+
// REM: this is a clone with _Method mInvokeOnWith
2222
auto* args = static_cast<VMArray*>(frame->Pop());
2323
auto* rcvr = frame->Pop();
2424
auto* mthd = static_cast<VMInvokable*>(frame->Pop());

src/primitives/Primitive.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66
class _Primitive : public PrimitiveContainer {
77
public:
88
_Primitive();
9-
10-
void InvokeOn_With_(VMFrame*);
119
};

src/vmobjects/VMFrame.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <string>
3333

3434
#include "../compiler/Disassembler.h"
35+
#include "../interpreter/Interpreter.h"
3536
#include "../memory/Heap.h"
3637
#include "../misc/defs.h"
3738
#include "../vm/Globals.h"
@@ -256,3 +257,8 @@ void VMFrame::CopyArgumentsFrom(VMFrame* frame) {
256257
std::string VMFrame::AsDebugString() const {
257258
return "VMFrame(" + GetMethod()->AsDebugString() + ")";
258259
}
260+
261+
void VMFrame::ResetBytecodeIndex() {
262+
bytecodeIndex = 0;
263+
Interpreter::ResetBytecodeIndex(this);
264+
}

src/vmobjects/VMFrame.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class VMFrame : public AbstractVMObject {
5252
arguments((gc_oop_t*)&(stack_ptr) + 1),
5353
locals(arguments + method->GetNumberOfArguments()),
5454
stack_ptr(locals + method->GetNumberOfLocals() - 1) {
55-
// initilize all other fields. Don't need to initalize arguments,
56-
// because they iwll be copied in still
55+
// initialize all other fields. Don't need to initialize arguments,
56+
// because they will be copied in still
5757
// --> until end of Frame
5858
auto* end = (gc_oop_t*)SHIFTED_PTR(this, totalObjectSize);
5959
size_t i = 0;
@@ -181,6 +181,14 @@ class VMFrame : public AbstractVMObject {
181181
size_t bytecodeIndex{0};
182182
size_t totalObjectSize;
183183

184+
void ResetStackPointer() {
185+
VMMethod* meth = GetMethod();
186+
// Set the stack pointer to its initial value thereby clearing the stack
187+
stack_ptr = locals + meth->GetNumberOfLocals() - 1;
188+
}
189+
190+
void ResetBytecodeIndex();
191+
184192
private:
185193
GCFrame* previousFrame;
186194
GCFrame* context{nullptr};

0 commit comments

Comments
 (0)