Author: kjs
Date: Thu Apr 12 03:34:12 2007
New Revision: 18160

Modified:
   trunk/compilers/pirc/src/jsonout.c
   trunk/compilers/pirc/src/jsonout.h
   trunk/compilers/pirc/src/pastout.c
   trunk/compilers/pirc/src/pastout.h
   trunk/compilers/pirc/src/pbcout.c
   trunk/compilers/pirc/src/pbcout.h
   trunk/compilers/pirc/src/pirmain.c
   trunk/compilers/pirc/src/pirout.c
   trunk/compilers/pirc/src/pirout.h
   trunk/compilers/pirc/src/pirutil.c
   trunk/compilers/pirc/src/pirutil.h
   trunk/compilers/pirc/src/pirvtable.c
   trunk/compilers/pirc/src/pirvtable.h

Log:
compilers/pirc:
* removed some #defines from back-ends; use const variables and real functions 
instead.
* implement -o <file> option; all back-ends can write to file now.
* refactored a bit (open_file() in pirutil.c)

Modified: trunk/compilers/pirc/src/jsonout.c
==============================================================================
--- trunk/compilers/pirc/src/jsonout.c  (original)
+++ trunk/compilers/pirc/src/jsonout.c  Thu Apr 12 03:34:12 2007
@@ -17,18 +17,20 @@
 */
 #include "jsonout.h"
 #include "pirvtable.h"
+#include "pirutil.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 
-#define OUT             stdout
-#define INDENT          4
 
-/* make it look like a function call */
-#define indent(D)       D->indent += INDENT
-#define dedent(D)       D->indent -= INDENT
 
-#define print_comma(D)  fprintf(OUT, "%*s,\n", data->indent, " ");
+static const int INDENT = 4;
+
+
+#define print_comma(D)  fprintf(data->file, "%*s,\n", data->indent, " ");
+
+
+
 
 /*
 
@@ -57,7 +59,9 @@
  *
  */
 typedef struct emit_data {
-    FILE *outfile; /* output file -- XXX TODO: get filename from -o option in 
pirmain.c */
+    /* output file */
+    FILE *file;
+    char *outputfile;
 
     int indent; /* keep track of indention */
     /* has the first item in a list already been emitted? If so, we need a 
comma as separator */
@@ -78,6 +82,20 @@
 
 */
 
+/* increase the indention level */
+static int
+indent(emit_data *data) {
+    data->indent += INDENT;
+    return data->indent;
+}
+
+/* decreate the indention level */
+static int
+dedent(emit_data *data) {
+    data->indent -= INDENT;
+    return data->indent;
+}
+
 
 /*
 
@@ -122,13 +140,14 @@
 
 static void
 json_start_object(emit_data *data) {
-    fprintf(OUT, "%*s{\n", data->indent, data->indent ? " " : "");
+    fprintf(data->file, "%*s{\n", data->indent, data->indent ? " " : "");
     indent(data);
 }
 
 static void
 json_init(emit_data *data) {
-    fprintf(OUT, "{\n");
+    data->file = open_file(data->outputfile, "w");
+    fprintf(data->file, "{\n");
     indent(data);
 }
 
@@ -136,67 +155,67 @@
 static void
 json_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s}\n", data->indent, (data->indent == 0) ? "" : " ");
+    fprintf(data->file, "%*s}\n", data->indent, (data->indent == 0) ? "" : " 
");
 }
 
 static void
 json_sub_start(emit_data *data) {
     if (data->need_comma) print_comma(data);
-    fprintf(OUT, "%*s{ \"sub\" :\n", data->indent, " ");
+    fprintf(data->file, "%*s{ \"sub\" :\n", data->indent, " ");
     indent(data);
 
-    fprintf(OUT, "%*s{\n", data->indent, " ");
+    fprintf(data->file, "%*s{\n", data->indent, " ");
     indent(data);
 }
 
 static void
 json_source(emit_data *data, char *source) {
-    fprintf(OUT, "%*s{ \"source\" : \"%s\" },\n", data->indent, " ", source);
+    fprintf(data->file, "%*s{ \"source\" : \"%s\" },\n", data->indent, " ", 
source);
 }
 
 static void
 json_position(emit_data *data, int pos) {
-    fprintf(OUT, "%*s{ \"pos\" : %d },\n", data->indent, " ", pos);
+    fprintf(data->file, "%*s{ \"pos\" : %d },\n", data->indent, " ", pos);
 }
 
 static void
 json_sub_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s}\n", data->indent, " ");
+    fprintf(data->file, "%*s}\n", data->indent, " ");
 
     dedent(data);
-    fprintf(OUT, "%*s}\n", data->indent, " ");
+    fprintf(data->file, "%*s}\n", data->indent, " ");
     data->need_comma = 1;
 }
 
 static void
 json_name(emit_data *data, char *name) {
-    fprintf(OUT, "%*s{ \"name\" : \"%s\" }\n", data->indent, " ", name);
+    fprintf(data->file, "%*s{ \"name\" : \"%s\" }\n", data->indent, " ", name);
 }
 
 static void
 json_stmts_start(emit_data *data) {
-    fprintf(OUT, "%*s{ \"instructions\" :\n", data->indent, " ");
+    fprintf(data->file, "%*s{ \"instructions\" :\n", data->indent, " ");
     indent(data);
-    fprintf(OUT, "%*s[\n", data->indent, " ");
+    fprintf(data->file, "%*s[\n", data->indent, " ");
     indent(data);
     data->need_comma = 0;
 }
 
 static void
 json_param_start(emit_data *data) {
-    fprintf(OUT, "%*s\"parameters\" :\n", data->indent, " ");
+    fprintf(data->file, "%*s\"parameters\" :\n", data->indent, " ");
     indent(data);
-    fprintf(OUT, "%*s[\n", data->indent, " ");
+    fprintf(data->file, "%*s[\n", data->indent, " ");
     indent(data);
     data->need_comma = 0;
 }
 
 static void
 json_sub_flag_start(emit_data *data) {
-    fprintf(OUT, "%*s{ \"subflags\" :\n", data->indent, " ");
+    fprintf(data->file, "%*s{ \"subflags\" :\n", data->indent, " ");
     indent(data);
-    fprintf(OUT, "%*s[\n", data->indent, " ");
+    fprintf(data->file, "%*s[\n", data->indent, " ");
     indent(data);
     data->need_comma = 0;
 }
@@ -204,20 +223,20 @@
 static void
 json_sub_flag_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s]\n", data->indent, " ");
+    fprintf(data->file, "%*s]\n", data->indent, " ");
     dedent(data);
-    fprintf(OUT, "%*s},\n", data->indent, " ");
+    fprintf(data->file, "%*s},\n", data->indent, " ");
 }
 
 static void
 json_sub_flag(emit_data *data, int flag) {
     if (data->need_comma) print_comma(data);
-    fprintf(OUT, "%*s{ \"subflag\" : \"%s\" }\n", data->indent, " ", "TODO");
+    fprintf(data->file, "%*s{ \"subflag\" : \"%s\" }\n", data->indent, " ", 
"TODO");
 }
 
 static void
 json_list_start(emit_data *data) {
-    fprintf(OUT, "%*s[", data->indent, " ");
+    fprintf(data->file, "%*s[", data->indent, " ");
     indent(data);
     data->need_comma = 0;
 }
@@ -225,40 +244,40 @@
 static void
 json_list_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s]\n", data->indent, " ");
+    fprintf(data->file, "%*s]\n", data->indent, " ");
 }
 
 static void
 json_stmts_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s]\n", data->indent, " ");
+    fprintf(data->file, "%*s]\n", data->indent, " ");
     dedent(data);
-    fprintf(OUT, "%*s}\n", data->indent, " ");
+    fprintf(data->file, "%*s}\n", data->indent, " ");
 }
 
 static void
 json_op_start(emit_data *data, char *op) {
     if (data->need_comma) print_comma(data);
-    fprintf(OUT, "%*s{ \"op\" : \"%s\" },\n", data->indent, " ", op);
-    fprintf(OUT, "%*s{ \"operands\" :\n", data->indent, " ");
+    fprintf(data->file, "%*s{ \"op\" : \"%s\" },\n", data->indent, " ", op);
+    fprintf(data->file, "%*s{ \"operands\" :\n", data->indent, " ");
     indent(data);
-    fprintf(OUT, "%*s[\n", data->indent, " ");
+    fprintf(data->file, "%*s[\n", data->indent, " ");
     indent(data);
 }
 
 static void
 json_op_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s]\n", data->indent, " ");
+    fprintf(data->file, "%*s]\n", data->indent, " ");
     dedent(data);
-    fprintf(OUT, "%*s}\n", data->indent, " ");
+    fprintf(data->file, "%*s}\n", data->indent, " ");
 
 }
 
 static void
 json_expr(emit_data *data, char *expr) {
     if (data->need_comma) print_comma(data);
-    fprintf(OUT, "%*s{ \"expr\" : \"%s\" }\n", data->indent, " ", expr);
+    fprintf(data->file, "%*s{ \"expr\" : \"%s\" }\n", data->indent, " ", expr);
     data->need_comma = 1;
 }
 
@@ -267,28 +286,28 @@
 
 static void
 json_method(emit_data *data, char *name) {
-    fprintf(OUT, "%*s{ \"method\" : \"%s\" }\n", data->indent, " ", name);
+    fprintf(data->file, "%*s{ \"method\" : \"%s\" }\n", data->indent, " ", 
name);
 }
 
 static void
 json_invocant(emit_data *data, char *invocant) {
-    fprintf(OUT, "%*s{ \"invocant\" : \"%s\" }\n", data->indent, " ", 
invocant);
+    fprintf(data->file, "%*s{ \"invocant\" : \"%s\" }\n", data->indent, " ", 
invocant);
 }
 
 static void
 json_args_start(emit_data *data) {
-    fprintf(OUT, "%*s{ \"arguments\" :\n", data->indent, " ");
+    fprintf(data->file, "%*s{ \"arguments\" :\n", data->indent, " ");
     indent(data);
-    fprintf(OUT, "%*s[\n", data->indent, " ");
+    fprintf(data->file, "%*s[\n", data->indent, " ");
     indent(data);
 }
 
 static void
 json_args_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s]\n", data->indent, " ");
+    fprintf(data->file, "%*s]\n", data->indent, " ");
     dedent(data);
-    fprintf(OUT, "%*s}\n", data->indent, " ");
+    fprintf(data->file, "%*s}\n", data->indent, " ");
 }
 
 
@@ -302,19 +321,19 @@
 
 static void
 json_invokable(emit_data *data, char *invokable) {
-    fprintf(OUT, "%*s{ \"invokable\" : \"%s\" }\n", data->indent, " ", 
invokable);
+    fprintf(data->file, "%*s{ \"invokable\" : \"%s\" }\n", data->indent, " ", 
invokable);
 }
 
 static void
 json_invocation_start(emit_data *data) {
-    fprintf(OUT, "%*s{ \"invocation\" :\n%*s{\n", data->indent, " ", 
data->indent, " ");
+    fprintf(data->file, "%*s{ \"invocation\" :\n%*s{\n", data->indent, " ", 
data->indent, " ");
     indent(data);
 }
 
 static void
 json_invocation_end(emit_data *data) {
     dedent(data);
-    fprintf(OUT, "%*s}\n", data->indent, " ");
+    fprintf(data->file, "%*s}\n", data->indent, " ");
 }
 
 
@@ -329,6 +348,7 @@
 */
 static void
 json_destroy(emit_data *data) {
+    fclose(data->file);
     free(data);
     data = NULL;
 }
@@ -344,7 +364,7 @@
 
 */
 pirvtable *
-init_json_vtable(void) {
+init_json_vtable(char *outputfile) {
     pirvtable *vtable = new_pirvtable();
 
     /* set vtable methods to the appropiate implementation */
@@ -383,8 +403,9 @@
         exit(1);
     }
     vtable->data->indent = 0;
+    vtable->data->outputfile = outputfile;
+
 
-    /* vtable->next_expr   = json_next; */
 
     return vtable;
 }

Modified: trunk/compilers/pirc/src/jsonout.h
==============================================================================
--- trunk/compilers/pirc/src/jsonout.h  (original)
+++ trunk/compilers/pirc/src/jsonout.h  Thu Apr 12 03:34:12 2007
@@ -4,7 +4,7 @@
 /* predeclare */
 struct pirvtable;
 
-extern struct pirvtable *init_json_vtable(void);
+extern struct pirvtable *init_json_vtable(char *outputfile);
 
 #endif
 

Modified: trunk/compilers/pirc/src/pastout.c
==============================================================================
--- trunk/compilers/pirc/src/pastout.c  (original)
+++ trunk/compilers/pirc/src/pastout.c  Thu Apr 12 03:34:12 2007
@@ -14,15 +14,12 @@
 
 #include "pastout.h"
 #include "pirvtable.h"
+#include "pirutil.h"
 #include <stdio.h>
 #include <stdlib.h>
 
-
-/* keep outputfile possibility easy */
-#define OUT         stderr
-#define INDENT      4
-#define indent(D)   D->indent += INDENT
-#define dedent(D)   D->indent -= INDENT
+/* number of spaces for 1 indention level */
+static const int INDENT = 4;
 
 
 /*
@@ -38,10 +35,26 @@
 */
 typedef struct emit_data {
     int indent;
+    char *outputfile;
+    FILE *file;
 
 } emit_data;
 
 
+/* increase the indention level */
+static int
+indent(emit_data *data) {
+    data->indent += INDENT;
+    return data->indent;
+}
+
+/* decreate the indention level */
+static int
+dedent(emit_data *data) {
+    data->indent -= INDENT;
+    return data->indent;
+}
+
 /*
 
 =head1 PAST VTABLE ENTRIES
@@ -57,7 +70,14 @@
 */
 static void
 past_init(struct emit_data *data) {
-    fprintf(OUT, "\"past\" => PMC 'PAST::Block'  {\n");
+    data->file = fopen(data->outputfile, "w");
+
+    if (data->file == NULL) {
+        fprintf(stderr, "Failed to open file '%s'\n", data->outputfile);
+        exit(1);
+    }
+
+    fprintf(data->file, "\"past\" => PMC 'PAST::Block'  {\n");
     indent(data);
 }
 
@@ -72,18 +92,18 @@
 */
 static void
 past_block(struct emit_data *data) {
-    fprintf(OUT, "%*sPMC 'PAST::Block'  {\n", data->indent, " ");
+    fprintf(data->file, "%*sPMC 'PAST::Block'  {\n", data->indent, " ");
     indent(data);
 }
 
 static void
 past_source(emit_data *data, char *source) {
-    fprintf(OUT, "%*s<source> => \"%s\"\n", data->indent, " ", source);
+    fprintf(data->file, "%*s<source> => \"%s\"\n", data->indent, " ", source);
 }
 
 static void
 past_position(emit_data *data, int pos) {
-    fprintf(OUT, "%*s<pos> => %d\n", data->indent, " ", pos);
+    fprintf(data->file, "%*s<pos> => %d\n", data->indent, " ", pos);
 }
 
 /*
@@ -102,7 +122,7 @@
     /* check for indent == 0, if so, then print no space, this is
      * so otherwise a single space is printed.
      */
-    fprintf(OUT, "%*s}\n", data->indent, (data->indent == 0) ? "" : " ");
+    fprintf(data->file, "%*s}\n", data->indent, (data->indent == 0) ? "" : " 
");
 
 }
 
@@ -117,7 +137,7 @@
 */
 static void
 past_name(struct emit_data *data, char *name) {
-    fprintf(OUT, "%*s<name> => \"%s\"\n", data->indent, " ", name);
+    fprintf(data->file, "%*s<name> => \"%s\"\n", data->indent, " ", name);
 }
 
 /*
@@ -131,7 +151,7 @@
 */
 static void
 past_stmts(struct emit_data *data) {
-    fprintf(OUT, "%*s[%d] => PMC 'PAST::Stmts'  {\n", data->indent, " ", 0); 
/* fix array index */
+    fprintf(data->file, "%*s[%d] => PMC 'PAST::Stmts'  {\n", data->indent, " 
", 0); /* fix array index */
     indent(data);
 }
 
@@ -146,9 +166,9 @@
 */
 static void
 past_param(struct emit_data *data) {
-    fprintf(OUT, "%*sFIXTHIS => PMC 'PAST::Var'  {\n", data->indent, " ");
+    fprintf(data->file, "%*sFIXTHIS => PMC 'PAST::Var'  {\n", data->indent, " 
");
     indent(data);
-    fprintf(OUT, "%*s<scope> => \"parameter\"\n", data->indent, " ");
+    fprintf(data->file, "%*s<scope> => \"parameter\"\n", data->indent, " ");
 }
 
 /*
@@ -162,7 +182,7 @@
 */
 static void
 past_type(struct emit_data *data, char *type) {
-    fprintf(OUT, "%*s<type> => \"%s\"\n", data->indent, " ", type);
+    fprintf(data->file, "%*s<type> => \"%s\"\n", data->indent, " ", type);
 }
 
 /*
@@ -176,7 +196,7 @@
 */
 static void
 past_subflag(struct emit_data *data, int flag) {
-    /* fprintf(OUT, "%*s<???> => \"%s\"\n", data->indent, " ", type);
+    /* fprintf(data->file, "%*s<???> => \"%s\"\n", data->indent, " ", type);
     */
 }
 
@@ -192,9 +212,9 @@
 */
 static void
 past_op(struct emit_data *data, char *op) {
-    fprintf(OUT, "%*sFIXME => PMC 'PAST::Op'  {\n", data->indent, " ");
+    fprintf(data->file, "%*sFIXME => PMC 'PAST::Op'  {\n", data->indent, " ");
     indent(data);
-    fprintf(OUT, "%*s<pirop> => \"%s\"\n", data->indent, " ", op);
+    fprintf(data->file, "%*s<pirop> => \"%s\"\n", data->indent, " ", op);
 
 }
 
@@ -209,7 +229,7 @@
 */
 static void
 past_expr(struct emit_data *data, char *expr) {
-    fprintf(OUT, "%*s[%d] => \"%s\"\n", data->indent, " ", 0, expr); /* fix 
index */
+    fprintf(data->file, "%*s[%d] => \"%s\"\n", data->indent, " ", 0, expr); /* 
fix index */
 }
 
 /*
@@ -223,6 +243,7 @@
 */
 static void
 past_destroy(emit_data *data) {
+    fclose(data->file);
     free(data);
     data = NULL;
 }
@@ -237,7 +258,7 @@
 
 */
 pirvtable *
-init_past_vtable(void) {
+init_past_vtable(char *outputfile) {
     pirvtable *vtable = new_pirvtable();
 
     /* override vtable methods */
@@ -265,6 +286,7 @@
         exit(1);
     }
     vtable->data->indent = 0;
+    vtable->data->outputfile = outputfile;
 
     return vtable;
 }

Modified: trunk/compilers/pirc/src/pastout.h
==============================================================================
--- trunk/compilers/pirc/src/pastout.h  (original)
+++ trunk/compilers/pirc/src/pastout.h  Thu Apr 12 03:34:12 2007
@@ -4,7 +4,7 @@
 /* predeclare */
 struct pirvtable;
 
-extern struct pirvtable *init_past_vtable(void);
+extern struct pirvtable *init_past_vtable(char *outputfile);
 
 #endif
 

Modified: trunk/compilers/pirc/src/pbcout.c
==============================================================================
--- trunk/compilers/pirc/src/pbcout.c   (original)
+++ trunk/compilers/pirc/src/pbcout.c   Thu Apr 12 03:34:12 2007
@@ -9,6 +9,8 @@
 
 IIUC, the PBC format is not yet fixed.
 
+If compilers/bcg works, connect the vtable methods to those api calls.
+
 =cut
 
 */
@@ -17,7 +19,7 @@
 
 
 pirvtable *
-init_pbc_vtable(void) {
+init_pbc_vtable(char *outputfile) {
     pirvtable *vtable = new_pirvtable();
 
     return vtable;

Modified: trunk/compilers/pirc/src/pbcout.h
==============================================================================
--- trunk/compilers/pirc/src/pbcout.h   (original)
+++ trunk/compilers/pirc/src/pbcout.h   Thu Apr 12 03:34:12 2007
@@ -2,8 +2,7 @@
 #  define __PBCOUT_H
 
 
-
-extern struct pirvtable *init_pbc_vtable(void);
+extern struct pirvtable *init_pbc_vtable(char *outputfile);
 
 #endif
 

Modified: trunk/compilers/pirc/src/pirmain.c
==============================================================================
--- trunk/compilers/pirc/src/pirmain.c  (original)
+++ trunk/compilers/pirc/src/pirmain.c  Thu Apr 12 03:34:12 2007
@@ -143,23 +143,22 @@
             vtable = new_pirvtable();
             break;
         case OUTPUT_PIR:
-            vtable = init_pir_vtable();
+            vtable = init_pir_vtable(outputfile);
             break;
         case OUTPUT_PAST:
-            vtable = init_past_vtable();
+            vtable = init_past_vtable(outputfile);
             break;
         case OUTPUT_JSON:
-            vtable = init_json_vtable();
+            vtable = init_json_vtable(outputfile);
             break;
         case OUTPUT_PBC:
-            vtable = init_pbc_vtable();
+            vtable = init_pbc_vtable(outputfile);
             break;
         default:
             fprintf(stderr, "Unknown output type specified\n");
             exit(1);
     }
 
-
     /* create a new parser, specifying the file name and the vtable */
     p = new_parser(argv[0], vtable);
 

Modified: trunk/compilers/pirc/src/pirout.c
==============================================================================
--- trunk/compilers/pirc/src/pirout.c   (original)
+++ trunk/compilers/pirc/src/pirout.c   Thu Apr 12 03:34:12 2007
@@ -8,6 +8,7 @@
 
 */
 #include "pirout.h"
+#include "pirutil.h"
 #include "pirvtable.h"
 #include "pirlexer.h"
 #include <stdio.h>
@@ -19,11 +20,13 @@
  *
  */
 typedef struct emit_data {
-    int indent;
+    char *outputfile;
+    FILE *file;
 
 } emit_data;
 
-#define OUT stderr
+
+
 
 /*
 
@@ -35,82 +38,94 @@
 
 static void
 pir_name(struct emit_data *data, char *name) {
-    fprintf(OUT, " %s ", name);
+    fprintf(data->file, " %s ", name);
 }
 
 static void
 pir_sub(struct emit_data *data) {
-    fprintf(OUT, ".sub");
+    fprintf(data->file, ".sub");
 }
 
 static void
 pir_end(struct emit_data *data) {
-    fprintf(OUT, ".end\n");
+    fprintf(data->file, ".end\n");
 }
 
 static void
 pir_newline(struct emit_data *data) {
-    fprintf(OUT, "\n");
+    fprintf(data->file, "\n");
 }
 
 static void
 pir_param(struct emit_data *data) {
-    fprintf(OUT, "  .param ");
+    fprintf(data->file, "  .param ");
 }
 
 static void
 pir_type(struct emit_data *data, char *type) {
-    fprintf(OUT, "%s", type);
+    fprintf(data->file, "%s", type);
 }
 
 static void
 pir_sub_flag(struct emit_data *data, int flag) {
-    fprintf(OUT, "%s ", find_keyword(flag));
+    fprintf(data->file, "%s ", find_keyword(flag));
 }
 
 static void
 pir_expr(struct emit_data *data, char *expr) {
-    fprintf(OUT, "%s ", expr);
+    fprintf(data->file, "%s ", expr);
 }
 
 static void
 pir_op(struct emit_data *data, char *op) {
-    fprintf(OUT, "  %s ", op);
+    fprintf(data->file, "  %s ", op);
 }
 
 
 static void
 pir_list_start(struct emit_data *data) {
-    fprintf(OUT, "(");
+    fprintf(data->file, "(");
 }
 
 static void
 pir_list_end(struct emit_data *data) {
-    fprintf(OUT, ")");
+    fprintf(data->file, ")");
 }
 
 static void
 pir_sub_flag_start(struct emit_data *data) {
-    fprintf(OUT, "");
+    fprintf(data->file, "");
 }
 
 
 static void
 pir_sub_flag_end(struct emit_data *data) {
-    fprintf(OUT, "\n");
+    fprintf(data->file, "\n");
 }
 
+/*
+
+Close the output file, free the emit_data structure.
+
+*/
 static void
 pir_destroy(emit_data *data) {
+    fclose(data->file);
     free(data);
     data = NULL;
 }
 
 static void
 pir_begin_return(emit_data *data) {
-    fprintf(OUT, " ");
+    fprintf(data->file, " ");
 }
 
+static void
+pir_init(emit_data *data) {
+    data->file = open_file(data->outputfile, "w");
+}
+
+
 /*
 
 =over 4
@@ -124,10 +139,12 @@
 
 */
 struct pirvtable *
-init_pir_vtable(void) {
+init_pir_vtable(char *outputfile) {
+
     pirvtable *vtable = new_pirvtable();
 
     /* override the methods that are needed for PIR output */
+    vtable->initialize     = pir_init;
     vtable->destroy        = pir_destroy;
     vtable->sub_start      = pir_sub;
     vtable->sub_end        = pir_end;
@@ -150,6 +167,9 @@
         fprintf(stderr, "Failed to allocate memory for vtable data\n");
         exit(1);
     }
+
+    vtable->data->outputfile = outputfile;
+
     return vtable;
 }
 

Modified: trunk/compilers/pirc/src/pirout.h
==============================================================================
--- trunk/compilers/pirc/src/pirout.h   (original)
+++ trunk/compilers/pirc/src/pirout.h   Thu Apr 12 03:34:12 2007
@@ -4,7 +4,7 @@
 /* predeclare */
 struct pirvtable;
 
-extern struct pirvtable *init_pir_vtable(void);
+extern struct pirvtable *init_pir_vtable(char *outputfile);
 
 #endif
 

Modified: trunk/compilers/pirc/src/pirutil.c
==============================================================================
--- trunk/compilers/pirc/src/pirutil.c  (original)
+++ trunk/compilers/pirc/src/pirutil.c  Thu Apr 12 03:34:12 2007
@@ -45,6 +45,27 @@
     return ptr;
 }
 
+/*
+
+=item open_file(char *filename, char *mode)
+
+Open the file C<filename> in mode C<mode>. If this fails, an error message
+is printed, and the program is terminated.
+
+=cut
+
+*/
+FILE *
+open_file(char *filename, char *mode) {
+    FILE *file = fopen(filename, mode);
+
+    if (file == NULL) {
+        fprintf(stderr, "Failed to open file '%s' in mode 
'%s'\nTerminating\n", filename, mode);
+        exit(1);
+    }
+
+    return file;
+}
 
 /*
 

Modified: trunk/compilers/pirc/src/pirutil.h
==============================================================================
--- trunk/compilers/pirc/src/pirutil.h  (original)
+++ trunk/compilers/pirc/src/pirutil.h  Thu Apr 12 03:34:12 2007
@@ -1,10 +1,15 @@
 #ifndef __PIRUTIL_H
 #  define __PIRUTIL_H
 
+
+#  include <stdio.h>
+
+
 /* command argument switches */
 typedef enum pirc_flags {
     PIRC_VERBOSE     = 1,
     PIRC_DEBUG       = 2,
+
 } pirc_flag;
 
 extern void printverbose(char *message);
@@ -13,13 +18,18 @@
 
 extern char *clone_string(char const *str);
 
+extern int is_op(char *id);
+
+extern FILE *open_file(char *filename, char *mode);
+
+
 /* where do we leave the flags?
 #  define verbose(P,S)  if (P->flags & PIRC_VERBOSE) printverbose(S)
 #  define debug(P,S)    if (P->flags & PIRC_DEBUG)   printdebug(S)
 */
 
 
-extern int is_op(char *id);
+
 
 #endif
 

Modified: trunk/compilers/pirc/src/pirvtable.c
==============================================================================
--- trunk/compilers/pirc/src/pirvtable.c        (original)
+++ trunk/compilers/pirc/src/pirvtable.c        Thu Apr 12 03:34:12 2007
@@ -58,6 +58,7 @@
 */
 pirvtable *
 new_pirvtable(void) {
+
     pirvtable *vtable = (pirvtable *)malloc(sizeof(pirvtable));
 
     if (vtable == NULL) {

Modified: trunk/compilers/pirc/src/pirvtable.h
==============================================================================
--- trunk/compilers/pirc/src/pirvtable.h        (original)
+++ trunk/compilers/pirc/src/pirvtable.h        Thu Apr 12 03:34:12 2007
@@ -1,16 +1,17 @@
 #ifndef __VTABLE_H
 #  define __VTABLE_H
 
-struct parser_state;
-
+/* predeclaration; the actual definition is left to the back-end(s) */
 struct emit_data;
 
+
+
 /* vtable that contains function pointers for emit
  * routines, and a pointer to some data. The definition of the
  * 'emit_data' structure is left to each back-end separately.
  */
 typedef struct pirvtable {
-    struct emit_data         *data; /* keep data here; where to store else? */
+    struct emit_data         *data; /* keep data here */
 
     /* initializer and destructor */
     void (* initialize)      (struct emit_data *data);
@@ -98,8 +99,10 @@
 #  define emit_invocation_start(P) (*P->vtable->invocation_start) 
(P->vtable->data)
 #  define emit_invocation_end(P)   (*P->vtable->invocation_end)   
(P->vtable->data)
 
+/* constructor */
 extern pirvtable *new_pirvtable(void);
 
+/* destructor */
 extern void destroy_pirvtable(pirvtable *vtable);
 
 

Reply via email to