Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class MaximalMunch
- {
- Scanner reader;
- Vector<Node> nodes;
- Stack<String> instructions;
- Node root;
- //this is the list of subtree roots to evaluate
- Stack<Node> subtree_nodes;
- public MaximalMunch (String filename)
- {
- try
- {
- reader = new Scanner (new File (filename));
- }
- catch (Exception f)
- {
- System.out.println("File not found");
- System.exit(1);
- }
- nodes = new Vector<Node> ();
- subtree_nodes = new Stack<Node> ();
- read();
- assemble();
- }
- //read in the nodes
- public void read ()
- {
- while (reader.hasNext())
- {
- String line = reader.next();
- int n = 0;
- //count how deep the node is
- for (int i = 0; i < line.length(); i++)
- {
- if (line.charAt(i) == '=')
- n++;
- else
- break;
- }
- nodes.add (new Node (line.substring(n), n));
- }
- }
- //hurp, build tree
- public void assemble ()
- {
- root = nodes.remove(0);
- Node current = nodes.remove(0);
- while (nodes.size() > 0)
- {
- Node inspect = nodes.remove(0);
- //check if need to backtrack up tree
- if (inspect.nest < current.nest)
- {
- for (int i = 0; i < current.nest - inspect.nest+1; i++)
- current = current.parent;
- current.addChild(inspect);
- current = inspect;
- }
- else
- current.addChild(inspect);
- }
- }
- //match LOAD
- public boolean match_load (Node root)
- {
- boolean match = false;
- if (root.value.equals("MEM") && root.children.size() > 0)
- {
- //check for ADDI matches
- if (root.children.get(0).value.equals("CONST"))
- {
- subtree_nodes.push(root.children.get(1));
- instructions.push("LOAD r<- M[r + " + root.children.get(0).children.get(0).value + "]");
- }
- else if (root.children.get(1).value.equals("CONST"))
- {
- subtree_nodes.push(root.children.get(0));
- instructions.push("LOAD r<- M[r + " + root.children.get(1).children.get(1).value + "]");
- }
- }
- return match;
- }
- //tries to match store tiles
- public boolean match_store (Node root)
- {
- boolean match = false;
- if (root.value.equals("MOVE"))
- {
- if (root.children.get(0).value.equals("MEM"))
- {
- if (
- subtree_nodes.push(root.children.get(0));
- instructions.push("STORE M[r+" + "<-r");
- }
- else if (root.children.get(1).value.equals("MEM"))
- {
- subtree_nodes.push(root.children.get(0));
- instructions.push("STORE M[r+" + "<-r");
- }
- }
- return match;
- }
- //tries to match arithmetic operations ( + * - / )
- public boolean match_arith (Node root)
- {
- boolean match = false;
- if (root.children.size() > 0)
- {
- //check for operator
- if (root.children.get(0).value.equals("+"))
- {
- if (root.children.get(0).children.size() == 2)
- match = true;
- subtree_nodes.push(root.children.get(0));
- instructions.push("ADD r<- M[r+r]");
- }
- else if (root.children.get(0).value.equals("-"))
- {
- if (root.children.get(0).children.size() == 2)
- match = true;
- subtree_nodes.push(root.children.get(0));
- instructions.push("SUB r<- M[r-r]");
- }
- else if (root.children.get(0).value.equals("*"))
- {
- if (root.children.get(0).children.size() == 2)
- match = true;
- subtree_nodes.push(root.children.get(0));
- instructions.push("MUL r<- M[rxr]");
- }
- else if (root.children.get(0).value.equals("/"))
- {
- if (root.children.get(0).children.size() == 2)
- match = true;
- subtree_nodes.push(root.children.get(0));
- instructions.push("DIV r<- M[r/r]");
- }
- }
- //push the subtree nodes into the list of subtrees to evaluate
- subtree_nodes.push(root.children.get(0));
- subtree_nodes.push(root.children.get(1));
- }
- return match;
- }
- //match ADDI
- public boolean match_addi (Node root)
- {
- boolean match = false;
- if (root.value.equals("+") && root.children.size() == 2)
- {
- if (root.children.get(0).value.equals("CONST"))
- {
- subtree_nodes.push(root.children.get(1));
- }
- else if (root.children.get(1).value.equals("CONST"))
- {
- subtree_nodes.push(root.children.get(0));
- }
- }
- return match;
- }
- //match CONST
- public boolean match_const (Node root)
- {
- boolean match = false;
- if (root.value.equals("CONST"))
- match = true;
- return match;
- }
- //match SUBI
- public boolean match_subi (Node root)
- {
- boolean match = false;
- if (root.value.equals("-") && root.children.size() == 2)
- {
- if (root.children.get(1).value.equals("CONST"))
- {
- match = true;
- subtree_nodes.push(root.children.get(0));
- }
- }
- return match;
- }
- //match MOVEM
- public boolean match_movem (Node root)
- {
- boolean match = false;
- if (root.value.equals("MOVE") && root.children.size() == 2)
- {
- if (root.children.get(0).equals("MEM") && root.children.get(1).equals("MEM") && root.children.get(0).children.size() > 0 && root.children.get(1).children.size() > 0)
- {
- match = true;
- subtree_nodes.push(root.children.get(0));
- subtree_nodes.push(root.children.get(1));
- }
- }
- return match;
- }
- //matches tiles
- public void munch ()
- {
- subtree_nodes.push(root);
- //tries to match tiles in order of biggest to smallest
- while (!subtree_nodes.isEmpty())
- {
- Node current = subtree_nodes.pop();
- if (match_store(current))
- {
- }
- else if (match_load(current))
- {
- }
- else if (match_arith(current))
- {
- }
- else if (match_movem(current))
- {
- }
- else if (match_addi(current))
- {
- }
- else if (match_subi(current))
- {
- }
- else
- {
- }
- }
- }
- //write the stuff to file
- public void write ()
- {
- }
- public static void main (String[] args)
- {
- MaximalMunch tree = new MaximalMunch(args[0]);
- }
- //IR node class
- public class Node
- {
- public String value;
- public Node parent;
- public Vector<Node> children;
- public int nest;
- public Node (String v, int n)
- {
- value = v;
- nest = n;
- children = new Vector<Node>();
- }
- public Node (Node p, String v, int n)
- {
- parent = p;
- value = v;
- nest = n;
- children = new Vector<Node>();
- }
- public void addChild (Node n)
- {
- n.parent = this;
- children.add(n);
- }
- }
- }
Add Comment
Please, Sign In to add comment