NCDSugar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * @file NCDSugar.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 <stdlib.h>
  30. #include <misc/debug.h>
  31. #include "NCDSugar.h"
  32. struct desugar_state {
  33. NCDProgram *prog;
  34. size_t template_name_ctr;
  35. };
  36. static int add_template (struct desugar_state *state, NCDBlock block, NCDValue *out_name_val);
  37. static int desugar_block (struct desugar_state *state, NCDBlock *block);
  38. static int desugar_if (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next);
  39. static int desugar_do (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next);
  40. static int desugar_foreach (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next);
  41. static int desugar_blockstmt (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next);
  42. static int add_template (struct desugar_state *state, NCDBlock block, NCDValue *out_name_val)
  43. {
  44. char name[40];
  45. snprintf(name, sizeof(name), "__tmpl%zu", state->template_name_ctr);
  46. state->template_name_ctr++;
  47. if (!desugar_block(state, &block)) {
  48. NCDBlock_Free(&block);
  49. return 0;
  50. }
  51. NCDProcess proc_tmp;
  52. if (!NCDProcess_Init(&proc_tmp, 1, name, block)) {
  53. NCDBlock_Free(&block);
  54. return 0;
  55. }
  56. NCDProgramElem elem;
  57. NCDProgramElem_InitProcess(&elem, proc_tmp);
  58. if (!NCDProgram_PrependElem(state->prog, elem)) {
  59. NCDProgramElem_Free(&elem);
  60. return 0;
  61. }
  62. if (!NCDValue_InitString(out_name_val, name)) {
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. static int desugar_block (struct desugar_state *state, NCDBlock *block)
  68. {
  69. NCDStatement *stmt = NCDBlock_FirstStatement(block);
  70. while (stmt) {
  71. switch (NCDStatement_Type(stmt)) {
  72. case NCDSTATEMENT_REG: {
  73. stmt = NCDBlock_NextStatement(block, stmt);
  74. } break;
  75. case NCDSTATEMENT_IF: {
  76. int iftype = NCDStatement_IfType(stmt);
  77. int res = 0;
  78. if (iftype == NCDIFTYPE_IF) {
  79. res = desugar_if(state, block, stmt, &stmt);
  80. } else if (iftype == NCDIFTYPE_DO) {
  81. res = desugar_do(state, block, stmt, &stmt);
  82. }
  83. if (!res) {
  84. return 0;
  85. }
  86. } break;
  87. case NCDSTATEMENT_FOREACH: {
  88. if (!desugar_foreach(state, block, stmt, &stmt)) {
  89. return 0;
  90. }
  91. } break;
  92. case NCDSTATEMENT_BLOCK: {
  93. if (!desugar_blockstmt(state, block, stmt, &stmt)) {
  94. return 0;
  95. }
  96. } break;
  97. default: {
  98. return 0;
  99. } break;
  100. }
  101. }
  102. return 1;
  103. }
  104. static int desugar_if (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  105. {
  106. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_IF)
  107. ASSERT(NCDStatement_IfType(stmt) == NCDIFTYPE_IF)
  108. NCDValue args;
  109. NCDValue_InitList(&args);
  110. NCDIfBlock *ifblock = NCDStatement_IfBlock(stmt);
  111. while (NCDIfBlock_FirstIf(ifblock)) {
  112. NCDIf ifc = NCDIfBlock_GrabIf(ifblock, NCDIfBlock_FirstIf(ifblock));
  113. NCDValue if_cond;
  114. NCDBlock if_block;
  115. NCDIf_FreeGrab(&ifc, &if_cond, &if_block);
  116. if (!NCDValue_ListAppend(&args, if_cond)) {
  117. NCDValue_Free(&if_cond);
  118. NCDBlock_Free(&if_block);
  119. goto fail_args;
  120. }
  121. NCDValue action_arg;
  122. if (!add_template(state, if_block, &action_arg)) {
  123. goto fail_args;
  124. }
  125. if (!NCDValue_ListAppend(&args, action_arg)) {
  126. NCDValue_Free(&action_arg);
  127. goto fail_args;
  128. }
  129. }
  130. NCDValue action_arg;
  131. if (NCDStatement_IfElse(stmt)) {
  132. NCDBlock else_block = NCDStatement_IfGrabElse(stmt);
  133. if (!add_template(state, else_block, &action_arg)) {
  134. goto fail_args;
  135. }
  136. } else {
  137. if (!NCDValue_InitString(&action_arg, "<none>")) {
  138. goto fail_args;
  139. }
  140. }
  141. if (!NCDValue_ListAppend(&args, action_arg)) {
  142. NCDValue_Free(&action_arg);
  143. goto fail_args;
  144. }
  145. NCDValue func;
  146. if (!NCDValue_InitString(&func, "ifel")) {
  147. goto fail_args;
  148. }
  149. NCDValue invoc;
  150. if (!NCDValue_InitInvoc(&invoc, func, args)) {
  151. NCDValue_Free(&func);
  152. goto fail_args;
  153. }
  154. NCDValue stmt_args;
  155. NCDValue_InitList(&stmt_args);
  156. if (!NCDValue_ListAppend(&stmt_args, invoc)) {
  157. NCDValue_Free(&stmt_args);
  158. NCDValue_Free(&invoc);
  159. goto fail0;
  160. }
  161. NCDStatement new_stmt;
  162. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "embcall", stmt_args)) {
  163. NCDValue_Free(&stmt_args);
  164. goto fail0;
  165. }
  166. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  167. *out_next = NCDBlock_NextStatement(block, stmt);
  168. return 1;
  169. fail_args:
  170. NCDValue_Free(&args);
  171. fail0:
  172. return 0;
  173. }
  174. static int desugar_do (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  175. {
  176. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_IF)
  177. ASSERT(NCDStatement_IfType(stmt) == NCDIFTYPE_DO)
  178. NCDIfBlock *ifblock = NCDStatement_IfBlock(stmt);
  179. NCDValue stmt_args;
  180. NCDValue_InitList(&stmt_args);
  181. while (NCDIfBlock_FirstIf(ifblock)) {
  182. NCDIf the_if = NCDIfBlock_GrabIf(ifblock, NCDIfBlock_FirstIf(ifblock));
  183. NCDBlock if_block = NCDIf_FreeGrabBlock(&the_if);
  184. NCDValue action_arg;
  185. if (!add_template(state, if_block, &action_arg)) {
  186. goto fail1;
  187. }
  188. if (!NCDValue_ListAppend(&stmt_args, action_arg)) {
  189. NCDValue_Free(&action_arg);
  190. goto fail1;
  191. }
  192. }
  193. NCDStatement new_stmt;
  194. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "do", stmt_args)) {
  195. goto fail1;
  196. }
  197. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  198. *out_next = NCDBlock_NextStatement(block, stmt);
  199. return 1;
  200. fail1:
  201. NCDValue_Free(&stmt_args);
  202. return 0;
  203. }
  204. static int desugar_foreach (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  205. {
  206. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_FOREACH)
  207. NCDValue args;
  208. NCDValue_InitList(&args);
  209. NCDValue collection;
  210. NCDBlock foreach_block;
  211. NCDStatement_ForeachGrab(stmt, &collection, &foreach_block);
  212. NCDValue template_arg;
  213. if (!add_template(state, foreach_block, &template_arg)) {
  214. NCDValue_Free(&collection);
  215. goto fail;
  216. }
  217. if (!NCDValue_ListAppend(&args, collection)) {
  218. NCDValue_Free(&template_arg);
  219. NCDValue_Free(&collection);
  220. goto fail;
  221. }
  222. if (!NCDValue_ListAppend(&args, template_arg)) {
  223. NCDValue_Free(&template_arg);
  224. goto fail;
  225. }
  226. NCDValue name1_arg;
  227. if (!NCDValue_InitString(&name1_arg, NCDStatement_ForeachName1(stmt))) {
  228. goto fail;
  229. }
  230. if (!NCDValue_ListAppend(&args, name1_arg)) {
  231. NCDValue_Free(&name1_arg);
  232. goto fail;
  233. }
  234. if (NCDStatement_ForeachName2(stmt)) {
  235. NCDValue name2_arg;
  236. if (!NCDValue_InitString(&name2_arg, NCDStatement_ForeachName2(stmt))) {
  237. goto fail;
  238. }
  239. if (!NCDValue_ListAppend(&args, name2_arg)) {
  240. NCDValue_Free(&name2_arg);
  241. goto fail;
  242. }
  243. }
  244. NCDStatement new_stmt;
  245. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "foreach_emb", args)) {
  246. goto fail;
  247. }
  248. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  249. *out_next = NCDBlock_NextStatement(block, stmt);
  250. return 1;
  251. fail:
  252. NCDValue_Free(&args);
  253. return 0;
  254. }
  255. static int desugar_blockstmt (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  256. {
  257. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_BLOCK)
  258. NCDValue args;
  259. NCDValue_InitList(&args);
  260. NCDBlock block_block = NCDStatement_BlockGrabBlock(stmt);
  261. NCDValue template_arg;
  262. if (!add_template(state, block_block, &template_arg)) {
  263. goto fail;
  264. }
  265. if (!NCDValue_ListAppend(&args, template_arg)) {
  266. NCDValue_Free(&template_arg);
  267. goto fail;
  268. }
  269. NCDStatement new_stmt;
  270. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "inline_code", args)) {
  271. goto fail;
  272. }
  273. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  274. *out_next = NCDBlock_NextStatement(block, stmt);
  275. return 1;
  276. fail:
  277. NCDValue_Free(&args);
  278. return 0;
  279. }
  280. int NCDSugar_Desugar (NCDProgram *prog)
  281. {
  282. ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE))
  283. ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE_GUARD))
  284. struct desugar_state state;
  285. state.prog = prog;
  286. state.template_name_ctr = 0;
  287. for (NCDProgramElem *elem = NCDProgram_FirstElem(prog); elem; elem = NCDProgram_NextElem(prog, elem)) {
  288. ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
  289. NCDProcess *proc = NCDProgramElem_Process(elem);
  290. if (!desugar_block(&state, NCDProcess_Block(proc))) {
  291. return 0;
  292. }
  293. }
  294. return 1;
  295. }