Skip to content

Commit

Permalink
Passing global constants to BindC functions (lcompilers#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Nov 4, 2022
1 parent 08fd9a2 commit 14cd448
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
3 changes: 2 additions & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ RUN(NAME print_01 LABELS cpython llvm c wasm) # wasm not yet supports
# CPython and LLVM
RUN(NAME const_01 LABELS cpython llvm c)
RUN(NAME const_02 LABELS cpython llvm c)
RUN(NAME const_03 LABELS cpython llvm c)
RUN(NAME const_03 LABELS cpython llvm c
EXTRAFILES const_03b.c)
RUN(NAME expr_01 LABELS cpython llvm c wasm)
RUN(NAME expr_02 LABELS cpython llvm c wasm)
RUN(NAME expr_03 LABELS cpython llvm c wasm)
Expand Down
8 changes: 7 additions & 1 deletion integration_tests/const_03.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ltypes import i64, f64, i32, Const
from ltypes import i64, f64, i32, Const, ccall

CONST_1: Const[f64] = 32.0
CONST_2: Const[f64] = CONST_1 * 2.0
Expand All @@ -10,10 +10,16 @@
def print_value(value: Const[f64]):
print(value)

@ccall
def print_value_c(value: Const[f64]):
pass

def test_global_consts():
print_value(CONST_1)
print_value(CONST_2)
print(CONST_12)
print_value_c(CONST_1)
print_value_c(CONST_2)
assert CONST_1 == 32
assert CONST_2 == 64
assert abs(CONST_3 - 96.0) < 1e-12
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/const_03b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "const_03b.h"
#include <stdio.h>

void print_value_c(const double value) {
printf("print_value_c: %lf\n", value);
}
1 change: 1 addition & 0 deletions integration_tests/const_03b.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void print_value_c(const double value);
16 changes: 9 additions & 7 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5831,13 +5831,15 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
throw CodeGenError(std::string(arg->m_name) + " isn't defined in any scope.");
}
this->visit_expr_wrapper(arg->m_value, true);
llvm::BasicBlock &entry_block = builder->GetInsertBlock()->getParent()->getEntryBlock();
llvm::IRBuilder<> builder0(context);
builder0.SetInsertPoint(&entry_block, entry_block.getFirstInsertionPt());
llvm::AllocaInst *target = builder0.CreateAlloca(
get_type_from_ttype_t_util(arg->m_type), nullptr, "call_arg_value");
builder->CreateStore(tmp, target);
tmp = target;
if( x_abi != ASR::abiType::BindC ) {
llvm::BasicBlock &entry_block = builder->GetInsertBlock()->getParent()->getEntryBlock();
llvm::IRBuilder<> builder0(context);
builder0.SetInsertPoint(&entry_block, entry_block.getFirstInsertionPt());
llvm::AllocaInst *target = builder0.CreateAlloca(
get_type_from_ttype_t_util(arg->m_type), nullptr, "call_arg_value");
builder->CreateStore(tmp, target);
tmp = target;
}
} else {
llvm::Value* ptr = module->getOrInsertGlobal(nested_desc_name,
nested_global_struct);
Expand Down

0 comments on commit 14cd448

Please sign in to comment.