From: Götz Lindenmaier Date: Wed, 30 Apr 2003 08:56:41 +0000 (+0000) Subject: Adapted example to proper semantics of string constants. X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=f9c6c0f9e3a68b9669ef7517522130aa89952573;p=libfirm Adapted example to proper semantics of string constants. changed makefile so that make run and make reference work again. [r1103] --- diff --git a/testprograms/Makefile.in b/testprograms/Makefile.in index 12bf7e34f..51f6112d5 100644 --- a/testprograms/Makefile.in +++ b/testprograms/Makefile.in @@ -25,7 +25,7 @@ SOURCES := Makefile.in \ GENFILES = %.vcg results.txt bin_EXAMPLES = $(CFILES:.c=) -run_bin_EXAMPLES = $(patsubst %.c,../%;,$(CFILES)) +run_bin_EXAMPLES = $(patsubst %.c,./%;,$(CFILES)) include $(topdir)/MakeRules @@ -47,9 +47,10 @@ $(bin_EXAMPLES): $(topdir)/libfirm.a run: $(run_bin_EXAMPLES) -test: all +test: realclean all ls >.ignore - rm -rf results;mkdir results;cd results;($(run_bin_EXAMPLES)) >run-result.txt + rm -rf results; mkdir results; ($(run_bin_EXAMPLES)) >results/run-result.txt; \ + mv *.vcg results; cd results; @($(DIFF) --exclude=CVS results $(REF_DIR) && \ echo Libfirm test successful ) || echo Libfirm test failed ; diff --git a/testprograms/call_str_example.c b/testprograms/call_str_example.c index 45efbecbb..2232baac6 100644 --- a/testprograms/call_str_example.c +++ b/testprograms/call_str_example.c @@ -18,9 +18,11 @@ * * void f(char *); * -* main() { -* f("Hello world !"); +* void CALL_STR_EXAMPLE_main () { + f("Hello World\n"); * } +* +* This program shall demonstrate how to represent string constants. **/ int main(int argc, char **argv) @@ -29,19 +31,33 @@ 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 (NULL); - 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_Bu))); + /* 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)); + } + /* 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 @@ -65,6 +81,7 @@ 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 "CALL_STR_EXAMPLE_main" ent = new_entity (owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), @@ -77,21 +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 */ - /* length 13 because of the terminating NULL character */ - const_str = new_Const (mode_P, new_tarval_from_str ("Hello world!", 13, mode_P)); + /* 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 */ @@ -122,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"); diff --git a/testprograms/ref-results/CALL_STR_EXAMPLE_main.vcg b/testprograms/ref-results/CALL_STR_EXAMPLE_main.vcg index 11a10ad9b..2cc53ed38 100644 --- a/testprograms/ref-results/CALL_STR_EXAMPLE_main.vcg +++ b/testprograms/ref-results/CALL_STR_EXAMPLE_main.vcg @@ -7,33 +7,33 @@ orientation: bottom_to_top classname 1: "Data" classname 2: "Block" classname 3: "Entity type"classname 4: "Entity owner"classname 5: "Method Param"classname 6: "Method Res"classname 7: "Super"classname 8: "Union"classname 9: "Points-to"classname 10: "Array Element Type"classname 11: "Overwrites"classname 12: "Member" -graph: { title: "n32" label: "32" status:clustered color:yellow -edge: {sourcename: "n32" targetname: "n33" label: "0" color: red} -node: {title: "n31" label: "End 31" color: blue} +graph: { title: "n45" label: "45" status:clustered color:yellow +edge: {sourcename: "n45" targetname: "n46" label: "0" color: red} +node: {title: "n44" label: "End 44" color: blue} } -graph: { title: "n34" label: "34" status:clustered color:yellow -edge: {sourcename: "n34" targetname: "n35" label: "0" color: red} -node: {title: "n33" label: "Return 33" } -edge: {sourcename: "n33" targetname: "n38" label: "0" color: blue} -node: {title: "n38" label: "ProjM 0 38" color: yellow} -edge: {sourcename: "n38" targetname: "n39" label: "0" color: blue} -node: {title: "n39" label: "Call 39" } -edge: {sourcename: "n39" targetname: "n42" label: "0" color: blue} -edge: {sourcename: "n39" targetname: "n41" label: "1" } -edge: {sourcename: "n39" targetname: "n40" label: "2" } -node: {title: "n41" label: "SymC f 41" } +graph: { title: "n47" label: "47" status:clustered color:yellow +edge: {sourcename: "n47" targetname: "n48" label: "0" color: red} +node: {title: "n46" label: "Return 46" } +edge: {sourcename: "n46" targetname: "n51" label: "0" color: blue} +node: {title: "n51" label: "ProjM 0 51" color: yellow} +edge: {sourcename: "n51" targetname: "n52" label: "0" color: blue} +node: {title: "n52" label: "Call 52" } +edge: {sourcename: "n52" targetname: "n55" label: "0" color: blue} +edge: {sourcename: "n52" targetname: "n54" label: "1" } +edge: {sourcename: "n52" targetname: "n53" label: "2" } +node: {title: "n54" label: "SymC f 54" } } -graph: { title: "n36" label: "36" status:clustered color:yellow -edge: {sourcename: "n36" targetname: "n35" label: "0" color: red} -node: {title: "n35" label: "ProjX 0 35" color: yellow} -edge: {sourcename: "n35" targetname: "n37" label: "0" color: red} -node: {title: "n37" label: "Start 37" color: blue} -node: {title: "n40" label: "!!TODO: BROKEN TEST CODEP 40" color: yellow} -node: {title: "n42" label: "ProjM 1 42" color: yellow} -edge: {sourcename: "n42" targetname: "n37" label: "0" color: blue} +graph: { title: "n49" label: "49" status:clustered color:yellow +edge: {sourcename: "n49" targetname: "n48" label: "0" color: red} +node: {title: "n48" label: "ProjX 0 48" color: yellow} +edge: {sourcename: "n48" targetname: "n50" label: "0" color: red} +node: {title: "n50" label: "Start 50" color: blue} +node: {title: "n53" label: "&(GlobalType_constStr)P 53" color: yellow} +node: {title: "n55" label: "ProjM 1 55" color: yellow} +edge: {sourcename: "n55" targetname: "n50" label: "0" color: blue} } -node: {title: "n46" label: "Bad 46" } +node: {title: "n59" label: "Bad 59" } }