Ejemplo n.º 1
0
        public void BasicGlobalVariable_ParseCheck_Expected_Equal()
        {
            string globalVariableIdentifier = "@my_global_int";
            string value  = "12345";
            var    global = new LGlobal(new LValueRef(LType.UInt32Type(), globalVariableIdentifier), value);

            Assert.AreEqual($"{globalVariableIdentifier} = {LKeywords.Global} i32 {value}", global.Parse());
        }
Ejemplo n.º 2
0
        public void BasicGlobalVariable_CharArray_ParseCheck_Expected_Equal()
        {
            string globalVariableIdentifier = "@my_global_int";
            string value  = "c\"Hello World!\00\"";
            var    global = new LGlobal(new LArrayRef(globalVariableIdentifier, new LValueRef(LType.Int8Type(), ""), 13), value)
            {
                IsConstant = true
            };

            Assert.AreEqual($"{globalVariableIdentifier} = {LKeywords.Constant} [13 x i8] {value}", global.Parse());
        }
Ejemplo n.º 3
0
        public void BasicGlobalVariable_LocalDoubleArray_ParseCheck_Expected_Equal()
        {
            string globalVariableIdentifier = "@my_global_double_array";
            string value  = "[double 1.000000e+03, double 2.000000e+00, double 3.400000e+00, double 7.000000e+00, double 5.000000e+01]";
            var    global = new LGlobal(new LArrayRef(globalVariableIdentifier, new LValueRef(LType.F64Type(), ""), 5), value)
            {
                IsConstant = true,
                RuntimePreemptionSpecifier = Enums.LRuntimePreemptionSpecifier.dso_local
            };

            Assert.AreEqual($"{globalVariableIdentifier} = {LKeywords.DsoLocal} {LKeywords.Constant} [5 x double] {value}", global.Parse());
        }
Ejemplo n.º 4
0
        public void BasicGlobalVariable_LocalCharArray_ParseCheck_Expected_Equal()
        {
            string globalVariableIdentifier = "@my_global_char_array";
            string value  = "c\"Hello World!\00\"";
            var    global = new LGlobal(new LArrayRef(globalVariableIdentifier, new LValueRef(LType.Int8Type(), ""), 13), value)
            {
                IsConstant = true,
                RuntimePreemptionSpecifier = Enums.LRuntimePreemptionSpecifier.dso_local
            };

            Assert.AreEqual($"{globalVariableIdentifier} = {LKeywords.DsoLocal} {LKeywords.Constant} [13 x i8] {value}", global.Parse());
        }
Ejemplo n.º 5
0
        public void GlobalVariable_IsExternalAndConstant_Expected_Exception()
        {
            string globalVariableIdentifier = "@my_global_int";
            string value  = "12345";
            var    global = new LGlobal(new LValueRef(LType.UInt32Type(), globalVariableIdentifier), value)
            {
                IsExternal = true
            };

            Assert.Throws <Exception>(() => {
                global.IsConstant = true;
            });
        }
Ejemplo n.º 6
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());
        }