Esempio n. 1
0
        public MidAttributeDecl CacheAttr(
            MidExp exp,
            MidType type)
        {
            MidAttributeDecl attrDecl = null;

            if (_attrCache.TryGetValue(exp, out attrDecl))
            {
                return(attrDecl);
            }

            if (exp is MidAttributeRef)
            {
                var attrRef = (MidAttributeRef)exp;
                attrDecl = attrRef.Decl;
                if (attrDecl.Element == this && attrDecl.Exp != null)
                {
                    _attrCache[exp] = attrDecl;
                    return(attrDecl);
                }
            }

            attrDecl = new MidAttributeDecl(
                _name.Factory.unique("attr"),
                this,
                type,
                exp);
            _attrCache[exp] = attrDecl;
            AddAttribute(attrDecl);
            return(attrDecl);
        }
Esempio n. 2
0
 private MidExp PostTransform(MidExp exp)
 {
     if (_postTransform == null)
     {
         return(exp);
     }
     return(_postTransform(exp));
 }
Esempio n. 3
0
        public MidExp Transform(MidExp exp)
        {
            var e = PreTransform(exp);

            TransformChildren(e);
            e = PostTransform(e);
            return(e);
        }
Esempio n. 4
0
        public static ISpan Dump(
            this MidExp exp)
        {
            var span = new Span();

            exp.Dump(span);
            return(span);
        }
Esempio n. 5
0
 public MidCase(
     MidVal value,
     MidExp body,
     SourceRange range)
 {
     this.Value = value;
     this.Body  = body;
     _range     = range;
 }
Esempio n. 6
0
        public T MaybeMoveTemp <T>(MidExp exp, MidType type)
            where T : MidExp
        {
            if (exp is T)
            {
                return((T)exp);
            }

            return((T)(MidExp)CreateTemp(exp, type));
        }
Esempio n. 7
0
        public MidExp Wrap(MidExp body)
        {
            var result = body;

            foreach (var wrapper in _wrappers)
            {
                result = wrapper(result);
            }
            return(result);
        }
Esempio n. 8
0
 public MidLabelExp(
     SourceRange range,
     MidLabel label,
     MidExp body,
     MidType type)
     : base(range, type)
 {
     _label = label;
     _body  = body;
 }
Esempio n. 9
0
 public MidIfExp(
     MidVal condition,
     MidExp thenExp,
     MidExp elseExp,
     SourceRange range)
     : base(range, new MidDummyType())
 {
     this.Condition = condition;
     this.Then      = thenExp;
     this.Else      = elseExp;
 }
Esempio n. 10
0
 public MidLetExp(
     SourceRange range,
     MidVar var,
     MidExp exp,
     MidExp body)
     : base(range, new MidDummyType())
 {
     _var  = var;
     _exp  = exp;
     _body = body;
 }
Esempio n. 11
0
 public MidAttributeDecl(
     Identifier name,
     MidElementDecl element,
     MidType type,
     MidExp exp)
 {
     _name    = name;
     _element = element;
     _type    = type;
     _exp     = exp;
 }
Esempio n. 12
0
 public MidForExp(
     SourceRange range,
     MidVar var,
     MidVal seq,
     MidExp body)
     : base(range, new MidVoidType())
 {
     _var  = var;
     _seq  = seq;
     _body = body;
 }
Esempio n. 13
0
        private MidExp SimplifyLabelExpImpl(
            MidLabelExp labelExp,
            MidExp exp,
            SimplifyEnv env)
        {
            if (!UsesLabel(exp, labelExp.Label))
            {
                return(exp);
            }

            return(labelExp);
        }
Esempio n. 14
0
        private MidExp SimplifyExp(MidExp exp, SimplifyEnv env)
        {
            if (exp == null)
            {
                return(null);
            }

            var transform = new MidTransform(
                null, // no pre-transform
                (e) => SimplifyExpImpl((dynamic)e, env));

            return(transform.Transform(exp));
        }
Esempio n. 15
0
        public static void MarkOutputs(MidExp exp)
        {
            MidTransform transform = new MidTransform(
                (e) =>
            {
                if (e is MidAttributeFetch)
                {
                    ((MidAttributeFetch)e).Attribute.IsOutput = true;
                }
                return(e);
            });

            transform.Transform(exp);
        }
Esempio n. 16
0
        public override MidVal CreateTemp(MidExp exp, MidType type)
        {
            if (_element != null)
            {
                var attrDecl = _element.CacheAttr(exp, type);
                return(_exps.AttributeRef(exp.Range, attrDecl));
            }
            else
            {
                MidVar var = new MidVar(_identifiers.unique("temp"), type);

                Func <MidExp, MidExp> wrapper = (body) => new MidLetExp(exp.Range, var, exp, body);

                _wrappers.Insert(0, wrapper);

                return(new MidVarRef(exp.Range, var));
            }
        }
Esempio n. 17
0
        private bool UsesVar(
            MidExp exp,
            MidVar var)
        {
            bool result    = false;
            var  transform = new MidTransform(
                (e) =>
            {
                if (e is MidVarRef && (e as MidVarRef).Var == var)
                {
                    result = true;
                }
                return(e);
            });

            transform.Transform(exp);

            return(result);
        }
Esempio n. 18
0
        private bool UsesLabel(
            MidExp exp,
            MidLabel label)
        {
            bool result    = false;
            var  transform = new MidTransform(
                (e) =>
            {
                if (e is MidBreakExp && (e as MidBreakExp).Label == label)
                {
                    result = true;
                }
                return(e);
            });

            transform.Transform(exp);

            return(result);
        }
Esempio n. 19
0
 private MidExp TryFoldPath(
     MidVar var,
     MidExp exp,
     MidPath path)
 {
     if (exp is MidFieldRef)
     {
         var midFieldRef = (MidFieldRef)exp;
         if (midFieldRef.Obj is MidVarRef)
         {
             var midVarRef = (MidVarRef)midFieldRef.Obj;
             if (midVarRef.Var == var)
             {
                 midFieldRef.Obj = path;
                 return(midFieldRef);
             }
         }
     }
     return(exp);
 }
Esempio n. 20
0
        private bool MightHaveSideEffects(
            MidExp exp)
        {
            bool result    = false;
            var  transform = new MidTransform(
                (e) =>
            {
                if (e is MidAssignExp)
                {
                    result = true;
                }
                if (e is MidBreakExp)
                {
                    result = true;
                }
                if (e is MidIfExp)
                {
                    result = true;
                }
                if (e is MidForExp)
                {
                    result = true;
                }
                if (e is MidBuiltinApp)
                {
                    // \todo: Need a *huge* fix for this. Stdlib functions that might
                    // have side-effects need to be marked in some way to avoid this kind of thing... :(
                    var app = (MidBuiltinApp)e;
                    if (app.Decl.Name.ToString() == "Append")
                    {
                        result = true;
                    }
                }
                return(e);
            });

            transform.Transform(exp);

            return(result);
        }
Esempio n. 21
0
 public override MidVal CreateTemp(MidExp exp, MidType type)
 {
     throw new NotImplementedException();
 }
Esempio n. 22
0
 public void TransformChildren(MidExp exp)
 {
     TransformChildrenImpl((dynamic)exp);
 }
Esempio n. 23
0
 public abstract MidVal CreateTemp(MidExp exp, MidType type);
Esempio n. 24
0
 public override MidVal CreateTemp(MidExp exp, MidType type)
 {
     return(_parent.CreateTemp(exp, type));
 }
Esempio n. 25
0
 public MidExp PreTransform(MidExp exp)
 {
     return(exp);
 }
Esempio n. 26
0
 private MidExp SimplifyExpImpl(MidExp exp, SimplifyEnv env)
 {
     return(exp);
 }
Esempio n. 27
0
 private MidExp Replace(
     MidExp exp)
 {
     return(ReplaceImpl((dynamic)exp));
 }
Esempio n. 28
0
 public MidExp CleanupExp(MidExp exp)
 {
     return(exp);
 }
Esempio n. 29
0
 public static void Dump(
     this MidExp exp,
     Span span)
 {
     DumpExpImpl((dynamic)exp, span);
 }