1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/BytecodeCompiler.h Tue Jul 24 23:53:34 2012 -0700
1.3 @@ -0,0 +1,149 @@
1.4 +/* vim: set ts=4 sw=4 tw=99 et:
1.5 + *
1.6 + * Copyright (C) 2012 David Anderson
1.7 + *
1.8 + * This file is part of JITCraft.
1.9 + *
1.10 + * JITCraft is free software: you can redistribute it and/or modify it under
1.11 + * the terms of the GNU General Public License as published by the Free
1.12 + * Software Foundation, either version 3 of the License, or (at your option)
1.13 + * any later version.
1.14 + *
1.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License along with
1.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
1.21 + */
1.22 +#ifndef _include_bytecode_compiler_h_
1.23 +#define _include_bytecode_compiler_h_
1.24 +
1.25 +#include "AST.h"
1.26 +#include "BytecodeEmitter.h"
1.27 +#include "Functions.h"
1.28 +
1.29 +namespace ke {
1.30 +
1.31 +class Module;
1.32 +
1.33 +struct ModuleData
1.34 +{
1.35 + ScopedRoot<Module> module;
1.36 + PoolList<ScopedRoot<Function> > publics;
1.37 + ScopedRoot<FixedArray> natives;
1.38 +};
1.39 +
1.40 +class BytecodeCompiler : public AstVisitor
1.41 +{
1.42 + Zone *zone_;
1.43 + ParseTree *tree_;
1.44 + BytecodeEmitter emitter_;
1.45 + ScopedRoot<FixedArray> locals_;
1.46 + Scope *scope_;
1.47 + ModuleData *moduleData_;
1.48 +
1.49 + class LoopScope
1.50 + {
1.51 + Scope *scope_;
1.52 + BytecodeEmitter::Label *bk_;
1.53 + BytecodeEmitter::Label *ct_;
1.54 + LoopScope **prevp_;
1.55 + LoopScope *prev_;
1.56 +
1.57 + public:
1.58 + LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
1.59 + LoopScope **prevp)
1.60 + : scope_(scope),
1.61 + bk_(bk),
1.62 + ct_(ct),
1.63 + prevp_(prevp),
1.64 + prev_(*prevp)
1.65 + {
1.66 + *prevp_ = this;
1.67 + }
1.68 +
1.69 + ~LoopScope()
1.70 + {
1.71 + *prevp_ = prev_;
1.72 + }
1.73 +
1.74 + BytecodeEmitter::Label *break_() {
1.75 + return bk_;
1.76 + }
1.77 + BytecodeEmitter::Label *continue_() {
1.78 + return ct_;
1.79 + }
1.80 + Scope *scope() {
1.81 + return scope_;
1.82 + }
1.83 + };
1.84 +
1.85 + LoopScope *loop_;
1.86 +
1.87 + public:
1.88 + class AutoEnterScope
1.89 + {
1.90 + BytecodeCompiler *bc_;
1.91 + Scope *saved_;
1.92 +
1.93 + public:
1.94 + AutoEnterScope(BytecodeCompiler *bc, Scope *scope)
1.95 + : bc_(bc),
1.96 + saved_(bc->scope_)
1.97 + {
1.98 + if (scope)
1.99 + bc->scope_ = scope;
1.100 + }
1.101 + ~AutoEnterScope() {
1.102 + bc_->scope_ = saved_;
1.103 + }
1.104 + };
1.105 +
1.106 + private:
1.107 + void recordLifoSlot(Scope *scope);
1.108 + void unwindTo(Scope *scope);
1.109 + void visitForTest(Expression *expression,
1.110 + BytecodeEmitter::Label *trueBranch,
1.111 + BytecodeEmitter::Label *falseBranch,
1.112 + BytecodeEmitter::Label *fallthrough);
1.113 + Code *compileCode(Scope *scope, unsigned lineno);
1.114 + Function *compileFunction(ModuleData *module, FunctionStatement *fun);
1.115 + void visitForALU(Type *needed, Expression *node);
1.116 +
1.117 + void getEnvSlot(Variable *var);
1.118 + void setEnvSlot(Variable *var);
1.119 +
1.120 + public:
1.121 + BytecodeCompiler(Zone *zone, ParseTree *tree, ModuleData *module);
1.122 +
1.123 + Module *compile();
1.124 +
1.125 + public:
1.126 + void visit(NameProxy *name);
1.127 + void visit(Assignment *node);
1.128 + void visit(BinaryExpression *node);
1.129 + void visit(ReturnStatement *node);
1.130 + void visit(ForStatement *node);
1.131 + void visit(BlockStatement *node);
1.132 + void visit(VariableDeclaration *node);
1.133 + void visit(IntegerLiteral *node);
1.134 + void visit(FloatLiteral *node);
1.135 + void visit(StringLiteral *node);
1.136 + void visit(ExpressionStatement *node);
1.137 + void visit(FunctionStatement *node);
1.138 + void visit(CallExpression *node);
1.139 + void visit(IfStatement *node);
1.140 + void visit(IndexExpression *node);
1.141 + void visit(EnumStatement *node);
1.142 + void visit(WhileStatement *node);
1.143 + void visit(BreakStatement *node);
1.144 + void visit(ContinueStatement *node);
1.145 + void visit(IncDecExpression *node);
1.146 + void visit(UnaryExpression *node);
1.147 + void visit(TernaryExpression *node);
1.148 +};
1.149 +
1.150 +}
1.151 +
1.152 +#endif // _include_bytecode_generator_h_