NCDInterpProcess.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/split_string.h>
  35. #include <misc/hashfun.h>
  36. #include <misc/maxalign.h>
  37. #include <misc/strdup.h>
  38. #include <base/BLog.h>
  39. #include "NCDInterpProcess.h"
  40. #include <generated/blog_channel_ncd.h>
  41. #include "NCDInterpProcess_trie.h"
  42. #include <structure/CStringTrie_impl.h>
  43. static int compute_prealloc (NCDInterpProcess *o)
  44. {
  45. int size = 0;
  46. for (int i = 0; i < o->num_stmts; i++) {
  47. int mod = size % BMAX_ALIGN;
  48. int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
  49. if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
  50. return 0;
  51. }
  52. o->stmts[i].prealloc_offset = size + align_size;
  53. size += align_size + o->stmts[i].alloc_size;
  54. }
  55. ASSERT(size >= 0)
  56. o->prealloc_size = size;
  57. return 1;
  58. }
  59. static int convert_value_recurser (NCDPlaceholderDb *pdb, NCDValue *value, NCDValMem *mem, NCDValRef *out)
  60. {
  61. ASSERT(pdb)
  62. ASSERT((NCDValue_Type(value), 1))
  63. ASSERT(mem)
  64. ASSERT(out)
  65. switch (NCDValue_Type(value)) {
  66. case NCDVALUE_STRING: {
  67. *out = NCDVal_NewStringBin(mem, (const uint8_t *)NCDValue_StringValue(value), NCDValue_StringLength(value));
  68. if (NCDVal_IsInvalid(*out)) {
  69. goto fail;
  70. }
  71. } break;
  72. case NCDVALUE_LIST: {
  73. *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
  74. if (NCDVal_IsInvalid(*out)) {
  75. goto fail;
  76. }
  77. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  78. NCDValRef vval;
  79. if (!convert_value_recurser(pdb, e, mem, &vval)) {
  80. goto fail;
  81. }
  82. NCDVal_ListAppend(*out, vval);
  83. }
  84. } break;
  85. case NCDVALUE_MAP: {
  86. *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
  87. if (NCDVal_IsInvalid(*out)) {
  88. goto fail;
  89. }
  90. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  91. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  92. NCDValRef vkey;
  93. NCDValRef vval;
  94. if (!convert_value_recurser(pdb, ekey, mem, &vkey) ||
  95. !convert_value_recurser(pdb, eval, mem, &vval)
  96. ) {
  97. goto fail;
  98. }
  99. int res = NCDVal_MapInsert(*out, vkey, vval);
  100. ASSERT_EXECUTE(res) // we assume different variables get different placeholder ids
  101. }
  102. } break;
  103. case NCDVALUE_VAR: {
  104. int plid;
  105. if (!NCDPlaceholderDb_AddVariable(pdb, NCDValue_VarName(value), &plid)) {
  106. goto fail;
  107. }
  108. if (NCDVAL_MINIDX + plid >= -1) {
  109. goto fail;
  110. }
  111. *out = NCDVal_NewPlaceholder(mem, plid);
  112. } break;
  113. default:
  114. goto fail;
  115. }
  116. return 1;
  117. fail:
  118. return 0;
  119. }
  120. int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
  121. {
  122. ASSERT(process)
  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. if (!NCDInterpProcess__Trie_Init(&o->trie)) {
  137. BLog(BLOG_ERROR, "NCDInterpProcess__Trie_Init failed");
  138. goto fail1;
  139. }
  140. if (!(o->name = b_strdup(NCDProcess_Name(process)))) {
  141. BLog(BLOG_ERROR, "b_strdup failed");
  142. goto fail2;
  143. }
  144. o->num_stmts = 0;
  145. o->prealloc_size = -1;
  146. o->is_template = NCDProcess_IsTemplate(process);
  147. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  148. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  149. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  150. e->name = NULL;
  151. e->cmdname = NULL;
  152. e->objnames = NULL;
  153. e->num_objnames = 0;
  154. e->alloc_size = 0;
  155. if (NCDStatement_Name(s) && !(e->name = b_strdup(NCDStatement_Name(s)))) {
  156. BLog(BLOG_ERROR, "b_strdup failed");
  157. goto loop_fail0;
  158. }
  159. if (!(e->cmdname = b_strdup(NCDStatement_RegCmdName(s)))) {
  160. BLog(BLOG_ERROR, "b_strdup failed");
  161. goto loop_fail0;
  162. }
  163. NCDValMem mem;
  164. NCDValMem_Init(&mem);
  165. NCDValRef val;
  166. if (!convert_value_recurser(pdb, NCDStatement_RegArgs(s), &mem, &val)) {
  167. BLog(BLOG_ERROR, "convert_value_recurser failed");
  168. NCDValMem_Free(&mem);
  169. goto loop_fail0;
  170. }
  171. e->arg_ref = NCDVal_ToSafe(val);
  172. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  173. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  174. NCDValMem_Free(&mem);
  175. goto loop_fail0;
  176. }
  177. if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
  178. BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
  179. NCDValMem_Free(&mem);
  180. goto loop_fail1;
  181. }
  182. if (NCDStatement_RegObjName(s)) {
  183. if (!(e->objnames = b_strdup(NCDStatement_RegObjName(s)))) {
  184. BLog(BLOG_ERROR, "b_strdup failed");
  185. goto loop_fail2;
  186. }
  187. e->num_objnames = split_string_inplace2(e->objnames, '.') + 1;
  188. e->binding.method_name_id = NCDMethodIndex_GetMethodNameId(method_index, NCDStatement_RegCmdName(s));
  189. if (e->binding.method_name_id == -1) {
  190. BLog(BLOG_ERROR, "NCDMethodIndex_GetMethodNameId failed");
  191. goto loop_fail3;
  192. }
  193. } else {
  194. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, NCDStatement_RegCmdName(s));
  195. }
  196. if (e->name) {
  197. int next_idx = NCDInterpProcess__Trie_Get(&o->trie, e->name);
  198. ASSERT(next_idx >= -1)
  199. ASSERT(next_idx < o->num_stmts)
  200. e->trie_next = next_idx;
  201. if (!NCDInterpProcess__Trie_Set(&o->trie, e->name, o->num_stmts)) {
  202. BLog(BLOG_ERROR, "NCDInterpProcess__Trie_Set failed");
  203. goto loop_fail3;
  204. }
  205. }
  206. o->num_stmts++;
  207. continue;
  208. loop_fail3:
  209. free(e->objnames);
  210. loop_fail2:
  211. BFree(e->arg_data);
  212. loop_fail1:
  213. NCDValReplaceProg_Free(&e->arg_prog);
  214. loop_fail0:
  215. free(e->cmdname);
  216. free(e->name);
  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. free(e->objnames);
  226. BFree(e->arg_data);
  227. NCDValReplaceProg_Free(&e->arg_prog);
  228. free(e->cmdname);
  229. free(e->name);
  230. }
  231. free(o->name);
  232. fail2:
  233. NCDInterpProcess__Trie_Free(&o->trie);
  234. fail1:
  235. BFree(o->stmts);
  236. fail0:
  237. return 0;
  238. }
  239. void NCDInterpProcess_Free (NCDInterpProcess *o)
  240. {
  241. DebugObject_Free(&o->d_obj);
  242. while (o->num_stmts-- > 0) {
  243. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  244. free(e->objnames);
  245. BFree(e->arg_data);
  246. NCDValReplaceProg_Free(&e->arg_prog);
  247. free(e->cmdname);
  248. free(e->name);
  249. }
  250. free(o->name);
  251. NCDInterpProcess__Trie_Free(&o->trie);
  252. BFree(o->stmts);
  253. }
  254. int NCDInterpProcess_FindStatement (NCDInterpProcess *o, int from_index, const char *name)
  255. {
  256. DebugObject_Access(&o->d_obj);
  257. ASSERT(from_index >= 0)
  258. ASSERT(from_index <= o->num_stmts)
  259. ASSERT(name)
  260. int stmt_idx = NCDInterpProcess__Trie_Get(&o->trie, name);
  261. ASSERT(stmt_idx >= -1)
  262. ASSERT(stmt_idx < o->num_stmts)
  263. while (stmt_idx >= 0) {
  264. struct NCDInterpProcess__stmt *e = &o->stmts[stmt_idx];
  265. ASSERT(e->name)
  266. if (!strcmp(e->name, name) && stmt_idx < from_index) {
  267. return stmt_idx;
  268. }
  269. stmt_idx = e->trie_next;
  270. ASSERT(stmt_idx >= -1)
  271. ASSERT(stmt_idx < o->num_stmts)
  272. }
  273. return -1;
  274. }
  275. const char * NCDInterpProcess_StatementCmdName (NCDInterpProcess *o, int i)
  276. {
  277. DebugObject_Access(&o->d_obj);
  278. ASSERT(i >= 0)
  279. ASSERT(i < o->num_stmts)
  280. ASSERT(o->stmts[i].cmdname)
  281. return o->stmts[i].cmdname;
  282. }
  283. void NCDInterpProcess_StatementObjNames (NCDInterpProcess *o, int i, const char **out_objnames, size_t *out_num_objnames)
  284. {
  285. DebugObject_Access(&o->d_obj);
  286. ASSERT(i >= 0)
  287. ASSERT(i < o->num_stmts)
  288. ASSERT(out_objnames)
  289. ASSERT(out_num_objnames)
  290. *out_objnames = o->stmts[i].objnames;
  291. *out_num_objnames = o->stmts[i].num_objnames;
  292. }
  293. const struct NCDModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpProcess *o, int i)
  294. {
  295. DebugObject_Access(&o->d_obj);
  296. ASSERT(i >= 0)
  297. ASSERT(i < o->num_stmts)
  298. ASSERT(!o->stmts[i].objnames)
  299. return o->stmts[i].binding.simple_module;
  300. }
  301. const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, const char *obj_type, NCDMethodIndex *method_index)
  302. {
  303. DebugObject_Access(&o->d_obj);
  304. ASSERT(i >= 0)
  305. ASSERT(i < o->num_stmts)
  306. ASSERT(o->stmts[i].objnames)
  307. ASSERT(obj_type)
  308. ASSERT(method_index)
  309. return NCDMethodIndex_GetMethodModule(method_index, obj_type, o->stmts[i].binding.method_name_id);
  310. }
  311. int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  312. {
  313. DebugObject_Access(&o->d_obj);
  314. ASSERT(i >= 0)
  315. ASSERT(i < o->num_stmts)
  316. ASSERT(out_valmem)
  317. ASSERT(out_val)
  318. ASSERT(out_prog)
  319. struct NCDInterpProcess__stmt *e = &o->stmts[i];
  320. if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
  321. return 0;
  322. }
  323. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  324. *out_prog = e->arg_prog;
  325. return 1;
  326. }
  327. void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int alloc_size)
  328. {
  329. DebugObject_Access(&o->d_obj);
  330. ASSERT(i >= 0)
  331. ASSERT(i < o->num_stmts)
  332. ASSERT(alloc_size >= 0)
  333. if (alloc_size > o->stmts[i].alloc_size) {
  334. o->stmts[i].alloc_size = alloc_size;
  335. o->prealloc_size = -1;
  336. }
  337. }
  338. int NCDInterpProcess_PreallocSize (NCDInterpProcess *o)
  339. {
  340. DebugObject_Access(&o->d_obj);
  341. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  342. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  343. return -1;
  344. }
  345. return o->prealloc_size;
  346. }
  347. int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i)
  348. {
  349. DebugObject_Access(&o->d_obj);
  350. ASSERT(i >= 0)
  351. ASSERT(i < o->num_stmts)
  352. ASSERT(o->prealloc_size >= 0)
  353. return o->stmts[i].alloc_size;
  354. }
  355. int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i)
  356. {
  357. DebugObject_Access(&o->d_obj);
  358. ASSERT(i >= 0)
  359. ASSERT(i < o->num_stmts)
  360. ASSERT(o->prealloc_size >= 0)
  361. return o->stmts[i].prealloc_offset;
  362. }
  363. const char * NCDInterpProcess_Name (NCDInterpProcess *o)
  364. {
  365. DebugObject_Access(&o->d_obj);
  366. return o->name;
  367. }
  368. int NCDInterpProcess_IsTemplate (NCDInterpProcess *o)
  369. {
  370. DebugObject_Access(&o->d_obj);
  371. return o->is_template;
  372. }
  373. int NCDInterpProcess_NumStatements (NCDInterpProcess *o)
  374. {
  375. DebugObject_Access(&o->d_obj);
  376. return o->num_stmts;
  377. }