src/compiler/Scopes.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 17 18:41:01 2012 -0800 (2012-11-17)
changeset 195 c1ba166f3fc4
parent 145 src/Scopes.h@e9d804821c48
child 256 3c184218462d
permissions -rw-r--r--
Massive SemA/BC refactoring to support better type systems. Read on for more.

The previous compiler had a simple pipeline, best outlined as:
(1) Parsing and name binding -> AST
(2) Storage allocation -> AST
(3) Semantic Analysis (SemA) -> AST
(4) Bytecode Compilation (BC) -> Code

This pipeline, unfortunately, had two problems. First, name binding cannot be performed in one pass if we wish to have multi-file support in the form of modules. Name binding must be two-pass.

Second, because SemA was only capable of annotating AST nodes with a single type, most coercion work had to be duplicated in the BC phase, as coercion was not explicit in the SemA output. This made introducing new type rules extremely difficult, and all but precluded concepts like operator overloading (or overloading at all).

This patch rewrites most of Keima's backend. Of interest are the following changes:
(1) CompileContext has been refactored around future multi-file support.
(2) Parsing no longer performs any name binding.
(3) The grammar for type-and-name has been changed from:
(Label | Identifier)? Identifier
to:
Type? Identifier
where:
Type ::= OldType | NewType
OldType ::= Label
NewType ::= Identifier (. NewType)*


Although we do not implement the full production for Type yet, this
distinction is important. A type identifier is now affixed to the AST
as an Expression (right now, always a NameProxy), so it can fully
participate in name binding.

(4) Immediately after parsing, the AST goes through a NamePopulation phase.
This phase creates Scope objects for every scope which declares a name,
and if those names are declaring types or functions, a Symbol is created
and entered into the scope. Symbols entirely replace the old BoundName
classes.

(5) After NamePopulation comes NameBinding. This phase performs three steps:
(a) Links Scope objects together, to form a tree.
(b) Binds any free name to an existing name in the scope chain.
(c) Creates and registers any Symbols for names which have not yet been
added to the scope (for example, local variables).

In accordance, all "allocation" and "binding" concepts have been removed
from Scopes, which are now lightweight container classes.

(6) SemA now generates bytecode for each statement in the AST. Expression
compilation is performed via a separate mechanism called HIR (High-level
Intermediate Representation). To analyze an expression, SemA will walk
the AST, and produce a HIR object for each node. HIR is typed, and SemA
may insert coercion nodes, or even expand an AST node into multiple HIR
nodes. The result of evaluating an expression in SemA is therefore the
the root of a HIR tree, which SemA then sends to the HIRTranslator, which
performs bytecode generation.

As before, SemA still performs full semantic analysis. However, not it
also produces each function's bytecode.

This split allows us to decompose potentially complex semantics into
a more fine-grained, AST-like structure, which can have very simple
code-generation logic. For example, (1.0 + 5) might look like:

HAdd(HFloat(1.0), HConvert(HInteger(5), <Float>))

As part of this decomposition, many opcodes have been removed. Rather than
using typed jumps, we now expand jumps into longer tests. For example:
jge.f <label>
becomes:
ge.f
cvt.f2b
jt <label>

This simplifies the pipeline, and JITs should be able to melt the added
work away.

HIR is not used at a statement level, and HIR does not have any concept
of control-flow.

(7) The old BytecodeCompiler has been removed, as its work is now split
between SemA and HIRTranslator.


In addition, some minor refactorings have taken place:

(1) Opcodes.tbl no longer hardcodes numbers (aaah).
(2) Type has been split into smaller, typed structs.
(3) BytecodeEmitter no longer relies on Pools or RootScopes.
(4) SemA is now responsible for variable/storage allocation.
(5) Native table are now global, rather than per-module. As such, native
declarations now result in a Native object (which still requires a
CALLNATIVE opcode), which is an index into the global table. Natives
must be bound globally. This is in preparation for module support.
(6) Publics are no longer tracked, but rather registered via a global
callback upon module load. This callback can be set by embedders. This
is in preparation for multi-file support.
(7) The BytecodeEmitter now performs Symbol allocations itself, and it
also generates Code objects itself, which removes a good deal of
complexity.

Finally, a test harness has been added. This harness is a python script which finds *.test files, recursively, in the tests folder. For each file it runs the corresponding .sp. The contents of the .test file must match stdout+stderr.
dvander@0
     1
/*
dvander@0
     2
 * Copyright (C) 2012 David Anderson
dvander@0
     3
 *
dvander@0
     4
 * This file is part of JITCraft.
dvander@0
     5
 *
dvander@0
     6
 * JITCraft is free software: you can redistribute it and/or modify it under
dvander@0
     7
 * the terms of the GNU General Public License as published by the Free
dvander@0
     8
 * Software Foundation, either version 3 of the License, or (at your option)
dvander@0
     9
 * any later version.
dvander@0
    10
 * 
dvander@0
    11
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
dvander@0
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dvander@0
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
dvander@0
    14
 *
dvander@0
    15
 * You should have received a copy of the GNU General Public License along with
dvander@0
    16
 * JITCraft. If not, see http://www.gnu.org/licenses/.
dvander@0
    17
 */
dvander@0
    18
#ifndef _include_jitcraft_scopes_h_
dvander@0
    19
#define _include_jitcraft_scopes_h_
dvander@0
    20
dvander@195
    21
#include "../PoolAllocator.h"
dvander@195
    22
#include "../Structures.h"
dvander@0
    23
#include "AST.h"
dvander@0
    24
#include "NameBinding.h"
dvander@0
    25
dvander@59
    26
namespace ke {
dvander@0
    27
dvander@84
    28
class CompileContext;
dvander@85
    29
class EnvironmentMap;
dvander@195
    30
class Struct;
dvander@84
    31
dvander@0
    32
class Scope : public PoolObject
dvander@0
    33
{
dvander@195
    34
  protected:
dvander@0
    35
    PoolAllocator &pool_;
dvander@195
    36
    PoolList<Symbol *> names_;
dvander@58
    37
    PoolList<Scope *> children_;
dvander@1
    38
    
dvander@0
    39
    bool empty() const {
dvander@58
    40
        return names_.length() == 0;
dvander@0
    41
    }
dvander@0
    42
    void unlink();
dvander@0
    43
dvander@0
    44
  public:
dvander@0
    45
    enum Kind {
dvander@195
    46
        IMPORT,
dvander@0
    47
        GLOBAL,
dvander@0
    48
        FUNCTION,
dvander@0
    49
        BLOCK
dvander@0
    50
    };
dvander@0
    51
dvander@0
    52
  public:
dvander@0
    53
    Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
dvander@0
    54
    bool initialize();
dvander@0
    55
dvander@0
    56
    Kind kind() const {
dvander@0
    57
        return kind_;
dvander@0
    58
    }
dvander@0
    59
    Scope *enclosing() const {
dvander@0
    60
        return enclosing_;
dvander@0
    61
    }
dvander@195
    62
dvander@195
    63
    bool addSymbol(Symbol *symbol);
dvander@0
    64
dvander@0
    65
    Scope *unlinkIfEmpty();
dvander@195
    66
    Symbol *localLookup(Handle<String> name);
dvander@195
    67
    Symbol *lookup(Handle<String> name);
dvander@25
    68
dvander@85
    69
    EnvironmentMap *createEnvironmentMap(Zone *zone);
dvander@85
    70
dvander@195
    71
    void setParent(Scope *parent) {
dvander@195
    72
        assert(!enclosing_ || enclosing_ == parent);
dvander@195
    73
        enclosing_ = parent;
dvander@195
    74
    }
dvander@0
    75
dvander@0
    76
  private:
dvander@0
    77
    Kind kind_;
dvander@0
    78
    Scope *enclosing_;
dvander@0
    79
};
dvander@0
    80
dvander@195
    81
class ArgumentScope : public Scope
dvander@195
    82
{
dvander@195
    83
  public:
dvander@195
    84
    static ArgumentScope *New(PoolAllocator &pool);
dvander@195
    85
dvander@195
    86
  private:
dvander@195
    87
    ArgumentScope(PoolAllocator &pool);
dvander@195
    88
};
dvander@195
    89
dvander@195
    90
class LocalScope : public Scope
dvander@195
    91
{
dvander@195
    92
  public:
dvander@195
    93
    static LocalScope *New(PoolAllocator &pool);
dvander@195
    94
dvander@195
    95
  private:
dvander@195
    96
    LocalScope(PoolAllocator &pool);
dvander@195
    97
};
dvander@195
    98
dvander@195
    99
class GlobalScope : public Scope
dvander@195
   100
{
dvander@195
   101
  public:
dvander@195
   102
    static GlobalScope *New(PoolAllocator &pool, ImportScope *parent);
dvander@195
   103
dvander@195
   104
    bool addPublic(Symbol *symbol);
dvander@195
   105
    Symbol *findPublic(Handle<String> name);
dvander@195
   106
    Struct *build(Zone *zone);
dvander@195
   107
dvander@195
   108
  private:
dvander@195
   109
    GlobalScope(PoolAllocator &pool, ImportScope *parent);
dvander@195
   110
dvander@195
   111
  private:
dvander@195
   112
    PoolList<Symbol *> publics_;
dvander@195
   113
};
dvander@195
   114
dvander@195
   115
class ImportScope : public Scope
dvander@195
   116
{
dvander@195
   117
  public:
dvander@195
   118
    static ImportScope *New(PoolAllocator &pool);
dvander@195
   119
dvander@195
   120
  private:
dvander@195
   121
    ImportScope(PoolAllocator &pool);
dvander@195
   122
};
dvander@195
   123
dvander@0
   124
}
dvander@0
   125
dvander@0
   126
#endif // _include_jitcraft_scopes_h_