src/BytecodeCompiler.h
author David Anderson <dvander@alliedmods.net>
Sat Sep 15 13:32:01 2012 -0700 (2012-09-15)
changeset 183 05571208642f
parent 174 e718b56aba78
child 187 9308d52205a0
permissions -rw-r--r--
Fix bug in error reporting for array assignment dimension mismatches.
     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,
   107                       BytecodeEmitter::Label *trueBranch,
   108                       BytecodeEmitter::Label *falseBranch,
   109                       BytecodeEmitter::Label *fallthrough);
   110     Code *compileCode(Scope *scope, unsigned lineno);
   111     Function *compileFunction(ModuleData *module, FunctionStatement *fun);
   112     void visitForALU(Type *needed, Expression *node);
   113 
   114     void getEnvSlot(Variable *var);
   115     void setEnvSlot(Variable *var);
   116 
   117     bool tableswitch(SwitchStatement *node);
   118 
   119   public:
   120     BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module);
   121 
   122     Module *compile();
   123 
   124   public:
   125     void visit(NameProxy *name);
   126     void visit(Assignment *node);
   127     void visit(BinaryExpression *node);
   128     void visit(ReturnStatement *node);
   129     void visit(ForStatement *node);
   130     void visit(BlockStatement *node);
   131     void visit(VariableDeclaration *node);
   132     void visit(IntegerLiteral *node);
   133     void visit(BooleanLiteral *node);
   134     void visit(FloatLiteral *node);
   135     void visit(StringLiteral *node);
   136     void visit(ExpressionStatement *node);
   137     void visit(FunctionStatement *node);
   138     void visit(CallExpression *node);
   139     void visit(IfStatement *node);
   140     void visit(IndexExpression *node);
   141     void visit(EnumStatement *node);
   142     void visit(WhileStatement *node);
   143     void visit(BreakStatement *node);
   144     void visit(ContinueStatement *node);
   145     void visit(IncDecExpression *node);
   146     void visit(UnaryExpression *node);
   147     void visit(TernaryExpression *node);
   148     void visit(StructureStatement *node);
   149     void visit(FieldExpression *node);
   150     void visit(SwitchStatement *node);
   151     void visit(FunctionTypeStatement *node);
   152 };
   153 
   154 }
   155 
   156 #endif // _include_bytecode_generator_h_