Author: kjs
Date: Tue Dec  2 13:13:07 2008
New Revision: 33447

Modified:
   trunk/compilers/pirc/new/bcgen.c
   trunk/compilers/pirc/new/bcgen.h
   trunk/compilers/pirc/new/main.c
   trunk/compilers/pirc/new/pircompiler.h
   trunk/compilers/pirc/new/piremit.c
   trunk/compilers/pirc/new/piremit.h

Log:
[pirc] link bcgen to pirc sources. Use -b option to generate PBC. 
Unfortunately, doesn't work yet (wrong instructions in pbc file, but it /can/ 
be disassembled, which is good.)



Modified: trunk/compilers/pirc/new/bcgen.c
==============================================================================
--- trunk/compilers/pirc/new/bcgen.c    (original)
+++ trunk/compilers/pirc/new/bcgen.c    Tue Dec  2 13:13:07 2008
@@ -6,9 +6,9 @@
 #include <assert.h>
 #include "parrot/parrot.h"
 #include "parrot/embed.h"
-/*
+
 #include "bcgen.h"
-*/
+
 
 /* private bits of the bytecode generator */
 
@@ -32,7 +32,8 @@
 
 };
 
-typedef struct bytecode bytecode;
+/*typedef struct bytecode bytecode;
+*/
 
 static bc_const * new_const(bytecode * const bc);
 
@@ -190,6 +191,7 @@
         emit_opcode(bc, op);
 }
 
+static STRING *add_string_const_from_cstring(bytecode * const bc, char const * 
const str);
 /*
 
 XXX think of better name.
@@ -208,7 +210,7 @@
 Add a sub PMC to the constant table. This function initializes the sub PMC.
 
 */
-static void
+void
 add_sub_pmc(bytecode * const bc,
             char const * const subname, /* .sub foo --> "foo" */
             char const * const nsentry, /* .sub foo :nsentry('bar') --> "bar" 
*/
@@ -342,6 +344,7 @@
 Test driver.
 
 */
+/*
 int
 main(int argc, char **argv) {
     Interp *interp = Parrot_new(NULL);
@@ -359,6 +362,8 @@
     fprintf(stderr, "written pbc file\n");
 }
 
+*/
+
 
 /*
 

Modified: trunk/compilers/pirc/new/bcgen.h
==============================================================================
--- trunk/compilers/pirc/new/bcgen.h    (original)
+++ trunk/compilers/pirc/new/bcgen.h    Tue Dec  2 13:13:07 2008
@@ -6,24 +6,26 @@
 #ifndef PARROT_BCGEN_H_GUARD
 #define PARROT_BCGEN_H_GUARD
 
-
+#include "parrot/parrot.h"
+#include "parrot/embed.h"
 
 /* the type name is exported, but not its private bits */
 struct bytecode;
 
 typedef struct bytecode bytecode;
 
+struct bc_const;
 
 
-bytecode *new_bytecode(void);
+bytecode *new_bytecode(Interp *interp, char const * const filename, int bytes, 
int codesize);
 
 /* call this to write the PBC file */
-void write_pbc_file(bytecode * const bc);
+void write_pbc_file(bytecode * const bc, char const * const filename) ;
 
 
 /* emitting ops */
+void emit_opcode(bytecode * const bc, opcode_t op);
 
-void emit_opcode(bytecode * const bc, int opcode);
 
 /* does a look-up of the op by name, then emit that bytecode */
 void emit_op_by_name(bytecode * const bc, char const * const name);
@@ -33,24 +35,31 @@
 
 void emit_int_arg(bytecode * const bc, int argvalue);
 
+/*
 void emit_pmc_arg(bytecode * const bc, int pmc_const_index);
 
 void emit_num_arg(bytecode * const bc, int num_const_index);
 
 void emit_string_arg(bytecode * const bc, int string_const_index);
+*/
 
+struct bc_const * add_key_const(bytecode * const bc, PMC *key);
+struct bc_const * add_num_const(bytecode * const bc, FLOATVAL f);
+struct bc_const * add_string_const(bytecode * const bc, char const * const 
str);
+struct bc_const * add_pmc_const(bytecode * const bc, PMC * pmc) ;
 
 /* for adding constants */
 
 /* returns the id in the constant table */
+/*
 int add_pmc_const(bytecode * const bc);
 
 int add_string_const(bytecode * const bc, STRING *s);
 
-int add_num_const(bytecode * const bc, NUMVAL f);
+int add_num_const(bytecode * const bc, FLOATVAL f);
 
 int add_int_const(bytecode * const bc, INTVAL i);
-
+  */
 
 /* some functions to update constants */
 
@@ -68,6 +77,11 @@
 void remove_const(bytecode * const bc, int const_index);
 /* removes constant in slot C<const_index> from constant table */
 
+void add_sub_pmc(bytecode * const bc,
+            char const * const subname, char const * const nsentry, char const 
* const subid,
+            int vtable_index, int regs_used[]);
+
+
 #endif /* PARROT_BCGEN_H_GUARD */
 
 /*

Modified: trunk/compilers/pirc/new/main.c
==============================================================================
--- trunk/compilers/pirc/new/main.c     (original)
+++ trunk/compilers/pirc/new/main.c     Tue Dec  2 13:13:07 2008
@@ -15,6 +15,7 @@
 #include "pirlexer.h"
 #include "pirheredoc.h"
 #include "pirregalloc.h"
+#include "piremit.h"
 
 /* global variable to set parser in debug mode.
  * It is not clear to me whether the global can be replaced
@@ -66,8 +67,9 @@
 {
     fprintf(stderr, "Usage: %s [options] <file>\n", program_name);
     fprintf(stderr, "Options:\n\n"
-    "  -E        run heredoc and macro preprocessors only\n"
+    "  -b        generate bytecode\n"
     "  -d        show debug messages of parser\n"
+    "  -E        run heredoc and macro preprocessors only\n"
     "  -h        show this help message\n"
     "  -H        heredoc preprocessing only\n"
     "  -m <size> specify initial macro buffer size; default is 4096 bytes\n"
@@ -187,12 +189,14 @@
             fprintf(stdout, "ok\n");
         else if (TEST_FLAG(lexer->flags, LEXER_FLAG_PREPROCESS))
             emit_pir_subs(lexer);
-        else {
+        else if (TEST_FLAG(lexer->flags, LEXER_FLAG_OUTPUTPBC))
+            emit_pbc(lexer);
+        else
             /*
             fprintf(stderr, "Parse successful!\n");
             */
             print_subs(lexer);
-        }
+
 
         fclose(lexer->outfile);
 
@@ -269,6 +273,9 @@
      * the standard funtion for that, right now. This is a TODO. */
     while (argc > 0 && argv[0][0] == '-') {
         switch (argv[0][1]) {
+            case 'b':
+                SET_FLAG(flags, LEXER_FLAG_OUTPUTPBC);
+                break;
             case 'E':
                 SET_FLAG(flags, LEXER_FLAG_PREPROCESS);
                 break;

Modified: trunk/compilers/pirc/new/pircompiler.h
==============================================================================
--- trunk/compilers/pirc/new/pircompiler.h      (original)
+++ trunk/compilers/pirc/new/pircompiler.h      Tue Dec  2 13:13:07 2008
@@ -36,7 +36,8 @@
     LEXER_FLAG_HEREDOCONLY         = 1 << 5, /* preprocess heredocs only */
     LEXER_FLAG_NOOUTPUT            = 1 << 6, /* don't print anything on 
success, except 'ok' */
     LEXER_FLAG_REGALLOC            = 1 << 7, /* use register allocation 
optimizer */
-    LEXER_FLAG_PASMFILE            = 1 << 8  /* the input is PASM, not PIR 
code */
+    LEXER_FLAG_PASMFILE            = 1 << 8, /* the input is PASM, not PIR 
code */
+    LEXER_FLAG_OUTPUTPBC         = 1 << 9  /* generate PBC file */
 
 } lexer_flags;
 
@@ -141,6 +142,9 @@
     /* register allocation */
     lsr_allocator            *lsr;
 
+    /* bytecode generation */
+    struct bytecode                 *bc;
+
 } lexer_state;
 
 /* accessor for current macro; always first on the list. */

Modified: trunk/compilers/pirc/new/piremit.c
==============================================================================
--- trunk/compilers/pirc/new/piremit.c  (original)
+++ trunk/compilers/pirc/new/piremit.c  Tue Dec  2 13:13:07 2008
@@ -9,6 +9,8 @@
 
 #include <stdio.h>
 
+#include "bcgen.h"
+
 
 /*
 
@@ -16,9 +18,6 @@
 
 This file contains emit functions.
 
-XXX Take the print_* function as a template to create the
-PBC-generating functions.
-
 =over 4
 
 =cut
@@ -379,6 +378,7 @@
 
     /* emit the opcode */
 
+    emit_opcode(lexer->bc, instr->opcode);
 
     /* emit the arguments */
 
@@ -430,6 +430,12 @@
     if (lexer->subs == NULL)
         return;
 
+
+    /* XXX fix the numbers here */
+    lexer->bc = new_bytecode(lexer->interp, lexer->filename,
+                             lexer->instr_counter * 4, lexer->instr_counter);
+
+
     subiter = lexer->subs->next;
 
     /* initialize data structures in which PBC is going to be emitted. */
@@ -437,10 +443,15 @@
 
     /* iterate over all instructions and emit them */
     do {
+        add_sub_pmc(lexer->bc, subiter->sub_name, NULL, NULL, -1, 
subiter->regs_used);
+
         emit_pbc_instructions(lexer, subiter);
         subiter = subiter->next;
     }
     while (subiter != lexer->subs->next);
+
+    /* write the output to a file. */
+    write_pbc_file(lexer->bc, "a.pbc");
 }
 
 /*

Modified: trunk/compilers/pirc/new/piremit.h
==============================================================================
--- trunk/compilers/pirc/new/piremit.h  (original)
+++ trunk/compilers/pirc/new/piremit.h  Tue Dec  2 13:13:07 2008
@@ -10,6 +10,7 @@
 
 void print_subs(struct lexer_state * const lexer);
 void emit_pir_subs(struct lexer_state * const lexer);
+void emit_pbc(struct lexer_state * const lexer);
 
 #endif /* PARROT_PIR_PIREMIT_H_GUARD */
 

Reply via email to