zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 1 | #include "slang_rs_export_func.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 3 | #include "llvm/Target/TargetData.h" |
Shih-wei Liao | 0a3f20e | 2010-08-10 13:09:49 -0700 | [diff] [blame] | 4 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 5 | #include "clang/AST/Decl.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 6 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 7 | #include "slang_rs_context.h" |
| 8 | #include "slang_rs_export_type.h" |
| 9 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 10 | using namespace slang; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 11 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 12 | RSExportFunc *RSExportFunc::Create(RSContext *Context, |
| 13 | const clang::FunctionDecl *FD) { |
| 14 | llvm::StringRef Name = FD->getName(); |
| 15 | RSExportFunc *F; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 16 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 17 | assert(!Name.empty() && "Function must have a name"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 19 | F = new RSExportFunc(Context, Name); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 20 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 21 | // Check whether the parameters passed to the function is exportable |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 22 | for (unsigned i = 0, e = FD->getNumParams(); i != e; i++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 23 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
| 24 | const llvm::StringRef ParamName = PVD->getName(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 25 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 26 | assert(!ParamName.empty() && "Parameter must have a name"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 28 | if (PVD->hasDefaultArg()) |
| 29 | fprintf(stderr, |
| 30 | "Note: parameter '%s' in function '%s' has default value " |
| 31 | "will not support\n", |
| 32 | ParamName.str().c_str(), |
| 33 | Name.str().c_str()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 34 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 35 | // Check type |
| 36 | RSExportType *PET = RSExportType::CreateFromDecl(Context, PVD); |
| 37 | if (PET != NULL) { |
| 38 | F->mParams.push_back(new Parameter(PET, ParamName)); |
| 39 | } else { |
| 40 | fprintf(stderr, "Note: parameter '%s' in function '%s' uses unsupported " |
| 41 | "type\n", ParamName.str().c_str(), Name.str().c_str()); |
| 42 | delete F; |
| 43 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 44 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 45 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 46 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 47 | return F; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | const RSExportRecordType *RSExportFunc::getParamPacketType() const { |
| 51 | // Pack parameters |
| 52 | if ((mParamPacketType == NULL) && hasParam()) { |
| 53 | int Index = 0; |
| 54 | RSExportRecordType *ParamPacketType = |
| 55 | new RSExportRecordType(mContext, |
| 56 | "", |
| 57 | /* IsPacked = */false, |
| 58 | /* IsArtificial = */true); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 59 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 60 | for (const_param_iterator PI = params_begin(), |
| 61 | PE = params_end(); |
| 62 | PI != PE; |
| 63 | PI++, Index++) { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 64 | // For-Loop's body should be: |
| 65 | const RSExportFunc::Parameter *P = *PI; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 66 | std::string nam = P->getName(); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 67 | const RSExportType *typ = P->getType(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 68 | std::string typNam = typ->getName(); |
| 69 | // If (type conversion is needed) |
| 70 | if (typNam.find("rs_") == 0) { |
| 71 | // P's type set to [1 x i32]; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 72 | RSExportConstantArrayType *ECT = new RSExportConstantArrayType |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 73 | (mContext, "addObj", |
| 74 | RSExportPrimitiveType::DataTypeSigned32, |
| 75 | RSExportPrimitiveType::DataKindUser, |
| 76 | false, |
| 77 | 1); |
| 78 | ParamPacketType->mFields.push_back( |
| 79 | new RSExportRecordType::Field(ECT, |
| 80 | nam, |
| 81 | ParamPacketType, |
| 82 | Index) ); |
| 83 | } else { |
| 84 | ParamPacketType->mFields.push_back( |
| 85 | new RSExportRecordType::Field(P->getType(), |
| 86 | nam, |
| 87 | ParamPacketType, |
| 88 | Index) ); |
| 89 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 92 | |
| 93 | |
| 94 | ParamPacketType->AllocSize = |
| 95 | mContext->getTargetData()->getTypeAllocSize( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 96 | ParamPacketType->getLLVMType()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 97 | |
| 98 | mParamPacketType = ParamPacketType; |
| 99 | } |
| 100 | |
| 101 | return mParamPacketType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | RSExportFunc::~RSExportFunc() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | for (const_param_iterator PI = params_begin(), |
| 106 | PE = params_end(); |
| 107 | PI != params_end(); |
| 108 | PI++) |
| 109 | delete *PI; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 110 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 111 | if (mParamPacketType != NULL) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 112 | delete mParamPacketType; |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 113 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 114 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 115 | } |