Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2010 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 17 | |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | """Convert Slang data files to assembly output.""" |
| 20 | |
| 21 | import sys |
| 22 | |
| 23 | |
| 24 | def PrintHeader(var_name): |
| 25 | """Print out header for assembly file.""" |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 26 | sys.stdout.write(""" |
| 27 | #ifdef __APPLE_CC__ |
| 28 | /*\n\ |
| 29 | * The mid-2007 version of gcc that ships with Macs requires a\n\ |
| 30 | * comma on the .section line, but the rest of the world thinks\n\ |
| 31 | * that's a syntax error. It also wants globals to be explicitly\n\ |
| 32 | * prefixed with \"_\" as opposed to modern gccs that do the\n\ |
| 33 | * prefixing for you.\n\ |
| 34 | */\n\ |
| 35 | .globl _%s\n\ |
| 36 | .section .rodata,\n\ |
| 37 | .align 8\n\ |
| 38 | _%s:\n\ |
| 39 | #else\n\ |
| 40 | .globl %s\n\ |
| 41 | .section .rodata\n\ |
| 42 | .align 8\n\ |
| 43 | %s:\n\ |
| 44 | #endif\n\ |
| 45 | """ % (var_name, var_name, var_name, var_name)) |
| 46 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 47 | |
| 48 | def File2Asm(var_name): |
| 49 | """Convert file to assembly output.""" |
| 50 | PrintHeader(var_name) |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 51 | |
| 52 | input_size = 0 |
| 53 | col = 0 |
| 54 | while True: |
| 55 | buf = sys.stdin.read(1024) |
| 56 | if len(buf) <= 0: |
| 57 | break |
| 58 | input_size += len(buf) |
| 59 | for c in buf: |
| 60 | if col == 0: |
| 61 | sys.stdout.write(".byte ") |
| 62 | sys.stdout.write("0x%02x" % ord(c)) |
| 63 | col += 1 |
| 64 | if col == 16: |
| 65 | sys.stdout.write("\n") |
| 66 | col = 0 |
| 67 | elif col % 4 == 0: |
| 68 | sys.stdout.write(", ") |
| 69 | else: |
| 70 | sys.stdout.write(",") |
Zonr Chang | d124ee6 | 2010-10-22 18:26:25 +0800 | [diff] [blame] | 71 | # always ends with 0x0 |
| 72 | sys.stdout.write("0x00") |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 73 | if col != 0: |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 74 | sys.stdout.write("\n") |
| 75 | |
| 76 | # encode file size |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 77 | PrintHeader(var_name + "_size") |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 78 | sys.stdout.write(" .long %d\n" % input_size) |
| 79 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 80 | |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 81 | def main(argv): |
| 82 | if len(argv) < 2: |
| 83 | print "usage: %s <name>" % argv[0] |
| 84 | return 1 |
| 85 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 86 | File2Asm(argv[1]) |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 87 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 88 | if __name__ == "__main__": |
Zonr Chang | 3c250c5 | 2010-10-07 12:19:23 +0800 | [diff] [blame] | 89 | main(sys.argv) |