public static string LoadDllMethods() { var sb = new CodeOutput(); sb.BlankLine() .Append("System_Void mapLibs()") .BracketOpen(); var pos = 0; foreach (var library in LinkingData.Libraries) { sb.Append("//---------------------------------------------------------\n") .AppendFormat("// {0} methods\n", library.DllName) .Append("//---------------------------------------------------------\n") .AppendFormat("auto lib_{0} = LoadNativeLibrary(L\"{1}\");\n", pos, library.DllName) .BlankLine(); foreach (var method in library.Methods.Values) { sb.AppendFormat("{0} = ({0}_type)LoadNativeMethod(lib_{2}, \"{1}\");\n", method.FormattedName(), method.EntryPoint, pos); } pos++; } return(sb.BracketClose() .BlankLine() .ToString()); }
static void WriteMainBody(MethodInterpreter interpreter, CodeOutput sb, ClosureEntities crRuntime) { sb.Append("System_Void initializeRuntime();\n"); sb.Append("int main(int argc, char**argv)").BracketOpen(); sb.Append("auto argsAsList = System_getArgumentsAsList(argc, argv);\n"); sb.Append("initializeRuntime();\n"); var entryPoint = interpreter.Method as MethodInfo; if (entryPoint.ReturnType != typeof(void)) { sb.Append("return "); } var parameterInfos = entryPoint.GetParameters(); var args = string.Empty; if (parameterInfos.Length != 0) { args = "argsAsList"; } sb.AppendFormat("{0}({1});\n", entryPoint.ClangMethodSignature(crRuntime), args); sb.BlankLine(); sb.Append("return 0;"); sb.BracketClose(); }
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 WriteStructWithFields(CodeOutput codeOutput, ClosureEntities crRuntime, Type type) { if (DelegateManager.IsTypeDelegate(type)) return; var mappedType = type.GetMappedType(crRuntime); type = mappedType.GetReversedMappedType(crRuntime); if (!type.IsValueType && type.BaseType != null) { //Not Necessary // codeOutput.AppendFormat("struct {0} : public {1} {2} {{", type.ToCppMangling(), type.BaseType.ToCppMangling(),type.GetInterfaces().Any()? " ,"+type.GetInterfaces().Select(j=>j.ToCppMangling()).Aggregate((a,b)=>a + " , " + b):""); codeOutput.AppendFormat( "struct {0} : public {1}", type.ToCppMangling(), type.BaseType.ToCppMangling()) .BracketOpen(); } else if (!type.IsValueType && type.IsInterface) { codeOutput.AppendFormat("struct {0} : public {1}", type.ToCppMangling(), typeof(object).ToCppMangling()) .BracketOpen(); } else { codeOutput.AppendFormat("struct {0}", type.ToCppMangling()) .BracketOpen(); } if (type == typeof(object)) { codeOutput.Append("int _typeId;\n"); } //String Support if (type == typeof(string)) { crRuntime.AddType(typeof(string)); var usedTypes = crRuntime.MappedTypes.Values.ToList(); var typeTable = new TypeDescriptionTable(usedTypes, crRuntime); codeOutput.Append("System_String()") .BracketOpen() .AppendFormat("_typeId = {0};\n", typeTable.GetTypeId(typeof(string))) .BracketClose() .BlankLine(); } WriteClassFieldsBody(codeOutput, mappedType, crRuntime); codeOutput.BracketClose(true) .Append(";\n") .BlankLine(); var typedesc = UsedTypeList.Set(type, crRuntime); typedesc.WriteStaticFieldInitialization(codeOutput); }
public string BuildStringTable() { var sb = new CodeOutput(); sb.BlankLine() .Append("System_Void buildStringTable()") .BracketOpen(); var stringDataBuilder = new List <string>(); var jump = 0; foreach (var strItem in _table) { sb.AppendFormat("_AddJumpAndLength({0}, {1});\n", jump, strItem.Length); var itemTextData = TextData(strItem); AddTextToStringTable(stringDataBuilder, itemTextData, strItem); jump += strItem.Length + 1; } sb.BracketClose(true) .Append(" // buildStringTable\n"); var stringTableContent = string.Join(", " + Environment.NewLine, stringDataBuilder); var length = jump == 0 ? 1 : jump; sb.BlankLine() .AppendFormat("const System_Char _stringTable[{0}] =", length) .BracketOpen(); sb.Append(jump == 0 ? "0" : stringTableContent); sb.BracketClose(true) .Append("; // _stringTable\n"); return(sb.ToString()); }
public void WriteLayout(CodeOutput codeOutput) { BaseType?.WriteLayout(codeOutput); var noOffsetFields = new List <FieldDescription>(); var dictionary = new SortedDictionary <int, List <FieldDescription> >(); BuildUnionLayouts(noOffsetFields, dictionary); foreach (var fieldList in dictionary.Values) { codeOutput.Append("union") .BracketOpen(); WriteFieldListToLayout(codeOutput, fieldList); codeOutput.BracketClose(); } WriteFieldListToLayout(codeOutput, noOffsetFields); }
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(); }
public static string BuildConstantTable() { var sb = new CodeOutput(); sb.BlankLine() .Append("System_Void RuntimeHelpersBuildConstantTable()") .BracketOpen(); foreach (var item in Instance.ItemList) { var rightArray = item.Data; var rightArrayItems = string.Join(", ", rightArray); sb.AppendFormat("AddConstantByteArray(new byte[{0}]);", rightArray.Length) .BracketOpen() .AppendFormat("{1}", rightArrayItems) .BracketClose(); } return(sb.BracketClose() .ToString()); }