Commits

Maciej Fijalkowski committed 9c1a8b0

a first draft of new ideas for codeviewer/jitviewer

  • Participants
  • Parent commits fa3119d

Comments (0)

Files changed (6)

File bench/numeric/arr.py

 
 try:
     import pypyjit
-#    pypyjit.set_optimize_hook(hook)
 except:
     pass
 

File codeviewer/dump.py

+
+import json
+from pypy.translator.goal import richards
+from lib_pypy.disassembler import dis
+from pypy.tool.jitlogparser.module_finder import gather_all_code_objs
+
+def load_code(fname):
+    res = gather_all_code_objs(fname)
+    return res
+
+codes = gather_all_code_objs(richards.__file__)
+lines = {}
+for code in codes.values():
+    for opcode in dis(code).opcodes:
+        lines.setdefault(opcode.lineno, []).append(str(opcode))
+json.dump({'lines': lines, 'count': max(lines)}, open("info.json", "w"))

File codeviewer/x.css

+.active {
+    background-color: #AAEEEE;
+}
+
+.main {
+    padding-right: 330px;
+}
+
+.rightpan {
+    position: fixed;
+    top: 20px;
+    right: 20px;
+    width: 300px;
+    height: 500px;
+}

File codeviewer/x.jinja

+<html>
+<head>
+   <script type="text/javascript" src="jquery.min.js"></script>
+   <script type="text/javascript" src="jquery.scrollTo-1.4.2-min.js"></script>
+   <script type="text/javascript" src="jquery.viewport.mini.js"></script>
+   <script type="text/javascript" src="x.js"></script>
+   <link rel="stylesheet" href="pygments.css">
+   <link rel="stylesheet" href="x.css">
+</head>
+<body>
+  <div class="main">
+  {{ source }}
+  </div>
+  <div class="rightpan">
+    <div id="container">
+      </div>
+  </div>
+</body>
+</html>

File codeviewer/x.js

+
+function KEYS() {
+    this.DOWN = 40;
+    this.UP   = 38;
+    this.CTRL = 17;
+    this.PAGE_UP = 33;
+    this.PAGE_DOWN = 34;
+    this.L = 76;
+    
+    this.accepts = function(key) {
+        return (key == this.DOWN || key == this.UP || key == this.CTRL ||
+                key == this.PAGE_UP || key == this.PAGE_DOWN || key == this.L);
+    };
+
+    this.arrows = function(key) {
+        return key == this.DOWN || key == this.UP;
+    };
+
+    this.pages = function(key) {
+        return key == this.PAGE_UP || key == this.PAGE_DOWN;
+    };
+
+    this.ctrl_pressed = false;
+
+}
+keys = new KEYS();
+
+MIN_POSITION = 100;
+prev_style = null;
+single_element_height = null;
+MAX_NUM = 0;
+
+function hex(v) {
+    return Math.round(v * 255).toString(16);
+}
+
+function colorize(v) {
+    var red, green, blue;
+    if (v > 1)
+        v = 1;
+    if (v < 0)
+        v = 0;
+    v = v*v;
+    v = ((1 - v) * 0x88 / 0x100) + (0xF8 - 0x88) / 0x100;
+    return "#" + hex(v) + hex(v) + "F8";
+}
+
+function ondown(ev) {
+    var elem, pos, cur, ofs, counter = 0, num;
+    var keycode = ev.keyCode;
+
+    if (keys.accepts(keycode)) {
+        ev.preventDefault();
+        if (keycode == keys.CTRL) {
+            keys.ctrl_pressed = true;
+            return;
+        }
+        cur = $(".active");
+        if (keycode == keys.L && keys.ctrl_pressed) {
+            ofs = cur.offset().top - $(window).height() / 2;
+            if (ofs < 0) {
+                ofs = 0;
+            }
+            $.scrollTo(ofs);
+            return;
+        }
+        if (keys.arrows(keycode)) {
+            if (keycode == keys.UP) {
+                counter = -1;
+            } else {
+                counter = 1;
+            }
+        } else if (keys.pages(keycode)) {
+            pos = document.body.scrollTop;
+            if (keycode == keys.PAGE_UP) {
+                counter = -$(window).height() / single_element_height;
+            } else {
+                counter = $(window).height() / single_element_height;
+            }
+            counter = Math.round(counter);
+        }
+        if (counter != 0) {
+            elem = cur;
+            num = parseInt(elem.attr("id").substr(2)) + counter;
+            if (num < 1) {
+                num = 1;
+            }
+            if (num > MAX_NUM) {
+                num = MAX_NUM;
+            }
+            elem = $("#w-" + num);
+            // empty lines
+            activate(elem);
+            var per_line_info = lines[num];
+            var txt = "";
+            if (per_line_info) {
+                for (var i in per_line_info) {
+                    txt += per_line_info[i] + "<br/>";
+                }
+            }
+            $("#container").html(txt);
+            pos = elem.position().top - document.body.scrollTop;
+            if (pos < MIN_POSITION) {
+                $.scrollTo("-=" + (MIN_POSITION - pos), 0.5);
+            }
+            if (pos > $(window).height() - MIN_POSITION) {
+                $.scrollTo("+=" + (pos - $(window).height() + MIN_POSITION), 0.5);
+            }
+        }
+    }
+}
+
+function onup(ev) {
+    if (ev.keyCode == keys.CTRL) {
+        keys.ctrl_pressed = false;
+    }
+}
+
+function activate(elem) {
+    prev_elem = $(".active");
+    if (prev_elem) {
+        prev_elem.children().css("background-color", prev_style);
+        prev_elem.toggleClass("active");
+    }
+    prev_style = elem.children().css("background-color");
+    elem.children().css("background-color", "");
+    elem.toggleClass('active');
+}
+
+function load_data(per_line_info) {
+    var line;
+    lines = per_line_info['lines']; // just store it in a global
+    for (var i in lines) {
+        line = lines[i];
+        $("#" + i).css("background-color", colorize(line.length/20));
+    }
+    activate($("#w-1"));
+    single_element_height = $("#w-1").height();
+    MAX_NUM = per_line_info['count'];
+}
+
+$(document).ready(function () {
+    $(document).keydown(ondown);
+    $(document).keyup(onup);
+    $.getJSON("info.json", null, load_data);
+});

File codeviewer/xdemo.py

+from pygments import highlight
+from pygments.lexers import PythonLexer
+from pygments.formatters import HtmlFormatter
+from jinja2 import Template
+
+class CustomHtmlFormatter(HtmlFormatter):
+    def wrap(self, source, outfile):
+        total = 1
+        for i, t in HtmlFormatter.wrap(self, source, outfile):
+            if i:
+                if not t:
+                    t = " "
+                yield i, '<div id="w-%d" class="wrapper"><span class="line" id="%d">' % (total, total) + t + '</span></div>'
+                total += 1
+            else:
+                yield i, t
+
+code = open('/home/fijal/src/pypy/pypy/translator/goal/richards.py', 'r').read()
+with open('pygments.css', 'w') as f:
+    f.write(HtmlFormatter().get_style_defs('.highlight'))
+with open('x.html', 'w') as f:
+    source = highlight(code, PythonLexer(), CustomHtmlFormatter())
+    f.write(Template(open("x.jinja").read()).render(source=source))