Skip to content
Permalink
Browse files

Use mrblib -- mruby library

  • Loading branch information...
kaz0505 committed Jul 19, 2018
1 parent 8795971 commit c55d28039d066bb2fecdc63c905584ba11ca0445
Showing with 180 additions and 52 deletions.
  1. +17 −0 mrblib/Makefile
  2. +29 −0 mrblib/mrblib.c
  3. +11 −0 mrblib/mrblib.rb
  4. +68 −0 mrblib/mrblib.txt
  5. +11 −0 mrblib/numeric.rb
  6. +4 −2 src/Makefile
  7. +1 −50 src/c_numeric.c
  8. +20 −0 src/class.c
  9. +19 −0 src/mrblib.c
@@ -0,0 +1,17 @@
#
# mruby/c Makefile
#
# Copyright (C) 2015,2016 Kyushu Institute of Technology.
# Copyright (C) 2015,2016 Shimane IT Open-Innovation Center.
#
# This file is distributed under BSD 3-Clause License.
#

SRC = numeric.rb
MRBC = mrbc

all: mrblib.c

mrblib.c: $(SRC)
cat $(SRC) > mrblib.rb
$(MRBC) -v -E -Bmrblib_bytecode mrblib.rb > mrblib.txt
@@ -0,0 +1,29 @@
/* dumped in big endian order.
use `mrbc -e` option for better performance on little endian CPU. */
#include <stdint.h>
extern const uint8_t mrblib_bytecode[];
const uint8_t
#if defined __GNUC__
__attribute__((aligned(4)))
#elif defined _MSC_VER
__declspec(align(4))
#endif
mrblib_bytecode[] = {
0x52,0x49,0x54,0x45,0x30,0x30,0x30,0x34,0x67,0x2e,0x00,0x00,0x01,0x0a,0x4d,0x41,
0x54,0x5a,0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x00,0xd5,0x30,0x30,
0x30,0x30,0x00,0x00,0x00,0x37,0x00,0x01,0x00,0x03,0x00,0x01,0x00,0x00,0x00,0x05,
0x00,0x80,0x00,0x05,0x01,0x00,0x00,0x05,0x00,0x80,0x00,0x43,0x00,0x80,0x00,0x45,
0x00,0x00,0x00,0x4a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x06,0x46,0x69,
0x78,0x6e,0x75,0x6d,0x00,0x00,0x00,0x00,0x36,0x00,0x01,0x00,0x03,0x00,0x01,0x00,
0x00,0x00,0x05,0x00,0x00,0x80,0x00,0x48,0x01,0x00,0x00,0xc0,0x00,0x80,0x00,0x46,
0x00,0x80,0x00,0x04,0x00,0x80,0x00,0x29,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x00,0x05,0x74,0x69,0x6d,0x65,0x73,0x00,0x00,0x00,0x00,0x65,0x00,0x03,0x00,0x06,
0x00,0x00,0x00,0x00,0x00,0x0f,0x00,0x00,0x00,0x00,0x00,0x26,0x01,0x3f,0xff,0x83,
0x00,0x40,0x03,0x17,0x02,0x00,0x80,0x01,0x01,0x80,0x00,0x2b,0x01,0x80,0x00,0xa0,
0x01,0x80,0x80,0x01,0x01,0x80,0x40,0xad,0x01,0x00,0xc0,0x01,0x01,0x80,0x80,0x01,
0x02,0x00,0x00,0x06,0x01,0x80,0x80,0xb3,0x01,0xbf,0xfb,0x18,0x01,0x80,0x00,0x06,
0x01,0x80,0x00,0x29,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x04,0x63,0x61,
0x6c,0x6c,0x00,0x00,0x01,0x2b,0x00,0x00,0x01,0x3c,0x00,0x4c,0x56,0x41,0x52,0x00,
0x00,0x00,0x17,0x00,0x00,0x00,0x01,0x00,0x01,0x69,0xff,0xff,0x00,0x00,0x00,0x00,
0x00,0x02,0x45,0x4e,0x44,0x00,0x00,0x00,0x00,0x08,
};
@@ -0,0 +1,11 @@

class Fixnum
def times
i = 0
while i < self
yield i
i += 1
end
self
end
end
@@ -0,0 +1,68 @@
mruby 1.3.0 (2017-7-4)
00002 NODE_SCOPE:
00002 NODE_BEGIN:
00002 NODE_CLASS:
00011 :Fixnum
00011 body:
00002 NODE_BEGIN:
00003 NODE_DEF:
00010 times
00003 local variables:
00003 i
00004 NODE_BEGIN:
00004 NODE_ASGN:
00004 lhs:
00004 NODE_LVAR i
00004 rhs:
00004 NODE_INT 0 base 10
00005 NODE_WHILE:
00008 cond:
00005 NODE_CALL(.):
00005 NODE_LVAR i
00005 method='<' (230)
00005 args:
00005 NODE_SELF
00008 body:
00006 NODE_BEGIN:
00006 NODE_YIELD:
00006 NODE_LVAR i
00007 NODE_OP_ASGN:
00007 lhs:
00007 NODE_LVAR i
00007 op='+' (107)
00007 NODE_INT 1 base 10
00009 NODE_SELF
irep 0xbf8de0 nregs=3 nlocals=1 pools=0 syms=1 reps=1
file: mrblib.rb
2 000 OP_LOADNIL R1
2 001 OP_LOADNIL R2
2 002 OP_CLASS R1 :Fixnum
2 003 OP_EXEC R1 I(+1)
2 004 OP_STOP

irep 0xbfefe0 nregs=3 nlocals=1 pools=0 syms=1 reps=1
file: mrblib.rb
3 000 OP_TCLASS R1
3 001 OP_LAMBDA R2 I(+1) method
3 002 OP_METHOD R1 :times
3 003 OP_LOADSYM R1 :times
3 004 OP_RETURN R1 normal

irep 0xc051e0 nregs=6 nlocals=3 pools=0 syms=3 reps=0
file: mrblib.rb
3 000 OP_ENTER 0:0:0:0:0:0:0
4 001 OP_LOADI R2 0 ; R2:i
5 002 OP_JMP 009
6 003 OP_MOVE R4 R2 ; R2:i
6 004 OP_BLKPUSH R3 0:0:0:0
6 005 OP_SEND R3 :call 1
7 006 OP_MOVE R3 R2 ; R2:i
7 007 OP_ADDI R3 :+ 1
7 008 OP_MOVE R2 R3 ; R2:i
5 009 OP_MOVE R3 R2 ; R2:i
5 010 OP_LOADSELF R4
5 011 OP_LT R3 :< 1
5 012 OP_JMPIF R3 003
9 013 OP_LOADSELF R3
9 014 OP_RETURN R3 normal

@@ -0,0 +1,11 @@

class Fixnum
def times
i = 0
while i < self
yield i
i += 1
end
self
end
end
@@ -10,9 +10,11 @@
CFLAGS = -Wall -Wpointer-arith -g -DMRBC_DEBUG # -std=c99 -pedantic -pedantic-errors

COMMON_SRCS = alloc.c class.c console.c global.c keyvalue.c load.c rrt0.c static.c symbol.c value.c vm.c hal/hal.c
RUBY_LIB_SRCS = c_array.c c_hash.c c_numeric.c c_math.c c_range.c c_string.c
RUBY_LIB_SRCS = c_array.c c_hash.c c_numeric.c c_math.c c_range.c c_string.c
MRBLIB_SRC = ../mrblib/mrblib.c

TARGET = libmrubyc.a
OBJS = $(COMMON_SRCS:.c=.o) $(RUBY_LIB_SRCS:.c=.o)
OBJS = $(COMMON_SRCS:.c=.o) $(RUBY_LIB_SRCS:.c=.o) $(MRBLIB_SRC:.c=.o)


all:
@@ -185,55 +185,6 @@ static void c_fixnum_to_f(mrb_vm *vm, mrb_value v[], int argc)
#endif


//================================================================
/*! (method) times
*/
static void c_fixnum_times(mrb_vm *vm, mrb_value v[], int argc)
{
uint32_t code[2] = {
MKOPCODE(OP_CALL) | MKARG_A(argc),
MKOPCODE(OP_ABORT)
};
mrb_irep irep = {
0, // nlocals
0, // nregs
0, // rlen
2, // ilen
0, // plen
(uint8_t *)code, // iseq
NULL, // pools
NULL, // ptr_to_sym
NULL, // reps
};

// count of times
int cnt = v[0].i;

mrbc_push_callinfo(vm, 0);

// adjust reg_top for reg[0]==Proc
vm->current_regs += v - vm->regs + 1;

int i;
for( i=0 ; i<cnt ; i++ ){
// set index
mrbc_release( &v[2] );
v[2].tt = MRB_TT_FIXNUM;
v[2].i = i;

// set OP_CALL irep
vm->pc = 0;
vm->pc_irep = &irep;

// execute OP_CALL
mrbc_vm_run(vm);
}

mrbc_pop_callinfo(vm);
}



#if MRBC_USE_STRING
//================================================================
/*! (method) chr
@@ -291,7 +242,7 @@ void mrbc_init_class_fixnum(mrb_vm *vm)
mrbc_define_method(vm, mrbc_class_fixnum, ">>", c_fixnum_rshift);
mrbc_define_method(vm, mrbc_class_fixnum, "abs", c_fixnum_abs);
mrbc_define_method(vm, mrbc_class_fixnum, "to_i", c_ineffect);
mrbc_define_method(vm, mrbc_class_fixnum, "times", c_fixnum_times);
// mrbc_define_method(vm, mrbc_class_fixnum, "times", c_fixnum_times);
#if MRBC_USE_FLOAT
mrbc_define_method(vm, mrbc_class_fixnum, "to_f", c_fixnum_to_f);
#endif
@@ -794,6 +794,24 @@ void c_ineffect(mrb_vm *vm, mrb_value v[], int argc)
}


//================================================================
/*! Run mrblib, which is mruby bytecode
*/
void mrbc_run_mrblib(void)
{
extern const uint8_t mrblib_bytecode[];

mrb_vm vm;
mrbc_vm_open(&vm);
mrbc_load_mrb(&vm, mrblib_bytecode);
mrbc_vm_begin(&vm);
mrbc_vm_run(&vm);
mrbc_vm_end(&vm);
//mrbc_vm_close(&vm);
}




//================================================================
// initialize
@@ -820,4 +838,6 @@ void mrbc_init_class(void)
mrbc_init_class_array(0);
mrbc_init_class_range(0);
mrbc_init_class_hash(0);

mrbc_run_mrblib();
}
@@ -0,0 +1,19 @@
/* dumped in big endian order.
use `mrbc -e` option for better performance on little endian CPU. */
#include <stdint.h>
extern const uint8_t mrblib_bytecode[];
const uint8_t
#if defined __GNUC__
__attribute__((aligned(4)))
#elif defined _MSC_VER
__declspec(align(4))
#endif
mrblib_bytecode[] = {
0x52,0x49,0x54,0x45,0x30,0x30,0x30,0x34,0xcc,0xf2,0x00,0x00,0x00,0x66,0x4d,0x41,
0x54,0x5a,0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x00,0x48,0x30,0x30,
0x30,0x30,0x00,0x00,0x00,0x40,0x00,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x04,
0x00,0x80,0x00,0x06,0x01,0x00,0x00,0x3d,0x00,0x80,0x00,0xa0,0x00,0x00,0x00,0x4a,
0x00,0x00,0x00,0x01,0x00,0x00,0x0c,0x54,0x65,0x73,0x74,0x20,0x50,0x72,0x69,0x67,
0x72,0x61,0x6d,0x00,0x00,0x00,0x01,0x00,0x04,0x70,0x75,0x74,0x73,0x00,0x45,0x4e,
0x44,0x00,0x00,0x00,0x00,0x08,
};

0 comments on commit c55d280

Please sign in to comment.
You can’t perform that action at this time.