Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
#!/usr/bin/env python3
"""Make the function table of src/eagl.c: one function of ours for every entry of the table that
Darwin's OpenGLES hands out through -[EAGLContext getMacroContextPrivate] (src/eagl_slots.txt).
mkeagl.py SLOTS GL.XML EXPORTED EAGL.C > obj/eagl_gen.h
GL.XML is the Khronos registry (it comes with Mesa: src/mapi/glapi/registry/gl.xml) and gives the
prototypes; EXPORTED lists the gl* functions the OpenGL ES library to be used exports, one a line;
EAGL.C is searched for the functions written by hand (static ... my_glName(void *context, ...)),
which take the place of the generated ones.
An entry is called as entry(table[0], arguments...). What is generated for it:
- a function that counts the call and passes the arguments on to the library's function of that
name, or of the name without its OES/EXT/APPLE/ARB ending where only that one is exported;
- where the library has nothing of the sort (OpenGL ES 1 for the most part), a function that says
once which entry was called and returns 0;
- the same for the offsets between the known entries.
The header is in two parts: with EAGL_GEN_DECLARATIONS defined, the types and the prototypes of
the library's functions; with EAGL_GEN_TABLE, the functions and eagl_fill_table().
"""
import re
import sys
import xml.etree.ElementTree as ET
slots_path, xml_path, exported_path, source_path = sys.argv[1:5]
slots = [] # (offset, used by QuartzCore, [names])
for line in open(slots_path):
line = line.split('#')[0].split()
if line:
slots.append((int(line[0], 16), line[1] == '*', line[2:]))
protos = {} # name -> (return type, [(type, name)])
for command in ET.parse(xml_path).getroot().iter('command'):
proto = command.find('proto')
if proto is None:
continue
name = proto.find('name').text
ret = ''.join(proto.itertext())
ret = ret[:ret.rindex(name)].strip()
params = []
for param in command.findall('param'):
text = ''.join(param.itertext()).strip()
pname = param.find('name').text
params.append((text[:text.rindex(pname)].strip(), pname))
protos[name] = (ret, params)
exported = set(open(exported_path).read().split())
by_hand = set(re.findall(r'^my_(gl\w+)\(', open(source_path).read(), re.M))
def trace_format(ctype):
if '*' in ctype or ctype in ('GLsync', 'GLeglImageOES', 'GLDEBUGPROC', 'GLDEBUGPROCKHR'):
return '%p'
if ctype in ('GLenum', 'GLbitfield'):
return '%#x'
if ctype in ('GLfloat', 'GLclampf', 'GLdouble', 'GLclampd'):
return '%g'
if ctype in ('GLintptr', 'GLsizeiptr'):
return '%ld'
if ctype in ('GLuint64', 'GLint64'):
return '%lld'
return '%d'
def trace_value(param):
ctype, name = param
if '*' in ctype or ctype in ('GLsync', 'GLeglImageOES', 'GLDEBUGPROC', 'GLDEBUGPROCKHR'):
return '(const void *)%s' % name
if ctype in ('GLfloat', 'GLclampf', 'GLdouble', 'GLclampd'):
return '(double)%s' % name
if ctype in ('GLuint64', 'GLint64'):
return '(long long)%s' % name
return name
def library_name(name):
"""The exported function that does what `name` does."""
if name in exported:
return name
plain = re.sub(r'(OES|EXT|APPLE|ARB|IMG)$', '', name)
if plain != name and plain in exported and plain in protos:
return plain
return None
out = []
emit = out.append
emit('/* Generated by userland/mkeagl.py from src/eagl_slots.txt; not to be edited. */')
emit('#ifdef EAGL_GEN_DECLARATIONS')
needed = {}
entries = [] # (offset, name, kind, function)
for offset, used, names in slots:
hand = [n for n in names if n in by_hand]
if hand:
entries.append((offset, names[0], 'hand', 'my_' + hand[0]))
continue
choice = None
for n in names:
target = library_name(n)
if target and n in protos:
choice = (n, target)
break
if choice is None:
entries.append((offset, names[0], 'none', None))
else:
entries.append((offset, choice[0], 'pass', choice[1]))
needed[choice[1]] = protos[choice[0]]
# ... and what the functions written by hand call
for name in set(re.findall(r'(?<![\w])(gl[A-Z]\w*)\s*\(', open(source_path).read())):
if name in exported and name in protos:
needed.setdefault(name, protos[name])
for name in sorted(needed):
ret, params = needed[name]
emit('extern %s %s(%s);' % (ret, name, ', '.join('%s %s' % p for p in params) or 'void'))
emit('#endif')
emit('#ifdef EAGL_GEN_TABLE')
known = {}
for offset, name, kind, function in entries:
known[offset] = name
if kind == 'pass':
ret, params = protos[name]
args = ', '.join(p[1] for p in params)
emit('static %s\nslot_%04x(void *context%s)\n{' % (ret, offset, ''.join(', %s %s' % p for p in params)))
emit('\tEAGL_ENTER(context, %#x);' % offset)
emit('\tif (eagl_tracing) {\n\t\teagl_trace("%s(%s)\\n"%s);' % (name, ', '.join(trace_format(p[0]) for p in params),
''.join(', ' + trace_value(p) for p in params)))
vector = re.match(r'glUniform(Matrix)?([1-4])fv$', name)
if vector:
n = int(vector.group(2))
emit('\t\teagl_trace_floats(value, count * %d);' % (n * n if vector.group(1) else n))
emit('\t}')
if ret == 'void':
emit('\t%s(%s);\n\tEAGL_LEAVE(context, %#x);\n}' % (function, args, offset))
else:
emit('\t%s result = %s(%s);\n\tEAGL_LEAVE(context, %#x);\n\treturn result;\n}' % (ret, function, args, offset))
elif kind == 'none':
emit('static long slot_%04x(void *context) { return eagl_missing(context, %#x); }' % (offset, offset))
top = max(known) + 0x100
for offset in range(8, top, 8):
if offset not in known:
emit('static long slot_%04x(void *context) { return eagl_missing(context, %#x); }' % (offset, offset))
emit('#define EAGL_TABLE_SIZE %#x' % top)
emit('static const char *\neagl_slot_name(unsigned offset)\n{\n\tswitch (offset) {')
for offset in sorted(known):
emit('\tcase %#x: return "%s";' % (offset, known[offset]))
emit('\tdefault: return "?";\n\t}\n}')
emit('static void\neagl_fill_table(void **table)\n{')
hand = {offset: function for offset, name, kind, function in entries if kind == 'hand'}
for offset in range(8, top, 8):
emit('\ttable[%#x / 8] = (void *)%s;' % (offset, hand.get(offset, 'slot_%04x' % offset)))
emit('}')
emit('#endif')
print('\n'.join(out))
passed = sum(1 for e in entries if e[2] == 'pass')
missing = [e[1] for e in entries if e[2] == 'none']
used_missing = [name for (offset, used, names), e in zip(slots, entries) if used and e[2] == 'none' for name in [e[1]]]
print('mkeagl.py: %d entries: %d passed on, %d by hand, %d without a function in the library%s' % (
len(entries), passed, len(hand), len(missing),
'; of those QuartzCore calls: ' + ' '.join(used_missing) if used_missing else ''), file=sys.stderr)