blob: f69a3af6773d32a52bc6d1f0e01583fbefae3563 [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
8import sys
9
10if len(sys.argv) < 2:
11 sys.exit("Usage: %s icu_data_file [output_assembly_file]" % sys.argv[0])
12
13input_file = sys.argv[1]
14n = input_file.find(".dat")
15if n == -1:
16 sys.exit("%s is not an ICU .dat file." % input_file)
17
18if len(sys.argv) < 3:
19 output_file = input_file[0:n] + "_dat.S"
20else:
21 output_file = sys.argv[2]
22
23if input_file.find("l.dat") == -1:
24 if input_file.find("b.dat") == -1:
25 sys.exit("%s has no endianness marker." % input_file)
26 else:
27 step = 1
28else:
29 step = -1
30
31input_data = open(input_file, 'rb').read()
32n = input_data.find("icudt")
33if n == -1:
34 sys.exit("Cannot find a version number in %s." % input_file)
35
36version_number = input_data[n + 5:n + 7]
37
38output = open(output_file, 'w')
39output.write(".globl icudt" + version_number + "_dat\n"
40 "\t.section .note.GNU-stack,\"\",%progbits\n"
41 "\t.section .rodata\n"
42 "\t.balign 16\n"
43 "#ifdef U_HIDE_DATA_SYMBOL\n"
44 "\t.hidden icudt" + version_number + "_dat\n"
45 "#endif\n"
46 "\t.type icudt" + version_number + "_dat,%object\n"
47 "icudt" + version_number + "_dat:\n")
48
49split = [binascii.hexlify(input_data[i:i + 4][::step]).upper().lstrip('0')
50 for i in range(0, len(input_data), 4)]
51
52for i in range(len(split)):
53 if (len(split[i]) == 0):
54 value = '0'
55 elif (len(split[i]) == 1):
56 if not any((c in '123456789') for c in split[i]):
57 value = '0x0' + split[i]
58 else:
59 value = split[i]
60 elif (len(split[i]) % 2 == 1):
61 value = '0x0' + split[i]
62 else:
63 value = '0x' + split[i]
64
65 if (i % 32 == 0):
66 output.write("\n.long ")
67 else:
68 output.write(",")
69 output.write(value)
70
71output.write("\n")
72output.close()
73print "Generated " + output_file