/* Compiler.pmc
 *  Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
 *  CVS Info
 *     $Id: compiler.pmc,v 1.13 2003/08/28 13:17:01 leo Exp $
 *  Overview:
 *     The vtable functions for implementing assembler/compilers
 *  Data Structure and Algorithms:
 *  History:
 *     Initial revision by leo 2003/01/16
 *  Notes:
 *  References:
 *     pdd06_pasm.pod
 */

#include "parrot/parrot.h"
#include "parrot/method_util.h"
#include "parrot/interp_guts.h"

pmclass Compiler extends NCI need_ext {

    void* invoke (void * code_ptr) {
	/*
	 * compile source code
	 * return a new Eval pmc (which has a new PackFile attached
	 */
        Parrot_csub_t func = (Parrot_csub_t)D2FPTR(PMC_data(SELF));
	PMC *code_seg, *p5;
        struct PackFile *eval_pf;
	STRING *s5;

	/*
	 * preserve regs
	 */
	s5 = REG_STR(5);
	p5 = REG_PMC(5);
        REG_STR(5) = (String*) code_ptr;
	Parrot_push_i(interpreter, &REG_INT(0));

        func(INTERP, SELF);

	Parrot_pop_i(interpreter, &REG_INT(0));
	/* return value PMC is in P5 */
        code_seg = REG_PMC(5);
	REG_PMC(5) = p5;

	eval_pf = (struct PackFile *) PMC_data(code_seg);
	/* morph the Byte_cointer *pointer in code_seg to an invokable sub */
	code_seg->vtable = &Parrot_base_vtables[enum_class_Eval];
	VTABLE_init(interpreter, code_seg);
	VTABLE_set_integer_native(interpreter, code_seg,
		(INTVAL) eval_pf);
        return code_seg;
    }
}

