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

cgen: initialize globals directly for simple cases (#11002)

This commit is contained in:
Uwe Krüger 2021-07-30 18:20:45 +02:00 committed by GitHub
parent f67922782e
commit a7c2aaf35b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 3 deletions

View File

@ -1989,3 +1989,32 @@ fn gen_all_registers(mut t Table, without_numbers []string, with_numbers map[str
} }
return res return res
} }
// is `expr` a literal, i.e. it does not depend on any other declarations (C compile time constant)
pub fn (expr Expr) is_literal() bool {
match expr {
BoolLiteral, CharLiteral, FloatLiteral, IntegerLiteral {
return true
}
PrefixExpr {
return expr.right.is_literal()
}
InfixExpr {
return expr.left.is_literal() && expr.right.is_literal()
}
ParExpr {
return expr.expr.is_literal()
}
CastExpr {
return !expr.has_arg && expr.expr.is_literal()
&& (expr.typ.is_ptr() || expr.typ.is_pointer()
|| expr.typ in [i8_type, i16_type, int_type, i64_type, byte_type, u8_type, u16_type, u32_type, u64_type, f32_type, f64_type, char_type, bool_type, rune_type])
}
SizeOf, IsRefType {
return expr.is_type || expr.expr.is_literal()
}
else {
return false
}
}
}

View File

@ -5156,8 +5156,13 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
} }
styp := g.typ(field.typ) styp := g.typ(field.typ)
if field.has_expr { if field.has_expr {
g.definitions.writeln('$mod$styp $field.name;') g.definitions.write_string('$mod$styp $field.name')
if field.expr.is_literal() {
g.definitions.writeln(' = ${g.expr_string(field.expr)}; // global')
} else {
g.definitions.writeln(';')
g.global_inits[key].writeln('\t$field.name = ${g.expr_string(field.expr)}; // global') g.global_inits[key].writeln('\t$field.name = ${g.expr_string(field.expr)}; // global')
}
} else { } else {
default_initializer := g.type_default(field.typ) default_initializer := g.type_default(field.typ)
if default_initializer == '{0}' { if default_initializer == '{0}' {

View File

@ -36,7 +36,7 @@ __global (
sem sync.Semaphore sem sync.Semaphore
shmap shared map[string]f64 shmap shared map[string]f64
mtx sync.RwMutex mtx sync.RwMutex
f1 = f64(34.0625) f1 = f64(545 / (sizeof(f64) + f32(8))) // directly initialized
f2 f64 f2 f64
) )
@ -116,6 +116,7 @@ fn switch2() u64 {
} }
fn test_global_mutex() { fn test_global_mutex() {
assert f1 == 34.0625
t := go switch2() t := go switch2()
for _ in 0 .. 2500000 { for _ in 0 .. 2500000 {
mtx.@lock() mtx.@lock()