Fix timeout handling in the test harness.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
26 #include "NativeTable.h"
31 Stack::Stack(Zone *zone)
46 buffer_ = (char *)malloc(STACK_SIZE);
50 top_.sp = reinterpret_cast<LocalSlot *>(buffer_);
53 end_ = buffer_ + STACK_SIZE;
57 InvokeArgs::~InvokeArgs()
59 stack_->popInvokeFrame(*this);
62 NativeArgs::~NativeArgs()
64 stack_->popNativeFrame(*this);
68 SizeForCodeFrame(Code *code)
70 return sizeof(Frame) + sizeof(LocalSlot) * (code->nlocals() + code->maxStackDepth());
74 Stack::pushInvokeFrame(Handle<Function> fun, InvokeArgs &args)
76 size_t nbytes = SizeForCodeFrame(fun->code()) +
77 sizeof(LocalSlot) * fun->nargs();
78 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
83 memset(top_.sp, 0, sizeof(LocalSlot) * fun->nargs());
85 args.nargs_ = fun->nargs();
87 top_.sp += fun->nargs();
89 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
90 new (frame) Frame(top_.fp, top_.pc, fun, fun->code(), zone_->heap().lifoPosition());
92 top_.pc = frame->code()->start();
93 top_.sp = frame->stackBase();
100 Stack::pushModuleFrame(Handle<Module> module, InvokeArgs &args)
102 size_t nbytes = SizeForCodeFrame(module->code());
103 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
108 args.args_ = top_.sp;
112 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
113 new (frame) Frame(top_.fp, top_.pc, NULL, module->code(), zone_->heap().lifoPosition());
115 top_.pc = frame->code()->start();
116 top_.sp = frame->stackBase();
123 Stack::pushNativeFrame(Handle<Native> native, NativeArgs &args, unsigned argc)
125 size_t nbytes = sizeof(intptr_t) * argc + sizeof(Frame);
126 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
131 memset(top_.sp, 0, sizeof(intptr_t) * argc);
132 args.argsBase_ = top_.sp;
134 top_.sp = reinterpret_cast<LocalSlot *>(reinterpret_cast<intptr_t *>(top_.sp) + argc);
136 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
137 new (frame) Frame(top_.fp, top_.pc, native, zone_->heap().lifoPosition());
140 top_.sp = reinterpret_cast<LocalSlot *>(frame + 1);
147 Stack::pushInlineFrame(Handle<Function> fun)
149 Code *code = fun->code();
150 size_t nbytes = SizeForCodeFrame(code);
151 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
152 zone_->reportError(Message_TooMuchRecursion);
156 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
157 new (frame) Frame(top_.fp, top_.pc, fun, code, zone_->heap().lifoPosition());
158 memset(frame->locals(), 0, sizeof(LocalSlot) * code->nlocals());
160 top_.pc = frame->code()->start();
161 top_.sp = frame->stackBase();
169 Stack::popInlineFrame()
171 zone_->heap().unwindLifo(top_.fp->lifoPosition());
173 Frame *prev = top_.fp->prev();
174 Function *fun = top_.fp->fun();
176 top_.pc = top_.fp->prevpc();
177 top_.sp = reinterpret_cast<LocalSlot *>(top_.fp) - fun->nargs();
183 Stack::popInvokeFrame(InvokeArgs &args)
185 zone_->heap().unwindLifo(top_.fp->lifoPosition());
187 top_.pc = top_.fp->prevpc();
188 top_.sp = args.args_;
189 top_.fp = top_.fp->prev();
193 Stack::popNativeFrame(NativeArgs &args)
195 zone_->heap().unwindLifo(top_.fp->lifoPosition());
197 top_.pc = top_.fp->prevpc();
198 top_.sp = args.argsBase_;
199 top_.fp = top_.fp->prev();
203 Frame::accept(VirtualObjectVisitor *visitor)
205 visitor->visit(reinterpret_cast<Object **>(&callee_));
206 visitor->visit(reinterpret_cast<Object **>(&code_));
210 TraceFunctionFrame(VirtualObjectVisitor *visitor, Frame *fp, LocalSlot *sp, bytecode *pc)
212 Code *code = fp->code();
213 LocalSlot *slots = fp->locals();
215 // Trace local variables.
216 for (unsigned i = 0; i < code->nlocals(); i++) {
217 Type *type = code->localType(i);
218 if (type->isTraceable())
219 visitor->visit(&slots[i].obj);
222 // Trace arguments, if a function call. Note that this means we usually
223 // mark arguments twice, once on the caller side and once on the callee.
224 // This makes it easier to deal with invoke frames which have no caller.
225 if (Function *fun = fp->maybeFun()) {
226 for (unsigned i = 0; i < fun->nargs(); i++) {
227 Type *type = fun->argType(i);
228 if (type->isTraceable())
229 visitor->visit(&fp->arg(i).obj);
233 // Finally, trace stack slots. Not every gc-point in the bytecode will
234 // have a safepoint; since they are actually uncommon, we don't emit
235 // them unless at least one stack slot would be marked.
236 if (uint8 *safepoint = code->safepointAt(code->pcToOffset(pc))) {
237 LocalSlot *base = fp->stackBase();
238 unsigned stackDepth = unsigned(sp - fp->stackBase());
239 assert(stackDepth <= code->maxStackDepth());
241 for (unsigned i = 0; i < stackDepth; i++) {
242 if (safepoint[i / 8] & (1 << (i % 8)))
243 visitor->visit(&base[i].obj);
249 Stack::accept(VirtualObjectVisitor *visitor)
251 LocalSlot *sp = top_.sp;
252 bytecode *pc = top_.pc;
254 for (Frame *frame = top_.fp; frame; frame = frame->prev()) {
255 if (!frame->isNative())
256 TraceFunctionFrame(visitor, frame, sp, pc);
257 frame->accept(visitor);
259 sp = reinterpret_cast<LocalSlot *>(frame);
260 pc = frame->prevpc();
265 Stack::isInCallStack(Handle<Module> module)
267 for (Frame *frame = top_.fp; frame; frame = frame->prev()) {
268 if (frame->code()->module() == module)
274 StackIter::StackIter(Zone *zone)
275 : pc_(zone->stack().top()->pc),
276 fp_(zone->stack().top()->fp)
281 StackIter::done() const
287 StackIter::function() const
290 return ZONE()->nativeTable()->name(fp_->native()->index())->chars();
291 if (Function *fun = fp_->maybeFun())
292 return fun->name()->chars();
297 StackIter::lineNumber() const
301 if (fp_->maybeFun()) {
302 unsigned offs = fp_->code()->pcToOffset(pc_);
303 return fp_->code()->pcToSource(offs).line;
305 return fp_->code()->lineNumber();
309 StackIter::source() const
313 return fp_->code()->module()->file()
314 ? fp_->code()->module()->file()->chars()