def from_reader(cls, r: typing.BinaryIO): o = ElementSegment() o.tableidx = bin_reader.read_count(r, 32) o.expr = Expression.from_reader(r) n = bin_reader.read_count(r, 32) o.init = [bin_reader.read_count(r, 32) for _ in range(n)] return o
def from_reader(cls, r: typing.BinaryIO): o = Code() n = bin_reader.read_count(r, 32) n = bin_reader.read_count(r, 32) for _ in range(n): l = Locals.from_reader(r) o.locals.extend([l.valtype for _ in range(l.n)]) o.expr = Expression.from_reader(r) return o
def from_reader(cls, r: typing.BinaryIO): code_byte = r.read(1) if not code_byte: return None code = ord(code_byte) code_size = bin_format.opcodes[code][1] if code_size == '': immediate_arguments = None elif code_size == 'u8': immediate_arguments = bin_reader.read_count(r, 8) elif code_size == 'u32': immediate_arguments = bin_reader.read_count(r, 32) elif code_size == 'i32': immediate_arguments = number.int2i32( bin_reader.read_count(r, 32, signed=True)) elif code_size == 'i64': immediate_arguments = number.int2i64( bin_reader.read_count(r, 64, signed=True)) elif code_size == 'f32': immediate_arguments = number.LittleEndian.f32(r.read(4)) elif code_size == 'f64': immediate_arguments = number.LittleEndian.f64(r.read(8)) elif code_size == 'u32,u8': immediate_arguments = [ bin_reader.read_count(r, 32), bin_reader.read_count(r, 8) ] elif code_size == 'u32,u32': immediate_arguments = [ bin_reader.read_count(r, 32) for _ in range(2) ] elif code == bin_format.br_table: n = bin_reader.read_count(r, 32) a = [bin_reader.read_count(r, 32) for _ in range(n)] b = bin_reader.read_count(r, 32) immediate_arguments = [a, b] else: raise Exception("Invalid code size!") return Instruction(code, immediate_arguments)
def from_reader(cls, r: typing.BinaryIO): o = Import() o.module = bin_reader.read_bytes(r, 32).decode() o.name = bin_reader.read_bytes(r, 32).decode() o.kind = ord(r.read(1)) if o.kind == bin_format.extern_func: o.desc = bin_reader.read_count(r, 32) elif o.kind == bin_format.extern_table: o.desc = TableType.from_reader(r) elif o.kind == bin_format.extern_mem: o.desc = MemoryType.from_reader(r) elif o.kind == bin_format.extern_global: o.desc = GlobalType.from_reader(r) else: raise Exception('Malformed!') return o
def from_reader(cls, r: typing.BinaryIO) -> 'Module': if list(r.read(4)) != [0x00, 0x61, 0x73, 0x6d]: raise Exception('Invalid magic number!') if list(r.read(4)) != [0x01, 0x00, 0x00, 0x00]: raise Exception('Invalid version!') mod = Module() while True: section_id_byte = r.read(1) if not section_id_byte: break section_id = ord(section_id_byte) n = bin_reader.read_count(r, 32) data = r.read(n) if len(data) != n: raise Exception('Invalid section size!') if section_id == bin_format.custom_section: custom_section = CustomSection.from_reader(io.BytesIO(data)) logger.debugln( f'{bin_format.section[section_id][0]:>9} {custom_section.name}' ) elif section_id == bin_format.type_section: type_section = TypeSection.from_reader(io.BytesIO(data)) for i, e in enumerate(type_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.types = type_section.vec elif section_id == bin_format.import_section: import_section = ImportSection.from_reader(io.BytesIO(data)) for i, e in enumerate(import_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.imports = import_section.vec elif section_id == bin_format.function_section: function_section = FunctionSection.from_reader( io.BytesIO(data)) num_imported_funcs = sum(1 for _ in filter( lambda ins: ins.kind == bin_format.extern_func, mod.imports)) for i, e in enumerate(function_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] func={num_imported_funcs + i} sig={e}' ) elif section_id == bin_format.table_section: table_section = TableSection.from_reader(io.BytesIO(data)) for i, e in enumerate(table_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.tables = table_section.vec elif section_id == bin_format.memory_section: memory_section = MemorySection.from_reader(io.BytesIO(data)) for i, e in enumerate(memory_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.mems = memory_section.vec elif section_id == bin_format.global_section: global_section = GlobalSection.from_reader(io.BytesIO(data)) for i, e in enumerate(global_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.globals = global_section.vec elif section_id == bin_format.export_section: export_section = ExportSection.from_reader(io.BytesIO(data)) for i, e in enumerate(export_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.exports = export_section.vec elif section_id == bin_format.start_section: start_section = StartSection.from_reader(io.BytesIO(data)) logger.debugln( f'{bin_format.section[section_id][0]:>12} {start_section.start_function}' ) mod.start = start_section.start_function.funcidx elif section_id == bin_format.element_section: element_section = ElementSection.from_reader(io.BytesIO(data)) for i, e in enumerate(element_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.elem = element_section.vec elif section_id == bin_format.code_section: code_section = CodeSection.from_reader(io.BytesIO(data)) def printex(instrs: typing.List[Instruction], prefix=0): for e in instrs: a = f' | {" " * prefix}{bin_format.opcodes[e.code][0]}' if e.code in [ bin_format.block, bin_format.loop, bin_format.if_ ]: logger.debugln( f'{a} {bin_format.blocktype[e.immediate_arguments][0]}' ) prefix += 2 elif e.code == bin_format.end: prefix -= 2 a = f' | {" " * prefix}{bin_format.opcodes[e.code][0]}' logger.debugln(f'{a}') elif e.immediate_arguments is None: logger.debugln(f'{a}') elif isinstance(e.immediate_arguments, list): logger.debugln( f'{a} {" ".join([str(e) for e in e.immediate_arguments])}' ) else: logger.debugln(f'{a} {e.immediate_arguments}') num_imported_funcs = sum(1 for _ in filter( lambda ins: ins.kind == bin_format.extern_func, mod.imports)) for i, e in enumerate(code_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] func={num_imported_funcs + i} {e}' ) printex(e.expr.data) func = Function() func.typeidx = function_section.vec[i] func.locals = e.locals func.expr = e.expr mod.funcs.append(func) elif section_id == bin_format.data_section: data_section = DataSection.from_reader(io.BytesIO(data)) for i, e in enumerate(data_section.vec): logger.debugln( f'{bin_format.section[section_id][0]:>9}[{i}] {e}') mod.data = data_section.vec else: raise Exception('Invalid section id!') logger.debugln('') return mod
def from_reader(cls, r: typing.BinaryIO): o = DataSection() n = bin_reader.read_count(r, 32) o.vec = [DataSegment.from_reader(r) for _ in range(n)] return o
def from_reader(cls, r: typing.BinaryIO): o = CodeSection() n = bin_reader.read_count(r, 32) o.vec = [Code.from_reader(r) for _ in range(n)] return o
def from_reader(cls, r: typing.BinaryIO): o = ExportSection n = bin_reader.read_count(r, 32) o.vec = [Export.from_reader(r) for _ in range(n)] return o
def from_reader(cls, r: typing.BinaryIO): flag = ord(r.read(1)) minimum = bin_reader.read_count(r) maximum = bin_reader.read_count(r) if flag else None return Limits(minimum, maximum)
def from_reader(cls, r: typing.BinaryIO): o = FunctionSection() n = bin_reader.read_count(r, 32) o.vec = [bin_reader.read_count(r, 32) for _ in range(n)] return o
def from_reader(cls, r: typing.BinaryIO): o = CustomSection() n = bin_reader.read_count(r, 32) o.name = r.read(n).decode() o.data = bytearray(r.read(-1)) return o
def from_reader(cls, r: typing.BinaryIO): o = Export() o.name = bin_reader.read_bytes(r, 32).decode() o.kind = ord(r.read(1)) o.desc = bin_reader.read_count(r, 32) return o
def from_reader(cls, r: typing.BinaryIO): o = StartFunction() o.funcidx = bin_reader.read_count(r, 32) return o
def from_reader(cls, r: typing.BinaryIO): o = DataSegment() o.memidx = bin_reader.read_count(r, 32) o.expr = Expression.from_reader(r) o.init = bytearray(bin_reader.read_bytes(r, 32)) return o
def from_reader(cls, r: typing.BinaryIO): o = Locals() o.n = bin_reader.read_count(r, 32) o.valtype = ord(r.read(1)) return o