NCDSugar.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 add_template (struct desugar_state *state, NCDBlock block, NCDValue *out_name_val)
  40. {
  41. char name[40];
  42. snprintf(name, sizeof(name), "__tmpl%zu", state->template_name_ctr);
  43. state->template_name_ctr++;
  44. if (!desugar_block(state, &block)) {
  45. NCDBlock_Free(&block);
  46. return 0;
  47. }
  48. NCDProcess proc_tmp;
  49. if (!NCDProcess_Init(&proc_tmp, 1, name, block)) {
  50. NCDBlock_Free(&block);
  51. return 0;
  52. }
  53. if (!NCDProgram_PrependProcess(state->prog, proc_tmp)) {
  54. NCDProcess_Free(&proc_tmp);
  55. return 0;
  56. }
  57. if (!NCDValue_InitString(out_name_val, name)) {
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. static int desugar_block (struct desugar_state *state, NCDBlock *block)
  63. {
  64. NCDStatement *stmt = NCDBlock_FirstStatement(block);
  65. while (stmt) {
  66. switch (NCDStatement_Type(stmt)) {
  67. case NCDSTATEMENT_REG: {
  68. stmt = NCDBlock_NextStatement(block, stmt);
  69. } break;
  70. case NCDSTATEMENT_IF: {
  71. if (!desugar_if(state, block, stmt, &stmt)) {
  72. return 0;
  73. }
  74. } break;
  75. default: ASSERT(0);
  76. }
  77. }
  78. return 1;
  79. }
  80. static int desugar_if (struct desugar_state *state, NCDBlock *block, NCDStatement *stmt, NCDStatement **out_next)
  81. {
  82. ASSERT(NCDStatement_Type(stmt) == NCDSTATEMENT_IF)
  83. NCDValue args;
  84. NCDValue_InitList(&args);
  85. NCDIfBlock *ifblock = NCDStatement_IfBlock(stmt);
  86. while (NCDIfBlock_FirstIf(ifblock)) {
  87. NCDIf ifc = NCDIfBlock_GrabIf(ifblock, NCDIfBlock_FirstIf(ifblock));
  88. NCDValue if_cond;
  89. NCDBlock if_block;
  90. NCDIf_FreeGrab(&ifc, &if_cond, &if_block);
  91. if (!NCDValue_ListAppend(&args, if_cond)) {
  92. NCDValue_Free(&if_cond);
  93. NCDBlock_Free(&if_block);
  94. goto fail;
  95. }
  96. NCDValue action_arg;
  97. if (!add_template(state, if_block, &action_arg)) {
  98. goto fail;
  99. }
  100. if (!NCDValue_ListAppend(&args, action_arg)) {
  101. NCDValue_Free(&action_arg);
  102. goto fail;
  103. }
  104. }
  105. if (NCDStatement_IfElse(stmt)) {
  106. NCDBlock else_block = NCDStatement_IfGrabElse(stmt);
  107. NCDValue action_arg;
  108. if (!add_template(state, else_block, &action_arg)) {
  109. goto fail;
  110. }
  111. if (!NCDValue_ListAppend(&args, action_arg)) {
  112. NCDValue_Free(&action_arg);
  113. goto fail;
  114. }
  115. }
  116. NCDStatement new_stmt;
  117. if (!NCDStatement_InitReg(&new_stmt, NCDStatement_Name(stmt), NULL, "embcall2_multif", args)) {
  118. goto fail;
  119. }
  120. stmt = NCDBlock_ReplaceStatement(block, stmt, new_stmt);
  121. *out_next = NCDBlock_NextStatement(block, stmt);
  122. return 1;
  123. fail:
  124. NCDValue_Free(&args);
  125. return 0;
  126. }
  127. int NCDSugar_Desugar (NCDProgram *prog)
  128. {
  129. struct desugar_state state;
  130. state.prog = prog;
  131. state.template_name_ctr = 0;
  132. for (NCDProcess *proc = NCDProgram_FirstProcess(prog); proc; proc = NCDProgram_NextProcess(prog, proc)) {
  133. if (!desugar_block(&state, NCDProcess_Block(proc))) {
  134. return 0;
  135. }
  136. }
  137. return 1;
  138. }