tlang/source/tlang/compiler/codegen/emit/core.d

105 lines
2.2 KiB
D
Raw Normal View History

2021-11-02 08:41:03 +00:00
module compiler.codegen.emit.core;
2021-06-01 14:18:09 +01:00
import compiler.symbols.data;
2021-11-02 08:41:03 +00:00
import compiler.typecheck.core;
import std.container.slist : SList;
import compiler.codegen.instruction;
import std.stdio;
import std.file;
import compiler.codegen.instruction : Instruction;
import std.range : walkLength;
2021-06-01 14:18:09 +01:00
/**
* TODO: Perhaps have an interface that can emit(Context/Parent, Statement)
*/
/* TODO: Module linking (general overhaul required) */
2021-11-02 08:41:03 +00:00
public abstract class CodeEmitter
2021-06-01 14:18:09 +01:00
{
protected TypeChecker typeChecker;
2021-11-02 08:41:03 +00:00
/**
* Required queues
*/
private Instruction[] initQueue;
private Instruction[] codeQueue;
// alias instructions = codeQueue;
protected File file;
private ulong codeQueueIdx = 0;
public final Instruction getCurrentCodeInstruction()
{
return codeQueue[codeQueueIdx];
}
public final bool hasCodeInstructions()
{
return codeQueueIdx < codeQueue.length;
}
public final void nextCodeInstruction()
{
codeQueueIdx++;
}
public final void previousCodeInstruction()
{
codeQueueIdx--;
}
public final ulong getInitQueueLen()
{
return initQueue.length;
}
public final ulong getCodeQueueLen()
{
return codeQueue.length;
}
this(TypeChecker typeChecker, File file)
2021-06-01 14:18:09 +01:00
{
2021-11-02 08:41:03 +00:00
this.typeChecker = typeChecker;
/* Extract the allocation queue, the code queue */
foreach(Instruction currentInstruction; typeChecker.getInitQueue())
{
initQueue~=currentInstruction;
}
foreach(Instruction currentInstruction; typeChecker.getCodeQueue())
{
codeQueue~=currentInstruction;
}
this.file = file;
2021-06-01 14:18:09 +01:00
}
2021-06-01 14:24:13 +01:00
/**
* Begins the emit process
*/
2021-11-02 08:41:03 +00:00
public abstract void emit();
/**
* Finalizes the emitting process (only
* to be called after the `emit()` finishes)
*/
public abstract void finalize();
/**
* Transforms or emits a single Instruction
* and returns the transformation
*
* Params:
* instruction = The Instruction to transform/emit
* Returns: The Instruction emit as a string
*/
public abstract string transform(Instruction instruction);
2021-06-01 14:18:09 +01:00
}