Changeset f229fc2


Ignore:
Timestamp:
Apr 13, 2018, 12:25:33 PM (4 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env, with_gc
Children:
b5aa3d8
Parents:
09a1ae6
Message:

Modify resolver to use young-generation collection per-top-level expression

Location:
src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/Common/GC.cc

    r09a1ae6 rf229fc2  
    2323#include <cassert>
    2424
    25 // #include <csignal>
    26 
    2725GC& GC::get() {
    2826        static GC gc;
     
    4745        if( obj )
    4846        {
    49                 bool isMarked = obj->mark == this->mark;
    50                 if( !isMarked ) {
     47                if( obj->mark != this->mark ) {
    5148                        obj->mark = this->mark;
    5249                        obj->trace( *this );
     
    5754
    5855void GC::register_object(GC_Object* obj) {
    59         // if ( obj == (GC_Object*)0x60f00000e410ul ) std::raise( SIGTRAP );
    6056        (using_young ? young : old).push_back(obj);
    61         obj->mark = ! this->mark;  // initialize as un-marked
     57        obj->mark = !this->mark;  // initialize as un-marked
    6258}
    6359
     
    6763
    6864void GC::new_generation() {
    69         using_young = true;
     65        assert(!using_young && "Cannot start new generation when young generation already in use");
     66       
     67        using_young = true;  // mark young generation as in-use
     68        mark = !mark;        // switch mark so aged young objects will still be unmarked in old
    7069}
    7170
     
    7877
    7978void GC::collect_young() {
    80         // check young generation, just reset mark if not using
    81         if ( ! using_young ) {
    82                 mark = !mark;
    83                 return;
    84         }
    85 
    8679        // collect young gen
    8780        for ( GC_Object*& obj : young ) {
     
    10093        young.clear();
    10194
    102         // reset mark for next collection
     95        // reset mark to return to old generation mark
    10396        mark = !mark;
    10497}
    10598
    10699void GC::collect() {
     100        // ensure not called when young gen is active
     101        assert(!using_young && "Cannot do old collection when young generation is active");
     102
    107103        // collect old gen
    108104        for ( GC_Object*& obj : old ) {
    109105                if ( obj->mark != mark ) {
    110                         // if ( obj == (GC_Object*)0x60f00000e410ul ) std::raise( SIGTRAP );
    111106                        delete obj;
    112107                        obj = nullptr;
     
    117112        old.erase( std::remove( old.begin(), old.end(), nullptr ), old.end() );
    118113
    119         // collect young gen (also resets mark)
    120         collect_young();
     114        // reset mark
     115        mark = !mark;
    121116}
    122117
  • src/Common/GC.h

    r09a1ae6 rf229fc2  
    4848        void collect_young();
    4949
    50         /// Collects all memory; use old generation afterward.
     50        /// Collects old generation; use old generation afterward.
     51        /// Error if currently using young generation
    5152        void collect();
    5253
  • src/ResolvExpr/Alternative.cc

    r09a1ae6 rf229fc2  
    2020#include <utility>                       // for move
    2121
     22#include "Common/GC.h"
     23#include "Common/PassVisitor.h"
    2224#include "Common/utility.h"              // for maybeClone
    2325#include "ResolvExpr/Cost.h"             // for Cost, Cost::zero, operator<<
    2426#include "ResolvExpr/TypeEnvironment.h"  // for TypeEnvironment
    2527#include "SynTree/Expression.h"          // for Expression
     28#include "SynTree/GcTracer.h"
    2629#include "SynTree/Type.h"                // for Type
    2730
     
    6467        }
    6568
     69        const GC& operator<< ( const GC& gc, const Alternative& alt ) {
     70                PassVisitor<GcTracer> tracer{ gc };
     71                maybeAccept( alt.expr, tracer );
     72                tracer << alt.env;
     73                return gc;
     74        }
     75
    6676} // namespace ResolvExpr
    6777
  • src/ResolvExpr/Alternative.h

    r09a1ae6 rf229fc2  
    2323
    2424class Expression;
     25
     26class GC;
    2527
    2628namespace ResolvExpr {
     
    5254                return os;
    5355        }
     56
     57        const GC& operator<< ( const GC&, const Alternative& );
    5458} // namespace ResolvExpr
    5559
  • src/ResolvExpr/Resolver.cc

    r09a1ae6 rf229fc2  
    2222#include "Alternative.h"                 // for Alternative, AltList
    2323#include "AlternativeFinder.h"           // for AlternativeFinder, resolveIn...
     24#include "Common/GC.h"                   // for new_generation, collect_young
    2425#include "Common/PassVisitor.h"          // for PassVisitor
    2526#include "Common/SemanticError.h"        // for SemanticError
     
    146147                void findUnfinishedKindExpression(Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, bool adjust = false, bool prune = true, bool failFast = true) {
    147148                        assertf( untyped, "expected a non-null expression." );
     149
     150                        new_generation();  // set up GC young generation for this top-level expression
     151
    148152                        TypeEnvironment env;
    149153                        AlternativeFinder finder( indexer, env );
     
    173177                        findMinCost( candidates.begin(), candidates.end(), back_inserter( winners ) );
    174178                        if ( winners.size() == 0 ) {
    175                                 SemanticError( untyped, toString( "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""), "expression: ") );
     179                                collect_young();
     180                                SemanticError( untyped, toString(
     181                                        "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""),
     182                                        "expression: ") );
    176183                        } else if ( winners.size() != 1 ) {
    177184                                std::ostringstream stream;
    178                                 stream << "Cannot choose between " << winners.size() << " alternatives for " << kindStr << (kindStr != "" ? " " : "") << "expression\n";
     185                                stream << "Cannot choose between " << winners.size() << " alternatives for "
     186                                        << kindStr << (kindStr != "" ? " " : "") << "expression\n";
    179187                                untyped->print( stream );
    180188                                stream << " Alternatives are:\n";
    181189                                printAlts( winners, stream, 1 );
     190                               
     191                                collect_young();
    182192                                SemanticError( untyped->location, stream.str() );
    183193                        }
     
    186196                        Alternative & choice = winners.front();
    187197                        if ( findDeletedExpr( choice.expr ) ) {
    188                                 SemanticError( choice.expr, "Unique best alternative includes deleted identifier in " );
     198                                collect_young( choice.expr );
     199                                SemanticError( choice.expr,
     200                                        "Unique best alternative includes deleted identifier in " );
    189201                        }
    190202                        alt = std::move( choice );
     203                        collect_young( alt );
    191204                }
    192205
  • src/ResolvExpr/TypeEnvironment.cc

    r09a1ae6 rf229fc2  
    1919#include <utility>                     // for pair
    2020
     21#include "Common/PassVisitor.h"        // for PassVisitor<GcTracer>
    2122#include "Common/utility.h"            // for maybeClone
     23#include "SynTree/GcTracer.h"          // for PassVisitor<GcTracer>
    2224#include "SynTree/Type.h"              // for Type, FunctionType, Type::Fora...
    2325#include "SynTree/TypeSubstitution.h"  // for TypeSubstitution
     
    222224                return out;
    223225        }
     226
     227        PassVisitor<GcTracer> & operator<<( PassVisitor<GcTracer> & gc, const TypeEnvironment & env ) {
     228                for ( const EqvClass & c : env ) {
     229                        maybeAccept( c.type, gc );
     230                }
     231                return gc;
     232        }
    224233} // namespace ResolvExpr
    225234
  • src/ResolvExpr/TypeEnvironment.h

    r09a1ae6 rf229fc2  
    2626#include "SynTree/Type.h"              // for Type, Type::ForallList
    2727#include "SynTree/TypeSubstitution.h"  // for TypeSubstitution
     28
     29template< typename Pass >
     30class PassVisitor;
     31class GcTracer;
    2832
    2933namespace ResolvExpr {
     
    116120
    117121        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
     122
     123        PassVisitor<GcTracer> & operator<<( PassVisitor<GcTracer> & gc, const TypeEnvironment & env );
    118124} // namespace ResolvExpr
    119125
Note: See TracChangeset for help on using the changeset viewer.