Skip to content

Commit b5e05bd

Browse files
authored
Merge pull request #7184 from jinangshah21/io_1
enh: add support for compiletime array source in `transfer`
2 parents 29229a8 + 011df7a commit b5e05bd

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
module array_04_transfer_mod
2+
use, intrinsic :: iso_fortran_env, only: int32, int64, real32
3+
implicit none
4+
integer(int32), parameter :: sc_constsub = int(z'deadbeef', int32)
5+
integer(int32), parameter :: int32_arr(2) = [sc_constsub, sc_constsub]
6+
real(real32), parameter :: real32_arr(2) = [real(1.23, real32), real(4.56, real32)]
7+
integer(int64), parameter :: int32_int64 = transfer(int32_arr, 0_int64)
8+
integer(int64), parameter :: real32_int64 = transfer(real32_arr, 0_int64)
9+
integer(int64), parameter :: real32_int32 = transfer(real32_arr, 0_int32)
10+
end module
111
program array_04_transfer
12+
use array_04_transfer_mod
213
implicit none
314
real :: value(5) = [1.1, 1.2, 1.3, 1.4, 1.5]
415
integer :: val(5)
516
val = transfer(value, val, 1 * size(value))
617
print * , val
718
if (all(val /= [1066192077, 1067030938, 1067869798, 1068708659, 1069547520])) error stop
19+
if (real32_int32 /= 1067282596) error stop
20+
if (real32_int64 /= 4652758847580893348_8) error stop
21+
if (int32_int64 /= -2401053088876216593_8) error stop
822
end program

src/lfortran/semantics/ast_common_visitor.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7145,6 +7145,45 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
71457145
source_bits.assign(reinterpret_cast<uint8_t*>(&val),
71467146
reinterpret_cast<uint8_t*>(&val) + sizeof(val));
71477147
}
7148+
} else if (ASRUtils::is_array(ASRUtils::expr_type(source)) && ASRUtils::is_value_constant(source_value)) {
7149+
ASR::ArrayConstant_t* const_source = ASR::down_cast<ASR::ArrayConstant_t>(ASRUtils::expr_value(source_value));
7150+
ASR::ttype_t* source_type = ASRUtils::expr_type(source_value);
7151+
int kind = ASRUtils::extract_kind_from_ttype_t(source_type);
7152+
size_t n_elements = ASRUtils::get_fixed_size_of_array(source_type);
7153+
7154+
if (ASRUtils::is_integer(*source_type)) {
7155+
if (kind == 4) {
7156+
for (size_t i=0; i < n_elements; i++) {
7157+
int32_t val = ASR::down_cast<ASR::IntegerConstant_t>(ASRUtils::fetch_ArrayConstant_value(al, const_source, i))->m_n;
7158+
source_bits.insert(source_bits.end(),
7159+
reinterpret_cast<uint8_t*>(&val),
7160+
reinterpret_cast<uint8_t*>(&val) + sizeof(val));
7161+
}
7162+
} else {
7163+
for (size_t i=0; i < n_elements; i++) {
7164+
int64_t val = ASR::down_cast<ASR::IntegerConstant_t>(ASRUtils::fetch_ArrayConstant_value(al, const_source, i))->m_n;
7165+
source_bits.insert(source_bits.end(),
7166+
reinterpret_cast<uint8_t*>(&val),
7167+
reinterpret_cast<uint8_t*>(&val) + sizeof(val));
7168+
}
7169+
}
7170+
} else if (ASRUtils::is_real(*source_type)) {
7171+
if (kind == 4) {
7172+
for (size_t i=0; i < n_elements; i++) {
7173+
float val = ASR::down_cast<ASR::RealConstant_t>(ASRUtils::fetch_ArrayConstant_value(al, const_source, i))->m_r;
7174+
source_bits.insert(source_bits.end(),
7175+
reinterpret_cast<uint8_t*>(&val),
7176+
reinterpret_cast<uint8_t*>(&val) + sizeof(val));
7177+
}
7178+
} else {
7179+
for (size_t i=0; i < n_elements; i++) {
7180+
double val = ASR::down_cast<ASR::RealConstant_t>(ASRUtils::fetch_ArrayConstant_value(al, const_source, i))->m_r;
7181+
source_bits.insert(source_bits.end(),
7182+
reinterpret_cast<uint8_t*>(&val),
7183+
reinterpret_cast<uint8_t*>(&val) + sizeof(val));
7184+
}
7185+
}
7186+
}
71487187
} else {
71497188
return ASR::make_BitCast_t(al, x.base.base.loc, source, mold, size, type, nullptr);
71507189
}

0 commit comments

Comments
 (0)