public static LLLiteral Create(LLType pType, string pValue) { LLLiteral literal = new LLLiteral(); literal.mType = pType; literal.mValue = pValue; return literal; }
public static LLLiteral Create(LLType pType, string pValue) { LLLiteral literal = new LLLiteral(); literal.mType = pType; literal.mValue = pValue; return(literal); }
public LLLocation EmitConversion(LLLocation pSource, LLType pType) { if (pSource.Type == pType) { return(pSource); } LLLocation destination = pSource; switch (pSource.Type.Primitive) { case LLPrimitive.Signed: { switch (pType.Primitive) { case LLPrimitive.Signed: // Signed to Signed { if (pSource.Type.SizeInBits != pType.SizeInBits) { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); } if (pSource.Type.SizeInBits > pType.SizeInBits) { EmitTruncate(destination, pSource); } else if (pSource.Type.SizeInBits < pType.SizeInBits) { EmitExtend(destination, pSource); } break; } case LLPrimitive.Unsigned: // Signed to Unsigned { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); if (pSource.Type.SizeInBits > pType.SizeInBits) { EmitTruncate(destination, pSource); } else if (pSource.Type.SizeInBits < pType.SizeInBits) { EmitExtend(destination, pSource); } else { EmitAdd(destination, pSource, LLLiteralLocation.Create(LLLiteral.Create(pType, "0"))); } break; } case LLPrimitive.Float: // Signed to Float { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitIntegerToFloat(destination, pSource); break; } case LLPrimitive.Pointer: // Signed to Pointer { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitIntegerToPointer(destination, pSource); break; } default: throw new NotSupportedException(); } break; } case LLPrimitive.Unsigned: { switch (pType.Primitive) { case LLPrimitive.Signed: // Unsigned to Signed { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); if (pSource.Type.SizeInBits > pType.SizeInBits) { EmitTruncate(destination, pSource); } else if (pSource.Type.SizeInBits < pType.SizeInBits) { EmitExtend(destination, pSource); } else { EmitAdd(destination, pSource, LLLiteralLocation.Create(LLLiteral.Create(pType, "0"))); } break; } case LLPrimitive.Unsigned: // Unsigned to Unsigned { if (pSource.Type.SizeInBits != pType.SizeInBits) { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); } if (pSource.Type.SizeInBits > pType.SizeInBits) { EmitTruncate(destination, pSource); } else if (pSource.Type.SizeInBits < pType.SizeInBits) { EmitExtend(destination, pSource); } break; } case LLPrimitive.Float: // Unsigned to Float { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitIntegerToFloat(destination, pSource); break; } case LLPrimitive.Pointer: // Unsigned to Pointer { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitIntegerToPointer(destination, pSource); break; } default: throw new NotSupportedException(); } break; } case LLPrimitive.Float: { switch (pType.Primitive) { case LLPrimitive.Signed: // Float to Signed { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitFloatToInteger(destination, pSource); break; } case LLPrimitive.Unsigned: // Float to Unsigned { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitFloatToInteger(destination, pSource); break; } case LLPrimitive.Float: // Float to Float { if (pSource.Type.SizeInBits != pType.SizeInBits) { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); } if (pSource.Type.SizeInBits > pType.SizeInBits) { EmitTruncate(destination, pSource); } else if (pSource.Type.SizeInBits < pType.SizeInBits) { EmitExtend(destination, pSource); } break; } case LLPrimitive.Pointer: // Float to Pointer { LLLocation temporaryInteger = LLTemporaryLocation.Create(Function.CreateTemporary(LLModule.GetOrCreateUnsignedType(pType.SizeInBits))); EmitFloatToInteger(temporaryInteger, pSource); destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitIntegerToPointer(destination, temporaryInteger); break; } default: throw new NotSupportedException(); } break; } case LLPrimitive.Pointer: { switch (pType.Primitive) { case LLPrimitive.Signed: // Pointer to Signed { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitPointerToInteger(destination, pSource); break; } case LLPrimitive.Unsigned: // Pointer to Unsigned { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitPointerToInteger(destination, pSource); break; } case LLPrimitive.Float: // Pointer to Float { LLLocation temporaryInteger = LLTemporaryLocation.Create(Function.CreateTemporary(LLModule.GetOrCreateUnsignedType(pSource.Type.SizeInBits))); EmitPointerToInteger(temporaryInteger, pSource); destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitIntegerToFloat(destination, temporaryInteger); break; } case LLPrimitive.Pointer: // Pointer to Pointer { destination = LLTemporaryLocation.Create(Function.CreateTemporary(pType)); EmitBitcast(destination, pSource); break; } default: throw new NotSupportedException(); } break; } default: throw new NotSupportedException(); } return(destination); }
public LLLiteral ToLiteral(params string[] pValues) { switch (mPrimitive) { case LLPrimitive.Signed: case LLPrimitive.Unsigned: case LLPrimitive.Float: case LLPrimitive.Pointer: if (pValues.Length != 1) { throw new ArgumentOutOfRangeException(); } return(LLLiteral.Create(this, pValues[0])); case LLPrimitive.Vector: { if (pValues.Length != mElementCount) { throw new ArgumentOutOfRangeException(); } StringBuilder sb = new StringBuilder(); sb.Append("< "); for (int index = 0; index < mElementCount; ++index) { if (index > 0) { sb.Append(", "); } sb.AppendFormat("{0} {1}", mElementType.ToString(), pValues[index]); } sb.Append(" >"); return(LLLiteral.Create(this, sb.ToString())); } case LLPrimitive.Array: { if (pValues.Length != mElementCount) { throw new ArgumentOutOfRangeException(); } StringBuilder sb = new StringBuilder(); sb.Append("[ "); for (int index = 0; index < mElementCount; ++index) { if (index > 0) { sb.Append(", "); } sb.AppendFormat("{0} {1}", mElementType.ToString(), pValues[index]); } sb.AppendFormat(" ]"); return(LLLiteral.Create(this, sb.ToString())); } case LLPrimitive.Structure: { if (pValues.Length != mStructureFields.Count) { throw new ArgumentOutOfRangeException(); } StringBuilder sb = new StringBuilder(); if (mStructurePacked) { sb.Append("<"); } sb.Append("{ "); for (int index = 0; index < mStructureFields.Count; ++index) { if (index > 0) { sb.Append(", "); } sb.AppendFormat("{0} {1}", mStructureFields[index], pValues[index]); } sb.Append(" }"); if (mStructurePacked) { sb.Append(">"); } return(LLLiteral.Create(this, sb.ToString())); } default: throw new NotSupportedException(); } }