|
10 | 10 | #include <scratchcpp/variable.h> |
11 | 11 | #include <scratchcpp/list.h> |
12 | 12 | #include <scratchcpp/dev/compilerconstant.h> |
| 13 | +#include <scratchcpp/dev/compilerlocalvariable.h> |
13 | 14 |
|
14 | 15 | #include "llvmcodebuilder.h" |
15 | 16 | #include "llvmexecutablecode.h" |
@@ -511,6 +512,64 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize() |
511 | 512 | break; |
512 | 513 | } |
513 | 514 |
|
| 515 | + case LLVMInstruction::Type::CreateLocalVariable: { |
| 516 | + assert(step.args.empty()); |
| 517 | + llvm::Type *type = nullptr; |
| 518 | + |
| 519 | + switch (step.functionReturnReg->type()) { |
| 520 | + case Compiler::StaticType::Number: |
| 521 | + type = m_builder.getDoubleTy(); |
| 522 | + break; |
| 523 | + |
| 524 | + case Compiler::StaticType::Bool: |
| 525 | + type = m_builder.getInt1Ty(); |
| 526 | + break; |
| 527 | + |
| 528 | + case Compiler::StaticType::String: |
| 529 | + std::cerr << "error: local variables do not support string type" << std::endl; |
| 530 | + break; |
| 531 | + |
| 532 | + default: |
| 533 | + assert(false); |
| 534 | + break; |
| 535 | + } |
| 536 | + |
| 537 | + step.functionReturnReg->value = m_builder.CreateAlloca(type); |
| 538 | + break; |
| 539 | + } |
| 540 | + |
| 541 | + case LLVMInstruction::Type::WriteLocalVariable: { |
| 542 | + assert(step.args.size() == 2); |
| 543 | + const auto &arg1 = step.args[0]; |
| 544 | + const auto &arg2 = step.args[1]; |
| 545 | + llvm::Value *converted = castValue(arg2.second, arg2.first); |
| 546 | + m_builder.CreateStore(converted, arg1.second->value); |
| 547 | + break; |
| 548 | + } |
| 549 | + |
| 550 | + case LLVMInstruction::Type::ReadLocalVariable: { |
| 551 | + assert(step.args.size() == 1); |
| 552 | + const auto &arg = step.args[0]; |
| 553 | + llvm::Type *type = nullptr; |
| 554 | + |
| 555 | + switch (step.functionReturnReg->type()) { |
| 556 | + case Compiler::StaticType::Number: |
| 557 | + type = m_builder.getDoubleTy(); |
| 558 | + break; |
| 559 | + |
| 560 | + case Compiler::StaticType::Bool: |
| 561 | + type = m_builder.getInt1Ty(); |
| 562 | + break; |
| 563 | + |
| 564 | + default: |
| 565 | + assert(false); |
| 566 | + break; |
| 567 | + } |
| 568 | + |
| 569 | + step.functionReturnReg->value = m_builder.CreateLoad(type, arg.second->value); |
| 570 | + break; |
| 571 | + } |
| 572 | + |
514 | 573 | case LLVMInstruction::Type::WriteVariable: { |
515 | 574 | assert(step.args.size() == 1); |
516 | 575 | assert(m_variablePtrs.find(step.workVariable) != m_variablePtrs.cend()); |
@@ -1049,6 +1108,11 @@ CompilerValue *LLVMCodeBuilder::addLoopIndex() |
1049 | 1108 | return createOp(LLVMInstruction::Type::LoopIndex, Compiler::StaticType::Number, {}, {}); |
1050 | 1109 | } |
1051 | 1110 |
|
| 1111 | +CompilerValue *LLVMCodeBuilder::addLocalVariableValue(CompilerLocalVariable *variable) |
| 1112 | +{ |
| 1113 | + return createOp(LLVMInstruction::Type::ReadLocalVariable, variable->type(), variable->type(), { variable->ptr() }); |
| 1114 | +} |
| 1115 | + |
1052 | 1116 | CompilerValue *LLVMCodeBuilder::addVariableValue(Variable *variable) |
1053 | 1117 | { |
1054 | 1118 | LLVMInstruction ins(LLVMInstruction::Type::ReadVariable); |
@@ -1256,6 +1320,19 @@ CompilerValue *LLVMCodeBuilder::createSelect(CompilerValue *cond, CompilerValue |
1256 | 1320 | return createOp(LLVMInstruction::Type::Select, valueType, { Compiler::StaticType::Bool, valueType, valueType }, { cond, trueValue, falseValue }); |
1257 | 1321 | } |
1258 | 1322 |
|
| 1323 | +CompilerLocalVariable *LLVMCodeBuilder::createLocalVariable(Compiler::StaticType type) |
| 1324 | +{ |
| 1325 | + CompilerValue *ptr = createOp(LLVMInstruction::Type::CreateLocalVariable, type); |
| 1326 | + auto var = std::make_shared<CompilerLocalVariable>(ptr); |
| 1327 | + m_localVars.push_back(var); |
| 1328 | + return var.get(); |
| 1329 | +} |
| 1330 | + |
| 1331 | +void LLVMCodeBuilder::createLocalVariableWrite(CompilerLocalVariable *variable, CompilerValue *value) |
| 1332 | +{ |
| 1333 | + createOp(LLVMInstruction::Type::WriteLocalVariable, Compiler::StaticType::Void, variable->type(), { variable->ptr(), value }); |
| 1334 | +} |
| 1335 | + |
1259 | 1336 | void LLVMCodeBuilder::createVariableWrite(Variable *variable, CompilerValue *value) |
1260 | 1337 | { |
1261 | 1338 | LLVMInstruction ins(LLVMInstruction::Type::WriteVariable); |
|
0 commit comments