Skip to content

Commit cf00d08

Browse files
macos: add debug for bus error
1 parent f2fdbfc commit cf00d08

4 files changed

Lines changed: 49 additions & 12 deletions

File tree

R/tinycc.R

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#' Get the address of an external pointer
2+
#' @param ptr External pointer
3+
#' @return Address as numeric
4+
#' @export
5+
get_external_ptr_addr <- function(ptr) {
6+
.Call("RC_get_external_ptr_addr", ptr)
7+
}
18
#' TinyCC paths
29
#'
310
#' Helpers to locate the bundled tinycc installation after the package is installed.
@@ -33,7 +40,9 @@ tcc_cli <- function() {
3340
exe <- if (.Platform$OS.type == "windows") "tcc.exe" else "tcc"
3441
candidates <- c(file.path(tcc_bin_path(), exe), file.path(tcc_prefix(), exe))
3542
existing <- candidates[file.exists(candidates)]
36-
if (length(existing)) return(existing[[1L]])
43+
if (length(existing)) {
44+
return(existing[[1L]])
45+
}
3746
candidates[[1L]]
3847
}
3948

@@ -58,11 +67,12 @@ tcc_include_paths <- function() {
5867
tcc_output_type <- function(output) {
5968
output <- match.arg(output, c("memory", "obj", "dll", "exe", "preprocess"))
6069
switch(output,
61-
memory = 1L, # TCC_OUTPUT_MEMORY
62-
obj = 3L, # TCC_OUTPUT_OBJ
63-
dll = 4L, # TCC_OUTPUT_DLL
64-
exe = 2L, # TCC_OUTPUT_EXE
65-
preprocess = 5L) # TCC_OUTPUT_PREPROCESS
70+
memory = 1L, # TCC_OUTPUT_MEMORY
71+
obj = 3L, # TCC_OUTPUT_OBJ
72+
dll = 4L, # TCC_OUTPUT_DLL
73+
exe = 2L, # TCC_OUTPUT_EXE
74+
preprocess = 5L
75+
) # TCC_OUTPUT_PREPROCESS
6676
}
6777

6878
check_cli_exists <- function() {
@@ -85,9 +95,11 @@ check_cli_exists <- function() {
8595
tcc_state <- function(output = c("memory", "obj", "dll", "exe", "preprocess"),
8696
include_path = tcc_include_paths(),
8797
lib_path = tcc_lib_paths()) {
88-
.Call(RC_libtcc_state_new, normalizePath(lib_path, winslash = "/", mustWork = FALSE),
89-
normalizePath(include_path, winslash = "/", mustWork = FALSE),
90-
tcc_output_type(output))
98+
.Call(
99+
RC_libtcc_state_new, normalizePath(lib_path, winslash = "/", mustWork = FALSE),
100+
normalizePath(include_path, winslash = "/", mustWork = FALSE),
101+
tcc_output_type(output)
102+
)
91103
}
92104

93105
#' Add a source file to a libtcc state

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ We provide a simple R interface to the
1212
[tinycc](https://github.com/TinyCC/tinycc) compiler including the cli
1313
and the libtcc library. This is mainly a vehicule for the tinycc
1414
compiler and libtcc library. Right now only basic functionalities are
15-
implemented and some warnings remain since libtcc uses printf and other
16-
symbols R CMD check does not like.
15+
implemented and we do not support windows.
1716

1817
## Installation
1918

@@ -54,7 +53,7 @@ tcc_relocate(state)
5453
tcc_call_symbol(state, "forty_two", return = "int")
5554
#> [1] 42
5655
tcc_get_symbol(state, "forty_two")
57-
#> <pointer: 0x6176ed22e000>
56+
#> <pointer: 0x5c9fb6cc3000>
5857
#> attr(,"class")
5958
#> [1] "tcc_symbol"
6059
```

inst/tinytest/test_libtcc.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ sym_ptr <- tcc_get_symbol(state, "forty_two")
2020
expect_true(inherits(sym_ptr, "tcc_symbol"))
2121
expect_true(tcc_symbol_is_valid(sym_ptr))
2222

23+
# Debug: print symbol pointer address and alignment (always enabled)
24+
addr <- get_external_ptr_addr(sym_ptr)
25+
cat(sprintf("[RTINYCC_DEBUG] symbol 'forty_two' address: 0x%x\n", addr))
26+
cat(sprintf("[RTINYCC_DEBUG] address %% 8: %d\n", addr %% 8))
27+
2328
# CLI compile to object
2429
src <- system.file("c_examples", "forty_two.c", package = "Rtinycc")
2530
expect_true(

src/RC_libtcc.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ SEXP RC_libtcc_call_symbol(SEXP ext, SEXP name, SEXP ret_type) {
127127
Rf_error("symbol '%s' not found", sym);
128128
}
129129
uintptr_t addr = (uintptr_t) fn;
130+
/* Debug: print address and alignment if RTINYCC_DEBUG is set */
131+
SEXP debug_env = PROTECT(Rf_mkString("RTINYCC_DEBUG"));
132+
SEXP debug_val = PROTECT(Rf_GetOption(debug_env, R_GlobalEnv));
133+
int debug_enabled = 0;
134+
if (!Rf_isNull(debug_val)) {
135+
debug_enabled = Rf_asLogical(debug_val);
136+
} else {
137+
const char *env = getenv("RTINYCC_DEBUG");
138+
if (env && env[0] == '1') debug_enabled = 1;
139+
}
140+
if (debug_enabled) {
141+
Rprintf("[RTINYCC_DEBUG][C] symbol '%s' address: 0x%lx\n", sym, (unsigned long)addr);
142+
Rprintf("[RTINYCC_DEBUG][C] address %% 8: %ld\n", (long)(addr % 8));
143+
}
144+
UNPROTECT(2);
130145
if (strcmp(rtype, "int") == 0) {
131146
int (*callable)(void) = (int (*)(void)) addr;
132147
return Rf_ScalarInteger(callable());
@@ -148,3 +163,9 @@ SEXP RC_libtcc_ptr_valid(SEXP ptr) {
148163
void *p = R_ExternalPtrAddr(ptr);
149164
return Rf_ScalarLogical(p != NULL);
150165
}
166+
167+
// Expose external pointer address to R
168+
SEXP RC_get_external_ptr_addr(SEXP ext) {
169+
void *addr = R_ExternalPtrAddr(ext);
170+
return Rf_ScalarReal((double)(uintptr_t)addr);
171+
}

0 commit comments

Comments
 (0)