예제 #1
0
        private LLVMValueRef DeclareArrayLength(IMethod Method)
        {
            var arrayType = Method.DeclaringType.AsArrayType();

            // Declare T[,...].Length.
            var abi      = LLVMSymbolTypeMember.GetLLVMAbi(Method, assembly);
            var funcType = DeclarePrototype(Method);
            var funcDef  = AddFunction(module, abi.Mangler.Mangle(Method, true), funcType);

            funcDef.SetLinkage(LLVMLinkage.LLVMWeakODRLinkage);
            AddAttributeAtIndex(
                funcDef,
                LLVMAttributeIndex.LLVMAttributeFunctionIndex,
                CreateEnumAttribute("nounwind"));

            // Define T[,...].Length's body.
            var codeGenerator = new Codegen.LLVMCodeGenerator(Method);

            // T[,...].Length computes the product of all dimensions.
            var dimensions = new ICodeBlock[arrayType.ArrayRank];

            for (int i = 0; i < dimensions.Length; i++)
            {
                dimensions[i] = codeGenerator.EmitDereferencePointer(
                    new Codegen.GetDimensionPtrBlock(
                        codeGenerator,
                        (Codegen.CodeBlock)codeGenerator.GetThis().EmitGet(),
                        i));
            }

            var body = (Codegen.CodeBlock)codeGenerator.EmitReturn(codeGenerator.EmitProduct(dimensions));

            // Emit T[,...].Length's body.
            var bodyBuilder       = new Codegen.FunctionBodyBuilder(this, funcDef);
            var entryPointBuilder = bodyBuilder.AppendBasicBlock("entry");

            entryPointBuilder = codeGenerator.Prologue.Emit(entryPointBuilder);
            body.Emit(entryPointBuilder);

            return(funcDef);
        }