- add support for opcode specific hash function
[libfirm] / ir / be / mips / mips_emitter.c
index 9f6e98c..a10de95 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
  * PURPOSE.
  */
 
-/* mips emitter */
-/* $Id$ */
+/**
+ * @file
+ * @brief   implementation of mips assembly emitter
+ * @author  Matthias Braun, Mehdi
+ * @version $Id$
+ */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -26,7 +30,6 @@
 #include <limits.h>
 
 #include "xmalloc.h"
-#include "tv.h"
 #include "iredges.h"
 #include "debug.h"
 #include "irgwalk.h"
 #include "irargs_t.h"
 #include "irprog_t.h"
 #include "irouts.h"
+#include "tv.h"
 #include "error.h"
 
 #include "../besched.h"
 #include "../benode_t.h"
 #include "../beutil.h"
+#include "../begnuas.h"
 
 #include "mips_emitter.h"
 #include "gen_mips_emitter.h"
 #include "mips_new_nodes.h"
 #include "mips_map_regs.h"
 
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
+
+#define BLOCK_PREFIX ".L"
+
 #define SNPRINTF_BUF_LEN 128
 
-static const arch_env_t *arch_env = NULL;
+static const mips_isa_t      *isa;
+static const arch_env_t      *arch_env;
+static const mips_code_gen_t *cg;
 
+/**
+ * Returns the register at in position pos.
+ */
+static const arch_register_t *get_in_reg(const ir_node *node, int pos)
+{
+       ir_node                *op;
+       const arch_register_t  *reg = NULL;
+
+       assert(get_irn_arity(node) > pos && "Invalid IN position");
+
+       /* The out register of the operator at position pos is the
+          in register we need. */
+       op = get_irn_n(node, pos);
+
+       reg = arch_get_irn_register(arch_env, op);
+
+       assert(reg && "no in register found");
+       return reg;
+}
+
+/**
+ * Returns the register at out position pos.
+ */
+static const arch_register_t *get_out_reg(const ir_node *node, int pos)
+{
+       ir_node                *proj;
+       const arch_register_t  *reg = NULL;
+
+       /* 1st case: irn is not of mode_T, so it has only                 */
+       /*           one OUT register -> good                             */
+       /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
+       /*           Proj with the corresponding projnum for the register */
+
+       if (get_irn_mode(node) != mode_T) {
+               reg = arch_get_irn_register(arch_env, node);
+       } else if (is_mips_irn(node)) {
+               reg = get_mips_out_reg(node, pos);
+       } else {
+               const ir_edge_t *edge;
+
+               foreach_out_edge(node, edge) {
+                       proj = get_edge_src_irn(edge);
+                       assert(is_Proj(proj) && "non-Proj from mode_T node");
+                       if (get_Proj_proj(proj) == pos) {
+                               reg = arch_get_irn_register(arch_env, proj);
+                               break;
+                       }
+               }
+       }
+
+       assert(reg && "no out register found");
+       return reg;
+}
 
 /*************************************************************
  *             _       _    __   _          _
@@ -63,6 +127,27 @@ static const arch_env_t *arch_env = NULL;
  * |_|                                       |_|
  *************************************************************/
 
+/**
+ * Emit the name of the source register at given input position.
+ */
+void mips_emit_source_register(const ir_node *node, int pos)
+{
+       const arch_register_t *reg = get_in_reg(node, pos);
+       be_emit_char('$');
+       be_emit_string(arch_register_get_name(reg));
+}
+
+/**
+ * Emit the name of the destination register at given output position.
+ */
+void mips_emit_dest_register(const ir_node *node, int pos)
+{
+       const arch_register_t *reg = get_out_reg(node, pos);
+       be_emit_char('$');
+       be_emit_string(arch_register_get_name(reg));
+}
+
+#if 0
 static const char *get_symconst_str(ir_node *node)
 {
        ident *id;
@@ -144,203 +229,67 @@ static const char *node_const_to_str(ir_node *n)
 
        return buf;
 }
+#endif
 
-/**
- * Returns node's offset as string.
- */
-static const char *node_offset_to_str(ir_node *n)
+void mips_emit_load_store_address(const ir_node *node, int pos)
 {
-       return "";
-}
+       const mips_load_store_attr_t *attr = get_mips_load_store_attr_const(node);
 
-/* We always pass the ir_node which is a pointer. */
-static int mips_get_arg_type(const lc_arg_occ_t *occ) {
-       return lc_arg_type_ptr;
+       be_emit_irprintf("%d(", attr->offset);
+       mips_emit_source_register(node, pos);
+       be_emit_char(')');
 }
 
-
-/**
- * Returns the register at in position pos.
- */
-static const arch_register_t *get_in_reg(ir_node *irn, int pos)
+void mips_emit_immediate_suffix(const ir_node *node, int pos)
 {
-       ir_node                *op;
-       const arch_register_t  *reg = NULL;
-
-       assert(get_irn_arity(irn) > pos && "Invalid IN position");
-
-       /* The out register of the operator at position pos is the
-          in register we need. */
-       op = get_irn_n(irn, pos);
-
-       reg = arch_get_irn_register(arch_env, op);
-
-       assert(reg && "no in register found");
-       return reg;
+       ir_node *op = get_irn_n(node, pos);
+       if(is_mips_Immediate(op))
+               be_emit_char('i');
 }
 
-/**
- * Returns the register at out position pos.
- */
-static const arch_register_t *get_out_reg(ir_node *irn, int pos)
+void mips_emit_immediate(const ir_node *node)
 {
-       ir_node                *proj;
-       const arch_register_t  *reg = NULL;
+       const mips_immediate_attr_t *attr = get_mips_immediate_attr_const(node);
 
-       /* 1st case: irn is not of mode_T, so it has only                 */
-       /*           one OUT register -> good                             */
-       /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
-       /*           Proj with the corresponding projnum for the register */
-
-       if (get_irn_mode(irn) != mode_T) {
-               reg = arch_get_irn_register(arch_env, irn);
-       }
-       else if (is_mips_irn(irn)) {
-               reg = get_mips_out_reg(irn, pos);
-       }
-       else {
-               const ir_edge_t *edge;
-
-               foreach_out_edge(irn, edge) {
-                       proj = get_edge_src_irn(edge);
-                       assert(is_Proj(proj) && "non-Proj from mode_T node");
-                       if (get_Proj_proj(proj) == pos) {
-                               reg = arch_get_irn_register(arch_env, proj);
-                               break;
-                       }
+       switch(attr->imm_type) {
+       case MIPS_IMM_CONST:
+               be_emit_irprintf("%d", attr->val);
+               break;
+       case MIPS_IMM_SYMCONST_LO:
+               be_emit_cstring("%lo($");
+               be_emit_ident(get_entity_ld_ident(attr->entity));
+               if(attr->val != 0) {
+                       be_emit_irprintf("%+d", attr->val);
                }
+               be_emit_char(')');
+               break;
+       case MIPS_IMM_SYMCONST_HI:
+               be_emit_cstring("%hi($");
+               be_emit_ident(get_entity_ld_ident(attr->entity));
+               if(attr->val != 0) {
+                       be_emit_irprintf("%+d", attr->val);
+               }
+               be_emit_char(')');
+               break;
+       default:
+               panic("invalid immediate type found");
        }
-
-       assert(reg && "no out register found");
-       return reg;
-}
-
-/**
- * Returns the number of the in register at position pos.
- */
-int get_mips_reg_nr(ir_node *irn, int pos, int in_out)
-{
-       const arch_register_t *reg;
-
-       if (in_out == 1) {
-               reg = get_in_reg(irn, pos);
-       }
-       else {
-               reg = get_out_reg(irn, pos);
-       }
-
-       return arch_register_get_index(reg);
-}
-
-/**
- * Returns the name of the in register at position pos.
- */
-const char *get_mips_reg_name(ir_node *irn, int pos, int in_out)
-{
-       const arch_register_t *reg;
-
-       if (in_out == 1) {
-               reg = get_in_reg(irn, pos);
-       }
-       else {
-               reg = get_out_reg(irn, pos);
-       }
-
-       return arch_register_get_name(reg);
-}
-
-/**
- * Get the register name for a node.
- */
-static int mips_get_reg_name(lc_appendable_t *app,
-    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
-{
-       const char *buf;
-       int res;
-       ir_node    *X  = arg->v_ptr;
-       int         nr = occ->width - 1;
-
-       if (!X)
-               return lc_arg_append(app, occ, "(null)", 6);
-
-       if (occ->conversion == 'S') {
-               buf = get_mips_reg_name(X, nr, 1);
-       }
-       else { /* 'D' */
-               buf = get_mips_reg_name(X, nr, 0);
-       }
-
-       res = lc_appendable_chadd(app, '$');
-       res += lc_appendable_snadd(app, buf, strlen(buf));
-       return res;
-}
-
-/**
- * Returns the tarval or offset of an mips node as a string.
- */
-static int mips_const_to_str(lc_appendable_t *app,
-    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
-{
-       const char *buf;
-       ir_node    *X = arg->v_ptr;
-
-       if (!X)
-               return lc_arg_append(app, occ, "(null)", 6);
-
-       if (occ->conversion == 'C') {
-               buf = node_const_to_str(X);
-       }
-       else { /* 'O' */
-               buf = node_offset_to_str(X);
-       }
-
-       return lc_arg_append(app, occ, buf, strlen(buf));
 }
 
 /**
- * Determines the SSE suffix depending on the mode.
+ * Emit the name of the destination register at given output position.
  */
-static int mips_get_mode_suffix(lc_appendable_t *app,
-    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
+void mips_emit_source_register_or_immediate(const ir_node *node, int pos)
 {
-       ir_node *X = arg->v_ptr;
-
-       if (!X)
-               return lc_arg_append(app, occ, "(null)", 6);
-
-       if (get_mode_size_bits(get_irn_mode(X)) == 32)
-               return lc_appendable_chadd(app, 's');
-       else
-               return lc_appendable_chadd(app, 'd');
-}
-
-/**
- * Return the mips printf arg environment.
- * We use the firm environment with some additional handlers.
- */
-const lc_arg_env_t *mips_get_arg_env(void)
-{
-       static lc_arg_env_t *env = NULL;
-
-       static const lc_arg_handler_t mips_reg_handler   = { mips_get_arg_type, mips_get_reg_name };
-       static const lc_arg_handler_t mips_const_handler = { mips_get_arg_type, mips_const_to_str };
-       static const lc_arg_handler_t mips_mode_handler  = { mips_get_arg_type, mips_get_mode_suffix };
-
-       if(env == NULL) {
-               /* extend the firm printer */
-               env = firm_get_arg_env();
-                       //lc_arg_new_env();
-
-               lc_arg_register(env, "mips:sreg", 'S', &mips_reg_handler);
-               lc_arg_register(env, "mips:dreg", 'D', &mips_reg_handler);
-               lc_arg_register(env, "mips:cnst", 'C', &mips_const_handler);
-               lc_arg_register(env, "mips:offs", 'O', &mips_const_handler);
-               lc_arg_register(env, "mips:mode", 'M', &mips_mode_handler);
+       const ir_node *op = get_irn_n(node, pos);
+       if(is_mips_Immediate(op)) {
+               mips_emit_immediate(op);
+       } else {
+               mips_emit_source_register(node, pos);
        }
-
-       return env;
 }
 
+#if 0
 /*
  * Add a number to a prefix. This number will not be used a second time.
  */
@@ -350,18 +299,20 @@ static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
        snprintf(buf, buflen, "%s%lu", prefix, ++id);
        return buf;
 }
+#endif
 
 /************************************************************************/
 /* ABI Handling                                                         */
 /************************************************************************/
 
-static void mips_emit_IncSP(const ir_node *node, mips_emit_env_t *env)
+static
+void mips_emit_IncSP(const ir_node *node)
 {
-       FILE *F      = env->out;
        int   offset = be_get_IncSP_offset(node);
 
        if(offset == 0) {
-               fprintf(F, "\t\t\t\t # omitted IncSP with 0\n");
+               be_emit_cstring("\t/* omitted IncSP with 0 */");
+               be_emit_finish_line_gas(node);
                return;
        }
 
@@ -370,85 +321,92 @@ static void mips_emit_IncSP(const ir_node *node, mips_emit_env_t *env)
        }
 
        if(offset > 0) {
-               fprintf(F, "\tsubu $sp, $sp, %d\n", offset);
+               be_emit_irprintf("\tsubu $sp, $sp, %d", offset);
        } else {
-               fprintf(F, "\taddu $sp, $sp, %d\n", -offset);
+               be_emit_irprintf("\taddu $sp, $sp, %d", -offset);
        }
+       be_emit_finish_line_gas(node);
 }
 
-static void mips_emit_Copy(const ir_node *node, mips_emit_env_t *env)
+static void mips_emit_Copy(const ir_node *node)
 {
-       FILE *F = env->out;
-
-       lc_efprintf(mips_get_arg_env(), F, "\tmove %1D, %1S\t\t\t# copy\n", node, node);
+       be_emit_cstring("\tmove ");
+       mips_emit_dest_register(node, 0);
+       be_emit_cstring(", ");
+       mips_emit_source_register(node, 0);
+       be_emit_finish_line_gas(node);
 }
 
-static void mips_emit_Return(const ir_node* node, mips_emit_env_t *env)
+static void mips_emit_Return(const ir_node* node)
 {
-       FILE *F = env->out;
-       fprintf(F, "\tj $ra\t\t\t\t# return\n");
+       be_emit_cstring("\tj $ra");
+       be_emit_finish_line_gas(node);
 }
 
-static void mips_emit_nops(FILE* F, int n)
+static __attribute__((unused))
+void mips_emit_nops(int n)
 {
        int i;
 
        for(i = 0; i < n; ++i) {
-               fprintf(F, "\tnop\n");
+               be_emit_cstring("\tnop\n");
+               be_emit_write_line();
        }
 }
 
-static void mips_emit_Perm(const ir_node *node, mips_emit_env_t *env)
+static void mips_emit_Perm(const ir_node *node)
 {
-       FILE *F = env->out;
-
        assert(get_irn_arity(node) == 2);
 
-       lc_efprintf(mips_get_arg_env(), F, "\txor %1S, %1S, %2S\t\t\t# perm\n", node, node, node);
-       mips_emit_nops(F, 3);
-       lc_efprintf(mips_get_arg_env(), F, "\txor %2S, %2S, %1S\n", node, node, node);
-       mips_emit_nops(F, 3);
-       lc_efprintf(mips_get_arg_env(), F, "\txor %1S, %1S, %2S\n", node, node, node);
-       mips_emit_nops(F, 3);
-}
+       be_emit_cstring("\txor ");
+       mips_emit_source_register(node, 0);
+       be_emit_cstring(", ");
+       mips_emit_source_register(node, 0);
+       be_emit_cstring(", ");
+       mips_emit_source_register(node, 1);
+       be_emit_finish_line_gas(node);
 
-static void mips_emit_Spill(const ir_node* node, mips_emit_env_t *env)
-{
-       FILE      *F   = env->out;
-       ir_entity *ent = be_get_frame_entity(node);
+       /* mips_emit_nops(3); */
 
-       lc_efprintf(mips_get_arg_env(), F, "\tsw %1S, %d($fp)\n", node, get_entity_offset(ent));
-}
+       be_emit_cstring("\txor ");
+       mips_emit_source_register(node, 1);
+       be_emit_cstring(", ");
+       mips_emit_source_register(node, 1);
+       be_emit_cstring(", ");
+       mips_emit_source_register(node, 0);
+       be_emit_finish_line_gas(node);
 
-static void mips_emit_Reload(const ir_node* node, mips_emit_env_t *env)
-{
-       FILE      *F   = env->out;
-       ir_entity *ent = be_get_frame_entity(node);
+       /* mips_emit_nops(3); */
 
-       lc_efprintf(mips_get_arg_env(), F, "\tlw %1D, %d($fp)\n", node, get_entity_offset(ent));
+       be_emit_cstring("\txor ");
+       mips_emit_source_register(node, 0);
+       be_emit_cstring(", ");
+       mips_emit_source_register(node, 0);
+       be_emit_cstring(", ");
+       mips_emit_source_register(node, 1);
+       be_emit_finish_line_gas(node);
+
+       /* mips_emit_nops(3); */
 }
 
 /************************************************************************/
 /* Calls                                                                */
 /************************************************************************/
 
-static void mips_emit_Call(ir_node *node, mips_emit_env_t *env)
+static void mips_emit_Call(const ir_node *node)
 {
-       FILE *F = env->out;
-       const arch_register_t *callee_reg;
+       ir_entity *callee;
+
+       be_emit_cstring("\tjal ");
 
-       // call to imediate value (label)
-       ir_entity *callee = be_Call_get_entity(node);
+       /* call of immediate value (label) */
+       callee = be_Call_get_entity(node);
        if(callee != NULL) {
-               fprintf(F, "\tjal %s\n", get_entity_name(callee));
-               return;
+               be_emit_ident(get_entity_ld_ident(callee));
+       } else {
+               mips_emit_source_register(node, be_pos_Call_ptr);
        }
-
-       // call to function pointer
-       callee_reg = get_in_reg(node, be_pos_Call_ptr);
-       assert(callee_reg != NULL);
-
-       fprintf(F, "\tjal %s\n", arch_register_get_name(callee_reg));
+       be_emit_finish_line_gas(node);
 }
 
 /************************************************************************
@@ -468,17 +426,32 @@ const char* mips_get_block_label(const ir_node* block)
        return buf;
 }
 
-static void mips_emit_Jump(ir_node *node, mips_emit_env_t *env)
+/**
+ * Emits a block label from the given block.
+ */
+static void mips_emit_block_label(const ir_node *block)
 {
-       FILE *F = env->out;
-       const ir_node *block = get_irn_link(node);
+       if (has_Block_label(block)) {
+               be_emit_string(be_gas_block_label_prefix());
+               be_emit_irprintf("%lu", get_Block_label(block));
+       } else {
+               be_emit_cstring(BLOCK_PREFIX);
+               be_emit_irprintf("%ld", get_irn_node_nr(block));
 
+       }
+}
+
+static void mips_emit_Jump(const ir_node *node)
+{
+       const ir_node *block = get_irn_link(node);
        assert(is_Block(block));
 
-       fprintf(F, "\tb %s\n", mips_get_block_label(block));
+       be_emit_cstring("\tb ");
+       mips_emit_block_label(block);
+       be_emit_finish_line_gas(node);
 }
 
-ir_node *mips_get_jump_block(const ir_node* node, int projn)
+ir_node *mips_get_jump_block(const ir_node* node, long projn)
 {
        const ir_edge_t *oute;
        for(oute = get_irn_out_edge_first(node); oute != NULL;
@@ -495,16 +468,43 @@ ir_node *mips_get_jump_block(const ir_node* node, int projn)
        return NULL;
 }
 
+void mips_emit_jump_target_proj(const ir_node *node, long projn)
+{
+       ir_node *jumpblock = mips_get_jump_block(node, projn);
+       assert(jumpblock != NULL);
+
+       mips_emit_block_label(jumpblock);
+}
+
+void mips_emit_jump_target(const ir_node *node)
+{
+       ir_node *jumpblock = get_irn_link(node);
+       assert(jumpblock != NULL);
+
+       mips_emit_block_label(jumpblock);
+}
+
+void mips_emit_jump_or_fallthrough(const ir_node *node, long pn)
+{
+       ir_node *jumpblock = mips_get_jump_block(node, pn);
+       assert(jumpblock != NULL);
+
+       /* TODO: use fallthrough when possible */
+       be_emit_cstring("b ");
+       mips_emit_block_label(jumpblock);
+}
+
 /************************************************************************
- *  ____          _ _       _         _                                                                        *
- * / ___|_      _(_) |_ ___| |__     | |_   _ _ __ ___  _ __                   *
- * \___ \ \ /\ / / | __/ __| '_ \ _  | | | | | '_ ` _ \| '_ \                  *
- *  ___) \ V  V /| | || (__| | | | |_| | |_| | | | | | | |_) |                 *
- * |____/ \_/\_/ |_|\__\___|_| |_|\___/ \__,_|_| |_| |_| .__/                  *
- *                                                     |_|                             *
+ *  ____          _ _       _         _                                 *
+ * / ___|_      _(_) |_ ___| |__     | |_   _ _ __ ___  _ __            *
+ * \___ \ \ /\ / / | __/ __| '_ \ _  | | | | | '_ ` _ \| '_ \           *
+ *  ___) \ V  V /| | || (__| | | | |_| | |_| | | | | | | |_) |          *
+ * |____/ \_/\_/ |_|\__\___|_| |_|\___/ \__,_|_| |_| |_| .__/           *
+ *                                                     |_|              *
  *                                                                      *
  ************************************************************************/
 
+#if 0
 /* jump table entry (target and corresponding number) */
 typedef struct _branch_t {
        ir_node *target;
@@ -524,7 +524,8 @@ typedef struct _jmp_tbl_t {
 /**
  * Compare two variables of type branch_t. Used to sort all switch cases
  */
-static int mips_cmp_branch_t(const void *a, const void *b) {
+static int mips_cmp_branch_t(const void *a, const void *b)
+{
        branch_t *b1 = (branch_t *)a;
        branch_t *b2 = (branch_t *)b;
 
@@ -546,12 +547,13 @@ const char* mips_get_jumptbl_label(const ir_node* switchjmp)
  * Emits code for a SwitchJmp (creates a jump table if
  * possible otherwise a cmp-jmp cascade). Stolen from ia32
  */
-void emit_mips_jump_table(const ir_node *irn, FILE* F) {
-       int              lastval, i, i2, pn;
-       jmp_tbl_t        tbl;
-       ir_node         *proj;
-       const ir_edge_t *edge;
-       mips_attr_t     *attr = get_mips_attr(irn);
+void emit_mips_jump_table(const ir_node *irn)
+{
+       int                lastval, i, i2, pn;
+       jmp_tbl_t          tbl;
+       ir_node           *proj;
+       const ir_edge_t   *edge;
+       const mips_attr_t *attr = get_mips_attr_const(irn);
 
        /* fill the table structure */
        tbl.label        = xmalloc(SNPRINTF_BUF_LEN);
@@ -583,17 +585,26 @@ void emit_mips_jump_table(const ir_node *irn, FILE* F) {
        /* sort the branches by their number */
        qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), mips_cmp_branch_t);
 
-       fprintf(F, "%s:\n", mips_get_jumptbl_label(irn));
+       be_emit_string(mips_get_jumptbl_label(irn));
+       be_emit_cstring(":\n");
+       be_emit_write_line();
        lastval = tbl.min_value;
        for(i = 0; i < tbl.num_branches; ++i) {
                const branch_t *branch = &tbl.branches[i];
                int value = branch->value;
 
                for(i2 = lastval + 1; i2 < value; ++i2) {
-                       fprintf(F, "\t.word %s\n", get_id_str(attr->symconst_id));
+                       be_emit_cstring("\t.word ");
+                       be_emit_ident(get_entity_ld_ident(attr->symconst));
+                       be_emit_char('\n');
+                       be_emit_write_line();
                }
 
-               fprintf(F, "\t.word %s\n", mips_get_block_label(branch->target));
+               be_emit_cstring("\t.word ");
+               mips_emit_block_label(branch->target);
+               be_emit_char('\n');
+               be_emit_write_line();
+
                lastval = branch->value;
        }
 
@@ -603,17 +614,22 @@ void emit_mips_jump_table(const ir_node *irn, FILE* F) {
                free(tbl.branches);
 }
 
-static void dump_jump_tables(ir_node* node, void *env)
+static void dump_jump_tables(ir_node* node, void *data)
 {
-       FILE* F = (FILE*) env;
+       (void) data;
 
        // emit jump tables
        if(is_mips_SwitchJump(node)) {
-               fprintf(F, ".data\n");
-               emit_mips_jump_table(node, F);
-               fprintf(F, ".text\n");
+               be_emit_cstring(".data\n");
+               be_emit_write_line();
+
+               emit_mips_jump_table(node);
+
+               be_emit_cstring(".text\n");
+               be_emit_write_line();
        }
 }
+#endif
 
 /***********************************************************************************
  *                  _          __                                             _
@@ -625,13 +641,27 @@ static void dump_jump_tables(ir_node* node, void *env)
  *
  ***********************************************************************************/
 
-static void mips_emit_nothing(ir_mode *mode, mips_emit_env_t *env)
+static void mips_emit_nothing(const ir_node *node)
+{
+       (void) node;
+}
+
+static void mips_emit_this_shouldnt_happen(const ir_node *node)
 {
+       panic("Found non-lowered node %+F while emitting", node);
 }
 
-static void mips_emit_this_shouldnt_happen(ir_mode *mode, mips_emit_env_t *env)
+/**
+ * The type of a emitter function.
+ */
+typedef void (*emit_func) (const ir_node *);
+
+/**
+ * Set a node emitter. Make it a bit more type safe.
+ */
+static void register_emitter(ir_op *op, emit_func func)
 {
-       assert(0);
+       op->ops.generic = (op_func) func;
 }
 
 /**
@@ -646,50 +676,40 @@ void mips_register_emitters(void)
        mips_register_spec_emitters();
 
        /* benode emitter */
-       op_be_IncSP->ops.generic = (op_func) mips_emit_IncSP;
-       op_be_SetSP->ops.generic = (op_func) mips_emit_this_shouldnt_happen;
-       op_be_AddSP->ops.generic = (op_func) mips_emit_this_shouldnt_happen;
-       op_be_Call->ops.generic = (op_func) mips_emit_Call;
-       op_be_Keep->ops.generic = (op_func) mips_emit_nothing;
-       op_be_Copy->ops.generic = (op_func) mips_emit_Copy;
-       op_be_Return->ops.generic = (op_func) mips_emit_Return;
-       op_be_RegParams->ops.generic = (op_func) mips_emit_nothing;
-       op_be_Spill->ops.generic = (op_func) mips_emit_Spill;
-       op_be_Reload->ops.generic = (op_func) mips_emit_Reload;
-       op_be_Perm->ops.generic = (op_func) mips_emit_Perm;
-
-       op_Start->ops.generic = (op_func) mips_emit_nothing;
-       op_Proj->ops.generic = (op_func) mips_emit_nothing;
-       op_SymConst->ops.generic = (op_func) mips_emit_nothing;
-       op_Jmp->ops.generic = (op_func) mips_emit_Jump;
-       op_Cmp->ops.generic = (op_func) mips_emit_this_shouldnt_happen;
-       op_Cond->ops.generic = (op_func) mips_emit_this_shouldnt_happen;
-}
-
-typedef void (*emit_func) (const ir_node *, mips_emit_env_t *);
+       register_emitter(op_be_IncSP, mips_emit_IncSP);
+       register_emitter(op_be_AddSP, mips_emit_this_shouldnt_happen);
+       register_emitter(op_be_Call, mips_emit_Call);
+       register_emitter(op_be_Copy, mips_emit_Copy);
+       register_emitter(op_be_Keep, mips_emit_nothing);
+       register_emitter(op_be_Barrier, mips_emit_nothing);
+       register_emitter(op_be_Return, mips_emit_Return);
+       register_emitter(op_be_RegParams, mips_emit_nothing);
+       register_emitter(op_be_Spill, mips_emit_this_shouldnt_happen);
+       register_emitter(op_be_Reload, mips_emit_this_shouldnt_happen);
+       register_emitter(op_be_Perm, mips_emit_Perm);
+
+       register_emitter(op_Start, mips_emit_nothing);
+       register_emitter(op_Proj, mips_emit_nothing);
+       register_emitter(op_SymConst, mips_emit_this_shouldnt_happen);
+       register_emitter(op_Const, mips_emit_this_shouldnt_happen);
+       register_emitter(op_Jmp, mips_emit_Jump);
+       register_emitter(op_Cmp, mips_emit_this_shouldnt_happen);
+       register_emitter(op_Cond, mips_emit_this_shouldnt_happen);
+       register_emitter(op_Phi, mips_emit_nothing);
+}
 
 /**
  * Emits assembly for a single node
  */
-static void mips_emit_node(ir_node *irn, mips_emit_env_t* env)
+static void mips_emit_node(const ir_node *node)
 {
-       mips_emit_env_t   *emit_env = env;
-       FILE              *F        = emit_env->out;
-       ir_op             *op       = get_irn_op(irn);
-       DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
-
-       DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
+       ir_op *op = get_irn_op(node);
 
        if (op->ops.generic) {
                emit_func emit = (emit_func) op->ops.generic;
-               (*emit) (irn, env);
-
-#if 0
-               if(emit != (emit_func) mips_emit_nothing)
-                       mips_emit_nops(F, 5);
-#endif
+               (*emit) (node);
        } else {
-               ir_fprintf(F, "\t\t\t\t\t# %+F\n", irn);
+               panic("No emitter defined for node %+F", node);
        }
 }
 
@@ -697,59 +717,81 @@ static void mips_emit_node(ir_node *irn, mips_emit_env_t* env)
  * Walks over the nodes in a block connected by scheduling edges
  * and emits code for each node.
  */
-void mips_gen_block(ir_node *block, void *env)
+void mips_gen_block(const ir_node *block)
 {
-       FILE *F = ((mips_emit_env_t *)env)->out;
-       ir_node *irn;
+       ir_node *node;
 
        if (! is_Block(block))
                return;
 
-       fprintf(F, "%s:\n", mips_get_block_label(block));
-       sched_foreach(block, irn) {
-               mips_emit_node(irn, env);
+       mips_emit_block_label(block);
+       be_emit_cstring(":\n");
+       be_emit_write_line();
+
+       sched_foreach(block, node) {
+               mips_emit_node(node);
        }
-       fprintf(F, "\n");
+
+       be_emit_char('\n');
+       be_emit_write_line();
 }
 
 /**
  * Emits code for function start.
  */
-void mips_emit_start(FILE *F, ir_graph *irg)
+void mips_emit_func_prolog(ir_graph *irg)
 {
-       const char *irg_name = get_entity_name(get_irg_entity(irg));
+       ident *irg_ident = get_entity_ld_ident(get_irg_entity(irg));
 
        // dump jump tables
-       irg_walk_graph(irg, NULL, dump_jump_tables, F);
+       //irg_walk_graph(irg, NULL, dump_jump_tables, env);
 
-       fprintf(F, "\n\n");
-       fprintf(F, "\t.balign\t4\n");
-       fprintf(F, "\t.global\t%s\n", irg_name);
-       fprintf(F, "\t.set\tnomips16\n");
-       fprintf(F, "\t.ent\t%s\n", irg_name);
-       fprintf(F, "%s:\n", irg_name);
-       fprintf(F, "\t.frame\t$fp, 24, $ra\n");
-       fprintf(F, "\t.mask\t0xc0000000, -4\n");
-       fprintf(F, "\t.fmask\t0x00000000, 0\n");
+       be_emit_write_line();
+       be_gas_emit_switch_section(GAS_SECTION_TEXT);
+
+       be_emit_cstring("\t.balign\t4\n");
+
+       be_emit_cstring("\t.global\t");
+       be_emit_ident(irg_ident);
+       be_emit_char('\n');
+
+       be_emit_cstring("\t.set\tnomips16\n");
+
+       be_emit_cstring("\t.ent\t");
+       be_emit_ident(irg_ident);
+       be_emit_char('\n');
+
+       be_emit_ident(irg_ident);
+       be_emit_cstring(":\n");
+
+       be_emit_cstring("\t.frame\t$fp, 24, $ra\n");
+       be_emit_cstring("\t.mask\t0xc0000000, -4\n");
+       be_emit_cstring("\t.fmask\t0x00000000, 0\n");
+
+       be_emit_write_line();
 }
 
 /**
  * Emits code for function end
  */
-void mips_emit_end(FILE *F, ir_graph *irg)
+void mips_emit_func_epilog(ir_graph *irg)
 {
-       const char *irg_name = get_entity_name(get_irg_entity(irg));
-       fprintf(F, "\t.end\t%s\n", irg_name);
+       ident *irg_ident = get_entity_ident(get_irg_entity(irg));
+
+       be_emit_cstring("\t.end\t");
+       be_emit_ident(irg_ident);
+       be_emit_char('\n');
+       be_emit_write_line();
 }
 
 /**
  * Sets labels for control flow nodes (jump target)
- * TODO: Jump optimization
  */
 void mips_gen_labels(ir_node *block, void *env)
 {
        ir_node *pred;
        int n = get_Block_n_cfgpreds(block);
+       (void) env;
 
        for (n--; n >= 0; n--) {
                pred = get_Block_cfgpred(block, n);
@@ -760,29 +802,31 @@ void mips_gen_labels(ir_node *block, void *env)
 /**
  * Main driver
  */
-void mips_gen_routine(FILE *F, ir_graph *irg, const mips_code_gen_t *cg)
+void mips_gen_routine(mips_code_gen_t *mips_cg, ir_graph *irg)
 {
-       mips_emit_env_t emit_env;
        int i, n;
 
-       emit_env.out      = F;
-       emit_env.arch_env = cg->arch_env;
-       emit_env.cg       = cg;
-       FIRM_DBG_REGISTER(emit_env.mod, "firm.be.mips.emit");
-
-       /* set the global arch_env (needed by print hooks) */
+       cg       = mips_cg;
+       isa      = (const mips_isa_t*) cg->arch_env;
        arch_env = cg->arch_env;
 
-       irg_block_walk_graph(irg, mips_gen_labels, NULL, &emit_env);
-       mips_emit_start(F, irg);
-//     irg_walk_blkwise_graph(irg, NULL, mips_gen_block, &emit_env);
+       mips_register_emitters();
+
+       irg_block_walk_graph(irg, mips_gen_labels, NULL, NULL);
+
+       mips_emit_func_prolog(irg);
 
        dump_ir_block_graph_sched(irg, "-kaputtelist");
 
        for (i = 0, n = mips_get_sched_n_blocks(cg); i < n; ++i) {
                ir_node *block = mips_get_sched_block(cg, i);
-               mips_gen_block(block, &emit_env);
+               mips_gen_block(block);
        }
 
-       mips_emit_end(F, irg);
+       mips_emit_func_epilog(irg);
+}
+
+void mips_init_emitter(void)
+{
+       FIRM_DBG_REGISTER(dbg, "firm.be.mips.emitter");
 }