NCDSugar.c 7.2 KB

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