<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to PatternRuleGuide</title><link>https://sourceforge.net/p/jburg/wiki/PatternRuleGuide/</link><description>Recent changes to PatternRuleGuide</description><atom:link href="https://sourceforge.net/p/jburg/wiki/PatternRuleGuide/feed" rel="self"/><language>en</language><lastBuildDate>Wed, 20 Mar 2013 20:09:04 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/jburg/wiki/PatternRuleGuide/feed" rel="self" type="application/rss+xml"/><item><title>WikiPage PatternRuleGuide modified by Tom Harwood</title><link>https://sourceforge.net/p/jburg/wiki/PatternRuleGuide/</link><description>&lt;div class="markdown_content"&gt;&lt;h1 id="pattern-matching-rules"&gt;Pattern-Matching Rules&lt;/h1&gt;
&lt;p&gt;All subtree rewrites start with a pattern match.  Pattern-matching rules are syntactically recognizable by their format:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;goalState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NODE_TYPE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subgoal&lt;/span&gt; &lt;span class="n"&gt;subtree&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The key feature that distinguishes a pattern-matching rule from other rules is the &lt;code&gt;(subgoal subtree)&lt;/code&gt; section. All pattern-matching rules have this; pattern matches at leaf nodes specify their (non-existent) subtrees' goals as &lt;code&gt;(void)&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="goal-aka-nonterminal-states"&gt;Goal (a.k.a. nonterminal) States&lt;/h2&gt;
&lt;p&gt;Every rewrite is done to produce a new kind of result; these results are grouped into classes known as &lt;em&gt;goal states&lt;/em&gt; or &lt;em&gt;nonterminal&lt;/em&gt; states. What a particular goal state means is up to you, as the programmer of the pattern matcher; the pattern matcher does not know anything about a goal state's meaning.&lt;/p&gt;
&lt;p&gt;Examples of goal states typically found in a code generator:&lt;br /&gt;
&lt;em&gt; &lt;code&gt;statement&lt;/code&gt;&lt;br /&gt;
&lt;/em&gt; &lt;code&gt;intExpression&lt;/code&gt;&lt;br /&gt;
&lt;em&gt; &lt;code&gt;intConstant&lt;/code&gt;&lt;br /&gt;
&lt;/em&gt; &lt;code&gt;stringConstant&lt;/code&gt;&lt;/p&gt;
&lt;h2 id="subtrees-and-subgoals"&gt;Subtrees and Subgoals&lt;/h2&gt;
&lt;p&gt;Any node that has children must match a pattern that has feasible and sensible goals for all the children.  For example, an ADD node generally does single-mode arithmetic:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ADD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This pattern will match a node whose operator code is ADD, and which has two children, both of which can be reduced to &lt;code&gt;intExpression&lt;/code&gt; results.  The result of applying this rule will itself be an &lt;code&gt;intExpression&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The subtrees' subgoals affect their parent's pattern match. For example, suppose we have two rules to add integers, one of which is an add immediate:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ADD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;cost1&lt;/span&gt;
&lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ADD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intExpression&lt;/span&gt; &lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intConstant&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;cost2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If a node's second subtree can be reduced to an &lt;code&gt;intConstant&lt;/code&gt; and the constant is in the range of the target's add immediate range, then the code generator can be programmed to prefer a reduction that emits an &lt;code&gt;addi&lt;/code&gt; instruction instead of an &lt;code&gt;add&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="cost-factors"&gt;Cost Factors&lt;/h2&gt;
&lt;p&gt;How does the code generator select the optimal rule? It totals up the cost factors for each viable alternative, including the cost of the subtrees' subgoals, and selects the least-cost alternative. (In case of a tie, the first rule specified wins.) For the example above, there are two ways to give the add immediate variant a lower cost:&lt;br /&gt;
1. Give the second rule a lower cost than the first rule.&lt;br /&gt;
2. Give the rule that converts an &lt;code&gt;intConstant&lt;/code&gt; into an &lt;code&gt;intExpression&lt;/code&gt; a greater aggregate cost.&lt;/p&gt;
&lt;p&gt;In practice, both these techniques are likely to be employed.&lt;/p&gt;
&lt;p&gt;Cost factors can be supplied in several different ways:&lt;br /&gt;
&lt;em&gt; As integer literals, e.g., &lt;code&gt;intExpr = ADD(intExpr terms+): 1&lt;/code&gt;&lt;br /&gt;
&lt;/em&gt; As &lt;code&gt;JBurg.Constant&lt;/code&gt; values, e.g., &lt;code&gt;intExpr = ADD(intExpr terms+): OrdinaryInstruction&lt;/code&gt; where &lt;code&gt;OrdinaryInstruction&lt;/code&gt; has been declared as a JBurg.Constant value in the specification.&lt;br /&gt;
&lt;em&gt; As &lt;em&gt;cost functions&lt;/em&gt;, explained below.&lt;/em&gt; As references to field values or other values in the compiler's host programming language.&lt;/p&gt;
&lt;p&gt;To produce a fast pattern matcher, specify the cost as an int literal or a &lt;code&gt;JBurg.Constant&lt;/code&gt; wherever possible; this allows JBurg to perform more analysis at compiler-compiler time, which can result in dramatically better pattern matchers.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tom Harwood</dc:creator><pubDate>Wed, 20 Mar 2013 20:09:04 -0000</pubDate><guid>https://sourceforge.net213654a64ea7bcbbcaca62f0ef2c4ec10e723bbd</guid></item></channel></rss>