zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1 | #include "slang_rs_reflect_utils.h" |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 2 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 3 | #include <cstdio> |
Mike Lockwood | b1980a2 | 2010-09-08 10:20:40 -0400 | [diff] [blame] | 4 | #include <cstring> |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 5 | |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame^] | 6 | #include "llvm/ADT/StringRef.h" |
| 7 | |
| 8 | #include "slang_utils.h" |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 9 | |
| 10 | namespace slang { |
| 11 | |
| 12 | using std::string; |
| 13 | |
Ying Wang | dba3111 | 2010-10-07 17:30:38 -0700 | [diff] [blame] | 14 | string RSSlangReflectUtils::GetFileNameStem(const char* fileName) { |
| 15 | const char *dot = fileName + strlen(fileName); |
| 16 | const char *slash = dot - 1; |
| 17 | while (slash >= fileName) { |
| 18 | if (*slash == '/') { |
| 19 | break; |
| 20 | } |
| 21 | if ((*slash == '.') && (*dot == 0)) { |
| 22 | dot = slash; |
| 23 | } |
| 24 | --slash; |
| 25 | } |
| 26 | ++slash; |
| 27 | return string(slash, dot - slash); |
| 28 | } |
| 29 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 30 | string RSSlangReflectUtils::ComputePackagedPath( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 31 | const char *prefixPath, const char *packageName) { |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 32 | string packaged_path(prefixPath); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 33 | if (!packaged_path.empty() && |
| 34 | (packaged_path[packaged_path.length() - 1] != '/')) { |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 35 | packaged_path += "/"; |
| 36 | } |
| 37 | size_t s = packaged_path.length(); |
| 38 | packaged_path += packageName; |
| 39 | while (s < packaged_path.length()) { |
| 40 | if (packaged_path[s] == '.') { |
| 41 | packaged_path[s] = '/'; |
| 42 | } |
| 43 | ++s; |
| 44 | } |
| 45 | return packaged_path; |
| 46 | } |
| 47 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 48 | static string InternalFileNameConvert(const char *rsFileName, bool toLower) { |
| 49 | const char *dot = rsFileName + strlen(rsFileName); |
| 50 | const char *slash = dot - 1; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 51 | while (slash >= rsFileName) { |
| 52 | if (*slash == '/') { |
| 53 | break; |
| 54 | } |
| 55 | if ((*slash == '.') && (*dot == 0)) { |
| 56 | dot = slash; |
| 57 | } |
| 58 | --slash; |
| 59 | } |
| 60 | ++slash; |
| 61 | char ret[256]; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 62 | int i = 0; |
| 63 | for (; (i < 255) && (slash < dot); ++slash) { |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 64 | if (isalnum(*slash) || *slash == '_') { |
| 65 | if (toLower) { |
| 66 | ret[i] = tolower(*slash); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 67 | } else { |
| 68 | ret[i] = *slash; |
| 69 | } |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 70 | ++i; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | ret[i] = 0; |
| 74 | return string(ret); |
| 75 | } |
| 76 | |
| 77 | std::string RSSlangReflectUtils::JavaClassNameFromRSFileName( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 78 | const char *rsFileName) { |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 79 | return InternalFileNameConvert(rsFileName, false); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | std::string RSSlangReflectUtils::BCFileNameFromRSFileName( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 84 | const char *rsFileName) { |
Stephen Hines | d417640 | 2010-09-16 17:14:35 -0700 | [diff] [blame] | 85 | return InternalFileNameConvert(rsFileName, true); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 88 | static bool GenerateAccessorHeader( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 89 | const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 90 | fprintf(pfout, "/*\n"); |
| 91 | fprintf(pfout, " * This file is auto-generated. DO NOT MODIFY!\n"); |
| 92 | fprintf(pfout, " * The source RenderScript file: %s\n", context.rsFileName); |
| 93 | fprintf(pfout, " */\n\n"); |
| 94 | fprintf(pfout, "package %s;\n\n", context.packageName); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 95 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 96 | // add imports here. |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | static bool GenerateAccessorMethodSignature( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 102 | const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 103 | // the prototype of the accessor method |
| 104 | fprintf(pfout, " // return byte array representation of the bitcode.\n"); |
| 105 | fprintf(pfout, " public static byte[] getBitCode() {\n"); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | // Java method size must not exceed 64k, |
| 110 | // so we have to split the bitcode into multiple segments. |
| 111 | static bool GenerateSegmentMethod( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 112 | const char *buff, int blen, int seg_num, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 113 | |
| 114 | fprintf(pfout, " private static byte[] getSegment_%d() {\n", seg_num); |
| 115 | fprintf(pfout, " byte[] data = {\n"); |
| 116 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 117 | static const int LINE_BYTE_NUM = 16; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 118 | char out_line[LINE_BYTE_NUM*6 + 10]; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 119 | const char *out_line_end = out_line + sizeof(out_line); |
| 120 | char *p = out_line; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 121 | |
| 122 | int write_length = 0; |
| 123 | while (write_length < blen) { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 124 | p += snprintf(p, out_line_end - p, |
| 125 | " %4d,", static_cast<int>(buff[write_length])); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 126 | ++write_length; |
| 127 | if (((write_length % LINE_BYTE_NUM) == 0) |
| 128 | || (write_length == blen)) { |
| 129 | fprintf(pfout, " "); |
Stephen Hines | 24f9b09 | 2010-10-06 12:58:40 -0700 | [diff] [blame] | 130 | fprintf(pfout, "%s", out_line); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 131 | fprintf(pfout, "\n"); |
| 132 | p = out_line; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fprintf(pfout, " };\n"); |
| 137 | fprintf(pfout, " return data;\n"); |
| 138 | fprintf(pfout, " }\n\n"); |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | static bool GenerateJavaCodeAccessorMethod( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 144 | const RSSlangReflectUtils::BitCodeAccessorContext &context, FILE *pfout) { |
| 145 | FILE *pfin = fopen(context.bcFileName, "rb"); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 146 | if (pfin == NULL) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 147 | fprintf(stderr, "Error: could not read file %s\n", context.bcFileName); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 151 | // start the accessor method |
| 152 | GenerateAccessorMethodSignature(context, pfout); |
| 153 | fprintf(pfout, " return getBitCodeInternal();\n"); |
| 154 | // end the accessor method |
| 155 | fprintf(pfout, " };\n\n"); |
| 156 | |
| 157 | // output the data |
| 158 | // make sure the generated function for a segment won't break the Javac |
| 159 | // size limitation (64K). |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 160 | static const int SEG_SIZE = 0x2000; |
| 161 | char *buff = new char[SEG_SIZE]; |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 162 | int read_length; |
| 163 | int seg_num = 0; |
| 164 | int total_length = 0; |
| 165 | while ((read_length = fread(buff, 1, SEG_SIZE, pfin)) > 0) { |
| 166 | GenerateSegmentMethod(buff, read_length, seg_num, pfout); |
| 167 | ++seg_num; |
| 168 | total_length += read_length; |
| 169 | } |
| 170 | delete []buff; |
| 171 | fclose(pfin); |
| 172 | |
| 173 | // output the internal accessor method |
| 174 | fprintf(pfout, " private static int bitCodeLength = %d;\n\n", total_length); |
| 175 | fprintf(pfout, " private static byte[] getBitCodeInternal() {\n"); |
| 176 | fprintf(pfout, " byte[] bc = new byte[bitCodeLength];\n"); |
| 177 | fprintf(pfout, " int offset = 0;\n"); |
| 178 | fprintf(pfout, " byte[] seg;\n"); |
| 179 | for (int i = 0; i < seg_num; ++i) { |
| 180 | fprintf(pfout, " seg = getSegment_%d();\n", i); |
| 181 | fprintf(pfout, " System.arraycopy(seg, 0, bc, offset, seg.length);\n"); |
| 182 | fprintf(pfout, " offset += seg.length;\n"); |
| 183 | } |
| 184 | fprintf(pfout, " return bc;\n"); |
| 185 | fprintf(pfout, " }\n\n"); |
| 186 | |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | static bool GenerateAccessorClass( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 191 | const RSSlangReflectUtils::BitCodeAccessorContext &context, |
| 192 | const char *clazz_name, FILE *pfout) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 193 | // begin the class. |
| 194 | fprintf(pfout, "/**\n"); |
| 195 | fprintf(pfout, " * @hide\n"); |
| 196 | fprintf(pfout, " */\n"); |
| 197 | fprintf(pfout, "public class %s {\n", clazz_name); |
| 198 | fprintf(pfout, "\n"); |
| 199 | |
| 200 | bool ret = true; |
| 201 | switch (context.bcStorage) { |
| 202 | case BCST_APK_RESOURCE: |
| 203 | break; |
| 204 | case BCST_JAVA_CODE: |
| 205 | ret = GenerateJavaCodeAccessorMethod(context, pfout); |
| 206 | break; |
| 207 | default: |
| 208 | ret = false; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 211 | // end the class. |
| 212 | fprintf(pfout, "}\n"); |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | bool RSSlangReflectUtils::GenerateBitCodeAccessor( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 219 | const BitCodeAccessorContext &context) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 220 | string output_path = ComputePackagedPath(context.reflectPath, |
| 221 | context.packageName); |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame^] | 222 | if (!SlangUtils::CreateDirectoryWithParents(llvm::StringRef(output_path), |
| 223 | NULL)) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 224 | fprintf(stderr, "Error: could not create dir %s\n", |
| 225 | output_path.c_str()); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | string clazz_name(JavaClassNameFromRSFileName(context.rsFileName)); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 230 | clazz_name += "BitCode"; |
| 231 | string filename(clazz_name); |
| 232 | filename += ".java"; |
| 233 | |
| 234 | string output_filename(output_path); |
| 235 | output_filename += "/"; |
| 236 | output_filename += filename; |
| 237 | printf("Generating %s ...\n", filename.c_str()); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 238 | FILE *pfout = fopen(output_filename.c_str(), "w"); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 239 | if (pfout == NULL) { |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 240 | fprintf(stderr, "Error: could not write to file %s\n", |
| 241 | output_filename.c_str()); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 245 | bool ret = GenerateAccessorHeader(context, pfout) && |
| 246 | GenerateAccessorClass(context, clazz_name.c_str(), pfout); |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 247 | |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 248 | fclose(pfout); |
Ying Wang | 0877f05 | 2010-09-09 17:19:33 -0700 | [diff] [blame] | 249 | return ret; |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 250 | } |
Ying Wang | 3f8b44d | 2010-09-04 01:17:01 -0700 | [diff] [blame] | 251 | } |