NCDInterpProcess.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /**
  2. * @file NCDInterpProcess.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdint.h>
  30. #include <limits.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/balloc.h>
  34. #include <misc/maxalign.h>
  35. #include <misc/strdup.h>
  36. #include <base/BLog.h>
  37. #include <ncd/make_name_indices.h>
  38. #include "NCDInterpProcess.h"
  39. #include <generated/blog_channel_ncd.h>
  40. static int compute_prealloc (NCDInterpProcess *o)
  41. {
  42. int size = 0;
  43. for (int i = 0; i < o->num_stmts; i++) {
  44. int mod = size % BMAX_ALIGN;
  45. int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
  46. if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
  47. return 0;
  48. }
  49. o->stmts[i].prealloc_offset = size + align_size;
  50. size += align_size + o->stmts[i].alloc_size;
  51. }
  52. ASSERT(size >= 0)
  53. o->prealloc_size = size;
  54. return 1;
  55. }
  56. static int convert_value_recurser (NCDPlaceholderDb *pdb, NCDValue *value, NCDValMem *mem, NCDValRef *out)
  57. {
  58. ASSERT(pdb)
  59. ASSERT((NCDValue_Type(value), 1))
  60. ASSERT(mem)
  61. ASSERT(out)
  62. switch (NCDValue_Type(value)) {
  63. case NCDVALUE_STRING: {
  64. *out = NCDVal_NewStringBin(mem, (const uint8_t *)NCDValue_StringValue(value), NCDValue_StringLength(value));
  65. if (NCDVal_IsInvalid(*out)) {
  66. goto fail;
  67. }
  68. } break;
  69. case NCDVALUE_LIST: {
  70. *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
  71. if (NCDVal_IsInvalid(*out)) {
  72. goto fail;
  73. }
  74. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  75. NCDValRef vval;
  76. if (!convert_value_recurser(pdb, e, mem, &vval)) {
  77. goto fail;
  78. }
  79. NCDVal_ListAppend(*out, vval);
  80. }
  81. } break;
  82. case NCDVALUE_MAP: {
  83. *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
  84. if (NCDVal_IsInvalid(*out)) {
  85. goto fail;
  86. }
  87. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  88. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  89. NCDValRef vkey;
  90. NCDValRef vval;
  91. if (!convert_value_recurser(pdb, ekey, mem, &vkey) ||
  92. !convert_value_recurser(pdb, eval, mem, &vval)
  93. ) {
  94. goto fail;
  95. }
  96. if (!NCDVal_MapInsert(*out, vkey, vval)) {
  97. BLog(BLOG_ERROR, "duplicate key in map");
  98. goto fail;
  99. }
  100. }
  101. } break;
  102. case NCDVALUE_VAR: {
  103. int plid;
  104. if (!NCDPlaceholderDb_AddVariable(pdb, NCDValue_VarName(value), &plid)) {
  105. goto fail;
  106. }
  107. if (NCDVAL_MINIDX + plid >= -1) {
  108. goto fail;
  109. }
  110. *out = NCDVal_NewPlaceholder(mem, plid);
  111. } break;
  112. default:
  113. goto fail;
  114. }
  115. return 1;
  116. fail:
  117. return 0;
  118. }
  119. int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
  120. {
  121. ASSERT(process)
  122. ASSERT(string_index)
  123. ASSERT(pdb)
  124. ASSERT(module_index)
  125. ASSERT(method_index)
  126. NCDBlock *block = NCDProcess_Block(process);
  127. if (NCDBlock_NumStatements(block) > INT_MAX) {
  128. BLog(BLOG_ERROR, "too many statements");
  129. goto fail0;
  130. }
  131. int num_stmts = NCDBlock_NumStatements(block);
  132. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  133. BLog(BLOG_ERROR, "BAllocArray failed");
  134. goto fail0;
  135. }
  136. o->num_hash_buckets = num_stmts;
  137. if (!(o->hash_buckets = BAllocArray(o->num_hash_buckets, sizeof(o->hash_buckets[0])))) {
  138. BLog(BLOG_ERROR, "BAllocArray failed");
  139. goto fail1;
  140. }
  141. for (size_t i = 0; i < o->num_hash_buckets; i++) {
  142. o->hash_buckets[i] = -1;
  143. }
  144. if (!(o->name = b_strdup(NCDProcess_Name(process)))) {
  145. BLog(BLOG_ERROR, "b_strdup failed");
  146. goto fail2;
  147. }
  148. o->num_stmts = 0;
  149. o->prealloc_size = -1;
  150. o->is_template = NCDProcess_IsTemplate(process);
  151. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  152. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  153. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  154. e->name = -1;
  155. e->cmdname = NULL;
  156. e->objnames = NULL;
  157. e->num_objnames = 0;
  158. e->alloc_size = 0;
  159. if (NCDStatement_Name(s)) {
  160. e->name = NCDStringIndex_Get(string_index, NCDStatement_Name(s));
  161. if (e->name < 0) {
  162. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  163. goto loop_fail0;
  164. }
  165. }
  166. if (!(e->cmdname = b_strdup(NCDStatement_RegCmdName(s)))) {
  167. BLog(BLOG_ERROR, "b_strdup failed");
  168. goto loop_fail0;
  169. }
  170. NCDValMem mem;
  171. NCDValMem_Init(&mem);
  172. NCDValRef val;
  173. if (!convert_value_recurser(pdb, NCDStatement_RegArgs(s), &mem, &val)) {
  174. BLog(BLOG_ERROR, "convert_value_recurser failed");
  175. NCDValMem_Free(&mem);
  176. goto loop_fail0;
  177. }
  178. e->arg_ref = NCDVal_ToSafe(val);
  179. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  180. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  181. NCDValMem_Free(&mem);
  182. goto loop_fail0;
  183. }
  184. if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
  185. BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
  186. NCDValMem_Free(&mem);
  187. goto loop_fail1;
  188. }
  189. if (NCDStatement_RegObjName(s)) {
  190. if (!ncd_make_name_indices(string_index, NCDStatement_RegObjName(s), &e->objnames, &e->num_objnames)) {
  191. BLog(BLOG_ERROR, "ncd_make_name_indices failed");
  192. goto loop_fail2;
  193. }
  194. e->binding.method_name_id = NCDMethodIndex_GetMethodNameId(method_index, NCDStatement_RegCmdName(s));
  195. if (e->binding.method_name_id == -1) {
  196. BLog(BLOG_ERROR, "NCDMethodIndex_GetMethodNameId failed");
  197. goto loop_fail3;
  198. }
  199. } else {
  200. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, NCDStatement_RegCmdName(s));
  201. }
  202. if (e->name >= 0) {
  203. size_t bucket_idx = e->name % o->num_hash_buckets;
  204. e->hash_next = o->hash_buckets[bucket_idx];
  205. o->hash_buckets[bucket_idx] = o->num_stmts;
  206. }
  207. o->num_stmts++;
  208. continue;
  209. loop_fail3:
  210. BFree(e->objnames);
  211. loop_fail2:
  212. BFree(e->arg_data);
  213. loop_fail1:
  214. NCDValReplaceProg_Free(&e->arg_prog);
  215. loop_fail0:
  216. free(e->cmdname);
  217. goto fail3;
  218. }
  219. ASSERT(o->num_stmts == num_stmts)
  220. DebugObject_Init(&o->d_obj);
  221. return 1;
  222. fail3:
  223. while (o->num_stmts-- > 0) {
  224. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  225. BFree(e->objnames);
  226. BFree(e->arg_data);
  227. NCDValReplaceProg_Free(&e->arg_prog);
  228. free(e->cmdname);
  229. }
  230. free(o->name);
  231. fail2:
  232. BFree(o->hash_buckets);
  233. fail1:
  234. BFree(o->stmts);
  235. fail0:
  236. return 0;
  237. }
  238. void NCDInterpProcess_Free (NCDInterpProcess *o)
  239. {
  240. DebugObject_Free(&o->d_obj);
  241. while (o->num_stmts-- > 0) {
  242. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  243. BFree(e->objnames);
  244. BFree(e->arg_data);
  245. NCDValReplaceProg_Free(&e->arg_prog);
  246. free(e->cmdname);
  247. }
  248. free(o->name);
  249. BFree(o->hash_buckets);
  250. BFree(o->stmts);
  251. }
  252. int NCDInterpProcess_FindStatement (NCDInterpProcess *o, int from_index, NCD_string_id_t name)
  253. {
  254. DebugObject_Access(&o->d_obj);
  255. ASSERT(from_index >= 0)
  256. ASSERT(from_index <= o->num_stmts)
  257. size_t bucket_idx = name % o->num_hash_buckets;
  258. int stmt_idx = o->hash_buckets[bucket_idx];
  259. ASSERT(stmt_idx >= -1)
  260. ASSERT(stmt_idx < o->num_stmts)
  261. while (stmt_idx >= 0) {
  262. if (stmt_idx < from_index && o->stmts[stmt_idx].name == name) {
  263. return stmt_idx;
  264. }
  265. stmt_idx = o->stmts[stmt_idx].hash_next;
  266. ASSERT(stmt_idx >= -1)
  267. ASSERT(stmt_idx < o->num_stmts)
  268. }
  269. return -1;
  270. }
  271. const char * NCDInterpProcess_StatementCmdName (NCDInterpProcess *o, int i)
  272. {
  273. DebugObject_Access(&o->d_obj);
  274. ASSERT(i >= 0)
  275. ASSERT(i < o->num_stmts)
  276. ASSERT(o->stmts[i].cmdname)
  277. return o->stmts[i].cmdname;
  278. }
  279. void NCDInterpProcess_StatementObjNames (NCDInterpProcess *o, int i, const NCD_string_id_t **out_objnames, size_t *out_num_objnames)
  280. {
  281. DebugObject_Access(&o->d_obj);
  282. ASSERT(i >= 0)
  283. ASSERT(i < o->num_stmts)
  284. ASSERT(out_objnames)
  285. ASSERT(out_num_objnames)
  286. *out_objnames = o->stmts[i].objnames;
  287. *out_num_objnames = o->stmts[i].num_objnames;
  288. }
  289. const struct NCDModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpProcess *o, int i)
  290. {
  291. DebugObject_Access(&o->d_obj);
  292. ASSERT(i >= 0)
  293. ASSERT(i < o->num_stmts)
  294. ASSERT(!o->stmts[i].objnames)
  295. return o->stmts[i].binding.simple_module;
  296. }
  297. const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, const char *obj_type, NCDMethodIndex *method_index)
  298. {
  299. DebugObject_Access(&o->d_obj);
  300. ASSERT(i >= 0)
  301. ASSERT(i < o->num_stmts)
  302. ASSERT(o->stmts[i].objnames)
  303. ASSERT(obj_type)
  304. ASSERT(method_index)
  305. return NCDMethodIndex_GetMethodModule(method_index, obj_type, o->stmts[i].binding.method_name_id);
  306. }
  307. int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  308. {
  309. DebugObject_Access(&o->d_obj);
  310. ASSERT(i >= 0)
  311. ASSERT(i < o->num_stmts)
  312. ASSERT(out_valmem)
  313. ASSERT(out_val)
  314. ASSERT(out_prog)
  315. struct NCDInterpProcess__stmt *e = &o->stmts[i];
  316. if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
  317. return 0;
  318. }
  319. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  320. *out_prog = e->arg_prog;
  321. return 1;
  322. }
  323. void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int alloc_size)
  324. {
  325. DebugObject_Access(&o->d_obj);
  326. ASSERT(i >= 0)
  327. ASSERT(i < o->num_stmts)
  328. ASSERT(alloc_size >= 0)
  329. if (alloc_size > o->stmts[i].alloc_size) {
  330. o->stmts[i].alloc_size = alloc_size;
  331. o->prealloc_size = -1;
  332. }
  333. }
  334. int NCDInterpProcess_PreallocSize (NCDInterpProcess *o)
  335. {
  336. DebugObject_Access(&o->d_obj);
  337. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  338. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  339. return -1;
  340. }
  341. return o->prealloc_size;
  342. }
  343. int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i)
  344. {
  345. DebugObject_Access(&o->d_obj);
  346. ASSERT(i >= 0)
  347. ASSERT(i < o->num_stmts)
  348. ASSERT(o->prealloc_size >= 0)
  349. return o->stmts[i].alloc_size;
  350. }
  351. int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i)
  352. {
  353. DebugObject_Access(&o->d_obj);
  354. ASSERT(i >= 0)
  355. ASSERT(i < o->num_stmts)
  356. ASSERT(o->prealloc_size >= 0)
  357. return o->stmts[i].prealloc_offset;
  358. }
  359. const char * NCDInterpProcess_Name (NCDInterpProcess *o)
  360. {
  361. DebugObject_Access(&o->d_obj);
  362. return o->name;
  363. }
  364. int NCDInterpProcess_IsTemplate (NCDInterpProcess *o)
  365. {
  366. DebugObject_Access(&o->d_obj);
  367. return o->is_template;
  368. }
  369. int NCDInterpProcess_NumStatements (NCDInterpProcess *o)
  370. {
  371. DebugObject_Access(&o->d_obj);
  372. return o->num_stmts;
  373. }