Пример #1
0
        public void SimpleFunctionType_ParseCheck() {
            var returnVal = new LValueRef(LType.F32Type(), "");
            var parameter = new LPointerRef(new LValueRef(LType.F32Type(), ""), "");
            LFunctionType fnType = new LFunctionType(returnVal, parameter);

            Assert.AreEqual("float (float*)", fnType.Parse());
        }
Пример #2
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)
                                );
        }
Пример #3
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()));
        }
Пример #4
0
        public void VarargFunctionType_ParseCheck() {
            var returnVal = new LValueRef(LType.F32Type(), "");
            var parameter = new LPointerRef(new LValueRef(LType.F32Type(), ""), "");
            LFunctionType fnType = new LFunctionType(returnVal, parameter) {
                HasVararg = true
            };

            Assert.AreEqual("float (float*, ...)", fnType.Parse());
        }
Пример #5
0
 public LPtrtointTo(LValueRef result, LPointerRef value)
 {
     if (!result.Type.IsIntegral() || !value.BaseType.IsIntegral())
     {
         throw new Exception("Only floating point types are allowed for ptrtoint ... to instruction." +
                             $"Result: {result.ParseType()}, Value: {value.ParseType()}");
     }
     Result = result;
     Value  = value;
 }
Пример #6
0
        public LIcmp(LPointerRef result, LICondition condition, LPointerRef op1, LPointerRef op2)
        {
            if (result.ParseType() != op1.ParseType() || op1.ParseType() != op2.ParseType())
            {
                throw new Exception($"Types of operands or result are not equal. Result: {result.ParseType()}" +
                                    $", Op1: {op1.ParseType()}, Op2: {op2.ParseType()}");
            }
            if (result.BaseType.IsFloatingPoint())
            {
                throw new Exception("Floating point types are not allowed for icmp.");
            }

            Result    = result;
            Condition = condition;
            Op1       = op1;
            Op2       = op2;
        }
Пример #7
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());
        }
Пример #8
0
 /// <summary>
 /// Returns identifier of reference.
 /// </summary>
 /// <param name="reference"></param>
 /// <returns>identifier</returns>
 public static string GetIdentifierOf(LBaseRef reference)
 {
     if (reference.IsValue())
     {
         LValueRef value = (LValueRef)reference;
         return(value.Identifier !);
     }
     else if (reference.IsPointer())
     {
         LPointerRef pointer = (LPointerRef)reference;
         return(pointer.Identifier);
     }
     else if (reference.IsArray())
     {
         LArrayRef array = (LArrayRef)reference;
         return(array.Identifier);
     }
     else if (reference.IsVector())
     {
         LVectorRef vector = (LVectorRef)reference;
         return(vector.Identifier !);
     }
     throw new Exception("unknown reference.");
 }
Пример #9
0
 public void SetUp()
 {
     _dst = new LPointerRef(new LValueRef(LType.Int32Type(), ""), "%ptr");
     _src = new LValueRef(LType.Int32Type(), "%val");
 }