jitcraft/BytecodeCompiler.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 17 00:38:18 2012 -0700 (2012-07-17)
changeset 75 662e81d9ed1c
parent 68 b40b0e18a918
child 76 c4c4fe5d1a72
permissions -rw-r--r--
Added support for && and ||.
     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 #if 0
    38 class AstContext
    39 {
    40   public:
    41     enum Kind {
    42         Value,
    43         Test
    44     };
    45 
    46     AstContext(Kind kind)
    47       : kind_(kind)
    48     {
    49     }
    50 
    51     Kind kind() const {
    52         return kind_;
    53     }
    54 
    55   private:
    56     Kind kind_;
    57 };
    58 
    59 class ValueContext
    60 {
    61   public:
    62 
    63 };
    64 #endif
    65 
    66 class BytecodeCompiler : public AstVisitor
    67 {
    68     Zone *zone_;
    69     ParseTree *tree_;
    70     BytecodeEmitter emitter_;
    71     ScopedRoot<FixedArray> locals_;
    72     Scope *scope_;
    73     ModuleData *moduleData_;
    74 
    75     class LoopScope
    76     {
    77         Scope *scope_;
    78         BytecodeEmitter::Label *bk_;
    79         BytecodeEmitter::Label *ct_;
    80         LoopScope **prevp_;
    81         LoopScope *prev_;
    82 
    83       public:
    84         LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
    85                   LoopScope **prevp)
    86           : scope_(scope),
    87             bk_(bk),
    88             ct_(ct),
    89             prevp_(prevp),
    90             prev_(*prevp)
    91         {
    92             *prevp_ = this;
    93         }
    94 
    95         ~LoopScope()
    96         {
    97             *prevp_ = prev_;
    98         }
    99 
   100         BytecodeEmitter::Label *break_() {
   101             return bk_;
   102         }
   103         BytecodeEmitter::Label *continue_() {
   104             return ct_;
   105         }
   106         Scope *scope() {
   107             return scope_;
   108         }
   109     };
   110 
   111     LoopScope *loop_;
   112 
   113   public:
   114     class AutoEnterScope
   115     {
   116         BytecodeCompiler *bc_;
   117         Scope *saved_;
   118 
   119       public:
   120         AutoEnterScope(BytecodeCompiler *bc, Scope *scope)
   121           : bc_(bc),
   122             saved_(bc->scope_)
   123         {
   124             if (scope)
   125                 bc->scope_ = scope;
   126         }
   127         ~AutoEnterScope() {
   128             bc_->scope_ = saved_;
   129         }
   130     };
   131 
   132   private:
   133     void recordLifoSlot(Scope *scope);
   134     void unwindTo(Scope *scope);
   135     void visitForTest(Expression *expression,
   136                       BytecodeEmitter::Label *trueBranch,
   137                       BytecodeEmitter::Label *falseBranch,
   138                       BytecodeEmitter::Label *fallthrough);
   139     Function *compileFunction(ModuleData *module, FunctionStatement *fun);
   140     void visitForALU(Type *needed, Expression *node);
   141 
   142   public:
   143     BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module);
   144 
   145     Module *compile();
   146 
   147   public:
   148     void visit(NameProxy *name);
   149     void visit(Assignment *node);
   150     void visit(BinaryExpression *node);
   151     void visit(ReturnStatement *node);
   152     void visit(ForStatement *node);
   153     void visit(BlockStatement *node);
   154     void visit(VariableDeclaration *node);
   155     void visit(IntegerLiteral *node);
   156     void visit(FloatLiteral *node);
   157     void visit(StringLiteral *node);
   158     void visit(ExpressionStatement *node);
   159     void visit(FunctionStatement *node);
   160     void visit(CallExpression *node);
   161     void visit(IfStatement *node);
   162     void visit(IndexExpression *node);
   163     void visit(EnumStatement *node);
   164     void visit(WhileStatement *node);
   165     void visit(BreakStatement *node);
   166     void visit(ContinueStatement *node);
   167     void visit(IncDecExpression *node);
   168 };
   169 
   170 }
   171 
   172 #endif // _include_bytecode_generator_h_