Initial revision
[libfirm] / testprograms / call_str_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 #include <stdio.h>
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /**
15 ***  This file constructs the ir for the following pseudo-program:
16 ***
17 ***  void f(char *);
18 ***
19 ***  main() {
20 ***    f("Hello world !");
21 ***  }
22 **/
23
24 int main(int argc, char **argv)
25 {
26   ir_graph *irg;          /* this variable contains the irgraph */
27   type_class *owner;      /* the class in which this method is defined */
28   entity *ent;            /* represents this method as entity of owner */
29   ir_node *x, *const_str, *proc_ptr, *call;
30
31   printf("creating an IR graph: CALL_STR_EXAMPLE...\n");
32
33   /* init library */
34   init_firm ();
35
36   /* FIRM was designed for oo languages where all methods belong to a class.
37    * For imperative languages like C we view a file as a large class containing
38    * all functions as methods in this file.
39    * Therefore we define a class "CALL_STR_EXAMPLE" with a method main as
40    * an entity.
41    */
42 #define CLASSNAME "CALL_STR_EXAMPLE"
43 #define ENTITYNAME "main"
44
45   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
46   ent = new_entity ((type *)owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), NULL);
47
48
49   /* Generates the basic graph for the method represented by entity ent, that
50    * is, generates start and end blocks and nodes and a first, initial block.
51    * The constructor needs to know how many local variables the method has.
52    */
53 #define NUM_OF_LOCAL_VARS 0
54
55   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
56
57   /* the string is enterd in the constant table. const_str is a pointer to the string */
58   const_str = new_Const (mode_p, tarval_p_from_str ("Hello world!"));
59
60   /* get the pointer to the procedure from the class type */
61   /* this is how a pointer to be fixed by the linker is represented after
62      lowering a Sel node. */
63 #define FUNCTIONNAME "f"
64   proc_ptr = new_Const (mode_p, tarval_p_from_str (FUNCTIONNAME));
65
66   /* call procedure set_a, first built array with parameters */
67   {
68     ir_node *in[1];
69     in[0] = const_str;
70     call = new_Call(get_store(), proc_ptr, 1, in, NULL);
71   }
72   /* make the possible change of call to memory visible */
73   set_store(new_Proj(call, mode_M, 0));
74
75
76   /* The constructor new_ir_graph() generated a region to place nodes in.
77    * This region is accessible via the attribut current_block of irg and
78    * it is not matured.
79    * Generate the return node into this region. The Return node is needed to
80    * return at least the store. */
81   {
82     ir_node *in[0]; /* this is the array containing the return parameters */
83     x = new_Return (get_store(), 0, in);
84   }
85   /* Now generate all instructions for this block and all its predecessor blocks
86    * so we can mature it. */
87   mature_block (irg->current_block);
88
89   /* This adds the in edge of the end block which originates at the return statement.
90    * The return node passes controlflow to the end block.  */
91   add_in_edge (irg->end_block, x);
92   /* Now we can mature the end block as all it's predecessors are known. */
93   mature_block (irg->end_block);
94
95   printf("\nDone building the graph.  Dumping it.\n");
96   dump_ir_block_graph (irg);
97
98   printf("use xvcg to view this graph:\n");
99   printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");
100
101   return (0);
102
103
104 }