1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix assembly in generic fns (#10370)

This commit is contained in:
crthpl 2021-06-07 00:53:33 -07:00 committed by GitHub
parent 114c382c46
commit 8a497080b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 3 deletions

View File

@ -4957,7 +4957,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
}
} else if to_type_sym.kind == .interface_ {
c.type_implements(node.expr_type, node.typ, node.pos)
} else if node.typ == ast.bool_type {
} else if node.typ == ast.bool_type && !c.inside_unsafe {
c.error('cannot cast to bool - use e.g. `some_int != 0` instead', node.pos)
} else if node.expr_type == ast.none_type && !node.typ.has_flag(.optional) {
type_name := c.table.type_to_str(node.typ)

View File

@ -1937,7 +1937,8 @@ fn (mut g Gen) gen_asm_stmt(stmt ast.AsmStmt) {
}
g.writeln(' (')
g.indent++
for mut template in stmt.templates {
for template_tmp in stmt.templates {
mut template := template_tmp
g.write('"')
if template.is_directive {
g.write('.')

View File

@ -183,3 +183,25 @@ fn test_flag_output() {
assert out
assert maybe_four == 9
}
fn test_asm_generic() {
u := u64(49)
b := unsafe { bool(123) }
assert generic_asm(u) == 14
assert u == 63
assert u64(generic_asm(b)) == 14
assert u64(b) == 137
}
fn generic_asm<T>(var &T) T {
mut ret := T(14)
unsafe {
asm volatile amd64 {
add var, ret
; +m (var[0]) as var
+r (ret)
; ; memory
}
}
return ret
}

View File

@ -1,5 +1,4 @@
import v.tests.assembly.util
// rename this file to asm_test.amd64.v (and make more for other architectures) once pure v code is enforced
fn test_inline_asm() {
a, mut b := 10, 0
@ -165,3 +164,25 @@ fn test_flag_output() {
assert out
assert maybe_four == 9
}
fn test_asm_generic() {
u := u64(49)
b := unsafe { bool(123) }
assert generic_asm(u) == 14
assert u == 63
assert u64(generic_asm(b)) == 14
assert u64(b) == 137
}
fn generic_asm<T>(var &T) T {
mut ret := T(14)
unsafe {
asm volatile amd64 {
add var, ret
; +m (var[0]) as var
+r (ret)
; ; memory
}
}
return ret
}