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 | |
| 18 | import os, sys |
| 19 | |
| 20 | def print_header(var_name): |
| 21 | sys.stdout.write(""" |
| 22 | #ifdef __APPLE_CC__ |
| 23 | /*\n\ |
| 24 | * The mid-2007 version of gcc that ships with Macs requires a\n\ |
| 25 | * comma on the .section line, but the rest of the world thinks\n\ |
| 26 | * that's a syntax error. It also wants globals to be explicitly\n\ |
| 27 | * prefixed with \"_\" as opposed to modern gccs that do the\n\ |
| 28 | * prefixing for you.\n\ |
| 29 | */\n\ |
| 30 | .globl _%s\n\ |
| 31 | .section .rodata,\n\ |
| 32 | .align 8\n\ |
| 33 | _%s:\n\ |
| 34 | #else\n\ |
| 35 | .globl %s\n\ |
| 36 | .section .rodata\n\ |
| 37 | .align 8\n\ |
| 38 | %s:\n\ |
| 39 | #endif\n\ |
| 40 | """ % (var_name, var_name, var_name, var_name)) |
| 41 | |
| 42 | def file2asm(var_name): |
| 43 | print_header(var_name) |
| 44 | |
| 45 | input_size = 0 |
| 46 | col = 0 |
| 47 | while True: |
| 48 | buf = sys.stdin.read(1024) |
| 49 | if len(buf) <= 0: |
| 50 | break |
| 51 | input_size += len(buf) |
| 52 | for c in buf: |
| 53 | if col == 0: |
| 54 | sys.stdout.write(".byte ") |
| 55 | sys.stdout.write("0x%02x" % ord(c)) |
| 56 | col += 1 |
| 57 | if col == 16: |
| 58 | sys.stdout.write("\n") |
| 59 | col = 0 |
| 60 | elif col % 4 == 0: |
| 61 | sys.stdout.write(", ") |
| 62 | else: |
| 63 | sys.stdout.write(",") |
| 64 | # always ends with 0x0 (can fix assembler warnings) |
| 65 | if col != 0: |
| 66 | sys.stdout.write("0x00") |
| 67 | sys.stdout.write("\n") |
| 68 | |
| 69 | # encode file size |
| 70 | print_header(var_name + "_size") |
| 71 | sys.stdout.write(" .long %d\n" % input_size) |
| 72 | |
| 73 | def main(argv): |
| 74 | if len(argv) < 2: |
| 75 | print "usage: %s <name>" % argv[0] |
| 76 | return 1 |
| 77 | |
| 78 | file2asm(argv[1]) |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | main(sys.argv) |