3 * File name: testprograms/call_str_example.c
4 * Purpose: Shows representation of constant string.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 * This file constructs the ir for the following pseudo-program:
25 * void CALL_STR_EXAMPLE_main () {
29 * This program demonstrates how to represent string constants.
32 int main(int argc, char **argv)
34 ir_graph *irg; /* this variable contains the irgraph */
35 type *owner; /* the class in which this method is defined */
36 type *proc_main; /* type information for the method main */
37 type *proc_called; /* type information for called method f */
38 type *U8, *U8array, *string_ptr; /* type for pointers to strings. */
39 entity *ent; /* represents this method as entity of owner */
40 entity *const_str; /* represents a constant string. */
41 char *str = "Hello World\n"; /* The constant string. */
42 ir_node *x, *str_addr, *proc_ptr, *call;
45 printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n");
50 /* An unsinged 8 bit type */
51 U8 = new_type_primitive (new_id_from_chars("char", 4), mode_Bu);
52 /* An array containing unsigned 8 bit elements. */
53 U8array = new_type_array (new_id_from_chars("char_arr", 8), 1, U8);
54 string_ptr = new_type_pointer (new_id_from_chars ("ptr_to_string", 13), U8array);
56 /* Make a global entity that represents the constant String. */
57 const_str = new_entity(get_glob_type(), new_id_from_str("constStr"), U8array);
58 set_entity_variability(const_str, variability_constant);
59 for (i = 0; i < strlen(str); i++) {
60 tarval *val = new_tarval_from_long(str[i], mode_Bu);
61 ir_node *con = new_Const(mode_Bu, val);
62 add_compound_ent_value(const_str, con, get_array_element_entity(U8array));
65 /* FIRM was designed for oo languages where all methods belong to a class.
66 * For imperative languages like C we view a program as a large class containing
67 * all functions of the program as methods in this class. This class is
68 * automatically generated.
69 * We use the same name for the method type as for the method entity.
71 #define METHODNAME "CALL_STR_EXAMPLE_main"
74 owner = get_glob_type();
75 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
78 /* Make type information for called method which also belongs to the
80 #define F_METHODNAME "f"
83 owner = get_glob_type();
84 proc_called = new_type_method(new_id_from_chars(F_METHODNAME, strlen(F_METHODNAME)),
86 set_method_param_type(proc_called, 0, string_ptr);
89 /* Make the entity for main needed for a correct ir_graph. */
90 #define ENTITYNAME "CALL_STR_EXAMPLE_main"
91 ent = new_entity (owner, new_id_from_chars (ENTITYNAME, strlen(ENTITYNAME)),
94 /* Generates the basic graph for the method represented by entity ent, that
95 * is, generates start and end blocks and nodes and a first, initial block.
96 * The constructor needs to know how many local variables the method has.
98 #define NUM_OF_LOCAL_VARS 0
99 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
101 /* get the pointer to the string constant */
103 sym.entity_p = const_str;
104 str_addr = new_SymConst(sym, symconst_addr_ent);
106 /* get the pointer to the procedure from the class type */
107 /* this is how a pointer to be fixed by the linker is represented. */
108 sym.ident_p = new_id_from_str (F_METHODNAME);
109 proc_ptr = new_SymConst (sym, symconst_addr_name);
111 /* call procedure set_a, first built array with parameters */
115 call = new_Call(get_store(), proc_ptr, 1, in, proc_called);
117 /* make the possible changes by the called method to memory visible */
118 set_store(new_Proj(call, mode_M, 0));
120 /* Make the return node returning the memory. */
121 x = new_Return (get_store(), 0, NULL);
122 /* Now we generated all instructions for this block and all its predecessor blocks
123 * so we can mature it. */
124 mature_immBlock (get_irg_current_block(irg));
126 /* This adds the in edge of the end block which originates at the return statement.
127 * The return node passes controlflow to the end block. */
128 add_immBlock_pred (get_irg_end_block(irg), x);
129 /* Now we can mature the end block as all it's predecessors are known. */
130 mature_immBlock (get_irg_end_block(irg));
132 irg_finalize_cons (irg);
134 printf("Optimizing ...\n");
135 dead_node_elimination(irg);
137 /* verify the graph */
140 printf("Done building the graph. Dumping it.\n");
141 char *dump_file_suffix = "";
142 dump_ir_block_graph (irg, dump_file_suffix);
143 dump_all_types(dump_file_suffix);
144 printf("Use xvcg to view this graph:\n");
145 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");