changeset 9:18d2a590bc10 draft

add fibonacci
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sun, 03 Jun 2012 22:06:40 +0900
parents 5dc0fa7caa57
children 972515f10c1d
files ABP/Makefile ABP/receiver.c ABP/receiver2.c ABP/receiver3.c ABP/scheduler.c ABP/scheduler2.c ABP/sender.c ABP/sender2.c ABP/sender3.c fibonacci/fibonacci.c fibonacci/fibonacci.cbc
diffstat 11 files changed, 134 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/ABP/Makefile	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/Makefile	Sun Jun 03 22:06:40 2012 +0900
@@ -1,5 +1,7 @@
-CC=gcc
-MCC=mcc
+#CC=gcc
+#MCC=mcc
+CC=cbc-gcc-4.6.0
+MCC=cbc-gcc-4.6.0
 TARGET=abp abp2 sender2 sender3 receiver2
 MCCFLAGS=-s
 CFLAGS=-g -Wall
@@ -7,8 +9,9 @@
 .SUFFIXES:	.c .o
 
 .c.o:
-	$(MCC) $(MCCFLAGS) $<
-	$(CC) $(CFLAGS) -o $@ -c $(<:.c=.s)
+#	$(MCC) $(MCCFLAGS) $<
+#	$(CC) $(CFLAGS) -o $@ -c $(<:.c=.s)
+	$(CC) $(CFLAGS) -o $@ -c $<
 
 all: $(TARGET)
 
--- a/ABP/receiver.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/receiver.c	Sun Jun 03 22:06:40 2012 +0900
@@ -1,16 +1,20 @@
+#include <stdio.h>
+
 extern int comm_bit_s;  /* communication bit for sender */
 extern int comm_bit_r;  /* communication bit for receiver */
 extern char *comm_msg;  /* communication message */
 
-code r_receiveState(struct packet, int);
-code r_sendState(struct packet, int);
-
 struct packet {
 	int        bit;  /*alternating bit */
 	char     **msg;  /* message */
 	code (*next)();  /* next code segment */
 };
 
+extern code schedule(struct packet, int);
+
+code r_receiveState(struct packet, int);
+code r_sendState(struct packet, int);
+
 code r_sendState(struct packet pkt, int i)
 {
 	comm_bit_r = pkt.bit;
--- a/ABP/receiver2.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/receiver2.c	Sun Jun 03 22:06:40 2012 +0900
@@ -1,17 +1,17 @@
 int comm_bit;  /* communication bit */
 char *comm_msg;  /* communication message */
 
-code (*ret)(int);
-void *env;
-code receiveState(struct packet, int);
-code sendState(struct packet, int);
-
 struct packet {
 	int        bit;  /*alternating bit */
 	char     **msg;  /* message */
 	code (*next)();  /* next code segment */
 };
 
+code (*ret)(int);
+void *env;
+code receiveState(struct packet, int);
+code sendState(struct packet, int);
+
 char *msgs[6] = { "hoge1", "hoge2", "hoge3", "hoge4", "hoge5", 0 };
 
 code sendState(struct packet pkt, int i)
--- a/ABP/receiver3.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/receiver3.c	Sun Jun 03 22:06:40 2012 +0900
@@ -3,9 +3,7 @@
 extern char *comm_msg;  /* communication message */
 
 extern struct queue *list;
-
-code r_receiveState(struct packet*, int);
-code r_sendState(struct packet*, int);
+typedef struct queue *QueuePtr;
 
 struct packet {
 	int        bit;  /*alternating bit */
@@ -13,6 +11,10 @@
 	code (*next)();  /* next code segment */
 };
 
+extern code schedule(QueuePtr task_list, int i);
+code r_receiveState(struct packet*, int);
+code r_sendState(struct packet*, int);
+
 code r_sendState(struct packet *pkt, int i)
 {
 	comm_bit_r = pkt->bit;
--- a/ABP/scheduler.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/scheduler.c	Sun Jun 03 22:06:40 2012 +0900
@@ -1,8 +1,11 @@
+#define __environment _CbC_environment
+#define __return _CbC_return
+
 int comm_bit_s;  /* communication bit for sender */
 int comm_bit_r;  /* communication bit for receiver */
 char *comm_msg;  /* communication message */
 
-code (*ret)(int);
+code (*ret)(int,void*);
 void *env;
 
 struct packet {
@@ -28,14 +31,14 @@
 {
 	if (pkt.next == s_sendState || pkt.next == s_receiveState) {
 		spkt = pkt;
-		if (msgs[i] == 0) goto ret(0), env;
+		if (msgs[i] == 0) goto ret(0, env);
 		goto rpkt.next(rpkt, i);
 	}
 	if (pkt.next == r_sendState || pkt.next == r_receiveState) {
 		rpkt = pkt;
 		goto spkt.next(spkt, i);
 	}
-	goto ret(0), env;
+	goto ret(0, env);
 }
 
 int main(void)
@@ -49,8 +52,8 @@
 	rpkt.msg = msgs;
 	spkt.next = s_sendState;
 	rpkt.next = r_receiveState;
-	ret = return;
-	env = environment;
+	ret = _CbC_return;
+	env = _CbC_environment;
 
 	goto s_sendState(spkt, 0);
 	// goto r_receiveState(rpkt, 0);
--- a/ABP/scheduler2.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/scheduler2.c	Sun Jun 03 22:06:40 2012 +0900
@@ -1,10 +1,14 @@
 #include "queue.h"
 
+#define __environment _CbC_environment
+#define __return _CbC_return
+
+
 int comm_bit_s;  /* communication bit for sender */
 int comm_bit_r;  /* communication bit for receiver */
 char *comm_msg;  /* communication message */
 
-code (*ret)(int);
+code (*ret)(int, void*);
 void *env;
 code schedule1(int);
 code schedule2(int);
@@ -60,7 +64,8 @@
 code schedule(QueuePtr task_list, int i)
 {
 	QueuePtr q;
-	if (!task_list) goto ret(0), env;
+//	if (!task_list) goto ret(0), env;
+	if (!task_list) goto ret(0, env);
 	task_list = dequeue(task_list, &q);
 	list = task_list;
 	goto q->address(i);
@@ -74,7 +79,7 @@
 		q = new_queue(adrs[i], pkts[i]);
 		if (!q) {
 			printf("error!\n");
-			goto ret(0), env;
+			goto ret(0, env);
 		}
 		task_list = enqueue(task_list, q);
 		i++;
@@ -97,8 +102,8 @@
 	rpkt.msg = msgs;
 	spkt.next = s_sendState;
 	rpkt.next = r_receiveState;
-	ret = return;
-	env = environment;
+	ret = _CbC_return;
+	env = _CbC_environment;
 	/*
 	printf("schdule1: %lx\n", (unsigned long)schedule1);
 	printf("schdule2: %lx\n", (unsigned long)schedule2);
--- a/ABP/sender.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/sender.c	Sun Jun 03 22:06:40 2012 +0900
@@ -1,9 +1,9 @@
+#include <stdio.h>
+
 extern int comm_bit_s;  /* communication bit for sender */
 extern int comm_bit_r;  /* communication bit for receiver */
 extern char *comm_msg;  /* communication message */
 
-code s_sendState(struct packet, int);
-code s_receiveState(struct packet, int);
 
 struct packet {
 	int        bit;  /* alternating bit */
@@ -11,6 +11,11 @@
 	code (*next)();  /* next code segment */
 };
 
+extern code schedule(struct packet pkt, int i);
+
+code s_sendState(struct packet, int);
+code s_receiveState(struct packet, int);
+
 code s_receiveState(struct packet pkt, int i)
 {
 	/* correct bit */
--- a/ABP/sender2.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/sender2.c	Sun Jun 03 22:06:40 2012 +0900
@@ -1,17 +1,20 @@
+#define __environment _CbC_environment
+#define __return _CbC_return
+
 int comm_bit;  /* communication bit */
 char *comm_msg;  /* communication message */
 
-code (*ret)(int);
-void *env;
-code sendState(struct packet, int);
-code receiveState(struct packet, int);
-
 struct packet {
 	int        bit;  /* alternating bit */
 	char     **msg;  /* messages */
 	code (*next)();  /* next code segment */
 };
 
+code (*ret)(int,void*);
+void *env;
+code sendState(struct packet, int);
+code receiveState(struct packet, int);
+
 char *msgs[6] = { "hoge1", "hoge2", "hoge3", "hoge4", "hoge5", 0 };
 
 code receiveState(struct packet pkt, int i)
@@ -33,7 +36,8 @@
 
 code sendState(struct packet pkt, int i)
 {
-	if (pkt.msg[i] == 0) goto ret(0), env;
+//	if (pkt.msg[i] == 0) goto ret(0), env;
+	if (pkt.msg[i] == 0) goto ret(0, env);
 	comm_bit = pkt.bit;
 	comm_msg = pkt.msg[i];
 	pkt.next = receiveState;
@@ -52,7 +56,7 @@
 int main(void)
 {
 	struct packet pkt;
-	ret = return;
-	env = environment;
+	ret = _CbC_return;
+	env = _CbC_environment;
 	goto initSender(pkt, 0);
 }
--- a/ABP/sender3.c	Tue May 01 13:48:30 2012 +0900
+++ b/ABP/sender3.c	Sun Jun 03 22:06:40 2012 +0900
@@ -1,11 +1,11 @@
+#include <stdio.h>
+
 extern int comm_bit_s;  /* communication bit for sender */
 extern int comm_bit_r;  /* communication bit for receiver */
 extern char *comm_msg;  /* communication message */
 
 extern struct queue *list;
-
-code s_sendState(struct packet *, int i);
-code s_receiveState(struct packet *, int i);
+typedef struct queue *QueuePtr;
 
 struct packet {
 	int        bit;  /* alternating bit */
@@ -13,6 +13,10 @@
 	code (*next)();  /* next code segment */
 };
 
+extern code schedule(QueuePtr task_list, int i);
+code s_sendState(struct packet *, int i);
+code s_receiveState(struct packet *, int i);
+
 code s_receiveState(struct packet *pkt, int i)
 {
 	/* correct bit */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fibonacci/fibonacci.c	Sun Jun 03 22:06:40 2012 +0900
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int fibonacci(int num) {
+	if (num == 0 ) return 0;
+	else if (num == 1 ) return 1;
+	else if (num < 0 ) {
+		printf("please enter nutural number\n");
+		return 0;
+	} else {
+		return fibonacci(num-1) + fibonacci(num-2);
+	}
+}
+
+
+int main(int argc, char* argv[]) {
+	int num = atoi(argv[1]);
+	int result = fibonacci(num);
+	printf("fibonacci(%d) = %d\n",num,result);
+
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fibonacci/fibonacci.cbc	Sun Jun 03 22:06:40 2012 +0900
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+__code print(__rectype *p, int num, int count, int result, int prev) {
+	printf("fibonacci(%d) = %d\n",num,result);	
+	exit(0);
+}
+
+__code fibonacci(__rectype *p, int num,  int count, int result, int prev) {
+	int tmp;
+	if (count == 0) {
+		result += 0;
+		count++;
+	} else if (count == 1) {
+		result += 1;
+		count++;
+	} else if (count > 1){
+		tmp = prev;
+		prev = result;
+		result = result + tmp;
+		count++;
+	} else {
+		printf("please enter nutural number\n");
+		exit(0);
+	}
+	if (num < count)
+		goto p(fibonacci, num,  count, result, prev);
+	goto fibonacci(p, num, count, result, prev);
+
+}
+
+
+
+
+int main(int argc, char* argv[]) {
+	if (argc < 2) {
+		printf("usage: ./fibonacci number \n");
+		exit(0);
+	}
+	int num = atoi(argv[1]);
+	goto fibonacci(print, num, 0, 0, 0);
+	
+	return 0;
+}