/* vim: set ts=4 sw=4 tw=99 et:
 *
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of JITCraft.
 *
 * JITCraft is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * JITCraft. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_bytecode_compiler_h_
#define _include_bytecode_compiler_h_

#include "../Functions.h"
#include "AST.h"
#include "BytecodeEmitter.h"

namespace ke {

class Module;
class Array;
class ArrayMap;

struct ModuleData
{
    ScopedRoot<Module> module;
    PoolList<ScopedRoot<Function> > publics;
    ScopedRoot<FixedArray> natives;
};

class BytecodeCompiler : public AstVisitor
{
    Zone *zone_;
    ParseTree *tree_;
    BytecodeEmitter emitter_;
    ScopedRoot<FixedArray> locals_;
    Scope *scope_;
    ModuleData *moduleData_;

    class LoopScope
    {
        Scope *scope_;
        BytecodeEmitter::Label *bk_;
        BytecodeEmitter::Label *ct_;
        LoopScope **prevp_;
        LoopScope *prev_;

      public:
        LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
                  LoopScope **prevp)
          : scope_(scope),
            bk_(bk),
            ct_(ct),
            prevp_(prevp),
            prev_(*prevp)
        {
            *prevp_ = this;
        }

        ~LoopScope()
        {
            *prevp_ = prev_;
        }

        BytecodeEmitter::Label *break_() {
            return bk_;
        }
        BytecodeEmitter::Label *continue_() {
            return ct_;
        }
        Scope *scope() {
            return scope_;
        }
    };

    LoopScope *loop_;

  public:
    class AutoEnterScope
    {
        BytecodeCompiler *bc_;
        Scope *saved_;

      public:
        AutoEnterScope(BytecodeCompiler *bc, Scope *scope)
          : bc_(bc),
            saved_(bc->scope_)
        {
            if (scope)
                bc->scope_ = scope;
        }
        ~AutoEnterScope() {
            bc_->scope_ = saved_;
        }
    };

  private:
    void recordLifoSlot(Scope *scope);
    void unwindTo(Scope *scope);
    void visitForTest(Expression *expression,
                      BytecodeEmitter::Label *trueBranch,
                      BytecodeEmitter::Label *falseBranch,
                      BytecodeEmitter::Label *fallthrough);
    Code *compileCode(Scope *scope, unsigned lineno);
    Function *compileFunction(ModuleData *module, FunctionStatement *fun);
    void visitForALU(Type *needed, Expression *node);

    void getEnvSlot(Variable *var);
    void setEnvSlot(Variable *var);

    bool tableswitch(SwitchStatement *node);

  public:
    BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module);

    Module *compile();

  public:
    void visit(NameProxy *name);
    void visit(Assignment *node);
    void visit(BinaryExpression *node);
    void visit(ReturnStatement *node);
    void visit(ForStatement *node);
    void visit(BlockStatement *node);
    void visit(VariableDeclaration *node);
    void visit(IntegerLiteral *node);
    void visit(BooleanLiteral *node);
    void visit(FloatLiteral *node);
    void visit(StringLiteral *node);
    void visit(ExpressionStatement *node);
    void visit(FunctionStatement *node);
    void visit(CallExpression *node);
    void visit(IfStatement *node);
    void visit(IndexExpression *node);
    void visit(EnumStatement *node);
    void visit(WhileStatement *node);
    void visit(BreakStatement *node);
    void visit(ContinueStatement *node);
    void visit(IncDecExpression *node);
    void visit(UnaryExpression *node);
    void visit(TernaryExpression *node);
    void visit(StructureStatement *node);
    void visit(FieldExpression *node);
    void visit(SwitchStatement *node);
    void visit(FunctionTypeStatement *node);
    void visit(ArrayLiteral *node);
    void visit(ImportStatement *node);
};

}

#endif // _include_bytecode_generator_h_
