blob: 94b07c81ef60d6ca134493ee57b7efb439751d32 [file] [log] [blame]
Miran Karice7d37b62016-07-21 14:02:36 -07001#!/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
7import binascii
Jungshik Shin4c2855a2016-07-22 13:31:33 -07008import optparse
Miran Karice7d37b62016-07-21 14:02:36 -07009import sys
10
Jungshik Shin4c2855a2016-07-22 13:31:33 -070011parser = optparse.OptionParser()
12parser.add_option("--mac",
13 help="generate assembly file for Mac/iOS (default: False)",
14 action="store_true", default=False)
Yang Guo2fd941d2019-03-05 11:30:15 +010015parser.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]
Jungshik Shin4c2855a2016-07-22 13:31:33 -070019 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 Karice7d37b62016-07-21 14:02:36 -070022
Jungshik Shin4c2855a2016-07-22 13:31:33 -070023if len(args) < 1:
24 parser.error("ICU data file is not given.")
25
26input_file = args[0]
Miran Karice7d37b62016-07-21 14:02:36 -070027n = input_file.find(".dat")
28if n == -1:
29 sys.exit("%s is not an ICU .dat file." % input_file)
30
Jungshik Shin4c2855a2016-07-22 13:31:33 -070031if len(args) < 2:
Miran Karice7d37b62016-07-21 14:02:36 -070032 output_file = input_file[0:n] + "_dat.S"
33else:
Jungshik Shin4c2855a2016-07-22 13:31:33 -070034 output_file = args[1]
Miran Karice7d37b62016-07-21 14:02:36 -070035
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("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]
50
51output = open(output_file, 'w')
Jungshik Shin4c2855a2016-07-22 13:31:33 -070052
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))
Yang Guo2fd941d2019-03-05 11:30:15 +010062elif options.win:
Yang Guof5d1dcc2019-05-09 15:09:18 +020063 output.write(".globl _icudt%s_dat\n"
Yang Guo2fd941d2019-03-05 11:30:15 +010064 "\t.section .rdata\n"
65 "\t.balign 16\n"
Yang Guof5d1dcc2019-05-09 15:09:18 +020066 "_icudt%s_dat:\n" % tuple([version_number] * 2))
Jungshik Shin4c2855a2016-07-22 13:31:33 -070067else:
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 Karice7d37b62016-07-21 14:02:36 -070077
78split = [binascii.hexlify(input_data[i:i + 4][::step]).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