jitcraft/BytecodeCompiler.h
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 22:31:14 2012 -0700 (2012-06-30)
changeset 14 0784ac5cc347
parent 12 75e385f68b7e
child 21 af5d8e985502
permissions -rw-r--r--
Added support for non-array references.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_bytecode_compiler_h_
    19 #define _include_bytecode_compiler_h_
    20 
    21 #include "AST.h"
    22 #include "BytecodeEmitter.h"
    23 #include "Code.h"
    24 
    25 namespace jc {
    26 
    27 class Module;
    28 
    29 class BytecodeCompiler : public AstVisitor
    30 {
    31     ParseTree *tree_;
    32     BytecodeEmitter emitter_;
    33     Module *module_;
    34     FixedArray *locals_;
    35     Scope *scope_;
    36 
    37     class LoopScope
    38     {
    39         Scope *scope_;
    40         BytecodeEmitter::Label *bk_;
    41         BytecodeEmitter::Label *ct_;
    42         LoopScope **prevp_;
    43         LoopScope *prev_;
    44 
    45       public:
    46         LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
    47                   LoopScope **prevp)
    48           : scope_(scope),
    49             bk_(bk),
    50             ct_(ct),
    51             prevp_(prevp),
    52             prev_(*prevp)
    53         {
    54             *prevp_ = this;
    55         }
    56 
    57         ~LoopScope()
    58         {
    59             *prevp_ = prev_;
    60         }
    61 
    62         BytecodeEmitter::Label *break_() {
    63             return bk_;
    64         }
    65         BytecodeEmitter::Label *continue_() {
    66             return ct_;
    67         }
    68         Scope *scope() {
    69             return scope_;
    70         }
    71     };
    72 
    73     LoopScope *loop_;
    74 
    75   public:
    76     class AutoEnterScope
    77     {
    78         BytecodeCompiler *bc_;
    79         Scope *saved_;
    80 
    81       public:
    82         AutoEnterScope(BytecodeCompiler *bc, Scope *scope)
    83           : bc_(bc),
    84             saved_(bc->scope_)
    85         {
    86             if (scope)
    87                 bc->scope_ = scope;
    88         }
    89         ~AutoEnterScope() {
    90             bc_->scope_ = saved_;
    91         }
    92     };
    93 
    94   private:
    95     void recordLifoSlot(Scope *scope);
    96     void unwindTo(Scope *scope);
    97     void visitForTest(Expression *expression, BytecodeEmitter::Label *label, bool onTrue);
    98     Code *compileFunction(Module *module, FunctionStatement *fun);
    99     void visitForALU(Type *needed, Expression *node);
   100 
   101   public:
   102     BytecodeCompiler(ParseTree *tree, Module *module);
   103 
   104     Module *compile();
   105 
   106   public:
   107     void visit(NameProxy *name);
   108     void visit(Assignment *node);
   109     void visit(BinaryExpression *node);
   110     void visit(ReturnStatement *node);
   111     void visit(ForStatement *node);
   112     void visit(BlockStatement *node);
   113     void visit(VariableDeclaration *node);
   114     void visit(IntegerLiteral *node);
   115     void visit(FloatLiteral *node);
   116     void visit(ExpressionStatement *node);
   117     void visit(FunctionStatement *node);
   118     void visit(CallExpression *node);
   119     void visit(IfStatement *node);
   120     void visit(IndexExpression *node);
   121     void visit(EnumStatement *node);
   122     void visit(WhileStatement *node);
   123     void visit(BreakStatement *node);
   124     void visit(ContinueStatement *node);
   125 };
   126 
   127 }
   128 
   129 #endif // _include_bytecode_generator_h_