c8cf6f0bd7c97501d506ab0fe729e4bb5cb811d4
[libfirm] / testprograms / float_example.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 Firm program to test float values.
17 ***
18 ***  This file constructs the ir for the following pseudo-program:
19 ***
20 ***  main() {
21 ***    ...
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   type     *prim_t_dbl;
33   entity   *ent;        /* represents this method as entity of owner */
34   ir_node  *x;          /* to build control flow */
35   tarval *tv;
36
37   printf("\nCreating an IR graph: EMPTY...\n");
38
39   /* init library */
40   init_firm ();
41
42   /** Build type information for the procedure. **/
43
44   /* FIRM was designed for oo languages where all methods belong to a class.
45    * For imperative languages like C we view a file as a large class containing
46    * all functions in this file as methods.
47    * This clas is generated automatically.
48    */
49   owner = get_glob_type();
50
51 #define METHODNAME "main"
52 #define NRARGS 0
53 #define NRES 1
54   /* The type of the method */
55   prim_t_dbl = new_type_primitive(id_from_str ("dbl", 3), mode_D);
56   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
57                               NRARGS, NRES);
58   set_method_res_type(proc_main, 0, prim_t_dbl);
59
60   /* An entity representing the method.  Owner of the entity is the global class
61      type mentioned above. */
62   ent = new_entity ((type *)owner,
63                     id_from_str (METHODNAME, strlen(METHODNAME)),
64                     (type *)proc_main);
65
66   /** Build code for the procedure. **/
67
68   /* Generates the basic graph for the method represented by entity ent, that
69    * is, generates start and end blocks and nodes and a first, initial block.
70    * The constructor needs to know the number of local variables (including
71    * the arguments) in the method.
72    */
73 #define NUM_OF_LOCAL_VARS 0
74   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
75
76   tv = tarval_d_from_str ("12345678901234567890.1234567890", 31);
77
78
79
80
81   {
82     ir_node *in[1]; /* this is the array containing the return parameters */
83     in[0] = new_Const(mode_D, tv);
84     x = new_Return (get_store(), 1, in);
85   }
86   /* Now we generated all instructions for this block and all its predecessor
87    * blocks so we can mature it.  (There are not too much.) */
88   mature_block (get_irg_current_block(irg));
89
90   /* This adds the in edge of the end block which originates at the return statement.
91    * The return node passes controlflow to the end block.  */
92   add_in_edge (get_irg_end_block(irg), x);
93   /* Now we can mature the end block as all it's predecessors are known. */
94   mature_block (get_irg_end_block(irg));
95
96   /* Verify the graph.  Finds some very bad errors in the graph. */
97   irg_vrfy(irg);
98   finalize_cons (irg);
99
100   printf("Done building the graph.  Dumping it.\n");
101   dump_ir_block_graph (irg);
102
103   printf("use xvcg to view this graph:\n");
104   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
105
106   return (0);
107 }