X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=testprograms%2Fif_example.c;h=9fcce41cbfb57df2447d34ee567f739d3dbd0ae8;hb=c15abf93dee3ccbe0be3d1ac6129dbca62e73b1b;hp=caaddab193c85e999c101a979d81edb0def3ce65;hpb=4ea1df63a99fe3e9bf8f0395cdf33c39d4a2c677;p=libfirm diff --git a/testprograms/if_example.c b/testprograms/if_example.c index caaddab19..9fcce41cb 100644 --- a/testprograms/if_example.c +++ b/testprograms/if_example.c @@ -1,128 +1,141 @@ -/* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe -** All rights reserved. -** -** Authors: Christian Schaefer, Goetz Lindenmaier -** -** testprogram. -*/ - +/* + * Project: libFIRM + * File name: testprograms/if_example.c + * Purpose: Shows construction of if. + * Author: Christian Schaefer, Goetz Lindenmaier + * Modified by: + * Created: + * CVS-ID: $Id$ + * Copyright: (c) 1999-2003 Universität Karlsruhe + * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. + */ + +# include +# include + +# include "irvrfy.h" # include "irdump.h" # include "firm.h" /** -*** This file constructs the ir for the following pseudo-program: -*** -*** Doesn't work for some reason!!!! -*** -*** main() { -*** int a = 0; -*** int b = 2; -*** if ( a == a ) // a constant condition would be eliminated, -*** // a realistic condition would no more be a small example -*** { a := a - 2; } -*** -*** // return a, bla; // das ist hier kein korrekter graph -*** } +* This file constructs the ir for the following pseudo-program: +* +* int main(int a) { +* int b = 2; +* if ( a == b ) +* { a := a - 3; } +* +* return a; +* } **/ int main(void) { ir_graph *irg; - type_class *owner; + type *owner; entity *ent; - type_method *proc_main; /* typeinformation for the method main */ - type_primitive *typ; - ir_node *x, *r, *t, *f, *a; + type *proc_main; /* type information for the method main */ + type *typ; + ir_node *x, *r, *t, *f, *a, *cmp; int a_pos, b_pos; - FILE *outfile; - printf("creating an IR graph: IF_EXAMPLE...\n"); + printf("\nCreating an IR graph: IF_EXAMPLE...\n"); - init_firm (); + init_firm (NULL); #define CLASSNAME "IF_EXAMPLE" -#define METHODNAME "main" -#define NRARGS 0 -#define NRES 2 - - owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME))); - proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)), NRARGS, NRES); - ent = new_entity ((type *)owner, - id_from_str (METHODNAME, strlen(METHODNAME)), - (type *)proc_main); - -#define RES_NAME "res1" - typ = new_type_primitive(ID_FROM_STR(RES_NAME, strlen(RES_NAME)), mode_i); - set_method_res_type(proc_main, 0, (type*)typ); -#undef RES_NAME - -#define RES_NAME "res2" - typ = new_type_primitive(ID_FROM_STR(RES_NAME, strlen(RES_NAME)), mode_i); - set_method_res_type(proc_main, 1, (type*)typ); -#undef RES_NAME - - - /* Generates start and end blocks and nodes and a first, initial block */ - irg = new_ir_graph (ent, 1); - - /* The value position used for a: */ +#define METHODNAME "IF_EXAMPLE_main" +#define NRARGS 1 +#define NRES 1 + + /** Type information for the procedure **/ + + owner = get_glob_type(); + /* Type information for the procedure */ + proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)), + NRARGS, NRES); + /* The entity for the procedure */ + ent = new_entity (owner, + new_id_from_chars (METHODNAME, strlen(METHODNAME)), + proc_main); + /* The type int. This type is necessary to model the result and parameters + the procedure. */ +#define PRIM_NAME "int" + typ = new_type_primitive(new_id_from_chars(PRIM_NAME, strlen(PRIM_NAME)), mode_Is); + /* The parameter and result types of the procedure. */ + set_method_param_type(proc_main, 0, typ); + set_method_res_type(proc_main, 0, typ); + + /** The code of the procedure **/ + + /* Generates start and end blocks and nodes, and a first, initial block */ +#define NRLOCS 2 + irg = new_ir_graph (ent, NRLOCS); + + /* The value position used for: */ a_pos = 0; b_pos = 1; - /* Generate the constant */ - set_value (a_pos, new_Const (mode_i, tarval_from_long (mode_i, 0))); - set_value (b_pos, new_Const (mode_i, tarval_from_long (mode_i, 2))); - mature_block (irg->current_block); + /* Get the procedure parameter and assign it to the parameter variable + a. */ + set_value (a_pos, new_Proj (get_irg_args(irg), mode_Is, 0)); + /* Generate the constant and assign it to b. The assignment is resovled to a + dataflow edge. */ + set_value (b_pos, new_Const (mode_Is, new_tarval_from_long (2, mode_Is))); + /* We know all predecessors of the block and all set_values and set_stores are + preformed. We can mature the block. */ + mature_immBlock (get_irg_current_block(irg)); /* Generate a conditional branch */ - x = new_Cond (new_Proj(new_Cmp(get_value(a_pos, mode_i), - get_value(a_pos, mode_i)), - mode_b, Eq)); + cmp = new_Cmp(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is)); + x = new_Cond (new_Proj(cmp, mode_b, pn_Cmp_Eq)); f = new_Proj (x, mode_X, 0); t = new_Proj (x, mode_X, 1); /* generate and fill the then block */ - r = new_Block (); - add_in_edge (r, t); - // mature_block (r); - a = new_Sub(get_value(a_pos, mode_i), - new_Const (mode_i, tarval_from_long (mode_i, 2)), - mode_i); + r = new_immBlock (); + add_immBlock_pred (r, t); + { + ir_node *b,*c; + c = new_Const (mode_Is, new_tarval_from_long (3, mode_Is)); + b = get_value(a_pos, mode_Is); + a = new_Sub(b, + c, + mode_Is); + } set_value (a_pos, a); - mature_block (r); + mature_immBlock (r); x = new_Jmp (); /* generate the fall through block and add all cfg edges */ - r = new_Block (); - add_in_edge (r, f); - add_in_edge (r, x); - mature_block (r); + r = new_immBlock (); + add_immBlock_pred (r, f); + add_immBlock_pred (r, x); + mature_immBlock (r); + /* The Return statement */ { - ir_node *in[3], *store ; - in[0] = get_value (a_pos, mode_i); - in[1] = a; - in[2] = get_value (b_pos, mode_i); + ir_node *in[1], *store ; + in[0] = get_value (a_pos, mode_Is); store = get_store(); - x = new_Return (store, 2, in); + x = new_Return (store, 1, in); } /* finalize the end block generated in new_ir_graph() */ - add_in_edge (irg->end_block, x); - mature_block (irg->end_block); + add_immBlock_pred (get_irg_end_block(irg), x); + mature_immBlock (get_irg_end_block(irg)); /* verify the graph */ - vrfy_graph(irg); + irg_vrfy(irg); + irg_finalize_cons (irg); /* output the vcg file */ - printf("\nDone building the graph. Dumping it.\n"); - - dump_ir_block_graph (irg); - + printf("Done building the graph. Dumping it.\n"); + dump_ir_block_graph (irg, 0); printf("use xvcg to view this graph:\n"); - printf("/ben/goetz/bin/xvcg GRAPHNAME\n"); + printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n"); return (0); }