jitcraft/BytecodeCompiler.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 10 02:11:13 2012 -0700 (2012-07-10)
changeset 68 b40b0e18a918
parent 65 7c942b5adebc
child 75 662e81d9ed1c
permissions -rw-r--r--
Added support for ISourcePawnEngine natives.
     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 ke {
    27 
    28 class Module;
    29 
    30 struct ModuleData
    31 {
    32     ScopedRoot<Module> module;
    33     PoolList<ScopedRoot<Function> > publics;
    34     ScopedRoot<FixedArray> natives;
    35 };
    36 
    37 class BytecodeCompiler : public AstVisitor
    38 {
    39     Zone *zone_;
    40     ParseTree *tree_;
    41     BytecodeEmitter emitter_;
    42     ScopedRoot<FixedArray> locals_;
    43     Scope *scope_;
    44     ModuleData *moduleData_;
    45 
    46     class LoopScope
    47     {
    48         Scope *scope_;
    49         BytecodeEmitter::Label *bk_;
    50         BytecodeEmitter::Label *ct_;
    51         LoopScope **prevp_;
    52         LoopScope *prev_;
    53 
    54       public:
    55         LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
    56                   LoopScope **prevp)
    57           : scope_(scope),
    58             bk_(bk),
    59             ct_(ct),
    60             prevp_(prevp),
    61             prev_(*prevp)
    62         {
    63             *prevp_ = this;
    64         }
    65 
    66         ~LoopScope()
    67         {
    68             *prevp_ = prev_;
    69         }
    70 
    71         BytecodeEmitter::Label *break_() {
    72             return bk_;
    73         }
    74         BytecodeEmitter::Label *continue_() {
    75             return ct_;
    76         }
    77         Scope *scope() {
    78             return scope_;
    79         }
    80     };
    81 
    82     LoopScope *loop_;
    83 
    84   public:
    85     class AutoEnterScope
    86     {
    87         BytecodeCompiler *bc_;
    88         Scope *saved_;
    89 
    90       public:
    91         AutoEnterScope(BytecodeCompiler *bc, Scope *scope)
    92           : bc_(bc),
    93             saved_(bc->scope_)
    94         {
    95             if (scope)
    96                 bc->scope_ = scope;
    97         }
    98         ~AutoEnterScope() {
    99             bc_->scope_ = saved_;
   100         }
   101     };
   102 
   103   private:
   104     void recordLifoSlot(Scope *scope);
   105     void unwindTo(Scope *scope);
   106     void visitForTest(Expression *expression, BytecodeEmitter::Label *label, bool onTrue);
   107     Function *compileFunction(ModuleData *module, FunctionStatement *fun);
   108     void visitForALU(Type *needed, Expression *node);
   109 
   110   public:
   111     BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module);
   112 
   113     Module *compile();
   114 
   115   public:
   116     void visit(NameProxy *name);
   117     void visit(Assignment *node);
   118     void visit(BinaryExpression *node);
   119     void visit(ReturnStatement *node);
   120     void visit(ForStatement *node);
   121     void visit(BlockStatement *node);
   122     void visit(VariableDeclaration *node);
   123     void visit(IntegerLiteral *node);
   124     void visit(FloatLiteral *node);
   125     void visit(StringLiteral *node);
   126     void visit(ExpressionStatement *node);
   127     void visit(FunctionStatement *node);
   128     void visit(CallExpression *node);
   129     void visit(IfStatement *node);
   130     void visit(IndexExpression *node);
   131     void visit(EnumStatement *node);
   132     void visit(WhileStatement *node);
   133     void visit(BreakStatement *node);
   134     void visit(ContinueStatement *node);
   135     void visit(IncDecExpression *node);
   136 };
   137 
   138 }
   139 
   140 #endif // _include_bytecode_generator_h_