dfd6d584c4467b20dadad317e6b02bc37eafd5a1
[libfirm] / firmjni / testprograms / Empty.java
1 /*
2  * Project:     libFIRM
3  * File name:   firmjni/testprograms/Empty.java
4  * Purpose:     This is an example of how to use the JNI interface of Firm.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 import firmjni.*;
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 class Empty {
27     public static void main (String[] args) {
28
29         System.out.println("\nCreating an IR graph: EMPTY...");
30
31         /* init library: Java did not support the callback, so ALWAYS use 0 here */
32         Firm.initFirm(0);
33
34         /** Build type information for the procedure. **/
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 in this file as methods.
39          * This class is generated automatically.        */
40         int owner = Irprog.getGlobType();
41
42         /* The type of the method */
43         int name = Ident.idFromStr("EMPTY_main", 10);
44         int proc_main = Type.newTypeMethod(name, 0, 0);
45         /* An entity representing the method.  Owner of the entity is the global
46            class type mentioned above. */
47         int ent = Entity.newEntity (owner, name, proc_main);
48
49         /** Build code for the procedure. **/
50
51         /* Generates the basic graph for the method represented by entity ent, that
52          * is, generates start and end blocks and nodes and a first, initial block.
53          * The constructor needs to know the number of local variables (including
54          * the arguments) in the method.
55          */
56         int irg = Irgraph.newIrGraph (ent, 0);
57
58         /* The constructor new_ir_graph() generated a region to place nodes in.
59          * This region is accessible via the attribut current_block of irg and
60          * it is not matured.
61          * Generate the return node into this region. The Return node is needed to
62          * return at least the memory. */
63         int []in  = new int[0];
64         int x = Ircons.newReturn (Ircons.getStore(), in.length, in);
65
66         /* Now we generated all instructions for this block and all its predecessor
67          * blocks so we can mature it.  (There are not too much.) */
68         Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
69
70         /* This adds the in edge of the end block which originates at the return statement.
71          * The return node passes controlflow to the end block.  */
72         Ircons.addInEdge (Irgraph.getIrgEndBlock(irg), x);
73         /* Now we can mature the end block as all it's predecessors are known. */
74         Ircons.matureBlock (Irgraph.getIrgEndBlock(irg));
75
76         /* Verify the graph.  Finds some very bad errors in the graph. */
77         Irvrfy.irgVrfy(irg);
78         Ircons.finalizeCons (irg);
79
80         System.out.println("Done building the graph.  Dumping it.");
81         Irdump.dumpIrBlockGraph (irg);
82         Irdump.dumpAllTypes();
83
84
85         System.out.println("use xvcg to view this graph:");
86         System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");
87     }
88 }