summaryrefslogtreecommitdiff
path: root/libguile/programs.c
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2009-01-29 21:09:04 +0100
committerAndy Wingo <wingo@pobox.com>2009-01-29 21:12:00 +0100
commit53e28ed9b28c530cbc5fb64206639c7bab66850a (patch)
treeade1f13bd7dfae10e6a67e1331885650f32a856d /libguile/programs.c
parentf1d7723bb3ab0417bcfaf04647461fb0487c8cd4 (diff)
downloadguile-53e28ed9b28c530cbc5fb64206639c7bab66850a.tar.gz
static opcodes; refactor program/objcode division; use new assembly pipeline
* gdbinit: Untested attempts to get the stack fondling macros to deal with the new program representation. * libguile/frames.c (scm_vm_frame_arguments, scm_vm_frame_source) (scm_vm_frame_local_ref, scm_vm_frame_local_set_x): SCM_PROGRAM_DATA is a struct scm_objcode*. * libguile/instructions.h: * libguile/instructions.c: Hide the instruction table and the struct scm_instruction structure; all access to instructions now goes through procedures. This is because instructions are no longer in a packed array indexed by opcode. Also, declare a mask that all instructions should fit in. * libguile/objcodes.h: * libguile/objcodes.c: Rewrite so that object code directly maps its arity and length from its bytecode. This makes it unnecessary to keep this information in programs, allowing programs to be simple conses between the code (objcodes) and data (the object table and the closure variables). * libguile/programs.c (scm_make_program): Rework so that make-program takes objcode, an object table, and externals as arguments. It's much clearer this way, and we avoid malloc(). * libguile/stacks.c (is_vm_bootstrap_frame): Update for program/objcode changes. * libguile/vm-engine.c (vm_run): Initialize the jump table on the first run, with the opcodes declared in the instruction sources, and with bad instructions raising an error instead of wandering off into the Unknown. * libguile/vm-engine.h (FETCH_LENGTH): Always represent lengths as 3 bytes. The old code was too error-prone. (NEXT_JUMP): Mask the instruction with SCM_VM_INSTRUCTION_MASK. (NEW_FRAME): Update for program/objcode changes. * libguile/vm-expand.h (VM_DEFINE_FUNCTION, VM_DEFINE_INSTRUCTION) (VM_DEFINE_LOADER): Update so that we explicitly specify opcodes, so that we have a stable bytecode API. * libguile/vm-i-loader.c: Update license to LGPLv2+. Explicitly declare opcodes. (load-integer): Use an int instead of a long as the accumulator; still need to revisit this code at some point, I think. (load-program): Simplify, thankfully!! Just creates the objcode slice and rolls with it. * libguile/vm-i-scheme.c: Number the opcodes explicitly. * libguile/vm-i-system.c: Update license to LGPLv2+. Explicitly declare opcodes. (make-closure): Update for new program API. * libguile/vm.c (vm_make_boot_program): Update for new program/objcode API. Still a bit ugly. (scm_load_compiled_with_vm): Update for new program/objcode API. * module/language/assembly.scm (byte-length): Fix byte-length calculation for loaders, and load-program. (code-pack, code-unpack): Start to move things from (system vm conv) here. (object->code, code->object): More things from conv.scm. * module/language/glil.scm (<glil-program>): Add a new field, closure-level. (make-glil-program, compute-closure-level): Calculate the "closure level" when making a glil program. This is the maximum depth of external binding refs in this closure. (unparse-glil): Fix label serialization. * module/language/glil/compile-assembly.scm (make-meta): Prepend #f for the meta's object table, though maybe in the future we can avoid creating assembly in the first place. (assoc-ref-or-acons, object-index-and-alist): GRRR! Caught again by the different sets of arguments to assoc and assoc-ref! (glil->assembly): Attempt to make the <glil-program> case more readable, and fix the bugs. Sorry I don't know how to comment this change any more than this. (glil->assembly): For <glil-module> serialize the whole key, not just the name. (dump-object): subprogram-code is already a list. Serialize integers as strings, not u8vectors. Fix the order of lists and vectors. * module/language/glil/spec.scm (glil): Switch orders, so we prefer glil -> assembly -> objcode. Actually glil->objcode doesn't work any more, needs to be removed I think. * module/language/objcode/spec.scm (objcode->value): s/objcode->program/make-program/. * module/language/scheme/inline.scm: Add acons inline. * module/system/vm/conv.scm (make-byte-decoder): Skip the first 8 bytes, they are header. Handle subprograms properly. Still needs help though. (decode-length): Lengths are always 3 bytes now. * module/system/vm/disasm.scm: Superficial changes to keep things working. I'd like to fix this better in the future. * module/system/vm/frame.scm (bootstrap-frame?): Fixes for program-bytecode. * module/system/vm/program.scm: Export make-program. It's program-objcode now, no more program-bytecode. * module/system/vm/vm.scm (vm-load): Use make-program. * test-suite/tests/asm-to-bytecode.test: New test, very minimal. * module/system/vm/objcode.scm: Export word-size, byte-order, and write-objcode.
Diffstat (limited to 'libguile/programs.c')
-rw-r--r--libguile/programs.c151
1 files changed, 50 insertions, 101 deletions
diff --git a/libguile/programs.c b/libguile/programs.c
index f5b5d42ba..e9c093a2e 100644
--- a/libguile/programs.c
+++ b/libguile/programs.c
@@ -53,71 +53,35 @@
scm_t_bits scm_tc16_program;
-static SCM zero_vector;
static SCM write_program = SCM_BOOL_F;
-SCM
-scm_c_make_program (void *addr, size_t size, SCM objs, SCM holder)
-#define FUNC_NAME "scm_c_make_program"
+SCM_DEFINE (scm_make_program, "make-program", 1, 2, 0,
+ (SCM objcode, SCM objtable, SCM external),
+ "")
+#define FUNC_NAME s_scm_make_program
{
- struct scm_program *p = scm_gc_malloc (sizeof (struct scm_program),
- "program");
- p->size = size;
- p->nargs = 0;
- p->nrest = 0;
- p->nlocs = 0;
- p->nexts = 0;
- p->objs = objs;
- p->external = SCM_EOL;
- p->holder = holder;
-
- /* If nobody holds bytecode's address, then allocate a new memory */
- if (SCM_FALSEP (holder))
- {
- p->base = scm_gc_malloc (size, "program-base");
- memcpy (p->base, addr, size);
- }
+ SCM_VALIDATE_OBJCODE (1, objcode);
+ if (SCM_UNLIKELY (SCM_UNBNDP (objtable)))
+ objtable = SCM_BOOL_F;
+ else if (scm_is_true (objtable))
+ SCM_VALIDATE_VECTOR (2, objtable);
+ if (SCM_UNLIKELY (SCM_UNBNDP (external)))
+ external = SCM_EOL;
else
- p->base = addr;
+ SCM_VALIDATE_LIST (3, external);
- SCM_RETURN_NEWSMOB (scm_tc16_program, p);
+ SCM_RETURN_NEWSMOB3 (scm_tc16_program, objcode, objtable, external);
}
#undef FUNC_NAME
-SCM
-scm_c_make_closure (SCM program, SCM external)
-{
- struct scm_program *p = scm_gc_malloc (sizeof (struct scm_program),
- "program");
- *p = *SCM_PROGRAM_DATA (program);
- p->holder = program;
- p->external = external;
- SCM_RETURN_NEWSMOB (scm_tc16_program, p);
-}
-
static SCM
program_mark (SCM obj)
{
- struct scm_program *p = SCM_PROGRAM_DATA (obj);
- if (scm_is_true (p->objs))
- scm_gc_mark (p->objs);
- if (!scm_is_null (p->external))
- scm_gc_mark (p->external);
- return p->holder;
-}
-
-static scm_sizet
-program_free (SCM obj)
-{
- struct scm_program *p = SCM_PROGRAM_DATA (obj);
- scm_sizet size = (sizeof (struct scm_program));
-
- if (SCM_FALSEP (p->holder))
- scm_gc_free (p->base, p->size, "program-base");
-
- scm_gc_free (p, size, "program");
-
- return 0;
+ if (scm_is_true (SCM_PROGRAM_OBJTABLE (obj)))
+ scm_gc_mark (SCM_PROGRAM_OBJTABLE (obj));
+ if (!scm_is_null (SCM_PROGRAM_EXTERNALS (obj)))
+ scm_gc_mark (SCM_PROGRAM_EXTERNALS (obj));
+ return SCM_PROGRAM_OBJCODE (obj);
}
static SCM
@@ -175,7 +139,7 @@ SCM_DEFINE (scm_program_arity, "program-arity", 1, 0, 0,
"")
#define FUNC_NAME s_scm_program_arity
{
- struct scm_program *p;
+ struct scm_objcode *p;
SCM_VALIDATE_PROGRAM (1, program);
@@ -187,6 +151,28 @@ SCM_DEFINE (scm_program_arity, "program-arity", 1, 0, 0,
}
#undef FUNC_NAME
+SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
+ (SCM program),
+ "")
+#define FUNC_NAME s_scm_program_objects
+{
+ SCM_VALIDATE_PROGRAM (1, program);
+ return SCM_PROGRAM_OBJTABLE (program);
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
+ (SCM program),
+ "")
+#define FUNC_NAME s_scm_program_module
+{
+ SCM objs;
+ SCM_VALIDATE_PROGRAM (1, program);
+ objs = SCM_PROGRAM_OBJTABLE (program);
+ return scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F;
+}
+#undef FUNC_NAME
+
SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
(SCM program),
"")
@@ -194,19 +180,17 @@ SCM_DEFINE (scm_program_meta, "program-meta", 1, 0, 0,
{
SCM objs;
SCM_VALIDATE_PROGRAM (1, program);
- objs = SCM_PROGRAM_DATA (program)->objs;
+ objs = SCM_PROGRAM_OBJTABLE (program);
return scm_is_true (objs) ? scm_c_vector_ref (objs, 1) : SCM_BOOL_F;
}
#undef FUNC_NAME
extern SCM
-scm_c_program_source (struct scm_program *p, size_t ip)
+scm_c_program_source (SCM program, size_t ip)
{
SCM meta, sources, source;
- if (scm_is_false (p->objs))
- return SCM_BOOL_F;
- meta = scm_c_vector_ref (p->objs, 1);
+ meta = scm_program_meta (program);
if (scm_is_false (meta))
return SCM_BOOL_F;
meta = scm_call_0 (meta);
@@ -220,35 +204,13 @@ scm_c_program_source (struct scm_program *p, size_t ip)
return scm_cdr (source); /* a #(line column file) vector */
}
-SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0,
- (SCM program),
- "")
-#define FUNC_NAME s_scm_program_objects
-{
- SCM_VALIDATE_PROGRAM (1, program);
- return SCM_PROGRAM_DATA (program)->objs;
-}
-#undef FUNC_NAME
-
-SCM_DEFINE (scm_program_module, "program-module", 1, 0, 0,
- (SCM program),
- "")
-#define FUNC_NAME s_scm_program_module
-{
- SCM objs;
- SCM_VALIDATE_PROGRAM (1, program);
- objs = SCM_PROGRAM_DATA (program)->objs;
- return scm_is_true (objs) ? scm_c_vector_ref (objs, 0) : SCM_BOOL_F;
-}
-#undef FUNC_NAME
-
SCM_DEFINE (scm_program_external, "program-external", 1, 0, 0,
(SCM program),
"")
#define FUNC_NAME s_scm_program_external
{
SCM_VALIDATE_PROGRAM (1, program);
- return SCM_PROGRAM_DATA (program)->external;
+ return SCM_PROGRAM_EXTERNALS (program);
}
#undef FUNC_NAME
@@ -260,29 +222,19 @@ SCM_DEFINE (scm_program_external_set_x, "program-external-set!", 2, 0, 0,
{
SCM_VALIDATE_PROGRAM (1, program);
SCM_VALIDATE_LIST (2, external);
- SCM_PROGRAM_DATA (program)->external = external;
+ SCM_PROGRAM_EXTERNALS (program) = external;
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
-SCM_DEFINE (scm_program_bytecode, "program-bytecode", 1, 0, 0,
+SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0,
(SCM program),
- "Return a u8vector containing @var{program}'s bytecode.")
-#define FUNC_NAME s_scm_program_bytecode
+ "Return a @var{program}'s object code.")
+#define FUNC_NAME s_scm_program_objcode
{
- size_t size;
- scm_t_uint8 *c_bytecode;
-
SCM_VALIDATE_PROGRAM (1, program);
- size = SCM_PROGRAM_DATA (program)->size;
- c_bytecode = malloc (size);
- if (!c_bytecode)
- return SCM_BOOL_F;
-
- memcpy (c_bytecode, SCM_PROGRAM_DATA (program)->base, size);
-
- return scm_take_u8vector (c_bytecode, size);
+ return SCM_PROGRAM_OBJCODE (program);
}
#undef FUNC_NAME
@@ -291,11 +243,8 @@ SCM_DEFINE (scm_program_bytecode, "program-bytecode", 1, 0, 0,
void
scm_bootstrap_programs (void)
{
- zero_vector = scm_permanent_object (scm_c_make_vector (0, SCM_BOOL_F));
-
scm_tc16_program = scm_make_smob_type ("program", 0);
scm_set_smob_mark (scm_tc16_program, program_mark);
- scm_set_smob_free (scm_tc16_program, program_free);
scm_set_smob_apply (scm_tc16_program, program_apply, 0, 0, 1);
scm_set_smob_print (scm_tc16_program, program_print);
}