Index: mojo/monacl/generator/generate_monacl_bindings.py |
diff --git a/mojo/monacl/generator/generate_monacl_bindings.py b/mojo/monacl/generator/generate_monacl_bindings.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..8c8c0a180c63e2f312528d4f6ad66bb6af997c91 |
--- /dev/null |
+++ b/mojo/monacl/generator/generate_monacl_bindings.py |
@@ -0,0 +1,354 @@ |
+#!/usr/bin/python |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+# pylint: disable=W0104,W0106,F0401,R0201 |
+ |
+import optparse |
+import os.path |
+import string |
+import sys |
+ |
+import interface |
+ |
+# Accumulate lines of code with varying levels of indentation. |
+class CodeWriter(object): |
+ def __init__(self): |
+ self._lines = [] |
+ self._margin = '' |
+ self._margin_stack = [] |
+ |
+ def __lshift__(self, line): |
+ self._lines.append((self._margin + line).rstrip()) |
+ |
+ def pushMargin(self): |
Mark Seaborn
2014/09/10 23:19:20
I realised that a lot of these method names are no
Nick Bray (chromium)
2014/09/11 00:56:39
Done.
|
+ self._margin_stack.append(self._margin) |
+ self._margin += ' ' |
+ |
+ def popMargin(self): |
+ self._margin = self._margin_stack.pop() |
+ |
+ def getValue(self): |
+ return '\n'.join(self._lines).rstrip() + '\n' |
+ |
+ def indent(self): |
+ return Indent(self) |
+ |
+ |
+# Context handler that automatically indents and dedents a CodeWriter |
+class Indent(object): |
+ def __init__(self, writer): |
+ self.writer = writer |
Mark Seaborn
2014/09/10 23:19:20
This field can be private
Nick Bray (chromium)
2014/09/11 00:56:39
Done.
|
+ |
+ def __enter__(self): |
+ self.writer.pushMargin() |
+ |
+ def __exit__(self, type_, value, traceback): |
+ self.writer.popMargin() |
+ |
+ |
+def TemplateFile(name): |
+ return os.path.join(os.path.dirname(__file__), name) |
+ |
+ |
+def LoadTemplate(filename): |
+ data = open(TemplateFile(filename)).read() |
+ template = string.Template(data) |
+ return template |
+ |
+ |
+# Wraps comma separated lists as needed. |
+def Wrap(pre, items, post): |
+ complete = pre + ', '.join(items) + post |
+ if len(complete) <= 80: |
+ return [complete] |
+ lines = [pre] |
+ indent = ' ' |
+ for i, item in enumerate(items): |
+ if i < len(items) - 1: |
+ lines.append(indent + item + ',') |
+ else: |
+ lines.append(indent + item + post) |
+ return lines |
+ |
+def GeneratorWarning(): |
+ return ('// WARNING this file was generated by %s\n// Do not edit by hand.' % |
+ os.path.basename(__file__)) |
+ |
+# Untrusted library implementing the public Mojo API. |
+def GenerateLibMojo(functions, out): |
+ template = LoadTemplate('libmojo.cc.template') |
+ |
+ code = CodeWriter() |
+ |
+ for f in functions: |
+ for line in Wrap('%s %s(' % (f.returnType, f.name), f.paramList(), '){'): |
+ code << line |
+ numParams = len(f.params) + 2 |
Mark Seaborn
2014/09/10 23:19:20
"num_params"
Nick Bray (chromium)
2014/09/11 00:56:40
Done.
|
+ |
+ with code.indent(): |
+ code << 'uint32_t params[%d];' % numParams |
+ returnType = f.resultParam.baseType |
Mark Seaborn
2014/09/10 23:19:20
"return_type"
Nick Bray (chromium)
2014/09/11 00:56:39
Done.
|
+ if returnType == 'MojoResult': |
+ default = 'MOJO_RESULT_INVALID_ARGUMENT' |
+ elif returnType == 'MojoTimeTicks': |
+ default = '0' |
+ else: |
+ raise Exception('Unhandled return type: ' + returnType) |
+ code << '%s %s = %s;' % (returnType, f.resultParam.name, default) |
+ |
+ # Message ID |
+ code << 'params[0] = %d;' % f.uid |
+ # Parameter pointers |
+ castTemplate = 'params[%d] = reinterpret_cast<uint32_t>(%s);' |
Mark Seaborn
2014/09/10 23:19:20
"cast_template"
Nick Bray (chromium)
2014/09/11 00:56:40
Done.
|
+ for p in f.params: |
+ ptr = p.name |
+ if p.isPassedByValue(): |
+ ptr = '&' + ptr |
+ code << castTemplate % (p.uid + 1, ptr) |
Mark Seaborn
2014/09/10 23:19:20
Nit: remove one space after "<<"
Nick Bray (chromium)
2014/09/11 00:56:40
Done.
|
+ # Return value pointer |
+ code << castTemplate % (numParams - 1, '&' + f.resultParam.name) |
+ |
+ code << 'DoMojoCall(params, sizeof(params));' |
+ code << 'return %s;' % f.resultParam.name |
+ |
+ code << '}' |
+ code << '' |
+ |
+ body = code.getValue() |
+ text = template.substitute( |
+ generator_warning=GeneratorWarning(), |
+ body=body) |
+ out.write(text) |
+ |
+ |
+# Parameters passed into trusted code are handled differently depending on |
+# details of the parameter. Encapsulate these differences in a polymorphic type. |
+class ParamImpl(object): |
+ def __init__(self, param): |
+ self.param = param |
+ |
+ def copyOut(self, code): |
+ pass |
+ |
+ def isArray(self): |
+ return False |
+ |
+ |
+class ScalarInputImpl(ParamImpl): |
+ def declareVars(self, code): |
+ code << '%s %s_value;' % (self.param.baseType, self.param.name) |
+ |
+ def convertParam(self): |
+ p = self.param |
+ return ('ConvertScalarInput(nap, params[%d], &%s_value)' % |
+ (p.uid + 1, p.name)) |
+ |
+ def callParam(self): |
+ return '%s_value' % self.param.name |
+ |
+ |
+class ScalarOutputImpl(ParamImpl): |
+ def declareVars(self, code): |
+ code << '%s volatile* %s_ptr;' % (self.param.baseType, self.param.name) |
+ code << '%s %s_value;' % (self.param.baseType, self.param.name) |
+ |
+ def convertParam(self): |
+ p = self.param |
+ return 'ConvertScalarOutput(nap, params[%d], &%s_ptr)' % (p.uid + 1, p.name) |
+ |
+ def callParam(self): |
+ return '&%s_value' % self.param.name |
+ |
+ def copyOut(self, code): |
+ name = self.param.name |
+ code << '*%s_ptr = %s_value;' % (name, name) |
+ |
+ |
+class ScalarInOutImpl(ParamImpl): |
+ def declareVars(self, code): |
+ code << '%s volatile* %s_ptr;' % (self.param.baseType, self.param.name) |
+ code << '%s %s_value;' % (self.param.baseType, self.param.name) |
+ |
+ def convertParam(self): |
+ p = self.param |
+ return ('ConvertScalarInOut(nap, params[%d], %s, &%s_value, &%s_ptr)' % |
+ (p.uid + 1, CBool(p.isOptional), p.name, p.name)) |
+ |
+ def callParam(self): |
+ name = self.param.name |
+ expr = '&%s_value' % name |
+ if self.param.isOptional: |
+ expr = '%s_ptr ? %s : NULL' % (name, expr) |
+ return expr |
+ |
+ def copyOut(self, code): |
+ name = self.param.name |
+ if self.param.isOptional: |
+ code << 'if (%s_ptr != NULL) {' % (name) |
+ with code.indent(): |
+ code << '*%s_ptr = %s_value;' % (name, name) |
+ code << '}' |
+ else: |
+ code << '*%s_ptr = %s_value;' % (name, name) |
+ |
+ |
+class ArrayImpl(ParamImpl): |
+ def declareVars(self, code): |
+ code << '%s %s;' % (self.param.paramType, self.param.name) |
+ |
+ def convertParam(self): |
+ p = self.param |
+ if p.baseType == 'void': |
+ return ('ConvertBytes(nap, params[%d], %s, %s, &%s)' % |
+ (p.uid + 1, p.size + '_value', CBool(p.isOptional), p.name)) |
+ else: |
+ return ('ConvertArray(nap, params[%d], %s, %s, &%s)' % |
+ (p.uid + 1, p.size + '_value', CBool(p.isOptional), p.name)) |
+ |
+ def callParam(self): |
+ return self.param.name |
+ |
+ def isArray(self): |
+ return True |
+ |
+ |
+class StructInputImpl(ParamImpl): |
+ def declareVars(self, code): |
+ code << '%s %s;' % (self.param.paramType, self.param.name) |
+ |
+ def convertParam(self): |
+ p = self.param |
+ return ('ConvertStruct(nap, params[%d], %s, &%s)' % |
+ (p.uid + 1, CBool(p.isOptional), p.name)) |
+ |
+ def callParam(self): |
+ return self.param.name |
+ |
+ |
+def ImplForParam(p): |
+ if p.isScalar(): |
+ if p.isOutput: |
+ if p.isInput: |
+ return ScalarInOutImpl(p) |
+ else: |
+ return ScalarOutputImpl(p) |
+ else: |
+ return ScalarInputImpl(p) |
+ elif p.isArray: |
+ return ArrayImpl(p) |
+ elif p.isStruct: |
+ return StructInputImpl(p) |
+ else: |
+ assert False, p |
+ |
+ |
+def CBool(value): |
+ return 'true' if value else 'false' |
+ |
+# A trusted wrapper that validates the arguments passed from untrusted code |
+# before passing them to the underlying public Mojo API. |
+def GenerateMojoSyscall(functions, out): |
+ template = LoadTemplate('mojo_syscall.cc.template') |
+ |
+ code = CodeWriter() |
+ code.pushMargin() |
+ |
+ for f in functions: |
+ impls = [ImplForParam(p) for p in f.params] |
+ impls.append(ImplForParam(f.resultParam)) |
+ |
+ code << 'case %d:' % f.uid |
+ |
+ code.pushMargin() |
+ |
+ code << '{' |
+ |
+ with code.indent(): |
+ numParams = len(f.params) + 2 |
+ code << 'if (num_params != %d) {' % numParams |
+ with code.indent(): |
+ code << 'return -1;' |
+ code << '}' |
+ |
+ # Declare temporaries. |
+ for impl in impls: |
+ impl.declareVars(code) |
+ |
+ def convertParam(code, impl): |
+ code << 'if (!%s) {' % impl.convertParam() |
+ with code.indent(): |
+ code << 'return -1;' |
+ code << '}' |
+ |
+ code << '{' |
+ with code.indent(): |
+ code << 'ScopedCopyLock copy_lock(nap);' |
+ # Convert and validate pointers in two passes. |
+ # Arrays cannot be validated util the size parameter has been converted. |
+ for impl in impls: |
+ if not impl.isArray(): |
+ convertParam(code, impl) |
+ for impl in impls: |
+ if impl.isArray(): |
+ convertParam(code, impl) |
+ code << '}' |
+ code << '' |
+ |
+ # Call |
+ getParams = [impl.callParam() for impl in impls[:-1]] |
+ code << 'result_value = %s(%s);' % (f.name, ', '.join(getParams)) |
+ code << '' |
+ |
+ # Write outputs |
+ code << '{' |
+ with code.indent(): |
+ code << 'ScopedCopyLock copy_lock(nap);' |
+ # TODO copy out on failure? |
Mark Seaborn
2014/09/10 23:19:20
Can you elaborate or remove this?
Nick Bray (chromium)
2014/09/11 00:56:40
Done.
|
+ for impl in impls: |
+ impl.copyOut(code) |
+ code << '}' |
+ code << '' |
+ |
+ code << 'return 0;' |
+ code << '}' |
+ |
+ code.popMargin() |
+ |
+ body = code.getValue() |
+ text = template.substitute( |
+ generator_warning=GeneratorWarning(), |
+ body=body) |
+ out.write(text) |
+ |
+def OutFile(dir_path, name): |
+ if not os.path.exists(dir_path): |
+ os.makedirs(dir_path) |
+ return open(os.path.join(dir_path, name), 'w') |
+ |
+def main(args): |
+ usage = 'usage: %prog [options]' |
+ parser = optparse.OptionParser(usage=usage) |
+ parser.add_option( |
+ '-d', |
+ dest='out_dir', |
+ metavar='DIR', |
+ help='output generated code into directory DIR') |
+ options, args = parser.parse_args(args=args) |
+ if not options.out_dir: |
+ parser.error('-d is required') |
+ if args: |
+ parser.error('unexpected positional arguments: %s' % ' '.join(args)) |
+ |
+ mojo = interface.MakeInterface() |
+ |
+ out = OutFile(options.out_dir, 'libmojo.cc') |
+ GenerateLibMojo(mojo.functions, out) |
+ |
+ out = OutFile(options.out_dir, 'mojo_syscall.cc') |
+ GenerateMojoSyscall(mojo.functions, out) |
+ |
+ |
+if __name__ == '__main__': |
+ main(sys.argv[1:]) |