src/OldByCo.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 14:56:09 2012 -0800 (2012-11-18)
changeset 213 a9b8d24967d6
parent 195 c1ba166f3fc4
child 263 ba85a47ee414
permissions -rw-r--r--
Fix GCC retardedness.
     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 "../Functions.h"
    23 #include "AST.h"
    24 #include "BytecodeEmitter.h"
    25 
    26 namespace ke {
    27 
    28 class Module;
    29 class Array;
    30 class ArrayMap;
    31 
    32 struct ModuleData
    33 {
    34     ScopedRoot<Module> module;
    35     PoolList<ScopedRoot<Function> > publics;
    36     ScopedRoot<FixedArray> natives;
    37 };
    38 
    39 class BytecodeCompiler : public AstVisitor
    40 {
    41     Zone *zone_;
    42     ParseTree *tree_;
    43     BytecodeEmitter emitter_;
    44     ScopedRoot<FixedArray> locals_;
    45     Scope *scope_;
    46     ModuleData *moduleData_;
    47 
    48     class LoopScope
    49     {
    50         Scope *scope_;
    51         BytecodeEmitter::Label *bk_;
    52         BytecodeEmitter::Label *ct_;
    53         LoopScope **prevp_;
    54         LoopScope *prev_;
    55 
    56       public:
    57         LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
    58                   LoopScope **prevp)
    59           : scope_(scope),
    60             bk_(bk),
    61             ct_(ct),
    62             prevp_(prevp),
    63             prev_(*prevp)
    64         {
    65             *prevp_ = this;
    66         }
    67 
    68         ~LoopScope()
    69         {
    70             *prevp_ = prev_;
    71         }
    72 
    73         BytecodeEmitter::Label *break_() {
    74             return bk_;
    75         }
    76         BytecodeEmitter::Label *continue_() {
    77             return ct_;
    78         }
    79         Scope *scope() {
    80             return scope_;
    81         }
    82     };
    83 
    84     LoopScope *loop_;
    85 
    86   public:
    87     class AutoEnterScope
    88     {
    89         BytecodeCompiler *bc_;
    90         Scope *saved_;
    91 
    92       public:
    93         AutoEnterScope(BytecodeCompiler *bc, Scope *scope)
    94           : bc_(bc),
    95             saved_(bc->scope_)
    96         {
    97             if (scope)
    98                 bc->scope_ = scope;
    99         }
   100         ~AutoEnterScope() {
   101             bc_->scope_ = saved_;
   102         }
   103     };
   104 
   105   private:
   106     void recordLifoSlot(Scope *scope);
   107     void unwindTo(Scope *scope);
   108     void visitForTest(Expression *expression,
   109                       BytecodeEmitter::Label *trueBranch,
   110                       BytecodeEmitter::Label *falseBranch,
   111                       BytecodeEmitter::Label *fallthrough);
   112     Code *compileCode(Scope *scope, unsigned lineno);
   113     Function *compileFunction(ModuleData *module, FunctionStatement *fun);
   114     void visitForALU(Type *needed, Expression *node);
   115 
   116     void getEnvSlot(Variable *var);
   117     void setEnvSlot(Variable *var);
   118 
   119     bool tableswitch(SwitchStatement *node);
   120 
   121   public:
   122     BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module);
   123 
   124     Module *compile();
   125 
   126   public:
   127     void visit(NameProxy *name);
   128     void visit(Assignment *node);
   129     void visit(BinaryExpression *node);
   130     void visit(ReturnStatement *node);
   131     void visit(ForStatement *node);
   132     void visit(BlockStatement *node);
   133     void visit(VariableDeclaration *node);
   134     void visit(IntegerLiteral *node);
   135     void visit(BooleanLiteral *node);
   136     void visit(FloatLiteral *node);
   137     void visit(StringLiteral *node);
   138     void visit(ExpressionStatement *node);
   139     void visit(FunctionStatement *node);
   140     void visit(CallExpression *node);
   141     void visit(IfStatement *node);
   142     void visit(IndexExpression *node);
   143     void visit(EnumStatement *node);
   144     void visit(WhileStatement *node);
   145     void visit(BreakStatement *node);
   146     void visit(ContinueStatement *node);
   147     void visit(IncDecExpression *node);
   148     void visit(UnaryExpression *node);
   149     void visit(TernaryExpression *node);
   150     void visit(StructureStatement *node);
   151     void visit(FieldExpression *node);
   152     void visit(SwitchStatement *node);
   153     void visit(FunctionTypeStatement *node);
   154     void visit(ArrayLiteral *node);
   155     void visit(ImportStatement *node);
   156 };
   157 
   158 }
   159 
   160 #endif // _include_bytecode_generator_h_