Commit d7b29b59 authored by Matthias Braun's avatar Matthias Braun
Browse files

introduce TEMPLATE_emitf

parent 60b3885b
......@@ -46,7 +46,7 @@
#include "TEMPLATE_nodes_attr.h"
#include "TEMPLATE_new_nodes.h"
void TEMPLATE_emit_immediate(const ir_node *node)
static void TEMPLATE_emit_immediate(const ir_node *node)
{
const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
be_emit_irprintf("%T", attr->value);
......@@ -57,13 +57,13 @@ static void emit_register(const arch_register_t *reg)
be_emit_string(arch_register_get_name(reg));
}
void TEMPLATE_emit_source_register(const ir_node *node, int pos)
static void TEMPLATE_emit_source_register(const ir_node *node, int pos)
{
const arch_register_t *reg = arch_get_irn_register_in(node, pos);
emit_register(reg);
}
void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
static void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
{
const arch_register_t *reg = arch_get_irn_register_out(node, pos);
emit_register(reg);
......@@ -78,16 +78,96 @@ static void TEMPLATE_emit_cfop_target(const ir_node *node)
be_gas_emit_block_name(block);
}
void TEMPLATE_emitf(const ir_node *node, const char *format, ...)
{
va_list ap;
va_start(ap, format);
be_emit_char('\t');
for (;;) {
const char *start = format;
while (*format != '%' && *format != '\0')
++format;
be_emit_string_len(start, format-start);
if (*format == '\0')
break;
++format;
switch (*format++) {
case '%':
be_emit_char('%');
break;
case 'S': {
if (*format < '0' || '9' <= *format)
goto unknown;
unsigned const pos = *format++ - '0';
TEMPLATE_emit_source_register(node, pos);
break;
}
case 'D': {
if (*format < '0' || '9' <= *format)
goto unknown;
unsigned const pos = *format++ - '0';
TEMPLATE_emit_dest_register(node, pos);
break;
}
case 'I':
TEMPLATE_emit_immediate(node);
break;
case 'X': {
int num = va_arg(ap, int);
be_emit_irprintf("%X", num);
break;
}
case 'u': {
unsigned num = va_arg(ap, unsigned);
be_emit_irprintf("%u", num);
break;
}
case 'd': {
int num = va_arg(ap, int);
be_emit_irprintf("%d", num);
break;
}
case 's': {
const char *string = va_arg(ap, const char *);
be_emit_string(string);
break;
}
case 'L': {
TEMPLATE_emit_cfop_target(node);
break;
}
case '\n':
be_emit_char('\n');
be_emit_write_line();
be_emit_char('\t');
break;
default:
unknown:
panic("unknown format conversion in arm_emitf()");
}
}
va_end(ap);
be_emit_finish_line_gas(node);
}
/**
* Emits code for a unconditional jump.
*/
static void emit_TEMPLATE_Jmp(const ir_node *node)
{
be_emit_cstring("\tjmp ");
TEMPLATE_emit_cfop_target(node);
be_emit_finish_line_gas(node);
TEMPLATE_emitf(node, "jmp %L");
}
static void emit_be_IncSP(const ir_node *node)
......@@ -98,17 +178,13 @@ static void emit_be_IncSP(const ir_node *node)
return;
/* downwards growing stack */
if (offset > 0) {
be_emit_cstring("\tsub ");
} else {
be_emit_cstring("\tadd ");
const char *op = "add";
if (offset < 0) {
op = "sub";
offset = -offset;
}
TEMPLATE_emit_source_register(node, 0);
be_emit_irprintf(", %d, ", offset);
TEMPLATE_emit_dest_register(node, 0);
be_emit_finish_line_gas(node);
TEMPLATE_emitf(node, "%s %S0, %d, %D0", op, offset);
}
static void emit_be_Start(const ir_node *node)
......@@ -121,11 +197,7 @@ static void emit_be_Start(const ir_node *node)
/* allocate stackframe */
if (size > 0) {
be_emit_cstring("\tsub ");
emit_register(&TEMPLATE_registers[REG_SP]);
be_emit_irprintf(", %u, ", size);
emit_register(&TEMPLATE_registers[REG_SP]);
be_emit_finish_line_gas(node);
TEMPLATE_emitf(node, "sub %%sp, %u, %%sp", size);
}
}
......@@ -139,16 +211,11 @@ static void emit_be_Return(const ir_node *node)
/* deallocate stackframe */
if (size > 0) {
be_emit_cstring("\tadd ");
emit_register(&TEMPLATE_registers[REG_SP]);
be_emit_irprintf(", %u, ", size);
emit_register(&TEMPLATE_registers[REG_SP]);
be_emit_finish_line_gas(node);
TEMPLATE_emitf(node, "add %%sp, %u, %%sp", size);
}
/* return */
be_emit_cstring("\tret");
be_emit_finish_line_gas(node);
TEMPLATE_emitf(node, "ret");
}
static void emit_nothing(const ir_node *node)
......
......@@ -33,9 +33,22 @@
#include "bearch_TEMPLATE_t.h"
void TEMPLATE_emit_source_register(const ir_node *node, int pos);
void TEMPLATE_emit_dest_register(const ir_node *node, int pos);
void TEMPLATE_emit_immediate(const ir_node *node);
/**
* emit assembler instructions with format string. Automatically indents
* instructions and adds debug comments at the end (in verbose-asm mode).
* Format specifiers:
*
* fmt parameter output
* ---- ---------------------- ---------------------------------------------
* %% %
* %r const arch_register_t* register
* %Sx <node> source register x
* %Dx <node> destination register x
* %O <node> shifter operand
* %I <node> immediate
* %L <node> target label
*/
void TEMPLATE_emitf(const ir_node *node, const char *format, ...);
void TEMPLATE_emit_routine(ir_graph *irg);
......
......@@ -103,22 +103,6 @@ $mode_fp = "mode_F"; # mode used by floatingpoint registers
]
);
%emit_templates = (
S1 => "${arch}_emit_source_register(node, 0);",
S2 => "${arch}_emit_source_register(node, 1);",
S3 => "${arch}_emit_source_register(node, 2);",
S4 => "${arch}_emit_source_register(node, 3);",
S5 => "${arch}_emit_source_register(node, 4);",
S6 => "${arch}_emit_source_register(node, 5);",
D1 => "${arch}_emit_dest_register(node, 0);",
D2 => "${arch}_emit_dest_register(node, 1);",
D3 => "${arch}_emit_dest_register(node, 2);",
D4 => "${arch}_emit_dest_register(node, 3);",
D5 => "${arch}_emit_dest_register(node, 4);",
D6 => "${arch}_emit_dest_register(node, 5);",
C => "${arch}_emit_immediate(node);"
);
$default_attr_type = "TEMPLATE_attr_t";
$default_copy_attr = "TEMPLATE_copy_attr";
......@@ -130,7 +114,7 @@ Add => {
op_flags => [ "commutative" ],
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "gp" ] },
emit => '. add %S1, %S2, %D1',
emit => 'add %S1, %S2, %D1',
mode => $mode_gp,
},
......@@ -138,7 +122,7 @@ Mul => {
op_flags => [ "commutative" ],
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "gp" ] },
emit =>'. mul %S1, %S2, %D1',
emit =>'mul %S1, %S2, %D1',
mode => $mode_gp,
},
......@@ -146,7 +130,7 @@ And => {
op_flags => [ "commutative" ],
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "gp" ] },
emit => '. and %S1, %S2, %D1',
emit => 'and %S1, %S2, %D1',
mode => $mode_gp,
},
......@@ -154,7 +138,7 @@ Or => {
op_flags => [ "commutative" ],
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "gp" ] },
emit => '. or %S1, %S2, %D1',
emit => 'or %S1, %S2, %D1',
mode => $mode_gp,
},
......@@ -162,35 +146,35 @@ Xor => {
op_flags => [ "commutative" ],
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "gp" ] },
emit => '. xor %S1, %S2, %D1',
emit => 'xor %S1, %S2, %D1',
mode => $mode_gp,
},
Sub => {
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "gp" ] },
emit => '. sub %S1, %S2, %D1',
emit => 'sub %S1, %S2, %D1',
mode => $mode_gp,
},
Shl => {
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "gp" ] },
emit => '. shl %S1, %S2, %D1',
emit => 'shl %S1, %S2, %D1',
mode => $mode_gp,
},
Shr => {
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp", "gp" ], out => [ "in_r1" ] },
emit => '. shr %S2, %D1',
emit => 'shr %S2, %D1',
mode => $mode_gp,
},
Minus => {
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "gp" ], out => [ "gp" ] },
emit => '. neg %S1, %D1',
emit => 'neg %S1, %D1',
mode => $mode_gp,
},
......@@ -198,7 +182,7 @@ Not => {
arity => 1,
remat => 1,
reg_req => { in => [ "gp" ], out => [ "gp" ] },
emit => '. not %S1, %D1',
emit => 'not %S1, %D1',
mode => $mode_gp,
},
......@@ -208,7 +192,7 @@ Const => {
attr => "ir_tarval *value",
custominit => "set_TEMPLATE_value(res, value);",
reg_req => { out => [ "gp" ] },
emit => '. mov %C, %D1',
emit => 'mov %I, %D1',
cmp_attr =>
'
/* TODO: compare Const attributes */
......@@ -234,7 +218,7 @@ Load => {
irn_flags => [ "rematerializable" ],
state => "exc_pinned",
reg_req => { in => [ "gp", "none" ], out => [ "gp" ] },
emit => '. mov (%S1), %D1',
emit => 'mov (%S1), %D1',
},
Store => {
......@@ -242,7 +226,7 @@ Store => {
irn_flags => [ "rematerializable" ],
state => "exc_pinned",
reg_req => { in => [ "gp", "gp", "none" ] },
emit => '. movl %S2, (%S1)',
emit => 'movl %S2, (%S1)',
},
# Floating Point operations
......@@ -251,34 +235,34 @@ fAdd => {
op_flags => [ "commutative" ],
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "fp", "fp" ], out => [ "fp" ] },
emit => '. fadd %S1, %S2, %D1',
emit => 'fadd %S1, %S2, %D1',
mode => $mode_fp,
},
fMul => {
op_flags => [ "commutative" ],
reg_req => { in => [ "fp", "fp" ], out => [ "fp" ] },
emit =>'. fmul %S1, %S2, %D1',
emit =>'fmul %S1, %S2, %D1',
mode => $mode_fp,
},
fSub => {
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "fp", "fp" ], out => [ "fp" ] },
emit => '. fsub %S1, %S2, %D1',
emit => 'fsub %S1, %S2, %D1',
mode => $mode_fp,
},
fDiv => {
reg_req => { in => [ "fp", "fp" ], out => [ "fp" ] },
emit => '. fdiv %S1, %S2, %D1',
emit => 'fdiv %S1, %S2, %D1',
mode => $mode_fp,
},
fMinus => {
irn_flags => [ "rematerializable" ],
reg_req => { in => [ "fp" ], out => [ "fp" ] },
emit => '. fneg %S1, %D1',
emit => 'fneg %S1, %D1',
mode => $mode_fp,
},
......@@ -286,7 +270,7 @@ fConst => {
op_flags => [ "constlike" ],
irn_flags => [ "rematerializable" ],
reg_req => { out => [ "fp" ] },
emit => '. fmov %C, %D1',
emit => 'fmov %I, %D1',
cmp_attr =>
'
/* TODO: compare fConst attributes */
......@@ -302,7 +286,7 @@ fLoad => {
irn_flags => [ "rematerializable" ],
state => "exc_pinned",
reg_req => { in => [ "gp", "none" ], out => [ "fp" ] },
emit => '. fmov (%S1), %D1',
emit => 'fmov (%S1), %D1',
},
fStore => {
......@@ -310,7 +294,7 @@ fStore => {
irn_flags => [ "rematerializable" ],
state => "exc_pinned",
reg_req => { in => [ "gp", "fp", "none" ] },
emit => '. fmov %S2, (%S1)',
emit => 'fmov %S2, (%S1)',
},
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment