示例#1
0
func (PS *PgeSearch) GenInitExprAddMethod1() []expr.Expr {

	exprs := make([]expr.Expr, 0)

	// WARNING adding a single coefficient results in the same traversal but with the added extra coefficient
	cf := new(expr.Constant)
	cf.P = 1
	af := new(expr.Add)
	af.Insert(cf)
	exprs = append(exprs, af)

	for _, i := range PS.cnfg.treecfg.UsableVars {
		c := new(expr.Constant)
		c.P = -1
		v := new(expr.Var)
		v.P = i

		m := expr.NewMul()
		m.Insert(c)
		m.Insert(v)

		a := expr.NewAdd()
		a.Insert(m)
		exprs = append(exprs, a)
	}

	fmt.Println("Initial Add:  ", exprs)
	return exprs
}
示例#2
0
// add complexity to a single multiplication term
func (PS *PgeSearch) WidenTermInExprMethod1(O, E expr.Expr, pos int) (ret []expr.Expr) {
	ret = make([]expr.Expr, 0)

	// insert leafs  f()*L
	for _, L := range PS.GenLeafs {
		l := L.Clone()
		C := O.Clone()
		P := pos
		e := C.GetExpr(&P)
		// fmt.Printf("pos(%d): %v\n", pos, e)
		M := e.(*expr.Mul)
		M.Insert(l)
		sort.Sort(M)
		C.CalcExprStats()
		good := PS.cnfg.treecfg.CheckExpr(C)
		if good {
			ret = append(ret, C)
		}
	}

	// insert node(L)  :  f() * c*node(L)
	for _, N := range PS.GenNodes {
		for _, L := range PS.GenLeafs {
			c := new(expr.Constant)
			c.P = -1

			l := L.Clone()
			n := N.Clone()
			p := 1
			n.SetExpr(&p, l)

			var E expr.Expr
			if N.ExprType() == expr.DIV {
				E = expr.NewDiv(c, l)
			} else {
				// mul it
				M := expr.NewMul()
				M.Insert(c)
				M.Insert(n)
				E = M
			}

			C := O.Clone()
			P := pos
			e := C.GetExpr(&P)
			// fmt.Printf("pos(%d): %v\n", pos, e)
			M := e.(*expr.Mul)

			M.Insert(E)
			sort.Sort(M)
			C.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(C)
			if good {
				ret = append(ret, C)
			}
		}
	}
	return ret
}
示例#3
0
func (PS *PgeSearch) GenInitExprDivMethod1() []expr.Expr {
	exprs := make([]expr.Expr, 0)

	for _, i := range PS.cnfg.treecfg.UsableVars {
		c := new(expr.Constant)
		c.P = -1
		v := new(expr.Var)
		v.P = i

		d := new(expr.Div)
		d.Numer = c
		d.Denom = v

		exprs = append(exprs, d)
	}

	fmt.Println("Initial Div:  ", exprs)
	return exprs
}
示例#4
0
func (PS *PgeSearch) GenInitExprMulMethod1() []expr.Expr {
	exprs := make([]expr.Expr, 0)

	for _, i := range PS.cnfg.treecfg.UsableVars {
		c := new(expr.Constant)
		c.P = -1
		v := new(expr.Var)
		v.P = i

		m := expr.NewMul()
		m.Insert(c)
		m.Insert(v)

		exprs = append(exprs, m)
	}

	fmt.Println("Initial Mul:  ", exprs)
	return exprs
}
示例#5
0
// add another term to an add expr
func (PS *PgeSearch) AddTermToExprMethod1(O, E expr.Expr, pos int) (ret []expr.Expr) {
	ret = make([]expr.Expr, 0)
	A := E.(*expr.Add)

	// f() + cL
	for _, L := range PS.GenLeafs {
		c := new(expr.Constant)
		c.P = -1
		l := L.Clone()

		// mul it
		M := expr.NewMul()
		M.Insert(c)
		M.Insert(l)

		// skip if the same term already exists in the add
		skip := false
		for _, e := range A.CS {
			if e == nil {
				continue
			}
			// fmt.Printf("ACMP  %v  %v\n", M, e)
			if e.AmIAlmostSame(M) || M.AmIAlmostSame(e) {
				skip = true
				break
			}
		}
		if skip {
			continue
		}

		C := O.Clone()
		P := pos
		AM := C.GetExpr(&P).(*expr.Add)
		AM.Insert(M)
		sort.Sort(AM)
		C.CalcExprStats()
		good := PS.cnfg.treecfg.CheckExpr(C)
		if good {
			ret = append(ret, C)
		}
	}

	// f() + c*node(L)
	for _, N := range PS.GenNodes {
		for _, L := range PS.GenLeafs {

			c := new(expr.Constant)
			c.P = -1

			l := L.Clone()
			n := N.Clone()
			p := 1
			n.SetExpr(&p, l)

			var E expr.Expr
			if N.ExprType() == expr.DIV {
				E = expr.NewDiv(c, l)
			} else {
				// mul it
				M := expr.NewMul()
				M.Insert(c)
				M.Insert(n)
				E = M
			}

			// skip if the same term already exists in the add
			skip := false
			for _, e := range A.CS {
				if e == nil {
					continue
				}
				// fmt.Printf("ACMP  %v  %v\n", M, e)
				if e.AmIAlmostSame(E) || E.AmIAlmostSame(e) {
					skip = true
					break
				}
			}
			if skip {
				continue
			}

			// fmt.Println(E.String())

			C := O.Clone()
			P := pos
			AM := C.GetExpr(&P).(*expr.Add)
			AM.Insert(E)
			sort.Sort(AM)
			C.CalcExprStats()
			good := PS.cnfg.treecfg.CheckExpr(C)
			if good {
				ret = append(ret, C)
			}
		}
	}

	return ret
}