Commit 77eef701 authored by Matthias Braun's avatar Matthias Braun
Browse files

update testprograms to latest libfirm

[r20905]
parent 862c3177
-include config.mak
FIRM_CFLAGS ?= `pkg-config --cflags libfirm`
FIRM_LIBS ?= `pkg-config --libs libfirm`
CC ?= gcc
CPPFLAGS += $(FIRM_CFLAGS)
CFLAGS += -Wall -W -Wstrict-prototypes -Wmissing-prototypes -Werror -pedantic
CFLAGS += -O0 -g3
LFLAGS += $(FIRM_LIBS)
SOURCES = $(wildcard *.c)
GOALS = $(basename $(SOURCES))
.PHONY: all
all: $(GOALS)
%: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< $(LFLAGS)
strip $@
clean:
rm -f $(GOALS)
......@@ -10,13 +10,10 @@
* Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
*/
#include <string.h>
#include <stdio.h>
# include <string.h>
# include <stdio.h>
# include "irvrfy.h"
# include "irdump.h"
# include "firm.h"
#include <libfirm/firm.h>
/**
* variables of imperative programs.
......@@ -52,20 +49,22 @@
int
main(void)
{
char *dump_file_suffix = "";
/* describes the method main */
type *owner;
type *proc_main;
entity *proc_main_e;
ir_type *owner;
ir_type *proc_main;
ir_entity *proc_main_e;
/* describes types defined by the language */
type *prim_t_int;
ir_type *prim_t_int;
/* describes the array and its fields. */
type *array_type; /* the type information for the array */
entity *array_ent; /* the entity representing a field of the array */
ir_type *array_type; /* the ir_type information for the array */
ir_entity *array_ent; /* the ir_entity representing a field of the array */
/* Needed while finding the element size. */
type *elt_type;
ir_type *elt_type;
ir_mode *elt_type_mode;
int size;
ir_node *arr_size;
......@@ -76,7 +75,7 @@ main(void)
init_firm (NULL);
/* make basic type information for primitive type int.
/* make basic ir_type information for primitive ir_type int.
In Sather primitive types are represented by a class.
This is the modeling appropriate for other languages.
Mode_i says that all integers shall be implemented as a
......@@ -88,10 +87,10 @@ main(void)
/* first build procedure main */
owner = get_glob_type();
proc_main = new_type_method(new_id_from_chars("ARRAY-HEAP_EXAMPLE_main", 23), 0, 1);
set_method_res_type(proc_main, 0, (type *)prim_t_int);
proc_main_e = new_entity ((type*)owner, new_id_from_chars("ARRAY-HEAP_EXAMPLE_main", 23), (type *)proc_main);
set_method_res_type(proc_main, 0, (ir_type *)prim_t_int);
proc_main_e = new_entity ((ir_type*)owner, new_id_from_chars("ARRAY-HEAP_EXAMPLE_main", 23), (ir_type *)proc_main);
/* make type information for the array and set the bounds */
/* make ir_type information for the array and set the bounds */
# define N_DIMS 1
# define L_BOUND 0
# define U_BOUND 9
......@@ -101,7 +100,7 @@ main(void)
new_Const(mode_Iu, new_tarval_from_long (L_BOUND, mode_Iu)),
new_Const(mode_Iu, new_tarval_from_long (U_BOUND, mode_Iu)));
/* As the array is accessed by Sel nodes, we need information about
the entity the node selects. Entities of an array are it's elements
the ir_entity the node selects. Entities of an array are it's elements
which are, in this case, integers. */
main_irg = new_ir_graph (proc_main_e, 4);
array_ent = get_array_element_entity(array_type);
......@@ -112,20 +111,20 @@ main(void)
/* first compute size in bytes. */
elt_type = get_array_element_type(array_type);
elt_type_mode = get_type_mode(elt_type);
/* better: read bounds out of array type information */
/* better: read bounds out of array ir_type information */
size = (U_BOUND - L_BOUND + 1) * get_mode_size_bytes(elt_type_mode);
/* make constant representing the size */
arr_size = new_Const(mode_Iu, new_tarval_from_long (size, mode_Iu));
/* allocate and generate the Proj nodes. */
array = new_Alloc(get_store(), arr_size, (type*)array_type, stack_alloc);
set_store(new_Proj(array, mode_M, 0)); /* make the changed memory visible */
array_ptr = new_Proj(array, mode_P, 2); /* remember the pointer to the array */
array = new_Alloc(get_store(), arr_size, (ir_type*)array_type, stack_alloc);
set_store(new_Proj(array, mode_M, pn_Alloc_M)); /* make the changed memory visible */
array_ptr = new_Proj(array, mode_P, pn_Alloc_res); /* remember the pointer to the array */
/* Now the "real" program: */
/* Load element 3 of the array. For this first generate the pointer to this
array element by a select node. (Alternative: increase array pointer
by (three * elt_size), but this complicates some optimizations. The
type information accessible via the entity allows to generate the
ir_type information accessible via the ir_entity allows to generate the
pointer increment later. */
c3 = new_Const (mode_Iu, new_tarval_from_long (3, mode_Iu));
{
......@@ -134,8 +133,8 @@ main(void)
elt = new_Sel(get_store(), array_ptr, 1, in, array_ent);
}
val = new_Load(get_store(), elt, mode_Is);
set_store(new_Proj(val, mode_M, 0));
val = new_Proj(val, mode_Is, 2);
set_store(new_Proj(val, mode_M, pn_Load_M));
val = new_Proj(val, mode_Is, pn_Load_res);
/* return the result of procedure main */
{
......@@ -158,13 +157,12 @@ main(void)
/* verify the graph */
irg_vrfy(main_irg);
printf("Dumping the graph and a type graph.\n");
char *dump_file_suffix = "";
printf("Dumping the graph and a ir_type graph.\n");
dump_ir_block_graph (main_irg, dump_file_suffix);
dump_type_graph(main_irg, dump_file_suffix);
dump_all_types(dump_file_suffix);
printf("use xvcg to view these graphs:\n");
printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
printf("Use ycomp to view these graphs:\n");
printf("ycomp GRAPHNAME\n\n");
return (0);
return 0;
}
......@@ -12,12 +12,12 @@
*/
# include <string.h>
# include <stdio.h>
#include <string.h>
#include <stdio.h>
# include "irvrfy.h"
# include "irdump.h"
# include "firm.h"
#include <libfirm/firm.h>
/**
* imperative programs.
......@@ -31,7 +31,7 @@
* end;
*
* The array is placed on the stack, i.e., a pointer to the array
* is obtained by selecting the entity "a" from the stack. The variables
* is obtained by selecting the ir_entity "a" from the stack. The variables
* on the stack are considered to be entities of the method, as locals
* of a method are only visible within the method. (An alternative to
* make the method owner of the stack variables is to give the ownership
......@@ -45,20 +45,21 @@
int
main(void)
{
char *dump_file_suffix = "";
/* describes the general structure of a C-file */
type *owner; /* the class standing for everything in this file */
type *proc_main; /* Typeinformation for method main. */
entity *proc_main_e; /* The entity describing that method main is an
entity of the fake class representing the file. */
ir_type *owner; /* the class standing for everything in this file */
ir_type *proc_main; /* Typeinformation for method main. */
ir_entity *proc_main_e; /* The ir_entity describing that method main is an
ir_entity of the fake class representing the file. */
/* describes types defined by the language */
type *prim_t_int;
ir_type *prim_t_int;
/* describes the array and its fields. */
entity *array_ent; /* the entity representing the array as member
ir_entity *array_ent; /* the ir_entity representing the array as member
of the stack/method */
type *array_type; /* the type information for the array */
entity *field_ent; /* the entity representing a field of the
ir_type *array_type; /* the ir_type information for the array */
ir_entity *field_ent; /* the ir_entity representing a field of the
array */
/* holds the graph and nodes. */
......@@ -69,7 +70,7 @@ main(void)
printf("\nCreating an IR graph: ARRAY-STACK_EXAMPLE...\n");
/* make basic type information for primitive type int.
/* make basic ir_type information for primitive ir_type int.
In Sather primitive types are represented by a class.
This is the modeling appropriate for other languages.
Mode_i says that all language-integers shall be implemented
......@@ -83,7 +84,7 @@ main(void)
proc_main_e = new_entity (owner, new_id_from_chars ("main", 4), proc_main);
get_entity_ld_name(proc_main_e); /* force name mangling */
/* make type information for the array and set the bounds */
/* make ir_type information for the array and set the bounds */
# define N_DIMS 1
# define L_BOUND 0
# define U_BOUND 9
......@@ -95,13 +96,13 @@ main(void)
main_irg = new_ir_graph (proc_main_e, 4);
/* The array is an entity of the method, placed on the mehtod's own memory,
/* The array is an ir_entity of the method, placed on the mehtod's own memory,
the stack frame. */
array_ent = new_entity(get_cur_frame_type(), new_id_from_chars("a", 1), array_type);
/* As the array is accessed by Sel nodes, we need information about
the entity the node selects. Entities of an array are it's elements
the ir_entity the node selects. Entities of an array are it's elements
which are, in this case, integers. */
/* change entity owner types. */
/* change ir_entity owner types. */
field_ent = get_array_element_entity(array_type);
......@@ -112,7 +113,7 @@ main(void)
/* Load element 3 of the array. For this first generate the pointer
to this the element by a select node. (Alternative: increase
array pointer by (three * elt_size), but this complicates some
optimizations.) The type information accessible via the entity
optimizations.) The ir_type information accessible via the ir_entity
allows to generate the pointer increment later. */
c3 = new_Const (mode_Iu, new_tarval_from_long (3, mode_Iu));
{
......@@ -121,8 +122,8 @@ main(void)
elt = new_Sel(get_store(), array_ptr, 1, in, field_ent);
}
val = new_Load(get_store(), elt, mode_Is);
set_store(new_Proj(val, mode_M, 0));
val = new_Proj(val, mode_Is, 2);
set_store(new_Proj(val, mode_M, pn_Load_M));
val = new_Proj(val, mode_Is, pn_Load_res);
/* return the result of procedure main */
{
......@@ -144,14 +145,13 @@ main(void)
/* verify the graph */
irg_vrfy(main_irg);
char *dump_file_suffix = "";
printf("Dumping the graph and a type graph.\n");
printf("Dumping the graph and a ir_type graph.\n");
dump_ir_block_graph (main_irg, dump_file_suffix);
dump_type_graph(main_irg, dump_file_suffix);
dump_ir_block_graph_w_types(main_irg, dump_file_suffix);
dump_all_types(dump_file_suffix);
printf("Use xvcg to view these graphs:\n");
printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
printf("Use ycomp to view these graphs:\n");
printf("ycomp GRAPHNAME\n\n");
return (0);
}
......@@ -10,12 +10,10 @@
* Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
*/
# include <string.h>
# include <stdio.h>
#include <string.h>
#include <stdio.h>
# include "irvrfy.h"
# include "irdump.h"
# include "firm.h"
#include <libfirm/firm.h>
/**
* This file constructs the ir for the following pseudo-program:
......@@ -29,25 +27,27 @@
* This program demonstrates how to represent string constants.
*/
int main(int argc, char **argv)
int main(void)
{
char *dump_file_suffix = "";
ir_graph *irg; /* this variable contains the irgraph */
type *owner; /* the class in which this method is defined */
type *proc_main; /* type information for the method main */
type *proc_called; /* type information for called method f */
type *U8, *U8array, *string_ptr; /* type for pointers to strings. */
entity *ent; /* represents this method as entity of owner */
entity *const_str; /* represents a constant string. */
ir_type *owner; /* the class in which this method is defined */
ir_type *proc_main; /* ir_type information for the method main */
ir_type *proc_called; /* ir_type information for called method f */
ir_type *U8, *U8array, *string_ptr; /* ir_type for pointers to strings. */
ir_entity *ent; /* represents this method as ir_entity of owner */
ir_entity *const_str; /* represents a constant string. */
char *str = "Hello World\n"; /* The constant string. */
ir_node *x, *str_addr, *proc_ptr, *call;
int i;
symconst_symbol sym;
size_t i;
printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n");
/* init library */
init_firm (NULL);
/* An unsinged 8 bit type */
/* An unsinged 8 bit ir_type */
U8 = new_type_primitive (new_id_from_chars("char", 4), mode_Bu);
/* An array containing unsigned 8 bit elements. */
U8array = new_type_array (new_id_from_chars("char_arr", 8), 1, U8);
......@@ -55,7 +55,7 @@ int main(int argc, char **argv)
string_ptr = new_type_pointer (new_id_from_chars ("ptr_to_string", 13), U8array, mode_P);
/* Make a global entity that represents the constant String. */
/* Make a global ir_entity that represents the constant String. */
const_str = new_entity(get_glob_type(), new_id_from_str("constStr"), U8array);
set_entity_variability(const_str, variability_constant);
for (i = 0; i < strlen(str); i++) {
......@@ -68,7 +68,7 @@ int main(int argc, char **argv)
* For imperative languages like C we view a program as a large class containing
* all functions of the program as methods in this class. This class is
* automatically generated.
* We use the same name for the method type as for the method entity.
* We use the same name for the method ir_type as for the method ir_entity.
*/
#define METHODNAME "CALL_STR_EXAMPLE_main"
#define NRARGS 0
......@@ -77,8 +77,8 @@ int main(int argc, char **argv)
proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
NRARGS, NRES);
/* Make type information for called method which also belongs to the
global type. */
/* Make ir_type information for called method which also belongs to the
global ir_type. */
#define F_METHODNAME "f"
#define F_NRARGS 1
#define F_NRES 0
......@@ -88,12 +88,12 @@ int main(int argc, char **argv)
set_method_param_type(proc_called, 0, string_ptr);
/* Make the entity for main needed for a correct ir_graph. */
/* Make the ir_entity for main needed for a correct ir_graph. */
#define ENTITYNAME "CALL_STR_EXAMPLE_main"
ent = new_entity (owner, new_id_from_chars (ENTITYNAME, strlen(ENTITYNAME)),
proc_main);
/* Generates the basic graph for the method represented by entity ent, that
/* Generates the basic graph for the method represented by ir_entity ent, that
* is, generates start and end blocks and nodes and a first, initial block.
* The constructor needs to know how many local variables the method has.
*/
......@@ -101,14 +101,13 @@ int main(int argc, char **argv)
irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
/* get the pointer to the string constant */
symconst_symbol sym;
sym.entity_p = const_str;
str_addr = new_SymConst(sym, symconst_addr_ent);
str_addr = new_SymConst(mode_P, sym, symconst_addr_ent);
/* get the pointer to the procedure from the class type */
/* get the pointer to the procedure from the class ir_type */
/* this is how a pointer to be fixed by the linker is represented. */
sym.ident_p = new_id_from_str (F_METHODNAME);
proc_ptr = new_SymConst (sym, symconst_addr_name);
proc_ptr = new_SymConst (mode_P, sym, symconst_addr_name);
/* call procedure set_a, first built array with parameters */
{
......@@ -117,7 +116,7 @@ int main(int argc, char **argv)
call = new_Call(get_store(), proc_ptr, 1, in, proc_called);
}
/* make the possible changes by the called method to memory visible */
set_store(new_Proj(call, mode_M, 0));
set_store(new_Proj(call, mode_M, pn_Call_M));
/* Make the return node returning the memory. */
x = new_Return (get_store(), 0, NULL);
......@@ -140,11 +139,10 @@ int main(int argc, char **argv)
irg_vrfy(irg);
printf("Done building the graph. Dumping it.\n");
char *dump_file_suffix = "";
dump_ir_block_graph (irg, dump_file_suffix);
dump_all_types(dump_file_suffix);
printf("Use xvcg to view this graph:\n");
printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
printf("Use ycomp to view this graph:\n");
printf("ycomp GRAPHNAME\n\n");
return (0);
}
......@@ -10,13 +10,10 @@
* Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
*/
#include <stdio.h>
# include <string.h>
#include <string.h>
# include "irvrfy.h"
# include "irdump.h"
# include "firm.h"
#include <libfirm/firm.h>
/**
* This file constructs the ir for the following pseudo-program:
......@@ -28,47 +25,47 @@
* return a;
**/
int main(int argc, char **argv)
int main(void)
{
type *prim_t_int;
ir_type *prim_t_int;
ir_graph *irg; /* this variable contains the irgraph */
type *owner; /* the class in which this method is defined */
type *method; /* the type of this method */
entity *ent; /* represents this method as entity of owner */
ir_type *owner; /* the class in which this method is defined */
ir_type *method; /* the ir_type of this method */
ir_entity *ent; /* represents this method as ir_entity of owner */
ir_node *x, *x_then, *arg1, *c2, *c10, *cmpGt, *cmpLt, *and, *f, *t, *b;
printf("\nCreating an IR graph: COND_EXAMPLE...\n");
/* init library */
init_firm (NULL);
init_firm(NULL);
/*** Make basic type information for primitive type int. ***/
prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
/*** Make basic ir_type information for primitive ir_type int. ***/
prim_t_int = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
/* FIRM was designed for oo languages where all methods belong to a class.
* For imperative languages like C we view a file as a large class containing
* all functions as methods in this file.
* Therefore we define a class "COND_EXAMPLE" with a method main as an
* entity.
* ir_entity.
*/
#define CLASSNAME "COND_EXAMPLE"
#define ENTITYNAME "main"
owner = new_type_class (new_id_from_chars (CLASSNAME, strlen(CLASSNAME)));
method = new_type_method (new_id_from_chars("main", 4), 1, 1);
owner = new_type_class(new_id_from_chars(CLASSNAME, strlen(CLASSNAME)));
method = new_type_method(new_id_from_chars("main", 4), 1, 1);
set_method_param_type(method, 0, prim_t_int);
set_method_res_type(method, 0, prim_t_int);
ent = new_entity (owner, new_id_from_chars (ENTITYNAME, strlen(ENTITYNAME)), method);
ent = new_entity(owner, new_id_from_chars(ENTITYNAME, strlen(ENTITYNAME)), method);
get_entity_ld_name(ent);
/* Generates the basic graph for the method represented by entity ent, that
/* Generates the basic graph for the method represented by ir_entity ent, that
* is, generates start and end blocks and nodes and a first, initial block.
* The constructor needs to know how many local variables the method has.
*/
#define NUM_OF_LOCAL_VARS 1
irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
/* get the first argument a of method main - see irgraph.h */
arg1 = new_Proj(get_irg_args(irg), mode_Is, 0);
......@@ -78,58 +75,58 @@ int main(int argc, char **argv)
/* the expression that evaluates the condition */
/* cmpGt = a > 2 */
c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
c2 = new_Const(mode_Is, new_tarval_from_long(2, mode_Is));
cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, pn_Cmp_Gt);
cmpGt = new_Conv(cmpGt, mode_Is);
/* cmpLt = a < 10 */
c10 = new_Const (mode_Is, new_tarval_from_long (10, mode_Is));
c10 = new_Const(mode_Is, new_tarval_from_long(10, mode_Is));
cmpLt = new_Proj(new_Cmp(get_value(0, mode_Is), c10), mode_b, pn_Cmp_Lt);
cmpLt = new_Conv(cmpLt, mode_Is);
/* cmpGt && cmpLt */
and = new_And(cmpGt, cmpLt, mode_Is);
/* compare result and 0 because we have no cast from integer to bool */
and = new_Cmp(and, new_Const (mode_Is, new_tarval_from_long (0, mode_Is)));
and = new_Cmp(and, new_Const(mode_Is, new_tarval_from_long(0, mode_Is)));
and = new_Proj(and, mode_b, pn_Cmp_Lg);
/* the conditional branch */
x = new_Cond (and);
f = new_Proj (x, mode_X, 0); /* if condition is false */
t = new_Proj (x, mode_X, 1); /* if condition is true */
x = new_Cond(and);
f = new_Proj(x, mode_X, pn_Cond_false); /* if condition is false */
t = new_Proj(x, mode_X, pn_Cond_true); /* if condition is true */
mature_immBlock (get_irg_current_block(irg));
mature_immBlock(get_irg_current_block(irg));
/* generate and fill the then block */
b = new_immBlock ();
add_immBlock_pred (b, t);
set_value (0, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
mature_immBlock (b);
x_then = new_Jmp ();
b = new_immBlock();
add_immBlock_pred(b, t);
set_value(0, new_Const(mode_Is, new_tarval_from_long(1, mode_Is)));
mature_immBlock(b);
x_then = new_Jmp();
/* generate the fall through block and add all cfg edges */
b = new_immBlock ();
add_immBlock_pred (b, x_then);
add_immBlock_pred (b, f);
b = new_immBlock();
add_immBlock_pred(b, x_then);
add_immBlock_pred(b, f);
/* Generate the return node into current region. */
{
ir_node *in[1]; /* this is the array containing the return parameters */
in[0] = get_value(0, mode_Is);
x = new_Return (get_store(), 1, in);
x = new_Return(get_store(), 1, in);
}
/* Now generate all instructions for this block and all its predecessor blocks
* so we can mature it. */
mature_immBlock (get_irg_current_block(irg));
mature_immBlock(get_irg_current_block(irg));
/* This adds the in edge of the end block which originates at the
return statement. The return node passes controlflow to the end block.*/
add_immBlock_pred (get_irg_end_block(irg), x);
add_immBlock_pred(get_irg_end_block(irg), x);
/* Now we can mature the end block as all it's predecessors are known. */
mature_immBlock (get_irg_end_block(irg));
mature_immBlock(get_irg_end_block(irg));
irg_finalize_cons (irg);
irg_finalize_cons(irg);
printf("Optimizing ...\n");
dead_node_elimination(irg);
......@@ -138,9 +135,9 @@ int main(int argc, char **argv)
irg_vrfy(irg);
printf("Done building the graph. Dumping it.\n");
dump_ir_block_graph (irg, 0);
printf("Use xvcg to view this graph:\n");
printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
dump_ir_block_graph(irg, 0);
printf("Use ycomp to view this graph:\n");
printf("ycomp GRAPHNAME\n\n");
return (0);
return 0;
}
FIRM_HOME = $(HOME)/projects/firm
FIRM_BUILD = $(FIRM_HOME)/build/i686-pc-linux-gnu/debug/
FIRM_CFLAGS = -I$(FIRM_HOME)/libfirm/include -I$(FIRM_HOME)/obstack -I$(FIRM_HOME)/libcore -I$(FIRM_HOME)/libcore/libcore -I$(FIRM_HOME)
FIRM_LIBS = -L$(FIRM_BUILD) -lfirm -llpp -lcore -lm -lz -ldl
/*
* Project: libFIRM
* File name: testprograms/const_ent_example.c
* Purpose: Shows how to construct type information for constant entities.
* Purpose: Shows how to construct ir_type information for constant entities.
* Author: Goetz Lindenmaier
* Modified by:
* Created:
......@@ -11,22 +11,22 @@
*/
# include <stdio.h>
# include <string.h>
#include <stdio.h>
#include <string.h>
# include "irvrfy.h"
# include "irdump.h"
# include "firm.h"
#include <libfirm/firm.h>
/**