From 8c59e1e41700802575cefb289600028909c5ffc2 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Sun, 13 Aug 2017 15:07:17 +0300 Subject: [PATCH] =?utf8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BC?= =?utf8?q?=D0=BE=D0=B6=D0=BD=D0=BE=20=D1=83=D0=BA=D0=B0=D0=B7=D1=8B=D0=B2?= =?utf8?q?=D0=B0=D1=82=D1=8C=20=D0=BA=D1=83=D0=B4=D0=B0=20=D1=81=D0=B3?= =?utf8?q?=D1=80=D1=83=D0=B6=D0=B0=D1=82=D1=8C=20=D0=B2=D1=8B=D1=81=D0=B5?= =?utf8?q?=D1=80=20=D0=BA=D0=BE=D0=B4=D0=BE=D0=B3=D0=B5=D0=BD=D0=B5=D1=80?= =?utf8?q?=D0=B0=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Test.obn | 3 +++ include/oberon.h | 11 +------- obn-compile.sh | 9 +++---- obn-dev-test.sh | 2 +- obn-gdb-compile.sh | 5 ---- src/backends/jvm/generator-jvm-abi.c | 2 +- src/backends/jvm/generator-jvm-asm.c | 11 +++++--- src/backends/jvm/generator-jvm-asm.h | 2 +- src/backends/jvm/generator-jvm.c | 38 ++++++++++++++++++++-------- src/backends/jvm/generator-jvm.h | 2 ++ src/generator.h | 2 ++ src/main.c | 35 ++++++++++++++++++++----- 12 files changed, 79 insertions(+), 43 deletions(-) delete mode 100755 obn-gdb-compile.sh diff --git a/Test.obn b/Test.obn index 82fbedb..f769178 100644 --- a/Test.obn +++ b/Test.obn @@ -4,6 +4,9 @@ VAR i : SHORTINT; s : SET; +PROCEDURE X; +END X; + BEGIN i := SHORT(12345); END Test. diff --git a/include/oberon.h b/include/oberon.h index b2f24b4..2f51a77 100644 --- a/include/oberon.h +++ b/include/oberon.h @@ -19,15 +19,6 @@ extern oberon_module_t * oberon_compile_module(oberon_context_t * ctx, const char * code); extern void -oberon_generate_code(oberon_context_t * ctx); - -extern void -oberon_generator_dump(oberon_context_t * ctx, char * path); - -extern void * -oberon_generator_get_procedure(oberon_context_t * ctx, const char * name); - -extern void * -oberon_generator_get_var(oberon_context_t * ctx, const char * name); +oberon_set_out_directory(oberon_context_t * ctx, const char * path); #endif // OBERON_H diff --git a/obn-compile.sh b/obn-compile.sh index f89bf03..c02d178 100755 --- a/obn-compile.sh +++ b/obn-compile.sh @@ -2,13 +2,12 @@ set -e -rm -rf classes -rm -f *.j *.jad -mkdir -p classes +rm -rf classes tmp +mkdir -p classes tmp -./a.out $1 +./a.out -d tmp $1 -jasmin -d classes *.j +jasmin -d classes tmp/*.j javac -d classes rtl/*.java diff --git a/obn-dev-test.sh b/obn-dev-test.sh index 424cf4e..5d49ab8 100755 --- a/obn-dev-test.sh +++ b/obn-dev-test.sh @@ -7,7 +7,7 @@ set -e # -a -- for asm as comments # -noinner -jad -o -b -noinner classes/* +jad -d tmp -o -b -noinner classes/* ./obn-run.sh Test diff --git a/obn-gdb-compile.sh b/obn-gdb-compile.sh deleted file mode 100755 index dbb00a5..0000000 --- a/obn-gdb-compile.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -set -e - -exec gdb ./a.out $* diff --git a/src/backends/jvm/generator-jvm-abi.c b/src/backends/jvm/generator-jvm-abi.c index b404fca..8d966cb 100644 --- a/src/backends/jvm/generator-jvm-abi.c +++ b/src/backends/jvm/generator-jvm-abi.c @@ -398,7 +398,7 @@ jvm_generate_frame_class(oberon_object_t * proc) abscname = "SYSTEM$FRAME"; cname = jvm_get_field_full_name_safe(proc); cname = new_string("%s$FRAME", cname); - class = jvm_create_class(cname); + class = jvm_create_class(proc -> gen_proc -> class -> dir, cname); fp = class -> fp; class -> full_name = cname; diff --git a/src/backends/jvm/generator-jvm-asm.c b/src/backends/jvm/generator-jvm-asm.c index 4aee145..1d1220d 100644 --- a/src/backends/jvm/generator-jvm-asm.c +++ b/src/backends/jvm/generator-jvm-asm.c @@ -70,15 +70,20 @@ jvm_generate_function_end(gen_proc_t * p) } struct gen_class * -jvm_create_class(char * full_name) +jvm_create_class(const char * dir, char * full_name) { struct gen_class * class = GC_MALLOC(sizeof *class); memset(class, 0, sizeof *class); - char * fname = new_string("%s.j", full_name); + char * fname = new_string("%s/%s.j", dir, full_name); class -> full_name = new_string(full_name); class -> fp = fopen(fname, "w"); - assert(class -> fp); + class -> dir = new_string(dir); + + if(class -> fp == NULL) + { + gen_error("unable to create file %s", fname); + } return class; } diff --git a/src/backends/jvm/generator-jvm-asm.h b/src/backends/jvm/generator-jvm-asm.h index 2b5911c..b6084c5 100644 --- a/src/backends/jvm/generator-jvm-asm.h +++ b/src/backends/jvm/generator-jvm-asm.h @@ -1,5 +1,5 @@ struct gen_class * -jvm_create_class(char * full_name); +jvm_create_class(const char * dir, char * full_name); void jvm_destroy_class(struct gen_class * class); diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index 5b45cc1..2456aa7 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -319,6 +319,7 @@ oberon_generator_init_context(oberon_context_t * ctx) memset(gen_context, 0, sizeof *gen_context); ctx -> gen_context = gen_context; + gen_context -> dir = "."; } void @@ -327,7 +328,7 @@ oberon_generator_destroy_context(oberon_context_t * ctx) } static void -oberon_generate_procedure_class(oberon_type_t * proc) +oberon_generate_procedure_class(const char * dir, oberon_type_t * proc) { FILE * fp; char * cname; @@ -335,7 +336,7 @@ oberon_generate_procedure_class(oberon_type_t * proc) struct gen_class * class; cname = jvm_get_class_full_name(proc); - class = jvm_create_class(cname); + class = jvm_create_class(dir, cname); fp = class -> fp; fprintf(fp, ".source SYSTEM\n"); @@ -357,7 +358,7 @@ oberon_generate_procedure_class(oberon_type_t * proc) } static void -oberon_generate_procedure_pointer_class(oberon_object_t * proc) +oberon_generate_procedure_pointer_class(const char * dir, oberon_object_t * proc) { FILE * fp; char * cname; @@ -367,7 +368,7 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc) struct gen_class * class; cname = jvm_get_field_full_name_safe(proc); - class = jvm_create_class(cname); + class = jvm_create_class(dir, cname); abscname = jvm_get_class_full_name(proc -> type); absdesc = jvm_get_descriptor(proc -> type); fp = class -> fp; @@ -440,7 +441,7 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc) } static void -oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec) +oberon_generate_record_class(const char * dir, gen_module_t * m, oberon_type_t * rec) { char * cname; struct gen_class * class; @@ -450,7 +451,7 @@ oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec) m -> rec_id += 1; cname = jvm_get_class_full_name(rec); - class = jvm_create_class(cname); + class = jvm_create_class(dir, cname); fprintf(class -> fp, ".source %s\n", rec -> module -> name); fprintf(class -> fp, ".class public %s\n", cname); @@ -475,6 +476,9 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) memset(t, 0, sizeof *t); type -> gen_type = t; + gen_context_t * c; + c = ctx -> gen_context; + gen_module_t * m; switch(type -> class) { @@ -491,10 +495,10 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) break; case OBERON_TYPE_RECORD: m = type -> module -> gen_mod; - oberon_generate_record_class(m, type); + oberon_generate_record_class(c -> dir, m, type); break; case OBERON_TYPE_PROCEDURE: - oberon_generate_procedure_class(type); + oberon_generate_procedure_class(c -> dir, type); break; default: gen_error("oberon_generator_init_type: unk calss %i", type -> class); @@ -693,8 +697,11 @@ oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod) memset(m, 0, sizeof *m); mod -> gen_mod = m; + gen_context_t * c; + c = ctx -> gen_context; + struct gen_class * class; - class = jvm_create_class(mod -> name); + class = jvm_create_class(c -> dir, mod -> name); fprintf(class -> fp, ".source %s\n", mod -> name); fprintf(class -> fp, ".class %s\n", mod -> name); @@ -702,7 +709,7 @@ oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod) m -> class = class; - ctx -> gen_context -> current_m = m; + c -> current_m = m; } void @@ -893,7 +900,7 @@ oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc) } else { - oberon_generate_procedure_pointer_class(proc); + oberon_generate_procedure_pointer_class(p -> class -> dir, proc); signature = jvm_get_procedure_signature(proc -> type); } @@ -1795,3 +1802,12 @@ oberon_generate_halt(oberon_context_t * ctx, int64_t n) jvm_generate(p, 2, 0, "invokestatic SYSTEM/HALT(J)V"); } +void +oberon_set_out_directory(oberon_context_t * ctx, const char * path) +{ + gen_context_t * c; + + c = ctx -> gen_context; + + c -> dir = new_string(path); +} diff --git a/src/backends/jvm/generator-jvm.h b/src/backends/jvm/generator-jvm.h index 00c3e50..5e10df4 100644 --- a/src/backends/jvm/generator-jvm.h +++ b/src/backends/jvm/generator-jvm.h @@ -23,6 +23,7 @@ struct gen_class FILE * fp; gen_proc_t * p; struct gen_class * base; + const char * dir; }; enum gen_storage @@ -90,6 +91,7 @@ struct gen_var_t struct gen_context_t { gen_module_t * current_m; + const char * dir; }; struct gen_module_t diff --git a/src/generator.h b/src/generator.h index 80bab13..5617e79 100644 --- a/src/generator.h +++ b/src/generator.h @@ -44,3 +44,5 @@ void oberon_generate_assert(oberon_context_t * ctx, oberon_expr_t * cond); void oberon_generate_trap(oberon_context_t * ctx, int64_t n); void oberon_generate_halt(oberon_context_t * ctx, int64_t n); void oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr); + +void oberon_set_out_directory(oberon_context_t * ctx, const char * path); diff --git a/src/main.c b/src/main.c index a97f2d7..994ae80 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,8 @@ static oberon_context_t * ctx; static oberon_module_t * mod; +static const char * module; +static const char * out_path; static const char * import_module(const char * name) @@ -37,22 +39,43 @@ import_module(const char * name) return source; } +static void +init(int argc, char ** argv) +{ + int i = 1; + out_path = "."; + + if(argc > 2 && strcmp(argv[i], "-d") == 0) + { + out_path = argv[i + 1]; + argc -= 2; + i += 2; + } + + if(argc != 2 || argv[i][0] == '-') + { + printf("usage: obn [-d out_dir] module\n"); + exit(1); + } + + module = argv[i]; +} + int main(int argc, char ** argv) { const char * code; - if(argc != 2) - { - printf("use: %s \n", argv[0]); - return 1; - } + init(argc, argv); - code = import_module(argv[1]); + code = import_module(module); ctx = oberon_create_context(import_module); + oberon_set_out_directory(ctx, out_path); + mod = oberon_compile_module(ctx, code); oberon_destroy_context(ctx); + return 0; } -- 2.29.2