2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief The codegenerator (transform FIRM into ppc FIRM)
23 * @author Moritz Kroll, Jens Mueller
29 #include "irgraph_t.h"
33 #include "iredges_t.h"
41 #include "../benode_t.h"
42 #include "bearch_ppc32_t.h"
44 #include "ppc32_nodes_attr.h"
46 #include "ppc32_transform.h"
47 #include "ppc32_new_nodes.h"
48 #include "ppc32_map_regs.h"
50 #include "gen_ppc32_regalloc_if.h"
52 extern ir_op *get_op_Mulh(void);
54 int is_direct_entity(ir_entity *ent);
56 ir_mode* ppc32_mode_Cond = NULL;
59 * Returns the proj of a given node with the given proj number
61 static inline ir_node *get_succ_Proj(ir_node *node, long proj)
63 const ir_edge_t *edge;
64 foreach_out_edge(node, edge)
66 if (is_Proj(edge->src) && get_Proj_proj(edge->src) == proj)
73 * Returns a singleton condition mode
75 ir_mode *get_ppc32_mode_Cond(void) {
77 return ppc32_mode_Cond;
79 ppc32_mode_Cond = new_ir_mode("mode_Cond", irms_int_number, 4, 0, irma_none, 0);
80 return ppc32_mode_Cond;
85 * Calculates the ir_modecode with size, sort and signed attributes
87 ir_modecode get_nice_modecode(ir_mode *irmode)
89 ir_modecode mode = irm_max;
90 int sign = mode_is_signed(irmode);
91 int bits = get_mode_size_bits(irmode);
92 if(mode_is_int(irmode))
97 mode = sign ? irm_Bs : irm_Bu;
100 mode = sign ? irm_Hs : irm_Hu;
103 mode = sign ? irm_Is : irm_Iu;
107 else if(mode_is_float(irmode))
119 else if(mode_is_reference(irmode))
132 * Returns true, if the given node is a Const node and it's value fits into
133 * a signed 16-bit variable
135 int is_16bit_signed_const(ir_node *node)
139 if(!is_ppc32_Const(node)) return 0;
141 tv_const = get_ppc32_constant_tarval(node);
143 switch(get_nice_modecode(get_irn_mode(node)))
152 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
153 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
161 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
169 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
170 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
171 if(val2==0 && val3==0)
173 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
178 if(!(val2==0xff && val3==0xff))
180 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
188 panic("is_16bit_signed_const(): Mode not supported: %F", get_irn_mode(node));
193 * Returns true, if the given node is a Const node and it's value fits into
194 * a unsigned 16-bit variable
196 int is_16bit_unsigned_const(ir_node *node)
200 if(!is_ppc32_Const(node)) return 0;
202 tv_const = get_ppc32_constant_tarval(node);
203 switch(get_nice_modecode(get_irn_mode(node)))
214 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
215 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
221 panic("is_16bit_unsigned_const(): Mode not supported: %F", get_irn_mode(node));
226 /****************************************************************************************************
228 * | | | | / _| | | (_)
229 * _ __ ___ __| | ___ | |_ _ __ __ _ _ __ ___| |_ ___ _ __ _ __ ___ __ _| |_ _ ___ _ __
230 * | '_ \ / _ \ / _` |/ _ \ | __| '__/ _` | '_ \/ __| _/ _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \
231 * | | | | (_) | (_| | __/ | |_| | | (_| | | | \__ \ || (_) | | | | | | | | (_| | |_| | (_) | | | |
232 * |_| |_|\___/ \__,_|\___| \__|_| \__,_|_| |_|___/_| \___/|_| |_| |_| |_|\__,_|\__|_|\___/|_| |_|
234 ****************************************************************************************************/
237 * Creates an ppc Add.
239 * @param env The transformation environment
240 * @return the created ppc Add node
242 static ir_node *gen_Add(ppc32_transform_env_t *env) {
243 ir_node *op1 = get_Add_left(env->irn);
244 ir_node *op2 = get_Add_right(env->irn);
246 switch(get_nice_modecode(env->mode)){
248 return new_rd_ppc32_fAdd(env->dbg, env->irg, env->block, op1, op2, env->mode);
250 return new_rd_ppc32_fAdds(env->dbg, env->irg, env->block, op1, op2, env->mode);
258 if(is_16bit_signed_const(op1))
260 ir_node *addnode = new_rd_ppc32_Addi(env->dbg, env->irg, env->block, op2, env->mode);
261 set_ppc32_constant_tarval(addnode, get_ppc32_constant_tarval(op1));
262 set_ppc32_offset_mode(addnode, ppc32_ao_None);
265 if(is_16bit_signed_const(op2))
267 ir_node *addnode = new_rd_ppc32_Addi(env->dbg, env->irg, env->block, op1, env->mode);
268 set_ppc32_constant_tarval(addnode, get_ppc32_constant_tarval(op2));
269 set_ppc32_offset_mode(addnode, ppc32_ao_None);
273 return new_rd_ppc32_Add(env->dbg, env->irg, env->block, op1, op2, env->mode);
276 panic("Mode for Add not supported: %F", env->mode);
281 * Creates an ppc Mul.
283 * @param env The transformation environment
284 * @return the created ppc Mul node
286 static ir_node *gen_Mul(ppc32_transform_env_t *env) {
287 ir_node *op1 = get_Mul_left(env->irn);
288 ir_node *op2 = get_Mul_right(env->irn);
290 switch(get_nice_modecode(env->mode)){
292 return new_rd_ppc32_fMul(env->dbg, env->irg, env->block, op1, op2, env->mode);
294 return new_rd_ppc32_fMuls(env->dbg, env->irg, env->block, op1, op2, env->mode);
301 return new_rd_ppc32_Mullw(env->dbg, env->irg, env->block, op1, op2, env->mode);
305 panic("Mode for Mul not supported: %F", env->mode);
310 * Creates an ppc Mulh.
312 * @param env The transformation environment
313 * @return the created ppc Mulh node
315 static ir_node *gen_Mulh(ppc32_transform_env_t *env) {
316 ir_node *op1 = get_irn_n(env->irn, 0);
317 ir_node *op2 = get_irn_n(env->irn, 1);
319 switch(get_nice_modecode(env->mode)){
323 return new_rd_ppc32_Mulhw(env->dbg, env->irg, env->block, op1, op2, env->mode);
328 return new_rd_ppc32_Mulhwu(env->dbg, env->irg, env->block, op1, op2, env->mode);
334 panic("Mode for Mulh not supported: %F", env->mode);
339 * Creates an ppc And.
341 * @param env The transformation environment
342 * @return the created ppc And node
344 static ir_node *gen_And(ppc32_transform_env_t *env) {
345 ir_node *op1 = get_And_left(env->irn);
346 ir_node *op2 = get_And_right(env->irn);
348 return new_rd_ppc32_And(env->dbg, env->irg, env->block, op1, op2, env->mode);
354 * @param env The transformation environment
355 * @return the created ppc Or node
357 static ir_node *gen_Or(ppc32_transform_env_t *env) {
358 ir_node *op1 = get_Or_left(env->irn);
359 ir_node *op2 = get_Or_right(env->irn);
361 return new_rd_ppc32_Or(env->dbg, env->irg, env->block, op1, op2, env->mode);
365 * Creates an ppc Xor.
367 * @param env The transformation environment
368 * @return the created ppc Xor node
370 static ir_node *gen_Eor(ppc32_transform_env_t *env) {
371 ir_node *op1 = get_Eor_left(env->irn);
372 ir_node *op2 = get_Eor_right(env->irn);
374 return new_rd_ppc32_Xor(env->dbg, env->irg, env->block, op1, op2, env->mode);
378 * Creates an ppc Sub.
380 * @param env The transformation environment
381 * @return the created ppc Sub node
383 static ir_node *gen_Sub(ppc32_transform_env_t *env) {
384 ir_node *op1 = get_Sub_left(env->irn);
385 ir_node *op2 = get_Sub_right(env->irn);
387 switch(get_nice_modecode(env->mode)){
389 return new_rd_ppc32_fSub(env->dbg, env->irg, env->block, op1, op2, env->mode);
391 return new_rd_ppc32_fSubs(env->dbg, env->irg, env->block, op1, op2, env->mode);
399 return new_rd_ppc32_Sub(env->dbg, env->irg, env->block, op1, op2, env->mode);
402 panic("Mode for Sub not supported: %F", env->mode);
407 * Creates an ppc floating Div.
409 * @param env The transformation environment
410 * @return the created ppc fDiv node
412 static ir_node *gen_Quot(ppc32_transform_env_t *env) {
413 ir_node *op1 = get_Quot_left(env->irn);
414 ir_node *op2 = get_Quot_right(env->irn);
416 switch(get_nice_modecode(env->mode)){
418 return new_rd_ppc32_fDiv(env->dbg, env->irg, env->block, op1, op2, env->mode);
420 return new_rd_ppc32_fDivs(env->dbg, env->irg, env->block, op1, op2, env->mode);
423 panic("Mode for Quot not supported: %F", env->mode);
428 * Creates an ppc integer Div.
430 * @param env The transformation environment
431 * @return the created ppc Div node
433 static ir_node *gen_Div(ppc32_transform_env_t *env) {
434 ir_node *op1 = get_Div_left(env->irn);
435 ir_node *op2 = get_Div_right(env->irn);
437 switch(get_nice_modecode(get_irn_mode(op1))){
441 return new_rd_ppc32_Divw(env->dbg, env->irg, env->block, op1, op2, mode_T);
446 return new_rd_ppc32_Divwu(env->dbg, env->irg, env->block, op1, op2, mode_T);
449 panic("Mode for Div not supported: %F", get_irn_mode(op1));
454 * Creates an ppc integer Div and Mod.
456 * @param env The transformation environment
457 * @return the created ppc Div node
459 static ir_node *gen_DivMod(ppc32_transform_env_t *env) {
460 ir_node *op1 = get_DivMod_left(env->irn);
461 ir_node *op2 = get_DivMod_right(env->irn);
462 ir_node *proj_div = NULL, *proj_mod = NULL;
464 const ir_edge_t *edge;
467 foreach_out_edge(env->irn, edge)
469 if (is_Proj(edge->src))
471 switch(get_Proj_proj(edge->src)){
472 case pn_DivMod_res_div:
473 proj_div = edge->src;
475 case pn_DivMod_res_mod:
476 proj_mod = edge->src;
484 assert(proj_div!=NULL || proj_mod!=NULL);
486 res_mode = get_irn_mode(proj_div);
488 switch(get_nice_modecode(res_mode))
493 div_result = new_rd_ppc32_Divw(env->dbg, env->irg, env->block, op1, op2, mode_T);
499 div_result = new_rd_ppc32_Divwu(env->dbg, env->irg, env->block, op1, op2, mode_T);
503 panic("Mode for DivMod not supported: %F", res_mode);
506 if (proj_div == NULL)
507 proj_div = new_rd_Proj(env->dbg, env->irg, env->block, div_result, get_irn_mode(proj_mod), pn_DivMod_res_div);
513 mul_result = new_rd_ppc32_Mullw(env->dbg, env->irg, env->block, proj_div, op2, res_mode);
514 mod_result = new_rd_ppc32_Sub(env->dbg, env->irg, env->block, op1, mul_result, res_mode);
516 exchange(proj_mod, mod_result);
523 * Creates an ppc integer Mod.
525 * @param env The transformation environment
526 * @return the created ppc Mod result node
528 static ir_node *gen_Mod(ppc32_transform_env_t *env) {
529 ir_node *op1 = get_Mod_left(env->irn);
530 ir_node *op2 = get_Mod_right(env->irn);
531 ir_node *proj_div = NULL, *proj_mod = NULL;
537 proj_mod = get_succ_Proj(env->irn, pn_Mod_res);
539 assert(proj_mod != NULL);
540 res_mode = get_irn_mode(proj_mod);
542 switch(get_nice_modecode(res_mode))
547 div_result = new_rd_ppc32_Divw(env->dbg, env->irg, env->block, op1, op2, mode_T);
553 div_result = new_rd_ppc32_Divwu(env->dbg, env->irg, env->block, op1, op2, mode_T);
557 fprintf(stderr, "Mode for Mod not supported: %s\n", get_mode_name(res_mode));
563 proj_div = new_rd_Proj(env->dbg, env->irg, env->block, div_result, res_mode, pn_DivMod_res_div);
565 mul_result = new_rd_ppc32_Mullw(env->dbg, env->irg, env->block, proj_div, op2, res_mode);
566 mod_result = new_rd_ppc32_Sub(env->dbg, env->irg, env->block, op1, mul_result, res_mode);
568 exchange(proj_mod, mod_result);
574 * Creates an ppc Shl.
576 * @param env The transformation environment
577 * @return the created ppc Shl node
579 static ir_node *gen_Shl(ppc32_transform_env_t *env) {
580 ir_node *op1 = get_Shl_left(env->irn);
581 ir_node *op2 = get_Shl_right(env->irn);
583 if(is_ppc32_Const(op2))
585 ir_node *shift = new_rd_ppc32_Rlwinm(env->dbg, env->irg, env->block, op1, env->mode);
586 tarval *tv_const = get_ppc32_constant_tarval(op2);
587 int sh = get_tarval_long(tv_const);
588 assert(0<=sh && sh<=31);
589 set_ppc32_rlwimi_const(shift, sh, 0, 31-sh);
592 return new_rd_ppc32_Slw(env->dbg, env->irg, env->block, op1, op2, env->mode);
596 * Creates an ppc Srw.
598 * @param env The transformation environment
599 * @return the created ppc Shr node
601 static ir_node *gen_Shr(ppc32_transform_env_t *env) {
602 ir_node *op1 = get_Shr_left(env->irn);
603 ir_node *op2 = get_Shr_right(env->irn);
605 if(is_ppc32_Const(op2))
607 ir_node *shift = new_rd_ppc32_Rlwinm(env->dbg, env->irg, env->block, op1, env->mode);
608 tarval *tv_const = get_ppc32_constant_tarval(op2);
609 int sh = get_tarval_long(tv_const);
610 assert(0<=sh && sh<=31);
611 set_ppc32_rlwimi_const(shift, 32-sh, sh, 31);
614 return new_rd_ppc32_Srw(env->dbg, env->irg, env->block, op1, op2, env->mode);
618 * Creates an ppc Sraw.
620 * @param env The transformation environment
621 * @return the created ppc Sraw node
623 static ir_node *gen_Shrs(ppc32_transform_env_t *env) {
624 ir_node *op1 = get_Shrs_left(env->irn);
625 ir_node *op2 = get_Shrs_right(env->irn);
627 if(is_ppc32_Const(op2))
629 ir_node *shift = new_rd_ppc32_Srawi(env->dbg, env->irg, env->block, op1, env->mode);
630 tarval *tv_const = get_ppc32_constant_tarval(op2);
631 int sh = get_tarval_long(tv_const);
632 assert(0<=sh && sh<=31);
634 set_ppc32_constant_tarval(shift, tv_const);
635 set_ppc32_offset_mode(shift, ppc32_ao_None);
638 return new_rd_ppc32_Sraw(env->dbg, env->irg, env->block, op1, op2, env->mode);
642 * Creates an ppc Rotl.
644 * @param env The transformation environment
645 * @return the created ppc Rotl node
647 static ir_node *gen_Rotl(ppc32_transform_env_t *env) {
648 ir_node *op1 = get_Rotl_left(env->irn);
649 ir_node *op2 = get_Rotl_right(env->irn);
651 if(is_ppc32_Const(op2))
653 ir_node *rot = new_rd_ppc32_Rlwinm(env->dbg, env->irg, env->block, op1, env->mode);
654 tarval *tv_const = get_ppc32_constant_tarval(op2);
655 int sh = get_tarval_long(tv_const);
656 assert(0<=sh && sh<=31);
657 set_ppc32_rlwimi_const(rot, sh, 0, 31);
660 return new_rd_ppc32_Rlwnm(env->dbg, env->irg, env->block, op1, op2, env->mode);
664 * Creates an ppc Cmp.
666 * @param env The transformation environment
667 * @return the created ppc Cmp node
669 static ir_node *gen_Cmp(ppc32_transform_env_t *env) {
670 ir_node *op1 = get_Cmp_left(env->irn);
671 ir_node *op2 = get_Cmp_right(env->irn);
673 const ir_edge_t *edge;
674 foreach_out_edge(env->irn, edge)
676 if (is_Proj(edge->src))
677 set_irn_mode(edge->src, get_ppc32_mode_Cond());
680 if(mode_is_float(env->mode))
681 return new_rd_ppc32_fCmpu(env->dbg, env->irg, env->block, op1, op2, env->mode);
682 else if(mode_is_signed(env->mode))
684 if(is_16bit_signed_const(op2))
686 ir_node *cmp = new_rd_ppc32_Cmpi(env->dbg, env->irg, env->block, op1, env->mode);
687 tarval *tv_const = get_ppc32_constant_tarval(op2);
688 set_ppc32_constant_tarval(cmp, tv_const);
689 set_ppc32_offset_mode(cmp, ppc32_ao_None);
694 return new_rd_ppc32_Cmp(env->dbg, env->irg, env->block, op1, op2, env->mode);
699 if(is_16bit_unsigned_const(op2))
701 ir_node *cmp = new_rd_ppc32_Cmpli(env->dbg, env->irg, env->block, op1, env->mode);
702 tarval *tv_const = get_ppc32_constant_tarval(op2);
703 set_ppc32_constant_tarval(cmp, tv_const);
704 set_ppc32_offset_mode(cmp, ppc32_ao_None);
710 return new_rd_ppc32_Cmpl(env->dbg, env->irg, env->block, op1, op2, env->mode);
716 * Transforms a Minus node.
718 * @param env The transformation environment
719 * @return the created ppc Minus node
721 static ir_node *gen_Minus(ppc32_transform_env_t *env) {
722 ir_node *op = get_Minus_op(env->irn);
724 switch(get_nice_modecode(env->mode)){
727 return new_rd_ppc32_fNeg(env->dbg, env->irg, env->block, op, env->mode);
735 return new_rd_ppc32_Neg(env->dbg, env->irg, env->block, op, env->mode);
738 panic("Mode for Neg not supported: %F", env->mode);
743 * Transforms a Not node.
745 * @param env The transformation environment
746 * @return the created ppc Not node
748 static ir_node *gen_Not(ppc32_transform_env_t *env) {
749 return new_rd_ppc32_Not(env->dbg, env->irg, env->block, get_Not_op(env->irn), env->mode);
753 static ir_node *own_gen_Andi_dot_lo16(ppc32_transform_env_t *env, ir_node *op, int mask)
755 ir_node *andi = new_rd_ppc32_Andi_dot(env->dbg, env->irg, env->block, op, mode_T);
757 set_ppc32_offset_mode(andi, ppc32_ao_Lo16);
758 set_ppc32_constant_tarval(andi, new_tarval_from_long(mask, mode_Is));
759 in[0] = new_rd_Proj(env->dbg, env->irg, env->block, andi, env->mode,1);
760 be_new_Keep(&ppc32_reg_classes[CLASS_ppc32_condition], env->irg, env->block, 1, in);
761 return new_rd_Proj(env->dbg, env->irg, env->block, andi, env->mode,0);
765 * Transforms a Conv node.
767 * @param env The transformation environment
768 * @return the created ppc Conv node
770 static ir_node *gen_Conv(ppc32_transform_env_t *env) {
771 ir_node *op = get_Conv_op(env->irn);
772 ir_modecode from_mode=get_nice_modecode(get_irn_mode(op));
773 ir_modecode to_mode=get_nice_modecode(env->mode);
775 #define SKIP return op
777 if(from_mode == to_mode) SKIP;
793 return new_rd_ppc32_fRsp(env->dbg, env->irg, env->block, op, env->mode);
804 return new_rd_ppc32_Extsh(env->dbg, env->irg, env->block, op, env->mode);
806 return own_gen_Andi_dot_lo16(env, op, 0xffff);
808 return new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, op, env->mode);
810 return own_gen_Andi_dot_lo16(env, op, 0xff);
824 if(from_mode==irm_Hu)
826 return own_gen_Andi_dot_lo16(env, op, 0xffff);
830 return new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, op, env->mode);
832 return own_gen_Andi_dot_lo16(env, op, 0xff);
834 return new_rd_ppc32_Extsh(env->dbg, env->irg, env->block, op, env->mode);
846 if(from_mode==irm_Bs)
848 return own_gen_Andi_dot_lo16(env, op, 0xff);
853 return new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, op, env->mode);
859 if(to_mode==irm_Is || to_mode==irm_Iu) SKIP;
865 panic("Mode for Conv not supported: %F -> %F", get_irn_mode(op), env->mode);
870 * Transforms an Abs node.
872 * @param env The transformation environment
873 * @return the ppc node generating the absolute value
875 static ir_node *gen_Abs(ppc32_transform_env_t *env) {
876 ir_node *op = get_Abs_op(env->irn);
880 switch(get_nice_modecode(env->mode))
884 return new_rd_ppc32_fAbs(env->dbg, env->irg, env->block, op, env->mode);
890 n1 = new_rd_ppc32_Srawi(env->dbg, env->irg, env->block, op, env->mode);
891 set_ppc32_constant_tarval(n1, new_tarval_from_long(shift, mode_Is));
892 set_ppc32_offset_mode(n1, ppc32_ao_None);
893 n2 = new_rd_ppc32_Add(env->dbg, env->irg, env->block, op, n1, env->mode);
894 return new_rd_ppc32_Xor(env->dbg, env->irg, env->block, n2, n1, env->mode);
898 panic("Mode for Abs not supported: %F", env->mode);
903 * Transforms an Cond node.
905 * @param env The transformation environment
906 * @return a ppc branch node
908 static ir_node *gen_Cond(ppc32_transform_env_t *env) {
909 ir_node *selector = get_Cond_selector(env->irn);
910 ir_mode *projmode = get_irn_mode(selector);
911 if(is_Proj(selector) && projmode==get_ppc32_mode_Cond())
913 int projnum = get_Proj_proj(selector);
914 ir_node *branch = new_rd_ppc32_Branch(env->dbg, env->irg, env->block, selector, env->mode);
915 set_ppc32_proj_nr(branch, projnum);
920 ir_node *unknown_gpr = new_rd_ppc32_Unknown(env->dbg, env->irg, env->block, mode_Is);
921 ir_node *unknown_cond = new_rd_ppc32_cUnknown(env->dbg, env->irg, env->block, get_ppc32_mode_Cond());
923 ir_node *switch_node = new_rd_ppc32_Switch(env->dbg, env->irg, env->block, selector,
924 unknown_gpr, unknown_cond, env->mode);
925 set_ppc32_proj_nr(switch_node, get_Cond_defaultProj(env->irn));
932 * Transforms an Unknown node.
934 * @param env The transformation environment
935 * @return a ppc Unknown node
937 static ir_node *gen_Unknown(ppc32_transform_env_t *env) {
938 if(mode_is_float(env->mode))
939 return new_rd_ppc32_fUnknown(env->dbg, env->irg, env->block, env->mode);
940 else if (mode_is_int(env->mode))
941 return new_rd_ppc32_Unknown(env->dbg, env->irg, env->block, env->mode);
943 panic("Mode %F for unknown value not supported.", env->mode);
946 static ir_node *ldst_insert_const(ir_node *ptr, tarval **ptv, ident **pid, ppc32_transform_env_t *env) {
947 tarval *tv_const = NULL;
948 ident *id_symconst = NULL;
950 if(is_ppc32_Const(ptr))
952 tv_const = get_ppc32_constant_tarval(ptr);
953 ptr = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, mode_P, ppc32_ao_Ha16, tv_const, NULL);
955 else if(is_ppc32_SymConst(ptr))
957 ir_entity *ent = get_ppc32_frame_entity(ptr);
958 if(is_direct_entity(ent))
960 id_symconst = get_entity_ident(ent);
961 ptr = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, mode_P, ppc32_ao_Ha16, NULL, id_symconst);
972 * @param env The transformation environment
973 * @return the created ppc Load node
975 static ir_node *gen_Load(ppc32_transform_env_t *env) {
976 ir_node *node = env->irn;
977 ir_node *loadptr = get_Load_ptr(node);
979 ir_mode *mode = get_Load_mode(node);
980 tarval *tv_const = NULL;
981 ident *id_symconst = NULL;
983 loadptr = ldst_insert_const(loadptr, &tv_const, &id_symconst, env);
984 switch(get_nice_modecode(mode)){
986 load = new_rd_ppc32_Lbz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
991 ir_node *proj_load, *extsb_node;
992 load = new_rd_ppc32_Lbz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
993 proj_load = new_rd_Proj(env->dbg, env->irg, env->block, load, mode, pn_Load_res);
994 extsb_node = new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, proj_load, mode);
995 exchange(get_succ_Proj(env->irn, pn_Load_res), extsb_node);
1001 load = new_rd_ppc32_Lhz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1004 load =new_rd_ppc32_Lha(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1009 load = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1013 load = new_rd_ppc32_Lfd(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1016 load = new_rd_ppc32_Lfs(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1020 panic("Mode for Load not supported: %F", env->mode);
1025 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1026 set_ppc32_constant_tarval(load, tv_const);
1028 else if(id_symconst)
1030 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1031 set_ppc32_symconst_ident(load, id_symconst);
1039 * Transforms a Store.
1041 * @param env The transformation environment
1042 * @return the created ppc Store node
1044 static ir_node *gen_Store(ppc32_transform_env_t *env) {
1045 ir_node *node = env->irn;
1046 ir_node *storeptr = get_Store_ptr(node);
1047 ir_node *valuenode = get_Store_value(node);
1048 ir_mode *mode = get_irn_mode(valuenode);
1050 tarval *tv_const = NULL;
1051 ident *id_symconst = NULL;
1053 storeptr = ldst_insert_const(storeptr, &tv_const, &id_symconst, env);
1055 switch(get_nice_modecode(mode)){
1058 store = new_rd_ppc32_Stb(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1063 store = new_rd_ppc32_Sth(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1068 store = new_rd_ppc32_Stw(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1072 store = new_rd_ppc32_Stfd(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1075 store = new_rd_ppc32_Stfs(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1079 panic("Mode for Store not supported: %F", env->mode);
1083 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1084 set_ppc32_constant_tarval(store, tv_const);
1086 else if(id_symconst)
1088 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1089 set_ppc32_symconst_ident(store, id_symconst);
1095 * Transforms a CopyB.
1097 * @param env The transformation environment
1098 * @return the created ppc CopyB node
1100 static ir_node *gen_CopyB(ppc32_transform_env_t *env) {
1101 ir_node *mem = get_CopyB_mem(env->irn);
1102 ir_node *src = get_CopyB_src(env->irn);
1103 ir_node *dest = get_CopyB_dst(env->irn);
1104 ir_type *type = get_CopyB_type(env->irn);
1105 int size = get_type_size_bytes(type);
1108 ir_node *load, *store = NULL;
1113 tarval *offset0 = new_tarval_from_long(0, mode_Is);
1114 tarval *offset4 = new_tarval_from_long(4, mode_Is);
1116 load = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, src, mem);
1117 set_ppc32_constant_tarval(load, offset0);
1118 set_ppc32_offset_mode(load, ppc32_ao_None);
1119 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1120 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1122 store = new_rd_ppc32_Stw(env->dbg, env->irg, env->block, dest, res, mem);
1123 set_ppc32_constant_tarval(store, offset0);
1124 set_ppc32_offset_mode(store, ppc32_ao_None);
1125 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1129 load = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, src, mem);
1130 set_ppc32_constant_tarval(load, offset4);
1131 set_ppc32_offset_mode(load, ppc32_ao_None);
1132 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1133 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1135 store = new_rd_ppc32_Stw(env->dbg, env->irg, env->block, dest, res, mem);
1136 set_ppc32_constant_tarval(store, offset4);
1137 set_ppc32_offset_mode(store, ppc32_ao_None);
1138 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1144 ir_node *ornode, *mtctrnode;
1146 assert(size/4-1<=0xffff);
1149 ornode = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1150 set_ppc32_offset_mode(ornode, ppc32_ao_None);
1154 ir_node *zeroreg = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1155 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1156 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1157 ornode = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, zeroreg, mode_Is);
1158 set_ppc32_offset_mode(ornode, ppc32_ao_Lo16);
1161 set_ppc32_constant_tarval(ornode, new_tarval_from_long(size/4-1, mode_Is));
1162 mtctrnode = new_rd_ppc32_Mtctr(env->dbg, env->irg, env->block, ornode, mode_Is);
1163 store = new_rd_ppc32_LoopCopy(env->dbg, env->irg, env->block, src, dest, mtctrnode, mem, mode_T);
1165 in[0] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 1); // src
1166 in[1] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 2); // dest
1167 in[2] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 4); // temp
1168 be_new_Keep(&ppc32_reg_classes[CLASS_ppc32_gp], env->irg, env->block, 3, in);
1169 in[0] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 3); // ctr
1170 be_new_Keep(&ppc32_reg_classes[CLASS_ppc32_count], env->irg, env->block, 1, in);
1172 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, 0);
1181 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1182 load = new_rd_ppc32_Lhz(env->dbg, env->irg, env->block, src, mem);
1183 set_ppc32_constant_tarval(load, offset_tarval);
1184 set_ppc32_offset_mode(load, ppc32_ao_None);
1185 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1186 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1188 store = new_rd_ppc32_Sth(env->dbg, env->irg, env->block, dest, res, mem);
1189 set_ppc32_constant_tarval(store, offset_tarval);
1190 set_ppc32_offset_mode(store, ppc32_ao_None);
1191 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1199 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1200 load = new_rd_ppc32_Lbz(env->dbg, env->irg, env->block, src, mem);
1201 set_ppc32_constant_tarval(load, offset_tarval);
1202 set_ppc32_offset_mode(load, ppc32_ao_None);
1203 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1204 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1206 store = new_rd_ppc32_Stb(env->dbg, env->irg, env->block, dest, res, mem);
1207 set_ppc32_constant_tarval(store, offset_tarval);
1208 set_ppc32_offset_mode(store, ppc32_ao_None);
1209 // mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1216 * Transforms a FrameAddr into a ppc Add.
1218 * @param env The transformation environment
1220 static ir_node *gen_be_FrameAddr(ppc32_transform_env_t *env) {
1221 ir_node *op = get_irn_n(env->irn, 0);
1222 ir_node *add = new_rd_ppc32_Addi(env->dbg, env->irg, env->block, op, mode_P);
1223 set_ppc32_frame_entity(add, be_get_frame_entity(env->irn));
1228 /*********************************************************
1231 * _ __ ___ __ _ _ _ __ __| |_ __ ___ _____ _ __
1232 * | '_ ` _ \ / _` | | '_ \ / _` | '__| \ \ / / _ \ '__|
1233 * | | | | | | (_| | | | | | | (_| | | | |\ V / __/ |
1234 * |_| |_| |_|\__,_|_|_| |_| \__,_|_| |_| \_/ \___|_|
1236 *********************************************************/
1239 * the BAD transformer.
1241 static ir_node *bad_transform(ppc32_transform_env_t *env) {
1242 panic("Transformation not implemented: %+F\n", env->irn);
1246 * Enters all transform functions into the generic pointer
1248 void ppc32_register_transformers(void) {
1249 ir_op *op_Max, *op_Min, *op_Mulh;
1251 /* first clear the generic function pointer for all ops */
1252 clear_irp_opcodes_generic_func();
1254 #define FIRM_OP(a) op_##a->ops.generic = (op_func)gen_##a
1255 #define BAD(a) op_##a->ops.generic = (op_func)bad_transform
1286 /* TODO: implement these nodes */
1289 /* You probably don't need to handle the following nodes */
1319 FIRM_OP(be_FrameAddr);
1320 op_Mulh = get_op_Mulh();
1323 op_Max = get_op_Max();
1326 op_Min = get_op_Min();
1331 typedef ir_node *(transform_func)(ppc32_transform_env_t *env);
1334 * Transforms the given firm node (and maybe some other related nodes)
1335 * into one or more assembler nodes.
1337 * @param node the firm node
1338 * @param env the debug module
1340 void ppc32_transform_node(ir_node *node, void *env) {
1341 ppc32_code_gen_t *cg = (ppc32_code_gen_t *)env;
1342 ir_op *op = get_irn_op(node);
1343 ir_node *asm_node = NULL;
1349 DBG((cg->mod, LEVEL_1, "check %+F ... ", node));
1351 if (op->ops.generic) {
1352 ppc32_transform_env_t tenv;
1353 transform_func *transform = (transform_func *)op->ops.generic;
1355 tenv.block = get_nodes_block(node);
1356 tenv.dbg = get_irn_dbg_info(node);
1357 tenv.irg = current_ir_graph;
1359 tenv.mode = get_irn_mode(node);
1360 DEBUG_ONLY(tenv.mod = cg->mod;)
1362 asm_node = (*transform)(&tenv);
1366 exchange(node, asm_node);
1367 DB((cg->mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1370 DB((cg->mod, LEVEL_1, "ignored\n"));
1375 * Constant generating code
1383 /** Compares two (entity, tarval) combinations */
1384 static int cmp_tv_ent(const void *a, const void *b, size_t len) {
1385 const struct tv_ent *e1 = a;
1386 const struct tv_ent *e2 = b;
1389 return !(e1->tv == e2->tv);
1392 /** Generates a SymConst node for a known FP const */
1393 static ir_node *gen_fp_known_symconst(ppc32_transform_env_t *env, tarval *known_const) {
1394 static set *const_set = NULL;
1395 static ir_type *tp = NULL;
1397 struct tv_ent *entry;
1398 ir_node *cnst,*symcnst;
1400 ir_entity *ent = NULL;
1403 const_set = new_set(cmp_tv_ent, 10);
1405 tp = new_type_primitive(new_id_from_str("const_double_t"), env->mode);
1407 key.tv = known_const;
1410 entry = set_insert(const_set, &key, sizeof(key), HASH_PTR(key.tv));
1414 sprintf(buf, "const_%ld", get_irn_node_nr(env->irn));
1415 ent = new_entity(get_glob_type(), new_id_from_str(buf), tp);
1417 set_entity_ld_ident(ent, get_entity_ident(ent));
1418 set_entity_visibility(ent, visibility_local);
1419 set_entity_variability(ent, variability_constant);
1420 set_entity_allocation(ent, allocation_static);
1422 /* we create a new entity here: It's initialization must resist on the
1424 rem = current_ir_graph;
1425 current_ir_graph = get_const_code_irg();
1426 cnst = new_Const(env->mode, key.tv);
1427 current_ir_graph = rem;
1429 set_atomic_ent_value(ent, cnst);
1431 /* set the entry for hashmap */
1433 } // TODO: Wird nicht richtig in global type gesteckt, ppc32_gen_decls.c findet ihn nicht
1435 symcnst = new_rd_ppc32_SymConst(env->dbg, env->irg, env->block, env->mode);
1436 set_ppc32_frame_entity(symcnst, ent);
1440 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env);
1443 * Transforms a Const.
1445 * @param mod the debug module
1446 * @param block the block the new node should belong to
1447 * @param node the ir Const node
1448 * @param mode node mode
1449 * @return the created ppc Load immediate node
1451 static ir_node *gen_ppc32_Const(ppc32_transform_env_t *env) {
1452 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1455 switch(get_nice_modecode(env->mode)){
1458 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1461 ir_node *zeroreg = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1462 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1463 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1464 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, zeroreg, mode_Is);
1465 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1472 node = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, env->mode);
1473 set_ppc32_offset_mode(node, ppc32_ao_None);
1479 unsigned char val2 = get_tarval_sub_bits(tv_const,2);
1480 unsigned char val3 = get_tarval_sub_bits(tv_const,3);
1483 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1486 ir_node *zeroreg = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1487 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1488 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1489 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, zeroreg, mode_Is);
1490 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1494 node = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, env->mode);
1495 set_ppc32_offset_mode(node, ppc32_ao_None);
1500 unsigned char val0 = get_tarval_sub_bits(tv_const,0);
1501 unsigned char val1 = get_tarval_sub_bits(tv_const,1);
1502 node = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Hi16, tv_const, NULL);
1505 set_ppc32_constant_tarval(node, tv_const);
1506 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, node, env->mode);
1507 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1514 panic("Mode for Const not supported: %F", env->mode);
1516 set_ppc32_constant_tarval(node, tv_const);
1521 * Transforms a fConst.
1523 * @param mod the debug module
1524 * @param block the block the new node should belong to
1525 * @param node the ir Const node
1526 * @param mode node mode
1527 * @return the created ppc float Load node
1529 static ir_node *gen_ppc32_fConst(ppc32_transform_env_t *env) {
1530 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1532 switch(get_nice_modecode(env->mode)){
1536 ir_node *addr, *load;
1537 ir_mode *mode = env->mode;
1539 env->irn = gen_fp_known_symconst(env, tv_const);
1541 ent = get_ppc32_frame_entity(env->irn);
1542 if(is_direct_entity(ent))
1544 ident *id_symconst = get_entity_ident(ent);
1545 ir_node *node_addis = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1548 load = new_rd_ppc32_Lfd(env->dbg, env->irg, env->block, node_addis, new_NoMem());
1550 load = new_rd_ppc32_Lfs(env->dbg, env->irg, env->block, node_addis, new_NoMem());
1552 set_ppc32_symconst_ident(load, id_symconst);
1553 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1557 addr = gen_ppc32_SymConst (env);
1559 load = new_rd_ppc32_Lfd(env->dbg, env->irg, env->block, addr, new_NoMem());
1561 load = new_rd_ppc32_Lfs(env->dbg, env->irg, env->block, addr, new_NoMem());
1563 return new_rd_Proj(env->dbg, env->irg, env->block, load, mode, pn_Load_res);
1567 panic("Mode for fConst not supported: %F", env->mode);
1573 * Returns true, if the entity can be accessed directly,
1574 * or false, if the address must be loaded first
1576 int is_direct_entity(ir_entity *ent) {
1577 return get_entity_visibility(ent) != visibility_external_allocated;
1578 /* visibility vis = get_entity_visibility(ent);
1579 if(is_Method_type(get_entity_type(ent)))
1581 return (vis!=visibility_external_allocated);
1585 return (vis==visibility_local);
1590 * Transforms a SymConst.
1592 * @param mod the debug module
1593 * @param block the block the new node should belong to
1594 * @param node the ir Const node
1595 * @param mode node mode
1596 * @return the created ppc Load immediate node
1598 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env) {
1599 ir_entity *ent = get_ppc32_frame_entity(env->irn);
1600 ident *id_symconst = get_entity_ident(ent);
1602 switch(get_nice_modecode(env->mode)){
1605 if (is_direct_entity(ent))
1607 ir_node *node_addis = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Hi16, NULL, id_symconst);
1608 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, node_addis, env->mode);
1609 set_ppc32_symconst_ident(node, id_symconst);
1610 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1614 ir_node *node_addis = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1615 node = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, node_addis, new_NoMem());
1616 set_ppc32_symconst_ident(node, id_symconst);
1617 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1618 node = new_rd_Proj(env->dbg, env->irg, env->block, node, env->mode, pn_Load_res);
1624 panic("Mode for SymConst not supported: %F", env->mode);
1630 * Transforms the given firm node (and maybe some other related nodes)
1631 * into one or more assembler nodes.
1633 * @param node the firm node
1634 * @param env the debug module
1636 void ppc32_transform_const(ir_node *node, void *env) {
1637 ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
1638 ir_node *asm_node = NULL;
1639 ppc32_transform_env_t tenv;
1645 tenv.block = get_nodes_block(node);
1646 tenv.dbg = get_irn_dbg_info(node);
1647 tenv.irg = current_ir_graph;
1649 DEBUG_ONLY(tenv.mod = cgenv->mod;)
1650 tenv.mode = get_irn_mode(node);
1652 #define OTHER_GEN(a) \
1653 if (get_irn_op(node) == get_op_##a()) { \
1654 asm_node = gen_##a(&tenv); \
1657 DBG((tenv.mod, LEVEL_1, "check %+F ... ", node));
1659 OTHER_GEN(ppc32_Const)
1660 else OTHER_GEN(ppc32_fConst)
1661 else OTHER_GEN(ppc32_SymConst)
1664 exchange(node, asm_node);
1665 DB((tenv.mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1668 DB((tenv.mod, LEVEL_1, "ignored\n"));