changeset 41:087d7b61c86b

cbc cmake version
author kono
date Sat, 02 Mar 2019 19:01:03 +0900
parents 162d92edbb0a
children 997c54d8912c
files CMakeLists.txt src/Makefile src/console.c src/console.cbc src/context.h
diffstat 5 files changed, 1027 insertions(+), 459 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeLists.txt	Sat Mar 02 19:01:03 2019 +0900
@@ -0,0 +1,12 @@
+cmake_minimum_required(VERSION 3.3)
+
+# output compile log
+set(CMAKE_VERBOSE_MAKEFILE 1)
+
+# set compiler
+set(CMAKE_C_COMPILER $ENV{CBC_COMPILER})
+
+# compile option
+add_definitions("-Wall -g -O0")
+
+add_subdirectory(src)
--- a/src/Makefile	Sat Mar 02 18:37:03 2019 +0900
+++ b/src/Makefile	Sat Mar 02 19:01:03 2019 +0900
@@ -1,75 +1,178 @@
-# specify path to QEMU, installed with MacPorts 
-QEMU = qemu-system-arm
+# CMAKE generated file: DO NOT EDIT!
+# Generated by "Unix Makefiles" Generator, CMake Version 3.13
+
+# Default target executed when no arguments are given to make.
+default_target: all
+
+.PHONY : default_target
+
+# Allow only one "make -f Makefile2" at a time, but pass parallelism.
+.NOTPARALLEL:
+
+
+#=============================================================================
+# Special targets provided by cmake.
+
+# Disable implicit rules so canonical targets will work.
+.SUFFIXES:
+
 
-include makefile.inc
+# Remove some rules from gmake that .SUFFIXES does not remove.
+SUFFIXES =
+
+.SUFFIXES: .hpux_make_needs_suffix_list
+
+
+# Suppress display of executed commands.
+$(VERBOSE).SILENT:
+
 
-# link the libgcc.a for __aeabi_idiv. ARM has no native support for div
-LIBS = $(LIBGCC)
+# A target that is always out of date.
+cmake_force:
+
+.PHONY : cmake_force
+
+#=============================================================================
+# Set environment variables for the build.
+
+# The shell in which to execute make rules.
+SHELL = /bin/sh
+
+# The CMake executable.
+CMAKE_COMMAND = /usr/local/Cellar/cmake/3.13.4/bin/cmake
 
-OBJS = \
-	lib/string.o \
-	\
-	arm.o\
-	asm.o\
-	bio.o\
-	buddy.o\
-	console.o\
-	exec.o\
-	file.o\
-	fs.o\
-	log.o\
-	main.o\
-	memide.o\
-	pipe.o\
-	proc.o\
-	spinlock.o\
-	start.o\
-	swtch.o\
-	syscall.o\
-	sysfile.o\
-	sysproc.o\
-	trap_asm.o\
-	trap.o\
-	vm.o \
-	\
-	device/picirq.o \
-	device/timer.o \
-	device/uart.o
+# The command to remove a file.
+RM = /usr/local/Cellar/cmake/3.13.4/bin/cmake -E remove -f
+
+# Escaping for special characters.
+EQUALS = =
+
+# The top-level source directory on which CMake was run.
+CMAKE_SOURCE_DIR = /Users/one/src/xv6-gears/src
+
+# The top-level build directory on which CMake was run.
+CMAKE_BINARY_DIR = /Users/one/src/xv6-gears/src
+
+#=============================================================================
+# Targets provided globally by CMake.
+
+# Special rule for the target rebuild_cache
+rebuild_cache:
+	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
+	/usr/local/Cellar/cmake/3.13.4/bin/cmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
+.PHONY : rebuild_cache
+
+# Special rule for the target rebuild_cache
+rebuild_cache/fast: rebuild_cache
+
+.PHONY : rebuild_cache/fast
+
+# Special rule for the target edit_cache
+edit_cache:
+	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
+	/usr/local/Cellar/cmake/3.13.4/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
+.PHONY : edit_cache
+
+# Special rule for the target edit_cache
+edit_cache/fast: edit_cache
+
+.PHONY : edit_cache/fast
+
+# The main all target
+all: cmake_check_build_system
+	$(CMAKE_COMMAND) -E cmake_progress_start /Users/one/src/xv6-gears/src/CMakeFiles /Users/one/src/xv6-gears/src/CMakeFiles/progress.marks
+	$(MAKE) -f CMakeFiles/Makefile2 all
+	$(CMAKE_COMMAND) -E cmake_progress_start /Users/one/src/xv6-gears/src/CMakeFiles 0
+.PHONY : all
 
-KERN_OBJS = $(OBJS) entry.o
-kernel.elf: $(addprefix build/,$(KERN_OBJS)) kernel.ld build/initcode build/fs.img
-	cp -f build/initcode initcode
-	cp -f build/fs.img fs.img
-	$(call LINK_BIN, kernel.ld, kernel.elf, \
-		$(addprefix build/,$(KERN_OBJS)), \
-		initcode fs.img)
-	$(OBJDUMP) -S kernel.elf > kernel.asm
-	$(OBJDUMP) -t kernel.elf | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym
-	rm -f initcode fs.img
+# The main clean target
+clean:
+	$(MAKE) -f CMakeFiles/Makefile2 clean
+.PHONY : clean
+
+# The main clean target
+clean/fast: clean
+
+.PHONY : clean/fast
+
+# Prepare targets for installation.
+preinstall: all
+	$(MAKE) -f CMakeFiles/Makefile2 preinstall
+.PHONY : preinstall
+
+# Prepare targets for installation.
+preinstall/fast:
+	$(MAKE) -f CMakeFiles/Makefile2 preinstall
+.PHONY : preinstall/fast
 
-qemu: kernel.elf
-	@clear
-	@echo "Press Ctrl-A and then X to terminate QEMU session\n"
-	$(QEMU) -M versatilepb -m 128 -cpu arm1176  -nographic -kernel kernel.elf
+# clear depends
+depend:
+	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
+.PHONY : depend
+
+#=============================================================================
+# Target rules for targets named kernel
+
+# Build rule for target.
+kernel: cmake_check_build_system
+	$(MAKE) -f CMakeFiles/Makefile2 kernel
+.PHONY : kernel
+
+# fast build rule for target.
+kernel/fast:
+	$(MAKE) -f CMakeFiles/kernel.dir/build.make CMakeFiles/kernel.dir/build
+.PHONY : kernel/fast
+
+c/kernel-context.o: c/kernel-context.c.o
+
+.PHONY : c/kernel-context.o
 
-INITCODE_OBJ = initcode.o
-$(addprefix build/,$(INITCODE_OBJ)): initcode.S
-	$(call build-directory)
-	$(call AS_WITH, -nostdinc -I.)
+# target to build an object file
+c/kernel-context.c.o:
+	$(MAKE) -f CMakeFiles/kernel.dir/build.make CMakeFiles/kernel.dir/c/kernel-context.c.o
+.PHONY : c/kernel-context.c.o
+
+c/kernel-context.i: c/kernel-context.c.i
+
+.PHONY : c/kernel-context.i
 
-#initcode is linked into the kernel, it will be used to craft the first process
-build/initcode: $(addprefix build/,$(INITCODE_OBJ))
-	$(call LINK_INIT, -N -e start -Ttext 0)
-	$(call OBJCOPY_INIT)
-	$(OBJDUMP) -S $< > initcode.asm
+# target to preprocess a source file
+c/kernel-context.c.i:
+	$(MAKE) -f CMakeFiles/kernel.dir/build.make CMakeFiles/kernel.dir/c/kernel-context.c.i
+.PHONY : c/kernel-context.c.i
+
+c/kernel-context.s: c/kernel-context.c.s
+
+.PHONY : c/kernel-context.s
+
+# target to generate assembly for a file
+c/kernel-context.c.s:
+	$(MAKE) -f CMakeFiles/kernel.dir/build.make CMakeFiles/kernel.dir/c/kernel-context.c.s
+.PHONY : c/kernel-context.c.s
 
-build/fs.img:
-	make -C tools
-	make -C usr
+# Help Target
+help:
+	@echo "The following are some of the valid targets for this Makefile:"
+	@echo "... all (the default if no target is provided)"
+	@echo "... clean"
+	@echo "... depend"
+	@echo "... rebuild_cache"
+	@echo "... edit_cache"
+	@echo "... kernel"
+	@echo "... c/kernel-context.o"
+	@echo "... c/kernel-context.i"
+	@echo "... c/kernel-context.s"
+.PHONY : help
 
-clean: 
-	rm -rf build
-	rm -f *.o *.d *.asm *.sym vectors.S bootblock entryother \
-	initcode initcode.out kernel xv6.img fs.img kernel.elf memfs
-	make -C tools clean
-	make -C usr clean
+
+
+#=============================================================================
+# Special targets to cleanup operation of make.
+
+# Special rule to run CMake to check the build system integrity.
+# No rule that depends on this can have commands that come from listfiles
+# because they might be regenerated.
+cmake_check_build_system:
+	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
+.PHONY : cmake_check_build_system
+
--- a/src/console.c	Sat Mar 02 18:37:03 2019 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,393 +0,0 @@
-// Console input and output.
-// Input is from the keyboard or serial port.
-// Output is written to the screen and serial port.
-
-#include "types.h"
-#include "defs.h"
-#include "param.h"
-#include "spinlock.h"
-#include "fs.h"
-#include "file.h"
-#include "memlayout.h"
-#include "mmu.h"
-#include "proc.h"
-
-__code cbc_consoleread1 ();
-__code cbc_consoleread2 ();
-
-static void consputc (int);
-
-static int panicked = 0;
-
-static struct {
-    struct spinlock lock;
-    int locking;
-} cons;
-
-static void printint (int xx, int base, int sign)
-{
-    static char digits[] = "0123456789abcdef";
-    char buf[16];
-    int i;
-    uint x;
-
-    if (sign && (sign = xx < 0)) {
-        x = -xx;
-    } else {
-        x = xx;
-    }
-
-    i = 0;
-
-    do {
-        buf[i++] = digits[x % base];
-    } while ((x /= base) != 0);
-
-    if (sign) {
-        buf[i++] = '-';
-    }
-
-    while (--i >= 0) {
-        consputc(buf[i]);
-    }
-}
-//PAGEBREAK: 50
-
-// Print to the console. only understands %d, %x, %p, %s.
-void cprintf (char *fmt, ...)
-{
-    int i, c, locking;
-    uint *argp;
-    char *s;
-
-    locking = cons.locking;
-
-    if (locking) {
-        acquire(&cons.lock);
-    }
-
-    if (fmt == 0) {
-        panic("null fmt");
-    }
-
-    argp = (uint*) (void*) (&fmt + 1);
-
-    for (i = 0; (c = fmt[i] & 0xff) != 0; i++) {
-        if (c != '%') {
-            consputc(c);
-            continue;
-        }
-
-        c = fmt[++i] & 0xff;
-
-        if (c == 0) {
-            break;
-        }
-
-        switch (c) {
-        case 'd':
-            printint(*argp++, 10, 1);
-            break;
-
-        case 'x':
-        case 'p':
-            printint(*argp++, 16, 0);
-            break;
-
-        case 's':
-            if ((s = (char*) *argp++) == 0) {
-                s = "(null)";
-            }
-
-            for (; *s; s++) {
-                consputc(*s);
-            }
-            break;
-
-        case '%':
-            consputc('%');
-            break;
-
-        default:
-            // Print unknown % sequence to draw attention.
-            consputc('%');
-            consputc(c);
-            break;
-        }
-    }
-
-    if (locking) {
-        release(&cons.lock);
-    }
-}
-
-__code cbc_panic (char *s)
-{
-    cli();
-
-    cons.locking = 0;
-
-    cprintf("cpu%d: panic: ", cpu->id);
-
-    show_callstk(s);
-    panicked = 1; // freeze other CPU
-
-    while (1)
-        ;
-}
-
-void panic (char *s)
-{
-    cli();
-
-    cons.locking = 0;
-
-    cprintf("cpu%d: panic: ", cpu->id);
-
-    show_callstk(s);
-    panicked = 1; // freeze other CPU
-
-    while (1)
-        ;
-}
-
-//PAGEBREAK: 50
-#define BACKSPACE 0x100
-#define CRTPORT 0x3d4
-
-void consputc (int c)
-{
-    if (panicked) {
-        cli();
-        while (1)
-            ;
-    }
-
-    if (c == BACKSPACE) {
-        uartputc('\b');
-        uartputc(' ');
-        uartputc('\b');
-    } else {
-        uartputc(c);
-    }
-
-    // cgaputc(c);
-}
-
-#define INPUT_BUF 512
-struct {
-    struct spinlock lock;
-    char buf[INPUT_BUF];
-    uint r;  // Read index
-    uint w;  // Write index
-    uint e;  // Edit index
-} input;
-
-#define C(x)  ((x)-'@')  // Control-x
-void consoleintr (int (*getc) (void))
-{
-    int c;
-
-    acquire(&input.lock);
-
-    while ((c = getc()) >= 0) {
-        switch (c) {
-        case C('P'):  // Process listing.
-            procdump();
-            break;
-
-        case C('U'):  // Kill line.
-            while ((input.e != input.w) && (input.buf[(input.e - 1) % INPUT_BUF] != '\n')) {
-                input.e--;
-                consputc(BACKSPACE);
-            }
-
-            break;
-
-        case C('H'):
-        case '\x7f':  // Backspace
-            if (input.e != input.w) {
-                input.e--;
-                consputc(BACKSPACE);
-            }
-
-            break;
-
-        default:
-            if ((c != 0) && (input.e - input.r < INPUT_BUF)) {
-                c = (c == '\r') ? '\n' : c;
-
-                input.buf[input.e++ % INPUT_BUF] = c;
-                consputc(c);
-
-                if (c == '\n' || c == C('D') || input.e == input.r + INPUT_BUF) {
-                    input.w = input.e;
-                    wakeup(&input.r);
-                }
-            }
-
-            break;
-        }
-    }
-
-    release(&input.lock);
-}
-
-__code cbc_consoleread2 ()
-{
-    struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
-    __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
-    if (input.r == input.w) {
-        if (proc->killed) {
-            release(&input.lock);
-            ilock(ip);
-            goto next(-1);
-        }
-       goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
-    }
-    goto cbc_consoleread1();
-}
-
-__code cbc_consoleread1 ()
-{
-    int cont = 1;
-    int n = proc->cbc_arg.cbc_console_arg.n;
-    int target = proc->cbc_arg.cbc_console_arg.target;
-    char* dst = proc->cbc_arg.cbc_console_arg.dst;
-    struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
-    __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
-    
-    int c = input.buf[input.r++ % INPUT_BUF];
-
-    if (c == C('D')) {  // EOF
-        if (n < target) {
-            // Save ^D for next time, to make sure
-            // caller gets a 0-byte result.
-            input.r--;  
-        }
-        cont = 0;
-    }
-
-    *dst++ = c;
-    --n;
-
-    if (c == '\n') {
-        cont = 0;
-    }
-
-    if (cont == 1) {
-        if (n > 0) {
-            proc->cbc_arg.cbc_console_arg.n = n;
-            proc->cbc_arg.cbc_console_arg.target = target;
-            proc->cbc_arg.cbc_console_arg.dst = dst;
-            proc->cbc_arg.cbc_console_arg.ip = ip;
-            proc->cbc_arg.cbc_console_arg.next = next;
-            goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
-        }
-    }
-    
-    release(&input.lock);
-    ilock(ip);
-
-    goto next(target - n);
-}
-
-__code cbc_consoleread (struct inode *ip, char *dst, int n, __code(*next)(int ret))
-{
-    uint target;
-
-    iunlock(ip);
-
-    target = n;
-    acquire(&input.lock);
-
-    if (n > 0) {
-        proc->cbc_arg.cbc_console_arg.n = n;
-        proc->cbc_arg.cbc_console_arg.target = target;
-    	proc->cbc_arg.cbc_console_arg.dst = dst;
-	    proc->cbc_arg.cbc_console_arg.ip = ip;
-	    proc->cbc_arg.cbc_console_arg.next = next;
-        goto cbc_consoleread2();    
-    }
-	goto cbc_consoleread1();
-}
-
-int consoleread (struct inode *ip, char *dst, int n)
-{
-    uint target;
-    int c;
-
-    iunlock(ip);
-
-    target = n;
-    acquire(&input.lock);
-
-    while (n > 0) {
-        while (input.r == input.w) {
-            if (proc->killed) {
-                release(&input.lock);
-                ilock(ip);
-                return -1;
-            }
-
-            sleep(&input.r, &input.lock);
-        }
-
-        c = input.buf[input.r++ % INPUT_BUF];
-
-        if (c == C('D')) {  // EOF
-            if (n < target) {
-                // Save ^D for next time, to make sure
-                // caller gets a 0-byte result.
-                input.r--;
-            }
-
-            break;
-        }
-
-        *dst++ = c;
-        --n;
-
-        if (c == '\n') {
-            break;
-        }
-    }
-
-    release(&input.lock);
-    ilock(ip);
-
-    return target - n;
-}
-
-int consolewrite (struct inode *ip, char *buf, int n)
-{
-    int i;
-
-    iunlock(ip);
-
-    acquire(&cons.lock);
-
-    for (i = 0; i < n; i++) {
-        consputc(buf[i] & 0xff);
-    }
-
-    release(&cons.lock);
-
-    ilock(ip);
-
-    return n;
-}
-
-void consoleinit (void)
-{
-    initlock(&cons.lock, "console");
-    initlock(&input.lock, "input");
-
-    devsw[CONSOLE].write = consolewrite;
-    devsw[CONSOLE].read = consoleread;
-    //cbc_devsw[CONSOLE].write = cbc_consolewrite;
-    cbc_devsw[CONSOLE].read = cbc_consoleread;
-
-    cons.locking = 1;
-}
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/console.cbc	Sat Mar 02 19:01:03 2019 +0900
@@ -0,0 +1,393 @@
+// Console input and output.
+// Input is from the keyboard or serial port.
+// Output is written to the screen and serial port.
+
+#include "types.h"
+#include "defs.h"
+#include "param.h"
+#include "spinlock.h"
+#include "fs.h"
+#include "file.h"
+#include "memlayout.h"
+#include "mmu.h"
+#include "proc.h"
+
+__code cbc_consoleread1 ();
+__code cbc_consoleread2 ();
+
+static void consputc (int);
+
+static int panicked = 0;
+
+static struct {
+    struct spinlock lock;
+    int locking;
+} cons;
+
+static void printint (int xx, int base, int sign)
+{
+    static char digits[] = "0123456789abcdef";
+    char buf[16];
+    int i;
+    uint x;
+
+    if (sign && (sign = xx < 0)) {
+        x = -xx;
+    } else {
+        x = xx;
+    }
+
+    i = 0;
+
+    do {
+        buf[i++] = digits[x % base];
+    } while ((x /= base) != 0);
+
+    if (sign) {
+        buf[i++] = '-';
+    }
+
+    while (--i >= 0) {
+        consputc(buf[i]);
+    }
+}
+//PAGEBREAK: 50
+
+// Print to the console. only understands %d, %x, %p, %s.
+void cprintf (char *fmt, ...)
+{
+    int i, c, locking;
+    uint *argp;
+    char *s;
+
+    locking = cons.locking;
+
+    if (locking) {
+        acquire(&cons.lock);
+    }
+
+    if (fmt == 0) {
+        panic("null fmt");
+    }
+
+    argp = (uint*) (void*) (&fmt + 1);
+
+    for (i = 0; (c = fmt[i] & 0xff) != 0; i++) {
+        if (c != '%') {
+            consputc(c);
+            continue;
+        }
+
+        c = fmt[++i] & 0xff;
+
+        if (c == 0) {
+            break;
+        }
+
+        switch (c) {
+        case 'd':
+            printint(*argp++, 10, 1);
+            break;
+
+        case 'x':
+        case 'p':
+            printint(*argp++, 16, 0);
+            break;
+
+        case 's':
+            if ((s = (char*) *argp++) == 0) {
+                s = "(null)";
+            }
+
+            for (; *s; s++) {
+                consputc(*s);
+            }
+            break;
+
+        case '%':
+            consputc('%');
+            break;
+
+        default:
+            // Print unknown % sequence to draw attention.
+            consputc('%');
+            consputc(c);
+            break;
+        }
+    }
+
+    if (locking) {
+        release(&cons.lock);
+    }
+}
+
+__code cbc_panic (char *s)
+{
+    cli();
+
+    cons.locking = 0;
+
+    cprintf("cpu%d: panic: ", cpu->id);
+
+    show_callstk(s);
+    panicked = 1; // freeze other CPU
+
+    while (1)
+        ;
+}
+
+void panic (char *s)
+{
+    cli();
+
+    cons.locking = 0;
+
+    cprintf("cpu%d: panic: ", cpu->id);
+
+    show_callstk(s);
+    panicked = 1; // freeze other CPU
+
+    while (1)
+        ;
+}
+
+//PAGEBREAK: 50
+#define BACKSPACE 0x100
+#define CRTPORT 0x3d4
+
+void consputc (int c)
+{
+    if (panicked) {
+        cli();
+        while (1)
+            ;
+    }
+
+    if (c == BACKSPACE) {
+        uartputc('\b');
+        uartputc(' ');
+        uartputc('\b');
+    } else {
+        uartputc(c);
+    }
+
+    // cgaputc(c);
+}
+
+#define INPUT_BUF 512
+struct {
+    struct spinlock lock;
+    char buf[INPUT_BUF];
+    uint r;  // Read index
+    uint w;  // Write index
+    uint e;  // Edit index
+} input;
+
+#define C(x)  ((x)-'@')  // Control-x
+void consoleintr (int (*getc) (void))
+{
+    int c;
+
+    acquire(&input.lock);
+
+    while ((c = getc()) >= 0) {
+        switch (c) {
+        case C('P'):  // Process listing.
+            procdump();
+            break;
+
+        case C('U'):  // Kill line.
+            while ((input.e != input.w) && (input.buf[(input.e - 1) % INPUT_BUF] != '\n')) {
+                input.e--;
+                consputc(BACKSPACE);
+            }
+
+            break;
+
+        case C('H'):
+        case '\x7f':  // Backspace
+            if (input.e != input.w) {
+                input.e--;
+                consputc(BACKSPACE);
+            }
+
+            break;
+
+        default:
+            if ((c != 0) && (input.e - input.r < INPUT_BUF)) {
+                c = (c == '\r') ? '\n' : c;
+
+                input.buf[input.e++ % INPUT_BUF] = c;
+                consputc(c);
+
+                if (c == '\n' || c == C('D') || input.e == input.r + INPUT_BUF) {
+                    input.w = input.e;
+                    wakeup(&input.r);
+                }
+            }
+
+            break;
+        }
+    }
+
+    release(&input.lock);
+}
+
+__code cbc_consoleread2 ()
+{
+    struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
+    __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
+    if (input.r == input.w) {
+        if (proc->killed) {
+            release(&input.lock);
+            ilock(ip);
+            goto next(-1);
+        }
+       goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
+    }
+    goto cbc_consoleread1();
+}
+
+__code cbc_consoleread1 ()
+{
+    int cont = 1;
+    int n = proc->cbc_arg.cbc_console_arg.n;
+    int target = proc->cbc_arg.cbc_console_arg.target;
+    char* dst = proc->cbc_arg.cbc_console_arg.dst;
+    struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
+    __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
+    
+    int c = input.buf[input.r++ % INPUT_BUF];
+
+    if (c == C('D')) {  // EOF
+        if (n < target) {
+            // Save ^D for next time, to make sure
+            // caller gets a 0-byte result.
+            input.r--;  
+        }
+        cont = 0;
+    }
+
+    *dst++ = c;
+    --n;
+
+    if (c == '\n') {
+        cont = 0;
+    }
+
+    if (cont == 1) {
+        if (n > 0) {
+            proc->cbc_arg.cbc_console_arg.n = n;
+            proc->cbc_arg.cbc_console_arg.target = target;
+            proc->cbc_arg.cbc_console_arg.dst = dst;
+            proc->cbc_arg.cbc_console_arg.ip = ip;
+            proc->cbc_arg.cbc_console_arg.next = next;
+            goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
+        }
+    }
+    
+    release(&input.lock);
+    ilock(ip);
+
+    goto next(target - n);
+}
+
+__code cbc_consoleread (struct inode *ip, char *dst, int n, __code(*next)(int ret))
+{
+    uint target;
+
+    iunlock(ip);
+
+    target = n;
+    acquire(&input.lock);
+
+    if (n > 0) {
+        proc->cbc_arg.cbc_console_arg.n = n;
+        proc->cbc_arg.cbc_console_arg.target = target;
+    	proc->cbc_arg.cbc_console_arg.dst = dst;
+	    proc->cbc_arg.cbc_console_arg.ip = ip;
+	    proc->cbc_arg.cbc_console_arg.next = next;
+        goto cbc_consoleread2();    
+    }
+	goto cbc_consoleread1();
+}
+
+int consoleread (struct inode *ip, char *dst, int n)
+{
+    uint target;
+    int c;
+
+    iunlock(ip);
+
+    target = n;
+    acquire(&input.lock);
+
+    while (n > 0) {
+        while (input.r == input.w) {
+            if (proc->killed) {
+                release(&input.lock);
+                ilock(ip);
+                return -1;
+            }
+
+            sleep(&input.r, &input.lock);
+        }
+
+        c = input.buf[input.r++ % INPUT_BUF];
+
+        if (c == C('D')) {  // EOF
+            if (n < target) {
+                // Save ^D for next time, to make sure
+                // caller gets a 0-byte result.
+                input.r--;
+            }
+
+            break;
+        }
+
+        *dst++ = c;
+        --n;
+
+        if (c == '\n') {
+            break;
+        }
+    }
+
+    release(&input.lock);
+    ilock(ip);
+
+    return target - n;
+}
+
+int consolewrite (struct inode *ip, char *buf, int n)
+{
+    int i;
+
+    iunlock(ip);
+
+    acquire(&cons.lock);
+
+    for (i = 0; i < n; i++) {
+        consputc(buf[i] & 0xff);
+    }
+
+    release(&cons.lock);
+
+    ilock(ip);
+
+    return n;
+}
+
+void consoleinit (void)
+{
+    initlock(&cons.lock, "console");
+    initlock(&input.lock, "input");
+
+    devsw[CONSOLE].write = consolewrite;
+    devsw[CONSOLE].read = consoleread;
+    //cbc_devsw[CONSOLE].write = cbc_consolewrite;
+    cbc_devsw[CONSOLE].read = cbc_consoleread;
+
+    cons.locking = 1;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/context.h	Sat Mar 02 19:01:03 2019 +0900
@@ -0,0 +1,453 @@
+/* Context definition for llrb example */
+#ifndef CONTEXT_H
+#define CONTEXT_H
+#include <stdlib.h>
+#include <pthread.h>
+#ifdef USE_CUDAWorker
+#include <cuda.h>
+#include <driver_types.h>
+#include <cuda_runtime.h>
+#include "helper_cuda.h"
+#endif
+
+#define ALLOCATE_SIZE 20000000
+#define NEW(type) (type*)(calloc(1, sizeof(type)))
+#define NEWN(n, type) (type*)(calloc(n, sizeof(type)))
+
+#define ALLOC_DATA(context, dseg) ({\
+    Meta* meta = (Meta*)context->heap;\
+    meta->type = D_##dseg;\
+    meta->size = sizeof(dseg);\
+    meta->len = 1;\
+    context->heap += sizeof(Meta);\
+    context->data[D_##dseg] = context->heap; context->heap += sizeof(dseg); (dseg *)context->data[D_##dseg]; })
+
+#define ALLOC_DATA_TYPE(context, dseg, t) ({\
+    Meta* meta = (Meta*)context->heap;\
+    meta->type = D_##t;\
+    meta->size = sizeof(t);\
+    meta->len = 1;\
+    context->heap += sizeof(Meta);\
+    context->data[D_##dseg] = context->heap; context->heap += sizeof(t); (t *)context->data[D_##dseg]; })
+
+#define ALLOCATE(context, t) ({ \
+    Meta* meta = (Meta*)context->heap;\
+    context->heap += sizeof(Meta);\
+    union Data* data = context->heap; \
+    context->heap += sizeof(t); \
+    meta->type = D_##t; \
+    meta->size = sizeof(t);     \
+    meta->len = 1;\
+    data; })
+
+#define ALLOCATE_ARRAY(context, t, length) ({ \
+    Meta* meta = (Meta*)context->heap;\
+    context->heap += sizeof(Meta);\
+    union Data* data = context->heap; \
+    context->heap += sizeof(t)*length; \
+    meta->type = D_##t; \
+    meta->size = sizeof(t)*length; \
+    meta->len = length; \
+    data;   })
+
+#define ALLOCATE_PTR_ARRAY(context, dseg, length) ({\
+    Meta* meta = (Meta*)context->heap;\
+    context->heap += sizeof(Meta);\
+    union Data* data = context->heap; \
+    context->heap += sizeof(dseg *)*length; \
+    meta->type = D_##dseg; \
+    meta->size = sizeof(dseg *)*length; \
+    meta->len = length; \
+    data; })
+
+#define ALLOCATE_DATA_GEAR(context, t) ({ \
+        union Data* data = ALLOCATE(context, t); \
+        Meta* meta = GET_META(data); \
+        meta->wait = createSynchronizedQueue(context); \
+        data; })
+
+#define ALLOC(context, t) (&ALLOCATE(context, t)->t)
+
+#define GET_META(dseg) ((Meta*)(((void*)dseg) - sizeof(Meta)))
+#define GET_TYPE(dseg) (GET_META(dseg)->type)
+#define GET_SIZE(dseg) (GET_META(dseg)->size)
+#define GET_LEN(dseg) (GET_META(dseg)->len)
+#define GET_WAIT_LIST(dseg) (GET_META(dseg)->wait)
+
+#define Gearef(context, t) (&(context)->data[D_##t]->t)
+
+// (SingleLinkedStack *)context->data[D_Stack]->Stack.stack->Stack.stack
+
+#define GearImpl(context, intf, name) (Gearef(context, intf)->name->intf.name)
+
+#include "c/enumCode.h"
+
+enum Relational {
+    EQ,
+    GT,
+    LT,
+};
+
+#include "c/enumData.h"
+
+struct Context {
+    enum Code next;
+    struct Worker* worker;
+    struct TaskManager* taskManager;
+    int codeNum;
+    __code (**code) (struct Context*);
+    union Data **data;
+    void* heapStart;
+    void* heap;
+    long heapLimit;
+    int dataNum;
+
+    // task parameter
+    int idgCount; //number of waiting dataGear
+    int idg;
+    int maxIdg;
+    int odg;
+    int maxOdg;
+    int gpu; // GPU task
+    struct Context* task;
+    struct Element* taskList;
+#ifdef USE_CUDAWorker
+    int num_exec;
+    CUmodule module;
+    CUfunction function;
+#endif
+    /* multi dimension parameter */
+    int iterate;
+    struct Iterator* iterator;
+    enum Code before;
+};
+
+typedef int Int;
+#ifndef USE_CUDAWorker
+typedef unsigned long long CUdeviceptr;
+#endif
+union Data {
+    struct Meta {
+        enum DataType type;
+        long size;
+        long len;
+        struct Queue* wait; // tasks waiting this dataGear
+    } Meta;
+    struct Context Context;
+    struct Timer {
+        union Data* timer;
+        enum Code start;
+        enum Code end;
+        enum Code next;
+    } Timer;
+    struct TimerImpl {
+        double time;
+    } TimerImpl;
+    struct LoopCounter {
+        int i;
+    } LoopCounter;
+    struct TaskManager {
+        union Data* taskManager;
+        enum Code spawn;      // start NEW context on the worker
+        enum Code spawnTasks; // start NEW tasks on the worker
+        enum Code shutdown;
+        enum Code incrementTaskCount;
+        enum Code decrementTaskCount;
+        enum Code next;
+        enum Code next1;
+        enum Code setWaitTask;
+        struct Context* task;
+        struct Element* taskList;
+        union Data* data;
+    } TaskManager;
+    struct TaskManagerImpl {
+        enum Code next;
+        int numWorker;
+        int sendCPUWorkerIndex;
+        int sendGPUWorkerIndex;
+        int taskCount;
+        pthread_mutex_t mutex;
+        struct Queue* activeQueue;
+        struct Worker** workers;
+        struct Element* taskList;
+        int loopCounter;
+        int cpu;
+        int gpu;
+        int io;
+        int maxCPU;
+    } TaskManagerImpl;
+    struct Worker {
+        union Data* worker;
+        enum Code taskReceive;
+        enum Code shutdown;
+        enum Code next;
+        struct Queue* tasks;
+        pthread_t thread;
+        struct TaskManager* taskManager;
+        struct Context* task;
+    } Worker;
+    struct CPUWorker {
+        pthread_mutex_t mutex;
+        pthread_cond_t cond;
+        struct Context* context;
+        int id;
+        int loopCounter;
+    } CPUWorker;
+#ifdef USE_CUDAWorker
+    struct CUDAWorker {
+        CUdevice device;
+        CUcontext cuCtx;
+        struct Context* context;
+        int id;
+        int loopCounter;
+        int deviceNum;
+        struct Queue* tasks;
+        int runFlag;
+        enum Code next;
+        int numStream;
+        struct Executor* executor;
+        CUstream *stream;
+    } CUDAWorker;
+#else
+    struct CUDAWorker {
+    } CUDAWorker;
+#endif
+    struct Main {
+        enum Code code;
+        enum Code next;
+        struct Queue* args;
+    } Main;
+    // Queue Interface
+    struct Queue {
+        union Data* queue;
+        union Data* data;
+        enum Code whenEmpty;
+        enum Code clear;
+        enum Code put;
+        enum Code take;
+        enum Code isEmpty;
+        enum Code next;
+    } Queue;
+    struct SingleLinkedQueue {
+        struct Element* top;
+        struct Element* last;
+    } SingleLinkedQueue;
+    struct SynchronizedQueue {
+        struct Element* top;
+        struct Element* last;
+        struct Atomic* atomic;
+    } SynchronizedQueue;
+    // Stack Interface
+    struct Stack {
+        union Data* stack;
+        union Data* data;
+        union Data* data1;
+        enum Code whenEmpty;
+        enum Code clear;
+        enum Code push;
+        enum Code pop;
+        enum Code pop2;
+        enum Code isEmpty;
+        enum Code get;
+        enum Code get2;
+        enum Code next;
+    } Stack;
+    // Stack implementations
+    struct SingleLinkedStack {
+        struct Element* top;
+    } SingleLinkedStack;
+    struct ArrayStack {
+        int size;
+        int limit;
+        struct Element* array;
+    } ArrayStack;
+    // Stack implementation end
+    struct Element {
+        union Data* data;
+        struct Element* next;
+    } Element;
+    struct Array {
+        int prefix;
+        Int* array;
+    } Array;
+    struct Tree {
+        union Data* tree;
+        struct Node* node;
+        enum Code put;
+        enum Code get;
+        enum Code remove;
+        enum Code clear;
+        enum Code next;
+    } Tree;
+    struct RedBlackTree {
+        struct Node* root;
+        struct Node* current; // reading node of original tree
+        struct Node* previous; // parent of reading node of original tree
+        struct Node* newNode; // writing node of new tree
+        struct Node* parent;
+        struct Node* grandparent;
+        struct Stack* nodeStack;
+        enum Code findNodeNext;
+        int result;
+    } RedBlackTree;
+    struct RotateTree {
+        enum Code next;
+        struct RedBlackTree* traverse;
+        struct Tree* tree;
+    } RotateTree;
+    struct Node {
+        int key; // comparable data segment
+        union Data* value;
+        struct Node* left;
+        struct Node* right;
+        // need to balancing
+        enum Color {
+            Red,
+            Black,
+            // Red eq 0,Black eq 1. enum name convert intager.
+        } color;
+    } Node;
+    struct Atomic {
+        union Data* atomic;
+        union Data** ptr;
+        union Data* oldData;
+        union Data* newData;
+        enum Code checkAndSet;
+        enum Code next;
+        enum Code fail;
+    } Atomic;
+    struct AtomicReference {
+    } AtomicReference;
+    struct Semaphore {
+        union Data* semaphore;
+        enum Code p;
+        enum Code v;
+        enum Code next;
+    } Semaphore;
+    struct SemaphoreImpl {
+        int value;
+        struct Lock* lock;
+        struct Queue* waitThreadQueue;
+    } SemaphoreImpl;
+    struct Allocate {
+        enum Code next;
+        long size;
+    } Allocate;
+    struct Integer {
+        int value;
+    } Integer;
+    struct SortArray {
+        struct Integer *array; //Array arrayじゃできない?
+        int loopCounter;
+        int block;
+        int first;
+        int prefix;
+    } SortArray;
+    struct Iterator {
+        union Data* iterator;
+        struct Context* task;
+        int numGPU;
+        enum Code exec;
+        enum Code barrier;
+        enum Code whenWait;
+        enum Code next;
+    } Iterator;
+    struct MultiDimIterator {
+        int x;
+        int y;
+        int z;
+        int count;
+        int counterX;
+        int counterY;
+        int counterZ;
+    } MultiDimIterator;
+    struct MultiDim {
+        int x;
+        int y;
+        int z;
+    } MultiDim;
+    struct Executor {
+        union Data* executor;
+        struct Context* task;
+        enum Code read;
+        enum Code exec;
+        enum Code write;
+        enum Code next;
+    } Executor;
+#ifdef USE_CUDAWorker
+    struct CUDAExecutor {
+        CUdeviceptr** kernelParams;
+        struct CUDABuffer* buffer;
+        int maxThreadPerBlock;
+        int maxThreadPerBlockX;
+        int maxThreadPerBlockY;
+        int maxThreadPerBlockZ;
+        struct Timer* timer;
+    } CUDAExecutor;
+    struct CUDABuffer {
+        int inputLen;
+        int outputLen;
+        union Data** inputData;
+        union Data** outputData;
+    } CUDABuffer;
+    CUdeviceptr CUdeviceptr;
+#else
+    struct CUDAExecutor {
+    } CUDAExecutor;
+    struct CUDABuffer {
+    } CUDABuffer;
+    CUdeviceptr CUdeviceptr;
+#endif
+    Int Int;
+    struct Memory {
+        union Data* adr;
+        int length;
+        union Data* body;
+        int hash;
+    } Memory;
+    struct Buffer {
+        union Data* buffer;
+        union Data* data;
+        enum Code put;
+        enum Code take;
+        enum Code next;
+    } Buffer;
+    struct BoundedBuffer {
+        struct Element* top;
+        struct Element* last;
+        struct Semaphore* fullCount;
+        struct Semaphore* emptyCount;
+        struct Semaphore* lock;
+    } BoundedBuffer;
+    struct Lock {
+        union Data* lock;
+        enum Code doLock;
+        enum Code doUnlock;
+        enum Code next;
+    } Lock;
+    struct LockImpl {
+        Int* lock;
+        struct Queue* waitThreadQueue;
+        struct Atomic* atomic;
+        struct Context* lockContext;
+    } LockImpl;
+    struct SpinLock {
+        volatile Int* lock;
+        struct Atomic* atomic;
+        struct Context* lockContext;
+    } SpinLock;
+}; // union Data end       this is necessary for context generator
+typedef union Data Data;
+
+#include "c/typedefData.h"
+
+#include "c/extern.h"
+
+extern __code start_code(struct Context* context);
+extern __code exit_code(struct Context* context);
+extern __code meta(struct Context* context, enum Code next);
+//extern __code par_meta(struct Context* context, enum Code spawns, enum Code next);
+extern __code parGotoMeta(struct Context* context, enum Code next);
+extern void initContext(struct Context* context);
+
+#endif