Search in sources :

Example 1 with Fun2

use of suite.util.FunUtil2.Fun2 in project suite by stupidsing.

the class Fractional method format.

public Node format(Fract<I> fract) {
    OpGroup add = ex.add;
    OpGroup mul = ex.mul;
    Fun2<I, I, Node> f = (n, d) -> {
        Node i0 = format_.apply(n);
        Node i1 = format_.apply(d);
        return mul.apply(i0, mul.inverse(i1));
    };
    I n_ = fract.t0;
    I d_ = fract.t1;
    I nn = neg_.apply(n_);
    if (0 <= sgn_.apply(n_))
        return f.apply(n_, d_);
    else
        return add.inverse(f.apply(nn, d_));
}
Also used : Ring(suite.math.sym.Sym.Ring) Pattern(suite.BindArrayUtil.Pattern) OpGroup(suite.math.sym.Express.OpGroup) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) Opt(suite.adt.Opt) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) Fun2(suite.util.FunUtil2.Fun2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) Field(suite.math.sym.Sym.Field) SwitchNode(suite.node.io.SwitchNode) Int(suite.node.Int) OpGroup(suite.math.sym.Express.OpGroup) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode)

Example 2 with Fun2

use of suite.util.FunUtil2.Fun2 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

Fun2 (suite.util.FunUtil2.Fun2)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Pattern (suite.BindArrayUtil.Pattern)1 Opt (suite.adt.Opt)1 Pair (suite.adt.pair.Pair)1 Amd64 (suite.assembler.Amd64)1 OpMem (suite.assembler.Amd64.OpMem)1 OpReg (suite.assembler.Amd64.OpReg)1 Operand (suite.assembler.Amd64.Operand)1 Funp (suite.funp.Funp_.Funp)1 FunpDontCare (suite.funp.P0.FunpDontCare)1 FunpNumber (suite.funp.P0.FunpNumber)1 FunpTree (suite.funp.P0.FunpTree)1 FunpTree2 (suite.funp.P0.FunpTree2)1 FunpFramePointer (suite.funp.P2.FunpFramePointer)1 FunpMemory (suite.funp.P2.FunpMemory)1 OpGroup (suite.math.sym.Express.OpGroup)1 Field (suite.math.sym.Sym.Field)1 Ring (suite.math.sym.Sym.Ring)1