1#!/usr/bin/env python 2 3# Copyright 2019 the V8 project authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be found 5# in the LICENSE file. 6 7import io 8import math 9import struct 10import sys 11 12CUSTOM_SECTION_ID = 0 13FUNCTION_SECTION_ID = 3 14 15def peek_uint8(fin): 16 bs = fin.peek(1)[:1] 17 if len(bs) != 1: 18 return None, bs 19 return ord(bs[0]), bs 20 21def read_uint8(fin): 22 value, bs = peek_uint8(fin) 23 fin.read(len(bs)) 24 return value, bs 25 26def peek_uint32(fin): 27 bs = fin.peek(4)[:4] 28 if len(bs) != 4: 29 return None, bs 30 return ord(bs[0]) | ord(bs[1]) << 8 | ord(bs[2]) << 16 | ord(bs[3]) << 24, bs 31 32def read_uint32(fin): 33 value, bs = peek_uint32(fin) 34 fin.read(len(bs)) 35 return value, bs 36 37def peek_varuintN(fin): 38 value = 0 39 shift = 0 40 n = 1 41 while True: 42 bs = fin.peek(n)[:n] 43 if len(bs) < n: 44 return None, bs 45 b = ord(bs[-1]) 46 value |= (b & 0x7F) << shift; 47 if (b & 0x80) == 0x00: 48 return value, bs 49 shift += 7; 50 n += 1 51 52def read_varuintN(fin): 53 value, bs = peek_varuintN(fin) 54 fin.read(len(bs)) 55 return value, bs 56 57def to_varuintN(value): 58 bs = "" 59 while True: 60 b = value & 0x7F 61 value >>= 7 62 if (value != 0x00): 63 b |= 0x80 64 bs += chr(b) 65 if value == 0x00: 66 return bs 67 68def write_varuintN(value, fout): 69 bs = to_varuintN(value) 70 fout.write(bs) 71 return bs 72 73def peek_magic_number(fin, expected_magic_number=0x6d736100): 74 magic_number, bs = peek_uint32(fin) 75 assert magic_number == expected_magic_number, "unexpected magic number" 76 return magic_number, bs 77 78def read_magic_number(fin, expected_magic_number=0x6d736100): 79 magic_number, bs = peek_magic_number(fin, expected_magic_number) 80 fin.read(len(bs)) 81 return magic_number, bs 82 83def peek_version(fin, expected_version=1): 84 version, bs = peek_uint32(fin) 85 assert version == expected_version, "unexpected version" 86 return version, bs 87 88def read_version(fin, expected_version=1): 89 version, bs = peek_version(fin, expected_version) 90 fin.read(len(bs)) 91 return version, bs 92 93def write_custom_section(fout, section_name_bs, payload_bs): 94 section_name_length_bs = to_varuintN(len(section_name_bs)) 95 payload_length_bs = to_varuintN(len(section_name_bs) \ 96 + len(section_name_length_bs) + len(payload_bs)) 97 section_id_bs = to_varuintN(CUSTOM_SECTION_ID) 98 fout.write(section_id_bs) 99 fout.write(payload_length_bs) 100 fout.write(section_name_length_bs) 101 fout.write(section_name_bs) 102 fout.write(payload_bs) 103 104def write_compilation_hints_section(fout, hints_bs): 105 num_compilation_hints_bs = to_varuintN(len(hints_bs)) 106 section_name_bs = b"compilationHints" 107 payload_bs = num_compilation_hints_bs + hints_bs 108 write_custom_section(fout, section_name_bs, payload_bs) 109