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 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 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 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, env->block, 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 fprintf(stderr, "Mode for Mod not supported: %s\n", get_mode_name(res_mode));
572 proj_div = new_rd_Proj(env->dbg, env->block, div_result, res_mode, pn_DivMod_res_div);
574 mul_result = new_bd_ppc32_Mullw(env->dbg, env->block, proj_div, op2, res_mode);
575 mod_result = new_bd_ppc32_Sub(env->dbg, env->block, op1, mul_result, res_mode);
577 exchange(proj_mod, mod_result);
583 * Creates an ppc Shl.
585 * @param env The transformation environment
586 * @return the created ppc Shl node
588 static ir_node *gen_Shl(ppc32_transform_env_t *env)
590 ir_node *op1 = get_Shl_left(env->irn);
591 ir_node *op2 = get_Shl_right(env->irn);
593 if (is_ppc32_Const(op2))
595 ir_node *shift = new_bd_ppc32_Rlwinm(env->dbg, env->block, op1, env->mode);
596 tarval *tv_const = get_ppc32_constant_tarval(op2);
597 int sh = get_tarval_long(tv_const);
598 assert(0<=sh && sh<=31);
599 set_ppc32_rlwimi_const(shift, sh, 0, 31-sh);
602 return new_bd_ppc32_Slw(env->dbg, env->block, op1, op2, env->mode);
606 * Creates an ppc Srw.
608 * @param env The transformation environment
609 * @return the created ppc Shr node
611 static ir_node *gen_Shr(ppc32_transform_env_t *env)
613 ir_node *op1 = get_Shr_left(env->irn);
614 ir_node *op2 = get_Shr_right(env->irn);
616 if (is_ppc32_Const(op2))
618 ir_node *shift = new_bd_ppc32_Rlwinm(env->dbg, env->block, op1, env->mode);
619 tarval *tv_const = get_ppc32_constant_tarval(op2);
620 int sh = get_tarval_long(tv_const);
621 assert(0<=sh && sh<=31);
622 set_ppc32_rlwimi_const(shift, 32-sh, sh, 31);
625 return new_bd_ppc32_Srw(env->dbg, env->block, op1, op2, env->mode);
629 * Creates an ppc Sraw.
631 * @param env The transformation environment
632 * @return the created ppc Sraw node
634 static ir_node *gen_Shrs(ppc32_transform_env_t *env)
636 ir_node *op1 = get_Shrs_left(env->irn);
637 ir_node *op2 = get_Shrs_right(env->irn);
639 if (is_ppc32_Const(op2))
641 ir_node *shift = new_bd_ppc32_Srawi(env->dbg, env->block, op1, env->mode);
642 tarval *tv_const = get_ppc32_constant_tarval(op2);
643 int sh = get_tarval_long(tv_const);
644 assert(0<=sh && sh<=31);
646 set_ppc32_constant_tarval(shift, tv_const);
647 set_ppc32_offset_mode(shift, ppc32_ao_None);
650 return new_bd_ppc32_Sraw(env->dbg, env->block, op1, op2, env->mode);
654 * Creates an ppc Rotl.
656 * @param env The transformation environment
657 * @return the created ppc Rotl node
659 static ir_node *gen_Rotl(ppc32_transform_env_t *env)
661 ir_node *op1 = get_Rotl_left(env->irn);
662 ir_node *op2 = get_Rotl_right(env->irn);
664 if (is_ppc32_Const(op2))
666 ir_node *rot = new_bd_ppc32_Rlwinm(env->dbg, env->block, op1, env->mode);
667 tarval *tv_const = get_ppc32_constant_tarval(op2);
668 int sh = get_tarval_long(tv_const);
669 assert(0<=sh && sh<=31);
670 set_ppc32_rlwimi_const(rot, sh, 0, 31);
673 return new_bd_ppc32_Rlwnm(env->dbg, env->block, op1, op2, env->mode);
677 * Creates an ppc Cmp.
679 * @param env The transformation environment
680 * @return the created ppc Cmp node
682 static ir_node *gen_Cmp(ppc32_transform_env_t *env)
684 ir_node *op1 = get_Cmp_left(env->irn);
685 ir_node *op2 = get_Cmp_right(env->irn);
687 const ir_edge_t *edge;
688 foreach_out_edge(env->irn, edge)
690 if (is_Proj(edge->src))
691 set_irn_mode(edge->src, get_ppc32_mode_Cond());
694 if (mode_is_float(env->mode))
695 return new_bd_ppc32_fCmpu(env->dbg, env->block, op1, op2, env->mode);
696 else if (mode_is_signed(env->mode))
698 if (is_16bit_signed_const(op2))
700 ir_node *cmp = new_bd_ppc32_Cmpi(env->dbg, env->block, op1, env->mode);
701 tarval *tv_const = get_ppc32_constant_tarval(op2);
702 set_ppc32_constant_tarval(cmp, tv_const);
703 set_ppc32_offset_mode(cmp, ppc32_ao_None);
708 return new_bd_ppc32_Cmp(env->dbg, env->block, op1, op2, env->mode);
713 if (is_16bit_unsigned_const(op2))
715 ir_node *cmp = new_bd_ppc32_Cmpli(env->dbg, env->block, op1, env->mode);
716 tarval *tv_const = get_ppc32_constant_tarval(op2);
717 set_ppc32_constant_tarval(cmp, tv_const);
718 set_ppc32_offset_mode(cmp, ppc32_ao_None);
724 return new_bd_ppc32_Cmpl(env->dbg, env->block, op1, op2, env->mode);
730 * Transforms a Minus node.
732 * @param env The transformation environment
733 * @return the created ppc Minus node
735 static ir_node *gen_Minus(ppc32_transform_env_t *env)
737 ir_node *op = get_Minus_op(env->irn);
739 switch (get_nice_modecode(env->mode)){
742 return new_bd_ppc32_fNeg(env->dbg, env->block, op, env->mode);
750 return new_bd_ppc32_Neg(env->dbg, env->block, op, env->mode);
753 panic("Mode for Neg not supported: %F", env->mode);
758 * Transforms a Not node.
760 * @param env The transformation environment
761 * @return the created ppc Not node
763 static ir_node *gen_Not(ppc32_transform_env_t *env)
765 return new_bd_ppc32_Not(env->dbg, env->block, get_Not_op(env->irn), env->mode);
769 static ir_node *own_gen_Andi_dot_lo16(ppc32_transform_env_t *env, ir_node *op, int mask)
771 ir_node *andi = new_bd_ppc32_Andi_dot(env->dbg, env->block, op, mode_T);
773 set_ppc32_offset_mode(andi, ppc32_ao_Lo16);
774 set_ppc32_constant_tarval(andi, new_tarval_from_long(mask, mode_Is));
775 in[0] = new_rd_Proj(env->dbg, env->block, andi, env->mode,1);
776 be_new_Keep(env->block, 1, in);
777 return new_rd_Proj(env->dbg, env->block, andi, env->mode,0);
781 * Transforms a Conv node.
783 * @param env The transformation environment
784 * @return the created ppc Conv node
786 static ir_node *gen_Conv(ppc32_transform_env_t *env)
788 ir_node *op = get_Conv_op(env->irn);
789 ppc32_modecode from_mode=get_nice_modecode(get_irn_mode(op));
790 ppc32_modecode to_mode=get_nice_modecode(env->mode);
792 #define SKIP return op
794 if (from_mode == to_mode) SKIP;
810 return new_bd_ppc32_fRsp(env->dbg, env->block, op, env->mode);
821 return new_bd_ppc32_Extsh(env->dbg, env->block, op, env->mode);
823 return own_gen_Andi_dot_lo16(env, op, 0xffff);
825 return new_bd_ppc32_Extsb(env->dbg, env->block, op, env->mode);
827 return own_gen_Andi_dot_lo16(env, op, 0xff);
841 if (from_mode==irm_Hu)
843 return own_gen_Andi_dot_lo16(env, op, 0xffff);
847 return new_bd_ppc32_Extsb(env->dbg, env->block, op, env->mode);
849 return own_gen_Andi_dot_lo16(env, op, 0xff);
851 return new_bd_ppc32_Extsh(env->dbg, env->block, op, env->mode);
863 if (from_mode==irm_Bs)
865 return own_gen_Andi_dot_lo16(env, op, 0xff);
870 return new_bd_ppc32_Extsb(env->dbg, env->block, op, env->mode);
876 if (to_mode==irm_Is || to_mode==irm_Iu) SKIP;
882 panic("Mode for Conv not supported: %F -> %F", get_irn_mode(op), env->mode);
887 * Transforms an Abs node.
889 * @param env The transformation environment
890 * @return the ppc node generating the absolute value
892 static ir_node *gen_Abs(ppc32_transform_env_t *env)
894 ir_node *op = get_Abs_op(env->irn);
898 switch (get_nice_modecode(env->mode))
902 return new_bd_ppc32_fAbs(env->dbg, env->block, op, env->mode);
908 n1 = new_bd_ppc32_Srawi(env->dbg, env->block, op, env->mode);
909 set_ppc32_constant_tarval(n1, new_tarval_from_long(shift, mode_Is));
910 set_ppc32_offset_mode(n1, ppc32_ao_None);
911 n2 = new_bd_ppc32_Add(env->dbg, env->block, op, n1, env->mode);
912 return new_bd_ppc32_Xor(env->dbg, env->block, n2, n1, env->mode);
916 panic("Mode for Abs not supported: %F", env->mode);
921 * Transforms an Cond node.
923 * @param env The transformation environment
924 * @return a ppc branch node
926 static ir_node *gen_Cond(ppc32_transform_env_t *env)
928 ir_node *selector = get_Cond_selector(env->irn);
929 ir_mode *projmode = get_irn_mode(selector);
930 if (is_Proj(selector) && projmode==get_ppc32_mode_Cond())
932 int projnum = get_Proj_proj(selector);
933 ir_node *branch = new_bd_ppc32_Branch(env->dbg, env->block, selector, env->mode);
934 set_ppc32_proj_nr(branch, projnum);
939 ir_node *unknown_gpr = new_bd_ppc32_Unknown(env->dbg, env->block, mode_Is);
940 ir_node *unknown_cond = new_bd_ppc32_cUnknown(env->dbg, env->block, get_ppc32_mode_Cond());
942 ir_node *switch_node = new_bd_ppc32_Switch(env->dbg, env->block, selector,
943 unknown_gpr, unknown_cond, env->mode);
944 set_ppc32_proj_nr(switch_node, get_Cond_default_proj(env->irn));
951 * Transforms an Unknown node.
953 * @param env The transformation environment
954 * @return a ppc Unknown node
956 static ir_node *gen_Unknown(ppc32_transform_env_t *env)
958 if (mode_is_float(env->mode))
959 return new_bd_ppc32_fUnknown(env->dbg, env->block, env->mode);
960 else if (mode_is_int(env->mode))
961 return new_bd_ppc32_Unknown(env->dbg, env->block, env->mode);
963 panic("Mode %F for unknown value not supported.", env->mode);
966 static ir_node *ldst_insert_const(ir_node *ptr, tarval **ptv, ident **pid, ppc32_transform_env_t *env)
968 tarval *tv_const = NULL;
969 ident *id_symconst = NULL;
971 if (is_ppc32_Const(ptr))
973 tv_const = get_ppc32_constant_tarval(ptr);
974 ptr = new_bd_ppc32_Addis_zero(env->dbg, env->block, mode_P, ppc32_ao_Ha16, tv_const, NULL);
976 else if (is_ppc32_SymConst(ptr))
979 ir_entity *ent = get_ppc32_frame_entity(ptr);
980 if (is_direct_entity(ent))
982 id_symconst = get_entity_ident(ent);
983 ptr = new_bd_ppc32_Addis_zero(env->dbg, env->block, mode_P, ppc32_ao_Ha16, NULL, id_symconst);
995 * @param env The transformation environment
996 * @return the created ppc Load node
998 static ir_node *gen_Load(ppc32_transform_env_t *env)
1000 ir_node *node = env->irn;
1001 ir_node *loadptr = get_Load_ptr(node);
1003 ir_mode *mode = get_Load_mode(node);
1004 tarval *tv_const = NULL;
1005 ident *id_symconst = NULL;
1007 loadptr = ldst_insert_const(loadptr, &tv_const, &id_symconst, env);
1008 switch (get_nice_modecode(mode)){
1010 load = new_bd_ppc32_Lbz(env->dbg, env->block, loadptr, get_Load_mem(node));
1015 ir_node *proj_load, *extsb_node;
1016 load = new_bd_ppc32_Lbz(env->dbg, env->block, loadptr, get_Load_mem(node));
1017 proj_load = new_rd_Proj(env->dbg, env->block, load, mode, pn_Load_res);
1018 extsb_node = new_bd_ppc32_Extsb(env->dbg, env->block, proj_load, mode);
1019 exchange(get_succ_Proj(env->irn, pn_Load_res), extsb_node);
1025 load = new_bd_ppc32_Lhz(env->dbg, env->block, loadptr, get_Load_mem(node));
1028 load =new_bd_ppc32_Lha(env->dbg, env->block, loadptr, get_Load_mem(node));
1033 load = new_bd_ppc32_Lwz(env->dbg, env->block, loadptr, get_Load_mem(node));
1037 load = new_bd_ppc32_Lfd(env->dbg, env->block, loadptr, get_Load_mem(node));
1040 load = new_bd_ppc32_Lfs(env->dbg, env->block, loadptr, get_Load_mem(node));
1044 panic("Mode for Load not supported: %F", env->mode);
1049 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1050 set_ppc32_constant_tarval(load, tv_const);
1052 else if (id_symconst)
1054 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1055 set_ppc32_symconst_ident(load, id_symconst);
1063 * Transforms a Store.
1065 * @param env The transformation environment
1066 * @return the created ppc Store node
1068 static ir_node *gen_Store(ppc32_transform_env_t *env)
1070 ir_node *node = env->irn;
1071 ir_node *storeptr = get_Store_ptr(node);
1072 ir_node *valuenode = get_Store_value(node);
1073 ir_mode *mode = get_irn_mode(valuenode);
1075 tarval *tv_const = NULL;
1076 ident *id_symconst = NULL;
1078 storeptr = ldst_insert_const(storeptr, &tv_const, &id_symconst, env);
1080 switch (get_nice_modecode(mode)){
1083 store = new_bd_ppc32_Stb(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1088 store = new_bd_ppc32_Sth(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1093 store = new_bd_ppc32_Stw(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1097 store = new_bd_ppc32_Stfd(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1100 store = new_bd_ppc32_Stfs(env->dbg, env->block, storeptr, get_Store_value(node), get_Store_mem(node));
1104 panic("Mode for Store not supported: %F", env->mode);
1108 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1109 set_ppc32_constant_tarval(store, tv_const);
1111 else if (id_symconst)
1113 set_ppc32_offset_mode(store, ppc32_ao_Lo16);
1114 set_ppc32_symconst_ident(store, id_symconst);
1120 * Transforms a CopyB.
1122 * @param env The transformation environment
1123 * @return the created ppc CopyB node
1125 static ir_node *gen_CopyB(ppc32_transform_env_t *env)
1127 ir_node *mem = get_CopyB_mem(env->irn);
1128 ir_node *src = get_CopyB_src(env->irn);
1129 ir_node *dest = get_CopyB_dst(env->irn);
1130 ir_type *type = get_CopyB_type(env->irn);
1131 int size = get_type_size_bytes(type);
1134 ir_node *load, *store = NULL;
1139 tarval *offset0 = new_tarval_from_long(0, mode_Is);
1140 tarval *offset4 = new_tarval_from_long(4, mode_Is);
1142 load = new_bd_ppc32_Lwz(env->dbg, env->block, src, mem);
1143 set_ppc32_constant_tarval(load, offset0);
1144 set_ppc32_offset_mode(load, ppc32_ao_None);
1145 mem = new_rd_Proj(env->dbg, env->block, load, mode_M, pn_Load_M);
1146 res = new_rd_Proj(env->dbg, env->block, load, mode_Is, pn_Load_res);
1148 store = new_bd_ppc32_Stw(env->dbg, env->block, dest, res, mem);
1149 set_ppc32_constant_tarval(store, offset0);
1150 set_ppc32_offset_mode(store, ppc32_ao_None);
1151 mem = new_rd_Proj(env->dbg, env->block, store, mode_M, pn_Store_M);
1155 load = new_bd_ppc32_Lwz(env->dbg, env->block, src, mem);
1156 set_ppc32_constant_tarval(load, offset4);
1157 set_ppc32_offset_mode(load, ppc32_ao_None);
1158 mem = new_rd_Proj(env->dbg, env->block, load, mode_M, pn_Load_M);
1159 res = new_rd_Proj(env->dbg, env->block, load, mode_Is, pn_Load_res);
1161 store = new_bd_ppc32_Stw(env->dbg, env->block, dest, res, mem);
1162 set_ppc32_constant_tarval(store, offset4);
1163 set_ppc32_offset_mode(store, ppc32_ao_None);
1164 mem = new_rd_Proj(env->dbg, env->block, store, mode_M, pn_Store_M);
1170 ir_node *ornode, *mtctrnode;
1172 assert(size/4-1<=0xffff);
1173 if (size/4-1<0x8000)
1175 ornode = new_bd_ppc32_Addi_zero(env->dbg, env->block, mode_Is);
1176 set_ppc32_offset_mode(ornode, ppc32_ao_None);
1180 ir_node *zeroreg = new_bd_ppc32_Addi_zero(env->dbg, env->block, mode_Is);
1181 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1182 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1183 ornode = new_bd_ppc32_Ori(env->dbg, env->block, zeroreg, mode_Is);
1184 set_ppc32_offset_mode(ornode, ppc32_ao_Lo16);
1187 set_ppc32_constant_tarval(ornode, new_tarval_from_long(size/4-1, mode_Is));
1188 mtctrnode = new_bd_ppc32_Mtctr(env->dbg, env->block, ornode, mode_Is);
1189 store = new_bd_ppc32_LoopCopy(env->dbg, env->block, src, dest, mtctrnode, mem, mode_T);
1191 in[0] = new_rd_Proj(env->dbg, env->block, store, mode_Is, 1); // src
1192 in[1] = new_rd_Proj(env->dbg, env->block, store, mode_Is, 2); // dest
1193 in[2] = new_rd_Proj(env->dbg, env->block, store, mode_Is, 4); // temp
1194 be_new_Keep(env->block, 3, in);
1195 in[0] = new_rd_Proj(env->dbg, env->block, store, mode_Is, 3); // ctr
1196 be_new_Keep(env->block, 1, in);
1198 mem = new_rd_Proj(env->dbg, env->block, store, mode_M, 0);
1207 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1208 load = new_bd_ppc32_Lhz(env->dbg, env->block, src, mem);
1209 set_ppc32_constant_tarval(load, offset_tarval);
1210 set_ppc32_offset_mode(load, ppc32_ao_None);
1211 mem = new_rd_Proj(env->dbg, env->block, load, mode_M, pn_Load_M);
1212 res = new_rd_Proj(env->dbg, env->block, load, mode_Is, pn_Load_res);
1214 store = new_bd_ppc32_Sth(env->dbg, env->block, dest, res, mem);
1215 set_ppc32_constant_tarval(store, offset_tarval);
1216 set_ppc32_offset_mode(store, ppc32_ao_None);
1217 mem = new_rd_Proj(env->dbg, env->block, store, mode_M, pn_Store_M);
1225 tarval* offset_tarval = new_tarval_from_long(offset, mode_Is);
1226 load = new_bd_ppc32_Lbz(env->dbg, env->block, src, mem);
1227 set_ppc32_constant_tarval(load, offset_tarval);
1228 set_ppc32_offset_mode(load, ppc32_ao_None);
1229 mem = new_rd_Proj(env->dbg, env->block, load, mode_M, pn_Load_M);
1230 res = new_rd_Proj(env->dbg, env->block, load, mode_Is, pn_Load_res);
1232 store = new_bd_ppc32_Stb(env->dbg, env->block, dest, res, mem);
1233 set_ppc32_constant_tarval(store, offset_tarval);
1234 set_ppc32_offset_mode(store, ppc32_ao_None);
1235 // mem = new_rd_Proj(env->dbg, env->irg, env->block, store, mode_M, pn_Store_M);
1242 * Transforms a FrameAddr into a ppc Add.
1244 * @param env The transformation environment
1246 static ir_node *gen_be_FrameAddr(ppc32_transform_env_t *env)
1248 ir_node *op = get_irn_n(env->irn, 0);
1249 ir_node *add = new_bd_ppc32_Addi(env->dbg, env->block, op, mode_P);
1250 set_ppc32_frame_entity(add, be_get_frame_entity(env->irn));
1255 /*********************************************************
1258 * _ __ ___ __ _ _ _ __ __| |_ __ ___ _____ _ __
1259 * | '_ ` _ \ / _` | | '_ \ / _` | '__| \ \ / / _ \ '__|
1260 * | | | | | | (_| | | | | | | (_| | | | |\ V / __/ |
1261 * |_| |_| |_|\__,_|_|_| |_| \__,_|_| |_| \_/ \___|_|
1263 *********************************************************/
1266 * the BAD transformer.
1268 static ir_node *bad_transform(ppc32_transform_env_t *env)
1270 panic("Transformation not implemented: %+F\n", env->irn);
1274 * Enters all transform functions into the generic pointer
1276 void ppc32_register_transformers(void)
1278 /* first clear the generic function pointer for all ops */
1279 clear_irp_opcodes_generic_func();
1281 #define FIRM_OP(a) op_##a->ops.generic = (op_func)gen_##a
1282 #define BAD(a) op_##a->ops.generic = (op_func)bad_transform
1314 /* TODO: implement these nodes */
1317 /* You probably don't need to handle the following nodes */
1347 /* handle builtins */
1350 /* handle generic backend nodes */
1351 FIRM_OP(be_FrameAddr);
1354 typedef ir_node *(transform_func)(ppc32_transform_env_t *env);
1357 * Transforms the given firm node (and maybe some other related nodes)
1358 * into one or more assembler nodes.
1360 * @param node the firm node
1361 * @param env the debug module
1363 void ppc32_transform_node(ir_node *node, void *env)
1365 ppc32_code_gen_t *cg = (ppc32_code_gen_t *)env;
1366 ir_op *op = get_irn_op(node);
1367 ir_node *asm_node = NULL;
1373 DBG((cg->mod, LEVEL_1, "check %+F ... ", node));
1375 if (op->ops.generic) {
1376 ppc32_transform_env_t tenv;
1377 transform_func *transform = (transform_func *)op->ops.generic;
1379 tenv.block = get_nodes_block(node);
1380 tenv.dbg = get_irn_dbg_info(node);
1381 tenv.irg = current_ir_graph;
1383 tenv.mode = get_irn_mode(node);
1384 DEBUG_ONLY(tenv.mod = cg->mod;)
1386 asm_node = (*transform)(&tenv);
1390 exchange(node, asm_node);
1391 DB((cg->mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1394 DB((cg->mod, LEVEL_1, "ignored\n"));
1399 * Constant generating code
1407 /** Compares two (entity, tarval) combinations */
1408 static int cmp_tv_ent(const void *a, const void *b, size_t len)
1410 const struct tv_ent *e1 = a;
1411 const struct tv_ent *e2 = b;
1414 return !(e1->tv == e2->tv);
1417 /** Generates a SymConst node for a known FP const */
1418 static ir_node *gen_fp_known_symconst(ppc32_transform_env_t *env,
1419 tarval *known_const)
1421 static set *const_set = NULL;
1422 static ir_type *tp = NULL;
1424 struct tv_ent *entry;
1425 ir_node *cnst,*symcnst;
1427 ir_entity *ent = NULL;
1430 const_set = new_set(cmp_tv_ent, 10);
1432 tp = new_type_primitive(env->mode);
1434 key.tv = known_const;
1437 entry = set_insert(const_set, &key, sizeof(key), HASH_PTR(key.tv));
1441 sprintf(buf, "const_%ld", get_irn_node_nr(env->irn));
1442 ent = new_entity(get_glob_type(), new_id_from_str(buf), tp);
1444 set_entity_ld_ident(ent, get_entity_ident(ent));
1445 set_entity_visibility(ent, ir_visibility_local);
1446 add_entity_linkage(ent, IR_LINKAGE_CONSTANT);
1448 /* we create a new entity here: It's initialization must resist on the
1450 rem = current_ir_graph;
1451 current_ir_graph = get_const_code_irg();
1452 cnst = new_Const(key.tv);
1453 current_ir_graph = rem;
1455 set_atomic_ent_value(ent, cnst);
1457 /* set the entry for hashmap */
1459 } // TODO: Wird nicht richtig in global type gesteckt, ppc32_gen_decls.c findet ihn nicht
1461 symcnst = new_bd_ppc32_SymConst(env->dbg, env->block, env->mode);
1462 set_ppc32_frame_entity(symcnst, ent);
1466 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env);
1469 * Transforms a Const.
1471 * @param mod the debug module
1472 * @param block the block the new node should belong to
1473 * @param node the ir Const node
1474 * @param mode node mode
1475 * @return the created ppc Load immediate node
1477 static ir_node *gen_ppc32_Const(ppc32_transform_env_t *env)
1479 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1482 switch (get_nice_modecode(env->mode)){
1485 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1488 ir_node *zeroreg = new_bd_ppc32_Addi_zero(env->dbg, 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_bd_ppc32_Ori(env->dbg, env->block, zeroreg, mode_Is);
1492 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1499 node = new_bd_ppc32_Addi_zero(env->dbg, env->block, env->mode);
1500 set_ppc32_offset_mode(node, ppc32_ao_None);
1506 unsigned char val2 = get_tarval_sub_bits(tv_const,2);
1507 unsigned char val3 = get_tarval_sub_bits(tv_const,3);
1510 unsigned char val1 = get_tarval_sub_bits(tv_const, 1);
1513 ir_node *zeroreg = new_bd_ppc32_Addi_zero(env->dbg, env->block, mode_Is);
1514 set_ppc32_constant_tarval(zeroreg, new_tarval_from_long(0, mode_Is));
1515 set_ppc32_offset_mode(zeroreg, ppc32_ao_None);
1516 node = new_bd_ppc32_Ori(env->dbg, env->block, zeroreg, mode_Is);
1517 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1521 node = new_bd_ppc32_Addi_zero(env->dbg, env->block, env->mode);
1522 set_ppc32_offset_mode(node, ppc32_ao_None);
1527 unsigned char val0 = get_tarval_sub_bits(tv_const,0);
1528 unsigned char val1 = get_tarval_sub_bits(tv_const,1);
1529 node = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Hi16, tv_const, NULL);
1532 set_ppc32_constant_tarval(node, tv_const);
1533 node = new_bd_ppc32_Ori(env->dbg, env->block, node, env->mode);
1534 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1541 panic("Mode for Const not supported: %F", env->mode);
1543 set_ppc32_constant_tarval(node, tv_const);
1548 * Transforms a fConst.
1550 * @param mod the debug module
1551 * @param block the block the new node should belong to
1552 * @param node the ir Const node
1553 * @param mode node mode
1554 * @return the created ppc float Load node
1556 static ir_node *gen_ppc32_fConst(ppc32_transform_env_t *env)
1558 tarval *tv_const = get_ppc32_constant_tarval(env->irn);
1560 switch (get_nice_modecode(env->mode)){
1564 ir_node *addr, *load;
1565 ir_mode *mode = env->mode;
1567 env->irn = gen_fp_known_symconst(env, tv_const);
1569 ent = get_ppc32_frame_entity(env->irn);
1571 if (is_direct_entity(ent))
1573 ident *id_symconst = get_entity_ident(ent);
1574 ir_node *node_addis = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1577 load = new_bd_ppc32_Lfd(env->dbg, env->block, node_addis, new_NoMem());
1579 load = new_bd_ppc32_Lfs(env->dbg, env->block, node_addis, new_NoMem());
1581 set_ppc32_symconst_ident(load, id_symconst);
1582 set_ppc32_offset_mode(load, ppc32_ao_Lo16);
1587 addr = gen_ppc32_SymConst (env);
1589 load = new_bd_ppc32_Lfd(env->dbg, env->block, addr, new_NoMem());
1591 load = new_bd_ppc32_Lfs(env->dbg, env->block, addr, new_NoMem());
1595 return new_rd_Proj(env->dbg, env->block, load, mode, pn_Load_res);
1599 panic("Mode for fConst not supported: %F", env->mode);
1604 * Transforms a SymConst.
1606 * @param mod the debug module
1607 * @param block the block the new node should belong to
1608 * @param node the ir Const node
1609 * @param mode node mode
1610 * @return the created ppc Load immediate node
1612 static ir_node *gen_ppc32_SymConst(ppc32_transform_env_t *env)
1614 ir_entity *ent = get_ppc32_frame_entity(env->irn);
1615 ident *id_symconst = get_entity_ident(ent);
1617 switch (get_nice_modecode(env->mode)){
1621 if (is_direct_entity(ent))
1624 ir_node *node_addis = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Hi16, NULL, id_symconst);
1625 node = new_bd_ppc32_Ori(env->dbg, env->block, node_addis, env->mode);
1626 set_ppc32_symconst_ident(node, id_symconst);
1627 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1632 ir_node *node_addis = new_bd_ppc32_Addis_zero(env->dbg, env->block, env->mode, ppc32_ao_Ha16, NULL, id_symconst);
1633 node = new_bd_ppc32_Lwz(env->dbg, env->block, node_addis, new_NoMem());
1634 set_ppc32_symconst_ident(node, id_symconst);
1635 set_ppc32_offset_mode(node, ppc32_ao_Lo16);
1636 node = new_rd_Proj(env->dbg, env->block, node, env->mode, pn_Load_res);
1643 panic("Mode for SymConst not supported: %F", env->mode);
1649 * Transforms the given firm node (and maybe some other related nodes)
1650 * into one or more assembler nodes.
1652 * @param node the firm node
1653 * @param env the debug module
1655 void ppc32_transform_const(ir_node *node, void *env)
1657 ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
1658 ir_node *asm_node = NULL;
1659 ppc32_transform_env_t tenv;
1665 tenv.block = get_nodes_block(node);
1666 tenv.dbg = get_irn_dbg_info(node);
1667 tenv.irg = current_ir_graph;
1669 DEBUG_ONLY(tenv.mod = cgenv->mod;)
1670 tenv.mode = get_irn_mode(node);
1672 #define OTHER_GEN(a) \
1673 if (get_irn_op(node) == get_op_##a()) { \
1674 asm_node = gen_##a(&tenv); \
1677 DBG((tenv.mod, LEVEL_1, "check %+F ... ", node));
1679 OTHER_GEN(ppc32_Const)
1680 else OTHER_GEN(ppc32_fConst)
1681 else OTHER_GEN(ppc32_SymConst)
1684 exchange(node, asm_node);
1685 DB((tenv.mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
1688 DB((tenv.mod, LEVEL_1, "ignored\n"));