Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: mojo/nacl/generator/generate_nacl_bindings.py

Issue 385983008: Mojo + NaCl prototype. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Edits Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/nacl/generator/NOT_FOR_COMMIT/mojo_syscall.cc ('k') | mojo/nacl/generator/interface.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 # pylint: disable=W0104,W0106,F0401,R0201
7
8 import optparse
9 import os.path
10 import string
11 import sys
12
13 import interface
14
15 # Accumulate lines of code with varying levels of indentation.
16 class CodeWriter(object):
17 def __init__(self):
18 self._lines = []
19 self._margin = ''
20 self._margin_stack = []
21
22 def __lshift__(self, line):
23 self._lines.append((self._margin + line).rstrip())
24
25 def PushMargin(self):
26 self._margin_stack.append(self._margin)
27 self._margin += ' '
28
29 def PopMargin(self):
30 self._margin = self._margin_stack.pop()
31
32 def GetValue(self):
33 return '\n'.join(self._lines).rstrip() + '\n'
34
35 def Indent(self):
36 return Indent(self)
37
38
39 # Context handler that automatically indents and dedents a CodeWriter
40 class Indent(object):
41 def __init__(self, writer):
42 self._writer = writer
43
44 def __enter__(self):
45 self._writer.PushMargin()
46
47 def __exit__(self, type_, value, traceback):
48 self._writer.PopMargin()
49
50
51 def TemplateFile(name):
52 return os.path.join(os.path.dirname(__file__), name)
53
54
55 def LoadTemplate(filename):
56 data = open(TemplateFile(filename)).read()
57 template = string.Template(data)
darin (slow to review) 2014/09/11 07:32:20 not that this should hold up your CL, but in the m
Nick Bray (chromium) 2014/09/11 18:16:23 I was unaware jinja2 was being used. I did a simp
58 return template
59
60
61 # Wraps comma separated lists as needed.
62 def Wrap(pre, items, post):
63 complete = pre + ', '.join(items) + post
64 if len(complete) <= 80:
65 return [complete]
66 lines = [pre]
67 indent = ' '
68 for i, item in enumerate(items):
69 if i < len(items) - 1:
70 lines.append(indent + item + ',')
71 else:
72 lines.append(indent + item + post)
73 return lines
74
75 def GeneratorWarning():
76 return ('// WARNING this file was generated by %s\n// Do not edit by hand.' %
77 os.path.basename(__file__))
78
79 # Untrusted library implementing the public Mojo API.
80 def GenerateLibMojo(functions, out):
81 template = LoadTemplate('libmojo.cc.template')
82
83 code = CodeWriter()
84
85 for f in functions:
86 for line in Wrap('%s %s(' % (f.returnType, f.name), f.ParamList(), '){'):
87 code << line
88 num_params = len(f.params) + 2
89
90 with code.Indent():
91 code << 'uint32_t params[%d];' % num_params
92 return_type = f.resultParam.baseType
93 if return_type == 'MojoResult':
94 default = 'MOJO_RESULT_INVALID_ARGUMENT'
95 elif return_type == 'MojoTimeTicks':
96 default = '0'
97 else:
98 raise Exception('Unhandled return type: ' + return_type)
99 code << '%s %s = %s;' % (return_type, f.resultParam.name, default)
100
101 # Message ID
102 code << 'params[0] = %d;' % f.uid
103 # Parameter pointers
104 cast_template = 'params[%d] = reinterpret_cast<uint32_t>(%s);'
105 for p in f.params:
106 ptr = p.name
107 if p.IsPassedByValue():
108 ptr = '&' + ptr
109 code << cast_template % (p.uid + 1, ptr)
110 # Return value pointer
111 code << cast_template % (num_params - 1, '&' + f.resultParam.name)
112
113 code << 'DoMojoCall(params, sizeof(params));'
114 code << 'return %s;' % f.resultParam.name
115
116 code << '}'
117 code << ''
118
119 body = code.GetValue()
120 text = template.substitute(
121 generator_warning=GeneratorWarning(),
122 body=body)
123 out.write(text)
124
125
126 # Parameters passed into trusted code are handled differently depending on
127 # details of the parameter. Encapsulate these differences in a polymorphic type.
128 class ParamImpl(object):
129 def __init__(self, param):
130 self.param = param
131
132 def CopyOut(self, code):
133 pass
134
135 def IsArray(self):
136 return False
137
138
139 class ScalarInputImpl(ParamImpl):
140 def DeclareVars(self, code):
141 code << '%s %s_value;' % (self.param.baseType, self.param.name)
142
143 def ConvertParam(self):
144 p = self.param
145 return ('ConvertScalarInput(nap, params[%d], &%s_value)' %
146 (p.uid + 1, p.name))
147
148 def CallParam(self):
149 return '%s_value' % self.param.name
150
151
152 class ScalarOutputImpl(ParamImpl):
153 def DeclareVars(self, code):
154 code << '%s volatile* %s_ptr;' % (self.param.baseType, self.param.name)
155 code << '%s %s_value;' % (self.param.baseType, self.param.name)
156
157 def ConvertParam(self):
158 p = self.param
159 return 'ConvertScalarOutput(nap, params[%d], &%s_ptr)' % (p.uid + 1, p.name)
160
161 def CallParam(self):
162 return '&%s_value' % self.param.name
163
164 def CopyOut(self, code):
165 name = self.param.name
166 code << '*%s_ptr = %s_value;' % (name, name)
167
168
169 class ScalarInOutImpl(ParamImpl):
170 def DeclareVars(self, code):
171 code << '%s volatile* %s_ptr;' % (self.param.baseType, self.param.name)
172 code << '%s %s_value;' % (self.param.baseType, self.param.name)
173
174 def ConvertParam(self):
175 p = self.param
176 return ('ConvertScalarInOut(nap, params[%d], %s, &%s_value, &%s_ptr)' %
177 (p.uid + 1, CBool(p.isOptional), p.name, p.name))
178
179 def CallParam(self):
180 name = self.param.name
181 expr = '&%s_value' % name
182 if self.param.isOptional:
183 expr = '%s_ptr ? %s : NULL' % (name, expr)
184 return expr
185
186 def CopyOut(self, code):
187 name = self.param.name
188 if self.param.isOptional:
189 code << 'if (%s_ptr != NULL) {' % (name)
190 with code.Indent():
191 code << '*%s_ptr = %s_value;' % (name, name)
192 code << '}'
193 else:
194 code << '*%s_ptr = %s_value;' % (name, name)
195
196
197 class ArrayImpl(ParamImpl):
198 def DeclareVars(self, code):
199 code << '%s %s;' % (self.param.paramType, self.param.name)
200
201 def ConvertParam(self):
202 p = self.param
203 if p.baseType == 'void':
204 return ('ConvertBytes(nap, params[%d], %s, %s, &%s)' %
205 (p.uid + 1, p.size + '_value', CBool(p.isOptional), p.name))
206 else:
207 return ('ConvertArray(nap, params[%d], %s, %s, &%s)' %
208 (p.uid + 1, p.size + '_value', CBool(p.isOptional), p.name))
209
210 def CallParam(self):
211 return self.param.name
212
213 def IsArray(self):
214 return True
215
216
217 class StructInputImpl(ParamImpl):
218 def DeclareVars(self, code):
219 code << '%s %s;' % (self.param.paramType, self.param.name)
220
221 def ConvertParam(self):
222 p = self.param
223 return ('ConvertStruct(nap, params[%d], %s, &%s)' %
224 (p.uid + 1, CBool(p.isOptional), p.name))
225
226 def CallParam(self):
227 return self.param.name
228
229
230 def ImplForParam(p):
231 if p.IsScalar():
232 if p.isOutput:
233 if p.isInput:
234 return ScalarInOutImpl(p)
235 else:
236 return ScalarOutputImpl(p)
237 else:
238 return ScalarInputImpl(p)
239 elif p.isArray:
240 return ArrayImpl(p)
241 elif p.isStruct:
242 return StructInputImpl(p)
243 else:
244 assert False, p
245
246
247 def CBool(value):
248 return 'true' if value else 'false'
249
250 # A trusted wrapper that validates the arguments passed from untrusted code
251 # before passing them to the underlying public Mojo API.
252 def GenerateMojoSyscall(functions, out):
253 template = LoadTemplate('mojo_syscall.cc.template')
254
255 code = CodeWriter()
256 code.PushMargin()
257
258 for f in functions:
259 impls = [ImplForParam(p) for p in f.params]
260 impls.append(ImplForParam(f.resultParam))
261
262 code << 'case %d:' % f.uid
263
264 code.PushMargin()
265
266 code << '{'
267
268 with code.Indent():
269 num_params = len(f.params) + 2
270 code << 'if (num_params != %d) {' % num_params
271 with code.Indent():
272 code << 'return -1;'
273 code << '}'
274
275 # Declare temporaries.
276 for impl in impls:
277 impl.DeclareVars(code)
278
279 def ConvertParam(code, impl):
280 code << 'if (!%s) {' % impl.ConvertParam()
281 with code.Indent():
282 code << 'return -1;'
283 code << '}'
284
285 code << '{'
286 with code.Indent():
287 code << 'ScopedCopyLock copy_lock(nap);'
288 # Convert and validate pointers in two passes.
289 # Arrays cannot be validated util the size parameter has been converted.
290 for impl in impls:
291 if not impl.IsArray():
292 ConvertParam(code, impl)
293 for impl in impls:
294 if impl.IsArray():
295 ConvertParam(code, impl)
296 code << '}'
297 code << ''
298
299 # Call
300 getParams = [impl.CallParam() for impl in impls[:-1]]
301 code << 'result_value = %s(%s);' % (f.name, ', '.join(getParams))
302 code << ''
303
304 # Write outputs
305 code << '{'
306 with code.Indent():
307 code << 'ScopedCopyLock copy_lock(nap);'
308 for impl in impls:
309 impl.CopyOut(code)
310 code << '}'
311 code << ''
312
313 code << 'return 0;'
314 code << '}'
315
316 code.PopMargin()
317
318 body = code.GetValue()
319 text = template.substitute(
320 generator_warning=GeneratorWarning(),
321 body=body)
322 out.write(text)
323
324 def OutFile(dir_path, name):
325 if not os.path.exists(dir_path):
326 os.makedirs(dir_path)
327 return open(os.path.join(dir_path, name), 'w')
328
329 def main(args):
330 usage = 'usage: %prog [options]'
331 parser = optparse.OptionParser(usage=usage)
332 parser.add_option(
333 '-d',
334 dest='out_dir',
335 metavar='DIR',
336 help='output generated code into directory DIR')
337 options, args = parser.parse_args(args=args)
338 if not options.out_dir:
339 parser.error('-d is required')
340 if args:
341 parser.error('unexpected positional arguments: %s' % ' '.join(args))
342
343 mojo = interface.MakeInterface()
344
345 out = OutFile(options.out_dir, 'libmojo.cc')
346 GenerateLibMojo(mojo.functions, out)
347
348 out = OutFile(options.out_dir, 'mojo_syscall.cc')
349 GenerateMojoSyscall(mojo.functions, out)
350
351
352 if __name__ == '__main__':
353 main(sys.argv[1:])
OLDNEW
« no previous file with comments | « mojo/nacl/generator/NOT_FOR_COMMIT/mojo_syscall.cc ('k') | mojo/nacl/generator/interface.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698