summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 351f950)
raw | patch | inline | side by side (parent: 351f950)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Thu, 27 Jul 2017 18:06:22 +0000 (21:06 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Thu, 27 Jul 2017 18:06:22 +0000 (21:06 +0300) |
src/backends/jvm/generator-jvm-basic.c | [new file with mode: 0644] | patch | blob |
src/backends/jvm/generator-jvm-basic.h | [new file with mode: 0644] | patch | blob |
src/backends/jvm/generator-jvm.c | patch | blob | history | |
src/backends/jvm/generator-jvm.h | patch | blob | history | |
src/generator.h | patch | blob | history | |
src/oberon-internals.h | patch | blob | history | |
src/oberon.c | patch | blob | history | |
src/test.c | patch | blob | history |
diff --git a/src/backends/jvm/generator-jvm-basic.c b/src/backends/jvm/generator-jvm-basic.c
--- /dev/null
@@ -0,0 +1,344 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <string.h>
+#include <assert.h>
+
+#include <gc.h>
+
+#include "../../../include/oberon.h"
+#include "../../oberon-internals.h"
+#include "generator-jvm.h"
+#include "generator-jvm-basic.h"
+
+char *
+new_string(const char * format, ...)
+{
+ va_list ptr;
+ va_start(ptr, format);
+
+ char buf[1024];
+ vsnprintf(buf, 1024, format, ptr);
+
+ va_end(ptr);
+
+ char * result;
+ int size;
+
+ size = strlen(buf);
+ result = GC_MALLOC(size + 1);
+ memset(result, 0, size);
+ strncpy(result, buf, size);
+
+ return result;
+}
+
+void
+gen_error(const char * fmt, ...)
+{
+ va_list ptr;
+ va_start(ptr, fmt);
+ fprintf(stderr, "generator: ");
+ vfprintf(stderr, fmt, ptr);
+ fprintf(stderr, "\n");
+ va_end(ptr);
+ exit(1);
+}
+
+char *
+jvm_get_descriptor(oberon_type_t * type)
+{
+ char * desc;
+
+ switch(type -> class)
+ {
+ case OBERON_TYPE_VOID:
+ return new_string("V");
+ break;
+ case OBERON_TYPE_INTEGER:
+ switch(type -> size)
+ {
+ case 1:
+ return new_string("B");
+ break;
+ case 2:
+ return new_string("S");
+ break;
+ case 4:
+ return new_string("I");
+ break;
+ case 8:
+ return new_string("J");
+ break;
+ default:
+ gen_error("jvm_get_descriptor: unsupported int size %i", type -> size);
+ break;
+ }
+ break;
+ case OBERON_TYPE_REAL:
+ switch(type -> size)
+ {
+ case 4:
+ return new_string("F");
+ break;
+ case 8:
+ return new_string("D");
+ break;
+ default:
+ gen_error("jvm_get_descriptor: unsupported float size %i", type -> size);
+ break;
+ }
+ break;
+ case OBERON_TYPE_BOOLEAN:
+ return new_string("Z");
+ break;
+ case OBERON_TYPE_POINTER:
+ return jvm_get_descriptor(type -> base);
+ break;
+ case OBERON_TYPE_PROCEDURE:
+ case OBERON_TYPE_RECORD:
+ desc = jvm_get_class_full_name(type);
+ return new_string("L%s;", desc);
+ break;
+ case OBERON_TYPE_ARRAY:
+ desc = jvm_get_descriptor(type -> base);
+ return new_string("[%s", desc);
+ break;
+ default:
+ gen_error("jvm_get_descriptor: unsupported type class %i", type -> class);
+ break;
+ }
+
+ return NULL;
+}
+
+char
+jvm_get_prefix(oberon_type_t * type)
+{
+ int size = type -> size;
+ switch(type -> class)
+ {
+ case OBERON_TYPE_BOOLEAN:
+ case OBERON_TYPE_INTEGER:
+ return (size <= 4) ? ('i') : ('l');
+ break;
+ case OBERON_TYPE_PROCEDURE:
+ case OBERON_TYPE_ARRAY:
+ case OBERON_TYPE_RECORD:
+ case OBERON_TYPE_POINTER:
+ return 'a';
+ break;
+ case OBERON_TYPE_REAL:
+ return (size <= 4) ? ('f') : ('d');
+ break;
+ }
+
+ gen_error("jvm_get_prefix: wat");
+ return '!';
+}
+
+char *
+jvm_get_field_full_name(oberon_object_t * x)
+{
+ switch(x -> class)
+ {
+ case OBERON_CLASS_VAR:
+ return new_string("%s/%s", x -> module -> name, x -> name);
+ case OBERON_CLASS_FIELD:;
+ char * rec_name = jvm_get_class_full_name(x -> parent_type);
+ return new_string("%s/%s", rec_name, x -> name);
+ case OBERON_CLASS_MODULE:
+ return new_string(x -> module -> name);
+ default:
+ gen_error("jvm_get_field_full_name: wat");
+ break;
+ }
+
+ return NULL;
+}
+
+char *
+jvm_get_class_full_name(oberon_type_t * type)
+{
+ int rec_id;
+ char * name = NULL;
+
+ switch(type -> class)
+ {
+ case OBERON_TYPE_PROCEDURE:
+ name = new_string("SYSTEM$PROCEDURE");
+
+ char * desc;
+ desc = jvm_get_descriptor(type -> base);
+ name = new_string("%s$%s", name, desc);
+
+ int num = type -> num_decl;
+ oberon_object_t * arg = type -> decl;
+ for(int i = 0; i < num; i++)
+ {
+ desc = jvm_get_descriptor(arg -> type);
+ name = new_string("%s%s", name, desc);
+ arg = arg -> next;
+ }
+
+ break;
+ case OBERON_TYPE_RECORD:
+ rec_id = type -> gen_type -> rec_id;
+ name = new_string("%s$RECORD%i", type -> module -> name, rec_id);
+ break;
+ default:
+ gen_error("jvm_get_class_full_name: unk type class %i", type -> class);
+ break;
+ }
+
+ return name;
+}
+
+char *
+jvm_get_procedure_signature(oberon_type_t * proc)
+{
+ char * signature;
+ char * desc;
+
+ signature = new_string("(");
+
+ int num = proc -> num_decl;
+ oberon_object_t * arg = proc -> decl;
+ for(int i = 0; i < num; i++)
+ {
+ desc = jvm_get_descriptor(arg -> type);
+ signature = new_string("%s%s", signature, desc);
+ arg = arg -> next;
+ }
+
+ desc = jvm_get_descriptor(proc -> base);
+ signature = new_string("%s)%s", signature, desc);
+
+ return signature;
+}
+
+int
+jvm_cell_size_for_type(oberon_type_t * type)
+{
+ if(type -> class == OBERON_TYPE_INTEGER
+ || type -> class == OBERON_TYPE_REAL)
+ {
+ if(type -> size > 4)
+ {
+ return 2;
+ }
+ }
+
+ return 1;
+}
+
+int
+jvm_cell_size_for_prefix(char prefix)
+{
+ switch(prefix)
+ {
+ case 'a':
+ case 'b':
+ case 's':
+ case 'i':
+ case 'z':
+ case 'f':
+ return 1;
+ case 'l':
+ case 'd':
+ return 2;
+ default:
+ gen_error("jvm_cell_size_for_prefix: unk prefix %c", prefix);
+ }
+
+ return -666;
+}
+
+bool
+jvm_is_wide_type(oberon_type_t * type)
+{
+ int cell;
+ cell = jvm_cell_size_for_type(type);
+ assert(cell <= 2);
+ return (cell == 2);
+}
+
+bool
+jvm_is_free_register(struct gen_register_file * rf, int i, bool wide)
+{
+ if(wide)
+ {
+ assert(i + 1 < MAX_REGISTERS);
+ return !(rf -> reg[i].used || rf -> reg[i + 1].used);
+ }
+ else
+ {
+ assert(i < MAX_REGISTERS);
+ return !(rf -> reg[i].used);
+ }
+}
+
+int
+jvm_alloc_register_untyped(struct gen_register_file * rf, bool wide)
+{
+ int i = 0;
+ while(i < MAX_REGISTERS && !jvm_is_free_register(rf, i, wide))
+ {
+ i += 1;
+ }
+
+ if(wide)
+ {
+ assert(i + 1 <= MAX_REGISTERS);
+ rf -> num_used += 2;
+ rf -> reg[i].used = true;
+ rf -> reg[i + 1].used = true;
+ rf -> reg[i].used = true;
+ rf -> reg[i + 1].wide = false;
+ }
+ else
+ {
+ assert(i <= MAX_REGISTERS);
+ rf -> num_used += 1;
+ rf -> reg[i].used = true;
+ rf -> reg[i].wide = false;
+ }
+
+ if(rf -> num_used > rf -> max_used)
+ {
+ rf -> max_used = rf -> num_used;
+ }
+
+ return i;
+}
+
+int
+jvm_alloc_register(struct gen_register_file * rf, oberon_type_t * type)
+{
+ bool wide;
+ wide = jvm_is_wide_type(type);
+ return jvm_alloc_register_untyped(rf, wide);
+}
+
+char
+jvm_get_type_of_prefix(char prefix)
+{
+ switch(prefix)
+ {
+ case 'b':
+ return 'B';
+ case 'c':
+ return 'C';
+ case 'd':
+ return 'D';
+ case 'f':
+ return 'F';
+ case 'i':
+ return 'I';
+ case 'l':
+ return 'J';
+ }
+
+ assert(0);
+}
diff --git a/src/backends/jvm/generator-jvm-basic.h b/src/backends/jvm/generator-jvm-basic.h
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * В этом модуле представлены базовые функции для
+ * генерации информации о сигнатурах и резервирования ресурсов.
+ */
+
+char *
+new_string(const char * format, ...);
+
+void
+gen_error(const char * fmt, ...);
+
+
+
+char *
+jvm_get_descriptor(oberon_type_t * type);
+
+char
+jvm_get_prefix(oberon_type_t * type);
+
+char *
+jvm_get_field_full_name(oberon_object_t * x);
+
+char *
+jvm_get_class_full_name(oberon_type_t * type);
+
+char *
+jvm_get_procedure_signature(oberon_type_t * proc);
+
+
+
+int
+jvm_cell_size_for_type(oberon_type_t * type);
+
+int
+jvm_cell_size_for_prefix(char prefix);
+
+bool
+jvm_is_wide_type(oberon_type_t * type);
+
+bool
+jvm_is_free_register(struct gen_register_file * rf, int i, bool wide);
+
+int
+jvm_alloc_register_untyped(struct gen_register_file * rf, bool wide);
+
+int
+jvm_alloc_register(struct gen_register_file * rf, oberon_type_t * type);
+
+
+
+char
+jvm_get_type_of_prefix(char prefix);
index 215f6d757b1d92fd1b1b2c67ed088333959e927d..7c426d546c7bdf190f3594e7b5bda31293f89543 100644 (file)
#include <stdio.h>
+#include <stdlib.h>
#include <stdarg.h>
+#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include "../../../include/oberon.h"
#include "../../oberon-internals.h"
#include "generator-jvm.h"
+#include "generator-jvm-basic.h"
-char *
-new_string(const char * format, ...)
-{
- va_list ptr;
- va_start(ptr, format);
+// ==========================================
+// ==========================================
+// ==========================================
- char buf[1024];
- vsnprintf(buf, 1024, format, ptr);
+gen_proc_t *
+jvm_create_proc(struct gen_class * class)
+{
+ gen_proc_t * p = GC_MALLOC(sizeof *p);
+ memset(p, 0, sizeof *p);
- va_end(ptr);
+ struct gen_register_file * rf = GC_MALLOC(sizeof *rf);
+ memset(rf, 0, sizeof *rf);
- char * result;
- int size;
+ struct gen_stack * stack = GC_MALLOC(sizeof *stack);
+ memset(rf, 0, sizeof *stack);
- size = strlen(buf);
- result = GC_MALLOC(size + 1);
- memset(result, 0, size);
- strncpy(result, buf, size);
+ p -> rf = rf;
+ p -> stack = stack;
+ p -> class = class;
+ p -> label_id = 0;
- return result;
+ return p;
}
void
-oberon_generator_init_context(oberon_context_t * ctx)
+jvm_generate_function_header(gen_proc_t * p, char * access, char * name, char * signature)
{
- gen_context_t * gen_context = GC_MALLOC(sizeof *gen_context);
- memset(gen_context, 0, sizeof *gen_context);
+ struct gen_class * class;
+ class = p -> class;
- ctx -> gen_context = gen_context;
+ /* Делаем процедуру текущей в этом классе */
+ assert(class -> p == NULL);
+ class -> p = p;
+
+ fprintf(class -> fp, ".method %s %s%s\n", access, name, signature);
}
void
-oberon_generator_destroy_context(oberon_context_t * ctx)
+jvm_generate_function_end(gen_proc_t * p)
{
- ctx -> gen_context = NULL;
-}
+ struct gen_class * class;
+ class = p -> class;
-static char * get_class_full_name(oberon_context_t * ctx, oberon_type_t * type);
+ assert(class -> p);
+ class -> p = NULL;
-static char *
-get_descriptor(oberon_context_t * ctx, oberon_type_t * type)
+ fprintf(class -> fp, " .limit stack %i\n", p -> stack -> max_pointer);
+ fprintf(class -> fp, " .limit locals %i\n", p -> rf -> max_used);
+ fprintf(class -> fp, ".end method\n\n");
+}
+
+struct gen_class *
+jvm_create_class(char * full_name)
{
- char * desc;
+ struct gen_class * class = GC_MALLOC(sizeof *class);
+ memset(class, 0, sizeof *class);
- switch(type -> class)
- {
- case OBERON_TYPE_VOID:
- return new_string("V");
- break;
- case OBERON_TYPE_INTEGER:
- switch(type -> size)
- {
- case 1:
- return new_string("B");
- break;
- case 2:
- return new_string("S");
- break;
- case 4:
- return new_string("I");
- break;
- case 8:
- return new_string("J");
- break;
- default:
- oberon_error(ctx, "get_descriptor: unsupported int size %i", type -> size);
- break;
- }
- break;
- case OBERON_TYPE_REAL:
- switch(type -> size)
- {
- case 4:
- return new_string("F");
- break;
- case 8:
- return new_string("D");
- break;
- default:
- oberon_error(ctx, "get_descriptor: unsupported float size %i", type -> size);
- break;
- }
- break;
- case OBERON_TYPE_BOOLEAN:
- return new_string("Z");
- break;
- case OBERON_TYPE_POINTER:
- return get_descriptor(ctx, type -> base);
- break;
- case OBERON_TYPE_PROCEDURE:
- case OBERON_TYPE_RECORD:
- desc = get_class_full_name(ctx, type);
- return new_string("L%s;", desc);
- break;
- case OBERON_TYPE_ARRAY:
- desc = get_descriptor(ctx, type -> base);
- return new_string("[%s", desc);
- break;
- default:
- oberon_error(ctx, "print_descriptor: unsupported type class %i", type -> class);
- break;
- }
+ char * fname = new_string("%s.j", full_name);
+ class -> fp = fopen(fname, "w");
+ assert(class -> fp);
- return NULL;
+ return class;
}
-static char
-get_prefix(oberon_context_t * ctx, oberon_type_t * type)
+static void
+jvm_destroy_class(struct gen_class * class)
{
- int size = type -> size;
- switch(type -> class)
- {
- case OBERON_TYPE_BOOLEAN:
- case OBERON_TYPE_INTEGER:
- return (size <= 4) ? ('i') : ('l');
- break;
- case OBERON_TYPE_PROCEDURE:
- case OBERON_TYPE_ARRAY:
- case OBERON_TYPE_RECORD:
- case OBERON_TYPE_POINTER:
- return 'a';
- break;
- case OBERON_TYPE_REAL:
- return (size <= 4) ? ('f') : ('d');
- break;
- }
+ assert(class -> p == NULL);
+ fclose(class -> fp);
+}
- oberon_error(ctx, "get_prefix: wat");
- return '!';
+static void
+jvm_generate_field(struct gen_class * class, char * access, char * name, char * desc)
+{
+ assert(class -> p == NULL);
+ fprintf(class -> fp, ".field %s %s %s\n\n", access, name, desc);
}
-static char *
-get_field_full_name(oberon_context_t * ctx, oberon_object_t * x)
+static void
+jvm_stack_push(gen_proc_t * p, unsigned size)
{
- switch(x -> class)
+ p -> stack -> pointer += size;
+ if(p -> stack -> pointer > p -> stack -> max_pointer)
{
- case OBERON_CLASS_VAR:
- return new_string("%s/%s", x -> module -> name, x -> name);
- case OBERON_CLASS_FIELD:;
- char * rec_name = get_class_full_name(ctx, x -> parent_type);
- return new_string("%s/%s", rec_name, x -> name);
- case OBERON_CLASS_MODULE:
- return new_string(x -> module -> name);
- default:
- oberon_error(ctx, "get_field_full_name: wat");
- break;
+ p -> stack -> max_pointer = p -> stack -> pointer;
}
+}
- return NULL;
+static void
+jvm_stack_pop(gen_proc_t * p, unsigned size)
+{
+ p -> stack -> pointer -= size;
+ assert(p -> stack -> pointer >= 0);
}
-static char *
-get_class_full_name(oberon_context_t * ctx, oberon_type_t * type)
+static void
+jvm_generate(gen_proc_t * p, unsigned get, unsigned push, char * format, ...)
{
- int rec_id;
- char * name = NULL;
+ va_list ptr;
+ va_start(ptr, format);
- switch(type -> class)
- {
- case OBERON_TYPE_PROCEDURE:
- name = new_string("SYSTEM$PROCEDURE");
+ jvm_stack_pop(p, get);
+ fprintf(p -> class -> fp, " ");
+ vfprintf(p -> class -> fp, format, ptr);
+ fprintf(p -> class -> fp, "\n");
+ jvm_stack_push(p, push);
- char * desc;
- desc = get_descriptor(ctx, type -> base);
- name = new_string("%s$%s", name, desc);
+ va_end(ptr);
+}
- int num = type -> num_decl;
- oberon_object_t * arg = type -> decl;
- for(int i = 0; i < num; i++)
- {
- desc = get_descriptor(ctx, arg -> type);
- name = new_string("%s%s", name, desc);
- arg = arg -> next;
- }
+static void
+jvm_generate_push_int(gen_proc_t * p, int64_t i)
+{
+ if(i == -1)
+ {
+ jvm_generate(p, 0, 1, "iconst_m1");
+ }
+ else if(i >= 0 && i <= 5)
+ {
+ jvm_generate(p, 0, 1, "iconst_%li", i);
+ }
+ else if(i >= -128 && i <= 127)
+ {
+ jvm_generate(p, 0, 1, "bipush %li", i);
+ }
+ else if(i >= -32768 && i <= 32767)
+ {
+ jvm_generate(p, 0, 1, "sipush %li", i);
+ }
+ else if(i >= -2147483648 && i <= 2147483647)
+ {
+ jvm_generate(p, 0, 1, "ldc %li", i);
+ }
+ else
+ {
+ jvm_generate(p, 0, 2, "ldc2 %li", i);
+ }
+}
- break;
- case OBERON_TYPE_RECORD:
- rec_id = type -> gen_type -> rec_id;
- name = new_string("%s$RECORD%i", type -> module -> name, rec_id);
- break;
- default:
- oberon_error(ctx, "get_record_full_name: unk type class %i", type -> class);
- break;
+static void
+jvm_generate_push_float(gen_proc_t * p, double f, int size)
+{
+ if(size <= 4)
+ {
+ if(f == 0.0)
+ {
+ jvm_generate(p, 0, 1, "fconst_0");
+ }
+ if(f == 1.0)
+ {
+ jvm_generate(p, 0, 1, "fconst_1");
+ }
+ if(f == 2.0)
+ {
+ jvm_generate(p, 0, 1, "fconst_2");
+ }
+ else
+ {
+ jvm_generate(p, 0, 1, "ldc %lf", f);
+ }
}
+ else
+ {
+ if(f == 0.0)
+ {
+ jvm_generate(p, 0, 2, "dconst_0");
+ }
+ if(f == 1.0)
+ {
+ jvm_generate(p, 0, 2, "dconst_1");
+ }
+ else
+ {
+ jvm_generate(p, 0, 2, "ldc2 %lf", f);
+ }
+ }
+}
- return name;
+static int
+jvm_new_label_id(gen_proc_t * p)
+{
+ int label_id = p -> label_id;
+ p -> label_id += 1;
+ return label_id;
}
static char *
-get_procedure_signature(oberon_context_t * ctx, oberon_type_t * proc)
+jvm_get_label_name(int label_id)
{
- char * signature;
- char * desc;
+ return new_string("L%i", label_id);
+}
- signature = new_string("(");
+static void
+jvm_generate_label(gen_proc_t * p, int label_id)
+{
+ jvm_generate(p, 0, 0, "L%i:", label_id);
+}
- int num = proc -> num_decl;
- oberon_object_t * arg = proc -> decl;
- for(int i = 0; i < num; i++)
- {
- desc = get_descriptor(ctx, arg -> type);
- signature = new_string("%s%s", signature, desc);
- arg = arg -> next;
- }
+// ==========================================
+// ==========================================
+// ==========================================
- desc = get_descriptor(ctx, proc -> base);
- signature = new_string("%s)%s", signature, desc);
+void
+oberon_generator_init_context(oberon_context_t * ctx)
+{
+ gen_context_t * gen_context = GC_MALLOC(sizeof *gen_context);
+ memset(gen_context, 0, sizeof *gen_context);
- return signature;
+ ctx -> gen_context = gen_context;
+}
+
+void
+oberon_generator_destroy_context(oberon_context_t * ctx)
+{
}
static void
-oberon_generate_procedure_class(oberon_context_t * ctx, oberon_type_t * proc)
+oberon_generate_procedure_class(oberon_type_t * proc)
{
FILE * fp;
char * cname;
- char * fname;
char * signature;
+ struct gen_class * class;
- cname = get_class_full_name(ctx, proc);
- fname = new_string("%s.j", cname);
-
- fp = fopen(fname, "w");
+ cname = jvm_get_class_full_name(proc);
+ class = jvm_create_class(cname);
+ fp = class -> fp;
fprintf(fp, ".source SYSTEM\n");
fprintf(fp, ".class public abstract %s\n", cname);
fprintf(fp, ".super java/lang/Object\n\n");
- signature = get_procedure_signature(ctx, proc);
-
fprintf(fp, ".method public <init>()V\n");
fprintf(fp, " aload_0\n");
fprintf(fp, " invokespecial java/lang/Object/<init>()V\n");
fprintf(fp, " return\n");
fprintf(fp, ".end method\n\n");
+ signature = jvm_get_procedure_signature(proc);
+
fprintf(fp, ".method public abstract invoke%s\n", signature);
fprintf(fp, ".end method\n\n");
- fclose(fp);
+ jvm_destroy_class(class);
}
static void
-oberon_generate_record_class(oberon_context_t * ctx, oberon_type_t * rec)
+oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec)
{
- FILE * fp;
char * cname;
- char * fname;
+ struct gen_class * class;
/* Устанавливаем новоый id */
- rec -> gen_type -> rec_id = rec -> module -> gen_mod -> rec_id;
- rec -> module -> gen_mod -> rec_id += 1;
-
- cname = get_class_full_name(ctx, rec);
- fname = new_string("%s.j", cname);
+ rec -> gen_type -> rec_id = m -> rec_id;
+ m -> rec_id += 1;
- fp = fopen(fname, "w");
+ cname = jvm_get_class_full_name(rec);
+ class = jvm_create_class(cname);
- fprintf(fp, ".source %s\n", rec -> module -> name);
- fprintf(fp, ".class public %s\n", cname);
- fprintf(fp, ".super java/lang/Object\n\n");
+ fprintf(class -> fp, ".source %s\n", rec -> module -> name);
+ fprintf(class -> fp, ".class public %s\n", cname);
+ fprintf(class -> fp, ".super java/lang/Object\n\n");
- rec -> gen_type -> fp = fp;
+ rec -> gen_type -> class = class;
}
void
case OBERON_TYPE_REAL:
break;
case OBERON_TYPE_RECORD:
- oberon_generate_record_class(ctx, type);
+ ;
+ gen_module_t * m;
+ m = type -> module -> gen_mod;
+ oberon_generate_record_class(m, type);
break;
case OBERON_TYPE_PROCEDURE:
- oberon_generate_procedure_class(ctx, type);
+ oberon_generate_procedure_class(type);
break;
case OBERON_TYPE_POINTER:
break;
default:
- oberon_error(ctx, "oberon_generator_init_type: unk calss %i", type -> class);
+ gen_error("oberon_generator_init_type: unk calss %i", type -> class);
break;
}
}
static void
-oberon_generate_object(oberon_context_t * ctx, FILE * fp, oberon_object_t * x)
+jvm_generate_object(struct gen_class * class, oberon_object_t * x)
{
char * name;
char * desc;
name = x -> name;
- desc = get_descriptor(ctx, x -> type);
+ desc = jvm_get_descriptor(x -> type);
switch(x -> class)
{
case OBERON_CLASS_VAR:
if(x -> local == 0)
{
- fprintf(fp, ".field public static %s %s\n\n", name, desc);
+ jvm_generate_field(class, "public static", name, desc);
+ }
+ else
+ {
+ gen_error("jvm_generate_object: local variable as class field? wat");
}
break;
case OBERON_CLASS_FIELD:
- fprintf(fp, ".field public %s %s\n\n", name, desc);
+ jvm_generate_field(class, "public", name, desc);
break;
default:
- oberon_error(ctx, "oberon_generate_object: unk class %i", x -> class);
+ gen_error("jvm_generate_object: unk class %i", x -> class);
break;
}
}
@@ -332,25 +343,24 @@ oberon_generate_object(oberon_context_t * ctx, FILE * fp, oberon_object_t * x)
void
oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
{
- FILE * fp;
-
- fp = rec -> gen_type -> fp;
+ struct gen_class * class;
+ class = rec -> gen_type -> class;
int num = rec -> num_decl;
oberon_object_t * field = rec -> decl;
for(int i = 0; i < num; i++)
{
- oberon_generate_object(ctx, fp, field);
+ jvm_generate_object(class, field);
field = field -> next;
}
- fprintf(fp, ".method public <init>()V\n");
- fprintf(fp, " aload_0\n");
- fprintf(fp, " invokespecial java/lang/Object/<init>()V\n");
- fprintf(fp, " return\n");
- fprintf(fp, ".end method\n");
+ fprintf(class -> fp, ".method public <init>()V\n");
+ fprintf(class -> fp, " aload_0\n");
+ fprintf(class -> fp, " invokespecial java/lang/Object/<init>()V\n");
+ fprintf(class -> fp, " return\n");
+ fprintf(class -> fp, ".end method\n");
- fclose(fp);
+ jvm_destroy_class(class);
}
void
memset(v, 0, sizeof *v);
var -> gen_var = v;
- gen_module_t * m = ctx -> mod -> gen_mod;
+ gen_module_t * m;
+ m = ctx -> mod -> gen_mod;
+
+ struct gen_class * class;
+ class = m -> class;
switch(var -> class)
{
case OBERON_CLASS_VAR_PARAM:
- oberon_error(ctx, "generator: VAR-parameters not implemented");
+ gen_error("generator: VAR-parameters not implemented");
break;
case OBERON_CLASS_PARAM:
case OBERON_CLASS_FIELD:
break;
case OBERON_CLASS_VAR:
- oberon_generate_object(ctx, m -> fp, var);
+ jvm_generate_object(class, var);
break;
default:
- oberon_error(ctx, "oberon_generator_init_var: unk var class %i", var -> class);
+ gen_error("oberon_generator_init_var: unk var class %i", var -> class);
break;
}
}
void
oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
{
- gen_proc_t * p = GC_MALLOC(sizeof *p);
- memset(p, 0, sizeof *p);
- proc -> gen_proc = p;
-
- struct gen_register_file * rf = GC_MALLOC(sizeof *rf);
- memset(rf, 0, sizeof *rf);
- p -> rf = rf;
+ struct gen_class * class = proc -> module -> gen_mod -> class;
+ proc -> gen_proc = jvm_create_proc(class);
if(proc -> local)
{
- oberon_error(ctx, "generator: local procedures not implemented");
+ gen_error("generator: local procedures not implemented");
}
}
memset(m, 0, sizeof *m);
mod -> gen_mod = m;
- int fnamesz = strlen(mod -> name) + 3;
- char fname[fnamesz + 1];
- snprintf(fname, fnamesz, "%s.j", mod -> name);
+ struct gen_class * class;
+ class = jvm_create_class(mod -> name);
- FILE * fp;
- fp = fopen(fname, "w");
- assert(fp != NULL);
+ fprintf(class -> fp, ".source %s\n", mod -> name);
+ fprintf(class -> fp, ".class %s\n", mod -> name);
+ fprintf(class -> fp, ".super java/lang/Object\n\n");
- fprintf(fp, ".source %s\n", mod -> name);
- fprintf(fp, ".class %s\n", mod -> name);
- fprintf(fp, ".super java/lang/Object\n\n");
+ m -> class = class;
- m -> fp = fp;
+ ctx -> gen_context -> current_m = m;
}
-// =======================================================================
-// GENERATOR
-// =======================================================================
+void
+oberon_generator_fini_module(oberon_module_t * mod)
+{
+ jvm_destroy_class(mod -> gen_mod -> class);
+}
static void
-push_expr(oberon_context_t * ctx, FILE * fp, oberon_expr_t * expr);
+push_expr(gen_proc_t * p, oberon_expr_t * expr);
void
oberon_generate_begin_module(oberon_context_t * ctx)
{
- gen_module_t * m = ctx -> mod -> gen_mod;
- fprintf(m -> fp, ".method public <init>()V\n");
- fprintf(m -> fp, " aload_0\n");
- fprintf(m -> fp, " invokespecial java/lang/Object/<init>()V\n");
+ struct gen_class * class = ctx -> mod -> gen_mod -> class;
+ gen_proc_t * p = jvm_create_proc(class);
+
+ jvm_generate_function_header(p, "public", "<init>", "()V");
+ jvm_alloc_register_untyped(p -> rf, false);
+ jvm_generate(p, 0, 1, "aload_0");
+ jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
}
void
oberon_generate_end_module(oberon_context_t * ctx)
{
- gen_module_t * m = ctx -> mod -> gen_mod;
-
- fprintf(m -> fp, " .limit stack 32\n");
- fprintf(m -> fp, " .limit locals 32\n");
- fprintf(m -> fp, " return\n");
- fprintf(m -> fp, ".end method\n");
-}
-
-static int
-jvm_cell_size_for_type(oberon_type_t * type)
-{
- if(type -> class == OBERON_TYPE_INTEGER
- || type -> class == OBERON_TYPE_REAL)
- {
- if(type -> size > 4)
- {
- return 2;
- }
- }
-
- return 1;
-}
-
-static bool
-jvm_is_wide_type(oberon_type_t * type)
-{
- int cell;
- cell = jvm_cell_size_for_type(type);
- assert(cell <= 2);
- return (cell == 2);
-}
-
-static bool
-jvm_is_free_register(struct gen_register_file * rf, int i, bool wide)
-{
- if(wide)
- {
- assert(i + 1 < MAX_REGISTERS);
- return !(rf -> reg[i].used || rf -> reg[i + 1].used);
- }
- else
- {
- assert(i < MAX_REGISTERS);
- return !(rf -> reg[i].used);
- }
-}
-
-static int
-jvm_alloc_register(struct gen_register_file * rf, oberon_type_t * type)
-{
- bool wide;
- wide = jvm_is_wide_type(type);
-
- int i = 0;
- while(i < MAX_REGISTERS && !jvm_is_free_register(rf, i, wide))
- {
- i += 1;
- }
-
- if(wide)
- {
- assert(i + 1 <= MAX_REGISTERS);
- rf -> num_used += 2;
- rf -> reg[i].used = true;
- rf -> reg[i + 1].used = true;
- rf -> reg[i].used = true;
- rf -> reg[i + 1].wide = false;
- }
- else
- {
- assert(i <= MAX_REGISTERS);
- rf -> num_used += 1;
- rf -> reg[i].used = true;
- rf -> reg[i].wide = false;
- }
-
- if(rf -> num_used > rf -> max_used)
- {
- rf -> max_used = rf -> num_used;
- }
+ struct gen_class * class = ctx -> mod -> gen_mod -> class;
+ gen_proc_t * p = class -> p;
- return i;
+ jvm_generate(p, 0, 0, "return");
+ jvm_generate_function_end(class -> p);
}
static void
-jvm_init_local_object(oberon_context_t * ctx, FILE * fp, oberon_object_t * x)
+jvm_init_local_object(gen_proc_t * p, oberon_object_t * x)
{
- int reg;
- gen_context_t * c;
gen_var_t * v;
char * desc;
+ struct gen_class * class;
- c = ctx -> gen_context;
v = x -> gen_var;
- desc = get_descriptor(ctx, x -> type);
+ class = p -> class;
+ desc = jvm_get_descriptor(x -> type);
+
+ // Убеждаемся что сейчас находимся в функции
+ assert(class -> p);
+ assert(x -> local);
- reg = -1;
switch(x -> class)
{
case OBERON_CLASS_VAR:
case OBERON_CLASS_PARAM:
- if(x -> local)
- {
- reg = jvm_alloc_register(c -> rf, x -> type);
- }
- v -> reg = reg;
- fprintf(fp, ".var %i is %s %s from start to end\n", reg, x -> name, desc);
+ v -> reg = jvm_alloc_register(p -> rf, x -> type);
+ fprintf(class -> fp, ".var %i is %s %s from start to end\n", v -> reg, x -> name, desc);
break;
default:
- oberon_error(ctx, "jvm_init_local_object: wat");
+ gen_error("jvm_init_local_object: wat");
break;
}
}
@@ -554,189 +486,120 @@ jvm_init_local_object(oberon_context_t * ctx, FILE * fp, oberon_object_t * x)
void
oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
{
- gen_context_t * c;
- gen_module_t * m;
gen_proc_t * p;
char * signature;
- c = ctx -> gen_context;
- m = ctx -> mod -> gen_mod;
p = proc -> gen_proc;
- signature = get_procedure_signature(ctx, proc -> type);
- fprintf(m -> fp, ".method public static %s%s\n", proc -> name, signature);
+ signature = jvm_get_procedure_signature(proc -> type);
- /* Сохраняем регистровый файл в стеке */
- p -> rf -> up = c -> rf;
- c -> rf = p -> rf;
+ jvm_generate_function_header(p, "public static", proc -> name, signature);
oberon_object_t * var = proc -> type -> decl;
while(var)
{
- jvm_init_local_object(ctx, m -> fp, var);
+ jvm_init_local_object(p, var);
var = var -> next;
}
-
- fprintf(m -> fp, "start:\n");
}
void
oberon_generate_end_proc(oberon_context_t * ctx)
{
- gen_context_t * c;
gen_module_t * m;
+ gen_proc_t * p;
- c = ctx -> gen_context;
m = ctx -> mod -> gen_mod;
+ p = m -> class -> p;
- fprintf(m -> fp, "end:\n");
- fprintf(m -> fp, " .limit stack 32\n");
- fprintf(m -> fp, " .limit locals %i\n", c -> rf -> max_used);
- fprintf(m -> fp, ".end method\n\n");
-
- /* Возвращаем исходный регистровый файл */
- c -> rf = c -> rf -> up;
+ jvm_generate_function_end(p);
}
-void
-oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
+static void
+jvm_generate_call_proc(gen_proc_t * p, oberon_expr_t * desig)
{
+ assert(desig -> is_item);
+ assert(desig -> item.mode == MODE_CALL);
+
oberon_object_t * proc;
- gen_module_t * m;
char * fullname;
char * signature;
- assert(desig -> is_item);
- assert(desig -> item.mode == MODE_CALL);
-
- m = ctx -> mod -> gen_mod;
proc = desig -> item.var;
- fullname = get_field_full_name(ctx, proc);
- signature = get_procedure_signature(ctx, proc -> type);
+ fullname = jvm_get_field_full_name(proc);
+ signature = jvm_get_procedure_signature(proc -> type);
+
+ int args_cells = 0;
+ int result_cells = jvm_cell_size_for_type(proc -> type -> base);
int num = desig -> item.num_args;
oberon_expr_t * arg = desig -> item.args;
for(int i = 0; i < num; i++)
{
- push_expr(ctx, m -> fp, arg);
+ args_cells += jvm_cell_size_for_type(arg -> result);
+ push_expr(p, arg);
arg = arg -> next;
}
- fprintf(m -> fp, "invokestatic %s%s\n", fullname, signature);
+ jvm_generate(p, args_cells, result_cells, "invokestatic %s%s", fullname, signature);
+}
+
+void
+oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
+{
+ jvm_generate_call_proc(ctx -> gen_context -> current_m -> class -> p, desig);
}
void
oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
{
gen_module_t * m;
+ gen_proc_t * p;
char prefix;
+ int cell_size;
m = ctx -> mod -> gen_mod;
+ p = m -> class -> p;
if(expr)
{
- push_expr(ctx, m -> fp, expr);
- prefix = get_prefix(ctx, expr -> result);
- fprintf(m -> fp, " %creturn\n", prefix);
- }
- else
- {
- fprintf(m -> fp, " return\n");
- }
-}
-
-static void
-push_int(FILE * fp, long i)
-{
- if(i == -1)
- {
- fprintf(fp, "iconst_m1\n");
- }
- else if(i >= 0 && i <= 5)
- {
- fprintf(fp, "iconst_%li\n", i);
- }
- else if(i >= -128 && i <= 127)
- {
- fprintf(fp, "bipush %li\n", i);
- }
- else if(i >= -32768 && i <= 32767)
- {
- fprintf(fp, "sipush %li\n", i);
- }
- else if(i >= -2147483648 && i <= 2147483647)
- {
- fprintf(fp, "ldc %li\n", i);
+ push_expr(p, expr);
+ prefix = jvm_get_prefix(expr -> result);
+ cell_size = jvm_cell_size_for_type(expr -> result);
+ jvm_generate(p, cell_size, 0, "%creturn", prefix);
}
else
{
- fprintf(fp, "ldc2 %li\n", i);
+ jvm_generate(p, 0, 0, "return");
}
}
static void
-push_float(FILE * fp, double f, int size)
+jvm_push_var(gen_proc_t * p, oberon_object_t * var)
{
- if(size <= 4)
- {
- if(f == 0.0)
- {
- fprintf(fp, "fconst_0\n");
- }
- if(f == 1.0)
- {
- fprintf(fp, "fconst_1\n");
- }
- if(f == 2.0)
- {
- fprintf(fp, "fconst_2\n");
- }
- else
- {
- fprintf(fp, "ldc %lf\n", f);
- }
- }
- else
- {
- if(f == 0.0)
- {
- fprintf(fp, "dconst_0\n");
- }
- if(f == 1.0)
- {
- fprintf(fp, "dconst_1\n");
- }
- else
- {
- fprintf(fp, "ldc2 %lf\n", f);
- }
- }
-}
+ int cell_size = jvm_cell_size_for_type(var -> type);
-static void
-push_var(oberon_context_t * ctx, FILE * fp, oberon_object_t * var)
-{
if(var -> local)
{
int reg = var -> gen_var -> reg;
- char prefix = get_prefix(ctx, var -> type);
- fprintf(fp, "%cload %i\n", prefix, reg);
+ char prefix = jvm_get_prefix(var -> type);
+ jvm_generate(p, 0, cell_size, "%cload %i", prefix, reg);
}
else
{
- char * fullname = get_field_full_name(ctx, var);
- char * desc = get_descriptor(ctx, var -> type);
- fprintf(fp, "getstatic %s %s\n", fullname, desc);
+ char * fullname = jvm_get_field_full_name(var);
+ char * desc = jvm_get_descriptor(var -> type);
+ jvm_generate(p, 0, cell_size, "getstatic %s %s", fullname, desc);
}
}
static void
-jvm_generate_new_static(oberon_context_t * ctx, FILE * fp, oberon_type_t * type, int num, oberon_expr_t * arg)
+jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type, int num, oberon_expr_t * arg)
{
//char * desc;
char * cname;
- //desc = get_descriptor(ctx, type);
- cname = get_class_full_name(ctx, type);
+ //desc = jvm_get_descriptor(type);
+ cname = jvm_get_class_full_name(type);
switch(type -> class)
{
@@ -745,224 +608,186 @@ jvm_generate_new_static(oberon_context_t * ctx, FILE * fp, oberon_type_t * type,
case OBERON_TYPE_PROCEDURE:
case OBERON_TYPE_REAL:
case OBERON_TYPE_POINTER:
- /* ниÑ\87его не генеÑ\80иÑ\80Ñ\83ем */
+ /* ниÑ\87его не надо делаÑ\82Ñ\8c пÑ\80и Ñ\81Ñ\82аÑ\82иÑ\87еÑ\81кой иниÑ\86иализаÑ\86ии */
break;
case OBERON_TYPE_RECORD:
- fprintf(fp, "new %s\n", cname);
- fprintf(fp, "dup\n");
- fprintf(fp, "invokespecial %s/<init>()V\n", cname);
+ jvm_generate(p, 0, 1, "new %s", cname);
+ jvm_generate(p, 1, 2, "dup");
+ jvm_generate(p, 1, 0, "invokespecial %s/<init>()V", cname);
break;
case OBERON_TYPE_ARRAY:
- oberon_error(ctx, "jvm_generate_new_static: TODO array");
+ gen_error("jvm_generate_new_static: TODO array");
break;
default:
- oberon_error(ctx, "jvm_generate_new_static: unk type class %i", type -> class);
+ gen_error("jvm_generate_new_static: unk type class %i", type -> class);
break;
}
}
static void
-jvm_generate_new_pointer(oberon_context_t * ctx, FILE * fp, oberon_type_t * type, int num, oberon_expr_t * arg)
+jvm_generate_new_pointer(gen_proc_t * p, oberon_type_t * type, int num, oberon_expr_t * arg)
{
assert(type -> class == OBERON_TYPE_POINTER);
- jvm_generate_new_static(ctx, fp, type -> base, num, arg);
+ jvm_generate_new_static(p, type -> base, num, arg);
}
static void
-push_item(oberon_context_t * ctx, FILE * fp, oberon_item_t * item)
+push_item(gen_proc_t * p, oberon_item_t * item)
{
switch(item -> mode)
{
case MODE_VAR:
- push_var(ctx, fp, item -> var);
+ jvm_push_var(p, item -> var);
break;
case MODE_INTEGER:
- push_int(fp, item -> integer);
+ jvm_generate_push_int(p, item -> integer);
break;
case MODE_BOOLEAN:
- push_int(fp, item -> boolean);
+ jvm_generate_push_int(p, item -> boolean);
break;
case MODE_CALL:
- oberon_generate_call_proc(ctx, (oberon_expr_t *) item);
+ jvm_generate_call_proc(p, (oberon_expr_t *) item);
break;
case MODE_INDEX:
- oberon_error(ctx, "push_item: TODO index");
+ gen_error("push_item: TODO index");
break;
case MODE_FIELD:
- push_item(ctx, fp, item -> parent);
- char * field = get_field_full_name(ctx, item -> var);
- char * desc = get_descriptor(ctx, item -> var -> type);
- fprintf(fp, "getfield %s %s\n", field, desc);
+ push_item(p, item -> parent);
+ char * field = jvm_get_field_full_name(item -> var);
+ char * desc = jvm_get_descriptor(item -> var -> type);
+ jvm_generate(p, 1, 1, "getfield %s %s", field, desc);
break;
case MODE_DEREF:
/* Все объекты представляются как увказатели */
- push_item(ctx, fp, item -> parent);
+ push_item(p, item -> parent);
break;
case MODE_NIL:
- fprintf(fp, "aconst_null\n");
+ jvm_generate(p, 0, 1, "aconst_null");
break;
case MODE_NEW:
- jvm_generate_new_pointer(ctx, fp, item -> result, item -> num_args, item -> args);
+ jvm_generate_new_pointer(p, item -> result, item -> num_args, item -> args);
break;
case MODE_REAL:
- push_float(fp, item -> real, item -> result -> size);
+ jvm_generate_push_float(p, item -> real, item -> result -> size);
break;
default:
- oberon_error(ctx, "push_item: unk mode %i", item -> mode);
+ gen_error("push_item: unk mode %i", item -> mode);
break;
}
}
-static int
-jvm_new_label_id(oberon_context_t * ctx)
-{
- gen_context_t * c = ctx -> gen_context;
- int label_id = c -> label_id;
- c -> label_id += 1;
- return label_id;
-}
-
-static char *
-jvm_get_label_name(int label_id)
-{
- return new_string("L%i", label_id);
-}
-
static void
-jvm_generate_label(FILE * fp, int label_id)
+jvm_generate_logical_not(gen_proc_t * p)
{
- fprintf(fp, "L%i:\n", label_id);
-}
-
-static void
-jvm_generate_logical_not(oberon_context_t * ctx, FILE * fp)
-{
- int label_true = jvm_new_label_id(ctx);
- int label_false = jvm_new_label_id(ctx);
- char * label_name_true = jvm_get_label_name(label_true);
+ int label_done = jvm_new_label_id(p);
+ int label_false = jvm_new_label_id(p);
+ char * label_name_done = jvm_get_label_name(label_done);
char * label_name_false = jvm_get_label_name(label_false);
- fprintf(fp, "ifne %s\n", label_name_false);
- fprintf(fp, "iconst_1\n");
- fprintf(fp, "goto %s\n", label_name_true);
- jvm_generate_label(fp, label_false);
- fprintf(fp, "iconst_0\n");
- jvm_generate_label(fp, label_true);
-}
-
-static char
-jvm_get_type_of_prefix(char prefix)
-{
- switch(prefix)
- {
- case 'b':
- return 'B';
- case 'c':
- return 'C';
- case 'd':
- return 'D';
- case 'f':
- return 'F';
- case 'i':
- return 'I';
- case 'l':
- return 'J';
- }
-
- assert(0);
+ jvm_generate(p, 1, 0, "ifne %s", label_name_false);
+ jvm_generate(p, 0, 1, "iconst_1");
+ jvm_generate(p, 0, 0, "goto %s", label_name_done);
+ jvm_generate_label(p, label_false);
+ jvm_generate(p, 0, 1, "iconst_0");
+ jvm_generate_label(p, label_done);
}
static void
-jvm_generate_abs(FILE * fp, char prefix)
+jvm_generate_abs(gen_proc_t * p, char prefix)
{
char t = jvm_get_type_of_prefix(prefix);
- fprintf(fp, "invokestatic java/lang/Math/abs(%c)%c\n", t, t);
+ int cell_size = jvm_cell_size_for_prefix(prefix);
+ jvm_generate(p, cell_size, cell_size, "invokestatic java/lang/Math/abs(%c)%c", t, t);
}
static void
-jvm_generate_compare_op(oberon_context_t * ctx, FILE *fp, char prefix, int op)
+jvm_generate_compare_op(gen_proc_t * p, char prefix, int op)
{
- int label_true = jvm_new_label_id(ctx);
- int label_false = jvm_new_label_id(ctx);
+ int label_true = jvm_new_label_id(p);
+ int label_done = jvm_new_label_id(p);
char * label_name_true = jvm_get_label_name(label_true);
- char * label_name_false = jvm_get_label_name(label_false);
+ char * label_name_done = jvm_get_label_name(label_done);
+ int cell_size = 2 * jvm_cell_size_for_prefix(prefix);
assert(prefix == 'i' || prefix == 'a');
+ const char * cmpop = "";
switch(op)
{
case OP_EQ:
- fprintf(fp, "if_%ccmpeq %s\n", prefix, label_name_true);
+ cmpop = "eq";
break;
case OP_NEQ:
- fprintf(fp, "if_%ccmpne %s\n", prefix, label_name_true);
+ cmpop = "ne";
break;
case OP_LSS:
- fprintf(fp, "if_icmplt %s\n", label_name_true);
+ cmpop = "lt";
break;
case OP_LEQ:
- fprintf(fp, "if_icmple %s\n", label_name_true);
+ cmpop = "le";
break;
case OP_GRT:
- fprintf(fp, "if_icmpgt %s\n", label_name_true);
+ cmpop = "gt";
break;
case OP_GEQ:
- fprintf(fp, "if_icmpge %s\n", label_name_true);
+ cmpop = "ge";
break;
default:
- oberon_error(ctx, "jvm_generate_compare_op: wat");
+ gen_error("jvm_generate_compare_op: wat");
break;
}
- fprintf(fp, "iconst_0\n");
- fprintf(fp, "goto %s\n", label_name_false);
- jvm_generate_label(fp, label_true);
- fprintf(fp, "iconst_1\n");
- jvm_generate_label(fp, label_false);
+ jvm_generate(p, cell_size, 0, "if_%ccmp%s %s", prefix, cmpop, label_name_true);
+ jvm_generate(p, 0, 1, "iconst_0");
+ jvm_generate(p, 0, 0, "goto %s", label_name_done);
+ jvm_generate_label(p, label_true);
+ jvm_generate(p, 0, 1, "iconst_1");
+ jvm_generate_label(p, label_done);
}
static void
-jvm_generate_operator(oberon_context_t * ctx, FILE *fp, char prefix, int op)
+jvm_generate_operator(gen_proc_t * p, char prefix, int op)
{
+ int cell_size = jvm_cell_size_for_prefix(prefix);
switch(op)
{
case OP_UNARY_MINUS:
- fprintf(fp, "%cneg\n", prefix);
+ jvm_generate(p, cell_size, cell_size, "%cneg", prefix);
break;
case OP_BITWISE_NOT:
- push_int(fp, -1);
- fprintf(fp, "%cxor\n", prefix);
+ jvm_generate_push_int(p, -1);
+ jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
break;
case OP_LOGIC_NOT:
- jvm_generate_logical_not(ctx, fp);
+ jvm_generate_logical_not(p);
break;
case OP_ABS:
- jvm_generate_abs(fp, prefix);
+ jvm_generate_abs(p, prefix);
break;
case OP_ADD:
- fprintf(fp, "%cadd\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%cadd", prefix);
break;
case OP_SUB:
- fprintf(fp, "%csub\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%csub", prefix);
break;
case OP_MUL:
- fprintf(fp, "%cmul\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%cmul", prefix);
break;
case OP_DIV:
- fprintf(fp, "%cdiv\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%cdiv", prefix);
break;
case OP_MOD:
- fprintf(fp, "%crem\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%crem", prefix);
break;
case OP_BITWISE_AND:
- fprintf(fp, "%cand\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%cand", prefix);
break;
case OP_BITWISE_XOR:
- fprintf(fp, "%cxor\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
break;
case OP_BITWISE_OR:
- fprintf(fp, "%cor\n", prefix);
+ jvm_generate(p, 2 * cell_size, cell_size, "%cor", prefix);
break;
case OP_EQ:
case OP_LEQ:
case OP_GRT:
case OP_GEQ:
- jvm_generate_compare_op(ctx, fp, prefix, op);
+ jvm_generate_compare_op(p, prefix, op);
break;
default:
- oberon_error(ctx, "jvm_generate_operator: unk op %i", op);
+ gen_error("jvm_generate_operator: unk op %i", op);
break;
}
}
static void
-jvm_generate_logical_or(oberon_context_t * ctx, FILE * fp, oberon_expr_t * p, oberon_expr_t * q)
+jvm_generate_logical_or(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
{
- int label_calc_q = jvm_new_label_id(ctx);
- int label_done = jvm_new_label_id(ctx);
- char * label_name_calc_q = jvm_get_label_name(label_calc_q);
+ int label_calc_b = jvm_new_label_id(p);
+ int label_done = jvm_new_label_id(p);
+ char * label_name_calc_b = jvm_get_label_name(label_calc_b);
char * label_name_done = jvm_get_label_name(label_done);
- /* p OR q -- если p, то TRUE, иначе q */
+ /* a OR b -- если a, то TRUE, иначе b */
- push_expr(ctx, fp, p);
- fprintf(fp, "ifne %s\n", label_name_calc_q);
- fprintf(fp, "iconst_1\n");
- fprintf(fp, "goto %s\n", label_name_done);
- jvm_generate_label(fp, label_calc_q);
- push_expr(ctx, fp, q);
- jvm_generate_label(fp, label_done);
+ push_expr(p, a);
+ jvm_generate(p, 1, 0, "ifne %s", label_name_calc_b);
+ jvm_generate(p, 0, 1, "iconst_1");
+ jvm_generate(p, 0, 0, "goto %s", label_name_done);
+ jvm_generate_label(p, label_calc_b);
+ push_expr(p, b);
+ jvm_generate_label(p, label_done);
}
static void
-jvm_generate_logical_and(oberon_context_t * ctx, FILE * fp, oberon_expr_t * p, oberon_expr_t * q)
+jvm_generate_logical_and(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
{
- int label_false = jvm_new_label_id(ctx);
- int label_done = jvm_new_label_id(ctx);
+ int label_false = jvm_new_label_id(p);
+ int label_done = jvm_new_label_id(p);
char * label_name_false = jvm_get_label_name(label_false);
char * label_name_done = jvm_get_label_name(label_done);
- /* p AND q -- если p, то q, иначе FALSE */
+ /* a AND b -- если a, то b, иначе FALSE */
- push_expr(ctx, fp, p);
- fprintf(fp, "ifne %s\n", label_name_false);
- push_expr(ctx, fp, q);
- fprintf(fp, "goto %s\n", label_name_done);
- jvm_generate_label(fp, label_false);
- fprintf(fp, "iconst_0\n");
- jvm_generate_label(fp, label_done);
+ push_expr(p, a);
+ jvm_generate(p, 1, 0, "ifne %s", label_name_false);
+ push_expr(p, b);
+ jvm_generate(p, 0, 0, "goto %s", label_name_done);
+ jvm_generate_label(p, label_false);
+ jvm_generate(p, 0, 1, "iconst_0");
+ jvm_generate_label(p, label_done);
}
static void
-push_operator(oberon_context_t * ctx, FILE * fp, oberon_oper_t * oper)
+push_operator(gen_proc_t * p, oberon_oper_t * oper)
{
- char prefix = get_prefix(ctx, oper -> result);
+ char prefix = jvm_get_prefix(oper -> result);
int op = oper -> op;
switch(op)
{
case OP_BITWISE_NOT:
case OP_LOGIC_NOT:
case OP_ABS:
- push_expr(ctx, fp, oper -> left);
- jvm_generate_operator(ctx, fp, prefix, op);
+ push_expr(p, oper -> left);
+ jvm_generate_operator(p, prefix, op);
break;
case OP_ADD:
case OP_LEQ:
case OP_GRT:
case OP_GEQ:
- push_expr(ctx, fp, oper -> left);
- push_expr(ctx, fp, oper -> right);
- jvm_generate_operator(ctx, fp, prefix, op);
+ push_expr(p, oper -> left);
+ push_expr(p, oper -> right);
+ jvm_generate_operator(p, prefix, op);
break;
case OP_LOGIC_OR:
- jvm_generate_logical_or(ctx, fp, oper -> left, oper -> right);
+ jvm_generate_logical_or(p, oper -> left, oper -> right);
break;
case OP_LOGIC_AND:
- jvm_generate_logical_and(ctx, fp, oper -> left, oper -> right);
+ jvm_generate_logical_and(p, oper -> left, oper -> right);
break;
default:
- oberon_error(ctx, "push_oper: unk op %i", op);
+ gen_error("push_oper: unk op %i", op);
break;
}
}
static void
-push_expr(oberon_context_t * ctx, FILE * fp, oberon_expr_t * expr)
+push_expr(gen_proc_t * p, oberon_expr_t * expr)
{
if(expr -> is_item)
{
- push_item(ctx, fp, (oberon_item_t *) expr);
+ push_item(p, (oberon_item_t *) expr);
}
else
{
- push_operator(ctx, fp, (oberon_oper_t *) expr);
+ push_operator(p, (oberon_oper_t *) expr);
}
}
static void
-store_expr(oberon_context_t * ctx, FILE * fp, oberon_expr_t * dst, oberon_expr_t * src)
+store_expr(gen_proc_t * p, oberon_expr_t * dst, oberon_expr_t * src)
{
assert(dst -> is_item);
oberon_item_t * item = (oberon_item_t *) dst;
+ int cell_size = jvm_cell_size_for_type(src -> result);
+
char prefix;
switch(item -> mode)
{
case MODE_VAR:
- push_expr(ctx, fp, src);
+ push_expr(p, src);
if(item -> var -> local)
{
int reg = item -> var -> gen_var -> reg;
- prefix = get_prefix(ctx, item -> result);
- fprintf(fp, "%cstore %i\n", prefix, reg);
+ prefix = jvm_get_prefix(item -> result);
+ jvm_generate(p, cell_size, 0, "%cstore %i", prefix, reg);
}
else
{
- char * fullname = get_field_full_name(ctx, item -> var);
- char * desc = get_descriptor(ctx, item -> result);
- fprintf(fp, "putstatic %s %s\n", fullname, desc);
+ char * fullname = jvm_get_field_full_name(item -> var);
+ char * desc = jvm_get_descriptor(item -> result);
+ jvm_generate(p, cell_size, 0, "putstatic %s %s", fullname, desc);
}
break;
case MODE_INDEX:
- oberon_error(ctx, "store_expr: TODO index");
+ gen_error("store_expr: TODO index");
break;
case MODE_FIELD:
{
- char * fullname = get_field_full_name(ctx, item -> var);
- char * desc = get_descriptor(ctx, item -> result);
- printf("push parent\n");
- push_item(ctx, fp, item -> parent);
- printf("push expr\n");
- push_expr(ctx, fp, src);
- printf("store to field %s:%s\n", fullname, desc);
- fprintf(fp, "putfield %s %s ; store field\n", fullname, desc);
+ char * fullname = jvm_get_field_full_name(item -> var);
+ char * desc = jvm_get_descriptor(item -> result);
+ push_item(p, item -> parent);
+ push_expr(p, src);
+ jvm_generate(p, 1 + cell_size, 0, "putfield %s %s", fullname, desc);
}
break;
default:
- oberon_error(ctx, "store_expr: unk mode %i", item -> mode);
+ gen_error("store_expr: unk mode %i", item -> mode);
break;
}
}
oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
{
gen_module_t * m;
+ gen_proc_t * p;
+
m = ctx -> mod -> gen_mod;
+ p = m -> class -> p;
- store_expr(ctx, m -> fp, dst, src);
+ store_expr(p, dst, src);
}
void
index 3ccc9f50ac3cbdfd6563aab5d0ef208320e90a27..d5abe38fe3fa39a99a0ce38bdd363ea7d3f91bae 100644 (file)
#define MAX_REGISTERS 256
-struct gen_register_file {
+struct gen_register_file
+{
struct
{
bool used;
} reg[MAX_REGISTERS];
int num_used;
int max_used;
- struct gen_register_file * up;
};
+struct gen_stack
+{
+ int pointer;
+ int max_pointer;
+};
+
+struct gen_class
+{
+ FILE * fp;
+ gen_proc_t * p;
+};
+
+
+
+
struct gen_proc_t
{
+ int label_id;
struct gen_register_file * rf;
+ struct gen_stack * stack;
+ struct gen_class * class;
};
struct gen_type_t
{
int rec_id;
- FILE * fp;
+ struct gen_class * class;
};
struct gen_var_t
struct gen_context_t
{
- gen_module_t * m;
- struct gen_register_file * rf;
- int label_id;
+ gen_module_t * current_m;
};
struct gen_module_t
{
+ struct gen_class * class;
int rec_id;
- FILE * fp;
};
diff --git a/src/generator.h b/src/generator.h
index 885b9faca57509a697088c697353a5808e3b7296..e988ba0c3f1c1e1bb5f8a8dd3a8dfb69d761a3f6 100644 (file)
--- a/src/generator.h
+++ b/src/generator.h
void oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * type);
void oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var);
void oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc);
-void oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod);
void oberon_generator_destroy_context(oberon_context_t * ctx);
+void oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod);
+void oberon_generator_fini_module(oberon_module_t * mod);
+
/*
* Функции генерации тела модуля
*/
diff --git a/src/oberon-internals.h b/src/oberon-internals.h
index d6c3881d2d54e2cb67f554371479a2c0386153fe..ac6b6959ff7799fb2d3a0ed35576fce40dd99ac8 100644 (file)
--- a/src/oberon-internals.h
+++ b/src/oberon-internals.h
oberon_oper_t oper;
};
-extern void
-oberon_error(oberon_context_t * ctx, const char * fmt, ...);
-
#endif // OBERON_INTERNALS_H
diff --git a/src/oberon.c b/src/oberon.c
index 09a6b8850c988e64986859aa3f2a73e3cd17d2ae..89f4be48a38e95db5eb56d4f1dd59f4569f7bb8c 100644 (file)
--- a/src/oberon.c
+++ b/src/oberon.c
// UTILS
// =======================================================================
-void
+static void
oberon_error(oberon_context_t * ctx, const char * fmt, ...)
{
va_list ptr;
{
oberon_error(ctx, "module name not matched");
}
+
+ oberon_generator_fini_module(ctx -> mod);
}
// =======================================================================
diff --git a/src/test.c b/src/test.c
index 8d2a44f52eaf937af51e28e19712cc92b0e1443a..bbf6b3938c810ed3babf5acb9c60f9a909b290ef 100644 (file)
--- a/src/test.c
+++ b/src/test.c
"MODULE Test;"
"TYPE"
" Rec = POINTER TO RecDesc;"
- " RecDesc = RECORD n, m, h : INTEGER END;"
+ " RecDesc = RECORD x, y, z : INTEGER END;"
"VAR"
+ " a : INTEGER;"
" r : Rec;"
- " i : INTEGER;"
- ""
- "BEGIN;"
+ "BEGIN"
" NEW(r);"
- " i := 666;"
- " r.n := r.m;"
+ " a := 123;"
+ " a := -(3 * a + 456);"
+ " r.x := a;"
+ " r.y := r.x;"
+ " r.z := r.y * 3;"
"END Test."
;