Make generated code more consistent with current rustfmt.

This commit is contained in:
Dan Gohman
2017-10-30 10:06:23 -07:00
parent 1b61d5cd1e
commit fae5ffb556
4 changed files with 34 additions and 26 deletions

View File

@@ -133,17 +133,21 @@ def gen_isa(isa, fmt):
'''.format(isa.name)) '''.format(isa.name))
if len(isa.all_recipes) == 0: if len(isa.all_recipes) == 0:
# No encoding recipes: Emit a stub. # No encoding recipes: Emit a stub.
with fmt.indented( with fmt.indented('pub fn emit_inst<CS: CodeSink + ?Sized>('):
'pub fn emit_inst<CS: CodeSink + ?Sized>' fmt.line('func: &Function,')
'(func: &Function, inst: Inst, ' fmt.line('inst: Inst,')
'_divert: &mut RegDiversions, _sink: &mut CS) {', '}'): fmt.line('_divert: &mut RegDiversions,')
fmt.line('_sink: &mut CS,')
with fmt.indented(') {', '}'):
fmt.line('bad_encoding(func, inst)') fmt.line('bad_encoding(func, inst)')
else: else:
fmt.line('#[allow(unused_variables, unreachable_code)]') fmt.line('#[allow(unused_variables, unreachable_code)]')
with fmt.indented( with fmt.indented('pub fn emit_inst<CS: CodeSink + ?Sized>('):
'pub fn emit_inst<CS: CodeSink + ?Sized>' fmt.line('func: &Function,')
'(func: &Function, inst: Inst, ' fmt.line('inst: Inst,')
'divert: &mut RegDiversions, sink: &mut CS) {', '}'): fmt.line('divert: &mut RegDiversions,')
fmt.line('sink: &mut CS,')
with fmt.indented(') {', '}'):
fmt.line('let encoding = func.encodings[inst];') fmt.line('let encoding = func.encodings[inst];')
fmt.line('let bits = encoding.bits();') fmt.line('let bits = encoding.bits();')
with fmt.indented('match func.encodings[inst].recipe() {', '}'): with fmt.indented('match func.encodings[inst].recipe() {', '}'):
@@ -151,7 +155,7 @@ def gen_isa(isa, fmt):
fmt.comment(recipe.name) fmt.comment(recipe.name)
with fmt.indented('{} => {{'.format(i), '}'): with fmt.indented('{} => {{'.format(i), '}'):
gen_recipe(recipe, fmt) gen_recipe(recipe, fmt)
fmt.line('_ => {}') fmt.line('_ => {},')
# Allow for un-encoded ghost instructions. # Allow for un-encoded ghost instructions.
# Verifier checks the details. # Verifier checks the details.
with fmt.indented('if encoding.is_legal() {', '}'): with fmt.indented('if encoding.is_legal() {', '}'):

View File

@@ -97,7 +97,7 @@ def gen_arguments_method(fmt, is_mut):
capture = 'ref {}args, '.format(mut) capture = 'ref {}args, '.format(mut)
arg = 'args' arg = 'args'
fmt.line( fmt.line(
'{} {{ {} .. }} => {},' '{} {{ {}.. }} => {},'
.format(n, capture, arg)) .format(n, capture, arg))
@@ -271,7 +271,7 @@ def gen_opcodes(groups, fmt):
'Opcode::{} => true,', 'Opcode::{} => true,',
i.camel_name, i.name) i.camel_name, i.name)
fmt.line('_ => false') fmt.line('_ => false,')
# Generate a private opcode_format table. # Generate a private opcode_format table.
with fmt.indented( with fmt.indented(
@@ -352,7 +352,7 @@ def gen_typesets_table(fmt, type_sets):
fmt.comment('Table of value type sets.') fmt.comment('Table of value type sets.')
assert len(type_sets.table) <= typeset_limit, "Too many type sets" assert len(type_sets.table) <= typeset_limit, "Too many type sets"
with fmt.indented( with fmt.indented(
'const TYPE_SETS : [ir::instructions::ValueTypeSet; {}] = [' 'const TYPE_SETS: [ir::instructions::ValueTypeSet; {}] = ['
.format(len(type_sets.table)), '];'): .format(len(type_sets.table)), '];'):
for ts in type_sets.table: for ts in type_sets.table:
with fmt.indented('ir::instructions::ValueTypeSet {', '},'): with fmt.indented('ir::instructions::ValueTypeSet {', '},'):
@@ -385,7 +385,7 @@ def gen_type_constraints(fmt, instrs):
fmt.comment('Table of opcode constraints.') fmt.comment('Table of opcode constraints.')
with fmt.indented( with fmt.indented(
'const OPCODE_CONSTRAINTS : [OpcodeConstraints; {}] = [' 'const OPCODE_CONSTRAINTS: [OpcodeConstraints; {}] = ['
.format(len(instrs)), '];'): .format(len(instrs)), '];'):
for i in instrs: for i in instrs:
# Collect constraints for the value results, not including # Collect constraints for the value results, not including
@@ -442,7 +442,7 @@ def gen_type_constraints(fmt, instrs):
fmt.comment('Table of operand constraint sequences.') fmt.comment('Table of operand constraint sequences.')
with fmt.indented( with fmt.indented(
'const OPERAND_CONSTRAINTS : [OperandConstraint; {}] = [' 'const OPERAND_CONSTRAINTS: [OperandConstraint; {}] = ['
.format(len(operand_seqs.table)), '];'): .format(len(operand_seqs.table)), '];'):
for c in operand_seqs.table: for c in operand_seqs.table:
fmt.line('OperandConstraint::{},'.format(c)) fmt.line('OperandConstraint::{},'.format(c))

View File

@@ -62,7 +62,7 @@ def gen_getter(setting, sgrp, fmt):
.format(setting.byte_offset), '}'): .format(setting.byte_offset), '}'):
for i, v in enumerate(setting.values): for i, v in enumerate(setting.values):
fmt.line('{} => {}::{},'.format(i, ty, camel_case(v))) fmt.line('{} => {}::{},'.format(i, ty, camel_case(v)))
fmt.line('_ => panic!("Invalid enum value")') fmt.line('_ => panic!("Invalid enum value"),')
else: else:
raise AssertionError("Unknown setting kind") raise AssertionError("Unknown setting kind")
@@ -197,7 +197,7 @@ def gen_template(sgrp, fmt):
fmt.line('enumerators: &ENUMERATORS,') fmt.line('enumerators: &ENUMERATORS,')
fmt.line('hash_table: &HASH_TABLE,') fmt.line('hash_table: &HASH_TABLE,')
vs = ', '.join('{:#04x}'.format(x) for x in v) vs = ', '.join('{:#04x}'.format(x) for x in v)
fmt.line('defaults: &[ {} ],'.format(vs)) fmt.line('defaults: &[{}],'.format(vs))
fmt.line('presets: &PRESETS,') fmt.line('presets: &PRESETS,')
fmt.doc_comment( fmt.doc_comment(

View File

@@ -131,11 +131,13 @@ Iret = EncRecipe(
emit=''' emit='''
// Return instructions are always a jalr to %x1. // Return instructions are always a jalr to %x1.
// The return address is provided as a special-purpose link argument. // The return address is provided as a special-purpose link argument.
put_i(bits, put_i(
bits,
1, // rs1 = %x1 1, // rs1 = %x1
0, // no offset. 0, // no offset.
0, // rd = %x0: no address written. 0, // rd = %x0: no address written.
sink); sink,
);
''') ''')
# I-type encoding for `jalr` as an indirect call. # I-type encoding for `jalr` as an indirect call.
@@ -143,11 +145,13 @@ Icall = EncRecipe(
'Icall', IndirectCall, size=4, ins=GPR, outs=(), 'Icall', IndirectCall, size=4, ins=GPR, outs=(),
emit=''' emit='''
// Indirect instructions are jalr with rd=%x1. // Indirect instructions are jalr with rd=%x1.
put_i(bits, put_i(
bits,
in_reg0, in_reg0,
0, // no offset. 0, // no offset.
1, // rd = %x1: link register. 1, // rd = %x1: link register.
sink); sink,
);
''') ''')