|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 |
|
3 | 3 | #include "comparison.h" |
| 4 | +#include "../llvminstruction.h" |
4 | 5 | #include "../llvmbuildutils.h" |
5 | 6 |
|
| 7 | +using namespace libscratchcpp; |
6 | 8 | using namespace libscratchcpp::llvmins; |
7 | 9 |
|
8 | 10 | ProcessResult Comparison::process(LLVMInstruction *ins) |
9 | 11 | { |
| 12 | + ProcessResult ret(true, ins); |
| 13 | + |
| 14 | + switch (ins->type) { |
| 15 | + case LLVMInstruction::Type::CmpEQ: |
| 16 | + ret.next = buildCmpEQ(ins); |
| 17 | + break; |
| 18 | + |
| 19 | + case LLVMInstruction::Type::CmpGT: |
| 20 | + ret.next = buildCmpGT(ins); |
| 21 | + break; |
| 22 | + |
| 23 | + case LLVMInstruction::Type::CmpLT: |
| 24 | + ret.next = buildCmpLT(ins); |
| 25 | + break; |
| 26 | + |
| 27 | + case LLVMInstruction::Type::StrCmpEQCS: |
| 28 | + ret.next = buildStrCmpEQCS(ins); |
| 29 | + break; |
| 30 | + |
| 31 | + case LLVMInstruction::Type::StrCmpEQCI: |
| 32 | + ret.next = buildStrCmpEQCI(ins); |
| 33 | + break; |
| 34 | + |
| 35 | + default: |
| 36 | + ret.match = false; |
| 37 | + break; |
| 38 | + } |
| 39 | + |
| 40 | + return ret; |
| 41 | +} |
| 42 | + |
| 43 | +LLVMInstruction *Comparison::buildCmpEQ(LLVMInstruction *ins) |
| 44 | +{ |
| 45 | + assert(ins->args.size() == 2); |
| 46 | + const auto &arg1 = ins->args[0].second; |
| 47 | + const auto &arg2 = ins->args[1].second; |
| 48 | + ins->functionReturnReg->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::EQ); |
| 49 | + |
| 50 | + return ins->next; |
| 51 | +} |
| 52 | + |
| 53 | +LLVMInstruction *Comparison::buildCmpGT(LLVMInstruction *ins) |
| 54 | +{ |
| 55 | + assert(ins->args.size() == 2); |
| 56 | + const auto &arg1 = ins->args[0].second; |
| 57 | + const auto &arg2 = ins->args[1].second; |
| 58 | + ins->functionReturnReg->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::GT); |
| 59 | + |
| 60 | + return ins->next; |
| 61 | +} |
| 62 | + |
| 63 | +LLVMInstruction *Comparison::buildCmpLT(LLVMInstruction *ins) |
| 64 | +{ |
| 65 | + assert(ins->args.size() == 2); |
| 66 | + const auto &arg1 = ins->args[0].second; |
| 67 | + const auto &arg2 = ins->args[1].second; |
| 68 | + ins->functionReturnReg->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::LT); |
| 69 | + |
| 70 | + return ins->next; |
| 71 | +} |
| 72 | + |
| 73 | +LLVMInstruction *Comparison::buildStrCmpEQCS(LLVMInstruction *ins) |
| 74 | +{ |
| 75 | + assert(ins->args.size() == 2); |
| 76 | + const auto &arg1 = ins->args[0].second; |
| 77 | + const auto &arg2 = ins->args[1].second; |
| 78 | + ins->functionReturnReg->value = m_utils.createStringComparison(arg1, arg2, true); |
| 79 | + |
| 80 | + return ins->next; |
| 81 | +} |
| 82 | + |
| 83 | +LLVMInstruction *Comparison::buildStrCmpEQCI(LLVMInstruction *ins) |
| 84 | +{ |
| 85 | + assert(ins->args.size() == 2); |
| 86 | + const auto &arg1 = ins->args[0].second; |
| 87 | + const auto &arg2 = ins->args[1].second; |
| 88 | + ins->functionReturnReg->value = m_utils.createStringComparison(arg1, arg2, false); |
| 89 | + |
| 90 | + return ins->next; |
10 | 91 | } |
0 commit comments