Skip to content

Commit 959c11b

Browse files
authored
native: add a temporary special case for C.EOF (#24724)
1 parent 099b57f commit 959c11b

File tree

7 files changed

+100
-5
lines changed

7 files changed

+100
-5
lines changed

vlib/v/gen/native/amd64.v

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ fn (mut c Amd64) cmp_var_reg(var Var, reg Register, config VarConfig) {
314314
ExternVar {
315315
c.cmp_var_reg(var_object as ExternVar, reg, config)
316316
}
317+
PreprocVar {
318+
c.cmp_var_reg(var_object as PreprocVar, reg, config)
319+
}
317320
}
318321
}
319322
LocalVar {
@@ -336,6 +339,9 @@ fn (mut c Amd64) cmp_var_reg(var Var, reg Register, config VarConfig) {
336339
ExternVar {
337340
c.g.n_error('${@LOCATION} unsupported var type ${var}')
338341
}
342+
PreprocVar {
343+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
344+
}
339345
}
340346
}
341347

@@ -357,6 +363,9 @@ fn (mut c Amd64) cmp_var(var Var, val i32, config VarConfig) {
357363
ExternVar {
358364
c.cmp_var(var_object as ExternVar, val, config)
359365
}
366+
PreprocVar {
367+
c.cmp_var(var_object as PreprocVar, val, config)
368+
}
360369
}
361370
}
362371
LocalVar {
@@ -379,6 +388,9 @@ fn (mut c Amd64) cmp_var(var Var, val i32, config VarConfig) {
379388
ExternVar {
380389
c.g.n_error('${@LOCATION} unsupported var type ${var}')
381390
}
391+
PreprocVar {
392+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
393+
}
382394
}
383395
}
384396

@@ -401,6 +413,9 @@ fn (mut c Amd64) dec_var(var Var, config VarConfig) {
401413
ExternVar {
402414
c.dec_var(var_object as ExternVar, config)
403415
}
416+
PreprocVar {
417+
c.dec_var(var_object as PreprocVar, config)
418+
}
404419
}
405420
}
406421
LocalVar {
@@ -423,6 +438,9 @@ fn (mut c Amd64) dec_var(var Var, config VarConfig) {
423438
ExternVar {
424439
c.g.n_error('${@LOCATION} unsupported var type ${var}')
425440
}
441+
PreprocVar {
442+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
443+
}
426444
}
427445
}
428446

@@ -446,6 +464,9 @@ fn (mut c Amd64) inc_var(var Var, config VarConfig) {
446464
ExternVar {
447465
c.inc_var(var_object as ExternVar, config)
448466
}
467+
PreprocVar {
468+
c.inc_var(var_object as PreprocVar, config)
469+
}
449470
}
450471
}
451472
LocalVar {
@@ -492,6 +513,9 @@ fn (mut c Amd64) inc_var(var Var, config VarConfig) {
492513
ExternVar {
493514
c.g.n_error('${@LOCATION} unsupported var type ${var}')
494515
}
516+
PreprocVar {
517+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
518+
}
495519
}
496520
}
497521

@@ -689,6 +713,9 @@ fn (mut c Amd64) mov_reg_to_var(var Var, r Register, config VarConfig) {
689713
ExternVar {
690714
c.mov_reg_to_var(var_object as ExternVar, reg, config)
691715
}
716+
PreprocVar {
717+
c.mov_reg_to_var(var_object as PreprocVar, reg, config)
718+
}
692719
}
693720
}
694721
LocalVar {
@@ -773,6 +800,9 @@ fn (mut c Amd64) mov_reg_to_var(var Var, r Register, config VarConfig) {
773800
ExternVar {
774801
c.g.n_error('${@LOCATION} unsupported var type ${var}')
775802
}
803+
PreprocVar {
804+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
805+
}
776806
}
777807
}
778808

@@ -793,6 +823,9 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) {
793823
ExternVar {
794824
c.mov_int_to_var(var_object as ExternVar, integer, config)
795825
}
826+
PreprocVar {
827+
c.mov_int_to_var(var_object as PreprocVar, integer, config)
828+
}
796829
}
797830
}
798831
LocalVar {
@@ -858,6 +891,9 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) {
858891
ExternVar {
859892
c.g.n_error('${@LOCATION} unsupported var type ${var}')
860893
}
894+
PreprocVar {
895+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
896+
}
861897
}
862898
}
863899

@@ -907,6 +943,9 @@ fn (mut c Amd64) mov_var_to_reg(reg Register, var Var, config VarConfig) {
907943
ExternVar {
908944
c.mov_var_to_reg(reg, var_object as ExternVar, config)
909945
}
946+
PreprocVar {
947+
c.mov_var_to_reg(reg, var_object as PreprocVar, config)
948+
}
910949
}
911950
}
912951
LocalVar {
@@ -975,6 +1014,9 @@ fn (mut c Amd64) mov_var_to_reg(reg Register, var Var, config VarConfig) {
9751014
ExternVar {
9761015
c.g.n_error('${@LOCATION} unsupported var type ${var}')
9771016
}
1017+
PreprocVar {
1018+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
1019+
}
9781020
}
9791021
}
9801022

@@ -2147,6 +2189,9 @@ fn (mut c Amd64) assign_var(var IdentVar, raw_type ast.Type) {
21472189
ExternVar {
21482190
c.mov_reg_to_var(var as ExternVar, Amd64Register.rax)
21492191
}
2192+
PreprocVar {
2193+
c.mov_reg_to_var(var as PreprocVar, Amd64Register.rax)
2194+
}
21502195
}
21512196
} else {
21522197
c.g.n_error('${@LOCATION} error assigning type ${typ} with size ${size}: ${info}')
@@ -3771,6 +3816,9 @@ fn (mut c Amd64) init_struct(var Var, init ast.StructInit) {
37713816
ExternVar {
37723817
c.init_struct(var_object as ExternVar, init)
37733818
}
3819+
PreprocVar {
3820+
c.init_struct(var_object as PreprocVar, init)
3821+
}
37743822
}
37753823
}
37763824
LocalVar {
@@ -3816,6 +3864,9 @@ fn (mut c Amd64) init_struct(var Var, init ast.StructInit) {
38163864
ExternVar {
38173865
c.g.n_error('${@LOCATION} unsupported var type ${var}')
38183866
}
3867+
PreprocVar {
3868+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
3869+
}
38193870
}
38203871
}
38213872

@@ -3867,6 +3918,9 @@ fn (mut c Amd64) init_array(var Var, node ast.ArrayInit) {
38673918
ExternVar {
38683919
c.init_array(var_object as ExternVar, node)
38693920
}
3921+
PreprocVar {
3922+
c.init_array(var_object as PreprocVar, node)
3923+
}
38703924
}
38713925
}
38723926
LocalVar {
@@ -3883,6 +3937,9 @@ fn (mut c Amd64) init_array(var Var, node ast.ArrayInit) {
38833937
ExternVar {
38843938
c.g.n_error('${@LOCATION} unsupported var type ${var}')
38853939
}
3940+
PreprocVar {
3941+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
3942+
}
38863943
}
38873944
}
38883945

@@ -4182,6 +4239,9 @@ fn (mut c Amd64) mov_ssereg_to_var(var Var, reg Amd64SSERegister, config VarConf
41824239
ExternVar {
41834240
c.mov_ssereg_to_var(var_object as ExternVar, reg, config)
41844241
}
4242+
PreprocVar {
4243+
c.mov_ssereg_to_var(var_object as PreprocVar, reg, config)
4244+
}
41854245
}
41864246
}
41874247
LocalVar {
@@ -4210,6 +4270,9 @@ fn (mut c Amd64) mov_ssereg_to_var(var Var, reg Amd64SSERegister, config VarConf
42104270
ExternVar {
42114271
c.g.n_error('${@LOCATION} unsupported var type ${var}')
42124272
}
4273+
PreprocVar {
4274+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
4275+
}
42134276
}
42144277
}
42154278

@@ -4232,6 +4295,9 @@ fn (mut c Amd64) mov_var_to_ssereg(reg Amd64SSERegister, var Var, config VarConf
42324295
ExternVar {
42334296
c.mov_var_to_ssereg(reg, var_object as ExternVar, config)
42344297
}
4298+
PreprocVar {
4299+
c.mov_var_to_ssereg(reg, var_object as PreprocVar, config)
4300+
}
42354301
}
42364302
}
42374303
LocalVar {
@@ -4260,6 +4326,9 @@ fn (mut c Amd64) mov_var_to_ssereg(reg Amd64SSERegister, var Var, config VarConf
42604326
ExternVar {
42614327
c.g.n_error('${@LOCATION} unsupported var type ${var}')
42624328
}
4329+
PreprocVar {
4330+
c.g.n_error('${@LOCATION} unsupported var type ${var}')
4331+
}
42634332
}
42644333
}
42654334

vlib/v/gen/native/blacklist.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const blacklist = {
3737
'int_min': false
3838
'flush_stdout': false
3939
'flush_stderr': false
40-
'print_character': true
40+
'print_character': false
4141
'u8.is_alnum': false
4242
'u8.is_bin_digit': false
4343
'u8.is_capital': false

vlib/v/gen/native/expr.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ fn (mut g Gen) expr(node ast.Expr) {
5151
ExternVar {
5252
g.extern_var_ident(var)
5353
}
54+
PreprocVar {
55+
g.preproc_var_ident(var)
56+
}
5457
else {
5558
g.n_error('${@LOCATION} Unsupported variable kind')
5659
}
@@ -203,6 +206,11 @@ fn (mut g Gen) extern_var_ident(var ExternVar) {
203206
}
204207
}
205208

209+
fn (mut g Gen) preproc_var_ident(var PreprocVar) {
210+
main_reg := g.code_gen.main_reg()
211+
g.code_gen.mov64(main_reg, var.val)
212+
}
213+
206214
fn (mut g Gen) condition(expr ast.Expr, neg bool) i32 {
207215
g.println('; condition cjmp if ${neg}:')
208216
g.expr(expr)

vlib/v/gen/native/gen.v

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import v.eval
1515
import term
1616
import strconv
1717

18+
const c_preprocessed = {
19+
'C.EOF': -1
20+
}
21+
1822
@[heap; minify]
1923
pub struct Gen {
2024
out_name string
@@ -243,6 +247,12 @@ struct ExternVar {
243247
name string
244248
}
245249

250+
struct PreprocVar {
251+
typ ast.Type
252+
name string
253+
val i64
254+
}
255+
246256
struct GlobalVar {}
247257

248258
@[params]
@@ -252,9 +262,9 @@ pub:
252262
typ ast.Type // type of the value you want to process e.g. struct fields.
253263
}
254264

255-
type Var = GlobalVar | ExternVar | LocalVar | ast.Ident
265+
type Var = GlobalVar | ExternVar | LocalVar | PreprocVar | ast.Ident
256266

257-
type IdentVar = GlobalVar | ExternVar | LocalVar | Register
267+
type IdentVar = GlobalVar | ExternVar | LocalVar | Register | PreprocVar
258268

259269
enum JumpOp {
260270
je
@@ -281,6 +291,14 @@ fn (mut g Gen) get_var_from_ident(ident ast.Ident) IdentVar {
281291
if ident.name in g.extern_symbols {
282292
return ExternVar{ident.info.typ, ident.name}
283293
}
294+
mut is_preprocessed := true
295+
mut preprocessed_val := c_preprocessed[ident.name] or {
296+
is_preprocessed = false
297+
0
298+
}
299+
if is_preprocessed {
300+
return PreprocVar{ident.info.typ, ident.name, preprocessed_val}
301+
}
284302
mut obj := ident.obj
285303
if obj !in [ast.Var, ast.ConstField, ast.GlobalField, ast.AsmRegister] {
286304
obj = ident.scope.find(ident.name) or {

vlib/v/gen/native/tests/builtin.vv

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ println(int_max(-32, 32))
135135
println(int_min(32, -32))
136136
println(int_min(-32, 32))
137137

138-
//print_character(`a`) enable when C vars will work
139-
140138
// assert u8(`a`).ascii_str() == 'a' when ptr index will work
141139
// assert u8(`b`).ascii_str() != 'a'
142140
// assert u8(0x4F).hex() == '4F' when fixed array index will work

vlib/v/gen/native/tests/linux.vv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ fn main() {
2525
test_c_extern_vars()
2626
flush_stderr()
2727
flush_stdout()
28+
print_character(`a`)
2829
}

vlib/v/gen/native/tests/linux.vv.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
2
66
99
77
abc
8+
a

0 commit comments

Comments
 (0)