Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 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 | |
| 7 | import binascii |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 8 | import optparse |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 9 | import sys |
| 10 | |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 11 | parser = optparse.OptionParser() |
| 12 | parser.add_option("--mac", |
| 13 | help="generate assembly file for Mac/iOS (default: False)", |
| 14 | action="store_true", default=False) |
Yang Guo | 2fd941d | 2019-03-05 11:30:15 +0100 | [diff] [blame] | 15 | parser.add_option("--win", |
| 16 | help="generate assembly file for Windows (default: False)", |
| 17 | action="store_true", default=False) |
| 18 | parser.set_usage("""make_data_assembly icu_data [assembly_file] [--mac] [--win] |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 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() |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 22 | |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 23 | if len(args) < 1: |
| 24 | parser.error("ICU data file is not given.") |
| 25 | |
| 26 | input_file = args[0] |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 27 | n = input_file.find(".dat") |
| 28 | if n == -1: |
| 29 | sys.exit("%s is not an ICU .dat file." % input_file) |
| 30 | |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 31 | if len(args) < 2: |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 32 | output_file = input_file[0:n] + "_dat.S" |
| 33 | else: |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 34 | output_file = args[1] |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 35 | |
| 36 | if 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 |
| 41 | else: |
| 42 | step = -1 |
| 43 | |
| 44 | input_data = open(input_file, 'rb').read() |
| 45 | n = input_data.find("icudt") |
| 46 | if n == -1: |
| 47 | sys.exit("Cannot find a version number in %s." % input_file) |
| 48 | |
| 49 | version_number = input_data[n + 5:n + 7] |
| 50 | |
| 51 | output = open(output_file, 'w') |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 52 | |
| 53 | if 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)) |
Yang Guo | 2fd941d | 2019-03-05 11:30:15 +0100 | [diff] [blame] | 62 | elif 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)) |
Jungshik Shin | 4c2855a | 2016-07-22 13:31:33 -0700 | [diff] [blame] | 67 | else: |
| 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)) |
Miran Karic | e7d37b6 | 2016-07-21 14:02:36 -0700 | [diff] [blame] | 77 | |
| 78 | split = [binascii.hexlify(input_data[i:i + 4][::step]).upper().lstrip('0') |
| 79 | for i in range(0, len(input_data), 4)] |
| 80 | |
| 81 | for 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 | |
| 100 | output.write("\n") |
| 101 | output.close() |
| 102 | print "Generated " + output_file |