f00430d6389a8fecb853d4134259cf9793ec64f7
[libfirm] / testprograms / empty.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  * das leere FIRM Programm
16  */
17
18 /**
19 ***  This file constructs the ir for the following pseudo-program:
20 ***
21 ***  main() {
22 ***    return;
23 ***  }
24 **/
25
26 int main(int argc, char **argv)
27 {
28   ir_graph *irg;          /* this variable contains the irgraph */
29   type_class *owner;      /* the class in which this method is defined */
30   type_method *proc_main; /* type information for the method main */
31   entity *ent;            /* represents this method as entity of owner */
32   ir_node *x;
33
34   printf("\nCreating an IR graph: EMPTY...\n");
35
36   /* init library */
37   init_firm ();
38
39   /* Don't optimize anything */
40   set_optimize(1);
41
42   /* FIRM was designed for oo languages where all methods belong to a class.
43    * For imperative languages like C we view a file as a large class containing
44    * all functions as methods in this file.
45    * This class now is automatically generated.
46    */
47 #define METHODNAME "main"
48 #define NRARGS 0
49 #define NRES 0
50
51   owner = get_glob_type();
52   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
53                               NRARGS, NRES);
54   ent = new_entity ((type *)owner,
55                     id_from_str (METHODNAME, strlen(METHODNAME)),
56                     (type *)proc_main);
57
58   /* Generates the basic graph for the method represented by entity ent, that
59    * is, generates start and end blocks and nodes and a first, initial block.
60    * The constructor needs to know how many local variables the method has.
61    */
62 #define NUM_OF_LOCAL_VARS 0
63
64   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
65
66
67   /* The constructor new_ir_graph() generated a region to place nodes in.
68    * This region is accessible via the attribut current_block of irg and
69    * it is not matured.
70    * Generate the return node into this region. The Return node is needed to
71    * return at least the store. */
72   {
73     ir_node *in[0]; /* this is the array containing the return parameters */
74     x = new_Return (get_store(), 0, in);
75   }
76   /* Now generate all instructions for this block and all its predecessor blocks
77    * so we can mature it. */
78   mature_block (get_irg_current_block(irg));
79
80   /* This adds the in edge of the end block which originates at the return statement.
81    * The return node passes controlflow to the end block.  */
82   add_in_edge (get_irg_end_block(irg), x);
83   /* Now we can mature the end block as all it's predecessors are known. */
84   mature_block (get_irg_end_block(irg));
85
86   /* verify the graph */
87   irg_vrfy(irg);
88
89   printf("Optimizing ...\n");
90   dead_node_elimination(irg);
91
92   printf("Done building the graph.  Dumping it.\n");
93   dump_ir_block_graph (irg);
94
95   printf("use xvcg to view this graph:\n");
96   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
97
98   return (0);
99 }