Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs @ 5991

Last change on this file since 5991 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 3.9 KB
RevLine 
[3294]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3294]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[4068]23using System.Collections.Generic;
[3294]24using System.Linq;
25
[5499]26namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[3294]27  public class SymbolicExpressionTreeCompiler {
[3462]28    private Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
29    private List<Func<Instruction, Instruction>> postInstructionCompiledHooks = new List<Func<Instruction, Instruction>>();
[3294]30
[5571]31    public Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
[3294]32      List<Instruction> code = new List<Instruction>();
33      entryPoint.Clear();
[4022]34      // compile main body branches
[5733]35      foreach (var branch in tree.Root.GetSubtree(0).Subtrees) {
[4022]36        code.AddRange(Compile(branch, opCodeMapper));
[4068]37      }
[4022]38      // compile function branches
[3294]39      var functionBranches = from node in tree.IterateNodesPrefix()
40                             where node.Symbol is Defun
41                             select node;
42      foreach (DefunTreeNode branch in functionBranches) {
[3462]43        if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue).");
44        entryPoint[branch.FunctionName] = (ushort)code.Count;
[5733]45        code.AddRange(Compile(branch.GetSubtree(0), opCodeMapper));
[3294]46      }
[3442]47      // address of all functions is fixed now
48      // iterate through code again and fill in the jump locations
49      for (int i = 0; i < code.Count; i++) {
50        Instruction instr = code[i];
[3462]51        if (instr.dynamicNode.Symbol is InvokeFunction) {
[3442]52          var invokeNode = (InvokeFunctionTreeNode)instr.dynamicNode;
53          instr.iArg0 = entryPoint[invokeNode.Symbol.FunctionName];
54          code[i] = instr;
55        }
56      }
57
[3294]58      return code.ToArray();
59    }
60
[5499]61    private IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
[3462]62      foreach (var node in branch.IterateNodesPrefix()) {
[3294]63        Instruction instr = new Instruction();
[5733]64        int subtreesCount = node.SubtreesCount;
65        if (subtreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
66        instr.nArguments = (byte)subtreesCount;
[3462]67        instr.opCode = opCodeMapper(node);
[3747]68        if (node.Symbol is Argument) {
[3462]69          var argNode = (ArgumentTreeNode)node;
70          instr.iArg0 = (ushort)argNode.Symbol.ArgumentIndex;
[3294]71        }
[3462]72        instr.dynamicNode = node;
73        foreach (var hook in postInstructionCompiledHooks) {
74          instr = hook(instr);
75        }
[3294]76        yield return instr;
77      }
78    }
79
[3462]80    /// <summary>
81    /// Adds a function that will be called every time an instruction is compiled.
82    /// The compiled will insert the instruction returned by the hook into the code.
83    /// </summary>
84    /// <param name="hook">The hook that should be called for each compiled instruction.</param>
85    public void AddInstructionPostProcessingHook(Func<Instruction, Instruction> hook) {
86      postInstructionCompiledHooks.Add(hook);
[3294]87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.