jitcraft/BytecodeCompiler.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 03 00:36:48 2012 -0700 (2012-07-03)
changeset 33 95baf120f7d4
parent 32 af1ed58252ac
child 59 b7d61ddb3c8d
permissions -rw-r--r--
Handle-ize BytecodeEmitter.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     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)
    10  * any later version.
    11  * 
    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.
    15  *
    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/.
    18  */
    19 #ifndef _include_bytecode_compiler_h_
    20 #define _include_bytecode_compiler_h_
    21 
    22 #include "AST.h"
    23 #include "BytecodeEmitter.h"
    24 #include "Functions.h"
    25 
    26 namespace jc {
    27 
    28 class Module;
    29 
    30 class BytecodeCompiler : public AstVisitor
    31 {
    32     Zone *zone_;
    33     ParseTree *tree_;
    34     BytecodeEmitter emitter_;
    35     ScopedRoot<Module> module_;
    36     ScopedRoot<FixedArray> locals_;
    37     Scope *scope_;
    38 
    39     class LoopScope
    40     {
    41         Scope *scope_;
    42         BytecodeEmitter::Label *bk_;
    43         BytecodeEmitter::Label *ct_;
    44         LoopScope **prevp_;
    45         LoopScope *prev_;
    46 
    47       public:
    48         LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
    49                   LoopScope **prevp)
    50           : scope_(scope),
    51             bk_(bk),
    52             ct_(ct),
    53             prevp_(prevp),
    54             prev_(*prevp)
    55         {
    56             *prevp_ = this;
    57         }
    58 
    59         ~LoopScope()
    60         {
    61             *prevp_ = prev_;
    62         }
    63 
    64         BytecodeEmitter::Label *break_() {
    65             return bk_;
    66         }
    67         BytecodeEmitter::Label *continue_() {
    68             return ct_;
    69         }
    70         Scope *scope() {
    71             return scope_;
    72         }
    73     };
    74 
    75     LoopScope *loop_;
    76 
    77   public:
    78     class AutoEnterScope
    79     {
    80         BytecodeCompiler *bc_;
    81         Scope *saved_;
    82 
    83       public:
    84         AutoEnterScope(BytecodeCompiler *bc, Scope *scope)
    85           : bc_(bc),
    86             saved_(bc->scope_)
    87         {
    88             if (scope)
    89                 bc->scope_ = scope;
    90         }
    91         ~AutoEnterScope() {
    92             bc_->scope_ = saved_;
    93         }
    94     };
    95 
    96   private:
    97     void recordLifoSlot(Scope *scope);
    98     void unwindTo(Scope *scope);
    99     void visitForTest(Expression *expression, BytecodeEmitter::Label *label, bool onTrue);
   100     Function *compileFunction(Module *module, FunctionStatement *fun);
   101     void visitForALU(Type *needed, Expression *node);
   102 
   103   public:
   104     BytecodeCompiler(Zone *zone, ParseTree *tree, Module *module);
   105 
   106     Module *compile();
   107 
   108   public:
   109     void visit(NameProxy *name);
   110     void visit(Assignment *node);
   111     void visit(BinaryExpression *node);
   112     void visit(ReturnStatement *node);
   113     void visit(ForStatement *node);
   114     void visit(BlockStatement *node);
   115     void visit(VariableDeclaration *node);
   116     void visit(IntegerLiteral *node);
   117     void visit(FloatLiteral *node);
   118     void visit(StringLiteral *node);
   119     void visit(ExpressionStatement *node);
   120     void visit(FunctionStatement *node);
   121     void visit(CallExpression *node);
   122     void visit(IfStatement *node);
   123     void visit(IndexExpression *node);
   124     void visit(EnumStatement *node);
   125     void visit(WhileStatement *node);
   126     void visit(BreakStatement *node);
   127     void visit(ContinueStatement *node);
   128     void visit(IncDecExpression *node);
   129 };
   130 
   131 }
   132 
   133 #endif // _include_bytecode_generator_h_