| 373 | enum class BinOp { Add, Subtract, Multiply, Divide }; |
| 374 | |
| 375 | struct BinaryOperationNodeType : NodeType { |
| 376 | BinaryOperationNodeType(LangModule& mod, DataType ty, BinOp binaryOperation) |
| 377 | : NodeType(mod), mBinOp(binaryOperation), mType{ty} { |
| 378 | makePure(); |
| 379 | |
| 380 | std::string opStr = [](BinOp b) { |
| 381 | switch (b) { |
| 382 | case BinOp::Add: return "+"; |
| 383 | case BinOp::Subtract: return "-"; |
| 384 | case BinOp::Multiply: return "*"; |
| 385 | case BinOp::Divide: return "/"; |
| 386 | default: return ""; |
| 387 | } |
| 388 | return ""; |
| 389 | }(binaryOperation); |
| 390 | |
| 391 | setName(ty.unqualifiedName() + opStr + ty.unqualifiedName()); |
| 392 | |
| 393 | std::string opVerb = [](BinOp b) { |
| 394 | switch (b) { |
| 395 | case BinOp::Add: return "Add"; |
| 396 | case BinOp::Subtract: return "Subtract"; |
| 397 | case BinOp::Multiply: return "Multiply"; |
| 398 | case BinOp::Divide: return "Divide"; |
| 399 | default: return ""; |
| 400 | } |
| 401 | return ""; |
| 402 | }(binaryOperation); |
| 403 | |
| 404 | setDescription(opVerb + " two " + ty.unqualifiedName() + "s"); |
| 405 | |
| 406 | setDataInputs({{"a", ty}, {"b", ty}}); |
| 407 | setDataOutputs({{"", ty}}); |
| 408 | } |
| 409 | |
| 410 | Result codegen( |
| 411 | size_t /*execInputID*/, const llvm::DebugLoc& nodeLocation, |
| 412 | const std::vector<llvm::Value*>& io, llvm::BasicBlock* codegenInto, |
| 413 | const std::vector<llvm::BasicBlock*>& outputBlocks, |
| 414 | std::unordered_map<std::string, std::shared_ptr<void>>& /*compileCache*/) override { |
| 415 | assert(io.size() == 3 && codegenInto != nullptr && outputBlocks.size() == 1); |
| 416 | |
| 417 | llvm::IRBuilder<> builder(codegenInto); |
| 418 | builder.SetCurrentDebugLocation(nodeLocation); |
| 419 | |
| 420 | llvm::Value* result = nullptr; |
| 421 | if (mType.unqualifiedName() == "i32") { |
| 422 | result = [&](BinOp b) { |
| 423 | switch (b) { |
| 424 | case BinOp::Add: return builder.CreateAdd(io[0], io[1]); |
| 425 | case BinOp::Subtract: return builder.CreateSub(io[0], io[1]); |
| 426 | case BinOp::Multiply: return builder.CreateMul(io[0], io[1]); |
| 427 | case BinOp::Divide: return builder.CreateSDiv(io[0], io[1]); |
| 428 | default: return (llvm::Value*)nullptr; |
| 429 | } |
| 430 | return (llvm::Value*)nullptr; |
| 431 | }(mBinOp); |
| 432 |
nothing calls this directly
no outgoing calls
no test coverage detected