Exemple #1
0
        public static string WritePlatformInvokeMethod(this PlatformInvokeMethod platformInvoke,
                                                       ClosureEntities crRuntime)
        {
            var methodId = Import(platformInvoke.LibraryName,
                                  platformInvoke.MethodName,
                                  platformInvoke.CallingConvention,
                                  platformInvoke.EntryPoint);

            var codeOutput = new CodeOutput();

            codeOutput.AppendFormat(platformInvoke.WritePInvokeDefinition(methodId));
            codeOutput.BlankLine();
            codeOutput.Append(platformInvoke.Method.WriteHeaderMethod(crRuntime, false));

            // write PInvoke implementation
            codeOutput.BracketOpen();

            var argumentsCall = platformInvoke.Method.GetParameters()
                                .Select(CallMarshallerFactory.CreateMarshaller)
                                .Each(marshaller => { codeOutput.Append(marshaller.GetTransformationCode()); })
                                .Once(marshallers => { codeOutput.BlankLine(); })
                                .Select(p => p.GetParameterString())
                                .Join(", ");

            if (!platformInvoke.Method.GetReturnType().IsVoid())
            {
                codeOutput.Append("return ");
            }
            codeOutput.AppendFormat("{0}({1});", methodId, argumentsCall);
            codeOutput.BracketClose();

            return(codeOutput.ToString());
        }
        static void WriteCppMethods(List <MethodInterpreter> closure, CodeOutput sb, ClosureEntities crRuntime)
        {
            var cppMethods = closure
                             .Where(m => m.Kind == MethodKind.RuntimeCppMethod)
                             .ToArray();

            var methodInterpreter = cppMethods.FirstOrDefault();

            if (methodInterpreter == null)
            {
                return;
            }
            foreach (var interpreter in cppMethods)
            {
                var cppInterpreter = (CppMethodInterpreter)interpreter;
                var runtimeLibrary = cppInterpreter.CppRepresentation;
                if (LinkingData.SetInclude(runtimeLibrary.Header))
                {
                    sb.AppendFormat("#include \"{0}\"\n", runtimeLibrary.Header);
                }
                CppWriteSignature.WriteSignature(sb, interpreter, crRuntime, false);
                sb.BracketOpen()
                .Append(runtimeLibrary.Source)
                .BracketClose();
            }
        }
Exemple #3
0
        static void HandleSwitch(LocalOperation operation, CodeOutput bodySb)
        {
            var assign           = (Assignment)operation;
            var instructionTable = (int[])((ConstValue)assign.Right).Value;

            var instructionLabelIds = instructionTable;

            bodySb.AppendFormat("switch({0})", assign.AssignedTo.Name);
            bodySb.BracketOpen();
            var pos = 0;

            foreach (var instructionLabelId in instructionLabelIds)
            {
                bodySb.AppendFormat("case {0}:", pos++);
                bodySb.AppendFormat("\tgoto label_{0};", instructionLabelId.ToHex());
            }
            bodySb.BracketClose();
        }