Esempio n. 1
0
        public void ExtremumCal(double up, double down, string exp,
                                out double min, out double max,
                                int concurr_num = 50000)
        {
            if (up < down)
            {
                throw new Exception("上界不可小于下界!");
            }
            taskList.Clear();
            MinMaxVal result = new MinMaxVal();

            funcExp = exp;
            Function.Function function = new Function.Function(exp);
            result.MaxVal = function.GetValue(down);
            result.MinVal = function.GetValue(down);

            ProduceTask(up, down, concurr_num, result, function);
            Task.WaitAll(taskList.ToArray());

            foreach (Task <MinMaxVal> t in taskList)
            {
                result.MinVal = Math.Min(result.MinVal, t.Result.MinVal);
                result.MaxVal = Math.Max(result.MaxVal, t.Result.MaxVal);
            }
            min = result.MinVal;
            max = result.MaxVal;
        }
Esempio n. 2
0
 private void ProduceTask(double up, double down,
                          int concurr_num, ref double result, Function.Function function)
 {
     if (concurr_num >= (int)((up - down) / precision))
     {
         for (double x = down + precision; x < up; x += precision)
         {
             double        temp = x;
             Task <double> task = Task.Run(() => ThreadCalSingle(temp));
             taskList.Add(task);
         }
     }
     else
     {
         interval = (up - down) / (2 * concurr_num);
         double x = down + interval;
         for (; x < up; x += interval)
         {
             double        temp = x;
             Task <double> task = Task.Run(() => ThreadCalMutiple(temp));
             taskList.Add(task);
         }
         double sum = 0;
         for (x = (x - interval + precision); x < up; x += precision)
         {
             sum += 2 * function.GetValue(x);
         }
         result += sum;
     }
 }
Esempio n. 3
0
        public ILabel GenerateEpilogue(Function.Function function, Node retVal)
        {
            var operations =
                new List <Node> {
                new Comment("Restore callee saved registers.")
            }
            .Concat(
                this.calleeSavedMapping
                .Select(
                    kvp =>
                    this.readWriteGenerator.GenerateWrite(
                        function,
                        kvp.Key,
                        new RegisterRead(kvp.Value))))
            .Append(new Comment("Save result to RAX"))
            .Append(new RegisterWrite(HardwareRegister.RAX, retVal))
            .Append(new Comment("Restore RSP from RBP"))
            .Append(
                this.readWriteGenerator.GenerateWrite(
                    function,
                    HardwareRegister.RSP,
                    new RegisterRead(HardwareRegister.RBP)))
            .Append(new Comment("Restore RBP from stack"))
            .Append(new Pop(HardwareRegister.RBP))
            .Append(new Comment("Clear direction flag."))
            .Append(new ClearDF())
            .Append(
                new UsesDefinesNode(
                    HardwareRegisterUtils.CalleeSavedRegisters,
                    new List <VirtualRegister> {
                HardwareRegister.RSP
            }));

            return(operations.MakeTreeChain(this.labelFactory, new Ret()));
        }
Esempio n. 4
0
        private double ThreadCalSingle(double x)
        {
            Function.Function function = new Function.Function(funcExp);
            double            temp     = 2 * function.GetValue(x);

            return(temp);
        }
Esempio n. 5
0
 private void ProduceTask(double up, double down,
                          int concurr_num, MinMaxVal result, Function.Function function)
 {
     if (concurr_num >= (int)((up - down) / precision))
     {
         for (double x = down + precision; x < up; x += precision)
         {
             double           temp = x;
             Task <MinMaxVal> task = Task.Run(() => ThreadCalSingle(temp));
             taskList.Add(task);
         }
         result.MinVal = Math.Min(result.MinVal, function.GetValue(up));
         result.MaxVal = Math.Max(result.MaxVal, function.GetValue(up));
     }
     else
     {
         interval = (up - down) / (2 * concurr_num);
         double x = down + interval;
         for (; x < up; x += interval)
         {
             double           temp = x;
             Task <MinMaxVal> task = Task.Run(() => ThreadCalMutiple(temp));
             taskList.Add(task);
         }
         for (x = (x - interval + precision); x < up; x += precision)
         {
             result.MinVal = Math.Min(result.MinVal, function.GetValue(x));
             result.MaxVal = Math.Max(result.MaxVal, function.GetValue(x));
         }
         result.MinVal = Math.Min(result.MinVal, function.GetValue(up));
         result.MaxVal = Math.Max(result.MaxVal, function.GetValue(up));
     }
 }
Esempio n. 6
0
        //梯形逼近法计算函数积分
        public double IntegCal(double up, double down, string exp, int concurr_num = 50000)
        {
            taskList.Clear();
            double sign   = 1;
            double result = 0;

            funcExp = exp;
            Function.Function function = new Function.Function(exp);
            if (up < down)
            {
                double temp = up;
                up   = down;
                down = temp;
                sign = -1;
            }
            result += function.GetValue(up);
            result += function.GetValue(down);

            ProduceTask(up, down, concurr_num, ref result, function);
            Task.WaitAll(taskList.ToArray());
            foreach (Task <double> t in taskList)
            {
                result += t.Result;
            }
            return(sign * result * precision / 2);
        }
Esempio n. 7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void identifyFeatureFunction(java.util.Stack<Object> objects) throws org.maltparser.core.exception.MaltChainedException
        protected internal virtual void identifyFeatureFunction(Stack <object> objects)
        {
            Function.Function function = featureEngine.newFunction(objects.Peek().ToString(), registry);
            if (function != null)
            {
                objects.Pop();
                if (objects.Count > 0)
                {
                    identifyFeatureFunction(objects);
                }
                initializeFunction(function, objects);
            }
            else
            {
                if (objects.Count > 0)
                {
                    object o = objects.Pop();
                    if (objects.Count > 0)
                    {
                        identifyFeatureFunction(objects);
                    }
                    objects.Push(o);
                }
            }
        }
Esempio n. 8
0
        // We will use standard x86-64 conventions -> RDI, RSI, RDX, RCX, R8, R9.
        // TODO: instruction templates covering hw register modifications
        public ILabel GenerateCall(
            ILocation result,
            IEnumerable <VirtualRegister> callArguments,
            ILabel onReturn,
            Function.Function callerFunction,
            Function.Function function)
        {
            var needStackOffset = function.GetStackArgumentsCount() % 2 == 1 ? 8 : 0;
            var preCall         = new List <Node>
            {
                new AlignStackPointer(needStackOffset)
            }
            .Append(new Comment("Pass arguments"))
            .Concat(this.PassArguments(callerFunction, callArguments, function.Parent))
            .Append(new ClearDF())
            .Append(new Comment($"Call {function.MangledName}"))
            .Append(
                new UsesDefinesNode(
                    HardwareRegisterUtils.ArgumentRegisters.Take(callArguments.Count() + 1).ToList(),
                    HardwareRegisterUtils.CallerSavedRegisters));

            var postCall = new List <Node>
            {
                new Comment("Copy function result to variable"),
                this.readWriteGenerator.GenerateWrite(callerFunction, result, new RegisterRead(HardwareRegister.RAX)),
                new Comment("Restore RSP alignment"),
                new AlignStackPointer(-(needStackOffset + (8 * function.GetStackArgumentsCount()))),
                new Comment("End of call"),
            };

            var controlFlow = new FunctionCall(function, postCall.MakeTreeChain(this.labelFactory, onReturn));

            return(preCall.MakeTreeChain(this.labelFactory, controlFlow));
        }
Esempio n. 9
0
        /*
         *          Argument position on wrt. stack frame (if needed):
         |             ...            |
         | (i+7)th argument           | rbp + 16 + 8i
         |             ...            |
         | 7th argument               | rbp + 16
         | return stack pointer value | rbp + 8
         |          rbp -> | previous rbp value         |
         |          Static link is the last argument, either in register or on stack.
         */
        private IEnumerable <Node> PassArguments(
            Function.Function callerFunction,
            IEnumerable <VirtualRegister> argRegisters,
            Function.Function parentFunction)
        {
            Node readStaticLink = null;

            if (parentFunction != null)
            {
                readStaticLink = this.GetClosureForFunction(callerFunction, parentFunction).readLink;
            }

            var values = argRegisters
                         .Select(argVR => (Node) new RegisterRead(argVR)).ToList();

            if (parentFunction != null)
            {
                values.Add(readStaticLink);
            }

            var result = values.Skip(HardwareRegisterUtils.ArgumentRegisters.Count).Reverse().Select(value => (Node) new Push(value)).ToList();

            result.AddRange(values.Zip(HardwareRegisterUtils.ArgumentRegisters, (value, hwReg) => new RegisterWrite(hwReg, value)));
            return(result);
        }
Esempio n. 10
0
        public ILabel AllocateStruct(Function.Function function, StructType structType, ILocation target, ILabel after)
        {
            if (structType.Fields.Count == 0)
            {
                return(after);
            }

            var allocateFunctionName = NameMangler.GetMangledName("allocate", new List <AST.DataType>()
            {
                IntType.Instance
            }, null);

            var allocateFunc = new Function.Function(
                null,
                allocateFunctionName,
                new List <AST.VariableDeclaration>()
            {
                new AST.VariableDeclaration(null, IntType.Instance, "size", null)
            },
                isEntryPoint: false,
                isForeign: true);

            var sizeRegister = new VirtualRegister();

            var call = this.callGenerator.GenerateCall(target, new List <VirtualRegister>()
            {
                sizeRegister
            }, after, callerFunction: function, function: allocateFunc);

            var writeSize  = new RegisterWrite(sizeRegister, new IntegerImmediateValue(structType.Fields.Count * 8));
            var startLabel = this.labelFactory.GetLabel(new Tree(writeSize, new UnconditionalJump(call)));

            return(startLabel);
        }
Esempio n. 11
0
        public double DerivCal(double x, string exp)
        {
            Function.Function function = new Function.Function(exp);
            double            up       = function.GetValue(x + precision);
            double            down     = function.GetValue(x - precision);

            return((up - down) / (2 * precision));
        }
Esempio n. 12
0
        private MinMaxVal ThreadCalSingle(double x)
        {
            Function.Function function = new Function.Function(funcExp);
            MinMaxVal         temp     = new MinMaxVal();

            temp.MaxVal = function.GetValue(x);
            temp.MinVal = temp.MaxVal;
            return(temp);
        }
Esempio n. 13
0
 public ILabel BuildFunctionBody(Function.Function function, AST.InstructionBlock body)
 {
     return(new GenerateProcess(
                this.labelFactory,
                this.readWriteGenerator,
                this.prologueEpilogueGenerator,
                this.callGenerator,
                function)
            .BuildFunctionBody(body));
 }
Esempio n. 14
0
        private List <Node> GetArgumentSources(Function.Function function)
        {
            var registerArguments = HardwareRegisterUtils
                                    .ArgumentRegisters
                                    .Select(reg => new RegisterRead(reg));

            var memoryArguments = Enumerable.Range(0, function.GetStackArgumentsCount())
                                  .Select(n => (Node) new MemoryRead(HardwareRegister.RBP.OffsetAddress(n + 2)));

            return(registerArguments.Concat(memoryArguments).ToList());
        }
Esempio n. 15
0
        public Function.Function GetCallingSibling(Function.Function caller, Function.Function parent)
        {
            var result = caller;

            while (result.Parent != parent)
            {
                result = result.Parent;
            }

            return(result);
        }
Esempio n. 16
0
        private double ThreadCalMutiple(double x)
        {
            Function.Function function = new Function.Function(funcExp);
            double            temp     = 0;

            for (double t = 0; t < interval; t += precision)
            {
                temp += 2 * function.GetValue(x - t);
            }
            return(temp);
        }
Esempio n. 17
0
        private List <ILocation> GetArgumentTargets(Function.Function function)
        {
            List <ILocation> arguments = function.Parameters
                                         .Select(parameter => parameter.IntermediateVariable).ToList();

            if (function.Parent != null)
            {
                arguments.Add(function.Link);
            }

            return(arguments);
        }
Esempio n. 18
0
        private Node GenerateStackVariableLocation(Function.Function function, MemoryLocation loc)
        {
            if (loc.Function == function)
            {
                return(new ArithmeticBinaryOperation(
                           AST.ArithmeticOperationType.Addition,
                           new RegisterRead(HardwareRegister.RBP),
                           new IntegerImmediateValue(loc.Offset)));
            }

            throw new ArgumentException($"Attempt to access non-local stack variable of {function}");
        }
Esempio n. 19
0
 public (Node readLink, StructType linkType) GetClosureForFunction(Function.Function callerFunction, Function.Function parentFunction)
 {
     if (callerFunction == parentFunction)
     {
         return(this.readWriteGenerator.GenerateRead(callerFunction, callerFunction.ClosurePointer), parentFunction.ClosureType);
     }
     else
     {
         var sibling = this.callingSiblingFinder.GetCallingSibling(callerFunction, parentFunction);
         return(this.readWriteGenerator.GenerateRead(callerFunction, sibling.Link), parentFunction.ClosureType);
     }
 }
Esempio n. 20
0
 public GenerateProcess(
     ILabelFactory labelFactory,
     ReadWriteGenerator readWriteGenerator,
     PrologueEpilogueGenerator prologueEpilogueGenerator,
     CallGenerator callGenerator,
     Function.Function function)
 {
     this.labelFactory              = labelFactory;
     this.readWriteGenerator        = readWriteGenerator;
     this.prologueEpilogueGenerator = prologueEpilogueGenerator;
     this.callGenerator             = callGenerator;
     this.function = function;
 }
Esempio n. 21
0
        private MinMaxVal ThreadCalMutiple(double x)
        {
            Function.Function function = new Function.Function(funcExp);
            MinMaxVal         temp     = new MinMaxVal();

            temp.MaxVal = function.GetValue(x);
            temp.MinVal = temp.MaxVal;
            for (double t = precision; t < interval; t += precision)
            {
                double result = function.GetValue(x);
                temp.MaxVal = Math.Max(temp.MaxVal, result);
                temp.MinVal = Math.Min(temp.MinVal, result);
            }
            return(temp);
        }
Esempio n. 22
0
        private void Button_confirm_3D_Click(object sender, EventArgs e)
        {
            string exp = textBox_exp_3D.Text;

            try
            {
                Function.Function func = new Function.Function(exp);
                using (Paint3DForm form = new Paint3DForm(exp, func.GetValue))
                {
                    form.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 23
0
        private Node GenerateWrite(Function.Function function, ILocation variable, Node value, Node closurePointer)
        {
            switch (variable)
            {
            case VirtualRegister virtualRegister:
                return(new RegisterWrite(virtualRegister, value));

            case MemoryLocation location:
                return(new MemoryWrite(this.GenerateStackVariableLocation(function, location), value));

            case HeapLocation heapLocation:
                var address = this.GenerateHeapVariableLocation(function, heapLocation, closurePointer);
                return(new MemoryWrite(address, value));

            default:
                throw new ArgumentException($"Unexpected Location kind {variable}");
            }
        }
Esempio n. 24
0
        private Node GenerateHeapVariableLocation(Function.Function function, HeapLocation loc, Node closurePointer)
        {
            if (loc.Function == function)
            {
                return(new ArithmeticBinaryOperation(
                           AST.ArithmeticOperationType.Addition,
                           closurePointer,
                           new IntegerImmediateValue(loc.Offset)));
            }

            if (function.Parent == null)
            {
                throw new ArgumentException("Variable not found in parents chain");
            }

            var parentClosurePointer = this.GenerateRead(function, function.Link, closurePointer);

            return(this.GenerateHeapVariableLocation(function.Parent, loc, parentClosurePointer));
        }
Esempio n. 25
0
 private void btnAnswer_Click(object sender, EventArgs e)
 {
     try
     {
         if (HandleEquation.IsGeneralOp(equation.Last()))
         {
             MessageBox.Show("请在算式最后输入参与计算的数字!");
         }
         else
         {
             Function.Function func = new Function.Function(equation);
             equation = richTxtEquation.Text = func.GetValue().ToString();
         }
     }
     catch
     {
         MessageBox.Show("不支持计算此算式,请重新输入!");
     }
 }
Esempio n. 26
0
        private Node GenerateRead(Function.Function function, ILocation variable, Node closurePointer)
        {
            switch (variable)
            {
            case VirtualRegister virtualRegister:
                return(new RegisterRead(virtualRegister));

            case MemoryLocation memoryLocation:
                var stackAddress = this.GenerateStackVariableLocation(function, memoryLocation);
                return(new MemoryRead(stackAddress));

            case HeapLocation heapLocation:
                var heapAddress = this.GenerateHeapVariableLocation(function, heapLocation, closurePointer);
                return(new MemoryRead(heapAddress));

            default:
                throw new ArgumentException($"Unexpected Location kind {variable}");
            }
        }
Esempio n. 27
0
        private void Button_draw_3D_Click(object sender, EventArgs e)
        {
            string exp = textBox_exp_3D.Text;

            if (exp == "")
            {
                MessageBox.Show("请输入表达式。");
                return;
            }
            try
            {
                Function.Function func = new Function.Function(exp);
                Caculate3D = func.GetValue;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            if (MaxX_3D <= MinX_3D || MaxY_3D <= MinY_3D)
            {
                MessageBox.Show("定义域输入不合法。");
                return;
            }
            Bitmap bitmap = new Bitmap(pictureBox_3D.Width, pictureBox_3D.Height);

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                painter3D = new Painter3D(pictureBox_3D, g, pen_3D, Caculate3D, MinX_3D, MaxX_3D, MinY_3D, MaxY_3D, rate);
                try
                {
                    painter3D.Draw();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                pictureBox_3D.Image = bitmap;
            }
        }
Esempio n. 28
0
        public ILabel GenerateClosureCall(
            ILocation result,
            IEnumerable <VirtualRegister> callArguments,
            ILabel onReturn,
            Function.Function callerFunction,
            FunType funType,
            VirtualRegister funPtr)
        {
            var needStackOffset = funType.GetStackArgumentsCount() % 2 == 1 ? 8 : 0;
            var funPtrRead      = new RegisterRead(funPtr);
            var funCodePtr      = new MemoryRead(
                new ArithmeticBinaryOperation(AST.ArithmeticOperationType.Addition, funPtrRead, new IntegerImmediateValue(0)));
            var funClosurePtr = new MemoryRead(
                new ArithmeticBinaryOperation(AST.ArithmeticOperationType.Addition, funPtrRead, new IntegerImmediateValue(16)));
            var preCall = new List <Node>
            {
                new AlignStackPointer(needStackOffset)
            }
            .Append(new Comment("Pass arguments"))
            .Concat(this.PassClosureArguments(callArguments, funClosurePtr))
            .Append(new ClearDF())
            .Append(new Comment($"Call closure function"))
            .Append(
                new UsesDefinesNode(
                    HardwareRegisterUtils.ArgumentRegisters.Take(callArguments.Count() + 1).ToList(),
                    HardwareRegisterUtils.CallerSavedRegisters))
            .Append(new RegisterWrite(HardwareRegister.RAX, funCodePtr));

            var postCall = new List <Node>
            {
                new Comment("Copy function result to variable"),
                this.readWriteGenerator.GenerateWrite(callerFunction, result, new RegisterRead(HardwareRegister.RAX)),
                new Comment("Restore RSP alignment"),
                new AlignStackPointer(-(needStackOffset + (8 * funType.GetStackArgumentsCount()))),
                new Comment("End of call"),
            };

            var controlFlow = new ComputedFunctionCall(postCall.MakeTreeChain(this.labelFactory, onReturn));

            return(preCall.MakeTreeChain(this.labelFactory, controlFlow));
        }
Esempio n. 29
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public org.maltparser.core.feature.function.Function newFunction(String functionName, org.maltparser.core.feature.FeatureRegistry registry) throws org.maltparser.core.exception.MaltChainedException
        public virtual Function.Function newFunction(string functionName, FeatureRegistry registry)
        {
            int i = 0;

            Function.Function func = null;
            while (true)
            {
                FunctionDescription funcDesc = get(functionName + "~~" + i);
                if (funcDesc == null)
                {
                    break;
                }
                func = funcDesc.newFunction(registry);
                if (func != null)
                {
                    break;
                }
                i++;
            }
            return(func);
        }
Esempio n. 30
0
 public FeatureValue(Function.Function function) : base(function)
 {
     NullValue = true;
 }