Skip to content

Commit 60ac6ce

Browse files
committed
cgen: linux tcc fix
1 parent d96fe54 commit 60ac6ce

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

vlib/v/builder/builder_test.v

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fn test_run_with_obscure_source_filenames() {
287287
os.rmdir_all(obscure_dir) or {}
288288
os.mkdir_all(obscure_dir)!
289289
source := "println('hi')\n"
290-
for file_name in [
290+
for idx, file_name in [
291291
"quote's.v",
292292
'"hi".v',
293293
"'.v",
@@ -301,11 +301,14 @@ fn test_run_with_obscure_source_filenames() {
301301
] {
302302
src_file := os.join_path(obscure_dir, file_name)
303303
os.write_file(src_file, source)!
304+
out_file := os.join_path(obscure_dir, 'obscure_output_${idx}')
304305
display_name := file_name.replace('\n', '\\n')
305-
res := os.execute('${os.quoted_path(vexe)} run ${os.quoted_path(src_file)}')
306+
res :=
307+
os.execute('${os.quoted_path(vexe)} -o ${os.quoted_path(out_file)} run ${os.quoted_path(src_file)}')
306308
assert res.exit_code == 0, '${display_name}: ${res.output}'
307309
assert res.output.trim_space() == 'hi', '${display_name}: ${res.output}'
308310
assert os.read_file(src_file)! == source
311+
os.rm(out_file) or {}
309312
}
310313
}
311314

vlib/v/gen/c/cgen.v

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5964,11 +5964,31 @@ fn (mut g Gen) expr(node_ ast.Expr) {
59645964
}
59655965
is_safe_inc := g.do_int_overflow_checks && node.op == .inc
59665966
is_safe_dec := g.do_int_overflow_checks && node.op == .dec
5967+
is_atomic_postfix := node.typ.has_flag(.atomic_f) && node.op in [.inc, .dec]
5968+
&& g.pref.ccompiler_type != .msvc
59675969
g.inside_map_postfix = true
5968-
if node.is_c2v_prefix {
5970+
if is_atomic_postfix {
5971+
atomic_op := if node.op == .inc {
5972+
'__atomic_fetch_add'
5973+
} else {
5974+
'__atomic_fetch_sub'
5975+
}
5976+
atomic_value_styp := g.styp(node.typ.clear_flag(.atomic_f))
5977+
g.write('${atomic_op}((${atomic_value_styp}*)&(')
5978+
if node.expr.is_auto_deref_var() {
5979+
g.write('*')
5980+
}
5981+
g.expr(node.expr)
5982+
if node.typ.has_flag(.shared_f) {
5983+
g.write('->val')
5984+
}
5985+
g.write('), 1, 5)')
5986+
} else if node.is_c2v_prefix {
59695987
g.write(node.op.str())
59705988
}
5971-
if node.expr.is_auto_deref_var() {
5989+
if is_atomic_postfix {
5990+
// already emitted above
5991+
} else if node.expr.is_auto_deref_var() {
59725992
g.write('(*')
59735993
g.expr(node.expr)
59745994
g.write(')')
@@ -6023,7 +6043,9 @@ fn (mut g Gen) expr(node_ ast.Expr) {
60236043
}
60246044
}
60256045
g.inside_map_postfix = false
6026-
if !node.is_c2v_prefix && node.op != .question && !is_safe_inc && !is_safe_dec {
6046+
if is_atomic_postfix {
6047+
// already emitted above
6048+
} else if !node.is_c2v_prefix && node.op != .question && !is_safe_inc && !is_safe_dec {
60276049
g.write(node.op.str())
60286050
} else if is_safe_inc || is_safe_dec {
60296051
overflow_styp := g.styp(get_overflow_fn_type(node.typ))
@@ -12898,7 +12920,7 @@ return ${cast_shared_struct_str};
1289812920
}
1289912921
iin_idx := already_generated_mwrappers[interface_index_name] - iinidx_minimum_base + 1
1290012922
if g.pref.build_mode != .build_module {
12901-
sb.writeln('${g.static_modifier}const u32 ${interface_index_name} = ${iin_idx};')
12923+
sb.writeln('enum { ${interface_index_name} = ${iin_idx} };')
1290212924
} else {
1290312925
sb.writeln('extern const u32 ${interface_index_name};')
1290412926
}
@@ -13005,9 +13027,13 @@ fn (mut g Gen) panic_debug_info(pos token.Pos) (int, string, string, string) {
1300513027
return paline, pafile, pamod, pafn
1300613028
}
1300713029

13030+
// get_guarded_include_text returns C preprocessor code that includes `iname`
13031+
// when available, or emits `imessage` through a C error otherwise.
1300813032
pub fn get_guarded_include_text(iname string, imessage string) string {
1300913033
res := '
13010-
|#if defined(__has_include)
13034+
|#if defined(__TINYC__)
13035+
|#include ${iname}
13036+
|#elif defined(__has_include)
1301113037
|#if __has_include(${iname})
1301213038
|#include ${iname}
1301313039
|#else
@@ -13020,9 +13046,13 @@ pub fn get_guarded_include_text(iname string, imessage string) string {
1302013046
return res
1302113047
}
1302213048

13049+
// get_inttypes_or_stdint_include_text returns C preprocessor code that includes
13050+
// the best available integer types header, or emits `imessage` otherwise.
1302313051
pub fn get_inttypes_or_stdint_include_text(imessage string) string {
1302413052
res := '
13025-
|#if defined(__has_include)
13053+
|#if defined(__TINYC__)
13054+
|#include <stdint.h>
13055+
|#elif defined(__has_include)
1302613056
|#if __has_include(<inttypes.h>)
1302713057
|#include <inttypes.h>
1302813058
|#elif __has_include(<stdint.h>)

0 commit comments

Comments
 (0)