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