Exemple #1
0
        void TestArithmetic(MachineInfo mi, string type1, string type2, CBasicType result)
        {
            var report = new Report(new TestPrinter());

            var compiler = new Compiler.CCompiler(mi, report);

            compiler.AddCode("test.c", type1 + " v1; " + type2 + " v2;");
            var exe     = compiler.Compile();
            var context = new ExecutableContext(new Executable(mi), report);

            var ty1 = exe.Globals.First(x => x.Name == "v1").VariableType;

            Assert.IsInstanceOfType(ty1, typeof(CBasicType));
            var ty2 = exe.Globals.First(x => x.Name == "v2").VariableType;

            Assert.IsInstanceOfType(ty2, typeof(CBasicType));

            var bty1 = (CBasicType)ty1;
            var bty2 = (CBasicType)ty2;

            Assert.IsTrue(bty1.IsIntegral);
            Assert.IsTrue(bty2.IsIntegral);

            var aty1 = bty1.ArithmeticConvert(bty2, context);
            var aty2 = bty2.ArithmeticConvert(bty1, context);

            Assert.AreEqual(aty1.Signedness, result.Signedness);
            Assert.AreEqual(aty1.GetByteSize(context), result.GetByteSize(context));
            Assert.AreEqual(aty2.Signedness, result.Signedness);
            Assert.AreEqual(aty2.GetByteSize(context), result.GetByteSize(context));
        }
        public InternalFunction(MachineInfo machineInfo, string prototype, InternalFunctionAction?action = null)
        {
            var report   = new Report(new Report.TextWriterPrinter(Console.Out));
            var parser   = new CParser();
            var tu       = parser.ParseTranslationUnit("_internal.h", prototype + ";", ((_, __) => null), report);
            var compiler = new Compiler.CCompiler(machineInfo, report);

            compiler.Add(tu);
            var exe = compiler.Compile();

            if (tu.Functions.Count == 0)
            {
                throw new Exception("Failed to parse function prototype: " + prototype);
            }
            var f = tu.Functions[0];

            Name         = f.Name;
            NameContext  = f.NameContext;
            FunctionType = f.FunctionType;
            if (action != null)
            {
                Action = action;
            }
            else
            {
                Action = _ => { };
            }
        }
Exemple #3
0
        public static Executable Compile(string code)
        {
            var compiler = new Compiler.CCompiler();

            compiler.AddCode("main.c", code);
            var exe = compiler.Compile();

            if (compiler.Options.Report.Errors.Any(x => x.IsError))
            {
                var m = string.Join("\n", compiler.Options.Report.Errors.Where(x => x.IsError));
                throw new ArgumentException(m, nameof(code));
            }
            return(exe);
        }
Exemple #4
0
        void TestPromote(MachineInfo mi, string type, int resultBytes, Signedness signedness)
        {
            var report  = new Report(new TestPrinter());
            var context = new ExecutableContext(new Executable(mi), report);

            var compiler = new Compiler.CCompiler(mi, report);

            compiler.AddCode("test.c", type + " v;");
            var exe = compiler.Compile();

            var ty = exe.Globals.First(x => x.Name == "v").VariableType;

            Assert.IsInstanceOfType(ty, typeof(CBasicType));
            var bty = (CBasicType)ty;

            Assert.IsTrue(bty.IsIntegral);
            var pty = bty.IntegerPromote(context);

            Assert.AreEqual(pty.Signedness, signedness);
            Assert.AreEqual(pty.GetByteSize(context), resultBytes);
        }