NCDInterpBlock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <base/BLog.h>
  38. #include "NCDInterpBlock.h"
  39. #include <generated/blog_channel_ncd.h>
  40. #include "NCDInterpBlock_hash.h"
  41. #include <structure/CHash_impl.h>
  42. static int compute_prealloc (NCDInterpBlock *o)
  43. {
  44. int size = 0;
  45. for (int i = 0; i < o->num_stmts; i++) {
  46. int mod = size % BMAX_ALIGN;
  47. int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
  48. if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
  49. return 0;
  50. }
  51. o->stmts[i].prealloc_offset = size + align_size;
  52. size += align_size + o->stmts[i].alloc_size;
  53. }
  54. ASSERT(size >= 0)
  55. o->prealloc_size = size;
  56. return 1;
  57. }
  58. int convert_value_recurser (NCDPlaceholderDb *pdb, NCDValue *value, NCDValMem *mem, NCDValRef *out)
  59. {
  60. ASSERT(pdb)
  61. ASSERT((NCDValue_Type(value), 1))
  62. ASSERT(mem)
  63. ASSERT(out)
  64. switch (NCDValue_Type(value)) {
  65. case NCDVALUE_STRING: {
  66. *out = NCDVal_NewStringBin(mem, (const uint8_t *)NCDValue_StringValue(value), NCDValue_StringLength(value));
  67. if (NCDVal_IsInvalid(*out)) {
  68. goto fail;
  69. }
  70. } break;
  71. case NCDVALUE_LIST: {
  72. *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
  73. if (NCDVal_IsInvalid(*out)) {
  74. goto fail;
  75. }
  76. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  77. NCDValRef vval;
  78. if (!convert_value_recurser(pdb, e, mem, &vval)) {
  79. goto fail;
  80. }
  81. NCDVal_ListAppend(*out, vval);
  82. }
  83. } break;
  84. case NCDVALUE_MAP: {
  85. *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
  86. if (NCDVal_IsInvalid(*out)) {
  87. goto fail;
  88. }
  89. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  90. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  91. NCDValRef vkey;
  92. NCDValRef vval;
  93. if (!convert_value_recurser(pdb, ekey, mem, &vkey) ||
  94. !convert_value_recurser(pdb, eval, mem, &vval)
  95. ) {
  96. goto fail;
  97. }
  98. int res = NCDVal_MapInsert(*out, vkey, vval);
  99. ASSERT(res) // we assume different variables get different placeholder ids
  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 NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block, NCDProcess *process, NCDPlaceholderDb *pdb)
  120. {
  121. ASSERT(block)
  122. ASSERT(process)
  123. ASSERT(pdb)
  124. if (NCDBlock_NumStatements(block) > INT_MAX) {
  125. BLog(BLOG_ERROR, "too many statements");
  126. goto fail0;
  127. }
  128. int num_stmts = NCDBlock_NumStatements(block);
  129. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  130. BLog(BLOG_ERROR, "BAllocArray failed");
  131. goto fail0;
  132. }
  133. if (!NCDInterpBlock__Hash_Init(&o->hash, num_stmts)) {
  134. BLog(BLOG_ERROR, "NCDInterpBlock__Hash_Init failed");
  135. goto fail1;
  136. }
  137. o->num_stmts = 0;
  138. o->prealloc_size = -1;
  139. o->process = process;
  140. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  141. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  142. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  143. e->name = NCDStatement_Name(s);
  144. e->cmdname = NCDStatement_RegCmdName(s);
  145. e->objnames = NULL;
  146. e->alloc_size = 0;
  147. NCDValMem mem;
  148. NCDValMem_Init(&mem);
  149. NCDValRef val;
  150. if (!convert_value_recurser(pdb, NCDStatement_RegArgs(s), &mem, &val)) {
  151. BLog(BLOG_ERROR, "convert_value_recurser failed");
  152. NCDValMem_Free(&mem);
  153. goto loop_fail0;
  154. }
  155. e->arg_ref = NCDVal_ToSafe(val);
  156. if (!NCDValReplaceProg_Init(&e->arg_prog, val)) {
  157. BLog(BLOG_ERROR, "NCDValReplaceProg_Init failed");
  158. NCDValMem_Free(&mem);
  159. goto loop_fail0;
  160. }
  161. if (!NCDValMem_FreeExport(&mem, &e->arg_data, &e->arg_len)) {
  162. BLog(BLOG_ERROR, "NCDValMem_FreeExport failed");
  163. NCDValMem_Free(&mem);
  164. goto loop_fail1;
  165. }
  166. if (NCDStatement_RegObjName(s) && !(e->objnames = split_string(NCDStatement_RegObjName(s), '.'))) {
  167. BLog(BLOG_ERROR, "split_string failed");
  168. goto loop_fail2;
  169. }
  170. if (e->name) {
  171. NCDInterpBlock__HashRef ref = {e, o->num_stmts};
  172. NCDInterpBlock__Hash_InsertMulti(&o->hash, o->stmts, ref);
  173. }
  174. o->num_stmts++;
  175. continue;
  176. loop_fail2:
  177. BFree(e->arg_data);
  178. loop_fail1:
  179. NCDValReplaceProg_Free(&e->arg_prog);
  180. loop_fail0:
  181. goto fail2;
  182. }
  183. ASSERT(o->num_stmts == num_stmts)
  184. DebugObject_Init(&o->d_obj);
  185. return 1;
  186. fail2:
  187. while (o->num_stmts-- > 0) {
  188. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  189. if (e->objnames) {
  190. free_strings(e->objnames);
  191. }
  192. BFree(e->arg_data);
  193. NCDValReplaceProg_Free(&e->arg_prog);
  194. }
  195. fail1:
  196. BFree(o->stmts);
  197. fail0:
  198. return 0;
  199. }
  200. void NCDInterpBlock_Free (NCDInterpBlock *o)
  201. {
  202. DebugObject_Free(&o->d_obj);
  203. while (o->num_stmts-- > 0) {
  204. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  205. if (e->objnames) {
  206. free_strings(e->objnames);
  207. }
  208. BFree(e->arg_data);
  209. NCDValReplaceProg_Free(&e->arg_prog);
  210. }
  211. NCDInterpBlock__Hash_Free(&o->hash);
  212. BFree(o->stmts);
  213. }
  214. int NCDInterpBlock_FindStatement (NCDInterpBlock *o, int from_index, const char *name)
  215. {
  216. DebugObject_Access(&o->d_obj);
  217. ASSERT(from_index >= 0)
  218. ASSERT(from_index <= o->num_stmts)
  219. ASSERT(name)
  220. // We rely on that we get matching statements here in reverse order of insertion,
  221. // to properly return the greatest matching statement lesser than from_index.
  222. NCDInterpBlock__HashRef ref = NCDInterpBlock__Hash_Lookup(&o->hash, o->stmts, name);
  223. while (ref.link != NCDInterpBlock__HashNullLink()) {
  224. ASSERT(ref.link >= 0)
  225. ASSERT(ref.link < o->num_stmts)
  226. ASSERT(ref.ptr == &o->stmts[ref.link])
  227. ASSERT(!strcmp(ref.ptr->name, name))
  228. if (ref.link < from_index) {
  229. return ref.link;
  230. }
  231. ref = NCDInterpBlock__Hash_GetNextEqual(&o->hash, o->stmts, ref);
  232. }
  233. return -1;
  234. }
  235. const char * NCDInterpBlock_StatementCmdName (NCDInterpBlock *o, int i)
  236. {
  237. DebugObject_Access(&o->d_obj);
  238. ASSERT(i >= 0)
  239. ASSERT(i < o->num_stmts)
  240. return o->stmts[i].cmdname;
  241. }
  242. char ** NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i)
  243. {
  244. DebugObject_Access(&o->d_obj);
  245. ASSERT(i >= 0)
  246. ASSERT(i < o->num_stmts)
  247. return o->stmts[i].objnames;
  248. }
  249. int NCDInterpBlock_CopyStatementArgs (NCDInterpBlock *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)
  250. {
  251. DebugObject_Access(&o->d_obj);
  252. ASSERT(i >= 0)
  253. ASSERT(i < o->num_stmts)
  254. ASSERT(out_valmem)
  255. ASSERT(out_val)
  256. ASSERT(out_prog)
  257. struct NCDInterpBlock__stmt *e = &o->stmts[i];
  258. if (!NCDValMem_InitImport(out_valmem, e->arg_data, e->arg_len)) {
  259. return 0;
  260. }
  261. *out_val = NCDVal_FromSafe(out_valmem, e->arg_ref);
  262. *out_prog = e->arg_prog;
  263. return 1;
  264. }
  265. void NCDInterpBlock_StatementBumpAllocSize (NCDInterpBlock *o, int i, int alloc_size)
  266. {
  267. DebugObject_Access(&o->d_obj);
  268. ASSERT(i >= 0)
  269. ASSERT(i < o->num_stmts)
  270. ASSERT(alloc_size >= 0)
  271. if (alloc_size > o->stmts[i].alloc_size) {
  272. o->stmts[i].alloc_size = alloc_size;
  273. o->prealloc_size = -1;
  274. }
  275. }
  276. int NCDInterpBlock_StatementPreallocSize (NCDInterpBlock *o, int i)
  277. {
  278. DebugObject_Access(&o->d_obj);
  279. ASSERT(i >= 0)
  280. ASSERT(i < o->num_stmts)
  281. return o->stmts[i].alloc_size;
  282. }
  283. int NCDInterpBlock_PreallocSize (NCDInterpBlock *o)
  284. {
  285. DebugObject_Access(&o->d_obj);
  286. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  287. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  288. return -1;
  289. }
  290. return o->prealloc_size;
  291. }
  292. int NCDInterpBlock_StatementPreallocOffset (NCDInterpBlock *o, int i)
  293. {
  294. DebugObject_Access(&o->d_obj);
  295. ASSERT(i >= 0)
  296. ASSERT(i < o->num_stmts)
  297. ASSERT(o->prealloc_size >= 0)
  298. return o->stmts[i].prealloc_offset;
  299. }
  300. NCDProcess * NCDInterpBlock_Process (NCDInterpBlock *o)
  301. {
  302. DebugObject_Access(&o->d_obj);
  303. return o->process;
  304. }