Ejemplo n.º 1
0
        public void Ret_ParseCheck_i32_Identifier_Ok()
        {
            LValueRef valueRef = new LValueRef(LType.Int32Type(), _function.GetValueRefIdentifier());
            LRet      ret      = new LRet(valueRef);

            Assert.AreEqual($"{LKeywords.Ret} i32 {valueRef.ValueOrIdentifier}", LHelper.Trim(ret.ParseInstruction()));
        }
Ejemplo n.º 2
0
        public void Ret_ParseCheck_i32_Value_Ok()
        {
            LValueRef valueRef = new LValueRef(LType.Int32Type(), "12");
            LRet      ret      = new LRet(valueRef);

            Assert.AreEqual($"{LKeywords.Ret} i32 12", LHelper.Trim(ret.ParseInstruction()));
        }
Ejemplo n.º 3
0
        public void SimpleAllocaParse_Expected_True()
        {
            LAlloca alloca = new LAlloca(_function, LType.Int32Type());

            Assert.AreEqual($"{alloca.PointerRef.Identifier} = {LKeywords.Alloca} {LType.Int32Type().Parse()}",
                            LHelper.Trim(alloca.ParseInstruction()));
        }
Ejemplo n.º 4
0
        public void SimpleFneg_ResultInt32_Exception()
        {
            LFneg fneg;

            Assert.Throws <Exception>(() =>
                                      fneg = new LFneg(_op1, new LValueRef(LType.Int32Type(), "%result"))
                                      );
        }
Ejemplo n.º 5
0
        public void DoubleValueArrayRef_ParseCheck_Expected_Equals()
        {
            LArrayRef arrayRef  = new LArrayRef("arrayRef", new LValueRef(LType.Int32Type(), ""), 5);
            LArrayRef arrayRef2 = new LArrayRef("arrayRef2", arrayRef, 5);

            Assert.AreEqual("[5 x i32]", arrayRef.ParseType());
            Assert.AreEqual("[5 x [5 x i32]]", arrayRef2.ParseType());
        }
Ejemplo n.º 6
0
        public void SimpleLshr_WithConstant_ParseCheck()
        {
            LValueRef op2 = new LValueRef(LType.Int32Type(), "12");
            LShl      shl = new LShl(_valueResult, _valueOp1, op2);

            Assert.AreEqual($"{_valueResult.Identifier} = shl {_valueResult.ParseType()} {_valueOp1.ValueOrIdentifier}, {op2.ValueOrIdentifier}",
                            LHelper.Trim(shl.ParseInstruction()));
        }
Ejemplo n.º 7
0
        public void SimpleAnd_WithConstant_ParseCheck()
        {
            LValueRef op2 = new LValueRef(LType.Int32Type(), "12");
            LAnd      and = new LAnd(_valueResult, _valueOp1, op2);

            Assert.AreEqual($"{_valueResult.Identifier} = and {_valueResult.ParseType()} {_valueOp1.ValueOrIdentifier}, {op2.ValueOrIdentifier}",
                            LHelper.Trim(and.ParseInstruction()));
        }
Ejemplo n.º 8
0
        public void SimpleLoadParse_Expected_True()
        {
            LLoad load = new LLoad(_function, _alloca.PointerRef);

            Assert.AreEqual(
                $"{load.ValueRef.Identifier} = {LKeywords.Load} {LType.Int32Type().Parse()}, {load.PointerRef.ParseType()} {load.PointerRef.Identifier}",
                LHelper.Trim(load.ParseInstruction()));
        }
Ejemplo n.º 9
0
        public void SimpleFneg_Op1Int32_Exception()
        {
            LFneg fneg;

            Assert.Throws <Exception>(() =>
                                      fneg = new LFneg(new LValueRef(LType.Int32Type(), "%result"), _result)
                                      );
        }
Ejemplo n.º 10
0
        public void DoubleArrayRef_Expected_NoException()
        {
            LArrayRef arrayRef = new LArrayRef("arrayRef", new LValueRef(LType.Int32Type(), ""), 5);
            LArrayRef arrayRef2;

            Assert.DoesNotThrow(() =>
                                arrayRef2 = new LArrayRef("arrayRef2", arrayRef, 5)
                                );
        }
Ejemplo n.º 11
0
        public void PointerArrayRef_Expected_NoException()
        {
            LPointerRef pointerRef = new LPointerRef(new LValueRef(LType.Int32Type(), ""), "pointerRef");
            LArrayRef   arrayRef;

            Assert.DoesNotThrow(() =>
                                arrayRef = new LArrayRef("arrayRef2", pointerRef, 5)
                                );
        }
Ejemplo n.º 12
0
        public void Switch_Unconditional_ParseCheck()
        {
            LValueRef  valueRef     = new LValueRef(LType.Int32Type(), "%1");
            LLabelType defaultLabel = new LLabelType("%default");
            LSwitch    @switch      = new LSwitch(valueRef, defaultLabel);

            Assert.AreEqual("switch i32 %1, label %default [ ]",
                            LHelper.Trim(@switch.ParseInstruction()));
        }
Ejemplo n.º 13
0
        public void AllocaTriplePointerType_Expected_Equal()
        {
            LPointerRef singlePointer = new LPointerRef(new LValueRef(LType.Int32Type(), ""), _function.GetPointerRefIdentifier());
            LPointerRef doublePointer = new LPointerRef(singlePointer, _function.GetPointerRefIdentifier());
            LAlloca     alloca        = new LAlloca(_function, doublePointer);

            Assert.AreEqual($"{alloca.PointerRef.Identifier} = {LKeywords.Alloca} {LType.Int32Type().Parse()}**", LHelper.Trim(alloca.ParseInstruction()));
            Assert.AreEqual($"{LType.Int32Type().Parse()}***", LHelper.Trim(alloca.PointerRef.ParseType()));
        }
Ejemplo n.º 14
0
        public void SimpleAllocaAlignmentExceeded_Expected_Exception()
        {
            LAlloca alloca;

            Assert.Throws <Exception>(() =>
                                      alloca = new LAlloca(_function, LType.Int32Type())
            {
                Alignment = 1 << 30
            });
        }
Ejemplo n.º 15
0
        public void ConditionalBr_TypeCheck_i32_Exception()
        {
            LConditionalBr conditionalBr;

            Assert.Throws <Exception>(() =>
                                      conditionalBr = new LConditionalBr(
                                          new LValueRef(LType.Int32Type(), _function.GetValueRefIdentifier()),
                                          _ifTrueLabel, _ifFalseLabel)
                                      );
        }
Ejemplo n.º 16
0
        public void AllocaParseNumOfElements_Expected_True()
        {
            LAlloca alloca = new LAlloca(_function, LType.Int32Type())
            {
                NumOfElements = 5
            };

            Assert.AreEqual(
                $"{alloca.PointerRef.Identifier} = {LKeywords.Alloca} {LType.Int32Type().Parse()}, {LType.Int32Type().Parse()} 5",
                LHelper.Trim(alloca.ParseInstruction()));
        }
Ejemplo n.º 17
0
        public void AllocaParseAddrspace_Expected_True()
        {
            LAlloca alloca = new LAlloca(_function, LType.Int32Type())
            {
                Addrspace = 4
            };

            Assert.AreEqual(
                $"{alloca.PointerRef.Identifier} = {LKeywords.Alloca} {LType.Int32Type().Parse()}, {LKeywords.Addrspace}(4)",
                LHelper.Trim(alloca.ParseInstruction()));
        }
Ejemplo n.º 18
0
        public void SimpleLabel_RegisterInstructionTest_NoException()
        {
            var func = LFunction.Create("foo_function", new LValueRef(LType.Int32Type(), ""));

            func.Register(new LLabelType("entry"));

            Assert.DoesNotThrow(() => {
                var alloca     = func.Register(new LAlloca(func, LType.F32Type()));
                var pointerRef = alloca.PointerRef;
                var load       = func.Register(new LLoad(func, pointerRef));
            });
        }
Ejemplo n.º 19
0
        public void AdvancedLabel_RegisterInstructionByLabelTest_LabelNotFound_Exception()
        {
            var func   = LFunction.Create("foo_function", new LValueRef(LType.Int32Type(), ""));
            var label1 = func.Register(new LLabelType("entry"));
            var label2 = new LLabelType("label2");

            Assert.Throws <Exception>(() => {
                var alloca     = func.Register(new LAlloca(func, LType.F32Type()));
                var pointerRef = alloca.PointerRef;
                var load       = func.Register(label2, new LLoad(func, pointerRef));
            });
        }
Ejemplo n.º 20
0
        public void Switch_ParseCheck()
        {
            LValueRef  valueRef     = new LValueRef(LType.Int32Type(), "%1");
            LLabelType defaultLabel = new LLabelType("%default");
            LSwitch    @switch      = new LSwitch(valueRef, defaultLabel);

            @switch.JumpTableDestinations.Add((0, new LLabelType("%one")));
            @switch.JumpTableDestinations.Add((1, new LLabelType("%two")));
            @switch.JumpTableDestinations.Add((2, new LLabelType("%three")));
            @switch.JumpTableDestinations.Add((3, new LLabelType("%four")));

            Assert.AreEqual($"switch i32 %1, label %default [ i32 0, label %one{Environment.NewLine}i32 1, label %two{Environment.NewLine}i32 2, label %three{Environment.NewLine}i32 3, label %four ]",
                            LHelper.Trim(@switch.ParseInstruction()));
        }
Ejemplo n.º 21
0
        static void Main(string[] args)
        {
            string valueIdentifier = "@value";
            string value           = "c\"Hello World!\\00\"";

            var module = new LModule("LLVMHelloWorld");
            var func   = LFunction.Create("@main", new LValueRef(LType.Int32Type(), ""));

            var arrayRef = new LArrayRef(valueIdentifier, new LValueRef(LType.Int8Type(), ""), 13);
            // Create char array constant.
            var global = new LGlobal(arrayRef, value)
            {
                IsConstant = true
            };

            var fnType = new LFunctionType(new LValueRef(LType.Int32Type(), "%printf"), new LPointerRef(new LValueRef(LType.Int8Type(), ""), "%result"))
            {
                HasVararg = true
            };
            var external = new LExternal(fnType, "@printf");

            var result = new LPointerRef(new LValueRef(LType.Int8Type(), ""), "%result");
            var gep    = new LGetelementptr(result, global.GetPointerRef());

            gep.Indexes.Add((LType.Int32Type(), 0));
            gep.Indexes.Add((LType.Int32Type(), 0));

            var call = new LCall(fnType, external.FnIdentifier);

            var ret = new LRet(new LValueRef(LType.Int32Type(), "0"));

            // Create and register entry label
            func.Register(new LLabelType("entry"));
            // Register function and char array constant to module.
            module.Register(global);
            module.Register(func);
            module.Register(external);
            // Register instructions.
            func.Register(gep);
            func.Register(call);
            func.Register(ret);

            Console.WriteLine(module.Parse());
        }
Ejemplo n.º 22
0
 public void SetUp()
 {
     _dst = new LPointerRef(new LValueRef(LType.Int32Type(), ""), "%ptr");
     _src = new LValueRef(LType.Int32Type(), "%val");
 }
Ejemplo n.º 23
0
 public void SetUp()
 {
     _alloca = new LAlloca(_function, LType.Int32Type());
 }
Ejemplo n.º 24
0
        public void LoadParseIsAtomicVolatileAlignment_Expected_True()
        {
            LLoad load = new LLoad(_function, _alloca.PointerRef)
            {
                IsAtomic   = true,
                IsVolatile = true,
                Alignment  = 5012
            };

            Assert.AreEqual(
                $"{load.ValueRef.Identifier} = {LKeywords.Load} {LKeywords.Atomic} {LKeywords.Volatile} {LType.Int32Type().Parse()}, {load.PointerRef.ParseType()} {load.PointerRef.Identifier}, {LKeywords.Align} {5012}",
                LHelper.Trim(load.ParseInstruction()));
        }