Skip to content

Commit 05df945

Browse files
committed
Adding more warning messages
1 parent 3d2ee13 commit 05df945

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed

src/app/bytecode-viewer/bytecode-viewer.component.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@
4242
<img src="assets/gear.gif" alt="Spinner" style="width: 32px; height: auto;" />&nbsp;Wait for a few seconds... Crunching zeros and ones just for you!
4343
</ng-container>
4444
<ng-template #elseTemplate7>
45-
{{disassembler.getCodeSize() + ' bytes'}}
46-
<ng-container *ngIf="disassembler.getCodeSize() < 0">
45+
{{disassembler.getDeclaredCodeSize() + ' bytes'}}
46+
<ng-container *ngIf="disassembler.getDeclaredCodeSize() < 0">
4747
&nbsp;
4848
<fa-icon [icon]="exclamationIcon" class="pulse red-colored" data-toggle="tooltip" data-placement="bottom" title="Negative value is not expected here!"></fa-icon>
4949
</ng-container>
50+
<ng-container *ngIf="disassembler.getDeclaredCodeSize() !== disassembler.getRealCodeSize()">
51+
&nbsp;
52+
<fa-icon [icon]="exclamationIcon" class="pulse red-colored" data-toggle="tooltip" data-placement="bottom" title="Code size declared here does not match real code size!"></fa-icon>
53+
</ng-container>
5054
</ng-template>
5155
</td>
5256
</tr>
@@ -72,7 +76,7 @@
7276
<img src="assets/gear.gif" alt="Spinner" style="width: 32px; height: auto;" />&nbsp;Wait for a few seconds... Crunching zeros and ones just for you!
7377
</ng-container>
7478
<ng-template #elseTemplate9>
75-
<ng-container *ngIf="disassembler.getEntryPoint() < disassembler.getCodeSize(); else elseTemplate11">
79+
<ng-container *ngIf="disassembler.getEntryPoint() < disassembler.getRealCodeSize(); else elseTemplate11">
7680
<a href="{{'#address-' + disassembler.getEntryPoint()}}">{{disassembler.getEntryPoint()}}</a>
7781
</ng-container>
7882
<ng-template #elseTemplate11>

src/app/shared/disassembler.shared.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
class Instruction {
20+
export class Instruction {
2121
public readonly opcode: number;
2222
public readonly mnemonic: string;
2323
}
@@ -52,22 +52,23 @@ class DisassembledInstruction {
5252

5353
export class Disassembler {
5454

55-
uint8Array: Uint8Array;
56-
current: number = 0;
57-
address: number = 0;
58-
headerSize: number = 0;
55+
private uint8Array: Uint8Array;
56+
private current: number = 0;
57+
private address: number = 0;
58+
private headerSize: number = 0;
5959
public disassembledInstructions: DisassembledInstruction[];
6060

61-
opcode: number;
62-
operand1: number;
63-
operand2: number;
61+
private opcode: number;
62+
private operand1: number;
63+
private operand2: number;
6464

65-
codeSize: number;
66-
dataSize: number;
67-
entryPoint: number;
65+
private declaredCodeSize: number;
66+
private realCodeSize = 0;
67+
private dataSize: number;
68+
private entryPoint: number;
6869

69-
jumpDestination: number;
70-
warning: string;
70+
private jumpDestination: number;
71+
private warning: string;
7172

7273
public static readonly MIN_OBJ_SIZE: number = 1 + 1 + 3 * 4 + 1;
7374

@@ -149,6 +150,8 @@ export class Disassembler {
149150
public static readonly DUP_X1: Instruction = {mnemonic: "dup_x1", opcode: 59};
150151
public static readonly DUP_X2: Instruction = {mnemonic: "dup_x2", opcode: 60};
151152

153+
public static readonly INVALID_INSTR: Instruction = {mnemonic: "", opcode: 61};
154+
152155
public static readonly INSTRUCTIONS: Instruction[] = [
153156
Disassembler.LOAD, Disassembler.LOAD_0, Disassembler.LOAD_1, Disassembler.LOAD_2,
154157
Disassembler.LOAD_3, Disassembler.STORE, Disassembler.STORE_0, Disassembler.STORE_1,
@@ -175,6 +178,7 @@ export class Disassembler {
175178
}
176179

177180
get() {
181+
this.realCodeSize++;
178182
return this.uint8Array[this.current++] & 0x0ff;
179183
}
180184

@@ -207,9 +211,11 @@ export class Disassembler {
207211
else if (String.fromCharCode(this.get()) != 'M' || String.fromCharCode(this.get()) != 'J')
208212
throw new Error("The file has invalid header!");
209213

210-
this.codeSize = this.get4();
214+
this.declaredCodeSize = this.get4();
211215
this.dataSize = this.get4();
212216
this.entryPoint = this.get4();
217+
218+
this.realCodeSize = 0;
213219

214220
this.headerSize = this.current;
215221
this.address = this.current - this.headerSize;
@@ -265,11 +271,17 @@ export class Disassembler {
265271
}
266272
case Disassembler.GETSTATIC.opcode: {
267273
this.operand1 = this.get2();
274+
if (this.operand1 >= this.dataSize) {
275+
this.warning = "This adress is out of Data memory area address range" + (this.dataSize > 0 ? "([0, " + (this.dataSize - 1) + "])" : "") + "!";
276+
}
268277
this.put(Disassembler.GETSTATIC, new Uint8Array([this.opcode, (this.operand1 >> 8) & 0x0ff, this.operand1 & 0x0ff]), this.operand1.toString());
269278
break;
270279
}
271280
case Disassembler.PUTSTATIC.opcode: {
272281
this.operand1 = this.get2();
282+
if (this.operand1 >= this.dataSize) {
283+
this.warning = "This adress is out of Data memory area address range" + (this.dataSize > 0 ? "([0, " + (this.dataSize - 1) + "])" : "") + "!";
284+
}
273285
this.put(Disassembler.PUTSTATIC, new Uint8Array([this.opcode,(this.operand1 >> 8) & 0x0ff, this.operand1 & 0x0ff]), this.operand1.toString());
274286
break;
275287
}
@@ -508,7 +520,7 @@ export class Disassembler {
508520
}
509521
default: {
510522
this.warning = 'This is not a valid instruction!';
511-
this.put({opcode: -1, mnemonic: ''}, new Uint8Array([this.opcode]));
523+
this.put(Disassembler.INVALID_INSTR, new Uint8Array([this.opcode]));
512524
break;
513525
}
514526
}
@@ -521,8 +533,8 @@ export class Disassembler {
521533
});
522534
}
523535

524-
getCodeSize() {
525-
return this.codeSize;
536+
getDeclaredCodeSize() {
537+
return this.declaredCodeSize;
526538
}
527539

528540
getDataSize() {
@@ -533,4 +545,8 @@ export class Disassembler {
533545
return this.entryPoint;
534546
}
535547

548+
getRealCodeSize() {
549+
return this.realCodeSize;
550+
}
551+
536552
}

src/app/stats/stats.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ <h3>Instructions</h3>
7878
<code>{{leftOutInstruction.mnemonic}}</code>
7979
</li>
8080
</ol>
81-
<p>Instructions that do appear in this object file are:</p>
82-
<div class="table-responsive">
81+
<p *ngIf="!loading && instructionCountArray.length">Instructions that do appear in this object file are:</p>
82+
<div *ngIf="!loading && instructionCountArray.length" class="table-responsive">
8383
<table class="table table-bordered table-hover">
8484
<thead>
8585
<tr>

src/app/stats/stats.component.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import { Component, OnInit, Input } from '@angular/core';
21-
import { Disassembler } from '../shared/disassembler.shared';
21+
import { Disassembler, Instruction } from '../shared/disassembler.shared';
2222

2323
@Component({
2424
selector: 'stats-component',
@@ -38,7 +38,7 @@ export class StatsComponent implements OnInit {
3838
public instructionCountArray = [];
3939

4040
public totalNumInstructions = Disassembler.INSTRUCTIONS.length;
41-
public leftOutInstructions = Object.assign([], Disassembler.INSTRUCTIONS);
41+
public leftOutInstructions: Instruction[] = [];
4242

4343
constructor() { }
4444

@@ -57,20 +57,23 @@ export class StatsComponent implements OnInit {
5757
this.numJumpInstructions = 0;
5858
this.instructionCount = {};
5959
this.instructionCountArray = [];
60+
this.leftOutInstructions = Object.assign([], Disassembler.INSTRUCTIONS);
6061

61-
this.numInstructions = disassembler.disassembledInstructions.length;
6262
disassembler.disassembledInstructions.forEach(disassembledInstruction => {
63-
this.averageInstructionLength += disassembledInstruction.bytes.length;
64-
if (disassembledInstruction.instruction == Disassembler.RETURN)
65-
this.numSubroutines++;
66-
else if (disassembledInstruction.instruction.opcode >= Disassembler.JMP.opcode && disassembledInstruction.instruction.opcode <= Disassembler.JGE.opcode && !disassembledInstruction.warning) {
67-
this.averageJumpOffset += disassembledInstruction.referencedAddress - disassembledInstruction.address;
68-
this.numJumpInstructions++;
63+
if (disassembledInstruction.instruction !== Disassembler.INVALID_INSTR) {
64+
this.numInstructions++;
65+
this.averageInstructionLength += disassembledInstruction.bytes.length;
66+
if (disassembledInstruction.instruction == Disassembler.RETURN)
67+
this.numSubroutines++;
68+
else if (disassembledInstruction.instruction.opcode >= Disassembler.JMP.opcode && disassembledInstruction.instruction.opcode <= Disassembler.JGE.opcode && !disassembledInstruction.warning) {
69+
this.averageJumpOffset += disassembledInstruction.referencedAddress - disassembledInstruction.address;
70+
this.numJumpInstructions++;
71+
}
72+
let mnemonic = disassembledInstruction.instruction.mnemonic;
73+
if (!(mnemonic in this.instructionCount))
74+
this.instructionCount[mnemonic] = 0;
75+
this.instructionCount[mnemonic]++;
6976
}
70-
let mnemonic = disassembledInstruction.instruction.mnemonic;
71-
if (!(mnemonic in this.instructionCount))
72-
this.instructionCount[mnemonic] = 0;
73-
this.instructionCount[mnemonic]++;
7477
});
7578
this.averageInstructionLength /= this.numInstructions;
7679
this.averageJumpOffset = this.numJumpInstructions ? this.averageJumpOffset / this.numJumpInstructions : 0;

0 commit comments

Comments
 (0)