Author: bernhard
Date: Wed Sep 28 15:05:47 2005
New Revision: 9264

Modified:
   trunk/languages/m4/examples/eval.imc
   trunk/languages/m4/src/eval.c
Log:
Fiddling with Parrot m4's arithmetic expression compiler.
Still broken, as PBC with the old calling convention is generated.


Modified: trunk/languages/m4/examples/eval.imc
==============================================================================
--- trunk/languages/m4/examples/eval.imc        (original)
+++ trunk/languages/m4/examples/eval.imc        Wed Sep 28 15:05:47 2005
@@ -1,6 +1,5 @@
 # $Id$
 
-
 =head1 NAME
 
 eval.imc - Integer arithmetic evaluation for Parrot m4
@@ -37,7 +36,7 @@ COMPILE_CODE:
     print expression
     print "\n"
     .local pmc compiled_code
-    compile compiled_code, m4_eval_compiler, expression
+    compiled_code = m4_eval_compiler( expression )
     if compiled_code goto INVOKE_COMPILED_CODE
        printerr "Could not get the compiler.\n"     
        end

Modified: trunk/languages/m4/src/eval.c
==============================================================================
--- trunk/languages/m4/src/eval.c       (original)
+++ trunk/languages/m4/src/eval.c       Wed Sep 28 15:05:47 2005
@@ -85,7 +85,7 @@ static eval_error          exp_term( eva
 static eval_error          unary_term( eval_token, eval_t * );
 static eval_error          simple_term( eval_token, eval_t * );
 boolean_for_m4             evaluate (const char *, eval_t *);
-PMC *                      m4_eval_compiler(Parrot_Interp, const char *);
+PMC *                      compile_m4_arithmetic_expression(Parrot_Interp, 
const char *);
 void                       Parrot_lib_m4_eval_compiler_init(Parrot_Interp , 
PMC* );
 
 /*--------------------.
@@ -795,7 +795,8 @@ $ make -C examples/compilers/
 void
 Parrot_lib_m4_eval_compiler_init(Parrot_Interp interp, PMC* lib)
 {
-    Parrot_compreg(interp, const_string(interp, "m4_eval_compiler"), 
m4_eval_compiler);
+    STRING *m4_eval_compiler = const_string(interp, "m4_eval_compiler");
+    Parrot_compreg(interp, m4_eval_compiler, compile_m4_arithmetic_expression);
 }
 
 
@@ -824,65 +825,88 @@ unescape(char *string)
 
 
 /*
- * simple compiler - no error checking
+ * This one is actually generating PBC.
+ * First the aritmetic expression is evaluated.
+ * Then we generaten a sub, that does nothing but return the result of the 
evaluation.
+ * I suppose that is cheating in a confuse way.
  */
 PMC *
-m4_eval_compiler( Parrot_Interp interp, const char *program )
+compile_m4_arithmetic_expression( Parrot_Interp interp, const char *program )
 {
     eval_t value;  /* will be returned to caller */
 
-    struct PackFile_ByteCode *cur_cs, *old_cs;
-    opcode_t* pc;
+    struct PackFile_ByteCode *new_cs;    /* generated PBC goes there */
+    struct PackFile_ByteCode *old_cs;    /* Continue there, when new_cs is 
finished */
+    opcode_t* program_counter;
     PMC *sub;
     parrot_sub_t sub_data;
 
-
     /*
      * The real work is done here
      */
     evaluate( program, &value );
 
+    /* This is from ast/ast_main.c
+     * What does this do?
+     */
+    /*
+    if (interp->imc_info->last_unit) {
+        imc_info = mem_sys_allocate_zeroed(sizeof(imc_info_t));
+        imc_info->ghash = interp->imc_info->ghash;
+        imc_info->prev = interp->imc_info;
+        interp->imc_info = imc_info;
+    }
+    */
+
+    /* m4_eval_compiler always compiles to interp->code->cur_cs
+     * make new, switch and save old cs
+     */
+    sprintf(name, "EVAL_" INTVAL_FMT, ++interp->code->base.pf->eval_nr);
+    new_cs = PF_create_default_segs(interp, name, 0);
+    old_cs = Parrot_switch_to_cs(interp, new_cs, 0);
+    interp->imc_info->cur_namespace = NULL;
+
     /*
      * need a packfile segment
      */
-    cur_cs = (struct PackFile_ByteCode*)PackFile_Segment_new_seg(interp, 
interp->code->base.dir, PF_BYTEC_SEG, "m4_eval_bc", 1);
-    old_cs = Parrot_switch_to_cs(interp, cur_cs, 0);
     /*
      * alloc byte code mem
      */
-    cur_cs->base.data = mem_sys_allocate(CODE_SIZE * sizeof(opcode_t));
-    cur_cs->base.size = CODE_SIZE;
+    new_cs->base.data = mem_sys_allocate(CODE_SIZE * sizeof(opcode_t));
+    new_cs->base.size = CODE_SIZE;
 
     /*
      * Generate some bytecode
+     * This is actually simulating the now obsolete calling conventions.
+     * TODO: Let the PIR-compiler generate the PBC
      */
-    pc = cur_cs->base.data;
+    program_counter = new_cs->base.data;
     /* set the single integer return value */
-    *pc++ = interp->op_lib->op_code("set_i_ic", 1);
-    *pc++ = 5;
-    *pc++ = value;
+    *program_counter++ = interp->op_lib->op_code("set_i_ic", 1);
+    *program_counter++ = 5;
+    *program_counter++ = value;
     /* promise to fill in the counters */
-    *pc++ = interp->op_lib->op_code("set_i_ic", 1);
-    *pc++ = 0;
-    *pc++ = 1;
+    *program_counter++ = interp->op_lib->op_code("set_i_ic", 1);
+    *program_counter++ = 0;
+    *program_counter++ = 1;
     /* one integer return value */
-    *pc++ = interp->op_lib->op_code("set_i_ic", 1);
-    *pc++ = 1;
-    *pc++ = 1;
+    *program_counter++ = interp->op_lib->op_code("set_i_ic", 1);
+    *program_counter++ = 1;
+    *program_counter++ = 1;
     /* no string return values */
-    *pc++ = interp->op_lib->op_code("set_i_ic", 1);
-    *pc++ = 2;
-    *pc++ = 0;
+    *program_counter++ = interp->op_lib->op_code("set_i_ic", 1);
+    *program_counter++ = 2;
+    *program_counter++ = 0;
     /* no PMC return values */
-    *pc++ = interp->op_lib->op_code("set_i_ic", 1);
-    *pc++ = 3;
-    *pc++ = 0;
+    *program_counter++ = interp->op_lib->op_code("set_i_ic", 1);
+    *program_counter++ = 3;
+    *program_counter++ = 0;
     /* no numeric return values */
-    *pc++ = interp->op_lib->op_code("set_i_ic", 1);
-    *pc++ = 4;
-    *pc++ = 0;
+    *program_counter++ = interp->op_lib->op_code("set_i_ic", 1);
+    *program_counter++ = 4;
+    *program_counter++ = 0;
     /* invoke the return continuation */
-    *pc++ = interp->op_lib->op_code("returncc", 1);
+    *program_counter++ = interp->op_lib->op_code("returncc", 1);
 
     if (old_cs) {
         /* restore old byte_code, */
@@ -893,9 +917,9 @@ m4_eval_compiler( Parrot_Interp interp, 
      */
     sub = pmc_new(interp, enum_class_Eval);
     sub_data = PMC_sub(sub);
-    sub_data->seg = cur_cs;
-    sub_data->address = cur_cs->base.data;
-    sub_data->end = cur_cs->base.data + cur_cs->base.size;
+    sub_data->seg = new_cs;
+    sub_data->address = new_cs->base.data;
+    sub_data->end = new_cs->base.data + new_cs->base.size;
     sub_data->name = string_from_cstring(interp, "m4 eval", 0);
 
     return sub;

Reply via email to