1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package hat.tools.textmodel.ui;
26
27 import hat.tools.jdot.DotBuilder;
28 import hat.tools.jdot.ui.JDot;
29 import hat.tools.textmodel.BabylonTextModel;
30 import jdk.incubator.code.dialect.core.CoreOp;
31
32 import javax.swing.BoxLayout;
33 import javax.swing.JFrame;
34 import javax.swing.JPanel;
35 import javax.swing.JSplitPane;
36 import javax.swing.SwingUtilities;
37 import java.awt.BorderLayout;
38 import java.awt.Font;
39 import java.io.IOException;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 public class SSAIDViewer extends JPanel {
48
49 public SSAIDViewer(BabylonTextModel cr) {
50 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
51 var font = new Font("Monospaced", Font.PLAIN, 14);
52 var styleMapper = new BabylonStyleMapper( new FuncOpTextModelViewer.FuncOpTextPane(font,false), false);
53 var funcOpTextModelViewer = new FuncOpTextModelViewer(cr, styleMapper);
54
55 var dotViewer = new JDot(DotBuilder.dotDigraph("name", g -> {
56 g.nodeShape("record");
57 cr.ssaEdgeList.forEach(edge -> {
58 var ssaDef = edge.ssaDef();
59 String def = "%" + ssaDef.id;
60 g.record(def, def);
61 });
62 cr.ssaEdgeList.forEach(edge -> {
63 var ssaDef = edge.ssaDef();
64 String def = "%" + ssaDef.id;
65 int line = ssaDef.pos().line();
66 cr.ssaEdgeList.forEach(e -> {
67 var ssaRef = e.ssaRef();
68 if (ssaRef.pos().line() == line) {
69 String ref = "%" + ssaRef.id;
70 g.edge(def, ref);
71 }
72 });
73 });
74 }));
75 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
76 splitPane.setLeftComponent(funcOpTextModelViewer.scrollPane);
77 splitPane.setRightComponent(dotViewer.pane);
78 add(splitPane);
79 }
80
81 public static void launch(BabylonTextModel crDoc) {
82 SwingUtilities.invokeLater(() -> {
83 var viewer = new SSAIDViewer(crDoc);
84 var frame = new JFrame();
85 frame.setLayout(new BorderLayout());
86 frame.getContentPane().add(viewer);
87 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
88 frame.pack();
89 frame.setVisible(true);
90 });
91
92 }
93
94 public static void launch(CoreOp.FuncOp javaFunc) {
95 BabylonTextModel crDoc = BabylonTextModel.of(javaFunc);
96 launch(crDoc);
97 }
98
99 public static void launch(Path path) throws IOException {
100 BabylonTextModel crDoc = BabylonTextModel.of(Files.readString(path));
101 launch(crDoc);
102 }
103
104 public static void main(String[] args) throws IOException {
105 SSAIDViewer.launch(Path.of(args[0]));
106 }
107 }
108