This repository was archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyas-viz.js
467 lines (402 loc) · 17 KB
/
yas-viz.js
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
$(document).ready(function () {
var editor = ace.edit("editor");
var assembledCode = null;
var vm = null;
var renderHex = false;
// Takes an assembled code array produced by Yas.assemble and fills a table
// with the id "code" with it, one row per line
function renderCodeAsTable(code) {
// Add headers
$('#code').empty()
.append($('<thead />')
.append($('<tr />')
.append($('<th />').text('#'))
.append($('<th />').text('Address'))
.append($('<th />').text('Opcode'))
.append($('<th />').text('Source'))))
.append($('<tbody/>'));
// Takes an array of bytes and produces a string representation of it in
// hexadecimal
function opcodeArrayToString(opcodeArray) {
var str = '';
opcodeArray.forEach(function (entry) {
var hexRepresentation = entry.toString(16);
if (hexRepresentation.length === 1) {
hexRepresentation = '0' + hexRepresentation;
}
str += hexRepresentation;
});
return str;
}
// Go through each line and add a row to the table
code.forEach(function (line, index) {
var row = $('<tr />');
var lineNumber = $('<td />')
.attr('class', 'assembledLineNum')
.text((index + 1).toString());
var address = $('<td />')
.attr('class', 'assembledAddress')
.text('0x' + line.address.toString(16));
var opcode = $('<td />').attr('class', 'assembledOpcode');
if (line.opcode !== null) {
opcode.text('0x' + opcodeArrayToString(line.opcode));
}
var source = $('<td />')
.attr('class', 'assembledSource')
.append($('<code />').text(line.source));
row = row
.append(lineNumber)
.append(address)
.append(opcode)
.append(source);
$('#code > tbody').append(row);
});
}
// Search through the table and highlight the line with the given address
function highlightLine(addr) {
$('.highlightedLine').removeClass('highlightedLine');
$('#code tr').filter(function () {
return ($('.assembledAddress', this).text() ===
'0x' + addr.toString(16));
}).last().addClass('highlightedLine');
// Scroll to the highlighted line if it's not in view
if ($('.highlightedLine').length !== 0) {
$('#codeContainer').scrollTo($('.highlightedLine'), {
offset : -80
});
}
}
// Convert a number to either a hexadecimal or decimal number, based on the
// value of the renderHex variable
function numToPreferredString(num) {
if (renderHex) {
return '0x' + num.toString(16);
} else {
return num.toString(10);
}
}
// Replace the contents of any element with the refreshSVG class with its
// own content to force the browser to re-render any SVGs inside
function refreshSVG() {
$('.refreshSVG').each(function () {
$(this).html($(this).html());
});
}
// Calculate the maximum width of all of the strings passed, rendered in SVG
// with the given attributes
function calcMaxTextWidth(strings, attributes) {
// We first check cached values, because we don't want to recalculate
// something we've already calculated
this.cache = this.cache || {};
var key = JSON.stringify(Array.prototype.slice.call(arguments));
if (this.cache[key] !== undefined) {
return this.cache[key];
}
// Make a temporary div at the end of the body to do work with
var max = 0;
$('<div />').attr({
'id' : 'calcTextWidthTempDiv',
'class' : 'refreshSVG'
}).appendTo('body');
// Put an SVG in our temporary div
$('<svg />')
.attr('id', 'calcTextWidthTempSVG')
.appendTo($('#calcTextWidthTempDiv'));
// Render each string as a text element in our SVG and calculate its
// length
strings.forEach(function (str) {
$('<text />')
.attr('id', 'calcTextWidthTempText')
.attr(attributes)
.append(str)
.appendTo($('#calcTextWidthTempSVG'));
refreshSVG(); // We have to refresh the rendering to get good data
var textWidth = $('#calcTextWidthTempText').get(0)
.getComputedTextLength();
// Compare to the current maximum
max = textWidth > max ? textWidth : max;
// Clean up the temporary text
$('#calcTextWidthTempText').remove();
});
// Clean up our temporary div
$('#calcTextWidthTempDiv').remove();
// Store the value for future calls
this.cache[key] = max;
return max;
}
// Create an SVG element which represets a memory cell with the given
// address and data
function makeMemCell(addr, data) {
var boxHeight = 30;
var boxWidth = 200;
var addressPadding = 10;
// How much we need to shift the text down so that our y-position
// represents the center of the text
var textVerticalCenterCorrection = 6.5;
// We are going to be printing 4-digit numbers. Because one of the
// digits must be the widest digit, one of these has to be the
// widest 4-digit number (mod kerning).
var maxAddressTextLen = calcMaxTextWidth(['0x0000', '0x1111', '0x2222',
'0x3333', '0x4444', '0x5555',
'0x6666', '0x7777', '0x8888',
'0x9999', '0xaaaa', '0xbbbb',
'0xcccc', '0xdddd', '0xeeee',
'0xffff'], {});
var addressText = $('<text />').attr({
'x' : maxAddressTextLen,
'y' : boxHeight + textVerticalCenterCorrection,
'text-anchor' : 'end'
}).text(addr);
var box = $('<rect />').attr({
'x' : maxAddressTextLen + addressPadding,
'y' : 1,
'width' : boxWidth,
'height' : boxHeight,
'fill' : 'none',
'stroke' : 'black' });
var dataText = $('<text />').attr({
'x' : maxAddressTextLen + addressPadding + boxWidth/2,
'y' : boxHeight/2 + textVerticalCenterCorrection,
'text-anchor' : 'middle'
}).text(data);
return $('<svg />')
.append(box)
.append(addressText)
.append(dataText);
}
// Create an SVG element representing a register with the given name and
// data. If labelAbove is true, the register name will be placed above the
// data instead of to the left.
function makeRegister(reg, data, labelAbove) {
var boxHeight = 30;
var boxWidth = 100;
var labelPadding = 10;
var boxVerticalOffset = 11;
var textVerticalCenterCorrection = 6;
var textVerticalTopCorrection = 13;
// We are going to be printing 4-digit numbers. Because one of the
// digits must be the widest digit, one of these has to be the
// widest 4-digit number (mod kerning).
var maxLabelLen = calcMaxTextWidth(['%eax','%ebx', '%ecx',
'%edx', '%edi', '%esi',
'%ebp', '%esp', 'PC'], {});
var labelText = $('<text />').attr({
'x' : labelAbove ? boxWidth/2 : maxLabelLen,
'y' : labelAbove ? 1 + textVerticalTopCorrection : boxHeight/2 + textVerticalCenterCorrection,
'text-anchor' : labelAbove ? 'middle' : 'end'
}).text(reg);
var box = $('<rect />').attr({
'x' : labelAbove ? 1 : maxLabelLen + labelPadding,
'y' : labelAbove ? textVerticalTopCorrection + boxVerticalOffset + 1 : 1,
'width' : boxWidth,
'height' : boxHeight,
'fill' : 'none',
'stroke' : 'black' });
var dataText = $('<text />').attr({
'x' : labelAbove ? boxWidth/2 : maxLabelLen + labelPadding + boxWidth/2,
'y' : (labelAbove ? boxHeight/2 + boxVerticalOffset + textVerticalTopCorrection : boxHeight/2) + textVerticalCenterCorrection,
'text-anchor' : 'middle'
}).text(data);
return $('<svg />')
.append(box)
.append(labelText)
.append(dataText);
}
// Update the memory diagram to match the current state of a provided
// virtual machine
function updateVisualization(virtualMachine) {
// Clear out the current diagram
$('#memoryDiagram').empty();
// Definition section
// Make an arrowhead that can be added to paths
$('#memoryDiagram').append($('<defs />').append($('<marker />').attr({
'id' : 'markerArrowhead',
'markerWidth' : 6,
'markerHeight' : 10,
'refx' : 6,
'refy' : 5,
'orient' : 'auto'
}).append($('<path />').attr({
'd' : 'M0,0 L6,5 L0,10',
'fill' : 'none',
'stroke' : 'black'
}))));
// Draw memory
var numMemoryCells = 5;
if (virtualMachine.registers['%esp'] !== 0) {
numMemoryCells = (0x1000 - virtualMachine.registers['%esp'])/4 + 5;
}
for (var i = 0; i < numMemoryCells; ++i) {
var addr = 0x1000 - 4*(numMemoryCells - i);
var cell =
makeMemCell(numToPreferredString(addr + 4),
numToPreferredString(
virtualMachine.getIntAt(addr)));
$('#memoryDiagram').append(cell.attr({
'x' : 190,
'y' : 30 * i + (11 + 13)
}));
}
// Draw the registers on the right hand side of the diagram
var rightHandRegisters = ['%eax', '%ebx', '%ecx',
'%edx', '%edi', '%esi'];
rightHandRegisters.forEach(function (register, index) {
var reg =
makeRegister(register,
numToPreferredString(
virtualMachine.registers[register]));
$('#memoryDiagram').append(reg.attr({
'x' : 500,
'y' : (30 + 15) * index + (11 + 13),
}));
});
// Draw the program counter
$('#memoryDiagram')
.append(makeRegister('PC',
numToPreferredString(
virtualMachine.programCounter),
true).attr({
'x' : 680,
'y' : 0,
}));
// Draw the status indicator
$('#memoryDiagram')
.append(makeRegister('Status',
virtualMachine.statusCode,
true).attr({
'x' : 680,
'y' : 90,
}));
// Draw the %esp register
var espx = 0;
var espy = 135;
var addressLen = calcMaxTextWidth(['0x0000', '0x1111', '0x2222',
'0x3333', '0x4444', '0x5555',
'0x6666', '0x7777', '0x8888',
'0x9999', '0xaaaa', '0xbbbb',
'0xcccc', '0xdddd', '0xeeee',
'0xffff'], {}) + 10;
var espStartpointX = espx + 100;
var espStartpointY = espy + (11 + 13 + 1) + 30/2;
var espEndpointX = 190 + addressLen;
var espEndpointY = (30 * (virtualMachine.registers['%esp']/4 -
(0x1000/4 - numMemoryCells)) + (11 + 13 + 1));
$('#memoryDiagram')
.append(makeRegister('%esp',
numToPreferredString(
virtualMachine.registers['%esp']),
true).attr({
'x' : espx,
'y' : espy,
}));
// Draw the arrow from the %esp register to the current memory address
if (virtualMachine.registers['%esp'] !== 0) {
$('#memoryDiagram').append($('<path />').attr({
'd' : 'M' + espStartpointX + ',' + espStartpointY + ' ' +
'C' + (espStartpointX + 80) + ',' + espStartpointY + ' ' +
(espEndpointX - 80) + ',' + espEndpointY + ' ' +
espEndpointX + ',' + espEndpointY,
'fill' : 'none',
'stroke' : 'black',
'style' : 'marker-end: url(#markerArrowhead)'
}));
}
// Draw the %ebp register
var ebpx = 0;
var ebpy = 45;
var ebpStartpointX = ebpx + 100;
var ebpStartpointY = ebpy + (11 + 13 + 1) + 30/2;
var ebpEndpointX = 190 + addressLen;
var ebpEndpointY = (30 * (virtualMachine.registers['%ebp']/4 -
(0x1000/4 - numMemoryCells)) + (11 + 13 + 1));
$('#memoryDiagram')
.append(makeRegister('%ebp',
numToPreferredString(
virtualMachine.registers['%ebp']),
true).attr({
'x' : ebpx,
'y' : ebpy,
}));
// Draw the arrow from the %ebp register to the current memory address
if (virtualMachine.registers['%ebp'] !== 0) {
$('#memoryDiagram').append($('<path />').attr({
'd' : 'M' + ebpStartpointX + ',' + ebpStartpointY + ' ' +
'C' + (ebpStartpointX + 80) + ',' + ebpStartpointY + ' ' +
(ebpEndpointX - 80) + ',' + ebpEndpointY + ' ' +
ebpEndpointX + ',' + ebpEndpointY,
'fill' : 'none',
'stroke' : 'black',
'style' : 'marker-end: url(#markerArrowhead)'
}));
}
// Highlight the current line in the code
highlightLine(virtualMachine.programCounter);
// Update the SVG rendering
refreshSVG();
// Update the size of the diagram to fit everything in the SVG
var bbox = $('#memoryDiagram').get(0).getBBox();
$('#memoryDiagram').attr({
'width' : bbox.x + bbox.width + 1,
'height' : bbox.y + bbox.height + 1,
});
}
// Bind the assemble button to assembling the code
$('body').on('click', '#assemble', function (event) {
var assemblyFailed = false;
try {
assembledCode = Yas.assemble(editor.getValue());
} catch (e) {
if (e instanceof Yas.ParseException) {
assemblyFailed = true;
e.errors.forEach(function (error) {
$('<div />').addClass('assembleError')
.append($('<p />')
.text('line ' + error.lineNumber +
': ' + error.message))
.appendTo($('body'))
.dialog({
title: 'Assemble Error',
minHeight: 0,
close: function() {
$(this).remove();
}
});
});
} else {
throw e;
}
}
if (!assemblyFailed) {
$('#preAssemble').hide();
$('#postAssemble').show();
renderCodeAsTable(assembledCode);
vm = new Yas.VM(assembledCode, function () {
return prompt("The program needs input.");});
updateVisualization(vm);
}
});
// Bind the advance button to advancing the VM and updating the diagram
$('body').on('click', '#advance', function (event) {
vm.advance();
updateVisualization(vm);
$('#output').text(vm.output);
});
// Bind the toggleHex button to changing whether hex or decimal numbers are
// shown in the diagram
$('body').on('click', '#toggleHex', function (event) {
renderHex = !renderHex;
$('#toggleHex').text(renderHex ? 'Decimal' : 'Hexadecimal');
updateVisualization(vm);
});
// Bind the edit button to re-show the editor
$('body').on('click', '#edit', function (event) {
$('#preAssemble').show();
$('#postAssemble').hide();
});
// Bind the clear button to clear the editor
$('body').on('click', '#clear', function (event) {
editor.setValue('');
});
// Show only the editor on page load
$('#postAssemble').hide();
});