blob: f0cedf26ce1c6acd0564f5ed0f48f05c08d520eb [file] [log] [blame]
Mike Kleina7080262017-01-09 10:20:13 -05001#!/usr/bin/env python2.7
2#
3# Copyright 2017 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8import re
9import subprocess
10import sys
11
Mike Klein8e619a22017-01-09 17:21:32 -050012cflags = '-std=c++11 -Os -fomit-frame-pointer'.split()
Mike Kleina7080262017-01-09 10:20:13 -050013
Mike Klein8e619a22017-01-09 17:21:32 -050014hsw = '-mavx2 -mfma -mf16c'.split()
15subprocess.check_call(['clang++'] + cflags + hsw +
Mike Kleina7080262017-01-09 10:20:13 -050016 ['-c', 'src/splicer/SkSplicer_stages.cpp'] +
Mike Klein8e619a22017-01-09 17:21:32 -050017 ['-o', 'hsw.o'])
Mike Kleinf7200982017-01-15 18:14:07 -050018subprocess.check_call(['clang++'] + cflags + hsw +
19 ['-c', 'src/splicer/SkSplicer_stages_lowp.cpp'] +
20 ['-o', 'hsw_lowp.o'])
Mike Klein8e619a22017-01-09 17:21:32 -050021
22aarch64 = [
23 '--target=aarch64-linux-android',
24 '--sysroot=' +
25 '/Users/mtklein/brew/opt/android-ndk/platforms/android-21/arch-arm64',
26]
27subprocess.check_call(['clang++'] + cflags + aarch64 +
28 ['-c', 'src/splicer/SkSplicer_stages.cpp'] +
29 ['-o', 'aarch64.o'])
Mike Kleinf7200982017-01-15 18:14:07 -050030subprocess.check_call(['clang++'] + cflags + aarch64 +
31 ['-c', 'src/splicer/SkSplicer_stages_lowp.cpp'] +
32 ['-o', 'aarch64_lowp.o'])
Mike Kleina7080262017-01-09 10:20:13 -050033
Mike Klein4ef8cb32017-01-12 11:36:46 -050034armv7 = [
35 '--target=arm-linux-androideabi',
36 '--sysroot=' +
37 '/Users/mtklein/brew/opt/android-ndk/platforms/android-18/arch-arm',
38 '-march=armv7-a',
39 '-mfpu=neon-vfpv4',
40]
41subprocess.check_call(['clang++'] + cflags + armv7 +
42 ['-c', 'src/splicer/SkSplicer_stages.cpp'] +
43 ['-o', 'armv7.o'])
Mike Kleinf7200982017-01-15 18:14:07 -050044subprocess.check_call(['clang++'] + cflags + armv7 +
45 ['-c', 'src/splicer/SkSplicer_stages_lowp.cpp'] +
46 ['-o', 'armv7_lowp.o'])
Mike Klein4ef8cb32017-01-12 11:36:46 -050047
Mike Kleinf7200982017-01-15 18:14:07 -050048def parse_object_file(dst, dot_o, array_type, done, target=None):
Mike Klein4ef8cb32017-01-12 11:36:46 -050049 cmd = ['gobjdump', '-d', dot_o]
50 if target:
51 cmd += ['--target', target]
52 for line in subprocess.check_output(cmd).split('\n'):
Mike Kleinfb003902017-01-10 16:11:15 -050053 line = line.strip()
54 if not line or line.startswith(dot_o) or line.startswith('Disassembly'):
55 continue
56
57 # E.g. 00000000000003a4 <_load_f16>:
Mike Klein4ef8cb32017-01-12 11:36:46 -050058 m = re.match('''[0-9a-f]+ <_?(.*)>:''', line)
Mike Kleinfb003902017-01-10 16:11:15 -050059 if m:
Mike Kleinf7200982017-01-15 18:14:07 -050060 print >>dst,'static const', array_type, 'kSplice_' + m.group(1) + '[] = {'
Mike Kleinfb003902017-01-10 16:11:15 -050061 continue
62
63 columns = line.split('\t')
64 code = columns[1]
Mike Klein4ef8cb32017-01-12 11:36:46 -050065 if len(columns) >= 4:
Mike Kleinfb003902017-01-10 16:11:15 -050066 inst = columns[2]
67 args = columns[3]
68 else:
69 inst, args = columns[2].split(' ', 1)
70 code, inst, args = code.strip(), inst.strip(), args.strip()
71
72 # We can't splice code that uses ip-relative addressing.
73 for arg in args:
74 assert 'rip' not in arg # TODO: detect on aarch64 too
75
76 if code == done:
Mike Kleinf7200982017-01-15 18:14:07 -050077 print >>dst,'};'
Mike Kleinfb003902017-01-10 16:11:15 -050078 continue
79
80 hexed = ''.join('0x'+x+',' for x in code.split(' '))
Mike Kleinf7200982017-01-15 18:14:07 -050081 print >>dst,' ' + hexed + ' '*(44-len(hexed)) + \
82 '// ' + inst + ' '*(14-len(inst)) + args
Mike Kleinfb003902017-01-10 16:11:15 -050083
Mike Kleinf7200982017-01-15 18:14:07 -050084for suffix in ['', '_lowp']:
85 with open('src/splicer/SkSplicer_generated%s.h' % suffix, 'w') as f:
86 print >>f,'''/*
Mike Kleina7080262017-01-09 10:20:13 -050087 * Copyright 2017 Google Inc.
88 *
89 * Use of this source code is governed by a BSD-style license that can be
90 * found in the LICENSE file.
91 */
92
Mike Kleinf7200982017-01-15 18:14:07 -050093#ifndef SkSplicer_generated%s_DEFINED
94#define SkSplicer_generated%s_DEFINED
Mike Kleina7080262017-01-09 10:20:13 -050095
96// This file is generated semi-automatically with this command:
Mike Kleinf7200982017-01-15 18:14:07 -050097// $ src/splicer/build_stages.py
Mike Klein8e619a22017-01-09 17:21:32 -050098
99#if defined(__aarch64__)
Mike Kleinf7200982017-01-15 18:14:07 -0500100''' % (suffix, suffix)
101 parse_object_file(f, 'aarch64%s.o' % suffix, 'unsigned int', '14000000')
102 print >>f,'\n#elif defined(__ARM_NEON__)\n'
103 parse_object_file(f, 'armv7%s.o' % suffix, 'unsigned int', 'eafffffe',
104 target='elf32-littlearm')
105 print >>f,'\n#else\n'
106 parse_object_file(f, 'hsw%s.o' % suffix, 'unsigned char', 'e9 00 00 00 00')
107 print >>f,'\n#endif\n'
108 print >>f,'#endif//SkSplicer_generated%s_DEFINED' % suffix