예제 #1
0
파일: LLType.cs 프로젝트: Astaelan/Neutron
 public LLType AdjustPointerDepth(int pPointerDepth)
 {
     if (pPointerDepth == 0)
     {
         return(this);
     }
     if (pPointerDepth < 0)
     {
         if (mPointerDepth == 0)
         {
             return(this);
         }
         int depth = mPointerDepth + pPointerDepth;
         if (depth <= 0)
         {
             return(mPointerBase);
         }
         return(LLModule.GetOrCreatePointerType(mPointerBase, depth));
     }
     if (mPointerDepth == 0)
     {
         return(LLModule.GetOrCreatePointerType(this, pPointerDepth));
     }
     return(LLModule.GetOrCreatePointerType(mPointerBase, mPointerDepth + pPointerDepth));
 }
예제 #2
0
        private static LLFunction CreateFunction(string pName, bool pExplicitName, bool pExternal, bool pAbstract, LLType pReturnType, List <LLParameter> pParameters)
        {
            LLFunction function = new LLFunction();

            function.Name                   = pName;
            function.ExplicitName           = pExplicitName;
            function.External               = pExternal;
            function.Abstract               = pAbstract;
            function.Identifier             = LLModule.GetFunctionIdentifier(pName, pReturnType, pParameters);
            function.IdentifierHash         = "F_" + BitConverter.ToString(SHA256.Create().ComputeHash(Encoding.ASCII.GetBytes(function.Identifier))).Replace("-", "").ToUpper().Substring(0, 16);
            function.ReturnType             = pReturnType;
            function.Parameters             = new LLParameterList(pParameters);
            sFunctions[function.Identifier] = function;
            return(function);
        }
예제 #3
0
        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);
        }