Skip to content

Commit b44f815

Browse files
committed
v.gen.c: fix codegen for const a = u64(5) + 5
1 parent 063ffe3 commit b44f815

File tree

3 files changed

+95
-27
lines changed

3 files changed

+95
-27
lines changed

vlib/builtin/int.v

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ const (
3131

3232
// This implementation is the quickest with gcc -O2
3333
// str_l returns the string representation of the integer nn with max chars.
34-
[inline] [direct_array_access]
34+
[direct_array_access; inline]
3535
fn (nn int) str_l(max int) string {
3636
unsafe {
3737
mut n := i64(nn)
3838
mut d := 0
3939
if n == 0 {
4040
return '0'
4141
}
42-
42+
4343
mut is_neg := false
4444
if n < 0 {
4545
n = -n
@@ -84,7 +84,7 @@ fn (nn int) str_l(max int) string {
8484
*/
8585
return tos(buf, diff)
8686

87-
//return tos(memdup(&buf[0] + index, (max - index)), (max - index))
87+
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
8888
}
8989
}
9090

@@ -114,7 +114,7 @@ pub fn (n int) str() string {
114114

115115
// str returns the value of the `u32` as a `string`.
116116
// Example: assert u32(20000).str() == '20000'
117-
[inline] [direct_array_access]
117+
[direct_array_access; inline]
118118
pub fn (nn u32) str() string {
119119
unsafe {
120120
mut n := nn
@@ -146,7 +146,7 @@ pub fn (nn u32) str() string {
146146
C.memmove(buf, buf + index, diff + 1)
147147
return tos(buf, diff)
148148

149-
//return tos(memdup(&buf[0] + index, (max - index)), (max - index))
149+
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
150150
}
151151
}
152152

@@ -158,7 +158,7 @@ pub fn (n int_literal) str() string {
158158

159159
// str returns the value of the `i64` as a `string`.
160160
// Example: assert i64(-200000).str() == '-200000'
161-
[inline] [direct_array_access]
161+
[direct_array_access; inline]
162162
pub fn (nn i64) str() string {
163163
unsafe {
164164
mut n := nn
@@ -199,13 +199,13 @@ pub fn (nn i64) str() string {
199199
diff := max - index
200200
C.memmove(buf, buf + index, diff + 1)
201201
return tos(buf, diff)
202-
//return tos(memdup(&buf[0] + index, (max - index)), (max - index))
202+
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
203203
}
204204
}
205205

206206
// str returns the value of the `u64` as a `string`.
207207
// Example: assert u64(2000000).str() == '2000000'
208-
[inline] [direct_array_access]
208+
[direct_array_access; inline]
209209
pub fn (nn u64) str() string {
210210
unsafe {
211211
mut n := nn
@@ -236,7 +236,7 @@ pub fn (nn u64) str() string {
236236
diff := max - index
237237
C.memmove(buf, buf + index, diff + 1)
238238
return tos(buf, diff)
239-
//return tos(memdup(&buf[0] + index, (max - index)), (max - index))
239+
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
240240
}
241241
}
242242

@@ -254,7 +254,7 @@ pub fn (b bool) str() string {
254254
//
255255

256256
// u64_to_hex converts the number `nn` to a (zero padded if necessary) hexadecimal `string`.
257-
[inline] [direct_array_access]
257+
[direct_array_access; inline]
258258
fn u64_to_hex(nn u64, len byte) string {
259259
mut n := nn
260260
mut buf := [256]byte{}
@@ -270,7 +270,7 @@ fn u64_to_hex(nn u64, len byte) string {
270270
}
271271

272272
// u64_to_hex_no_leading_zeros converts the number `nn` to hexadecimal `string`.
273-
[inline] [direct_array_access]
273+
[direct_array_access; inline]
274274
fn u64_to_hex_no_leading_zeros(nn u64, len byte) string {
275275
mut n := nn
276276
mut buf := [256]byte{}
@@ -391,32 +391,54 @@ pub fn (nn voidptr) str() string {
391391

392392
// hex returns the value of the `byteptr` as a hexadecimal `string`.
393393
// Note that the output is ***not*** zero padded.
394-
//pub fn (nn byteptr) str() string {
394+
// pub fn (nn byteptr) str() string {
395395
pub fn (nn byteptr) str() string {
396396
return u64(nn).hex()
397397
}
398398

399-
/*
400-
pub fn (nn byte) hex_full() string { return u64_to_hex(nn, 2) }
401-
pub fn (nn i8) hex_full() string { return u64_to_hex(byte(nn), 2) }
402-
pub fn (nn u16) hex_full() string { return u64_to_hex(nn, 4) }
403-
pub fn (nn i16) hex_full() string { return u64_to_hex(u16(nn), 4) }
404-
pub fn (nn u32) hex_full() string { return u64_to_hex(nn, 8) }
405-
pub fn (nn int) hex_full() string { return u64_to_hex(u32(nn), 8) }
406-
*/
399+
pub fn (nn byte) hex_full() string {
400+
return u64_to_hex(u64(nn), 2)
401+
}
402+
403+
pub fn (nn i8) hex_full() string {
404+
return u64_to_hex(u64(nn), 2)
405+
}
406+
407+
pub fn (nn u16) hex_full() string {
408+
return u64_to_hex(u64(nn), 4)
409+
}
410+
411+
pub fn (nn i16) hex_full() string {
412+
return u64_to_hex(u64(nn), 4)
413+
}
414+
415+
pub fn (nn u32) hex_full() string {
416+
return u64_to_hex(u64(nn), 8)
417+
}
418+
419+
pub fn (nn int) hex_full() string {
420+
return u64_to_hex(u64(nn), 8)
421+
}
422+
423+
pub fn (nn i64) hex_full() string {
424+
return u64_to_hex(u64(nn), 16)
425+
}
426+
427+
pub fn (nn voidptr) hex_full() string {
428+
return u64_to_hex(u64(nn), 16)
429+
}
430+
431+
pub fn (nn int_literal) hex_full() string {
432+
return u64_to_hex(u64(nn), 16)
433+
}
434+
407435
// hex_full returns the value of the `u64` as a *full* 16-digit hexadecimal `string`.
408436
// Example: assert u64(2).hex_full() == '0000000000000002'
409437
// Example: assert u64(255).hex_full() == '00000000000000ff'
410438
pub fn (nn u64) hex_full() string {
411439
return u64_to_hex(nn, 16)
412440
}
413441

414-
/*
415-
pub fn (nn i64) hex_full() string { return u64_to_hex(u64(nn), 16) }
416-
pub fn (nn int_literal) hex_full() string { return u64_to_hex(nn, 16) }
417-
pub fn (nn voidptr) hex_full() string { return u64_to_hex(nn, 16) }
418-
pub fn (nn byteptr) hex_full() string { return u64_to_hex(nn, 16) }
419-
*/
420442
// str returns the contents of `byte` as a zero terminated `string`.
421443
// Example: assert byte(111).str() == '111'
422444
pub fn (b byte) str() string {

vlib/v/gen/c/cgen.v

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5000,6 +5000,9 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
50005000
fn (mut g Gen) const_decl_precomputed(mod string, name string, ct_value ast.ComptTimeConstValue, typ ast.Type) bool {
50015001
mut styp := g.typ(typ)
50025002
cname := '_const_$name'
5003+
$if trace_const_precomputed ? {
5004+
eprintln('> styp: $styp | cname: $cname | ct_value: $ct_value | $ct_value.type_name()')
5005+
}
50035006
match ct_value {
50045007
byte {
50055008
g.const_decl_write_precomputed(styp, cname, ct_value.str())
@@ -5026,7 +5029,11 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, ct_value ast.Comp
50265029
g.const_decl_simple_define(name, ct_value.str())
50275030
return true
50285031
}
5029-
g.const_decl_write_precomputed(styp, cname, ct_value.str())
5032+
if typ == ast.u64_type {
5033+
g.const_decl_write_precomputed(styp, cname, ct_value.str() + 'U')
5034+
} else {
5035+
g.const_decl_write_precomputed(styp, cname, ct_value.str())
5036+
}
50305037
}
50315038
u64 {
50325039
g.const_decl_write_precomputed(styp, cname, ct_value.str() + 'U')
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const zzz_byte_a = byte(`A`)
2+
3+
const zzz_u16_a = u16(999) + 5
4+
5+
const zzza = u64(123)
6+
7+
const zzzb = 5 + zzzc
8+
9+
const zzzc = 6 + zzza
10+
11+
const zzzx = zzza - 124
12+
13+
const zzz_zz = i64(-1)
14+
15+
struct Abc {
16+
x int
17+
}
18+
19+
const zzz_struct = Abc{123}
20+
21+
const zzzs = 'xyz' + 'abc'
22+
23+
fn test_number_consts() {
24+
assert zzz_byte_a.hex_full() == '41'
25+
assert zzz_u16_a.hex_full() == '03ec'
26+
assert zzza.hex_full() == '000000000000007b'
27+
assert zzzb.hex_full() == '0000000000000086'
28+
assert zzzc.hex_full() == '0000000000000081'
29+
// assert zzzx.hex_full() == '00000000ffffffff' // TODO: see why
30+
assert zzz_zz.hex_full() == 'ffffffffffffffff'
31+
}
32+
33+
fn test_struct_consts() {
34+
assert zzz_struct.str().contains('x: 123')
35+
}
36+
37+
fn test_string_consts() {
38+
assert zzzs == 'xyzabc'
39+
}

0 commit comments

Comments
 (0)