-- demo\rosetta\Compiler\cgen.exw -- Generates 32-bit machine code (see note in vm.exw) without js -- (machine code!) include cgen.e function get_var_name(atom addr) integer n = (addr-var_mem)/4+1 if n<1 or n>length(vars) then ?9/0 end if return vars[n] end function function hxl(integer pc, object oh, string fmt, sequence args={}) -- helper routine to display the octal/hex bytes just decoded, -- along with the code offset and the human-readable text. if length(args) then fmt = sprintf(fmt,args) end if sequence octhex = {} atom base = code_mem+pc integer len = 0 if integer(oh) then -- all octal for i=1 to oh do octhex = append(octhex,sprintf("0o%03o",peek(base))) base += 1 end for len = oh else -- some octal and some hex for i=1 to length(oh) by 2 do for j=1 to oh[i] do octhex = append(octhex,sprintf("0o%03o",peek(base))) base += 1 end for len += oh[i] for j=1 to oh[i+1] do octhex = append(octhex,sprintf("#%02x",peek(base))) base += 1 end for len += oh[i+1] end for end if printf(output_file,"%4d: %-30s %s\n",{pc+1,join(octhex,","),fmt}) return len end function constant cccodes = {"o?" ,"no?","b?" ,"ae?","z" ,"ne" ,"be?","a?", -- 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , "s?" ,"ns?","pe?","po?","l" ,"ge" ,"le" ,"g" } -- 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 constant regs = {"eax","ecx","edx"} -- (others as/when needed) procedure decode() -- for a much more complete (and better organised) disassembler, see p2asm.e integer pc = 0, -- nb 0-based opcode, xrm while pc