1#!/usr/bin/env python 2 3# Copyright 2016 The Chromium Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import binascii 8import optparse 9import sys 10 11parser = optparse.OptionParser() 12parser.add_option("--mac", 13 help="generate assembly file for Mac/iOS (default: False)", 14 action="store_true", default=False) 15parser.add_option("--win", 16 help="generate assembly file for Windows (default: False)", 17 action="store_true", default=False) 18parser.set_usage("""make_data_assembly icu_data [assembly_file] [--mac] [--win] 19 icu_data: ICU data file to generate assembly from. 20 assembly_file: Output file converted from icu_data file.""") 21(options, args) = parser.parse_args() 22 23if len(args) < 1: 24 parser.error("ICU data file is not given.") 25 26input_file = args[0] 27n = input_file.find(".dat") 28if n == -1: 29 sys.exit("%s is not an ICU .dat file." % input_file) 30 31if len(args) < 2: 32 output_file = input_file[0:n] + "_dat.S" 33else: 34 output_file = args[1] 35 36if input_file.find("l.dat") == -1: 37 if input_file.find("b.dat") == -1: 38 sys.exit("%s has no endianness marker." % input_file) 39 else: 40 step = 1 41else: 42 step = -1 43 44input_data = open(input_file, 'rb').read() 45n = input_data.find(b'icudt') 46if n == -1: 47 sys.exit("Cannot find a version number in %s." % input_file) 48 49version_number = input_data[n + 5:n + 7].decode("ascii") 50 51output = open(output_file, 'w') 52 53if options.mac: 54 output.write(".globl _icudt%s_dat\n" 55 "#ifdef U_HIDE_DATA_SYMBOL\n" 56 "\t.private_extern _icudt%s_dat\n" 57 "#endif\n" 58 "\t.data\n" 59 "\t.const\n" 60 "\t.align 4\n" 61 "_icudt%s_dat:\n" %tuple([version_number] * 3)) 62elif options.win: 63 output.write(".globl _icudt%s_dat\n" 64 "\t.section .rdata\n" 65 "\t.balign 16\n" 66 "_icudt%s_dat:\n" % tuple([version_number] * 2)) 67else: 68 output.write(".globl icudt%s_dat\n" 69 "\t.section .note.GNU-stack,\"\",%%progbits\n" 70 "\t.section .rodata\n" 71 "\t.balign 16\n" 72 "#ifdef U_HIDE_DATA_SYMBOL\n" 73 "\t.hidden icudt%s_dat\n" 74 "#endif\n" 75 "\t.type icudt%s_dat,%%object\n" 76 "icudt%s_dat:\n" % tuple([version_number] * 4)) 77 78split = [binascii.hexlify(input_data[i:i + 4][::step]).decode('ascii').upper().lstrip('0') 79 for i in range(0, len(input_data), 4)] 80 81for i in range(len(split)): 82 if (len(split[i]) == 0): 83 value = '0' 84 elif (len(split[i]) == 1): 85 if not any((c in '123456789') for c in split[i]): 86 value = '0x0' + split[i] 87 else: 88 value = split[i] 89 elif (len(split[i]) % 2 == 1): 90 value = '0x0' + split[i] 91 else: 92 value = '0x' + split[i] 93 94 if (i % 32 == 0): 95 output.write("\n.long ") 96 else: 97 output.write(",") 98 output.write(value) 99 100output.write("\n") 101output.close() 102print("Generated " + output_file) 103