-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsubroutine.R
More file actions
204 lines (180 loc) · 5.92 KB
/
Copy pathsubroutine.R
File metadata and controls
204 lines (180 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
new_fortran_subroutine <- function(
name,
closure,
parent = environment(closure)
) {
check_fortran_subroutine_name_valid(name)
check_all_var_names_valid(closure)
# translate body, and populate scope with variables
body <- body(closure)
# defuse calls like `-1` and `1+1i`. Not really necessary, but simplifies downstream a little.
body <- defuse_numeric_literals(body)
# TODO: try harder here to use one of the input vars as the output var
body <- ensure_last_expr_sym(body)
# update closure with sym return value
base::body(closure) <- body
# body <- rlang::zap_srcref(body)
scope <- new_scope(closure, parent)
attr(scope, "return_names") <- unique(unname(closure_return_var_names(
closure
)))
# inject symbols for var sizes in declare calls, so like:
# declare(type(foo = integer(nr, NA)),
# type(bar = integer(nr, 3)))
# become:
# declare(type(foo = integer(foo_dim_1_, foo_dim_2_)),
# type(bar = integer(foo_dim_1_, 3L)))
body <- substitute_declared_sizes(body)
stmts <- as.list(body)[-1L]
body <- compile_nonreturn_statements(drop_last(stmts), scope)
# check all input vars were declared
# TODO: this check might be too late, because r2f() might throw cryptic errors
# when handling undeclared variables. Either throw better errors from r2f(), or
# handle all declares first
for (arg_name in names(formals(closure))) {
var <- get0(arg_name, scope)
if (is.null(var) || !inherits(var, Variable)) {
stop("arg not declared: ", arg_name)
}
}
# Ensure return vars are marked (primarily to mark logical outputs as integer
# storage for bind(c) and to support early "external-ness" checks).
for (return_name in attr(scope, "return_names", exact = TRUE)) {
if (!is.null(var <- get0(return_name, scope))) {
stopifnot(inherits(var, Variable))
var@is_return <- TRUE
if (identical(var@mode, "logical")) {
var@logical_as_int <- TRUE
}
scope[[return_name]] <- var
}
}
manifest <- r2f.scope(scope)
fsub_arg_names <- attr(manifest, "signature", TRUE)
internal_procs <- attr(scope, "internal_procs", exact = TRUE) %||% list()
contains_block <- if (length(internal_procs)) {
procs_code <- lapply(internal_procs, `[[`, "code") |>
unlist(use.names = FALSE) |>
str_flatten_lines()
str_flatten_lines("contains", indent(procs_code))
} else {
NULL
}
contains_block_indented <- if (is.null(contains_block)) {
NULL
} else {
indent(contains_block)
}
body_section <- indent(body)
if (!is.null(contains_block_indented)) {
body_section <- str_flatten_lines(body_section, "", contains_block_indented)
}
uses_rng <- isTRUE(attr(scope, 'uses_rng', TRUE))
used_iso_bindings <- iso_c_binding_symbols(
vars = scope_vars(scope),
body_code = body,
logical_is_c_int = function(var) var@name %in% fsub_arg_names,
uses_rng = uses_rng
)
if (uses_rng) {
rng_interface <- glue::trim(
'
interface
function unif_rand() bind(c, name = "unif_rand") result(u)
use iso_c_binding, only: c_double
real(c_double) :: u
end function unif_rand
end interface
'
)
manifest <- str_flatten_lines(manifest, "", rng_interface)
}
subroutine <- glue(
"
subroutine {name}({str_flatten_commas(fsub_arg_names)}) {fortran_bind_clause(name)}
use iso_c_binding, only: {str_flatten_commas(used_iso_bindings)}
implicit none
{indent(manifest)}
{body_section}
end subroutine
"
)
subroutine <- glue::trim(subroutine)
subroutine <- insert_fortran_line_continuations(subroutine)
FortranSubroutine(
subroutine,
name = name,
signature = fsub_arg_names,
scope = scope,
closure = closure
)
}
check_fortran_subroutine_name_valid <- function(name) {
if (!is_string(name) || !nzchar(name)) {
stop("`name` must be a non-empty string.", call. = FALSE)
}
# Must be valid in:
# - Fortran: used as the procedure name.
# - C: used as the binding label and referenced from the C bridge.
if (!grepl("^[A-Za-z][A-Za-z0-9_]*$", name)) {
suggestion <- gsub("[^A-Za-z0-9_]", "_", name)
if (!grepl("^[A-Za-z]", suggestion)) {
suggestion <- paste0("quick_", suggestion)
}
stop(
"Invalid `quick()` name: '",
name,
"'. The name must match the pattern '^[A-Za-z][A-Za-z0-9_]*$' ",
"(letters, digits, underscore; starting with a letter). ",
"Suggested name: '",
suggestion,
"'.",
call. = FALSE
)
}
invisible(TRUE)
}
fortran_bind_clause <- function(name) {
# Fortran is case-insensitive, but C symbol names are case-sensitive.
# When `bind(c)` is used without an explicit `name=`, some toolchains
# canonicalize the external symbol name (e.g., lowercase), which can cause
# the C bridge to look up the wrong symbol for mixed-case procedure names.
if (identical(name, tolower(name))) {
return("bind(c)")
}
glue('bind(c, name = "{name}")')
}
insert_fortran_line_continuations <- function(
code,
preserve_attributes = TRUE
) {
attrs_in <- attributes(code)
code <- as.character(code)
lines <- str_split_lines(code)
lines <- trimws(lines, "right")
if (any(too_long <- nchar(lines) > 132)) {
# remove leading indentation
lines[too_long] <- trimws(lines[too_long], "left")
# move trailing comment at the end
lines[too_long] <- sub("^(.*)!(.*)$", "!\\2\n\\1", lines[too_long])
lines <- str_split_lines(lines)
# maximum 255 continuations are allowed
for (i in 1:256) {
if (!any(too_long <- nchar(lines) > 132)) {
break
}
lines[too_long] <- sub("^(.{1,130})\\s", "\\1 &\n", lines[too_long])
lines <- str_split_lines(lines)
}
if (i > 255L) {
stop(
"Too long line encountered. Please split long expressions into a sequence of smaller expressions."
)
}
}
code <- str_flatten_lines(lines)
if (preserve_attributes) {
attributes(code) <- attrs_in
}
code
}