public static void DoGenerate() { var edsl_base = @"..\..\..\..\Libptx.Edsl\Libptx.Edsl\"; var dir_sregs = edsl_base + @"Expressions\Sregs\"; Func<String, String> dir2ns = dir => dir.Replace(@"..\..\..\..\Libptx.Edsl\", String.Empty).Replace(@"\", ".").Slice(0, -1); foreach (var t in Sregs.All) { var buf = new StringBuilder(); var w = new StringWriter(buf).Indented(); w.WriteLine("using Libptx.Edsl.Common.Types.Scalar;"); w.WriteLine("using Libptx.Edsl.Common.Types.Vector;"); w.WriteLineNoTabs(String.Empty); w.WriteLine("namespace {0}", dir2ns(dir_sregs)); w.WriteLine("{"); w.Indent++; w.WriteLine("public class {0} : {1}, sreg", t.Name, t.FullName); w.WriteLine("{"); w.Indent++; var xid = t.Name == "tid" || t.Name == "ntid" || t.Name == "ctaid" || t.Name == "nctaid"; if (xid) { var post_20 = Context.Current.Version >= SoftwareIsa.PTX_20; if (post_20) { w.EmitTypeSpec(t.Name, new Type { Name = TypeName.U16, Mod = TypeMod.V4 }, Space.Other); w.EmitTypeSpec(t.Name, new Type { Name = TypeName.U32, Mod = TypeMod.V4 }, Space.Other); } else { w.EmitTypeSpec(t.Name, new Type { Name = TypeName.U16, Mod = TypeMod.V4 }, Space.Other); } } else { var type = t.Attr<SregAttribute>().Type; w.EmitTypeSpec(t.Name, type, Space.Other); } // todo. also emit same stuff as gets emitted for non-reg vars of appropriate type // e.g. .x, .y, .z accessors for grid-related special registers w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); var fname = dir_sregs + t.Name + ".cs"; File.WriteAllText(fname, buf.ToString()); } }
public static void DoGenerate() { foreach (var op in Ptxops.All) { var dir = @"..\..\..\..\" + op.Namespace.Replace(".", @"\") + @"\"; var file = dir + op.Name + ".cs"; var text = File.ReadAllText(file); Action<String> use = ns => { if (!text.Contains("using " + ns + ";")) { var liof = text.LastIndexOf("using") + 1; var next = text.IndexOf(Environment.NewLine, liof); var ins = next == -1 ? 0 : (next + Environment.NewLine.Length); text = text.Insert(ins, "using " + ns + ";" + Environment.NewLine); } }; var buf = new StringBuilder(); var w = new StringWriter(buf).Indented(); w.Indent += 2; w.WriteLineNoTabs(String.Empty); w.WriteLineNoTabs(String.Empty); var names = op.Signatures().SelectMany(s => { var bal = 0; var eof = s.TakeWhile(c => { if (c == '{') bal++; if (c == '}') bal--; if (bal == 0 && c == ' ') return false; return true; }).Count(); var arg_list = s.Slice(eof + 1).Trim(); arg_list = arg_list.Extract("^(?<name>.*?);?$"); var parsed_args = arg_list.Split(",".MkArray(), StringSplitOptions.RemoveEmptyEntries).Select(arg => { arg = arg.Trim().Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", ""); var parsed = arg.Trim().Parse(@"^(?<prefix>[!-])?(?<name>[\w\d]+)(\|(?<othername>[\w\d]+))?(\.(?<suffix>[\w\d]+))?$"); return parsed["name"]; }).ToReadOnly(); return parsed_args; }).Distinct().ToReadOnly(); if (names.IsEmpty()) continue; names.ForEach(name => { use("Libptx.Expressions"); var decl = String.Format("public Expression {0} {{ get; set; }}", name); if (text.Contains(decl)) { var iof_decl = text.IndexOf(decl); iof_decl -= " ".Length; iof_decl -= Environment.NewLine.Length * 2; var iof_brace = text.IndexOf("}", iof_decl); text = text.Remove(iof_decl, iof_brace - iof_decl + 1); } w.WriteLine(decl); }); w.WriteLineNoTabs(String.Empty); use("Libcuda.Versions"); w.WriteLine("protected override void custom_validate_ops(SoftwareIsa target_swisa, HardwareIsa target_hwisa)"); w.WriteLine("{"); w.Indent++; names.ForEach(name => { var props = op.GetProperties(BF.PublicInstance).Where(p => p.PropertyType == typeof(Type)).Select(p => p.Name).ToHashSet(); var prop = "N/A" as String; if (props.Contains(name + "type")) prop = name + "type"; if (props.Contains("type")) prop = "type"; use("XenoGears.Assertions"); w.WriteLine("agree({0}, {1}).AssertTrue();", name, prop); }); w.Indent--; w.Write("}"); var iof = text.IndicesOf(c => c == '}').ThirdLastOrDefault(-1); if (iof == -1) continue; text = text.Insert(iof + 1, buf.ToString()); File.WriteAllText(file, text); } }
public static void DoGenerate() { var edsl_base = @"..\..\..\..\Libptx.Edsl\Libptx.Edsl\"; var dir_types = edsl_base + @"Common\Types\"; Func<String, String> dir2ns = dir => { var rel = dir.Replace(@"..\..\..\..\Libptx.Edsl\", String.Empty); rel = (rel + @"\").Unfold(s => s.Slice(0, -1), s => s.EndsWith(@"\")).Last(); return rel.Replace(@"\", ".").Slice(0, -1); }; Types.Opaque.ForEach(t => { var dir_opaques = dir_types + @"Opaque\"; var buf = new StringBuilder(); var w = new StringWriter(buf).Indented(); w.WriteLine("using {0};", typeof(Expression).Namespace); w.WriteLineNoTabs(String.Empty); w.WriteLine("namespace {0}", dir2ns(dir_opaques)); w.WriteLine("{"); w.Indent++; w.WriteLine("public partial class {0} : typed_expr", t); w.WriteLine("{"); w.Indent++; w.WriteLine("public {0}(Expression expr)", t); w.Indent++; w.WriteLine(": base(expr)"); w.Indent--; w.WriteLine("{"); w.Indent++; w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); var fname = dir_opaques + t + ".cs"; if (!Directory.Exists(dir_opaques)) Directory.CreateDirectory(dir_opaques); File.WriteAllText(fname, buf.ToString()); }); Types.Other.ForEach(t => { var dir_opaques = dir_types + @"Other\"; var buf = new StringBuilder(); var w = new StringWriter(buf).Indented(); w.WriteLine("using {0};", typeof(Expression).Namespace); w.WriteLineNoTabs(String.Empty); w.WriteLine("namespace {0}", dir2ns(dir_opaques)); w.WriteLine("{"); w.Indent++; w.WriteLine("public partial class {0} : typed_expr", t); w.WriteLine("{"); w.Indent++; w.WriteLine("public {0}(Expression expr)", t); w.Indent++; w.WriteLine(": base(expr)"); w.Indent--; w.WriteLine("{"); w.Indent++; w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); var fname = dir_opaques + t + ".cs"; if (!Directory.Exists(dir_opaques)) Directory.CreateDirectory(dir_opaques); File.WriteAllText(fname, buf.ToString()); }); var scalars = Combinatorics.CartesianProduct(Types.Scalar, new []{ null, "reg" }, new []{null, "relaxed"}).ToReadOnly(); scalars.Zip((t, mod1, mod2) => { if (mod1 == null && mod2 == "relaxed") return; var mod = mod1 == null && mod2 == null ? null : mod1 == "reg" && mod2 == null ? "reg" : mod1 == "reg" && mod2 == "relaxed" ? "relaxed_reg" : ((Func<String>)(() => { throw AssertionHelper.Fail(); }))(); var name = t.Name.Signature(); if (mod != null) name = mod + "_" + name; var dir = dir_types + @"Scalar\"; var buf = new StringBuilder(); var w = new StringWriter(buf).Indented(); w.WriteLine("using {0};", typeof(Expression).Namespace); w.WriteLineNoTabs(String.Empty); w.WriteLine("namespace {0}", dir2ns(dir)); w.WriteLine("{"); w.Indent++; w.WriteLine("public partial class {0} : typed_expr", name); w.WriteLine("{"); w.Indent++; w.WriteLine("public {0}(Expression expr)", name); w.Indent++; w.WriteLine(": base(expr)"); w.Indent--; w.WriteLine("{"); w.Indent++; w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); var fname = dir + name + ".cs"; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); File.WriteAllText(fname, buf.ToString()); }); var vectors = Combinatorics.CartesianProduct(Types.Vector, new []{ null, "reg" }, new []{null, "relaxed"}).ToReadOnly(); vectors.Zip((t, mod1, mod2) => { if (mod1 == null && mod2 == "relaxed") return; var mod = mod1 == null && mod2 == null ? null : mod1 == "reg" && mod2 == null ? "reg" : mod1 == "reg" && mod2 == "relaxed" ? "relaxed_reg" : ((Func<String>)(() => { throw AssertionHelper.Fail(); }))(); var name = String.Format("v{0}_{1}", t.vec_rank(), t.vec_el().Name.Signature()); if (mod != null) name = mod + "_" + name; var dir = dir_types + @"Vector\"; var buf = new StringBuilder(); var w = new StringWriter(buf).Indented(); w.WriteLine("using {0};", typeof(Expression).Namespace); w.WriteLineNoTabs(String.Empty); w.WriteLine("namespace {0}", dir2ns(dir)); w.WriteLine("{"); w.Indent++; w.WriteLine("public partial class {0} : typed_expr", name); w.WriteLine("{"); w.Indent++; w.WriteLine("public {0}(Expression expr)", name); w.Indent++; w.WriteLine(": base(expr)"); w.Indent--; w.WriteLine("{"); w.Indent++; w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); var fname = dir + name + ".cs"; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); File.WriteAllText(fname, buf.ToString()); }); }
public static void DoGenerate() { var edsl_base = @"..\..\..\..\Libptx.Edsl\Libptx.Edsl\"; var dir_vector = edsl_base + @"Expressions\Immediate\"; var dir_vectors = edsl_base + @"Expressions\Immediate\Vectors\"; Func<String, String> dir2ns = dir => { var rel = dir.Replace(@"..\..\..\..\Libptx.Edsl\", String.Empty); rel = (rel + @"\").Unfold(s => s.Slice(0, -1), s => s.EndsWith(@"\")).Last(); return rel.Replace(@"\", ".").Slice(0, -1); }; var shortcuts = new Dictionary<int, List<String>>(); new []{1, 2, 4}.ForEach(i => shortcuts.Add(i, new List<String>())); Types.Vector.ForEach(t => { t.is_vec().AssertTrue(); var name = String.Format("v{0}_{1}", t.vec_rank(), t.vec_el().Name.Signature()); var buf_vec = new StringBuilder(); var w_vec = new StringWriter(buf_vec).Indented(); w_vec.WriteLine("using {0};", typeof(AssertionHelper).Namespace); w_vec.WriteLine("using {0};", typeof(Libptx.Edsl.Expressions.Slots.var).Namespace); w_vec.WriteLine("using Libptx.Edsl.Common.Types.Scalar;"); w_vec.WriteLineNoTabs(String.Empty); w_vec.WriteLine("namespace {0}", dir2ns(dir_vectors)); w_vec.WriteLine("{"); w_vec.Indent++; w_vec.WriteLine("public class {0} : vector", name); w_vec.WriteLine("{"); w_vec.Indent++; Func<int, String> arg_name = i => i == 1 ? "x" : i == 2 ? "y" : i == 3 ? "z" : i == 4 ? "w" : ((Func<String>)(() => { throw AssertionHelper.Fail(); }))(); var args = 1.UpTo(t.vec_rank()).Select(i => String.Format("reg_{0} {1}", t.vec_el().Name.Signature(), arg_name(i))).StringJoin(", "); w_vec.WriteLine("public {0}({1})", name, args); var arg_names = 1.UpTo(t.vec_rank()).Select(arg_name).StringJoin(", "); shortcuts[t.vec_rank()].Add(String.Format("public static {0} v{1}({2}) {{ return new {0}({3}); }}", name, t.vec_rank(), args, arg_names)); w_vec.WriteLine("{"); w_vec.Indent++; w_vec.WriteLine("ElementType = {0};", t.vec_el().Name.Signature()); 1.UpTo(t.vec_rank()).ForEach(i => w_vec.WriteLine("Elements.Add({0}.AssertCast<var>());", arg_name(i))); w_vec.Indent--; w_vec.WriteLine("}"); w_vec.WriteLineNoTabs(String.Empty); w_vec.EmitTypeSpec(name, t, Space.Reg, true); w_vec.Indent--; w_vec.WriteLine("}"); w_vec.Indent--; w_vec.WriteLine("}"); var fname_reg = dir_vectors + name + ".cs"; if (!Directory.Exists(dir_vectors)) Directory.CreateDirectory(dir_vectors); File.WriteAllText(fname_reg, buf_vec.ToString()); }); var buf = new StringBuilder(); var w = new StringWriter(buf).Indented(); w.WriteLine("using {0};", typeof(Vector).Namespace); w.WriteLine("using Libptx.Edsl.Common.Types.Scalar;"); w.WriteLine("using {0};", dir2ns(dir_vectors)); w.WriteLineNoTabs(String.Empty); w.WriteLine("namespace {0}", dir2ns(dir_vector)); w.WriteLine("{"); w.Indent++; w.WriteLine("public class vector : Vector"); w.WriteLine("{"); w.Indent++; var write_shortcuts = shortcuts.Keys.Select<int, Action>(i => () => shortcuts[i].ForEach(w.WriteLine)); var write_empty_lines = Seq.Nats.Select<int, Action>(_ => () => w.WriteLineNoTabs(String.Empty)); write_shortcuts.Intersperse(write_empty_lines).SkipLast(1).RunEach(); w.Indent--; w.WriteLine("}"); w.Indent--; w.WriteLine("}"); var fname = dir_vector + "vector.cs"; if (!Directory.Exists(dir_vector)) Directory.CreateDirectory(dir_vector); File.WriteAllText(fname, buf.ToString()); }