Author: kjs
Date: Thu Jan  1 12:00:43 2009
New Revision: 34760

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

Log:
[pirc] copy stuff for :instanceof from imcc. + function doc. + make a .yielding 
sub a coroutine.

Modified: trunk/compilers/pirc/new/bcgen.c
==============================================================================
--- trunk/compilers/pirc/new/bcgen.c    (original)
+++ trunk/compilers/pirc/new/bcgen.c    Thu Jan  1 12:00:43 2009
@@ -48,8 +48,8 @@
 
       // write opcodes
       int opcode = ...
-
       emit_opcode(bc, opcode);
+
       // emit constants
       int mystring = add_string_const(bc, "hello");
 
@@ -402,6 +402,49 @@
 }
 
 
+/*
+
+=item C<static void
+build_key(lexer_state * const lexer, key * const k)>
+
+=cut
+
+*/
+static void
+build_key(bytecode * const bc, opcode_t * key) {
+    PackFile_Constant   *pfc;
+    const opcode_t      *rc;
+    int                  index;
+
+    pfc   = mem_allocate_typed(PackFile_Constant);
+    rc    = PackFile_Constant_unpack_key(bc->interp, 
bc->interp->code->const_table, pfc, key);
+
+    if (!rc) {
+        mem_sys_free(pfc);
+        fprintf(stderr, "add_const_key: PackFile_Constant error\n");
+        exit(EXIT_FAILURE); /* XXX how to release memory? or maybe throw an 
exception? */
+    }
+
+    index = add_key_const(bc, pfc->u.key);
+
+
+    mem_sys_free(pfc);
+
+
+/*
+    const SymReg * const r =
+        _get_sym(&IMCC_INFO(interp)->globals->cs->key_consts, s_key);
+
+
+    if (r)
+        return r->color;
+
+    store_key_const(interp, s_key, k);
+
+    return k;
+*/
+
+}
 
 /* XXX remove or update prototype once the XXX below has been resolved. */
 static STRING *add_string_const_from_cstring(bytecode * const bc, char const * 
const str);
@@ -488,7 +531,7 @@
 /*
 
 =item C<static PMC *
-create_lexinfo()>
+create_lexinfo(bytecode * const bc, PMC * sub, lexical * const lexicals, int 
lexflag)>
 
 Create a lexinfo PMC for the sub C<sub>. If there are no lexicals,
 but the C<:lex> flag was specified, or the sub has an C<:outer> flag,
@@ -538,8 +581,12 @@
 
 /*
 
-Find the outer sub that has name C<outername>.
-If not found, NULL is returned.
+=item C<static PMC *
+find_outer_sub(bytecode * const bc, char const * const outername)>
+
+Find the outer sub that has name C<outername>. If not found, NULL is returned.
+
+=cut
 
 */
 static PMC *
@@ -603,18 +650,44 @@
 */
 int
 add_sub_pmc(bytecode * const bc, sub_info * const info, int needlex, int 
subpragmas) {
-    PMC                   * sub_pmc;
-    Parrot_sub            * sub;
-    int                     subconst_index;
-    int                     subname_index;
-    int                     i;
-    PackFile_Constant     * subname_const;
+    PMC                   *sub_pmc;
+    Parrot_sub            *sub;
+    int                    subconst_index;
+    int                    subname_index;
+    int                    i;
+    PackFile_Constant     *subname_const;
+    INTVAL                 type;
+
+    type = info->iscoroutine ? enum_class_Coroutine : enum_class_Sub;
+
+    /* Do we have to create an instance of a specific type for this sub? */
+    if (info->instanceof) {
+        /* Look it up as a class and as a PMC type. */
+        STRING * const classname
+            = string_from_cstring(bc->interp, info->instanceof + 1, 
strlen(info->instanceof) - 2);
+
+        PMC * const classobj = Parrot_oo_get_class_str(bc->interp, classname);
+
+        if (!PMC_IS_NULL(classobj))
+            sub_pmc = VTABLE_instantiate(bc->interp, classobj, PMCNULL);
+        else {
+            const INTVAL type = pmc_type(bc->interp, classname);
+
+            if (type <= 0)
+                Parrot_ex_throw_from_c_args(bc->interp, NULL, 
EXCEPTION_NO_CLASS,
+                    "Requested sub class '%Ss' in :instanceof() not found", 
classname);
 
+            sub_pmc = pmc_new(bc->interp, type);
+        }
+    }
+    else {
+        /* use a possible type mapping for the Sub PMCs, and create it */
+        type = Parrot_get_ctx_HLL_type(bc->interp, type);
+
+        /* TODO create constant - see also src/packfile.c */
+        sub_pmc = pmc_new(bc->interp, type);
+    }
 
-    /* The .sub is represented by a "Sub" PMC.
-     * If that should be changed into something else, fix that here (e.g. 
"Coroutine").
-     */
-    sub_pmc               = pmc_new(bc->interp, enum_class_Sub);
     sub                   = PMC_sub(sub_pmc);
     subname_index         = add_string_const(bc, info->subname);
     subname_const         = 
bc->interp->code->const_table->constants[subname_index];
@@ -638,6 +711,8 @@
 
     sub->multi_signature  = generate_multi_signature(bc, info->multi_types, 
info->num_multi_types);
 
+
+
     /* copy sub pragma flags such as :immediate etc. */
     PObj_get_FLAGS(sub_pmc)     |= subpragmas & SUB_FLAG_PF_MASK;
     Sub_comp_get_FLAGS(sub_pmc) |= subpragmas & SUB_COMP_FLAG_MASK;

Modified: trunk/compilers/pirc/new/bcgen.h
==============================================================================
--- trunk/compilers/pirc/new/bcgen.h    (original)
+++ trunk/compilers/pirc/new/bcgen.h    Thu Jan  1 12:00:43 2009
@@ -63,6 +63,7 @@
     char const    *nsentry;
     char const    *subid;
     char const    *outersub;
+    char const    *instanceof;
     int            vtable_index;
     unsigned       regs_used[4];
     int            startoffset;
@@ -71,6 +72,7 @@
     multi_type    *multi_types;   /* array with :multi data types, if this is 
a multi sub */
     lexical       *lexicals;
     multi_type    *name_space; /* can be a string or key */
+    int            iscoroutine;
 
 } sub_info;
 

Modified: trunk/compilers/pirc/new/pircompunit.c
==============================================================================
--- trunk/compilers/pirc/new/pircompunit.c      (original)
+++ trunk/compilers/pirc/new/pircompunit.c      Thu Jan  1 12:00:43 2009
@@ -240,7 +240,7 @@
 */
 void
 set_sub_instanceof(lexer_state * const lexer, char const * const classname) {
-    CURRENT_SUB(lexer)->instanceof = classname;
+    CURRENT_SUB(lexer)->info.instanceof = classname;
 }
 
 /*
@@ -1675,8 +1675,11 @@
             inv->sub    = va_arg(arg_ptr, target *);
             inv->method = va_arg(arg_ptr, expression *);
             break;
-        case CALL_RETURN:   /* no extra args */
         case CALL_YIELD: /* no extra args */
+            /* if there's a yield, the current subroutine is a coroutine */
+            CURRENT_SUB(lexer)->info.iscoroutine = TRUE;
+            break;
+        case CALL_RETURN:   /* no extra args */
         case CALL_TAILCALL:  /* no extra args */
         case CALL_METHOD_TAILCALL:
             break;

Modified: trunk/compilers/pirc/new/pircompunit.h
==============================================================================
--- trunk/compilers/pirc/new/pircompunit.h      (original)
+++ trunk/compilers/pirc/new/pircompunit.h      Thu Jan  1 12:00:43 2009
@@ -79,14 +79,14 @@
     PIRC_SUB_FLAG_POSTCOMP   = SUB_FLAG_PF_POSTCOMP,  /* executed after 
compilation */
     PIRC_SUB_FLAG_IMMEDIATE  = SUB_FLAG_PF_IMMEDIATE,  /* similar to POSTCOMP 
above; check PDD19 */
 
-    PIRC_SUB_FLAG_METHOD     = 1 << 10,  /* the sub is a method */
-    PIRC_SUB_FLAG_HAS_OUTER  = 1 << 11,  /* the sub is lexically nested */
-    PIRC_SUB_FLAG_IS_OUTER   = 1 << 12,  /* the sub contains lexically nested 
subs. */
-    PIRC_SUB_FLAG_VTABLE     = 1 << 13,  /* this sub overrides a vtable method 
*/
+    PIRC_SUB_FLAG_METHOD     = 1 << 10, /* the sub is a method */
+    PIRC_SUB_FLAG_HAS_OUTER  = 1 << 11, /* the sub is lexically nested */
+    PIRC_SUB_FLAG_IS_OUTER   = 1 << 12, /* the sub contains lexically nested 
subs. */
+    PIRC_SUB_FLAG_VTABLE     = 1 << 13, /* this sub overrides a vtable method 
*/
     PIRC_SUB_FLAG_LEX        = 1 << 14, /* this sub needs a LexPad */
     PIRC_SUB_FLAG_MULTI      = 1 << 15, /* this sub is a multi method/sub */
     PIRC_SUB_FLAG_SUBID      = 1 << 16, /* this sub has a namespace-unaware 
identifier */
-    PIRC_SUB_FLAG_INSTANCEOF = 1 << 17  /* this sub has an :instanceof flag. 
XXX document this */
+    PIRC_SUB_FLAG_INSTANCEOF = 1 << 17  /* this sub has an :instanceof flag */
 
 } sub_flag;
 
@@ -234,7 +234,7 @@
  * .return foo(), .return foo.bar(), .return x :flat
  */
 typedef struct invocation {
-    invoke_type         type;
+    invoke_type         type;          /* type of invocation (PCC, NCI, 
return/yield) */
     expression         *method;        /* method */
     target             *sub;           /* invoked sub, or the object on which 
method is invoked */
     target             *retcc;         /* return continuation, if any */
@@ -251,9 +251,9 @@
     char         const *label;        /* label of this instruction */
     char         const *opname;       /* name of the instruction, such as 
"print" and "set" */
     expression         *operands;     /* operands like "$I0" and "42" in "set 
$I0, 42" */
-    int                 oplabelbits;
+    int                 oplabelbits;  /* bits indicating which operands are 
labels */
     struct op_info_t   *opinfo;       /* pointer to the op_info containing 
this op's meta data */
-    int                 opcode;       /* the opcode of one of this op */
+    int                 opcode;       /* the opcode of this instruction */
 
     struct instruction *next;
 } instruction;
@@ -287,8 +287,8 @@
 
 /* hashtable structure */
 typedef struct hashtable {
-    bucket   **contents;
-    unsigned   size;
+    bucket   **contents;       /* array of bucket pointers */
+    unsigned   size;           /* number of slots in contents array */
     unsigned   obj_count;
 
 } hashtable;
@@ -303,12 +303,10 @@
 /* a sub */
 typedef struct subroutine {
     key                *name_space;    /* this sub's namespace */
-
-    char const         *instanceof;    /* XXX document this XXX */
     char const         *methodname;    /* name of this sub by which it's 
stored as a method */
     int                 flags;         /* this sub's flags */
 
-    struct sub_info     info;
+    struct sub_info     info;          /* see bcgen.h */
 
     target             *parameters;    /* parameters of this sub */
     instruction        *statements;    /* statements of this sub */

Modified: trunk/compilers/pirc/new/piremit.c
==============================================================================
--- trunk/compilers/pirc/new/piremit.c  (original)
+++ trunk/compilers/pirc/new/piremit.c  Thu Jan  1 12:00:43 2009
@@ -528,21 +528,6 @@
 }
 
 
-/*
-
-=item C<static void
-build_key(lexer_state * const lexer, key * const k)>
-
-=cut
-
-*/
-static void
-build_key(lexer_state * const lexer, key * const k) {
-    /* XXX TODO
-     *
-     * who can help? :-)
-     */
-}
 
 
 

Reply via email to