From 9e54688fcdfd935211217017f49764eef8c3e94a Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 29 Feb 2024 13:38:17 +0200 Subject: [PATCH] DGen - 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)` --- source/tlang/compiler/codegen/emit/dgen.d | 52 ++++++++++++++--------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/source/tlang/compiler/codegen/emit/dgen.d b/source/tlang/compiler/codegen/emit/dgen.d index 2070cfed..6c3c3ae9 100644 --- a/source/tlang/compiler/codegen/emit/dgen.d +++ b/source/tlang/compiler/codegen/emit/dgen.d @@ -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); @@ -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); } } } @@ -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(); @@ -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)); @@ -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)); @@ -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"); @@ -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"); @@ -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()) @@ -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)