33#include " variables.h"
44#include " ../llvminstruction.h"
55#include " ../llvmbuildutils.h"
6+ #include " ../llvmconstantregister.h"
67
78using namespace libscratchcpp ;
89using namespace libscratchcpp ::llvmins;
@@ -43,31 +44,9 @@ ProcessResult Variables::process(LLVMInstruction *ins)
4344LLVMInstruction *Variables::buildCreateLocalVariable (LLVMInstruction *ins)
4445{
4546 assert (ins->args .empty ());
46- llvm::Type *type = nullptr ;
4747
48- switch (ins->functionReturnReg ->type ()) {
49- case Compiler::StaticType::Number:
50- type = m_builder.getDoubleTy ();
51- break ;
52-
53- case Compiler::StaticType::Bool:
54- type = m_builder.getInt1Ty ();
55- break ;
56-
57- case Compiler::StaticType::String:
58- std::cerr << " error: local variables do not support string type" << std::endl;
59- break ;
60-
61- case Compiler::StaticType::Pointer:
62- std::cerr << " error: local variables do not support pointer type" << std::endl;
63- break ;
64-
65- default :
66- assert (false );
67- break ;
68- }
69-
70- ins->functionReturnReg ->value = m_utils.addAlloca (type);
48+ LLVMConstantRegister null (ins->functionReturnReg ->type (), Value ());
49+ ins->functionReturnReg ->value = m_utils.createValue (&null);
7150 return ins->next ;
7251}
7352
@@ -76,32 +55,22 @@ LLVMInstruction *Variables::buildWriteLocalVariable(LLVMInstruction *ins)
7655 assert (ins->args .size () == 2 );
7756 const auto &arg1 = ins->args [0 ];
7857 const auto &arg2 = ins->args [1 ];
79- llvm::Value *converted = m_utils.castValue (arg2.second , arg2.first );
80- m_builder.CreateStore (converted, arg1.second ->value );
58+ llvm::Value *typeVar = m_utils.addAlloca (m_builder.getInt32Ty ());
59+ m_builder.CreateStore (m_builder.getInt32 (static_cast <uint32_t >(m_utils.mapType (arg2.first ))), typeVar);
60+
61+ // TODO: Add integer support for local variables
62+ llvm::Value *isIntVar = m_utils.addAlloca (m_builder.getInt1Ty ());
63+ llvm::Value *intVar = m_utils.addAlloca (m_builder.getInt64Ty ());
64+
65+ m_utils.createValueStore (arg1.second ->value , typeVar, isIntVar, intVar, arg2.second , arg2.first , arg2.first );
8166 return ins->next ;
8267}
8368
8469LLVMInstruction *Variables::buildReadLocalVariable (LLVMInstruction *ins)
8570{
8671 assert (ins->args .size () == 1 );
8772 const auto &arg = ins->args [0 ];
88- llvm::Type *type = nullptr ;
89-
90- switch (ins->functionReturnReg ->type ()) {
91- case Compiler::StaticType::Number:
92- type = m_builder.getDoubleTy ();
93- break ;
94-
95- case Compiler::StaticType::Bool:
96- type = m_builder.getInt1Ty ();
97- break ;
98-
99- default :
100- assert (false );
101- break ;
102- }
103-
104- ins->functionReturnReg ->value = m_builder.CreateLoad (type, arg.second ->value );
73+ ins->functionReturnReg ->value = m_utils.castValue (arg.second , ins->functionReturnReg ->type ());
10574 return ins->next ;
10675}
10776
0 commit comments