Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit b8819cd

Browse files
committed
[[ Toolchain ]] Assembly Support for lc-compile
This patch allows multiple LCB files to be passed to lc-compile. In this case, the LCB files must be in reverse dependency order (i.e. referenced files followed by referencing file) and all of the LCB files will be compiled into a single assembly with main (first module) being the last specified LCB file.
1 parent 71ea9a2 commit b8819cd

File tree

2 files changed

+63
-46
lines changed

2 files changed

+63
-46
lines changed

toolchain/lc-compile/src/emit.cpp

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
274286
struct 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+
636680
void 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

701751
static 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-
754777
static bool
755778
EmitEndModuleOutputC (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,

toolchain/lc-compile/src/main.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void
9696
usage(int status)
9797
{
9898
fprintf(stderr,
99-
"Usage: lc-compile [OPTION ...] --output OUTFILE [--] LCBFILE\n"
99+
"Usage: lc-compile [OPTION ...] --output OUTFILE [--] LCBFILE ... LCBFILE\n"
100100
" lc-compile [OPTION ...] --outputc OUTFILE [--] LCBFILE ... LCBFILE\n"
101101
" lc-compile [OPTION ...] --deps DEPTYPE [--] LCBFILE ... LCBFILE\n"
102102
"\n"
@@ -262,12 +262,6 @@ static void full_main(int argc, char *argv[])
262262
}
263263
else
264264
{
265-
if (have_input_file == 1)
266-
{
267-
fprintf(stderr, "WARNING: Ignoring multiple input filenames.\n");
268-
continue;
269-
}
270-
271265
AddFile(opt);
272266
have_input_file = 1;
273267
}

0 commit comments

Comments
 (0)