[svn:parrot] r42141 - in trunk/compilers/pirc: macro src

kjs at svn.parrot.org kjs at svn.parrot.org
Tue Oct 27 20:41:23 UTC 2009


Author: kjs
Date: Tue Oct 27 20:41:22 2009
New Revision: 42141
URL: https://trac.parrot.org/parrot/changeset/42141

Log:
[pirc] implement (first draft) of multi keys. not sure if works, but this looks sane and compiles

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

Modified: trunk/compilers/pirc/macro/macro.l
==============================================================================
--- trunk/compilers/pirc/macro/macro.l	Tue Oct 27 20:38:02 2009	(r42140)
+++ trunk/compilers/pirc/macro/macro.l	Tue Oct 27 20:41:22 2009	(r42141)
@@ -692,6 +692,13 @@
     return 0;
 }
 
+
+int
+main(int argc, char **argv) {
+    
+    return 0;   
+}
+
 /*
 
 =back

Modified: trunk/compilers/pirc/src/bcgen.c
==============================================================================
--- trunk/compilers/pirc/src/bcgen.c	Tue Oct 27 20:38:02 2009	(r42140)
+++ trunk/compilers/pirc/src/bcgen.c	Tue Oct 27 20:41:22 2009	(r42141)
@@ -16,6 +16,7 @@
 
 #include "bcgen.h" /* XXX future maybe parrot/bcgen.h */
 
+
 /* HEADERIZER HFILE: compilers/pirc/src/bcgen.h */
 
 /* HEADERIZER BEGIN: static */
@@ -824,8 +825,12 @@
                 break;
             }
             case MULTI_TYPE_KEYED: {
-                /* XXX implement this */
-
+                /* XXX not sure if this is correct. Reuse the emit_pbc_key infrastructure
+                   for emitting key bytecode. Need to see whether this works...
+                 */
+                int index = emit_pbc_key(bc, types[i].entry.key);
+                sig_pmc   = bc->interp->code->const_table->constants[index]->u.key;
+                
                 break;
             }
             default:
@@ -840,6 +845,115 @@
     return multi_signature;
 }
 
+/*
+
+=item C<void
+emit_pbc_key(lexer_state * const lexer, key * const k)>
+
+Emit bytecode for the key C<k>. First the bytecode is
+written to a temporary buffer, which is later unpacked
+in the actual PackFile. See C<store_key_bytecode()>.
+
+
+=cut
+
+*/
+int
+emit_pbc_key(bytecode * const bc, key * const k) {
+    key_entry  *iter;
+    opcode_t   *key;
+    opcode_t    keysize;    /* total size of key in bytecode */
+    opcode_t   *pc;         /* cursor to write into key array */
+    expression *operand;
+    int         index;
+
+    /* create an array of opcode_t for storing the bytecode representation
+     * of the key. Initialize the cursor (pc) to write into this buffer.
+     * The size is 2 opcode_t's for each key plus 1 opcode_t for storing the size.
+     */
+    pc  =
+    key = (opcode_t *)mem_sys_allocate((k->keylength * 2 + 1) * sizeof (opcode_t));
+
+    /* store key length in slot 0 */
+    *pc++ = k->keylength;
+
+    /* initialize iterator */
+    iter  = k->head;
+
+    while (iter) {
+        switch (iter->expr->type) {
+            case EXPR_CONSTANT: {
+                constant *c = iter->expr->expr.c;
+                switch (c->type) {
+                    case INT_VAL:
+                        *pc++ = PARROT_ARG_IC;
+                        *pc++ = c->val.ival;
+                        break;
+                    case STRING_VAL:
+                        *pc++ = PARROT_ARG_SC;
+                        *pc++ = add_string_const(bc, c->val.sval, "ascii");
+                        break;
+                    case USTRING_VAL:
+                        *pc++ = PARROT_ARG_SC;
+                        *pc++ = add_string_const(bc, c->val.ustr->contents,
+                                                            c->val.ustr->charset);
+                        break;
+                    default:
+                        fprintf(stderr, "wrong type of key");
+                        break;
+                }
+
+                break;
+            }
+            case EXPR_TARGET: {
+                target *t = iter->expr->expr.t;
+
+                switch (t->info->type) {
+                    case INT_TYPE:
+                        *pc++ = PARROT_ARG_I;
+                        *pc++ = t->info->color;
+                        break;
+                    case STRING_TYPE:
+                        *pc++ = PARROT_ARG_S;
+                        *pc++ = t->info->color;
+                        break;
+                    default:
+                        fprintf(stderr, "wrong type of key");
+                        break;
+                }
+                break;
+            }
+            case EXPR_KEY:
+                fprintf(stderr, "Nested keys are not supported.");
+                break;
+
+            default:
+                fprintf(stderr, "unknown expression type");
+                break;
+
+        }
+
+        iter = iter->next;
+    }
+
+    /* calculate size of key in bytecode; each field has 2 INTVALs:
+     * flags/types and the register/constant index.
+     */
+    keysize = pc - key;
+/*
+    fprintf(stderr, "key: ");
+    for (index = 0; index < keysize; ++index) {
+        fprintf(stderr, "%d|", key[index]);
+    }
+*/
+    /* store the key, and emit the index at which it's stored into the code segment */
+    index = store_key_bytecode(bc, key);
+    emit_int_arg(bc, index);
+
+    return index;
+
+}
+
 
 /*
 

Modified: trunk/compilers/pirc/src/bcgen.h
==============================================================================
--- trunk/compilers/pirc/src/bcgen.h	Tue Oct 27 20:38:02 2009	(r42140)
+++ trunk/compilers/pirc/src/bcgen.h	Tue Oct 27 20:41:22 2009	(r42141)
@@ -9,6 +9,7 @@
 #include "parrot/parrot.h"
 #include "parrot/embed.h"
 
+#include "pircompunit.h"
 
 /* the type name is exported, but not its private bits */
 struct bytecode;
@@ -35,7 +36,7 @@
 
     union multi_union {
         char const     *ident;
-        multi_key_type *key;
+        struct key     *key;
 
     } entry;
 
@@ -86,7 +87,7 @@
 } sub_info;
 
 struct lexer_state;
-struct _IMC_Unit;
+
 
 /* HEADERIZER BEGIN: compilers/pirc/src/bcgen.c */
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
@@ -282,6 +283,8 @@
 STRING *get_string_const(bytecode * const bc, unsigned index);
 
 
+int emit_pbc_key(bytecode * const bc, struct key * const k);
+
 /*
 
 int add_string_const(bytecode * const bc, STRING *s);

Modified: trunk/compilers/pirc/src/pircompunit.c
==============================================================================
--- trunk/compilers/pirc/src/pircompunit.c	Tue Oct 27 20:38:02 2009	(r42140)
+++ trunk/compilers/pirc/src/pircompunit.c	Tue Oct 27 20:41:22 2009	(r42141)
@@ -137,15 +137,15 @@
 
         switch (multitype->type) {
             case EXPR_CONSTANT:
-                mtype->entry.ident    = multitype->expr.c->val.sval;
-                mtype->entry_type = MULTI_TYPE_IDENT;
+                mtype->entry.ident = multitype->expr.c->val.sval;
+                mtype->entry_type  = MULTI_TYPE_IDENT;
                 break;
             case EXPR_IDENT:
-                mtype->entry.ident    = multitype->expr.id;
-                mtype->entry_type = MULTI_TYPE_IDENT;
+                mtype->entry.ident = multitype->expr.id;
+                mtype->entry_type  = MULTI_TYPE_IDENT;
                 break;
             case EXPR_KEY:
-                /* mtype->u.key      = XXX todo */
+                mtype->entry.key  = multitype->expr.k; 
                 mtype->entry_type = MULTI_TYPE_KEYED;
                 break;
             default:

Modified: trunk/compilers/pirc/src/piremit.c
==============================================================================
--- trunk/compilers/pirc/src/piremit.c	Tue Oct 27 20:38:02 2009	(r42140)
+++ trunk/compilers/pirc/src/piremit.c	Tue Oct 27 20:41:22 2009	(r42141)
@@ -552,114 +552,7 @@
 
 
 
-/*
-
-=item C<static void
-emit_pbc_key(lexer_state * const lexer, key * const k)>
-
-Emit bytecode for the key C<k>. First the bytecode is
-written to a temporary buffer, which is later unpacked
-in the actual PackFile. See C<store_key_bytecode()>.
-
-
-=cut
-
-*/
-static int
-emit_pbc_key(lexer_state * const lexer, key * const k) {
-    key_entry  *iter;
-    opcode_t   *key;
-    opcode_t    keysize;    /* total size of key in bytecode */
-    opcode_t   *pc;         /* cursor to write into key array */
-    expression *operand;
-    int         index;
-
-    /* create an array of opcode_t for storing the bytecode representation
-     * of the key. Initialize the cursor (pc) to write into this buffer.
-     * The size is 2 opcode_t's for each key plus 1 opcode_t for storing the size.
-     */
-    pc  =
-    key = (opcode_t *)pir_mem_allocate(lexer, (k->keylength * 2 + 1) * sizeof (opcode_t));
 
-    /* store key length in slot 0 */
-    *pc++ = k->keylength;
-
-    /* initialize iterator */
-    iter  = k->head;
-
-    while (iter) {
-        switch (iter->expr->type) {
-            case EXPR_CONSTANT: {
-                constant *c = iter->expr->expr.c;
-                switch (c->type) {
-                    case INT_VAL:
-                        *pc++ = PARROT_ARG_IC;
-                        *pc++ = c->val.ival;
-                        break;
-                    case STRING_VAL:
-                        *pc++ = PARROT_ARG_SC;
-                        *pc++ = add_string_const(lexer->bc, c->val.sval, "ascii");
-                        break;
-                    case USTRING_VAL:
-                        *pc++ = PARROT_ARG_SC;
-                        *pc++ = add_string_const(lexer->bc, c->val.ustr->contents,
-                                                            c->val.ustr->charset);
-                        break;
-                    default:
-                        panic(lexer, "wrong type of key");
-                        break;
-                }
-
-                break;
-            }
-            case EXPR_TARGET: {
-                target *t = iter->expr->expr.t;
-
-                switch (t->info->type) {
-                    case INT_TYPE:
-                        *pc++ = PARROT_ARG_I;
-                        *pc++ = t->info->color;
-                        break;
-                    case STRING_TYPE:
-                        *pc++ = PARROT_ARG_S;
-                        *pc++ = t->info->color;
-                        break;
-                    default:
-                        panic(lexer, "wrong type of key");
-                        break;
-                }
-                break;
-            }
-            case EXPR_KEY:
-                fprintf(stderr, "Nested keys are not supported.");
-                break;
-
-            default:
-                panic(lexer, "unknown expression type");
-                break;
-
-        }
-
-        iter = iter->next;
-    }
-
-    /* calculate size of key in bytecode; each field has 2 INTVALs:
-     * flags/types and the register/constant index.
-     */
-    keysize = pc - key;
-/*
-    fprintf(stderr, "key: ");
-    for (index = 0; index < keysize; ++index) {
-        fprintf(stderr, "%d|", key[index]);
-    }
-*/
-    /* store the key, and emit the index at which it's stored into the code segment */
-    index = store_key_bytecode(lexer->bc, key);
-    emit_int_arg(lexer->bc, index);
-
-    return index;
-
-}
 
 
 /*
@@ -681,7 +574,7 @@
 
     /* if t has a key, emit that as well */
     if (t->key) {
-        emit_pbc_key(lexer, t->key);
+        emit_pbc_key(lexer->bc, t->key);
     }
 }
 
@@ -710,7 +603,7 @@
             emit_pbc_label_arg(lexer, operand->expr.l);
             break;
         case EXPR_KEY:
-            emit_pbc_key(lexer, operand->expr.k);
+            emit_pbc_key(lexer->bc, operand->expr.k);
             break;
         /*
         case EXPR_IDENT:

Modified: trunk/compilers/pirc/src/piremit.h
==============================================================================
--- trunk/compilers/pirc/src/piremit.h	Tue Oct 27 20:38:02 2009	(r42140)
+++ trunk/compilers/pirc/src/piremit.h	Tue Oct 27 20:41:22 2009	(r42141)
@@ -14,6 +14,7 @@
 void emit_pir_subs(struct lexer_state * const lexer, char const * const outfile);
 void emit_pbc(struct lexer_state * const lexer, const char *outfile);
 
+
 int emit_pbc_const(struct lexer_state * const lexer, struct constant * const pirconst);
 
 #endif /* PARROT_PIR_PIREMIT_H_GUARD */


More information about the parrot-commits mailing list