Search in sources :

Example 1 with Funp

use of suite.funp.Funp_.Funp in project suite by stupidsing.

the class P2InferType method captureLambdas.

private Funp captureLambdas(Funp node0) {
    class Capture {

        private Fun<String, Funp> accesses;

        private ISet<String> locals;

        private ISet<String> globals;

        private Capture(Fun<String, Funp> accesses, ISet<String> locals, ISet<String> globals) {
            this.accesses = accesses;
            this.locals = locals;
            this.globals = globals;
        }

        private Funp capture(Funp n) {
            return inspect.rewrite(Funp.class, this::capture_, n);
        }

        private Funp capture_(Funp n) {
            return // 
            n.<// 
            Funp>switch_().applyIf(FunpDefine.class, f -> f.apply((isPolyType, var, value, expr) -> {
                Capture c1 = new Capture(accesses, locals.add(var), globals);
                return FunpDefine.of(isPolyType, var, capture(value), c1.capture(expr));
            })).applyIf(FunpDefineRec.class, f -> f.apply((vars, expr) -> {
                List<Pair<String, Funp>> vars1 = new ArrayList<>();
                ISet<String> locals1 = locals;
                for (Pair<String, Funp> pair : vars) {
                    locals1 = locals1.add(pair.t0);
                    vars1.add(Pair.of(pair.t0, capture(pair.t1)));
                }
                Capture c1 = new Capture(accesses, locals1, globals);
                return FunpDefineRec.of(vars1, c1.capture(expr));
            })).applyIf(FunpGlobal.class, f -> f.apply((var, value, expr) -> {
                Capture c1 = new Capture(accesses, locals, globals.add(var));
                return FunpGlobal.of(var, capture(value), c1.capture(expr));
            })).applyIf(FunpIterate.class, f -> f.apply((var, init, cond, iterate) -> {
                Capture c1 = new Capture(accesses, locals.add(var), globals);
                return FunpIterate.of(var, capture(init), c1.capture(cond), c1.capture(iterate));
            })).applyIf(FunpLambda.class, f -> f.apply((var, expr) -> {
                ISet<String> locals1 = ISet.empty();
                String capn = "cap" + Util.temp();
                FunpVariable cap = FunpVariable.of(capn);
                FunpReference ref = FunpReference.of(cap);
                Set<String> set = new HashSet<>();
                List<Pair<String, Funp>> list = new ArrayList<>();
                FunpStruct struct = FunpStruct.of(list);
                Capture c1 = new Capture(v -> {
                    if (set.add(v))
                        list.add(Pair.of(v, FunpVariable.of(v)));
                    return FunpField.of(ref, v);
                }, locals1.add(capn).add(var), globals);
                return FunpGlobal.of(capn, struct, FunpLambdaCapture.of(var, capn, cap, c1.capture(expr)));
            // TODO allocate cap on heap
            // TODO free cap after use
            })).applyIf(FunpVariable.class, f -> f.apply(var -> {
                return locals.contains(var) || globals.contains(var) ? f : accesses.apply(var);
            })).result();
        }
    }
    return new Capture(v -> Fail.t(), ISet.empty(), ISet.empty()).capture(node0);
}
Also used : FunpAllocGlobal(suite.funp.P2.FunpAllocGlobal) FunpInvokeIo(suite.funp.P2.FunpInvokeIo) Mutable(suite.adt.Mutable) FunpOperand(suite.funp.P2.FunpOperand) FunpDefine(suite.funp.P0.FunpDefine) Fun(suite.util.FunUtil.Fun) FunpLambda(suite.funp.P0.FunpLambda) FunpFramePointer(suite.funp.P2.FunpFramePointer) FunpPredefine(suite.funp.P0.FunpPredefine) Operand(suite.assembler.Amd64.Operand) Map(java.util.Map) FunpWhile(suite.funp.P2.FunpWhile) FunpData(suite.funp.P2.FunpData) FunpInvoke(suite.funp.P2.FunpInvoke) UnNode(suite.fp.Unify.UnNode) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpDefineRec(suite.funp.P0.FunpDefineRec) IMap(suite.immutable.IMap) Set(java.util.Set) ISet(suite.immutable.ISet) Unify(suite.fp.Unify) FunpRoutineIo(suite.funp.P2.FunpRoutineIo) Pair(suite.adt.pair.Pair) List(java.util.List) FunpInvoke2(suite.funp.P2.FunpInvoke2) FunpRepeat(suite.funp.P0.FunpRepeat) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpSaveRegisters(suite.funp.P2.FunpSaveRegisters) FunpVariable(suite.funp.P0.FunpVariable) FunpIterate(suite.funp.P0.FunpIterate) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) AutoObject(suite.util.AutoObject) FunpBoolean(suite.funp.P0.FunpBoolean) Util(suite.util.Util) FunpArray(suite.funp.P0.FunpArray) HashMap(java.util.HashMap) OpReg(suite.assembler.Amd64.OpReg) FunpIndex(suite.funp.P0.FunpIndex) FunpIoCat(suite.funp.P0.FunpIoCat) FunpReference(suite.funp.P0.FunpReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FunpApply(suite.funp.P0.FunpApply) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) String_(suite.util.String_) FunpIf(suite.funp.P0.FunpIf) Rethrow(suite.util.Rethrow) FunpGlobal(suite.funp.P0.FunpGlobal) FunpIo(suite.funp.P0.FunpIo) FunpAsm(suite.funp.P0.FunpAsm) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) FunpField(suite.funp.P0.FunpField) TermOp(suite.node.io.TermOp) FunpAssign(suite.funp.P2.FunpAssign) FunpAssignReference(suite.funp.P0.FunpAssignReference) FunpCheckType(suite.funp.P0.FunpCheckType) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) FunpError(suite.funp.P0.FunpError) FunpRoutine2(suite.funp.P2.FunpRoutine2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) FunpNumber(suite.funp.P0.FunpNumber) FunpRoutine(suite.funp.P2.FunpRoutine) FixieFun1(suite.adt.pair.Fixie_.FixieFun1) FixieFun2(suite.adt.pair.Fixie_.FixieFun2) Switch(suite.util.Switch) FunpStruct(suite.funp.P0.FunpStruct) FixieFun0(suite.adt.pair.Fixie_.FixieFun0) FunpAllocStack(suite.funp.P2.FunpAllocStack) Fail(suite.util.Fail) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) Set(java.util.Set) ISet(suite.immutable.ISet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) FunpIterate(suite.funp.P0.FunpIterate) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) Funp(suite.funp.Funp_.Funp) FunpDefineRec(suite.funp.P0.FunpDefineRec) FunpStruct(suite.funp.P0.FunpStruct) FunpVariable(suite.funp.P0.FunpVariable) List(java.util.List) ArrayList(java.util.ArrayList) ISet(suite.immutable.ISet) Fun(suite.util.FunUtil.Fun) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 2 with Funp

use of suite.funp.Funp_.Funp in project suite by stupidsing.

the class P2InferType method infer.

public Funp infer(Funp n0) {
    UnNode<Type> t = unify.newRef();
    Funp n1 = extractPredefine(n0);
    Funp n2 = Boolean.FALSE ? captureLambdas(n1) : n1;
    if (unify.unify(t, new Infer(IMap.empty()).infer(n2)))
        return new Erase(0, IMap.empty()).erase(n2);
    else
        return Fail.t("cannot infer type for " + n0);
}
Also used : Funp(suite.funp.Funp_.Funp) FunpCheckType(suite.funp.P0.FunpCheckType)

Example 3 with Funp

use of suite.funp.Funp_.Funp in project suite by stupidsing.

the class P3Optimize method optimize_.

private Funp optimize_(Funp n) {
    return // 
    n.<// 
    Funp>switch_().applyIf(FunpCoerce.class, f -> f.apply((coerce, expr) -> {
        return !(expr instanceof FunpDontCare) ? n : optimize(expr);
    })).applyIf(FunpData.class, f -> f.apply(pairs -> {
        return FunpData.of(Read.from2(pairs).concatMap((expr, range) -> {
            Funp expr1 = optimize(expr);
            int start = range.t0;
            Streamlet<Pair<Funp, IntIntPair>> pairsx = new // 
            Switch<Streamlet<Pair<Funp, IntIntPair>>>(// 
            expr1).applyIf(FunpData.class, g -> g.apply(pairs1 -> {
                return // 
                Read.from2(// 
                pairs1).map((exprc, range1) -> Pair.of(optimize(exprc), IntIntPair.of(start + range1.t0, start + range1.t1)));
            })).result();
            return pairsx != null ? pairsx : Read.each(Pair.of(expr1, range));
        }).toList());
    })).applyIf(FunpDeref.class, f -> f.apply(pointer -> {
        return optimize(pointer).<Funp>switch_().applyIf(FunpReference.class, g -> g.expr).result();
    })).applyIf(FunpIf.class, f -> f.apply((if_, then, else_) -> {
        return // 
        optimize(if_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
            return b ? then : else_;
        })).result();
    })).applyIf(FunpMemory.class, f -> f.apply((pointer, start, end) -> {
        return // 
        optimize(pointer).<Funp>switch_().applyIf(FunpData.class, g -> g.apply(pairs -> {
            for (Pair<Funp, IntIntPair> pair : pairs) {
                IntIntPair range = pair.t1;
                if (start == range.t0 && end == range.t1)
                    return pair.t0;
            }
            return null;
        })).applyIf(FunpReference.class, g -> {
            return FunpTree.of(TermOp.PLUS__, g.expr, FunpNumber.ofNumber(start));
        }).result();
    })).applyIf(FunpReference.class, f -> f.apply(expr -> {
        return optimize(expr).<Funp>switch_().applyIf(FunpMemory.class, g -> g.pointer).result();
    })).applyIf(FunpTree.class, f -> f.apply((operator, lhs, rhs) -> {
        IntInt_Bool iib = TreeUtil.boolOperations.get(operator);
        IntInt_Int iii = TreeUtil.intOperations.get(operator);
        if (iib != null)
            return evaluate(iib, lhs, rhs);
        else if (iii != null)
            return evaluate(iii, lhs, rhs);
        else
            return null;
    })).applyIf(FunpTree2.class, f -> f.apply((operator, lhs, rhs) -> {
        return evaluate(TreeUtil.tupleOperations.get(operator), lhs, rhs);
    })).applyIf(FunpWhile.class, f -> f.apply((while_, do_, expr) -> {
        return // 
        optimize(while_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
            return b ? null : expr;
        })).result();
    })).result();
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpBoolean(suite.funp.P0.FunpBoolean) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) TermOp(suite.node.io.TermOp) FunpReference(suite.funp.P0.FunpReference) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) IntInt_Int(suite.primitive.IntInt_Int) Pair(suite.adt.pair.Pair) Streamlet(suite.streamlet.Streamlet) FunpIf(suite.funp.P0.FunpIf) FunpNumber(suite.funp.P0.FunpNumber) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpWhile(suite.funp.P2.FunpWhile) Switch(suite.util.Switch) FunpData(suite.funp.P2.FunpData) TreeUtil(suite.node.util.TreeUtil) FunpDontCare(suite.funp.P0.FunpDontCare) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) FunpWhile(suite.funp.P2.FunpWhile) FunpMemory(suite.funp.P2.FunpMemory) FunpBoolean(suite.funp.P0.FunpBoolean) Funp(suite.funp.Funp_.Funp) FunpDeref(suite.funp.P0.FunpDeref) Streamlet(suite.streamlet.Streamlet) FunpCoerce(suite.funp.P0.FunpCoerce) IntInt_Int(suite.primitive.IntInt_Int) FunpTree(suite.funp.P0.FunpTree) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IntIntPair(suite.primitive.adt.pair.IntIntPair) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 4 with Funp

use of suite.funp.Funp_.Funp in project suite by stupidsing.

the class P2InferType method extractPredefine.

private Funp extractPredefine(Funp node0) {
    List<Pair<String, Funp>> evs = new ArrayList<>();
    Funp node1 = new Object() {

        private Funp extract_(Funp n) {
            return inspect.rewrite(Funp.class, n_ -> {
                return // 
                n_.<// 
                Funp>switch_().applyIf(FunpDefine.class, f -> f.apply((isPolyType, var, value, expr) -> {
                    return FunpDefine.of(isPolyType, var, extractPredefine(value), extract_(expr));
                })).applyIf(FunpDefineRec.class, f -> f.apply((pairs0, expr) -> {
                    List<Pair<String, Funp>> pairs1 = Read.from2(pairs0).mapValue(P2InferType.this::extractPredefine).toList();
                    return FunpDefineRec.of(pairs1, extract_(expr));
                })).applyIf(FunpGlobal.class, f -> f.apply((var, value, expr) -> {
                    return FunpGlobal.of(var, extractPredefine(value), extract_(expr));
                })).applyIf(FunpLambda.class, f -> f.apply((var, expr) -> {
                    return FunpLambda.of(var, extractPredefine(expr));
                })).applyIf(FunpPredefine.class, f -> f.apply(expr -> {
                    String ev = "ev" + Util.temp();
                    evs.add(Pair.of(ev, expr));
                    Funp var = FunpVariable.of(ev);
                    return FunpAssignReference.of(FunpReference.of(var), expr, var);
                })).result();
            }, n);
        }
    }.extract_(node0);
    for (Pair<String, Funp> pair : evs) node1 = FunpDefine.of(false, pair.t0, FunpDontCare.of(), node1);
    return node1;
}
Also used : Funp(suite.funp.Funp_.Funp) FunpAllocGlobal(suite.funp.P2.FunpAllocGlobal) FunpInvokeIo(suite.funp.P2.FunpInvokeIo) Mutable(suite.adt.Mutable) FunpOperand(suite.funp.P2.FunpOperand) FunpDefine(suite.funp.P0.FunpDefine) Fun(suite.util.FunUtil.Fun) FunpLambda(suite.funp.P0.FunpLambda) FunpFramePointer(suite.funp.P2.FunpFramePointer) FunpPredefine(suite.funp.P0.FunpPredefine) Operand(suite.assembler.Amd64.Operand) Map(java.util.Map) FunpWhile(suite.funp.P2.FunpWhile) FunpData(suite.funp.P2.FunpData) FunpInvoke(suite.funp.P2.FunpInvoke) UnNode(suite.fp.Unify.UnNode) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpDefineRec(suite.funp.P0.FunpDefineRec) IMap(suite.immutable.IMap) Set(java.util.Set) ISet(suite.immutable.ISet) Unify(suite.fp.Unify) FunpRoutineIo(suite.funp.P2.FunpRoutineIo) Pair(suite.adt.pair.Pair) List(java.util.List) FunpInvoke2(suite.funp.P2.FunpInvoke2) FunpRepeat(suite.funp.P0.FunpRepeat) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpSaveRegisters(suite.funp.P2.FunpSaveRegisters) FunpVariable(suite.funp.P0.FunpVariable) FunpIterate(suite.funp.P0.FunpIterate) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) AutoObject(suite.util.AutoObject) FunpBoolean(suite.funp.P0.FunpBoolean) Util(suite.util.Util) FunpArray(suite.funp.P0.FunpArray) HashMap(java.util.HashMap) OpReg(suite.assembler.Amd64.OpReg) FunpIndex(suite.funp.P0.FunpIndex) FunpIoCat(suite.funp.P0.FunpIoCat) FunpReference(suite.funp.P0.FunpReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FunpApply(suite.funp.P0.FunpApply) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) String_(suite.util.String_) FunpIf(suite.funp.P0.FunpIf) Rethrow(suite.util.Rethrow) FunpGlobal(suite.funp.P0.FunpGlobal) FunpIo(suite.funp.P0.FunpIo) FunpAsm(suite.funp.P0.FunpAsm) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) FunpField(suite.funp.P0.FunpField) TermOp(suite.node.io.TermOp) FunpAssign(suite.funp.P2.FunpAssign) FunpAssignReference(suite.funp.P0.FunpAssignReference) FunpCheckType(suite.funp.P0.FunpCheckType) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) FunpError(suite.funp.P0.FunpError) FunpRoutine2(suite.funp.P2.FunpRoutine2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) FunpNumber(suite.funp.P0.FunpNumber) FunpRoutine(suite.funp.P2.FunpRoutine) FixieFun1(suite.adt.pair.Fixie_.FixieFun1) FixieFun2(suite.adt.pair.Fixie_.FixieFun2) Switch(suite.util.Switch) FunpStruct(suite.funp.P0.FunpStruct) FixieFun0(suite.adt.pair.Fixie_.FixieFun0) FunpAllocStack(suite.funp.P2.FunpAllocStack) Fail(suite.util.Fail) FunpDontCare(suite.funp.P0.FunpDontCare) FunpDefineRec(suite.funp.P0.FunpDefineRec) ArrayList(java.util.ArrayList) FunpLambda(suite.funp.P0.FunpLambda) AutoObject(suite.util.AutoObject) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 5 with Funp

use of suite.funp.Funp_.Funp in project suite by stupidsing.

the class P4DecomposeOperand method decomposeOpMem.

public OpMem decomposeOpMem(int fd, Funp n0, int disp0, int size) {
    class Decompose {

        private Operator operator;

        private List<Funp> nodes = new ArrayList<>();

        private Decompose(Operator operator) {
            this.operator = operator;
        }

        private void decompose(Funp n_) {
            FunpTree tree;
            if (n_ instanceof FunpTree && (tree = (FunpTree) n_).operator == operator) {
                decompose(tree.left);
                decompose(tree.right);
            } else
                nodes.add(n_);
        }
    }
    Fun2<Operator, Funp, List<Funp>> decompose = (operator, n_) -> {
        Decompose dec = new Decompose(operator);
        dec.decompose(n_);
        return dec.nodes;
    };
    class DecomposeMult {

        private long scale = 1;

        private OpReg reg;

        private List<Funp> mults = new ArrayList<>();

        private void decompose(Funp n0) {
            FunpTree2 tree;
            Funp r;
            for (Funp n1 : decompose.apply(TermOp.MULT__, n0)) if (n1 instanceof FunpFramePointer && isUseEbp && reg == null)
                reg = amd64.ebp;
            else if (n1 instanceof FunpNumber)
                scale *= ((FunpNumber) n1).i.get();
            else if (// 
            n1 instanceof FunpTree2 && // 
            (tree = (FunpTree2) n1).operator == TreeUtil.SHL && (r = tree.right) instanceof FunpNumber) {
                decompose(tree.left);
                scale <<= ((FunpNumber) r).i.get();
            } else
                mults.add(n1);
        }
    }
    class DecomposePlus {

        private OpReg baseReg = null, indexReg = null;

        private int scale = 1, disp = disp0;

        private boolean ok = is124(size);

        private DecomposePlus(Funp n0) {
            for (Funp n1 : decompose.apply(TermOp.PLUS__, n0)) if (n1 instanceof FunpFramePointer && !isUseEbp) {
                addReg(amd64.esp, 1);
                disp -= fd;
            } else {
                DecomposeMult dec = new DecomposeMult();
                dec.decompose(n1);
                if (dec.mults.isEmpty()) {
                    OpReg reg_ = dec.reg;
                    long scale_ = dec.scale;
                    if (reg_ != null)
                        addReg(reg_, scale_);
                    else
                        disp += scale_;
                } else
                    ok = false;
            }
        }

        private void addReg(OpReg reg_, long scale_) {
            if (scale_ == 1 && baseReg == null)
                baseReg = reg_;
            else if (is1248(scale_) && indexReg == null) {
                indexReg = reg_;
                scale = (int) scale_;
            } else
                ok = false;
        }

        private OpMem op() {
            return ok ? amd64.mem(baseReg, indexReg, scale, disp, size) : null;
        }
    }
    return new DecomposePlus(n0).op();
}
Also used : Operator(suite.node.io.Operator) OpMem(suite.assembler.Amd64.OpMem) FunpTree(suite.funp.P0.FunpTree) Amd64(suite.assembler.Amd64) TermOp(suite.node.io.TermOp) OpReg(suite.assembler.Amd64.OpReg) ArrayList(java.util.ArrayList) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) List(java.util.List) FunpFramePointer(suite.funp.P2.FunpFramePointer) Fun2(suite.util.FunUtil2.Fun2) FunpNumber(suite.funp.P0.FunpNumber) Operand(suite.assembler.Amd64.Operand) FunpMemory(suite.funp.P2.FunpMemory) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) FunpDontCare(suite.funp.P0.FunpDontCare) FunpTree2(suite.funp.P0.FunpTree2) FunpNumber(suite.funp.P0.FunpNumber) Funp(suite.funp.Funp_.Funp) ArrayList(java.util.ArrayList) List(java.util.List) FunpFramePointer(suite.funp.P2.FunpFramePointer) FunpTree(suite.funp.P0.FunpTree) OpReg(suite.assembler.Amd64.OpReg)

Aggregations

Funp (suite.funp.Funp_.Funp)6 FunpNumber (suite.funp.P0.FunpNumber)5 FunpTree (suite.funp.P0.FunpTree)5 ArrayList (java.util.ArrayList)4 FunpDontCare (suite.funp.P0.FunpDontCare)4 FunpIf (suite.funp.P0.FunpIf)4 FunpReference (suite.funp.P0.FunpReference)4 FunpTree2 (suite.funp.P0.FunpTree2)4 FunpMemory (suite.funp.P2.FunpMemory)4 TermOp (suite.node.io.TermOp)4 List (java.util.List)3 Pair (suite.adt.pair.Pair)3 OpReg (suite.assembler.Amd64.OpReg)3 Operand (suite.assembler.Amd64.Operand)3 FunpApply (suite.funp.P0.FunpApply)3 FunpArray (suite.funp.P0.FunpArray)3 FunpDefine (suite.funp.P0.FunpDefine)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2