NCDSugar.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_foreach (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next);
  40. static int desugar_blockstmt (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next);
  41. static int add_template (struct desugar_state *state, NCDBlock block, NCDValue *out_name_val)
  42. {
  43. char name[40];
  44. snprintf(name, sizeof(name), "__tmpl%zu", state->template_name_ctr);
  45. state->template_name_ctr++;
  46. if (!desugar_block(state, &block)) {
  47. NCDBlock_Free(&block);
  48. return 0;
  49. }
  50. NCDProcess proc_tmp;
  51. if (!NCDProcess_Init(&proc_tmp, 1, name, block)) {
  52. NCDBlock_Free(&block);
  53. return 0;
  54. }
  55. NCDProgramElem elem;
  56. NCDProgramElem_InitProcess(&elem, proc_tmp);
  57. if (!NCDProgram_PrependElem(state->prog, elem)) {
  58. NCDProgramElem_Free(&elem);
  59. return 0;
  60. }
  61. if (!NCDValue_InitString(out_name_val, name)) {
  62. return 0;
  63. }
  64. return 1;
  65. }
  66. static int desugar_block (struct desugar_state *state, NCDBlock *block)
  67. {
  68. NCDStatement *stmt = NCDBlock_FirstStatement(block);
  69. while (stmt) {
  70. switch (NCDStatement_Type(stmt)) {
  71. case NCDSTATEMENT_REG: {
  72. stmt = NCDBlock_NextStatement(block, stmt);
  73. } break;
  74. case NCDSTATEMENT_IF: {
  75. if (!desugar_if(state, block, stmt, &stmt)) {
  76. return 0;
  77. }
  78. } break;
  79. case NCDSTATEMENT_FOREACH: {
  80. if (!desugar_foreach(state, block, stmt, &stmt)) {
  81. return 0;
  82. }
  83. } break;
  84. case NCDSTATEMENT_BLOCK: {
  85. if (!desugar_blockstmt(state, block, stmt, &stmt)) {
  86. return 0;
  87. }
  88. } break;
  89. default: {
  90. return 0;
  91. } break;
  92. }
  93. }
  94. return 1;
  95. }
  96. static int desugar_if (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  97. {
  98. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_IF)
  99. NCDValue args;
  100. NCDValue_InitList(&args);
  101. NCDIfBlock *ifblock = NCDStatement_IfBlock(stmt);
  102. while (NCDIfBlock_FirstIf(ifblock)) {
  103. NCDIf ifc = NCDIfBlock_GrabIf(ifblock, NCDIfBlock_FirstIf(ifblock));
  104. NCDValue if_cond;
  105. NCDBlock if_block;
  106. NCDIf_FreeGrab(&ifc, &if_cond, &if_block);
  107. if (!NCDValue_ListAppend(&args, if_cond)) {
  108. NCDValue_Free(&if_cond);
  109. NCDBlock_Free(&if_block);
  110. goto fail;
  111. }
  112. NCDValue action_arg;
  113. if (!add_template(state, if_block, &action_arg)) {
  114. goto fail;
  115. }
  116. if (!NCDValue_ListAppend(&args, action_arg)) {
  117. NCDValue_Free(&action_arg);
  118. goto fail;
  119. }
  120. }
  121. if (NCDStatement_IfElse(stmt)) {
  122. NCDBlock else_block = NCDStatement_IfGrabElse(stmt);
  123. NCDValue action_arg;
  124. if (!add_template(state, else_block, &action_arg)) {
  125. goto fail;
  126. }
  127. if (!NCDValue_ListAppend(&args, action_arg)) {
  128. NCDValue_Free(&action_arg);
  129. goto fail;
  130. }
  131. }
  132. NCDStatement new_stmt;
  133. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "embcall2_multif", args)) {
  134. goto fail;
  135. }
  136. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  137. *out_next = NCDBlock_NextStatement(block, stmt);
  138. return 1;
  139. fail:
  140. NCDValue_Free(&args);
  141. return 0;
  142. }
  143. static int desugar_foreach (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  144. {
  145. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_FOREACH)
  146. NCDValue args;
  147. NCDValue_InitList(&args);
  148. NCDValue collection;
  149. NCDBlock foreach_block;
  150. NCDStatement_ForeachGrab(stmt, &collection, &foreach_block);
  151. NCDValue template_arg;
  152. if (!add_template(state, foreach_block, &template_arg)) {
  153. NCDValue_Free(&collection);
  154. goto fail;
  155. }
  156. if (!NCDValue_ListAppend(&args, collection)) {
  157. NCDValue_Free(&template_arg);
  158. NCDValue_Free(&collection);
  159. goto fail;
  160. }
  161. if (!NCDValue_ListAppend(&args, template_arg)) {
  162. NCDValue_Free(&template_arg);
  163. goto fail;
  164. }
  165. NCDValue name1_arg;
  166. if (!NCDValue_InitString(&name1_arg, NCDStatement_ForeachName1(stmt))) {
  167. goto fail;
  168. }
  169. if (!NCDValue_ListAppend(&args, name1_arg)) {
  170. NCDValue_Free(&name1_arg);
  171. goto fail;
  172. }
  173. if (NCDStatement_ForeachName2(stmt)) {
  174. NCDValue name2_arg;
  175. if (!NCDValue_InitString(&name2_arg, NCDStatement_ForeachName2(stmt))) {
  176. goto fail;
  177. }
  178. if (!NCDValue_ListAppend(&args, name2_arg)) {
  179. NCDValue_Free(&name2_arg);
  180. goto fail;
  181. }
  182. }
  183. NCDStatement new_stmt;
  184. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "foreach_emb", args)) {
  185. goto fail;
  186. }
  187. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  188. *out_next = NCDBlock_NextStatement(block, stmt);
  189. return 1;
  190. fail:
  191. NCDValue_Free(&args);
  192. return 0;
  193. }
  194. static int desugar_blockstmt (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  195. {
  196. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_BLOCK)
  197. NCDValue args;
  198. NCDValue_InitList(&args);
  199. NCDBlock block_block = NCDStatement_BlockGrabBlock(stmt);
  200. NCDValue template_arg;
  201. if (!add_template(state, block_block, &template_arg)) {
  202. goto fail;
  203. }
  204. if (!NCDValue_ListAppend(&args, template_arg)) {
  205. NCDValue_Free(&template_arg);
  206. goto fail;
  207. }
  208. NCDStatement new_stmt;
  209. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "inline_code", args)) {
  210. goto fail;
  211. }
  212. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  213. *out_next = NCDBlock_NextStatement(block, stmt);
  214. return 1;
  215. fail:
  216. NCDValue_Free(&args);
  217. return 0;
  218. }
  219. int NCDSugar_Desugar (NCDProgram *prog)
  220. {
  221. ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE))
  222. ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE_GUARD))
  223. struct desugar_state state;
  224. state.prog = prog;
  225. state.template_name_ctr = 0;
  226. for (NCDProgramElem *elem = NCDProgram_FirstElem(prog); elem; elem = NCDProgram_NextElem(prog, elem)) {
  227. ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
  228. NCDProcess *proc = NCDProgramElem_Process(elem);
  229. if (!desugar_block(&state, NCDProcess_Block(proc))) {
  230. return 0;
  231. }
  232. }
  233. return 1;
  234. }