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
31 #include "irgraph_t.h"
35 #include "iredges_t.h"
43 #include "../benode_t.h"
44 #include "bearch_ppc32_t.h"
46 #include "ppc32_nodes_attr.h"
48 #include "ppc32_transform.h"
49 #include "ppc32_new_nodes.h"
50 #include "ppc32_map_regs.h"
52 #include "gen_ppc32_regalloc_if.h"
54 extern ir_op *get_op_Mulh(void);
56 int is_direct_entity(ir_entity *ent);
58 ir_mode* ppc32_mode_Cond = NULL;
61 * Returns the proj of a given node with the given proj number
63 static INLINE ir_node *get_succ_Proj(ir_node *node, long proj)
65 const ir_edge_t *edge;
66 foreach_out_edge(node, edge)
68 if (is_Proj(edge->src) && get_Proj_proj(edge->src) == proj)
75 * Returns a singleton condition mode
77 ir_mode *get_ppc32_mode_Cond(void) {
79 return ppc32_mode_Cond;
81 ppc32_mode_Cond = new_ir_mode("mode_Cond", irms_int_number, 4, 0, irma_none, 0);
82 return ppc32_mode_Cond;
87 * Calculates the ir_modecode with size, sort and signed attributes
89 ir_modecode get_nice_modecode(ir_mode *irmode)
91 ir_modecode mode = irm_max;
92 int sign = mode_is_signed(irmode);
93 int bits = get_mode_size_bits(irmode);
94 if(mode_is_int(irmode))
99 mode = sign ? irm_Bs : irm_Bu;
102 mode = sign ? irm_Hs : irm_Hu;
105 mode = sign ? irm_Is : irm_Iu;
109 else if(mode_is_float(irmode))
121 else if(mode_is_reference(irmode))
134 * Returns true, if the given node is a Const node and it's value fits into
135 * a signed 16-bit variable
137 int is_16bit_signed_const(ir_node *node)
141 if(!is_ppc32_Const(node)) return 0;
143 tv_const = get_ppc32_constant_tarval(node);
145 switch(get_nice_modecode(get_irn_mode(node)))
154 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
155 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
163 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
171 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
172 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
173 if(val2==0 && val3==0)
175 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
180 if(!(val2==0xff && val3==0xff))
182 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
190 panic("is_16bit_signed_const(): Mode not supported: %F", get_irn_mode(node));
195 * Returns true, if the given node is a Const node and it's value fits into
196 * a unsigned 16-bit variable
198 int is_16bit_unsigned_const(ir_node *node)
202 if(!is_ppc32_Const(node)) return 0;
204 tv_const = get_ppc32_constant_tarval(node);
205 switch(get_nice_modecode(get_irn_mode(node)))
216 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
217 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
223 panic("is_16bit_unsigned_const(): Mode not supported: %F", get_irn_mode(node));
228 /****************************************************************************************************
230 * | | | | / _| | | (_)
231 * _ __ ___ __| | ___ | |_ _ __ __ _ _ __ ___| |_ ___ _ __ _ __ ___ __ _| |_ _ ___ _ __
232 * | '_ \ / _ \ / _` |/ _ \ | __| '__/ _` | '_ \/ __| _/ _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \
233 * | | | | (_) | (_| | __/ | |_| | | (_| | | | \__ \ || (_) | | | | | | | | (_| | |_| | (_) | | | |
234 * |_| |_|\___/ \__,_|\___| \__|_| \__,_|_| |_|___/_| \___/|_| |_| |_| |_|\__,_|\__|_|\___/|_| |_|
236 ****************************************************************************************************/
239 * Creates an ppc Add.
241 * @param env The transformation environment
242 * @return the created ppc Add node
244 static ir_node *gen_Add(ppc32_transform_env_t *env) {
245 ir_node *op1 = get_Add_left(env->irn);
246 ir_node *op2 = get_Add_right(env->irn);
248 switch(get_nice_modecode(env->mode)){
250 return new_rd_ppc32_fAdd(env->dbg, env->irg, env->block, op1, op2, env->mode);
252 return new_rd_ppc32_fAdds(env->dbg, env->irg, env->block, op1, op2, env->mode);
260 if(is_16bit_signed_const(op1))
262 ir_node *addnode = new_rd_ppc32_Addi(env->dbg, env->irg, env->block, op2, env->mode);
263 set_ppc32_constant_tarval(addnode, get_ppc32_constant_tarval(op1));
264 set_ppc32_offset_mode(addnode, ppc32_ao_None);
267 if(is_16bit_signed_const(op2))
269 ir_node *addnode = new_rd_ppc32_Addi(env->dbg, env->irg, env->block, op1, env->mode);
270 set_ppc32_constant_tarval(addnode, get_ppc32_constant_tarval(op2));
271 set_ppc32_offset_mode(addnode, ppc32_ao_None);
275 return new_rd_ppc32_Add(env->dbg, env->irg, env->block, op1, op2, env->mode);
278 panic("Mode for Add not supported: %F", env->mode);
283 * Creates an ppc Mul.
285 * @param env The transformation environment
286 * @return the created ppc Mul node
288 static ir_node *gen_Mul(ppc32_transform_env_t *env) {
289 ir_node *op1 = get_Mul_left(env->irn);
290 ir_node *op2 = get_Mul_right(env->irn);
292 switch(get_nice_modecode(env->mode)){
294 return new_rd_ppc32_fMul(env->dbg, env->irg, env->block, op1, op2, env->mode);
296 return new_rd_ppc32_fMuls(env->dbg, env->irg, env->block, op1, op2, env->mode);
303 return new_rd_ppc32_Mullw(env->dbg, env->irg, env->block, op1, op2, env->mode);
307 panic("Mode for Mul not supported: %F", env->mode);
312 * Creates an ppc Mulh.
314 * @param env The transformation environment
315 * @return the created ppc Mulh node
317 static ir_node *gen_Mulh(ppc32_transform_env_t *env) {
318 ir_node *op1 = get_irn_n(env->irn, 0);
319 ir_node *op2 = get_irn_n(env->irn, 1);
321 switch(get_nice_modecode(env->mode)){
325 return new_rd_ppc32_Mulhw(env->dbg, env->irg, env->block, op1, op2, env->mode);
330 return new_rd_ppc32_Mulhwu(env->dbg, env->irg, env->block, op1, op2, env->mode);
336 panic("Mode for Mulh not supported: %F", env->mode);
341 * Creates an ppc And.
343 * @param env The transformation environment
344 * @return the created ppc And node
346 static ir_node *gen_And(ppc32_transform_env_t *env) {
347 ir_node *op1 = get_And_left(env->irn);
348 ir_node *op2 = get_And_right(env->irn);
350 return new_rd_ppc32_And(env->dbg, env->irg, env->block, op1, op2, env->mode);
356 * @param env The transformation environment
357 * @return the created ppc Or node
359 static ir_node *gen_Or(ppc32_transform_env_t *env) {
360 ir_node *op1 = get_Or_left(env->irn);
361 ir_node *op2 = get_Or_right(env->irn);
363 return new_rd_ppc32_Or(env->dbg, env->irg, env->block, op1, op2, env->mode);
367 * Creates an ppc Xor.
369 * @param env The transformation environment
370 * @return the created ppc Xor node
372 static ir_node *gen_Eor(ppc32_transform_env_t *env) {
373 ir_node *op1 = get_Eor_left(env->irn);
374 ir_node *op2 = get_Eor_right(env->irn);
376 return new_rd_ppc32_Xor(env->dbg, env->irg, env->block, op1, op2, env->mode);
380 * Creates an ppc Sub.
382 * @param env The transformation environment
383 * @return the created ppc Sub node
385 static ir_node *gen_Sub(ppc32_transform_env_t *env) {
386 ir_node *op1 = get_Sub_left(env->irn);
387 ir_node *op2 = get_Sub_right(env->irn);
389 switch(get_nice_modecode(env->mode)){
391 return new_rd_ppc32_fSub(env->dbg, env->irg, env->block, op1, op2, env->mode);
393 return new_rd_ppc32_fSubs(env->dbg, env->irg, env->block, op1, op2, env->mode);
401 return new_rd_ppc32_Sub(env->dbg, env->irg, env->block, op1, op2, env->mode);
404 panic("Mode for Sub not supported: %F", env->mode);
409 * Creates an ppc floating Div.
411 * @param env The transformation environment
412 * @return the created ppc fDiv node
414 static ir_node *gen_Quot(ppc32_transform_env_t *env) {
415 ir_node *op1 = get_Quot_left(env->irn);
416 ir_node *op2 = get_Quot_right(env->irn);
418 switch(get_nice_modecode(env->mode)){
420 return new_rd_ppc32_fDiv(env->dbg, env->irg, env->block, op1, op2, env->mode);
422 return new_rd_ppc32_fDivs(env->dbg, env->irg, env->block, op1, op2, env->mode);
425 panic("Mode for Quot not supported: %F", env->mode);
430 * Creates an ppc integer Div.
432 * @param env The transformation environment
433 * @return the created ppc Div node
435 static ir_node *gen_Div(ppc32_transform_env_t *env) {
436 ir_node *op1 = get_Div_left(env->irn);
437 ir_node *op2 = get_Div_right(env->irn);
439 switch(get_nice_modecode(get_irn_mode(op1))){
443 return new_rd_ppc32_Divw(env->dbg, env->irg, env->block, op1, op2, mode_T);
448 return new_rd_ppc32_Divwu(env->dbg, env->irg, env->block, op1, op2, mode_T);
451 panic("Mode for Div not supported: %F", get_irn_mode(op1));
456 * Creates an ppc integer Div and Mod.
458 * @param env The transformation environment
459 * @return the created ppc Div node
461 static ir_node *gen_DivMod(ppc32_transform_env_t *env) {
462 ir_node *op1 = get_DivMod_left(env->irn);
463 ir_node *op2 = get_DivMod_right(env->irn);
464 ir_node *proj_div = NULL, *proj_mod = NULL;
466 const ir_edge_t *edge;
469 foreach_out_edge(env->irn, edge)
471 if (is_Proj(edge->src))
473 switch(get_Proj_proj(edge->src)){
474 case pn_DivMod_res_div:
475 proj_div = edge->src;
477 case pn_DivMod_res_mod:
478 proj_mod = edge->src;
486 assert(proj_div!=NULL || proj_mod!=NULL);
488 res_mode = get_irn_mode(proj_div);
490 switch(get_nice_modecode(res_mode))
495 div_result = new_rd_ppc32_Divw(env->dbg, env->irg, env->block, op1, op2, mode_T);
501 div_result = new_rd_ppc32_Divwu(env->dbg, env->irg, env->block, op1, op2, mode_T);
505 panic("Mode for DivMod not supported: %F", res_mode);
508 if (proj_div == NULL)
509 proj_div = new_rd_Proj(env->dbg, env->irg, env->block, div_result, get_irn_mode(proj_mod), pn_DivMod_res_div);
515 mul_result = new_rd_ppc32_Mullw(env->dbg, env->irg, env->block, proj_div, op2, res_mode);
516 mod_result = new_rd_ppc32_Sub(env->dbg, env->irg, env->block, op1, mul_result, res_mode);
518 exchange(proj_mod, mod_result);
525 * Creates an ppc integer Mod.
527 * @param env The transformation environment
528 * @return the created ppc Mod result node
530 static ir_node *gen_Mod(ppc32_transform_env_t *env) {
531 ir_node *op1 = get_Mod_left(env->irn);
532 ir_node *op2 = get_Mod_right(env->irn);
533 ir_node *proj_div = NULL, *proj_mod = NULL;
539 proj_mod = get_succ_Proj(env->irn, pn_Mod_res);
541 assert(proj_mod != NULL);
542 res_mode = get_irn_mode(proj_mod);
544 switch(get_nice_modecode(res_mode))
549 div_result = new_rd_ppc32_Divw(env->dbg, env->irg, env->block, op1, op2, mode_T);
555 div_result = new_rd_ppc32_Divwu(env->dbg, env->irg, env->block, op1, op2, mode_T);
559 fprintf(stderr, "Mode for Mod not supported: %s\n", get_mode_name(res_mode));
565 proj_div = new_rd_Proj(env->dbg, env->irg, env->block, div_result, res_mode, pn_DivMod_res_div);
567 mul_result = new_rd_ppc32_Mullw(env->dbg, env->irg, env->block, proj_div, op2, res_mode);
568 mod_result = new_rd_ppc32_Sub(env->dbg, env->irg, env->block, op1, mul_result, res_mode);
570 exchange(proj_mod, mod_result);
576 * Creates an ppc Shl.
578 * @param env The transformation environment
579 * @return the created ppc Shl node
581 static ir_node *gen_Shl(ppc32_transform_env_t *env) {
582 ir_node *op1 = get_Shl_left(env->irn);
583 ir_node *op2 = get_Shl_right(env->irn);
585 if(is_ppc32_Const(op2))
587 ir_node *shift = new_rd_ppc32_Rlwinm(env->dbg, env->irg, env->block, op1, env->mode);
588 tarval *tv_const = get_ppc32_constant_tarval(op2);
589 int sh = get_tarval_long(tv_const);
590 assert(0<=sh && sh<=31);
591 set_ppc32_rlwimi_const(shift, sh, 0, 31-sh);
594 return new_rd_ppc32_Slw(env->dbg, env->irg, env->block, op1, op2, env->mode);
598 * Creates an ppc Srw.
600 * @param env The transformation environment
601 * @return the created ppc Shr node
603 static ir_node *gen_Shr(ppc32_transform_env_t *env) {
604 ir_node *op1 = get_Shr_left(env->irn);
605 ir_node *op2 = get_Shr_right(env->irn);
607 if(is_ppc32_Const(op2))
609 ir_node *shift = new_rd_ppc32_Rlwinm(env->dbg, env->irg, env->block, op1, env->mode);
610 tarval *tv_const = get_ppc32_constant_tarval(op2);
611 int sh = get_tarval_long(tv_const);
612 assert(0<=sh && sh<=31);
613 set_ppc32_rlwimi_const(shift, 32-sh, sh, 31);
616 return new_rd_ppc32_Srw(env->dbg, env->irg, env->block, op1, op2, env->mode);
620 * Creates an ppc Sraw.
622 * @param env The transformation environment
623 * @return the created ppc Sraw node
625 static ir_node *gen_Shrs(ppc32_transform_env_t *env) {
626 ir_node *op1 = get_Shrs_left(env->irn);
627 ir_node *op2 = get_Shrs_right(env->irn);
629 if(is_ppc32_Const(op2))
631 ir_node *shift = new_rd_ppc32_Srawi(env->dbg, env->irg, env->block, op1, env->mode);
632 tarval *tv_const = get_ppc32_constant_tarval(op2);
633 int sh = get_tarval_long(tv_const);
634 assert(0<=sh && sh<=31);
636 set_ppc32_constant_tarval(shift, tv_const);
637 set_ppc32_offset_mode(shift, ppc32_ao_None);
640 return new_rd_ppc32_Sraw(env->dbg, env->irg, env->block, op1, op2, env->mode);
644 * Creates an ppc Rotl.
646 * @param env The transformation environment
647 * @return the created ppc Rotl node
649 static ir_node *gen_Rotl(ppc32_transform_env_t *env) {
650 ir_node *op1 = get_Rotl_left(env->irn);
651 ir_node *op2 = get_Rotl_right(env->irn);
653 if(is_ppc32_Const(op2))
655 ir_node *rot = new_rd_ppc32_Rlwinm(env->dbg, env->irg, env->block, op1, env->mode);
656 tarval *tv_const = get_ppc32_constant_tarval(op2);
657 int sh = get_tarval_long(tv_const);
658 assert(0<=sh && sh<=31);
659 set_ppc32_rlwimi_const(rot, sh, 0, 31);
662 return new_rd_ppc32_Rlwnm(env->dbg, env->irg, env->block, op1, op2, env->mode);
666 * Creates an ppc Cmp.
668 * @param env The transformation environment
669 * @return the created ppc Cmp node
671 static ir_node *gen_Cmp(ppc32_transform_env_t *env) {
672 ir_node *op1 = get_Cmp_left(env->irn);
673 ir_node *op2 = get_Cmp_right(env->irn);
675 const ir_edge_t *edge;
676 foreach_out_edge(env->irn, edge)
678 if (is_Proj(edge->src))
679 set_irn_mode(edge->src, get_ppc32_mode_Cond());
682 if(mode_is_float(env->mode))
683 return new_rd_ppc32_fCmpu(env->dbg, env->irg, env->block, op1, op2, env->mode);
684 else if(mode_is_signed(env->mode))
686 if(is_16bit_signed_const(op2))
688 ir_node *cmp = new_rd_ppc32_Cmpi(env->dbg, env->irg, env->block, op1, env->mode);
689 tarval *tv_const = get_ppc32_constant_tarval(op2);
690 set_ppc32_constant_tarval(cmp, tv_const);
691 set_ppc32_offset_mode(cmp, ppc32_ao_None);
696 return new_rd_ppc32_Cmp(env->dbg, env->irg, env->block, op1, op2, env->mode);
701 if(is_16bit_unsigned_const(op2))
703 ir_node *cmp = new_rd_ppc32_Cmpli(env->dbg, env->irg, env->block, op1, env->mode);
704 tarval *tv_const = get_ppc32_constant_tarval(op2);
705 set_ppc32_constant_tarval(cmp, tv_const);
706 set_ppc32_offset_mode(cmp, ppc32_ao_None);
712 return new_rd_ppc32_Cmpl(env->dbg, env->irg, env->block, op1, op2, env->mode);
718 * Transforms a Minus node.
720 * @param env The transformation environment
721 * @return the created ppc Minus node
723 static ir_node *gen_Minus(ppc32_transform_env_t *env) {
724 ir_node *op = get_Minus_op(env->irn);
726 switch(get_nice_modecode(env->mode)){
729 return new_rd_ppc32_fNeg(env->dbg, env->irg, env->block, op, env->mode);
737 return new_rd_ppc32_Neg(env->dbg, env->irg, env->block, op, env->mode);
740 panic("Mode for Neg not supported: %F", env->mode);
745 * Transforms a Not node.
747 * @param env The transformation environment
748 * @return the created ppc Not node
750 static ir_node *gen_Not(ppc32_transform_env_t *env) {
751 return new_rd_ppc32_Not(env->dbg, env->irg, env->block, get_Not_op(env->irn), env->mode);
755 static ir_node *own_gen_Andi_dot_lo16(ppc32_transform_env_t *env, ir_node *op, int mask)
757 ir_node *andi = new_rd_ppc32_Andi_dot(env->dbg, env->irg, env->block, op, mode_T);
759 set_ppc32_offset_mode(andi, ppc32_ao_Lo16);
760 set_ppc32_constant_tarval(andi, new_tarval_from_long(mask, mode_Is));
761 in[0] = new_rd_Proj(env->dbg, env->irg, env->block, andi, env->mode,1);
762 be_new_Keep(&ppc32_reg_classes[CLASS_ppc32_condition], env->irg, env->block, 1, in);
763 return new_rd_Proj(env->dbg, env->irg, env->block, andi, env->mode,0);
767 * Transforms a Conv node.
769 * @param env The transformation environment
770 * @return the created ppc Conv node
772 static ir_node *gen_Conv(ppc32_transform_env_t *env) {
773 ir_node *op = get_Conv_op(env->irn);
774 ir_modecode from_mode=get_nice_modecode(get_irn_mode(op));
775 ir_modecode to_mode=get_nice_modecode(env->mode);
777 #define SKIP return op
779 if(from_mode == to_mode) SKIP;
795 return new_rd_ppc32_fRsp(env->dbg, env->irg, env->block, op, env->mode);
806 return new_rd_ppc32_Extsh(env->dbg, env->irg, env->block, op, env->mode);
808 return own_gen_Andi_dot_lo16(env, op, 0xffff);
810 return new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, op, env->mode);
812 return own_gen_Andi_dot_lo16(env, op, 0xff);
826 if(from_mode==irm_Hu)
828 return own_gen_Andi_dot_lo16(env, op, 0xffff);
832 return new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, op, env->mode);
834 return own_gen_Andi_dot_lo16(env, op, 0xff);
836 return new_rd_ppc32_Extsh(env->dbg, env->irg, env->block, op, env->mode);
848 if(from_mode==irm_Bs)
850 return own_gen_Andi_dot_lo16(env, op, 0xff);
855 return new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, op, env->mode);
861 if(to_mode==irm_Is || to_mode==irm_Iu) SKIP;
867 panic("Mode for Conv not supported: %F -> %F", get_irn_mode(op), env->mode);
872 * Transforms an Abs node.
874 * @param env The transformation environment
875 * @return the ppc node generating the absolute value
877 static ir_node *gen_Abs(ppc32_transform_env_t *env) {
878 ir_node *op = get_Abs_op(env->irn);
882 switch(get_nice_modecode(env->mode))
886 return new_rd_ppc32_fAbs(env->dbg, env->irg, env->block, op, env->mode);
892 n1 = new_rd_ppc32_Srawi(env->dbg, env->irg, env->block, op, env->mode);
893 set_ppc32_constant_tarval(n1, new_tarval_from_long(shift, mode_Is));
894 set_ppc32_offset_mode(n1, ppc32_ao_None);
895 n2 = new_rd_ppc32_Add(env->dbg, env->irg, env->block, op, n1, env->mode);
896 return new_rd_ppc32_Xor(env->dbg, env->irg, env->block, n2, n1, env->mode);
900 panic("Mode for Abs not supported: %F", env->mode);
905 * Transforms an Cond node.
907 * @param env The transformation environment
908 * @return a ppc branch node
910 static ir_node *gen_Cond(ppc32_transform_env_t *env) {
911 ir_node *selector = get_Cond_selector(env->irn);
912 ir_mode *projmode = get_irn_mode(selector);
913 if(is_Proj(selector) && projmode==get_ppc32_mode_Cond())
915 int projnum = get_Proj_proj(selector);
916 ir_node *branch = new_rd_ppc32_Branch(env->dbg, env->irg, env->block, selector, env->mode);
917 set_ppc32_proj_nr(branch, projnum);
922 ir_node *unknown_gpr = new_rd_ppc32_Unknown(env->dbg, env->irg, env->block, mode_Is);
923 ir_node *unknown_cond = new_rd_ppc32_cUnknown(env->dbg, env->irg, env->block, get_ppc32_mode_Cond());
925 ir_node *switch_node = new_rd_ppc32_Switch(env->dbg, env->irg, env->block, selector,
926 unknown_gpr, unknown_cond, env->mode);
927 set_ppc32_proj_nr(switch_node, get_Cond_defaultProj(env->irn));
934 * Transforms an Unknown node.
936 * @param env The transformation environment
937 * @return a ppc Unknown node
939 static ir_node *gen_Unknown(ppc32_transform_env_t *env) {
940 if(mode_is_float(env->mode))
941 return new_rd_ppc32_fUnknown(env->dbg, env->irg, env->block, env->mode);
942 else if (mode_is_int(env->mode))
943 return new_rd_ppc32_Unknown(env->dbg, env->irg, env->block, env->mode);
945 panic("Mode %F for unknown value not supported.", env->mode);
948 static ir_node *ldst_insert_const(ir_node *ptr, tarval **ptv, ident **pid, ppc32_transform_env_t *env) {
949 tarval *tv_const = NULL;
950 ident *id_symconst = NULL;
952 if(is_ppc32_Const(ptr))
954 tv_const = get_ppc32_constant_tarval(ptr);
955 ptr = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, mode_P, ppc32_ao_Ha16, tv_const, NULL);
957 else if(is_ppc32_SymConst(ptr))
959 ir_entity *ent = get_ppc32_frame_entity(ptr);
960 if(is_direct_entity(ent))
962 id_symconst = get_entity_ident(ent);
963 ptr = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, mode_P, ppc32_ao_Ha16, NULL, id_symconst);
974 * @param env The transformation environment
975 * @return the created ppc Load node
977 static ir_node *gen_Load(ppc32_transform_env_t *env) {
978 ir_node *node = env->irn;
979 ir_node *loadptr = get_Load_ptr(node);
981 ir_mode *mode = get_Load_mode(node);
982 tarval *tv_const = NULL;
983 ident *id_symconst = NULL;
985 loadptr = ldst_insert_const(loadptr, &tv_const, &id_symconst, env);
986 switch(get_nice_modecode(mode)){
988 load = new_rd_ppc32_Lbz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
993 ir_node *proj_load, *extsb_node;
994 load = new_rd_ppc32_Lbz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
995 proj_load = new_rd_Proj(env->dbg, env->irg, env->block, load, mode, pn_Load_res);
996 extsb_node = new_rd_ppc32_Extsb(env->dbg, env->irg, env->block, proj_load, mode);
997 exchange(get_succ_Proj(env->irn, pn_Load_res), extsb_node);
1003 load = new_rd_ppc32_Lhz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1006 load =new_rd_ppc32_Lha(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1011 load = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1015 load = new_rd_ppc32_Lfd(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1018 load = new_rd_ppc32_Lfs(env->dbg, env->irg, env->block, loadptr, get_Load_mem(node));
1022 panic("Mode for Load not supported: %F", env->mode);
1027 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1028 set_ppc32_constant_tarval(load, tv_const);
1030 else if(id_symconst)
1032 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1033 set_ppc32_symconst_ident(load, id_symconst);
1041 * Transforms a Store.
1043 * @param env The transformation environment
1044 * @return the created ppc Store node
1046 static ir_node *gen_Store(ppc32_transform_env_t *env) {
1047 ir_node *node = env->irn;
1048 ir_node *storeptr = get_Store_ptr(node);
1049 ir_node *valuenode = get_Store_value(node);
1050 ir_mode *mode = get_irn_mode(valuenode);
1052 tarval *tv_const = NULL;
1053 ident *id_symconst = NULL;
1055 storeptr = ldst_insert_const(storeptr, &tv_const, &id_symconst, env);
1057 switch(get_nice_modecode(mode)){
1060 store = new_rd_ppc32_Stb(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1065 store = new_rd_ppc32_Sth(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1070 store = new_rd_ppc32_Stw(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1074 store = new_rd_ppc32_Stfd(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1077 store = new_rd_ppc32_Stfs(env->dbg, env->irg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1081 panic("Mode for Store not supported: %F", env->mode);
1085 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1086 set_ppc32_constant_tarval(store, tv_const);
1088 else if(id_symconst)
1090 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1091 set_ppc32_symconst_ident(store, id_symconst);
1097 * Transforms a CopyB.
1099 * @param env The transformation environment
1100 * @return the created ppc CopyB node
1102 static ir_node *gen_CopyB(ppc32_transform_env_t *env) {
1103 ir_node *mem = get_CopyB_mem(env->irn);
1104 ir_node *src = get_CopyB_src(env->irn);
1105 ir_node *dest = get_CopyB_dst(env->irn);
1106 ir_type *type = get_CopyB_type(env->irn);
1107 int size = get_type_size_bytes(type);
1110 ir_node *load, *store = NULL;
1115 tarval *offset0 = new_tarval_from_long(0, mode_Is);
1116 tarval *offset4 = new_tarval_from_long(4, mode_Is);
1118 load = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, src, mem);
1119 set_ppc32_constant_tarval(load, offset0);
1120 set_ppc32_offset_mode(load, ppc32_ao_None);
1121 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1122 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1124 store = new_rd_ppc32_Stw(env->dbg, env->irg, env->block, dest, res, mem);
1125 set_ppc32_constant_tarval(store, offset0);
1126 set_ppc32_offset_mode(store, ppc32_ao_None);
1127 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1131 load = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, src, mem);
1132 set_ppc32_constant_tarval(load, offset4);
1133 set_ppc32_offset_mode(load, ppc32_ao_None);
1134 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1135 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1137 store = new_rd_ppc32_Stw(env->dbg, env->irg, env->block, dest, res, mem);
1138 set_ppc32_constant_tarval(store, offset4);
1139 set_ppc32_offset_mode(store, ppc32_ao_None);
1140 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1146 ir_node *ornode, *mtctrnode;
1148 assert(size/4-1<=0xffff);
1151 ornode = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1152 set_ppc32_offset_mode(ornode, ppc32_ao_None);
1156 ir_node *zeroreg = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1157 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1158 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1159 ornode = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, zeroreg, mode_Is);
1160 set_ppc32_offset_mode(ornode, ppc32_ao_Lo16);
1163 set_ppc32_constant_tarval(ornode, new_tarval_from_long(size/4-1, mode_Is));
1164 mtctrnode = new_rd_ppc32_Mtctr(env->dbg, env->irg, env->block, ornode, mode_Is);
1165 store = new_rd_ppc32_LoopCopy(env->dbg, env->irg, env->block, src, dest, mtctrnode, mem, mode_T);
1167 in[0] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 1); // src
1168 in[1] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 2); // dest
1169 in[2] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 4); // temp
1170 be_new_Keep(&ppc32_reg_classes[CLASS_ppc32_gp], env->irg, env->block, 3, in);
1171 in[0] = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_Is, 3); // ctr
1172 be_new_Keep(&ppc32_reg_classes[CLASS_ppc32_count], env->irg, env->block, 1, in);
1174 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, 0);
1183 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1184 load = new_rd_ppc32_Lhz(env->dbg, env->irg, env->block, src, mem);
1185 set_ppc32_constant_tarval(load, offset_tarval);
1186 set_ppc32_offset_mode(load, ppc32_ao_None);
1187 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1188 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1190 store = new_rd_ppc32_Sth(env->dbg, env->irg, env->block, dest, res, mem);
1191 set_ppc32_constant_tarval(store, offset_tarval);
1192 set_ppc32_offset_mode(store, ppc32_ao_None);
1193 mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1201 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1202 load = new_rd_ppc32_Lbz(env->dbg, env->irg, env->block, src, mem);
1203 set_ppc32_constant_tarval(load, offset_tarval);
1204 set_ppc32_offset_mode(load, ppc32_ao_None);
1205 mem = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_M, pn_Load_M);
1206 res = new_rd_Proj(env->dbg, env->irg, env->block, load, mode_Is, pn_Load_res);
1208 store = new_rd_ppc32_Stb(env->dbg, env->irg, env->block, dest, res, mem);
1209 set_ppc32_constant_tarval(store, offset_tarval);
1210 set_ppc32_offset_mode(store, ppc32_ao_None);
1211 // mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1218 * Transforms a FrameAddr into a ppc Add.
1220 * @param env The transformation environment
1222 static ir_node *gen_be_FrameAddr(ppc32_transform_env_t *env) {
1223 ir_node *op = get_irn_n(env->irn, 0);
1224 ir_node *add = new_rd_ppc32_Addi(env->dbg, env->irg, env->block, op, mode_P);
1225 set_ppc32_frame_entity(add, be_get_frame_entity(env->irn));
1230 /*********************************************************
1233 * _ __ ___ __ _ _ _ __ __| |_ __ ___ _____ _ __
1234 * | '_ ` _ \ / _` | | '_ \ / _` | '__| \ \ / / _ \ '__|
1235 * | | | | | | (_| | | | | | | (_| | | | |\ V / __/ |
1236 * |_| |_| |_|\__,_|_|_| |_| \__,_|_| |_| \_/ \___|_|
1238 *********************************************************/
1241 * the BAD transformer.
1243 static ir_node *bad_transform(ppc32_transform_env_t *env) {
1244 panic("Transformation not implemented: %+F\n", env->irn);
1248 * Enters all transform functions into the generic pointer
1250 void ppc32_register_transformers(void) {
1251 ir_op *op_Max, *op_Min, *op_Mulh;
1253 /* first clear the generic function pointer for all ops */
1254 clear_irp_opcodes_generic_func();
1256 #define FIRM_OP(a) op_##a->ops.generic = (op_func)gen_##a
1257 #define BAD(a) op_##a->ops.generic = (op_func)bad_transform
1288 /* TODO: implement these nodes */
1291 /* You probably don't need to handle the following nodes */
1321 FIRM_OP(be_FrameAddr);
1322 op_Mulh = get_op_Mulh();
1325 op_Max = get_op_Max();
1328 op_Min = get_op_Min();
1333 typedef ir_node *(transform_func)(ppc32_transform_env_t *env);
1336 * Transforms the given firm node (and maybe some other related nodes)
1337 * into one or more assembler nodes.
1339 * @param node the firm node
1340 * @param env the debug module
1342 void ppc32_transform_node(ir_node *node, void *env) {
1343 ppc32_code_gen_t *cg = (ppc32_code_gen_t *)env;
1344 ir_op *op = get_irn_op(node);
1345 ir_node *asm_node = NULL;
1351 DBG((cg->mod, LEVEL_1, "check %+F ... ", node));
1353 if (op->ops.generic) {
1354 ppc32_transform_env_t tenv;
1355 transform_func *transform = (transform_func *)op->ops.generic;
1357 tenv.block = get_nodes_block(node);
1358 tenv.dbg = get_irn_dbg_info(node);
1359 tenv.irg = current_ir_graph;
1361 tenv.mode = get_irn_mode(node);
1362 DEBUG_ONLY(tenv.mod = cg->mod;)
1364 asm_node = (*transform)(&tenv);
1368 exchange(node, asm_node);
1369 DB((cg->mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1372 DB((cg->mod, LEVEL_1, "ignored\n"));
1377 * Constant generating code
1385 /** Compares two (entity, tarval) combinations */
1386 static int cmp_tv_ent(const void *a, const void *b, size_t len) {
1387 const struct tv_ent *e1 = a;
1388 const struct tv_ent *e2 = b;
1391 return !(e1->tv == e2->tv);
1394 /** Generates a SymConst node for a known FP const */
1395 static ir_node *gen_fp_known_symconst(ppc32_transform_env_t *env, tarval *known_const) {
1396 static set *const_set = NULL;
1397 static ir_type *tp = NULL;
1399 struct tv_ent *entry;
1400 ir_node *cnst,*symcnst;
1402 ir_entity *ent = NULL;
1405 const_set = new_set(cmp_tv_ent, 10);
1407 tp = new_type_primitive(new_id_from_str("const_double_t"), env->mode);
1409 key.tv = known_const;
1412 entry = set_insert(const_set, &key, sizeof(key), HASH_PTR(key.tv));
1416 sprintf(buf, "const_%ld", get_irn_node_nr(env->irn));
1417 ent = new_entity(get_glob_type(), new_id_from_str(buf), tp);
1419 set_entity_ld_ident(ent, get_entity_ident(ent));
1420 set_entity_visibility(ent, visibility_local);
1421 set_entity_variability(ent, variability_constant);
1422 set_entity_allocation(ent, allocation_static);
1424 /* we create a new entity here: It's initialization must resist on the
1426 rem = current_ir_graph;
1427 current_ir_graph = get_const_code_irg();
1428 cnst = new_Const(env->mode, key.tv);
1429 current_ir_graph = rem;
1431 set_atomic_ent_value(ent, cnst);
1433 /* set the entry for hashmap */
1435 } // TODO: Wird nicht richtig in global type gesteckt, ppc32_gen_decls.c findet ihn nicht
1437 symcnst = new_rd_ppc32_SymConst(env->dbg, env->irg, env->block, env->mode);
1438 set_ppc32_frame_entity(symcnst, ent);
1442 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env);
1445 * Transforms a Const.
1447 * @param mod the debug module
1448 * @param block the block the new node should belong to
1449 * @param node the ir Const node
1450 * @param mode node mode
1451 * @return the created ppc Load immediate node
1453 static ir_node *gen_ppc32_Const(ppc32_transform_env_t *env) {
1454 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1457 switch(get_nice_modecode(env->mode)){
1460 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1463 ir_node *zeroreg = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1464 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1465 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1466 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, zeroreg, mode_Is);
1467 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1474 node = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, env->mode);
1475 set_ppc32_offset_mode(node, ppc32_ao_None);
1481 unsigned char val2 = get_tarval_sub_bits(tv_const,2);
1482 unsigned char val3 = get_tarval_sub_bits(tv_const,3);
1485 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1488 ir_node *zeroreg = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, mode_Is);
1489 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1490 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1491 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, zeroreg, mode_Is);
1492 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1496 node = new_rd_ppc32_Addi_zero(env->dbg, env->irg, env->block, env->mode);
1497 set_ppc32_offset_mode(node, ppc32_ao_None);
1502 unsigned char val0 = get_tarval_sub_bits(tv_const,0);
1503 unsigned char val1 = get_tarval_sub_bits(tv_const,1);
1504 node = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Hi16, tv_const, NULL);
1507 set_ppc32_constant_tarval(node, tv_const);
1508 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, node, env->mode);
1509 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1516 panic("Mode for Const not supported: %F", env->mode);
1518 set_ppc32_constant_tarval(node, tv_const);
1523 * Transforms a fConst.
1525 * @param mod the debug module
1526 * @param block the block the new node should belong to
1527 * @param node the ir Const node
1528 * @param mode node mode
1529 * @return the created ppc float Load node
1531 static ir_node *gen_ppc32_fConst(ppc32_transform_env_t *env) {
1532 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1534 switch(get_nice_modecode(env->mode)){
1538 ir_node *addr, *load;
1539 ir_mode *mode = env->mode;
1541 env->irn = gen_fp_known_symconst(env, tv_const);
1543 ent = get_ppc32_frame_entity(env->irn);
1544 if(is_direct_entity(ent))
1546 ident *id_symconst = get_entity_ident(ent);
1547 ir_node *node_addis = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1550 load = new_rd_ppc32_Lfd(env->dbg, env->irg, env->block, node_addis, new_NoMem());
1552 load = new_rd_ppc32_Lfs(env->dbg, env->irg, env->block, node_addis, new_NoMem());
1554 set_ppc32_symconst_ident(load, id_symconst);
1555 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1559 addr = gen_ppc32_SymConst (env);
1561 load = new_rd_ppc32_Lfd(env->dbg, env->irg, env->block, addr, new_NoMem());
1563 load = new_rd_ppc32_Lfs(env->dbg, env->irg, env->block, addr, new_NoMem());
1565 return new_rd_Proj(env->dbg, env->irg, env->block, load, mode, pn_Load_res);
1569 panic("Mode for fConst not supported: %F", env->mode);
1575 * Returns true, if the entity can be accessed directly,
1576 * or false, if the address must be loaded first
1578 int is_direct_entity(ir_entity *ent) {
1579 return get_entity_visibility(ent) != visibility_external_allocated;
1580 /* visibility vis = get_entity_visibility(ent);
1581 if(is_Method_type(get_entity_type(ent)))
1583 return (vis!=visibility_external_allocated);
1587 return (vis==visibility_local);
1592 * Transforms a SymConst.
1594 * @param mod the debug module
1595 * @param block the block the new node should belong to
1596 * @param node the ir Const node
1597 * @param mode node mode
1598 * @return the created ppc Load immediate node
1600 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env) {
1601 ir_entity *ent = get_ppc32_frame_entity(env->irn);
1602 ident *id_symconst = get_entity_ident(ent);
1604 switch(get_nice_modecode(env->mode)){
1607 if (is_direct_entity(ent))
1609 ir_node *node_addis = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Hi16, NULL, id_symconst);
1610 node = new_rd_ppc32_Ori(env->dbg, env->irg, env->block, node_addis, env->mode);
1611 set_ppc32_symconst_ident(node, id_symconst);
1612 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1616 ir_node *node_addis = new_rd_ppc32_Addis_zero(env->dbg, env->irg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1617 node = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, node_addis, new_NoMem());
1618 set_ppc32_symconst_ident(node, id_symconst);
1619 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1620 node = new_rd_Proj(env->dbg, env->irg, env->block, node, env->mode, pn_Load_res);
1626 panic("Mode for SymConst not supported: %F", env->mode);
1632 * Transforms the given firm node (and maybe some other related nodes)
1633 * into one or more assembler nodes.
1635 * @param node the firm node
1636 * @param env the debug module
1638 void ppc32_transform_const(ir_node *node, void *env) {
1639 ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
1640 ir_node *asm_node = NULL;
1641 ppc32_transform_env_t tenv;
1647 tenv.block = get_nodes_block(node);
1648 tenv.dbg = get_irn_dbg_info(node);
1649 tenv.irg = current_ir_graph;
1651 DEBUG_ONLY(tenv.mod = cgenv->mod;)
1652 tenv.mode = get_irn_mode(node);
1654 #define OTHER_GEN(a) \
1655 if (get_irn_op(node) == get_op_##a()) { \
1656 asm_node = gen_##a(&tenv); \
1659 DBG((tenv.mod, LEVEL_1, "check %+F ... ", node));
1661 OTHER_GEN(ppc32_Const)
1662 else OTHER_GEN(ppc32_fConst)
1663 else OTHER_GEN(ppc32_SymConst)
1666 exchange(node, asm_node);
1667 DB((tenv.mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1670 DB((tenv.mod, LEVEL_1, "ignored\n"));