From 29091971d2a0d13351f6938214d04e123e3e2517 Mon Sep 17 00:00:00 2001
From: Wolfgang Bumiller <wolfgang.linux@bumiller.com>
Date: Wed, 4 Jul 2012 13:29:26 +0200
Subject: [PATCH] add AINSTR_END to the end of functions so the debug-printing
 knows when to end...

---
 ir.c            |  9 +++++++++
 test/ast-test.c | 23 ++++++++++++++---------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/ir.c b/ir.c
index 557a821..4ac089d 100644
--- a/ir.c
+++ b/ir.c
@@ -2310,6 +2310,7 @@ tailcall:
 static bool gen_function_code(ir_function *self)
 {
     ir_block *block;
+    prog_section_statement stmt;
 
     /* Starting from entry point, we generate blocks "as they come"
      * for now. Dead blocks will not be translated obviously.
@@ -2327,6 +2328,14 @@ static bool gen_function_code(ir_function *self)
         printf("failed to generate blocks for '%s'\n", self->name);
         return false;
     }
+
+    /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
+    stmt.opcode = AINSTR_END;
+    stmt.o1.u1 = 0;
+    stmt.o2.u1 = 0;
+    stmt.o3.u1 = 0;
+    if (code_statements_add(stmt) < 0)
+        return false;
     return true;
 }
 
diff --git a/test/ast-test.c b/test/ast-test.c
index 2217a39..026dbf3 100644
--- a/test/ast-test.c
+++ b/test/ast-test.c
@@ -29,6 +29,8 @@ int main()
     DEFVAR(f5);
     DEFVAR(print);
 
+    /* opts_debug = true; */
+
 #if 0
     BUILTIN(print, TYPE_VOID, -1);
     PARAM(TYPE_STRING, text);
@@ -43,19 +45,22 @@ MKCONSTFLOAT(f0, 0.0);
 MKCONSTFLOAT(f1, 1.0);
 MKCONSTFLOAT(f5, 5.0);
 
+FUNCTION(foo, TYPE_VOID);
+ENDFUNCTION(foo);
+
 FUNCTION(main, TYPE_VOID);
 
-VAR(TYPE_FLOAT, vi);
-VAR(TYPE_FLOAT, vx);
+    VAR(TYPE_FLOAT, vi);
+    VAR(TYPE_FLOAT, vx);
 
-MKLOCAL(vi);
-MKLOCAL(vx);
+    MKLOCAL(vi);
+    MKLOCAL(vx);
 
-STATE(ASSIGN(STORE_F, vi, f0));
-WHILE(BIN(LT, vi, f5));
-STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
-STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
-ENDWHILE();
+    STATE(ASSIGN(STORE_F, vi, f0));
+    WHILE(BIN(LT, vi, f5));
+        STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
+        STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
+    ENDWHILE();
 
 ENDFUNCTION(main);
 
-- 
2.39.5