Ejemplo n.º 1
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            var value2 = compiler.CurrentBasicBlock.GetState().StackPop();
            var value1 = compiler.CurrentBasicBlock.GetState().StackPop();

            LLVMValueRef result;

            if (value1.TypeInfo == TypeInfo.FloatingPrimitive)
            {
                result = LLVM.BuildFCmp(builder, GetFloatPredicateFromCode(insn.OpCode.Code), value1.Value, value2.Value, string.Empty);
            }
            else
            {
                result = LLVM.BuildICmp(builder, GetIntPredicateFromCode(insn.OpCode.Code), value1.Value, value2.Value, string.Empty);
            }

            compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(result, TypeInfo.SiIntPrimitive));
        }
Ejemplo n.º 2
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            Code code = insn.OpCode.Code;

            int index;

            if (code >= Code.Stloc_0 && code <= Code.Stloc_3)
            {
                index = insn.OpCode.Code - Code.Stloc_0;
            }
            else
            {
                VariableDefinition def = (VariableDefinition)insn.Operand;
                index = def.Index;
            }

            var value = compiler.CurrentBasicBlock.GetState().StackPop();

            LLVM.BuildStore(builder, value.Value, compiler.LocalValues[index].Value);
        }
Ejemplo n.º 3
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            Code code = insn.OpCode.Code;

            int index;

            if (code >= Code.Ldarg_0 && code <= Code.Ldarg_3)
            {
                index = insn.OpCode.Code - Code.Ldarg_0;
            }
            else
            {
                VariableDefinition def = (VariableDefinition)insn.Operand;
                index = def.Index;
            }

            var arg   = compiler.ArgumentValues[index];
            var value = LLVM.BuildLoad(builder, arg.Value, string.Empty);

            compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(value, arg.TypeInfo));
        }
Ejemplo n.º 4
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            var address = compiler.CurrentBasicBlock.GetState().StackPop();
            var value   = LLVM.BuildLoad(builder, address.Value, string.Empty);

            TypeInfo typeInfo;

            Code code = insn.OpCode.Code;

            switch (code)
            {
            case Code.Ldind_I:
            case Code.Ldind_I1:
            case Code.Ldind_I2:
            case Code.Ldind_I4:
            case Code.Ldind_I8:
                typeInfo = TypeInfo.SiIntPrimitive;
                break;

            case Code.Ldind_U1:
            case Code.Ldind_U2:
            case Code.Ldind_U4:
                typeInfo = TypeInfo.UnIntPrimitive;
                break;

            case Code.Ldind_R4:
            case Code.Ldind_R8:
                typeInfo = TypeInfo.FloatingPrimitive;
                break;

            case Code.Ldind_Ref:
                typeInfo = TypeInfo.Reference;
                break;

            default:
                throw new InvalidOperationException("Unexpected code " + code);
            }

            compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(value, typeInfo));
        }
Ejemplo n.º 5
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            var            value    = compiler.CurrentBasicBlock.GetState().StackPop();
            var            obj      = compiler.CurrentBasicBlock.GetState().StackPop();
            FieldReference fieldRef = (FieldReference)insn.Operand;

            uint idx = compiler.TypeLookup.GetIndexInStructure(fieldRef);

            LLVMValueRef result;

            if (obj.TypeInfo == TypeInfo.Structure)
            {
                // This is a value type, directly go with extracting the value.
                result = LLVM.BuildInsertValue(builder, obj.Value, value.Value, idx, string.Empty);
            }
            else
            {
                // Have to go through a pointer.
                var gep = LLVM.BuildStructGEP(builder, obj.Value, idx, string.Empty);
                LLVM.BuildStore(builder, value.Value, gep);
            }
        }
Ejemplo n.º 6
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            var          value2 = compiler.CurrentBasicBlock.GetState().StackPop();
            var          value1 = compiler.CurrentBasicBlock.GetState().StackPop();
            LLVMValueRef result;
            Code         code = insn.OpCode.Code;

            switch (code)
            {
            case Code.And:
                result = LLVM.BuildAnd(builder, value1.Value, value2.Value, string.Empty);
                break;

            case Code.Or:
                result = LLVM.BuildOr(builder, value1.Value, value2.Value, string.Empty);
                break;

            case Code.Xor:
                result = LLVM.BuildXor(builder, value1.Value, value2.Value, string.Empty);
                break;

            case Code.Shl:
                result = LLVM.BuildShl(builder, value1.Value, value2.Value, string.Empty);
                break;

            case Code.Shr:
                result = LLVM.BuildAShr(builder, value1.Value, value2.Value, string.Empty);
                break;

            case Code.Shr_Un:
                result = LLVM.BuildLShr(builder, value1.Value, value2.Value, string.Empty);
                break;

            default:
                throw new InvalidOperationException("Unexpected code " + code);
            }
            compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(result, value1.TypeInfo));
        }
Ejemplo n.º 7
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            MethodReference methodRef = (MethodReference)insn.Operand;

            int offset   = methodRef.HasThis ? 1 : 0;
            int argCount = methodRef.Parameters.Count + offset;

            LLVMValueRef[] args = new LLVMValueRef[argCount];
            for (int i = 0; i < argCount; ++i)
            {
                args[argCount - i - 1] = compiler.CurrentBasicBlock.GetState().StackPop().Value;
            }

            if (methodRef.HasThis)
            {
                // TODO: don't always do this
                var newType = compiler.TypeLookup.GetLLVMTypeRef(methodRef.DeclaringType);
                if (methodRef.DeclaringType.IsValueType)
                {
                    newType = LLVM.PointerType(newType, 0);
                }
                args[0] = LLVM.BuildPointerCast(builder, args[0], newType, string.Empty);
            }

            var ret = LLVM.BuildCall(builder, compiler.MethodLookup.GetMethod(methodRef), args, string.Empty);

            if (insn.HasPrefix(Code.Tail))
            {
                LLVM.SetTailCall(ret, true);
            }

            if (methodRef.ReturnType.MetadataType != MetadataType.Void)
            {
                compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(ret, methodRef.ReturnType.GetTypeInfo()));
            }
        }
Ejemplo n.º 8
0
 protected override LLVMValueRef ProcessIntegral(MethodCompiler compiler, LLVMValueRef lhs, LLVMValueRef rhs, LLVMBuilderRef builder)
 => LLVM.BuildSub(builder, lhs, rhs, string.Empty);
Ejemplo n.º 9
0
 protected override LLVMValueRef ProcessFloating(MethodCompiler compiler, LLVMValueRef lhs, LLVMValueRef rhs, LLVMBuilderRef builder)
 => LLVM.BuildFAdd(builder, lhs, rhs, string.Empty);
Ejemplo n.º 10
0
 public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
 {
     compiler.CurrentBasicBlock.GetState().StackPop();
 }
Ejemplo n.º 11
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            var value = LLVM.ConstReal(LLVM.DoubleType(), (double)(float)insn.Operand);

            compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(value, TypeInfo.FloatingPrimitive));
        }
Ejemplo n.º 12
0
 public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
 {
     // Intentionally empty:
     //   - nop compiles to nothing.
     //   - tail is handled in the call processors.
 }
Ejemplo n.º 13
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            var value = compiler.CurrentBasicBlock.GetState().StackPeek();

            compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(value));
        }
Ejemplo n.º 14
0
        public void Process(MethodCompiler compiler, Instruction insn, LLVMBuilderRef builder)
        {
            var          value = compiler.CurrentBasicBlock.GetState().StackPop();
            LLVMValueRef result;
            TypeInfo     typeInfo;

            Code code = insn.OpCode.Code;

            switch (code)
            {
            case Code.Conv_R4:
                result = ToFP(value, builder, LLVM.FloatType(), out typeInfo);
                break;

            case Code.Conv_R8:
                result = ToFP(value, builder, LLVM.DoubleType(), out typeInfo);
                break;

            case Code.Conv_I:
                result = ToSiInt(value, builder, compiler.TypeLookup.NativeInt, out typeInfo);
                break;

            case Code.Conv_I1:
                result = ToSiInt(value, builder, LLVM.Int8Type(), out typeInfo);
                break;

            case Code.Conv_I2:
                result = ToSiInt(value, builder, LLVM.Int16Type(), out typeInfo);
                break;

            case Code.Conv_I4:
                result = ToSiInt(value, builder, LLVM.Int32Type(), out typeInfo);
                break;

            case Code.Conv_I8:
                result = ToSiInt(value, builder, LLVM.Int64Type(), out typeInfo);
                break;

            case Code.Conv_U:
                result = ToUnInt(value, builder, compiler.TypeLookup.NativeInt, out typeInfo);
                break;

            case Code.Conv_U1:
                result = ToUnInt(value, builder, LLVM.Int8Type(), out typeInfo);
                break;

            case Code.Conv_U2:
                result = ToUnInt(value, builder, LLVM.Int16Type(), out typeInfo);
                break;

            case Code.Conv_U4:
                result = ToUnInt(value, builder, LLVM.Int32Type(), out typeInfo);
                break;

            case Code.Conv_U8:
                result = ToUnInt(value, builder, LLVM.Int64Type(), out typeInfo);
                break;

            default:
                throw new InvalidOperationException("Unexpected code " + code);
            }

            compiler.CurrentBasicBlock.GetState().StackPush(new EmulatedStateValue(result, typeInfo));
        }
Ejemplo n.º 15
0
 protected abstract LLVMValueRef ProcessFloating(MethodCompiler compiler, LLVMValueRef lhs, LLVMValueRef rhs, LLVMBuilderRef builder);
Ejemplo n.º 16
0
 protected abstract LLVMValueRef ProcessIntegral(MethodCompiler compiler, LLVMValueRef lhs, LLVMValueRef rhs, LLVMBuilderRef builder);