vfmt: split up asm code into more granular methods, add test (#10878)

pull/10872/head
Lukas Neubert 2021-07-23 07:58:06 +02:00 committed by GitHub
parent 091ce6cc08
commit 3cc54d8e81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 35 deletions

View File

@ -14,30 +14,9 @@ fn (mut f Fmt) asm_stmt(stmt ast.AsmStmt) {
} }
f.writeln('$stmt.arch {') f.writeln('$stmt.arch {')
f.indent++ f.indent++
for template in stmt.templates {
if template.is_directive { f.asm_templates(stmt.templates)
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)
}
}
if stmt.output.len != 0 || stmt.input.len != 0 || stmt.clobbered.len != 0 { if stmt.output.len != 0 || stmt.input.len != 0 || stmt.clobbered.len != 0 {
f.write('; ') f.write('; ')
} }
@ -51,18 +30,8 @@ fn (mut f Fmt) asm_stmt(stmt ast.AsmStmt) {
if stmt.clobbered.len != 0 { if stmt.clobbered.len != 0 {
f.write('; ') f.write('; ')
} }
for i, clob in stmt.clobbered { f.asm_clobbered(stmt.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)
}
}
f.indent-- f.indent--
f.writeln('}') f.writeln('}')
if f.indent == 0 { if f.indent == 0 {
@ -151,6 +120,46 @@ fn (mut f Fmt) asm_reg(reg ast.AsmRegister) {
f.write(reg.name) 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) { fn (mut f Fmt) asm_ios(ios []ast.AsmIO) {
for i, io in ios { for i, io in ios {
if i != 0 { if i != 0 {

View File

@ -0,0 +1,10 @@
fn main() {
asm volatile amd64 {
mov ecx, 5
loop_start:
add j, 3
loop loop_start
; +r (j)
; ; ecx
}
}