All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.luaj.vm2.luajc.BasicBlock Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
/**
 * 
 */
package org.luaj.vm2.luajc;

import java.util.Vector;

import org.luaj.vm2.Lua;
import org.luaj.vm2.Prototype;

public class BasicBlock {
	int pc0,pc1;        // range of program counter values for the block
	BasicBlock[] prev;  // previous basic blocks (0-n of these)
	BasicBlock[] next;  // next basic blocks (0, 1, or 2 of these)
	boolean islive;     // true if this block is used
	
	public BasicBlock(Prototype p, int pc0) {
		this.pc0 = this.pc1 = pc0;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();		
		sb.append( (pc0+1)+"-"+(pc1+1)
				+(prev!=null? "  prv: "+str(prev,1): "")
				+(next!=null? "  nxt: "+str(next,0): "")
				+"\n" );
		return sb.toString();
	}
	
	private String str(BasicBlock[] b, int p) {
		if ( b == null )
			return "";
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		for ( int i=0, n=b.length; i 0 )
				sb.append( "," );
			sb.append( String.valueOf( p==1? b[i].pc1+1: b[i].pc0+1 ) );
		}
		sb.append(")");
		return sb.toString();
	}

	public static BasicBlock[] findBasicBlocks(Prototype p) {
		
		// mark beginnings, endings
		final int n = p.code.length;
		final boolean[] isbeg = new boolean[n];
		final boolean[] isend = new boolean[n];
		isbeg[0] = true;
		BranchVisitor bv = new BranchVisitor(isbeg) {
			public void visitBranch(int pc0, int pc1) {
				isend[pc0] = true;
				isbeg[pc1] = true;
			}
			public void visitReturn(int pc) {
				isend[pc] = true;
			}
		};
		visitBranches(p, bv); // 1st time to mark branches
		visitBranches(p, bv); // 2nd time to catch merges
			
		// create basic blocks
		final BasicBlock[] blocks = new BasicBlock[n];
		for ( int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy