merge
authorMario de Sousa <msousa@fe.up.pt>
Sun, 16 Apr 2017 08:46:58 +0100
changeset 1049 4d7183013481
parent 1048 37966f855bed (current diff)
parent 1039 52f63e622604 (diff)
child 1050 bdc21971f95d
merge
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c_st.cc
--- a/stage4/generate_c/generate_c.cc	Sun Apr 16 08:45:56 2017 +0100
+++ b/stage4/generate_c/generate_c.cc	Sun Apr 16 08:46:58 2017 +0100
@@ -1597,7 +1597,7 @@
     stage4out_c *s4o_ptr;
 
   public:
-    generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
+    generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long long time)
       : generate_c_base_and_typeid_c(s4o_ptr) {
       current_configuration = config_scope;
       search_config_instance   = new search_var_instance_decl_c(config_scope);
--- a/stage4/generate_c/generate_c_st.cc	Sun Apr 16 08:45:56 2017 +0100
+++ b/stage4/generate_c/generate_c_st.cc	Sun Apr 16 08:46:58 2017 +0100
@@ -1233,19 +1233,26 @@
 /* B 3.2.4 Iteration Statements */
 /********************************/
 void *visit(for_statement_c *symbol) {
-  s4o.print("for(");
-  symbol->control_variable->accept(*this);
-  s4o.print(" = ");
-  symbol->beg_expression->accept(*this);
-  s4o.print("; ");
+  /* Due to the way the GET/SET_GLOBAL accessor macros access VAR_GLOBAL variables,
+   * these varibles cannot be used within a C for(;;) loop.
+   * We must therefore implemnt the FOR END_FOR loop as a C while() loop
+   */
+  s4o.print("/* FOR ... */\n" + s4o.indent_spaces);
+  /* For the initialization part, we create an assignment_statement_c   */
+  /* and have this visitor visit it!                                    */ 
+  assignment_statement_c ini_assignment(symbol->control_variable, symbol->beg_expression);
+  ini_assignment.accept(*this);
+  //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
+  //s4o.print(" = ");
+  //symbol->beg_expression->accept(*this);
+  
+  /* comparison // check for end of loop */
+  s4o.print(";\n" + s4o.indent_spaces + "while( ");
   if (symbol->by_expression == NULL) {
-    /* increment by 1 */
+    /* increment by 1 */    
     symbol->control_variable->accept(*this);
     s4o.print(" <= ");
     symbol->end_expression->accept(*this);
-    s4o.print("; ");
-    symbol->control_variable->accept(*this);
-    s4o.print("++");
   } else {
     /* increment by user defined value  */
     /* The user defined increment value may be negative, in which case
@@ -1265,19 +1272,47 @@
     symbol->control_variable->accept(*this);
     s4o.print(" >= (");
     symbol->end_expression->accept(*this);
-    s4o.print(")); ");
-    symbol->control_variable->accept(*this);
-    s4o.print(" += (");
-    symbol->by_expression->accept(*this);
-    s4o.print(")");
-  }
-  s4o.print(")");
+    s4o.print(")) ");
+  }
+  s4o.print(" ) {\n");
   
-  s4o.print(" {\n");
+  /* the body part */
   s4o.indent_right();
   symbol->statement_list->accept(*this);
+
+  /* increment part */
+  s4o.print(s4o.indent_spaces + "/* BY ... (of FOR loop) */\n");
+  s4o.print(s4o.indent_spaces); 
+  if (symbol->by_expression == NULL) {
+    /* increment by 1 */    
+    /* For the increment part, we create an add_expression_c and assignment_statement_c   */
+    /* and have this visitor vist the latter!                                             */ 
+    integer_c              integer_oneval("1");
+    add_expression_c       add_expression(symbol->control_variable, &integer_oneval);
+    assignment_statement_c inc_assignment(symbol->control_variable, &add_expression);
+    integer_oneval.const_value._int64 .set(1);                    // set the stage3 anottation we need 
+    integer_oneval.const_value._uint64.set(1);                    // set the stage3 anottation we need
+    integer_oneval.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
+    add_expression.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
+    inc_assignment.accept(*this);
+    //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
+    //s4o.print("++");
+  } else {
+    /* increment by user defined value  */
+    /* For the increment part, we create an add_expression_c and assignment_statement_c   */
+    /* and have this visitor vist the latter!                                             */ 
+    add_expression_c       add_expression(symbol->control_variable, symbol->by_expression);
+    assignment_statement_c inc_assignment(symbol->control_variable, &add_expression);
+    add_expression.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
+    inc_assignment.accept(*this);
+    //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
+    //s4o.print(" += (");
+    //symbol->by_expression->accept(*this);
+    //s4o.print(")");
+  }  
+  
   s4o.indent_left();
-  s4o.print(s4o.indent_spaces); s4o.print("}");
+  s4o.print(";\n" + s4o.indent_spaces + "} /* END_FOR */");
   return NULL;
 }