Ejemplo n.º 1
0
 public override bool Replace(Node oldValue, Node newValue)
 {
   return 
     Replace(Statement, oldValue, newValue, n => Statement = n)
     ||
     base.Replace(oldValue, newValue);
 }
Ejemplo n.º 2
0
    public CaseClause(Expression expression, BlockStatement body)
    {
      Expression = expression;
      Body = body;
      SourceOffset = body.SourceOffset;

      Use(Expression);
    }
Ejemplo n.º 3
0
    public FunctionExpression(Scope scope, ReadIdentifierExpression name, List<ReadIdentifierExpression> parameters, BlockStatement statement)
    {
      Scope = scope;
      Name = name;
      Parameters = parameters;
      Statement = statement;
      SourceOffset = statement.SourceOffset;

      Use(Statement);
    }
Ejemplo n.º 4
0
 public override bool Replace(Node oldValue, Node newValue)
 {
   return
     Replace(Initialization, oldValue, newValue, n => Initialization = n)
     ||
     Replace(Expression, oldValue, newValue, n => Expression = n)
     ||
     Replace(ExtendedBody, oldValue, newValue, n => ExtendedBody= n)
     ||
     Replace(IteratorInitialization, oldValue, newValue, n => IteratorInitialization = n)
     ||
     base.Replace(oldValue, newValue);
 }
Ejemplo n.º 5
0
    public InlinedInvocation(
      JSFunctionMetadata targetFunctionMetadata
      , Scope scope
      , BlockStatement statement
      , ReadIdentifierExpression returnValue)
      : base(returnValue)
    {
      TargetFunctionMetadata = targetFunctionMetadata;
      Scope = scope;
      Statement = statement;

      Use(Statement);
    }
Ejemplo n.º 6
0
      public override void Visit(BlockStatement node)
      {
        var prevCurrBlock = _currBlock;
        var prevCurrStatement = _currStatementIndex;

        _currBlock = node;
        for (_currStatementIndex = node.Statements.Count - 1; _currStatementIndex >= 0; --_currStatementIndex)
        {
          VisitNode(node.Statements[_currStatementIndex]);
        }

        _currStatementIndex = prevCurrStatement;
        _currBlock = prevCurrBlock;
      }
Ejemplo n.º 7
0
    public ForEachInStatement(
      Statement initialization, 
      ToObject expression, 
      Statement body, 
      Statement iteratorInitialization,
      ToBoolean iteratorCondition,
      BlockStatement extendedBody
    )
      : base(iteratorCondition, extendedBody)
    {
      Initialization = initialization;
      Expression = expression;
      OriginalBody = body;

      IteratorInitialization = iteratorInitialization;
      ExtendedBody = extendedBody;

      //Use(Expression); //We don't need this since IteratorInitialization must be already using it!
      Debug.Assert(Expression.HasUser, "{0} must already have a user", Expression);
    }
Ejemplo n.º 8
0
      public void Execute(JSFunctionMetadata funcMetadata)
      {
        if (funcMetadata.IsAnalyzed)
          return;

        if (!funcMetadata.Scope.IsEvalFunction)
          return; //This is not the body in an eval()
        _currFuncMetadata = funcMetadata;
        _implicitReturn = _currFuncMetadata.Scope.GetOrAddSymbol("_implicit_eval_return");
        _implicitReturn.SymbolType = JSSymbol.SymbolTypes.HiddenLocal;

        _currBlock = null;
        _currStatementIndex = -1;
        _currExpressionStatement = null;

        VisitNode(_currFuncMetadata.FunctionIR.Statement);

        _currFuncMetadata.FunctionIR.Statement.Statements.Add(
            new ReturnStatement(
                new ReadIdentifierExpression(_implicitReturn)
            )
        );
      }
Ejemplo n.º 9
0
      //      void AsPrimitive()  //AsDValue()
      //      {
      //        //var type = _resultType;
      //        //if (type == mdr.ValueTypes.DValue)
      //        //  return;

      //        //_ilGen.Call(Types.DValue.Create(type));
      //        //_resultType = mdr.ValueTypes.DValue;
      //        throw new NotImplementedException();
      //      }

      //      void AsBoolean()
      //      {
      //        var type = _result.ValueType;
      //        if (type == mdr.ValueTypes.Boolean)
      //          return;

      //        switch (type)
      //        {
      //          //case mdr.ValueTypes.Undefined:
      //          case mdr.ValueTypes.String:
      //            _ilGen.Call(Types.Operations.Convert.ToBoolean.Get(mdr.ValueTypes.String));
      //            break;
      //          case mdr.ValueTypes.Double:
      //            _ilGen.Conv_I4();
      //            break;
      //          case mdr.ValueTypes.Int32:
      //            break;
      //          case mdr.ValueTypes.Boolean:
      //            break;
      //          //case mdr.ValueTypes.Null:
      //          case mdr.ValueTypes.Object:
      //          case mdr.ValueTypes.Function:
      //          case mdr.ValueTypes.Array:
      //          case mdr.ValueTypes.Property:
      //            _ilGen.Callvirt(Types.DObject.ToBoolean);
      //            break;
      //          case mdr.ValueTypes.DValueRef:
      //            _ilGen.Call(Types.DValue.AsBoolean);
      //            break;
      //          default:
      //            Trace.Fail("Cannot convert type {0} to boolean", type);
      //            break;
      //        }
      //#if DEBUG
      //        //This is to help the CSharpILGenerator produce valid C# code
      //        if (type == mdr.ValueTypes.Double || type == mdr.ValueTypes.Int32)
      //        {
      //          _ilGen.Ldc_I4_0();
      //          _ilGen.Ceq();
      //          _ilGen.Ldc_I4(false);
      //          _ilGen.Ceq();
      //        }
      //#endif
      //        _result.ValueType = mdr.ValueTypes.Boolean;
      //      }
      //      protected override void AsBoolean()
      //      {
      //        LoadStackPop();
      //        _result.ValueType = mdr.ValueTypes.DValueRef;
      //        base.AsBoolean();
      //      }

      //      void AsInt32()
      //      {
      //        var type = _result.ValueType;
      //        switch (type)
      //        {
      //          //case mdr.ValueTypes.Undefined:
      //          case mdr.ValueTypes.String:
      //            _ilGen.Call(Types.Operations.Convert.ToInt32.Get(type));
      //            break;
      //          case mdr.ValueTypes.Double:
      //            _ilGen.Conv_I4();
      //            break;
      //          case mdr.ValueTypes.Int32:
      //            break;
      //          case mdr.ValueTypes.Boolean:
      //            break;
      //          //case mdr.ValueTypes.Null:
      //          case mdr.ValueTypes.Object:
      //          case mdr.ValueTypes.Function:
      //          case mdr.ValueTypes.Array:
      //          case mdr.ValueTypes.Property:
      //            _ilGen.Callvirt(Types.DObject.ToInt32);
      //            break;
      //          case mdr.ValueTypes.DValueRef:
      //            _ilGen.Call(Types.DValue.AsInt32);
      //            break;
      //          default:
      //            Trace.Fail("Cannot convert type {0} to int", type);
      //            break;
      //        }
      //        _result.ValueType = mdr.ValueTypes.Int32;
      //      }

      //      void AsUInt32() { throw new NotImplementedException(); }
      //      protected override void AsUInt32()
      //      {
      //        LoadStackPop();
      //        _result.ValueType = mdr.ValueTypes.DValueRef;
      //        base.AsUInt32();
      //      }

      //      void AsUInt16() { throw new NotImplementedException(); }

      //      void AsDouble()
      //      {
      //        var type = _result.ValueType;
      //        switch (type)
      //        {
      //          //case mdr.ValueTypes.Undefined:
      //          case mdr.ValueTypes.String:
      //            _ilGen.Call(Types.Operations.Convert.ToDouble.Get(type));
      //            break;
      //          case mdr.ValueTypes.Double:
      //            break;
      //          case mdr.ValueTypes.Int32:
      //            _ilGen.Conv_R8();
      //            break;
      //          case mdr.ValueTypes.Boolean:
      //            _ilGen.Conv_R8();
      //            break;
      //          //case mdr.ValueTypes.Null:
      //          case mdr.ValueTypes.Object:
      //          case mdr.ValueTypes.Function:
      //          case mdr.ValueTypes.Array:
      //          case mdr.ValueTypes.Property:
      //            _ilGen.Callvirt(Types.DObject.ToDouble);
      //            break;
      //          case mdr.ValueTypes.DValueRef:
      //            _ilGen.Call(Types.DValue.AsDouble);
      //            break;
      //          default:
      //            Trace.Fail("Cannot convert type {0} to double", type);
      //            break;
      //        }
      //        _result.ValueType = mdr.ValueTypes.Double;
      //      }
      //      protected override void AsDouble()
      //      {
      //        LoadStackPop();
      //        _result.ValueType = mdr.ValueTypes.DValueRef;
      //        base.AsDouble();
      //      }

      //      void AsString()
      //      {
      //        var type = _result.ValueType;
      //        switch (type)
      //        {
      //          //case mdr.ValueTypes.Undefined:
      //          case mdr.ValueTypes.String:
      //            break;
      //          case mdr.ValueTypes.Double:
      //          case mdr.ValueTypes.Int32:
      //          case mdr.ValueTypes.Boolean:
      //            _ilGen.Call(Types.Operations.Convert.ToString.Get(type));
      //            break;
      //          //case mdr.ValueTypes.Null:
      //          case mdr.ValueTypes.Object:
      //          case mdr.ValueTypes.Function:
      //          case mdr.ValueTypes.Array:
      //          case mdr.ValueTypes.Property:
      //            _ilGen.Callvirt(Types.DObject.ToString);
      //            break;
      //          case mdr.ValueTypes.DValueRef:
      //            _ilGen.Call(Types.DValue.AsString);
      //            break;
      //          default:
      //            Trace.Fail("Cannot convert type {0} to string", type);
      //            break;
      //        }
      //        _result.ValueType = mdr.ValueTypes.String;
      //      }
      //      protected override void AsString()
      //      {
      //        LoadStackPop();
      //        _result.ValueType = mdr.ValueTypes.DValueRef;
      //        base.AsString();
      //      }

      //      void AsDObject()
      //      {
      //        var type = _result.ValueType;
      //        switch (type)
      //        {
      //          case mdr.ValueTypes.Object:
      //          case mdr.ValueTypes.Property:
      //          case mdr.ValueTypes.Function:
      //          case mdr.ValueTypes.Array:
      //            break;
      //          case mdr.ValueTypes.DValueRef:
      //            _ilGen.Call(Types.DValue.AsDObject);
      //            _result.ValueType = mdr.ValueTypes.Object;
      //            break;
      //          default:
      //            Trace.Fail("Cannot convert type {0} to DObject", type);
      //            break;
      //        }
      //      }
      //      protected override void AsDObject()
      //      {
      //        LoadStackPop();
      //        _result.ValueType = mdr.ValueTypes.DValueRef;
      //        base.AsDObject();
      //      }

      //      void AsDArray()
      //      {
      //        var type = _result.ValueType;
      //        switch (type)
      //        {
      //          case mdr.ValueTypes.Array:
      //            break;
      //          case mdr.ValueTypes.DValueRef:
      //            _ilGen.Call(Types.DValue.AsDArray);
      //            _result.ValueType = mdr.ValueTypes.Array;
      //            break;
      //          default:
      //            Trace.Fail("Cannot convert type {0} to DArray", type);
      //            break;
      //        }
      //      }

      //      void AsObject(Type t = null)
      //      {
      //        var type = _result.ValueType;
      //        switch (type)
      //        {
      //          case mdr.ValueTypes.Any:
      //            break;
      //          case mdr.ValueTypes.DValueRef:
      //            _ilGen.Call(Types.DValue.AsObject);
      //            break;
      //          default:
      //            Trace.Fail("Cannot convert type {0} to DObject", type);
      //            break;
      //        }
      //        if (t != null)
      //        {
      //          _ilGen.Castclass(t);
      //          _result.Type = t;
      //        }
      //        else
      //          _result.ValueType = mdr.ValueTypes.Object;
      //      }
      //      protected override void AsObject(Type t = null)
      //      {
      //        LoadStackPop();
      //        _result.ValueType = mdr.ValueTypes.DValueRef;
      //        base.AsObject(t);
      //      }
      #endregion

      #region Statements; ECMA 12. -------------------------------------------------------------------------------------

      public override void Visit(BlockStatement node)
      {
        var currSP = _stackModel.StackPointer;
        base.Visit(node);
        Debug.Assert(currSP == _stackModel.StackPointer, new JSSourceLocation(_currFuncMetadata, node), "Stack depth at begining ({0}) and end ({1}) of block do not match!", currSP, _stackModel.StackPointer);
      }
Ejemplo n.º 10
0
      public override void Visit(ReturnStatement node)
      {
        var gotoEnd = new GotoStatement(_returnLabelName);
        if (node.Expression != null)
        {
          //We have a return value as well
          var cloned = new BlockStatement(new List<Statement>());

          var returnValue = GetCloneOf(node.Expression);

          var retAssign = new WriteIdentifierExpression(_returnValueSymbol, returnValue);
          cloned.Statements.Add(gotoEnd);
          cloned.Statements.Add(new ExpressionStatement(retAssign));
          result = cloned;
        }
        else
        {
          result = gotoEnd;
        }
      }
Ejemplo n.º 11
0
 private void AddVarDeclarationsForParams(BlockStatement body)
 {
   foreach (var p in _targetFuncMetadata.FunctionIR.Parameters)
   {
     var s = p.Symbol;
     Debug.Assert(s.IsParameter, "Invalid situation, symbol {0} in function {1} must be a Parameter", s.Name, _targetFuncMetadata.Declaration);
     var renamedSymbol = GetRenamedSymbolOf(s);
     WriteIdentifierExpression initialization = null;
     if (s.ParameterIndex < _call.Arguments.Count)
     {
       //TODO: in the following, we have not removed the user of the argument, so it will introduce a WriteTemporary. 
       initialization = new WriteIdentifierExpression(renamedSymbol, _call.Arguments[s.ParameterIndex]);
     }
     var declaration = new VariableDeclarationStatement(new List<VariableDeclaration>() { new VariableDeclaration(renamedSymbol, initialization) });
     body.Statements.Add(declaration);
   }
 }
Ejemplo n.º 12
0
 private BlockStatement BuildInlinedBody()
 {
   var body = new BlockStatement(new List<Statement>());
   AddVarDeclarationsForParams(body);
   AddRenamedSymbols();
   _returnLabelName = RenameSymbol("#ReturnLabel:");
   var clonedBody = GetCloneOf(_targetFuncMetadata.FunctionIR.Statement);
   Debug.Assert(clonedBody != null, "Invalid situation! Cloning failed!");
   var returnLabel = new LabelStatement(_returnLabelName, clonedBody);
   body.Statements.Add(returnLabel);
   return body;
 }
Ejemplo n.º 13
0
        public FunctionExpression(Scope scope, ReadIdentifierExpression name, List <ReadIdentifierExpression> parameters, BlockStatement statement)
        {
            Scope        = scope;
            Name         = name;
            Parameters   = parameters;
            Statement    = statement;
            SourceOffset = statement.SourceOffset;

            Use(Statement);
        }
Ejemplo n.º 14
0
 public override void Visit(BlockStatement node)
 {
   PushLocation(node);
   VisitNodes(node.Statements);
   PopLocation();
 }
Ejemplo n.º 15
0
 public override void Visit(BlockStatement node) { Visit((Statement)node); }
Ejemplo n.º 16
0
 public override void Visit(BlockStatement node)
 {
   VisitNodes(node.Statements);
   Visit((Statement)node);
 }
Ejemplo n.º 17
0
      public override void Visit(BlockStatement node)
      {

        var oldStackDepth = _stackModel.StackDepth;

        //base.Visit(node); //For some reason this is not working. 
        PushLocation(node);
        VisitNodes(node.Statements);
        PopLocation();

        Debug.Assert(oldStackDepth == _stackModel.StackDepth, new JSSourceLocation(_currFuncMetadata, node), "Stack depth at begining ({0}) and end ({1}) of block do not match!", oldStackDepth, _stackModel.StackDepth);

      }