NCDSugar.c 7.5 KB

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