package dspl.backend.x86.operands;

import dspl.intermediate.variable.Variable;

/**
 * 
 * 
 * @author David
 */
public class X86Register extends X86Operand {
	String registerName; 
	/**
	 * Holds the size of the data type currently stored in the register.<br>
	 * Used with toString to return the appriopriate register name
	 */
	int regSize = 4; 
	
	
	public X86Register(String reg) { 
		registerName = reg; 
	}
	
	public void freeRegister() { 
		
	}
	
	public void setRegisterContentsSize(int regSize) { 
		this.regSize = regSize; 
	}
	
	public void setRegisterContentsSize(Variable v) {
		setRegisterContentsSize(v.getDataTypeByteSize()); 
	}
	
	public int getRegisterContentsSize() {
		return regSize; 
	}
	
	public String toString() {
		if (regSize==4) {
			return registerName;
		}
		else if (regSize==1) { //special processing for bytes 
			if (registerName.compareTo("%eax")==0) { 
				return "%al";
			}
			else if (registerName.compareTo("%ebx")==0) { 
				return "%bl";
			}
			else if (registerName.compareTo("%ecx")==0) { 
				return "%cl";
			}
			else if (registerName.compareTo("%edx")==0) { 
				return "%dl";
			}
			throw new Error("invalid register with size 1, only eax, ebx, ecx, and edx support 8bit reg size");
		}
		throw new Error("invalid operand size for register. neither 4 nor 1"); 
	}
	
	public void freeOperand() {
		
	}
	
	public int getOperandType() {
		return X86Operand.OPERAND_TYPE_REGISTER; 
	}
	
	public int getOperandSize() {
		return regSize; 
	}
}
