develooper Front page | perl.cvs.parrot | Postings from January 2009

[svn:parrot] r35018 - trunk/compilers/pirc/src

From:
kjs
Date:
January 6, 2009 03:16
Subject:
[svn:parrot] r35018 - trunk/compilers/pirc/src
Message ID:
20090106111611.4E49CCB9F9@x12.develooper.com
Author: kjs
Date: Tue Jan  6 03:16:10 2009
New Revision: 35018

Modified:
   trunk/compilers/pirc/src/bcgen.c
   trunk/compilers/pirc/src/bcgen.h
   trunk/compilers/pirc/src/pircompunit.c
   trunk/compilers/pirc/src/piremit.c

Log:
[pirc] more emit stuff for annotations.

Modified: trunk/compilers/pirc/src/bcgen.c
==============================================================================
--- trunk/compilers/pirc/src/bcgen.c	(original)
+++ trunk/compilers/pirc/src/bcgen.c	Tue Jan  6 03:16:10 2009
@@ -416,38 +416,41 @@
 
 /*
 
-=item C<void
+=item C<opcode_t
 emit_opcode(bytecode * const bc, opcode_t op)>
 
-Write the opcode C<op> into the bytecode stream.
+Write the opcode C<op> into the bytecode stream. The bytecode
+offset where the instruction is written is returned.
 
 =cut
 
 */
-void
+PARROT_IGNORABLE_RESULT
+opcode_t
 emit_opcode(bytecode * const bc, opcode_t op) {
-    *bc->opcursor++ = op;
+    *bc->opcursor = op;
     fprintf(stderr, "[%d]", op);
+    return (bc->opcursor++ - bc->interp->code->base.data);
 
 }
 
 /*
 
-=item C<void
+=item C<opcode_t
 emit_int_arg(bytecode * const bc, int intval)>
 
-Write an integer argument into the bytecode stream.
-XXX Possibly use 1 function for emitting opcodes and ints; they're
-the same anyway?
+Write an integer argument into the bytecode stream. The offset
+in bytecode where the instruction is written is returned.
 
 =cut
 
 */
-void
+PARROT_IGNORABLE_RESULT
+opcode_t
 emit_int_arg(bytecode * const bc, int intval) {
-    *bc->opcursor++ = intval;
+    *bc->opcursor = intval;
     fprintf(stderr, "{%d}", intval);
-
+    return (bc->opcursor++ - bc->interp->code->base.data);
 }
 
 

Modified: trunk/compilers/pirc/src/bcgen.h
==============================================================================
--- trunk/compilers/pirc/src/bcgen.h	(original)
+++ trunk/compilers/pirc/src/bcgen.h	Tue Jan  6 03:16:10 2009
@@ -87,16 +87,11 @@
 
 
 /* emitting ops */
-void emit_opcode(bytecode * const bc, opcode_t op);
-
-
-/* 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);
-
+opcode_t emit_opcode(bytecode * const bc, opcode_t op);
 
 /* emitting operands */
+opcode_t emit_int_arg(bytecode * const bc, int argvalue);
 
-void emit_int_arg(bytecode * const bc, int argvalue);
 
 /* storing constants in constant table */
 

Modified: trunk/compilers/pirc/src/pircompunit.c
==============================================================================
--- trunk/compilers/pirc/src/pircompunit.c	(original)
+++ trunk/compilers/pirc/src/pircompunit.c	Tue Jan  6 03:16:10 2009
@@ -2483,7 +2483,9 @@
 =item C<void
 annotate(lexer_state * const lexer, char const * const key, constant * const value)>
 
-Add a new annotation with key C<key> and value C<value>.
+Add a new annotation with key C<key> and value C<value>. This function assumes
+that there's an instruction in place, as it will store a pointer to the current
+instruction.
 
 =cut
 
@@ -2497,6 +2499,9 @@
     ann->bytecode_index = lexer->stmt_counter;
     ++lexer->num_annotations; /* keep track of number of annotations */
 
+    /* store a pointer to the current instruction */
+    ann->instr = CURRENT_INSTRUCTION(lexer);
+
     /* store the annotation in a list, managed by the lexer
      * the list is circular linked, so that the order of annotations is preserved.
      */

Modified: trunk/compilers/pirc/src/piremit.c
==============================================================================
--- trunk/compilers/pirc/src/piremit.c	(original)
+++ trunk/compilers/pirc/src/piremit.c	Tue Jan  6 03:16:10 2009
@@ -806,6 +806,7 @@
 emit_pbc_instr(lexer_state * const lexer, instruction * const instr) {
     int         i;
     expression *operand;
+    opcode_t offset;
 
     /* emit the opcode */
 
@@ -819,7 +820,19 @@
     optimize_instr(lexer, instr);
 
 
-    emit_opcode(lexer->bc, instr->opcode);
+    offset = emit_opcode(lexer->bc, instr->opcode);
+
+    /* the offset at which the instruction is written must be equal
+     * to the offset that we calculated ourselves. Sanity check!
+     */
+
+    /*
+    if (offset != instr->offset)
+        fprintf(stderr, "unexpected offset: %d / %d\n",  offset, instr->offset);
+    else
+        fprintf(stderr, "offset ok\n");
+
+    */
 
     /* emit the arguments */
 
@@ -875,6 +888,50 @@
     }
 }
 
+
+/*
+
+=item C<static void
+emit_pbc_annotations(lexer_state * const lexer)>
+
+Emit all annotations into the PackFile.
+
+=cut
+
+*/
+static void
+emit_pbc_annotations(lexer_state * const lexer) {
+    annotation *iter;
+
+    if (lexer->annotations == NULL)
+        return;
+
+#if 0
+
+    /* create an annotations segment, which is not created by default. */
+    create_annotations_segment(lexer->bc, lexer->interp->code->base.data);
+
+    /* initialize to the first annotation */
+    iter = lexer->annotations->next;
+
+    /* iterate over annotations and store them */
+    do {
+
+        /* add the key */
+        opcode_t key  = ....
+
+        opcode_t type = ...
+        opcode_t offset = (opcode_t)iter->instr->offset;
+
+        add_annotation(lexer->bc, offset, key, type, value);
+
+        iter = iter->next;
+    }
+    while (iter != lexer->annotations->next);
+
+#endif
+}
+
 /*
 
 =item C<void
@@ -921,6 +978,9 @@
     }
     while (subiter != lexer->subs->next);
 
+    /* emit annotations */
+    emit_pbc_annotations(lexer);
+
     /* write the output to a file. */
     write_pbc_file(lexer->bc, "a.pbc");  /* XXX fix output file specified by user */
 



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About