src/compiler/SemanticAnalysis.h
author David Anderson <dvander@alliedmods.net>
Sat Dec 22 18:22:14 2012 -0800 (2012-12-22)
changeset 230 fb3cd08bc7fb
parent 221 0c588a96837c
child 232 2b8512340180
permissions -rw-r--r--
Re-add LIFO stack optimizations.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of SourcePawn.
     5  *
     6  * SourcePawn is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_sp2_sema_h_
    19 #define _include_sp2_sema_h_
    20 
    21 #include "AST.h"
    22 #include "HIR.h"
    23 #include "Scopes.h"
    24 #include "BytecodeEmitter.h"
    25 
    26 namespace ke {
    27 
    28 // Semantic analysis occurs after all names and types have been bound. It
    29 // performs the important task of validating the semantics and type safety
    30 // of a parse tree.
    31 // 
    32 // The semantic analysis phase also produces bytecode. This occurs on a
    33 // statement-by-statement basis. The result of analyzing an expression is a
    34 // tree of operations which is then flattened.
    35 class SemanticAnalysis : public AstVisitor
    36 {
    37   public:
    38     SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit);
    39 
    40     void analyze();
    41 
    42   private:
    43     SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun);
    44     Function *compile(FunctionStatement *stmt);
    45 
    46   public:
    47     void visitFunctionStatement(FunctionStatement *node);
    48     void visitBlockStatement(BlockStatement *node);
    49     void visitExpressionStatement(ExpressionStatement *node);
    50     void visitReturnStatement(ReturnStatement *node);
    51 
    52     void visitCallExpression(CallExpression *node);
    53     void visitNameProxy(NameProxy *name);
    54     void visitIntegerLiteral(IntegerLiteral *node);
    55     void visitFloatLiteral(FloatLiteral *node);
    56     void visitVariableDeclaration(VariableDeclaration *node);
    57     void visitForStatement(ForStatement *node);
    58     void visitWhileStatement(WhileStatement *node);
    59     void visitAssignment(Assignment *node);
    60     void visitBinaryExpression(BinaryExpression *node);
    61     void visitBreakStatement(BreakStatement *node);
    62     void visitContinueStatement(ContinueStatement *node);
    63     void visitIfStatement(IfStatement *node);
    64     void visitIndexExpression(IndexExpression *node);
    65     void visitIncDecExpression(IncDecExpression *node);
    66 #if 0
    67     void visitFunctionTypeStatement(FunctionTypeStatement *node);
    68     void visitImportStatement(ImportStatement *node);
    69     void visitEnumStatement(EnumStatement *node);
    70     void visitStructureStatement(StructureStatement *node);
    71     void visitSwitchStatement(SwitchStatement *node);
    72 
    73     void visitStringLiteral(StringLiteral *node);
    74     void visitUnaryExpression(UnaryExpression *node);
    75     void visitTernaryExpression(TernaryExpression *node);
    76     void visitBooleanLiteral(BooleanLiteral *node);
    77     void visitFieldExpression(FieldExpression *node);
    78     void visitArrayLiteral(ArrayLiteral *node);
    79 #endif
    80 
    81   private:
    82     bool checkArgumentCount(Handle<FunctionType> type, unsigned actual);
    83 
    84   private:
    85     enum CoercionKind {
    86         Coerce_Arg,
    87         Coerce_Return,
    88         Coerce_Assign
    89     };
    90 
    91     void requireLifoStack();
    92     void visitLoopBody(Statement *stmt);
    93     void visitForTest(Expression *expr,
    94                       BytecodeEmitter::Label *trueBranch,
    95                       BytecodeEmitter::Label *falseBranch,
    96                       BytecodeEmitter::Label *fallthrough);
    97     void set(VariableSymbol *sym);
    98     HIR *eval(Expression *expr);
    99     HIR *binary(AstNode *node, TokenKind token, HIR *left, HIR *right);
   100     HIR *coerce(HIR *from, Handle<Type> to, CoercionKind kind);
   101     Type *coercionType(TokenKind token, HIR *left, HIR *right);
   102     HLValue *lvalue(Expression *expr);
   103     void discharge(HIR *hir);
   104 
   105     bool typeCheckArray(const SourcePosition &pos,
   106                         Handle<Type> argGiven,
   107                         Handle<Type> argActual,
   108                         CoercionKind kind);
   109 
   110   private:
   111     // Used to target break/catch statements.
   112     class LoopScope
   113     {
   114         Scope *scope_;
   115         BytecodeEmitter::Label *bk_;
   116         BytecodeEmitter::Label *ct_;
   117         LoopScope **prevp_;
   118         LoopScope *prev_;
   119         bool usesLifoStack_;
   120 
   121       public:
   122         LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
   123                   LoopScope **prevp)
   124           : scope_(scope),
   125             bk_(bk),
   126             ct_(ct),
   127             prevp_(prevp),
   128             prev_(*prevp),
   129             usesLifoStack_(false)
   130         {
   131             *prevp_ = this;
   132         }
   133 
   134         ~LoopScope()
   135         {
   136             *prevp_ = prev_;
   137         }
   138 
   139         BytecodeEmitter::Label *break_() {
   140             return bk_;
   141         }
   142         BytecodeEmitter::Label *continue_() {
   143             return ct_;
   144         }
   145         Scope *scope() {
   146             return scope_;
   147         }
   148         void setUsesLifoStack() {
   149             usesLifoStack_ = true;
   150         }
   151         bool usesLifoStack() const {
   152             return usesLifoStack_;
   153         }
   154     };
   155     
   156   private:
   157     Zone *zone_;
   158     PoolAllocator &pool_;
   159     CompileContext &cc_;
   160     TranslationUnit *unit_;
   161     HIR *hir_;
   162     Scope *scope_;
   163     BytecodeEmitter emitter_;
   164     ScopedRoot<Module> module_;
   165     ScopedRoot<FunctionType> fun_;
   166     LoopScope *loop_;
   167 };
   168 
   169 } // namespace ke
   170 
   171 #endif // _include_sp2_sema_h_