Chromium Code Reviews| 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..a4964d4e6950b52d0fe86210e7bd56d45995f311 |
| --- /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 = [] |
|
Mark Seaborn
2014/09/09 19:13:11
Can these be made private?
Nick Bray (chromium)
2014/09/09 23:12:32
Done.
|
| + self.margin = "" |
| + self.margin_stack = [] |
| + |
| + def __lshift__(self, line): |
| + self.lines.append((self.margin + line).rstrip()) |
| + |
| + def pushMargin(self): |
| + 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 |
| + |
| + 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 seperated lists as needed. |
|
Mark Seaborn
2014/09/09 19:13:11
"separated"
Nick Bray (chromium)
2014/09/09 23:12:32
Done.
|
| +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 |
| + |
| + with code.indent(): |
| + code << "uint32_t params[%d];" % numParams |
| + returnType = f.resultParam.baseType |
| + 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);" |
| + for p in f.params: |
| + ptr = p.name |
| + if p.isPassedByValue(): |
| + ptr = "&" + ptr |
| + code << castTemplate % (p.uid + 1, ptr) |
| + # 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 (numParams != %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? |
| + 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:]) |