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.h"
42 #include "bearch_ppc32_t.h"
44 #include "ppc32_nodes_attr.h"
45 #include "ppc32_transform.h"
46 #include "ppc32_new_nodes.h"
47 #include "ppc32_map_regs.h"
49 #include "gen_ppc32_regalloc_if.h"
51 extern ir_op *get_op_Mulh(void);
53 ir_mode* ppc32_mode_Cond = NULL;
56 * Returns the proj of a given node with the given proj number
58 static inline ir_node *get_succ_Proj(ir_node *node, long proj)
60 const ir_edge_t *edge;
61 foreach_out_edge(node, edge)
63 if (is_Proj(edge->src) && get_Proj_proj(edge->src) == proj)
70 * Returns a singleton condition mode
72 static ir_mode *get_ppc32_mode_Cond(void)
75 return ppc32_mode_Cond;
77 ppc32_mode_Cond = new_ir_mode("mode_Cond", irms_int_number, 4, 0, irma_none, 0);
78 return ppc32_mode_Cond;
83 * Calculates the ppc32_modecode with size, sort and signed attributes
85 ppc32_modecode get_nice_modecode(ir_mode *irmode)
87 ppc32_modecode mode = irm_max;
88 int sign = mode_is_signed(irmode);
89 int bits = get_mode_size_bits(irmode);
90 if (mode_is_int(irmode))
95 mode = sign ? irm_Bs : irm_Bu;
98 mode = sign ? irm_Hs : irm_Hu;
101 mode = sign ? irm_Is : irm_Iu;
105 else if (mode_is_float(irmode))
117 else if (mode_is_reference(irmode))
130 * Returns true, if the given node is a Const node and it's value fits into
131 * a signed 16-bit variable
133 static int is_16bit_signed_const(ir_node *node)
137 if (!is_ppc32_Const(node)) return 0;
139 tv_const = get_ppc32_constant_tarval(node);
141 switch (get_nice_modecode(get_irn_mode(node)))
150 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
151 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
159 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
167 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
168 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
169 if (val2==0 && val3==0)
171 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
176 if (!(val2==0xff && val3==0xff))
178 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
186 panic("is_16bit_signed_const(): Mode not supported: %F", get_irn_mode(node));
191 * Returns true, if the given node is a Const node and it's value fits into
192 * a unsigned 16-bit variable
194 static int is_16bit_unsigned_const(ir_node *node)
198 if (!is_ppc32_Const(node)) return 0;
200 tv_const = get_ppc32_constant_tarval(node);
201 switch (get_nice_modecode(get_irn_mode(node)))
212 unsigned char val2 = get_tarval_sub_bits(tv_const, 2);
213 unsigned char val3 = get_tarval_sub_bits(tv_const, 3);
219 panic("is_16bit_unsigned_const(): Mode not supported: %F", get_irn_mode(node));
224 /****************************************************************************************************
226 * | | | | / _| | | (_)
227 * _ __ ___ __| | ___ | |_ _ __ __ _ _ __ ___| |_ ___ _ __ _ __ ___ __ _| |_ _ ___ _ __
228 * | '_ \ / _ \ / _` |/ _ \ | __| '__/ _` | '_ \/ __| _/ _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \
229 * | | | | (_) | (_| | __/ | |_| | | (_| | | | \__ \ || (_) | | | | | | | | (_| | |_| | (_) | | | |
230 * |_| |_|\___/ \__,_|\___| \__|_| \__,_|_| |_|___/_| \___/|_| |_| |_| |_|\__,_|\__|_|\___/|_| |_|
232 ****************************************************************************************************/
235 * Creates an ppc Add.
237 * @param env The transformation environment
238 * @return the created ppc Add node
240 static ir_node *gen_Add(ppc32_transform_env_t *env)
242 ir_node *op1 = get_Add_left(env->irn);
243 ir_node *op2 = get_Add_right(env->irn);
245 switch (get_nice_modecode(env->mode)){
247 return new_bd_ppc32_fAdd(env->dbg, env->block, op1, op2, env->mode);
249 return new_bd_ppc32_fAdds(env->dbg, env->block, op1, op2, env->mode);
257 if (is_16bit_signed_const(op1))
259 ir_node *addnode = new_bd_ppc32_Addi(env->dbg, env->block, op2, env->mode);
260 set_ppc32_constant_tarval(addnode, get_ppc32_constant_tarval(op1));
261 set_ppc32_offset_mode(addnode, ppc32_ao_None);
264 if (is_16bit_signed_const(op2))
266 ir_node *addnode = new_bd_ppc32_Addi(env->dbg, env->block, op1, env->mode);
267 set_ppc32_constant_tarval(addnode, get_ppc32_constant_tarval(op2));
268 set_ppc32_offset_mode(addnode, ppc32_ao_None);
272 return new_bd_ppc32_Add(env->dbg, env->block, op1, op2, env->mode);
275 panic("Mode for Add not supported: %F", env->mode);
280 * Creates an ppc Mul.
282 * @param env The transformation environment
283 * @return the created ppc Mul node
285 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_bd_ppc32_fMul(env->dbg, env->block, op1, op2, env->mode);
294 return new_bd_ppc32_fMuls(env->dbg, env->block, op1, op2, env->mode);
301 return new_bd_ppc32_Mullw(env->dbg, 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)
317 ir_node *op1 = get_irn_n(env->irn, 0);
318 ir_node *op2 = get_irn_n(env->irn, 1);
320 switch (get_nice_modecode(env->mode)){
324 return new_bd_ppc32_Mulhw(env->dbg, env->block, op1, op2, env->mode);
329 return new_bd_ppc32_Mulhwu(env->dbg, env->block, op1, op2, env->mode);
335 panic("Mode for Mulh not supported: %F", env->mode);
340 * Creates an ppc And.
342 * @param env The transformation environment
343 * @return the created ppc And node
345 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_bd_ppc32_And(env->dbg, 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)
361 ir_node *op1 = get_Or_left(env->irn);
362 ir_node *op2 = get_Or_right(env->irn);
364 return new_bd_ppc32_Or(env->dbg, env->block, op1, op2, env->mode);
368 * Creates an ppc Xor.
370 * @param env The transformation environment
371 * @return the created ppc Xor node
373 static ir_node *gen_Eor(ppc32_transform_env_t *env)
375 ir_node *op1 = get_Eor_left(env->irn);
376 ir_node *op2 = get_Eor_right(env->irn);
378 return new_bd_ppc32_Xor(env->dbg, env->block, op1, op2, env->mode);
382 * Creates an ppc Sub.
384 * @param env The transformation environment
385 * @return the created ppc Sub node
387 static ir_node *gen_Sub(ppc32_transform_env_t *env)
389 ir_node *op1 = get_Sub_left(env->irn);
390 ir_node *op2 = get_Sub_right(env->irn);
392 switch (get_nice_modecode(env->mode)){
394 return new_bd_ppc32_fSub(env->dbg, env->block, op1, op2, env->mode);
396 return new_bd_ppc32_fSubs(env->dbg, env->block, op1, op2, env->mode);
404 return new_bd_ppc32_Sub(env->dbg, env->block, op1, op2, env->mode);
407 panic("Mode for Sub not supported: %F", env->mode);
412 * Creates an ppc floating Div.
414 * @param env The transformation environment
415 * @return the created ppc fDiv node
417 static ir_node *gen_Quot(ppc32_transform_env_t *env)
419 ir_node *op1 = get_Quot_left(env->irn);
420 ir_node *op2 = get_Quot_right(env->irn);
422 switch (get_nice_modecode(env->mode)){
424 return new_bd_ppc32_fDiv(env->dbg, env->block, op1, op2, env->mode);
426 return new_bd_ppc32_fDivs(env->dbg, env->block, op1, op2, env->mode);
429 panic("Mode for Quot not supported: %F", env->mode);
434 * Creates an ppc integer Div.
436 * @param env The transformation environment
437 * @return the created ppc Div node
439 static ir_node *gen_Div(ppc32_transform_env_t *env)
441 ir_node *op1 = get_Div_left(env->irn);
442 ir_node *op2 = get_Div_right(env->irn);
444 switch (get_nice_modecode(get_irn_mode(op1))){
448 return new_bd_ppc32_Divw(env->dbg, env->block, op1, op2, mode_T);
453 return new_bd_ppc32_Divwu(env->dbg, env->block, op1, op2, mode_T);
456 panic("Mode for Div not supported: %F", get_irn_mode(op1));
461 * Creates an ppc integer Div and Mod.
463 * @param env The transformation environment
464 * @return the created ppc Div node
466 static ir_node *gen_DivMod(ppc32_transform_env_t *env)
468 ir_node *op1 = get_DivMod_left(env->irn);
469 ir_node *op2 = get_DivMod_right(env->irn);
470 ir_node *proj_div = NULL, *proj_mod = NULL;
472 const ir_edge_t *edge;
475 foreach_out_edge(env->irn, edge)
477 if (is_Proj(edge->src))
479 switch (get_Proj_proj(edge->src)){
480 case pn_DivMod_res_div:
481 proj_div = edge->src;
483 case pn_DivMod_res_mod:
484 proj_mod = edge->src;
492 assert(proj_div!=NULL || proj_mod!=NULL);
494 res_mode = get_irn_mode(proj_div);
496 switch (get_nice_modecode(res_mode))
501 div_result = new_bd_ppc32_Divw(env->dbg, env->block, op1, op2, mode_T);
507 div_result = new_bd_ppc32_Divwu(env->dbg, env->block, op1, op2, mode_T);
511 panic("Mode for DivMod not supported: %F", res_mode);
514 if (proj_div == NULL)
515 proj_div = new_rd_Proj(env->dbg, div_result, get_irn_mode(proj_mod), pn_DivMod_res_div);
521 mul_result = new_bd_ppc32_Mullw(env->dbg, env->block, proj_div, op2, res_mode);
522 mod_result = new_bd_ppc32_Sub(env->dbg, env->block, op1, mul_result, res_mode);
524 exchange(proj_mod, mod_result);
531 * Creates an ppc integer Mod.
533 * @param env The transformation environment
534 * @return the created ppc Mod result node
536 static ir_node *gen_Mod(ppc32_transform_env_t *env)
538 ir_node *op1 = get_Mod_left(env->irn);
539 ir_node *op2 = get_Mod_right(env->irn);
540 ir_node *proj_div = NULL, *proj_mod = NULL;
546 proj_mod = get_succ_Proj(env->irn, pn_Mod_res);
548 assert(proj_mod != NULL);
549 res_mode = get_irn_mode(proj_mod);
551 switch (get_nice_modecode(res_mode))
556 div_result = new_bd_ppc32_Divw(env->dbg, env->block, op1, op2, mode_T);
562 div_result = new_bd_ppc32_Divwu(env->dbg, env->block, op1, op2, mode_T);
566 panic("Mode for Mod not supported: %s\n", get_mode_name(res_mode));
569 proj_div = new_rd_Proj(env->dbg, div_result, res_mode, pn_DivMod_res_div);
571 mul_result = new_bd_ppc32_Mullw(env->dbg, env->block, proj_div, op2, res_mode);
572 mod_result = new_bd_ppc32_Sub(env->dbg, env->block, op1, mul_result, res_mode);
574 exchange(proj_mod, mod_result);
580 * Creates an ppc Shl.
582 * @param env The transformation environment
583 * @return the created ppc Shl node
585 static ir_node *gen_Shl(ppc32_transform_env_t *env)
587 ir_node *op1 = get_Shl_left(env->irn);
588 ir_node *op2 = get_Shl_right(env->irn);
590 if (is_ppc32_Const(op2))
592 ir_node *shift = new_bd_ppc32_Rlwinm(env->dbg, env->block, op1, env->mode);
593 tarval *tv_const = get_ppc32_constant_tarval(op2);
594 int sh = get_tarval_long(tv_const);
595 assert(0<=sh && sh<=31);
596 set_ppc32_rlwimi_const(shift, sh, 0, 31-sh);
599 return new_bd_ppc32_Slw(env->dbg, env->block, op1, op2, env->mode);
603 * Creates an ppc Srw.
605 * @param env The transformation environment
606 * @return the created ppc Shr node
608 static ir_node *gen_Shr(ppc32_transform_env_t *env)
610 ir_node *op1 = get_Shr_left(env->irn);
611 ir_node *op2 = get_Shr_right(env->irn);
613 if (is_ppc32_Const(op2))
615 ir_node *shift = new_bd_ppc32_Rlwinm(env->dbg, env->block, op1, env->mode);
616 tarval *tv_const = get_ppc32_constant_tarval(op2);
617 int sh = get_tarval_long(tv_const);
618 assert(0<=sh && sh<=31);
619 set_ppc32_rlwimi_const(shift, 32-sh, sh, 31);
622 return new_bd_ppc32_Srw(env->dbg, env->block, op1, op2, env->mode);
626 * Creates an ppc Sraw.
628 * @param env The transformation environment
629 * @return the created ppc Sraw node
631 static ir_node *gen_Shrs(ppc32_transform_env_t *env)
633 ir_node *op1 = get_Shrs_left(env->irn);
634 ir_node *op2 = get_Shrs_right(env->irn);
636 if (is_ppc32_Const(op2))
638 ir_node *shift = new_bd_ppc32_Srawi(env->dbg, env->block, op1, env->mode);
639 tarval *tv_const = get_ppc32_constant_tarval(op2);
640 int sh = get_tarval_long(tv_const);
641 assert(0<=sh && sh<=31);
643 set_ppc32_constant_tarval(shift, tv_const);
644 set_ppc32_offset_mode(shift, ppc32_ao_None);
647 return new_bd_ppc32_Sraw(env->dbg, env->block, op1, op2, env->mode);
651 * Creates an ppc Rotl.
653 * @param env The transformation environment
654 * @return the created ppc Rotl node
656 static ir_node *gen_Rotl(ppc32_transform_env_t *env)
658 ir_node *op1 = get_Rotl_left(env->irn);
659 ir_node *op2 = get_Rotl_right(env->irn);
661 if (is_ppc32_Const(op2))
663 ir_node *rot = new_bd_ppc32_Rlwinm(env->dbg, env->block, op1, env->mode);
664 tarval *tv_const = get_ppc32_constant_tarval(op2);
665 int sh = get_tarval_long(tv_const);
666 assert(0<=sh && sh<=31);
667 set_ppc32_rlwimi_const(rot, sh, 0, 31);
670 return new_bd_ppc32_Rlwnm(env->dbg, env->block, op1, op2, env->mode);
674 * Creates an ppc Cmp.
676 * @param env The transformation environment
677 * @return the created ppc Cmp node
679 static ir_node *gen_Cmp(ppc32_transform_env_t *env)
681 ir_node *op1 = get_Cmp_left(env->irn);
682 ir_node *op2 = get_Cmp_right(env->irn);
684 const ir_edge_t *edge;
685 foreach_out_edge(env->irn, edge)
687 if (is_Proj(edge->src))
688 set_irn_mode(edge->src, get_ppc32_mode_Cond());
691 if (mode_is_float(env->mode))
692 return new_bd_ppc32_fCmpu(env->dbg, env->block, op1, op2, env->mode);
693 else if (mode_is_signed(env->mode))
695 if (is_16bit_signed_const(op2))
697 ir_node *cmp = new_bd_ppc32_Cmpi(env->dbg, env->block, op1, env->mode);
698 tarval *tv_const = get_ppc32_constant_tarval(op2);
699 set_ppc32_constant_tarval(cmp, tv_const);
700 set_ppc32_offset_mode(cmp, ppc32_ao_None);
705 return new_bd_ppc32_Cmp(env->dbg, env->block, op1, op2, env->mode);
710 if (is_16bit_unsigned_const(op2))
712 ir_node *cmp = new_bd_ppc32_Cmpli(env->dbg, env->block, op1, env->mode);
713 tarval *tv_const = get_ppc32_constant_tarval(op2);
714 set_ppc32_constant_tarval(cmp, tv_const);
715 set_ppc32_offset_mode(cmp, ppc32_ao_None);
721 return new_bd_ppc32_Cmpl(env->dbg, env->block, op1, op2, env->mode);
727 * Transforms a Minus node.
729 * @param env The transformation environment
730 * @return the created ppc Minus node
732 static ir_node *gen_Minus(ppc32_transform_env_t *env)
734 ir_node *op = get_Minus_op(env->irn);
736 switch (get_nice_modecode(env->mode)){
739 return new_bd_ppc32_fNeg(env->dbg, env->block, op, env->mode);
747 return new_bd_ppc32_Neg(env->dbg, env->block, op, env->mode);
750 panic("Mode for Neg not supported: %F", env->mode);
755 * Transforms a Not node.
757 * @param env The transformation environment
758 * @return the created ppc Not node
760 static ir_node *gen_Not(ppc32_transform_env_t *env)
762 return new_bd_ppc32_Not(env->dbg, env->block, get_Not_op(env->irn), env->mode);
766 static ir_node *own_gen_Andi_dot_lo16(ppc32_transform_env_t *env, ir_node *op, int mask)
768 ir_node *andi = new_bd_ppc32_Andi_dot(env->dbg, env->block, op, mode_T);
770 set_ppc32_offset_mode(andi, ppc32_ao_Lo16);
771 set_ppc32_constant_tarval(andi, new_tarval_from_long(mask, mode_Is));
772 in[0] = new_rd_Proj(env->dbg, andi, env->mode,1);
773 be_new_Keep(env->block, 1, in);
774 return new_rd_Proj(env->dbg, andi, env->mode,0);
778 * Transforms a Conv node.
780 * @param env The transformation environment
781 * @return the created ppc Conv node
783 static ir_node *gen_Conv(ppc32_transform_env_t *env)
785 ir_node *op = get_Conv_op(env->irn);
786 ppc32_modecode from_mode=get_nice_modecode(get_irn_mode(op));
787 ppc32_modecode to_mode=get_nice_modecode(env->mode);
789 #define SKIP return op
791 if (from_mode == to_mode) SKIP;
807 return new_bd_ppc32_fRsp(env->dbg, env->block, op, env->mode);
818 return new_bd_ppc32_Extsh(env->dbg, env->block, op, env->mode);
820 return own_gen_Andi_dot_lo16(env, op, 0xffff);
822 return new_bd_ppc32_Extsb(env->dbg, env->block, op, env->mode);
824 return own_gen_Andi_dot_lo16(env, op, 0xff);
838 if (from_mode==irm_Hu)
840 return own_gen_Andi_dot_lo16(env, op, 0xffff);
844 return new_bd_ppc32_Extsb(env->dbg, env->block, op, env->mode);
846 return own_gen_Andi_dot_lo16(env, op, 0xff);
848 return new_bd_ppc32_Extsh(env->dbg, env->block, op, env->mode);
860 if (from_mode==irm_Bs)
862 return own_gen_Andi_dot_lo16(env, op, 0xff);
867 return new_bd_ppc32_Extsb(env->dbg, env->block, op, env->mode);
873 if (to_mode==irm_Is || to_mode==irm_Iu) SKIP;
879 panic("Mode for Conv not supported: %F -> %F", get_irn_mode(op), env->mode);
884 * Transforms an Abs node.
886 * @param env The transformation environment
887 * @return the ppc node generating the absolute value
889 static ir_node *gen_Abs(ppc32_transform_env_t *env)
891 ir_node *op = get_Abs_op(env->irn);
895 switch (get_nice_modecode(env->mode))
899 return new_bd_ppc32_fAbs(env->dbg, env->block, op, env->mode);
905 n1 = new_bd_ppc32_Srawi(env->dbg, env->block, op, env->mode);
906 set_ppc32_constant_tarval(n1, new_tarval_from_long(shift, mode_Is));
907 set_ppc32_offset_mode(n1, ppc32_ao_None);
908 n2 = new_bd_ppc32_Add(env->dbg, env->block, op, n1, env->mode);
909 return new_bd_ppc32_Xor(env->dbg, env->block, n2, n1, env->mode);
913 panic("Mode for Abs not supported: %F", env->mode);
917 * Transforms an Cond node.
919 * @param env The transformation environment
920 * @return a ppc branch node
922 static ir_node *gen_Cond(ppc32_transform_env_t *env)
924 ir_node *selector = get_Cond_selector(env->irn);
925 ir_mode *projmode = get_irn_mode(selector);
926 if (is_Proj(selector) && projmode==get_ppc32_mode_Cond())
928 int projnum = get_Proj_proj(selector);
929 ir_node *branch = new_bd_ppc32_Branch(env->dbg, env->block, selector, env->mode);
930 set_ppc32_proj_nr(branch, projnum);
935 ir_node *unknown_gpr = new_bd_ppc32_Unknown(env->dbg, env->block, mode_Is);
936 ir_node *unknown_cond = new_bd_ppc32_cUnknown(env->dbg, env->block, get_ppc32_mode_Cond());
938 ir_node *switch_node = new_bd_ppc32_Switch(env->dbg, env->block, selector,
939 unknown_gpr, unknown_cond, env->mode);
940 set_ppc32_proj_nr(switch_node, get_Cond_default_proj(env->irn));
947 * Transforms an Unknown node.
949 * @param env The transformation environment
950 * @return a ppc Unknown node
952 static ir_node *gen_Unknown(ppc32_transform_env_t *env)
954 if (mode_is_float(env->mode))
955 return new_bd_ppc32_fUnknown(env->dbg, env->block, env->mode);
956 else if (mode_is_int(env->mode))
957 return new_bd_ppc32_Unknown(env->dbg, env->block, env->mode);
959 panic("Mode %F for unknown value not supported.", env->mode);
962 static ir_node *ldst_insert_const(ir_node *ptr, tarval **ptv, ident **pid, ppc32_transform_env_t *env)
964 tarval *tv_const = NULL;
965 ident *id_symconst = NULL;
967 if (is_ppc32_Const(ptr))
969 tv_const = get_ppc32_constant_tarval(ptr);
970 ptr = new_bd_ppc32_Addis_zero(env->dbg, env->block, mode_P, ppc32_ao_Ha16, tv_const, NULL);
972 else if (is_ppc32_SymConst(ptr))
975 ir_entity *ent = get_ppc32_frame_entity(ptr);
976 if (is_direct_entity(ent))
978 id_symconst = get_entity_ident(ent);
979 ptr = new_bd_ppc32_Addis_zero(env->dbg, env->block, mode_P, ppc32_ao_Ha16, NULL, id_symconst);
991 * @param env The transformation environment
992 * @return the created ppc Load node
994 static ir_node *gen_Load(ppc32_transform_env_t *env)
996 ir_node *node = env->irn;
997 ir_node *loadptr = get_Load_ptr(node);
999 ir_mode *mode = get_Load_mode(node);
1000 tarval *tv_const = NULL;
1001 ident *id_symconst = NULL;
1003 loadptr = ldst_insert_const(loadptr, &tv_const, &id_symconst, env);
1004 switch (get_nice_modecode(mode)){
1006 load = new_bd_ppc32_Lbz(env->dbg, env->block, loadptr, get_Load_mem(node));
1011 ir_node *proj_load, *extsb_node;
1012 load = new_bd_ppc32_Lbz(env->dbg, env->block, loadptr, get_Load_mem(node));
1013 proj_load = new_rd_Proj(env->dbg, load, mode, pn_Load_res);
1014 extsb_node = new_bd_ppc32_Extsb(env->dbg, env->block, proj_load, mode);
1015 exchange(get_succ_Proj(env->irn, pn_Load_res), extsb_node);
1021 load = new_bd_ppc32_Lhz(env->dbg, env->block, loadptr, get_Load_mem(node));
1024 load =new_bd_ppc32_Lha(env->dbg, env->block, loadptr, get_Load_mem(node));
1029 load = new_bd_ppc32_Lwz(env->dbg, env->block, loadptr, get_Load_mem(node));
1033 load = new_bd_ppc32_Lfd(env->dbg, env->block, loadptr, get_Load_mem(node));
1036 load = new_bd_ppc32_Lfs(env->dbg, env->block, loadptr, get_Load_mem(node));
1040 panic("Mode for Load not supported: %F", env->mode);
1045 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1046 set_ppc32_constant_tarval(load, tv_const);
1048 else if (id_symconst)
1050 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1051 set_ppc32_symconst_ident(load, id_symconst);
1059 * Transforms a Store.
1061 * @param env The transformation environment
1062 * @return the created ppc Store node
1064 static ir_node *gen_Store(ppc32_transform_env_t *env)
1066 ir_node *node = env->irn;
1067 ir_node *storeptr = get_Store_ptr(node);
1068 ir_node *valuenode = get_Store_value(node);
1069 ir_mode *mode = get_irn_mode(valuenode);
1071 tarval *tv_const = NULL;
1072 ident *id_symconst = NULL;
1074 storeptr = ldst_insert_const(storeptr, &tv_const, &id_symconst, env);
1076 switch (get_nice_modecode(mode)){
1079 store = new_bd_ppc32_Stb(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1084 store = new_bd_ppc32_Sth(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1089 store = new_bd_ppc32_Stw(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1093 store = new_bd_ppc32_Stfd(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1096 store = new_bd_ppc32_Stfs(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1100 panic("Mode for Store not supported: %F", env->mode);
1104 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1105 set_ppc32_constant_tarval(store, tv_const);
1107 else if (id_symconst)
1109 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1110 set_ppc32_symconst_ident(store, id_symconst);
1116 * Transforms a CopyB.
1118 * @param env The transformation environment
1119 * @return the created ppc CopyB node
1121 static ir_node *gen_CopyB(ppc32_transform_env_t *env)
1123 ir_node *mem = get_CopyB_mem(env->irn);
1124 ir_node *src = get_CopyB_src(env->irn);
1125 ir_node *dest = get_CopyB_dst(env->irn);
1126 ir_type *type = get_CopyB_type(env->irn);
1127 int size = get_type_size_bytes(type);
1130 ir_node *load, *store = NULL;
1135 tarval *offset0 = new_tarval_from_long(0, mode_Is);
1136 tarval *offset4 = new_tarval_from_long(4, mode_Is);
1138 load = new_bd_ppc32_Lwz(env->dbg, env->block, src, mem);
1139 set_ppc32_constant_tarval(load, offset0);
1140 set_ppc32_offset_mode(load, ppc32_ao_None);
1141 mem = new_rd_Proj(env->dbg, load, mode_M, pn_Load_M);
1142 res = new_rd_Proj(env->dbg, load, mode_Is, pn_Load_res);
1144 store = new_bd_ppc32_Stw(env->dbg, env->block, dest, res, mem);
1145 set_ppc32_constant_tarval(store, offset0);
1146 set_ppc32_offset_mode(store, ppc32_ao_None);
1147 mem = new_rd_Proj(env->dbg, store, mode_M, pn_Store_M);
1151 load = new_bd_ppc32_Lwz(env->dbg, env->block, src, mem);
1152 set_ppc32_constant_tarval(load, offset4);
1153 set_ppc32_offset_mode(load, ppc32_ao_None);
1154 mem = new_rd_Proj(env->dbg, load, mode_M, pn_Load_M);
1155 res = new_rd_Proj(env->dbg, load, mode_Is, pn_Load_res);
1157 store = new_bd_ppc32_Stw(env->dbg, env->block, dest, res, mem);
1158 set_ppc32_constant_tarval(store, offset4);
1159 set_ppc32_offset_mode(store, ppc32_ao_None);
1160 mem = new_rd_Proj(env->dbg, store, mode_M, pn_Store_M);
1166 ir_node *ornode, *mtctrnode;
1168 assert(size/4-1<=0xffff);
1169 if (size/4-1<0x8000)
1171 ornode = new_bd_ppc32_Addi_zero(env->dbg, env->block, mode_Is);
1172 set_ppc32_offset_mode(ornode, ppc32_ao_None);
1176 ir_node *zeroreg = new_bd_ppc32_Addi_zero(env->dbg, env->block, mode_Is);
1177 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1178 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1179 ornode = new_bd_ppc32_Ori(env->dbg, env->block, zeroreg, mode_Is);
1180 set_ppc32_offset_mode(ornode, ppc32_ao_Lo16);
1183 set_ppc32_constant_tarval(ornode, new_tarval_from_long(size/4-1, mode_Is));
1184 mtctrnode = new_bd_ppc32_Mtctr(env->dbg, env->block, ornode, mode_Is);
1185 store = new_bd_ppc32_LoopCopy(env->dbg, env->block, src, dest, mtctrnode, mem, mode_T);
1187 in[0] = new_rd_Proj(env->dbg, store, mode_Is, 1); // src
1188 in[1] = new_rd_Proj(env->dbg, store, mode_Is, 2); // dest
1189 in[2] = new_rd_Proj(env->dbg, store, mode_Is, 4); // temp
1190 be_new_Keep(env->block, 3, in);
1191 in[0] = new_rd_Proj(env->dbg, store, mode_Is, 3); // ctr
1192 be_new_Keep(env->block, 1, in);
1194 mem = new_rd_Proj(env->dbg, store, mode_M, 0);
1203 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1204 load = new_bd_ppc32_Lhz(env->dbg, env->block, src, mem);
1205 set_ppc32_constant_tarval(load, offset_tarval);
1206 set_ppc32_offset_mode(load, ppc32_ao_None);
1207 mem = new_rd_Proj(env->dbg, load, mode_M, pn_Load_M);
1208 res = new_rd_Proj(env->dbg, load, mode_Is, pn_Load_res);
1210 store = new_bd_ppc32_Sth(env->dbg, env->block, dest, res, mem);
1211 set_ppc32_constant_tarval(store, offset_tarval);
1212 set_ppc32_offset_mode(store, ppc32_ao_None);
1213 mem = new_rd_Proj(env->dbg, store, mode_M, pn_Store_M);
1221 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1222 load = new_bd_ppc32_Lbz(env->dbg, env->block, src, mem);
1223 set_ppc32_constant_tarval(load, offset_tarval);
1224 set_ppc32_offset_mode(load, ppc32_ao_None);
1225 mem = new_rd_Proj(env->dbg, load, mode_M, pn_Load_M);
1226 res = new_rd_Proj(env->dbg, load, mode_Is, pn_Load_res);
1228 store = new_bd_ppc32_Stb(env->dbg, env->block, dest, res, mem);
1229 set_ppc32_constant_tarval(store, offset_tarval);
1230 set_ppc32_offset_mode(store, ppc32_ao_None);
1231 // mem = new_rd_Proj(env->dbg, env->irg, store, mode_M, pn_Store_M);
1238 * Transforms a FrameAddr into a ppc Add.
1240 * @param env The transformation environment
1242 static ir_node *gen_be_FrameAddr(ppc32_transform_env_t *env)
1244 ir_node *op = get_irn_n(env->irn, 0);
1245 ir_node *add = new_bd_ppc32_Addi(env->dbg, env->block, op, mode_P);
1246 set_ppc32_frame_entity(add, be_get_frame_entity(env->irn));
1251 /*********************************************************
1254 * _ __ ___ __ _ _ _ __ __| |_ __ ___ _____ _ __
1255 * | '_ ` _ \ / _` | | '_ \ / _` | '__| \ \ / / _ \ '__|
1256 * | | | | | | (_| | | | | | | (_| | | | |\ V / __/ |
1257 * |_| |_| |_|\__,_|_|_| |_| \__,_|_| |_| \_/ \___|_|
1259 *********************************************************/
1262 * the BAD transformer.
1264 static ir_node *bad_transform(ppc32_transform_env_t *env)
1266 panic("Transformation not implemented: %+F\n", env->irn);
1270 * Enters all transform functions into the generic pointer
1272 void ppc32_register_transformers(void)
1274 /* first clear the generic function pointer for all ops */
1275 clear_irp_opcodes_generic_func();
1277 #define FIRM_OP(a) op_##a->ops.generic = (op_func)gen_##a
1278 #define BAD(a) op_##a->ops.generic = (op_func)bad_transform
1310 /* TODO: implement these nodes */
1313 /* You probably don't need to handle the following nodes */
1343 /* handle builtins */
1346 /* handle generic backend nodes */
1347 FIRM_OP(be_FrameAddr);
1350 typedef ir_node *(transform_func)(ppc32_transform_env_t *env);
1353 * Transforms the given firm node (and maybe some other related nodes)
1354 * into one or more assembler nodes.
1356 * @param node the firm node
1357 * @param env the debug module
1359 void ppc32_transform_node(ir_node *node, void *env)
1361 ppc32_code_gen_t *cg = (ppc32_code_gen_t *)env;
1362 ir_op *op = get_irn_op(node);
1363 ir_node *asm_node = NULL;
1369 DBG((cg->mod, LEVEL_1, "check %+F ... ", node));
1371 if (op->ops.generic) {
1372 ppc32_transform_env_t tenv;
1373 transform_func *transform = (transform_func *)op->ops.generic;
1375 tenv.block = get_nodes_block(node);
1376 tenv.dbg = get_irn_dbg_info(node);
1377 tenv.irg = current_ir_graph;
1379 tenv.mode = get_irn_mode(node);
1380 DEBUG_ONLY(tenv.mod = cg->mod;)
1382 asm_node = (*transform)(&tenv);
1386 exchange(node, asm_node);
1387 DB((cg->mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1390 DB((cg->mod, LEVEL_1, "ignored\n"));
1395 * Constant generating code
1403 /** Compares two (entity, tarval) combinations */
1404 static int cmp_tv_ent(const void *a, const void *b, size_t len)
1406 const struct tv_ent *e1 = a;
1407 const struct tv_ent *e2 = b;
1410 return !(e1->tv == e2->tv);
1413 /** Generates a SymConst node for a known FP const */
1414 static ir_node *gen_fp_known_symconst(ppc32_transform_env_t *env,
1415 tarval *known_const)
1417 static set *const_set = NULL;
1418 static ir_type *tp = NULL;
1420 struct tv_ent *entry;
1421 ir_node *cnst,*symcnst;
1423 ir_entity *ent = NULL;
1426 const_set = new_set(cmp_tv_ent, 10);
1428 tp = new_type_primitive(env->mode);
1430 key.tv = known_const;
1433 entry = set_insert(const_set, &key, sizeof(key), HASH_PTR(key.tv));
1437 sprintf(buf, "const_%ld", get_irn_node_nr(env->irn));
1438 ent = new_entity(get_glob_type(), new_id_from_str(buf), tp);
1440 set_entity_ld_ident(ent, get_entity_ident(ent));
1441 set_entity_visibility(ent, ir_visibility_local);
1442 add_entity_linkage(ent, IR_LINKAGE_CONSTANT);
1444 /* we create a new entity here: It's initialization must resist on the
1446 rem = current_ir_graph;
1447 current_ir_graph = get_const_code_irg();
1448 cnst = new_Const(key.tv);
1449 current_ir_graph = rem;
1451 set_atomic_ent_value(ent, cnst);
1453 /* set the entry for hashmap */
1455 } // TODO: Wird nicht richtig in global type gesteckt, ppc32_gen_decls.c findet ihn nicht
1457 symcnst = new_bd_ppc32_SymConst(env->dbg, env->block, env->mode);
1458 set_ppc32_frame_entity(symcnst, ent);
1462 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env);
1465 * Transforms a Const.
1467 * @param mod the debug module
1468 * @param block the block the new node should belong to
1469 * @param node the ir Const node
1470 * @param mode node mode
1471 * @return the created ppc Load immediate node
1473 static ir_node *gen_ppc32_Const(ppc32_transform_env_t *env)
1475 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1478 switch (get_nice_modecode(env->mode)){
1481 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1484 ir_node *zeroreg = new_bd_ppc32_Addi_zero(env->dbg, env->block, mode_Is);
1485 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1486 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1487 node = new_bd_ppc32_Ori(env->dbg, env->block, zeroreg, mode_Is);
1488 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1495 node = new_bd_ppc32_Addi_zero(env->dbg, env->block, env->mode);
1496 set_ppc32_offset_mode(node, ppc32_ao_None);
1502 unsigned char val2 = get_tarval_sub_bits(tv_const,2);
1503 unsigned char val3 = get_tarval_sub_bits(tv_const,3);
1506 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1509 ir_node *zeroreg = new_bd_ppc32_Addi_zero(env->dbg, env->block, mode_Is);
1510 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1511 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1512 node = new_bd_ppc32_Ori(env->dbg, env->block, zeroreg, mode_Is);
1513 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1517 node = new_bd_ppc32_Addi_zero(env->dbg, env->block, env->mode);
1518 set_ppc32_offset_mode(node, ppc32_ao_None);
1523 unsigned char val0 = get_tarval_sub_bits(tv_const,0);
1524 unsigned char val1 = get_tarval_sub_bits(tv_const,1);
1525 node = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Hi16, tv_const, NULL);
1528 set_ppc32_constant_tarval(node, tv_const);
1529 node = new_bd_ppc32_Ori(env->dbg, env->block, node, env->mode);
1530 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1537 panic("Mode for Const not supported: %F", env->mode);
1539 set_ppc32_constant_tarval(node, tv_const);
1544 * Transforms a fConst.
1546 * @param mod the debug module
1547 * @param block the block the new node should belong to
1548 * @param node the ir Const node
1549 * @param mode node mode
1550 * @return the created ppc float Load node
1552 static ir_node *gen_ppc32_fConst(ppc32_transform_env_t *env)
1554 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1556 switch (get_nice_modecode(env->mode)){
1560 ir_node *addr, *load;
1561 ir_mode *mode = env->mode;
1563 env->irn = gen_fp_known_symconst(env, tv_const);
1565 ent = get_ppc32_frame_entity(env->irn);
1567 if (is_direct_entity(ent))
1569 ident *id_symconst = get_entity_ident(ent);
1570 ir_node *node_addis = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1573 load = new_bd_ppc32_Lfd(env->dbg, env->block, node_addis, new_NoMem());
1575 load = new_bd_ppc32_Lfs(env->dbg, env->block, node_addis, new_NoMem());
1577 set_ppc32_symconst_ident(load, id_symconst);
1578 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1583 addr = gen_ppc32_SymConst (env);
1585 load = new_bd_ppc32_Lfd(env->dbg, env->block, addr, new_NoMem());
1587 load = new_bd_ppc32_Lfs(env->dbg, env->block, addr, new_NoMem());
1591 return new_rd_Proj(env->dbg, load, mode, pn_Load_res);
1595 panic("Mode for fConst not supported: %F", env->mode);
1600 * Transforms a SymConst.
1602 * @param mod the debug module
1603 * @param block the block the new node should belong to
1604 * @param node the ir Const node
1605 * @param mode node mode
1606 * @return the created ppc Load immediate node
1608 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env)
1610 ir_entity *ent = get_ppc32_frame_entity(env->irn);
1611 ident *id_symconst = get_entity_ident(ent);
1613 switch (get_nice_modecode(env->mode)){
1617 if (is_direct_entity(ent))
1620 ir_node *node_addis = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Hi16, NULL, id_symconst);
1621 node = new_bd_ppc32_Ori(env->dbg, env->block, node_addis, env->mode);
1622 set_ppc32_symconst_ident(node, id_symconst);
1623 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1628 ir_node *node_addis = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1629 node = new_bd_ppc32_Lwz(env->dbg, env->block, node_addis, new_NoMem());
1630 set_ppc32_symconst_ident(node, id_symconst);
1631 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1632 node = new_rd_Proj(env->dbg, node, env->mode, pn_Load_res);
1639 panic("Mode for SymConst not supported: %F", env->mode);
1645 * Transforms the given firm node (and maybe some other related nodes)
1646 * into one or more assembler nodes.
1648 * @param node the firm node
1649 * @param env the debug module
1651 void ppc32_transform_const(ir_node *node, void *env)
1653 ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
1654 ir_node *asm_node = NULL;
1655 ppc32_transform_env_t tenv;
1661 tenv.block = get_nodes_block(node);
1662 tenv.dbg = get_irn_dbg_info(node);
1663 tenv.irg = current_ir_graph;
1665 DEBUG_ONLY(tenv.mod = cgenv->mod;)
1666 tenv.mode = get_irn_mode(node);
1668 #define OTHER_GEN(a) \
1669 if (get_irn_op(node) == get_op_##a()) { \
1670 asm_node = gen_##a(&tenv); \
1673 DBG((tenv.mod, LEVEL_1, "check %+F ... ", node));
1675 OTHER_GEN(ppc32_Const)
1676 else OTHER_GEN(ppc32_fConst)
1677 else OTHER_GEN(ppc32_SymConst)
1680 exchange(node, asm_node);
1681 DB((tenv.mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1684 DB((tenv.mod, LEVEL_1, "ignored\n"));