@@ -5,7 +5,7 @@ import { WasmSection, WasmTypeSectionPayload, WasmSectionPayload, WasmExportSect
55import { WasmSectionType , WasmType , WasmValueType , getWasmValueType , getExternalType , WasmExternalKind } from "./wasmTypes" ;
66import { FuncType , printSignature } from "./FuncType" ;
77import { FunctionBody , FunctionLocal , formatOpcodes } from "./FunctionBody" ;
8- import { WasmOpcode , WasmOpcodeDefinition , WasmOpcodes , Immediate } from "./WasmOpcodes" ;
8+ import { WasmOpcode , WasmOpcodeDefinition , WasmOpcodes , Immediate , BlockType } from "./WasmOpcodes" ;
99import { OpcodeImmediateType } from "./OpcodeImmediateType" ;
1010
1111
@@ -59,6 +59,16 @@ export class WasmBinaryParser {
5959 } ;
6060 const wasmPostProcessed = this . postProcess ( wasmBinary ) ;
6161 // console.log(JSON.stringify(wasmPostProcessed))
62+ const cod : WasmCodeSectionPayload = findSection ( wasmPostProcessed . sections , WasmSectionType . Code ) . payload as WasmCodeSectionPayload
63+ let i = 0
64+ let outp = ''
65+ for ( const o of cod . functions ) {
66+ outp += `== ${ i } == \n`
67+ outp += o . formattedOpcodes
68+ i ++
69+ }
70+ const fs = require ( 'fs' )
71+ fs . writeFileSync ( './output.txt' , outp )
6272 return wasmPostProcessed
6373 }
6474
@@ -99,8 +109,6 @@ export class WasmBinaryParser {
99109 for ( const fun of codeSectionPayload . functions ) {
100110 const formattedOpcodes = formatOpcodes ( fun . opcodes , importSectionPayload , codeSectionPayload )
101111 fun . formattedOpcodes = formattedOpcodes
102- // deleting opcodes for now, not really needed in the response, maybe in the future
103- delete fun . opcodes
104112 }
105113 return wasmBinary
106114 }
@@ -282,6 +290,8 @@ export class WasmBinaryParser {
282290 parseFunctionBytecode ( bytecode : Buffer ) : WasmOpcode [ ] {
283291 const reader = new BytesReader ( bytecode )
284292 const opcodes : WasmOpcode [ ] = [ ]
293+ let depth = 0
294+ let blockType : BlockType = BlockType . NONE
285295 while ( ! reader . finished ( ) ) {
286296 const opcodeByte = reader . readBytesToNumber ( 1 )
287297 const immediates : string [ ] = [ ]
@@ -321,10 +331,35 @@ export class WasmBinaryParser {
321331 immediates . push ( valueFormatted )
322332 }
323333 }
324- opcodes . push ( {
334+ const newOpcode = {
325335 opcode : opcodeDefinition ,
326- immediates
327- } )
336+ immediates,
337+ depth,
338+ blockType
339+ } ;
340+ opcodes . push ( newOpcode )
341+ if ( WasmOpcodes . isBlockEnd ( newOpcode ) ) {
342+ depth --
343+ }
344+ if ( WasmOpcodes . isBlockStart ( newOpcode ) ) {
345+ depth ++
346+ }
347+ // if(newOpcode.opcode.name === 'block') {
348+ // blockType = BlockType.BLOCK
349+ // }
350+ // if (newOpcode.opcode.name === 'loop') {
351+ // blockType = BlockType.LOOP
352+ // }
353+ // if(newOpcode.opcode.name === 'if') {
354+ // blockType = BlockType.IF
355+ // }
356+ // if(newOpcode.opcode.name === 'else') {
357+ // blockType = BlockType.ELSE
358+ // }
359+ // if(depth === 0) {
360+ // blockType = BlockType.NONE
361+ // }
362+
328363 }
329364 return opcodes
330365 }
0 commit comments