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
5353export 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 }
266272case 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 }
271280case 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 }
509521default : {
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}
0 commit comments