@@ -271,6 +271,18 @@ static const char *s_output_code_filename = NULL;
271271
272272// ////////
273273
274+ struct CompiledModule
275+ {
276+ CompiledModule *next;
277+ NameRef name;
278+ byte_t *bytecode;
279+ size_t bytecode_len;
280+ };
281+
282+ static CompiledModule *s_compiled_modules = NULL ;
283+
284+ // ////////
285+
274286struct EmittedModule
275287{
276288 EmittedModule *next;
@@ -633,8 +645,46 @@ static void __EmitModuleOrder(NameRef p_name)
633645 s_ordered_modules[s_ordered_module_count++] = p_name;
634646}
635647
648+ static bool
649+ EmitCompiledModules (void )
650+ {
651+ const char *t_filename = nil;
652+ FILE *t_file = OpenOutputBytecodeFile (&t_filename);
653+
654+ if (nil == t_file)
655+ goto error_cleanup;
656+
657+ while (s_compiled_modules != nullptr )
658+ {
659+ size_t t_written;
660+ t_written = fwrite (s_compiled_modules->bytecode , sizeof (byte_t ), s_compiled_modules->bytecode_len , t_file);
661+
662+ if (t_written != s_compiled_modules->bytecode_len )
663+ goto error_cleanup;
664+
665+ s_compiled_modules = s_compiled_modules->next ;
666+ }
667+
668+ fflush (t_file);
669+ fclose (t_file);
670+
671+ return true ;
672+
673+ error_cleanup:
674+ if (nil != t_file)
675+ fclose (t_file);
676+ Error_CouldNotWriteOutputFile (t_filename);
677+ return false ;
678+ }
679+
636680void EmitFinish (void )
637681{
682+ if (s_compiled_modules != NULL &&
683+ !EmitCompiledModules ())
684+ {
685+ goto error_cleanup;
686+ }
687+
638688 if (!EmitEmittedBuiltins ())
639689 {
640690 goto error_cleanup;
@@ -699,7 +749,7 @@ void EmitBeginLibraryModule(NameRef p_name, intptr_t& r_index)
699749}
700750
701751static bool
702- EmitEndModuleGetByteCodeBuffer (MCAutoByteArray & r_bytecode)
752+ EmitEndModuleGetByteCodeBuffer (byte_t * & r_bytecode, size_t & r_bytecode_len )
703753{
704754 MCAutoValueRefBase<MCStreamRef> t_stream;
705755 MCMemoryOutputStreamCreate (&t_stream);
@@ -714,7 +764,8 @@ EmitEndModuleGetByteCodeBuffer (MCAutoByteArray & r_bytecode)
714764 t_bytecode_len);
715765
716766 MCAssert (t_bytecode_len <= UINDEX_MAX);
717- r_bytecode.Give ((byte_t *) t_bytecode, (uindex_t )t_bytecode_len);
767+ r_bytecode = (byte_t *)t_bytecode;
768+ r_bytecode_len = t_bytecode_len;
718769
719770 return true ;
720771
@@ -723,34 +774,6 @@ EmitEndModuleGetByteCodeBuffer (MCAutoByteArray & r_bytecode)
723774 return false ;
724775}
725776
726- static bool
727- EmitEndModuleOutputBytecode (const byte_t *p_bytecode,
728- size_t p_bytecode_len)
729- {
730- const char *t_filename = nil;
731- FILE *t_file = OpenOutputBytecodeFile (&t_filename);
732-
733- if (nil == t_file)
734- goto error_cleanup;
735-
736- size_t t_written;
737- t_written = fwrite (p_bytecode, sizeof (byte_t ), p_bytecode_len, t_file);
738-
739- if (t_written != p_bytecode_len)
740- goto error_cleanup;
741-
742- fflush (t_file);
743- fclose (t_file);
744-
745- return true ;
746-
747- error_cleanup:
748- if (nil != t_file)
749- fclose (t_file);
750- Error_CouldNotWriteOutputFile (t_filename);
751- return false ;
752- }
753-
754777static bool
755778EmitEndModuleOutputC (NameRef p_module_name,
756779 const char *p_module_name_string,
@@ -888,8 +911,7 @@ EmitEndModule (void)
888911{
889912 const char *t_module_string = nil;
890913
891- MCAutoByteArray t_bytecode;
892- const byte_t *t_bytecode_buf = nil;
914+ byte_t *t_bytecode_buf = nil;
893915 size_t t_bytecode_len = 0 ;
894916
895917 MCAutoByteArray t_interface;
@@ -905,12 +927,9 @@ EmitEndModule (void)
905927
906928 /* ---------- 1. Get bytecode */
907929
908- if (!EmitEndModuleGetByteCodeBuffer (t_bytecode ))
930+ if (!EmitEndModuleGetByteCodeBuffer (t_bytecode_buf, t_bytecode_len ))
909931 goto cleanup;
910932
911- t_bytecode_buf = t_bytecode.Bytes ();
912- t_bytecode_len = t_bytecode.ByteCount ();
913-
914933 /* ---------- 2. Output module contents */
915934 if (OutputFileAsC)
916935 {
@@ -920,9 +939,13 @@ EmitEndModule (void)
920939 }
921940 else if (OutputFileAsBytecode)
922941 {
923- if (!EmitEndModuleOutputBytecode (t_bytecode_buf, t_bytecode_len))
924- goto cleanup;
925- }
942+ CompiledModule *t_cmodule = (CompiledModule*)Allocate (sizeof (CompiledModule));
943+ t_cmodule->next = s_compiled_modules;
944+ t_cmodule->name = s_module_name;
945+ t_cmodule->bytecode = t_bytecode_buf;
946+ t_cmodule->bytecode_len = t_bytecode_len;
947+ s_compiled_modules = t_cmodule;
948+ }
926949
927950 /* ---------- 3. Output module interface */
928951 if (!EmitEndModuleGetInterfaceBuffer (t_bytecode_buf, t_bytecode_len,
0 commit comments