|
| 1 | +// |
| 2 | +// Created by bytedance on 2021/3/4. |
| 3 | +// |
| 4 | + |
| 5 | +#include "ExternFunctionHandler.h" |
| 6 | + |
| 7 | +#include "ErrHelper.h" |
| 8 | + |
| 9 | +Value * |
| 10 | +EchoFunctionHandler::tryhandle(LLVMContext &context, Module &module, std::string callName, std::vector<Value *> *argV) { |
| 11 | + if (callName == "echo" && argV != NIL) { |
| 12 | + std::string format_str; |
| 13 | + for (auto v : *argV){ |
| 14 | + if (v->getType()->isIntegerTy()){ |
| 15 | + switch (dyn_cast<IntegerType>(v->getType())->getBitWidth()) { |
| 16 | + case 32 : |
| 17 | + format_str.append("%d"); |
| 18 | + break; |
| 19 | + case 64: |
| 20 | + format_str.append("%ld"); |
| 21 | + break; |
| 22 | + default: |
| 23 | + format_str.append("%d"); |
| 24 | + } |
| 25 | + } |
| 26 | + else if (v->getType()->isPointerTy() && v->getType()->getContainedType(0)->isIntegerTy(8)){ |
| 27 | + // 字符数组 |
| 28 | + format_str.append("%s"); |
| 29 | + } else { |
| 30 | + // not implemented |
| 31 | + return LogErrorV(("打印函数的参数无法理解:"+v->getName()).str().c_str()); |
| 32 | + } |
| 33 | + format_str.append(" "); |
| 34 | + } |
| 35 | + format_str.append("\n"); |
| 36 | + auto symbol_value = ConstantDataArray::getString(context,format_str); |
| 37 | + auto symbol_mem = Builder.CreateAlloca(symbol_value->getType()); |
| 38 | + Builder.CreateStore(symbol_value,symbol_mem); |
| 39 | + auto symbol_pointer = Builder.CreateInBoundsGEP(symbol_mem, |
| 40 | + {ConstantInt::get(Type::getInt32Ty(context),0), |
| 41 | + ConstantInt::get(Type::getInt32Ty(context),0)}); |
| 42 | + argV->insert(argV->begin(),symbol_pointer); |
| 43 | + return Builder.CreateCall(getOrAddPrintfFunc(context,module),*argV,"echo"); |
| 44 | + } |
| 45 | + return NIL; |
| 46 | +} |
| 47 | + |
| 48 | +EchoFunctionHandler::EchoFunctionHandler() = default; |
| 49 | + |
| 50 | +int EchoFunctionHandler::getPriority() { |
| 51 | + return INTERNAL_IMPL; |
| 52 | +} |
| 53 | + |
| 54 | +ExternFunctionHandler::ExternFunctionHandler() = default; |
| 55 | + |
| 56 | +bool |
| 57 | +ExternFunctionHandler::externFunctionHandlerCompRule(ExternFunctionHandler *handler1, ExternFunctionHandler *handler2) { |
| 58 | + return handler1->getPriority() > handler2->getPriority(); |
| 59 | +} |
| 60 | + |
| 61 | +int ExternFunctionHandler::getPriority() { |
| 62 | + return MIN_IMPL; |
| 63 | +} |
| 64 | + |
| 65 | +Value *SleepFunctionHandler::tryhandle(LLVMContext &context, Module &module, std::string callName, |
| 66 | + std::vector<Value *> *argV) { |
| 67 | + if (callName == "sleep") { |
| 68 | + return Builder.CreateCall(getOrAddSleepFunc(context,module),*argV); |
| 69 | + } |
| 70 | + return NIL; |
| 71 | +} |
| 72 | + |
| 73 | +SleepFunctionHandler::SleepFunctionHandler() = default; |
| 74 | + |
| 75 | +int SleepFunctionHandler::getPriority() { |
| 76 | + return INTERNAL_IMPL; |
| 77 | +} |
| 78 | + |
| 79 | +Value * |
| 80 | +TimeFunctionHandler::tryhandle(LLVMContext &context, Module &module, std::string callName, std::vector<Value *> *argV) { |
| 81 | + if (callName == "now") { |
| 82 | + auto func = getOrAddTimeFunc(context,module); |
| 83 | + return Builder.CreateCall(func); |
| 84 | + } |
| 85 | + return NIL; |
| 86 | +} |
| 87 | + |
| 88 | +TimeFunctionHandler::TimeFunctionHandler() = default; |
| 89 | + |
| 90 | +int TimeFunctionHandler::getPriority() { |
| 91 | + return INTERNAL_IMPL; |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +Function *ExternFunctionHandler::getExternFunc(LLVMContext &context, Module &module, const std::string &func_name, |
| 97 | + std::vector<Value *> *vector) { |
| 98 | + // 判断是否可用加 |
| 99 | + if (func_name == "echo") { |
| 100 | + return ExternFunctionHandler::getOrAddPrintfFunc(context, module); |
| 101 | + } else if (func_name == "time") { |
| 102 | + return ExternFunctionHandler::getOrAddTimeFunc(context, module); |
| 103 | + } else if (func_name == "sleep") { |
| 104 | + return ExternFunctionHandler::getOrAddSleepFunc(context, module); |
| 105 | + } |
| 106 | + // 还未适配 |
| 107 | + return NIL; |
| 108 | +} |
| 109 | + |
| 110 | +Function *ExternFunctionHandler::getOrAddPrintfFunc(LLVMContext &context, Module &module) { |
| 111 | + auto funcs = module.functions(); |
| 112 | + auto it = funcs.begin(); |
| 113 | + for (; it != funcs.end(); it++) { |
| 114 | + if ((*it).getName() == "printf") { |
| 115 | + return &(*it); |
| 116 | + } |
| 117 | + } |
| 118 | + FunctionType *ty = FunctionType::get(Type::getInt32Ty(context), {Type::getInt8PtrTy(context)}, true); |
| 119 | + auto func = Function::Create(ty, llvm::GlobalValue::ExternalLinkage, "printf", module); |
| 120 | + return func; |
| 121 | +} |
| 122 | + |
| 123 | +Function *ExternFunctionHandler::getOrAddTimeFunc(LLVMContext &context, Module &module) { |
| 124 | + auto funcs = module.functions(); |
| 125 | + auto it = funcs.begin(); |
| 126 | + for (; it != funcs.end(); it++) { |
| 127 | + if ((*it).getName() == "__getms") { |
| 128 | + return &(*it); |
| 129 | + } |
| 130 | + } |
| 131 | +// std::vector<Type*> args; |
| 132 | +// args.push_back(Type::getInt64PtrTy(context)); |
| 133 | + FunctionType *ty = FunctionType::get(Type::getInt64Ty(context), false); |
| 134 | + auto func = Function::Create(ty, llvm::GlobalValue::ExternalLinkage, "__getms", module); |
| 135 | + return func; |
| 136 | +} |
| 137 | + |
| 138 | +Function *ExternFunctionHandler::getOrAddSleepFunc(LLVMContext &context, Module &module) { |
| 139 | + auto funcs = module.functions(); |
| 140 | + auto it = funcs.begin(); |
| 141 | + for (; it != funcs.end(); it++) { |
| 142 | + if ((*it).getName() == "sleep") { |
| 143 | + return &(*it); |
| 144 | + } |
| 145 | + } |
| 146 | +// std::vector<Type*> args; |
| 147 | +// args.push_back(Type::getInt64PtrTy(context)); |
| 148 | + FunctionType *ty = FunctionType::get(Type::getInt32Ty(context), Type::getInt32Ty(context), false); |
| 149 | + auto func = Function::Create(ty, llvm::GlobalValue::ExternalLinkage, "sleep", module); |
| 150 | + return func; |
| 151 | +} |
0 commit comments