package org.spbu.bdd.samples.schedule;

public class Instruction {
	public static enum InstrType {MUL, ADD, STORE, LOAD, NOP, FIN};
	static int[] latencies = {2,1,2,2,1,0};
	
	private String str;
	private InstrType type;
	private int latency;
	//mem[0]         - is written by the instruction
	//mem[1..size-1] - are read by the instruction
	private String[] mem;

	public String getStr() {
		return str;
	}

	public InstrType getType() {
		return type;
	}

	public String[] getMem() {
		return mem;
	}
	
	public int getLatency() {
		return latency;
	}

	//str must be valid instruction!
	public Instruction(String str) {
		this.str = str;
		String[] tmp = str.split("\\s");
		if (tmp[0].equals("nop")) {
			type=InstrType.NOP;
			mem=null;
		} else if (tmp[0].equals("fin")) {
			type=InstrType.FIN;
			mem=null;
		} else if (tmp[0].equals("store")) {
			type=InstrType.STORE;
			mem=new String[2];
			//store [to] [from]
			mem[0]=tmp[1];
			mem[1]=tmp[2];
		} else if (tmp[0].equals("load")) {
			type=InstrType.LOAD;
			mem=new String[2];
			//load [to] [from]
			mem[0]=tmp[1];
			mem[1]=tmp[2];
		} else if (tmp[3].equals("*")) {
			type=InstrType.MUL;
			mem=new String[3];
			//[to] := [from] * [from]
			mem[0]=tmp[0];
			mem[1]=tmp[2];
			mem[2]=tmp[4];
		} else if (tmp[3].equals("+")) {
			type=InstrType.ADD;
			mem=new String[3];
			//[to] := [from] + [from]
			mem[0]=tmp[0];
			mem[1]=tmp[2];
			mem[2]=tmp[4];
		}
		latency=latencies[type.ordinal()];
	}
}
