Programs to test the jni for firm
[libfirm] / firmjni / testprograms / Empty.java
1 import firmjni.*;
2
3 /* Copyright (C) 2002 by Universitaet Karlsruhe
4 ** All rights reserved.
5 **
6 ** Author: Goetz Lindenmaier
7 **
8 ** This is an example of how to use the JNI interface of Firm.
9 **
10 */
11
12 /**
13 ***  An empty Firm program.
14 ***
15 ***  This file constructs the ir for the following pseudo-program:
16 ***
17 ***  main() {
18 ***    return;
19 ***  }
20 ***
21 ***
22 **/
23 class Empty {
24     public static void main (String[] args) {
25         Firm firm = new Firm();
26
27         System.out.println("\nCreating an IR graph: EMPTY...");
28
29         /* init library */
30         Firm.initFirm();
31
32         /** Build type information for the procedure. **/
33
34         /* FIRM was designed for oo languages where all methods beint to a class.
35          * For imperative languages like C we view a file as a large class containing
36          * all functions in this file as methods.
37          * This clas is generated automatically.
38          */
39         int owner = Irprog.getGlobType();
40
41         /* The type of the method */
42         int name = Ident.idFromStr("main", 4);
43         int proc_main = Type.newTypeMethod(name, 0, 0);
44         /* An entity representing the method.  Owner of the entity is the global class
45            type mentioned above. */
46         int ent = Entity.newEntity (owner, name, proc_main);
47
48         /** Build code for the procedure. **/
49
50         /* Generates the basic graph for the method represented by entity ent, that
51          * is, generates start and end blocks and nodes and a first, initial block.
52          * The constructor needs to know the number of local variables (including
53          * the arguments) in the method.
54          */
55         int irg = Irgraph.newIrGraph (ent, 0);
56
57         /* The constructor new_ir_graph() generated a region to place nodes in.
58          * This region is accessible via the attribut current_block of irg and
59          * it is not matured.
60          * Generate the return node into this region. The Return node is needed to
61          * return at least the memory. */
62         //ir_node *in[0]; /* this is the array containing the return parameters */
63         int []in  = new int[0];
64         int x = Ircons.newReturn (Ircons.getStore(), in.length, in);
65
66         /* Test enumerators */
67         if (Irnode.getIrnModecode(x) != Irmode.irm_X)
68             System.out.println(" wrong modecode");
69         else
70             System.out.println(" proper modecode Execution.");
71
72         /* Now we generated all instructions for this block and all its predecessor
73          * blocks so we can mature it.  (There are not too much.) */
74         Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
75
76         /* This adds the in edge of the end block which originates at the return statement.
77          * The return node passes controlflow to the end block.  */
78         Ircons.addInEdge (Irgraph.getIrgEndBlock(irg), x);
79         /* Now we can mature the end block as all it's predecessors are known. */
80         Ircons.matureBlock (Irgraph.getIrgEndBlock(irg));
81
82         /* Verify the graph.  Finds some very bad errors in the graph. */
83         //irg_vrfy(irg);
84         Ircons.finalizeCons (irg);
85
86         System.out.println("Done building the graph.  Dumping it.");
87         Irdump.dumpIrBlockGraph (irg);
88         Irdump.dumpAllTypes();
89
90
91         System.out.println("use xvcg to view this graph:");
92         System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");
93     }
94 }