NCDInterpProcess.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/hashfun.h>
  35. #include <misc/maxalign.h>
  36. #include <misc/strdup.h>
  37. #include <base/BLog.h>
  38. #include <ncd/make_name_indices.h>
  39. #include "NCDInterpProcess.h"
  40. #include <generated/blog_channel_ncd.h>
  41. #include "NCDInterpProcess_hash.h"
  42. #include <structure/CHash_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. if (!NCDVal_MapInsert(*out, vkey, vval)) {
  100. BLog(BLOG_ERROR, "duplicate key in map");
  101. goto fail;
  102. }
  103. }
  104. } break;
  105. case NCDVALUE_VAR: {
  106. int plid;
  107. if (!NCDPlaceholderDb_AddVariable(pdb, NCDValue_VarName(value), &plid)) {
  108. goto fail;
  109. }
  110. if (NCDVAL_MINIDX + plid >= -1) {
  111. goto fail;
  112. }
  113. *out = NCDVal_NewPlaceholder(mem, plid);
  114. } break;
  115. default:
  116. goto fail;
  117. }
  118. return 1;
  119. fail:
  120. return 0;
  121. }
  122. int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
  123. {
  124. ASSERT(process)
  125. ASSERT(string_index)
  126. ASSERT(pdb)
  127. ASSERT(module_index)
  128. ASSERT(method_index)
  129. NCDBlock *block = NCDProcess_Block(process);
  130. if (NCDBlock_NumStatements(block) > INT_MAX) {
  131. BLog(BLOG_ERROR, "too many statements");
  132. goto fail0;
  133. }
  134. int num_stmts = NCDBlock_NumStatements(block);
  135. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  136. BLog(BLOG_ERROR, "BAllocArray failed");
  137. goto fail0;
  138. }
  139. if (!NCDInterpProcess__Hash_Init(&o->hash, num_stmts)) {
  140. BLog(BLOG_ERROR, "NCDInterpProcess__Hash_Init failed");
  141. goto fail1;
  142. }
  143. if (!(o->name = b_strdup(NCDProcess_Name(process)))) {
  144. BLog(BLOG_ERROR, "b_strdup failed");
  145. goto fail2;
  146. }
  147. o->num_stmts = 0;
  148. o->prealloc_size = -1;
  149. o->is_template = NCDProcess_IsTemplate(process);
  150. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  151. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  152. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  153. e->name = -1;
  154. e->cmdname = NULL;
  155. e->objnames = NULL;
  156. e->num_objnames = 0;
  157. e->alloc_size = 0;
  158. if (NCDStatement_Name(s)) {
  159. e->name = NCDStringIndex_Get(string_index, NCDStatement_Name(s));
  160. if (e->name < 0) {
  161. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  162. goto loop_fail0;
  163. }
  164. }
  165. if (!(e->cmdname = b_strdup(NCDStatement_RegCmdName(s)))) {
  166. BLog(BLOG_ERROR, "b_strdup failed");
  167. goto loop_fail0;
  168. }
  169. NCDValMem mem;
  170. NCDValMem_Init(&mem);
  171. NCDValRef val;
  172. if (!convert_value_recurser(pdb, NCDStatement_RegArgs(s), &mem, &val)) {
  173. BLog(BLOG_ERROR, "convert_value_recurser failed");
  174. NCDValMem_Free(&mem);
  175. goto loop_fail0;
  176. }
  177. e->arg_ref = NCDVal_ToSafe(val);
  178. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  179. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  180. NCDValMem_Free(&mem);
  181. goto loop_fail0;
  182. }
  183. if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
  184. BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
  185. NCDValMem_Free(&mem);
  186. goto loop_fail1;
  187. }
  188. if (NCDStatement_RegObjName(s)) {
  189. if (!ncd_make_name_indices(string_index, NCDStatement_RegObjName(s), &e->objnames, &e->num_objnames)) {
  190. BLog(BLOG_ERROR, "ncd_make_name_indices failed");
  191. goto loop_fail2;
  192. }
  193. e->binding.method_name_id = NCDMethodIndex_GetMethodNameId(method_index, NCDStatement_RegCmdName(s));
  194. if (e->binding.method_name_id == -1) {
  195. BLog(BLOG_ERROR, "NCDMethodIndex_GetMethodNameId failed");
  196. goto loop_fail3;
  197. }
  198. } else {
  199. e->binding.simple_module = NCDModuleIndex_FindModule(module_index, NCDStatement_RegCmdName(s));
  200. }
  201. if (e->name >= 0) {
  202. NCDInterpProcess__HashRef ref = {e, o->num_stmts};
  203. NCDInterpProcess__Hash_InsertMulti(&o->hash, o->stmts, ref);
  204. }
  205. o->num_stmts++;
  206. continue;
  207. loop_fail3:
  208. BFree(e->objnames);
  209. loop_fail2:
  210. BFree(e->arg_data);
  211. loop_fail1:
  212. NCDValReplaceProg_Free(&e->arg_prog);
  213. loop_fail0:
  214. free(e->cmdname);
  215. goto fail3;
  216. }
  217. ASSERT(o->num_stmts == num_stmts)
  218. DebugObject_Init(&o->d_obj);
  219. return 1;
  220. fail3:
  221. while (o->num_stmts-- > 0) {
  222. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  223. BFree(e->objnames);
  224. BFree(e->arg_data);
  225. NCDValReplaceProg_Free(&e->arg_prog);
  226. free(e->cmdname);
  227. }
  228. free(o->name);
  229. fail2:
  230. NCDInterpProcess__Hash_Free(&o->hash);
  231. fail1:
  232. BFree(o->stmts);
  233. fail0:
  234. return 0;
  235. }
  236. void NCDInterpProcess_Free (NCDInterpProcess *o)
  237. {
  238. DebugObject_Free(&o->d_obj);
  239. while (o->num_stmts-- > 0) {
  240. struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
  241. BFree(e->objnames);
  242. BFree(e->arg_data);
  243. NCDValReplaceProg_Free(&e->arg_prog);
  244. free(e->cmdname);
  245. }
  246. free(o->name);
  247. NCDInterpProcess__Hash_Free(&o->hash);
  248. BFree(o->stmts);
  249. }
  250. int NCDInterpProcess_FindStatement (NCDInterpProcess *o, int from_index, NCD_string_id_t name)
  251. {
  252. DebugObject_Access(&o->d_obj);
  253. ASSERT(from_index >= 0)
  254. ASSERT(from_index <= o->num_stmts)
  255. int stmt_idx = NCDInterpProcess__Hash_Lookup(&o->hash, o->stmts, name).link;
  256. ASSERT(stmt_idx >= -1)
  257. ASSERT(stmt_idx < o->num_stmts)
  258. while (stmt_idx >= 0) {
  259. if (stmt_idx < from_index) {
  260. return stmt_idx;
  261. }
  262. NCDInterpProcess__HashRef ref = {&o->stmts[stmt_idx], stmt_idx};
  263. stmt_idx = NCDInterpProcess__Hash_GetNextEqual(&o->hash, o->stmts, ref).link;
  264. }
  265. return -1;
  266. }
  267. const char * NCDInterpProcess_StatementCmdName (NCDInterpProcess *o, int i)
  268. {
  269. DebugObject_Access(&o->d_obj);
  270. ASSERT(i >= 0)
  271. ASSERT(i < o->num_stmts)
  272. ASSERT(o->stmts[i].cmdname)
  273. return o->stmts[i].cmdname;
  274. }
  275. void NCDInterpProcess_StatementObjNames (NCDInterpProcess *o, int i, const NCD_string_id_t **out_objnames, size_t *out_num_objnames)
  276. {
  277. DebugObject_Access(&o->d_obj);
  278. ASSERT(i >= 0)
  279. ASSERT(i < o->num_stmts)
  280. ASSERT(out_objnames)
  281. ASSERT(out_num_objnames)
  282. *out_objnames = o->stmts[i].objnames;
  283. *out_num_objnames = o->stmts[i].num_objnames;
  284. }
  285. const struct NCDModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpProcess *o, int i)
  286. {
  287. DebugObject_Access(&o->d_obj);
  288. ASSERT(i >= 0)
  289. ASSERT(i < o->num_stmts)
  290. ASSERT(!o->stmts[i].objnames)
  291. return o->stmts[i].binding.simple_module;
  292. }
  293. const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, const char *obj_type, NCDMethodIndex *method_index)
  294. {
  295. DebugObject_Access(&o->d_obj);
  296. ASSERT(i >= 0)
  297. ASSERT(i < o->num_stmts)
  298. ASSERT(o->stmts[i].objnames)
  299. ASSERT(obj_type)
  300. ASSERT(method_index)
  301. return NCDMethodIndex_GetMethodModule(method_index, obj_type, o->stmts[i].binding.method_name_id);
  302. }
  303. int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  304. {
  305. DebugObject_Access(&o->d_obj);
  306. ASSERT(i >= 0)
  307. ASSERT(i < o->num_stmts)
  308. ASSERT(out_valmem)
  309. ASSERT(out_val)
  310. ASSERT(out_prog)
  311. struct NCDInterpProcess__stmt *e = &o->stmts[i];
  312. if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
  313. return 0;
  314. }
  315. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  316. *out_prog = e->arg_prog;
  317. return 1;
  318. }
  319. void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int alloc_size)
  320. {
  321. DebugObject_Access(&o->d_obj);
  322. ASSERT(i >= 0)
  323. ASSERT(i < o->num_stmts)
  324. ASSERT(alloc_size >= 0)
  325. if (alloc_size > o->stmts[i].alloc_size) {
  326. o->stmts[i].alloc_size = alloc_size;
  327. o->prealloc_size = -1;
  328. }
  329. }
  330. int NCDInterpProcess_PreallocSize (NCDInterpProcess *o)
  331. {
  332. DebugObject_Access(&o->d_obj);
  333. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  334. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  335. return -1;
  336. }
  337. return o->prealloc_size;
  338. }
  339. int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i)
  340. {
  341. DebugObject_Access(&o->d_obj);
  342. ASSERT(i >= 0)
  343. ASSERT(i < o->num_stmts)
  344. ASSERT(o->prealloc_size >= 0)
  345. return o->stmts[i].alloc_size;
  346. }
  347. int NCDInterpProcess_StatementPreallocOffset (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].prealloc_offset;
  354. }
  355. const char * NCDInterpProcess_Name (NCDInterpProcess *o)
  356. {
  357. DebugObject_Access(&o->d_obj);
  358. return o->name;
  359. }
  360. int NCDInterpProcess_IsTemplate (NCDInterpProcess *o)
  361. {
  362. DebugObject_Access(&o->d_obj);
  363. return o->is_template;
  364. }
  365. int NCDInterpProcess_NumStatements (NCDInterpProcess *o)
  366. {
  367. DebugObject_Access(&o->d_obj);
  368. return o->num_stmts;
  369. }