Code for debugging const_value annotations in abstract syntax tree.
authorMario de Sousa <msousa@fe.up.pt>
Mon, 11 Jun 2012 18:48:58 +0100
changeset 580 b6350d9f204d
parent 579 983a3b743085
child 581 1e158dc9f9c1
Code for debugging const_value annotations in abstract syntax tree.
stage4/generate_iec/generate_iec.cc
stage4/stage4.cc
stage4/stage4.hh
--- a/stage4/generate_iec/generate_iec.cc	Mon Jun 11 15:58:44 2012 +0100
+++ b/stage4/generate_iec/generate_iec.cc	Mon Jun 11 18:48:58 2012 +0100
@@ -44,6 +44,8 @@
 
 
 
+/* Compile with the following option, to print out the const_value of each symbol, which was filled in by constant_folding_c during stage3 */
+// #define DEBUG_CONST_VALUE
 
 
 
@@ -78,11 +80,63 @@
 
   private:
 
+    
+void print_const_value(symbol_c *symbol) {
+#ifdef DEBUG_CONST_VALUE
+  if (NULL == symbol) return;
+  bool first = true;
+  
+  if (NULL != symbol->const_value_uint64) {
+    first?s4o.print("{"):s4o.print("; ");
+    first = false;
+    s4o.print("uint64:");
+    if (symbol->const_value_uint64->status == symbol_c::cs_const_value)
+      s4o.print_uint64(symbol->const_value_uint64->value);
+    if (symbol->const_value_uint64->status == symbol_c::cs_overflow)
+    s4o.print("OVERFLOW");
+  }
+  if (NULL != symbol->const_value_int64) {
+    first?s4o.print("{"):s4o.print("; ");
+    first = false;
+    s4o.print("int64:");
+    if (symbol->const_value_int64->status == symbol_c::cs_const_value)
+      s4o.print_int64(symbol->const_value_int64->value);
+    if (symbol->const_value_int64->status == symbol_c::cs_overflow)
+    s4o.print("OVERFLOW");
+  }
+  if (NULL != symbol->const_value_real64) {
+    first?s4o.print("{"):s4o.print("; ");
+    first = false;
+    s4o.print("real64:");
+    if (symbol->const_value_real64->status == symbol_c::cs_const_value)
+      s4o.print_real64(symbol->const_value_real64->value);
+    if (symbol->const_value_real64->status == symbol_c::cs_overflow)
+    s4o.print("OVERFLOW");
+  }
+  if (NULL != symbol->const_value_bool) {
+    first?s4o.print("{"):s4o.print("; ");
+    first = false;
+    s4o.print("bool:");
+    if (symbol->const_value_bool->status == symbol_c::cs_const_value)
+      s4o.print((symbol->const_value_bool->value)?"true":"false");
+    if (symbol->const_value_bool->status == symbol_c::cs_overflow)
+    s4o.print("OVERFLOW");
+  }
+  if (!first) s4o.print("}");  
+#endif
+  return;
+}
+
+
+
 void *print_token(token_c *token) {
+  print_const_value(token);
   return s4o.print(token->value);
 }
 
+
 void *print_literal(symbol_c *type, symbol_c *value) {
+  print_const_value(value);
   if (NULL != type) {
     type->accept(*this);
     s4o.print("#");
@@ -112,9 +166,11 @@
 }
 
 
-void *print_binary_expression(symbol_c *l_exp,
+void *print_binary_expression(symbol_c *symbol,
+			      symbol_c *l_exp,
 			      symbol_c *r_exp,
 			      const char *operation) {
+  print_const_value(symbol);
   s4o.print("(");
   l_exp->accept(*this);
   s4o.print(operation);
@@ -123,8 +179,10 @@
   return NULL;
 }
 
-void *print_unary_expression(symbol_c *exp,
+void *print_unary_expression(symbol_c *symbol,
+			     symbol_c *exp,
 			     const char *operation) {
+  print_const_value(symbol);
   s4o.print(operation);
   exp->accept(*this);
   return NULL;
@@ -192,9 +250,9 @@
 /* B 1.2.1 - Numeric Literals */
 /******************************/
 void *visit(real_c *symbol)               {return print_token(symbol);}
-void *visit(neg_real_c *symbol)           {return print_unary_expression(symbol->exp, "-");}
+void *visit(neg_real_c *symbol)           {return print_unary_expression(symbol, symbol->exp, "-");}
 void *visit(integer_c *symbol)            {return print_token(symbol);}
-void *visit(neg_integer_c *symbol)        {return print_unary_expression(symbol->exp, "-");}
+void *visit(neg_integer_c *symbol)        {return print_unary_expression(symbol, symbol->exp, "-");}
 void *visit(binary_integer_c *symbol)     {return print_token(symbol);}
 void *visit(octal_integer_c *symbol)      {return print_token(symbol);}
 void *visit(hex_integer_c *symbol)        {return print_token(symbol);}
@@ -1785,23 +1843,23 @@
 /***********************/
 /* B 3.1 - Expressions */
 /***********************/
-void *visit(or_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " OR ");}
-void *visit(xor_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " XOR ");}
-void *visit(and_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " AND ");}
-void *visit(equ_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " = ");}
-void *visit(notequ_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " <> ");}
-void *visit(lt_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");}
-void *visit(gt_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");}
-void *visit(le_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");}
-void *visit(ge_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");}
-void *visit(add_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");}
-void *visit(sub_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");}
-void *visit(mul_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");}
-void *visit(div_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");}
-void *visit(mod_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " MOD ");}
-void *visit(power_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");}
-void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol->exp, "-");}
-void *visit(not_expression_c *symbol) {return print_unary_expression(symbol->exp, "NOT ");}
+void *visit(or_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " OR ");}
+void *visit(xor_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " XOR ");}
+void *visit(and_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " AND ");}
+void *visit(equ_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " = ");}
+void *visit(notequ_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " <> ");}
+void *visit(lt_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " < ");}
+void *visit(gt_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " > ");}
+void *visit(le_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " <= ");}
+void *visit(ge_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " >= ");}
+void *visit(add_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " + ");}
+void *visit(sub_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " - ");}
+void *visit(mul_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " * ");}
+void *visit(div_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " / ");}
+void *visit(mod_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " MOD ");}
+void *visit(power_expression_c *symbol) {return print_binary_expression(symbol, symbol->l_exp, symbol->r_exp, " ** ");}
+void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol, symbol->exp, "-");}
+void *visit(not_expression_c *symbol) {return print_unary_expression(symbol, symbol->exp, "NOT ");}
 
 void *visit(function_invocation_c *symbol) {
   symbol->function_name->accept(*this);
--- a/stage4/stage4.cc	Mon Jun 11 15:58:44 2012 +0100
+++ b/stage4/stage4.cc	Mon Jun 11 18:48:58 2012 +0100
@@ -163,6 +163,24 @@
   return NULL;
 }
 
+void *stage4out_c::print_int64(int64_t integer) {
+  if (!allow_output) return NULL;
+  *out << integer;
+  return NULL;
+}
+
+void *stage4out_c::print_uint64(uint64_t integer) {
+  if (!allow_output) return NULL;
+  *out << integer;
+  return NULL;
+}
+
+void *stage4out_c::print_real64(real64_t integer) {
+  if (!allow_output) return NULL;
+  *out << integer;
+  return NULL;
+}
+
 void *stage4out_c::printupper(const char *str) {
   if (!allow_output) return NULL;
   for (int i = 0; str[i] != '\0'; i++)
--- a/stage4/stage4.hh	Mon Jun 11 15:58:44 2012 +0100
+++ b/stage4/stage4.hh	Mon Jun 11 18:48:58 2012 +0100
@@ -68,6 +68,10 @@
     void *print_long_integer(unsigned long l_integer, bool suffix=true);
     void *print_long_long_integer(unsigned long long ll_integer, bool suffix=true);
 
+    void *print_int64(int64_t integer);
+    void *print_uint64(uint64_t integer);
+    void *print_real64(real64_t integer);
+
     void *printupper(const char *str);
     void *printupper(std::string str);