33#include < llvm/Support/TargetSelect.h>
44#include < llvm/IR/Verifier.h>
55#include < llvm/ExecutionEngine/Orc/LLJIT.h>
6- #include < scratchcpp/value.h>
76
87#include " llvmcodebuilder.h"
98#include " llvmexecutablecode.h"
@@ -30,16 +29,6 @@ LLVMCodeBuilder::LLVMCodeBuilder(const std::string &id) :
3029 initTypes ();
3130}
3231
33- LLVMCodeBuilder::~LLVMCodeBuilder ()
34- {
35- for (const auto &values : m_constValues) {
36- for (const auto &v : values) {
37- if (v)
38- value_free (v.get ());
39- }
40- }
41- }
42-
4332std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize ()
4433{
4534 size_t functionIndex = 0 ;
@@ -291,15 +280,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
291280 std::cout << " ==============" << std::endl << std::endl;
292281#endif
293282
294- std::vector<std::unique_ptr<ValueData>> constValues;
295-
296- for (auto &values : m_constValues) {
297- for (auto &v : values)
298- constValues.push_back (std::move (v));
299- }
300-
301- m_constValues.clear ();
302- return std::make_shared<LLVMExecutableCode>(std::move (m_module), constValues);
283+ return std::make_shared<LLVMExecutableCode>(std::move (m_module));
303284}
304285
305286void LLVMCodeBuilder::addFunctionCall (const std::string &functionName, Compiler::StaticType returnType, const std::vector<Compiler::StaticType> &argTypes)
@@ -331,16 +312,10 @@ void LLVMCodeBuilder::addFunctionCall(const std::string &functionName, Compiler:
331312void LLVMCodeBuilder::addConstValue (const Value &value)
332313{
333314 auto reg = std::make_shared<Register>(TYPE_MAP[value.type ()]);
334- reg->isRawValue = false ;
335315 reg->isConstValue = true ;
336- reg->constValueIndex = m_constValues[m_currentFunction]. size () ;
316+ reg->constValue = value ;
337317 m_regs[m_currentFunction].push_back (reg);
338318 m_tmpRegs.push_back (reg);
339-
340- std::unique_ptr<ValueData> v = std::make_unique<ValueData>();
341- value_init (v.get ());
342- value_assign_copy (v.get (), &value.data ());
343- m_constValues[m_currentFunction].push_back (std::move (v));
344319}
345320
346321void LLVMCodeBuilder::addVariableValue (Variable *variable)
@@ -448,35 +423,6 @@ llvm::Function *LLVMCodeBuilder::beginFunction(size_t index)
448423 llvm::BasicBlock *entry = llvm::BasicBlock::Create (m_ctx, " entry" , func);
449424 m_builder.SetInsertPoint (entry);
450425
451- // Add const value pointers
452- const auto &constValues = m_constValues[index];
453- std::vector<llvm::Value *> constPtrs;
454-
455- for (size_t i = 0 ; i < constValues.size (); i++) {
456- llvm::Value *intAddress = m_builder.getInt64 ((uintptr_t )constValues[i].get ());
457- llvm::Value *ptr = m_builder.CreateIntToPtr (intAddress, m_valueDataType->getPointerTo ());
458- constPtrs.push_back (ptr);
459- }
460-
461- // Add registers
462- auto ®s = m_regs[index];
463- size_t regIndex = 0 ;
464-
465- for (auto ® : regs) {
466- if (reg->isConstValue ) {
467- // Do not allocate space for existing constant values
468- reg->value = constPtrs[reg->constValueIndex ];
469- } else {
470- /* const std::string name = "r" + std::to_string(regIndex);
471-
472- llvm::Value *valueData = m_builder.CreateAlloca(m_valueDataType, nullptr, name);
473- m_builder.CreateCall(resolve_value_init(), { valueData });
474-
475- reg->value = valueData;
476- regIndex++;*/
477- }
478- }
479-
480426 return func;
481427}
482428
@@ -509,6 +455,9 @@ void LLVMCodeBuilder::freeHeap()
509455
510456llvm::Value *LLVMCodeBuilder::castValue (std::shared_ptr<Register> reg, Compiler::StaticType targetType)
511457{
458+ if (reg->isConstValue )
459+ return castConstValue (reg->constValue , targetType);
460+
512461 if (reg->isRawValue )
513462 return castRawValue (reg, targetType);
514463
@@ -655,6 +604,24 @@ llvm::Value *LLVMCodeBuilder::castRawValue(std::shared_ptr<Register> reg, Compil
655604 }
656605}
657606
607+ llvm::Value *LLVMCodeBuilder::castConstValue (const Value &value, Compiler::StaticType targetType)
608+ {
609+ switch (targetType) {
610+ case Compiler::StaticType::Number:
611+ return llvm::ConstantFP::get (m_ctx, llvm::APFloat (value.toDouble ()));
612+
613+ case Compiler::StaticType::Bool:
614+ return m_builder.getInt1 (value.toBool ());
615+
616+ case Compiler::StaticType::String:
617+ return m_builder.CreateGlobalStringPtr (value.toString ());
618+
619+ default :
620+ assert (false );
621+ return nullptr ;
622+ }
623+ }
624+
658625llvm::Type *LLVMCodeBuilder::getType (Compiler::StaticType type)
659626{
660627 switch (type) {
0 commit comments