public BinaryOperation(IROpCodes opcode, VirtualRegister target, GenericOperand left, GenericOperand right) :
			base(opcode, target)
		{
			if (left == null)
				throw new ArgumentNullException("left");
			else
				LeftOperand = left;

			if (right == null)
				throw new ArgumentNullException("right");
			else
				RightOperand = right;
			
			switch (opcode)
			{
			case IROpCodes.ADD:
			case IROpCodes.SUB:
			case IROpCodes.MUL:
			case IROpCodes.DIV:
			case IROpCodes.MIN:
			case IROpCodes.MAX:
				Util.CheckArgumentType(Util.NumericTypes, target.DataType, "target");
				Util.CheckArgumentType(target.DataType, left.DataType, "left");
				Util.CheckArgumentType(target.DataType, right.DataType, "right");
				break;
			case IROpCodes.REM:
				Util.CheckArgumentType(Util.IntegralTypes, target.DataType, "target");
				Util.CheckArgumentType(target.DataType, left.DataType, "left");
				Util.CheckArgumentType(target.DataType, right.DataType, "right");
				break;
			case IROpCodes.AND:
			case IROpCodes.OR:
			case IROpCodes.XOR:
				Util.CheckArgumentType(Util.IntegralTypes, target.DataType, "target");
				Util.CheckArgumentType(target.DataType, left.DataType, "left");
				Util.CheckArgumentType(target.DataType, right.DataType, "right");
				break;
			case IROpCodes.EQ:
			case IROpCodes.NE:
			case IROpCodes.GE:
			case IROpCodes.GT:
			case IROpCodes.LE:
			case IROpCodes.LT:
				Util.CheckArgumentType(typeof(int), target.DataType, "target");
				Util.CheckArgumentType(Util.NumericTypes, left.DataType, "left");
				Util.CheckArgumentType(left.DataType, right.DataType, "right");
				break;
			default:
				throw new InvalidOperationException();
			}
		}
		public MADOperation(VirtualRegister target, GenericOperand mleft, GenericOperand mright, GenericOperand ad) :
			base(IROpCodes.MAD, target)
		{
			if (mleft == null)
				throw new ArgumentNullException("mleft");
			else
				MulLeftOperand = mleft;

			if (mright == null)
				throw new ArgumentNullException("mright");
			else
				MulRightOperand = mright;
			
			if (ad == null)
				throw new ArgumentNullException("addop");
			else
				AddOperand = ad;
			
			Util.CheckArgumentType(Util.NumericTypes, target.DataType, "target");
			Util.CheckArgumentType(target.DataType, mleft.DataType, "mleft");
			Util.CheckArgumentType(target.DataType, mright.DataType, "mright");
			Util.CheckArgumentType(target.DataType, ad.DataType, "ad");
		}
Example #3
0
		private VirtualRegister CalculateAddress(VirtualRegister array, GenericOperand index)
		{
			VirtualRegister address = AllocateRegister(array.UnderlyingType, array.StateSpace);
			index = ConvertOperand(index, array.DataType);
			int elemsize = array.UnderlyingType.SizeOf();
			code.Add(elemsize == 1 ?
				new BinaryOperation(IROpCodes.ADD, address, index, array) as BasicBlockInstruction :
				new MADOperation(address, index, new ImmediateValue(
					(ValueType)Convert.ChangeType(elemsize, array.DataType)), array) as BasicBlockInstruction);
			ReleaseOperand(array);
			ReleaseOperand(index);
			return address;
		}
		public UnaryOperation(IROpCodes opcode, VirtualRegister target, GenericOperand operand) :
			base(opcode, target)
		{
			if (operand == null)
				throw new ArgumentNullException("operand");
			else
				Operand = operand;
			
			switch (opcode)
			{
			case IROpCodes.NEG:
			case IROpCodes.ABS:
				Util.CheckArgumentType(Util.SIntTypes.Concat(Util.RealTypes), target.DataType, "target");
				goto case IROpCodes.MOV;
			case IROpCodes.NOT:
				Util.CheckArgumentType(Util.IntegralTypes, target.DataType, "target");
				goto case IROpCodes.MOV;
			case IROpCodes.SQRT:
			case IROpCodes.RSQRT:
				Util.CheckArgumentType(Util.RealTypes, target.DataType, "target");
				goto case IROpCodes.MOV;
			case IROpCodes.SIN:
			case IROpCodes.COS:
			case IROpCodes.LG2:
			case IROpCodes.EX2:
				Util.CheckArgumentType(typeof(float), target.DataType, "target");
				goto case IROpCodes.MOV;
			case IROpCodes.MOV:
				Util.CheckArgumentType(target.DataType, operand.DataType, "operand");
				break;
			case IROpCodes.CVT:
				if (!(Util.NumericTypes.Contains(operand.DataType) && Util.NumericTypes.Contains(target.DataType)) ||
					target.DataType == operand.DataType)
					throw new InvalidCastException(string.Format("There is no conversion from {0} to {1}",
						operand.DataType.Format(), target.DataType.Format()));
				break;
			default:
				throw new InvalidOperationException();
			}
		}
Example #5
0
		private GenericOperand ConvertOperand(GenericOperand operand, Type type)
		{
			if (operand.DataType != UpconvertMapping[type])
			{
				VirtualRegister target = AllocateRegister(UpconvertMapping[type]);
				code.Add(new UnaryOperation(IROpCodes.CVT, target, operand));
				ReleaseOperand(operand);
				return target;
			}
			return operand;
		}
Example #6
0
		private VirtualRegister MapToRegister(GenericOperand operand)
		{
			if (operand is VirtualRegister)
				return operand as VirtualRegister;
			else
			{
				VirtualRegister reg = AllocateRegister(operand.DataType);
				code.Add(new UnaryOperation(IROpCodes.MOV, reg, operand));
				return reg;
			}
		}
Example #7
0
		private void PushByRef(GenericOperand operand)
		{
			stack.Push(new Tuple<Tuple<GenericOperand, bool>, InstructionSelector>(
				new Tuple<GenericOperand, bool>(operand, true), this));
		}
Example #8
0
		public void AddInitialization(GenericOperand actual, VirtualRegister formal)
		{
			code.Add(new UnaryOperation(IROpCodes.MOV, formal, actual));
			ReleaseOperand(actual);
		}
Example #9
0
		private void ReleaseOperand(GenericOperand operand)
		{
			if (operand is DynamicRegister)
				(operand as DynamicRegister).Live = false;
		}