//------------------------------------------------------------ // 意味解析。 public void SemanticAnalyze(SemanticAnalyzeComponent aComp) { // TypeInfo作成 TypeInfo typeInfo = aComp.CreateTypeInfo(mVariableDecl.TypePath, mIsConst, false); // シンボルノードを作成 VariableSymbolNode symbolNode = new VariableSymbolNode( aComp.PrevSymbolNode() , mVariableDecl.Ident , typeInfo ); aComp.AddSymbolNode(symbolNode); // 評価ノードを作成 var evaluateNode = new EvaluateNode(symbolNode, mVariableDecl.Expression()); // 評価準備 evaluateNode.SendEvent(aComp, EvaluateNodeEventKind.Analyze); // 評価実行 evaluateNode.SendEvent(aComp, EvaluateNodeEventKind.Evaluate); // 親の評価終了 evaluateNode.SendEvent(aComp, EvaluateNodeEventKind.Release); // 割り当て済みシンボルノードとして登録 aComp.AddEvaluatedSymbolNode(new EvaluatedSymbolNode(symbolNode, evaluateNode)); // Scope終了イベントのために追加 aComp.AddEvaluateNode(evaluateNode); }
/** * <summary>Gets the positional information resulting from the collection evaluation.</summary> * <param name="evaluator">Expression used to evaluate the positional matching.</param> */ private int Evaluate( EvaluateNode evaluateNode ) { /* * NOTE: Layer hierarchies are represented through a somewhat flatten structure which needs * to be evaluated in order to match nodes in their actual place. */ PdfArray baseDataObject = BaseDataObject; int nodeIndex = -1; bool groupAllowed = true; for ( int baseIndex = 0, baseLength = base.Count; baseIndex < baseLength; baseIndex++ ) { PdfDataObject itemDataObject = baseDataObject.Resolve(baseIndex); if (itemDataObject is PdfDictionary || (itemDataObject is PdfArray && groupAllowed)) { nodeIndex++; int evaluation = evaluateNode(nodeIndex, baseIndex); if (evaluation > -1) { return(evaluation); } } groupAllowed = !(itemDataObject is PdfDictionary); } return(evaluateNode(nodeIndex, -1)); }
public Dictionary <int, StructuredAlias> PurelyRandom(StructuredAlias realMap, EvaluateNode Eval) { AliasChallengePriorityQueue = new SimplePriorityQueue <Dictionary <int, StructuredAlias> >(); iterationRandomRestart = 0; graphPlot.Clear(); ParameterManager pMan = ParameterManager.Instance; TimeCap = pMan.timeCap; BatchAliases = pMan.aliasNum; System.Diagnostics.Stopwatch sWatch = StopwatchProxy.Instance.Stopwatch; AliasGeneratorManager aGMan = AliasGeneratorManager.Instance; sWatch.Stop(); sWatch.Reset(); sWatch.Start(); int totalIterations = 0; while (totalIterations < randomIterations) { Dictionary <int, StructuredAlias> randomAliases = aGMan.GenerateNRandomAliasFromRealMap(realMap, BatchAliases); try { if (sWatch.ElapsedMilliseconds > pMan.timeCap * 1000f && pMan.timeCap >= 0) { throw new Exception("Time cap elapsed.\n"); } float tmpEval = (float)Eval(realMap, randomAliases); AliasChallengePriorityQueue.Enqueue(randomAliases, -(float)tmpEval); graphPlot.Add(new Tuple <float, float>(iterationRandomRestart, Mathf.Abs((float)tmpEval))); } catch (Exception e) { ErrorManager.ManageError(ErrorManager.Error.SOFT_ERROR, e.Message + sWatch.ElapsedMilliseconds / 1000f + "s #iteration: " + totalIterations + SaveAliasPurelyRandom()); sWatch.Stop(); sWatch.Reset(); return(AliasChallengePriorityQueue.Dequeue()); } totalIterations++; } GeneratorUIManager.Instance.showMessageDialogBox("SIMPLERANDOM-F= " + Mathf.Abs(AliasChallengePriorityQueue.GetPriority(AliasChallengePriorityQueue.First)) + "\nExecution time: " + sWatch.ElapsedMilliseconds / 1000f + "s #iteration: " + totalIterations + SaveAliasPurelyRandom()); sWatch.Stop(); sWatch.Reset(); return(AliasChallengePriorityQueue.Dequeue()); }
//HillClimber improve the evaluation so MAXIMIZE! public Dictionary <int, StructuredAlias> HillClimber(StructuredAlias realMap, EvaluateNode Eval) { Dictionary <int, StructuredAlias> currentNode = new Dictionary <int, StructuredAlias>(); //inizialization: take BATCH_ALIASES random alias currentNode = AliasGeneratorManager.Instance.GenerateNRandomAliasFromRealMap(realMap, ParameterManager.Instance.aliasNum); int iterations = 0; TimeCap = ParameterManager.Instance.timeCap; BatchAliases = ParameterManager.Instance.hillClimberNumBatch; iterationHCSwap = 0; while (true) { Dictionary <int, StructuredAlias> toSwapAliases = AliasGeneratorManager.Instance.GenerateNRandomAliasFromRealMap(realMap, BatchAliases); double maxEval = double.MinValue; Dictionary <int, StructuredAlias> nextNode = null; double currEval = Eval(realMap, currentNode); foreach (var m in currentNode) { foreach (var n in toSwapAliases) { Dictionary <int, StructuredAlias> tmpAliases = new Dictionary <int, StructuredAlias>(currentNode); tmpAliases.Remove(m.Key); tmpAliases.Add(n.Key, n.Value); double tmpEval; try{ tmpEval = Eval(realMap, tmpAliases); } catch (Exception e) { returnIter = iterations; returnEval = currEval; return(currentNode); } if (tmpEval > maxEval) { maxEval = tmpEval; nextNode = tmpAliases; } if (maxEval <= currEval) { graphPlot.Add(new Tuple <float, float>(iterationRandomRestart, Mathf.Abs((float)currEval))); } else { graphPlot.Add(new Tuple <float, float>(iterationRandomRestart, Mathf.Abs((float)maxEval))); } iterationHCSwap++; } } if (maxEval <= currEval) { returnIter = iterations; returnEval = currEval; return(currentNode); } currentNode = nextNode; iterations++; } }
//HillClimber improve the evaluation so MAXIMIZE! public Dictionary <int, StructuredAlias> RandomRestartHillClimber(StructuredAlias realMap, EvaluateNode Eval) { AliasChallengePriorityQueue = new SimplePriorityQueue <Dictionary <int, StructuredAlias> >(); iterationRandomRestart = 0; graphPlot.Clear(); ParameterManager pMan = ParameterManager.Instance; System.Diagnostics.Stopwatch sWatch = StopwatchProxy.Instance.Stopwatch; sWatch.Stop(); sWatch.Reset(); sWatch.Start(); int totalIterations = 0; while (iterationRandomRestart < maxIterations) { try { if (sWatch.ElapsedMilliseconds > pMan.timeCap * 1000f && pMan.timeCap >= 0) { throw new Exception("Time cap elapsed.\n"); } AliasChallengePriorityQueue.Enqueue(HillClimber(realMap, Eval), -(float)returnEval); } catch (Exception e) { ErrorManager.ManageError(ErrorManager.Error.SOFT_ERROR, e.Message + sWatch.ElapsedMilliseconds / 1000f + "s #iteration: " + iterationRandomRestart + " (" + totalIterations + returnIter + ")." + SaveAliasChallengeOptimization()); sWatch.Stop(); sWatch.Reset(); return(AliasChallengePriorityQueue.Dequeue()); } totalIterations += returnIter; iterationRandomRestart++; } GeneratorUIManager.Instance.showMessageDialogBox("F= " + Mathf.Abs(AliasChallengePriorityQueue.GetPriority(AliasChallengePriorityQueue.First)) + "\nExecution time: " + sWatch.ElapsedMilliseconds / 1000f + "s #iteration: " + iterationRandomRestart + " (" + maxIterations * pMan.hillClimberNumBatch + ")." + SaveAliasChallengeOptimization()); sWatch.Stop(); sWatch.Reset(); return(AliasChallengePriorityQueue.Dequeue()); }
public Dictionary <int, StructuredAlias> StochasticFirstChoiceHillClimber(StructuredAlias realMap, EvaluateNode Eval) { Dictionary <int, StructuredAlias> currentNode = new Dictionary <int, StructuredAlias>(); //inizialization: take BATCH_ALIASES random alias currentNode = AliasGeneratorManager.Instance.GenerateNRandomAliasFromRealMap(realMap, ParameterManager.Instance.aliasNum); int iterations = 0; TimeCap = ParameterManager.Instance.timeCap; StopwatchProxy.Instance.Stopwatch.Start(); while (iterations < maxIterations) { Tuple <int, StructuredAlias> toSwapAlias = AliasGeneratorManager.Instance.Generate1RandomAliasFromRealMap(realMap); double maxEval = double.MinValue; Dictionary <int, StructuredAlias> nextNode = null; double currEval = Eval(realMap, currentNode); foreach (var m in currentNode) { Dictionary <int, StructuredAlias> tmpAliases = new Dictionary <int, StructuredAlias>(currentNode); tmpAliases.Remove(m.Key); tmpAliases.Add(toSwapAlias.Item1, toSwapAlias.Item2); double tmpEval; try { tmpEval = Eval(realMap, tmpAliases); } catch (Exception e) { ErrorManager.ManageError(ErrorManager.Error.SOFT_ERROR, e.Message + StopwatchProxy.Instance.Stopwatch.ElapsedMilliseconds / 1000f + "s #iteration: " + iterations + SaveAliasChallengeOptimization()); StopwatchProxy.Instance.Stopwatch.Stop(); StopwatchProxy.Instance.Stopwatch.Reset(); returnEval = currEval; return(currentNode); } if (tmpEval > maxEval) { maxEval = tmpEval; nextNode = tmpAliases; } } if (maxEval <= currEval) { returnEval = currEval; } else { returnEval = maxEval; currentNode = nextNode; } iterations++; } GeneratorUIManager.Instance.showMessageDialogBox("Execution time: " + StopwatchProxy.Instance.Stopwatch.ElapsedMilliseconds / 1000f + "s #iteration: " + iterations + SaveAliasChallengeOptimization()); StopwatchProxy.Instance.Stopwatch.Stop(); StopwatchProxy.Instance.Stopwatch.Reset(); return(currentNode); }
//------------------------------------------------------------ // シンボルを展開する。 public void SymbolExpand(SymbolExpandCmdKind aCmdKind) { if (aCmdKind != SymbolExpandCmdKind.FunctionNodeImpl) {// 関数実装以外なら何もしない return; } // BCFunction作成 mBCFunction = mBCObjectType.GenerateFunction(this); // コンポーネント SemanticAnalyzeComponent comp = new SemanticAnalyzeComponent( mBCFunction , mParent , this ); // コピーするレジスタの数をメモする変数 byte copyRegCount = 0; {// 関数内部のスコープ // スコープ追加 comp.ScopeEnter(); // 戻り値対応 if (mReturnTypeInfo.Symbol.GetKind() != TypeInfo.TypeSymbol.Kind.BuiltIn || mReturnTypeInfo.Symbol.GetBuiltInType() != BuiltInType.Void ) {// void以外なら。 // todo: いろんな型の対応 // 組み込み型のint,boolしか対応していません。 if (mReturnTypeInfo.Symbol.GetKind() != TypeInfo.TypeSymbol.Kind.BuiltIn || (mReturnTypeInfo.Symbol.GetBuiltInType() != BuiltInType.SInt32 && mReturnTypeInfo.Symbol.GetBuiltInType() != BuiltInType.Bool) ) { comp.ThrowErrorException(SymbolTree.ErrorKind.NOT_SUPPORTED_TYPENAME, mReturnTypeInfo.Symbol.GetToken()); } // EI作成 var returnEI = EvaluateInfo.CreateAsValue(mReturnTypeInfo); // SRを確保 returnEI.SR = comp.SRReserve(); // 戻り値として登録 comp.ReturnEvaluateInfoSet(returnEI); // コピーするレジスタカウントアップ ++copyRegCount; } // this if (!mFunctionDecl.IsStatic()) { // TypeInfo作成 var ti = comp.CreateTypeInfo( new TypePath(new IdentPath(mParent)) , mFunctionDecl.IsConst() , true ); // ダミートークンを作成 var token = new Token(); token.Value = Token.Kind.KeyThis; token.pos = GetIdentifier().Token.pos; token.posColumn = GetIdentifier().Token.posColumn; token.posLine = GetIdentifier().Token.posLine; // シンボルノードを作成 VariableSymbolNode symbolNode = new VariableSymbolNode( comp.PrevSymbolNode() , new Identifier(token) , ti ); // ノードを追加 comp.AddSymbolNode(symbolNode); // 評価ノードを作成 var evaluateNode = new EvaluateNode(symbolNode); // 評価イベント送信 evaluateNode.SendEvent(comp, EvaluateNodeEventKind.Analyze); evaluateNode.SendEvent(comp, EvaluateNodeEventKind.Evaluate); // 追加 comp.AddEvaluatedSymbolNode(new EvaluatedSymbolNode(symbolNode, evaluateNode)); // コピーするレジスタカウントアップ ++copyRegCount; } // 引数 foreach (var arg in mArgTypeInfos) { // シンボルノードを作成 VariableSymbolNode symbolNode = new VariableSymbolNode( comp.PrevSymbolNode() , arg.Ident , arg.TypeInfo ); // ノードを追加 comp.AddSymbolNode(symbolNode); // 評価ノードを作成 var evaluateNode = new EvaluateNode(symbolNode); // 評価イベント送信 evaluateNode.SendEvent(comp, EvaluateNodeEventKind.Analyze); evaluateNode.SendEvent(comp, EvaluateNodeEventKind.Evaluate); // 追加 comp.AddEvaluatedSymbolNode(new EvaluatedSymbolNode(symbolNode, evaluateNode)); // コピーするレジスタカウントアップ ++copyRegCount; } {// Statement // スコープに入る comp.ScopeEnter(); // Returnラベル確保・登録 BCLabel labelReturn = comp.BCFunction.LabelCreate(); comp.RegisterLabelReturn(labelReturn); // 解析 mFunctionDecl.Statement().SemanticAnalyze(comp); // スコープから出る comp.ScopeLeave(); // Returnラベル挿入 comp.BCFunction.LabelInsert(labelReturn); } // スコープ終了 comp.ScopeLeave(); } // 関数命令を追加 // todo: レジスタ使いすぎチェック mBCFunction.PushFrontOPCode_CU1_CU1(BCOpCode.OpType.FENTER, (byte)comp.SRPeakCount(), copyRegCount); mBCFunction.AddOPCode_CU1(BCOpCode.OpType.FLEAVE, (byte)comp.SRPeakCount()); // ラベル解決 mBCFunction.LabelResolve(); }