Skip to content

Commit 0239e47

Browse files
committed
Sketch in call stack region data structure.
1 parent 96b74d9 commit 0239e47

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

build/Makefile.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ OBJECTS = src/core/callsite@obj@ \
7474
src/core/compunit@obj@ \
7575
src/core/bytecode@obj@ \
7676
src/core/frame@obj@ \
77+
src/core/callstack@obj@ \
7778
src/core/validation@obj@ \
7879
src/core/bytecodedump@obj@ \
7980
src/core/threads@obj@ \
@@ -216,6 +217,7 @@ HEADERS = src/moar.h \
216217
src/core/interp.h \
217218
src/core/alloc.h \
218219
src/core/frame.h \
220+
src/core/callstack.h \
219221
src/core/compunit.h \
220222
src/core/bytecode.h \
221223
src/core/ops.h \

src/core/callstack.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "moar.h"
2+
3+
/* Allocates a new call stack region, not incorporated into the regions double
4+
* linked list yet. */
5+
static MVMCallStackRegion * create_region() {
6+
MVMCallStackRegion *region = MVM_malloc(MVM_CALLSTACK_REGIION_SIZE);
7+
region->prev = region->next = NULL;
8+
region->alloc = (char *)region + sizeof(MVMCallStackRegion);
9+
region->alloc_limit = (char *)region + MVM_CALLSTACK_REGIION_SIZE;
10+
return region;
11+
}
12+
13+
/* Called upon thread creation to set up an initial callstack region for the
14+
* thread. */
15+
void MVM_callstack_region_init(MVMThreadContext *tc) {
16+
tc->stack_first = tc->stack_current = create_region();
17+
}
18+
19+
/* Moves the current call stack region we're allocating/freeing in along to
20+
* the next one in the region chain, allocating that next one if needed. */
21+
MVMCallStackRegion * MVM_callstack_region_next(MVMThreadContext *tc) {
22+
MVMCallStackRegion *next_region = tc->stack_current->next;
23+
if (!next_region) {
24+
next_region = create_region();
25+
tc->stack_current->next = next_region;
26+
next_region->prev = tc->stack_current;
27+
}
28+
tc->stack_current = next_region;
29+
return next_region;
30+
}
31+
32+
/* Switches to the previous call stack region, if any. Otherwise, stays in
33+
* the current region. */
34+
MVMCallStackRegion * MVM_callstack_region_prev(MVMThreadContext *tc) {
35+
MVMCallStackRegion *prev_region = tc->stack_current->prev;
36+
if (prev_region)
37+
tc->stack_current = prev_region;
38+
else
39+
prev_region = tc->stack_current;
40+
return prev_region;
41+
}
42+
43+
/* Called at thread exit to destroy all callstack regions the thread has. */
44+
void MVM_callstack_region_destroy_all(MVMThreadContext *tc) {
45+
MVMCallStackRegion *cur = tc->stack_first;
46+
while (cur) {
47+
MVMCallStackRegion *next = cur->next;
48+
MVM_free(cur);
49+
cur = next;
50+
}
51+
tc->stack_first = NULL;
52+
}

src/core/callstack.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* A region of the call stack, used for call frames that have not escaped to
2+
* the heap. */
3+
struct MVMCallStackRegion {
4+
/* Next call stack region, which we start allocating in if this one is
5+
* full. NULL if none has been allocated yet. */
6+
MVMCallStackRegion *next;
7+
8+
/* Previous call stack region, if any. */
9+
MVMCallStackRegion *prev;
10+
11+
/* The place we'll allocate the next frame. */
12+
char *alloc;
13+
14+
/* The end of the allocatable region. */
15+
char *alloc_limit;
16+
};
17+
18+
/* The default size of a call stack region. */
19+
#define MVM_CALLSTACK_REGIION_SIZE 131072
20+
21+
/* Checks if a frame is allocated on a call stack or on the heap. If it is on
22+
* the call stack, then it will have zeroed flags (since heap-allocated frames
23+
* always have the "I'm a heap frame" bit set). */
24+
MVM_STATIC_INLINE MVMuint32 MVM_FRAME_IS_ON_CALLSTACK(MVMThreadContext *tc, MVMFrame *frame) {
25+
return frame->header.flags == 0;
26+
}
27+
28+
/* Functions for working with call stack regions. */
29+
void MVM_callstack_region_init(MVMThreadContext *tc);
30+
MVMCallStackRegion * MVM_callstack_region_next(MVMThreadContext *tc);
31+
MVMCallStackRegion * MVM_callstack_region_prev(MVMThreadContext *tc);
32+
void MVM_callstack_region_destroy_all(MVMThreadContext *tc);

src/core/threadcontext.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ struct MVMThreadContext {
5454
/* Thread object representing the thread. */
5555
MVMThread *thread_obj;
5656

57-
/* The frame lying at the base of the current thread. */
58-
MVMFrame *thread_entry_frame;
59-
6057
/* Pointer to where the interpreter's current opcode is stored. */
6158
MVMuint8 **interp_cur_op;
6259

@@ -71,9 +68,18 @@ struct MVMThreadContext {
7168
* is stored. */
7269
MVMCompUnit **interp_cu;
7370

71+
/* First call stack memory region, so we can traverse them for cleanup. */
72+
MVMCallStackRegion *stack_first;
73+
74+
/* Current call stack region, which the next frame will be allocated in. */
75+
MVMCallStackRegion *stack_current;
76+
7477
/* The frame we're currently executing. */
7578
MVMFrame *cur_frame;
7679

80+
/* The frame lying at the base of the current thread. */
81+
MVMFrame *thread_entry_frame;
82+
7783
/* libuv event loop */
7884
uv_loop_t *loop;
7985

src/moar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ MVM_PUBLIC const MVMint32 MVM_jit_support(void);
100100
#include "core/exceptions.h"
101101
#include "core/alloc.h"
102102
#include "core/frame.h"
103+
#include "core/callstack.h"
103104
#include "core/validation.h"
104105
#include "core/bytecode.h"
105106
#include "core/bytecodedump.h"

src/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef struct MVMCallCapture MVMCallCapture;
1717
typedef struct MVMCallCaptureBody MVMCallCaptureBody;
1818
typedef struct MVMCallsite MVMCallsite;
1919
typedef struct MVMCallsiteInterns MVMCallsiteInterns;
20+
typedef struct MVMCallStackRegion MVMCallStackRegion;
2021
typedef struct MVMCFunction MVMCFunction;
2122
typedef struct MVMCFunctionBody MVMCFunctionBody;
2223
typedef struct MVMCode MVMCode;

0 commit comments

Comments
 (0)