public void WriteSource(CodeTextStorage storage)
        {
            using (var _ = storage.EnterScope(translateContext.FocusedAssemblyInformation.IdentName))
            {
                foreach (var module in translateContext.FocusedAssemblyInformation.Modules.Values)
                {
                    foreach (var type in module.Types.Values)
                    {
                        var codeWriter = storage.Wirter(type.TypeName + ".cpp");
                        typeSourceWriters[type.FullName] = codeWriter;
                        codeWriter.WriteLine(EnvIncludes);
                        codeWriter.WriteLine($"#include <{type.TypeName}.h>");
                        foreach (var method in type.Methods)
                        {
                            CXXMethodTranslateContext methodContext = new CXXMethodTranslateContext(translateContext, method);
                            // [2-1] Stack Code
                            codeWriter.WriteLine($"\n//{method.CXXMethodName}\n//[2-1] Here Begins Stack Declaration");
                            codeWriter.WriteLine($"struct {method.CXXStackName}");
                            codeWriter.WriteLine("{");
                            codeWriter.indent();
                            foreach (var localVar in method.LocalVariables)
                            {
                                codeWriter.WriteLine($"{localVar.CXXTypeName} v{localVar.Index};");
                            }
                            codeWriter.WriteLine("template<bool InitLocals> static void Init(){};//Active with MethodBody.InitLocals Property.");
                            codeWriter.unindent();
                            codeWriter.WriteLine("};\n");

                            // [2-2-1] Method Code
                            codeWriter.WriteLine("//[2-2] Here Begins Method Body");
                            codeWriter.WriteLine(
                                method.CXXRetType + " " + method.CXXMethodName + method.CXXParamSequence);
                            codeWriter.WriteLine("{");
                            // [2-2-2] Code Body
                            codeWriter.indent();
                            codeWriter.WriteLine($"{method.CXXStackName} stack;");
                            codeWriter.WriteLine($"stack.Init<{method.InitLocals.ToString().ToLower()}>();");
                            foreach (var instruction in method.Body.Instructions)
                            {
                                codeWriter.WriteLine(NoteILInstruction(instruction, methodContext));
                                codeWriter.WriteLine(TranslateILInstruction(instruction, methodContext));
                            }
                            codeWriter.unindent();
                            codeWriter.WriteLine("}");
                        }
                        typeSourceWriters[type.FullName].Flush();
                    }
                }
            }//End Dispose EnterScope
        }
Esempio n. 2
0
        public void WriteMethodRecursive(CodeTextWriter codeWriter, Metadata.TypeInformation type)
        {
            foreach (var nested in type.Nested)
            {
                WriteMethodRecursive(codeWriter, nested);
            }
            foreach (var method in type.Methods)
            {
                if (method.Body == null)
                {
                    continue;
                }
                CXXMethodTranslateContext methodContext = new CXXMethodTranslateContext(translateContext, method);

                // [2-2-1] Method Code
                if (type.HasGenericParameters)
                {
                    codeWriter.WriteLine($"template<{type.CXXTemplateParam}>");
                }
                if (method.HasGenericParameters)
                {
                    codeWriter.WriteLine($"template<{method.CXXTemplateParam}>");
                }

                codeWriter.WriteLine(
                    method.CXXRetType + " " + method.CXXMethodDeclareName + method.CXXParamSequence(false));

                codeWriter.WriteLine("{");
                // [2-2-2] Code Body
                codeWriter.indent();
                if (method.IsConstructor && method.IsStatic)
                {
                    codeWriter.WriteLine("static std::once_flag flag;");
                    codeWriter.WriteLine("std::call_once(flag,[&]()");
                    codeWriter.WriteLine("{");
                    codeWriter.indent();
                }
                foreach (var localVar in method.LocalVariables)
                {
                    if (method.InitLocals)
                    {
                        codeWriter.WriteLine($"{localVar.CXXVarDeclaration} v{localVar.Index} = {localVar.CXXVarInitVal};");
                    }
                    else
                    {
                        codeWriter.WriteLine($"{localVar.CXXVarDeclaration} v{localVar.Index};");
                    }
                }
                foreach (var instruction in method.Body.Instructions)
                {
                    codeWriter.WriteLine(NoteILInstruction(instruction, methodContext));
                    codeWriter.WriteLine(
                        instruction.GetLabel() + ": " +
                        TranslateILInstruction(instruction, methodContext));
                }
                if (method.IsConstructor && method.IsStatic)
                {
                    codeWriter.unindent();
                    codeWriter.WriteLine("});");
                }
                codeWriter.unindent();
                codeWriter.WriteLine("}");
            }
        }