Added AMBuild support.
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/.
29 Stack::Stack(Zone *zone)
44 buffer_ = (char *)malloc(STACK_SIZE);
48 top_.sp = reinterpret_cast<LocalSlot *>(buffer_);
51 end_ = buffer_ + STACK_SIZE;
55 InvokeArgs::~InvokeArgs()
57 stack_->popInvokeFrame(*this);
60 NativeArgs::~NativeArgs()
62 stack_->popNativeFrame(*this);
66 SizeForCodeFrame(Code *code)
68 return sizeof(Frame) + sizeof(LocalSlot) * (code->nlocals() + code->maxStackDepth());
72 Stack::pushInvokeFrame(Handle<Function> fun, InvokeArgs &args)
74 size_t nbytes = SizeForCodeFrame(fun->code()) +
75 sizeof(LocalSlot) * fun->nargs();
76 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
81 memset(top_.sp, 0, sizeof(LocalSlot) * fun->nargs());
83 args.nargs_ = fun->nargs();
85 top_.sp += fun->nargs();
87 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
88 new (frame) Frame(top_.fp, top_.pc, fun, fun->code(), zone_->heap().lifoPosition());
90 top_.pc = frame->code()->start();
91 top_.sp = frame->stackBase();
98 Stack::pushModuleFrame(Handle<Module> module, InvokeArgs &args)
100 size_t nbytes = SizeForCodeFrame(module->code());
101 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
106 args.args_ = top_.sp;
110 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
111 new (frame) Frame(top_.fp, top_.pc, NULL, module->code(), zone_->heap().lifoPosition());
113 top_.pc = frame->code()->start();
114 top_.sp = frame->stackBase();
121 Stack::pushNativeFrame(Handle<Native> native, NativeArgs &args, unsigned argc)
123 size_t nbytes = sizeof(intptr_t) * argc + sizeof(Frame);
124 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
129 memset(top_.sp, 0, sizeof(intptr_t) * argc);
130 args.argsBase_ = top_.sp;
132 top_.sp = reinterpret_cast<LocalSlot *>(reinterpret_cast<intptr_t *>(top_.sp) + argc);
134 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
135 new (frame) Frame(top_.fp, top_.pc, native, zone_->heap().lifoPosition());
138 top_.sp = reinterpret_cast<LocalSlot *>(frame + 1);
145 Stack::pushInlineFrame(Handle<Function> fun)
147 Code *code = fun->code();
148 size_t nbytes = SizeForCodeFrame(code);
149 if (reinterpret_cast<char *>(top_.sp) + nbytes >= end_) {
150 zone_->reportError(Message_TooMuchRecursion);
154 Frame *frame = reinterpret_cast<Frame *>(top_.sp);
155 new (frame) Frame(top_.fp, top_.pc, fun, code, zone_->heap().lifoPosition());
156 memset(frame->locals(), 0, sizeof(LocalSlot) * code->nlocals());
158 top_.pc = frame->code()->start();
159 top_.sp = frame->stackBase();
167 Stack::popInlineFrame()
169 zone_->heap().unwindLifo(top_.fp->lifoPosition());
171 Frame *prev = top_.fp->prev();
172 Function *fun = top_.fp->fun();
174 top_.pc = top_.fp->prevpc();
175 top_.sp = reinterpret_cast<LocalSlot *>(top_.fp) - fun->nargs();
181 Stack::popInvokeFrame(InvokeArgs &args)
183 zone_->heap().unwindLifo(top_.fp->lifoPosition());
185 top_.pc = top_.fp->prevpc();
186 top_.sp = args.args_;
187 top_.fp = top_.fp->prev();
191 Stack::popNativeFrame(NativeArgs &args)
193 zone_->heap().unwindLifo(top_.fp->lifoPosition());
195 top_.pc = top_.fp->prevpc();
196 top_.sp = args.argsBase_;
197 top_.fp = top_.fp->prev();
201 Frame::accept(VirtualObjectVisitor *visitor)
203 visitor->visit(reinterpret_cast<Object **>(&callee_));
204 visitor->visit(reinterpret_cast<Object **>(&code_));
208 TraceFunctionFrame(VirtualObjectVisitor *visitor, Frame *fp, LocalSlot *sp, bytecode *pc)
210 Code *code = fp->code();
211 LocalSlot *slots = fp->locals();
213 // Trace local variables.
214 for (unsigned i = 0; i < code->nlocals(); i++) {
215 Type *type = code->localType(i);
216 if (type->isTraceable())
217 visitor->visit(&slots[i].obj);
220 // Trace arguments, if a function call. Note that this means we usually
221 // mark arguments twice, once on the caller side and once on the callee.
222 // This makes it easier to deal with invoke frames which have no caller.
223 if (Function *fun = fp->maybeFun()) {
224 for (unsigned i = 0; i < fun->nargs(); i++) {
225 Type *type = fun->argType(i);
226 if (type->isTraceable())
227 visitor->visit(&fp->arg(i).obj);
231 // Finally, trace stack slots. Not every gc-point in the bytecode will
232 // have a safepoint; since they are actually uncommon, we don't emit
233 // them unless at least one stack slot would be marked.
234 if (uint8 *safepoint = code->safepointAt(code->pcToOffset(pc))) {
235 LocalSlot *base = fp->stackBase();
236 unsigned stackDepth = unsigned(sp - fp->stackBase());
237 assert(stackDepth <= code->maxStackDepth());
239 for (unsigned i = 0; i < stackDepth; i++) {
240 if (safepoint[i / 8] & (1 << (i % 8)))
241 visitor->visit(&base[i].obj);
247 Stack::accept(VirtualObjectVisitor *visitor)
249 LocalSlot *sp = top_.sp;
250 bytecode *pc = top_.pc;
252 for (Frame *frame = top_.fp; frame; frame = frame->prev()) {
253 if (!frame->isNative())
254 TraceFunctionFrame(visitor, frame, sp, pc);
255 frame->accept(visitor);
257 sp = reinterpret_cast<LocalSlot *>(frame);
258 pc = frame->prevpc();
263 Stack::isInCallStack(Handle<Module> module)
265 for (Frame *frame = top_.fp; frame; frame = frame->prev()) {
266 if (frame->code()->module() == module)
272 StackIter::StackIter(Zone *zone)
273 : pc_(zone->stack().top()->pc),
274 fp_(zone->stack().top()->fp)
279 StackIter::done() const
285 StackIter::function() const
288 return fp_->native()->name()->chars();
289 if (Function *fun = fp_->maybeFun())
290 return fun->name()->chars();
295 StackIter::lineNumber() const
299 if (fp_->maybeFun()) {
300 unsigned offs = fp_->code()->pcToOffset(pc_);
301 return fp_->code()->pcToSource(offs).line;
303 return fp_->code()->lineNumber();
307 StackIter::source() const
311 return fp_->code()->module()->file()
312 ? fp_->code()->module()->file()->chars()