src/RootScopes.cpp
changeset 86 32ad0523b309
parent 69 dfce0539a77d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/RootScopes.cpp	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* vim: set ts=4 sw=4 tw=99 et:
     1.5 + *
     1.6 + * Copyright (C) 2012 David Anderson
     1.7 + *
     1.8 + * This file is part of JITCraft.
     1.9 + *
    1.10 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.11 + * the terms of the GNU General Public License as published by the Free
    1.12 + * Software Foundation, either version 3 of the License, or (at your option)
    1.13 + * any later version.
    1.14 + * 
    1.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License along with
    1.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.21 + */
    1.22 +#include <stdio.h>
    1.23 +#include <stdlib.h>
    1.24 +#include "RootScopes.h"
    1.25 +
    1.26 +using namespace ke;
    1.27 +
    1.28 +RootScopeManager::RootScopeManager()
    1.29 +  : blocks_(&first_),
    1.30 +    first_(NULL),
    1.31 +    scope_(NULL)
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +RootScopeManager::~RootScopeManager()
    1.36 +{
    1.37 +    while (blocks_ != &first_) {
    1.38 +        Block *temp = blocks_;
    1.39 +        delete blocks_;
    1.40 +        blocks_ = temp;
    1.41 +    }
    1.42 +}
    1.43 +
    1.44 +bool
    1.45 +RootScopeManager::grow()
    1.46 +{
    1.47 +    assert(blocks_->cur == blocks_->end);
    1.48 +
    1.49 +    Block *block = new Block(blocks_);
    1.50 +    if (!block) {
    1.51 +        // We are not prepared for this. We'll start dishing out NULL-valued
    1.52 +        // ScopedRoots, and the engine generally assumes infallbility. Rather
    1.53 +        // then NULL check all sorts of nodes on parse trees, or add boolean
    1.54 +        // setters, for now we just abort.
    1.55 +        fprintf(stderr, "Unrecoverable error: could not allocate a local root scope. Aborting!\n");
    1.56 +        abort();
    1.57 +        return false;
    1.58 +    }
    1.59 +
    1.60 +    blocks_ = block;
    1.61 +    return true;
    1.62 +}
    1.63 +
    1.64 +void
    1.65 +RootScopeManager::unwindSlow(Object **pos)
    1.66 +{
    1.67 +    for (;;) {
    1.68 +        if (pos >= blocks_->objects && pos <= blocks_->end)
    1.69 +            break;
    1.70 +        assert(blocks_ != &first_);
    1.71 +        Block *temp = blocks_;
    1.72 +        delete blocks_;
    1.73 +        blocks_ = temp;
    1.74 +    }
    1.75 +
    1.76 +    blocks_->cur = pos;
    1.77 +}
    1.78 +
    1.79 +void
    1.80 +RootScopeManager::accept(VirtualObjectVisitor *visitor)
    1.81 +{
    1.82 +    Block *block = blocks_;
    1.83 +    while (block) {
    1.84 +        for (Object **ptr = block->objects; ptr < block->cur; ptr++)
    1.85 +            visitor->visit(ptr);
    1.86 +        block = block->prev;
    1.87 +    }
    1.88 +}