private void UpdateCodeBox() { CodeBox.Clear(); int index = PassesListBox.SelectedIndex; if (index == -1) { return; } TextSection section = (TextSection)PassesListBox.SelectedItem; TextSection prior = index == 0 ? null : PassesListBox.Items[index - 1] as TextSection; if (prior == null || section.Text == prior.Text || RightButton.Checked) { CodeBox.Text = section.Text; } else if (LeftButton.Checked) { CodeBox.Text = (prior == null) ? "(no prior text)" : prior.Text; } else { TextSection.RunDiff(prior, section, CodeBox); } CodeBox.Modified = false; InvalidateApplyChanges(); }
private void ApplyChangesButton_Click(object sender, EventArgs e) { // Turn the text into the expected encoding. IDxcBlobEncoding sourceBlob = EditorForm.CreateBlobForText(this.Library, this.CodeBox.Text); sourceBlob = this.Library.GetBlobAstUf8(sourceBlob); IDxcBlob bitcodeBlob = sourceBlob; List <string> passes = new List <string>(); passes.Add("hlsl-passes-resume"); for (int i = PassesListBox.SelectedIndex; i < PassesListBox.Items.Count; ++i) { passes.Add(((TextSection)PassesListBox.Items[i]).Title); } string[] options = EditorForm.CreatePassOptions(passes, false, true); EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, bitcodeBlob); if (!opt.Succeeded) { MessageBox.Show("Failed to optimize: " + opt.ResultText); return; } OptEditorForm form = new OptEditorForm(); form.CodeFont = this.CodeBox.Font; form.Library = this.Library; form.HighLevelSource = this.HighLevelSource; form.Sections = TextSection.EnumerateSections(new string[] { "MODULE-PRINT", "Phase:" }, opt.ResultText).ToArray(); form.StartPosition = FormStartPosition.CenterParent; form.Show(this); }
private void btnViewCFGOnly_Click(object sender, EventArgs e) { if (PassesListBox.SelectedIndex == -1) { MessageBox.Show("Select a pass first"); return; } TextSection section = (TextSection)PassesListBox.SelectedItem; var source = EditorForm.CreateBlobForText(this.Library, section.Text); source = this.Library.GetBlobAstUf8(source); string[] options = new string [1]; options[0] = "-view-cfg-only"; EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, source); if (!opt.Succeeded) { MessageBox.Show("Failed to optimize: " + opt.ResultText); return; } string dotText = opt.ResultText.Substring(opt.ResultText.IndexOf("digraph")); EditorForm.LogContextMenuHelper.ShowDot(dotText); }
private void btnSaveAll_Click(object sender, EventArgs e) { saveFileDialog1.ShowDialog(); string fileName = saveFileDialog1.FileName; for (int i = 0; i < sections.Length; i++) { TextSection Section = sections[i]; string fullName = string.Format("{0}_{1}_{2}.ll", fileName, i, Section.Title); System.IO.File.WriteAllLines(fullName, Section.Lines); } }
private void ApplyChangesButton_Click(object sender, EventArgs e) { // Turn the text into a container. IDxcBlobEncoding sourceBlob = EditorForm.CreateBlobForText(this.Library, this.CodeBox.Text); EditorForm.AssembleResult assembleResult = EditorForm.RunAssembly(this.Library, sourceBlob); if (assembleResult.Blob == null) { MessageBox.Show("Failed to assemble: " + assembleResult.ResultText); return; } // Extract the bitcode portion. const uint DxilKind = 0x4c495844; // 'LIXD' - DXIL uint index; IDxcContainerReflection reflection = HlslDxcLib.CreateDxcContainerReflection(); reflection.Load(assembleResult.Blob); reflection.FindFirstPartKind(DxilKind, out index); IDxcBlob bitcodeBlob = reflection.GetPartContent(index); List <string> passes = new List <string>(); passes.Add("hlsl-passes-resume"); for (int i = PassesListBox.SelectedIndex; i < PassesListBox.Items.Count; ++i) { passes.Add(((TextSection)PassesListBox.Items[i]).Title); } string[] options = EditorForm.CreatePassOptions(passes, false, true); EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, bitcodeBlob); if (!opt.Succeeded) { MessageBox.Show("Failed to optimize: " + opt.ResultText); return; } OptEditorForm form = new OptEditorForm(); form.CodeFont = this.CodeBox.Font; form.Library = this.Library; form.HighLevelSource = this.HighLevelSource; form.Sections = TextSection.EnumerateSections(new string[] { "MODULE-PRINT", "Phase:" }, opt.ResultText).ToArray(); form.StartPosition = FormStartPosition.CenterParent; form.Show(this); }
public static void RunDiff(TextSection oldText, TextSection newText, RichTextBox rtb) { // Longest common subsequence, simple edition. If/when something faster is needed, // should probably take a dependency on a proper diff package. Other than shorter // comparison windows, other things to look for are avoiding creating strings here, // working on the RichTextBox buffer directly for color, and unique'ing lines. string[] oldLines = oldText.Lines; string[] newLines = newText.Lines; // Reduce strings to hashes. int[] oldHashes = oldText.LineHashes; int[] newHashes = newText.LineHashes; // Reduce by trimming prefix and suffix. int diffStart = 0; while (diffStart < oldHashes.Length && diffStart < newHashes.Length && oldHashes[diffStart] == newHashes[diffStart]) { diffStart++; } int newDiffEndExc = newLines.Length, oldDiffEndExc = oldLines.Length; while (newDiffEndExc > diffStart && oldDiffEndExc > diffStart) { if (oldHashes[oldDiffEndExc - 1] == newHashes[newDiffEndExc - 1]) { oldDiffEndExc--; newDiffEndExc--; } else { break; } } int suffixLength = (newLines.Length - newDiffEndExc); // Build LCS table. int oldLen = oldDiffEndExc - diffStart, newLen = newDiffEndExc - diffStart; int[,] lcs = new int[oldLen + 1, newLen + 1]; // already zero-initialized for (int i = 0; i < oldLen; i++) { for (int j = 0; j < newLen; j++) { if (oldHashes[i + diffStart] == newHashes[j + diffStart]) { lcs[i + 1, j + 1] = lcs[i, j] + 1; } else { lcs[i + 1, j + 1] = Math.Max(lcs[i, j + 1], lcs[i + 1, j]); } } } // Print the diff - common prefix, backtracked diff and common suffix. rtb.AppendLines(" ", newLines, 0, diffStart, Color.White); { int i = oldLen, j = newLen; Stack <string> o = new Stack <string>(); for (;;) { if (i > 0 && j > 0 && oldHashes[diffStart + i - 1] == newHashes[diffStart + j - 1]) { o.Push(" " + oldLines[diffStart + i - 1]); i--; j--; } else if (j > 0 && (i == 0 || lcs[i, j - 1] >= lcs[i - 1, j])) { o.Push("+ " + newLines[diffStart + j - 1]); j--; } else if (i > 0 && (j == 0 || lcs[i, j - 1] < lcs[i - 1, j])) { o.Push("- " + oldLines[diffStart + i - 1]); i--; } else { break; } } while (o.Count != 0) { string line = o.Pop(); Color c = (line[0] == ' ') ? Color.White : ((line[0] == '+') ? Color.Yellow : Color.Red); rtb.AppendLine(line, c); } } rtb.AppendLines(" ", newLines, newDiffEndExc, (newLines.Length - newDiffEndExc), Color.White); }