Skip to content

Commit 94e0063

Browse files
committed
WIP for Unification including global_mrb
1 parent fe3939a commit 94e0063

File tree

8 files changed

+59
-52
lines changed

8 files changed

+59
-52
lines changed

include/mrc_common.h

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919
MRC_STRINGIZE(MRC_RELEASE_MONTH) "-" \
2020
MRC_STRINGIZE(MRC_RELEASE_DAY)
2121

22-
#ifdef MRC_CUSTOM_ALLOC
23-
#include <prism_xallocator.h>
24-
#else
25-
#include <stdlib.h>
26-
#define mrc_malloc(c, size) malloc(size)
27-
#define mrc_realloc(c, ptr, size) realloc(ptr, size)
28-
#define mrc_calloc(c, nmemb, size) calloc(nmemb, size)
29-
#define mrc_free(c, ptr) free(ptr)
30-
#endif
31-
3222
#ifdef MRB_USE_CXX_ABI
3323
#define MRC_USE_CXX_ABI
3424
#endif
@@ -48,13 +38,17 @@
4838
# define MRC_END_DECL
4939
#endif
5040

51-
#ifdef MRC_DEBUG
52-
#include <assert.h>
53-
#define mrc_assert(p) assert(p)
54-
#define mrc_assert_int_fit(t1,n,t2,max) assert((n)>=0 && ((sizeof(n)<=sizeof(t2))||(n<=(t1)(max))))
41+
/** Declare a public mruby API function. */
42+
#ifndef MRC_API
43+
#if defined(MRC_BUILD_AS_DLL)
44+
#if defined(MRC_CORE) || defined(MRC_LIB)
45+
# define MRC_API __declspec(dllexport)
5546
#else
56-
#define mrc_assert(p) ((void)0)
57-
#define mrc_assert_int_fit(t1,n,t2,max) ((void)0)
47+
# define MRC_API __declspec(dllimport)
48+
#endif
49+
#else
50+
# define MRC_API extern
51+
#endif
5852
#endif
5953

6054
#if defined(__cplusplus) || (defined(__bool_true_false_are_defined) && __bool_true_false_are_defined)
@@ -128,17 +122,21 @@ typedef uint8_t mrc_code;
128122
*/
129123
typedef uint32_t mrc_aspec;
130124

131-
/** Declare a public mruby API function. */
132-
#ifndef MRC_API
133-
#if defined(MRC_BUILD_AS_DLL)
134-
#if defined(MRC_CORE) || defined(MRC_LIB)
135-
# define MRC_API __declspec(dllexport)
136-
#else
137-
# define MRC_API __declspec(dllimport)
125+
#ifndef MRC_CUSTOM_ALLOC
126+
#include <stdlib.h>
127+
// #define mrc_malloc(c, size) malloc(size)
128+
// #define mrc_realloc(c, ptr, size) realloc(ptr, size)
129+
// #define mrc_calloc(c, nmemb, size) calloc(nmemb, size)
130+
// #define mrc_free(c, ptr) free(ptr)
138131
#endif
132+
133+
#ifdef MRC_DEBUG
134+
#include <assert.h>
135+
#define mrc_assert(p) assert(p)
136+
#define mrc_assert_int_fit(t1,n,t2,max) assert((n)>=0 && ((sizeof(n)<=sizeof(t2))||(n<=(t1)(max))))
139137
#else
140-
# define MRC_API extern
141-
#endif
138+
#define mrc_assert(p) ((void)0)
139+
#define mrc_assert_int_fit(t1,n,t2,max) ((void)0)
142140
#endif
143141

144142
#endif /* MRC_COMMON_H */

include/prism_xallocator.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
#ifndef PRISM_CUSTOM_ALLOCATOR_H
22
#define PRISM_CUSTOM_ALLOCATOR_H
33

4-
void *picorb_alloc(unsigned int size);
5-
void *picorb_calloc(unsigned int nmemb, unsigned int size);
6-
void *picorb_realloc(void *ptr, unsigned int size);
7-
void picorb_free(void *ptr);
4+
#if defined(MRC_TARGET_MRUBY)
5+
#include <mruby.h>
86

9-
#define xmalloc picorb_alloc
10-
#define xcalloc picorb_calloc
11-
#define xrealloc picorb_realloc
12-
#define xfree picorb_free
7+
extern mrb_state *global_mrb;
138

14-
#define mrc_malloc picorb_alloc
15-
#define mrc_calloc picorb_calloc
16-
#define mrc_realloc picorb_realloc
17-
#define mrc_free picorb_free
9+
#define xmalloc(size) mrb_malloc(global_mrb, size)
10+
#define xcalloc(nmemb,size) mrb_calloc(global_mrb, nmemb, size)
11+
#define xrealloc(ptr,size) mrb_realloc(global_mrb, ptr, size)
12+
#define xfree(ptr) mrb_free(global_mrb, ptr)
13+
14+
#define mrc_malloc(c,size) mrb_malloc(c->mrb, size)
15+
#define mrc_calloc(c,nmemb,size) mrb_calloc(c->mrb, nmemb, size)
16+
#define mrc_realloc(c,ptr,size) mrb_realloc(c->mrb, ptr, size)
17+
#define mrc_free(c,ptr) mrb_free(c->mrb, ptr)
18+
#else
19+
#include <mrubyc.h>
20+
// TODO
21+
#endif
1822

1923
#endif
2024

mrbgem.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ MRuby::Gem::Specification.new('mruby-compiler2') do |spec|
1313

1414
if cc.defines.include? "PICORB_VM_MRUBY"
1515
cc.defines << "MRC_TARGET_MRUBY"
16-
cc.include_paths << "#{build.gems['picoruby-mruby']}/lib/mruby/include"
16+
cc.include_paths << "#{build.gems['picoruby-mruby'].dir}/lib/mruby/include"
1717
end
1818

1919
if cc.defines.any? { _1.match? /\A(PICORUBY|MRB)_NO_FLOAT(=|\z)/ }

src/ccontext.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ mrc_ccontext_new(mrb_state *mrb)
88
{
99
mrc_ccontext temp_c = {0};
1010
temp_c.mrb = mrb;
11-
mrc_ccontext *c = (mrc_ccontext *)mrc_calloc(&temp_c, 1, sizeof(mrc_ccontext));
12-
c->p = (mrc_parser_state *)mrc_malloc(c, sizeof(mrc_parser_state));
11+
mrc_ccontext *c = (mrc_ccontext *)mrc_calloc((&temp_c), 1, sizeof(mrc_ccontext));
12+
c->p = (mrc_parser_state *)mrc_malloc((&temp_c), sizeof(mrc_parser_state));
1313
c->options = NULL;
1414
c->mrb = temp_c.mrb;
1515
return c;

src/codegen.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ scope_new(mrc_ccontext *c, mrc_codegen_scope *prev, mrc_constant_id_list *nlv)
538538
return NULL;
539539
}
540540
*s = codegen_scope_zero;
541+
if (prev) {
542+
s->c = prev->c;
543+
} else {
544+
s->c = c;
545+
}
541546
s->mpool = pool;
542547
if (!prev) return s;
543548
s->prev = prev;

src/compile.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mrc_pm_parse(mrc_ccontext *c)
186186
// save top-level locals for IRB
187187
pm_program_node_t *program = (pm_program_node_t *)node;
188188
uint32_t nlocals = program->locals.size;
189-
pm_options_t *options = (pm_options_t *)mrc_malloc(c->mrb, sizeof(pm_options_t));
189+
pm_options_t *options = (pm_options_t *)mrc_malloc(c, sizeof(pm_options_t));
190190
memset(options, 0, sizeof(pm_options_t));
191191
pm_string_t *encoding = &options->encoding;
192192
pm_string_constant_init(encoding, "UTF-8", 5);
@@ -201,7 +201,7 @@ mrc_pm_parse(mrc_ccontext *c)
201201
scope_local = &options_scope->locals[i];
202202
id = program->locals.ids[i];
203203
local = pm_constant_pool_id_to_constant(&c->p->constant_pool, id);
204-
allocated = (char *)mrc_malloc(c->mrb, local->length);
204+
allocated = (char *)mrc_malloc(c, local->length);
205205
memcpy(allocated, local->start, local->length);
206206
pm_string_constant_init(scope_local, (const char *)allocated, local->length);
207207
}
@@ -224,7 +224,7 @@ mrc_parse_file_cxt(mrc_ccontext *c, const char **filenames, uint8_t **source)
224224
while (filenames[filecount]) {
225225
filecount++;
226226
}
227-
c->filename_table = (mrc_filename_table *)mrc_malloc(c->mrb, sizeof(mrc_filename_table) * filecount);
227+
c->filename_table = (mrc_filename_table *)mrc_malloc(c, sizeof(mrc_filename_table) * filecount);
228228
c->filename_table_length = filecount;
229229
c->current_filename_index = 0;
230230
ssize_t length = read_input_files(c, filenames, source, c->filename_table);
@@ -258,7 +258,7 @@ mrc_parse_string_cxt(mrc_ccontext *c, const uint8_t **source, size_t length)
258258
{
259259
pm_string_t string;
260260
pm_string_owned_init(&string, (uint8_t *)source, length);
261-
c->filename_table = (mrc_filename_table *)mrc_malloc(c->mrb, sizeof(mrc_filename_table));
261+
c->filename_table = (mrc_filename_table *)mrc_malloc(c, sizeof(mrc_filename_table));
262262
c->filename_table[0].filename = "-e";
263263
c->filename_table[0].start = 0;
264264
c->filename_table_length = 1;

src/custom_allocator.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#ifdef MRC_CUSTOM_ALLOC
22

3+
#include <prism_xallocator.h>
34
#include <string.h>
4-
#include "../include/mrc_ccontext.h"
55

66
#ifdef MRC_TARGET_MRUBY
77

88
#include <mruby.h>
9-
#define picorb_alloc(c, size) mrb_malloc(c->mrb, size)
10-
#define picorb_calloc(c, nmemb, size) mrb_calloc(c->mrb, nmemb, size)
11-
#define picorb_realloc(c, ptr, size) mrb_realloc(c->mrb, ptr, size)
12-
#define picorb_free(c, ptr) mrb_free(c->mrb, ptr)
9+
#define picorb_alloc(c, size) mrb_malloc(c, size)
10+
#define picorb_calloc(c, nmemb, size) mrb_calloc(c, nmemb, size)
11+
#define picorb_realloc(c, ptr, size) mrb_realloc(c, ptr, size)
12+
#define picorb_free(c, ptr) mrb_free(c, ptr)
1313

14-
#else /* MRC_TARGET_MRUBY */
14+
#else /* MRC_TARGET_MRUBYC */
1515

1616
#include <mrubyc.h>
1717
#define picorb_alloc mrbc_raw_alloc
@@ -31,6 +31,6 @@ picorb_realloc(void *ptr, unsigned int size)
3131
}
3232
}
3333

34-
#endif /* MRC_TARGET_MRUBY */
34+
#endif /* MRC_TARGET_MRUBYC */
3535

3636
#endif // MRC_CUSTOM_ALLOC

src/diagnostic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ mrc_diagnostic_code_to_string(mrc_diagnostic_code code)
6161
void
6262
mrc_diagnostic_list_append(mrc_ccontext *c, const uint8_t * location_start, const char *message, mrc_diagnostic_code code)
6363
{
64-
mrc_diagnostic_list *list = mrc_calloc(c->mrb, 1, sizeof(mrc_diagnostic_list));
64+
mrc_diagnostic_list *list = mrc_calloc(c, 1, sizeof(mrc_diagnostic_list));
6565
line_and_column_by_start_and_offset(c->p->start, location_start, &list->line, &list->column);
6666
char buf[256];
6767
const char *diagnostic_code_str = mrc_diagnostic_code_to_string(code);

0 commit comments

Comments
 (0)