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