minor imporvements: comments, output
[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 */
7
8 #include <stdio.h>
9
10 # include "irdump.h"
11 # include "firm.h"
12
13 /**
14 ***  An empty Firm program.
15 ***
16 ***  This file constructs the ir for the following pseudo-program:
17 ***
18 ***  main() {
19 ***    return;
20 ***  }
21 ***
22 ***
23 **/
24
25 int main(int argc, char **argv)
26 {
27   ir_graph *irg;        /* this variable contains the irgraph */
28   type     *owner;      /* the class in which this method is defined */
29   type     *proc_main;  /* type information for the method main */
30   entity   *ent;        /* represents this method as entity of owner */
31   ir_node  *x;          /* to build control flow */
32
33   printf("\nCreating an IR graph: EMPTY...\n");
34
35   /* init library */
36   init_firm ();
37
38   /** Build type information for the procedure. **/
39
40   /* FIRM was designed for oo languages where all methods belong to a class.
41    * For imperative languages like C we view a file as a large class containing
42    * all functions in this file as methods.
43    * This clas is generated automatically.
44    */
45   owner = get_glob_type();
46
47 #define METHODNAME "main"
48 #define NRARGS 0
49 #define NRES 0
50   /* The type of the method */
51   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
52                               NRARGS, NRES);
53   /* An entity representing the method.  Owner of the entity is the global class
54      type mentioned above. */
55   ent = new_entity ((type *)owner,
56                     id_from_str (METHODNAME, strlen(METHODNAME)),
57                     (type *)proc_main);
58
59   /** Build code for the procedure. **/
60
61   /* Generates the basic graph for the method represented by entity ent, that
62    * is, generates start and end blocks and nodes and a first, initial block.
63    * The constructor needs to know the number of local variables (including
64    * the arguments) in the method.
65    */
66 #define NUM_OF_LOCAL_VARS 0
67   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
68
69   /* The constructor new_ir_graph() generated a region to place nodes in.
70    * This region is accessible via the attribut current_block of irg and
71    * it is not matured.
72    * Generate the return node into this region. The Return node is needed to
73    * return at least the memory. */
74   {
75     ir_node *in[0]; /* this is the array containing the return parameters */
76     x = new_Return (get_store(), 0, in);
77   }
78   /* Now we generated all instructions for this block and all its predecessor
79    * blocks so we can mature it.  (There are not too much.) */
80   mature_block (get_irg_current_block(irg));
81
82   /* This adds the in edge of the end block which originates at the return statement.
83    * The return node passes controlflow to the end block.  */
84   add_in_edge (get_irg_end_block(irg), x);
85   /* Now we can mature the end block as all it's predecessors are known. */
86   mature_block (get_irg_end_block(irg));
87
88   /* Verify the graph.  Finds some very bad errors in the graph. */
89   irg_vrfy(irg);
90
91   printf("Done building the graph.  Dumping it.\n");
92   dump_ir_block_graph (irg);
93
94   printf("use xvcg to view this graph:\n");
95   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
96
97   return (0);
98 }