changeset 42:9e4f9e20b8f1

add some examples.
author kent@teto.cr.ie.u-ryukyu.ac.jp
date Mon, 25 Jan 2010 17:13:59 +0900
parents c816ae4380d7
children a14cd9f25ac4
files CbC-examples/matrix/compute_power.c CbC-examples/matrix/matrix.c CbC-examples/matrix/matrix.h CbC-examples/parallel_check/c-int-double.c CbC-examples/parallel_check/c-int.c CbC-examples/parallel_check/c-struct.c CbC-examples/quicksort/Makefile CbC-examples/quicksort/mc/Makefile CbC-examples/return_check/test_return.c CbC-examples/return_check/typedeffed.c CbC-examples/return_check/variable_return_type.c
diffstat 11 files changed, 676 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/matrix/compute_power.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,107 @@
+#include <stdio.h>
+#include "matrix.h"
+
+void compute();
+double ** create_identity_matrix(int size);
+double ** multiply(double **a, double **b, int l, int m, int n);
+void print_matrix(double **a, int row, int col);
+
+int
+main(int argc, char **argv)
+{
+	compute();
+	return 0;
+}
+
+void
+compute()
+{
+	double **A, **B, **C;
+	A = create_identity_matrix(4);
+	B = create_identity_matrix(4);
+	printf("A = \n");
+	print_matrix(A, 4, 4);
+	printf("B = \n");
+	print_matrix(B, 4, 4);
+
+	C = multiply(A, B, 4,4,4);
+	printf("C = \n");
+	print_matrix(C, 4, 4);
+
+
+	free(A);
+	free(B);
+	free(C);
+}
+
+double **
+create_identity_matrix(int size)
+{
+	int i,j;
+	double **ret;
+	ret = create_matrix(sizeof(double), size, size);
+
+	for (j=0; j<size; j++) {
+		for (i=0; i<size; i++) {
+			ret[j][i] = 0;
+		}
+	}
+	for (i=0; i<size; i++) {
+		ret[i][i] = 1.0f;
+	}
+	return ret;
+}
+
+double **
+power_of_matrix(double **a, int n)
+{
+	int r,c,i;
+	double **result;
+	result = create_identity_matrix(n);
+
+	for (r=0; r<l; r++) {
+		for (c=0; c<n; c++) {
+			result[r][c] = 0;
+			for (i=0; i<m; i++) {
+				result[r][c] += a[r][i] * b[i][c];
+			}
+		}
+	}
+	return result;
+}
+
+double **
+multiply(double **a, double **b, int l, int m, int n)
+{
+	int r,c,i;
+	double **result;
+	result = create_matrix(sizeof(double), l, n);
+
+	for (r=0; r<l; r++) {
+		for (c=0; c<n; c++) {
+			result[r][c] = 0;
+			for (i=0; i<m; i++) {
+				result[r][c] += a[r][i] * b[i][c];
+			}
+		}
+	}
+	return result;
+}
+
+/** 表示用 */
+void
+print_matrix(double **a, int row, int col)
+{
+        int i,j;
+        for( j=0; j<row; j++){
+                printf("\t");
+                for( i=0; i<col; i++){
+                        printf("%6.2f ", a[j][i]);
+                }
+                printf("\n");
+        }
+}
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/matrix/matrix.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,21 @@
+#include "matrix.h"
+
+void
+*create_matrix(size_t size, int row, int col)
+{
+	int i;
+	void *body;
+	void **ret;
+
+	ret = malloc( size*row*col + sizeof(void*)*row );
+	if (ret==NULL) return NULL;
+
+	body = (void*)(ret+row);
+	for( i=0; i<row; i++){
+		ret[i] = body + size*col*i;
+	}
+	return ret;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/matrix/matrix.h	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,3 @@
+#include "stdlib.h"
+
+void *create_matrix(size_t size, int row, int col);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/parallel_check/c-int-double.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,46 @@
+#include<stdio.h>
+#include<math.h>
+
+#define dprint(f, args...) \
+	printf("in %s env=%p: "f, __FUNCTION__, __builtin_frame_address(1), ## args)
+
+int
+test(int a)
+{
+	return (int)pow(a, 2.0);
+}
+
+int
+callee(double a, double b, double c, int d)
+{
+	dprint("a=%d,b=%d,c=%d,d=%d\n", a,b,c,d);
+	return a+b+c+d;
+}
+
+int
+caller(int a, double b, double c, double d)
+{
+	int x;
+	double y,z,w;
+	//x = test(a);
+	//y = test(b);
+	//z = test(c);
+	//w = test(d);
+	x=a, y=b;
+	z=c, w=d;
+
+	return callee(y,z,w,x);
+	//return callee(b,c,d, a);
+}
+
+int
+main (int argc, char **argv)
+{
+	int r;
+	r = caller(11,22,33,44);
+	//r = caller(11,22,33,44, 55,66,77,88);
+	printf("r = %d\n", r);
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/parallel_check/c-int.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,71 @@
+#include<stdio.h>
+#define dprint(f, args...) \
+	printf("in %s env=%p: "f, __FUNCTION__, __builtin_frame_address(1), ## args)
+
+void
+callee(int a, int b, int c, int d)
+{
+	int r;
+	dprint("a=%d,b=%d,c=%d,d=%d\n", a,b,c,d);
+	r = a+b+c+d;
+	printf("r = %d\n", r);
+	return;
+}
+
+void
+caller1(int a, int b, int c, int d)
+{
+	int x,y,z,w;
+	x=a, y=b;
+	z=c, w=d;
+
+	callee(x,y,z,w);
+	return;
+}
+
+void
+caller2(int a, int b, int c, int d)
+{
+	int x,y,z,w;
+	x=a, y=b;
+	z=c, w=d;
+
+	callee(y,z,w,x);
+	return;
+}
+
+void
+caller3(int a, int b, int c, int d)
+{
+	callee(b,c,d,a);
+	return;
+}
+
+void
+caller4(int a, int b, int c, int d)
+{
+	callee(a+b,b+c,c+d,d+a);
+	return;
+}
+
+void
+caller5(int a, int b, int c, int d)
+{
+	int x,y,z,w;
+	x = a+b;
+	y = b+c;
+	z = c+d;
+	w = d+a;
+
+	callee(x,y,z,w);
+	return;
+}
+
+int
+main (int argc, char **argv)
+{
+	int r;
+	caller(11,22,33,44);
+	//r = caller(11,22,33,44, 55,66,77,88);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/parallel_check/c-struct.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,51 @@
+#include<stdio.h>
+#include<math.h>
+
+#define dprint(f, args...) \
+	printf("in %s env=%p: "f, __FUNCTION__, __builtin_frame_address(1), ## args)
+
+typedef struct {
+	int a;
+	double b;
+	char *c;
+} STRUCT;
+
+int
+test(int a)
+{
+	return (int)pow(a, 2.0);
+}
+
+
+int
+callee(int a, STRUCT s, int b)
+	/*  |-|----|-|  */
+{
+	dprint("a=%d,b=%d\n", a,b);
+	dprint("s.a=%d,s.b=%lf,s.c=%s\n", s.a, s.b, s.c);
+	return a+b+ s.a;
+}
+
+int
+caller(STRUCT s, int a, double b)
+	/*  |----|-|--|  */
+{
+	STRUCT s0;// = {44, 55.5, "aiueo2"};
+	//int a0 = 55;
+	//a0 = a;
+	s0 = s;
+
+	return callee(10, s0, 20);
+}
+
+int
+main (int argc, char **argv)
+{
+	int r;
+	STRUCT s = { 33, 44.4, "aiueo" };
+
+	r = caller(s, 11, 22.2);
+	//r = caller(11,22,33,44, 55,66,77,88);
+	printf("r = %d\n", r);
+}
+
--- a/CbC-examples/quicksort/Makefile	Mon Jan 25 16:58:29 2010 +0900
+++ b/CbC-examples/quicksort/Makefile	Mon Jan 25 17:13:59 2010 +0900
@@ -1,8 +1,8 @@
 
-CbCC=../../../build-gcc/INSTALL_DIR/bin/cbc-gcc
+CbCC=../../../build_gcc/INSTALL_DIR/bin/gcc
 
 #CC=gcc
-CC=../../../build-gcc/INSTALL_DIR/bin/cbc-gcc
+CC=../../../build_gcc/INSTALL_DIR/bin/gcc
 
 HEADERMAKER=../../CbC-scripts/make_headers.py2
 
--- a/CbC-examples/quicksort/mc/Makefile	Mon Jan 25 16:58:29 2010 +0900
+++ b/CbC-examples/quicksort/mc/Makefile	Mon Jan 25 17:13:59 2010 +0900
@@ -2,7 +2,7 @@
 CbCC=../../../../device/mc
 
 #CC=gcc
-CC=../../../../build-gcc/INSTALL_DIR/bin/cbc-gcc
+CC=../../../../build_gcc/INSTALL_DIR/bin/gcc
 
 HEADERMAKER=../../../CbC-scripts/make_headers.py2
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/return_check/test_return.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,89 @@
+#include<stdio.h>
+
+#if 0
+typedef float testtype;
+testtype good = 33.3f;
+testtype bad = 0.0f;
+void print_testtype(testtype t)
+{
+	printf("return value = %2.3f  good=%2.3f,bad=%2.3f\n", t,good,bad);
+}
+#elif 1
+typedef char testtype;
+testtype good = 33;
+testtype bad  = 0;
+void print_testtype(testtype t)
+{
+	printf("return value = %d, good=%d,bad=%d\n", t,good,bad);
+}
+#elif 0
+typedef double testtype;
+testtype good = 333.3;
+testtype bad  = 0.00;
+void print_testtype(testtype t)
+{
+	printf("return value = %3.3lf, good=%3.3lf,bad=%3.3lf\n", t,good,bad);
+}
+#elif 0
+typedef
+struct {
+	int a;
+	float b;
+	int c[4];
+} testtype;
+testtype good = {33, 33.3, {4,4,4,4}};
+testtype bad  = {0, 00.0, {0,0,0,0}};
+void print_testtype(testtype t)
+{
+	printf( "return value = {\n"
+			"	a = %d\n"
+			"	b = %2.3f\n"
+			"	c = { %d, %d, %d, %d }"
+			"}\n", t.a, t.b,
+			t.c[0],t.c[1],t.c[2],t.c[3]);
+}
+#else
+typedef int testtype;
+testtype good = 33;
+testtype bad = 0;
+void print_testtype(testtype t)
+{
+	printf("return value = %d,  good=%d,bad=%d\n", t,good,bad);
+}
+#endif
+
+typedef void (*RET_FUNC)(testtype, void *);
+
+void g(RET_FUNC func)
+{
+	func(good, NULL);
+}
+
+testtype f_cbc()
+{
+	//__label__ _cbc_exit0;
+	//int retval;
+	void *ret;
+
+	ret = _CbC_return;
+
+	printf("f0: fp = %p\n", __builtin_frame_address(0));
+	printf("__return_func = %p\n", ret);
+	g(ret);
+
+	printf("not good\n");
+	return bad;
+//_cbc_exit0:
+	//printf("f1: fp = 0x%x\n", __builtin_frame_address(0));
+	//return retval;
+}
+
+int main(int argc, char **argv)
+{
+	testtype t;
+	printf("main before: fp = %p\n", __builtin_frame_address(0));
+	t = f_cbc();
+	print_testtype(t);
+	printf("main after: fp = %p\n", __builtin_frame_address(0));
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/return_check/typedeffed.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,89 @@
+#include<stdio.h>
+
+#if 0
+typedef float testtype;
+testtype good = 33.3f;
+testtype bad = 0.0f;
+void print_testtype(testtype t)
+{
+	printf("return value = %2.3f  good=%2.3f,bad=%2.3f\n", t,good,bad);
+}
+#elif 1
+typedef char testtype;
+testtype good = 33;
+testtype bad  = 0;
+void print_testtype(testtype t)
+{
+	printf("return value = %d, good=%d,bad=%d\n", t,good,bad);
+}
+#elif 0
+typedef double testtype;
+testtype good = 333.3;
+testtype bad  = 0.00;
+void print_testtype(testtype t)
+{
+	printf("return value = %3.3lf, good=%3.3lf,bad=%3.3lf\n", t,good,bad);
+}
+#elif 0
+typedef
+struct {
+	int a;
+	float b;
+	int c[4];
+} testtype;
+testtype good = {33, 33.3, {4,4,4,4}};
+testtype bad  = {0, 00.0, {0,0,0,0}};
+void print_testtype(testtype t)
+{
+	printf( "return value = {\n"
+			"	a = %d\n"
+			"	b = %2.3f\n"
+			"	c = { %d, %d, %d, %d }"
+			"}\n", t.a, t.b,
+			t.c[0],t.c[1],t.c[2],t.c[3]);
+}
+#else
+typedef int testtype;
+testtype good = 33;
+testtype bad = 0;
+void print_testtype(testtype t)
+{
+	printf("return value = %d,  good=%d,bad=%d\n", t,good,bad);
+}
+#endif
+
+typedef void (*RET_FUNC)(testtype, void *);
+
+void g(RET_FUNC func)
+{
+	func(good, NULL);
+}
+
+testtype f_cbc()
+{
+	//__label__ _cbc_exit0;
+	//int retval;
+	void *ret;
+
+	ret = _CbC_return;
+
+	printf("f0: fp = %p\n", __builtin_frame_address(0));
+	printf("__return_func = %p\n", ret);
+	g(ret);
+
+	printf("not good\n");
+	return bad;
+//_cbc_exit0:
+	//printf("f1: fp = 0x%x\n", __builtin_frame_address(0));
+	//return retval;
+}
+
+int main(int argc, char **argv)
+{
+	testtype t;
+	printf("main before: fp = %p\n", __builtin_frame_address(0));
+	t = f_cbc();
+	print_testtype(t);
+	printf("main after: fp = %p\n", __builtin_frame_address(0));
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/return_check/variable_return_type.c	Mon Jan 25 17:13:59 2010 +0900
@@ -0,0 +1,196 @@
+#include<stdio.h>
+#include<assert.h>
+
+#define dprint(f, args...) \
+	fprintf(stdout, "in %s\t: "f, __FUNCTION__, ## args)
+
+
+/* for integer.  */
+int goodint = 33;
+int badint = 0;
+typedef void (*RETINT_FUNC)(int, void *);
+void g_int(RETINT_FUNC func)
+{
+	func(goodint, NULL);
+}
+int f_int()
+{
+	void *ret;
+
+	ret = _CbC_return;
+
+	dprint("fp = %p\n", __builtin_frame_address(0));
+	dprint("__return_func = %p\n", ret);
+	g_int(ret);
+	//goto g(ret);
+
+	dprint("not good\n");
+	return badint;
+}
+
+
+/* for double.  */
+double gooddouble = 333.3;
+double baddouble  = 0.00;
+typedef void (*RETDOUBLE_FUNC)(double, void *);
+void g_double(RETDOUBLE_FUNC func)
+{
+	func(gooddouble, NULL);
+}
+double f_double()
+{
+	void *ret;
+	ret = _CbC_return;
+
+	dprint("fp = %p\n", __builtin_frame_address(0));
+	dprint("__return_func = %p\n", ret);
+	g_double(ret);
+	//goto g_double(ret);
+
+	dprint("not good\n");
+	return baddouble;
+}
+
+/* for float.  */
+float goodfloat = 33.3f;
+float badfloat = 0.0f;
+typedef void (*RETFLOAT_FUNC)(float, void *);
+void g_float(RETFLOAT_FUNC func)
+{
+	func(goodfloat, NULL);
+}
+float f_float()
+{
+	void *ret;
+	ret = _CbC_return;
+
+	dprint("fp = %p\n", __builtin_frame_address(0));
+	dprint("__return_func = %p\n", ret);
+	g_float(ret);
+	//goto g_float(ret);
+
+	dprint("not good\n");
+	return badfloat;
+}
+
+/* for char.  */
+char goodchar = 33;
+char badchar  = 0;
+typedef void (*RETCHAR_FUNC)(char, void *);
+void g_char(RETCHAR_FUNC func)
+{
+	func(goodchar, NULL);
+}
+char f_char()
+{
+	void *ret;
+
+	ret = _CbC_return;
+
+	dprint("fp = %p\n", __builtin_frame_address(0));
+	dprint("__return_func = %p\n", ret);
+	g_char(ret);
+	//goto g(ret);
+
+	dprint("not good\n");
+	return badchar;
+}
+
+
+/* for struct.  */
+struct ifid {
+	int a;
+	float b;
+	int c[4];
+	double d;
+};
+struct ifid goodstruct = {33, 33.3, {4,4,4,4}, 333.333};
+struct ifid badstruct  = {0, 00.0, {0,0,0,0}, 0.0};
+typedef void (*RETSTRUCT_FUNC)(struct ifid, void *);
+void g_struct(RETSTRUCT_FUNC func)
+{
+	func(goodstruct, NULL);
+}
+struct ifid f_struct()
+{
+	void *ret;
+
+	ret = _CbC_return;
+
+	dprint("fp = %p\n", __builtin_frame_address(0));
+	dprint("__return_func = %p\n", ret);
+	g_struct(ret);
+	//goto g(ret);
+
+	dprint("not good\n");
+	return badstruct;
+}
+
+int main(int argc, char **argv)
+{
+	void *bptr;
+	int rint;
+	float rfloat;
+	double rdouble;
+	char rchar;
+	struct ifid rstruct;
+
+	bptr = __builtin_frame_address(0);
+
+	dprint("before int: fp = %p\n", __builtin_frame_address(0));
+	rint = f_int();
+	dprint("f_int = %d,  good=%d,bad=%d\n", rint,goodint,badint);
+
+	dprint("before float: fp = %p\n", __builtin_frame_address(0));
+	rfloat = f_float();
+	dprint("f_float = %3.3f, good=%3.3f,bad=%3.3f\n", rfloat,goodfloat,badfloat);
+	assert(bptr==__builtin_frame_address(0));
+
+	dprint("before double: fp = %p\n", __builtin_frame_address(0));
+	rdouble = f_double();
+	dprint("f_double = %3.3lf, good=%3.3lf,bad=%3.3lf\n", rdouble,gooddouble,baddouble);
+	assert(bptr==__builtin_frame_address(0));
+
+	dprint("before char: fp = %p\n", __builtin_frame_address(0));
+	rchar = f_char();
+	dprint("f_char = %d,  good=%d,bad=%d\n", rchar,goodchar,badchar);
+	assert(bptr==__builtin_frame_address(0));
+
+	dprint("before struct: fp = %p\n", __builtin_frame_address(0));
+	rstruct = f_struct();
+	dprint( "return value = {\n"
+			"	a = %d\n"
+			"	b = %2.3f\n"
+			"	c = { %d, %d, %d, %d }\n"
+			"	d = %3.3f\n"
+			"}\n", rstruct.a, rstruct.b,
+			rstruct.c[0],rstruct.c[1],rstruct.c[2],rstruct.c[3], rstruct.d);
+
+
+
+	dprint("end: fp = %p\n", __builtin_frame_address(0));
+
+	if (bptr!=__builtin_frame_address(0)) {
+		dprint("CbC_return failure!\n");
+		return 1;
+	}
+	if ( rint!=goodint 
+		|| rchar!=goodchar
+		|| (rfloat < goodfloat-0.01 || goodfloat+0.01 < rfloat)
+		|| (rdouble < gooddouble-0.01 || gooddouble+0.01 < rdouble)
+		|| rstruct.a!=goodstruct.a
+		|| (rstruct.b < goodstruct.b-0.01 || goodstruct.b+0.01 < rstruct.b)
+		|| (rstruct.d < goodstruct.d-0.01 || goodstruct.d+0.01 < rstruct.d)
+		|| rstruct.c[0]!=goodstruct.c[0]
+		|| rstruct.c[1]!=goodstruct.c[1]
+		|| rstruct.c[2]!=goodstruct.c[2]
+		|| rstruct.c[3]!=goodstruct.c[3] ) {
+		dprint("CbC_return failure!\n");
+		return 1;
+	}
+
+
+	dprint("CbC_return successful!\n");
+	return 0;
+}
+