Skip to content

Commit

Permalink
DGen
Browse files Browse the repository at this point in the history
- When we find an entrypoint use `a` mode when opening the file for it, `w` mode would wipe the previously written C source code
- Also added a note regarding this branch (if-else) above
- Updated error message when no entry point (and not a test file) is found
- Fixed `emitStaticAllocations(File modOut, Module mod)` to select the static init queue using the provided `Module`
- Fixed `emitFunctionPrototypes(File modOut, Module mod)` and `emitFunctionDefinitions(File modOut, Module mod)` to use the inheritted `CodeEmitter` methods correctly by passing in a `Module`, similar changes affecting the `TypeChecker` are now conformed to in s similar manner
- Fixed `emitFunctionPrototype(File modOut, Module mod, string functionName)` and `emitFunctionDefinition(File modOut, Module mod, string functionName)` to select the function definition queue of the given `functionName` by the given `Module`
- Fixed `emitCodeQueue(File modOut, Module mod)` to select the globals queue by `Module`
- Added some comments about future features to `emitEntrypoint(File modOut, Module mod)`
  • Loading branch information
deavmi committed Feb 29, 2024
1 parent d491839 commit 9e54688
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions source/tlang/compiler/codegen/emit/dgen.d
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,9 @@ public final class DCodeEmitter : CodeEmitter
Function mainFunction;
if(findEntrypoint(mainModule, mainFunction))
{
// FIXME: Disable (needed "a", because "w" overwrote previous writes)
File entryModOut;
entryModOut.open(format("%s.c",mainModule.getName()), "w");
entryModOut.open(format("%s.c", mainModule.getName()), "a");

// Emit entry point
emitEntrypoint(entryModOut, mainModule);
Expand Down Expand Up @@ -883,7 +884,7 @@ public final class DCodeEmitter : CodeEmitter
}
else
{
gprintln("Could not find an entry point module and function", DebugType.ERROR);
gprintln("Could not find an entry point module and function. Missing a main() maybe?", DebugType.ERROR);
}
}
}
Expand Down Expand Up @@ -968,8 +969,9 @@ public final class DCodeEmitter : CodeEmitter
*/
private void emitStaticAllocations(File modOut, Module mod)
{
// FIXME: Select queue with module as well
selectQueue(QueueType.ALLOC_QUEUE);
// Select the static initializations code queue for
// the given module
selectQueue(mod, QueueType.ALLOC_QUEUE);
gprintln("Static allocations needed: "~to!(string)(getQueueLength()));

modOut.writeln();
Expand All @@ -984,12 +986,11 @@ public final class DCodeEmitter : CodeEmitter
*/
private void emitFunctionPrototypes(File modOut, Module mod)
{
// FIXME: Select queue with module as well
gprintln("Function definitions needed: "~to!(string)(getFunctionDefinitionsCount()));
gprintln("Function definitions needed: "~to!(string)(getFunctionDefinitionsCount(mod)));

Instruction[][string] functionBodyInstrs = typeChecker.getFunctionBodyCodeQueues();

string[] functionNames = getFunctionDefinitionNames();
// Get complete map (should we bypass anything in CodeEmitter for this? Guess it is fair?)
Instruction[][string] functionBodyInstrs = typeChecker.getFunctionBodyCodeQueues(mod);
string[] functionNames = getFunctionDefinitionNames(mod);

gprintln("WOAH: "~to!(string)(functionNames));

Expand All @@ -1009,12 +1010,12 @@ public final class DCodeEmitter : CodeEmitter
*/
private void emitFunctionDefinitions(File modOut, Module mod)
{
// FIXME: Select queue with module as well
gprintln("Function definitions needed: "~to!(string)(getFunctionDefinitionsCount()));
gprintln("Function definitions needed: "~to!(string)(getFunctionDefinitionsCount(mod)));

Instruction[][string] functionBodyInstrs = typeChecker.getFunctionBodyCodeQueues();
// Get the function definitions of the current module
Instruction[][string] functionBodyInstrs = typeChecker.getFunctionBodyCodeQueues(mod);

string[] functionNames = getFunctionDefinitionNames();
string[] functionNames = getFunctionDefinitionNames(mod);

gprintln("WOAH: "~to!(string)(functionNames));

Expand Down Expand Up @@ -1089,8 +1090,9 @@ public final class DCodeEmitter : CodeEmitter
*/
private void emitFunctionPrototype(File modOut, Module mod, string functionName)
{
// FIXME: Select queue with module as well
selectQueue(QueueType.FUNCTION_DEF_QUEUE, functionName);
// Select the function definition code queue by module and function name
// TODO: Is this needed for protptype def? I think not (REMOVE PLEASE)
selectQueue(mod, QueueType.FUNCTION_DEF_QUEUE, functionName);

gprintln("emotFunctionDefinition(): Function: "~functionName~", with "~to!(string)(getSelectedQueueLength())~" many instructions");

Expand All @@ -1113,8 +1115,8 @@ public final class DCodeEmitter : CodeEmitter
*/
private void emitFunctionDefinition(File modOut, Module mod, string functionName)
{
// FIXME: Select queue with module as well
selectQueue(QueueType.FUNCTION_DEF_QUEUE, functionName);
// Select the function definition code queue by module and function name
selectQueue(mod, QueueType.FUNCTION_DEF_QUEUE, functionName);

gprintln("emotFunctionDefinition(): Function: "~functionName~", with "~to!(string)(getSelectedQueueLength())~" many instructions");

Expand Down Expand Up @@ -1162,8 +1164,8 @@ public final class DCodeEmitter : CodeEmitter
*/
private void emitCodeQueue(File modOut, Module mod)
{
// FIXME: Select queue with module as well
selectQueue(QueueType.GLOBALS_QUEUE);
// Select the global code queue of the current module
selectQueue(mod, QueueType.GLOBALS_QUEUE);
gprintln("Code emittings needed: "~to!(string)(getQueueLength()));

while(hasInstructions())
Expand Down Expand Up @@ -1196,6 +1198,18 @@ public final class DCodeEmitter : CodeEmitter
gprintln("IMPLEMENT ME", DebugType.ERROR);
gprintln("IMPLEMENT ME", DebugType.ERROR);
gprintln("IMPLEMENT ME", DebugType.ERROR);
gprintln("We have NOT YET implemented the init method", DebugType.ERROR);

// modOut.writeln("fok");

// TODO: In future, for runtime init,
// I will want to co-opt main(int, args)
// for use for runtime init to then
// call ANOTHER REAL main (specified)
// by the user

// Therefore there must be some sort
// of renaming stage somewhere
}

private void emitTestingEntrypoint(File modOut, Module mod)
Expand Down

0 comments on commit 9e54688

Please sign in to comment.