Skip to content

Commit 3e9b726

Browse files
committed
Style the generator script more PEP8y.
1 parent 24416a2 commit 3e9b726

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/etc/platform-intrinsics/generator.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class PlatformInfo(object):
2424
def __init__(self, json):
2525
self._platform = json['platform']
2626
self._intrinsic_prefix = json['intrinsic_prefix']
27+
2728
def intrinsic_prefix(self):
2829
return self._intrinsic_prefix
2930

30-
3131
class IntrinsicSet(object):
3232
def __init__(self, platform, json):
3333
self._llvm_prefix = json['llvm_prefix']
@@ -41,10 +41,13 @@ def intrinsics(self):
4141
yield GenericIntrinsic(self,
4242
raw['intrinsic'], raw['width'], raw['llvm'],
4343
raw['ret'], raw['args'])
44+
4445
def platform(self):
4546
return self._platform
47+
4648
def llvm_prefix(self):
4749
return self._llvm_prefix
50+
4851
def width_info(self, bitwidth):
4952
return self._widths[str(bitwidth)]
5053

@@ -67,8 +70,10 @@ class PlatformTypeInfo(object):
6770
def __init__(self, llvm_name, properties):
6871
self.properties = properties
6972
self.llvm_name = llvm_name
73+
7074
def __getattr__(self, name):
7175
return self.properties[name]
76+
7277
def vectorize(self, length, width_info):
7378
props = self.properties.copy()
7479
props.update(width_info)
@@ -80,12 +85,14 @@ def __init__(self, bitwidth):
8085

8186
def bitwidth(self):
8287
return self._bitwidth
88+
8389
def modify(self, spec, width):
8490
raise NotImplementedError()
8591

8692
class Number(Type):
8793
def __init__(self, bitwidth):
8894
Type.__init__(self, bitwidth)
95+
8996
def modify(self, spec, width):
9097
if spec == 'u':
9198
return Unsigned(self.bitwidth())
@@ -106,30 +113,40 @@ def type_info(self, platform_info):
106113
class Signed(Number):
107114
def __init__(self, bitwidth):
108115
Number.__init__(self, bitwidth)
116+
109117
def compiler_ctor(self):
110118
return 'i({})'.format(self.bitwidth())
119+
111120
def llvm_name(self):
112121
return 'i{}'.format(self.bitwidth())
122+
113123
def rust_name(self):
114124
return 'i{}'.format(self.bitwidth())
115125

116126
class Unsigned(Number):
117127
def __init__(self, bitwidth):
118128
Number.__init__(self, bitwidth)
129+
119130
def compiler_ctor(self):
120131
return 'u({})'.format(self.bitwidth())
132+
121133
def llvm_name(self):
122134
return 'i{}'.format(self.bitwidth())
135+
123136
def rust_name(self):
124137
return 'u{}'.format(self.bitwidth())
138+
125139
class Float(Number):
126140
def __init__(self, bitwidth):
127141
assert bitwidth in (32, 64)
128142
Number.__init__(self, bitwidth)
143+
129144
def compiler_ctor(self):
130145
return 'f({})'.format(self.bitwidth())
146+
131147
def llvm_name(self):
132148
return 'f{}'.format(self.bitwidth())
149+
133150
def rust_name(self):
134151
return 'f{}'.format(self.bitwidth())
135152

@@ -140,6 +157,7 @@ def __init__(self, elem, length):
140157
elem.bitwidth() * length)
141158
self._length = length
142159
self._elem = elem
160+
143161
def modify(self, spec, width):
144162
if spec == 'h':
145163
return Vector(self._elem, self._length // 2)
@@ -150,10 +168,13 @@ def modify(self, spec, width):
150168
return Vector(self._elem, new_bitwidth // self._elem.bitwidth())
151169
else:
152170
return Vector(self._elem.modify(spec, width), self._length)
171+
153172
def compiler_ctor(self):
154173
return 'v({}, {})'.format(self._elem.compiler_ctor(), self._length)
174+
155175
def rust_name(self):
156176
return '{}x{}'.format(self._elem.rust_name(), self._length)
177+
157178
def type_info(self, platform_info):
158179
elem_info = self._elem.type_info(platform_info)
159180
return elem_info.vectorize(self._length,
@@ -163,15 +184,18 @@ class Aggregate(Type):
163184
def __init__(self, flatten, elems):
164185
self._flatten = flatten
165186
self._elems = elems
166-
Type.__init__(self,
167-
sum(elem.bitwidth() for elem in elems))
187+
Type.__init__(self, sum(elem.bitwidth() for elem in elems))
188+
168189
def __repr__(self):
169190
return '<Aggregate {}>'.format(self._elems)
191+
170192
def compiler_ctor(self):
171193
return 'agg({}, vec![{}])'.format('true' if self._flatten else 'false',
172194
', '.join(elem.compiler_ctor() for elem in self._elems))
195+
173196
def rust_name(self):
174197
return '({})'.format(', '.join(elem.rust_name() for elem in self._elems))
198+
175199
def type_info(self, platform_info):
176200
#return PlatformTypeInfo(None, None, self._llvm_name)
177201
return None
@@ -188,6 +212,7 @@ def __init__(self, spec):
188212
spec = [spec]
189213

190214
self.spec = spec
215+
191216
def enumerate(self, width):
192217
for spec in self.spec:
193218
match = SPEC.match(spec)
@@ -208,13 +233,13 @@ def enumerate(self, width):
208233
for ctor in type_ctors:
209234
scalar = ctor(bitwidth)
210235
if is_vector:
211-
212236
yield Vector(scalar, width // bitwidth)
213237
else:
214238
yield scalar
215239
bitwidth *= 2
216240
else:
217241
print('Failed to parse: `{}`'.format(spec), file=sys.stderr)
242+
218243
def resolve(self, width, zero):
219244
assert len(self.spec) == 1
220245
spec = self.spec[0]
@@ -272,23 +297,30 @@ def __init__(self, platform, intrinsic, width, llvm_name, ret, args):
272297
self._ret = ret.type_info(platform)
273298
self._args_raw = args
274299
self._args = [arg.type_info(platform) for arg in args]
300+
275301
def llvm_name(self):
276302
if self._llvm_name.startswith('!'):
277303
return self._llvm_name[1:].format(self._ret, *self._args)
278304
else:
279305
return self._platform.llvm_prefix() + self._llvm_name.format(self._ret, *self._args)
306+
280307
def intrinsic_suffix(self):
281308
return self._intrinsic.format(self._ret,
282309
*self._args,
283310
width = self._width)
311+
284312
def intrinsic_name(self):
285313
return self._platform.platform().intrinsic_prefix() + self.intrinsic_suffix()
314+
286315
def compiler_args(self):
287316
return ', '.join(arg.compiler_ctor() for arg in self._args_raw)
317+
288318
def compiler_ret(self):
289319
return self._ret_raw.compiler_ctor()
320+
290321
def compiler_signature(self):
291322
return '({}) -> {}'.format(self.compiler_args(), self.compiler_ret())
323+
292324
def intrinsic_signature(self):
293325
names = 'xyzwabcdef'
294326
return '({}) -> {}'.format(', '.join('{}: {}'.format(name, arg.rust_name())
@@ -410,17 +442,21 @@ def parse_args():
410442
class ExternBlock(object):
411443
def __init__(self):
412444
pass
445+
413446
def open(self, platform):
414447
return 'extern "platform-intrinsic" {'
448+
415449
def render(self, mono):
416450
return ' fn {}{};'.format(mono.intrinsic_name(),
417451
mono.intrinsic_signature())
452+
418453
def close(self):
419454
return '}'
420455

421456
class CompilerDefs(object):
422457
def __init__(self):
423458
pass
459+
424460
def open(self, platform):
425461
return '''\
426462
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
@@ -456,6 +492,7 @@ def render(self, mono):
456492
mono.compiler_args(),
457493
mono.compiler_ret(),
458494
mono.llvm_name())
495+
459496
def close(self):
460497
return '''\
461498
_ => return None,
@@ -499,6 +536,7 @@ def main():
499536
for intr in intrinsics.intrinsics():
500537
for mono in intr.monomorphise():
501538
print(out_format.render(mono), file=out)
539+
502540
print(out_format.close(), file=out)
503541

504542
if __name__ == '__main__':

0 commit comments

Comments
 (0)