Author: kjs
Date: Mon Dec 29 07:15:17 2008
New Revision: 34577
Modified:
trunk/compilers/pirc/new/piremit.c
Log:
[pirc] optimize box_p_sc and box_p_nc also as set_p_pc. + refactor.
Modified: trunk/compilers/pirc/new/piremit.c
==============================================================================
--- trunk/compilers/pirc/new/piremit.c (original)
+++ trunk/compilers/pirc/new/piremit.c Mon Dec 29 07:15:17 2008
@@ -570,27 +570,20 @@
/*
=item C<static void
-emit_pbc_instr(lexer_state * const lexer, instruction * const instr)>
+optimize_instr(lexer_state * const lexer, instruction * const instr)>
-Emit PBC for one instruction.
+Optimize the instruction C<instr>. Currently, these instructions are optimized:
+
+ box_p_ic --> set_p_pc
+ box_p_nc --> set_p_pc
+ box_p_sc --> set_p_pc
=cut
*/
static void
-emit_pbc_instr(lexer_state * const lexer, instruction * const instr) {
- int i;
- expression *operand;
-
- /* emit the opcode */
+optimize_instr(lexer_state * const lexer, instruction * const instr) {
- if (instr->opinfo == NULL)
- return;
-
-
- /* optimize, if possible.
- * XXX is this a good place to do that?
- */
switch (instr->opcode) {
case PARROT_OP_box_p_ic: {
/* box P0, 42 --> set P0, <Integer PMC const with value 42> */
@@ -609,13 +602,76 @@
break;
}
- case PARROT_OP_box_p_nc:
+ case PARROT_OP_box_p_nc: {
+ /* box P0, 3.14 --> set P0, <Integer PMC const with value 3.14> */
+
+ /* the last operand, which is the second in this case */
+ expression *second_operand = instr->operands;
+ PMC *numconst = pmc_new(lexer->interp,
+ Parrot_get_ctx_HLL_type(lexer->interp,
enum_class_Float));
+ int index = add_pmc_const(lexer->bc, numconst);
+ VTABLE_set_number_native(lexer->interp, numconst,
second_operand->expr.c->val.nval);
+
+ instr->opcode = PARROT_OP_set_p_pc;
+
+ /* replace 2nd operand with the new one. */
+ second_operand->expr.c->val.ival = index;
+ second_operand->expr.c->type = INT_TYPE;
+
break;
- case PARROT_OP_box_p_sc:
+ }
+ case PARROT_OP_box_p_sc: {
+ /* box P0, "hi" --> set P0, <String PMC const with value "hi"> */
+
+ /* the last operand, which is the second in this case */
+ expression *second_operand = instr->operands;
+ PMC *strconst = pmc_new(lexer->interp,
+ Parrot_get_ctx_HLL_type(lexer->interp,
enum_class_String));
+ int index = add_pmc_const(lexer->bc, strconst);
+
+ VTABLE_set_string_native(lexer->interp, strconst,
+ string_from_cstring(lexer->interp,
+
second_operand->expr.c->val.sval,
+
strlen(second_operand->expr.c->val.sval)));
+
+ instr->opcode = PARROT_OP_set_p_pc;
+
+ /* replace 2nd operand with the new one. */
+ second_operand->expr.c->val.ival = index;
+ second_operand->expr.c->type = INT_TYPE;
+
break;
+ }
default:
break;
}
+}
+
+/*
+
+=item C<static void
+emit_pbc_instr(lexer_state * const lexer, instruction * const instr)>
+
+Emit PBC for one instruction.
+
+=cut
+
+*/
+static void
+emit_pbc_instr(lexer_state * const lexer, instruction * const instr) {
+ int i;
+ expression *operand;
+
+ /* emit the opcode */
+
+ if (instr->opinfo == NULL)
+ return;
+
+
+ /* optimize, if possible.
+ * XXX is this a good place to do that?
+ */
+ optimize_instr(lexer, instr);
emit_opcode(lexer->bc, instr->opcode);