diff --git a/vlib/v/fmt/asm.v b/vlib/v/fmt/asm.v index 6bac5a075d..9de83dd6b7 100644 --- a/vlib/v/fmt/asm.v +++ b/vlib/v/fmt/asm.v @@ -14,30 +14,9 @@ fn (mut f Fmt) asm_stmt(stmt ast.AsmStmt) { } f.writeln('$stmt.arch {') f.indent++ - for template in stmt.templates { - if template.is_directive { - f.write('.') - } - f.write('$template.name') - if template.is_label { - f.write(':') - } else { - if template.args.len > 0 { - f.write(' ') - } - } - for i, arg in template.args { - f.asm_arg(arg) - if i + 1 < template.args.len { - f.write(', ') - } - } - if template.comments.len == 0 { - f.writeln('') - } else { - f.comments(template.comments, inline: false) - } - } + + f.asm_templates(stmt.templates) + if stmt.output.len != 0 || stmt.input.len != 0 || stmt.clobbered.len != 0 { f.write('; ') } @@ -51,18 +30,8 @@ fn (mut f Fmt) asm_stmt(stmt ast.AsmStmt) { if stmt.clobbered.len != 0 { f.write('; ') } - for i, clob in stmt.clobbered { - if i != 0 { - f.write(' ') - } - f.write(clob.reg.name) + f.asm_clobbered(stmt.clobbered) - if clob.comments.len == 0 { - f.writeln('') - } else { - f.comments(clob.comments, inline: false) - } - } f.indent-- f.writeln('}') if f.indent == 0 { @@ -151,6 +120,46 @@ fn (mut f Fmt) asm_reg(reg ast.AsmRegister) { f.write(reg.name) } +fn (mut f Fmt) asm_templates(templates []ast.AsmTemplate) { + for template in templates { + if template.is_directive { + f.write('.') + } + f.write('$template.name') + if template.is_label { + f.write(':') + } else if template.args.len > 0 { + f.write(' ') + } + for i, arg in template.args { + f.asm_arg(arg) + if i + 1 < template.args.len { + f.write(', ') + } + } + if template.comments.len == 0 { + f.writeln('') + } else { + f.comments(template.comments, inline: false) + } + } +} + +fn (mut f Fmt) asm_clobbered(clobbered []ast.AsmClobbered) { + for i, clob in clobbered { + if i != 0 { + f.write(' ') + } + f.write(clob.reg.name) + + if clob.comments.len == 0 { + f.writeln('') + } else { + f.comments(clob.comments, inline: false) + } + } +} + fn (mut f Fmt) asm_ios(ios []ast.AsmIO) { for i, io in ios { if i != 0 { diff --git a/vlib/v/fmt/tests/assembly/asm_keep.vv b/vlib/v/fmt/tests/assembly/asm_keep.vv new file mode 100644 index 0000000000..a6f9e56971 --- /dev/null +++ b/vlib/v/fmt/tests/assembly/asm_keep.vv @@ -0,0 +1,10 @@ +fn main() { + asm volatile amd64 { + mov ecx, 5 + loop_start: + add j, 3 + loop loop_start + ; +r (j) + ; ; ecx + } +}