X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=testprograms%2Fcall_str_example.c;h=2232baac6c32a359459c7e50ab91e581aed06dbb;hb=67067849894a6b5e0f87253ede8539331e3bdc3d;hp=57e677e9dcb5f2f74f536c3d2514a7666e811ed5;hpb=328ae18da3e796f4f9fda2aba629cc34e2849ed7;p=libfirm diff --git a/testprograms/call_str_example.c b/testprograms/call_str_example.c index 57e677e9d..2232baac6 100644 --- a/testprograms/call_str_example.c +++ b/testprograms/call_str_example.c @@ -1,24 +1,28 @@ /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe -** All rights reserved. -** -** Authors: Christian Schaefer, Goetz Lindenmaier -** -** testprogram. +* All rights reserved. +* +* Authors: Christian Schaefer, Goetz Lindenmaier +* +* testprogram. */ -#include +# include +# include +# include "irvrfy.h" # include "irdump.h" # include "firm.h" /** -*** This file constructs the ir for the following pseudo-program: -*** -*** void f(char *); -*** -*** main() { -*** f("Hello world !"); -*** } +* This file constructs the ir for the following pseudo-program: +* +* void f(char *); +* +* void CALL_STR_EXAMPLE_main () { + f("Hello World\n"); +* } +* +* This program shall demonstrate how to represent string constants. **/ int main(int argc, char **argv) @@ -27,26 +31,40 @@ int main(int argc, char **argv) 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 *string_ptr; /* type for pointers to strings. */ + type *U8, *U8array, *string_ptr; /* type for pointers to strings. */ entity *ent; /* represents this method as entity of owner */ - ir_node *x, *const_str, *proc_ptr, *call; + 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; printf("\nCreating an IR graph: CALL_STR_EXAMPLE...\n"); /* init library */ - init_firm (); + init_firm (NULL); + + /* An unsinged 8 bit type */ + U8 = new_type_primitive (id_from_str("char", 4), mode_Bu); + /* An array containing unsigned 8 bit elements. */ + U8array = new_type_array (id_from_str ("char_arr", 8), 1, U8); + string_ptr = new_type_pointer (id_from_str ("ptr_to_string", 13), U8array); + + /* Make a global entity that represents the constant String. */ + const_str = new_entity(get_glob_type(), new_id_from_str("constStr"), U8array); + set_entity_variability(const_str, constant); + for (i = 0; i < strlen(str); i++) { + tarval *val = new_tarval_from_long(str[i], mode_Bu); + ir_node *con = new_Const(mode_Bu, val); + add_compound_ent_value(const_str, con, get_array_element_entity(U8array)); + } - string_ptr = new_type_pointer ( - id_from_str ("ptr_to_string", 13), - new_type_array (id_from_str ("char_arr", 8), 1, - new_type_primitive (id_from_str("char", 4), mode_c))); /* FIRM was designed for oo languages where all methods belong to a class. * 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. */ -#define METHODNAME "main" +#define METHODNAME "CALL_STR_EXAMPLE_main" #define NRARGS 0 #define NRES 0 owner = get_glob_type(); @@ -63,8 +81,9 @@ int main(int argc, char **argv) F_NRARGS, F_NRES); set_method_param_type(proc_called, 0, string_ptr); + /* Make the entity for main needed for a correct ir_graph. */ -#define ENTITYNAME "main" +#define ENTITYNAME "CALL_STR_EXAMPLE_main" ent = new_entity (owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), proc_main); @@ -75,20 +94,18 @@ int main(int argc, char **argv) #define NUM_OF_LOCAL_VARS 0 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS); - /* the string is entered in the constant table. const_str is a pointer to the string */ - const_str = new_Const (mode_p, tarval_p_from_str ("Hello world!")); + /* get the pointer to the string constant */ + str_addr = new_Const(mode_P, new_tarval_from_entity(const_str, mode_P)); /* get the pointer to the procedure from the class type */ - /* this is how a pointer to be fixed by the linker is represented after - lowering a Sel node. */ -#define FUNCTIONNAME "f" - proc_ptr = new_SymConst ((type_or_id_p)id_from_str (FUNCTIONNAME, strlen(FUNCTIONNAME)), + /* this is how a pointer to be fixed by the linker is represented. */ + proc_ptr = new_SymConst ((type_or_id_p)id_from_str (F_METHODNAME, strlen(F_METHODNAME)), linkage_ptr_info); /* call procedure set_a, first built array with parameters */ { ir_node *in[1]; - in[0] = const_str; + in[0] = str_addr; call = new_Call(get_store(), proc_ptr, 1, in, proc_called); } /* make the possible changes by the called method to memory visible */ @@ -119,6 +136,7 @@ int main(int argc, char **argv) printf("Done building the graph. Dumping it.\n"); dump_ir_block_graph (irg); + dump_all_types(); printf("Use xvcg to view this graph:\n"); printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");