forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 33
/
html.go
471 lines (404 loc) · 11.2 KB
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssa
import (
"bytes"
"fmt"
"html"
"io"
"os"
)
type HTMLWriter struct {
Logger
*os.File
}
func NewHTMLWriter(path string, logger Logger, funcname string) *HTMLWriter {
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
logger.Fatalf(0, "%v", err)
}
html := HTMLWriter{File: out, Logger: logger}
html.start(funcname)
return &html
}
func (w *HTMLWriter) start(name string) {
if w == nil {
return
}
w.WriteString("<html>")
w.WriteString(`<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style>
#helplink {
margin-bottom: 15px;
display: block;
margin-top: -15px;
}
#help {
display: none;
}
.stats {
font-size: 60%;
}
table {
border: 1px solid black;
table-layout: fixed;
width: 300px;
}
th, td {
border: 1px solid black;
overflow: hidden;
width: 400px;
vertical-align: top;
padding: 5px;
}
li {
list-style-type: none;
}
li.ssa-long-value {
text-indent: -2em; /* indent wrapped lines */
}
li.ssa-value-list {
display: inline;
}
li.ssa-start-block {
padding: 0;
margin: 0;
}
li.ssa-end-block {
padding: 0;
margin: 0;
}
ul.ssa-print-func {
padding-left: 0;
}
dl.ssa-gen {
padding-left: 0;
}
dt.ssa-prog-src {
padding: 0;
margin: 0;
float: left;
width: 4em;
}
dd.ssa-prog {
padding: 0;
margin-right: 0;
margin-left: 4em;
}
.dead-value {
color: gray;
}
.dead-block {
opacity: 0.5;
}
.depcycle {
font-style: italic;
}
.highlight-yellow { background-color: yellow; }
.highlight-aquamarine { background-color: aquamarine; }
.highlight-coral { background-color: coral; }
.highlight-lightpink { background-color: lightpink; }
.highlight-lightsteelblue { background-color: lightsteelblue; }
.highlight-palegreen { background-color: palegreen; }
.highlight-powderblue { background-color: powderblue; }
.highlight-lightgray { background-color: lightgray; }
.outline-blue { outline: blue solid 2px; }
.outline-red { outline: red solid 2px; }
.outline-blueviolet { outline: blueviolet solid 2px; }
.outline-darkolivegreen { outline: darkolivegreen solid 2px; }
.outline-fuchsia { outline: fuchsia solid 2px; }
.outline-sienna { outline: sienna solid 2px; }
.outline-gold { outline: gold solid 2px; }
</style>
<script type="text/javascript">
// ordered list of all available highlight colors
var highlights = [
"highlight-yellow",
"highlight-aquamarine",
"highlight-coral",
"highlight-lightpink",
"highlight-lightsteelblue",
"highlight-palegreen",
"highlight-lightgray"
];
// state: which value is highlighted this color?
var highlighted = {};
for (var i = 0; i < highlights.length; i++) {
highlighted[highlights[i]] = "";
}
// ordered list of all available outline colors
var outlines = [
"outline-blue",
"outline-red",
"outline-blueviolet",
"outline-darkolivegreen",
"outline-fuchsia",
"outline-sienna",
"outline-gold"
];
// state: which value is outlined this color?
var outlined = {};
for (var i = 0; i < outlines.length; i++) {
outlined[outlines[i]] = "";
}
window.onload = function() {
var ssaElemClicked = function(elem, event, selections, selected) {
event.stopPropagation()
// TODO: pushState with updated state and read it on page load,
// so that state can survive across reloads
// find all values with the same name
var c = elem.classList.item(0);
var x = document.getElementsByClassName(c);
// if selected, remove selections from all of them
// otherwise, attempt to add
var remove = "";
for (var i = 0; i < selections.length; i++) {
var color = selections[i];
if (selected[color] == c) {
remove = color;
break;
}
}
if (remove != "") {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove(remove);
}
selected[remove] = "";
return;
}
// we're adding a selection
// find first available color
var avail = "";
for (var i = 0; i < selections.length; i++) {
var color = selections[i];
if (selected[color] == "") {
avail = color;
break;
}
}
if (avail == "") {
alert("out of selection colors; go add more");
return;
}
// set that as the selection
for (var i = 0; i < x.length; i++) {
x[i].classList.add(avail);
}
selected[avail] = c;
};
var ssaValueClicked = function(event) {
ssaElemClicked(this, event, highlights, highlighted);
}
var ssaBlockClicked = function(event) {
ssaElemClicked(this, event, outlines, outlined);
}
var ssavalues = document.getElementsByClassName("ssa-value");
for (var i = 0; i < ssavalues.length; i++) {
ssavalues[i].addEventListener('click', ssaValueClicked);
}
var ssalongvalues = document.getElementsByClassName("ssa-long-value");
for (var i = 0; i < ssalongvalues.length; i++) {
// don't attach listeners to li nodes, just the spans they contain
if (ssalongvalues[i].nodeName == "SPAN") {
ssalongvalues[i].addEventListener('click', ssaValueClicked);
}
}
var ssablocks = document.getElementsByClassName("ssa-block");
for (var i = 0; i < ssablocks.length; i++) {
ssablocks[i].addEventListener('click', ssaBlockClicked);
}
};
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
</head>`)
// TODO: Add javascript click handlers for blocks
// to outline that block across all phases
w.WriteString("<body>")
w.WriteString("<h1>")
w.WriteString(html.EscapeString(name))
w.WriteString("</h1>")
w.WriteString(`
<a href="#" onclick="toggle_visibility('help');" id="helplink">help</a>
<div id="help">
<p>
Click on a value or block to toggle highlighting of that value/block and its uses.
Values and blocks are highlighted by ID, which may vary across passes.
(TODO: Fix this.)
</p>
<p>
Faded out values and blocks are dead code that has not been eliminated.
</p>
<p>
Values printed in italics have a dependency cycle.
</p>
</div>
`)
w.WriteString("<table>")
w.WriteString("<tr>")
}
func (w *HTMLWriter) Close() {
if w == nil {
return
}
w.WriteString("</tr>")
w.WriteString("</table>")
w.WriteString("</body>")
w.WriteString("</html>")
w.File.Close()
}
// WriteFunc writes f in a column headed by title.
func (w *HTMLWriter) WriteFunc(title string, f *Func) {
if w == nil {
return // avoid generating HTML just to discard it
}
w.WriteColumn(title, f.HTML())
// TODO: Add visual representation of f's CFG.
}
// WriteColumn writes raw HTML in a column headed by title.
// It is intended for pre- and post-compilation log output.
func (w *HTMLWriter) WriteColumn(title string, html string) {
if w == nil {
return
}
w.WriteString("<td>")
w.WriteString("<h2>" + title + "</h2>")
w.WriteString(html)
w.WriteString("</td>")
}
func (w *HTMLWriter) Printf(msg string, v ...interface{}) {
if _, err := fmt.Fprintf(w.File, msg, v...); err != nil {
w.Fatalf(0, "%v", err)
}
}
func (w *HTMLWriter) WriteString(s string) {
if _, err := w.File.WriteString(s); err != nil {
w.Fatalf(0, "%v", err)
}
}
func (v *Value) HTML() string {
// TODO: Using the value ID as the class ignores the fact
// that value IDs get recycled and that some values
// are transmuted into other values.
return fmt.Sprintf("<span class=\"%[1]s ssa-value\">%[1]s</span>", v.String())
}
func (v *Value) LongHTML() string {
// TODO: Any intra-value formatting?
// I'm wary of adding too much visual noise,
// but a little bit might be valuable.
// We already have visual noise in the form of punctuation
// maybe we could replace some of that with formatting.
s := fmt.Sprintf("<span class=\"%s ssa-long-value\">", v.String())
s += fmt.Sprintf("%s = %s", v.HTML(), v.Op.String())
s += " <" + html.EscapeString(v.Type.String()) + ">"
s += html.EscapeString(v.auxString())
for _, a := range v.Args {
s += fmt.Sprintf(" %s", a.HTML())
}
r := v.Block.Func.RegAlloc
if int(v.ID) < len(r) && r[v.ID] != nil {
s += " : " + r[v.ID].Name()
}
s += "</span>"
return s
}
func (b *Block) HTML() string {
// TODO: Using the value ID as the class ignores the fact
// that value IDs get recycled and that some values
// are transmuted into other values.
return fmt.Sprintf("<span class=\"%[1]s ssa-block\">%[1]s</span>", html.EscapeString(b.String()))
}
func (b *Block) LongHTML() string {
// TODO: improve this for HTML?
s := fmt.Sprintf("<span class=\"%s ssa-block\">%s</span>", html.EscapeString(b.String()), html.EscapeString(b.Kind.String()))
if b.Aux != nil {
s += html.EscapeString(fmt.Sprintf(" {%v}", b.Aux))
}
if b.Control != nil {
s += fmt.Sprintf(" %s", b.Control.HTML())
}
if len(b.Succs) > 0 {
s += " →" // right arrow
for _, e := range b.Succs {
c := e.b
s += " " + c.HTML()
}
}
switch b.Likely {
case BranchUnlikely:
s += " (unlikely)"
case BranchLikely:
s += " (likely)"
}
return s
}
func (f *Func) HTML() string {
var buf bytes.Buffer
fmt.Fprint(&buf, "<code>")
p := htmlFuncPrinter{w: &buf}
fprintFunc(p, f)
// fprintFunc(&buf, f) // TODO: HTML, not text, <br /> for line breaks, etc.
fmt.Fprint(&buf, "</code>")
return buf.String()
}
type htmlFuncPrinter struct {
w io.Writer
}
func (p htmlFuncPrinter) header(f *Func) {}
func (p htmlFuncPrinter) startBlock(b *Block, reachable bool) {
// TODO: Make blocks collapsable?
var dead string
if !reachable {
dead = "dead-block"
}
fmt.Fprintf(p.w, "<ul class=\"%s ssa-print-func %s\">", b, dead)
fmt.Fprintf(p.w, "<li class=\"ssa-start-block\">%s:", b.HTML())
if len(b.Preds) > 0 {
io.WriteString(p.w, " ←") // left arrow
for _, e := range b.Preds {
pred := e.b
fmt.Fprintf(p.w, " %s", pred.HTML())
}
}
io.WriteString(p.w, "</li>")
if len(b.Values) > 0 { // start list of values
io.WriteString(p.w, "<li class=\"ssa-value-list\">")
io.WriteString(p.w, "<ul>")
}
}
func (p htmlFuncPrinter) endBlock(b *Block) {
if len(b.Values) > 0 { // end list of values
io.WriteString(p.w, "</ul>")
io.WriteString(p.w, "</li>")
}
io.WriteString(p.w, "<li class=\"ssa-end-block\">")
fmt.Fprint(p.w, b.LongHTML())
io.WriteString(p.w, "</li>")
io.WriteString(p.w, "</ul>")
// io.WriteString(p.w, "</span>")
}
func (p htmlFuncPrinter) value(v *Value, live bool) {
var dead string
if !live {
dead = "dead-value"
}
fmt.Fprintf(p.w, "<li class=\"ssa-long-value %s\">", dead)
fmt.Fprint(p.w, v.LongHTML())
io.WriteString(p.w, "</li>")
}
func (p htmlFuncPrinter) startDepCycle() {
fmt.Fprintln(p.w, "<span class=\"depcycle\">")
}
func (p htmlFuncPrinter) endDepCycle() {
fmt.Fprintln(p.w, "</span>")
}
func (p htmlFuncPrinter) named(n LocalSlot, vals []*Value) {
// TODO
}