-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.cpp
169 lines (140 loc) · 4.18 KB
/
runtime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
#include "runtime.hpp"
#include "gc.hpp"
#include "object.hpp"
static void printSchemeStackTrace(ThreadState *ts,
intptr_t maxLevel = -1) {
dprintf(2, "### Stack trace:\n");
FrameDescr fd = ts->lastFrameDescr();
intptr_t stackPtr = ts->lastStackPtr();
intptr_t stackTop = ts->firstStackPtr();
// Do a stack walk.
// XXX: duplicate code since gcScavScheme does almost the same.
intptr_t level = 0;
while (maxLevel == -1 || level < maxLevel) {
Object *thisClosure = NULL;
for (intptr_t i = 0; i < fd.frameSize; ++i) {
if (fd.isPtr(i)) {
Object **loc = reinterpret_cast<Object **>(stackPtr + i * 8);
if (i == fd.frameSize - 2) {
// fd, thisClo.
thisClosure = *loc;
}
else {
dprintf(2, "#%3ld Frame[%ld] ", level, i);
(*loc)->displayDetail(2);
dprintf(2, "\n");
}
}
}
assert(thisClosure);
dprintf(2, "#%3ld ^ Inside ", level);
thisClosure->displayDetail(2);
dprintf(2, "\n");
++level;
// Find prev stack
stackPtr += (1 + fd.frameSize) * 8;
if (stackPtr == stackTop) {
break;
}
dprintf(2, "-------------------------------\n");
assert(stackPtr < stackTop);
fd = *reinterpret_cast<FrameDescr *>(stackPtr - 16);
}
}
void Runtime::handleNotAClosure(Object *wat, ThreadState *ts) {
dprintf(2, "Not a closure: ");
wat->displayDetail(2);
dprintf(2, "\n");
printSchemeStackTrace(ts);
ts->destroy();
exit(1);
}
void Runtime::handleArgCountMismatch(Object *wat, intptr_t argc,
ThreadState *ts) {
dprintf(2, "Argument count mismatch: ");
wat->displayDetail(2);
dprintf(2, " need %ld, but got %ld\n",
wat->raw()->cloInfo()->funcArity(), argc);
printSchemeStackTrace(ts);
ts->destroy();
exit(1);
}
void Runtime::handleUserError(Object *wat, ThreadState *ts) {
Util::logObj("UserError", wat);
printSchemeStackTrace(ts);
ts->destroy();
exit(1);
}
void Runtime::handleStackOvf(ThreadState *ts) {
dprintf(2, "Runtime stack overflow.\n");
dprintf(2, "Stack starts at %p, but has grow to %p. Total size = %ld\n",
(void *) ts->firstStackPtr(), (void *) ts->lastStackPtr(),
ts->firstStackPtr() - ts->lastStackPtr());
dprintf(2, "We display 5 most recent call stack here.\n");
printSchemeStackTrace(ts, 5);
ts->destroy();
exit(1);
}
void Runtime::collectAndAlloc(ThreadState *ts) {
if (Option::global().kLogInfo) {
dprintf(2, "[Runtime::collect]\n");
}
ts->gcCollect();
/*
FrameDescr fd = ts->lastFrameDescr();
while (true) {
dprintf(2, "[Stack Walk] stackPtr = %p, fd = %ld, size = %d\n",
(void *) stackPtr, fd.pack(), fd.frameSize);
for (intptr_t i = 0; i < fd.frameSize; ++i) {
if (fd.isPtr(i)) {
dprintf(2, "[Stack Walk] %ld isPtr\n", i);
Util::logObj("E.g.", *(Object **) (stackPtr + i * 8));
}
}
// Get next stackPtr
stackPtr += (1 + fd.frameSize) * 8;
if (stackPtr == ts->firstStackPtr()) {
dprintf(2, "[Stack Walk] done\n");
break;
}
assert(stackPtr < ts->firstStackPtr());
// Get next framedescr. -2 slot for (fd + retAddr)
fd = *reinterpret_cast<FrameDescr *>(stackPtr - 16);
}
exit(1);
*/
}
void Runtime::traceObject(Object *wat) {
dprintf(2, "[Runtime::Trace] ");
wat->displayDetail(2);
dprintf(2, "\n");
}
intptr_t Runtime::endOfCode(intptr_t entry) {
auto info = RawObject::from(entry - RawObject::kFuncCodeOffset);
return entry + info->funcSize() - RawObject::kFuncCodeOffset;
}
void Runtime::printNewLine(int fd) {
dprintf(fd, "\n");
}
static Option option;
static bool envIs(const char *name, const char *val) {
char *maybeVal = getenv(name);
if (maybeVal && strcmp(maybeVal, val) == 0) {
return true;
}
return false;
}
void Option::init() {
if (option.kInitialized) {
return;
}
option.kInitialized = true;
option.kTailCallOpt = !envIs("SANYA_TCO", "NO");
option.kInsertStackCheck = !envIs("SANYA_STACKCHECK", "NO");
option.kLogInfo = envIs("SANYA_LOGINFO", "YES");
}
Option &Option::global() {
return option;
}