NCDInterpBlock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * @file NCDInterpBlock.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 "NCDInterpBlock.h"
  40. #include <generated/blog_channel_ncd.h>
  41. #include "NCDInterpBlock_trie.h"
  42. #include <structure/CStringTrie_impl.h>
  43. static int compute_prealloc (NCDInterpBlock *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(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 NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block, NCDProcess *process, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
  121. {
  122. ASSERT(block)
  123. ASSERT(process)
  124. ASSERT(pdb)
  125. ASSERT(module_index)
  126. ASSERT(method_index)
  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 (!NCDInterpBlock__Trie_Init(&o->trie)) {
  137. BLog(BLOG_ERROR, "BStringTrie_Init failed");
  138. goto fail1;
  139. }
  140. o->num_stmts = 0;
  141. o->prealloc_size = -1;
  142. o->process = process;
  143. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  144. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  145. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  146. e->name = NCDStatement_Name(s);
  147. e->cmdname = NCDStatement_RegCmdName(s);
  148. e->objnames = NULL;
  149. e->num_objnames = 0;
  150. e->alloc_size = 0;
  151. NCDValMem mem;
  152. NCDValMem_Init(&mem);
  153. NCDValRef val;
  154. if (!convert_value_recurser(pdb, NCDStatement_RegArgs(s), &mem, &val)) {
  155. BLog(BLOG_ERROR, "convert_value_recurser failed");
  156. NCDValMem_Free(&mem);
  157. goto loop_fail0;
  158. }
  159. e->arg_ref = NCDVal_ToSafe(val);
  160. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  161. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  162. NCDValMem_Free(&mem);
  163. goto loop_fail0;
  164. }
  165. if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
  166. BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
  167. NCDValMem_Free(&mem);
  168. goto loop_fail1;
  169. }
  170. if (NCDStatement_RegObjName(s)) {
  171. if (!(e->objnames = b_strdup(NCDStatement_RegObjName(s)))) {
  172. BLog(BLOG_ERROR, "b_strdup failed");
  173. goto loop_fail2;
  174. }
  175. e->num_objnames = split_string_inplace2(e->objnames, '.') + 1;
  176. e->binding.method_name_id = NCDMethodIndex_GetMethodNameId(method_index, NCDStatement_RegCmdName(s));
  177. if (e->binding.method_name_id == -1) {
  178. BLog(BLOG_ERROR, "NCDMethodIndex_GetMethodNameId failed");
  179. goto loop_fail3;
  180. }
  181. } else {
  182. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, NCDStatement_RegCmdName(s));
  183. }
  184. if (e->name) {
  185. int next_idx = NCDInterpBlock__Trie_Get(&o->trie, e->name);
  186. ASSERT(next_idx >= -1)
  187. ASSERT(next_idx < o->num_stmts)
  188. e->trie_next = next_idx;
  189. if (!NCDInterpBlock__Trie_Set(&o->trie, e->name, o->num_stmts)) {
  190. BLog(BLOG_ERROR, "NCDInterpBlock__Trie_Set failed");
  191. goto loop_fail3;
  192. }
  193. }
  194. o->num_stmts++;
  195. continue;
  196. loop_fail3:
  197. free(e->objnames);
  198. loop_fail2:
  199. BFree(e->arg_data);
  200. loop_fail1:
  201. NCDValReplaceProg_Free(&e->arg_prog);
  202. loop_fail0:
  203. goto fail2;
  204. }
  205. ASSERT(o->num_stmts == num_stmts)
  206. DebugObject_Init(&o->d_obj);
  207. return 1;
  208. fail2:
  209. while (o->num_stmts-- > 0) {
  210. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  211. free(e->objnames);
  212. BFree(e->arg_data);
  213. NCDValReplaceProg_Free(&e->arg_prog);
  214. }
  215. NCDInterpBlock__Trie_Free(&o->trie);
  216. fail1:
  217. BFree(o->stmts);
  218. fail0:
  219. return 0;
  220. }
  221. void NCDInterpBlock_Free (NCDInterpBlock *o)
  222. {
  223. DebugObject_Free(&o->d_obj);
  224. while (o->num_stmts-- > 0) {
  225. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  226. free(e->objnames);
  227. BFree(e->arg_data);
  228. NCDValReplaceProg_Free(&e->arg_prog);
  229. }
  230. NCDInterpBlock__Trie_Free(&o->trie);
  231. BFree(o->stmts);
  232. }
  233. int NCDInterpBlock_FindStatement (NCDInterpBlock *o, int from_index, const char *name)
  234. {
  235. DebugObject_Access(&o->d_obj);
  236. ASSERT(from_index >= 0)
  237. ASSERT(from_index <= o->num_stmts)
  238. ASSERT(name)
  239. int stmt_idx = NCDInterpBlock__Trie_Get(&o->trie, name);
  240. ASSERT(stmt_idx >= -1)
  241. ASSERT(stmt_idx < o->num_stmts)
  242. while (stmt_idx >= 0) {
  243. struct NCDInterpBlock__stmt *e = &o->stmts[stmt_idx];
  244. if (!strcmp(e->name, name) && stmt_idx < from_index) {
  245. return stmt_idx;
  246. }
  247. stmt_idx = e->trie_next;
  248. ASSERT(stmt_idx >= -1)
  249. ASSERT(stmt_idx < o->num_stmts)
  250. }
  251. return -1;
  252. }
  253. const char * NCDInterpBlock_StatementCmdName (NCDInterpBlock *o, int i)
  254. {
  255. DebugObject_Access(&o->d_obj);
  256. ASSERT(i >= 0)
  257. ASSERT(i < o->num_stmts)
  258. return o->stmts[i].cmdname;
  259. }
  260. void NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i, const char **out_objnames, size_t *out_num_objnames)
  261. {
  262. DebugObject_Access(&o->d_obj);
  263. ASSERT(i >= 0)
  264. ASSERT(i < o->num_stmts)
  265. ASSERT(out_objnames)
  266. ASSERT(out_num_objnames)
  267. *out_objnames = o->stmts[i].objnames;
  268. *out_num_objnames = o->stmts[i].num_objnames;
  269. }
  270. const struct NCDModule * NCDInterpBlock_StatementGetSimpleModule (NCDInterpBlock *o, int i)
  271. {
  272. DebugObject_Access(&o->d_obj);
  273. ASSERT(i >= 0)
  274. ASSERT(i < o->num_stmts)
  275. ASSERT(!o->stmts[i].objnames)
  276. return o->stmts[i].binding.simple_module;
  277. }
  278. const struct NCDModule * NCDInterpBlock_StatementGetMethodModule (NCDInterpBlock *o, int i, const char *obj_type, NCDMethodIndex *method_index)
  279. {
  280. DebugObject_Access(&o->d_obj);
  281. ASSERT(i >= 0)
  282. ASSERT(i < o->num_stmts)
  283. ASSERT(o->stmts[i].objnames)
  284. ASSERT(obj_type)
  285. ASSERT(method_index)
  286. return NCDMethodIndex_GetMethodModule(method_index, obj_type, o->stmts[i].binding.method_name_id);
  287. }
  288. int NCDInterpBlock_CopyStatementArgs (NCDInterpBlock *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  289. {
  290. DebugObject_Access(&o->d_obj);
  291. ASSERT(i >= 0)
  292. ASSERT(i < o->num_stmts)
  293. ASSERT(out_valmem)
  294. ASSERT(out_val)
  295. ASSERT(out_prog)
  296. struct NCDInterpBlock__stmt *e = &o->stmts[i];
  297. if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
  298. return 0;
  299. }
  300. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  301. *out_prog = e->arg_prog;
  302. return 1;
  303. }
  304. void NCDInterpBlock_StatementBumpAllocSize (NCDInterpBlock *o, int i, int alloc_size)
  305. {
  306. DebugObject_Access(&o->d_obj);
  307. ASSERT(i >= 0)
  308. ASSERT(i < o->num_stmts)
  309. ASSERT(alloc_size >= 0)
  310. if (alloc_size > o->stmts[i].alloc_size) {
  311. o->stmts[i].alloc_size = alloc_size;
  312. o->prealloc_size = -1;
  313. }
  314. }
  315. int NCDInterpBlock_StatementPreallocSize (NCDInterpBlock *o, int i)
  316. {
  317. DebugObject_Access(&o->d_obj);
  318. ASSERT(i >= 0)
  319. ASSERT(i < o->num_stmts)
  320. return o->stmts[i].alloc_size;
  321. }
  322. int NCDInterpBlock_PreallocSize (NCDInterpBlock *o)
  323. {
  324. DebugObject_Access(&o->d_obj);
  325. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  326. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  327. return -1;
  328. }
  329. return o->prealloc_size;
  330. }
  331. int NCDInterpBlock_StatementPreallocOffset (NCDInterpBlock *o, int i)
  332. {
  333. DebugObject_Access(&o->d_obj);
  334. ASSERT(i >= 0)
  335. ASSERT(i < o->num_stmts)
  336. ASSERT(o->prealloc_size >= 0)
  337. return o->stmts[i].prealloc_offset;
  338. }
  339. NCDProcess * NCDInterpBlock_Process (NCDInterpBlock *o)
  340. {
  341. DebugObject_Access(&o->d_obj);
  342. return o->process;
  343. }