Skip to content

Commit

Permalink
Add hidden Fortran arguments in the wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Oct 6, 2023
1 parent 8ff8f7c commit 9b54946
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 128 deletions.
99 changes: 68 additions & 31 deletions gen/analyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function fortran_name_arguments(signature::String)
v = split(signature, "(")
fname = v[1]
arguments = split(v[2], ", ")
arguments = String.(arguments)
return fname, arguments
end

Expand Down Expand Up @@ -92,7 +93,7 @@ function type_mapping(type::String)
return julia_type
end

function type_detector(types::Vector{String}, arguments::Vector{<:AbstractString}, line::AbstractString, type::String)
function type_detector(types::Vector{String}, arguments::Vector{String}, line::AbstractString, type::String)
taille = length(line)
len = length(type)
julia_type = type_mapping(type)
Expand Down Expand Up @@ -136,11 +137,15 @@ function fortran_public(str::String)
return exported_symbols
end

function fortran_types(code::AbstractString, arguments::Vector{<:AbstractString}; verbose::Bool=false)
function fortran_types(code::AbstractString, arguments::Vector{String}; verbose::Bool=false)
narguments = length(arguments)
types = ["" for i=1:narguments]
lines = split(code, "\n")
nlines = length(lines)

# Number of characters for each string input
strlen = Dict{String,Int}()

for i = nlines:-1:1
lines[i] = replace(lines[i], "\t" => "")
lines[i] = replace(lines[i], "\r" => "")
Expand Down Expand Up @@ -193,20 +198,26 @@ function fortran_types(code::AbstractString, arguments::Vector{<:AbstractString}
# start=12 -> CHARACTER*N with N < 10
# start=13 -> CHARACTER*N with N ≥ 10 and N < 100
# start=14 -> CHARACTER*N with N ≥ 100 and N < 1000
for start in (12, 13, 14)
length(line) start || continue
variables = split(line[start:end], ',')
for variable in variables
for (i, argument) in enumerate(arguments)
if (argument == variable) || (uppercase(argument) == variable)
types[i] = "Ptr{UInt8}"
end
len = nothing
start = 14
while (len == nothing) && (start 12)
len = length(line) start ? tryparse(Int, line[11:start-1]) : nothing
(len == nothing) && (start = start - 1)
end

variables = split(line[start:end], ',')
variables = [split(variable, '(')[1] for variable in variables]
for variable in variables
for (i, argument) in enumerate(arguments)
if (argument == variable) || (uppercase(argument) == variable)
types[i] = "Ptr{UInt8}"
strlen[argument] = len
end
end
end
end
end
return types
return types, strlen
end

function fortran_analyzer(str::String, basename::String, extension::String)
Expand All @@ -230,16 +241,35 @@ function fortran_analyzer(str::String, basename::String, extension::String)
end
end

# We only want the signature of the subroutines and functions
patterns = ["END SUBROUTINE", "end subroutine", "subroutine.", "SUBROUTINES.",
"subroutines", "SUBROUTINES", "by subroutine", "of subroutine",
"see subroutine", "SUBROUTINE.", "the subroutine", "THE SUBROUTINE",
"THIS SUBROUTINE", "TO SUBROUTINE", "HARWELL SUBROUTINE", "ANALYSIS SUBROUTINE",
"compression subroutine", "A subroutine", "A SUBROUTINE", "This subroutine",
"Factorization subroutine", "END FUNCTION", "end function", "THE FUNCTION",
"THIS FUNCTION", "Subroutines", "Functions"]
for pattern in patterns
str = replace(str, pattern => "")
# Specific patterns
if !occursin("hsl", basename) && occursin("eb22", basename)
str = replace(str, "CHARACTER WANTRS*1" => "CHARACTER WANTRS")
end
if !occursin("hsl", basename) && occursin("ma62", basename)
str = replace(str, "CHARACTER FILNAM(2)*128" => "CHARACTER*128 FILNAM(2)")
end
if !occursin("hsl", basename) && occursin("mc36", basename)
str = replace(str, "CHARACTER TITLE*80" => "CHARACTER*80 TITLE")
str = replace(str, "CHARACTER TITLE*80" => "CHARACTER*80 TITLE")
end
if !occursin("hsl", basename) && occursin("mc54", basename)
str = replace(str, "CHARACTER TITLE*72,KEY*8" => "CHARACTER*72 TITLE\nCHARACTER*8 KEY")
end
if !occursin("hsl", basename) && occursin("mc55", basename)
str = replace(str, "CHARACTER TITLE*72,KEY*8,DATTYP*3,POSITN*1,ORGNIZ*1," => "CHARACTER*72 TITLE\nCHARACTER*8 KEY\nCHARACTER*3 DATTYP\nCHARACTER POSITN\nCHARACTER ORGNIZ")
str = replace(str, "CASEID*8" => "\nCHARACTER*8 CASEID")
end
if !occursin("hsl", basename) && occursin("mc56", basename)
str = replace(str, "CHARACTER TITLE*72,KEY*8,CASEID*8,DATTYP*3," => "CHARACTER*72 TITLE\nCHARACTER*8 KEY\nCHARACTER*8 CASEID\nCHARACTER*3 DATTYP")
str = replace(str, "POSITN*1,ORGNIZ*1" => "\nCHARACTER POSITN\nCHARACTER ORGNIZ")
str = replace(str, "CHARACTER BUFFER1*80,BUFFER2*80" => "CHARACTER*80 BUFFER1\nCHARACTER*80 BUFFER2")
str = replace(str, "CHARACTER PTRFMT*16,INDFMT*16,VALFMT*20" => "CHARACTER*16 PTRFMT\n CHARACTER*16 INDFMT\n CHARACTER*20 VALFMT")
end
if !occursin("hsl", basename) && occursin("me62", basename)
str = replace(str, "CHARACTER FILNAM(2)*128" => "CHARACTER*128 FILNAM(2)")
end
if !occursin("hsl", basename) && occursin("mf36", basename)
str = replace(str, "CHARACTER TITLE*80" => "CHARACTER*80 TITLE")
end

# For FORTRAN 90 files, not all functions or subroutines are public
Expand Down Expand Up @@ -280,7 +310,7 @@ function fortran_analyzer(str::String, basename::String, extension::String)
# Determine the type of the arguments
verbose = false
(fname == "unknown") && (verbose = true)
types = fortran_types(code, arguments, verbose=verbose)
types, strlen = fortran_types(code, arguments, verbose=verbose)

# Determine the type of the ouput
output_type = fortran_output(case, v[index])
Expand All @@ -295,25 +325,22 @@ function fortran_analyzer(str::String, basename::String, extension::String)
end
signature = signature * ")"

push!(functions, (signature, fname, arguments, types, output_type))
push!(functions, (signature, fname, arguments, types, output_type, strlen))
end
end
end
return functions
end

function main(name::String="all")
function main(name::String="all"; verbose::Bool=false)
# Create a vector with all symbols exported by the shared library libhsl
symbols = read(symbols_path, String)
symbols = split(symbols, "\n", keepempty=false)
symbols = [symbol[20:end] for symbol in symbols]

# Verbose mode
verbose = false

for (root, dirs, files) in walkdir(libhsl)

# We don't want to go inside "metis" and "libhsl" folders
# We don't want to go inside "examples", metis" and "libhsl" folders
mapreduce(excluded_folder -> occursin(excluded_folder, root), |, ["examples", "metis", "libhsl/libhsl"]) && continue

# Test that we are in one subfolder of libhsl
Expand Down Expand Up @@ -356,7 +383,7 @@ function main(name::String="all")
format = true
index = 0
for fun in fnames_package
signature, fname, arguments, types, output_type = fun
signature, fname, arguments, types, output_type, strlen = fun
narguments = length(arguments)

# Only define functions directly related to the HSL package
Expand All @@ -372,14 +399,24 @@ function main(name::String="all")
for k = 1:narguments
if types[k] == ""
format = false
@info "Unable to determine the type of $(arguments[k])"
@warn "Unable to determine the type of $(arguments[k])"
end
write(file_wrapper, "$(arguments[k])::$(types[k])")
(k < narguments) && write(file_wrapper, ", ")
end

# Hidden arguments
if "Ref{UInt8}" types || "Ptr{UInt8}" types
verbose && @info "Hidden argument in $fname."
end
for k = 1:narguments
(types[k] == "Ref{UInt8}") && write(file_wrapper, ", 1::Csize_t")
(types[k] == "Ptr{UInt8}") && write(file_wrapper, ", $(strlen[arguments[k]])::Csize_t")
end

if output_type == ""
format = false
@info "Unable to determine the output type"
@warn "Unable to determine the output type"
end
write(file_wrapper, ")::$(output_type)\n")
write(file_wrapper, "end\n")
Expand Down
4 changes: 2 additions & 2 deletions src/Fortran/eb22.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ end

function eb22wd(a, adim, n, p, r, work, wantrs, nvecs)
@ccall libhsl.eb22wd_(a::Ptr{Float64}, adim::Ref{Cint}, n::Ref{Cint}, p::Ref{Cint}, r::Ref{Cint},
work::Ptr{Float64}, wantrs::Ptr{UInt8}, nvecs::Ref{Cint})::Cvoid
work::Ptr{Float64}, wantrs::Ref{UInt8}, nvecs::Ref{Cint}, 1::Csize_t)::Cvoid
end

function eb22xd(a, b, c, d, e, f)
Expand Down Expand Up @@ -199,7 +199,7 @@ end

function eb22w(a, adim, n, p, r, work, wantrs, nvecs)
@ccall libhsl.eb22w_(a::Ptr{Float32}, adim::Ref{Cint}, n::Ref{Cint}, p::Ref{Cint}, r::Ref{Cint},
work::Ptr{Float32}, wantrs::Ptr{UInt8}, nvecs::Ref{Cint})::Cvoid
work::Ptr{Float32}, wantrs::Ref{UInt8}, nvecs::Ref{Cint}, 1::Csize_t)::Cvoid
end

function eb22x(a, b, c, d, e, f)
Expand Down
4 changes: 2 additions & 2 deletions src/Fortran/fd15.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function fd15ad(t)
@ccall libhsl.fd15ad_(t::Ref{UInt8})::Float64
@ccall libhsl.fd15ad_(t::Ref{UInt8}, 1::Csize_t)::Float64
end

function fd15a(t)
@ccall libhsl.fd15a_(t::Ref{UInt8})::Float32
@ccall libhsl.fd15a_(t::Ref{UInt8}, 1::Csize_t)::Float32
end
4 changes: 2 additions & 2 deletions src/Fortran/ma49.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function ma49ld(mat, trans, m, n, ifact, lft, r, lr, istack, q, lq, qstack, awor
b::Ptr{Float64}, x::Ptr{Float64}, ides::Ref{Cint}, nsteps::Ref{Cint},
maxfrt::Ref{Cint}, nproc::Ref{Cint}, nstk::Ptr{Cint}, pool::Ptr{Cint},
maxlea::Ref{Cint}, nbrad::Ref{Cint}, fils::Ptr{Cint}, frere::Ptr{Cint},
iflag::Ref{Cint}, ierror::Ref{Cint})::Cvoid
iflag::Ref{Cint}, ierror::Ref{Cint}, 1::Csize_t, 1::Csize_t)::Cvoid
end

function ma49md(n, rwblk, nstk, ndd, lerow, ma59cd, fils, frere, mstk, ncstk, nelim, nsteps, info,
Expand Down Expand Up @@ -388,7 +388,7 @@ function ma49l(mat, trans, m, n, ifact, lft, r, lr, istack, q, lq, qstack, awork
b::Ptr{Float32}, x::Ptr{Float32}, ides::Ref{Cint}, nsteps::Ref{Cint},
maxfrt::Ref{Cint}, nproc::Ref{Cint}, nstk::Ptr{Cint}, pool::Ptr{Cint},
maxlea::Ref{Cint}, nbrad::Ref{Cint}, fils::Ptr{Cint}, frere::Ptr{Cint},
iflag::Ref{Cint}, ierror::Ref{Cint})::Cvoid
iflag::Ref{Cint}, ierror::Ref{Cint}, 1::Csize_t, 1::Csize_t)::Cvoid
end

function ma49m(n, rwblk, nstk, ndd, lerow, ma59c, fils, frere, mstk, ncstk, nelim, nsteps, info,
Expand Down
4 changes: 2 additions & 2 deletions src/Fortran/ma62.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end

function ma62pd(istrm, filnam, lenbuf, icntl, isave, info)
@ccall libhsl.ma62pd_(istrm::Ptr{Cint}, filnam::Ptr{UInt8}, lenbuf::Ptr{Cint}, icntl::Ptr{Cint},
isave::Ptr{Cint}, info::Ptr{Cint})::Cvoid
isave::Ptr{Cint}, info::Ptr{Cint}, 128::Csize_t)::Cvoid
end

function ma62bd(nvar, ivar, ndf, last, lavar, avar, nrhsb, rhs, lx, x, lenbuf, lw, w, liw, iw,
Expand Down Expand Up @@ -142,7 +142,7 @@ end

function ma62p(istrm, filnam, lenbuf, icntl, isave, info)
@ccall libhsl.ma62p_(istrm::Ptr{Cint}, filnam::Ptr{UInt8}, lenbuf::Ptr{Cint}, icntl::Ptr{Cint},
isave::Ptr{Cint}, info::Ptr{Cint})::Cvoid
isave::Ptr{Cint}, info::Ptr{Cint}, 128::Csize_t)::Cvoid
end

function ma62b(nvar, ivar, ndf, last, lavar, avar, nrhsb, rhs, lx, x, lenbuf, lw, w, liw, iw, icntl,
Expand Down
16 changes: 10 additions & 6 deletions src/Fortran/mc36.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ function mc36ad(nmax, nzmax, title, elt, nrow, ncol, nnzero, colptr, rowind, val
rowind::Ptr{Cint}, values::Ptr{Float64}, maxrhs::Ref{Cint}, nrhs::Ref{Cint},
nzrhs::Ref{Cint}, rhsptr::Ptr{Cint}, rhsind::Ptr{Cint},
rhsval::Ptr{Float64}, guess::Ptr{Float64}, soln::Ptr{Float64},
iw::Ptr{Cint}, icntl::Ptr{Cint}, info::Ptr{Cint})::Cvoid
iw::Ptr{Cint}, icntl::Ptr{Cint}, info::Ptr{Cint}, 80::Csize_t)::Cvoid
end

function mc36bd(n, ind, fmtind, lunit)
@ccall libhsl.mc36bd_(n::Ref{Cint}, ind::Ptr{Cint}, fmtind::Ptr{UInt8}, lunit::Ref{Cint})::Cvoid
@ccall libhsl.mc36bd_(n::Ref{Cint}, ind::Ptr{Cint}, fmtind::Ptr{UInt8}, lunit::Ref{Cint},
16::Csize_t)::Cvoid
end

function mc36cd(n, a, fmta, lunit)
@ccall libhsl.mc36cd_(n::Ref{Cint}, a::Ptr{Float64}, fmta::Ptr{UInt8}, lunit::Ref{Cint})::Cvoid
@ccall libhsl.mc36cd_(n::Ref{Cint}, a::Ptr{Float64}, fmta::Ptr{UInt8}, lunit::Ref{Cint},
20::Csize_t)::Cvoid
end

function mc36a(nmax, nzmax, title, elt, nrow, ncol, nnzero, colptr, rowind, values, maxrhs, nrhs,
Expand All @@ -23,13 +25,15 @@ function mc36a(nmax, nzmax, title, elt, nrow, ncol, nnzero, colptr, rowind, valu
rowind::Ptr{Cint}, values::Ptr{Float32}, maxrhs::Ref{Cint}, nrhs::Ref{Cint},
nzrhs::Ref{Cint}, rhsptr::Ptr{Cint}, rhsind::Ptr{Cint}, rhsval::Ptr{Float32},
guess::Ptr{Float32}, soln::Ptr{Float32}, iw::Ptr{Cint}, icntl::Ptr{Cint},
info::Ptr{Cint})::Cvoid
info::Ptr{Cint}, 80::Csize_t)::Cvoid
end

function mc36b(n, ind, fmtind, lunit)
@ccall libhsl.mc36b_(n::Ref{Cint}, ind::Ptr{Cint}, fmtind::Ptr{UInt8}, lunit::Ref{Cint})::Cvoid
@ccall libhsl.mc36b_(n::Ref{Cint}, ind::Ptr{Cint}, fmtind::Ptr{UInt8}, lunit::Ref{Cint},
16::Csize_t)::Cvoid
end

function mc36c(n, a, fmta, lunit)
@ccall libhsl.mc36c_(n::Ref{Cint}, a::Ptr{Float32}, fmta::Ptr{UInt8}, lunit::Ref{Cint})::Cvoid
@ccall libhsl.mc36c_(n::Ref{Cint}, a::Ptr{Float32}, fmta::Ptr{UInt8}, lunit::Ref{Cint},
20::Csize_t)::Cvoid
end
Loading

0 comments on commit 9b54946

Please sign in to comment.